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