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