Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:57:11

0001 /*
0002  * Copyright (c) 2006-2007 Niels Provos <provos@citi.umich.edu>
0003  * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
0004  *
0005  * Redistribution and use in source and binary forms, with or without
0006  * modification, are permitted provided that the following conditions
0007  * are met:
0008  * 1. Redistributions of source code must retain the above copyright
0009  *    notice, this list of conditions and the following disclaimer.
0010  * 2. Redistributions in binary form must reproduce the above copyright
0011  *    notice, this list of conditions and the following disclaimer in the
0012  *    documentation and/or other materials provided with the distribution.
0013  * 3. The name of the author may not be used to endorse or promote products
0014  *    derived from this software without specific prior written permission.
0015  *
0016  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
0017  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
0018  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
0019  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
0020  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
0021  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0022  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0023  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0024  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
0025  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0026  */
0027 
0028 /*
0029  * The original DNS code is due to Adam Langley with heavy
0030  * modifications by Nick Mathewson.  Adam put his DNS software in the
0031  * public domain.  You can find his original copyright below.  Please,
0032  * aware that the code as part of Libevent is governed by the 3-clause
0033  * BSD license above.
0034  *
0035  * This software is Public Domain. To view a copy of the public domain dedication,
0036  * visit http://creativecommons.org/licenses/publicdomain/ or send a letter to
0037  * Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
0038  *
0039  * I ask and expect, but do not require, that all derivative works contain an
0040  * attribution similar to:
0041  *     Parts developed by Adam Langley <agl@imperialviolet.org>
0042  *
0043  * You may wish to replace the word "Parts" with something else depending on
0044  * the amount of original code.
0045  *
0046  * (Derivative works does not include programs which link against, run or include
0047  * the source verbatim in their source distributions)
0048  */
0049 
0050 /** @file event2/dns.h
0051  *
0052  * Welcome, gentle reader
0053  *
0054  * Async DNS lookups are really a whole lot harder than they should be,
0055  * mostly stemming from the fact that the libc resolver has never been
0056  * very good at them. Before you use this library you should see if libc
0057  * can do the job for you with the modern async call getaddrinfo_a
0058  * (see http://www.imperialviolet.org/page25.html#e498). Otherwise,
0059  * please continue.
0060  *
0061  * The library keeps track of the state of nameservers and will avoid
0062  * them when they go down. Otherwise it will round robin between them.
0063  *
0064  * Quick start guide:
0065  *   #include "evdns.h"
0066  *   void callback(int result, char type, int count, int ttl,
0067  *       void *addresses, void *arg);
0068  *   evdns_resolv_conf_parse(DNS_OPTIONS_ALL, "/etc/resolv.conf");
0069  *   evdns_resolve("www.hostname.com", 0, callback, NULL);
0070  *
0071  * When the lookup is complete the callback function is called. The
0072  * first argument will be one of the DNS_ERR_* defines in evdns.h.
0073  * Hopefully it will be DNS_ERR_NONE, in which case type will be
0074  * DNS_IPv4_A, count will be the number of IP addresses, ttl is the time
0075  * which the data can be cached for (in seconds), addresses will point
0076  * to an array of uint32_t's and arg will be whatever you passed to
0077  * evdns_resolve.
0078  *
0079  * Searching:
0080  *
0081  * In order for this library to be a good replacement for glibc's resolver it
0082  * supports searching. This involves setting a list of default domains, in
0083  * which names will be queried for. The number of dots in the query name
0084  * determines the order in which this list is used.
0085  *
0086  * Searching appears to be a single lookup from the point of view of the API,
0087  * although many DNS queries may be generated from a single call to
0088  * evdns_resolve. Searching can also drastically slow down the resolution
0089  * of names.
0090  *
0091  * To disable searching:
0092  *   1. Never set it up. If you never call evdns_resolv_conf_parse or
0093  *   evdns_search_add then no searching will occur.
0094  *
0095  *   2. If you do call evdns_resolv_conf_parse then don't pass
0096  *   DNS_OPTION_SEARCH (or DNS_OPTIONS_ALL, which implies it).
0097  *
0098  *   3. When calling evdns_resolve, pass the DNS_QUERY_NO_SEARCH flag.
0099  *
0100  * The order of searches depends on the number of dots in the name. If the
0101  * number is greater than the ndots setting then the names is first tried
0102  * globally. Otherwise each search domain is appended in turn.
0103  *
0104  * The ndots setting can either be set from a resolv.conf, or by calling
0105  * evdns_search_ndots_set.
0106  *
0107  * For example, with ndots set to 1 (the default) and a search domain list of
0108  * ["myhome.net"]:
0109  *  Query: www
0110  *  Order: www.myhome.net, www.
0111  *
0112  *  Query: www.abc
0113  *  Order: www.abc., www.abc.myhome.net
0114  *
0115  * Internals:
0116  *
0117  * Requests are kept in two queues. The first is the inflight queue. In
0118  * this queue requests have an allocated transaction id and nameserver.
0119  * They will soon be transmitted if they haven't already been.
0120  *
0121  * The second is the waiting queue. The size of the inflight ring is
0122  * limited and all other requests wait in waiting queue for space. This
0123  * bounds the number of concurrent requests so that we don't flood the
0124  * nameserver. Several algorithms require a full walk of the inflight
0125  * queue and so bounding its size keeps thing going nicely under huge
0126  * (many thousands of requests) loads.
0127  *
0128  * If a nameserver loses too many requests it is considered down and we
0129  * try not to use it. After a while we send a probe to that nameserver
0130  * (a lookup for google.com) and, if it replies, we consider it working
0131  * again. If the nameserver fails a probe we wait longer to try again
0132  * with the next probe.
0133  */
0134 
0135 #ifndef EVENT2_DNS_H_INCLUDED_
0136 #define EVENT2_DNS_H_INCLUDED_
0137 
0138 #include <event2/visibility.h>
0139 
0140 #ifdef __cplusplus
0141 extern "C" {
0142 #endif
0143 
0144 /* For integer types. */
0145 #include <event2/util.h>
0146 
0147 /** Error codes 0-5 are as described in RFC 1035. */
0148 #define DNS_ERR_NONE 0
0149 /** The name server was unable to interpret the query */
0150 #define DNS_ERR_FORMAT 1
0151 /** The name server was unable to process this query due to a problem with the
0152  * name server */
0153 #define DNS_ERR_SERVERFAILED 2
0154 /** The domain name does not exist */
0155 #define DNS_ERR_NOTEXIST 3
0156 /** The name server does not support the requested kind of query */
0157 #define DNS_ERR_NOTIMPL 4
0158 /** The name server refuses to reform the specified operation for policy
0159  * reasons */
0160 #define DNS_ERR_REFUSED 5
0161 /** The reply was truncated or ill-formatted */
0162 #define DNS_ERR_TRUNCATED 65
0163 /** An unknown error occurred */
0164 #define DNS_ERR_UNKNOWN 66
0165 /** Communication with the server timed out */
0166 #define DNS_ERR_TIMEOUT 67
0167 /** The request was canceled because the DNS subsystem was shut down. */
0168 #define DNS_ERR_SHUTDOWN 68
0169 /** The request was canceled via a call to evdns_cancel_request */
0170 #define DNS_ERR_CANCEL 69
0171 /** There were no answers and no error condition in the DNS packet.
0172  * This can happen when you ask for an address that exists, but a record
0173  * type that doesn't. */
0174 #define DNS_ERR_NODATA 70
0175 
0176 #define DNS_IPv4_A 1
0177 #define DNS_PTR 2
0178 #define DNS_IPv6_AAAA 3
0179 
0180 #define DNS_QUERY_NO_SEARCH 1
0181 
0182 /* Allow searching */
0183 #define DNS_OPTION_SEARCH 1
0184 /* Parse "nameserver" and add default if no such section */
0185 #define DNS_OPTION_NAMESERVERS 2
0186 /* Parse additional options like:
0187  * - timeout:
0188  * - getaddrinfo-allow-skew:
0189  * - max-timeouts:
0190  * - max-inflight:
0191  * - attempts:
0192  * - randomize-case:
0193  * - initial-probe-timeout:
0194  */
0195 #define DNS_OPTION_MISC 4
0196 /* Load hosts file (i.e. "/etc/hosts") */
0197 #define DNS_OPTION_HOSTSFILE 8
0198 /**
0199  * All above:
0200  * - DNS_OPTION_SEARCH
0201  * - DNS_OPTION_NAMESERVERS
0202  * - DNS_OPTION_MISC
0203  * - DNS_OPTION_HOSTSFILE
0204  */
0205 #define DNS_OPTIONS_ALL (    \
0206     DNS_OPTION_SEARCH      | \
0207     DNS_OPTION_NAMESERVERS | \
0208     DNS_OPTION_MISC        | \
0209     DNS_OPTION_HOSTSFILE   | \
0210     0                        \
0211 )
0212 /* Do not "default" nameserver (i.e. "127.0.0.1:53") if there is no nameservers
0213  * in resolv.conf, (iff DNS_OPTION_NAMESERVERS is set) */
0214 #define DNS_OPTION_NAMESERVERS_NO_DEFAULT 16
0215 
0216 /* Obsolete name for DNS_QUERY_NO_SEARCH */
0217 #define DNS_NO_SEARCH DNS_QUERY_NO_SEARCH
0218 
0219 /**
0220  * The callback that contains the results from a lookup.
0221  * - result is one of the DNS_ERR_* values (DNS_ERR_NONE for success)
0222  * - type is either DNS_IPv4_A or DNS_PTR or DNS_IPv6_AAAA
0223  * - count contains the number of addresses of form type
0224  * - ttl is the number of seconds the resolution may be cached for.
0225  * - addresses needs to be cast according to type.  It will be an array of
0226  *   4-byte sequences for ipv4, or an array of 16-byte sequences for ipv6,
0227  *   or a nul-terminated string for PTR.
0228  */
0229 typedef void (*evdns_callback_type) (int result, char type, int count, int ttl, void *addresses, void *arg);
0230 
0231 struct evdns_base;
0232 struct event_base;
0233 
0234 /** Flag for evdns_base_new: process resolv.conf.  */
0235 #define EVDNS_BASE_INITIALIZE_NAMESERVERS 1
0236 /** Flag for evdns_base_new: Do not prevent the libevent event loop from
0237  * exiting when we have no active dns requests. */
0238 #define EVDNS_BASE_DISABLE_WHEN_INACTIVE 0x8000
0239 /** Flag for evdns_base_new: If EVDNS_BASE_INITIALIZE_NAMESERVERS isset, do not
0240  * add default nameserver if there are no nameservers in resolv.conf
0241  * @see DNS_OPTION_NAMESERVERS_NO_DEFAULT */
0242 #define EVDNS_BASE_NAMESERVERS_NO_DEFAULT 0x10000
0243 
0244 /**
0245   Initialize the asynchronous DNS library.
0246 
0247   This function initializes support for non-blocking name resolution by
0248   calling evdns_resolv_conf_parse() on UNIX and
0249   evdns_config_windows_nameservers() on Windows.
0250 
0251   @param event_base the event base to associate the dns client with
0252   @param flags any of EVDNS_BASE_INITIALIZE_NAMESERVERS|
0253     EVDNS_BASE_DISABLE_WHEN_INACTIVE|EVDNS_BASE_NAMESERVERS_NO_DEFAULT
0254   @return evdns_base object if successful, or NULL if an error occurred.
0255   @see evdns_base_free()
0256  */
0257 EVENT2_EXPORT_SYMBOL
0258 struct evdns_base * evdns_base_new(struct event_base *event_base, int initialize_nameservers);
0259 
0260 
0261 /**
0262   Shut down the asynchronous DNS resolver and terminate all active requests.
0263 
0264   If the 'fail_requests' option is enabled, all active requests will return
0265   an empty result with the error flag set to DNS_ERR_SHUTDOWN. Otherwise,
0266   the requests will be silently discarded.
0267 
0268   @param evdns_base the evdns base to free
0269   @param fail_requests if zero, active requests will be aborted; if non-zero,
0270         active requests will return DNS_ERR_SHUTDOWN.
0271   @see evdns_base_new()
0272  */
0273 EVENT2_EXPORT_SYMBOL
0274 void evdns_base_free(struct evdns_base *base, int fail_requests);
0275 
0276 /**
0277    Remove all hosts entries that have been loaded into the event_base via
0278    evdns_base_load_hosts or via event_base_resolv_conf_parse.
0279 
0280    @param evdns_base the evdns base to remove outdated host addresses from
0281  */
0282 EVENT2_EXPORT_SYMBOL
0283 void evdns_base_clear_host_addresses(struct evdns_base *base);
0284 
0285 /**
0286   Convert a DNS error code to a string.
0287 
0288   @param err the DNS error code
0289   @return a string containing an explanation of the error code
0290 */
0291 EVENT2_EXPORT_SYMBOL
0292 const char *evdns_err_to_string(int err);
0293 
0294 
0295 /**
0296   Add a nameserver.
0297 
0298   The address should be an IPv4 address in network byte order.
0299   The type of address is chosen so that it matches in_addr.s_addr.
0300 
0301   @param base the evdns_base to which to add the name server
0302   @param address an IP address in network byte order
0303   @return 0 if successful, or -1 if an error occurred
0304   @see evdns_base_nameserver_ip_add()
0305  */
0306 EVENT2_EXPORT_SYMBOL
0307 int evdns_base_nameserver_add(struct evdns_base *base,
0308     unsigned long int address);
0309 
0310 /**
0311   Get the number of configured nameservers.
0312 
0313   This returns the number of configured nameservers (not necessarily the
0314   number of running nameservers).  This is useful for double-checking
0315   whether our calls to the various nameserver configuration functions
0316   have been successful.
0317 
0318   @param base the evdns_base to which to apply this operation
0319   @return the number of configured nameservers
0320   @see evdns_base_nameserver_add()
0321  */
0322 EVENT2_EXPORT_SYMBOL
0323 int evdns_base_count_nameservers(struct evdns_base *base);
0324 
0325 /**
0326   Remove all configured nameservers, and suspend all pending resolves.
0327 
0328   Resolves will not necessarily be re-attempted until evdns_base_resume() is called.
0329 
0330   @param base the evdns_base to which to apply this operation
0331   @return 0 if successful, or -1 if an error occurred
0332   @see evdns_base_resume()
0333  */
0334 EVENT2_EXPORT_SYMBOL
0335 int evdns_base_clear_nameservers_and_suspend(struct evdns_base *base);
0336 
0337 
0338 /**
0339   Resume normal operation and continue any suspended resolve requests.
0340 
0341   Re-attempt resolves left in limbo after an earlier call to
0342   evdns_base_clear_nameservers_and_suspend().
0343 
0344   @param base the evdns_base to which to apply this operation
0345   @return 0 if successful, or -1 if an error occurred
0346   @see evdns_base_clear_nameservers_and_suspend()
0347  */
0348 EVENT2_EXPORT_SYMBOL
0349 int evdns_base_resume(struct evdns_base *base);
0350 
0351 /**
0352   Add a nameserver by string address.
0353 
0354   This function parses a n IPv4 or IPv6 address from a string and adds it as a
0355   nameserver.  It supports the following formats:
0356   - [IPv6Address]:port
0357   - [IPv6Address]
0358   - IPv6Address
0359   - IPv4Address:port
0360   - IPv4Address
0361 
0362   If no port is specified, it defaults to 53.
0363 
0364   @param base the evdns_base to which to apply this operation
0365   @return 0 if successful, or -1 if an error occurred
0366   @see evdns_base_nameserver_add()
0367  */
0368 EVENT2_EXPORT_SYMBOL
0369 int evdns_base_nameserver_ip_add(struct evdns_base *base,
0370     const char *ip_as_string);
0371 
0372 /**
0373    Add a nameserver by sockaddr.
0374  **/
0375 EVENT2_EXPORT_SYMBOL
0376 int
0377 evdns_base_nameserver_sockaddr_add(struct evdns_base *base,
0378     const struct sockaddr *sa, ev_socklen_t len, unsigned flags);
0379 
0380 struct evdns_request;
0381 
0382 /**
0383   Lookup an A record for a given name.
0384 
0385   @param base the evdns_base to which to apply this operation
0386   @param name a DNS hostname
0387   @param flags either 0, or DNS_QUERY_NO_SEARCH to disable searching for this query.
0388   @param callback a callback function to invoke when the request is completed
0389   @param ptr an argument to pass to the callback function
0390   @return an evdns_request object if successful, or NULL if an error occurred.
0391   @see evdns_resolve_ipv6(), evdns_resolve_reverse(), evdns_resolve_reverse_ipv6(), evdns_cancel_request()
0392  */
0393 EVENT2_EXPORT_SYMBOL
0394 struct evdns_request *evdns_base_resolve_ipv4(struct evdns_base *base, const char *name, int flags, evdns_callback_type callback, void *ptr);
0395 
0396 /**
0397   Lookup an AAAA record for a given name.
0398 
0399   @param base the evdns_base to which to apply this operation
0400   @param name a DNS hostname
0401   @param flags either 0, or DNS_QUERY_NO_SEARCH to disable searching for this query.
0402   @param callback a callback function to invoke when the request is completed
0403   @param ptr an argument to pass to the callback function
0404   @return an evdns_request object if successful, or NULL if an error occurred.
0405   @see evdns_resolve_ipv4(), evdns_resolve_reverse(), evdns_resolve_reverse_ipv6(), evdns_cancel_request()
0406  */
0407 EVENT2_EXPORT_SYMBOL
0408 struct evdns_request *evdns_base_resolve_ipv6(struct evdns_base *base, const char *name, int flags, evdns_callback_type callback, void *ptr);
0409 
0410 struct in_addr;
0411 struct in6_addr;
0412 
0413 /**
0414   Lookup a PTR record for a given IP address.
0415 
0416   @param base the evdns_base to which to apply this operation
0417   @param in an IPv4 address
0418   @param flags either 0, or DNS_QUERY_NO_SEARCH to disable searching for this query.
0419   @param callback a callback function to invoke when the request is completed
0420   @param ptr an argument to pass to the callback function
0421   @return an evdns_request object if successful, or NULL if an error occurred.
0422   @see evdns_resolve_reverse_ipv6(), evdns_cancel_request()
0423  */
0424 EVENT2_EXPORT_SYMBOL
0425 struct evdns_request *evdns_base_resolve_reverse(struct evdns_base *base, const struct in_addr *in, int flags, evdns_callback_type callback, void *ptr);
0426 
0427 
0428 /**
0429   Lookup a PTR record for a given IPv6 address.
0430 
0431   @param base the evdns_base to which to apply this operation
0432   @param in an IPv6 address
0433   @param flags either 0, or DNS_QUERY_NO_SEARCH to disable searching for this query.
0434   @param callback a callback function to invoke when the request is completed
0435   @param ptr an argument to pass to the callback function
0436   @return an evdns_request object if successful, or NULL if an error occurred.
0437   @see evdns_resolve_reverse_ipv6(), evdns_cancel_request()
0438  */
0439 EVENT2_EXPORT_SYMBOL
0440 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);
0441 
0442 /**
0443   Cancels a pending DNS resolution request.
0444 
0445   @param base the evdns_base that was used to make the request
0446   @param req the evdns_request that was returned by calling a resolve function
0447   @see evdns_base_resolve_ipv4(), evdns_base_resolve_ipv6, evdns_base_resolve_reverse
0448 */
0449 EVENT2_EXPORT_SYMBOL
0450 void evdns_cancel_request(struct evdns_base *base, struct evdns_request *req);
0451 
0452 /**
0453   Set the value of a configuration option.
0454 
0455   The currently available configuration options are:
0456 
0457     ndots, timeout, max-timeouts, max-inflight, attempts, randomize-case,
0458     bind-to, initial-probe-timeout, getaddrinfo-allow-skew,
0459     so-rcvbuf, so-sndbuf.
0460 
0461   In versions before Libevent 2.0.3-alpha, the option name needed to end with
0462   a colon.
0463 
0464   @param base the evdns_base to which to apply this operation
0465   @param option the name of the configuration option to be modified
0466   @param val the value to be set
0467   @return 0 if successful, or -1 if an error occurred
0468  */
0469 EVENT2_EXPORT_SYMBOL
0470 int evdns_base_set_option(struct evdns_base *base, const char *option, const char *val);
0471 
0472 
0473 /**
0474   Parse a resolv.conf file.
0475 
0476   The 'flags' parameter determines what information is parsed from the
0477   resolv.conf file. See the man page for resolv.conf for the format of this
0478   file.
0479 
0480   The following directives are not parsed from the file: sortlist, rotate,
0481   no-check-names, inet6, debug.
0482 
0483   If this function encounters an error, the possible return values are: 1 =
0484   failed to open file, 2 = failed to stat file, 3 = file too large, 4 = out of
0485   memory, 5 = short read from file, 6 = no nameservers listed in the file
0486 
0487   @param base the evdns_base to which to apply this operation
0488   @param flags any of DNS_OPTION_NAMESERVERS|DNS_OPTION_SEARCH|DNS_OPTION_MISC|
0489     DNS_OPTION_HOSTSFILE|DNS_OPTIONS_ALL|DNS_OPTION_NAMESERVERS_NO_DEFAULT
0490   @param filename the path to the resolv.conf file
0491   @return 0 if successful, or various positive error codes if an error
0492     occurred (see above)
0493   @see resolv.conf(3), evdns_config_windows_nameservers()
0494  */
0495 EVENT2_EXPORT_SYMBOL
0496 int evdns_base_resolv_conf_parse(struct evdns_base *base, int flags, const char *const filename);
0497 
0498 /**
0499    Load an /etc/hosts-style file from 'hosts_fname' into 'base'.
0500 
0501    If hosts_fname is NULL, add minimal entries for localhost, and nothing
0502    else.
0503 
0504    Note that only evdns_getaddrinfo uses the /etc/hosts entries.
0505 
0506    This function does not replace previously loaded hosts entries; to do that,
0507    call evdns_base_clear_host_addresses first.
0508 
0509    Return 0 on success, negative on failure.
0510 */
0511 EVENT2_EXPORT_SYMBOL
0512 int evdns_base_load_hosts(struct evdns_base *base, const char *hosts_fname);
0513 
0514 #if defined(EVENT_IN_DOXYGEN_) || defined(_WIN32)
0515 /**
0516   Obtain nameserver information using the Windows API.
0517 
0518   Attempt to configure a set of nameservers based on platform settings on
0519   a win32 host.  Preferentially tries to use GetNetworkParams; if that fails,
0520   looks in the registry.
0521 
0522   @return 0 if successful, or -1 if an error occurred
0523   @see evdns_resolv_conf_parse()
0524  */
0525 EVENT2_EXPORT_SYMBOL
0526 int evdns_base_config_windows_nameservers(struct evdns_base *);
0527 #define EVDNS_BASE_CONFIG_WINDOWS_NAMESERVERS_IMPLEMENTED
0528 #endif
0529 
0530 
0531 /**
0532   Clear the list of search domains.
0533  */
0534 EVENT2_EXPORT_SYMBOL
0535 void evdns_base_search_clear(struct evdns_base *base);
0536 
0537 
0538 /**
0539   Add a domain to the list of search domains
0540 
0541   @param domain the domain to be added to the search list
0542  */
0543 EVENT2_EXPORT_SYMBOL
0544 void evdns_base_search_add(struct evdns_base *base, const char *domain);
0545 
0546 
0547 /**
0548   Set the 'ndots' parameter for searches.
0549 
0550   Sets the number of dots which, when found in a name, causes
0551   the first query to be without any search domain.
0552 
0553   @param ndots the new ndots parameter
0554  */
0555 EVENT2_EXPORT_SYMBOL
0556 void evdns_base_search_ndots_set(struct evdns_base *base, const int ndots);
0557 
0558 /**
0559   A callback that is invoked when a log message is generated
0560 
0561   @param is_warning indicates if the log message is a 'warning'
0562   @param msg the content of the log message
0563  */
0564 typedef void (*evdns_debug_log_fn_type)(int is_warning, const char *msg);
0565 
0566 
0567 /**
0568   Set the callback function to handle DNS log messages.  If this
0569   callback is not set, evdns log messages are handled with the regular
0570   Libevent logging system.
0571 
0572   @param fn the callback to be invoked when a log message is generated
0573  */
0574 EVENT2_EXPORT_SYMBOL
0575 void evdns_set_log_fn(evdns_debug_log_fn_type fn);
0576 
0577 /**
0578    Set a callback that will be invoked to generate transaction IDs.  By
0579    default, we pick transaction IDs based on the current clock time, which
0580    is bad for security.
0581 
0582    @param fn the new callback, or NULL to use the default.
0583 
0584    NOTE: This function has no effect in Libevent 2.0.4-alpha and later,
0585    since Libevent now provides its own secure RNG.
0586  */
0587 EVENT2_EXPORT_SYMBOL
0588 void evdns_set_transaction_id_fn(ev_uint16_t (*fn)(void));
0589 
0590 /**
0591    Set a callback used to generate random bytes.  By default, we use
0592    the same function as passed to evdns_set_transaction_id_fn to generate
0593    bytes two at a time.  If a function is provided here, it's also used
0594    to generate transaction IDs.
0595 
0596    NOTE: This function has no effect in Libevent 2.0.4-alpha and later,
0597    since Libevent now provides its own secure RNG.
0598 */
0599 EVENT2_EXPORT_SYMBOL
0600 void evdns_set_random_bytes_fn(void (*fn)(char *, size_t));
0601 
0602 /*
0603  * Functions used to implement a DNS server.
0604  */
0605 
0606 struct evdns_server_request;
0607 struct evdns_server_question;
0608 
0609 /**
0610    A callback to implement a DNS server.  The callback function receives a DNS
0611    request.  It should then optionally add a number of answers to the reply
0612    using the evdns_server_request_add_*_reply functions, before calling either
0613    evdns_server_request_respond to send the reply back, or
0614    evdns_server_request_drop to decline to answer the request.
0615 
0616    @param req A newly received request
0617    @param user_data A pointer that was passed to
0618       evdns_add_server_port_with_base().
0619  */
0620 typedef void (*evdns_request_callback_fn_type)(struct evdns_server_request *, void *);
0621 #define EVDNS_ANSWER_SECTION 0
0622 #define EVDNS_AUTHORITY_SECTION 1
0623 #define EVDNS_ADDITIONAL_SECTION 2
0624 
0625 #define EVDNS_TYPE_A       1
0626 #define EVDNS_TYPE_NS      2
0627 #define EVDNS_TYPE_CNAME   5
0628 #define EVDNS_TYPE_SOA     6
0629 #define EVDNS_TYPE_PTR    12
0630 #define EVDNS_TYPE_MX     15
0631 #define EVDNS_TYPE_TXT    16
0632 #define EVDNS_TYPE_AAAA   28
0633 
0634 #define EVDNS_QTYPE_AXFR 252
0635 #define EVDNS_QTYPE_ALL  255
0636 
0637 #define EVDNS_CLASS_INET   1
0638 
0639 /* flags that can be set in answers; as part of the err parameter */
0640 #define EVDNS_FLAGS_AA  0x400
0641 #define EVDNS_FLAGS_RD  0x080
0642 
0643 /** Create a new DNS server port.
0644 
0645     @param base The event base to handle events for the server port.
0646     @param socket A UDP socket to accept DNS requests.
0647     @param flags Always 0 for now.
0648     @param callback A function to invoke whenever we get a DNS request
0649       on the socket.
0650     @param user_data Data to pass to the callback.
0651     @return an evdns_server_port structure for this server port or NULL if
0652       an error occurred.
0653  */
0654 EVENT2_EXPORT_SYMBOL
0655 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);
0656 /** Close down a DNS server port, and free associated structures. */
0657 EVENT2_EXPORT_SYMBOL
0658 void evdns_close_server_port(struct evdns_server_port *port);
0659 
0660 /** Sets some flags in a reply we're building.
0661     Allows setting of the AA or RD flags
0662  */
0663 EVENT2_EXPORT_SYMBOL
0664 void evdns_server_request_set_flags(struct evdns_server_request *req, int flags);
0665 
0666 /* Functions to add an answer to an in-progress DNS reply.
0667  */
0668 EVENT2_EXPORT_SYMBOL
0669 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);
0670 EVENT2_EXPORT_SYMBOL
0671 int evdns_server_request_add_a_reply(struct evdns_server_request *req, const char *name, int n, const void *addrs, int ttl);
0672 EVENT2_EXPORT_SYMBOL
0673 int evdns_server_request_add_aaaa_reply(struct evdns_server_request *req, const char *name, int n, const void *addrs, int ttl);
0674 EVENT2_EXPORT_SYMBOL
0675 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);
0676 EVENT2_EXPORT_SYMBOL
0677 int evdns_server_request_add_cname_reply(struct evdns_server_request *req, const char *name, const char *cname, int ttl);
0678 
0679 /**
0680    Send back a response to a DNS request, and free the request structure.
0681 */
0682 EVENT2_EXPORT_SYMBOL
0683 int evdns_server_request_respond(struct evdns_server_request *req, int err);
0684 /**
0685    Free a DNS request without sending back a reply.
0686 */
0687 EVENT2_EXPORT_SYMBOL
0688 int evdns_server_request_drop(struct evdns_server_request *req);
0689 struct sockaddr;
0690 /**
0691     Get the address that made a DNS request.
0692  */
0693 EVENT2_EXPORT_SYMBOL
0694 int evdns_server_request_get_requesting_addr(struct evdns_server_request *req, struct sockaddr *sa, int addr_len);
0695 
0696 /** Callback for evdns_getaddrinfo. */
0697 typedef void (*evdns_getaddrinfo_cb)(int result, struct evutil_addrinfo *res, void *arg);
0698 
0699 struct evdns_base;
0700 struct evdns_getaddrinfo_request;
0701 /** Make a non-blocking getaddrinfo request using the dns_base in 'dns_base'.
0702  *
0703  * If we can answer the request immediately (with an error or not!), then we
0704  * invoke cb immediately and return NULL.  Otherwise we return
0705  * an evdns_getaddrinfo_request and invoke cb later.
0706  *
0707  * When the callback is invoked, we pass as its first argument the error code
0708  * that getaddrinfo would return (or 0 for no error).  As its second argument,
0709  * we pass the evutil_addrinfo structures we found (or NULL on error).  We
0710  * pass 'arg' as the third argument.
0711  *
0712  * Limitations:
0713  *
0714  * - The AI_V4MAPPED and AI_ALL flags are not currently implemented.
0715  * - For ai_socktype, we only handle SOCKTYPE_STREAM, SOCKTYPE_UDP, and 0.
0716  * - For ai_protocol, we only handle IPPROTO_TCP, IPPROTO_UDP, and 0.
0717  */
0718 EVENT2_EXPORT_SYMBOL
0719 struct evdns_getaddrinfo_request *evdns_getaddrinfo(
0720     struct evdns_base *dns_base,
0721     const char *nodename, const char *servname,
0722     const struct evutil_addrinfo *hints_in,
0723     evdns_getaddrinfo_cb cb, void *arg);
0724 
0725 /* Cancel an in-progress evdns_getaddrinfo.  This MUST NOT be called after the
0726  * getaddrinfo's callback has been invoked.  The resolves will be canceled,
0727  * and the callback will be invoked with the error EVUTIL_EAI_CANCEL. */
0728 EVENT2_EXPORT_SYMBOL
0729 void evdns_getaddrinfo_cancel(struct evdns_getaddrinfo_request *req);
0730 
0731 /**
0732    Retrieve the address of the 'idx'th configured nameserver.
0733 
0734    @param base The evdns_base to examine.
0735    @param idx The index of the nameserver to get the address of.
0736    @param sa A location to receive the server's address.
0737    @param len The number of bytes available at sa.
0738 
0739    @return the number of bytes written into sa on success.  On failure, returns
0740      -1 if idx is greater than the number of configured nameservers, or a
0741      value greater than 'len' if len was not high enough.
0742  */
0743 EVENT2_EXPORT_SYMBOL
0744 int evdns_base_get_nameserver_addr(struct evdns_base *base, int idx,
0745     struct sockaddr *sa, ev_socklen_t len);
0746 
0747 #ifdef __cplusplus
0748 }
0749 #endif
0750 
0751 #endif  /* !EVENT2_DNS_H_INCLUDED_ */