1238106Sdes/*
2238106Sdes * iterator/iter_delegpt.h - delegation point with NS and address information.
3238106Sdes *
4238106Sdes * Copyright (c) 2007, NLnet Labs. All rights reserved.
5238106Sdes *
6238106Sdes * This software is open source.
7238106Sdes *
8238106Sdes * Redistribution and use in source and binary forms, with or without
9238106Sdes * modification, are permitted provided that the following conditions
10238106Sdes * are met:
11238106Sdes *
12238106Sdes * Redistributions of source code must retain the above copyright notice,
13238106Sdes * this list of conditions and the following disclaimer.
14238106Sdes *
15238106Sdes * Redistributions in binary form must reproduce the above copyright notice,
16238106Sdes * this list of conditions and the following disclaimer in the documentation
17238106Sdes * and/or other materials provided with the distribution.
18238106Sdes *
19238106Sdes * Neither the name of the NLNET LABS nor the names of its contributors may
20238106Sdes * be used to endorse or promote products derived from this software without
21238106Sdes * specific prior written permission.
22238106Sdes *
23238106Sdes * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24269257Sdes * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25269257Sdes * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26269257Sdes * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27269257Sdes * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28269257Sdes * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29269257Sdes * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30269257Sdes * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31269257Sdes * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32269257Sdes * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33269257Sdes * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34238106Sdes */
35238106Sdes
36238106Sdes/**
37238106Sdes * \file
38238106Sdes *
39238106Sdes * This file implements the Delegation Point. It contains a list of name servers
40238106Sdes * and their addresses if known.
41238106Sdes */
42238106Sdes
43238106Sdes#ifndef ITERATOR_ITER_DELEGPT_H
44238106Sdes#define ITERATOR_ITER_DELEGPT_H
45238106Sdes#include "util/log.h"
46238106Sdesstruct regional;
47238106Sdesstruct delegpt_ns;
48238106Sdesstruct delegpt_addr;
49238106Sdesstruct dns_msg;
50238106Sdesstruct ub_packed_rrset_key;
51238106Sdesstruct msgreply_entry;
52238106Sdes
53238106Sdes/**
54238106Sdes * Delegation Point.
55238106Sdes * For a domain name, the NS rrset, and the A and AAAA records for those.
56238106Sdes */
57238106Sdesstruct delegpt {
58238106Sdes	/** the domain name of the delegation point. */
59238106Sdes	uint8_t* name;
60238106Sdes	/** length of the delegation point name */
61238106Sdes	size_t namelen;
62238106Sdes	/** number of labels in delegation point */
63238106Sdes	int namelabs;
64238106Sdes
65238106Sdes	/** the nameservers, names from the NS RRset rdata. */
66238106Sdes	struct delegpt_ns* nslist;
67238106Sdes	/** the target addresses for delegation */
68238106Sdes	struct delegpt_addr* target_list;
69238106Sdes	/** the list of usable targets; subset of target_list
70238106Sdes	 * the items in this list are not part of the result list.  */
71238106Sdes	struct delegpt_addr* usable_list;
72238106Sdes	/** the list of returned targets; subset of target_list */
73238106Sdes	struct delegpt_addr* result_list;
74238106Sdes
75238106Sdes	/** if true, the NS RRset was bogus. All info is bad. */
76238106Sdes	int bogus;
77238106Sdes	/** if true, the parent-side NS record has been applied:
78238106Sdes	 * its names have been added and their addresses can follow later.
79238106Sdes	 * Also true if the delegationpoint was created from a delegation
80238106Sdes	 * message and thus contains the parent-side-info already. */
81238106Sdes	uint8_t has_parent_side_NS;
82238106Sdes	/** for assertions on type of delegpt */
83238106Sdes	uint8_t dp_type_mlc;
84238106Sdes};
85238106Sdes
86238106Sdes/**
87238106Sdes * Nameservers for a delegation point.
88238106Sdes */
89238106Sdesstruct delegpt_ns {
90238106Sdes	/** next in list */
91238106Sdes	struct delegpt_ns* next;
92238106Sdes	/** name of nameserver */
93238106Sdes	uint8_t* name;
94238106Sdes	/** length of name */
95238106Sdes	size_t namelen;
96238106Sdes	/**
97238106Sdes	 * If the name has been resolved. false if not queried for yet.
98238106Sdes	 * true if the A, AAAA queries have been generated.
99238106Sdes	 * marked true if those queries fail.
100238106Sdes	 * and marked true if got4 and got6 are both true.
101238106Sdes	 */
102238106Sdes	int resolved;
103238106Sdes	/** if the ipv4 address is in the delegpt */
104238106Sdes	uint8_t got4;
105238106Sdes	/** if the ipv6 address is in the delegpt */
106238106Sdes	uint8_t got6;
107238106Sdes	/**
108238106Sdes	 * If the name is parent-side only and thus dispreferred.
109238106Sdes	 * Its addresses become dispreferred as well
110238106Sdes	 */
111238106Sdes	uint8_t lame;
112238106Sdes	/** if the parent-side ipv4 address has been looked up (last resort).
113238106Sdes	 * Also enabled if a parent-side cache entry exists, or a parent-side
114238106Sdes	 * negative-cache entry exists. */
115238106Sdes	uint8_t done_pside4;
116238106Sdes	/** if the parent-side ipv6 address has been looked up (last resort).
117238106Sdes	 * Also enabled if a parent-side cache entry exists, or a parent-side
118238106Sdes	 * negative-cache entry exists. */
119238106Sdes	uint8_t done_pside6;
120238106Sdes};
121238106Sdes
122238106Sdes/**
123238106Sdes * Address of target nameserver in delegation point.
124238106Sdes */
125238106Sdesstruct delegpt_addr {
126238106Sdes	/** next delegation point in results */
127238106Sdes	struct delegpt_addr* next_result;
128238106Sdes	/** next delegation point in usable list */
129238106Sdes	struct delegpt_addr* next_usable;
130238106Sdes	/** next delegation point in all targets list */
131238106Sdes	struct delegpt_addr* next_target;
132238106Sdes
133238106Sdes	/** delegation point address */
134238106Sdes	struct sockaddr_storage addr;
135238106Sdes	/** length of addr */
136238106Sdes	socklen_t addrlen;
137238106Sdes	/** number of attempts for this addr */
138238106Sdes	int attempts;
139238106Sdes	/** rtt stored here in the selection algorithm */
140238106Sdes	int sel_rtt;
141238106Sdes	/** if true, the A or AAAA RR was bogus, so this address is bad.
142238106Sdes	 * Also check the dp->bogus to see if everything is bogus. */
143269257Sdes	uint8_t bogus;
144238106Sdes	/** if true, this address is dispreferred: it is a lame IP address */
145269257Sdes	uint8_t lame;
146269257Sdes	/** if the address is dnsseclame, but this cannot be cached, this
147269257Sdes	 * option is useful to mark the address dnsseclame.
148269257Sdes	 * This value is not copied in addr-copy and dp-copy. */
149269257Sdes	uint8_t dnsseclame;
150238106Sdes};
151238106Sdes
152238106Sdes/**
153238106Sdes * Create new delegation point.
154238106Sdes * @param regional: where to allocate it.
155238106Sdes * @return new delegation point or NULL on error.
156238106Sdes */
157238106Sdesstruct delegpt* delegpt_create(struct regional* regional);
158238106Sdes
159238106Sdes/**
160238106Sdes * Create a copy of a delegation point.
161238106Sdes * @param dp: delegation point to copy.
162238106Sdes * @param regional: where to allocate it.
163238106Sdes * @return new delegation point or NULL on error.
164238106Sdes */
165238106Sdesstruct delegpt* delegpt_copy(struct delegpt* dp, struct regional* regional);
166238106Sdes
167238106Sdes/**
168238106Sdes * Set name of delegation point.
169238106Sdes * @param dp: delegation point.
170238106Sdes * @param regional: where to allocate the name copy.
171238106Sdes * @param name: name to use.
172238106Sdes * @return false on error.
173238106Sdes */
174238106Sdesint delegpt_set_name(struct delegpt* dp, struct regional* regional,
175238106Sdes	uint8_t* name);
176238106Sdes
177238106Sdes/**
178238106Sdes * Add a name to the delegation point.
179238106Sdes * @param dp: delegation point.
180238106Sdes * @param regional: where to allocate the info.
181238106Sdes * @param name: domain name in wire format.
182238106Sdes * @param lame: name is lame, disprefer it.
183238106Sdes * @return false on error.
184238106Sdes */
185238106Sdesint delegpt_add_ns(struct delegpt* dp, struct regional* regional,
186269257Sdes	uint8_t* name, uint8_t lame);
187238106Sdes
188238106Sdes/**
189238106Sdes * Add NS rrset; calls add_ns repeatedly.
190238106Sdes * @param dp: delegation point.
191238106Sdes * @param regional: where to allocate the info.
192238106Sdes * @param ns_rrset: NS rrset.
193238106Sdes * @param lame: rrset is lame, disprefer it.
194238106Sdes * @return 0 on alloc error.
195238106Sdes */
196238106Sdesint delegpt_rrset_add_ns(struct delegpt* dp, struct regional* regional,
197269257Sdes	struct ub_packed_rrset_key* ns_rrset, uint8_t lame);
198238106Sdes
199238106Sdes/**
200238106Sdes * Add target address to the delegation point.
201238106Sdes * @param dp: delegation point.
202238106Sdes * @param regional: where to allocate the info.
203238106Sdes * @param name: name for which target was found (must be in nslist).
204238106Sdes *	This name is marked resolved.
205238106Sdes * @param namelen: length of name.
206238106Sdes * @param addr: the address.
207238106Sdes * @param addrlen: the length of addr.
208238106Sdes * @param bogus: security status for the address, pass true if bogus.
209238106Sdes * @param lame: address is lame.
210238106Sdes * @return false on error.
211238106Sdes */
212238106Sdesint delegpt_add_target(struct delegpt* dp, struct regional* regional,
213238106Sdes	uint8_t* name, size_t namelen, struct sockaddr_storage* addr,
214269257Sdes	socklen_t addrlen, uint8_t bogus, uint8_t lame);
215238106Sdes
216238106Sdes/**
217238106Sdes * Add A RRset to delegpt.
218238106Sdes * @param dp: delegation point.
219238106Sdes * @param regional: where to allocate the info.
220238106Sdes * @param rrset: RRset A to add.
221238106Sdes * @param lame: rrset is lame, disprefer it.
222238106Sdes * @return 0 on alloc error.
223238106Sdes */
224238106Sdesint delegpt_add_rrset_A(struct delegpt* dp, struct regional* regional,
225269257Sdes	struct ub_packed_rrset_key* rrset, uint8_t lame);
226238106Sdes
227238106Sdes/**
228238106Sdes * Add AAAA RRset to delegpt.
229238106Sdes * @param dp: delegation point.
230238106Sdes * @param regional: where to allocate the info.
231238106Sdes * @param rrset: RRset AAAA to add.
232238106Sdes * @param lame: rrset is lame, disprefer it.
233238106Sdes * @return 0 on alloc error.
234238106Sdes */
235238106Sdesint delegpt_add_rrset_AAAA(struct delegpt* dp, struct regional* regional,
236269257Sdes	struct ub_packed_rrset_key* rrset, uint8_t lame);
237238106Sdes
238238106Sdes/**
239238106Sdes * Add any RRset to delegpt.
240238106Sdes * Does not check for duplicates added.
241238106Sdes * @param dp: delegation point.
242238106Sdes * @param regional: where to allocate the info.
243238106Sdes * @param rrset: RRset to add, NS, A, AAAA.
244238106Sdes * @param lame: rrset is lame, disprefer it.
245238106Sdes * @return 0 on alloc error.
246238106Sdes */
247238106Sdesint delegpt_add_rrset(struct delegpt* dp, struct regional* regional,
248269257Sdes	struct ub_packed_rrset_key* rrset, uint8_t lame);
249238106Sdes
250238106Sdes/**
251238106Sdes * Add address to the delegation point. No servername is associated or checked.
252238106Sdes * @param dp: delegation point.
253238106Sdes * @param regional: where to allocate the info.
254238106Sdes * @param addr: the address.
255238106Sdes * @param addrlen: the length of addr.
256238106Sdes * @param bogus: if address is bogus.
257238106Sdes * @param lame: if address is lame.
258238106Sdes * @return false on error.
259238106Sdes */
260238106Sdesint delegpt_add_addr(struct delegpt* dp, struct regional* regional,
261269257Sdes	struct sockaddr_storage* addr, socklen_t addrlen,
262269257Sdes	uint8_t bogus, uint8_t lame);
263238106Sdes
264238106Sdes/**
265238106Sdes * Find NS record in name list of delegation point.
266238106Sdes * @param dp: delegation point.
267238106Sdes * @param name: name of nameserver to look for, uncompressed wireformat.
268238106Sdes * @param namelen: length of name.
269238106Sdes * @return the ns structure or NULL if not found.
270238106Sdes */
271238106Sdesstruct delegpt_ns* delegpt_find_ns(struct delegpt* dp, uint8_t* name,
272238106Sdes	size_t namelen);
273238106Sdes
274238106Sdes/**
275238106Sdes * Find address record in total list of delegation point.
276238106Sdes * @param dp: delegation point.
277238106Sdes * @param addr: address
278238106Sdes * @param addrlen: length of addr
279238106Sdes * @return the addr structure or NULL if not found.
280238106Sdes */
281238106Sdesstruct delegpt_addr* delegpt_find_addr(struct delegpt* dp,
282238106Sdes	struct sockaddr_storage* addr, socklen_t addrlen);
283238106Sdes
284238106Sdes/**
285238106Sdes * Print the delegation point to the log. For debugging.
286238106Sdes * @param v: verbosity value that is needed to emit to log.
287238106Sdes * @param dp: delegation point.
288238106Sdes */
289238106Sdesvoid delegpt_log(enum verbosity_value v, struct delegpt* dp);
290238106Sdes
291238106Sdes/** count NS and number missing for logging */
292238106Sdesvoid delegpt_count_ns(struct delegpt* dp, size_t* numns, size_t* missing);
293238106Sdes
294238106Sdes/** count addresses, and number in result and available lists, for logging */
295238106Sdesvoid delegpt_count_addr(struct delegpt* dp, size_t* numaddr, size_t* numres,
296238106Sdes	size_t* numavail);
297238106Sdes
298238106Sdes/**
299238106Sdes * Add all usable targets to the result list.
300238106Sdes * @param dp: delegation point.
301238106Sdes */
302238106Sdesvoid delegpt_add_unused_targets(struct delegpt* dp);
303238106Sdes
304238106Sdes/**
305238106Sdes * Count number of missing targets. These are ns names with no resolved flag.
306238106Sdes * @param dp: delegation point.
307238106Sdes * @return number of missing targets (or 0).
308238106Sdes */
309238106Sdessize_t delegpt_count_missing_targets(struct delegpt* dp);
310238106Sdes
311238106Sdes/** count total number of targets in dp */
312238106Sdessize_t delegpt_count_targets(struct delegpt* dp);
313238106Sdes
314238106Sdes/**
315238106Sdes * Create new delegation point from a dns message
316238106Sdes *
317238106Sdes * Note that this method does not actually test to see if the message is an
318238106Sdes * actual referral. It really is just checking to see if it can construct a
319238106Sdes * delegation point, so the message could be of some other type (some ANSWER
320238106Sdes * messages, some CNAME messages, generally.) Note that the resulting
321238106Sdes * DelegationPoint will contain targets for all "relevant" glue (i.e.,
322238106Sdes * address records whose ownernames match the target of one of the NS
323238106Sdes * records), so if policy dictates that some glue should be discarded beyond
324238106Sdes * that, discard it before calling this method. Note that this method will
325238106Sdes * find "glue" in either the ADDITIONAL section or the ANSWER section.
326238106Sdes *
327238106Sdes * @param msg: the dns message, referral.
328238106Sdes * @param regional: where to allocate delegation point.
329238106Sdes * @return new delegation point or NULL on alloc error, or if the
330238106Sdes *         message was not appropriate.
331238106Sdes */
332238106Sdesstruct delegpt* delegpt_from_message(struct dns_msg* msg,
333238106Sdes	struct regional* regional);
334238106Sdes
335238106Sdes/**
336238106Sdes * Add negative message to delegation point.
337238106Sdes * @param dp: delegation point.
338238106Sdes * @param msg: the message added, marks off A or AAAA from an NS entry.
339238106Sdes */
340238106Sdesvoid delegpt_add_neg_msg(struct delegpt* dp, struct msgreply_entry* msg);
341238106Sdes
342238106Sdes/**
343238106Sdes * Register the fact that there is no ipv6 and thus AAAAs are not going
344238106Sdes * to be queried for or be useful.
345238106Sdes * @param dp: the delegation point. Updated to reflect no ipv6.
346238106Sdes */
347238106Sdesvoid delegpt_no_ipv6(struct delegpt* dp);
348238106Sdes
349238106Sdes/**
350238106Sdes * Register the fact that there is no ipv4 and thus As are not going
351238106Sdes * to be queried for or be useful.
352238106Sdes * @param dp: the delegation point. Updated to reflect no ipv4.
353238106Sdes */
354238106Sdesvoid delegpt_no_ipv4(struct delegpt* dp);
355238106Sdes
356238106Sdes/**
357238106Sdes * create malloced delegation point, with the given name
358238106Sdes * @param name: uncompressed wireformat of degegpt name.
359238106Sdes * @return NULL on alloc failure
360238106Sdes */
361238106Sdesstruct delegpt* delegpt_create_mlc(uint8_t* name);
362238106Sdes
363238106Sdes/**
364238106Sdes * free malloced delegation point.
365238106Sdes * @param dp: must have been created with delegpt_create_mlc, free'd.
366238106Sdes */
367238106Sdesvoid delegpt_free_mlc(struct delegpt* dp);
368238106Sdes
369238106Sdes/**
370238106Sdes * Set name of delegation point.
371238106Sdes * @param dp: delegation point. malloced.
372238106Sdes * @param name: name to use.
373238106Sdes * @return false on error.
374238106Sdes */
375238106Sdesint delegpt_set_name_mlc(struct delegpt* dp, uint8_t* name);
376238106Sdes
377238106Sdes/**
378238106Sdes * add a name to malloced delegation point.
379238106Sdes * @param dp: must have been created with delegpt_create_mlc.
380238106Sdes * @param name: the name to add.
381238106Sdes * @param lame: the name is lame, disprefer.
382238106Sdes * @return false on error.
383238106Sdes */
384269257Sdesint delegpt_add_ns_mlc(struct delegpt* dp, uint8_t* name, uint8_t lame);
385238106Sdes
386238106Sdes/**
387238106Sdes * add an address to a malloced delegation point.
388238106Sdes * @param dp: must have been created with delegpt_create_mlc.
389238106Sdes * @param addr: the address.
390238106Sdes * @param addrlen: the length of addr.
391238106Sdes * @param bogus: if address is bogus.
392238106Sdes * @param lame: if address is lame.
393238106Sdes * @return false on error.
394238106Sdes */
395238106Sdesint delegpt_add_addr_mlc(struct delegpt* dp, struct sockaddr_storage* addr,
396269257Sdes	socklen_t addrlen, uint8_t bogus, uint8_t lame);
397238106Sdes
398238106Sdes/**
399238106Sdes * Add target address to the delegation point.
400238106Sdes * @param dp: must have been created with delegpt_create_mlc.
401238106Sdes * @param name: name for which target was found (must be in nslist).
402238106Sdes *	This name is marked resolved.
403238106Sdes * @param namelen: length of name.
404238106Sdes * @param addr: the address.
405238106Sdes * @param addrlen: the length of addr.
406238106Sdes * @param bogus: security status for the address, pass true if bogus.
407238106Sdes * @param lame: address is lame.
408238106Sdes * @return false on error.
409238106Sdes */
410238106Sdesint delegpt_add_target_mlc(struct delegpt* dp, uint8_t* name, size_t namelen,
411269257Sdes	struct sockaddr_storage* addr, socklen_t addrlen, uint8_t bogus,
412269257Sdes	uint8_t lame);
413238106Sdes
414238106Sdes/** get memory in use by dp */
415238106Sdessize_t delegpt_get_mem(struct delegpt* dp);
416238106Sdes
417238106Sdes#endif /* ITERATOR_ITER_DELEGPT_H */
418