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