xref: /netbsd-src/external/bsd/libevent/dist/include/event2/dns.h (revision 657871a79c9a2060a6255a242fa1a1ef76b56ec6)
1 /*	$NetBSD: dns.h,v 1.1.1.4 2021/04/07 02:43:14 christos Exp $	*/
2 /*
3  * Copyright (c) 2006-2007 Niels Provos <provos@citi.umich.edu>
4  * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /*
30  * The original DNS code is due to Adam Langley with heavy
31  * modifications by Nick Mathewson.  Adam put his DNS software in the
32  * public domain.  You can find his original copyright below.  Please,
33  * aware that the code as part of Libevent is governed by the 3-clause
34  * BSD license above.
35  *
36  * This software is Public Domain. To view a copy of the public domain dedication,
37  * visit http://creativecommons.org/licenses/publicdomain/ or send a letter to
38  * Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
39  *
40  * I ask and expect, but do not require, that all derivative works contain an
41  * attribution similar to:
42  *     Parts developed by Adam Langley <agl@imperialviolet.org>
43  *
44  * You may wish to replace the word "Parts" with something else depending on
45  * the amount of original code.
46  *
47  * (Derivative works does not include programs which link against, run or include
48  * the source verbatim in their source distributions)
49  */
50 
51 /** @file event2/dns.h
52  *
53  * Welcome, gentle reader
54  *
55  * Async DNS lookups are really a whole lot harder than they should be,
56  * mostly stemming from the fact that the libc resolver has never been
57  * very good at them. Before you use this library you should see if libc
58  * can do the job for you with the modern async call getaddrinfo_a
59  * (see http://www.imperialviolet.org/page25.html#e498). Otherwise,
60  * please continue.
61  *
62  * The library keeps track of the state of nameservers and will avoid
63  * them when they go down. Otherwise it will round robin between them.
64  *
65  * Quick start guide:
66  *   #include "evdns.h"
67  *   void callback(int result, char type, int count, int ttl,
68  *		 void *addresses, void *arg);
69  *   evdns_resolv_conf_parse(DNS_OPTIONS_ALL, "/etc/resolv.conf");
70  *   evdns_resolve("www.hostname.com", 0, callback, NULL);
71  *
72  * When the lookup is complete the callback function is called. The
73  * first argument will be one of the DNS_ERR_* defines in evdns.h.
74  * Hopefully it will be DNS_ERR_NONE, in which case type will be
75  * DNS_IPv4_A, count will be the number of IP addresses, ttl is the time
76  * which the data can be cached for (in seconds), addresses will point
77  * to an array of uint32_t's and arg will be whatever you passed to
78  * evdns_resolve.
79  *
80  * Searching:
81  *
82  * In order for this library to be a good replacement for glibc's resolver it
83  * supports searching. This involves setting a list of default domains, in
84  * which names will be queried for. The number of dots in the query name
85  * determines the order in which this list is used.
86  *
87  * Searching appears to be a single lookup from the point of view of the API,
88  * although many DNS queries may be generated from a single call to
89  * evdns_resolve. Searching can also drastically slow down the resolution
90  * of names.
91  *
92  * To disable searching:
93  *   1. Never set it up. If you never call evdns_resolv_conf_parse or
94  *   evdns_search_add then no searching will occur.
95  *
96  *   2. If you do call evdns_resolv_conf_parse then don't pass
97  *   DNS_OPTION_SEARCH (or DNS_OPTIONS_ALL, which implies it).
98  *
99  *   3. When calling evdns_resolve, pass the DNS_QUERY_NO_SEARCH flag.
100  *
101  * The order of searches depends on the number of dots in the name. If the
102  * number is greater than the ndots setting then the names is first tried
103  * globally. Otherwise each search domain is appended in turn.
104  *
105  * The ndots setting can either be set from a resolv.conf, or by calling
106  * evdns_search_ndots_set.
107  *
108  * For example, with ndots set to 1 (the default) and a search domain list of
109  * ["myhome.net"]:
110  *  Query: www
111  *  Order: www.myhome.net, www.
112  *
113  *  Query: www.abc
114  *  Order: www.abc., www.abc.myhome.net
115  *
116  * Internals:
117  *
118  * Requests are kept in two queues. The first is the inflight queue. In
119  * this queue requests have an allocated transaction id and nameserver.
120  * They will soon be transmitted if they haven't already been.
121  *
122  * The second is the waiting queue. The size of the inflight ring is
123  * limited and all other requests wait in waiting queue for space. This
124  * bounds the number of concurrent requests so that we don't flood the
125  * nameserver. Several algorithms require a full walk of the inflight
126  * queue and so bounding its size keeps thing going nicely under huge
127  * (many thousands of requests) loads.
128  *
129  * If a nameserver loses too many requests it is considered down and we
130  * try not to use it. After a while we send a probe to that nameserver
131  * (a lookup for google.com) and, if it replies, we consider it working
132  * again. If the nameserver fails a probe we wait longer to try again
133  * with the next probe.
134  */
135 
136 #ifndef EVENT2_DNS_H_INCLUDED_
137 #define EVENT2_DNS_H_INCLUDED_
138 
139 #include <event2/visibility.h>
140 
141 #ifdef __cplusplus
142 extern "C" {
143 #endif
144 
145 /* For integer types. */
146 #include <event2/util.h>
147 
148 /** Error codes 0-5 are as described in RFC 1035. */
149 #define DNS_ERR_NONE 0
150 /** The name server was unable to interpret the query */
151 #define DNS_ERR_FORMAT 1
152 /** The name server was unable to process this query due to a problem with the
153  * name server */
154 #define DNS_ERR_SERVERFAILED 2
155 /** The domain name does not exist */
156 #define DNS_ERR_NOTEXIST 3
157 /** The name server does not support the requested kind of query */
158 #define DNS_ERR_NOTIMPL 4
159 /** The name server refuses to reform the specified operation for policy
160  * reasons */
161 #define DNS_ERR_REFUSED 5
162 /** The reply was truncated or ill-formatted */
163 #define DNS_ERR_TRUNCATED 65
164 /** An unknown error occurred */
165 #define DNS_ERR_UNKNOWN 66
166 /** Communication with the server timed out */
167 #define DNS_ERR_TIMEOUT 67
168 /** The request was canceled because the DNS subsystem was shut down. */
169 #define DNS_ERR_SHUTDOWN 68
170 /** The request was canceled via a call to evdns_cancel_request */
171 #define DNS_ERR_CANCEL 69
172 /** There were no answers and no error condition in the DNS packet.
173  * This can happen when you ask for an address that exists, but a record
174  * type that doesn't. */
175 #define DNS_ERR_NODATA 70
176 
177 #define DNS_IPv4_A 1
178 #define DNS_PTR 2
179 #define DNS_IPv6_AAAA 3
180 
181 #define DNS_QUERY_NO_SEARCH 1
182 
183 /* Allow searching */
184 #define DNS_OPTION_SEARCH 1
185 /* Parse "nameserver" and add default if no such section */
186 #define DNS_OPTION_NAMESERVERS 2
187 /* Parse additional options like:
188  * - timeout:
189  * - getaddrinfo-allow-skew:
190  * - max-timeouts:
191  * - max-inflight:
192  * - attempts:
193  * - randomize-case:
194  * - initial-probe-timeout:
195  */
196 #define DNS_OPTION_MISC 4
197 /* Load hosts file (i.e. "/etc/hosts") */
198 #define DNS_OPTION_HOSTSFILE 8
199 /**
200  * All above:
201  * - DNS_OPTION_SEARCH
202  * - DNS_OPTION_NAMESERVERS
203  * - DNS_OPTION_MISC
204  * - DNS_OPTION_HOSTSFILE
205  */
206 #define DNS_OPTIONS_ALL (    \
207     DNS_OPTION_SEARCH      | \
208     DNS_OPTION_NAMESERVERS | \
209     DNS_OPTION_MISC        | \
210     DNS_OPTION_HOSTSFILE   | \
211     0                        \
212 )
213 /* Do not "default" nameserver (i.e. "127.0.0.1:53") if there is no nameservers
214  * in resolv.conf, (iff DNS_OPTION_NAMESERVERS is set) */
215 #define DNS_OPTION_NAMESERVERS_NO_DEFAULT 16
216 
217 /* Obsolete name for DNS_QUERY_NO_SEARCH */
218 #define DNS_NO_SEARCH DNS_QUERY_NO_SEARCH
219 
220 /**
221  * The callback that contains the results from a lookup.
222  * - result is one of the DNS_ERR_* values (DNS_ERR_NONE for success)
223  * - type is either DNS_IPv4_A or DNS_PTR or DNS_IPv6_AAAA
224  * - count contains the number of addresses of form type
225  * - ttl is the number of seconds the resolution may be cached for.
226  * - addresses needs to be cast according to type.  It will be an array of
227  *   4-byte sequences for ipv4, or an array of 16-byte sequences for ipv6,
228  *   or a nul-terminated string for PTR.
229  */
230 typedef void (*evdns_callback_type) (int result, char type, int count, int ttl, void *addresses, void *arg);
231 
232 struct evdns_base;
233 struct event_base;
234 
235 /** Flag for evdns_base_new: process resolv.conf.  */
236 #define EVDNS_BASE_INITIALIZE_NAMESERVERS 1
237 /** Flag for evdns_base_new: Do not prevent the libevent event loop from
238  * exiting when we have no active dns requests. */
239 #define EVDNS_BASE_DISABLE_WHEN_INACTIVE 0x8000
240 /** Flag for evdns_base_new: If EVDNS_BASE_INITIALIZE_NAMESERVERS isset, do not
241  * add default nameserver if there are no nameservers in resolv.conf
242  * @see DNS_OPTION_NAMESERVERS_NO_DEFAULT */
243 #define EVDNS_BASE_NAMESERVERS_NO_DEFAULT 0x10000
244 
245 /**
246   Initialize the asynchronous DNS library.
247 
248   This function initializes support for non-blocking name resolution by
249   calling evdns_resolv_conf_parse() on UNIX and
250   evdns_config_windows_nameservers() on Windows.
251 
252   @param event_base the event base to associate the dns client with
253   @param flags any of EVDNS_BASE_INITIALIZE_NAMESERVERS|
254     EVDNS_BASE_DISABLE_WHEN_INACTIVE|EVDNS_BASE_NAMESERVERS_NO_DEFAULT
255   @return evdns_base object if successful, or NULL if an error occurred.
256   @see evdns_base_free()
257  */
258 EVENT2_EXPORT_SYMBOL
259 struct evdns_base * evdns_base_new(struct event_base *event_base, int initialize_nameservers);
260 
261 
262 /**
263   Shut down the asynchronous DNS resolver and terminate all active requests.
264 
265   If the 'fail_requests' option is enabled, all active requests will return
266   an empty result with the error flag set to DNS_ERR_SHUTDOWN. Otherwise,
267   the requests will be silently discarded.
268 
269   @param evdns_base the evdns base to free
270   @param fail_requests if zero, active requests will be aborted; if non-zero,
271 		active requests will return DNS_ERR_SHUTDOWN.
272   @see evdns_base_new()
273  */
274 EVENT2_EXPORT_SYMBOL
275 void evdns_base_free(struct evdns_base *base, int fail_requests);
276 
277 /**
278    Remove all hosts entries that have been loaded into the event_base via
279    evdns_base_load_hosts or via event_base_resolv_conf_parse.
280 
281    @param evdns_base the evdns base to remove outdated host addresses from
282  */
283 EVENT2_EXPORT_SYMBOL
284 void evdns_base_clear_host_addresses(struct evdns_base *base);
285 
286 /**
287   Convert a DNS error code to a string.
288 
289   @param err the DNS error code
290   @return a string containing an explanation of the error code
291 */
292 EVENT2_EXPORT_SYMBOL
293 const char *evdns_err_to_string(int err);
294 
295 
296 /**
297   Add a nameserver.
298 
299   The address should be an IPv4 address in network byte order.
300   The type of address is chosen so that it matches in_addr.s_addr.
301 
302   @param base the evdns_base to which to add the name server
303   @param address an IP address in network byte order
304   @return 0 if successful, or -1 if an error occurred
305   @see evdns_base_nameserver_ip_add()
306  */
307 EVENT2_EXPORT_SYMBOL
308 int evdns_base_nameserver_add(struct evdns_base *base,
309     unsigned long int address);
310 
311 /**
312   Get the number of configured nameservers.
313 
314   This returns the number of configured nameservers (not necessarily the
315   number of running nameservers).  This is useful for double-checking
316   whether our calls to the various nameserver configuration functions
317   have been successful.
318 
319   @param base the evdns_base to which to apply this operation
320   @return the number of configured nameservers
321   @see evdns_base_nameserver_add()
322  */
323 EVENT2_EXPORT_SYMBOL
324 int evdns_base_count_nameservers(struct evdns_base *base);
325 
326 /**
327   Remove all configured nameservers, and suspend all pending resolves.
328 
329   Resolves will not necessarily be re-attempted until evdns_base_resume() is called.
330 
331   @param base the evdns_base to which to apply this operation
332   @return 0 if successful, or -1 if an error occurred
333   @see evdns_base_resume()
334  */
335 EVENT2_EXPORT_SYMBOL
336 int evdns_base_clear_nameservers_and_suspend(struct evdns_base *base);
337 
338 
339 /**
340   Resume normal operation and continue any suspended resolve requests.
341 
342   Re-attempt resolves left in limbo after an earlier call to
343   evdns_base_clear_nameservers_and_suspend().
344 
345   @param base the evdns_base to which to apply this operation
346   @return 0 if successful, or -1 if an error occurred
347   @see evdns_base_clear_nameservers_and_suspend()
348  */
349 EVENT2_EXPORT_SYMBOL
350 int evdns_base_resume(struct evdns_base *base);
351 
352 /**
353   Add a nameserver by string address.
354 
355   This function parses a n IPv4 or IPv6 address from a string and adds it as a
356   nameserver.  It supports the following formats:
357   - [IPv6Address]:port
358   - [IPv6Address]
359   - IPv6Address
360   - IPv4Address:port
361   - IPv4Address
362 
363   If no port is specified, it defaults to 53.
364 
365   @param base the evdns_base to which to apply this operation
366   @return 0 if successful, or -1 if an error occurred
367   @see evdns_base_nameserver_add()
368  */
369 EVENT2_EXPORT_SYMBOL
370 int evdns_base_nameserver_ip_add(struct evdns_base *base,
371     const char *ip_as_string);
372 
373 /**
374    Add a nameserver by sockaddr.
375  **/
376 EVENT2_EXPORT_SYMBOL
377 int
378 evdns_base_nameserver_sockaddr_add(struct evdns_base *base,
379     const struct sockaddr *sa, ev_socklen_t len, unsigned flags);
380 
381 struct evdns_request;
382 
383 /**
384   Lookup an A record for a given name.
385 
386   @param base the evdns_base to which to apply this operation
387   @param name a DNS hostname
388   @param flags either 0, or DNS_QUERY_NO_SEARCH to disable searching for this query.
389   @param callback a callback function to invoke when the request is completed
390   @param ptr an argument to pass to the callback function
391   @return an evdns_request object if successful, or NULL if an error occurred.
392   @see evdns_resolve_ipv6(), evdns_resolve_reverse(), evdns_resolve_reverse_ipv6(), evdns_cancel_request()
393  */
394 EVENT2_EXPORT_SYMBOL
395 struct evdns_request *evdns_base_resolve_ipv4(struct evdns_base *base, const char *name, int flags, evdns_callback_type callback, void *ptr);
396 
397 /**
398   Lookup an AAAA record for a given name.
399 
400   @param base the evdns_base to which to apply this operation
401   @param name a DNS hostname
402   @param flags either 0, or DNS_QUERY_NO_SEARCH to disable searching for this query.
403   @param callback a callback function to invoke when the request is completed
404   @param ptr an argument to pass to the callback function
405   @return an evdns_request object if successful, or NULL if an error occurred.
406   @see evdns_resolve_ipv4(), evdns_resolve_reverse(), evdns_resolve_reverse_ipv6(), evdns_cancel_request()
407  */
408 EVENT2_EXPORT_SYMBOL
409 struct evdns_request *evdns_base_resolve_ipv6(struct evdns_base *base, const char *name, int flags, evdns_callback_type callback, void *ptr);
410 
411 struct in_addr;
412 struct in6_addr;
413 
414 /**
415   Lookup a PTR record for a given IP address.
416 
417   @param base the evdns_base to which to apply this operation
418   @param in an IPv4 address
419   @param flags either 0, or DNS_QUERY_NO_SEARCH to disable searching for this query.
420   @param callback a callback function to invoke when the request is completed
421   @param ptr an argument to pass to the callback function
422   @return an evdns_request object if successful, or NULL if an error occurred.
423   @see evdns_resolve_reverse_ipv6(), evdns_cancel_request()
424  */
425 EVENT2_EXPORT_SYMBOL
426 struct evdns_request *evdns_base_resolve_reverse(struct evdns_base *base, const struct in_addr *in, int flags, evdns_callback_type callback, void *ptr);
427 
428 
429 /**
430   Lookup a PTR record for a given IPv6 address.
431 
432   @param base the evdns_base to which to apply this operation
433   @param in an IPv6 address
434   @param flags either 0, or DNS_QUERY_NO_SEARCH to disable searching for this query.
435   @param callback a callback function to invoke when the request is completed
436   @param ptr an argument to pass to the callback function
437   @return an evdns_request object if successful, or NULL if an error occurred.
438   @see evdns_resolve_reverse_ipv6(), evdns_cancel_request()
439  */
440 EVENT2_EXPORT_SYMBOL
441 struct evdns_request *evdns_base_resolve_reverse_ipv6(struct evdns_base *base, const struct in6_addr *in, int flags, evdns_callback_type callback, void *ptr);
442 
443 /**
444   Cancels a pending DNS resolution request.
445 
446   @param base the evdns_base that was used to make the request
447   @param req the evdns_request that was returned by calling a resolve function
448   @see evdns_base_resolve_ipv4(), evdns_base_resolve_ipv6, evdns_base_resolve_reverse
449 */
450 EVENT2_EXPORT_SYMBOL
451 void evdns_cancel_request(struct evdns_base *base, struct evdns_request *req);
452 
453 /**
454   Set the value of a configuration option.
455 
456   The currently available configuration options are:
457 
458     ndots, timeout, max-timeouts, max-inflight, attempts, randomize-case,
459     bind-to, initial-probe-timeout, getaddrinfo-allow-skew,
460     so-rcvbuf, so-sndbuf.
461 
462   In versions before Libevent 2.0.3-alpha, the option name needed to end with
463   a colon.
464 
465   @param base the evdns_base to which to apply this operation
466   @param option the name of the configuration option to be modified
467   @param val the value to be set
468   @return 0 if successful, or -1 if an error occurred
469  */
470 EVENT2_EXPORT_SYMBOL
471 int evdns_base_set_option(struct evdns_base *base, const char *option, const char *val);
472 
473 
474 /**
475   Parse a resolv.conf file.
476 
477   The 'flags' parameter determines what information is parsed from the
478   resolv.conf file. See the man page for resolv.conf for the format of this
479   file.
480 
481   The following directives are not parsed from the file: sortlist, rotate,
482   no-check-names, inet6, debug.
483 
484   If this function encounters an error, the possible return values are: 1 =
485   failed to open file, 2 = failed to stat file, 3 = file too large, 4 = out of
486   memory, 5 = short read from file, 6 = no nameservers listed in the file
487 
488   @param base the evdns_base to which to apply this operation
489   @param flags any of DNS_OPTION_NAMESERVERS|DNS_OPTION_SEARCH|DNS_OPTION_MISC|
490     DNS_OPTION_HOSTSFILE|DNS_OPTIONS_ALL|DNS_OPTION_NAMESERVERS_NO_DEFAULT
491   @param filename the path to the resolv.conf file
492   @return 0 if successful, or various positive error codes if an error
493     occurred (see above)
494   @see resolv.conf(3), evdns_config_windows_nameservers()
495  */
496 EVENT2_EXPORT_SYMBOL
497 int evdns_base_resolv_conf_parse(struct evdns_base *base, int flags, const char *const filename);
498 
499 /**
500    Load an /etc/hosts-style file from 'hosts_fname' into 'base'.
501 
502    If hosts_fname is NULL, add minimal entries for localhost, and nothing
503    else.
504 
505    Note that only evdns_getaddrinfo uses the /etc/hosts entries.
506 
507    This function does not replace previously loaded hosts entries; to do that,
508    call evdns_base_clear_host_addresses first.
509 
510    Return 0 on success, negative on failure.
511 */
512 EVENT2_EXPORT_SYMBOL
513 int evdns_base_load_hosts(struct evdns_base *base, const char *hosts_fname);
514 
515 #if defined(EVENT_IN_DOXYGEN_) || defined(_WIN32)
516 /**
517   Obtain nameserver information using the Windows API.
518 
519   Attempt to configure a set of nameservers based on platform settings on
520   a win32 host.  Preferentially tries to use GetNetworkParams; if that fails,
521   looks in the registry.
522 
523   @return 0 if successful, or -1 if an error occurred
524   @see evdns_resolv_conf_parse()
525  */
526 EVENT2_EXPORT_SYMBOL
527 int evdns_base_config_windows_nameservers(struct evdns_base *);
528 #define EVDNS_BASE_CONFIG_WINDOWS_NAMESERVERS_IMPLEMENTED
529 #endif
530 
531 
532 /**
533   Clear the list of search domains.
534  */
535 EVENT2_EXPORT_SYMBOL
536 void evdns_base_search_clear(struct evdns_base *base);
537 
538 
539 /**
540   Add a domain to the list of search domains
541 
542   @param domain the domain to be added to the search list
543  */
544 EVENT2_EXPORT_SYMBOL
545 void evdns_base_search_add(struct evdns_base *base, const char *domain);
546 
547 
548 /**
549   Set the 'ndots' parameter for searches.
550 
551   Sets the number of dots which, when found in a name, causes
552   the first query to be without any search domain.
553 
554   @param ndots the new ndots parameter
555  */
556 EVENT2_EXPORT_SYMBOL
557 void evdns_base_search_ndots_set(struct evdns_base *base, const int ndots);
558 
559 /**
560   A callback that is invoked when a log message is generated
561 
562   @param is_warning indicates if the log message is a 'warning'
563   @param msg the content of the log message
564  */
565 typedef void (*evdns_debug_log_fn_type)(int is_warning, const char *msg);
566 
567 
568 /**
569   Set the callback function to handle DNS log messages.  If this
570   callback is not set, evdns log messages are handled with the regular
571   Libevent logging system.
572 
573   @param fn the callback to be invoked when a log message is generated
574  */
575 EVENT2_EXPORT_SYMBOL
576 void evdns_set_log_fn(evdns_debug_log_fn_type fn);
577 
578 /**
579    Set a callback that will be invoked to generate transaction IDs.  By
580    default, we pick transaction IDs based on the current clock time, which
581    is bad for security.
582 
583    @param fn the new callback, or NULL to use the default.
584 
585    NOTE: This function has no effect in Libevent 2.0.4-alpha and later,
586    since Libevent now provides its own secure RNG.
587  */
588 EVENT2_EXPORT_SYMBOL
589 void evdns_set_transaction_id_fn(ev_uint16_t (*fn)(void));
590 
591 /**
592    Set a callback used to generate random bytes.  By default, we use
593    the same function as passed to evdns_set_transaction_id_fn to generate
594    bytes two at a time.  If a function is provided here, it's also used
595    to generate transaction IDs.
596 
597    NOTE: This function has no effect in Libevent 2.0.4-alpha and later,
598    since Libevent now provides its own secure RNG.
599 */
600 EVENT2_EXPORT_SYMBOL
601 void evdns_set_random_bytes_fn(void (*fn)(char *, size_t));
602 
603 /*
604  * Functions used to implement a DNS server.
605  */
606 
607 struct evdns_server_request;
608 struct evdns_server_question;
609 
610 /**
611    A callback to implement a DNS server.  The callback function receives a DNS
612    request.  It should then optionally add a number of answers to the reply
613    using the evdns_server_request_add_*_reply functions, before calling either
614    evdns_server_request_respond to send the reply back, or
615    evdns_server_request_drop to decline to answer the request.
616 
617    @param req A newly received request
618    @param user_data A pointer that was passed to
619       evdns_add_server_port_with_base().
620  */
621 typedef void (*evdns_request_callback_fn_type)(struct evdns_server_request *, void *);
622 #define EVDNS_ANSWER_SECTION 0
623 #define EVDNS_AUTHORITY_SECTION 1
624 #define EVDNS_ADDITIONAL_SECTION 2
625 
626 #define EVDNS_TYPE_A	   1
627 #define EVDNS_TYPE_NS	   2
628 #define EVDNS_TYPE_CNAME   5
629 #define EVDNS_TYPE_SOA	   6
630 #define EVDNS_TYPE_PTR	  12
631 #define EVDNS_TYPE_MX	  15
632 #define EVDNS_TYPE_TXT	  16
633 #define EVDNS_TYPE_AAAA	  28
634 
635 #define EVDNS_QTYPE_AXFR 252
636 #define EVDNS_QTYPE_ALL	 255
637 
638 #define EVDNS_CLASS_INET   1
639 
640 /* flags that can be set in answers; as part of the err parameter */
641 #define EVDNS_FLAGS_AA	0x400
642 #define EVDNS_FLAGS_RD	0x080
643 
644 /** Create a new DNS server port.
645 
646     @param base The event base to handle events for the server port.
647     @param socket A UDP socket to accept DNS requests.
648     @param flags Always 0 for now.
649     @param callback A function to invoke whenever we get a DNS request
650       on the socket.
651     @param user_data Data to pass to the callback.
652     @return an evdns_server_port structure for this server port or NULL if
653       an error occurred.
654  */
655 EVENT2_EXPORT_SYMBOL
656 struct evdns_server_port *evdns_add_server_port_with_base(struct event_base *base, evutil_socket_t socket, int flags, evdns_request_callback_fn_type callback, void *user_data);
657 /** Close down a DNS server port, and free associated structures. */
658 EVENT2_EXPORT_SYMBOL
659 void evdns_close_server_port(struct evdns_server_port *port);
660 
661 /** Sets some flags in a reply we're building.
662     Allows setting of the AA or RD flags
663  */
664 EVENT2_EXPORT_SYMBOL
665 void evdns_server_request_set_flags(struct evdns_server_request *req, int flags);
666 
667 /* Functions to add an answer to an in-progress DNS reply.
668  */
669 EVENT2_EXPORT_SYMBOL
670 int evdns_server_request_add_reply(struct evdns_server_request *req, int section, const char *name, int type, int dns_class, int ttl, int datalen, int is_name, const char *data);
671 EVENT2_EXPORT_SYMBOL
672 int evdns_server_request_add_a_reply(struct evdns_server_request *req, const char *name, int n, const void *addrs, int ttl);
673 EVENT2_EXPORT_SYMBOL
674 int evdns_server_request_add_aaaa_reply(struct evdns_server_request *req, const char *name, int n, const void *addrs, int ttl);
675 EVENT2_EXPORT_SYMBOL
676 int evdns_server_request_add_ptr_reply(struct evdns_server_request *req, struct in_addr *in, const char *inaddr_name, const char *hostname, int ttl);
677 EVENT2_EXPORT_SYMBOL
678 int evdns_server_request_add_cname_reply(struct evdns_server_request *req, const char *name, const char *cname, int ttl);
679 
680 /**
681    Send back a response to a DNS request, and free the request structure.
682 */
683 EVENT2_EXPORT_SYMBOL
684 int evdns_server_request_respond(struct evdns_server_request *req, int err);
685 /**
686    Free a DNS request without sending back a reply.
687 */
688 EVENT2_EXPORT_SYMBOL
689 int evdns_server_request_drop(struct evdns_server_request *req);
690 struct sockaddr;
691 /**
692     Get the address that made a DNS request.
693  */
694 EVENT2_EXPORT_SYMBOL
695 int evdns_server_request_get_requesting_addr(struct evdns_server_request *req, struct sockaddr *sa, int addr_len);
696 
697 /** Callback for evdns_getaddrinfo. */
698 typedef void (*evdns_getaddrinfo_cb)(int result, struct evutil_addrinfo *res, void *arg);
699 
700 struct evdns_base;
701 struct evdns_getaddrinfo_request;
702 /** Make a non-blocking getaddrinfo request using the dns_base in 'dns_base'.
703  *
704  * If we can answer the request immediately (with an error or not!), then we
705  * invoke cb immediately and return NULL.  Otherwise we return
706  * an evdns_getaddrinfo_request and invoke cb later.
707  *
708  * When the callback is invoked, we pass as its first argument the error code
709  * that getaddrinfo would return (or 0 for no error).  As its second argument,
710  * we pass the evutil_addrinfo structures we found (or NULL on error).  We
711  * pass 'arg' as the third argument.
712  *
713  * Limitations:
714  *
715  * - The AI_V4MAPPED and AI_ALL flags are not currently implemented.
716  * - For ai_socktype, we only handle SOCKTYPE_STREAM, SOCKTYPE_UDP, and 0.
717  * - For ai_protocol, we only handle IPPROTO_TCP, IPPROTO_UDP, and 0.
718  */
719 EVENT2_EXPORT_SYMBOL
720 struct evdns_getaddrinfo_request *evdns_getaddrinfo(
721     struct evdns_base *dns_base,
722     const char *nodename, const char *servname,
723     const struct evutil_addrinfo *hints_in,
724     evdns_getaddrinfo_cb cb, void *arg);
725 
726 /* Cancel an in-progress evdns_getaddrinfo.  This MUST NOT be called after the
727  * getaddrinfo's callback has been invoked.  The resolves will be canceled,
728  * and the callback will be invoked with the error EVUTIL_EAI_CANCEL. */
729 EVENT2_EXPORT_SYMBOL
730 void evdns_getaddrinfo_cancel(struct evdns_getaddrinfo_request *req);
731 
732 /**
733    Retrieve the address of the 'idx'th configured nameserver.
734 
735    @param base The evdns_base to examine.
736    @param idx The index of the nameserver to get the address of.
737    @param sa A location to receive the server's address.
738    @param len The number of bytes available at sa.
739 
740    @return the number of bytes written into sa on success.  On failure, returns
741      -1 if idx is greater than the number of configured nameservers, or a
742      value greater than 'len' if len was not high enough.
743  */
744 EVENT2_EXPORT_SYMBOL
745 int evdns_base_get_nameserver_addr(struct evdns_base *base, int idx,
746     struct sockaddr *sa, ev_socklen_t len);
747 
748 #ifdef __cplusplus
749 }
750 #endif
751 
752 #endif  /* !EVENT2_DNS_H_INCLUDED_ */
753