Expand PMF_FN_* macros.
[netbsd-mini2440.git] / external / bsd / libevent / dist / evdns.h
blob8bad9184a68d97b64959142381dddd1d3cdf3681
1 /* $NetBSD$ */
2 /*
3 * Copyright (c) 2006 Niels Provos <provos@citi.umich.edu>
4 * All rights reserved.
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.
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.
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.
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.
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>
44 * You may wish to replace the word "Parts" with something else depending on
45 * the amount of original code.
47 * (Derivative works does not include programs which link against, run or include
48 * the source verbatim in their source distributions)
51 /** @file evdns.h
53 * Welcome, gentle reader
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.
62 * This code is based on libevent and you must call event_init before
63 * any of the APIs in this file. You must also seed the OpenSSL random
64 * source if you are using OpenSSL for ids (see below).
66 * This library is designed to be included and shipped with your source
67 * code. You statically link with it. You should also test for the
68 * existence of strtok_r and define HAVE_STRTOK_R if you have it.
70 * The DNS protocol requires a good source of id numbers and these
71 * numbers should be unpredictable for spoofing reasons. There are
72 * three methods for generating them here and you must define exactly
73 * one of them. In increasing order of preference:
75 * DNS_USE_GETTIMEOFDAY_FOR_ID:
76 * Using the bottom 16 bits of the usec result from gettimeofday. This
77 * is a pretty poor solution but should work anywhere.
78 * DNS_USE_CPU_CLOCK_FOR_ID:
79 * Using the bottom 16 bits of the nsec result from the CPU's time
80 * counter. This is better, but may not work everywhere. Requires
81 * POSIX realtime support and you'll need to link against -lrt on
82 * glibc systems at least.
83 * DNS_USE_OPENSSL_FOR_ID:
84 * Uses the OpenSSL RAND_bytes call to generate the data. You must
85 * have seeded the pool before making any calls to this library.
87 * The library keeps track of the state of nameservers and will avoid
88 * them when they go down. Otherwise it will round robin between them.
90 * Quick start guide:
91 * #include "evdns.h"
92 * void callback(int result, char type, int count, int ttl,
93 * void *addresses, void *arg);
94 * evdns_resolv_conf_parse(DNS_OPTIONS_ALL, "/etc/resolv.conf");
95 * evdns_resolve("www.hostname.com", 0, callback, NULL);
97 * When the lookup is complete the callback function is called. The
98 * first argument will be one of the DNS_ERR_* defines in evdns.h.
99 * Hopefully it will be DNS_ERR_NONE, in which case type will be
100 * DNS_IPv4_A, count will be the number of IP addresses, ttl is the time
101 * which the data can be cached for (in seconds), addresses will point
102 * to an array of uint32_t's and arg will be whatever you passed to
103 * evdns_resolve.
105 * Searching:
107 * In order for this library to be a good replacement for glibc's resolver it
108 * supports searching. This involves setting a list of default domains, in
109 * which names will be queried for. The number of dots in the query name
110 * determines the order in which this list is used.
112 * Searching appears to be a single lookup from the point of view of the API,
113 * although many DNS queries may be generated from a single call to
114 * evdns_resolve. Searching can also drastically slow down the resolution
115 * of names.
117 * To disable searching:
118 * 1. Never set it up. If you never call evdns_resolv_conf_parse or
119 * evdns_search_add then no searching will occur.
121 * 2. If you do call evdns_resolv_conf_parse then don't pass
122 * DNS_OPTION_SEARCH (or DNS_OPTIONS_ALL, which implies it).
124 * 3. When calling evdns_resolve, pass the DNS_QUERY_NO_SEARCH flag.
126 * The order of searches depends on the number of dots in the name. If the
127 * number is greater than the ndots setting then the names is first tried
128 * globally. Otherwise each search domain is appended in turn.
130 * The ndots setting can either be set from a resolv.conf, or by calling
131 * evdns_search_ndots_set.
133 * For example, with ndots set to 1 (the default) and a search domain list of
134 * ["myhome.net"]:
135 * Query: www
136 * Order: www.myhome.net, www.
138 * Query: www.abc
139 * Order: www.abc., www.abc.myhome.net
141 * Internals:
143 * Requests are kept in two queues. The first is the inflight queue. In
144 * this queue requests have an allocated transaction id and nameserver.
145 * They will soon be transmitted if they haven't already been.
147 * The second is the waiting queue. The size of the inflight ring is
148 * limited and all other requests wait in waiting queue for space. This
149 * bounds the number of concurrent requests so that we don't flood the
150 * nameserver. Several algorithms require a full walk of the inflight
151 * queue and so bounding its size keeps thing going nicely under huge
152 * (many thousands of requests) loads.
154 * If a nameserver loses too many requests it is considered down and we
155 * try not to use it. After a while we send a probe to that nameserver
156 * (a lookup for google.com) and, if it replies, we consider it working
157 * again. If the nameserver fails a probe we wait longer to try again
158 * with the next probe.
161 #ifndef EVENTDNS_H
162 #define EVENTDNS_H
164 #ifdef __cplusplus
165 extern "C" {
166 #endif
168 /* For integer types. */
169 #include <evutil.h>
171 /** Error codes 0-5 are as described in RFC 1035. */
172 #define DNS_ERR_NONE 0
173 /** The name server was unable to interpret the query */
174 #define DNS_ERR_FORMAT 1
175 /** The name server was unable to process this query due to a problem with the
176 * name server */
177 #define DNS_ERR_SERVERFAILED 2
178 /** The domain name does not exist */
179 #define DNS_ERR_NOTEXIST 3
180 /** The name server does not support the requested kind of query */
181 #define DNS_ERR_NOTIMPL 4
182 /** The name server refuses to reform the specified operation for policy
183 * reasons */
184 #define DNS_ERR_REFUSED 5
185 /** The reply was truncated or ill-formated */
186 #define DNS_ERR_TRUNCATED 65
187 /** An unknown error occurred */
188 #define DNS_ERR_UNKNOWN 66
189 /** Communication with the server timed out */
190 #define DNS_ERR_TIMEOUT 67
191 /** The request was canceled because the DNS subsystem was shut down. */
192 #define DNS_ERR_SHUTDOWN 68
194 #define DNS_IPv4_A 1
195 #define DNS_PTR 2
196 #define DNS_IPv6_AAAA 3
198 #define DNS_QUERY_NO_SEARCH 1
200 #define DNS_OPTION_SEARCH 1
201 #define DNS_OPTION_NAMESERVERS 2
202 #define DNS_OPTION_MISC 4
203 #define DNS_OPTIONS_ALL 7
206 * The callback that contains the results from a lookup.
207 * - type is either DNS_IPv4_A or DNS_PTR or DNS_IPv6_AAAA
208 * - count contains the number of addresses of form type
209 * - ttl is the number of seconds the resolution may be cached for.
210 * - addresses needs to be cast according to type
212 typedef void (*evdns_callback_type) (int result, char type, int count, int ttl, void *addresses, void *arg);
215 Initialize the asynchronous DNS library.
217 This function initializes support for non-blocking name resolution by
218 calling evdns_resolv_conf_parse() on UNIX and
219 evdns_config_windows_nameservers() on Windows.
221 @return 0 if successful, or -1 if an error occurred
222 @see evdns_shutdown()
224 int evdns_init(void);
228 Shut down the asynchronous DNS resolver and terminate all active requests.
230 If the 'fail_requests' option is enabled, all active requests will return
231 an empty result with the error flag set to DNS_ERR_SHUTDOWN. Otherwise,
232 the requests will be silently discarded.
234 @param fail_requests if zero, active requests will be aborted; if non-zero,
235 active requests will return DNS_ERR_SHUTDOWN.
236 @see evdns_init()
238 void evdns_shutdown(int fail_requests);
242 Convert a DNS error code to a string.
244 @param err the DNS error code
245 @return a string containing an explanation of the error code
247 const char *evdns_err_to_string(int err);
251 Add a nameserver.
253 The address should be an IPv4 address in network byte order.
254 The type of address is chosen so that it matches in_addr.s_addr.
256 @param address an IP address in network byte order
257 @return 0 if successful, or -1 if an error occurred
258 @see evdns_nameserver_ip_add()
260 int evdns_nameserver_add(unsigned long int address);
264 Get the number of configured nameservers.
266 This returns the number of configured nameservers (not necessarily the
267 number of running nameservers). This is useful for double-checking
268 whether our calls to the various nameserver configuration functions
269 have been successful.
271 @return the number of configured nameservers
272 @see evdns_nameserver_add()
274 int evdns_count_nameservers(void);
278 Remove all configured nameservers, and suspend all pending resolves.
280 Resolves will not necessarily be re-attempted until evdns_resume() is called.
282 @return 0 if successful, or -1 if an error occurred
283 @see evdns_resume()
285 int evdns_clear_nameservers_and_suspend(void);
289 Resume normal operation and continue any suspended resolve requests.
291 Re-attempt resolves left in limbo after an earlier call to
292 evdns_clear_nameservers_and_suspend().
294 @return 0 if successful, or -1 if an error occurred
295 @see evdns_clear_nameservers_and_suspend()
297 int evdns_resume(void);
301 Add a nameserver.
303 This wraps the evdns_nameserver_add() function by parsing a string as an IP
304 address and adds it as a nameserver.
306 @return 0 if successful, or -1 if an error occurred
307 @see evdns_nameserver_add()
309 int evdns_nameserver_ip_add(const char *ip_as_string);
313 Lookup an A record for a given name.
315 @param name a DNS hostname
316 @param flags either 0, or DNS_QUERY_NO_SEARCH to disable searching for this query.
317 @param callback a callback function to invoke when the request is completed
318 @param ptr an argument to pass to the callback function
319 @return 0 if successful, or -1 if an error occurred
320 @see evdns_resolve_ipv6(), evdns_resolve_reverse(), evdns_resolve_reverse_ipv6()
322 int evdns_resolve_ipv4(const char *name, int flags, evdns_callback_type callback, void *ptr);
326 Lookup an AAAA record for a given name.
328 @param name a DNS hostname
329 @param flags either 0, or DNS_QUERY_NO_SEARCH to disable searching for this query.
330 @param callback a callback function to invoke when the request is completed
331 @param ptr an argument to pass to the callback function
332 @return 0 if successful, or -1 if an error occurred
333 @see evdns_resolve_ipv4(), evdns_resolve_reverse(), evdns_resolve_reverse_ipv6()
335 int evdns_resolve_ipv6(const char *name, int flags, evdns_callback_type callback, void *ptr);
337 struct in_addr;
338 struct in6_addr;
341 Lookup a PTR record for a given IP address.
343 @param in an IPv4 address
344 @param flags either 0, or DNS_QUERY_NO_SEARCH to disable searching for this query.
345 @param callback a callback function to invoke when the request is completed
346 @param ptr an argument to pass to the callback function
347 @return 0 if successful, or -1 if an error occurred
348 @see evdns_resolve_reverse_ipv6()
350 int evdns_resolve_reverse(const struct in_addr *in, int flags, evdns_callback_type callback, void *ptr);
354 Lookup a PTR record for a given IPv6 address.
356 @param in an IPv6 address
357 @param flags either 0, or DNS_QUERY_NO_SEARCH to disable searching for this query.
358 @param callback a callback function to invoke when the request is completed
359 @param ptr an argument to pass to the callback function
360 @return 0 if successful, or -1 if an error occurred
361 @see evdns_resolve_reverse_ipv6()
363 int evdns_resolve_reverse_ipv6(const struct in6_addr *in, int flags, evdns_callback_type callback, void *ptr);
367 Set the value of a configuration option.
369 The currently available configuration options are:
371 ndots, timeout, max-timeouts, max-inflight, and attempts
373 @param option the name of the configuration option to be modified
374 @param val the value to be set
375 @param flags either 0 | DNS_OPTION_SEARCH | DNS_OPTION_MISC
376 @return 0 if successful, or -1 if an error occurred
378 int evdns_set_option(const char *option, const char *val, int flags);
382 Parse a resolv.conf file.
384 The 'flags' parameter determines what information is parsed from the
385 resolv.conf file. See the man page for resolv.conf for the format of this
386 file.
388 The following directives are not parsed from the file: sortlist, rotate,
389 no-check-names, inet6, debug.
391 If this function encounters an error, the possible return values are: 1 =
392 failed to open file, 2 = failed to stat file, 3 = file too large, 4 = out of
393 memory, 5 = short read from file, 6 = no nameservers listed in the file
395 @param flags any of DNS_OPTION_NAMESERVERS|DNS_OPTION_SEARCH|DNS_OPTION_MISC|
396 DNS_OPTIONS_ALL
397 @param filename the path to the resolv.conf file
398 @return 0 if successful, or various positive error codes if an error
399 occurred (see above)
400 @see resolv.conf(3), evdns_config_windows_nameservers()
402 int evdns_resolv_conf_parse(int flags, const char *const filename);
406 Obtain nameserver information using the Windows API.
408 Attempt to configure a set of nameservers based on platform settings on
409 a win32 host. Preferentially tries to use GetNetworkParams; if that fails,
410 looks in the registry.
412 @return 0 if successful, or -1 if an error occurred
413 @see evdns_resolv_conf_parse()
415 #ifdef WIN32
416 int evdns_config_windows_nameservers(void);
417 #endif
421 Clear the list of search domains.
423 void evdns_search_clear(void);
427 Add a domain to the list of search domains
429 @param domain the domain to be added to the search list
431 void evdns_search_add(const char *domain);
435 Set the 'ndots' parameter for searches.
437 Sets the number of dots which, when found in a name, causes
438 the first query to be without any search domain.
440 @param ndots the new ndots parameter
442 void evdns_search_ndots_set(const int ndots);
445 A callback that is invoked when a log message is generated
447 @param is_warning indicates if the log message is a 'warning'
448 @param msg the content of the log message
450 typedef void (*evdns_debug_log_fn_type)(int is_warning, const char *msg);
454 Set the callback function to handle log messages.
456 @param fn the callback to be invoked when a log message is generated
458 void evdns_set_log_fn(evdns_debug_log_fn_type fn);
461 Set a callback that will be invoked to generate transaction IDs. By
462 default, we pick transaction IDs based on the current clock time.
464 @param fn the new callback, or NULL to use the default.
466 void evdns_set_transaction_id_fn(ev_uint16_t (*fn)(void));
468 #define DNS_NO_SEARCH 1
471 * Structures and functions used to implement a DNS server.
474 struct evdns_server_request {
475 int flags;
476 int nquestions;
477 struct evdns_server_question **questions;
479 struct evdns_server_question {
480 int type;
481 #ifdef __cplusplus
482 int dns_question_class;
483 #else
484 /* You should refer to this field as "dns_question_class". The
485 * name "class" works in C for backward compatibility, and will be
486 * removed in a future version. (1.5 or later). */
487 int class;
488 #define dns_question_class class
489 #endif
490 char name[1];
492 typedef void (*evdns_request_callback_fn_type)(struct evdns_server_request *, void *);
493 #define EVDNS_ANSWER_SECTION 0
494 #define EVDNS_AUTHORITY_SECTION 1
495 #define EVDNS_ADDITIONAL_SECTION 2
497 #define EVDNS_TYPE_A 1
498 #define EVDNS_TYPE_NS 2
499 #define EVDNS_TYPE_CNAME 5
500 #define EVDNS_TYPE_SOA 6
501 #define EVDNS_TYPE_PTR 12
502 #define EVDNS_TYPE_MX 15
503 #define EVDNS_TYPE_TXT 16
504 #define EVDNS_TYPE_AAAA 28
506 #define EVDNS_QTYPE_AXFR 252
507 #define EVDNS_QTYPE_ALL 255
509 #define EVDNS_CLASS_INET 1
511 struct evdns_server_port *evdns_add_server_port(int socket, int is_tcp, evdns_request_callback_fn_type callback, void *user_data);
512 void evdns_close_server_port(struct evdns_server_port *port);
514 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);
515 int evdns_server_request_add_a_reply(struct evdns_server_request *req, const char *name, int n, void *addrs, int ttl);
516 int evdns_server_request_add_aaaa_reply(struct evdns_server_request *req, const char *name, int n, void *addrs, int ttl);
517 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);
518 int evdns_server_request_add_cname_reply(struct evdns_server_request *req, const char *name, const char *cname, int ttl);
520 int evdns_server_request_respond(struct evdns_server_request *req, int err);
521 int evdns_server_request_drop(struct evdns_server_request *req);
522 struct sockaddr;
523 int evdns_server_request_get_requesting_addr(struct evdns_server_request *_req, struct sockaddr *sa, int addr_len);
525 #ifdef __cplusplus
527 #endif
529 #endif /* !EVENTDNS_H */