libevent
event2/dns.h
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2006-2007 Niels Provos <provos@citi.umich.edu>
00003  * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. The name of the author may not be used to endorse or promote products
00014  *    derived from this software without specific prior written permission.
00015  *
00016  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00017  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00018  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00019  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00020  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00021  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00022  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00023  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00024  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00025  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026  */
00027 
00028 /*
00029  * The original DNS code is due to Adam Langley with heavy
00030  * modifications by Nick Mathewson.  Adam put his DNS software in the
00031  * public domain.  You can find his original copyright below.  Please,
00032  * aware that the code as part of Libevent is governed by the 3-clause
00033  * BSD license above.
00034  *
00035  * This software is Public Domain. To view a copy of the public domain dedication,
00036  * visit http://creativecommons.org/licenses/publicdomain/ or send a letter to
00037  * Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
00038  *
00039  * I ask and expect, but do not require, that all derivative works contain an
00040  * attribution similar to:
00041  *     Parts developed by Adam Langley <agl@imperialviolet.org>
00042  *
00043  * You may wish to replace the word "Parts" with something else depending on
00044  * the amount of original code.
00045  *
00046  * (Derivative works does not include programs which link against, run or include
00047  * the source verbatim in their source distributions)
00048  */
00049 
00135 #ifndef _EVENT2_DNS_H_
00136 #define _EVENT2_DNS_H_
00137 
00138 #ifdef __cplusplus
00139 extern "C" {
00140 #endif
00141 
00142 /* For integer types. */
00143 #include <event2/util.h>
00144 
00146 #define DNS_ERR_NONE 0
00147 
00148 #define DNS_ERR_FORMAT 1
00149 
00151 #define DNS_ERR_SERVERFAILED 2
00152 
00153 #define DNS_ERR_NOTEXIST 3
00154 
00155 #define DNS_ERR_NOTIMPL 4
00156 
00158 #define DNS_ERR_REFUSED 5
00159 
00160 #define DNS_ERR_TRUNCATED 65
00161 
00162 #define DNS_ERR_UNKNOWN 66
00163 
00164 #define DNS_ERR_TIMEOUT 67
00165 
00166 #define DNS_ERR_SHUTDOWN 68
00167 
00168 #define DNS_ERR_CANCEL 69
00169 
00172 #define DNS_ERR_NODATA 70
00173 
00174 #define DNS_IPv4_A 1
00175 #define DNS_PTR 2
00176 #define DNS_IPv6_AAAA 3
00177 
00178 #define DNS_QUERY_NO_SEARCH 1
00179 
00180 #define DNS_OPTION_SEARCH 1
00181 #define DNS_OPTION_NAMESERVERS 2
00182 #define DNS_OPTION_MISC 4
00183 #define DNS_OPTION_HOSTSFILE 8
00184 #define DNS_OPTIONS_ALL 15
00185 
00186 /* Obsolete name for DNS_QUERY_NO_SEARCH */
00187 #define DNS_NO_SEARCH DNS_QUERY_NO_SEARCH
00188 
00199 typedef void (*evdns_callback_type) (int result, char type, int count, int ttl, void *addresses, void *arg);
00200 
00201 struct evdns_base;
00202 struct event_base;
00203 
00216 struct evdns_base * evdns_base_new(struct event_base *event_base, int initialize_nameservers);
00217 
00218 
00231 void evdns_base_free(struct evdns_base *base, int fail_requests);
00232 
00239 const char *evdns_err_to_string(int err);
00240 
00241 
00253 int evdns_base_nameserver_add(struct evdns_base *base,
00254     unsigned long int address);
00255 
00268 int evdns_base_count_nameservers(struct evdns_base *base);
00269 
00279 int evdns_base_clear_nameservers_and_suspend(struct evdns_base *base);
00280 
00281 
00292 int evdns_base_resume(struct evdns_base *base);
00293 
00311 int evdns_base_nameserver_ip_add(struct evdns_base *base,
00312     const char *ip_as_string);
00313 
00317 int
00318 evdns_base_nameserver_sockaddr_add(struct evdns_base *base,
00319     const struct sockaddr *sa, ev_socklen_t len, unsigned flags);
00320 
00321 struct evdns_request;
00322 
00334 struct evdns_request *evdns_base_resolve_ipv4(struct evdns_base *base, const char *name, int flags, evdns_callback_type callback, void *ptr);
00335 
00347 struct evdns_request *evdns_base_resolve_ipv6(struct evdns_base *base, const char *name, int flags, evdns_callback_type callback, void *ptr);
00348 
00349 struct in_addr;
00350 struct in6_addr;
00351 
00363 struct evdns_request *evdns_base_resolve_reverse(struct evdns_base *base, const struct in_addr *in, int flags, evdns_callback_type callback, void *ptr);
00364 
00365 
00377 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);
00378 
00386 void evdns_cancel_request(struct evdns_base *base, struct evdns_request *req);
00387 
00404 int evdns_base_set_option(struct evdns_base *base, const char *option, const char *val);
00405 
00406 
00429 int evdns_base_resolv_conf_parse(struct evdns_base *base, int flags, const char *const filename);
00430 
00441 int evdns_base_load_hosts(struct evdns_base *base, const char *hosts_fname);
00442 
00453 #ifdef WIN32
00454 int evdns_base_config_windows_nameservers(struct evdns_base *);
00455 #define EVDNS_BASE_CONFIG_WINDOWS_NAMESERVERS_IMPLEMENTED
00456 #endif
00457 
00458 
00462 void evdns_base_search_clear(struct evdns_base *base);
00463 
00464 
00470 void evdns_base_search_add(struct evdns_base *base, const char *domain);
00471 
00472 
00481 void evdns_base_search_ndots_set(struct evdns_base *base, const int ndots);
00482 
00489 typedef void (*evdns_debug_log_fn_type)(int is_warning, const char *msg);
00490 
00491 
00499 void evdns_set_log_fn(evdns_debug_log_fn_type fn);
00500 
00511 void evdns_set_transaction_id_fn(ev_uint16_t (*fn)(void));
00512 
00522 void evdns_set_random_bytes_fn(void (*fn)(char *, size_t));
00523 
00524 /*
00525  * Functions used to implement a DNS server.
00526  */
00527 
00528 struct evdns_server_request;
00529 struct evdns_server_question;
00530 
00542 typedef void (*evdns_request_callback_fn_type)(struct evdns_server_request *, void *);
00543 #define EVDNS_ANSWER_SECTION 0
00544 #define EVDNS_AUTHORITY_SECTION 1
00545 #define EVDNS_ADDITIONAL_SECTION 2
00546 
00547 #define EVDNS_TYPE_A       1
00548 #define EVDNS_TYPE_NS      2
00549 #define EVDNS_TYPE_CNAME   5
00550 #define EVDNS_TYPE_SOA     6
00551 #define EVDNS_TYPE_PTR    12
00552 #define EVDNS_TYPE_MX     15
00553 #define EVDNS_TYPE_TXT    16
00554 #define EVDNS_TYPE_AAAA   28
00555 
00556 #define EVDNS_QTYPE_AXFR 252
00557 #define EVDNS_QTYPE_ALL  255
00558 
00559 #define EVDNS_CLASS_INET   1
00560 
00561 /* flags that can be set in answers; as part of the err parameter */
00562 #define EVDNS_FLAGS_AA  0x400
00563 #define EVDNS_FLAGS_RD  0x080
00564 
00575 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);
00577 void evdns_close_server_port(struct evdns_server_port *port);
00578 
00582 void evdns_server_request_set_flags(struct evdns_server_request *req, int flags);
00583 
00584 /* Functions to add an answer to an in-progress DNS reply.
00585  */
00586 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);
00587 int evdns_server_request_add_a_reply(struct evdns_server_request *req, const char *name, int n, const void *addrs, int ttl);
00588 int evdns_server_request_add_aaaa_reply(struct evdns_server_request *req, const char *name, int n, const void *addrs, int ttl);
00589 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);
00590 int evdns_server_request_add_cname_reply(struct evdns_server_request *req, const char *name, const char *cname, int ttl);
00591 
00595 int evdns_server_request_respond(struct evdns_server_request *req, int err);
00599 int evdns_server_request_drop(struct evdns_server_request *req);
00600 struct sockaddr;
00604 int evdns_server_request_get_requesting_addr(struct evdns_server_request *_req, struct sockaddr *sa, int addr_len);
00605 
00607 typedef void (*evdns_getaddrinfo_cb)(int result, struct evutil_addrinfo *res, void *arg);
00608 
00609 struct evdns_base;
00610 struct evdns_getaddrinfo_request;
00628 struct evdns_getaddrinfo_request *evdns_getaddrinfo(
00629     struct evdns_base *dns_base,
00630     const char *nodename, const char *servname,
00631     const struct evutil_addrinfo *hints_in,
00632     evdns_getaddrinfo_cb cb, void *arg);
00633 
00634 /* Cancel an in-progress evdns_getaddrinfo.  This MUST NOT be called after the
00635  * getaddrinfo's callback has been invoked.  The resolves will be canceled,
00636  * and the callback will be invoked with the error EVUTIL_EAI_CANCEL. */
00637 void evdns_getaddrinfo_cancel(struct evdns_getaddrinfo_request *req);
00638 
00639 #ifdef __cplusplus
00640 }
00641 #endif
00642 
00643 #endif  /* !_EVENT2_DNS_H_ */
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines