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