]> CyberLeo.Net >> Repos - FreeBSD/releng/9.3.git/blob - contrib/bind9/lib/dns/include/dns/adb.h
Fix multiple vulnerabilities in file(1) and libmagic(3).
[FreeBSD/releng/9.3.git] / contrib / bind9 / lib / dns / include / dns / adb.h
1 /*
2  * Copyright (C) 2004-2008, 2011, 2013  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: adb.h,v 1.88 2011/12/05 17:10:51 each Exp $ */
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         isc_uint32_t                    qtotal;
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  */
215 struct 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
253 isc_result_t
254 dns_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
282 void
283 dns_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
293 void
294 dns_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
304 void
305 dns_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
323 void
324 dns_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
333 isc_result_t
334 dns_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 isc_result_t
340 dns_adb_createfind2(dns_adb_t *adb, isc_task_t *task, isc_taskaction_t action,
341                     void *arg, dns_name_t *name, dns_name_t *qname,
342                     dns_rdatatype_t qtype, unsigned int options,
343                     isc_stdtime_t now, dns_name_t *target, in_port_t port,
344                     unsigned int depth, dns_adbfind_t **find);
345 /*%<
346  * Main interface for clients. The adb will look up the name given in
347  * "name" and will build up a list of found addresses, and perhaps start
348  * internal fetches to resolve names that are unknown currently.
349  *
350  * If other addresses resolve after this call completes, an event will
351  * be sent to the <task, taskaction, arg> with the sender of that event
352  * set to a pointer to the dns_adbfind_t returned by this function.
353  *
354  * If no events will be generated, the *find->result_v4 and/or result_v6
355  * members may be examined for address lookup status.  The usual #ISC_R_SUCCESS,
356  * #ISC_R_FAILURE, #DNS_R_NXDOMAIN, and #DNS_R_NXRRSET are returned, along with
357  * #ISC_R_NOTFOUND meaning the ADB has not _yet_ found the values.  In this
358  * latter case, retrying may produce more addresses.
359  *
360  * If events will be returned, the result_v[46] members are only valid
361  * when that event is actually returned.
362  *
363  * The list of addresses returned is unordered.  The caller must impose
364  * any ordering required.  The list will not contain "known bad" addresses,
365  * however.  For instance, it will not return hosts that are known to be
366  * lame for the zone in question.
367  *
368  * The caller cannot (directly) modify the contents of the address list's
369  * fields other than the "link" field.  All values can be read at any
370  * time, however.
371  *
372  * The "now" parameter is used only for determining which entries that
373  * have a specific time to live or expire time should be removed from
374  * the running database.  If specified as zero, the current time will
375  * be retrieved and used.
376  *
377  * If 'target' is not NULL and 'name' is an alias (i.e. the name is
378  * CNAME'd or DNAME'd to another name), then 'target' will be updated with
379  * the domain name that 'name' is aliased to.
380  *
381  * All addresses returned will have the sockaddr's port set to 'port.'
382  * The caller may change them directly in the dns_adbaddrinfo_t since
383  * they are copies of the internal address only.
384  *
385  * XXXMLG  Document options, especially the flags which control how
386  *         events are sent.
387  *
388  * Requires:
389  *
390  *\li   *adb be a valid isc_adb_t object.
391  *
392  *\li   If events are to be sent, *task be a valid task,
393  *      and isc_taskaction_t != NULL.
394  *
395  *\li   *name is a valid dns_name_t.
396  *
397  *\li   qname != NULL and *qname be a valid dns_name_t.
398  *
399  *\li   target == NULL or target is a valid name with a buffer.
400  *
401  *\li   find != NULL && *find == NULL.
402  *
403  * Returns:
404  *
405  *\li   #ISC_R_SUCCESS  Addresses might have been returned, and events will be
406  *                      delivered for unresolved addresses.
407  *\li   #ISC_R_NOMORE   Addresses might have been returned, but no events
408  *                      will ever be posted for this context.  This is only
409  *                      returned if task != NULL.
410  *\li   #ISC_R_NOMEMORY insufficient resources
411  *\li   #DNS_R_ALIAS    'name' is an alias for another name.
412  *
413  * Calls, and returns error codes from:
414  *
415  *\li   isc_stdtime_get()
416  *
417  * Notes:
418  *
419  *\li   No internal reference to "name" exists after this function
420  *      returns.
421  */
422
423 void
424 dns_adb_cancelfind(dns_adbfind_t *find);
425 /*%<
426  * Cancels the find, and sends the event off to the caller.
427  *
428  * It is an error to call dns_adb_cancelfind() on a find where
429  * no event is wanted, or will ever be sent.
430  *
431  * Note:
432  *
433  *\li   It is possible that the real completion event was posted just
434  *      before the dns_adb_cancelfind() call was made.  In this case,
435  *      dns_adb_cancelfind() will do nothing.  The event callback needs
436  *      to be prepared to find this situation (i.e. result is valid but
437  *      the caller expects it to be canceled).
438  *
439  * Requires:
440  *
441  *\li   'find' be a valid dns_adbfind_t pointer.
442  *
443  *\li   events would have been posted to the task.  This can be checked
444  *      with (find->options & DNS_ADBFIND_WANTEVENT).
445  *
446  * Ensures:
447  *
448  *\li   The event was posted to the task.
449  */
450
451 void
452 dns_adb_destroyfind(dns_adbfind_t **find);
453 /*%<
454  * Destroys the find reference.
455  *
456  * Note:
457  *
458  *\li   This can only be called after the event was delivered for a
459  *      find.  Additionally, the event MUST have been freed via
460  *      isc_event_free() BEFORE this function is called.
461  *
462  * Requires:
463  *
464  *\li   'find' != NULL and *find be valid dns_adbfind_t pointer.
465  *
466  * Ensures:
467  *
468  *\li   No "address found" events will be posted to the originating task
469  *      after this function returns.
470  */
471
472 void
473 dns_adb_dump(dns_adb_t *adb, FILE *f);
474 /*%<
475  * This function is only used for debugging.  It will dump as much of the
476  * state of the running system as possible.
477  *
478  * Requires:
479  *
480  *\li   adb be valid.
481  *
482  *\li   f != NULL, and is a file open for writing.
483  */
484
485 void
486 dns_adb_dumpfind(dns_adbfind_t *find, FILE *f);
487 /*%<
488  * This function is only used for debugging.  Dump the data associated
489  * with a find.
490  *
491  * Requires:
492  *
493  *\li   find is valid.
494  *
495  * \li  f != NULL, and is a file open for writing.
496  */
497
498 isc_result_t
499 dns_adb_marklame(dns_adb_t *adb, dns_adbaddrinfo_t *addr, dns_name_t *qname,
500                  dns_rdatatype_t type, isc_stdtime_t expire_time);
501 /*%<
502  * Mark the given address as lame for the <qname,qtype>.  expire_time should
503  * be set to the time when the entry should expire.  That is, if it is to
504  * expire 10 minutes in the future, it should set it to (now + 10 * 60).
505  *
506  * Requires:
507  *
508  *\li   adb be valid.
509  *
510  *\li   addr be valid.
511  *
512  *\li   qname be the qname used in the dns_adb_createfind() call.
513  *
514  * Returns:
515  *
516  *\li   #ISC_R_SUCCESS          -- all is well.
517  *\li   #ISC_R_NOMEMORY         -- could not mark address as lame.
518  */
519
520 /*
521  * A reasonable default for RTT adjustments
522  */
523 #define DNS_ADB_RTTADJDEFAULT           7       /*%< default scale */
524 #define DNS_ADB_RTTADJREPLACE           0       /*%< replace with our rtt */
525 #define DNS_ADB_RTTADJAGE               10      /*%< age this rtt */
526
527 void
528 dns_adb_adjustsrtt(dns_adb_t *adb, dns_adbaddrinfo_t *addr,
529                    unsigned int rtt, unsigned int factor);
530 /*%<
531  * Mix the round trip time into the existing smoothed rtt.
532
533  * The formula used
534  * (where srtt is the existing rtt value, and rtt and factor are arguments to
535  * this function):
536  *
537  *\code
538  *      new_srtt = (old_srtt / 10 * factor) + (rtt / 10 * (10 - factor));
539  *\endcode
540  *
541  * XXXRTH  Do we want to publish the formula?  What if we want to change how
542  *         this works later on?  Recommend/require that the units are
543  *         microseconds?
544  *
545  * Requires:
546  *
547  *\li   adb be valid.
548  *
549  *\li   addr be valid.
550  *
551  *\li   0 <= factor <= 10
552  *
553  * Note:
554  *
555  *\li   The srtt in addr will be updated to reflect the new global
556  *      srtt value.  This may include changes made by others.
557  */
558
559 void
560 dns_adb_changeflags(dns_adb_t *adb, dns_adbaddrinfo_t *addr,
561                     unsigned int bits, unsigned int mask);
562 /*%
563  * Change Flags.
564  *
565  * Set the flags as given by:
566  *
567  *\li   newflags = (oldflags & ~mask) | (bits & mask);
568  *
569  * Requires:
570  *
571  *\li   adb be valid.
572  *
573  *\li   addr be valid.
574  */
575
576 isc_result_t
577 dns_adb_findaddrinfo(dns_adb_t *adb, isc_sockaddr_t *sa,
578                      dns_adbaddrinfo_t **addrp, isc_stdtime_t now);
579 /*%<
580  * Return a dns_adbaddrinfo_t that is associated with address 'sa'.
581  *
582  * Requires:
583  *
584  *\li   adb is valid.
585  *
586  *\li   sa is valid.
587  *
588  *\li   addrp != NULL && *addrp == NULL
589  *
590  * Returns:
591  *\li   #ISC_R_SUCCESS
592  *\li   #ISC_R_NOMEMORY
593  *\li   #ISC_R_SHUTTINGDOWN
594  */
595
596 void
597 dns_adb_freeaddrinfo(dns_adb_t *adb, dns_adbaddrinfo_t **addrp);
598 /*%<
599  * Free a dns_adbaddrinfo_t allocated by dns_adb_findaddrinfo().
600  *
601  * Requires:
602  *
603  *\li   adb is valid.
604  *
605  *\li   *addrp is a valid dns_adbaddrinfo_t *.
606  */
607
608 void
609 dns_adb_flush(dns_adb_t *adb);
610 /*%<
611  * Flushes all cached data from the adb.
612  *
613  * Requires:
614  *\li   adb is valid.
615  */
616
617 void
618 dns_adb_setadbsize(dns_adb_t *adb, size_t size);
619 /*%<
620  * Set a target memory size.  If memory usage exceeds the target
621  * size entries will be removed before they would have expired on
622  * a random basis.
623  *
624  * If 'size' is 0 then memory usage is unlimited.
625  *
626  * Requires:
627  *\li   'adb' is valid.
628  */
629
630 void
631 dns_adb_flushname(dns_adb_t *adb, dns_name_t *name);
632 /*%<
633  * Flush 'name' from the adb cache.
634  *
635  * Requires:
636  *\li   'adb' is valid.
637  *\li   'name' is valid.
638  */
639
640 ISC_LANG_ENDDECLS
641
642 #endif /* DNS_ADB_H */