eventdns.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /*
  2. * The original DNS code is due to Adam Langley with heavy
  3. * modifications by Nick Mathewson. Adam put his DNS software in the
  4. * public domain. You can find his original copyright below. Please,
  5. * aware that the code as part of libevent is governed by the 3-clause
  6. * BSD license above.
  7. *
  8. * This software is Public Domain. To view a copy of the public domain dedication,
  9. * visit http://creativecommons.org/licenses/publicdomain/ or send a letter to
  10. * Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
  11. *
  12. * I ask and expect, but do not require, that all derivative works contain an
  13. * attribution similar to:
  14. * Parts developed by Adam Langley <agl@imperialviolet.org>
  15. *
  16. * You may wish to replace the word "Parts" with something else depending on
  17. * the amount of original code.
  18. *
  19. * (Derivative works does not include programs which link against, run or include
  20. * the source verbatim in their source distributions)
  21. */
  22. /*
  23. * Welcome, gentle reader
  24. *
  25. * Async DNS lookups are really a whole lot harder than they should be,
  26. * mostly stemming from the fact that the libc resolver has never been
  27. * very good at them. Before you use this library you should see if libc
  28. * can do the job for you with the modern async call getaddrinfo_a
  29. * (see http://www.imperialviolet.org/page25.html#e498). Otherwise,
  30. * please continue.
  31. *
  32. * This code is based on libevent and you must call event_init before
  33. * any of the APIs in this file. You must also seed the OpenSSL random
  34. * source if you are using OpenSSL for ids (see below).
  35. *
  36. * This library is designed to be included and shipped with your source
  37. * code. You statically link with it. You should also test for the
  38. * existence of strtok_r and define HAVE_STRTOK_R if you have it.
  39. *
  40. * The DNS protocol requires a good source of id numbers and these
  41. * numbers should be unpredictable for spoofing reasons. There are
  42. * three methods for generating them here and you must define exactly
  43. * one of them. In increasing order of preference:
  44. *
  45. * DNS_USE_GETTIMEOFDAY_FOR_ID:
  46. * Using the bottom 16 bits of the usec result from gettimeofday. This
  47. * is a pretty poor solution but should work anywhere.
  48. * DNS_USE_CPU_CLOCK_FOR_ID:
  49. * Using the bottom 16 bits of the nsec result from the CPU's time
  50. * counter. This is better, but may not work everywhere. Requires
  51. * POSIX realtime support and you'll need to link against -lrt on
  52. * glibc systems at least.
  53. * DNS_USE_OPENSSL_FOR_ID:
  54. * Uses the OpenSSL RAND_bytes call to generate the data. You must
  55. * have seeded the pool before making any calls to this library.
  56. *
  57. * The library keeps track of the state of nameservers and will avoid
  58. * them when they go down. Otherwise it will round robin between them.
  59. *
  60. * Quick start guide:
  61. * #include "evdns.h"
  62. * void callback(int result, char type, int count, int ttl,
  63. * void *addresses, void *arg);
  64. * evdns_resolv_conf_parse(DNS_OPTIONS_ALL, "/etc/resolv.conf");
  65. * evdns_resolve("www.hostname.com", 0, callback, NULL);
  66. *
  67. * When the lookup is complete the callback function is called. The
  68. * first argument will be one of the DNS_ERR_* defines in evdns.h.
  69. * Hopefully it will be DNS_ERR_NONE, in which case type will be
  70. * DNS_IPv4_A, count will be the number of IP addresses, ttl is the time
  71. * which the data can be cached for (in seconds), addresses will point
  72. * to an array of uint32_t's and arg will be whatever you passed to
  73. * evdns_resolve.
  74. *
  75. * Searching:
  76. *
  77. * In order for this library to be a good replacement for glibc's resolver it
  78. * supports searching. This involves setting a list of default domains, in
  79. * which names will be queried for. The number of dots in the query name
  80. * determines the order in which this list is used.
  81. *
  82. * Searching appears to be a single lookup from the point of view of the API,
  83. * although many DNS queries may be generated from a single call to
  84. * evdns_resolve. Searching can also drastically slow down the resolution
  85. * of names.
  86. *
  87. * To disable searching:
  88. * 1. Never set it up. If you never call evdns_resolv_conf_parse or
  89. * evdns_search_add then no searching will occur.
  90. *
  91. * 2. If you do call evdns_resolv_conf_parse then don't pass
  92. * DNS_OPTION_SEARCH (or DNS_OPTIONS_ALL, which implies it).
  93. *
  94. * 3. When calling evdns_resolve, pass the DNS_QUERY_NO_SEARCH flag.
  95. *
  96. * The order of searches depends on the number of dots in the name. If the
  97. * number is greater than the ndots setting then the names is first tried
  98. * globally. Otherwise each search domain is appended in turn.
  99. *
  100. * The ndots setting can either be set from a resolv.conf, or by calling
  101. * evdns_search_ndots_set.
  102. *
  103. * For example, with ndots set to 1 (the default) and a search domain list of
  104. * ["myhome.net"]:
  105. * Query: www
  106. * Order: www.myhome.net, www.
  107. *
  108. * Query: www.abc
  109. * Order: www.abc., www.abc.myhome.net
  110. *
  111. * API reference:
  112. *
  113. * int evdns_nameserver_add(uint32_t address)
  114. * Add a nameserver. The address should be an IP address in
  115. * network byte order. The type of address is chosen so that
  116. * it matches in_addr.s_addr.
  117. * Returns non-zero on error.
  118. *
  119. * int evdns_nameserver_ip_add(const char *ip_as_string)
  120. * This wraps the above function by parsing a string as an IP
  121. * address and adds it as a nameserver.
  122. * Returns non-zero on error
  123. *
  124. * int evdns_resolve(const char *name, int flags,
  125. * evdns_callback_type callback,
  126. * void *ptr)
  127. * Resolve a name. The name parameter should be a DNS name.
  128. * The flags parameter should be 0, or DNS_QUERY_NO_SEARCH
  129. * which disables searching for this query. (see defn of
  130. * searching above).
  131. *
  132. * The callback argument is a function which is called when
  133. * this query completes and ptr is an argument which is passed
  134. * to that callback function.
  135. *
  136. * Returns non-zero on error
  137. *
  138. * void evdns_search_clear()
  139. * Clears the list of search domains
  140. *
  141. * void evdns_search_add(const char *domain)
  142. * Add a domain to the list of search domains
  143. *
  144. * void evdns_search_ndots_set(int ndots)
  145. * Set the number of dots which, when found in a name, causes
  146. * the first query to be without any search domain.
  147. *
  148. * int evdns_count_nameservers(void)
  149. * Return the number of configured nameservers (not necessarily the
  150. * number of running nameservers). This is useful for double-checking
  151. * whether our calls to the various nameserver configuration functions
  152. * have been successful.
  153. *
  154. * int evdns_clear_nameservers_and_suspend(void)
  155. * Remove all currently configured nameservers, and suspend all pending
  156. * resolves. Resolves will not necessarily be re-attempted until
  157. * evdns_resume() is called.
  158. *
  159. * int evdns_resume(void)
  160. * Re-attempt resolves left in limbo after an earlier call to
  161. * evdns_clear_nameservers_and_suspend().
  162. *
  163. * int evdns_config_windows_nameservers(void)
  164. * Attempt to configure a set of nameservers based on platform settings on
  165. * a win32 host. Preferentially tries to use GetNetworkParams; if that fails,
  166. * looks in the registry. Returns 0 on success, nonzero on failure.
  167. *
  168. * int evdns_resolv_conf_parse(int flags, const char *filename)
  169. * Parse a resolv.conf like file from the given filename.
  170. *
  171. * See the man page for resolv.conf for the format of this file.
  172. * The flags argument determines what information is parsed from
  173. * this file:
  174. * DNS_OPTION_SEARCH - domain, search and ndots options
  175. * DNS_OPTION_NAMESERVERS - nameserver lines
  176. * DNS_OPTION_MISC - timeout and attempts options
  177. * DNS_OPTIONS_ALL - all of the above
  178. * The following directives are not parsed from the file:
  179. * sortlist, rotate, no-check-names, inet6, debug
  180. *
  181. * Returns non-zero on error:
  182. * 0 no errors
  183. * 1 failed to open file
  184. * 2 failed to stat file
  185. * 3 file too large
  186. * 4 out of memory
  187. * 5 short read from file
  188. * 6 no nameservers in file
  189. *
  190. * Internals:
  191. *
  192. * Requests are kept in two queues. The first is the inflight queue. In
  193. * this queue requests have an allocated transaction id and nameserver.
  194. * They will soon be transmitted if they haven't already been.
  195. *
  196. * The second is the waiting queue. The size of the inflight ring is
  197. * limited and all other requests wait in waiting queue for space. This
  198. * bounds the number of concurrent requests so that we don't flood the
  199. * nameserver. Several algorithms require a full walk of the inflight
  200. * queue and so bounding its size keeps thing going nicely under huge
  201. * (many thousands of requests) loads.
  202. *
  203. * If a nameserver loses too many requests it is considered down and we
  204. * try not to use it. After a while we send a probe to that nameserver
  205. * (a lookup for google.com) and, if it replies, we consider it working
  206. * again. If the nameserver fails a probe we wait longer to try again
  207. * with the next probe.
  208. */
  209. #ifndef TOR_EVENTDNS_H
  210. #define TOR_EVENTDNS_H
  211. /* Error codes 0-5 are as described in RFC 1035. */
  212. #define DNS_ERR_NONE 0
  213. /* The name server was unable to interpret the query */
  214. #define DNS_ERR_FORMAT 1
  215. /* The name server was unable to process this query due to a problem with the
  216. * name server */
  217. #define DNS_ERR_SERVERFAILED 2
  218. /* The domain name does not exist */
  219. #define DNS_ERR_NOTEXIST 3
  220. /* The name server does not support the requested kind of query */
  221. #define DNS_ERR_NOTIMPL 4
  222. /* The name server refuses to reform the specified operation for policy
  223. * reasons */
  224. #define DNS_ERR_REFUSED 5
  225. /* The reply was truncated or ill-formated */
  226. #define DNS_ERR_TRUNCATED 65
  227. /* An unknown error occurred */
  228. #define DNS_ERR_UNKNOWN 66
  229. /* Communication with the server timed out */
  230. #define DNS_ERR_TIMEOUT 67
  231. /* The request was canceled because the DNS subsystem was shut down. */
  232. #define DNS_ERR_SHUTDOWN 68
  233. #define DNS_IPv4_A 1
  234. #define DNS_PTR 2
  235. #define DNS_IPv6_AAAA 3
  236. #define DNS_QUERY_NO_SEARCH 1
  237. #define DNS_OPTION_SEARCH 1
  238. #define DNS_OPTION_NAMESERVERS 2
  239. #define DNS_OPTION_MISC 4
  240. #define DNS_OPTIONS_ALL 7
  241. /*
  242. * The callback that contains the results from a lookup.
  243. * - type is either DNS_IPv4_A or DNS_IPv6_AAAA or DNS_PTR
  244. * - count contains the number of addresses of form type
  245. * - ttl is the number of seconds the resolution may be cached for.
  246. * - addresses needs to be cast according to type
  247. */
  248. typedef void (*evdns_callback_type) (int result, char type, int count, int ttl, void *addresses, void *arg);
  249. int evdns_init(void);
  250. void evdns_shutdown(int fail_requests);
  251. const char *evdns_err_to_string(int err);
  252. int evdns_nameserver_add(uint32_t address);
  253. int evdns_count_nameservers(void);
  254. int evdns_clear_nameservers_and_suspend(void);
  255. int evdns_resume(void);
  256. int evdns_nameserver_ip_add(const char *ip_as_string);
  257. int evdns_nameserver_sockaddr_add(const struct sockaddr *sa, socklen_t len);
  258. void evdns_set_default_outgoing_bind_address(const struct sockaddr *addr, socklen_t addrlen);
  259. int evdns_resolve_ipv4(const char *name, int flags, evdns_callback_type callback, void *ptr);
  260. int evdns_resolve_ipv6(const char *name, int flags, evdns_callback_type callback, void *ptr);
  261. struct in_addr;
  262. struct in6_addr;
  263. int evdns_resolve_reverse(const struct in_addr *in, int flags, evdns_callback_type callback, void *ptr);
  264. int evdns_resolve_reverse_ipv6(const struct in6_addr *in, int flags, evdns_callback_type callback, void *ptr);
  265. int evdns_set_option(const char *option, const char *val, int flags);
  266. int evdns_resolv_conf_parse(int flags, const char *);
  267. #ifdef _WIN32
  268. int evdns_config_windows_nameservers(void);
  269. #endif
  270. void evdns_search_clear(void);
  271. void evdns_search_add(const char *domain);
  272. void evdns_search_ndots_set(const int ndots);
  273. typedef void (*evdns_debug_log_fn_type)(int is_warning, const char *msg);
  274. void evdns_set_log_fn(evdns_debug_log_fn_type fn);
  275. void evdns_set_transaction_id_fn(uint16_t (*fn)(void));
  276. void evdns_set_random_bytes_fn(void (*fn)(char *, size_t));
  277. #define DNS_NO_SEARCH 1
  278. /* Structures and functions used to implement a DNS server. */
  279. struct evdns_server_request {
  280. int flags;
  281. int nquestions;
  282. struct evdns_server_question **questions;
  283. };
  284. struct evdns_server_question {
  285. int type;
  286. int dns_question_class;
  287. char name[1];
  288. };
  289. typedef void (*evdns_request_callback_fn_type)(struct evdns_server_request *, void *);
  290. #define EVDNS_ANSWER_SECTION 0
  291. #define EVDNS_AUTHORITY_SECTION 1
  292. #define EVDNS_ADDITIONAL_SECTION 2
  293. #define EVDNS_TYPE_A 1
  294. #define EVDNS_TYPE_NS 2
  295. #define EVDNS_TYPE_CNAME 5
  296. #define EVDNS_TYPE_SOA 6
  297. #define EVDNS_TYPE_PTR 12
  298. #define EVDNS_TYPE_MX 15
  299. #define EVDNS_TYPE_TXT 16
  300. #define EVDNS_TYPE_AAAA 28
  301. #define EVDNS_QTYPE_AXFR 252
  302. #define EVDNS_QTYPE_ALL 255
  303. #define EVDNS_CLASS_INET 1
  304. struct evdns_server_port *evdns_add_server_port(tor_socket_t socket, int is_tcp, evdns_request_callback_fn_type callback, void *user_data);
  305. void evdns_close_server_port(struct evdns_server_port *port);
  306. int evdns_server_request_add_reply(struct evdns_server_request *req, int section, const char *name, int type, int class, int ttl, int datalen, int is_name, const char *data);
  307. int evdns_server_request_add_a_reply(struct evdns_server_request *req, const char *name, int n, const void *addrs, int ttl);
  308. int evdns_server_request_add_aaaa_reply(struct evdns_server_request *req, const char *name, int n, const void *addrs, int ttl);
  309. 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);
  310. int evdns_server_request_add_cname_reply(struct evdns_server_request *req, const char *name, const char *cname, int ttl);
  311. struct sockaddr;
  312. int evdns_server_request_get_requesting_addr(struct evdns_server_request *req, struct sockaddr *sa, int addr_len);
  313. int evdns_server_request_respond(struct evdns_server_request *req, int err);
  314. int evdns_server_request_drop(struct evdns_server_request *req);
  315. #endif // !EVENTDNS_H