1/*	$NetBSD: adb.h,v 1.2.6.1 2012/06/05 21:14:57 bouyer Exp $	*/
2
3/*
4 * Copyright (C) 2004-2008, 2011  Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 1999-2003  Internet Software Consortium.
6 *
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
18 */
19
20/* Id: adb.h,v 1.88 2011/12/05 17:10:51 each Exp  */
21
22#ifndef DNS_ADB_H
23#define DNS_ADB_H 1
24
25/*****
26 ***** Module Info
27 *****/
28
29/*! \file dns/adb.h
30 *\brief
31 * DNS Address Database
32 *
33 * This module implements an address database (ADB) for mapping a name
34 * to an isc_sockaddr_t. It also provides statistical information on
35 * how good that address might be.
36 *
37 * A client will pass in a dns_name_t, and the ADB will walk through
38 * the rdataset looking up addresses associated with the name.  If it
39 * is found on the internal lists, a structure is filled in with the
40 * address information and stats for found addresses.
41 *
42 * If the name cannot be found on the internal lists, a new entry will
43 * be created for a name if all the information needed can be found
44 * in the zone table or cache.  This new address will then be returned.
45 *
46 * If a request must be made to remote servers to satisfy a name lookup,
47 * this module will start fetches to try to complete these addresses.  When
48 * at least one more completes, an event is sent to the caller.  If none of
49 * them resolve before the fetch times out, an event indicating this is
50 * sent instead.
51 *
52 * Records are stored internally until a timer expires. The timer is the
53 * smaller of the TTL or signature validity period.
54 *
55 * Lameness is stored per <qname,qtype> tuple, and this data hangs off each
56 * address field.  When an address is marked lame for a given tuple the address
57 * will not be returned to a caller.
58 *
59 *
60 * MP:
61 *
62 *\li	The ADB takes care of all necessary locking.
63 *
64 *\li	Only the task which initiated the name lookup can cancel the lookup.
65 *
66 *
67 * Security:
68 *
69 *\li	None, since all data stored is required to be pre-filtered.
70 *	(Cache needs to be sane, fetches return bounds-checked and sanity-
71 *       checked data, caller passes a good dns_name_t for the zone, etc)
72 */
73
74/***
75 *** Imports
76 ***/
77
78#include <isc/lang.h>
79#include <isc/magic.h>
80#include <isc/mem.h>
81#include <isc/sockaddr.h>
82
83#include <dns/types.h>
84#include <dns/view.h>
85
86ISC_LANG_BEGINDECLS
87
88/***
89 *** Magic number checks
90 ***/
91
92#define DNS_ADBFIND_MAGIC	  ISC_MAGIC('a','d','b','H')
93#define DNS_ADBFIND_VALID(x)	  ISC_MAGIC_VALID(x, DNS_ADBFIND_MAGIC)
94#define DNS_ADBADDRINFO_MAGIC	  ISC_MAGIC('a','d','A','I')
95#define DNS_ADBADDRINFO_VALID(x)  ISC_MAGIC_VALID(x, DNS_ADBADDRINFO_MAGIC)
96
97
98/***
99 *** TYPES
100 ***/
101
102typedef struct dns_adbname		dns_adbname_t;
103
104/*!
105 *\brief
106 * Represents a lookup for a single name.
107 *
108 * On return, the client can safely use "list", and can reorder the list.
109 * Items may not be _deleted_ from this list, however, or added to it
110 * other than by using the dns_adb_*() API.
111 */
112struct dns_adbfind {
113	/* Public */
114	unsigned int			magic;		/*%< RO: magic */
115	dns_adbaddrinfolist_t		list;		/*%< RO: list of addrs */
116	unsigned int			query_pending;	/*%< RO: partial list */
117	unsigned int			partial_result;	/*%< RO: addrs missing */
118	unsigned int			options;	/*%< RO: options */
119	isc_result_t			result_v4;	/*%< RO: v4 result */
120	isc_result_t			result_v6;	/*%< RO: v6 result */
121	ISC_LINK(dns_adbfind_t)		publink;	/*%< RW: client use */
122
123	/* Private */
124	isc_mutex_t			lock;		/* locks all below */
125	in_port_t			port;
126	int				name_bucket;
127	unsigned int			flags;
128	dns_adbname_t		       *adbname;
129	dns_adb_t		       *adb;
130	isc_event_t			event;
131	ISC_LINK(dns_adbfind_t)		plink;
132};
133
134/*
135 * _INET:
136 * _INET6:
137 *	return addresses of that type.
138 *
139 * _EMPTYEVENT:
140 *	Only schedule an event if no addresses are known.
141 *	Must set _WANTEVENT for this to be meaningful.
142 *
143 * _WANTEVENT:
144 *	An event is desired.  Check this bit in the returned find to see
145 *	if one will actually be generated.
146 *
147 * _AVOIDFETCHES:
148 *	If set, fetches will not be generated unless no addresses are
149 *	available in any of the address families requested.
150 *
151 * _STARTATZONE:
152 *	Fetches will start using the closest zone data or use the root servers.
153 *	This is useful for reestablishing glue that has expired.
154 *
155 * _GLUEOK:
156 * _HINTOK:
157 *	Glue or hints are ok.  These are used when matching names already
158 *	in the adb, and when dns databases are searched.
159 *
160 * _RETURNLAME:
161 *	Return lame servers in a find, so that all addresses are returned.
162 *
163 * _LAMEPRUNED:
164 *	At least one address was omitted from the list because it was lame.
165 *	This bit will NEVER be set if _RETURNLAME is set in the createfind().
166 */
167/*% Return addresses of type INET. */
168#define DNS_ADBFIND_INET		0x00000001
169/*% Return addresses of type INET6. */
170#define DNS_ADBFIND_INET6		0x00000002
171#define DNS_ADBFIND_ADDRESSMASK		0x00000003
172/*%
173 *      Only schedule an event if no addresses are known.
174 *      Must set _WANTEVENT for this to be meaningful.
175 */
176#define DNS_ADBFIND_EMPTYEVENT		0x00000004
177/*%
178 *	An event is desired.  Check this bit in the returned find to see
179 *	if one will actually be generated.
180 */
181#define DNS_ADBFIND_WANTEVENT		0x00000008
182/*%
183 *	If set, fetches will not be generated unless no addresses are
184 *	available in any of the address families requested.
185 */
186#define DNS_ADBFIND_AVOIDFETCHES	0x00000010
187/*%
188 *	Fetches will start using the closest zone data or use the root servers.
189 *	This is useful for reestablishing glue that has expired.
190 */
191#define DNS_ADBFIND_STARTATZONE		0x00000020
192/*%
193 *	Glue or hints are ok.  These are used when matching names already
194 *	in the adb, and when dns databases are searched.
195 */
196#define DNS_ADBFIND_GLUEOK		0x00000040
197/*%
198 *	Glue or hints are ok.  These are used when matching names already
199 *	in the adb, and when dns databases are searched.
200 */
201#define DNS_ADBFIND_HINTOK		0x00000080
202/*%
203 *	Return lame servers in a find, so that all addresses are returned.
204 */
205#define DNS_ADBFIND_RETURNLAME		0x00000100
206/*%
207 *      Only schedule an event if no addresses are known.
208 *      Must set _WANTEVENT for this to be meaningful.
209 */
210#define DNS_ADBFIND_LAMEPRUNED		0x00000200
211
212/*%
213 * The answers to queries come back as a list of these.
214 */
215struct dns_adbaddrinfo {
216	unsigned int			magic;		/*%< private */
217
218	isc_sockaddr_t			sockaddr;	/*%< [rw] */
219	unsigned int			srtt;		/*%< [rw] microseconds */
220	unsigned int			flags;		/*%< [rw] */
221	dns_adbentry_t		       *entry;		/*%< private */
222	ISC_LINK(dns_adbaddrinfo_t)	publink;
223};
224
225/*!<
226 * The event sent to the caller task is just a plain old isc_event_t.  It
227 * contains no data other than a simple status, passed in the "type" field
228 * to indicate that another address resolved, or all partially resolved
229 * addresses have failed to resolve.
230 *
231 * "sender" is the dns_adbfind_t used to issue this query.
232 *
233 * This is simply a standard event, with the "type" set to:
234 *
235 *\li	#DNS_EVENT_ADBMOREADDRESSES   -- another address resolved.
236 *\li	#DNS_EVENT_ADBNOMOREADDRESSES -- all pending addresses failed,
237 *					were canceled, or otherwise will
238 *					not be usable.
239 *\li	#DNS_EVENT_ADBCANCELED	     -- The request was canceled by a
240 *					3rd party.
241 *\li	#DNS_EVENT_ADBNAMEDELETED     -- The name was deleted, so this request
242 *					was canceled.
243 *
244 * In each of these cases, the addresses returned by the initial call
245 * to dns_adb_createfind() can still be used until they are no longer needed.
246 */
247
248/****
249 **** FUNCTIONS
250 ****/
251
252
253isc_result_t
254dns_adb_create(isc_mem_t *mem, dns_view_t *view, isc_timermgr_t *tmgr,
255	       isc_taskmgr_t *taskmgr, dns_adb_t **newadb);
256/*%<
257 * Create a new ADB.
258 *
259 * Notes:
260 *
261 *\li	Generally, applications should not create an ADB directly, but
262 *	should instead call dns_view_createresolver().
263 *
264 * Requires:
265 *
266 *\li	'mem' must be a valid memory context.
267 *
268 *\li	'view' be a pointer to a valid view.
269 *
270 *\li	'tmgr' be a pointer to a valid timer manager.
271 *
272 *\li	'taskmgr' be a pointer to a valid task manager.
273 *
274 *\li	'newadb' != NULL && '*newadb' == NULL.
275 *
276 * Returns:
277 *
278 *\li	#ISC_R_SUCCESS	after happiness.
279 *\li	#ISC_R_NOMEMORY	after resource allocation failure.
280 */
281
282void
283dns_adb_attach(dns_adb_t *adb, dns_adb_t **adbp);
284/*%
285 * Attach to an 'adb' to 'adbp'.
286 *
287 * Requires:
288 *\li	'adb' to be a valid dns_adb_t, created via dns_adb_create().
289 *\li	'adbp' to be a valid pointer to a *dns_adb_t which is initialized
290 *	to NULL.
291 */
292
293void
294dns_adb_detach(dns_adb_t **adb);
295/*%
296 * Delete the ADB. Sets *ADB to NULL. Cancels any outstanding requests.
297 *
298 * Requires:
299 *
300 *\li	'adb' be non-NULL and '*adb' be a valid dns_adb_t, created via
301 *	dns_adb_create().
302 */
303
304void
305dns_adb_whenshutdown(dns_adb_t *adb, isc_task_t *task, isc_event_t **eventp);
306/*%
307 * Send '*eventp' to 'task' when 'adb' has shutdown.
308 *
309 * Requires:
310 *
311 *\li	'*adb' is a valid dns_adb_t.
312 *
313 *\li	eventp != NULL && *eventp is a valid event.
314 *
315 * Ensures:
316 *
317 *\li	*eventp == NULL
318 *
319 *\li	The event's sender field is set to the value of adb when the event
320 *	is sent.
321 */
322
323void
324dns_adb_shutdown(dns_adb_t *adb);
325/*%<
326 * Shutdown 'adb'.
327 *
328 * Requires:
329 *
330 * \li	'*adb' is a valid dns_adb_t.
331 */
332
333isc_result_t
334dns_adb_createfind(dns_adb_t *adb, isc_task_t *task, isc_taskaction_t action,
335		   void *arg, dns_name_t *name, dns_name_t *qname,
336		   dns_rdatatype_t qtype, unsigned int options,
337		   isc_stdtime_t now, dns_name_t *target,
338		   in_port_t port, dns_adbfind_t **find);
339/*%<
340 * Main interface for clients. The adb will look up the name given in
341 * "name" and will build up a list of found addresses, and perhaps start
342 * internal fetches to resolve names that are unknown currently.
343 *
344 * If other addresses resolve after this call completes, an event will
345 * be sent to the <task, taskaction, arg> with the sender of that event
346 * set to a pointer to the dns_adbfind_t returned by this function.
347 *
348 * If no events will be generated, the *find->result_v4 and/or result_v6
349 * members may be examined for address lookup status.  The usual #ISC_R_SUCCESS,
350 * #ISC_R_FAILURE, #DNS_R_NXDOMAIN, and #DNS_R_NXRRSET are returned, along with
351 * #ISC_R_NOTFOUND meaning the ADB has not _yet_ found the values.  In this
352 * latter case, retrying may produce more addresses.
353 *
354 * If events will be returned, the result_v[46] members are only valid
355 * when that event is actually returned.
356 *
357 * The list of addresses returned is unordered.  The caller must impose
358 * any ordering required.  The list will not contain "known bad" addresses,
359 * however.  For instance, it will not return hosts that are known to be
360 * lame for the zone in question.
361 *
362 * The caller cannot (directly) modify the contents of the address list's
363 * fields other than the "link" field.  All values can be read at any
364 * time, however.
365 *
366 * The "now" parameter is used only for determining which entries that
367 * have a specific time to live or expire time should be removed from
368 * the running database.  If specified as zero, the current time will
369 * be retrieved and used.
370 *
371 * If 'target' is not NULL and 'name' is an alias (i.e. the name is
372 * CNAME'd or DNAME'd to another name), then 'target' will be updated with
373 * the domain name that 'name' is aliased to.
374 *
375 * All addresses returned will have the sockaddr's port set to 'port.'
376 * The caller may change them directly in the dns_adbaddrinfo_t since
377 * they are copies of the internal address only.
378 *
379 * XXXMLG  Document options, especially the flags which control how
380 *         events are sent.
381 *
382 * Requires:
383 *
384 *\li	*adb be a valid isc_adb_t object.
385 *
386 *\li	If events are to be sent, *task be a valid task,
387 *	and isc_taskaction_t != NULL.
388 *
389 *\li	*name is a valid dns_name_t.
390 *
391 *\li	qname != NULL and *qname be a valid dns_name_t.
392 *
393 *\li	target == NULL or target is a valid name with a buffer.
394 *
395 *\li	find != NULL && *find == NULL.
396 *
397 * Returns:
398 *
399 *\li	#ISC_R_SUCCESS	Addresses might have been returned, and events will be
400 *			delivered for unresolved addresses.
401 *\li	#ISC_R_NOMORE	Addresses might have been returned, but no events
402 *			will ever be posted for this context.  This is only
403 *			returned if task != NULL.
404 *\li	#ISC_R_NOMEMORY	insufficient resources
405 *\li	#DNS_R_ALIAS	'name' is an alias for another name.
406 *
407 * Calls, and returns error codes from:
408 *
409 *\li	isc_stdtime_get()
410 *
411 * Notes:
412 *
413 *\li	No internal reference to "name" exists after this function
414 *	returns.
415 */
416
417void
418dns_adb_cancelfind(dns_adbfind_t *find);
419/*%<
420 * Cancels the find, and sends the event off to the caller.
421 *
422 * It is an error to call dns_adb_cancelfind() on a find where
423 * no event is wanted, or will ever be sent.
424 *
425 * Note:
426 *
427 *\li	It is possible that the real completion event was posted just
428 *	before the dns_adb_cancelfind() call was made.  In this case,
429 *	dns_adb_cancelfind() will do nothing.  The event callback needs
430 *	to be prepared to find this situation (i.e. result is valid but
431 *	the caller expects it to be canceled).
432 *
433 * Requires:
434 *
435 *\li	'find' be a valid dns_adbfind_t pointer.
436 *
437 *\li	events would have been posted to the task.  This can be checked
438 *	with (find->options & DNS_ADBFIND_WANTEVENT).
439 *
440 * Ensures:
441 *
442 *\li	The event was posted to the task.
443 */
444
445void
446dns_adb_destroyfind(dns_adbfind_t **find);
447/*%<
448 * Destroys the find reference.
449 *
450 * Note:
451 *
452 *\li	This can only be called after the event was delivered for a
453 *	find.  Additionally, the event MUST have been freed via
454 *	isc_event_free() BEFORE this function is called.
455 *
456 * Requires:
457 *
458 *\li	'find' != NULL and *find be valid dns_adbfind_t pointer.
459 *
460 * Ensures:
461 *
462 *\li	No "address found" events will be posted to the originating task
463 *	after this function returns.
464 */
465
466void
467dns_adb_dump(dns_adb_t *adb, FILE *f);
468/*%<
469 * This function is only used for debugging.  It will dump as much of the
470 * state of the running system as possible.
471 *
472 * Requires:
473 *
474 *\li	adb be valid.
475 *
476 *\li	f != NULL, and is a file open for writing.
477 */
478
479void
480dns_adb_dumpfind(dns_adbfind_t *find, FILE *f);
481/*%<
482 * This function is only used for debugging.  Dump the data associated
483 * with a find.
484 *
485 * Requires:
486 *
487 *\li	find is valid.
488 *
489 * \li	f != NULL, and is a file open for writing.
490 */
491
492isc_result_t
493dns_adb_marklame(dns_adb_t *adb, dns_adbaddrinfo_t *addr, dns_name_t *qname,
494		 dns_rdatatype_t type, isc_stdtime_t expire_time);
495/*%<
496 * Mark the given address as lame for the <qname,qtype>.  expire_time should
497 * be set to the time when the entry should expire.  That is, if it is to
498 * expire 10 minutes in the future, it should set it to (now + 10 * 60).
499 *
500 * Requires:
501 *
502 *\li	adb be valid.
503 *
504 *\li	addr be valid.
505 *
506 *\li	qname be the qname used in the dns_adb_createfind() call.
507 *
508 * Returns:
509 *
510 *\li	#ISC_R_SUCCESS		-- all is well.
511 *\li	#ISC_R_NOMEMORY		-- could not mark address as lame.
512 */
513
514/*
515 * A reasonable default for RTT adjustments
516 */
517#define DNS_ADB_RTTADJDEFAULT		7	/*%< default scale */
518#define DNS_ADB_RTTADJREPLACE		0	/*%< replace with our rtt */
519#define DNS_ADB_RTTADJAGE		10	/*%< age this rtt */
520
521void
522dns_adb_adjustsrtt(dns_adb_t *adb, dns_adbaddrinfo_t *addr,
523		   unsigned int rtt, unsigned int factor);
524/*%<
525 * Mix the round trip time into the existing smoothed rtt.
526
527 * The formula used
528 * (where srtt is the existing rtt value, and rtt and factor are arguments to
529 * this function):
530 *
531 *\code
532 *	new_srtt = (old_srtt / 10 * factor) + (rtt / 10 * (10 - factor));
533 *\endcode
534 *
535 * XXXRTH  Do we want to publish the formula?  What if we want to change how
536 *         this works later on?  Recommend/require that the units are
537 *	   microseconds?
538 *
539 * Requires:
540 *
541 *\li	adb be valid.
542 *
543 *\li	addr be valid.
544 *
545 *\li	0 <= factor <= 10
546 *
547 * Note:
548 *
549 *\li	The srtt in addr will be updated to reflect the new global
550 *	srtt value.  This may include changes made by others.
551 */
552
553void
554dns_adb_changeflags(dns_adb_t *adb, dns_adbaddrinfo_t *addr,
555		    unsigned int bits, unsigned int mask);
556/*%
557 * Change Flags.
558 *
559 * Set the flags as given by:
560 *
561 *\li	newflags = (oldflags & ~mask) | (bits & mask);
562 *
563 * Requires:
564 *
565 *\li	adb be valid.
566 *
567 *\li	addr be valid.
568 */
569
570isc_result_t
571dns_adb_findaddrinfo(dns_adb_t *adb, isc_sockaddr_t *sa,
572		     dns_adbaddrinfo_t **addrp, isc_stdtime_t now);
573/*%<
574 * Return a dns_adbaddrinfo_t that is associated with address 'sa'.
575 *
576 * Requires:
577 *
578 *\li	adb is valid.
579 *
580 *\li	sa is valid.
581 *
582 *\li	addrp != NULL && *addrp == NULL
583 *
584 * Returns:
585 *\li	#ISC_R_SUCCESS
586 *\li	#ISC_R_NOMEMORY
587 *\li	#ISC_R_SHUTTINGDOWN
588 */
589
590void
591dns_adb_freeaddrinfo(dns_adb_t *adb, dns_adbaddrinfo_t **addrp);
592/*%<
593 * Free a dns_adbaddrinfo_t allocated by dns_adb_findaddrinfo().
594 *
595 * Requires:
596 *
597 *\li	adb is valid.
598 *
599 *\li	*addrp is a valid dns_adbaddrinfo_t *.
600 */
601
602void
603dns_adb_flush(dns_adb_t *adb);
604/*%<
605 * Flushes all cached data from the adb.
606 *
607 * Requires:
608 *\li 	adb is valid.
609 */
610
611void
612dns_adb_setadbsize(dns_adb_t *adb, isc_uint32_t size);
613/*%<
614 * Set a target memory size.  If memory usage exceeds the target
615 * size entries will be removed before they would have expired on
616 * a random basis.
617 *
618 * If 'size' is 0 then memory usage is unlimited.
619 *
620 * Requires:
621 *\li	'adb' is valid.
622 */
623
624void
625dns_adb_flushname(dns_adb_t *adb, dns_name_t *name);
626/*%<
627 * Flush 'name' from the adb cache.
628 *
629 * Requires:
630 *\li	'adb' is valid.
631 *\li	'name' is valid.
632 */
633
634ISC_LANG_ENDDECLS
635
636#endif /* DNS_ADB_H */
637