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