]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/unbound/iterator/iter_delegpt.h
Upgrade Unbound to 1.6.8. More to follow.
[FreeBSD/FreeBSD.git] / contrib / unbound / iterator / iter_delegpt.h
1 /*
2  * iterator/iter_delegpt.h - delegation point with NS and address information.
3  *
4  * Copyright (c) 2007, NLnet Labs. All rights reserved.
5  *
6  * This software is open source.
7  * 
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 
12  * Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  * 
15  * Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  * 
19  * Neither the name of the NLNET LABS nor the names of its contributors may
20  * be used to endorse or promote products derived from this software without
21  * specific prior written permission.
22  * 
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35
36 /**
37  * \file
38  *
39  * This file implements the Delegation Point. It contains a list of name servers
40  * and their addresses if known.
41  */
42
43 #ifndef ITERATOR_ITER_DELEGPT_H
44 #define ITERATOR_ITER_DELEGPT_H
45 #include "util/log.h"
46 struct regional;
47 struct delegpt_ns;
48 struct delegpt_addr;
49 struct dns_msg;
50 struct ub_packed_rrset_key;
51 struct msgreply_entry;
52
53 /**
54  * Delegation Point.
55  * For a domain name, the NS rrset, and the A and AAAA records for those.
56  */
57 struct delegpt {
58         /** the domain name of the delegation point. */
59         uint8_t* name;
60         /** length of the delegation point name */
61         size_t namelen;
62         /** number of labels in delegation point */
63         int namelabs;
64
65         /** the nameservers, names from the NS RRset rdata. */
66         struct delegpt_ns* nslist;
67         /** the target addresses for delegation */
68         struct delegpt_addr* target_list;
69         /** the list of usable targets; subset of target_list 
70          * the items in this list are not part of the result list.  */
71         struct delegpt_addr* usable_list;
72         /** the list of returned targets; subset of target_list */
73         struct delegpt_addr* result_list;
74
75         /** if true, the NS RRset was bogus. All info is bad. */
76         int bogus;
77         /** if true, the parent-side NS record has been applied:
78          * its names have been added and their addresses can follow later.
79          * Also true if the delegationpoint was created from a delegation
80          * message and thus contains the parent-side-info already. */
81         uint8_t has_parent_side_NS;
82         /** for assertions on type of delegpt */
83         uint8_t dp_type_mlc;
84         /** use SSL for upstream query */
85         uint8_t ssl_upstream;
86 };
87
88 /**
89  * Nameservers for a delegation point.
90  */
91 struct delegpt_ns {
92         /** next in list */
93         struct delegpt_ns* next;
94         /** name of nameserver */
95         uint8_t* name;
96         /** length of name */
97         size_t namelen;
98         /** 
99          * If the name has been resolved. false if not queried for yet.
100          * true if the A, AAAA queries have been generated.
101          * marked true if those queries fail.
102          * and marked true if got4 and got6 are both true.
103          */
104         int resolved;
105         /** if the ipv4 address is in the delegpt */
106         uint8_t got4;
107         /** if the ipv6 address is in the delegpt */
108         uint8_t got6;
109         /**
110          * If the name is parent-side only and thus dispreferred.
111          * Its addresses become dispreferred as well
112          */
113         uint8_t lame;
114         /** if the parent-side ipv4 address has been looked up (last resort).
115          * Also enabled if a parent-side cache entry exists, or a parent-side
116          * negative-cache entry exists. */
117         uint8_t done_pside4;
118         /** if the parent-side ipv6 address has been looked up (last resort).
119          * Also enabled if a parent-side cache entry exists, or a parent-side
120          * negative-cache entry exists. */
121         uint8_t done_pside6;
122 };
123
124 /**
125  * Address of target nameserver in delegation point.
126  */
127 struct delegpt_addr {
128         /** next delegation point in results */
129         struct delegpt_addr* next_result;
130         /** next delegation point in usable list */
131         struct delegpt_addr* next_usable;
132         /** next delegation point in all targets list */
133         struct delegpt_addr* next_target;
134
135         /** delegation point address */
136         struct sockaddr_storage addr;
137         /** length of addr */
138         socklen_t addrlen;
139         /** number of attempts for this addr */
140         int attempts;
141         /** rtt stored here in the selection algorithm */
142         int sel_rtt;
143         /** if true, the A or AAAA RR was bogus, so this address is bad.
144          * Also check the dp->bogus to see if everything is bogus. */
145         uint8_t bogus;
146         /** if true, this address is dispreferred: it is a lame IP address */
147         uint8_t lame;
148         /** if the address is dnsseclame, but this cannot be cached, this
149          * option is useful to mark the address dnsseclame.
150          * This value is not copied in addr-copy and dp-copy. */
151         uint8_t dnsseclame;
152 };
153
154 /**
155  * Create new delegation point.
156  * @param regional: where to allocate it.
157  * @return new delegation point or NULL on error.
158  */
159 struct delegpt* delegpt_create(struct regional* regional);
160
161 /**
162  * Create a copy of a delegation point.
163  * @param dp: delegation point to copy.
164  * @param regional: where to allocate it.
165  * @return new delegation point or NULL on error.
166  */
167 struct delegpt* delegpt_copy(struct delegpt* dp, struct regional* regional);
168
169 /**
170  * Set name of delegation point.
171  * @param dp: delegation point.
172  * @param regional: where to allocate the name copy.
173  * @param name: name to use.
174  * @return false on error.
175  */
176 int delegpt_set_name(struct delegpt* dp, struct regional* regional, 
177         uint8_t* name);
178
179 /**
180  * Add a name to the delegation point.
181  * @param dp: delegation point.
182  * @param regional: where to allocate the info.
183  * @param name: domain name in wire format.
184  * @param lame: name is lame, disprefer it.
185  * @return false on error.
186  */
187 int delegpt_add_ns(struct delegpt* dp, struct regional* regional, 
188         uint8_t* name, uint8_t lame);
189
190 /**
191  * Add NS rrset; calls add_ns repeatedly.
192  * @param dp: delegation point.
193  * @param regional: where to allocate the info.
194  * @param ns_rrset: NS rrset.
195  * @param lame: rrset is lame, disprefer it.
196  * @return 0 on alloc error.
197  */
198 int delegpt_rrset_add_ns(struct delegpt* dp, struct regional* regional,
199         struct ub_packed_rrset_key* ns_rrset, uint8_t lame);
200
201 /**
202  * Add target address to the delegation point.
203  * @param dp: delegation point.
204  * @param regional: where to allocate the info.
205  * @param name: name for which target was found (must be in nslist).
206  *      This name is marked resolved.
207  * @param namelen: length of name.
208  * @param addr: the address.
209  * @param addrlen: the length of addr.
210  * @param bogus: security status for the address, pass true if bogus.
211  * @param lame: address is lame.
212  * @return false on error.
213  */
214 int delegpt_add_target(struct delegpt* dp, struct regional* regional, 
215         uint8_t* name, size_t namelen, struct sockaddr_storage* addr, 
216         socklen_t addrlen, uint8_t bogus, uint8_t lame);
217
218 /**
219  * Add A RRset to delegpt.
220  * @param dp: delegation point.
221  * @param regional: where to allocate the info.
222  * @param rrset: RRset A to add.
223  * @param lame: rrset is lame, disprefer it.
224  * @return 0 on alloc error.
225  */
226 int delegpt_add_rrset_A(struct delegpt* dp, struct regional* regional, 
227         struct ub_packed_rrset_key* rrset, uint8_t lame);
228
229 /**
230  * Add AAAA RRset to delegpt.
231  * @param dp: delegation point.
232  * @param regional: where to allocate the info.
233  * @param rrset: RRset AAAA to add.
234  * @param lame: rrset is lame, disprefer it.
235  * @return 0 on alloc error.
236  */
237 int delegpt_add_rrset_AAAA(struct delegpt* dp, struct regional* regional, 
238         struct ub_packed_rrset_key* rrset, uint8_t lame);
239
240 /**
241  * Add any RRset to delegpt.
242  * Does not check for duplicates added.
243  * @param dp: delegation point.
244  * @param regional: where to allocate the info.
245  * @param rrset: RRset to add, NS, A, AAAA.
246  * @param lame: rrset is lame, disprefer it.
247  * @return 0 on alloc error.
248  */
249 int delegpt_add_rrset(struct delegpt* dp, struct regional* regional, 
250         struct ub_packed_rrset_key* rrset, uint8_t lame);
251
252 /**
253  * Add address to the delegation point. No servername is associated or checked.
254  * @param dp: delegation point.
255  * @param regional: where to allocate the info.
256  * @param addr: the address.
257  * @param addrlen: the length of addr.
258  * @param bogus: if address is bogus.
259  * @param lame: if address is lame.
260  * @return false on error.
261  */
262 int delegpt_add_addr(struct delegpt* dp, struct regional* regional, 
263         struct sockaddr_storage* addr, socklen_t addrlen,
264         uint8_t bogus, uint8_t lame);
265
266 /** 
267  * Find NS record in name list of delegation point.
268  * @param dp: delegation point.
269  * @param name: name of nameserver to look for, uncompressed wireformat.
270  * @param namelen: length of name.
271  * @return the ns structure or NULL if not found.
272  */
273 struct delegpt_ns* delegpt_find_ns(struct delegpt* dp, uint8_t* name, 
274         size_t namelen);
275
276 /** 
277  * Find address record in total list of delegation point.
278  * @param dp: delegation point.
279  * @param addr: address
280  * @param addrlen: length of addr
281  * @return the addr structure or NULL if not found.
282  */
283 struct delegpt_addr* delegpt_find_addr(struct delegpt* dp, 
284         struct sockaddr_storage* addr, socklen_t addrlen);
285
286 /**
287  * Print the delegation point to the log. For debugging.
288  * @param v: verbosity value that is needed to emit to log.
289  * @param dp: delegation point.
290  */
291 void delegpt_log(enum verbosity_value v, struct delegpt* dp);
292
293 /** count NS and number missing for logging */
294 void delegpt_count_ns(struct delegpt* dp, size_t* numns, size_t* missing);
295
296 /** count addresses, and number in result and available lists, for logging */
297 void delegpt_count_addr(struct delegpt* dp, size_t* numaddr, size_t* numres, 
298         size_t* numavail);
299
300 /**
301  * Add all usable targets to the result list.
302  * @param dp: delegation point.
303  */
304 void delegpt_add_unused_targets(struct delegpt* dp);
305
306 /**
307  * Count number of missing targets. These are ns names with no resolved flag.
308  * @param dp: delegation point.
309  * @return number of missing targets (or 0).
310  */
311 size_t delegpt_count_missing_targets(struct delegpt* dp);
312
313 /** count total number of targets in dp */
314 size_t delegpt_count_targets(struct delegpt* dp);
315
316 /**
317  * Create new delegation point from a dns message
318  *
319  * Note that this method does not actually test to see if the message is an
320  * actual referral. It really is just checking to see if it can construct a
321  * delegation point, so the message could be of some other type (some ANSWER
322  * messages, some CNAME messages, generally.) Note that the resulting
323  * DelegationPoint will contain targets for all "relevant" glue (i.e.,
324  * address records whose ownernames match the target of one of the NS
325  * records), so if policy dictates that some glue should be discarded beyond
326  * that, discard it before calling this method. Note that this method will
327  * find "glue" in either the ADDITIONAL section or the ANSWER section.
328  *
329  * @param msg: the dns message, referral.
330  * @param regional: where to allocate delegation point.
331  * @return new delegation point or NULL on alloc error, or if the
332  *         message was not appropriate.
333  */
334 struct delegpt* delegpt_from_message(struct dns_msg* msg, 
335         struct regional* regional);
336
337 /**
338  * Add negative message to delegation point.
339  * @param dp: delegation point.
340  * @param msg: the message added, marks off A or AAAA from an NS entry.
341  */
342 void delegpt_add_neg_msg(struct delegpt* dp, struct msgreply_entry* msg);
343
344 /**
345  * Register the fact that there is no ipv6 and thus AAAAs are not going 
346  * to be queried for or be useful.
347  * @param dp: the delegation point. Updated to reflect no ipv6.
348  */
349 void delegpt_no_ipv6(struct delegpt* dp);
350
351 /**
352  * Register the fact that there is no ipv4 and thus As are not going 
353  * to be queried for or be useful.
354  * @param dp: the delegation point. Updated to reflect no ipv4.
355  */
356 void delegpt_no_ipv4(struct delegpt* dp);
357
358 /** 
359  * create malloced delegation point, with the given name 
360  * @param name: uncompressed wireformat of delegpt name.
361  * @return NULL on alloc failure
362  */
363 struct delegpt* delegpt_create_mlc(uint8_t* name);
364
365 /** 
366  * free malloced delegation point.
367  * @param dp: must have been created with delegpt_create_mlc, free'd. 
368  */
369 void delegpt_free_mlc(struct delegpt* dp);
370
371 /**
372  * Set name of delegation point.
373  * @param dp: delegation point. malloced.
374  * @param name: name to use.
375  * @return false on error.
376  */
377 int delegpt_set_name_mlc(struct delegpt* dp, uint8_t* name);
378
379 /**
380  * add a name to malloced delegation point.
381  * @param dp: must have been created with delegpt_create_mlc. 
382  * @param name: the name to add.
383  * @param lame: the name is lame, disprefer.
384  * @return false on error.
385  */
386 int delegpt_add_ns_mlc(struct delegpt* dp, uint8_t* name, uint8_t lame);
387
388 /**
389  * add an address to a malloced delegation point.
390  * @param dp: must have been created with delegpt_create_mlc. 
391  * @param addr: the address.
392  * @param addrlen: the length of addr.
393  * @param bogus: if address is bogus.
394  * @param lame: if address is lame.
395  * @return false on error.
396  */
397 int delegpt_add_addr_mlc(struct delegpt* dp, struct sockaddr_storage* addr,
398         socklen_t addrlen, uint8_t bogus, uint8_t lame);
399
400 /**
401  * Add target address to the delegation point.
402  * @param dp: must have been created with delegpt_create_mlc. 
403  * @param name: name for which target was found (must be in nslist).
404  *      This name is marked resolved.
405  * @param namelen: length of name.
406  * @param addr: the address.
407  * @param addrlen: the length of addr.
408  * @param bogus: security status for the address, pass true if bogus.
409  * @param lame: address is lame.
410  * @return false on error.
411  */
412 int delegpt_add_target_mlc(struct delegpt* dp, uint8_t* name, size_t namelen,
413         struct sockaddr_storage* addr, socklen_t addrlen, uint8_t bogus,
414         uint8_t lame);
415
416 /** get memory in use by dp */
417 size_t delegpt_get_mem(struct delegpt* dp);
418
419 #endif /* ITERATOR_ITER_DELEGPT_H */