dns.h 27 KB

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