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