1*657871a7Schristos /* $NetBSD: dns.h,v 1.1.1.4 2021/04/07 02:43:14 christos Exp $ */ 26ecf6635Schristos /* 36ecf6635Schristos * Copyright (c) 2006-2007 Niels Provos <provos@citi.umich.edu> 46ecf6635Schristos * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson 56ecf6635Schristos * 66ecf6635Schristos * Redistribution and use in source and binary forms, with or without 76ecf6635Schristos * modification, are permitted provided that the following conditions 86ecf6635Schristos * are met: 96ecf6635Schristos * 1. Redistributions of source code must retain the above copyright 106ecf6635Schristos * notice, this list of conditions and the following disclaimer. 116ecf6635Schristos * 2. Redistributions in binary form must reproduce the above copyright 126ecf6635Schristos * notice, this list of conditions and the following disclaimer in the 136ecf6635Schristos * documentation and/or other materials provided with the distribution. 146ecf6635Schristos * 3. The name of the author may not be used to endorse or promote products 156ecf6635Schristos * derived from this software without specific prior written permission. 166ecf6635Schristos * 176ecf6635Schristos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 186ecf6635Schristos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 196ecf6635Schristos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 206ecf6635Schristos * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 216ecf6635Schristos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 226ecf6635Schristos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 236ecf6635Schristos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 246ecf6635Schristos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 256ecf6635Schristos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 266ecf6635Schristos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 276ecf6635Schristos */ 286ecf6635Schristos 296ecf6635Schristos /* 306ecf6635Schristos * The original DNS code is due to Adam Langley with heavy 316ecf6635Schristos * modifications by Nick Mathewson. Adam put his DNS software in the 326ecf6635Schristos * public domain. You can find his original copyright below. Please, 336ecf6635Schristos * aware that the code as part of Libevent is governed by the 3-clause 346ecf6635Schristos * BSD license above. 356ecf6635Schristos * 366ecf6635Schristos * This software is Public Domain. To view a copy of the public domain dedication, 376ecf6635Schristos * visit http://creativecommons.org/licenses/publicdomain/ or send a letter to 386ecf6635Schristos * Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA. 396ecf6635Schristos * 406ecf6635Schristos * I ask and expect, but do not require, that all derivative works contain an 416ecf6635Schristos * attribution similar to: 426ecf6635Schristos * Parts developed by Adam Langley <agl@imperialviolet.org> 436ecf6635Schristos * 446ecf6635Schristos * You may wish to replace the word "Parts" with something else depending on 456ecf6635Schristos * the amount of original code. 466ecf6635Schristos * 476ecf6635Schristos * (Derivative works does not include programs which link against, run or include 486ecf6635Schristos * the source verbatim in their source distributions) 496ecf6635Schristos */ 506ecf6635Schristos 516ecf6635Schristos /** @file event2/dns.h 526ecf6635Schristos * 536ecf6635Schristos * Welcome, gentle reader 546ecf6635Schristos * 556ecf6635Schristos * Async DNS lookups are really a whole lot harder than they should be, 566ecf6635Schristos * mostly stemming from the fact that the libc resolver has never been 576ecf6635Schristos * very good at them. Before you use this library you should see if libc 586ecf6635Schristos * can do the job for you with the modern async call getaddrinfo_a 596ecf6635Schristos * (see http://www.imperialviolet.org/page25.html#e498). Otherwise, 606ecf6635Schristos * please continue. 616ecf6635Schristos * 626ecf6635Schristos * The library keeps track of the state of nameservers and will avoid 636ecf6635Schristos * them when they go down. Otherwise it will round robin between them. 646ecf6635Schristos * 656ecf6635Schristos * Quick start guide: 666ecf6635Schristos * #include "evdns.h" 676ecf6635Schristos * void callback(int result, char type, int count, int ttl, 686ecf6635Schristos * void *addresses, void *arg); 696ecf6635Schristos * evdns_resolv_conf_parse(DNS_OPTIONS_ALL, "/etc/resolv.conf"); 706ecf6635Schristos * evdns_resolve("www.hostname.com", 0, callback, NULL); 716ecf6635Schristos * 726ecf6635Schristos * When the lookup is complete the callback function is called. The 736ecf6635Schristos * first argument will be one of the DNS_ERR_* defines in evdns.h. 746ecf6635Schristos * Hopefully it will be DNS_ERR_NONE, in which case type will be 756ecf6635Schristos * DNS_IPv4_A, count will be the number of IP addresses, ttl is the time 766ecf6635Schristos * which the data can be cached for (in seconds), addresses will point 776ecf6635Schristos * to an array of uint32_t's and arg will be whatever you passed to 786ecf6635Schristos * evdns_resolve. 796ecf6635Schristos * 806ecf6635Schristos * Searching: 816ecf6635Schristos * 826ecf6635Schristos * In order for this library to be a good replacement for glibc's resolver it 836ecf6635Schristos * supports searching. This involves setting a list of default domains, in 846ecf6635Schristos * which names will be queried for. The number of dots in the query name 856ecf6635Schristos * determines the order in which this list is used. 866ecf6635Schristos * 876ecf6635Schristos * Searching appears to be a single lookup from the point of view of the API, 886ecf6635Schristos * although many DNS queries may be generated from a single call to 896ecf6635Schristos * evdns_resolve. Searching can also drastically slow down the resolution 906ecf6635Schristos * of names. 916ecf6635Schristos * 926ecf6635Schristos * To disable searching: 936ecf6635Schristos * 1. Never set it up. If you never call evdns_resolv_conf_parse or 946ecf6635Schristos * evdns_search_add then no searching will occur. 956ecf6635Schristos * 966ecf6635Schristos * 2. If you do call evdns_resolv_conf_parse then don't pass 976ecf6635Schristos * DNS_OPTION_SEARCH (or DNS_OPTIONS_ALL, which implies it). 986ecf6635Schristos * 996ecf6635Schristos * 3. When calling evdns_resolve, pass the DNS_QUERY_NO_SEARCH flag. 1006ecf6635Schristos * 1016ecf6635Schristos * The order of searches depends on the number of dots in the name. If the 1026ecf6635Schristos * number is greater than the ndots setting then the names is first tried 1036ecf6635Schristos * globally. Otherwise each search domain is appended in turn. 1046ecf6635Schristos * 1056ecf6635Schristos * The ndots setting can either be set from a resolv.conf, or by calling 1066ecf6635Schristos * evdns_search_ndots_set. 1076ecf6635Schristos * 1086ecf6635Schristos * For example, with ndots set to 1 (the default) and a search domain list of 1096ecf6635Schristos * ["myhome.net"]: 1106ecf6635Schristos * Query: www 1116ecf6635Schristos * Order: www.myhome.net, www. 1126ecf6635Schristos * 1136ecf6635Schristos * Query: www.abc 1146ecf6635Schristos * Order: www.abc., www.abc.myhome.net 1156ecf6635Schristos * 1166ecf6635Schristos * Internals: 1176ecf6635Schristos * 1186ecf6635Schristos * Requests are kept in two queues. The first is the inflight queue. In 1196ecf6635Schristos * this queue requests have an allocated transaction id and nameserver. 1206ecf6635Schristos * They will soon be transmitted if they haven't already been. 1216ecf6635Schristos * 1226ecf6635Schristos * The second is the waiting queue. The size of the inflight ring is 1236ecf6635Schristos * limited and all other requests wait in waiting queue for space. This 1246ecf6635Schristos * bounds the number of concurrent requests so that we don't flood the 1256ecf6635Schristos * nameserver. Several algorithms require a full walk of the inflight 1266ecf6635Schristos * queue and so bounding its size keeps thing going nicely under huge 1276ecf6635Schristos * (many thousands of requests) loads. 1286ecf6635Schristos * 1296ecf6635Schristos * If a nameserver loses too many requests it is considered down and we 1306ecf6635Schristos * try not to use it. After a while we send a probe to that nameserver 1316ecf6635Schristos * (a lookup for google.com) and, if it replies, we consider it working 1326ecf6635Schristos * again. If the nameserver fails a probe we wait longer to try again 1336ecf6635Schristos * with the next probe. 1346ecf6635Schristos */ 1356ecf6635Schristos 136805a1ce9Schristos #ifndef EVENT2_DNS_H_INCLUDED_ 137805a1ce9Schristos #define EVENT2_DNS_H_INCLUDED_ 138805a1ce9Schristos 139805a1ce9Schristos #include <event2/visibility.h> 1406ecf6635Schristos 1416ecf6635Schristos #ifdef __cplusplus 1426ecf6635Schristos extern "C" { 1436ecf6635Schristos #endif 1446ecf6635Schristos 1456ecf6635Schristos /* For integer types. */ 1466ecf6635Schristos #include <event2/util.h> 1476ecf6635Schristos 1486ecf6635Schristos /** Error codes 0-5 are as described in RFC 1035. */ 1496ecf6635Schristos #define DNS_ERR_NONE 0 1506ecf6635Schristos /** The name server was unable to interpret the query */ 1516ecf6635Schristos #define DNS_ERR_FORMAT 1 1526ecf6635Schristos /** The name server was unable to process this query due to a problem with the 1536ecf6635Schristos * name server */ 1546ecf6635Schristos #define DNS_ERR_SERVERFAILED 2 1556ecf6635Schristos /** The domain name does not exist */ 1566ecf6635Schristos #define DNS_ERR_NOTEXIST 3 1576ecf6635Schristos /** The name server does not support the requested kind of query */ 1586ecf6635Schristos #define DNS_ERR_NOTIMPL 4 1596ecf6635Schristos /** The name server refuses to reform the specified operation for policy 1606ecf6635Schristos * reasons */ 1616ecf6635Schristos #define DNS_ERR_REFUSED 5 1626ecf6635Schristos /** The reply was truncated or ill-formatted */ 1636ecf6635Schristos #define DNS_ERR_TRUNCATED 65 1646ecf6635Schristos /** An unknown error occurred */ 1656ecf6635Schristos #define DNS_ERR_UNKNOWN 66 1666ecf6635Schristos /** Communication with the server timed out */ 1676ecf6635Schristos #define DNS_ERR_TIMEOUT 67 1686ecf6635Schristos /** The request was canceled because the DNS subsystem was shut down. */ 1696ecf6635Schristos #define DNS_ERR_SHUTDOWN 68 1706ecf6635Schristos /** The request was canceled via a call to evdns_cancel_request */ 1716ecf6635Schristos #define DNS_ERR_CANCEL 69 1726ecf6635Schristos /** There were no answers and no error condition in the DNS packet. 1736ecf6635Schristos * This can happen when you ask for an address that exists, but a record 1746ecf6635Schristos * type that doesn't. */ 1756ecf6635Schristos #define DNS_ERR_NODATA 70 1766ecf6635Schristos 1776ecf6635Schristos #define DNS_IPv4_A 1 1786ecf6635Schristos #define DNS_PTR 2 1796ecf6635Schristos #define DNS_IPv6_AAAA 3 1806ecf6635Schristos 1816ecf6635Schristos #define DNS_QUERY_NO_SEARCH 1 1826ecf6635Schristos 183*657871a7Schristos /* Allow searching */ 1846ecf6635Schristos #define DNS_OPTION_SEARCH 1 185*657871a7Schristos /* Parse "nameserver" and add default if no such section */ 1866ecf6635Schristos #define DNS_OPTION_NAMESERVERS 2 187*657871a7Schristos /* Parse additional options like: 188*657871a7Schristos * - timeout: 189*657871a7Schristos * - getaddrinfo-allow-skew: 190*657871a7Schristos * - max-timeouts: 191*657871a7Schristos * - max-inflight: 192*657871a7Schristos * - attempts: 193*657871a7Schristos * - randomize-case: 194*657871a7Schristos * - initial-probe-timeout: 195*657871a7Schristos */ 1966ecf6635Schristos #define DNS_OPTION_MISC 4 197*657871a7Schristos /* Load hosts file (i.e. "/etc/hosts") */ 1986ecf6635Schristos #define DNS_OPTION_HOSTSFILE 8 199*657871a7Schristos /** 200*657871a7Schristos * All above: 201*657871a7Schristos * - DNS_OPTION_SEARCH 202*657871a7Schristos * - DNS_OPTION_NAMESERVERS 203*657871a7Schristos * - DNS_OPTION_MISC 204*657871a7Schristos * - DNS_OPTION_HOSTSFILE 205*657871a7Schristos */ 206*657871a7Schristos #define DNS_OPTIONS_ALL ( \ 207*657871a7Schristos DNS_OPTION_SEARCH | \ 208*657871a7Schristos DNS_OPTION_NAMESERVERS | \ 209*657871a7Schristos DNS_OPTION_MISC | \ 210*657871a7Schristos DNS_OPTION_HOSTSFILE | \ 211*657871a7Schristos 0 \ 212*657871a7Schristos ) 213*657871a7Schristos /* Do not "default" nameserver (i.e. "127.0.0.1:53") if there is no nameservers 214*657871a7Schristos * in resolv.conf, (iff DNS_OPTION_NAMESERVERS is set) */ 215*657871a7Schristos #define DNS_OPTION_NAMESERVERS_NO_DEFAULT 16 2166ecf6635Schristos 2176ecf6635Schristos /* Obsolete name for DNS_QUERY_NO_SEARCH */ 2186ecf6635Schristos #define DNS_NO_SEARCH DNS_QUERY_NO_SEARCH 2196ecf6635Schristos 2206ecf6635Schristos /** 2216ecf6635Schristos * The callback that contains the results from a lookup. 2226ecf6635Schristos * - result is one of the DNS_ERR_* values (DNS_ERR_NONE for success) 2236ecf6635Schristos * - type is either DNS_IPv4_A or DNS_PTR or DNS_IPv6_AAAA 2246ecf6635Schristos * - count contains the number of addresses of form type 2256ecf6635Schristos * - ttl is the number of seconds the resolution may be cached for. 2266ecf6635Schristos * - addresses needs to be cast according to type. It will be an array of 2276ecf6635Schristos * 4-byte sequences for ipv4, or an array of 16-byte sequences for ipv6, 2286ecf6635Schristos * or a nul-terminated string for PTR. 2296ecf6635Schristos */ 2306ecf6635Schristos typedef void (*evdns_callback_type) (int result, char type, int count, int ttl, void *addresses, void *arg); 2316ecf6635Schristos 2326ecf6635Schristos struct evdns_base; 2336ecf6635Schristos struct event_base; 2346ecf6635Schristos 235805a1ce9Schristos /** Flag for evdns_base_new: process resolv.conf. */ 236805a1ce9Schristos #define EVDNS_BASE_INITIALIZE_NAMESERVERS 1 237805a1ce9Schristos /** Flag for evdns_base_new: Do not prevent the libevent event loop from 238805a1ce9Schristos * exiting when we have no active dns requests. */ 239805a1ce9Schristos #define EVDNS_BASE_DISABLE_WHEN_INACTIVE 0x8000 240*657871a7Schristos /** Flag for evdns_base_new: If EVDNS_BASE_INITIALIZE_NAMESERVERS isset, do not 241*657871a7Schristos * add default nameserver if there are no nameservers in resolv.conf 242*657871a7Schristos * @see DNS_OPTION_NAMESERVERS_NO_DEFAULT */ 243*657871a7Schristos #define EVDNS_BASE_NAMESERVERS_NO_DEFAULT 0x10000 244805a1ce9Schristos 2456ecf6635Schristos /** 2466ecf6635Schristos Initialize the asynchronous DNS library. 2476ecf6635Schristos 2486ecf6635Schristos This function initializes support for non-blocking name resolution by 2496ecf6635Schristos calling evdns_resolv_conf_parse() on UNIX and 2506ecf6635Schristos evdns_config_windows_nameservers() on Windows. 2516ecf6635Schristos 2526ecf6635Schristos @param event_base the event base to associate the dns client with 253805a1ce9Schristos @param flags any of EVDNS_BASE_INITIALIZE_NAMESERVERS| 254*657871a7Schristos EVDNS_BASE_DISABLE_WHEN_INACTIVE|EVDNS_BASE_NAMESERVERS_NO_DEFAULT 2556ecf6635Schristos @return evdns_base object if successful, or NULL if an error occurred. 2566ecf6635Schristos @see evdns_base_free() 2576ecf6635Schristos */ 258805a1ce9Schristos EVENT2_EXPORT_SYMBOL 2596ecf6635Schristos struct evdns_base * evdns_base_new(struct event_base *event_base, int initialize_nameservers); 2606ecf6635Schristos 2616ecf6635Schristos 2626ecf6635Schristos /** 2636ecf6635Schristos Shut down the asynchronous DNS resolver and terminate all active requests. 2646ecf6635Schristos 2656ecf6635Schristos If the 'fail_requests' option is enabled, all active requests will return 2666ecf6635Schristos an empty result with the error flag set to DNS_ERR_SHUTDOWN. Otherwise, 2676ecf6635Schristos the requests will be silently discarded. 2686ecf6635Schristos 2696ecf6635Schristos @param evdns_base the evdns base to free 2706ecf6635Schristos @param fail_requests if zero, active requests will be aborted; if non-zero, 2716ecf6635Schristos active requests will return DNS_ERR_SHUTDOWN. 2726ecf6635Schristos @see evdns_base_new() 2736ecf6635Schristos */ 274805a1ce9Schristos EVENT2_EXPORT_SYMBOL 2756ecf6635Schristos void evdns_base_free(struct evdns_base *base, int fail_requests); 2766ecf6635Schristos 2776ecf6635Schristos /** 278805a1ce9Schristos Remove all hosts entries that have been loaded into the event_base via 279805a1ce9Schristos evdns_base_load_hosts or via event_base_resolv_conf_parse. 280805a1ce9Schristos 281805a1ce9Schristos @param evdns_base the evdns base to remove outdated host addresses from 282805a1ce9Schristos */ 283805a1ce9Schristos EVENT2_EXPORT_SYMBOL 284805a1ce9Schristos void evdns_base_clear_host_addresses(struct evdns_base *base); 285805a1ce9Schristos 286805a1ce9Schristos /** 2876ecf6635Schristos Convert a DNS error code to a string. 2886ecf6635Schristos 2896ecf6635Schristos @param err the DNS error code 2906ecf6635Schristos @return a string containing an explanation of the error code 2916ecf6635Schristos */ 292805a1ce9Schristos EVENT2_EXPORT_SYMBOL 2936ecf6635Schristos const char *evdns_err_to_string(int err); 2946ecf6635Schristos 2956ecf6635Schristos 2966ecf6635Schristos /** 2976ecf6635Schristos Add a nameserver. 2986ecf6635Schristos 2996ecf6635Schristos The address should be an IPv4 address in network byte order. 3006ecf6635Schristos The type of address is chosen so that it matches in_addr.s_addr. 3016ecf6635Schristos 3026ecf6635Schristos @param base the evdns_base to which to add the name server 3036ecf6635Schristos @param address an IP address in network byte order 3046ecf6635Schristos @return 0 if successful, or -1 if an error occurred 3056ecf6635Schristos @see evdns_base_nameserver_ip_add() 3066ecf6635Schristos */ 307805a1ce9Schristos EVENT2_EXPORT_SYMBOL 3086ecf6635Schristos int evdns_base_nameserver_add(struct evdns_base *base, 3096ecf6635Schristos unsigned long int address); 3106ecf6635Schristos 3116ecf6635Schristos /** 3126ecf6635Schristos Get the number of configured nameservers. 3136ecf6635Schristos 3146ecf6635Schristos This returns the number of configured nameservers (not necessarily the 3156ecf6635Schristos number of running nameservers). This is useful for double-checking 3166ecf6635Schristos whether our calls to the various nameserver configuration functions 3176ecf6635Schristos have been successful. 3186ecf6635Schristos 3196ecf6635Schristos @param base the evdns_base to which to apply this operation 3206ecf6635Schristos @return the number of configured nameservers 3216ecf6635Schristos @see evdns_base_nameserver_add() 3226ecf6635Schristos */ 323805a1ce9Schristos EVENT2_EXPORT_SYMBOL 3246ecf6635Schristos int evdns_base_count_nameservers(struct evdns_base *base); 3256ecf6635Schristos 3266ecf6635Schristos /** 3276ecf6635Schristos Remove all configured nameservers, and suspend all pending resolves. 3286ecf6635Schristos 3296ecf6635Schristos Resolves will not necessarily be re-attempted until evdns_base_resume() is called. 3306ecf6635Schristos 3316ecf6635Schristos @param base the evdns_base to which to apply this operation 3326ecf6635Schristos @return 0 if successful, or -1 if an error occurred 3336ecf6635Schristos @see evdns_base_resume() 3346ecf6635Schristos */ 335805a1ce9Schristos EVENT2_EXPORT_SYMBOL 3366ecf6635Schristos int evdns_base_clear_nameservers_and_suspend(struct evdns_base *base); 3376ecf6635Schristos 3386ecf6635Schristos 3396ecf6635Schristos /** 3406ecf6635Schristos Resume normal operation and continue any suspended resolve requests. 3416ecf6635Schristos 3426ecf6635Schristos Re-attempt resolves left in limbo after an earlier call to 3436ecf6635Schristos evdns_base_clear_nameservers_and_suspend(). 3446ecf6635Schristos 3456ecf6635Schristos @param base the evdns_base to which to apply this operation 3466ecf6635Schristos @return 0 if successful, or -1 if an error occurred 3476ecf6635Schristos @see evdns_base_clear_nameservers_and_suspend() 3486ecf6635Schristos */ 349805a1ce9Schristos EVENT2_EXPORT_SYMBOL 3506ecf6635Schristos int evdns_base_resume(struct evdns_base *base); 3516ecf6635Schristos 3526ecf6635Schristos /** 3536ecf6635Schristos Add a nameserver by string address. 3546ecf6635Schristos 3556ecf6635Schristos This function parses a n IPv4 or IPv6 address from a string and adds it as a 3566ecf6635Schristos nameserver. It supports the following formats: 3576ecf6635Schristos - [IPv6Address]:port 3586ecf6635Schristos - [IPv6Address] 3596ecf6635Schristos - IPv6Address 3606ecf6635Schristos - IPv4Address:port 3616ecf6635Schristos - IPv4Address 3626ecf6635Schristos 3636ecf6635Schristos If no port is specified, it defaults to 53. 3646ecf6635Schristos 3656ecf6635Schristos @param base the evdns_base to which to apply this operation 3666ecf6635Schristos @return 0 if successful, or -1 if an error occurred 3676ecf6635Schristos @see evdns_base_nameserver_add() 3686ecf6635Schristos */ 369805a1ce9Schristos EVENT2_EXPORT_SYMBOL 3706ecf6635Schristos int evdns_base_nameserver_ip_add(struct evdns_base *base, 3716ecf6635Schristos const char *ip_as_string); 3726ecf6635Schristos 3736ecf6635Schristos /** 3746ecf6635Schristos Add a nameserver by sockaddr. 3756ecf6635Schristos **/ 376805a1ce9Schristos EVENT2_EXPORT_SYMBOL 3776ecf6635Schristos int 3786ecf6635Schristos evdns_base_nameserver_sockaddr_add(struct evdns_base *base, 3796ecf6635Schristos const struct sockaddr *sa, ev_socklen_t len, unsigned flags); 3806ecf6635Schristos 3816ecf6635Schristos struct evdns_request; 3826ecf6635Schristos 3836ecf6635Schristos /** 3846ecf6635Schristos Lookup an A record for a given name. 3856ecf6635Schristos 3866ecf6635Schristos @param base the evdns_base to which to apply this operation 3876ecf6635Schristos @param name a DNS hostname 3886ecf6635Schristos @param flags either 0, or DNS_QUERY_NO_SEARCH to disable searching for this query. 3896ecf6635Schristos @param callback a callback function to invoke when the request is completed 3906ecf6635Schristos @param ptr an argument to pass to the callback function 3916ecf6635Schristos @return an evdns_request object if successful, or NULL if an error occurred. 3926ecf6635Schristos @see evdns_resolve_ipv6(), evdns_resolve_reverse(), evdns_resolve_reverse_ipv6(), evdns_cancel_request() 3936ecf6635Schristos */ 394805a1ce9Schristos EVENT2_EXPORT_SYMBOL 3956ecf6635Schristos struct evdns_request *evdns_base_resolve_ipv4(struct evdns_base *base, const char *name, int flags, evdns_callback_type callback, void *ptr); 3966ecf6635Schristos 3976ecf6635Schristos /** 3986ecf6635Schristos Lookup an AAAA record for a given name. 3996ecf6635Schristos 4006ecf6635Schristos @param base the evdns_base to which to apply this operation 4016ecf6635Schristos @param name a DNS hostname 4026ecf6635Schristos @param flags either 0, or DNS_QUERY_NO_SEARCH to disable searching for this query. 4036ecf6635Schristos @param callback a callback function to invoke when the request is completed 4046ecf6635Schristos @param ptr an argument to pass to the callback function 4056ecf6635Schristos @return an evdns_request object if successful, or NULL if an error occurred. 4066ecf6635Schristos @see evdns_resolve_ipv4(), evdns_resolve_reverse(), evdns_resolve_reverse_ipv6(), evdns_cancel_request() 4076ecf6635Schristos */ 408805a1ce9Schristos EVENT2_EXPORT_SYMBOL 4096ecf6635Schristos struct evdns_request *evdns_base_resolve_ipv6(struct evdns_base *base, const char *name, int flags, evdns_callback_type callback, void *ptr); 4106ecf6635Schristos 4116ecf6635Schristos struct in_addr; 4126ecf6635Schristos struct in6_addr; 4136ecf6635Schristos 4146ecf6635Schristos /** 4156ecf6635Schristos Lookup a PTR record for a given IP address. 4166ecf6635Schristos 4176ecf6635Schristos @param base the evdns_base to which to apply this operation 4186ecf6635Schristos @param in an IPv4 address 4196ecf6635Schristos @param flags either 0, or DNS_QUERY_NO_SEARCH to disable searching for this query. 4206ecf6635Schristos @param callback a callback function to invoke when the request is completed 4216ecf6635Schristos @param ptr an argument to pass to the callback function 4226ecf6635Schristos @return an evdns_request object if successful, or NULL if an error occurred. 4236ecf6635Schristos @see evdns_resolve_reverse_ipv6(), evdns_cancel_request() 4246ecf6635Schristos */ 425805a1ce9Schristos EVENT2_EXPORT_SYMBOL 4266ecf6635Schristos struct evdns_request *evdns_base_resolve_reverse(struct evdns_base *base, const struct in_addr *in, int flags, evdns_callback_type callback, void *ptr); 4276ecf6635Schristos 4286ecf6635Schristos 4296ecf6635Schristos /** 4306ecf6635Schristos Lookup a PTR record for a given IPv6 address. 4316ecf6635Schristos 4326ecf6635Schristos @param base the evdns_base to which to apply this operation 4336ecf6635Schristos @param in an IPv6 address 4346ecf6635Schristos @param flags either 0, or DNS_QUERY_NO_SEARCH to disable searching for this query. 4356ecf6635Schristos @param callback a callback function to invoke when the request is completed 4366ecf6635Schristos @param ptr an argument to pass to the callback function 4376ecf6635Schristos @return an evdns_request object if successful, or NULL if an error occurred. 4386ecf6635Schristos @see evdns_resolve_reverse_ipv6(), evdns_cancel_request() 4396ecf6635Schristos */ 440805a1ce9Schristos EVENT2_EXPORT_SYMBOL 4416ecf6635Schristos 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); 4426ecf6635Schristos 4436ecf6635Schristos /** 4446ecf6635Schristos Cancels a pending DNS resolution request. 4456ecf6635Schristos 4466ecf6635Schristos @param base the evdns_base that was used to make the request 4476ecf6635Schristos @param req the evdns_request that was returned by calling a resolve function 4486ecf6635Schristos @see evdns_base_resolve_ipv4(), evdns_base_resolve_ipv6, evdns_base_resolve_reverse 4496ecf6635Schristos */ 450805a1ce9Schristos EVENT2_EXPORT_SYMBOL 4516ecf6635Schristos void evdns_cancel_request(struct evdns_base *base, struct evdns_request *req); 4526ecf6635Schristos 4536ecf6635Schristos /** 4546ecf6635Schristos Set the value of a configuration option. 4556ecf6635Schristos 4566ecf6635Schristos The currently available configuration options are: 4576ecf6635Schristos 4586ecf6635Schristos ndots, timeout, max-timeouts, max-inflight, attempts, randomize-case, 459*657871a7Schristos bind-to, initial-probe-timeout, getaddrinfo-allow-skew, 460*657871a7Schristos so-rcvbuf, so-sndbuf. 4616ecf6635Schristos 4626ecf6635Schristos In versions before Libevent 2.0.3-alpha, the option name needed to end with 4636ecf6635Schristos a colon. 4646ecf6635Schristos 4656ecf6635Schristos @param base the evdns_base to which to apply this operation 4666ecf6635Schristos @param option the name of the configuration option to be modified 4676ecf6635Schristos @param val the value to be set 4686ecf6635Schristos @return 0 if successful, or -1 if an error occurred 4696ecf6635Schristos */ 470805a1ce9Schristos EVENT2_EXPORT_SYMBOL 4716ecf6635Schristos int evdns_base_set_option(struct evdns_base *base, const char *option, const char *val); 4726ecf6635Schristos 4736ecf6635Schristos 4746ecf6635Schristos /** 4756ecf6635Schristos Parse a resolv.conf file. 4766ecf6635Schristos 4776ecf6635Schristos The 'flags' parameter determines what information is parsed from the 4786ecf6635Schristos resolv.conf file. See the man page for resolv.conf for the format of this 4796ecf6635Schristos file. 4806ecf6635Schristos 4816ecf6635Schristos The following directives are not parsed from the file: sortlist, rotate, 4826ecf6635Schristos no-check-names, inet6, debug. 4836ecf6635Schristos 4846ecf6635Schristos If this function encounters an error, the possible return values are: 1 = 4856ecf6635Schristos failed to open file, 2 = failed to stat file, 3 = file too large, 4 = out of 4866ecf6635Schristos memory, 5 = short read from file, 6 = no nameservers listed in the file 4876ecf6635Schristos 4886ecf6635Schristos @param base the evdns_base to which to apply this operation 4896ecf6635Schristos @param flags any of DNS_OPTION_NAMESERVERS|DNS_OPTION_SEARCH|DNS_OPTION_MISC| 490*657871a7Schristos DNS_OPTION_HOSTSFILE|DNS_OPTIONS_ALL|DNS_OPTION_NAMESERVERS_NO_DEFAULT 4916ecf6635Schristos @param filename the path to the resolv.conf file 4926ecf6635Schristos @return 0 if successful, or various positive error codes if an error 4936ecf6635Schristos occurred (see above) 4946ecf6635Schristos @see resolv.conf(3), evdns_config_windows_nameservers() 4956ecf6635Schristos */ 496805a1ce9Schristos EVENT2_EXPORT_SYMBOL 4976ecf6635Schristos int evdns_base_resolv_conf_parse(struct evdns_base *base, int flags, const char *const filename); 4986ecf6635Schristos 4996ecf6635Schristos /** 5006ecf6635Schristos Load an /etc/hosts-style file from 'hosts_fname' into 'base'. 5016ecf6635Schristos 5026ecf6635Schristos If hosts_fname is NULL, add minimal entries for localhost, and nothing 5036ecf6635Schristos else. 5046ecf6635Schristos 5056ecf6635Schristos Note that only evdns_getaddrinfo uses the /etc/hosts entries. 5066ecf6635Schristos 507805a1ce9Schristos This function does not replace previously loaded hosts entries; to do that, 508805a1ce9Schristos call evdns_base_clear_host_addresses first. 509805a1ce9Schristos 5106ecf6635Schristos Return 0 on success, negative on failure. 5116ecf6635Schristos */ 512805a1ce9Schristos EVENT2_EXPORT_SYMBOL 5136ecf6635Schristos int evdns_base_load_hosts(struct evdns_base *base, const char *hosts_fname); 5146ecf6635Schristos 515*657871a7Schristos #if defined(EVENT_IN_DOXYGEN_) || defined(_WIN32) 5166ecf6635Schristos /** 5176ecf6635Schristos Obtain nameserver information using the Windows API. 5186ecf6635Schristos 5196ecf6635Schristos Attempt to configure a set of nameservers based on platform settings on 5206ecf6635Schristos a win32 host. Preferentially tries to use GetNetworkParams; if that fails, 5216ecf6635Schristos looks in the registry. 5226ecf6635Schristos 5236ecf6635Schristos @return 0 if successful, or -1 if an error occurred 5246ecf6635Schristos @see evdns_resolv_conf_parse() 5256ecf6635Schristos */ 526805a1ce9Schristos EVENT2_EXPORT_SYMBOL 5276ecf6635Schristos int evdns_base_config_windows_nameservers(struct evdns_base *); 5286ecf6635Schristos #define EVDNS_BASE_CONFIG_WINDOWS_NAMESERVERS_IMPLEMENTED 5296ecf6635Schristos #endif 5306ecf6635Schristos 5316ecf6635Schristos 5326ecf6635Schristos /** 5336ecf6635Schristos Clear the list of search domains. 5346ecf6635Schristos */ 535805a1ce9Schristos EVENT2_EXPORT_SYMBOL 5366ecf6635Schristos void evdns_base_search_clear(struct evdns_base *base); 5376ecf6635Schristos 5386ecf6635Schristos 5396ecf6635Schristos /** 5406ecf6635Schristos Add a domain to the list of search domains 5416ecf6635Schristos 5426ecf6635Schristos @param domain the domain to be added to the search list 5436ecf6635Schristos */ 544805a1ce9Schristos EVENT2_EXPORT_SYMBOL 5456ecf6635Schristos void evdns_base_search_add(struct evdns_base *base, const char *domain); 5466ecf6635Schristos 5476ecf6635Schristos 5486ecf6635Schristos /** 5496ecf6635Schristos Set the 'ndots' parameter for searches. 5506ecf6635Schristos 5516ecf6635Schristos Sets the number of dots which, when found in a name, causes 5526ecf6635Schristos the first query to be without any search domain. 5536ecf6635Schristos 5546ecf6635Schristos @param ndots the new ndots parameter 5556ecf6635Schristos */ 556805a1ce9Schristos EVENT2_EXPORT_SYMBOL 5576ecf6635Schristos void evdns_base_search_ndots_set(struct evdns_base *base, const int ndots); 5586ecf6635Schristos 5596ecf6635Schristos /** 5606ecf6635Schristos A callback that is invoked when a log message is generated 5616ecf6635Schristos 5626ecf6635Schristos @param is_warning indicates if the log message is a 'warning' 5636ecf6635Schristos @param msg the content of the log message 5646ecf6635Schristos */ 5656ecf6635Schristos typedef void (*evdns_debug_log_fn_type)(int is_warning, const char *msg); 5666ecf6635Schristos 5676ecf6635Schristos 5686ecf6635Schristos /** 5696ecf6635Schristos Set the callback function to handle DNS log messages. If this 5706ecf6635Schristos callback is not set, evdns log messages are handled with the regular 5716ecf6635Schristos Libevent logging system. 5726ecf6635Schristos 5736ecf6635Schristos @param fn the callback to be invoked when a log message is generated 5746ecf6635Schristos */ 575805a1ce9Schristos EVENT2_EXPORT_SYMBOL 5766ecf6635Schristos void evdns_set_log_fn(evdns_debug_log_fn_type fn); 5776ecf6635Schristos 5786ecf6635Schristos /** 5796ecf6635Schristos Set a callback that will be invoked to generate transaction IDs. By 5806ecf6635Schristos default, we pick transaction IDs based on the current clock time, which 5816ecf6635Schristos is bad for security. 5826ecf6635Schristos 5836ecf6635Schristos @param fn the new callback, or NULL to use the default. 5846ecf6635Schristos 5856ecf6635Schristos NOTE: This function has no effect in Libevent 2.0.4-alpha and later, 5866ecf6635Schristos since Libevent now provides its own secure RNG. 5876ecf6635Schristos */ 588805a1ce9Schristos EVENT2_EXPORT_SYMBOL 5896ecf6635Schristos void evdns_set_transaction_id_fn(ev_uint16_t (*fn)(void)); 5906ecf6635Schristos 5916ecf6635Schristos /** 5926ecf6635Schristos Set a callback used to generate random bytes. By default, we use 5936ecf6635Schristos the same function as passed to evdns_set_transaction_id_fn to generate 5946ecf6635Schristos bytes two at a time. If a function is provided here, it's also used 5956ecf6635Schristos to generate transaction IDs. 5966ecf6635Schristos 5976ecf6635Schristos NOTE: This function has no effect in Libevent 2.0.4-alpha and later, 5986ecf6635Schristos since Libevent now provides its own secure RNG. 5996ecf6635Schristos */ 600805a1ce9Schristos EVENT2_EXPORT_SYMBOL 6016ecf6635Schristos void evdns_set_random_bytes_fn(void (*fn)(char *, size_t)); 6026ecf6635Schristos 6036ecf6635Schristos /* 6046ecf6635Schristos * Functions used to implement a DNS server. 6056ecf6635Schristos */ 6066ecf6635Schristos 6076ecf6635Schristos struct evdns_server_request; 6086ecf6635Schristos struct evdns_server_question; 6096ecf6635Schristos 6106ecf6635Schristos /** 6116ecf6635Schristos A callback to implement a DNS server. The callback function receives a DNS 6126ecf6635Schristos request. It should then optionally add a number of answers to the reply 6136ecf6635Schristos using the evdns_server_request_add_*_reply functions, before calling either 6146ecf6635Schristos evdns_server_request_respond to send the reply back, or 6156ecf6635Schristos evdns_server_request_drop to decline to answer the request. 6166ecf6635Schristos 6176ecf6635Schristos @param req A newly received request 6186ecf6635Schristos @param user_data A pointer that was passed to 6196ecf6635Schristos evdns_add_server_port_with_base(). 6206ecf6635Schristos */ 6216ecf6635Schristos typedef void (*evdns_request_callback_fn_type)(struct evdns_server_request *, void *); 6226ecf6635Schristos #define EVDNS_ANSWER_SECTION 0 6236ecf6635Schristos #define EVDNS_AUTHORITY_SECTION 1 6246ecf6635Schristos #define EVDNS_ADDITIONAL_SECTION 2 6256ecf6635Schristos 6266ecf6635Schristos #define EVDNS_TYPE_A 1 6276ecf6635Schristos #define EVDNS_TYPE_NS 2 6286ecf6635Schristos #define EVDNS_TYPE_CNAME 5 6296ecf6635Schristos #define EVDNS_TYPE_SOA 6 6306ecf6635Schristos #define EVDNS_TYPE_PTR 12 6316ecf6635Schristos #define EVDNS_TYPE_MX 15 6326ecf6635Schristos #define EVDNS_TYPE_TXT 16 6336ecf6635Schristos #define EVDNS_TYPE_AAAA 28 6346ecf6635Schristos 6356ecf6635Schristos #define EVDNS_QTYPE_AXFR 252 6366ecf6635Schristos #define EVDNS_QTYPE_ALL 255 6376ecf6635Schristos 6386ecf6635Schristos #define EVDNS_CLASS_INET 1 6396ecf6635Schristos 6406ecf6635Schristos /* flags that can be set in answers; as part of the err parameter */ 6416ecf6635Schristos #define EVDNS_FLAGS_AA 0x400 6426ecf6635Schristos #define EVDNS_FLAGS_RD 0x080 6436ecf6635Schristos 6446ecf6635Schristos /** Create a new DNS server port. 6456ecf6635Schristos 6466ecf6635Schristos @param base The event base to handle events for the server port. 6476ecf6635Schristos @param socket A UDP socket to accept DNS requests. 6486ecf6635Schristos @param flags Always 0 for now. 6496ecf6635Schristos @param callback A function to invoke whenever we get a DNS request 6506ecf6635Schristos on the socket. 6516ecf6635Schristos @param user_data Data to pass to the callback. 652*657871a7Schristos @return an evdns_server_port structure for this server port or NULL if 653*657871a7Schristos an error occurred. 6546ecf6635Schristos */ 655805a1ce9Schristos EVENT2_EXPORT_SYMBOL 6566ecf6635Schristos 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); 6576ecf6635Schristos /** Close down a DNS server port, and free associated structures. */ 658805a1ce9Schristos EVENT2_EXPORT_SYMBOL 6596ecf6635Schristos void evdns_close_server_port(struct evdns_server_port *port); 6606ecf6635Schristos 6616ecf6635Schristos /** Sets some flags in a reply we're building. 6626ecf6635Schristos Allows setting of the AA or RD flags 6636ecf6635Schristos */ 664805a1ce9Schristos EVENT2_EXPORT_SYMBOL 6656ecf6635Schristos void evdns_server_request_set_flags(struct evdns_server_request *req, int flags); 6666ecf6635Schristos 6676ecf6635Schristos /* Functions to add an answer to an in-progress DNS reply. 6686ecf6635Schristos */ 669805a1ce9Schristos EVENT2_EXPORT_SYMBOL 6706ecf6635Schristos 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); 671805a1ce9Schristos EVENT2_EXPORT_SYMBOL 6726ecf6635Schristos int evdns_server_request_add_a_reply(struct evdns_server_request *req, const char *name, int n, const void *addrs, int ttl); 673805a1ce9Schristos EVENT2_EXPORT_SYMBOL 6746ecf6635Schristos int evdns_server_request_add_aaaa_reply(struct evdns_server_request *req, const char *name, int n, const void *addrs, int ttl); 675805a1ce9Schristos EVENT2_EXPORT_SYMBOL 6766ecf6635Schristos 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); 677805a1ce9Schristos EVENT2_EXPORT_SYMBOL 6786ecf6635Schristos int evdns_server_request_add_cname_reply(struct evdns_server_request *req, const char *name, const char *cname, int ttl); 6796ecf6635Schristos 6806ecf6635Schristos /** 6816ecf6635Schristos Send back a response to a DNS request, and free the request structure. 6826ecf6635Schristos */ 683805a1ce9Schristos EVENT2_EXPORT_SYMBOL 6846ecf6635Schristos int evdns_server_request_respond(struct evdns_server_request *req, int err); 6856ecf6635Schristos /** 6866ecf6635Schristos Free a DNS request without sending back a reply. 6876ecf6635Schristos */ 688805a1ce9Schristos EVENT2_EXPORT_SYMBOL 6896ecf6635Schristos int evdns_server_request_drop(struct evdns_server_request *req); 6906ecf6635Schristos struct sockaddr; 6916ecf6635Schristos /** 6926ecf6635Schristos Get the address that made a DNS request. 6936ecf6635Schristos */ 694805a1ce9Schristos EVENT2_EXPORT_SYMBOL 695805a1ce9Schristos int evdns_server_request_get_requesting_addr(struct evdns_server_request *req, struct sockaddr *sa, int addr_len); 6966ecf6635Schristos 6976ecf6635Schristos /** Callback for evdns_getaddrinfo. */ 6986ecf6635Schristos typedef void (*evdns_getaddrinfo_cb)(int result, struct evutil_addrinfo *res, void *arg); 6996ecf6635Schristos 7006ecf6635Schristos struct evdns_base; 7016ecf6635Schristos struct evdns_getaddrinfo_request; 7026ecf6635Schristos /** Make a non-blocking getaddrinfo request using the dns_base in 'dns_base'. 7036ecf6635Schristos * 7046ecf6635Schristos * If we can answer the request immediately (with an error or not!), then we 7056ecf6635Schristos * invoke cb immediately and return NULL. Otherwise we return 7066ecf6635Schristos * an evdns_getaddrinfo_request and invoke cb later. 7076ecf6635Schristos * 7086ecf6635Schristos * When the callback is invoked, we pass as its first argument the error code 7096ecf6635Schristos * that getaddrinfo would return (or 0 for no error). As its second argument, 7106ecf6635Schristos * we pass the evutil_addrinfo structures we found (or NULL on error). We 7116ecf6635Schristos * pass 'arg' as the third argument. 7126ecf6635Schristos * 7136ecf6635Schristos * Limitations: 7146ecf6635Schristos * 7156ecf6635Schristos * - The AI_V4MAPPED and AI_ALL flags are not currently implemented. 7166ecf6635Schristos * - For ai_socktype, we only handle SOCKTYPE_STREAM, SOCKTYPE_UDP, and 0. 7176ecf6635Schristos * - For ai_protocol, we only handle IPPROTO_TCP, IPPROTO_UDP, and 0. 7186ecf6635Schristos */ 719805a1ce9Schristos EVENT2_EXPORT_SYMBOL 7206ecf6635Schristos struct evdns_getaddrinfo_request *evdns_getaddrinfo( 7216ecf6635Schristos struct evdns_base *dns_base, 7226ecf6635Schristos const char *nodename, const char *servname, 7236ecf6635Schristos const struct evutil_addrinfo *hints_in, 7246ecf6635Schristos evdns_getaddrinfo_cb cb, void *arg); 7256ecf6635Schristos 7266ecf6635Schristos /* Cancel an in-progress evdns_getaddrinfo. This MUST NOT be called after the 7276ecf6635Schristos * getaddrinfo's callback has been invoked. The resolves will be canceled, 7286ecf6635Schristos * and the callback will be invoked with the error EVUTIL_EAI_CANCEL. */ 729805a1ce9Schristos EVENT2_EXPORT_SYMBOL 7306ecf6635Schristos void evdns_getaddrinfo_cancel(struct evdns_getaddrinfo_request *req); 7316ecf6635Schristos 732805a1ce9Schristos /** 733805a1ce9Schristos Retrieve the address of the 'idx'th configured nameserver. 734805a1ce9Schristos 735805a1ce9Schristos @param base The evdns_base to examine. 736805a1ce9Schristos @param idx The index of the nameserver to get the address of. 737805a1ce9Schristos @param sa A location to receive the server's address. 738805a1ce9Schristos @param len The number of bytes available at sa. 739805a1ce9Schristos 740805a1ce9Schristos @return the number of bytes written into sa on success. On failure, returns 741805a1ce9Schristos -1 if idx is greater than the number of configured nameservers, or a 742805a1ce9Schristos value greater than 'len' if len was not high enough. 743805a1ce9Schristos */ 744805a1ce9Schristos EVENT2_EXPORT_SYMBOL 745805a1ce9Schristos int evdns_base_get_nameserver_addr(struct evdns_base *base, int idx, 746805a1ce9Schristos struct sockaddr *sa, ev_socklen_t len); 747805a1ce9Schristos 7486ecf6635Schristos #ifdef __cplusplus 7496ecf6635Schristos } 7506ecf6635Schristos #endif 7516ecf6635Schristos 752805a1ce9Schristos #endif /* !EVENT2_DNS_H_INCLUDED_ */ 753