1275970Scy/*
2275970Scy * Copyright (c) 2006-2007 Niels Provos <provos@citi.umich.edu>
3275970Scy * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
4275970Scy *
5275970Scy * Redistribution and use in source and binary forms, with or without
6275970Scy * modification, are permitted provided that the following conditions
7275970Scy * are met:
8275970Scy * 1. Redistributions of source code must retain the above copyright
9275970Scy *    notice, this list of conditions and the following disclaimer.
10275970Scy * 2. Redistributions in binary form must reproduce the above copyright
11275970Scy *    notice, this list of conditions and the following disclaimer in the
12275970Scy *    documentation and/or other materials provided with the distribution.
13275970Scy * 3. The name of the author may not be used to endorse or promote products
14275970Scy *    derived from this software without specific prior written permission.
15275970Scy *
16275970Scy * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17275970Scy * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18275970Scy * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19275970Scy * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20275970Scy * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21275970Scy * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22275970Scy * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23275970Scy * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24275970Scy * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25275970Scy * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26275970Scy */
27275970Scy
28275970Scy/*
29275970Scy * The original DNS code is due to Adam Langley with heavy
30275970Scy * modifications by Nick Mathewson.  Adam put his DNS software in the
31275970Scy * public domain.  You can find his original copyright below.  Please,
32275970Scy * aware that the code as part of Libevent is governed by the 3-clause
33275970Scy * BSD license above.
34275970Scy *
35275970Scy * This software is Public Domain. To view a copy of the public domain dedication,
36275970Scy * visit http://creativecommons.org/licenses/publicdomain/ or send a letter to
37275970Scy * Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
38275970Scy *
39275970Scy * I ask and expect, but do not require, that all derivative works contain an
40275970Scy * attribution similar to:
41275970Scy *     Parts developed by Adam Langley <agl@imperialviolet.org>
42275970Scy *
43275970Scy * You may wish to replace the word "Parts" with something else depending on
44275970Scy * the amount of original code.
45275970Scy *
46275970Scy * (Derivative works does not include programs which link against, run or include
47275970Scy * the source verbatim in their source distributions)
48275970Scy */
49275970Scy
50275970Scy/** @file event2/dns.h
51275970Scy *
52275970Scy * Welcome, gentle reader
53275970Scy *
54275970Scy * Async DNS lookups are really a whole lot harder than they should be,
55275970Scy * mostly stemming from the fact that the libc resolver has never been
56275970Scy * very good at them. Before you use this library you should see if libc
57275970Scy * can do the job for you with the modern async call getaddrinfo_a
58275970Scy * (see http://www.imperialviolet.org/page25.html#e498). Otherwise,
59275970Scy * please continue.
60275970Scy *
61275970Scy * The library keeps track of the state of nameservers and will avoid
62275970Scy * them when they go down. Otherwise it will round robin between them.
63275970Scy *
64275970Scy * Quick start guide:
65275970Scy *   #include "evdns.h"
66275970Scy *   void callback(int result, char type, int count, int ttl,
67275970Scy *		 void *addresses, void *arg);
68275970Scy *   evdns_resolv_conf_parse(DNS_OPTIONS_ALL, "/etc/resolv.conf");
69275970Scy *   evdns_resolve("www.hostname.com", 0, callback, NULL);
70275970Scy *
71275970Scy * When the lookup is complete the callback function is called. The
72275970Scy * first argument will be one of the DNS_ERR_* defines in evdns.h.
73275970Scy * Hopefully it will be DNS_ERR_NONE, in which case type will be
74275970Scy * DNS_IPv4_A, count will be the number of IP addresses, ttl is the time
75275970Scy * which the data can be cached for (in seconds), addresses will point
76275970Scy * to an array of uint32_t's and arg will be whatever you passed to
77275970Scy * evdns_resolve.
78275970Scy *
79275970Scy * Searching:
80275970Scy *
81275970Scy * In order for this library to be a good replacement for glibc's resolver it
82275970Scy * supports searching. This involves setting a list of default domains, in
83275970Scy * which names will be queried for. The number of dots in the query name
84275970Scy * determines the order in which this list is used.
85275970Scy *
86275970Scy * Searching appears to be a single lookup from the point of view of the API,
87275970Scy * although many DNS queries may be generated from a single call to
88275970Scy * evdns_resolve. Searching can also drastically slow down the resolution
89275970Scy * of names.
90275970Scy *
91275970Scy * To disable searching:
92275970Scy *   1. Never set it up. If you never call evdns_resolv_conf_parse or
93275970Scy *   evdns_search_add then no searching will occur.
94275970Scy *
95275970Scy *   2. If you do call evdns_resolv_conf_parse then don't pass
96275970Scy *   DNS_OPTION_SEARCH (or DNS_OPTIONS_ALL, which implies it).
97275970Scy *
98275970Scy *   3. When calling evdns_resolve, pass the DNS_QUERY_NO_SEARCH flag.
99275970Scy *
100275970Scy * The order of searches depends on the number of dots in the name. If the
101275970Scy * number is greater than the ndots setting then the names is first tried
102275970Scy * globally. Otherwise each search domain is appended in turn.
103275970Scy *
104275970Scy * The ndots setting can either be set from a resolv.conf, or by calling
105275970Scy * evdns_search_ndots_set.
106275970Scy *
107275970Scy * For example, with ndots set to 1 (the default) and a search domain list of
108275970Scy * ["myhome.net"]:
109275970Scy *  Query: www
110275970Scy *  Order: www.myhome.net, www.
111275970Scy *
112275970Scy *  Query: www.abc
113275970Scy *  Order: www.abc., www.abc.myhome.net
114275970Scy *
115275970Scy * Internals:
116275970Scy *
117275970Scy * Requests are kept in two queues. The first is the inflight queue. In
118275970Scy * this queue requests have an allocated transaction id and nameserver.
119275970Scy * They will soon be transmitted if they haven't already been.
120275970Scy *
121275970Scy * The second is the waiting queue. The size of the inflight ring is
122275970Scy * limited and all other requests wait in waiting queue for space. This
123275970Scy * bounds the number of concurrent requests so that we don't flood the
124275970Scy * nameserver. Several algorithms require a full walk of the inflight
125275970Scy * queue and so bounding its size keeps thing going nicely under huge
126275970Scy * (many thousands of requests) loads.
127275970Scy *
128275970Scy * If a nameserver loses too many requests it is considered down and we
129275970Scy * try not to use it. After a while we send a probe to that nameserver
130275970Scy * (a lookup for google.com) and, if it replies, we consider it working
131275970Scy * again. If the nameserver fails a probe we wait longer to try again
132275970Scy * with the next probe.
133275970Scy */
134275970Scy
135275970Scy#ifndef EVENT2_DNS_H_INCLUDED_
136275970Scy#define EVENT2_DNS_H_INCLUDED_
137275970Scy
138275970Scy#include <event2/visibility.h>
139275970Scy
140275970Scy#ifdef __cplusplus
141275970Scyextern "C" {
142275970Scy#endif
143275970Scy
144275970Scy/* For integer types. */
145275970Scy#include <event2/util.h>
146275970Scy
147275970Scy/** Error codes 0-5 are as described in RFC 1035. */
148275970Scy#define DNS_ERR_NONE 0
149275970Scy/** The name server was unable to interpret the query */
150275970Scy#define DNS_ERR_FORMAT 1
151275970Scy/** The name server was unable to process this query due to a problem with the
152275970Scy * name server */
153275970Scy#define DNS_ERR_SERVERFAILED 2
154275970Scy/** The domain name does not exist */
155275970Scy#define DNS_ERR_NOTEXIST 3
156275970Scy/** The name server does not support the requested kind of query */
157275970Scy#define DNS_ERR_NOTIMPL 4
158275970Scy/** The name server refuses to reform the specified operation for policy
159275970Scy * reasons */
160275970Scy#define DNS_ERR_REFUSED 5
161275970Scy/** The reply was truncated or ill-formatted */
162275970Scy#define DNS_ERR_TRUNCATED 65
163275970Scy/** An unknown error occurred */
164275970Scy#define DNS_ERR_UNKNOWN 66
165275970Scy/** Communication with the server timed out */
166275970Scy#define DNS_ERR_TIMEOUT 67
167275970Scy/** The request was canceled because the DNS subsystem was shut down. */
168275970Scy#define DNS_ERR_SHUTDOWN 68
169275970Scy/** The request was canceled via a call to evdns_cancel_request */
170275970Scy#define DNS_ERR_CANCEL 69
171275970Scy/** There were no answers and no error condition in the DNS packet.
172275970Scy * This can happen when you ask for an address that exists, but a record
173275970Scy * type that doesn't. */
174275970Scy#define DNS_ERR_NODATA 70
175275970Scy
176275970Scy#define DNS_IPv4_A 1
177275970Scy#define DNS_PTR 2
178275970Scy#define DNS_IPv6_AAAA 3
179275970Scy
180275970Scy#define DNS_QUERY_NO_SEARCH 1
181275970Scy
182275970Scy#define DNS_OPTION_SEARCH 1
183275970Scy#define DNS_OPTION_NAMESERVERS 2
184275970Scy#define DNS_OPTION_MISC 4
185275970Scy#define DNS_OPTION_HOSTSFILE 8
186275970Scy#define DNS_OPTIONS_ALL 15
187275970Scy
188275970Scy/* Obsolete name for DNS_QUERY_NO_SEARCH */
189275970Scy#define DNS_NO_SEARCH DNS_QUERY_NO_SEARCH
190275970Scy
191275970Scy/**
192275970Scy * The callback that contains the results from a lookup.
193275970Scy * - result is one of the DNS_ERR_* values (DNS_ERR_NONE for success)
194275970Scy * - type is either DNS_IPv4_A or DNS_PTR or DNS_IPv6_AAAA
195275970Scy * - count contains the number of addresses of form type
196275970Scy * - ttl is the number of seconds the resolution may be cached for.
197275970Scy * - addresses needs to be cast according to type.  It will be an array of
198275970Scy *   4-byte sequences for ipv4, or an array of 16-byte sequences for ipv6,
199275970Scy *   or a nul-terminated string for PTR.
200275970Scy */
201275970Scytypedef void (*evdns_callback_type) (int result, char type, int count, int ttl, void *addresses, void *arg);
202275970Scy
203275970Scystruct evdns_base;
204275970Scystruct event_base;
205275970Scy
206275970Scy/** Flag for evdns_base_new: process resolv.conf.  */
207275970Scy#define EVDNS_BASE_INITIALIZE_NAMESERVERS 1
208275970Scy/** Flag for evdns_base_new: Do not prevent the libevent event loop from
209275970Scy * exiting when we have no active dns requests. */
210275970Scy#define EVDNS_BASE_DISABLE_WHEN_INACTIVE 0x8000
211275970Scy
212275970Scy/**
213275970Scy  Initialize the asynchronous DNS library.
214275970Scy
215275970Scy  This function initializes support for non-blocking name resolution by
216275970Scy  calling evdns_resolv_conf_parse() on UNIX and
217275970Scy  evdns_config_windows_nameservers() on Windows.
218275970Scy
219275970Scy  @param event_base the event base to associate the dns client with
220275970Scy  @param flags any of EVDNS_BASE_INITIALIZE_NAMESERVERS|
221275970Scy    EVDNS_BASE_DISABLE_WHEN_INACTIVE
222275970Scy  @return evdns_base object if successful, or NULL if an error occurred.
223275970Scy  @see evdns_base_free()
224275970Scy */
225275970ScyEVENT2_EXPORT_SYMBOL
226275970Scystruct evdns_base * evdns_base_new(struct event_base *event_base, int initialize_nameservers);
227275970Scy
228275970Scy
229275970Scy/**
230275970Scy  Shut down the asynchronous DNS resolver and terminate all active requests.
231275970Scy
232275970Scy  If the 'fail_requests' option is enabled, all active requests will return
233275970Scy  an empty result with the error flag set to DNS_ERR_SHUTDOWN. Otherwise,
234275970Scy  the requests will be silently discarded.
235275970Scy
236275970Scy  @param evdns_base the evdns base to free
237275970Scy  @param fail_requests if zero, active requests will be aborted; if non-zero,
238275970Scy		active requests will return DNS_ERR_SHUTDOWN.
239275970Scy  @see evdns_base_new()
240275970Scy */
241275970ScyEVENT2_EXPORT_SYMBOL
242275970Scyvoid evdns_base_free(struct evdns_base *base, int fail_requests);
243275970Scy
244275970Scy/**
245275970Scy   Remove all hosts entries that have been loaded into the event_base via
246275970Scy   evdns_base_load_hosts or via event_base_resolv_conf_parse.
247275970Scy
248275970Scy   @param evdns_base the evdns base to remove outdated host addresses from
249275970Scy */
250275970ScyEVENT2_EXPORT_SYMBOL
251275970Scyvoid evdns_base_clear_host_addresses(struct evdns_base *base);
252275970Scy
253275970Scy/**
254275970Scy  Convert a DNS error code to a string.
255275970Scy
256275970Scy  @param err the DNS error code
257275970Scy  @return a string containing an explanation of the error code
258275970Scy*/
259275970ScyEVENT2_EXPORT_SYMBOL
260275970Scyconst char *evdns_err_to_string(int err);
261275970Scy
262275970Scy
263275970Scy/**
264275970Scy  Add a nameserver.
265275970Scy
266275970Scy  The address should be an IPv4 address in network byte order.
267275970Scy  The type of address is chosen so that it matches in_addr.s_addr.
268275970Scy
269275970Scy  @param base the evdns_base to which to add the name server
270275970Scy  @param address an IP address in network byte order
271275970Scy  @return 0 if successful, or -1 if an error occurred
272275970Scy  @see evdns_base_nameserver_ip_add()
273275970Scy */
274275970ScyEVENT2_EXPORT_SYMBOL
275275970Scyint evdns_base_nameserver_add(struct evdns_base *base,
276275970Scy    unsigned long int address);
277275970Scy
278275970Scy/**
279275970Scy  Get the number of configured nameservers.
280275970Scy
281275970Scy  This returns the number of configured nameservers (not necessarily the
282275970Scy  number of running nameservers).  This is useful for double-checking
283275970Scy  whether our calls to the various nameserver configuration functions
284275970Scy  have been successful.
285275970Scy
286275970Scy  @param base the evdns_base to which to apply this operation
287275970Scy  @return the number of configured nameservers
288275970Scy  @see evdns_base_nameserver_add()
289275970Scy */
290275970ScyEVENT2_EXPORT_SYMBOL
291275970Scyint evdns_base_count_nameservers(struct evdns_base *base);
292275970Scy
293275970Scy/**
294275970Scy  Remove all configured nameservers, and suspend all pending resolves.
295275970Scy
296275970Scy  Resolves will not necessarily be re-attempted until evdns_base_resume() is called.
297275970Scy
298275970Scy  @param base the evdns_base to which to apply this operation
299275970Scy  @return 0 if successful, or -1 if an error occurred
300275970Scy  @see evdns_base_resume()
301275970Scy */
302275970ScyEVENT2_EXPORT_SYMBOL
303275970Scyint evdns_base_clear_nameservers_and_suspend(struct evdns_base *base);
304275970Scy
305275970Scy
306275970Scy/**
307275970Scy  Resume normal operation and continue any suspended resolve requests.
308275970Scy
309275970Scy  Re-attempt resolves left in limbo after an earlier call to
310275970Scy  evdns_base_clear_nameservers_and_suspend().
311275970Scy
312275970Scy  @param base the evdns_base to which to apply this operation
313275970Scy  @return 0 if successful, or -1 if an error occurred
314275970Scy  @see evdns_base_clear_nameservers_and_suspend()
315275970Scy */
316275970ScyEVENT2_EXPORT_SYMBOL
317275970Scyint evdns_base_resume(struct evdns_base *base);
318275970Scy
319275970Scy/**
320275970Scy  Add a nameserver by string address.
321275970Scy
322275970Scy  This function parses a n IPv4 or IPv6 address from a string and adds it as a
323275970Scy  nameserver.  It supports the following formats:
324275970Scy  - [IPv6Address]:port
325275970Scy  - [IPv6Address]
326275970Scy  - IPv6Address
327275970Scy  - IPv4Address:port
328275970Scy  - IPv4Address
329275970Scy
330275970Scy  If no port is specified, it defaults to 53.
331275970Scy
332275970Scy  @param base the evdns_base to which to apply this operation
333275970Scy  @return 0 if successful, or -1 if an error occurred
334275970Scy  @see evdns_base_nameserver_add()
335275970Scy */
336275970ScyEVENT2_EXPORT_SYMBOL
337275970Scyint evdns_base_nameserver_ip_add(struct evdns_base *base,
338275970Scy    const char *ip_as_string);
339275970Scy
340275970Scy/**
341275970Scy   Add a nameserver by sockaddr.
342275970Scy **/
343275970ScyEVENT2_EXPORT_SYMBOL
344275970Scyint
345275970Scyevdns_base_nameserver_sockaddr_add(struct evdns_base *base,
346275970Scy    const struct sockaddr *sa, ev_socklen_t len, unsigned flags);
347275970Scy
348275970Scystruct evdns_request;
349275970Scy
350275970Scy/**
351275970Scy  Lookup an A record for a given name.
352275970Scy
353275970Scy  @param base the evdns_base to which to apply this operation
354275970Scy  @param name a DNS hostname
355275970Scy  @param flags either 0, or DNS_QUERY_NO_SEARCH to disable searching for this query.
356275970Scy  @param callback a callback function to invoke when the request is completed
357275970Scy  @param ptr an argument to pass to the callback function
358275970Scy  @return an evdns_request object if successful, or NULL if an error occurred.
359275970Scy  @see evdns_resolve_ipv6(), evdns_resolve_reverse(), evdns_resolve_reverse_ipv6(), evdns_cancel_request()
360275970Scy */
361275970ScyEVENT2_EXPORT_SYMBOL
362275970Scystruct evdns_request *evdns_base_resolve_ipv4(struct evdns_base *base, const char *name, int flags, evdns_callback_type callback, void *ptr);
363275970Scy
364275970Scy/**
365275970Scy  Lookup an AAAA record for a given name.
366275970Scy
367275970Scy  @param base the evdns_base to which to apply this operation
368275970Scy  @param name a DNS hostname
369275970Scy  @param flags either 0, or DNS_QUERY_NO_SEARCH to disable searching for this query.
370275970Scy  @param callback a callback function to invoke when the request is completed
371275970Scy  @param ptr an argument to pass to the callback function
372275970Scy  @return an evdns_request object if successful, or NULL if an error occurred.
373275970Scy  @see evdns_resolve_ipv4(), evdns_resolve_reverse(), evdns_resolve_reverse_ipv6(), evdns_cancel_request()
374275970Scy */
375275970ScyEVENT2_EXPORT_SYMBOL
376275970Scystruct evdns_request *evdns_base_resolve_ipv6(struct evdns_base *base, const char *name, int flags, evdns_callback_type callback, void *ptr);
377275970Scy
378275970Scystruct in_addr;
379275970Scystruct in6_addr;
380275970Scy
381275970Scy/**
382275970Scy  Lookup a PTR record for a given IP address.
383275970Scy
384275970Scy  @param base the evdns_base to which to apply this operation
385275970Scy  @param in an IPv4 address
386275970Scy  @param flags either 0, or DNS_QUERY_NO_SEARCH to disable searching for this query.
387275970Scy  @param callback a callback function to invoke when the request is completed
388275970Scy  @param ptr an argument to pass to the callback function
389275970Scy  @return an evdns_request object if successful, or NULL if an error occurred.
390275970Scy  @see evdns_resolve_reverse_ipv6(), evdns_cancel_request()
391275970Scy */
392275970ScyEVENT2_EXPORT_SYMBOL
393275970Scystruct evdns_request *evdns_base_resolve_reverse(struct evdns_base *base, const struct in_addr *in, int flags, evdns_callback_type callback, void *ptr);
394275970Scy
395275970Scy
396275970Scy/**
397275970Scy  Lookup a PTR record for a given IPv6 address.
398275970Scy
399275970Scy  @param base the evdns_base to which to apply this operation
400275970Scy  @param in an IPv6 address
401275970Scy  @param flags either 0, or DNS_QUERY_NO_SEARCH to disable searching for this query.
402275970Scy  @param callback a callback function to invoke when the request is completed
403275970Scy  @param ptr an argument to pass to the callback function
404275970Scy  @return an evdns_request object if successful, or NULL if an error occurred.
405275970Scy  @see evdns_resolve_reverse_ipv6(), evdns_cancel_request()
406275970Scy */
407275970ScyEVENT2_EXPORT_SYMBOL
408275970Scystruct evdns_request *evdns_base_resolve_reverse_ipv6(struct evdns_base *base, const struct in6_addr *in, int flags, evdns_callback_type callback, void *ptr);
409275970Scy
410275970Scy/**
411275970Scy  Cancels a pending DNS resolution request.
412275970Scy
413275970Scy  @param base the evdns_base that was used to make the request
414275970Scy  @param req the evdns_request that was returned by calling a resolve function
415275970Scy  @see evdns_base_resolve_ipv4(), evdns_base_resolve_ipv6, evdns_base_resolve_reverse
416275970Scy*/
417275970ScyEVENT2_EXPORT_SYMBOL
418275970Scyvoid evdns_cancel_request(struct evdns_base *base, struct evdns_request *req);
419275970Scy
420275970Scy/**
421275970Scy  Set the value of a configuration option.
422275970Scy
423275970Scy  The currently available configuration options are:
424275970Scy
425275970Scy    ndots, timeout, max-timeouts, max-inflight, attempts, randomize-case,
426275970Scy    bind-to, initial-probe-timeout, getaddrinfo-allow-skew.
427275970Scy
428275970Scy  In versions before Libevent 2.0.3-alpha, the option name needed to end with
429275970Scy  a colon.
430275970Scy
431275970Scy  @param base the evdns_base to which to apply this operation
432275970Scy  @param option the name of the configuration option to be modified
433275970Scy  @param val the value to be set
434275970Scy  @return 0 if successful, or -1 if an error occurred
435275970Scy */
436275970ScyEVENT2_EXPORT_SYMBOL
437275970Scyint evdns_base_set_option(struct evdns_base *base, const char *option, const char *val);
438275970Scy
439275970Scy
440275970Scy/**
441275970Scy  Parse a resolv.conf file.
442275970Scy
443275970Scy  The 'flags' parameter determines what information is parsed from the
444275970Scy  resolv.conf file. See the man page for resolv.conf for the format of this
445275970Scy  file.
446275970Scy
447275970Scy  The following directives are not parsed from the file: sortlist, rotate,
448275970Scy  no-check-names, inet6, debug.
449275970Scy
450275970Scy  If this function encounters an error, the possible return values are: 1 =
451275970Scy  failed to open file, 2 = failed to stat file, 3 = file too large, 4 = out of
452275970Scy  memory, 5 = short read from file, 6 = no nameservers listed in the file
453275970Scy
454275970Scy  @param base the evdns_base to which to apply this operation
455275970Scy  @param flags any of DNS_OPTION_NAMESERVERS|DNS_OPTION_SEARCH|DNS_OPTION_MISC|
456290000Sglebius    DNS_OPTION_HOSTSFILE|DNS_OPTIONS_ALL
457275970Scy  @param filename the path to the resolv.conf file
458275970Scy  @return 0 if successful, or various positive error codes if an error
459275970Scy    occurred (see above)
460275970Scy  @see resolv.conf(3), evdns_config_windows_nameservers()
461275970Scy */
462275970ScyEVENT2_EXPORT_SYMBOL
463275970Scyint evdns_base_resolv_conf_parse(struct evdns_base *base, int flags, const char *const filename);
464275970Scy
465275970Scy/**
466275970Scy   Load an /etc/hosts-style file from 'hosts_fname' into 'base'.
467275970Scy
468275970Scy   If hosts_fname is NULL, add minimal entries for localhost, and nothing
469275970Scy   else.
470275970Scy
471275970Scy   Note that only evdns_getaddrinfo uses the /etc/hosts entries.
472275970Scy
473275970Scy   This function does not replace previously loaded hosts entries; to do that,
474275970Scy   call evdns_base_clear_host_addresses first.
475275970Scy
476275970Scy   Return 0 on success, negative on failure.
477275970Scy*/
478275970ScyEVENT2_EXPORT_SYMBOL
479275970Scyint evdns_base_load_hosts(struct evdns_base *base, const char *hosts_fname);
480275970Scy
481275970Scy/**
482275970Scy  Obtain nameserver information using the Windows API.
483275970Scy
484275970Scy  Attempt to configure a set of nameservers based on platform settings on
485275970Scy  a win32 host.  Preferentially tries to use GetNetworkParams; if that fails,
486275970Scy  looks in the registry.
487275970Scy
488275970Scy  @return 0 if successful, or -1 if an error occurred
489275970Scy  @see evdns_resolv_conf_parse()
490275970Scy */
491275970Scy#ifdef _WIN32
492275970ScyEVENT2_EXPORT_SYMBOL
493275970Scyint evdns_base_config_windows_nameservers(struct evdns_base *);
494275970Scy#define EVDNS_BASE_CONFIG_WINDOWS_NAMESERVERS_IMPLEMENTED
495275970Scy#endif
496275970Scy
497275970Scy
498275970Scy/**
499275970Scy  Clear the list of search domains.
500275970Scy */
501275970ScyEVENT2_EXPORT_SYMBOL
502275970Scyvoid evdns_base_search_clear(struct evdns_base *base);
503275970Scy
504275970Scy
505275970Scy/**
506275970Scy  Add a domain to the list of search domains
507275970Scy
508275970Scy  @param domain the domain to be added to the search list
509275970Scy */
510275970ScyEVENT2_EXPORT_SYMBOL
511275970Scyvoid evdns_base_search_add(struct evdns_base *base, const char *domain);
512275970Scy
513275970Scy
514275970Scy/**
515275970Scy  Set the 'ndots' parameter for searches.
516275970Scy
517275970Scy  Sets the number of dots which, when found in a name, causes
518275970Scy  the first query to be without any search domain.
519275970Scy
520275970Scy  @param ndots the new ndots parameter
521275970Scy */
522275970ScyEVENT2_EXPORT_SYMBOL
523275970Scyvoid evdns_base_search_ndots_set(struct evdns_base *base, const int ndots);
524275970Scy
525275970Scy/**
526275970Scy  A callback that is invoked when a log message is generated
527275970Scy
528275970Scy  @param is_warning indicates if the log message is a 'warning'
529275970Scy  @param msg the content of the log message
530275970Scy */
531275970Scytypedef void (*evdns_debug_log_fn_type)(int is_warning, const char *msg);
532275970Scy
533275970Scy
534275970Scy/**
535275970Scy  Set the callback function to handle DNS log messages.  If this
536275970Scy  callback is not set, evdns log messages are handled with the regular
537275970Scy  Libevent logging system.
538275970Scy
539275970Scy  @param fn the callback to be invoked when a log message is generated
540275970Scy */
541275970ScyEVENT2_EXPORT_SYMBOL
542275970Scyvoid evdns_set_log_fn(evdns_debug_log_fn_type fn);
543275970Scy
544275970Scy/**
545275970Scy   Set a callback that will be invoked to generate transaction IDs.  By
546275970Scy   default, we pick transaction IDs based on the current clock time, which
547275970Scy   is bad for security.
548275970Scy
549275970Scy   @param fn the new callback, or NULL to use the default.
550275970Scy
551275970Scy   NOTE: This function has no effect in Libevent 2.0.4-alpha and later,
552275970Scy   since Libevent now provides its own secure RNG.
553275970Scy */
554275970ScyEVENT2_EXPORT_SYMBOL
555275970Scyvoid evdns_set_transaction_id_fn(ev_uint16_t (*fn)(void));
556275970Scy
557275970Scy/**
558275970Scy   Set a callback used to generate random bytes.  By default, we use
559275970Scy   the same function as passed to evdns_set_transaction_id_fn to generate
560275970Scy   bytes two at a time.  If a function is provided here, it's also used
561275970Scy   to generate transaction IDs.
562275970Scy
563275970Scy   NOTE: This function has no effect in Libevent 2.0.4-alpha and later,
564275970Scy   since Libevent now provides its own secure RNG.
565275970Scy*/
566275970ScyEVENT2_EXPORT_SYMBOL
567275970Scyvoid evdns_set_random_bytes_fn(void (*fn)(char *, size_t));
568275970Scy
569275970Scy/*
570275970Scy * Functions used to implement a DNS server.
571275970Scy */
572275970Scy
573275970Scystruct evdns_server_request;
574275970Scystruct evdns_server_question;
575275970Scy
576275970Scy/**
577275970Scy   A callback to implement a DNS server.  The callback function receives a DNS
578275970Scy   request.  It should then optionally add a number of answers to the reply
579275970Scy   using the evdns_server_request_add_*_reply functions, before calling either
580275970Scy   evdns_server_request_respond to send the reply back, or
581275970Scy   evdns_server_request_drop to decline to answer the request.
582275970Scy
583275970Scy   @param req A newly received request
584275970Scy   @param user_data A pointer that was passed to
585275970Scy      evdns_add_server_port_with_base().
586275970Scy */
587275970Scytypedef void (*evdns_request_callback_fn_type)(struct evdns_server_request *, void *);
588275970Scy#define EVDNS_ANSWER_SECTION 0
589275970Scy#define EVDNS_AUTHORITY_SECTION 1
590275970Scy#define EVDNS_ADDITIONAL_SECTION 2
591275970Scy
592275970Scy#define EVDNS_TYPE_A	   1
593275970Scy#define EVDNS_TYPE_NS	   2
594275970Scy#define EVDNS_TYPE_CNAME   5
595275970Scy#define EVDNS_TYPE_SOA	   6
596275970Scy#define EVDNS_TYPE_PTR	  12
597275970Scy#define EVDNS_TYPE_MX	  15
598275970Scy#define EVDNS_TYPE_TXT	  16
599275970Scy#define EVDNS_TYPE_AAAA	  28
600275970Scy
601275970Scy#define EVDNS_QTYPE_AXFR 252
602275970Scy#define EVDNS_QTYPE_ALL	 255
603275970Scy
604275970Scy#define EVDNS_CLASS_INET   1
605275970Scy
606275970Scy/* flags that can be set in answers; as part of the err parameter */
607275970Scy#define EVDNS_FLAGS_AA	0x400
608275970Scy#define EVDNS_FLAGS_RD	0x080
609275970Scy
610275970Scy/** Create a new DNS server port.
611275970Scy
612275970Scy    @param base The event base to handle events for the server port.
613275970Scy    @param socket A UDP socket to accept DNS requests.
614275970Scy    @param flags Always 0 for now.
615275970Scy    @param callback A function to invoke whenever we get a DNS request
616275970Scy      on the socket.
617275970Scy    @param user_data Data to pass to the callback.
618275970Scy    @return an evdns_server_port structure for this server port.
619275970Scy */
620275970ScyEVENT2_EXPORT_SYMBOL
621275970Scystruct 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);
622275970Scy/** Close down a DNS server port, and free associated structures. */
623275970ScyEVENT2_EXPORT_SYMBOL
624275970Scyvoid evdns_close_server_port(struct evdns_server_port *port);
625275970Scy
626275970Scy/** Sets some flags in a reply we're building.
627275970Scy    Allows setting of the AA or RD flags
628275970Scy */
629275970ScyEVENT2_EXPORT_SYMBOL
630275970Scyvoid evdns_server_request_set_flags(struct evdns_server_request *req, int flags);
631275970Scy
632275970Scy/* Functions to add an answer to an in-progress DNS reply.
633275970Scy */
634275970ScyEVENT2_EXPORT_SYMBOL
635275970Scyint 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);
636275970ScyEVENT2_EXPORT_SYMBOL
637275970Scyint evdns_server_request_add_a_reply(struct evdns_server_request *req, const char *name, int n, const void *addrs, int ttl);
638275970ScyEVENT2_EXPORT_SYMBOL
639275970Scyint evdns_server_request_add_aaaa_reply(struct evdns_server_request *req, const char *name, int n, const void *addrs, int ttl);
640275970ScyEVENT2_EXPORT_SYMBOL
641275970Scyint evdns_server_request_add_ptr_reply(struct evdns_server_request *req, struct in_addr *in, const char *inaddr_name, const char *hostname, int ttl);
642275970ScyEVENT2_EXPORT_SYMBOL
643275970Scyint evdns_server_request_add_cname_reply(struct evdns_server_request *req, const char *name, const char *cname, int ttl);
644275970Scy
645275970Scy/**
646275970Scy   Send back a response to a DNS request, and free the request structure.
647275970Scy*/
648275970ScyEVENT2_EXPORT_SYMBOL
649275970Scyint evdns_server_request_respond(struct evdns_server_request *req, int err);
650275970Scy/**
651275970Scy   Free a DNS request without sending back a reply.
652275970Scy*/
653275970ScyEVENT2_EXPORT_SYMBOL
654275970Scyint evdns_server_request_drop(struct evdns_server_request *req);
655275970Scystruct sockaddr;
656275970Scy/**
657275970Scy    Get the address that made a DNS request.
658275970Scy */
659275970ScyEVENT2_EXPORT_SYMBOL
660275970Scyint evdns_server_request_get_requesting_addr(struct evdns_server_request *req, struct sockaddr *sa, int addr_len);
661275970Scy
662275970Scy/** Callback for evdns_getaddrinfo. */
663275970Scytypedef void (*evdns_getaddrinfo_cb)(int result, struct evutil_addrinfo *res, void *arg);
664275970Scy
665275970Scystruct evdns_base;
666275970Scystruct evdns_getaddrinfo_request;
667275970Scy/** Make a non-blocking getaddrinfo request using the dns_base in 'dns_base'.
668275970Scy *
669275970Scy * If we can answer the request immediately (with an error or not!), then we
670275970Scy * invoke cb immediately and return NULL.  Otherwise we return
671275970Scy * an evdns_getaddrinfo_request and invoke cb later.
672275970Scy *
673275970Scy * When the callback is invoked, we pass as its first argument the error code
674275970Scy * that getaddrinfo would return (or 0 for no error).  As its second argument,
675275970Scy * we pass the evutil_addrinfo structures we found (or NULL on error).  We
676275970Scy * pass 'arg' as the third argument.
677275970Scy *
678275970Scy * Limitations:
679275970Scy *
680275970Scy * - The AI_V4MAPPED and AI_ALL flags are not currently implemented.
681275970Scy * - For ai_socktype, we only handle SOCKTYPE_STREAM, SOCKTYPE_UDP, and 0.
682275970Scy * - For ai_protocol, we only handle IPPROTO_TCP, IPPROTO_UDP, and 0.
683275970Scy */
684275970ScyEVENT2_EXPORT_SYMBOL
685275970Scystruct evdns_getaddrinfo_request *evdns_getaddrinfo(
686275970Scy    struct evdns_base *dns_base,
687275970Scy    const char *nodename, const char *servname,
688275970Scy    const struct evutil_addrinfo *hints_in,
689275970Scy    evdns_getaddrinfo_cb cb, void *arg);
690275970Scy
691275970Scy/* Cancel an in-progress evdns_getaddrinfo.  This MUST NOT be called after the
692275970Scy * getaddrinfo's callback has been invoked.  The resolves will be canceled,
693275970Scy * and the callback will be invoked with the error EVUTIL_EAI_CANCEL. */
694275970ScyEVENT2_EXPORT_SYMBOL
695275970Scyvoid evdns_getaddrinfo_cancel(struct evdns_getaddrinfo_request *req);
696275970Scy
697290000Sglebius/**
698290000Sglebius   Retrieve the address of the 'idx'th configured nameserver.
699290000Sglebius
700290000Sglebius   @param base The evdns_base to examine.
701290000Sglebius   @param idx The index of the nameserver to get the address of.
702290000Sglebius   @param sa A location to receive the server's address.
703290000Sglebius   @param len The number of bytes available at sa.
704290000Sglebius
705290000Sglebius   @return the number of bytes written into sa on success.  On failure, returns
706290000Sglebius     -1 if idx is greater than the number of configured nameservers, or a
707290000Sglebius     value greater than 'len' if len was not high enough.
708290000Sglebius */
709290000SglebiusEVENT2_EXPORT_SYMBOL
710290000Sglebiusint evdns_base_get_nameserver_addr(struct evdns_base *base, int idx,
711290000Sglebius    struct sockaddr *sa, ev_socklen_t len);
712290000Sglebius
713275970Scy#ifdef __cplusplus
714275970Scy}
715275970Scy#endif
716275970Scy
717275970Scy#endif  /* !EVENT2_DNS_H_INCLUDED_ */
718