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