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