Home | History | Annotate | Download | only in libevent
      1 /* Copyright 2006-2007 Niels Provos
      2  * Copyright 2007-2012 Nick Mathewson and Niels Provos
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions
      6  * are met:
      7  * 1. Redistributions of source code must retain the above copyright
      8  *    notice, this list of conditions and the following disclaimer.
      9  * 2. Redistributions in binary form must reproduce the above copyright
     10  *    notice, this list of conditions and the following disclaimer in the
     11  *    documentation and/or other materials provided with the distribution.
     12  * 3. The name of the author may not be used to endorse or promote products
     13  *    derived from this software without specific prior written permission.
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     25  */
     26 
     27 /* Based on software by Adam Langly. Adam's original message:
     28  *
     29  * Async DNS Library
     30  * Adam Langley <agl (at) imperialviolet.org>
     31  * http://www.imperialviolet.org/eventdns.html
     32  * Public Domain code
     33  *
     34  * This software is Public Domain. To view a copy of the public domain dedication,
     35  * visit http://creativecommons.org/licenses/publicdomain/ or send a letter to
     36  * Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
     37  *
     38  * I ask and expect, but do not require, that all derivative works contain an
     39  * attribution similar to:
     40  *	Parts developed by Adam Langley <agl (at) imperialviolet.org>
     41  *
     42  * You may wish to replace the word "Parts" with something else depending on
     43  * the amount of original code.
     44  *
     45  * (Derivative works does not include programs which link against, run or include
     46  * the source verbatim in their source distributions)
     47  *
     48  * Version: 0.1b
     49  */
     50 
     51 #include <sys/types.h>
     52 #include "event2/event-config.h"
     53 
     54 #ifndef _FORTIFY_SOURCE
     55 #define _FORTIFY_SOURCE 3
     56 #endif
     57 
     58 #include <string.h>
     59 #include <fcntl.h>
     60 #ifdef _EVENT_HAVE_SYS_TIME_H
     61 #include <sys/time.h>
     62 #endif
     63 #ifdef _EVENT_HAVE_STDINT_H
     64 #include <stdint.h>
     65 #endif
     66 #include <stdlib.h>
     67 #include <string.h>
     68 #include <errno.h>
     69 #ifdef _EVENT_HAVE_UNISTD_H
     70 #include <unistd.h>
     71 #endif
     72 #include <limits.h>
     73 #include <sys/stat.h>
     74 #include <stdio.h>
     75 #include <stdarg.h>
     76 #ifdef WIN32
     77 #include <winsock2.h>
     78 #include <ws2tcpip.h>
     79 #ifndef _WIN32_IE
     80 #define _WIN32_IE 0x400
     81 #endif
     82 #include <shlobj.h>
     83 #endif
     84 
     85 #include "event2/dns.h"
     86 #include "event2/dns_struct.h"
     87 #include "event2/dns_compat.h"
     88 #include "event2/util.h"
     89 #include "event2/event.h"
     90 #include "event2/event_struct.h"
     91 #include "event2/thread.h"
     92 
     93 #include "event2/bufferevent.h"
     94 #include "event2/bufferevent_struct.h"
     95 #include "bufferevent-internal.h"
     96 
     97 #include "defer-internal.h"
     98 #include "log-internal.h"
     99 #include "mm-internal.h"
    100 #include "strlcpy-internal.h"
    101 #include "ipv6-internal.h"
    102 #include "util-internal.h"
    103 #include "evthread-internal.h"
    104 #ifdef WIN32
    105 #include <ctype.h>
    106 #include <winsock2.h>
    107 #include <windows.h>
    108 #include <iphlpapi.h>
    109 #include <io.h>
    110 #else
    111 #include <sys/socket.h>
    112 #include <netinet/in.h>
    113 #include <arpa/inet.h>
    114 #endif
    115 
    116 #ifdef _EVENT_HAVE_NETINET_IN6_H
    117 #include <netinet/in6.h>
    118 #endif
    119 
    120 #define EVDNS_LOG_DEBUG 0
    121 #define EVDNS_LOG_WARN 1
    122 #define EVDNS_LOG_MSG 2
    123 
    124 #ifndef HOST_NAME_MAX
    125 #define HOST_NAME_MAX 255
    126 #endif
    127 
    128 #include <stdio.h>
    129 
    130 #undef MIN
    131 #define MIN(a,b) ((a)<(b)?(a):(b))
    132 
    133 #define ASSERT_VALID_REQUEST(req) \
    134 	EVUTIL_ASSERT((req)->handle && (req)->handle->current_req == (req))
    135 
    136 #define u64 ev_uint64_t
    137 #define u32 ev_uint32_t
    138 #define u16 ev_uint16_t
    139 #define u8  ev_uint8_t
    140 
    141 /* maximum number of addresses from a single packet */
    142 /* that we bother recording */
    143 #define MAX_V4_ADDRS 32
    144 #define MAX_V6_ADDRS 32
    145 
    146 
    147 #define TYPE_A	       EVDNS_TYPE_A
    148 #define TYPE_CNAME     5
    149 #define TYPE_PTR       EVDNS_TYPE_PTR
    150 #define TYPE_SOA       EVDNS_TYPE_SOA
    151 #define TYPE_AAAA      EVDNS_TYPE_AAAA
    152 
    153 #define CLASS_INET     EVDNS_CLASS_INET
    154 
    155 /* Persistent handle.  We keep this separate from 'struct request' since we
    156  * need some object to last for as long as an evdns_request is outstanding so
    157  * that it can be canceled, whereas a search request can lead to multiple
    158  * 'struct request' instances being created over its lifetime. */
    159 struct evdns_request {
    160 	struct request *current_req;
    161 	struct evdns_base *base;
    162 
    163 	int pending_cb; /* Waiting for its callback to be invoked; not
    164 			 * owned by event base any more. */
    165 
    166 	/* elements used by the searching code */
    167 	int search_index;
    168 	struct search_state *search_state;
    169 	char *search_origname;	/* needs to be free()ed */
    170 	int search_flags;
    171 };
    172 
    173 struct request {
    174 	u8 *request;  /* the dns packet data */
    175 	u8 request_type; /* TYPE_PTR or TYPE_A or TYPE_AAAA */
    176 	unsigned int request_len;
    177 	int reissue_count;
    178 	int tx_count;  /* the number of times that this packet has been sent */
    179 	void *user_pointer;  /* the pointer given to us for this request */
    180 	evdns_callback_type user_callback;
    181 	struct nameserver *ns;	/* the server which we last sent it */
    182 
    183 	/* these objects are kept in a circular list */
    184 	/* XXX We could turn this into a CIRCLEQ. */
    185 	struct request *next, *prev;
    186 
    187 	struct event timeout_event;
    188 
    189 	u16 trans_id;  /* the transaction id */
    190 	unsigned request_appended :1;	/* true if the request pointer is data which follows this struct */
    191 	unsigned transmit_me :1;  /* needs to be transmitted */
    192 
    193 	/* XXXX This is a horrible hack. */
    194 	char **put_cname_in_ptr; /* store the cname here if we get one. */
    195 
    196 	struct evdns_base *base;
    197 
    198 	struct evdns_request *handle;
    199 };
    200 
    201 struct reply {
    202 	unsigned int type;
    203 	unsigned int have_answer : 1;
    204 	union {
    205 		struct {
    206 			u32 addrcount;
    207 			u32 addresses[MAX_V4_ADDRS];
    208 		} a;
    209 		struct {
    210 			u32 addrcount;
    211 			struct in6_addr addresses[MAX_V6_ADDRS];
    212 		} aaaa;
    213 		struct {
    214 			char name[HOST_NAME_MAX];
    215 		} ptr;
    216 	} data;
    217 };
    218 
    219 struct nameserver {
    220 	evutil_socket_t socket;	 /* a connected UDP socket */
    221 	struct sockaddr_storage address;
    222 	ev_socklen_t addrlen;
    223 	int failed_times;  /* number of times which we have given this server a chance */
    224 	int timedout;  /* number of times in a row a request has timed out */
    225 	struct event event;
    226 	/* these objects are kept in a circular list */
    227 	struct nameserver *next, *prev;
    228 	struct event timeout_event;  /* used to keep the timeout for */
    229 				     /* when we next probe this server. */
    230 				     /* Valid if state == 0 */
    231 	/* Outstanding probe request for this nameserver, if any */
    232 	struct evdns_request *probe_request;
    233 	char state;  /* zero if we think that this server is down */
    234 	char choked;  /* true if we have an EAGAIN from this server's socket */
    235 	char write_waiting;  /* true if we are waiting for EV_WRITE events */
    236 	struct evdns_base *base;
    237 };
    238 
    239 
    240 /* Represents a local port where we're listening for DNS requests. Right now, */
    241 /* only UDP is supported. */
    242 struct evdns_server_port {
    243 	evutil_socket_t socket; /* socket we use to read queries and write replies. */
    244 	int refcnt; /* reference count. */
    245 	char choked; /* Are we currently blocked from writing? */
    246 	char closing; /* Are we trying to close this port, pending writes? */
    247 	evdns_request_callback_fn_type user_callback; /* Fn to handle requests */
    248 	void *user_data; /* Opaque pointer passed to user_callback */
    249 	struct event event; /* Read/write event */
    250 	/* circular list of replies that we want to write. */
    251 	struct server_request *pending_replies;
    252 	struct event_base *event_base;
    253 
    254 #ifndef _EVENT_DISABLE_THREAD_SUPPORT
    255 	void *lock;
    256 #endif
    257 };
    258 
    259 /* Represents part of a reply being built.	(That is, a single RR.) */
    260 struct server_reply_item {
    261 	struct server_reply_item *next; /* next item in sequence. */
    262 	char *name; /* name part of the RR */
    263 	u16 type; /* The RR type */
    264 	u16 class; /* The RR class (usually CLASS_INET) */
    265 	u32 ttl; /* The RR TTL */
    266 	char is_name; /* True iff data is a label */
    267 	u16 datalen; /* Length of data; -1 if data is a label */
    268 	void *data; /* The contents of the RR */
    269 };
    270 
    271 /* Represents a request that we've received as a DNS server, and holds */
    272 /* the components of the reply as we're constructing it. */
    273 struct server_request {
    274 	/* Pointers to the next and previous entries on the list of replies */
    275 	/* that we're waiting to write.	 Only set if we have tried to respond */
    276 	/* and gotten EAGAIN. */
    277 	struct server_request *next_pending;
    278 	struct server_request *prev_pending;
    279 
    280 	u16 trans_id; /* Transaction id. */
    281 	struct evdns_server_port *port; /* Which port received this request on? */
    282 	struct sockaddr_storage addr; /* Where to send the response */
    283 	ev_socklen_t addrlen; /* length of addr */
    284 
    285 	int n_answer; /* how many answer RRs have been set? */
    286 	int n_authority; /* how many authority RRs have been set? */
    287 	int n_additional; /* how many additional RRs have been set? */
    288 
    289 	struct server_reply_item *answer; /* linked list of answer RRs */
    290 	struct server_reply_item *authority; /* linked list of authority RRs */
    291 	struct server_reply_item *additional; /* linked list of additional RRs */
    292 
    293 	/* Constructed response.  Only set once we're ready to send a reply. */
    294 	/* Once this is set, the RR fields are cleared, and no more should be set. */
    295 	char *response;
    296 	size_t response_len;
    297 
    298 	/* Caller-visible fields: flags, questions. */
    299 	struct evdns_server_request base;
    300 };
    301 
    302 struct evdns_base {
    303 	/* An array of n_req_heads circular lists for inflight requests.
    304 	 * Each inflight request req is in req_heads[req->trans_id % n_req_heads].
    305 	 */
    306 	struct request **req_heads;
    307 	/* A circular list of requests that we're waiting to send, but haven't
    308 	 * sent yet because there are too many requests inflight */
    309 	struct request *req_waiting_head;
    310 	/* A circular list of nameservers. */
    311 	struct nameserver *server_head;
    312 	int n_req_heads;
    313 
    314 	struct event_base *event_base;
    315 
    316 	/* The number of good nameservers that we have */
    317 	int global_good_nameservers;
    318 
    319 	/* inflight requests are contained in the req_head list */
    320 	/* and are actually going out across the network */
    321 	int global_requests_inflight;
    322 	/* requests which aren't inflight are in the waiting list */
    323 	/* and are counted here */
    324 	int global_requests_waiting;
    325 
    326 	int global_max_requests_inflight;
    327 
    328 	struct timeval global_timeout;	/* 5 seconds by default */
    329 	int global_max_reissues;  /* a reissue occurs when we get some errors from the server */
    330 	int global_max_retransmits;  /* number of times we'll retransmit a request which timed out */
    331 	/* number of timeouts in a row before we consider this server to be down */
    332 	int global_max_nameserver_timeout;
    333 	/* true iff we will use the 0x20 hack to prevent poisoning attacks. */
    334 	int global_randomize_case;
    335 
    336 	/* The first time that a nameserver fails, how long do we wait before
    337 	 * probing to see if it has returned?  */
    338 	struct timeval global_nameserver_probe_initial_timeout;
    339 
    340 	/** Port to bind to for outgoing DNS packets. */
    341 	struct sockaddr_storage global_outgoing_address;
    342 	/** ev_socklen_t for global_outgoing_address. 0 if it isn't set. */
    343 	ev_socklen_t global_outgoing_addrlen;
    344 
    345 	struct timeval global_getaddrinfo_allow_skew;
    346 
    347 	int getaddrinfo_ipv4_timeouts;
    348 	int getaddrinfo_ipv6_timeouts;
    349 	int getaddrinfo_ipv4_answered;
    350 	int getaddrinfo_ipv6_answered;
    351 
    352 	struct search_state *global_search_state;
    353 
    354 	TAILQ_HEAD(hosts_list, hosts_entry) hostsdb;
    355 
    356 #ifndef _EVENT_DISABLE_THREAD_SUPPORT
    357 	void *lock;
    358 #endif
    359 };
    360 
    361 struct hosts_entry {
    362 	TAILQ_ENTRY(hosts_entry) next;
    363 	union {
    364 		struct sockaddr sa;
    365 		struct sockaddr_in sin;
    366 		struct sockaddr_in6 sin6;
    367 	} addr;
    368 	int addrlen;
    369 	char hostname[1];
    370 };
    371 
    372 static struct evdns_base *current_base = NULL;
    373 
    374 struct evdns_base *
    375 evdns_get_global_base(void)
    376 {
    377 	return current_base;
    378 }
    379 
    380 /* Given a pointer to an evdns_server_request, get the corresponding */
    381 /* server_request. */
    382 #define TO_SERVER_REQUEST(base_ptr)					\
    383 	((struct server_request*)					\
    384 	  (((char*)(base_ptr) - evutil_offsetof(struct server_request, base))))
    385 
    386 #define REQ_HEAD(base, id) ((base)->req_heads[id % (base)->n_req_heads])
    387 
    388 static struct nameserver *nameserver_pick(struct evdns_base *base);
    389 static void evdns_request_insert(struct request *req, struct request **head);
    390 static void evdns_request_remove(struct request *req, struct request **head);
    391 static void nameserver_ready_callback(evutil_socket_t fd, short events, void *arg);
    392 static int evdns_transmit(struct evdns_base *base);
    393 static int evdns_request_transmit(struct request *req);
    394 static void nameserver_send_probe(struct nameserver *const ns);
    395 static void search_request_finished(struct evdns_request *const);
    396 static int search_try_next(struct evdns_request *const req);
    397 static struct request *search_request_new(struct evdns_base *base, struct evdns_request *handle, int type, const char *const name, int flags, evdns_callback_type user_callback, void *user_arg);
    398 static void evdns_requests_pump_waiting_queue(struct evdns_base *base);
    399 static u16 transaction_id_pick(struct evdns_base *base);
    400 static struct request *request_new(struct evdns_base *base, struct evdns_request *handle, int type, const char *name, int flags, evdns_callback_type callback, void *ptr);
    401 static void request_submit(struct request *const req);
    402 
    403 static int server_request_free(struct server_request *req);
    404 static void server_request_free_answers(struct server_request *req);
    405 static void server_port_free(struct evdns_server_port *port);
    406 static void server_port_ready_callback(evutil_socket_t fd, short events, void *arg);
    407 static int evdns_base_resolv_conf_parse_impl(struct evdns_base *base, int flags, const char *const filename);
    408 static int evdns_base_set_option_impl(struct evdns_base *base,
    409     const char *option, const char *val, int flags);
    410 static void evdns_base_free_and_unlock(struct evdns_base *base, int fail_requests);
    411 
    412 static int strtoint(const char *const str);
    413 
    414 #ifdef _EVENT_DISABLE_THREAD_SUPPORT
    415 #define EVDNS_LOCK(base)  _EVUTIL_NIL_STMT
    416 #define EVDNS_UNLOCK(base) _EVUTIL_NIL_STMT
    417 #define ASSERT_LOCKED(base) _EVUTIL_NIL_STMT
    418 #else
    419 #define EVDNS_LOCK(base)			\
    420 	EVLOCK_LOCK((base)->lock, 0)
    421 #define EVDNS_UNLOCK(base)			\
    422 	EVLOCK_UNLOCK((base)->lock, 0)
    423 #define ASSERT_LOCKED(base)			\
    424 	EVLOCK_ASSERT_LOCKED((base)->lock)
    425 #endif
    426 
    427 static void
    428 default_evdns_log_fn(int warning, const char *buf)
    429 {
    430 	if (warning == EVDNS_LOG_WARN)
    431 		event_warnx("[evdns] %s", buf);
    432 	else if (warning == EVDNS_LOG_MSG)
    433 		event_msgx("[evdns] %s", buf);
    434 	else
    435 		event_debug(("[evdns] %s", buf));
    436 }
    437 
    438 static evdns_debug_log_fn_type evdns_log_fn = NULL;
    439 
    440 void
    441 evdns_set_log_fn(evdns_debug_log_fn_type fn)
    442 {
    443 	evdns_log_fn = fn;
    444 }
    445 
    446 #ifdef __GNUC__
    447 #define EVDNS_LOG_CHECK	 __attribute__ ((format(printf, 2, 3)))
    448 #else
    449 #define EVDNS_LOG_CHECK
    450 #endif
    451 
    452 static void _evdns_log(int warn, const char *fmt, ...) EVDNS_LOG_CHECK;
    453 static void
    454 _evdns_log(int warn, const char *fmt, ...)
    455 {
    456 	va_list args;
    457 	char buf[512];
    458 	if (!evdns_log_fn)
    459 		return;
    460 	va_start(args,fmt);
    461 	evutil_vsnprintf(buf, sizeof(buf), fmt, args);
    462 	va_end(args);
    463 	if (evdns_log_fn) {
    464 		if (warn == EVDNS_LOG_MSG)
    465 			warn = EVDNS_LOG_WARN;
    466 		evdns_log_fn(warn, buf);
    467 	} else {
    468 		default_evdns_log_fn(warn, buf);
    469 	}
    470 
    471 }
    472 
    473 #define log _evdns_log
    474 
    475 /* This walks the list of inflight requests to find the */
    476 /* one with a matching transaction id. Returns NULL on */
    477 /* failure */
    478 static struct request *
    479 request_find_from_trans_id(struct evdns_base *base, u16 trans_id) {
    480 	struct request *req = REQ_HEAD(base, trans_id);
    481 	struct request *const started_at = req;
    482 
    483 	ASSERT_LOCKED(base);
    484 
    485 	if (req) {
    486 		do {
    487 			if (req->trans_id == trans_id) return req;
    488 			req = req->next;
    489 		} while (req != started_at);
    490 	}
    491 
    492 	return NULL;
    493 }
    494 
    495 /* a libevent callback function which is called when a nameserver */
    496 /* has gone down and we want to test if it has came back to life yet */
    497 static void
    498 nameserver_prod_callback(evutil_socket_t fd, short events, void *arg) {
    499 	struct nameserver *const ns = (struct nameserver *) arg;
    500 	(void)fd;
    501 	(void)events;
    502 
    503 	EVDNS_LOCK(ns->base);
    504 	nameserver_send_probe(ns);
    505 	EVDNS_UNLOCK(ns->base);
    506 }
    507 
    508 /* a libevent callback which is called when a nameserver probe (to see if */
    509 /* it has come back to life) times out. We increment the count of failed_times */
    510 /* and wait longer to send the next probe packet. */
    511 static void
    512 nameserver_probe_failed(struct nameserver *const ns) {
    513 	struct timeval timeout;
    514 	int i;
    515 
    516 	ASSERT_LOCKED(ns->base);
    517 	(void) evtimer_del(&ns->timeout_event);
    518 	if (ns->state == 1) {
    519 		/* This can happen if the nameserver acts in a way which makes us mark */
    520 		/* it as bad and then starts sending good replies. */
    521 		return;
    522 	}
    523 
    524 #define MAX_PROBE_TIMEOUT 3600
    525 #define TIMEOUT_BACKOFF_FACTOR 3
    526 
    527 	memcpy(&timeout, &ns->base->global_nameserver_probe_initial_timeout,
    528 	    sizeof(struct timeval));
    529 	for (i=ns->failed_times; i > 0 && timeout.tv_sec < MAX_PROBE_TIMEOUT; --i) {
    530 		timeout.tv_sec *= TIMEOUT_BACKOFF_FACTOR;
    531 		timeout.tv_usec *= TIMEOUT_BACKOFF_FACTOR;
    532 		if (timeout.tv_usec > 1000000) {
    533 			timeout.tv_sec += timeout.tv_usec / 1000000;
    534 			timeout.tv_usec %= 1000000;
    535 		}
    536 	}
    537 	if (timeout.tv_sec > MAX_PROBE_TIMEOUT) {
    538 		timeout.tv_sec = MAX_PROBE_TIMEOUT;
    539 		timeout.tv_usec = 0;
    540 	}
    541 
    542 	ns->failed_times++;
    543 
    544 	if (evtimer_add(&ns->timeout_event, &timeout) < 0) {
    545 		char addrbuf[128];
    546 		log(EVDNS_LOG_WARN,
    547 		    "Error from libevent when adding timer event for %s",
    548 		    evutil_format_sockaddr_port(
    549 			    (struct sockaddr *)&ns->address,
    550 			    addrbuf, sizeof(addrbuf)));
    551 	}
    552 }
    553 
    554 /* called when a nameserver has been deemed to have failed. For example, too */
    555 /* many packets have timed out etc */
    556 static void
    557 nameserver_failed(struct nameserver *const ns, const char *msg) {
    558 	struct request *req, *started_at;
    559 	struct evdns_base *base = ns->base;
    560 	int i;
    561 	char addrbuf[128];
    562 
    563 	ASSERT_LOCKED(base);
    564 	/* if this nameserver has already been marked as failed */
    565 	/* then don't do anything */
    566 	if (!ns->state) return;
    567 
    568 	log(EVDNS_LOG_MSG, "Nameserver %s has failed: %s",
    569 	    evutil_format_sockaddr_port(
    570 		    (struct sockaddr *)&ns->address,
    571 		    addrbuf, sizeof(addrbuf)),
    572 	    msg);
    573 
    574 	base->global_good_nameservers--;
    575 	EVUTIL_ASSERT(base->global_good_nameservers >= 0);
    576 	if (base->global_good_nameservers == 0) {
    577 		log(EVDNS_LOG_MSG, "All nameservers have failed");
    578 	}
    579 
    580 	ns->state = 0;
    581 	ns->failed_times = 1;
    582 
    583 	if (evtimer_add(&ns->timeout_event,
    584 		&base->global_nameserver_probe_initial_timeout) < 0) {
    585 		log(EVDNS_LOG_WARN,
    586 		    "Error from libevent when adding timer event for %s",
    587 		    evutil_format_sockaddr_port(
    588 			    (struct sockaddr *)&ns->address,
    589 			    addrbuf, sizeof(addrbuf)));
    590 		/* ???? Do more? */
    591 	}
    592 
    593 	/* walk the list of inflight requests to see if any can be reassigned to */
    594 	/* a different server. Requests in the waiting queue don't have a */
    595 	/* nameserver assigned yet */
    596 
    597 	/* if we don't have *any* good nameservers then there's no point */
    598 	/* trying to reassign requests to one */
    599 	if (!base->global_good_nameservers) return;
    600 
    601 	for (i = 0; i < base->n_req_heads; ++i) {
    602 		req = started_at = base->req_heads[i];
    603 		if (req) {
    604 			do {
    605 				if (req->tx_count == 0 && req->ns == ns) {
    606 					/* still waiting to go out, can be moved */
    607 					/* to another server */
    608 					req->ns = nameserver_pick(base);
    609 				}
    610 				req = req->next;
    611 			} while (req != started_at);
    612 		}
    613 	}
    614 }
    615 
    616 static void
    617 nameserver_up(struct nameserver *const ns)
    618 {
    619 	char addrbuf[128];
    620 	ASSERT_LOCKED(ns->base);
    621 	if (ns->state) return;
    622 	log(EVDNS_LOG_MSG, "Nameserver %s is back up",
    623 	    evutil_format_sockaddr_port(
    624 		    (struct sockaddr *)&ns->address,
    625 		    addrbuf, sizeof(addrbuf)));
    626 	evtimer_del(&ns->timeout_event);
    627 	if (ns->probe_request) {
    628 		evdns_cancel_request(ns->base, ns->probe_request);
    629 		ns->probe_request = NULL;
    630 	}
    631 	ns->state = 1;
    632 	ns->failed_times = 0;
    633 	ns->timedout = 0;
    634 	ns->base->global_good_nameservers++;
    635 }
    636 
    637 static void
    638 request_trans_id_set(struct request *const req, const u16 trans_id) {
    639 	req->trans_id = trans_id;
    640 	*((u16 *) req->request) = htons(trans_id);
    641 }
    642 
    643 /* Called to remove a request from a list and dealloc it. */
    644 /* head is a pointer to the head of the list it should be */
    645 /* removed from or NULL if the request isn't in a list. */
    646 /* when free_handle is one, free the handle as well. */
    647 static void
    648 request_finished(struct request *const req, struct request **head, int free_handle) {
    649 	struct evdns_base *base = req->base;
    650 	int was_inflight = (head != &base->req_waiting_head);
    651 	EVDNS_LOCK(base);
    652 	ASSERT_VALID_REQUEST(req);
    653 
    654 	if (head)
    655 		evdns_request_remove(req, head);
    656 
    657 	log(EVDNS_LOG_DEBUG, "Removing timeout for request %p", req);
    658 	if (was_inflight) {
    659 		evtimer_del(&req->timeout_event);
    660 		base->global_requests_inflight--;
    661 	} else {
    662 		base->global_requests_waiting--;
    663 	}
    664 	/* it was initialized during request_new / evtimer_assign */
    665 	event_debug_unassign(&req->timeout_event);
    666 
    667 	if (!req->request_appended) {
    668 		/* need to free the request data on it's own */
    669 		mm_free(req->request);
    670 	} else {
    671 		/* the request data is appended onto the header */
    672 		/* so everything gets free()ed when we: */
    673 	}
    674 
    675 	if (req->handle) {
    676 		EVUTIL_ASSERT(req->handle->current_req == req);
    677 
    678 		if (free_handle) {
    679 			search_request_finished(req->handle);
    680 			req->handle->current_req = NULL;
    681 			if (! req->handle->pending_cb) {
    682 				/* If we're planning to run the callback,
    683 				 * don't free the handle until later. */
    684 				mm_free(req->handle);
    685 			}
    686 			req->handle = NULL; /* If we have a bug, let's crash
    687 					     * early */
    688 		} else {
    689 			req->handle->current_req = NULL;
    690 		}
    691 	}
    692 
    693 	mm_free(req);
    694 
    695 	evdns_requests_pump_waiting_queue(base);
    696 	EVDNS_UNLOCK(base);
    697 }
    698 
    699 /* This is called when a server returns a funny error code. */
    700 /* We try the request again with another server. */
    701 /* */
    702 /* return: */
    703 /*   0 ok */
    704 /*   1 failed/reissue is pointless */
    705 static int
    706 request_reissue(struct request *req) {
    707 	const struct nameserver *const last_ns = req->ns;
    708 	ASSERT_LOCKED(req->base);
    709 	ASSERT_VALID_REQUEST(req);
    710 	/* the last nameserver should have been marked as failing */
    711 	/* by the caller of this function, therefore pick will try */
    712 	/* not to return it */
    713 	req->ns = nameserver_pick(req->base);
    714 	if (req->ns == last_ns) {
    715 		/* ... but pick did return it */
    716 		/* not a lot of point in trying again with the */
    717 		/* same server */
    718 		return 1;
    719 	}
    720 
    721 	req->reissue_count++;
    722 	req->tx_count = 0;
    723 	req->transmit_me = 1;
    724 
    725 	return 0;
    726 }
    727 
    728 /* this function looks for space on the inflight queue and promotes */
    729 /* requests from the waiting queue if it can. */
    730 /* */
    731 /* TODO: */
    732 /* add return code, see at nameserver_pick() and other functions. */
    733 static void
    734 evdns_requests_pump_waiting_queue(struct evdns_base *base) {
    735 	ASSERT_LOCKED(base);
    736 	while (base->global_requests_inflight < base->global_max_requests_inflight &&
    737 		   base->global_requests_waiting) {
    738 		struct request *req;
    739 
    740 		EVUTIL_ASSERT(base->req_waiting_head);
    741 		req = base->req_waiting_head;
    742 
    743 		req->ns = nameserver_pick(base);
    744 		if (!req->ns)
    745 			return;
    746 
    747 		/* move a request from the waiting queue to the inflight queue */
    748 		evdns_request_remove(req, &base->req_waiting_head);
    749 
    750 		base->global_requests_waiting--;
    751 		base->global_requests_inflight++;
    752 
    753 		request_trans_id_set(req, transaction_id_pick(base));
    754 
    755 		evdns_request_insert(req, &REQ_HEAD(base, req->trans_id));
    756 		evdns_request_transmit(req);
    757 		evdns_transmit(base);
    758 	}
    759 }
    760 
    761 /* TODO(nickm) document */
    762 struct deferred_reply_callback {
    763 	struct deferred_cb deferred;
    764 	struct evdns_request *handle;
    765 	u8 request_type;
    766 	u8 have_reply;
    767 	u32 ttl;
    768 	u32 err;
    769 	evdns_callback_type user_callback;
    770 	struct reply reply;
    771 };
    772 
    773 static void
    774 reply_run_callback(struct deferred_cb *d, void *user_pointer)
    775 {
    776 	struct deferred_reply_callback *cb =
    777 	    EVUTIL_UPCAST(d, struct deferred_reply_callback, deferred);
    778 
    779 	switch (cb->request_type) {
    780 	case TYPE_A:
    781 		if (cb->have_reply)
    782 			cb->user_callback(DNS_ERR_NONE, DNS_IPv4_A,
    783 			    cb->reply.data.a.addrcount, cb->ttl,
    784 			    cb->reply.data.a.addresses,
    785 			    user_pointer);
    786 		else
    787 			cb->user_callback(cb->err, 0, 0, cb->ttl, NULL, user_pointer);
    788 		break;
    789 	case TYPE_PTR:
    790 		if (cb->have_reply) {
    791 			char *name = cb->reply.data.ptr.name;
    792 			cb->user_callback(DNS_ERR_NONE, DNS_PTR, 1, cb->ttl,
    793 			    &name, user_pointer);
    794 		} else {
    795 			cb->user_callback(cb->err, 0, 0, cb->ttl, NULL, user_pointer);
    796 		}
    797 		break;
    798 	case TYPE_AAAA:
    799 		if (cb->have_reply)
    800 			cb->user_callback(DNS_ERR_NONE, DNS_IPv6_AAAA,
    801 			    cb->reply.data.aaaa.addrcount, cb->ttl,
    802 			    cb->reply.data.aaaa.addresses,
    803 			    user_pointer);
    804 		else
    805 			cb->user_callback(cb->err, 0, 0, cb->ttl, NULL, user_pointer);
    806 		break;
    807 	default:
    808 		EVUTIL_ASSERT(0);
    809 	}
    810 
    811 	if (cb->handle && cb->handle->pending_cb) {
    812 		mm_free(cb->handle);
    813 	}
    814 
    815 	mm_free(cb);
    816 }
    817 
    818 static void
    819 reply_schedule_callback(struct request *const req, u32 ttl, u32 err, struct reply *reply)
    820 {
    821 	struct deferred_reply_callback *d = mm_calloc(1, sizeof(*d));
    822 
    823 	if (!d) {
    824 		event_warn("%s: Couldn't allocate space for deferred callback.",
    825 		    __func__);
    826 		return;
    827 	}
    828 
    829 	ASSERT_LOCKED(req->base);
    830 
    831 	d->request_type = req->request_type;
    832 	d->user_callback = req->user_callback;
    833 	d->ttl = ttl;
    834 	d->err = err;
    835 	if (reply) {
    836 		d->have_reply = 1;
    837 		memcpy(&d->reply, reply, sizeof(struct reply));
    838 	}
    839 
    840 	if (req->handle) {
    841 		req->handle->pending_cb = 1;
    842 		d->handle = req->handle;
    843 	}
    844 
    845 	event_deferred_cb_init(&d->deferred, reply_run_callback,
    846 	    req->user_pointer);
    847 	event_deferred_cb_schedule(
    848 		event_base_get_deferred_cb_queue(req->base->event_base),
    849 		&d->deferred);
    850 }
    851 
    852 /* this processes a parsed reply packet */
    853 static void
    854 reply_handle(struct request *const req, u16 flags, u32 ttl, struct reply *reply) {
    855 	int error;
    856 	char addrbuf[128];
    857 	static const int error_codes[] = {
    858 		DNS_ERR_FORMAT, DNS_ERR_SERVERFAILED, DNS_ERR_NOTEXIST,
    859 		DNS_ERR_NOTIMPL, DNS_ERR_REFUSED
    860 	};
    861 
    862 	ASSERT_LOCKED(req->base);
    863 	ASSERT_VALID_REQUEST(req);
    864 
    865 	if (flags & 0x020f || !reply || !reply->have_answer) {
    866 		/* there was an error */
    867 		if (flags & 0x0200) {
    868 			error = DNS_ERR_TRUNCATED;
    869 		} else if (flags & 0x000f) {
    870 			u16 error_code = (flags & 0x000f) - 1;
    871 			if (error_code > 4) {
    872 				error = DNS_ERR_UNKNOWN;
    873 			} else {
    874 				error = error_codes[error_code];
    875 			}
    876 		} else if (reply && !reply->have_answer) {
    877 			error = DNS_ERR_NODATA;
    878 		} else {
    879 			error = DNS_ERR_UNKNOWN;
    880 		}
    881 
    882 		switch (error) {
    883 		case DNS_ERR_NOTIMPL:
    884 		case DNS_ERR_REFUSED:
    885 			/* we regard these errors as marking a bad nameserver */
    886 			if (req->reissue_count < req->base->global_max_reissues) {
    887 				char msg[64];
    888 				evutil_snprintf(msg, sizeof(msg), "Bad response %d (%s)",
    889 					 error, evdns_err_to_string(error));
    890 				nameserver_failed(req->ns, msg);
    891 				if (!request_reissue(req)) return;
    892 			}
    893 			break;
    894 		case DNS_ERR_SERVERFAILED:
    895 			/* rcode 2 (servfailed) sometimes means "we
    896 			 * are broken" and sometimes (with some binds)
    897 			 * means "that request was very confusing."
    898 			 * Treat this as a timeout, not a failure.
    899 			 */
    900 			log(EVDNS_LOG_DEBUG, "Got a SERVERFAILED from nameserver"
    901 				"at %s; will allow the request to time out.",
    902 			    evutil_format_sockaddr_port(
    903 				    (struct sockaddr *)&req->ns->address,
    904 				    addrbuf, sizeof(addrbuf)));
    905 			break;
    906 		default:
    907 			/* we got a good reply from the nameserver: it is up. */
    908 			if (req->handle == req->ns->probe_request) {
    909 				/* Avoid double-free */
    910 				req->ns->probe_request = NULL;
    911 			}
    912 
    913 			nameserver_up(req->ns);
    914 		}
    915 
    916 		if (req->handle->search_state &&
    917 		    req->request_type != TYPE_PTR) {
    918 			/* if we have a list of domains to search in,
    919 			 * try the next one */
    920 			if (!search_try_next(req->handle)) {
    921 				/* a new request was issued so this
    922 				 * request is finished and */
    923 				/* the user callback will be made when
    924 				 * that request (or a */
    925 				/* child of it) finishes. */
    926 				return;
    927 			}
    928 		}
    929 
    930 		/* all else failed. Pass the failure up */
    931 		reply_schedule_callback(req, ttl, error, NULL);
    932 		request_finished(req, &REQ_HEAD(req->base, req->trans_id), 1);
    933 	} else {
    934 		/* all ok, tell the user */
    935 		reply_schedule_callback(req, ttl, 0, reply);
    936 		if (req->handle == req->ns->probe_request)
    937 			req->ns->probe_request = NULL; /* Avoid double-free */
    938 		nameserver_up(req->ns);
    939 		request_finished(req, &REQ_HEAD(req->base, req->trans_id), 1);
    940 	}
    941 }
    942 
    943 static int
    944 name_parse(u8 *packet, int length, int *idx, char *name_out, int name_out_len) {
    945 	int name_end = -1;
    946 	int j = *idx;
    947 	int ptr_count = 0;
    948 #define GET32(x) do { if (j + 4 > length) goto err; memcpy(&_t32, packet + j, 4); j += 4; x = ntohl(_t32); } while (0)
    949 #define GET16(x) do { if (j + 2 > length) goto err; memcpy(&_t, packet + j, 2); j += 2; x = ntohs(_t); } while (0)
    950 #define GET8(x) do { if (j >= length) goto err; x = packet[j++]; } while (0)
    951 
    952 	char *cp = name_out;
    953 	const char *const end = name_out + name_out_len;
    954 
    955 	/* Normally, names are a series of length prefixed strings terminated */
    956 	/* with a length of 0 (the lengths are u8's < 63). */
    957 	/* However, the length can start with a pair of 1 bits and that */
    958 	/* means that the next 14 bits are a pointer within the current */
    959 	/* packet. */
    960 
    961 	for (;;) {
    962 		u8 label_len;
    963 		if (j >= length) return -1;
    964 		GET8(label_len);
    965 		if (!label_len) break;
    966 		if (label_len & 0xc0) {
    967 			u8 ptr_low;
    968 			GET8(ptr_low);
    969 			if (name_end < 0) name_end = j;
    970 			j = (((int)label_len & 0x3f) << 8) + ptr_low;
    971 			/* Make sure that the target offset is in-bounds. */
    972 			if (j < 0 || j >= length) return -1;
    973 			/* If we've jumped more times than there are characters in the
    974 			 * message, we must have a loop. */
    975 			if (++ptr_count > length) return -1;
    976 			continue;
    977 		}
    978 		if (label_len > 63) return -1;
    979 		if (cp != name_out) {
    980 			if (cp + 1 >= end) return -1;
    981 			*cp++ = '.';
    982 		}
    983 		if (cp + label_len >= end) return -1;
    984 		memcpy(cp, packet + j, label_len);
    985 		cp += label_len;
    986 		j += label_len;
    987 	}
    988 	if (cp >= end) return -1;
    989 	*cp = '\0';
    990 	if (name_end < 0)
    991 		*idx = j;
    992 	else
    993 		*idx = name_end;
    994 	return 0;
    995  err:
    996 	return -1;
    997 }
    998 
    999 /* parses a raw request from a nameserver */
   1000 static int
   1001 reply_parse(struct evdns_base *base, u8 *packet, int length) {
   1002 	int j = 0, k = 0;  /* index into packet */
   1003 	u16 _t;	 /* used by the macros */
   1004 	u32 _t32;  /* used by the macros */
   1005 	char tmp_name[256], cmp_name[256]; /* used by the macros */
   1006 	int name_matches = 0;
   1007 
   1008 	u16 trans_id, questions, answers, authority, additional, datalength;
   1009 	u16 flags = 0;
   1010 	u32 ttl, ttl_r = 0xffffffff;
   1011 	struct reply reply;
   1012 	struct request *req = NULL;
   1013 	unsigned int i;
   1014 
   1015 	ASSERT_LOCKED(base);
   1016 
   1017 	GET16(trans_id);
   1018 	GET16(flags);
   1019 	GET16(questions);
   1020 	GET16(answers);
   1021 	GET16(authority);
   1022 	GET16(additional);
   1023 	(void) authority; /* suppress "unused variable" warnings. */
   1024 	(void) additional; /* suppress "unused variable" warnings. */
   1025 
   1026 	req = request_find_from_trans_id(base, trans_id);
   1027 	if (!req) return -1;
   1028 	EVUTIL_ASSERT(req->base == base);
   1029 
   1030 	memset(&reply, 0, sizeof(reply));
   1031 
   1032 	/* If it's not an answer, it doesn't correspond to any request. */
   1033 	if (!(flags & 0x8000)) return -1;  /* must be an answer */
   1034 	if ((flags & 0x020f) && (flags & 0x020f) != DNS_ERR_NOTEXIST) {
   1035 		/* there was an error and it's not NXDOMAIN */
   1036 		goto err;
   1037 	}
   1038 	/* if (!answers) return; */  /* must have an answer of some form */
   1039 
   1040 	/* This macro skips a name in the DNS reply. */
   1041 #define SKIP_NAME						\
   1042 	do { tmp_name[0] = '\0';				\
   1043 		if (name_parse(packet, length, &j, tmp_name,	\
   1044 			sizeof(tmp_name))<0)			\
   1045 			goto err;				\
   1046 	} while (0)
   1047 #define TEST_NAME							\
   1048 	do { tmp_name[0] = '\0';					\
   1049 		cmp_name[0] = '\0';					\
   1050 		k = j;							\
   1051 		if (name_parse(packet, length, &j, tmp_name,		\
   1052 			sizeof(tmp_name))<0)				\
   1053 			goto err;					\
   1054 		if (name_parse(req->request, req->request_len, &k,	\
   1055 			cmp_name, sizeof(cmp_name))<0)			\
   1056 			goto err;					\
   1057 		if (base->global_randomize_case) {			\
   1058 			if (strcmp(tmp_name, cmp_name) == 0)		\
   1059 				name_matches = 1;			\
   1060 		} else {						\
   1061 			if (evutil_ascii_strcasecmp(tmp_name, cmp_name) == 0) \
   1062 				name_matches = 1;			\
   1063 		}							\
   1064 	} while (0)
   1065 
   1066 	reply.type = req->request_type;
   1067 
   1068 	/* skip over each question in the reply */
   1069 	for (i = 0; i < questions; ++i) {
   1070 		/* the question looks like
   1071 		 *   <label:name><u16:type><u16:class>
   1072 		 */
   1073 		TEST_NAME;
   1074 		j += 4;
   1075 		if (j > length) goto err;
   1076 	}
   1077 
   1078 	if (!name_matches)
   1079 		goto err;
   1080 
   1081 	/* now we have the answer section which looks like
   1082 	 * <label:name><u16:type><u16:class><u32:ttl><u16:len><data...>
   1083 	 */
   1084 
   1085 	for (i = 0; i < answers; ++i) {
   1086 		u16 type, class;
   1087 
   1088 		SKIP_NAME;
   1089 		GET16(type);
   1090 		GET16(class);
   1091 		GET32(ttl);
   1092 		GET16(datalength);
   1093 
   1094 		if (type == TYPE_A && class == CLASS_INET) {
   1095 			int addrcount, addrtocopy;
   1096 			if (req->request_type != TYPE_A) {
   1097 				j += datalength; continue;
   1098 			}
   1099 			if ((datalength & 3) != 0) /* not an even number of As. */
   1100 			    goto err;
   1101 			addrcount = datalength >> 2;
   1102 			addrtocopy = MIN(MAX_V4_ADDRS - reply.data.a.addrcount, (unsigned)addrcount);
   1103 
   1104 			ttl_r = MIN(ttl_r, ttl);
   1105 			/* we only bother with the first four addresses. */
   1106 			if (j + 4*addrtocopy > length) goto err;
   1107 			memcpy(&reply.data.a.addresses[reply.data.a.addrcount],
   1108 				   packet + j, 4*addrtocopy);
   1109 			j += 4*addrtocopy;
   1110 			reply.data.a.addrcount += addrtocopy;
   1111 			reply.have_answer = 1;
   1112 			if (reply.data.a.addrcount == MAX_V4_ADDRS) break;
   1113 		} else if (type == TYPE_PTR && class == CLASS_INET) {
   1114 			if (req->request_type != TYPE_PTR) {
   1115 				j += datalength; continue;
   1116 			}
   1117 			if (name_parse(packet, length, &j, reply.data.ptr.name,
   1118 						   sizeof(reply.data.ptr.name))<0)
   1119 				goto err;
   1120 			ttl_r = MIN(ttl_r, ttl);
   1121 			reply.have_answer = 1;
   1122 			break;
   1123 		} else if (type == TYPE_CNAME) {
   1124 			char cname[HOST_NAME_MAX];
   1125 			if (!req->put_cname_in_ptr || *req->put_cname_in_ptr) {
   1126 				j += datalength; continue;
   1127 			}
   1128 			if (name_parse(packet, length, &j, cname,
   1129 				sizeof(cname))<0)
   1130 				goto err;
   1131 			*req->put_cname_in_ptr = mm_strdup(cname);
   1132 		} else if (type == TYPE_AAAA && class == CLASS_INET) {
   1133 			int addrcount, addrtocopy;
   1134 			if (req->request_type != TYPE_AAAA) {
   1135 				j += datalength; continue;
   1136 			}
   1137 			if ((datalength & 15) != 0) /* not an even number of AAAAs. */
   1138 				goto err;
   1139 			addrcount = datalength >> 4;  /* each address is 16 bytes long */
   1140 			addrtocopy = MIN(MAX_V6_ADDRS - reply.data.aaaa.addrcount, (unsigned)addrcount);
   1141 			ttl_r = MIN(ttl_r, ttl);
   1142 
   1143 			/* we only bother with the first four addresses. */
   1144 			if (j + 16*addrtocopy > length) goto err;
   1145 			memcpy(&reply.data.aaaa.addresses[reply.data.aaaa.addrcount],
   1146 				   packet + j, 16*addrtocopy);
   1147 			reply.data.aaaa.addrcount += addrtocopy;
   1148 			j += 16*addrtocopy;
   1149 			reply.have_answer = 1;
   1150 			if (reply.data.aaaa.addrcount == MAX_V6_ADDRS) break;
   1151 		} else {
   1152 			/* skip over any other type of resource */
   1153 			j += datalength;
   1154 		}
   1155 	}
   1156 
   1157 	if (!reply.have_answer) {
   1158 		for (i = 0; i < authority; ++i) {
   1159 			u16 type, class;
   1160 			SKIP_NAME;
   1161 			GET16(type);
   1162 			GET16(class);
   1163 			GET32(ttl);
   1164 			GET16(datalength);
   1165 			if (type == TYPE_SOA && class == CLASS_INET) {
   1166 				u32 serial, refresh, retry, expire, minimum;
   1167 				SKIP_NAME;
   1168 				SKIP_NAME;
   1169 				GET32(serial);
   1170 				GET32(refresh);
   1171 				GET32(retry);
   1172 				GET32(expire);
   1173 				GET32(minimum);
   1174 				(void)expire;
   1175 				(void)retry;
   1176 				(void)refresh;
   1177 				(void)serial;
   1178 				ttl_r = MIN(ttl_r, ttl);
   1179 				ttl_r = MIN(ttl_r, minimum);
   1180 			} else {
   1181 				/* skip over any other type of resource */
   1182 				j += datalength;
   1183 			}
   1184 		}
   1185 	}
   1186 
   1187 	if (ttl_r == 0xffffffff)
   1188 		ttl_r = 0;
   1189 
   1190 	reply_handle(req, flags, ttl_r, &reply);
   1191 	return 0;
   1192  err:
   1193 	if (req)
   1194 		reply_handle(req, flags, 0, NULL);
   1195 	return -1;
   1196 }
   1197 
   1198 /* Parse a raw request (packet,length) sent to a nameserver port (port) from */
   1199 /* a DNS client (addr,addrlen), and if it's well-formed, call the corresponding */
   1200 /* callback. */
   1201 static int
   1202 request_parse(u8 *packet, int length, struct evdns_server_port *port, struct sockaddr *addr, ev_socklen_t addrlen)
   1203 {
   1204 	int j = 0;	/* index into packet */
   1205 	u16 _t;	 /* used by the macros */
   1206 	char tmp_name[256]; /* used by the macros */
   1207 
   1208 	int i;
   1209 	u16 trans_id, flags, questions, answers, authority, additional;
   1210 	struct server_request *server_req = NULL;
   1211 
   1212 	ASSERT_LOCKED(port);
   1213 
   1214 	/* Get the header fields */
   1215 	GET16(trans_id);
   1216 	GET16(flags);
   1217 	GET16(questions);
   1218 	GET16(answers);
   1219 	GET16(authority);
   1220 	GET16(additional);
   1221 	(void)answers;
   1222 	(void)additional;
   1223 	(void)authority;
   1224 
   1225 	if (flags & 0x8000) return -1; /* Must not be an answer. */
   1226 	flags &= 0x0110; /* Only RD and CD get preserved. */
   1227 
   1228 	server_req = mm_malloc(sizeof(struct server_request));
   1229 	if (server_req == NULL) return -1;
   1230 	memset(server_req, 0, sizeof(struct server_request));
   1231 
   1232 	server_req->trans_id = trans_id;
   1233 	memcpy(&server_req->addr, addr, addrlen);
   1234 	server_req->addrlen = addrlen;
   1235 
   1236 	server_req->base.flags = flags;
   1237 	server_req->base.nquestions = 0;
   1238 	server_req->base.questions = mm_calloc(sizeof(struct evdns_server_question *), questions);
   1239 	if (server_req->base.questions == NULL)
   1240 		goto err;
   1241 
   1242 	for (i = 0; i < questions; ++i) {
   1243 		u16 type, class;
   1244 		struct evdns_server_question *q;
   1245 		int namelen;
   1246 		if (name_parse(packet, length, &j, tmp_name, sizeof(tmp_name))<0)
   1247 			goto err;
   1248 		GET16(type);
   1249 		GET16(class);
   1250 		namelen = (int)strlen(tmp_name);
   1251 		q = mm_malloc(sizeof(struct evdns_server_question) + namelen);
   1252 		if (!q)
   1253 			goto err;
   1254 		q->type = type;
   1255 		q->dns_question_class = class;
   1256 		memcpy(q->name, tmp_name, namelen+1);
   1257 		server_req->base.questions[server_req->base.nquestions++] = q;
   1258 	}
   1259 
   1260 	/* Ignore answers, authority, and additional. */
   1261 
   1262 	server_req->port = port;
   1263 	port->refcnt++;
   1264 
   1265 	/* Only standard queries are supported. */
   1266 	if (flags & 0x7800) {
   1267 		evdns_server_request_respond(&(server_req->base), DNS_ERR_NOTIMPL);
   1268 		return -1;
   1269 	}
   1270 
   1271 	port->user_callback(&(server_req->base), port->user_data);
   1272 
   1273 	return 0;
   1274 err:
   1275 	if (server_req) {
   1276 		if (server_req->base.questions) {
   1277 			for (i = 0; i < server_req->base.nquestions; ++i)
   1278 				mm_free(server_req->base.questions[i]);
   1279 			mm_free(server_req->base.questions);
   1280 		}
   1281 		mm_free(server_req);
   1282 	}
   1283 	return -1;
   1284 
   1285 #undef SKIP_NAME
   1286 #undef GET32
   1287 #undef GET16
   1288 #undef GET8
   1289 }
   1290 
   1291 
   1292 void
   1293 evdns_set_transaction_id_fn(ev_uint16_t (*fn)(void))
   1294 {
   1295 }
   1296 
   1297 void
   1298 evdns_set_random_bytes_fn(void (*fn)(char *, size_t))
   1299 {
   1300 }
   1301 
   1302 /* Try to choose a strong transaction id which isn't already in flight */
   1303 static u16
   1304 transaction_id_pick(struct evdns_base *base) {
   1305 	ASSERT_LOCKED(base);
   1306 	for (;;) {
   1307 		u16 trans_id;
   1308 		evutil_secure_rng_get_bytes(&trans_id, sizeof(trans_id));
   1309 
   1310 		if (trans_id == 0xffff) continue;
   1311 		/* now check to see if that id is already inflight */
   1312 		if (request_find_from_trans_id(base, trans_id) == NULL)
   1313 			return trans_id;
   1314 	}
   1315 }
   1316 
   1317 /* choose a namesever to use. This function will try to ignore */
   1318 /* nameservers which we think are down and load balance across the rest */
   1319 /* by updating the server_head global each time. */
   1320 static struct nameserver *
   1321 nameserver_pick(struct evdns_base *base) {
   1322 	struct nameserver *started_at = base->server_head, *picked;
   1323 	ASSERT_LOCKED(base);
   1324 	if (!base->server_head) return NULL;
   1325 
   1326 	/* if we don't have any good nameservers then there's no */
   1327 	/* point in trying to find one. */
   1328 	if (!base->global_good_nameservers) {
   1329 		base->server_head = base->server_head->next;
   1330 		return base->server_head;
   1331 	}
   1332 
   1333 	/* remember that nameservers are in a circular list */
   1334 	for (;;) {
   1335 		if (base->server_head->state) {
   1336 			/* we think this server is currently good */
   1337 			picked = base->server_head;
   1338 			base->server_head = base->server_head->next;
   1339 			return picked;
   1340 		}
   1341 
   1342 		base->server_head = base->server_head->next;
   1343 		if (base->server_head == started_at) {
   1344 			/* all the nameservers seem to be down */
   1345 			/* so we just return this one and hope for the */
   1346 			/* best */
   1347 			EVUTIL_ASSERT(base->global_good_nameservers == 0);
   1348 			picked = base->server_head;
   1349 			base->server_head = base->server_head->next;
   1350 			return picked;
   1351 		}
   1352 	}
   1353 }
   1354 
   1355 /* this is called when a namesever socket is ready for reading */
   1356 static void
   1357 nameserver_read(struct nameserver *ns) {
   1358 	struct sockaddr_storage ss;
   1359 	ev_socklen_t addrlen = sizeof(ss);
   1360 	u8 packet[1500];
   1361 	char addrbuf[128];
   1362 	ASSERT_LOCKED(ns->base);
   1363 
   1364 	for (;;) {
   1365 		const int r = recvfrom(ns->socket, (void*)packet,
   1366 		    sizeof(packet), 0,
   1367 		    (struct sockaddr*)&ss, &addrlen);
   1368 		if (r < 0) {
   1369 			int err = evutil_socket_geterror(ns->socket);
   1370 			if (EVUTIL_ERR_RW_RETRIABLE(err))
   1371 				return;
   1372 			nameserver_failed(ns,
   1373 			    evutil_socket_error_to_string(err));
   1374 			return;
   1375 		}
   1376 		if (evutil_sockaddr_cmp((struct sockaddr*)&ss,
   1377 			(struct sockaddr*)&ns->address, 0)) {
   1378 			log(EVDNS_LOG_WARN, "Address mismatch on received "
   1379 			    "DNS packet.  Apparent source was %s",
   1380 			    evutil_format_sockaddr_port(
   1381 				    (struct sockaddr *)&ss,
   1382 				    addrbuf, sizeof(addrbuf)));
   1383 			return;
   1384 		}
   1385 
   1386 		ns->timedout = 0;
   1387 		reply_parse(ns->base, packet, r);
   1388 	}
   1389 }
   1390 
   1391 /* Read a packet from a DNS client on a server port s, parse it, and */
   1392 /* act accordingly. */
   1393 static void
   1394 server_port_read(struct evdns_server_port *s) {
   1395 	u8 packet[1500];
   1396 	struct sockaddr_storage addr;
   1397 	ev_socklen_t addrlen;
   1398 	int r;
   1399 	ASSERT_LOCKED(s);
   1400 
   1401 	for (;;) {
   1402 		addrlen = sizeof(struct sockaddr_storage);
   1403 		r = recvfrom(s->socket, (void*)packet, sizeof(packet), 0,
   1404 					 (struct sockaddr*) &addr, &addrlen);
   1405 		if (r < 0) {
   1406 			int err = evutil_socket_geterror(s->socket);
   1407 			if (EVUTIL_ERR_RW_RETRIABLE(err))
   1408 				return;
   1409 			log(EVDNS_LOG_WARN,
   1410 			    "Error %s (%d) while reading request.",
   1411 			    evutil_socket_error_to_string(err), err);
   1412 			return;
   1413 		}
   1414 		request_parse(packet, r, s, (struct sockaddr*) &addr, addrlen);
   1415 	}
   1416 }
   1417 
   1418 /* Try to write all pending replies on a given DNS server port. */
   1419 static void
   1420 server_port_flush(struct evdns_server_port *port)
   1421 {
   1422 	struct server_request *req = port->pending_replies;
   1423 	ASSERT_LOCKED(port);
   1424 	while (req) {
   1425 		int r = sendto(port->socket, req->response, (int)req->response_len, 0,
   1426 			   (struct sockaddr*) &req->addr, (ev_socklen_t)req->addrlen);
   1427 		if (r < 0) {
   1428 			int err = evutil_socket_geterror(port->socket);
   1429 			if (EVUTIL_ERR_RW_RETRIABLE(err))
   1430 				return;
   1431 			log(EVDNS_LOG_WARN, "Error %s (%d) while writing response to port; dropping", evutil_socket_error_to_string(err), err);
   1432 		}
   1433 		if (server_request_free(req)) {
   1434 			/* we released the last reference to req->port. */
   1435 			return;
   1436 		} else {
   1437 			EVUTIL_ASSERT(req != port->pending_replies);
   1438 			req = port->pending_replies;
   1439 		}
   1440 	}
   1441 
   1442 	/* We have no more pending requests; stop listening for 'writeable' events. */
   1443 	(void) event_del(&port->event);
   1444 	event_assign(&port->event, port->event_base,
   1445 				 port->socket, EV_READ | EV_PERSIST,
   1446 				 server_port_ready_callback, port);
   1447 
   1448 	if (event_add(&port->event, NULL) < 0) {
   1449 		log(EVDNS_LOG_WARN, "Error from libevent when adding event for DNS server.");
   1450 		/* ???? Do more? */
   1451 	}
   1452 }
   1453 
   1454 /* set if we are waiting for the ability to write to this server. */
   1455 /* if waiting is true then we ask libevent for EV_WRITE events, otherwise */
   1456 /* we stop these events. */
   1457 static void
   1458 nameserver_write_waiting(struct nameserver *ns, char waiting) {
   1459 	ASSERT_LOCKED(ns->base);
   1460 	if (ns->write_waiting == waiting) return;
   1461 
   1462 	ns->write_waiting = waiting;
   1463 	(void) event_del(&ns->event);
   1464 	event_assign(&ns->event, ns->base->event_base,
   1465 	    ns->socket, EV_READ | (waiting ? EV_WRITE : 0) | EV_PERSIST,
   1466 	    nameserver_ready_callback, ns);
   1467 	if (event_add(&ns->event, NULL) < 0) {
   1468 		char addrbuf[128];
   1469 		log(EVDNS_LOG_WARN, "Error from libevent when adding event for %s",
   1470 		    evutil_format_sockaddr_port(
   1471 			    (struct sockaddr *)&ns->address,
   1472 			    addrbuf, sizeof(addrbuf)));
   1473 		/* ???? Do more? */
   1474 	}
   1475 }
   1476 
   1477 /* a callback function. Called by libevent when the kernel says that */
   1478 /* a nameserver socket is ready for writing or reading */
   1479 static void
   1480 nameserver_ready_callback(evutil_socket_t fd, short events, void *arg) {
   1481 	struct nameserver *ns = (struct nameserver *) arg;
   1482 	(void)fd;
   1483 
   1484 	EVDNS_LOCK(ns->base);
   1485 	if (events & EV_WRITE) {
   1486 		ns->choked = 0;
   1487 		if (!evdns_transmit(ns->base)) {
   1488 			nameserver_write_waiting(ns, 0);
   1489 		}
   1490 	}
   1491 	if (events & EV_READ) {
   1492 		nameserver_read(ns);
   1493 	}
   1494 	EVDNS_UNLOCK(ns->base);
   1495 }
   1496 
   1497 /* a callback function. Called by libevent when the kernel says that */
   1498 /* a server socket is ready for writing or reading. */
   1499 static void
   1500 server_port_ready_callback(evutil_socket_t fd, short events, void *arg) {
   1501 	struct evdns_server_port *port = (struct evdns_server_port *) arg;
   1502 	(void) fd;
   1503 
   1504 	EVDNS_LOCK(port);
   1505 	if (events & EV_WRITE) {
   1506 		port->choked = 0;
   1507 		server_port_flush(port);
   1508 	}
   1509 	if (events & EV_READ) {
   1510 		server_port_read(port);
   1511 	}
   1512 	EVDNS_UNLOCK(port);
   1513 }
   1514 
   1515 /* This is an inefficient representation; only use it via the dnslabel_table_*
   1516  * functions, so that is can be safely replaced with something smarter later. */
   1517 #define MAX_LABELS 128
   1518 /* Structures used to implement name compression */
   1519 struct dnslabel_entry { char *v; off_t pos; };
   1520 struct dnslabel_table {
   1521 	int n_labels; /* number of current entries */
   1522 	/* map from name to position in message */
   1523 	struct dnslabel_entry labels[MAX_LABELS];
   1524 };
   1525 
   1526 /* Initialize dnslabel_table. */
   1527 static void
   1528 dnslabel_table_init(struct dnslabel_table *table)
   1529 {
   1530 	table->n_labels = 0;
   1531 }
   1532 
   1533 /* Free all storage held by table, but not the table itself. */
   1534 static void
   1535 dnslabel_clear(struct dnslabel_table *table)
   1536 {
   1537 	int i;
   1538 	for (i = 0; i < table->n_labels; ++i)
   1539 		mm_free(table->labels[i].v);
   1540 	table->n_labels = 0;
   1541 }
   1542 
   1543 /* return the position of the label in the current message, or -1 if the label */
   1544 /* hasn't been used yet. */
   1545 static int
   1546 dnslabel_table_get_pos(const struct dnslabel_table *table, const char *label)
   1547 {
   1548 	int i;
   1549 	for (i = 0; i < table->n_labels; ++i) {
   1550 		if (!strcmp(label, table->labels[i].v))
   1551 			return table->labels[i].pos;
   1552 	}
   1553 	return -1;
   1554 }
   1555 
   1556 /* remember that we've used the label at position pos */
   1557 static int
   1558 dnslabel_table_add(struct dnslabel_table *table, const char *label, off_t pos)
   1559 {
   1560 	char *v;
   1561 	int p;
   1562 	if (table->n_labels == MAX_LABELS)
   1563 		return (-1);
   1564 	v = mm_strdup(label);
   1565 	if (v == NULL)
   1566 		return (-1);
   1567 	p = table->n_labels++;
   1568 	table->labels[p].v = v;
   1569 	table->labels[p].pos = pos;
   1570 
   1571 	return (0);
   1572 }
   1573 
   1574 /* Converts a string to a length-prefixed set of DNS labels, starting */
   1575 /* at buf[j]. name and buf must not overlap. name_len should be the length */
   1576 /* of name.	 table is optional, and is used for compression. */
   1577 /* */
   1578 /* Input: abc.def */
   1579 /* Output: <3>abc<3>def<0> */
   1580 /* */
   1581 /* Returns the first index after the encoded name, or negative on error. */
   1582 /*	 -1	 label was > 63 bytes */
   1583 /*	 -2	 name too long to fit in buffer. */
   1584 /* */
   1585 static off_t
   1586 dnsname_to_labels(u8 *const buf, size_t buf_len, off_t j,
   1587 				  const char *name, const size_t name_len,
   1588 				  struct dnslabel_table *table) {
   1589 	const char *end = name + name_len;
   1590 	int ref = 0;
   1591 	u16 _t;
   1592 
   1593 #define APPEND16(x) do {						\
   1594 		if (j + 2 > (off_t)buf_len)				\
   1595 			goto overflow;					\
   1596 		_t = htons(x);						\
   1597 		memcpy(buf + j, &_t, 2);				\
   1598 		j += 2;							\
   1599 	} while (0)
   1600 #define APPEND32(x) do {						\
   1601 		if (j + 4 > (off_t)buf_len)				\
   1602 			goto overflow;					\
   1603 		_t32 = htonl(x);					\
   1604 		memcpy(buf + j, &_t32, 4);				\
   1605 		j += 4;							\
   1606 	} while (0)
   1607 
   1608 	if (name_len > 255) return -2;
   1609 
   1610 	for (;;) {
   1611 		const char *const start = name;
   1612 		if (table && (ref = dnslabel_table_get_pos(table, name)) >= 0) {
   1613 			APPEND16(ref | 0xc000);
   1614 			return j;
   1615 		}
   1616 		name = strchr(name, '.');
   1617 		if (!name) {
   1618 			const size_t label_len = end - start;
   1619 			if (label_len > 63) return -1;
   1620 			if ((size_t)(j+label_len+1) > buf_len) return -2;
   1621 			if (table) dnslabel_table_add(table, start, j);
   1622 			buf[j++] = (ev_uint8_t)label_len;
   1623 
   1624 			memcpy(buf + j, start, label_len);
   1625 			j += (int) label_len;
   1626 			break;
   1627 		} else {
   1628 			/* append length of the label. */
   1629 			const size_t label_len = name - start;
   1630 			if (label_len > 63) return -1;
   1631 			if ((size_t)(j+label_len+1) > buf_len) return -2;
   1632 			if (table) dnslabel_table_add(table, start, j);
   1633 			buf[j++] = (ev_uint8_t)label_len;
   1634 
   1635 			memcpy(buf + j, start, label_len);
   1636 			j += (int) label_len;
   1637 			/* hop over the '.' */
   1638 			name++;
   1639 		}
   1640 	}
   1641 
   1642 	/* the labels must be terminated by a 0. */
   1643 	/* It's possible that the name ended in a . */
   1644 	/* in which case the zero is already there */
   1645 	if (!j || buf[j-1]) buf[j++] = 0;
   1646 	return j;
   1647  overflow:
   1648 	return (-2);
   1649 }
   1650 
   1651 /* Finds the length of a dns request for a DNS name of the given */
   1652 /* length. The actual request may be smaller than the value returned */
   1653 /* here */
   1654 static size_t
   1655 evdns_request_len(const size_t name_len) {
   1656 	return 96 + /* length of the DNS standard header */
   1657 		name_len + 2 +
   1658 		4;  /* space for the resource type */
   1659 }
   1660 
   1661 /* build a dns request packet into buf. buf should be at least as long */
   1662 /* as evdns_request_len told you it should be. */
   1663 /* */
   1664 /* Returns the amount of space used. Negative on error. */
   1665 static int
   1666 evdns_request_data_build(const char *const name, const size_t name_len,
   1667     const u16 trans_id, const u16 type, const u16 class,
   1668     u8 *const buf, size_t buf_len) {
   1669 	off_t j = 0;  /* current offset into buf */
   1670 	u16 _t;	 /* used by the macros */
   1671 
   1672 	APPEND16(trans_id);
   1673 	APPEND16(0x0100);  /* standard query, recusion needed */
   1674 	APPEND16(1);  /* one question */
   1675 	APPEND16(0);  /* no answers */
   1676 	APPEND16(0);  /* no authority */
   1677 	APPEND16(0);  /* no additional */
   1678 
   1679 	j = dnsname_to_labels(buf, buf_len, j, name, name_len, NULL);
   1680 	if (j < 0) {
   1681 		return (int)j;
   1682 	}
   1683 
   1684 	APPEND16(type);
   1685 	APPEND16(class);
   1686 
   1687 	return (int)j;
   1688  overflow:
   1689 	return (-1);
   1690 }
   1691 
   1692 /* exported function */
   1693 struct evdns_server_port *
   1694 evdns_add_server_port_with_base(struct event_base *base, evutil_socket_t socket, int flags, evdns_request_callback_fn_type cb, void *user_data)
   1695 {
   1696 	struct evdns_server_port *port;
   1697 	if (flags)
   1698 		return NULL; /* flags not yet implemented */
   1699 	if (!(port = mm_malloc(sizeof(struct evdns_server_port))))
   1700 		return NULL;
   1701 	memset(port, 0, sizeof(struct evdns_server_port));
   1702 
   1703 
   1704 	port->socket = socket;
   1705 	port->refcnt = 1;
   1706 	port->choked = 0;
   1707 	port->closing = 0;
   1708 	port->user_callback = cb;
   1709 	port->user_data = user_data;
   1710 	port->pending_replies = NULL;
   1711 	port->event_base = base;
   1712 
   1713 	event_assign(&port->event, port->event_base,
   1714 				 port->socket, EV_READ | EV_PERSIST,
   1715 				 server_port_ready_callback, port);
   1716 	if (event_add(&port->event, NULL) < 0) {
   1717 		mm_free(port);
   1718 		return NULL;
   1719 	}
   1720 	EVTHREAD_ALLOC_LOCK(port->lock, EVTHREAD_LOCKTYPE_RECURSIVE);
   1721 	return port;
   1722 }
   1723 
   1724 struct evdns_server_port *
   1725 evdns_add_server_port(evutil_socket_t socket, int flags, evdns_request_callback_fn_type cb, void *user_data)
   1726 {
   1727 	return evdns_add_server_port_with_base(NULL, socket, flags, cb, user_data);
   1728 }
   1729 
   1730 /* exported function */
   1731 void
   1732 evdns_close_server_port(struct evdns_server_port *port)
   1733 {
   1734 	EVDNS_LOCK(port);
   1735 	if (--port->refcnt == 0) {
   1736 		EVDNS_UNLOCK(port);
   1737 		server_port_free(port);
   1738 	} else {
   1739 		port->closing = 1;
   1740 	}
   1741 }
   1742 
   1743 /* exported function */
   1744 int
   1745 evdns_server_request_add_reply(struct evdns_server_request *_req, int section, const char *name, int type, int class, int ttl, int datalen, int is_name, const char *data)
   1746 {
   1747 	struct server_request *req = TO_SERVER_REQUEST(_req);
   1748 	struct server_reply_item **itemp, *item;
   1749 	int *countp;
   1750 	int result = -1;
   1751 
   1752 	EVDNS_LOCK(req->port);
   1753 	if (req->response) /* have we already answered? */
   1754 		goto done;
   1755 
   1756 	switch (section) {
   1757 	case EVDNS_ANSWER_SECTION:
   1758 		itemp = &req->answer;
   1759 		countp = &req->n_answer;
   1760 		break;
   1761 	case EVDNS_AUTHORITY_SECTION:
   1762 		itemp = &req->authority;
   1763 		countp = &req->n_authority;
   1764 		break;
   1765 	case EVDNS_ADDITIONAL_SECTION:
   1766 		itemp = &req->additional;
   1767 		countp = &req->n_additional;
   1768 		break;
   1769 	default:
   1770 		goto done;
   1771 	}
   1772 	while (*itemp) {
   1773 		itemp = &((*itemp)->next);
   1774 	}
   1775 	item = mm_malloc(sizeof(struct server_reply_item));
   1776 	if (!item)
   1777 		goto done;
   1778 	item->next = NULL;
   1779 	if (!(item->name = mm_strdup(name))) {
   1780 		mm_free(item);
   1781 		goto done;
   1782 	}
   1783 	item->type = type;
   1784 	item->dns_question_class = class;
   1785 	item->ttl = ttl;
   1786 	item->is_name = is_name != 0;
   1787 	item->datalen = 0;
   1788 	item->data = NULL;
   1789 	if (data) {
   1790 		if (item->is_name) {
   1791 			if (!(item->data = mm_strdup(data))) {
   1792 				mm_free(item->name);
   1793 				mm_free(item);
   1794 				goto done;
   1795 			}
   1796 			item->datalen = (u16)-1;
   1797 		} else {
   1798 			if (!(item->data = mm_malloc(datalen))) {
   1799 				mm_free(item->name);
   1800 				mm_free(item);
   1801 				goto done;
   1802 			}
   1803 			item->datalen = datalen;
   1804 			memcpy(item->data, data, datalen);
   1805 		}
   1806 	}
   1807 
   1808 	*itemp = item;
   1809 	++(*countp);
   1810 	result = 0;
   1811 done:
   1812 	EVDNS_UNLOCK(req->port);
   1813 	return result;
   1814 }
   1815 
   1816 /* exported function */
   1817 int
   1818 evdns_server_request_add_a_reply(struct evdns_server_request *req, const char *name, int n, const void *addrs, int ttl)
   1819 {
   1820 	return evdns_server_request_add_reply(
   1821 		  req, EVDNS_ANSWER_SECTION, name, TYPE_A, CLASS_INET,
   1822 		  ttl, n*4, 0, addrs);
   1823 }
   1824 
   1825 /* exported function */
   1826 int
   1827 evdns_server_request_add_aaaa_reply(struct evdns_server_request *req, const char *name, int n, const void *addrs, int ttl)
   1828 {
   1829 	return evdns_server_request_add_reply(
   1830 		  req, EVDNS_ANSWER_SECTION, name, TYPE_AAAA, CLASS_INET,
   1831 		  ttl, n*16, 0, addrs);
   1832 }
   1833 
   1834 /* exported function */
   1835 int
   1836 evdns_server_request_add_ptr_reply(struct evdns_server_request *req, struct in_addr *in, const char *inaddr_name, const char *hostname, int ttl)
   1837 {
   1838 	u32 a;
   1839 	char buf[32];
   1840 	if (in && inaddr_name)
   1841 		return -1;
   1842 	else if (!in && !inaddr_name)
   1843 		return -1;
   1844 	if (in) {
   1845 		a = ntohl(in->s_addr);
   1846 		evutil_snprintf(buf, sizeof(buf), "%d.%d.%d.%d.in-addr.arpa",
   1847 				(int)(u8)((a	)&0xff),
   1848 				(int)(u8)((a>>8 )&0xff),
   1849 				(int)(u8)((a>>16)&0xff),
   1850 				(int)(u8)((a>>24)&0xff));
   1851 		inaddr_name = buf;
   1852 	}
   1853 	return evdns_server_request_add_reply(
   1854 		  req, EVDNS_ANSWER_SECTION, inaddr_name, TYPE_PTR, CLASS_INET,
   1855 		  ttl, -1, 1, hostname);
   1856 }
   1857 
   1858 /* exported function */
   1859 int
   1860 evdns_server_request_add_cname_reply(struct evdns_server_request *req, const char *name, const char *cname, int ttl)
   1861 {
   1862 	return evdns_server_request_add_reply(
   1863 		  req, EVDNS_ANSWER_SECTION, name, TYPE_CNAME, CLASS_INET,
   1864 		  ttl, -1, 1, cname);
   1865 }
   1866 
   1867 /* exported function */
   1868 void
   1869 evdns_server_request_set_flags(struct evdns_server_request *exreq, int flags)
   1870 {
   1871 	struct server_request *req = TO_SERVER_REQUEST(exreq);
   1872 	req->base.flags &= ~(EVDNS_FLAGS_AA|EVDNS_FLAGS_RD);
   1873 	req->base.flags |= flags;
   1874 }
   1875 
   1876 static int
   1877 evdns_server_request_format_response(struct server_request *req, int err)
   1878 {
   1879 	unsigned char buf[1500];
   1880 	size_t buf_len = sizeof(buf);
   1881 	off_t j = 0, r;
   1882 	u16 _t;
   1883 	u32 _t32;
   1884 	int i;
   1885 	u16 flags;
   1886 	struct dnslabel_table table;
   1887 
   1888 	if (err < 0 || err > 15) return -1;
   1889 
   1890 	/* Set response bit and error code; copy OPCODE and RD fields from
   1891 	 * question; copy RA and AA if set by caller. */
   1892 	flags = req->base.flags;
   1893 	flags |= (0x8000 | err);
   1894 
   1895 	dnslabel_table_init(&table);
   1896 	APPEND16(req->trans_id);
   1897 	APPEND16(flags);
   1898 	APPEND16(req->base.nquestions);
   1899 	APPEND16(req->n_answer);
   1900 	APPEND16(req->n_authority);
   1901 	APPEND16(req->n_additional);
   1902 
   1903 	/* Add questions. */
   1904 	for (i=0; i < req->base.nquestions; ++i) {
   1905 		const char *s = req->base.questions[i]->name;
   1906 		j = dnsname_to_labels(buf, buf_len, j, s, strlen(s), &table);
   1907 		if (j < 0) {
   1908 			dnslabel_clear(&table);
   1909 			return (int) j;
   1910 		}
   1911 		APPEND16(req->base.questions[i]->type);
   1912 		APPEND16(req->base.questions[i]->dns_question_class);
   1913 	}
   1914 
   1915 	/* Add answer, authority, and additional sections. */
   1916 	for (i=0; i<3; ++i) {
   1917 		struct server_reply_item *item;
   1918 		if (i==0)
   1919 			item = req->answer;
   1920 		else if (i==1)
   1921 			item = req->authority;
   1922 		else
   1923 			item = req->additional;
   1924 		while (item) {
   1925 			r = dnsname_to_labels(buf, buf_len, j, item->name, strlen(item->name), &table);
   1926 			if (r < 0)
   1927 				goto overflow;
   1928 			j = r;
   1929 
   1930 			APPEND16(item->type);
   1931 			APPEND16(item->dns_question_class);
   1932 			APPEND32(item->ttl);
   1933 			if (item->is_name) {
   1934 				off_t len_idx = j, name_start;
   1935 				j += 2;
   1936 				name_start = j;
   1937 				r = dnsname_to_labels(buf, buf_len, j, item->data, strlen(item->data), &table);
   1938 				if (r < 0)
   1939 					goto overflow;
   1940 				j = r;
   1941 				_t = htons( (short) (j-name_start) );
   1942 				memcpy(buf+len_idx, &_t, 2);
   1943 			} else {
   1944 				APPEND16(item->datalen);
   1945 				if (j+item->datalen > (off_t)buf_len)
   1946 					goto overflow;
   1947 				memcpy(buf+j, item->data, item->datalen);
   1948 				j += item->datalen;
   1949 			}
   1950 			item = item->next;
   1951 		}
   1952 	}
   1953 
   1954 	if (j > 512) {
   1955 overflow:
   1956 		j = 512;
   1957 		buf[2] |= 0x02; /* set the truncated bit. */
   1958 	}
   1959 
   1960 	req->response_len = j;
   1961 
   1962 	if (!(req->response = mm_malloc(req->response_len))) {
   1963 		server_request_free_answers(req);
   1964 		dnslabel_clear(&table);
   1965 		return (-1);
   1966 	}
   1967 	memcpy(req->response, buf, req->response_len);
   1968 	server_request_free_answers(req);
   1969 	dnslabel_clear(&table);
   1970 	return (0);
   1971 }
   1972 
   1973 /* exported function */
   1974 int
   1975 evdns_server_request_respond(struct evdns_server_request *_req, int err)
   1976 {
   1977 	struct server_request *req = TO_SERVER_REQUEST(_req);
   1978 	struct evdns_server_port *port = req->port;
   1979 	int r = -1;
   1980 
   1981 	EVDNS_LOCK(port);
   1982 	if (!req->response) {
   1983 		if ((r = evdns_server_request_format_response(req, err))<0)
   1984 			goto done;
   1985 	}
   1986 
   1987 	r = sendto(port->socket, req->response, (int)req->response_len, 0,
   1988 			   (struct sockaddr*) &req->addr, (ev_socklen_t)req->addrlen);
   1989 	if (r<0) {
   1990 		int sock_err = evutil_socket_geterror(port->socket);
   1991 		if (EVUTIL_ERR_RW_RETRIABLE(sock_err))
   1992 			goto done;
   1993 
   1994 		if (port->pending_replies) {
   1995 			req->prev_pending = port->pending_replies->prev_pending;
   1996 			req->next_pending = port->pending_replies;
   1997 			req->prev_pending->next_pending =
   1998 				req->next_pending->prev_pending = req;
   1999 		} else {
   2000 			req->prev_pending = req->next_pending = req;
   2001 			port->pending_replies = req;
   2002 			port->choked = 1;
   2003 
   2004 			(void) event_del(&port->event);
   2005 			event_assign(&port->event, port->event_base, port->socket, (port->closing?0:EV_READ) | EV_WRITE | EV_PERSIST, server_port_ready_callback, port);
   2006 
   2007 			if (event_add(&port->event, NULL) < 0) {
   2008 				log(EVDNS_LOG_WARN, "Error from libevent when adding event for DNS server");
   2009 			}
   2010 
   2011 		}
   2012 
   2013 		r = 1;
   2014 		goto done;
   2015 	}
   2016 	if (server_request_free(req)) {
   2017 		r = 0;
   2018 		goto done;
   2019 	}
   2020 
   2021 	if (port->pending_replies)
   2022 		server_port_flush(port);
   2023 
   2024 	r = 0;
   2025 done:
   2026 	EVDNS_UNLOCK(port);
   2027 	return r;
   2028 }
   2029 
   2030 /* Free all storage held by RRs in req. */
   2031 static void
   2032 server_request_free_answers(struct server_request *req)
   2033 {
   2034 	struct server_reply_item *victim, *next, **list;
   2035 	int i;
   2036 	for (i = 0; i < 3; ++i) {
   2037 		if (i==0)
   2038 			list = &req->answer;
   2039 		else if (i==1)
   2040 			list = &req->authority;
   2041 		else
   2042 			list = &req->additional;
   2043 
   2044 		victim = *list;
   2045 		while (victim) {
   2046 			next = victim->next;
   2047 			mm_free(victim->name);
   2048 			if (victim->data)
   2049 				mm_free(victim->data);
   2050 			mm_free(victim);
   2051 			victim = next;
   2052 		}
   2053 		*list = NULL;
   2054 	}
   2055 }
   2056 
   2057 /* Free all storage held by req, and remove links to it. */
   2058 /* return true iff we just wound up freeing the server_port. */
   2059 static int
   2060 server_request_free(struct server_request *req)
   2061 {
   2062 	int i, rc=1, lock=0;
   2063 	if (req->base.questions) {
   2064 		for (i = 0; i < req->base.nquestions; ++i)
   2065 			mm_free(req->base.questions[i]);
   2066 		mm_free(req->base.questions);
   2067 	}
   2068 
   2069 	if (req->port) {
   2070 		EVDNS_LOCK(req->port);
   2071 		lock=1;
   2072 		if (req->port->pending_replies == req) {
   2073 			if (req->next_pending && req->next_pending != req)
   2074 				req->port->pending_replies = req->next_pending;
   2075 			else
   2076 				req->port->pending_replies = NULL;
   2077 		}
   2078 		rc = --req->port->refcnt;
   2079 	}
   2080 
   2081 	if (req->response) {
   2082 		mm_free(req->response);
   2083 	}
   2084 
   2085 	server_request_free_answers(req);
   2086 
   2087 	if (req->next_pending && req->next_pending != req) {
   2088 		req->next_pending->prev_pending = req->prev_pending;
   2089 		req->prev_pending->next_pending = req->next_pending;
   2090 	}
   2091 
   2092 	if (rc == 0) {
   2093 		EVDNS_UNLOCK(req->port); /* ????? nickm */
   2094 		server_port_free(req->port);
   2095 		mm_free(req);
   2096 		return (1);
   2097 	}
   2098 	if (lock)
   2099 		EVDNS_UNLOCK(req->port);
   2100 	mm_free(req);
   2101 	return (0);
   2102 }
   2103 
   2104 /* Free all storage held by an evdns_server_port.  Only called when  */
   2105 static void
   2106 server_port_free(struct evdns_server_port *port)
   2107 {
   2108 	EVUTIL_ASSERT(port);
   2109 	EVUTIL_ASSERT(!port->refcnt);
   2110 	EVUTIL_ASSERT(!port->pending_replies);
   2111 	if (port->socket > 0) {
   2112 		evutil_closesocket(port->socket);
   2113 		port->socket = -1;
   2114 	}
   2115 	(void) event_del(&port->event);
   2116 	event_debug_unassign(&port->event);
   2117 	EVTHREAD_FREE_LOCK(port->lock, EVTHREAD_LOCKTYPE_RECURSIVE);
   2118 	mm_free(port);
   2119 }
   2120 
   2121 /* exported function */
   2122 int
   2123 evdns_server_request_drop(struct evdns_server_request *_req)
   2124 {
   2125 	struct server_request *req = TO_SERVER_REQUEST(_req);
   2126 	server_request_free(req);
   2127 	return 0;
   2128 }
   2129 
   2130 /* exported function */
   2131 int
   2132 evdns_server_request_get_requesting_addr(struct evdns_server_request *_req, struct sockaddr *sa, int addr_len)
   2133 {
   2134 	struct server_request *req = TO_SERVER_REQUEST(_req);
   2135 	if (addr_len < (int)req->addrlen)
   2136 		return -1;
   2137 	memcpy(sa, &(req->addr), req->addrlen);
   2138 	return req->addrlen;
   2139 }
   2140 
   2141 #undef APPEND16
   2142 #undef APPEND32
   2143 
   2144 /* this is a libevent callback function which is called when a request */
   2145 /* has timed out. */
   2146 static void
   2147 evdns_request_timeout_callback(evutil_socket_t fd, short events, void *arg) {
   2148 	struct request *const req = (struct request *) arg;
   2149 	struct evdns_base *base = req->base;
   2150 
   2151 	(void) fd;
   2152 	(void) events;
   2153 
   2154 	log(EVDNS_LOG_DEBUG, "Request %p timed out", arg);
   2155 	EVDNS_LOCK(base);
   2156 
   2157 	req->ns->timedout++;
   2158 	if (req->ns->timedout > req->base->global_max_nameserver_timeout) {
   2159 		req->ns->timedout = 0;
   2160 		nameserver_failed(req->ns, "request timed out.");
   2161 	}
   2162 
   2163 	if (req->tx_count >= req->base->global_max_retransmits) {
   2164 		/* this request has failed */
   2165 		log(EVDNS_LOG_DEBUG, "Giving up on request %p; tx_count==%d",
   2166 		    arg, req->tx_count);
   2167 		reply_schedule_callback(req, 0, DNS_ERR_TIMEOUT, NULL);
   2168 		request_finished(req, &REQ_HEAD(req->base, req->trans_id), 1);
   2169 	} else {
   2170 		/* retransmit it */
   2171 		struct nameserver *new_ns;
   2172 		log(EVDNS_LOG_DEBUG, "Retransmitting request %p; tx_count==%d",
   2173 		    arg, req->tx_count);
   2174 		(void) evtimer_del(&req->timeout_event);
   2175 		new_ns = nameserver_pick(base);
   2176 		if (new_ns)
   2177 			req->ns = new_ns;
   2178 		evdns_request_transmit(req);
   2179 	}
   2180 	EVDNS_UNLOCK(base);
   2181 }
   2182 
   2183 /* try to send a request to a given server. */
   2184 /* */
   2185 /* return: */
   2186 /*   0 ok */
   2187 /*   1 temporary failure */
   2188 /*   2 other failure */
   2189 static int
   2190 evdns_request_transmit_to(struct request *req, struct nameserver *server) {
   2191 	int r;
   2192 	ASSERT_LOCKED(req->base);
   2193 	ASSERT_VALID_REQUEST(req);
   2194 	r = sendto(server->socket, (void*)req->request, req->request_len, 0,
   2195 	    (struct sockaddr *)&server->address, server->addrlen);
   2196 	if (r < 0) {
   2197 		int err = evutil_socket_geterror(server->socket);
   2198 		if (EVUTIL_ERR_RW_RETRIABLE(err))
   2199 			return 1;
   2200 		nameserver_failed(req->ns, evutil_socket_error_to_string(err));
   2201 		return 2;
   2202 	} else if (r != (int)req->request_len) {
   2203 		return 1;  /* short write */
   2204 	} else {
   2205 		return 0;
   2206 	}
   2207 }
   2208 
   2209 /* try to send a request, updating the fields of the request */
   2210 /* as needed */
   2211 /* */
   2212 /* return: */
   2213 /*   0 ok */
   2214 /*   1 failed */
   2215 static int
   2216 evdns_request_transmit(struct request *req) {
   2217 	int retcode = 0, r;
   2218 
   2219 	ASSERT_LOCKED(req->base);
   2220 	ASSERT_VALID_REQUEST(req);
   2221 	/* if we fail to send this packet then this flag marks it */
   2222 	/* for evdns_transmit */
   2223 	req->transmit_me = 1;
   2224 	EVUTIL_ASSERT(req->trans_id != 0xffff);
   2225 
   2226 	if (!req->ns)
   2227 	{
   2228 		/* unable to transmit request if no nameservers */
   2229 		return 1;
   2230 	}
   2231 
   2232 	if (req->ns->choked) {
   2233 		/* don't bother trying to write to a socket */
   2234 		/* which we have had EAGAIN from */
   2235 		return 1;
   2236 	}
   2237 
   2238 	r = evdns_request_transmit_to(req, req->ns);
   2239 	switch (r) {
   2240 	case 1:
   2241 		/* temp failure */
   2242 		req->ns->choked = 1;
   2243 		nameserver_write_waiting(req->ns, 1);
   2244 		return 1;
   2245 	case 2:
   2246 		/* failed to transmit the request entirely. */
   2247 		retcode = 1;
   2248 		/* fall through: we'll set a timeout, which will time out,
   2249 		 * and make us retransmit the request anyway. */
   2250 	default:
   2251 		/* all ok */
   2252 		log(EVDNS_LOG_DEBUG,
   2253 		    "Setting timeout for request %p, sent to nameserver %p", req, req->ns);
   2254 		if (evtimer_add(&req->timeout_event, &req->base->global_timeout) < 0) {
   2255 			log(EVDNS_LOG_WARN,
   2256 		      "Error from libevent when adding timer for request %p",
   2257 			    req);
   2258 			/* ???? Do more? */
   2259 		}
   2260 		req->tx_count++;
   2261 		req->transmit_me = 0;
   2262 		return retcode;
   2263 	}
   2264 }
   2265 
   2266 static void
   2267 nameserver_probe_callback(int result, char type, int count, int ttl, void *addresses, void *arg) {
   2268 	struct nameserver *const ns = (struct nameserver *) arg;
   2269 	(void) type;
   2270 	(void) count;
   2271 	(void) ttl;
   2272 	(void) addresses;
   2273 
   2274 	if (result == DNS_ERR_CANCEL) {
   2275 		/* We canceled this request because the nameserver came up
   2276 		 * for some other reason.  Do not change our opinion about
   2277 		 * the nameserver. */
   2278 		return;
   2279 	}
   2280 
   2281 	EVDNS_LOCK(ns->base);
   2282 	ns->probe_request = NULL;
   2283 	if (result == DNS_ERR_NONE || result == DNS_ERR_NOTEXIST) {
   2284 		/* this is a good reply */
   2285 		nameserver_up(ns);
   2286 	} else {
   2287 		nameserver_probe_failed(ns);
   2288 	}
   2289 	EVDNS_UNLOCK(ns->base);
   2290 }
   2291 
   2292 static void
   2293 nameserver_send_probe(struct nameserver *const ns) {
   2294 	struct evdns_request *handle;
   2295 	struct request *req;
   2296 	char addrbuf[128];
   2297 	/* here we need to send a probe to a given nameserver */
   2298 	/* in the hope that it is up now. */
   2299 
   2300 	ASSERT_LOCKED(ns->base);
   2301 	log(EVDNS_LOG_DEBUG, "Sending probe to %s",
   2302 	    evutil_format_sockaddr_port(
   2303 		    (struct sockaddr *)&ns->address,
   2304 		    addrbuf, sizeof(addrbuf)));
   2305 	handle = mm_calloc(1, sizeof(*handle));
   2306 	if (!handle) return;
   2307 	req = request_new(ns->base, handle, TYPE_A, "google.com", DNS_QUERY_NO_SEARCH, nameserver_probe_callback, ns);
   2308 	if (!req) {
   2309 		mm_free(handle);
   2310 		return;
   2311 	}
   2312 	ns->probe_request = handle;
   2313 	/* we force this into the inflight queue no matter what */
   2314 	request_trans_id_set(req, transaction_id_pick(ns->base));
   2315 	req->ns = ns;
   2316 	request_submit(req);
   2317 }
   2318 
   2319 /* returns: */
   2320 /*   0 didn't try to transmit anything */
   2321 /*   1 tried to transmit something */
   2322 static int
   2323 evdns_transmit(struct evdns_base *base) {
   2324 	char did_try_to_transmit = 0;
   2325 	int i;
   2326 
   2327 	ASSERT_LOCKED(base);
   2328 	for (i = 0; i < base->n_req_heads; ++i) {
   2329 		if (base->req_heads[i]) {
   2330 			struct request *const started_at = base->req_heads[i], *req = started_at;
   2331 			/* first transmit all the requests which are currently waiting */
   2332 			do {
   2333 				if (req->transmit_me) {
   2334 					did_try_to_transmit = 1;
   2335 					evdns_request_transmit(req);
   2336 				}
   2337 
   2338 				req = req->next;
   2339 			} while (req != started_at);
   2340 		}
   2341 	}
   2342 
   2343 	return did_try_to_transmit;
   2344 }
   2345 
   2346 /* exported function */
   2347 int
   2348 evdns_base_count_nameservers(struct evdns_base *base)
   2349 {
   2350 	const struct nameserver *server;
   2351 	int n = 0;
   2352 
   2353 	EVDNS_LOCK(base);
   2354 	server = base->server_head;
   2355 	if (!server)
   2356 		goto done;
   2357 	do {
   2358 		++n;
   2359 		server = server->next;
   2360 	} while (server != base->server_head);
   2361 done:
   2362 	EVDNS_UNLOCK(base);
   2363 	return n;
   2364 }
   2365 
   2366 int
   2367 evdns_count_nameservers(void)
   2368 {
   2369 	return evdns_base_count_nameservers(current_base);
   2370 }
   2371 
   2372 /* exported function */
   2373 int
   2374 evdns_base_clear_nameservers_and_suspend(struct evdns_base *base)
   2375 {
   2376 	struct nameserver *server, *started_at;
   2377 	int i;
   2378 
   2379 	EVDNS_LOCK(base);
   2380 	server = base->server_head;
   2381 	started_at = base->server_head;
   2382 	if (!server) {
   2383 		EVDNS_UNLOCK(base);
   2384 		return 0;
   2385 	}
   2386 	while (1) {
   2387 		struct nameserver *next = server->next;
   2388 		(void) event_del(&server->event);
   2389 		if (evtimer_initialized(&server->timeout_event))
   2390 			(void) evtimer_del(&server->timeout_event);
   2391 		if (server->probe_request) {
   2392 			evdns_cancel_request(server->base, server->probe_request);
   2393 			server->probe_request = NULL;
   2394 		}
   2395 		if (server->socket >= 0)
   2396 			evutil_closesocket(server->socket);
   2397 		mm_free(server);
   2398 		if (next == started_at)
   2399 			break;
   2400 		server = next;
   2401 	}
   2402 	base->server_head = NULL;
   2403 	base->global_good_nameservers = 0;
   2404 
   2405 	for (i = 0; i < base->n_req_heads; ++i) {
   2406 		struct request *req, *req_started_at;
   2407 		req = req_started_at = base->req_heads[i];
   2408 		while (req) {
   2409 			struct request *next = req->next;
   2410 			req->tx_count = req->reissue_count = 0;
   2411 			req->ns = NULL;
   2412 			/* ???? What to do about searches? */
   2413 			(void) evtimer_del(&req->timeout_event);
   2414 			req->trans_id = 0;
   2415 			req->transmit_me = 0;
   2416 
   2417 			base->global_requests_waiting++;
   2418 			evdns_request_insert(req, &base->req_waiting_head);
   2419 			/* We want to insert these suspended elements at the front of
   2420 			 * the waiting queue, since they were pending before any of
   2421 			 * the waiting entries were added.  This is a circular list,
   2422 			 * so we can just shift the start back by one.*/
   2423 			base->req_waiting_head = base->req_waiting_head->prev;
   2424 
   2425 			if (next == req_started_at)
   2426 				break;
   2427 			req = next;
   2428 		}
   2429 		base->req_heads[i] = NULL;
   2430 	}
   2431 
   2432 	base->global_requests_inflight = 0;
   2433 
   2434 	EVDNS_UNLOCK(base);
   2435 	return 0;
   2436 }
   2437 
   2438 int
   2439 evdns_clear_nameservers_and_suspend(void)
   2440 {
   2441 	return evdns_base_clear_nameservers_and_suspend(current_base);
   2442 }
   2443 
   2444 
   2445 /* exported function */
   2446 int
   2447 evdns_base_resume(struct evdns_base *base)
   2448 {
   2449 	EVDNS_LOCK(base);
   2450 	evdns_requests_pump_waiting_queue(base);
   2451 	EVDNS_UNLOCK(base);
   2452 
   2453 	return 0;
   2454 }
   2455 
   2456 int
   2457 evdns_resume(void)
   2458 {
   2459 	return evdns_base_resume(current_base);
   2460 }
   2461 
   2462 static int
   2463 _evdns_nameserver_add_impl(struct evdns_base *base, const struct sockaddr *address, int addrlen) {
   2464 	/* first check to see if we already have this nameserver */
   2465 
   2466 	const struct nameserver *server = base->server_head, *const started_at = base->server_head;
   2467 	struct nameserver *ns;
   2468 	int err = 0;
   2469 	char addrbuf[128];
   2470 
   2471 	ASSERT_LOCKED(base);
   2472 	if (server) {
   2473 		do {
   2474 			if (!evutil_sockaddr_cmp((struct sockaddr*)&server->address, address, 1)) return 3;
   2475 			server = server->next;
   2476 		} while (server != started_at);
   2477 	}
   2478 	if (addrlen > (int)sizeof(ns->address)) {
   2479 		log(EVDNS_LOG_DEBUG, "Addrlen %d too long.", (int)addrlen);
   2480 		return 2;
   2481 	}
   2482 
   2483 	ns = (struct nameserver *) mm_malloc(sizeof(struct nameserver));
   2484 	if (!ns) return -1;
   2485 
   2486 	memset(ns, 0, sizeof(struct nameserver));
   2487 	ns->base = base;
   2488 
   2489 	evtimer_assign(&ns->timeout_event, ns->base->event_base, nameserver_prod_callback, ns);
   2490 
   2491 	ns->socket = socket(address->sa_family, SOCK_DGRAM, 0);
   2492 	if (ns->socket < 0) { err = 1; goto out1; }
   2493 	evutil_make_socket_closeonexec(ns->socket);
   2494 	evutil_make_socket_nonblocking(ns->socket);
   2495 
   2496 	if (base->global_outgoing_addrlen &&
   2497 	    !evutil_sockaddr_is_loopback(address)) {
   2498 		if (bind(ns->socket,
   2499 			(struct sockaddr*)&base->global_outgoing_address,
   2500 			base->global_outgoing_addrlen) < 0) {
   2501 			log(EVDNS_LOG_WARN,"Couldn't bind to outgoing address");
   2502 			err = 2;
   2503 			goto out2;
   2504 		}
   2505 	}
   2506 
   2507 	memcpy(&ns->address, address, addrlen);
   2508 	ns->addrlen = addrlen;
   2509 	ns->state = 1;
   2510 	event_assign(&ns->event, ns->base->event_base, ns->socket, EV_READ | EV_PERSIST, nameserver_ready_callback, ns);
   2511 	if (event_add(&ns->event, NULL) < 0) {
   2512 		err = 2;
   2513 		goto out2;
   2514 	}
   2515 
   2516 	log(EVDNS_LOG_DEBUG, "Added nameserver %s as %p",
   2517 	    evutil_format_sockaddr_port(address, addrbuf, sizeof(addrbuf)), ns);
   2518 
   2519 	/* insert this nameserver into the list of them */
   2520 	if (!base->server_head) {
   2521 		ns->next = ns->prev = ns;
   2522 		base->server_head = ns;
   2523 	} else {
   2524 		ns->next = base->server_head->next;
   2525 		ns->prev = base->server_head;
   2526 		base->server_head->next = ns;
   2527 		ns->next->prev = ns;
   2528 	}
   2529 
   2530 	base->global_good_nameservers++;
   2531 
   2532 	return 0;
   2533 
   2534 out2:
   2535 	evutil_closesocket(ns->socket);
   2536 out1:
   2537 	event_debug_unassign(&ns->event);
   2538 	mm_free(ns);
   2539 	log(EVDNS_LOG_WARN, "Unable to add nameserver %s: error %d",
   2540 	    evutil_format_sockaddr_port(address, addrbuf, sizeof(addrbuf)), err);
   2541 	return err;
   2542 }
   2543 
   2544 /* exported function */
   2545 int
   2546 evdns_base_nameserver_add(struct evdns_base *base, unsigned long int address)
   2547 {
   2548 	struct sockaddr_in sin;
   2549 	int res;
   2550 	memset(&sin, 0, sizeof(sin));
   2551 	sin.sin_addr.s_addr = address;
   2552 	sin.sin_port = htons(53);
   2553 	sin.sin_family = AF_INET;
   2554 	EVDNS_LOCK(base);
   2555 	res = _evdns_nameserver_add_impl(base, (struct sockaddr*)&sin, sizeof(sin));
   2556 	EVDNS_UNLOCK(base);
   2557 	return res;
   2558 }
   2559 
   2560 int
   2561 evdns_nameserver_add(unsigned long int address) {
   2562 	if (!current_base)
   2563 		current_base = evdns_base_new(NULL, 0);
   2564 	return evdns_base_nameserver_add(current_base, address);
   2565 }
   2566 
   2567 static void
   2568 sockaddr_setport(struct sockaddr *sa, ev_uint16_t port)
   2569 {
   2570 	if (sa->sa_family == AF_INET) {
   2571 		((struct sockaddr_in *)sa)->sin_port = htons(port);
   2572 	} else if (sa->sa_family == AF_INET6) {
   2573 		((struct sockaddr_in6 *)sa)->sin6_port = htons(port);
   2574 	}
   2575 }
   2576 
   2577 static ev_uint16_t
   2578 sockaddr_getport(struct sockaddr *sa)
   2579 {
   2580 	if (sa->sa_family == AF_INET) {
   2581 		return ntohs(((struct sockaddr_in *)sa)->sin_port);
   2582 	} else if (sa->sa_family == AF_INET6) {
   2583 		return ntohs(((struct sockaddr_in6 *)sa)->sin6_port);
   2584 	} else {
   2585 		return 0;
   2586 	}
   2587 }
   2588 
   2589 /* exported function */
   2590 int
   2591 evdns_base_nameserver_ip_add(struct evdns_base *base, const char *ip_as_string) {
   2592 	struct sockaddr_storage ss;
   2593 	struct sockaddr *sa;
   2594 	int len = sizeof(ss);
   2595 	int res;
   2596 	if (evutil_parse_sockaddr_port(ip_as_string, (struct sockaddr *)&ss,
   2597 		&len)) {
   2598 		log(EVDNS_LOG_WARN, "Unable to parse nameserver address %s",
   2599 			ip_as_string);
   2600 		return 4;
   2601 	}
   2602 	sa = (struct sockaddr *) &ss;
   2603 	if (sockaddr_getport(sa) == 0)
   2604 		sockaddr_setport(sa, 53);
   2605 
   2606 	EVDNS_LOCK(base);
   2607 	res = _evdns_nameserver_add_impl(base, sa, len);
   2608 	EVDNS_UNLOCK(base);
   2609 	return res;
   2610 }
   2611 
   2612 int
   2613 evdns_nameserver_ip_add(const char *ip_as_string) {
   2614 	if (!current_base)
   2615 		current_base = evdns_base_new(NULL, 0);
   2616 	return evdns_base_nameserver_ip_add(current_base, ip_as_string);
   2617 }
   2618 
   2619 int
   2620 evdns_base_nameserver_sockaddr_add(struct evdns_base *base,
   2621     const struct sockaddr *sa, ev_socklen_t len, unsigned flags)
   2622 {
   2623 	int res;
   2624 	EVUTIL_ASSERT(base);
   2625 	EVDNS_LOCK(base);
   2626 	res = _evdns_nameserver_add_impl(base, sa, len);
   2627 	EVDNS_UNLOCK(base);
   2628 	return res;
   2629 }
   2630 
   2631 /* remove from the queue */
   2632 static void
   2633 evdns_request_remove(struct request *req, struct request **head)
   2634 {
   2635 	ASSERT_LOCKED(req->base);
   2636 	ASSERT_VALID_REQUEST(req);
   2637 
   2638 #if 0
   2639 	{
   2640 		struct request *ptr;
   2641 		int found = 0;
   2642 		EVUTIL_ASSERT(*head != NULL);
   2643 
   2644 		ptr = *head;
   2645 		do {
   2646 			if (ptr == req) {
   2647 				found = 1;
   2648 				break;
   2649 			}
   2650 			ptr = ptr->next;
   2651 		} while (ptr != *head);
   2652 		EVUTIL_ASSERT(found);
   2653 
   2654 		EVUTIL_ASSERT(req->next);
   2655 	}
   2656 #endif
   2657 
   2658 	if (req->next == req) {
   2659 		/* only item in the list */
   2660 		*head = NULL;
   2661 	} else {
   2662 		req->next->prev = req->prev;
   2663 		req->prev->next = req->next;
   2664 		if (*head == req) *head = req->next;
   2665 	}
   2666 	req->next = req->prev = NULL;
   2667 }
   2668 
   2669 /* insert into the tail of the queue */
   2670 static void
   2671 evdns_request_insert(struct request *req, struct request **head) {
   2672 	ASSERT_LOCKED(req->base);
   2673 	ASSERT_VALID_REQUEST(req);
   2674 	if (!*head) {
   2675 		*head = req;
   2676 		req->next = req->prev = req;
   2677 		return;
   2678 	}
   2679 
   2680 	req->prev = (*head)->prev;
   2681 	req->prev->next = req;
   2682 	req->next = *head;
   2683 	(*head)->prev = req;
   2684 }
   2685 
   2686 static int
   2687 string_num_dots(const char *s) {
   2688 	int count = 0;
   2689 	while ((s = strchr(s, '.'))) {
   2690 		s++;
   2691 		count++;
   2692 	}
   2693 	return count;
   2694 }
   2695 
   2696 static struct request *
   2697 request_new(struct evdns_base *base, struct evdns_request *handle, int type,
   2698 	    const char *name, int flags, evdns_callback_type callback,
   2699 	    void *user_ptr) {
   2700 
   2701 	const char issuing_now =
   2702 	    (base->global_requests_inflight < base->global_max_requests_inflight) ? 1 : 0;
   2703 
   2704 	const size_t name_len = strlen(name);
   2705 	const size_t request_max_len = evdns_request_len(name_len);
   2706 	const u16 trans_id = issuing_now ? transaction_id_pick(base) : 0xffff;
   2707 	/* the request data is alloced in a single block with the header */
   2708 	struct request *const req =
   2709 	    mm_malloc(sizeof(struct request) + request_max_len);
   2710 	int rlen;
   2711 	char namebuf[256];
   2712 	(void) flags;
   2713 
   2714 	ASSERT_LOCKED(base);
   2715 
   2716 	if (!req) return NULL;
   2717 
   2718 	if (name_len >= sizeof(namebuf)) {
   2719 		mm_free(req);
   2720 		return NULL;
   2721 	}
   2722 
   2723 	memset(req, 0, sizeof(struct request));
   2724 	req->base = base;
   2725 
   2726 	evtimer_assign(&req->timeout_event, req->base->event_base, evdns_request_timeout_callback, req);
   2727 
   2728 	if (base->global_randomize_case) {
   2729 		unsigned i;
   2730 		char randbits[(sizeof(namebuf)+7)/8];
   2731 		strlcpy(namebuf, name, sizeof(namebuf));
   2732 		evutil_secure_rng_get_bytes(randbits, (name_len+7)/8);
   2733 		for (i = 0; i < name_len; ++i) {
   2734 			if (EVUTIL_ISALPHA(namebuf[i])) {
   2735 				if ((randbits[i >> 3] & (1<<(i & 7))))
   2736 					namebuf[i] |= 0x20;
   2737 				else
   2738 					namebuf[i] &= ~0x20;
   2739 			}
   2740 		}
   2741 		name = namebuf;
   2742 	}
   2743 
   2744 	/* request data lives just after the header */
   2745 	req->request = ((u8 *) req) + sizeof(struct request);
   2746 	/* denotes that the request data shouldn't be free()ed */
   2747 	req->request_appended = 1;
   2748 	rlen = evdns_request_data_build(name, name_len, trans_id,
   2749 	    type, CLASS_INET, req->request, request_max_len);
   2750 	if (rlen < 0)
   2751 		goto err1;
   2752 
   2753 	req->request_len = rlen;
   2754 	req->trans_id = trans_id;
   2755 	req->tx_count = 0;
   2756 	req->request_type = type;
   2757 	req->user_pointer = user_ptr;
   2758 	req->user_callback = callback;
   2759 	req->ns = issuing_now ? nameserver_pick(base) : NULL;
   2760 	req->next = req->prev = NULL;
   2761 	req->handle = handle;
   2762 	if (handle) {
   2763 		handle->current_req = req;
   2764 		handle->base = base;
   2765 	}
   2766 
   2767 	return req;
   2768 err1:
   2769 	mm_free(req);
   2770 	return NULL;
   2771 }
   2772 
   2773 static void
   2774 request_submit(struct request *const req) {
   2775 	struct evdns_base *base = req->base;
   2776 	ASSERT_LOCKED(base);
   2777 	ASSERT_VALID_REQUEST(req);
   2778 	if (req->ns) {
   2779 		/* if it has a nameserver assigned then this is going */
   2780 		/* straight into the inflight queue */
   2781 		evdns_request_insert(req, &REQ_HEAD(base, req->trans_id));
   2782 		base->global_requests_inflight++;
   2783 		evdns_request_transmit(req);
   2784 	} else {
   2785 		evdns_request_insert(req, &base->req_waiting_head);
   2786 		base->global_requests_waiting++;
   2787 	}
   2788 }
   2789 
   2790 /* exported function */
   2791 void
   2792 evdns_cancel_request(struct evdns_base *base, struct evdns_request *handle)
   2793 {
   2794 	struct request *req;
   2795 
   2796 	if (!handle->current_req)
   2797 		return;
   2798 
   2799 	if (!base) {
   2800 		/* This redundancy is silly; can we fix it? (Not for 2.0) XXXX */
   2801 		base = handle->base;
   2802 		if (!base)
   2803 			base = handle->current_req->base;
   2804 	}
   2805 
   2806 	EVDNS_LOCK(base);
   2807 	if (handle->pending_cb) {
   2808 		EVDNS_UNLOCK(base);
   2809 		return;
   2810 	}
   2811 
   2812 	req = handle->current_req;
   2813 	ASSERT_VALID_REQUEST(req);
   2814 
   2815 	reply_schedule_callback(req, 0, DNS_ERR_CANCEL, NULL);
   2816 	if (req->ns) {
   2817 		/* remove from inflight queue */
   2818 		request_finished(req, &REQ_HEAD(base, req->trans_id), 1);
   2819 	} else {
   2820 		/* remove from global_waiting head */
   2821 		request_finished(req, &base->req_waiting_head, 1);
   2822 	}
   2823 	EVDNS_UNLOCK(base);
   2824 }
   2825 
   2826 /* exported function */
   2827 struct evdns_request *
   2828 evdns_base_resolve_ipv4(struct evdns_base *base, const char *name, int flags,
   2829     evdns_callback_type callback, void *ptr) {
   2830 	struct evdns_request *handle;
   2831 	struct request *req;
   2832 	log(EVDNS_LOG_DEBUG, "Resolve requested for %s", name);
   2833 	handle = mm_calloc(1, sizeof(*handle));
   2834 	if (handle == NULL)
   2835 		return NULL;
   2836 	EVDNS_LOCK(base);
   2837 	if (flags & DNS_QUERY_NO_SEARCH) {
   2838 		req =
   2839 			request_new(base, handle, TYPE_A, name, flags,
   2840 				    callback, ptr);
   2841 		if (req)
   2842 			request_submit(req);
   2843 	} else {
   2844 		search_request_new(base, handle, TYPE_A, name, flags,
   2845 		    callback, ptr);
   2846 	}
   2847 	if (handle->current_req == NULL) {
   2848 		mm_free(handle);
   2849 		handle = NULL;
   2850 	}
   2851 	EVDNS_UNLOCK(base);
   2852 	return handle;
   2853 }
   2854 
   2855 int evdns_resolve_ipv4(const char *name, int flags,
   2856 					   evdns_callback_type callback, void *ptr)
   2857 {
   2858 	return evdns_base_resolve_ipv4(current_base, name, flags, callback, ptr)
   2859 		? 0 : -1;
   2860 }
   2861 
   2862 
   2863 /* exported function */
   2864 struct evdns_request *
   2865 evdns_base_resolve_ipv6(struct evdns_base *base,
   2866     const char *name, int flags,
   2867     evdns_callback_type callback, void *ptr)
   2868 {
   2869 	struct evdns_request *handle;
   2870 	struct request *req;
   2871 	log(EVDNS_LOG_DEBUG, "Resolve requested for %s", name);
   2872 	handle = mm_calloc(1, sizeof(*handle));
   2873 	if (handle == NULL)
   2874 		return NULL;
   2875 	EVDNS_LOCK(base);
   2876 	if (flags & DNS_QUERY_NO_SEARCH) {
   2877 		req = request_new(base, handle, TYPE_AAAA, name, flags,
   2878 				  callback, ptr);
   2879 		if (req)
   2880 			request_submit(req);
   2881 	} else {
   2882 		search_request_new(base, handle, TYPE_AAAA, name, flags,
   2883 		    callback, ptr);
   2884 	}
   2885 	if (handle->current_req == NULL) {
   2886 		mm_free(handle);
   2887 		handle = NULL;
   2888 	}
   2889 	EVDNS_UNLOCK(base);
   2890 	return handle;
   2891 }
   2892 
   2893 int evdns_resolve_ipv6(const char *name, int flags,
   2894     evdns_callback_type callback, void *ptr) {
   2895 	return evdns_base_resolve_ipv6(current_base, name, flags, callback, ptr)
   2896 		? 0 : -1;
   2897 }
   2898 
   2899 struct evdns_request *
   2900 evdns_base_resolve_reverse(struct evdns_base *base, const struct in_addr *in, int flags, evdns_callback_type callback, void *ptr) {
   2901 	char buf[32];
   2902 	struct evdns_request *handle;
   2903 	struct request *req;
   2904 	u32 a;
   2905 	EVUTIL_ASSERT(in);
   2906 	a = ntohl(in->s_addr);
   2907 	evutil_snprintf(buf, sizeof(buf), "%d.%d.%d.%d.in-addr.arpa",
   2908 			(int)(u8)((a	)&0xff),
   2909 			(int)(u8)((a>>8 )&0xff),
   2910 			(int)(u8)((a>>16)&0xff),
   2911 			(int)(u8)((a>>24)&0xff));
   2912 	handle = mm_calloc(1, sizeof(*handle));
   2913 	if (handle == NULL)
   2914 		return NULL;
   2915 	log(EVDNS_LOG_DEBUG, "Resolve requested for %s (reverse)", buf);
   2916 	EVDNS_LOCK(base);
   2917 	req = request_new(base, handle, TYPE_PTR, buf, flags, callback, ptr);
   2918 	if (req)
   2919 		request_submit(req);
   2920 	if (handle->current_req == NULL) {
   2921 		mm_free(handle);
   2922 		handle = NULL;
   2923 	}
   2924 	EVDNS_UNLOCK(base);
   2925 	return (handle);
   2926 }
   2927 
   2928 int evdns_resolve_reverse(const struct in_addr *in, int flags, evdns_callback_type callback, void *ptr) {
   2929 	return evdns_base_resolve_reverse(current_base, in, flags, callback, ptr)
   2930 		? 0 : -1;
   2931 }
   2932 
   2933 struct evdns_request *
   2934 evdns_base_resolve_reverse_ipv6(struct evdns_base *base, const struct in6_addr *in, int flags, evdns_callback_type callback, void *ptr) {
   2935 	/* 32 nybbles, 32 periods, "ip6.arpa", NUL. */
   2936 	char buf[73];
   2937 	char *cp;
   2938 	struct evdns_request *handle;
   2939 	struct request *req;
   2940 	int i;
   2941 	EVUTIL_ASSERT(in);
   2942 	cp = buf;
   2943 	for (i=15; i >= 0; --i) {
   2944 		u8 byte = in->s6_addr[i];
   2945 		*cp++ = "0123456789abcdef"[byte & 0x0f];
   2946 		*cp++ = '.';
   2947 		*cp++ = "0123456789abcdef"[byte >> 4];
   2948 		*cp++ = '.';
   2949 	}
   2950 	EVUTIL_ASSERT(cp + strlen("ip6.arpa") < buf+sizeof(buf));
   2951 	memcpy(cp, "ip6.arpa", strlen("ip6.arpa")+1);
   2952 	handle = mm_calloc(1, sizeof(*handle));
   2953 	if (handle == NULL)
   2954 		return NULL;
   2955 	log(EVDNS_LOG_DEBUG, "Resolve requested for %s (reverse)", buf);
   2956 	EVDNS_LOCK(base);
   2957 	req = request_new(base, handle, TYPE_PTR, buf, flags, callback, ptr);
   2958 	if (req)
   2959 		request_submit(req);
   2960 	if (handle->current_req == NULL) {
   2961 		mm_free(handle);
   2962 		handle = NULL;
   2963 	}
   2964 	EVDNS_UNLOCK(base);
   2965 	return (handle);
   2966 }
   2967 
   2968 int evdns_resolve_reverse_ipv6(const struct in6_addr *in, int flags, evdns_callback_type callback, void *ptr) {
   2969 	return evdns_base_resolve_reverse_ipv6(current_base, in, flags, callback, ptr)
   2970 		? 0 : -1;
   2971 }
   2972 
   2973 /* ================================================================= */
   2974 /* Search support */
   2975 /* */
   2976 /* the libc resolver has support for searching a number of domains */
   2977 /* to find a name. If nothing else then it takes the single domain */
   2978 /* from the gethostname() call. */
   2979 /* */
   2980 /* It can also be configured via the domain and search options in a */
   2981 /* resolv.conf. */
   2982 /* */
   2983 /* The ndots option controls how many dots it takes for the resolver */
   2984 /* to decide that a name is non-local and so try a raw lookup first. */
   2985 
   2986 struct search_domain {
   2987 	int len;
   2988 	struct search_domain *next;
   2989 	/* the text string is appended to this structure */
   2990 };
   2991 
   2992 struct search_state {
   2993 	int refcount;
   2994 	int ndots;
   2995 	int num_domains;
   2996 	struct search_domain *head;
   2997 };
   2998 
   2999 static void
   3000 search_state_decref(struct search_state *const state) {
   3001 	if (!state) return;
   3002 	state->refcount--;
   3003 	if (!state->refcount) {
   3004 		struct search_domain *next, *dom;
   3005 		for (dom = state->head; dom; dom = next) {
   3006 			next = dom->next;
   3007 			mm_free(dom);
   3008 		}
   3009 		mm_free(state);
   3010 	}
   3011 }
   3012 
   3013 static struct search_state *
   3014 search_state_new(void) {
   3015 	struct search_state *state = (struct search_state *) mm_malloc(sizeof(struct search_state));
   3016 	if (!state) return NULL;
   3017 	memset(state, 0, sizeof(struct search_state));
   3018 	state->refcount = 1;
   3019 	state->ndots = 1;
   3020 
   3021 	return state;
   3022 }
   3023 
   3024 static void
   3025 search_postfix_clear(struct evdns_base *base) {
   3026 	search_state_decref(base->global_search_state);
   3027 
   3028 	base->global_search_state = search_state_new();
   3029 }
   3030 
   3031 /* exported function */
   3032 void
   3033 evdns_base_search_clear(struct evdns_base *base)
   3034 {
   3035 	EVDNS_LOCK(base);
   3036 	search_postfix_clear(base);
   3037 	EVDNS_UNLOCK(base);
   3038 }
   3039 
   3040 void
   3041 evdns_search_clear(void) {
   3042 	evdns_base_search_clear(current_base);
   3043 }
   3044 
   3045 static void
   3046 search_postfix_add(struct evdns_base *base, const char *domain) {
   3047 	size_t domain_len;
   3048 	struct search_domain *sdomain;
   3049 	while (domain[0] == '.') domain++;
   3050 	domain_len = strlen(domain);
   3051 
   3052 	ASSERT_LOCKED(base);
   3053 	if (!base->global_search_state) base->global_search_state = search_state_new();
   3054 	if (!base->global_search_state) return;
   3055 	base->global_search_state->num_domains++;
   3056 
   3057 	sdomain = (struct search_domain *) mm_malloc(sizeof(struct search_domain) + domain_len);
   3058 	if (!sdomain) return;
   3059 	memcpy( ((u8 *) sdomain) + sizeof(struct search_domain), domain, domain_len);
   3060 	sdomain->next = base->global_search_state->head;
   3061 	sdomain->len = (int) domain_len;
   3062 
   3063 	base->global_search_state->head = sdomain;
   3064 }
   3065 
   3066 /* reverse the order of members in the postfix list. This is needed because, */
   3067 /* when parsing resolv.conf we push elements in the wrong order */
   3068 static void
   3069 search_reverse(struct evdns_base *base) {
   3070 	struct search_domain *cur, *prev = NULL, *next;
   3071 	ASSERT_LOCKED(base);
   3072 	cur = base->global_search_state->head;
   3073 	while (cur) {
   3074 		next = cur->next;
   3075 		cur->next = prev;
   3076 		prev = cur;
   3077 		cur = next;
   3078 	}
   3079 
   3080 	base->global_search_state->head = prev;
   3081 }
   3082 
   3083 /* exported function */
   3084 void
   3085 evdns_base_search_add(struct evdns_base *base, const char *domain) {
   3086 	EVDNS_LOCK(base);
   3087 	search_postfix_add(base, domain);
   3088 	EVDNS_UNLOCK(base);
   3089 }
   3090 void
   3091 evdns_search_add(const char *domain) {
   3092 	evdns_base_search_add(current_base, domain);
   3093 }
   3094 
   3095 /* exported function */
   3096 void
   3097 evdns_base_search_ndots_set(struct evdns_base *base, const int ndots) {
   3098 	EVDNS_LOCK(base);
   3099 	if (!base->global_search_state) base->global_search_state = search_state_new();
   3100 	if (base->global_search_state)
   3101 		base->global_search_state->ndots = ndots;
   3102 	EVDNS_UNLOCK(base);
   3103 }
   3104 void
   3105 evdns_search_ndots_set(const int ndots) {
   3106 	evdns_base_search_ndots_set(current_base, ndots);
   3107 }
   3108 
   3109 static void
   3110 search_set_from_hostname(struct evdns_base *base) {
   3111 	char hostname[HOST_NAME_MAX + 1], *domainname;
   3112 
   3113 	ASSERT_LOCKED(base);
   3114 	search_postfix_clear(base);
   3115 	if (gethostname(hostname, sizeof(hostname))) return;
   3116 	domainname = strchr(hostname, '.');
   3117 	if (!domainname) return;
   3118 	search_postfix_add(base, domainname);
   3119 }
   3120 
   3121 /* warning: returns malloced string */
   3122 static char *
   3123 search_make_new(const struct search_state *const state, int n, const char *const base_name) {
   3124 	const size_t base_len = strlen(base_name);
   3125 	const char need_to_append_dot = base_name[base_len - 1] == '.' ? 0 : 1;
   3126 	struct search_domain *dom;
   3127 
   3128 	for (dom = state->head; dom; dom = dom->next) {
   3129 		if (!n--) {
   3130 			/* this is the postfix we want */
   3131 			/* the actual postfix string is kept at the end of the structure */
   3132 			const u8 *const postfix = ((u8 *) dom) + sizeof(struct search_domain);
   3133 			const int postfix_len = dom->len;
   3134 			char *const newname = (char *) mm_malloc(base_len + need_to_append_dot + postfix_len + 1);
   3135 			if (!newname) return NULL;
   3136 			memcpy(newname, base_name, base_len);
   3137 			if (need_to_append_dot) newname[base_len] = '.';
   3138 			memcpy(newname + base_len + need_to_append_dot, postfix, postfix_len);
   3139 			newname[base_len + need_to_append_dot + postfix_len] = 0;
   3140 			return newname;
   3141 		}
   3142 	}
   3143 
   3144 	/* we ran off the end of the list and still didn't find the requested string */
   3145 	EVUTIL_ASSERT(0);
   3146 	return NULL; /* unreachable; stops warnings in some compilers. */
   3147 }
   3148 
   3149 static struct request *
   3150 search_request_new(struct evdns_base *base, struct evdns_request *handle,
   3151 		   int type, const char *const name, int flags,
   3152 		   evdns_callback_type user_callback, void *user_arg) {
   3153 	ASSERT_LOCKED(base);
   3154 	EVUTIL_ASSERT(type == TYPE_A || type == TYPE_AAAA);
   3155 	EVUTIL_ASSERT(handle->current_req == NULL);
   3156 	if ( ((flags & DNS_QUERY_NO_SEARCH) == 0) &&
   3157 	     base->global_search_state &&
   3158 		 base->global_search_state->num_domains) {
   3159 		/* we have some domains to search */
   3160 		struct request *req;
   3161 		if (string_num_dots(name) >= base->global_search_state->ndots) {
   3162 			req = request_new(base, handle, type, name, flags, user_callback, user_arg);
   3163 			if (!req) return NULL;
   3164 			handle->search_index = -1;
   3165 		} else {
   3166 			char *const new_name = search_make_new(base->global_search_state, 0, name);
   3167 			if (!new_name) return NULL;
   3168 			req = request_new(base, handle, type, new_name, flags, user_callback, user_arg);
   3169 			mm_free(new_name);
   3170 			if (!req) return NULL;
   3171 			handle->search_index = 0;
   3172 		}
   3173 		EVUTIL_ASSERT(handle->search_origname == NULL);
   3174 		handle->search_origname = mm_strdup(name);
   3175 		if (handle->search_origname == NULL) {
   3176 			/* XXX Should we dealloc req? If yes, how? */
   3177 			if (req)
   3178 				mm_free(req);
   3179 			return NULL;
   3180 		}
   3181 		handle->search_state = base->global_search_state;
   3182 		handle->search_flags = flags;
   3183 		base->global_search_state->refcount++;
   3184 		request_submit(req);
   3185 		return req;
   3186 	} else {
   3187 		struct request *const req = request_new(base, handle, type, name, flags, user_callback, user_arg);
   3188 		if (!req) return NULL;
   3189 		request_submit(req);
   3190 		return req;
   3191 	}
   3192 }
   3193 
   3194 /* this is called when a request has failed to find a name. We need to check */
   3195 /* if it is part of a search and, if so, try the next name in the list */
   3196 /* returns: */
   3197 /*   0 another request has been submitted */
   3198 /*   1 no more requests needed */
   3199 static int
   3200 search_try_next(struct evdns_request *const handle) {
   3201 	struct request *req = handle->current_req;
   3202 	struct evdns_base *base = req->base;
   3203 	struct request *newreq;
   3204 	ASSERT_LOCKED(base);
   3205 	if (handle->search_state) {
   3206 		/* it is part of a search */
   3207 		char *new_name;
   3208 		handle->search_index++;
   3209 		if (handle->search_index >= handle->search_state->num_domains) {
   3210 			/* no more postfixes to try, however we may need to try */
   3211 			/* this name without a postfix */
   3212 			if (string_num_dots(handle->search_origname) < handle->search_state->ndots) {
   3213 				/* yep, we need to try it raw */
   3214 				newreq = request_new(base, NULL, req->request_type, handle->search_origname, handle->search_flags, req->user_callback, req->user_pointer);
   3215 				log(EVDNS_LOG_DEBUG, "Search: trying raw query %s", handle->search_origname);
   3216 				if (newreq) {
   3217 					search_request_finished(handle);
   3218 					goto submit_next;
   3219 				}
   3220 			}
   3221 			return 1;
   3222 		}
   3223 
   3224 		new_name = search_make_new(handle->search_state, handle->search_index, handle->search_origname);
   3225 		if (!new_name) return 1;
   3226 		log(EVDNS_LOG_DEBUG, "Search: now trying %s (%d)", new_name, handle->search_index);
   3227 		newreq = request_new(base, NULL, req->request_type, new_name, handle->search_flags, req->user_callback, req->user_pointer);
   3228 		mm_free(new_name);
   3229 		if (!newreq) return 1;
   3230 		goto submit_next;
   3231 	}
   3232 	return 1;
   3233 
   3234 submit_next:
   3235 	request_finished(req, &REQ_HEAD(req->base, req->trans_id), 0);
   3236 	handle->current_req = newreq;
   3237 	newreq->handle = handle;
   3238 	request_submit(newreq);
   3239 	return 0;
   3240 }
   3241 
   3242 static void
   3243 search_request_finished(struct evdns_request *const handle) {
   3244 	ASSERT_LOCKED(handle->current_req->base);
   3245 	if (handle->search_state) {
   3246 		search_state_decref(handle->search_state);
   3247 		handle->search_state = NULL;
   3248 	}
   3249 	if (handle->search_origname) {
   3250 		mm_free(handle->search_origname);
   3251 		handle->search_origname = NULL;
   3252 	}
   3253 }
   3254 
   3255 /* ================================================================= */
   3256 /* Parsing resolv.conf files */
   3257 
   3258 static void
   3259 evdns_resolv_set_defaults(struct evdns_base *base, int flags) {
   3260 	/* if the file isn't found then we assume a local resolver */
   3261 	ASSERT_LOCKED(base);
   3262 	if (flags & DNS_OPTION_SEARCH) search_set_from_hostname(base);
   3263 	if (flags & DNS_OPTION_NAMESERVERS) evdns_base_nameserver_ip_add(base,"127.0.0.1");
   3264 }
   3265 
   3266 #ifndef _EVENT_HAVE_STRTOK_R
   3267 static char *
   3268 strtok_r(char *s, const char *delim, char **state) {
   3269 	char *cp, *start;
   3270 	start = cp = s ? s : *state;
   3271 	if (!cp)
   3272 		return NULL;
   3273 	while (*cp && !strchr(delim, *cp))
   3274 		++cp;
   3275 	if (!*cp) {
   3276 		if (cp == start)
   3277 			return NULL;
   3278 		*state = NULL;
   3279 		return start;
   3280 	} else {
   3281 		*cp++ = '\0';
   3282 		*state = cp;
   3283 		return start;
   3284 	}
   3285 }
   3286 #endif
   3287 
   3288 /* helper version of atoi which returns -1 on error */
   3289 static int
   3290 strtoint(const char *const str)
   3291 {
   3292 	char *endptr;
   3293 	const int r = strtol(str, &endptr, 10);
   3294 	if (*endptr) return -1;
   3295 	return r;
   3296 }
   3297 
   3298 /* Parse a number of seconds into a timeval; return -1 on error. */
   3299 static int
   3300 strtotimeval(const char *const str, struct timeval *out)
   3301 {
   3302 	double d;
   3303 	char *endptr;
   3304 	d = strtod(str, &endptr);
   3305 	if (*endptr) return -1;
   3306 	if (d < 0) return -1;
   3307 	out->tv_sec = (int) d;
   3308 	out->tv_usec = (int) ((d - (int) d)*1000000);
   3309 	if (out->tv_sec == 0 && out->tv_usec < 1000) /* less than 1 msec */
   3310 		return -1;
   3311 	return 0;
   3312 }
   3313 
   3314 /* helper version of atoi that returns -1 on error and clips to bounds. */
   3315 static int
   3316 strtoint_clipped(const char *const str, int min, int max)
   3317 {
   3318 	int r = strtoint(str);
   3319 	if (r == -1)
   3320 		return r;
   3321 	else if (r<min)
   3322 		return min;
   3323 	else if (r>max)
   3324 		return max;
   3325 	else
   3326 		return r;
   3327 }
   3328 
   3329 static int
   3330 evdns_base_set_max_requests_inflight(struct evdns_base *base, int maxinflight)
   3331 {
   3332 	int old_n_heads = base->n_req_heads, n_heads;
   3333 	struct request **old_heads = base->req_heads, **new_heads, *req;
   3334 	int i;
   3335 
   3336 	ASSERT_LOCKED(base);
   3337 	if (maxinflight < 1)
   3338 		maxinflight = 1;
   3339 	n_heads = (maxinflight+4) / 5;
   3340 	EVUTIL_ASSERT(n_heads > 0);
   3341 	new_heads = mm_calloc(n_heads, sizeof(struct request*));
   3342 	if (!new_heads)
   3343 		return (-1);
   3344 	if (old_heads) {
   3345 		for (i = 0; i < old_n_heads; ++i) {
   3346 			while (old_heads[i]) {
   3347 				req = old_heads[i];
   3348 				evdns_request_remove(req, &old_heads[i]);
   3349 				evdns_request_insert(req, &new_heads[req->trans_id % n_heads]);
   3350 			}
   3351 		}
   3352 		mm_free(old_heads);
   3353 	}
   3354 	base->req_heads = new_heads;
   3355 	base->n_req_heads = n_heads;
   3356 	base->global_max_requests_inflight = maxinflight;
   3357 	return (0);
   3358 }
   3359 
   3360 /* exported function */
   3361 int
   3362 evdns_base_set_option(struct evdns_base *base,
   3363     const char *option, const char *val)
   3364 {
   3365 	int res;
   3366 	EVDNS_LOCK(base);
   3367 	res = evdns_base_set_option_impl(base, option, val, DNS_OPTIONS_ALL);
   3368 	EVDNS_UNLOCK(base);
   3369 	return res;
   3370 }
   3371 
   3372 static inline int
   3373 str_matches_option(const char *s1, const char *optionname)
   3374 {
   3375 	/* Option names are given as "option:" We accept either 'option' in
   3376 	 * s1, or 'option:randomjunk'.  The latter form is to implement the
   3377 	 * resolv.conf parser. */
   3378 	size_t optlen = strlen(optionname);
   3379 	size_t slen = strlen(s1);
   3380 	if (slen == optlen || slen == optlen - 1)
   3381 		return !strncmp(s1, optionname, slen);
   3382 	else if (slen > optlen)
   3383 		return !strncmp(s1, optionname, optlen);
   3384 	else
   3385 		return 0;
   3386 }
   3387 
   3388 static int
   3389 evdns_base_set_option_impl(struct evdns_base *base,
   3390     const char *option, const char *val, int flags)
   3391 {
   3392 	ASSERT_LOCKED(base);
   3393 	if (str_matches_option(option, "ndots:")) {
   3394 		const int ndots = strtoint(val);
   3395 		if (ndots == -1) return -1;
   3396 		if (!(flags & DNS_OPTION_SEARCH)) return 0;
   3397 		log(EVDNS_LOG_DEBUG, "Setting ndots to %d", ndots);
   3398 		if (!base->global_search_state) base->global_search_state = search_state_new();
   3399 		if (!base->global_search_state) return -1;
   3400 		base->global_search_state->ndots = ndots;
   3401 	} else if (str_matches_option(option, "timeout:")) {
   3402 		struct timeval tv;
   3403 		if (strtotimeval(val, &tv) == -1) return -1;
   3404 		if (!(flags & DNS_OPTION_MISC)) return 0;
   3405 		log(EVDNS_LOG_DEBUG, "Setting timeout to %s", val);
   3406 		memcpy(&base->global_timeout, &tv, sizeof(struct timeval));
   3407 	} else if (str_matches_option(option, "getaddrinfo-allow-skew:")) {
   3408 		struct timeval tv;
   3409 		if (strtotimeval(val, &tv) == -1) return -1;
   3410 		if (!(flags & DNS_OPTION_MISC)) return 0;
   3411 		log(EVDNS_LOG_DEBUG, "Setting getaddrinfo-allow-skew to %s",
   3412 		    val);
   3413 		memcpy(&base->global_getaddrinfo_allow_skew, &tv,
   3414 		    sizeof(struct timeval));
   3415 	} else if (str_matches_option(option, "max-timeouts:")) {
   3416 		const int maxtimeout = strtoint_clipped(val, 1, 255);
   3417 		if (maxtimeout == -1) return -1;
   3418 		if (!(flags & DNS_OPTION_MISC)) return 0;
   3419 		log(EVDNS_LOG_DEBUG, "Setting maximum allowed timeouts to %d",
   3420 			maxtimeout);
   3421 		base->global_max_nameserver_timeout = maxtimeout;
   3422 	} else if (str_matches_option(option, "max-inflight:")) {
   3423 		const int maxinflight = strtoint_clipped(val, 1, 65000);
   3424 		if (maxinflight == -1) return -1;
   3425 		if (!(flags & DNS_OPTION_MISC)) return 0;
   3426 		log(EVDNS_LOG_DEBUG, "Setting maximum inflight requests to %d",
   3427 			maxinflight);
   3428 		evdns_base_set_max_requests_inflight(base, maxinflight);
   3429 	} else if (str_matches_option(option, "attempts:")) {
   3430 		int retries = strtoint(val);
   3431 		if (retries == -1) return -1;
   3432 		if (retries > 255) retries = 255;
   3433 		if (!(flags & DNS_OPTION_MISC)) return 0;
   3434 		log(EVDNS_LOG_DEBUG, "Setting retries to %d", retries);
   3435 		base->global_max_retransmits = retries;
   3436 	} else if (str_matches_option(option, "randomize-case:")) {
   3437 		int randcase = strtoint(val);
   3438 		if (!(flags & DNS_OPTION_MISC)) return 0;
   3439 		base->global_randomize_case = randcase;
   3440 	} else if (str_matches_option(option, "bind-to:")) {
   3441 		/* XXX This only applies to successive nameservers, not
   3442 		 * to already-configured ones.	We might want to fix that. */
   3443 		int len = sizeof(base->global_outgoing_address);
   3444 		if (!(flags & DNS_OPTION_NAMESERVERS)) return 0;
   3445 		if (evutil_parse_sockaddr_port(val,
   3446 			(struct sockaddr*)&base->global_outgoing_address, &len))
   3447 			return -1;
   3448 		base->global_outgoing_addrlen = len;
   3449 	} else if (str_matches_option(option, "initial-probe-timeout:")) {
   3450 		struct timeval tv;
   3451 		if (strtotimeval(val, &tv) == -1) return -1;
   3452 		if (tv.tv_sec > 3600)
   3453 			tv.tv_sec = 3600;
   3454 		if (!(flags & DNS_OPTION_MISC)) return 0;
   3455 		log(EVDNS_LOG_DEBUG, "Setting initial probe timeout to %s",
   3456 		    val);
   3457 		memcpy(&base->global_nameserver_probe_initial_timeout, &tv,
   3458 		    sizeof(tv));
   3459 	}
   3460 	return 0;
   3461 }
   3462 
   3463 int
   3464 evdns_set_option(const char *option, const char *val, int flags)
   3465 {
   3466 	if (!current_base)
   3467 		current_base = evdns_base_new(NULL, 0);
   3468 	return evdns_base_set_option(current_base, option, val);
   3469 }
   3470 
   3471 static void
   3472 resolv_conf_parse_line(struct evdns_base *base, char *const start, int flags) {
   3473 	char *strtok_state;
   3474 	static const char *const delims = " \t";
   3475 #define NEXT_TOKEN strtok_r(NULL, delims, &strtok_state)
   3476 
   3477 
   3478 	char *const first_token = strtok_r(start, delims, &strtok_state);
   3479 	ASSERT_LOCKED(base);
   3480 	if (!first_token) return;
   3481 
   3482 	if (!strcmp(first_token, "nameserver") && (flags & DNS_OPTION_NAMESERVERS)) {
   3483 		const char *const nameserver = NEXT_TOKEN;
   3484 
   3485 		if (nameserver)
   3486 			evdns_base_nameserver_ip_add(base, nameserver);
   3487 	} else if (!strcmp(first_token, "domain") && (flags & DNS_OPTION_SEARCH)) {
   3488 		const char *const domain = NEXT_TOKEN;
   3489 		if (domain) {
   3490 			search_postfix_clear(base);
   3491 			search_postfix_add(base, domain);
   3492 		}
   3493 	} else if (!strcmp(first_token, "search") && (flags & DNS_OPTION_SEARCH)) {
   3494 		const char *domain;
   3495 		search_postfix_clear(base);
   3496 
   3497 		while ((domain = NEXT_TOKEN)) {
   3498 			search_postfix_add(base, domain);
   3499 		}
   3500 		search_reverse(base);
   3501 	} else if (!strcmp(first_token, "options")) {
   3502 		const char *option;
   3503 		while ((option = NEXT_TOKEN)) {
   3504 			const char *val = strchr(option, ':');
   3505 			evdns_base_set_option_impl(base, option, val ? val+1 : "", flags);
   3506 		}
   3507 	}
   3508 #undef NEXT_TOKEN
   3509 }
   3510 
   3511 /* exported function */
   3512 /* returns: */
   3513 /*   0 no errors */
   3514 /*   1 failed to open file */
   3515 /*   2 failed to stat file */
   3516 /*   3 file too large */
   3517 /*   4 out of memory */
   3518 /*   5 short read from file */
   3519 int
   3520 evdns_base_resolv_conf_parse(struct evdns_base *base, int flags, const char *const filename) {
   3521 	int res;
   3522 	EVDNS_LOCK(base);
   3523 	res = evdns_base_resolv_conf_parse_impl(base, flags, filename);
   3524 	EVDNS_UNLOCK(base);
   3525 	return res;
   3526 }
   3527 
   3528 static char *
   3529 evdns_get_default_hosts_filename(void)
   3530 {
   3531 #ifdef WIN32
   3532 	/* Windows is a little coy about where it puts its configuration
   3533 	 * files.  Sure, they're _usually_ in C:\windows\system32, but
   3534 	 * there's no reason in principle they couldn't be in
   3535 	 * W:\hoboken chicken emergency\
   3536 	 */
   3537 	char path[MAX_PATH+1];
   3538 	static const char hostfile[] = "\\drivers\\etc\\hosts";
   3539 	char *path_out;
   3540 	size_t len_out;
   3541 
   3542 	if (! SHGetSpecialFolderPathA(NULL, path, CSIDL_SYSTEM, 0))
   3543 		return NULL;
   3544 	len_out = strlen(path)+strlen(hostfile);
   3545 	path_out = mm_malloc(len_out+1);
   3546 	evutil_snprintf(path_out, len_out, "%s%s", path, hostfile);
   3547 	return path_out;
   3548 #else
   3549 	return mm_strdup("/etc/hosts");
   3550 #endif
   3551 }
   3552 
   3553 static int
   3554 evdns_base_resolv_conf_parse_impl(struct evdns_base *base, int flags, const char *const filename) {
   3555 	size_t n;
   3556 	char *resolv;
   3557 	char *start;
   3558 	int err = 0;
   3559 
   3560 	log(EVDNS_LOG_DEBUG, "Parsing resolv.conf file %s", filename);
   3561 
   3562 	if (flags & DNS_OPTION_HOSTSFILE) {
   3563 		char *fname = evdns_get_default_hosts_filename();
   3564 		evdns_base_load_hosts(base, fname);
   3565 		if (fname)
   3566 			mm_free(fname);
   3567 	}
   3568 
   3569 	if ((err = evutil_read_file(filename, &resolv, &n, 0)) < 0) {
   3570 		if (err == -1) {
   3571 			/* No file. */
   3572 			evdns_resolv_set_defaults(base, flags);
   3573 			return 1;
   3574 		} else {
   3575 			return 2;
   3576 		}
   3577 	}
   3578 
   3579 	start = resolv;
   3580 	for (;;) {
   3581 		char *const newline = strchr(start, '\n');
   3582 		if (!newline) {
   3583 			resolv_conf_parse_line(base, start, flags);
   3584 			break;
   3585 		} else {
   3586 			*newline = 0;
   3587 			resolv_conf_parse_line(base, start, flags);
   3588 			start = newline + 1;
   3589 		}
   3590 	}
   3591 
   3592 	if (!base->server_head && (flags & DNS_OPTION_NAMESERVERS)) {
   3593 		/* no nameservers were configured. */
   3594 		evdns_base_nameserver_ip_add(base, "127.0.0.1");
   3595 		err = 6;
   3596 	}
   3597 	if (flags & DNS_OPTION_SEARCH && (!base->global_search_state || base->global_search_state->num_domains == 0)) {
   3598 		search_set_from_hostname(base);
   3599 	}
   3600 
   3601 	mm_free(resolv);
   3602 	return err;
   3603 }
   3604 
   3605 int
   3606 evdns_resolv_conf_parse(int flags, const char *const filename) {
   3607 	if (!current_base)
   3608 		current_base = evdns_base_new(NULL, 0);
   3609 	return evdns_base_resolv_conf_parse(current_base, flags, filename);
   3610 }
   3611 
   3612 
   3613 #ifdef WIN32
   3614 /* Add multiple nameservers from a space-or-comma-separated list. */
   3615 static int
   3616 evdns_nameserver_ip_add_line(struct evdns_base *base, const char *ips) {
   3617 	const char *addr;
   3618 	char *buf;
   3619 	int r;
   3620 	ASSERT_LOCKED(base);
   3621 	while (*ips) {
   3622 		while (isspace(*ips) || *ips == ',' || *ips == '\t')
   3623 			++ips;
   3624 		addr = ips;
   3625 		while (isdigit(*ips) || *ips == '.' || *ips == ':' ||
   3626 		    *ips=='[' || *ips==']')
   3627 			++ips;
   3628 		buf = mm_malloc(ips-addr+1);
   3629 		if (!buf) return 4;
   3630 		memcpy(buf, addr, ips-addr);
   3631 		buf[ips-addr] = '\0';
   3632 		r = evdns_base_nameserver_ip_add(base, buf);
   3633 		mm_free(buf);
   3634 		if (r) return r;
   3635 	}
   3636 	return 0;
   3637 }
   3638 
   3639 typedef DWORD(WINAPI *GetNetworkParams_fn_t)(FIXED_INFO *, DWORD*);
   3640 
   3641 /* Use the windows GetNetworkParams interface in iphlpapi.dll to */
   3642 /* figure out what our nameservers are. */
   3643 static int
   3644 load_nameservers_with_getnetworkparams(struct evdns_base *base)
   3645 {
   3646 	/* Based on MSDN examples and inspection of  c-ares code. */
   3647 	FIXED_INFO *fixed;
   3648 	HMODULE handle = 0;
   3649 	ULONG size = sizeof(FIXED_INFO);
   3650 	void *buf = NULL;
   3651 	int status = 0, r, added_any;
   3652 	IP_ADDR_STRING *ns;
   3653 	GetNetworkParams_fn_t fn;
   3654 
   3655 	ASSERT_LOCKED(base);
   3656 	if (!(handle = evutil_load_windows_system_library(
   3657 			TEXT("iphlpapi.dll")))) {
   3658 		log(EVDNS_LOG_WARN, "Could not open iphlpapi.dll");
   3659 		status = -1;
   3660 		goto done;
   3661 	}
   3662 	if (!(fn = (GetNetworkParams_fn_t) GetProcAddress(handle, "GetNetworkParams"))) {
   3663 		log(EVDNS_LOG_WARN, "Could not get address of function.");
   3664 		status = -1;
   3665 		goto done;
   3666 	}
   3667 
   3668 	buf = mm_malloc(size);
   3669 	if (!buf) { status = 4; goto done; }
   3670 	fixed = buf;
   3671 	r = fn(fixed, &size);
   3672 	if (r != ERROR_SUCCESS && r != ERROR_BUFFER_OVERFLOW) {
   3673 		status = -1;
   3674 		goto done;
   3675 	}
   3676 	if (r != ERROR_SUCCESS) {
   3677 		mm_free(buf);
   3678 		buf = mm_malloc(size);
   3679 		if (!buf) { status = 4; goto done; }
   3680 		fixed = buf;
   3681 		r = fn(fixed, &size);
   3682 		if (r != ERROR_SUCCESS) {
   3683 			log(EVDNS_LOG_DEBUG, "fn() failed.");
   3684 			status = -1;
   3685 			goto done;
   3686 		}
   3687 	}
   3688 
   3689 	EVUTIL_ASSERT(fixed);
   3690 	added_any = 0;
   3691 	ns = &(fixed->DnsServerList);
   3692 	while (ns) {
   3693 		r = evdns_nameserver_ip_add_line(base, ns->IpAddress.String);
   3694 		if (r) {
   3695 			log(EVDNS_LOG_DEBUG,"Could not add nameserver %s to list,error: %d",
   3696 				(ns->IpAddress.String),(int)GetLastError());
   3697 			status = r;
   3698 		} else {
   3699 			++added_any;
   3700 			log(EVDNS_LOG_DEBUG,"Successfully added %s as nameserver",ns->IpAddress.String);
   3701 		}
   3702 
   3703 		ns = ns->Next;
   3704 	}
   3705 
   3706 	if (!added_any) {
   3707 		log(EVDNS_LOG_DEBUG, "No nameservers added.");
   3708 		if (status == 0)
   3709 			status = -1;
   3710 	} else {
   3711 		status = 0;
   3712 	}
   3713 
   3714  done:
   3715 	if (buf)
   3716 		mm_free(buf);
   3717 	if (handle)
   3718 		FreeLibrary(handle);
   3719 	return status;
   3720 }
   3721 
   3722 static int
   3723 config_nameserver_from_reg_key(struct evdns_base *base, HKEY key, const TCHAR *subkey)
   3724 {
   3725 	char *buf;
   3726 	DWORD bufsz = 0, type = 0;
   3727 	int status = 0;
   3728 
   3729 	ASSERT_LOCKED(base);
   3730 	if (RegQueryValueEx(key, subkey, 0, &type, NULL, &bufsz)
   3731 	    != ERROR_MORE_DATA)
   3732 		return -1;
   3733 	if (!(buf = mm_malloc(bufsz)))
   3734 		return -1;
   3735 
   3736 	if (RegQueryValueEx(key, subkey, 0, &type, (LPBYTE)buf, &bufsz)
   3737 	    == ERROR_SUCCESS && bufsz > 1) {
   3738 		status = evdns_nameserver_ip_add_line(base,buf);
   3739 	}
   3740 
   3741 	mm_free(buf);
   3742 	return status;
   3743 }
   3744 
   3745 #define SERVICES_KEY TEXT("System\\CurrentControlSet\\Services\\")
   3746 #define WIN_NS_9X_KEY  SERVICES_KEY TEXT("VxD\\MSTCP")
   3747 #define WIN_NS_NT_KEY  SERVICES_KEY TEXT("Tcpip\\Parameters")
   3748 
   3749 static int
   3750 load_nameservers_from_registry(struct evdns_base *base)
   3751 {
   3752 	int found = 0;
   3753 	int r;
   3754 #define TRY(k, name) \
   3755 	if (!found && config_nameserver_from_reg_key(base,k,TEXT(name)) == 0) { \
   3756 		log(EVDNS_LOG_DEBUG,"Found nameservers in %s/%s",#k,name); \
   3757 		found = 1;						\
   3758 	} else if (!found) {						\
   3759 		log(EVDNS_LOG_DEBUG,"Didn't find nameservers in %s/%s", \
   3760 		    #k,#name);						\
   3761 	}
   3762 
   3763 	ASSERT_LOCKED(base);
   3764 
   3765 	if (((int)GetVersion()) > 0) { /* NT */
   3766 		HKEY nt_key = 0, interfaces_key = 0;
   3767 
   3768 		if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, WIN_NS_NT_KEY, 0,
   3769 				 KEY_READ, &nt_key) != ERROR_SUCCESS) {
   3770 			log(EVDNS_LOG_DEBUG,"Couldn't open nt key, %d",(int)GetLastError());
   3771 			return -1;
   3772 		}
   3773 		r = RegOpenKeyEx(nt_key, TEXT("Interfaces"), 0,
   3774 			     KEY_QUERY_VALUE|KEY_ENUMERATE_SUB_KEYS,
   3775 			     &interfaces_key);
   3776 		if (r != ERROR_SUCCESS) {
   3777 			log(EVDNS_LOG_DEBUG,"Couldn't open interfaces key, %d",(int)GetLastError());
   3778 			return -1;
   3779 		}
   3780 		TRY(nt_key, "NameServer");
   3781 		TRY(nt_key, "DhcpNameServer");
   3782 		TRY(interfaces_key, "NameServer");
   3783 		TRY(interfaces_key, "DhcpNameServer");
   3784 		RegCloseKey(interfaces_key);
   3785 		RegCloseKey(nt_key);
   3786 	} else {
   3787 		HKEY win_key = 0;
   3788 		if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, WIN_NS_9X_KEY, 0,
   3789 				 KEY_READ, &win_key) != ERROR_SUCCESS) {
   3790 			log(EVDNS_LOG_DEBUG, "Couldn't open registry key, %d", (int)GetLastError());
   3791 			return -1;
   3792 		}
   3793 		TRY(win_key, "NameServer");
   3794 		RegCloseKey(win_key);
   3795 	}
   3796 
   3797 	if (found == 0) {
   3798 		log(EVDNS_LOG_WARN,"Didn't find any nameservers.");
   3799 	}
   3800 
   3801 	return found ? 0 : -1;
   3802 #undef TRY
   3803 }
   3804 
   3805 int
   3806 evdns_base_config_windows_nameservers(struct evdns_base *base)
   3807 {
   3808 	int r;
   3809 	char *fname;
   3810 	if (base == NULL)
   3811 		base = current_base;
   3812 	if (base == NULL)
   3813 		return -1;
   3814 	EVDNS_LOCK(base);
   3815 	if (load_nameservers_with_getnetworkparams(base) == 0) {
   3816 		EVDNS_UNLOCK(base);
   3817 		return 0;
   3818 	}
   3819 	r = load_nameservers_from_registry(base);
   3820 
   3821 	fname = evdns_get_default_hosts_filename();
   3822 	evdns_base_load_hosts(base, fname);
   3823 	if (fname)
   3824 		mm_free(fname);
   3825 
   3826 	EVDNS_UNLOCK(base);
   3827 	return r;
   3828 }
   3829 
   3830 int
   3831 evdns_config_windows_nameservers(void)
   3832 {
   3833 	if (!current_base) {
   3834 		current_base = evdns_base_new(NULL, 1);
   3835 		return current_base == NULL ? -1 : 0;
   3836 	} else {
   3837 		return evdns_base_config_windows_nameservers(current_base);
   3838 	}
   3839 }
   3840 #endif
   3841 
   3842 struct evdns_base *
   3843 evdns_base_new(struct event_base *event_base, int initialize_nameservers)
   3844 {
   3845 	struct evdns_base *base;
   3846 
   3847 	if (evutil_secure_rng_init() < 0) {
   3848 		log(EVDNS_LOG_WARN, "Unable to seed random number generator; "
   3849 		    "DNS can't run.");
   3850 		return NULL;
   3851 	}
   3852 
   3853 	/* Give the evutil library a hook into its evdns-enabled
   3854 	 * functionality.  We can't just call evdns_getaddrinfo directly or
   3855 	 * else libevent-core will depend on libevent-extras. */
   3856 	evutil_set_evdns_getaddrinfo_fn(evdns_getaddrinfo);
   3857 
   3858 	base = mm_malloc(sizeof(struct evdns_base));
   3859 	if (base == NULL)
   3860 		return (NULL);
   3861 	memset(base, 0, sizeof(struct evdns_base));
   3862 	base->req_waiting_head = NULL;
   3863 
   3864 	EVTHREAD_ALLOC_LOCK(base->lock, EVTHREAD_LOCKTYPE_RECURSIVE);
   3865 	EVDNS_LOCK(base);
   3866 
   3867 	/* Set max requests inflight and allocate req_heads. */
   3868 	base->req_heads = NULL;
   3869 
   3870 	evdns_base_set_max_requests_inflight(base, 64);
   3871 
   3872 	base->server_head = NULL;
   3873 	base->event_base = event_base;
   3874 	base->global_good_nameservers = base->global_requests_inflight =
   3875 		base->global_requests_waiting = 0;
   3876 
   3877 	base->global_timeout.tv_sec = 5;
   3878 	base->global_timeout.tv_usec = 0;
   3879 	base->global_max_reissues = 1;
   3880 	base->global_max_retransmits = 3;
   3881 	base->global_max_nameserver_timeout = 3;
   3882 	base->global_search_state = NULL;
   3883 	base->global_randomize_case = 1;
   3884 	base->global_getaddrinfo_allow_skew.tv_sec = 3;
   3885 	base->global_getaddrinfo_allow_skew.tv_usec = 0;
   3886 	base->global_nameserver_probe_initial_timeout.tv_sec = 10;
   3887 	base->global_nameserver_probe_initial_timeout.tv_usec = 0;
   3888 
   3889 	TAILQ_INIT(&base->hostsdb);
   3890 
   3891 	if (initialize_nameservers) {
   3892 		int r;
   3893 #ifdef WIN32
   3894 		r = evdns_base_config_windows_nameservers(base);
   3895 #else
   3896 		r = evdns_base_resolv_conf_parse(base, DNS_OPTIONS_ALL, "/etc/resolv.conf");
   3897 #endif
   3898 		if (r == -1) {
   3899 			evdns_base_free_and_unlock(base, 0);
   3900 			return NULL;
   3901 		}
   3902 	}
   3903 	EVDNS_UNLOCK(base);
   3904 	return base;
   3905 }
   3906 
   3907 int
   3908 evdns_init(void)
   3909 {
   3910 	struct evdns_base *base = evdns_base_new(NULL, 1);
   3911 	if (base) {
   3912 		current_base = base;
   3913 		return 0;
   3914 	} else {
   3915 		return -1;
   3916 	}
   3917 }
   3918 
   3919 const char *
   3920 evdns_err_to_string(int err)
   3921 {
   3922     switch (err) {
   3923 	case DNS_ERR_NONE: return "no error";
   3924 	case DNS_ERR_FORMAT: return "misformatted query";
   3925 	case DNS_ERR_SERVERFAILED: return "server failed";
   3926 	case DNS_ERR_NOTEXIST: return "name does not exist";
   3927 	case DNS_ERR_NOTIMPL: return "query not implemented";
   3928 	case DNS_ERR_REFUSED: return "refused";
   3929 
   3930 	case DNS_ERR_TRUNCATED: return "reply truncated or ill-formed";
   3931 	case DNS_ERR_UNKNOWN: return "unknown";
   3932 	case DNS_ERR_TIMEOUT: return "request timed out";
   3933 	case DNS_ERR_SHUTDOWN: return "dns subsystem shut down";
   3934 	case DNS_ERR_CANCEL: return "dns request canceled";
   3935 	case DNS_ERR_NODATA: return "no records in the reply";
   3936 	default: return "[Unknown error code]";
   3937     }
   3938 }
   3939 
   3940 static void
   3941 evdns_nameserver_free(struct nameserver *server)
   3942 {
   3943 	if (server->socket >= 0)
   3944 	evutil_closesocket(server->socket);
   3945 	(void) event_del(&server->event);
   3946 	event_debug_unassign(&server->event);
   3947 	if (server->state == 0)
   3948 		(void) event_del(&server->timeout_event);
   3949 	if (server->probe_request) {
   3950 		evdns_cancel_request(server->base, server->probe_request);
   3951 		server->probe_request = NULL;
   3952 	}
   3953 	event_debug_unassign(&server->timeout_event);
   3954 	mm_free(server);
   3955 }
   3956 
   3957 static void
   3958 evdns_base_free_and_unlock(struct evdns_base *base, int fail_requests)
   3959 {
   3960 	struct nameserver *server, *server_next;
   3961 	struct search_domain *dom, *dom_next;
   3962 	int i;
   3963 
   3964 	/* Requires that we hold the lock. */
   3965 
   3966 	/* TODO(nickm) we might need to refcount here. */
   3967 
   3968 	for (server = base->server_head; server; server = server_next) {
   3969 		server_next = server->next;
   3970 		evdns_nameserver_free(server);
   3971 		if (server_next == base->server_head)
   3972 			break;
   3973 	}
   3974 	base->server_head = NULL;
   3975 	base->global_good_nameservers = 0;
   3976 
   3977 	for (i = 0; i < base->n_req_heads; ++i) {
   3978 		while (base->req_heads[i]) {
   3979 			if (fail_requests)
   3980 				reply_schedule_callback(base->req_heads[i], 0, DNS_ERR_SHUTDOWN, NULL);
   3981 			request_finished(base->req_heads[i], &REQ_HEAD(base, base->req_heads[i]->trans_id), 1);
   3982 		}
   3983 	}
   3984 	while (base->req_waiting_head) {
   3985 		if (fail_requests)
   3986 			reply_schedule_callback(base->req_waiting_head, 0, DNS_ERR_SHUTDOWN, NULL);
   3987 		request_finished(base->req_waiting_head, &base->req_waiting_head, 1);
   3988 	}
   3989 	base->global_requests_inflight = base->global_requests_waiting = 0;
   3990 
   3991 
   3992 	if (base->global_search_state) {
   3993 		for (dom = base->global_search_state->head; dom; dom = dom_next) {
   3994 			dom_next = dom->next;
   3995 			mm_free(dom);
   3996 		}
   3997 		mm_free(base->global_search_state);
   3998 		base->global_search_state = NULL;
   3999 	}
   4000 
   4001 	{
   4002 		struct hosts_entry *victim;
   4003 		while ((victim = TAILQ_FIRST(&base->hostsdb))) {
   4004 			TAILQ_REMOVE(&base->hostsdb, victim, next);
   4005 			mm_free(victim);
   4006 		}
   4007 	}
   4008 
   4009 	mm_free(base->req_heads);
   4010 
   4011 	EVDNS_UNLOCK(base);
   4012 	EVTHREAD_FREE_LOCK(base->lock, EVTHREAD_LOCKTYPE_RECURSIVE);
   4013 
   4014 	mm_free(base);
   4015 }
   4016 
   4017 void
   4018 evdns_base_free(struct evdns_base *base, int fail_requests)
   4019 {
   4020 	EVDNS_LOCK(base);
   4021 	evdns_base_free_and_unlock(base, fail_requests);
   4022 }
   4023 
   4024 void
   4025 evdns_shutdown(int fail_requests)
   4026 {
   4027 	if (current_base) {
   4028 		struct evdns_base *b = current_base;
   4029 		current_base = NULL;
   4030 		evdns_base_free(b, fail_requests);
   4031 	}
   4032 	evdns_log_fn = NULL;
   4033 }
   4034 
   4035 static int
   4036 evdns_base_parse_hosts_line(struct evdns_base *base, char *line)
   4037 {
   4038 	char *strtok_state;
   4039 	static const char *const delims = " \t";
   4040 	char *const addr = strtok_r(line, delims, &strtok_state);
   4041 	char *hostname, *hash;
   4042 	struct sockaddr_storage ss;
   4043 	int socklen = sizeof(ss);
   4044 	ASSERT_LOCKED(base);
   4045 
   4046 #define NEXT_TOKEN strtok_r(NULL, delims, &strtok_state)
   4047 
   4048 	if (!addr || *addr == '#')
   4049 		return 0;
   4050 
   4051 	memset(&ss, 0, sizeof(ss));
   4052 	if (evutil_parse_sockaddr_port(addr, (struct sockaddr*)&ss, &socklen)<0)
   4053 		return -1;
   4054 	if (socklen > (int)sizeof(struct sockaddr_in6))
   4055 		return -1;
   4056 
   4057 	if (sockaddr_getport((struct sockaddr*)&ss))
   4058 		return -1;
   4059 
   4060 	while ((hostname = NEXT_TOKEN)) {
   4061 		struct hosts_entry *he;
   4062 		size_t namelen;
   4063 		if ((hash = strchr(hostname, '#'))) {
   4064 			if (hash == hostname)
   4065 				return 0;
   4066 			*hash = '\0';
   4067 		}
   4068 
   4069 		namelen = strlen(hostname);
   4070 
   4071 		he = mm_calloc(1, sizeof(struct hosts_entry)+namelen);
   4072 		if (!he)
   4073 			return -1;
   4074 		EVUTIL_ASSERT(socklen <= (int)sizeof(he->addr));
   4075 		memcpy(&he->addr, &ss, socklen);
   4076 		memcpy(he->hostname, hostname, namelen+1);
   4077 		he->addrlen = socklen;
   4078 
   4079 		TAILQ_INSERT_TAIL(&base->hostsdb, he, next);
   4080 
   4081 		if (hash)
   4082 			return 0;
   4083 	}
   4084 
   4085 	return 0;
   4086 #undef NEXT_TOKEN
   4087 }
   4088 
   4089 static int
   4090 evdns_base_load_hosts_impl(struct evdns_base *base, const char *hosts_fname)
   4091 {
   4092 	char *str=NULL, *cp, *eol;
   4093 	size_t len;
   4094 	int err=0;
   4095 
   4096 	ASSERT_LOCKED(base);
   4097 
   4098 	if (hosts_fname == NULL ||
   4099 	    (err = evutil_read_file(hosts_fname, &str, &len, 0)) < 0) {
   4100 		char tmp[64];
   4101 		strlcpy(tmp, "127.0.0.1   localhost", sizeof(tmp));
   4102 		evdns_base_parse_hosts_line(base, tmp);
   4103 		strlcpy(tmp, "::1   localhost", sizeof(tmp));
   4104 		evdns_base_parse_hosts_line(base, tmp);
   4105 		return err ? -1 : 0;
   4106 	}
   4107 
   4108 	/* This will break early if there is a NUL in the hosts file.
   4109 	 * Probably not a problem.*/
   4110 	cp = str;
   4111 	for (;;) {
   4112 		eol = strchr(cp, '\n');
   4113 
   4114 		if (eol) {
   4115 			*eol = '\0';
   4116 			evdns_base_parse_hosts_line(base, cp);
   4117 			cp = eol+1;
   4118 		} else {
   4119 			evdns_base_parse_hosts_line(base, cp);
   4120 			break;
   4121 		}
   4122 	}
   4123 
   4124 	mm_free(str);
   4125 	return 0;
   4126 }
   4127 
   4128 int
   4129 evdns_base_load_hosts(struct evdns_base *base, const char *hosts_fname)
   4130 {
   4131 	int res;
   4132 	if (!base)
   4133 		base = current_base;
   4134 	EVDNS_LOCK(base);
   4135 	res = evdns_base_load_hosts_impl(base, hosts_fname);
   4136 	EVDNS_UNLOCK(base);
   4137 	return res;
   4138 }
   4139 
   4140 /* A single request for a getaddrinfo, either v4 or v6. */
   4141 struct getaddrinfo_subrequest {
   4142 	struct evdns_request *r;
   4143 	ev_uint32_t type;
   4144 };
   4145 
   4146 /* State data used to implement an in-progress getaddrinfo. */
   4147 struct evdns_getaddrinfo_request {
   4148 	struct evdns_base *evdns_base;
   4149 	/* Copy of the modified 'hints' data that we'll use to build
   4150 	 * answers. */
   4151 	struct evutil_addrinfo hints;
   4152 	/* The callback to invoke when we're done */
   4153 	evdns_getaddrinfo_cb user_cb;
   4154 	/* User-supplied data to give to the callback. */
   4155 	void *user_data;
   4156 	/* The port to use when building sockaddrs. */
   4157 	ev_uint16_t port;
   4158 	/* The sub_request for an A record (if any) */
   4159 	struct getaddrinfo_subrequest ipv4_request;
   4160 	/* The sub_request for an AAAA record (if any) */
   4161 	struct getaddrinfo_subrequest ipv6_request;
   4162 
   4163 	/* The cname result that we were told (if any) */
   4164 	char *cname_result;
   4165 
   4166 	/* If we have one request answered and one request still inflight,
   4167 	 * then this field holds the answer from the first request... */
   4168 	struct evutil_addrinfo *pending_result;
   4169 	/* And this event is a timeout that will tell us to cancel the second
   4170 	 * request if it's taking a long time. */
   4171 	struct event timeout;
   4172 
   4173 	/* And this field holds the error code from the first request... */
   4174 	int pending_error;
   4175 	/* If this is set, the user canceled this request. */
   4176 	unsigned user_canceled : 1;
   4177 	/* If this is set, the user can no longer cancel this request; we're
   4178 	 * just waiting for the free. */
   4179 	unsigned request_done : 1;
   4180 };
   4181 
   4182 /* Convert an evdns errors to the equivalent getaddrinfo error. */
   4183 static int
   4184 evdns_err_to_getaddrinfo_err(int e1)
   4185 {
   4186 	/* XXX Do this better! */
   4187 	if (e1 == DNS_ERR_NONE)
   4188 		return 0;
   4189 	else if (e1 == DNS_ERR_NOTEXIST)
   4190 		return EVUTIL_EAI_NONAME;
   4191 	else
   4192 		return EVUTIL_EAI_FAIL;
   4193 }
   4194 
   4195 /* Return the more informative of two getaddrinfo errors. */
   4196 static int
   4197 getaddrinfo_merge_err(int e1, int e2)
   4198 {
   4199 	/* XXXX be cleverer here. */
   4200 	if (e1 == 0)
   4201 		return e2;
   4202 	else
   4203 		return e1;
   4204 }
   4205 
   4206 static void
   4207 free_getaddrinfo_request(struct evdns_getaddrinfo_request *data)
   4208 {
   4209 	/* DO NOT CALL this if either of the requests is pending.  Only once
   4210 	 * both callbacks have been invoked is it safe to free the request */
   4211 	if (data->pending_result)
   4212 		evutil_freeaddrinfo(data->pending_result);
   4213 	if (data->cname_result)
   4214 		mm_free(data->cname_result);
   4215 	event_del(&data->timeout);
   4216 	mm_free(data);
   4217 	return;
   4218 }
   4219 
   4220 static void
   4221 add_cname_to_reply(struct evdns_getaddrinfo_request *data,
   4222     struct evutil_addrinfo *ai)
   4223 {
   4224 	if (data->cname_result && ai) {
   4225 		ai->ai_canonname = data->cname_result;
   4226 		data->cname_result = NULL;
   4227 	}
   4228 }
   4229 
   4230 /* Callback: invoked when one request in a mixed-format A/AAAA getaddrinfo
   4231  * request has finished, but the other one took too long to answer. Pass
   4232  * along the answer we got, and cancel the other request.
   4233  */
   4234 static void
   4235 evdns_getaddrinfo_timeout_cb(evutil_socket_t fd, short what, void *ptr)
   4236 {
   4237 	int v4_timedout = 0, v6_timedout = 0;
   4238 	struct evdns_getaddrinfo_request *data = ptr;
   4239 
   4240 	/* Cancel any pending requests, and note which one */
   4241 	if (data->ipv4_request.r) {
   4242 		/* XXXX This does nothing if the request's callback is already
   4243 		 * running (pending_cb is set). */
   4244 		evdns_cancel_request(NULL, data->ipv4_request.r);
   4245 		v4_timedout = 1;
   4246 		EVDNS_LOCK(data->evdns_base);
   4247 		++data->evdns_base->getaddrinfo_ipv4_timeouts;
   4248 		EVDNS_UNLOCK(data->evdns_base);
   4249 	}
   4250 	if (data->ipv6_request.r) {
   4251 		/* XXXX This does nothing if the request's callback is already
   4252 		 * running (pending_cb is set). */
   4253 		evdns_cancel_request(NULL, data->ipv6_request.r);
   4254 		v6_timedout = 1;
   4255 		EVDNS_LOCK(data->evdns_base);
   4256 		++data->evdns_base->getaddrinfo_ipv6_timeouts;
   4257 		EVDNS_UNLOCK(data->evdns_base);
   4258 	}
   4259 
   4260 	/* We only use this timeout callback when we have an answer for
   4261 	 * one address. */
   4262 	EVUTIL_ASSERT(!v4_timedout || !v6_timedout);
   4263 
   4264 	/* Report the outcome of the other request that didn't time out. */
   4265 	if (data->pending_result) {
   4266 		add_cname_to_reply(data, data->pending_result);
   4267 		data->user_cb(0, data->pending_result, data->user_data);
   4268 		data->pending_result = NULL;
   4269 	} else {
   4270 		int e = data->pending_error;
   4271 		if (!e)
   4272 			e = EVUTIL_EAI_AGAIN;
   4273 		data->user_cb(e, NULL, data->user_data);
   4274 	}
   4275 
   4276 	data->user_cb = NULL; /* prevent double-call if evdns callbacks are
   4277 			       * in-progress. XXXX It would be better if this
   4278 			       * weren't necessary. */
   4279 
   4280 	if (!v4_timedout && !v6_timedout) {
   4281 		/* should be impossible? XXXX */
   4282 		free_getaddrinfo_request(data);
   4283 	}
   4284 }
   4285 
   4286 static int
   4287 evdns_getaddrinfo_set_timeout(struct evdns_base *evdns_base,
   4288     struct evdns_getaddrinfo_request *data)
   4289 {
   4290 	return event_add(&data->timeout, &evdns_base->global_getaddrinfo_allow_skew);
   4291 }
   4292 
   4293 static inline int
   4294 evdns_result_is_answer(int result)
   4295 {
   4296 	return (result != DNS_ERR_NOTIMPL && result != DNS_ERR_REFUSED &&
   4297 	    result != DNS_ERR_SERVERFAILED && result != DNS_ERR_CANCEL);
   4298 }
   4299 
   4300 static void
   4301 evdns_getaddrinfo_gotresolve(int result, char type, int count,
   4302     int ttl, void *addresses, void *arg)
   4303 {
   4304 	int i;
   4305 	struct getaddrinfo_subrequest *req = arg;
   4306 	struct getaddrinfo_subrequest *other_req;
   4307 	struct evdns_getaddrinfo_request *data;
   4308 
   4309 	struct evutil_addrinfo *res;
   4310 
   4311 	struct sockaddr_in sin;
   4312 	struct sockaddr_in6 sin6;
   4313 	struct sockaddr *sa;
   4314 	int socklen, addrlen;
   4315 	void *addrp;
   4316 	int err;
   4317 	int user_canceled;
   4318 
   4319 	EVUTIL_ASSERT(req->type == DNS_IPv4_A || req->type == DNS_IPv6_AAAA);
   4320 	if (req->type == DNS_IPv4_A) {
   4321 		data = EVUTIL_UPCAST(req, struct evdns_getaddrinfo_request, ipv4_request);
   4322 		other_req = &data->ipv6_request;
   4323 	} else {
   4324 		data = EVUTIL_UPCAST(req, struct evdns_getaddrinfo_request, ipv6_request);
   4325 		other_req = &data->ipv4_request;
   4326 	}
   4327 
   4328 	EVDNS_LOCK(data->evdns_base);
   4329 	if (evdns_result_is_answer(result)) {
   4330 		if (req->type == DNS_IPv4_A)
   4331 			++data->evdns_base->getaddrinfo_ipv4_answered;
   4332 		else
   4333 			++data->evdns_base->getaddrinfo_ipv6_answered;
   4334 	}
   4335 	user_canceled = data->user_canceled;
   4336 	if (other_req->r == NULL)
   4337 		data->request_done = 1;
   4338 	EVDNS_UNLOCK(data->evdns_base);
   4339 
   4340 	req->r = NULL;
   4341 
   4342 	if (result == DNS_ERR_CANCEL && ! user_canceled) {
   4343 		/* Internal cancel request from timeout or internal error.
   4344 		 * we already answered the user. */
   4345 		if (other_req->r == NULL)
   4346 			free_getaddrinfo_request(data);
   4347 		return;
   4348 	}
   4349 
   4350 	if (data->user_cb == NULL) {
   4351 		/* We already answered.  XXXX This shouldn't be needed; see
   4352 		 * comments in evdns_getaddrinfo_timeout_cb */
   4353 		free_getaddrinfo_request(data);
   4354 		return;
   4355 	}
   4356 
   4357 	if (result == DNS_ERR_NONE) {
   4358 		if (count == 0)
   4359 			err = EVUTIL_EAI_NODATA;
   4360 		else
   4361 			err = 0;
   4362 	} else {
   4363 		err = evdns_err_to_getaddrinfo_err(result);
   4364 	}
   4365 
   4366 	if (err) {
   4367 		/* Looks like we got an error. */
   4368 		if (other_req->r) {
   4369 			/* The other request is still working; maybe it will
   4370 			 * succeed. */
   4371 			/* XXXX handle failure from set_timeout */
   4372 			evdns_getaddrinfo_set_timeout(data->evdns_base, data);
   4373 			data->pending_error = err;
   4374 			return;
   4375 		}
   4376 
   4377 		if (user_canceled) {
   4378 			data->user_cb(EVUTIL_EAI_CANCEL, NULL, data->user_data);
   4379 		} else if (data->pending_result) {
   4380 			/* If we have an answer waiting, and we weren't
   4381 			 * canceled, ignore this error. */
   4382 			add_cname_to_reply(data, data->pending_result);
   4383 			data->user_cb(0, data->pending_result, data->user_data);
   4384 			data->pending_result = NULL;
   4385 		} else {
   4386 			if (data->pending_error)
   4387 				err = getaddrinfo_merge_err(err,
   4388 				    data->pending_error);
   4389 			data->user_cb(err, NULL, data->user_data);
   4390 		}
   4391 		free_getaddrinfo_request(data);
   4392 		return;
   4393 	} else if (user_canceled) {
   4394 		if (other_req->r) {
   4395 			/* The other request is still working; let it hit this
   4396 			 * callback with EVUTIL_EAI_CANCEL callback and report
   4397 			 * the failure. */
   4398 			return;
   4399 		}
   4400 		data->user_cb(EVUTIL_EAI_CANCEL, NULL, data->user_data);
   4401 		free_getaddrinfo_request(data);
   4402 		return;
   4403 	}
   4404 
   4405 	/* Looks like we got some answers. We should turn them into addrinfos
   4406 	 * and then either queue those or return them all. */
   4407 	EVUTIL_ASSERT(type == DNS_IPv4_A || type == DNS_IPv6_AAAA);
   4408 
   4409 	if (type == DNS_IPv4_A) {
   4410 		memset(&sin, 0, sizeof(sin));
   4411 		sin.sin_family = AF_INET;
   4412 		sin.sin_port = htons(data->port);
   4413 
   4414 		sa = (struct sockaddr *)&sin;
   4415 		socklen = sizeof(sin);
   4416 		addrlen = 4;
   4417 		addrp = &sin.sin_addr.s_addr;
   4418 	} else {
   4419 		memset(&sin6, 0, sizeof(sin6));
   4420 		sin6.sin6_family = AF_INET6;
   4421 		sin6.sin6_port = htons(data->port);
   4422 
   4423 		sa = (struct sockaddr *)&sin6;
   4424 		socklen = sizeof(sin6);
   4425 		addrlen = 16;
   4426 		addrp = &sin6.sin6_addr.s6_addr;
   4427 	}
   4428 
   4429 	res = NULL;
   4430 	for (i=0; i < count; ++i) {
   4431 		struct evutil_addrinfo *ai;
   4432 		memcpy(addrp, ((char*)addresses)+i*addrlen, addrlen);
   4433 		ai = evutil_new_addrinfo(sa, socklen, &data->hints);
   4434 		if (!ai) {
   4435 			if (other_req->r) {
   4436 				evdns_cancel_request(NULL, other_req->r);
   4437 			}
   4438 			data->user_cb(EVUTIL_EAI_MEMORY, NULL, data->user_data);
   4439 			if (res)
   4440 				evutil_freeaddrinfo(res);
   4441 
   4442 			if (other_req->r == NULL)
   4443 				free_getaddrinfo_request(data);
   4444 			return;
   4445 		}
   4446 		res = evutil_addrinfo_append(res, ai);
   4447 	}
   4448 
   4449 	if (other_req->r) {
   4450 		/* The other request is still in progress; wait for it */
   4451 		/* XXXX handle failure from set_timeout */
   4452 		evdns_getaddrinfo_set_timeout(data->evdns_base, data);
   4453 		data->pending_result = res;
   4454 		return;
   4455 	} else {
   4456 		/* The other request is done or never started; append its
   4457 		 * results (if any) and return them. */
   4458 		if (data->pending_result) {
   4459 			if (req->type == DNS_IPv4_A)
   4460 				res = evutil_addrinfo_append(res,
   4461 				    data->pending_result);
   4462 			else
   4463 				res = evutil_addrinfo_append(
   4464 				    data->pending_result, res);
   4465 			data->pending_result = NULL;
   4466 		}
   4467 
   4468 		/* Call the user callback. */
   4469 		add_cname_to_reply(data, res);
   4470 		data->user_cb(0, res, data->user_data);
   4471 
   4472 		/* Free data. */
   4473 		free_getaddrinfo_request(data);
   4474 	}
   4475 }
   4476 
   4477 static struct hosts_entry *
   4478 find_hosts_entry(struct evdns_base *base, const char *hostname,
   4479     struct hosts_entry *find_after)
   4480 {
   4481 	struct hosts_entry *e;
   4482 
   4483 	if (find_after)
   4484 		e = TAILQ_NEXT(find_after, next);
   4485 	else
   4486 		e = TAILQ_FIRST(&base->hostsdb);
   4487 
   4488 	for (; e; e = TAILQ_NEXT(e, next)) {
   4489 		if (!evutil_ascii_strcasecmp(e->hostname, hostname))
   4490 			return e;
   4491 	}
   4492 	return NULL;
   4493 }
   4494 
   4495 static int
   4496 evdns_getaddrinfo_fromhosts(struct evdns_base *base,
   4497     const char *nodename, struct evutil_addrinfo *hints, ev_uint16_t port,
   4498     struct evutil_addrinfo **res)
   4499 {
   4500 	int n_found = 0;
   4501 	struct hosts_entry *e;
   4502 	struct evutil_addrinfo *ai=NULL;
   4503 	int f = hints->ai_family;
   4504 
   4505 	EVDNS_LOCK(base);
   4506 	for (e = find_hosts_entry(base, nodename, NULL); e;
   4507 	    e = find_hosts_entry(base, nodename, e)) {
   4508 		struct evutil_addrinfo *ai_new;
   4509 		++n_found;
   4510 		if ((e->addr.sa.sa_family == AF_INET && f == PF_INET6) ||
   4511 		    (e->addr.sa.sa_family == AF_INET6 && f == PF_INET))
   4512 			continue;
   4513 		ai_new = evutil_new_addrinfo(&e->addr.sa, e->addrlen, hints);
   4514 		if (!ai_new) {
   4515 			n_found = 0;
   4516 			goto out;
   4517 		}
   4518 		sockaddr_setport(ai_new->ai_addr, port);
   4519 		ai = evutil_addrinfo_append(ai, ai_new);
   4520 	}
   4521 	EVDNS_UNLOCK(base);
   4522 out:
   4523 	if (n_found) {
   4524 		/* Note that we return an empty answer if we found entries for
   4525 		 * this hostname but none were of the right address type. */
   4526 		*res = ai;
   4527 		return 0;
   4528 	} else {
   4529 		if (ai)
   4530 			evutil_freeaddrinfo(ai);
   4531 		return -1;
   4532 	}
   4533 }
   4534 
   4535 struct evdns_getaddrinfo_request *
   4536 evdns_getaddrinfo(struct evdns_base *dns_base,
   4537     const char *nodename, const char *servname,
   4538     const struct evutil_addrinfo *hints_in,
   4539     evdns_getaddrinfo_cb cb, void *arg)
   4540 {
   4541 	struct evdns_getaddrinfo_request *data;
   4542 	struct evutil_addrinfo hints;
   4543 	struct evutil_addrinfo *res = NULL;
   4544 	int err;
   4545 	int port = 0;
   4546 	int want_cname = 0;
   4547 
   4548 	if (!dns_base) {
   4549 		dns_base = current_base;
   4550 		if (!dns_base) {
   4551 			log(EVDNS_LOG_WARN,
   4552 			    "Call to getaddrinfo_async with no "
   4553 			    "evdns_base configured.");
   4554 			cb(EVUTIL_EAI_FAIL, NULL, arg); /* ??? better error? */
   4555 			return NULL;
   4556 		}
   4557 	}
   4558 
   4559 	/* If we _must_ answer this immediately, do so. */
   4560 	if ((hints_in && (hints_in->ai_flags & EVUTIL_AI_NUMERICHOST))) {
   4561 		res = NULL;
   4562 		err = evutil_getaddrinfo(nodename, servname, hints_in, &res);
   4563 		cb(err, res, arg);
   4564 		return NULL;
   4565 	}
   4566 
   4567 	if (hints_in) {
   4568 		memcpy(&hints, hints_in, sizeof(hints));
   4569 	} else {
   4570 		memset(&hints, 0, sizeof(hints));
   4571 		hints.ai_family = PF_UNSPEC;
   4572 	}
   4573 
   4574 	evutil_adjust_hints_for_addrconfig(&hints);
   4575 
   4576 	/* Now try to see if we _can_ answer immediately. */
   4577 	/* (It would be nice to do this by calling getaddrinfo directly, with
   4578 	 * AI_NUMERICHOST, on plaforms that have it, but we can't: there isn't
   4579 	 * a reliable way to distinguish the "that wasn't a numeric host!" case
   4580 	 * from any other EAI_NONAME cases.) */
   4581 	err = evutil_getaddrinfo_common(nodename, servname, &hints, &res, &port);
   4582 	if (err != EVUTIL_EAI_NEED_RESOLVE) {
   4583 		cb(err, res, arg);
   4584 		return NULL;
   4585 	}
   4586 
   4587 	/* If there is an entry in the hosts file, we should give it now. */
   4588 	if (!evdns_getaddrinfo_fromhosts(dns_base, nodename, &hints, port, &res)) {
   4589 		cb(0, res, arg);
   4590 		return NULL;
   4591 	}
   4592 
   4593 	/* Okay, things are serious now. We're going to need to actually
   4594 	 * launch a request.
   4595 	 */
   4596 	data = mm_calloc(1,sizeof(struct evdns_getaddrinfo_request));
   4597 	if (!data) {
   4598 		cb(EVUTIL_EAI_MEMORY, NULL, arg);
   4599 		return NULL;
   4600 	}
   4601 
   4602 	memcpy(&data->hints, &hints, sizeof(data->hints));
   4603 	data->port = (ev_uint16_t)port;
   4604 	data->ipv4_request.type = DNS_IPv4_A;
   4605 	data->ipv6_request.type = DNS_IPv6_AAAA;
   4606 	data->user_cb = cb;
   4607 	data->user_data = arg;
   4608 	data->evdns_base = dns_base;
   4609 
   4610 	want_cname = (hints.ai_flags & EVUTIL_AI_CANONNAME);
   4611 
   4612 	/* If we are asked for a PF_UNSPEC address, we launch two requests in
   4613 	 * parallel: one for an A address and one for an AAAA address.  We
   4614 	 * can't send just one request, since many servers only answer one
   4615 	 * question per DNS request.
   4616 	 *
   4617 	 * Once we have the answer to one request, we allow for a short
   4618 	 * timeout before we report it, to see if the other one arrives.  If
   4619 	 * they both show up in time, then we report both the answers.
   4620 	 *
   4621 	 * If too many addresses of one type time out or fail, we should stop
   4622 	 * launching those requests. (XXX we don't do that yet.)
   4623 	 */
   4624 
   4625 	if (hints.ai_family != PF_INET6) {
   4626 		log(EVDNS_LOG_DEBUG, "Sending request for %s on ipv4 as %p",
   4627 		    nodename, &data->ipv4_request);
   4628 
   4629 		data->ipv4_request.r = evdns_base_resolve_ipv4(dns_base,
   4630 		    nodename, 0, evdns_getaddrinfo_gotresolve,
   4631 		    &data->ipv4_request);
   4632 		if (want_cname)
   4633 			data->ipv4_request.r->current_req->put_cname_in_ptr =
   4634 			    &data->cname_result;
   4635 	}
   4636 	if (hints.ai_family != PF_INET) {
   4637 		log(EVDNS_LOG_DEBUG, "Sending request for %s on ipv6 as %p",
   4638 		    nodename, &data->ipv6_request);
   4639 
   4640 		data->ipv6_request.r = evdns_base_resolve_ipv6(dns_base,
   4641 		    nodename, 0, evdns_getaddrinfo_gotresolve,
   4642 		    &data->ipv6_request);
   4643 		if (want_cname)
   4644 			data->ipv6_request.r->current_req->put_cname_in_ptr =
   4645 			    &data->cname_result;
   4646 	}
   4647 
   4648 	evtimer_assign(&data->timeout, dns_base->event_base,
   4649 	    evdns_getaddrinfo_timeout_cb, data);
   4650 
   4651 	if (data->ipv4_request.r || data->ipv6_request.r) {
   4652 		return data;
   4653 	} else {
   4654 		mm_free(data);
   4655 		cb(EVUTIL_EAI_FAIL, NULL, arg);
   4656 		return NULL;
   4657 	}
   4658 }
   4659 
   4660 void
   4661 evdns_getaddrinfo_cancel(struct evdns_getaddrinfo_request *data)
   4662 {
   4663 	EVDNS_LOCK(data->evdns_base);
   4664 	if (data->request_done) {
   4665 		EVDNS_UNLOCK(data->evdns_base);
   4666 		return;
   4667 	}
   4668 	event_del(&data->timeout);
   4669 	data->user_canceled = 1;
   4670 	if (data->ipv4_request.r)
   4671 		evdns_cancel_request(data->evdns_base, data->ipv4_request.r);
   4672 	if (data->ipv6_request.r)
   4673 		evdns_cancel_request(data->evdns_base, data->ipv6_request.r);
   4674 	EVDNS_UNLOCK(data->evdns_base);
   4675 }
   4676