4 .\" Copyright (c) 2006 Niels Provos <provos@citi.umich.edu>
5 .\" All rights reserved.
7 .\" Redistribution and use in source and binary forms, with or without
8 .\" modification, are permitted provided that the following conditions
11 .\" 1. Redistributions of source code must retain the above copyright
12 .\" notice, this list of conditions and the following disclaimer.
13 .\" 2. Redistributions in binary form must reproduce the above copyright
14 .\" notice, this list of conditions and the following disclaimer in the
15 .\" documentation and/or other materials provided with the distribution.
16 .\" 3. The name of the author may not be used to endorse or promote products
17 .\" derived from this software without specific prior written permission.
19 .\" THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 .\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
21 .\" AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 .\" THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 .\" EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 .\" PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 .\" OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 .\" WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 .\" OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 .\" ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 .Nm evdns_err_to_string
37 .Nm evdns_nameserver_add
38 .Nm evdns_count_nameservers
39 .Nm evdns_clear_nameservers_and_suspend
41 .Nm evdns_nameserver_ip_add
42 .Nm evdns_resolve_ipv4
43 .Nm evdns_resolve_reverse
44 .Nm evdns_resolv_conf_parse
45 .Nm evdns_config_windows_nameservers
46 .Nm evdns_search_clear
48 .Nm evdns_search_ndots_set
50 .Nd asynchronous functions for DNS resolution.
52 .Fd #include <sys/time.h>
53 .Fd #include <event.h>
54 .Fd #include <evdns.h>
58 .Fn evdns_shutdown "int fail_requests"
60 .Fn evdns_err_to_string "int err"
62 .Fn evdns_nameserver_add "unsigned long int address"
64 .Fn evdns_count_nameservers
66 .Fn evdns_clear_nameservers_and_suspend
70 .Fn evdns_nameserver_ip_add(const char *ip_as_string);
72 .Fn evdns_resolve_ipv4 "const char *name" "int flags" "evdns_callback_type callback" "void *ptr"
74 .Fn evdns_resolve_reverse "struct in_addr *in" "int flags" "evdns_callback_type callback" "void *ptr"
76 .Fn evdns_resolv_conf_parse "int flags" "const char *"
78 .Fn evdns_search_clear
80 .Fn evdns_search_add "const char *domain"
82 .Fn evdns_search_ndots_set "const int ndots"
84 .Fn evdns_set_log_fn "evdns_debug_log_fn_type fn"
86 .Fn evdns_config_windows_nameservers
88 Welcome, gentle reader
90 Async DNS lookups are really a whole lot harder than they should be,
91 mostly stemming from the fact that the libc resolver has never been
92 very good at them. Before you use this library you should see if libc
93 can do the job for you with the modern async call getaddrinfo_a
94 (see http://www.imperialviolet.org/page25.html#e498). Otherwise,
97 This code is based on libevent and you must call event_init before
98 any of the APIs in this file. You must also seed the OpenSSL random
99 source if you are using OpenSSL for ids (see below).
101 This library is designed to be included and shipped with your source
102 code. You statically link with it. You should also test for the
103 existence of strtok_r and define HAVE_STRTOK_R if you have it.
105 The DNS protocol requires a good source of id numbers and these
106 numbers should be unpredictable for spoofing reasons. There are
107 three methods for generating them here and you must define exactly
108 one of them. In increasing order of preference:
110 .Bl -tag -width "DNS_USE_GETTIMEOFDAY_FOR_ID" -compact -offset indent
111 .It DNS_USE_GETTIMEOFDAY_FOR_ID
112 Using the bottom 16 bits of the usec result from gettimeofday. This
113 is a pretty poor solution but should work anywhere.
114 .It DNS_USE_CPU_CLOCK_FOR_ID
115 Using the bottom 16 bits of the nsec result from the CPU's time
116 counter. This is better, but may not work everywhere. Requires
117 POSIX realtime support and you'll need to link against -lrt on
118 glibc systems at least.
119 .It DNS_USE_OPENSSL_FOR_ID
120 Uses the OpenSSL RAND_bytes call to generate the data. You must
121 have seeded the pool before making any calls to this library.
124 The library keeps track of the state of nameservers and will avoid
125 them when they go down. Otherwise it will round robin between them.
129 void callback(int result, char type, int count, int ttl,
130 void *addresses, void *arg);
131 evdns_resolv_conf_parse(DNS_OPTIONS_ALL, "/etc/resolv.conf");
132 evdns_resolve("www.hostname.com", 0, callback, NULL);
134 When the lookup is complete the callback function is called. The
135 first argument will be one of the DNS_ERR_* defines in evdns.h.
136 Hopefully it will be DNS_ERR_NONE, in which case type will be
137 DNS_IPv4_A, count will be the number of IP addresses, ttl is the time
138 which the data can be cached for (in seconds), addresses will point
139 to an array of uint32_t's and arg will be whatever you passed to
144 In order for this library to be a good replacement for glibc's resolver it
145 supports searching. This involves setting a list of default domains, in
146 which names will be queried for. The number of dots in the query name
147 determines the order in which this list is used.
149 Searching appears to be a single lookup from the point of view of the API,
150 although many DNS queries may be generated from a single call to
151 evdns_resolve. Searching can also drastically slow down the resolution
154 To disable searching:
155 .Bl -enum -compact -offset indent
157 Never set it up. If you never call
158 .Fn evdns_resolv_conf_parse,
162 then no searching will occur.
165 .Fn evdns_resolv_conf_parse
167 .Va DNS_OPTION_SEARCH
175 .Va DNS_QUERY_NO_SEARCH
179 The order of searches depends on the number of dots in the name. If the
180 number is greater than the ndots setting then the names is first tried
181 globally. Otherwise each search domain is appended in turn.
183 The ndots setting can either be set from a resolv.conf, or by calling
184 evdns_search_ndots_set.
186 For example, with ndots set to 1 (the default) and a search domain list of
189 Order: www.myhome.net, www.
192 Order: www.abc., www.abc.myhome.net
196 .Bl -tag -width 0123456
197 .It Ft int Fn evdns_init
198 Initializes support for non-blocking name resolution by calling
199 .Fn evdns_resolv_conf_parse
201 .Fn evdns_config_windows_nameservers
203 .It Ft int Fn evdns_nameserver_add "unsigned long int address"
204 Add a nameserver. The address should be an IP address in
205 network byte order. The type of address is chosen so that
206 it matches in_addr.s_addr.
207 Returns non-zero on error.
208 .It Ft int Fn evdns_nameserver_ip_add "const char *ip_as_string"
209 This wraps the above function by parsing a string as an IP
210 address and adds it as a nameserver.
211 Returns non-zero on error
212 .It Ft int Fn evdns_resolve "const char *name" "int flags" "evdns_callback_type callback" "void *ptr"
213 Resolve a name. The name parameter should be a DNS name.
214 The flags parameter should be 0, or DNS_QUERY_NO_SEARCH
215 which disables searching for this query. (see defn of
218 The callback argument is a function which is called when
219 this query completes and ptr is an argument which is passed
220 to that callback function.
222 Returns non-zero on error
223 .It Ft void Fn evdns_search_clear
224 Clears the list of search domains
225 .It Ft void Fn evdns_search_add "const char *domain"
226 Add a domain to the list of search domains
227 .It Ft void Fn evdns_search_ndots_set "int ndots"
228 Set the number of dots which, when found in a name, causes
229 the first query to be without any search domain.
230 .It Ft int Fn evdns_count_nameservers "void"
231 Return the number of configured nameservers (not necessarily the
232 number of running nameservers). This is useful for double-checking
233 whether our calls to the various nameserver configuration functions
234 have been successful.
235 .It Ft int Fn evdns_clear_nameservers_and_suspend "void"
236 Remove all currently configured nameservers, and suspend all pending
237 resolves. Resolves will not necessarily be re-attempted until
238 evdns_resume() is called.
239 .It Ft int Fn evdns_resume "void"
240 Re-attempt resolves left in limbo after an earlier call to
241 evdns_clear_nameservers_and_suspend().
242 .It Ft int Fn evdns_config_windows_nameservers "void"
243 Attempt to configure a set of nameservers based on platform settings on
244 a win32 host. Preferentially tries to use GetNetworkParams; if that fails,
245 looks in the registry. Returns 0 on success, nonzero on failure.
246 .It Ft int Fn evdns_resolv_conf_parse "int flags" "const char *filename"
247 Parse a resolv.conf like file from the given filename.
249 See the man page for resolv.conf for the format of this file.
250 The flags argument determines what information is parsed from
252 .Bl -tag -width "DNS_OPTION_NAMESERVERS" -offset indent -compact -nested
253 .It DNS_OPTION_SEARCH
254 domain, search and ndots options
255 .It DNS_OPTION_NAMESERVERS
258 timeout and attempts options
263 The following directives are not parsed from the file:
264 sortlist, rotate, no-check-names, inet6, debug
266 Returns non-zero on error:
267 .Bl -tag -width "0" -offset indent -compact -nested
283 Requests are kept in two queues. The first is the inflight queue. In
284 this queue requests have an allocated transaction id and nameserver.
285 They will soon be transmitted if they haven't already been.
287 The second is the waiting queue. The size of the inflight ring is
288 limited and all other requests wait in waiting queue for space. This
289 bounds the number of concurrent requests so that we don't flood the
290 nameserver. Several algorithms require a full walk of the inflight
291 queue and so bounding its size keeps thing going nicely under huge
292 (many thousands of requests) loads.
294 If a nameserver loses too many requests it is considered down and we
295 try not to use it. After a while we send a probe to that nameserver
296 (a lookup for google.com) and, if it replies, we consider it working
297 again. If the nameserver fails a probe we wait longer to try again
301 .Xr gethostbyname 3 ,
306 API was developed by Adam Langley on top of the
309 The code was integrate into
311 by Nick Mathewson and finally put into
313 itself by Niels Provos.
317 API and code was written by Adam Langley with significant
318 contributions by Nick Mathewson.
320 This documentation is neither complete nor authoritative.
321 If you are in doubt about the usage of this API then
322 check the source code to find out how it works, write
323 up the missing piece of documentation and send it to
324 me for inclusion in this man page.