1 /* $NetBSD: dns.h,v 1.7 2024/08/18 20:47:22 christos Exp $ */ 2 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_INCLUDED_ 138 #define EVENT2_DNS_H_INCLUDED_ 139 140 #include <event2/visibility.h> 141 142 #ifdef __cplusplus 143 extern "C" { 144 #endif 145 146 /* For integer types. */ 147 #include <event2/util.h> 148 149 /** Error codes 0-5 are as described in RFC 1035. */ 150 #define DNS_ERR_NONE 0 151 /** The name server was unable to interpret the query */ 152 #define DNS_ERR_FORMAT 1 153 /** The name server was unable to process this query due to a problem with the 154 * name server */ 155 #define DNS_ERR_SERVERFAILED 2 156 /** The domain name does not exist */ 157 #define DNS_ERR_NOTEXIST 3 158 /** The name server does not support the requested kind of query */ 159 #define DNS_ERR_NOTIMPL 4 160 /** The name server refuses to reform the specified operation for policy 161 * reasons */ 162 #define DNS_ERR_REFUSED 5 163 /** The reply was truncated or ill-formatted */ 164 #define DNS_ERR_TRUNCATED 65 165 /** An unknown error occurred */ 166 #define DNS_ERR_UNKNOWN 66 167 /** Communication with the server timed out */ 168 #define DNS_ERR_TIMEOUT 67 169 /** The request was canceled because the DNS subsystem was shut down. */ 170 #define DNS_ERR_SHUTDOWN 68 171 /** The request was canceled via a call to evdns_cancel_request */ 172 #define DNS_ERR_CANCEL 69 173 /** There were no answers and no error condition in the DNS packet. 174 * This can happen when you ask for an address that exists, but a record 175 * type that doesn't. */ 176 #define DNS_ERR_NODATA 70 177 178 #define DNS_IPv4_A 1 179 #define DNS_PTR 2 180 #define DNS_IPv6_AAAA 3 181 182 #define DNS_QUERY_NO_SEARCH 1 183 184 /* Allow searching */ 185 #define DNS_OPTION_SEARCH 1 186 /* Parse "nameserver" and add default if no such section */ 187 #define DNS_OPTION_NAMESERVERS 2 188 /* Parse additional options like: 189 * - timeout: 190 * - getaddrinfo-allow-skew: 191 * - max-timeouts: 192 * - max-inflight: 193 * - attempts: 194 * - randomize-case: 195 * - initial-probe-timeout: 196 */ 197 #define DNS_OPTION_MISC 4 198 /* Load hosts file (i.e. "/etc/hosts") */ 199 #define DNS_OPTION_HOSTSFILE 8 200 /** 201 * All above: 202 * - DNS_OPTION_SEARCH 203 * - DNS_OPTION_NAMESERVERS 204 * - DNS_OPTION_MISC 205 * - DNS_OPTION_HOSTSFILE 206 */ 207 #define DNS_OPTIONS_ALL ( \ 208 DNS_OPTION_SEARCH | \ 209 DNS_OPTION_NAMESERVERS | \ 210 DNS_OPTION_MISC | \ 211 DNS_OPTION_HOSTSFILE | \ 212 0 \ 213 ) 214 /* Do not "default" nameserver (i.e. "127.0.0.1:53") if there is no nameservers 215 * in resolv.conf, (iff DNS_OPTION_NAMESERVERS is set) */ 216 #define DNS_OPTION_NAMESERVERS_NO_DEFAULT 16 217 218 /* Obsolete name for DNS_QUERY_NO_SEARCH */ 219 #define DNS_NO_SEARCH DNS_QUERY_NO_SEARCH 220 221 /** 222 * The callback that contains the results from a lookup. 223 * - result is one of the DNS_ERR_* values (DNS_ERR_NONE for success) 224 * - type is either DNS_IPv4_A or DNS_PTR or DNS_IPv6_AAAA 225 * - count contains the number of addresses of form type 226 * - ttl is the number of seconds the resolution may be cached for. 227 * - addresses needs to be cast according to type. It will be an array of 228 * 4-byte sequences for ipv4, or an array of 16-byte sequences for ipv6, 229 * or a nul-terminated string for PTR. 230 */ 231 typedef void (*evdns_callback_type) (int result, char type, int count, int ttl, void *addresses, void *arg); 232 233 struct evdns_base; 234 struct event_base; 235 236 /** Flag for evdns_base_new: process resolv.conf. */ 237 #define EVDNS_BASE_INITIALIZE_NAMESERVERS 1 238 /** Flag for evdns_base_new: Do not prevent the libevent event loop from 239 * exiting when we have no active dns requests. */ 240 #define EVDNS_BASE_DISABLE_WHEN_INACTIVE 0x8000 241 /** Flag for evdns_base_new: If EVDNS_BASE_INITIALIZE_NAMESERVERS isset, do not 242 * add default nameserver if there are no nameservers in resolv.conf 243 * @see DNS_OPTION_NAMESERVERS_NO_DEFAULT */ 244 #define EVDNS_BASE_NAMESERVERS_NO_DEFAULT 0x10000 245 246 /** 247 Initialize the asynchronous DNS library. 248 249 This function initializes support for non-blocking name resolution by 250 calling evdns_resolv_conf_parse() on UNIX and 251 evdns_config_windows_nameservers() on Windows. 252 253 @param event_base the event base to associate the dns client with 254 @param flags any of EVDNS_BASE_INITIALIZE_NAMESERVERS| 255 EVDNS_BASE_DISABLE_WHEN_INACTIVE|EVDNS_BASE_NAMESERVERS_NO_DEFAULT 256 @return evdns_base object if successful, or NULL if an error occurred. 257 @see evdns_base_free() 258 */ 259 EVENT2_EXPORT_SYMBOL 260 struct evdns_base * evdns_base_new(struct event_base *event_base, int initialize_nameservers); 261 262 263 /** 264 Shut down the asynchronous DNS resolver and terminate all active requests. 265 266 If the 'fail_requests' option is enabled, all active requests will return 267 an empty result with the error flag set to DNS_ERR_SHUTDOWN. Otherwise, 268 the requests will be silently discarded. 269 270 @param evdns_base the evdns base to free 271 @param fail_requests if zero, active requests will be aborted; if non-zero, 272 active requests will return DNS_ERR_SHUTDOWN. 273 @see evdns_base_new() 274 */ 275 EVENT2_EXPORT_SYMBOL 276 void evdns_base_free(struct evdns_base *base, int fail_requests); 277 278 /** 279 Remove all hosts entries that have been loaded into the event_base via 280 evdns_base_load_hosts or via event_base_resolv_conf_parse. 281 282 @param evdns_base the evdns base to remove outdated host addresses from 283 */ 284 EVENT2_EXPORT_SYMBOL 285 void evdns_base_clear_host_addresses(struct evdns_base *base); 286 287 /** 288 Convert a DNS error code to a string. 289 290 @param err the DNS error code 291 @return a string containing an explanation of the error code 292 */ 293 EVENT2_EXPORT_SYMBOL 294 const char *evdns_err_to_string(int err); 295 296 297 /** 298 Add a nameserver. 299 300 The address should be an IPv4 address in network byte order. 301 The type of address is chosen so that it matches in_addr.s_addr. 302 303 @param base the evdns_base to which to add the name server 304 @param address an IP address in network byte order 305 @return 0 if successful, or -1 if an error occurred 306 @see evdns_base_nameserver_ip_add() 307 */ 308 EVENT2_EXPORT_SYMBOL 309 int evdns_base_nameserver_add(struct evdns_base *base, 310 unsigned long int address); 311 312 /** 313 Get the number of configured nameservers. 314 315 This returns the number of configured nameservers (not necessarily the 316 number of running nameservers). This is useful for double-checking 317 whether our calls to the various nameserver configuration functions 318 have been successful. 319 320 @param base the evdns_base to which to apply this operation 321 @return the number of configured nameservers 322 @see evdns_base_nameserver_add() 323 */ 324 EVENT2_EXPORT_SYMBOL 325 int evdns_base_count_nameservers(struct evdns_base *base); 326 327 /** 328 Remove all configured nameservers, and suspend all pending resolves. 329 330 Resolves will not necessarily be re-attempted until evdns_base_resume() is called. 331 332 @param base the evdns_base to which to apply this operation 333 @return 0 if successful, or -1 if an error occurred 334 @see evdns_base_resume() 335 */ 336 EVENT2_EXPORT_SYMBOL 337 int evdns_base_clear_nameservers_and_suspend(struct evdns_base *base); 338 339 340 /** 341 Resume normal operation and continue any suspended resolve requests. 342 343 Re-attempt resolves left in limbo after an earlier call to 344 evdns_base_clear_nameservers_and_suspend(). 345 346 @param base the evdns_base to which to apply this operation 347 @return 0 if successful, or -1 if an error occurred 348 @see evdns_base_clear_nameservers_and_suspend() 349 */ 350 EVENT2_EXPORT_SYMBOL 351 int evdns_base_resume(struct evdns_base *base); 352 353 /** 354 Add a nameserver by string address. 355 356 This function parses a n IPv4 or IPv6 address from a string and adds it as a 357 nameserver. It supports the following formats: 358 - [IPv6Address]:port 359 - [IPv6Address] 360 - IPv6Address 361 - IPv4Address:port 362 - IPv4Address 363 364 If no port is specified, it defaults to 53. 365 366 @param base the evdns_base to which to apply this operation 367 @return 0 if successful, or -1 if an error occurred 368 @see evdns_base_nameserver_add() 369 */ 370 EVENT2_EXPORT_SYMBOL 371 int evdns_base_nameserver_ip_add(struct evdns_base *base, 372 const char *ip_as_string); 373 374 /** 375 Add a nameserver by sockaddr. 376 **/ 377 EVENT2_EXPORT_SYMBOL 378 int 379 evdns_base_nameserver_sockaddr_add(struct evdns_base *base, 380 const struct sockaddr *sa, ev_socklen_t len, unsigned flags); 381 382 struct evdns_request; 383 384 /** 385 Lookup an A record for a given name. 386 387 @param base the evdns_base to which to apply this operation 388 @param name a DNS hostname 389 @param flags either 0, or DNS_QUERY_NO_SEARCH to disable searching for this query. 390 @param callback a callback function to invoke when the request is completed 391 @param ptr an argument to pass to the callback function 392 @return an evdns_request object if successful, or NULL if an error occurred. 393 @see evdns_resolve_ipv6(), evdns_resolve_reverse(), evdns_resolve_reverse_ipv6(), evdns_cancel_request() 394 */ 395 EVENT2_EXPORT_SYMBOL 396 struct evdns_request *evdns_base_resolve_ipv4(struct evdns_base *base, const char *name, int flags, evdns_callback_type callback, void *ptr); 397 398 /** 399 Lookup an AAAA record for a given name. 400 401 @param base the evdns_base to which to apply this operation 402 @param name a DNS hostname 403 @param flags either 0, or DNS_QUERY_NO_SEARCH to disable searching for this query. 404 @param callback a callback function to invoke when the request is completed 405 @param ptr an argument to pass to the callback function 406 @return an evdns_request object if successful, or NULL if an error occurred. 407 @see evdns_resolve_ipv4(), evdns_resolve_reverse(), evdns_resolve_reverse_ipv6(), evdns_cancel_request() 408 */ 409 EVENT2_EXPORT_SYMBOL 410 struct evdns_request *evdns_base_resolve_ipv6(struct evdns_base *base, const char *name, int flags, evdns_callback_type callback, void *ptr); 411 412 struct in_addr; 413 struct in6_addr; 414 415 /** 416 Lookup a PTR record for a given IP address. 417 418 @param base the evdns_base to which to apply this operation 419 @param in an IPv4 address 420 @param flags either 0, or DNS_QUERY_NO_SEARCH to disable searching for this query. 421 @param callback a callback function to invoke when the request is completed 422 @param ptr an argument to pass to the callback function 423 @return an evdns_request object if successful, or NULL if an error occurred. 424 @see evdns_resolve_reverse_ipv6(), evdns_cancel_request() 425 */ 426 EVENT2_EXPORT_SYMBOL 427 struct evdns_request *evdns_base_resolve_reverse(struct evdns_base *base, const struct in_addr *in, int flags, evdns_callback_type callback, void *ptr); 428 429 430 /** 431 Lookup a PTR record for a given IPv6 address. 432 433 @param base the evdns_base to which to apply this operation 434 @param in an IPv6 address 435 @param flags either 0, or DNS_QUERY_NO_SEARCH to disable searching for this query. 436 @param callback a callback function to invoke when the request is completed 437 @param ptr an argument to pass to the callback function 438 @return an evdns_request object if successful, or NULL if an error occurred. 439 @see evdns_resolve_reverse_ipv6(), evdns_cancel_request() 440 */ 441 EVENT2_EXPORT_SYMBOL 442 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); 443 444 /** 445 Cancels a pending DNS resolution request. 446 447 @param base the evdns_base that was used to make the request 448 @param req the evdns_request that was returned by calling a resolve function 449 @see evdns_base_resolve_ipv4(), evdns_base_resolve_ipv6, evdns_base_resolve_reverse 450 */ 451 EVENT2_EXPORT_SYMBOL 452 void evdns_cancel_request(struct evdns_base *base, struct evdns_request *req); 453 454 /** 455 Set the value of a configuration option. 456 457 The currently available configuration options are: 458 459 ndots, timeout, max-timeouts, max-inflight, attempts, randomize-case, 460 bind-to, initial-probe-timeout, getaddrinfo-allow-skew, 461 so-rcvbuf, so-sndbuf. 462 463 In versions before Libevent 2.0.3-alpha, the option name needed to end with 464 a colon. 465 466 @param base the evdns_base to which to apply this operation 467 @param option the name of the configuration option to be modified 468 @param val the value to be set 469 @return 0 if successful, or -1 if an error occurred 470 */ 471 EVENT2_EXPORT_SYMBOL 472 int evdns_base_set_option(struct evdns_base *base, const char *option, const char *val); 473 474 475 /** 476 Parse a resolv.conf file. 477 478 The 'flags' parameter determines what information is parsed from the 479 resolv.conf file. See the man page for resolv.conf for the format of this 480 file. 481 482 The following directives are not parsed from the file: sortlist, rotate, 483 no-check-names, inet6, debug. 484 485 If this function encounters an error, the possible return values are: 1 = 486 failed to open file, 2 = failed to stat file, 3 = file too large, 4 = out of 487 memory, 5 = short read from file, 6 = no nameservers listed in the file 488 489 @param base the evdns_base to which to apply this operation 490 @param flags any of DNS_OPTION_NAMESERVERS|DNS_OPTION_SEARCH|DNS_OPTION_MISC| 491 DNS_OPTION_HOSTSFILE|DNS_OPTIONS_ALL|DNS_OPTION_NAMESERVERS_NO_DEFAULT 492 @param filename the path to the resolv.conf file 493 @return 0 if successful, or various positive error codes if an error 494 occurred (see above) 495 @see resolv.conf(3), evdns_config_windows_nameservers() 496 */ 497 EVENT2_EXPORT_SYMBOL 498 int evdns_base_resolv_conf_parse(struct evdns_base *base, int flags, const char *const filename); 499 500 /** 501 Load an /etc/hosts-style file from 'hosts_fname' into 'base'. 502 503 If hosts_fname is NULL, add minimal entries for localhost, and nothing 504 else. 505 506 Note that only evdns_getaddrinfo uses the /etc/hosts entries. 507 508 This function does not replace previously loaded hosts entries; to do that, 509 call evdns_base_clear_host_addresses first. 510 511 Return 0 on success, negative on failure. 512 */ 513 EVENT2_EXPORT_SYMBOL 514 int evdns_base_load_hosts(struct evdns_base *base, const char *hosts_fname); 515 516 #if defined(EVENT_IN_DOXYGEN_) || defined(_WIN32) 517 /** 518 Obtain nameserver information using the Windows API. 519 520 Attempt to configure a set of nameservers based on platform settings on 521 a win32 host. Preferentially tries to use GetNetworkParams; if that fails, 522 looks in the registry. 523 524 @return 0 if successful, or -1 if an error occurred 525 @see evdns_resolv_conf_parse() 526 */ 527 EVENT2_EXPORT_SYMBOL 528 int evdns_base_config_windows_nameservers(struct evdns_base *); 529 #define EVDNS_BASE_CONFIG_WINDOWS_NAMESERVERS_IMPLEMENTED 530 #endif 531 532 533 /** 534 Clear the list of search domains. 535 */ 536 EVENT2_EXPORT_SYMBOL 537 void evdns_base_search_clear(struct evdns_base *base); 538 539 540 /** 541 Add a domain to the list of search domains 542 543 @param domain the domain to be added to the search list 544 */ 545 EVENT2_EXPORT_SYMBOL 546 void evdns_base_search_add(struct evdns_base *base, const char *domain); 547 548 549 /** 550 Set the 'ndots' parameter for searches. 551 552 Sets the number of dots which, when found in a name, causes 553 the first query to be without any search domain. 554 555 @param ndots the new ndots parameter 556 */ 557 EVENT2_EXPORT_SYMBOL 558 void evdns_base_search_ndots_set(struct evdns_base *base, const int ndots); 559 560 /** 561 A callback that is invoked when a log message is generated 562 563 @param is_warning indicates if the log message is a 'warning' 564 @param msg the content of the log message 565 */ 566 typedef void (*evdns_debug_log_fn_type)(int is_warning, const char *msg); 567 568 569 /** 570 Set the callback function to handle DNS log messages. If this 571 callback is not set, evdns log messages are handled with the regular 572 Libevent logging system. 573 574 @param fn the callback to be invoked when a log message is generated 575 */ 576 EVENT2_EXPORT_SYMBOL 577 void evdns_set_log_fn(evdns_debug_log_fn_type fn); 578 579 /** 580 Set a callback that will be invoked to generate transaction IDs. By 581 default, we pick transaction IDs based on the current clock time, which 582 is bad for security. 583 584 @param fn the new callback, or NULL to use the default. 585 586 NOTE: This function has no effect in Libevent 2.0.4-alpha and later, 587 since Libevent now provides its own secure RNG. 588 */ 589 EVENT2_EXPORT_SYMBOL 590 void evdns_set_transaction_id_fn(ev_uint16_t (*fn)(void)); 591 592 /** 593 Set a callback used to generate random bytes. By default, we use 594 the same function as passed to evdns_set_transaction_id_fn to generate 595 bytes two at a time. If a function is provided here, it's also used 596 to generate transaction IDs. 597 598 NOTE: This function has no effect in Libevent 2.0.4-alpha and later, 599 since Libevent now provides its own secure RNG. 600 */ 601 EVENT2_EXPORT_SYMBOL 602 void evdns_set_random_bytes_fn(void (*fn)(char *, size_t)); 603 604 /* 605 * Functions used to implement a DNS server. 606 */ 607 608 struct evdns_server_request; 609 struct evdns_server_question; 610 611 /** 612 A callback to implement a DNS server. The callback function receives a DNS 613 request. It should then optionally add a number of answers to the reply 614 using the evdns_server_request_add_*_reply functions, before calling either 615 evdns_server_request_respond to send the reply back, or 616 evdns_server_request_drop to decline to answer the request. 617 618 @param req A newly received request 619 @param user_data A pointer that was passed to 620 evdns_add_server_port_with_base(). 621 */ 622 typedef void (*evdns_request_callback_fn_type)(struct evdns_server_request *, void *); 623 #define EVDNS_ANSWER_SECTION 0 624 #define EVDNS_AUTHORITY_SECTION 1 625 #define EVDNS_ADDITIONAL_SECTION 2 626 627 #define EVDNS_TYPE_A 1 628 #define EVDNS_TYPE_NS 2 629 #define EVDNS_TYPE_CNAME 5 630 #define EVDNS_TYPE_SOA 6 631 #define EVDNS_TYPE_PTR 12 632 #define EVDNS_TYPE_MX 15 633 #define EVDNS_TYPE_TXT 16 634 #define EVDNS_TYPE_AAAA 28 635 636 #define EVDNS_QTYPE_AXFR 252 637 #define EVDNS_QTYPE_ALL 255 638 639 #define EVDNS_CLASS_INET 1 640 641 /* flags that can be set in answers; as part of the err parameter */ 642 #define EVDNS_FLAGS_AA 0x400 643 #define EVDNS_FLAGS_RD 0x080 644 645 /** Create a new DNS server port. 646 647 @param base The event base to handle events for the server port. 648 @param socket A UDP socket to accept DNS requests. 649 @param flags Always 0 for now. 650 @param callback A function to invoke whenever we get a DNS request 651 on the socket. 652 @param user_data Data to pass to the callback. 653 @return an evdns_server_port structure for this server port or NULL if 654 an error occurred. 655 */ 656 EVENT2_EXPORT_SYMBOL 657 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); 658 /** Close down a DNS server port, and free associated structures. */ 659 EVENT2_EXPORT_SYMBOL 660 void evdns_close_server_port(struct evdns_server_port *port); 661 662 /** Sets some flags in a reply we're building. 663 Allows setting of the AA or RD flags 664 */ 665 EVENT2_EXPORT_SYMBOL 666 void evdns_server_request_set_flags(struct evdns_server_request *req, int flags); 667 668 /* Functions to add an answer to an in-progress DNS reply. 669 */ 670 EVENT2_EXPORT_SYMBOL 671 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); 672 EVENT2_EXPORT_SYMBOL 673 int evdns_server_request_add_a_reply(struct evdns_server_request *req, const char *name, int n, const void *addrs, int ttl); 674 EVENT2_EXPORT_SYMBOL 675 int evdns_server_request_add_aaaa_reply(struct evdns_server_request *req, const char *name, int n, const void *addrs, int ttl); 676 EVENT2_EXPORT_SYMBOL 677 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); 678 EVENT2_EXPORT_SYMBOL 679 int evdns_server_request_add_cname_reply(struct evdns_server_request *req, const char *name, const char *cname, int ttl); 680 681 /** 682 Send back a response to a DNS request, and free the request structure. 683 */ 684 EVENT2_EXPORT_SYMBOL 685 int evdns_server_request_respond(struct evdns_server_request *req, int err); 686 /** 687 Free a DNS request without sending back a reply. 688 */ 689 EVENT2_EXPORT_SYMBOL 690 int evdns_server_request_drop(struct evdns_server_request *req); 691 struct sockaddr; 692 /** 693 Get the address that made a DNS request. 694 */ 695 EVENT2_EXPORT_SYMBOL 696 int evdns_server_request_get_requesting_addr(struct evdns_server_request *req, struct sockaddr *sa, int addr_len); 697 698 /** Callback for evdns_getaddrinfo. */ 699 typedef void (*evdns_getaddrinfo_cb)(int result, struct evutil_addrinfo *res, void *arg); 700 701 struct evdns_base; 702 struct evdns_getaddrinfo_request; 703 /** Make a non-blocking getaddrinfo request using the dns_base in 'dns_base'. 704 * 705 * If we can answer the request immediately (with an error or not!), then we 706 * invoke cb immediately and return NULL. Otherwise we return 707 * an evdns_getaddrinfo_request and invoke cb later. 708 * 709 * When the callback is invoked, we pass as its first argument the error code 710 * that getaddrinfo would return (or 0 for no error). As its second argument, 711 * we pass the evutil_addrinfo structures we found (or NULL on error). We 712 * pass 'arg' as the third argument. 713 * 714 * Limitations: 715 * 716 * - The AI_V4MAPPED and AI_ALL flags are not currently implemented. 717 * - For ai_socktype, we only handle SOCKTYPE_STREAM, SOCKTYPE_UDP, and 0. 718 * - For ai_protocol, we only handle IPPROTO_TCP, IPPROTO_UDP, and 0. 719 */ 720 EVENT2_EXPORT_SYMBOL 721 struct evdns_getaddrinfo_request *evdns_getaddrinfo( 722 struct evdns_base *dns_base, 723 const char *nodename, const char *servname, 724 const struct evutil_addrinfo *hints_in, 725 evdns_getaddrinfo_cb cb, void *arg); 726 727 /* Cancel an in-progress evdns_getaddrinfo. This MUST NOT be called after the 728 * getaddrinfo's callback has been invoked. The resolves will be canceled, 729 * and the callback will be invoked with the error EVUTIL_EAI_CANCEL. */ 730 EVENT2_EXPORT_SYMBOL 731 void evdns_getaddrinfo_cancel(struct evdns_getaddrinfo_request *req); 732 733 /** 734 Retrieve the address of the 'idx'th configured nameserver. 735 736 @param base The evdns_base to examine. 737 @param idx The index of the nameserver to get the address of. 738 @param sa A location to receive the server's address. 739 @param len The number of bytes available at sa. 740 741 @return the number of bytes written into sa on success. On failure, returns 742 -1 if idx is greater than the number of configured nameservers, or a 743 value greater than 'len' if len was not high enough. 744 */ 745 EVENT2_EXPORT_SYMBOL 746 int evdns_base_get_nameserver_addr(struct evdns_base *base, int idx, 747 struct sockaddr *sa, ev_socklen_t len); 748 749 #ifdef __cplusplus 750 } 751 #endif 752 753 #endif /* !EVENT2_DNS_H_INCLUDED_ */ 754