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