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