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