]> git.llucax.com Git - software/libev.git/blob - evdns.c
add rpid/rstatus
[software/libev.git] / evdns.c
1 #define DNS_USE_GETTIMEOFDAY_FOR_ID 1
2 #define HAVE_STRUCT_IN6_ADDR 1
3
4 /* The original version of this module was written by Adam Langley; for
5  * a history of modifications, check out the subversion logs.
6  *
7  * When editing this module, try to keep it re-mergeable by Adam.  Don't
8  * reformat the whitespace, add Tor dependencies, or so on.
9  *
10  * TODO:
11  *   - Support IPv6 and PTR records.
12  *   - Replace all externally visible magic numbers with #defined constants.
13  *   - Write doccumentation for APIs of all external functions.
14  */
15
16 /* Async DNS Library
17  * Adam Langley <agl@imperialviolet.org>
18  * http://www.imperialviolet.org/eventdns.html
19  * Public Domain code
20  *
21  * This software is Public Domain. To view a copy of the public domain dedication,
22  * visit http://creativecommons.org/licenses/publicdomain/ or send a letter to
23  * Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
24  *
25  * I ask and expect, but do not require, that all derivative works contain an
26  * attribution similar to:
27  *      Parts developed by Adam Langley <agl@imperialviolet.org>
28  *
29  * You may wish to replace the word "Parts" with something else depending on
30  * the amount of original code.
31  *
32  * (Derivative works does not include programs which link against, run or include
33  * the source verbatim in their source distributions)
34  *
35  * Version: 0.1b
36  */
37
38 #include <sys/types.h>
39 #ifdef HAVE_CONFIG_H
40 #include "config.h"
41 #endif
42
43 #ifdef WIN32
44 #endif
45
46 /* #define NDEBUG */
47
48 #ifndef DNS_USE_CPU_CLOCK_FOR_ID
49 #ifndef DNS_USE_GETTIMEOFDAY_FOR_ID
50 #ifndef DNS_USE_OPENSSL_FOR_ID
51 #error Must configure at least one id generation method.
52 #error Please see the documentation.
53 #endif
54 #endif
55 #endif
56
57 /* #define _POSIX_C_SOURCE 200507 */
58 #define _GNU_SOURCE
59
60 #ifdef DNS_USE_CPU_CLOCK_FOR_ID
61 #ifdef DNS_USE_OPENSSL_FOR_ID
62 #error Multiple id options selected
63 #endif
64 #ifdef DNS_USE_GETTIMEOFDAY_FOR_ID
65 #error Multiple id options selected
66 #endif
67 #include <time.h>
68 #endif
69
70 #ifdef DNS_USE_OPENSSL_FOR_ID
71 #ifdef DNS_USE_GETTIMEOFDAY_FOR_ID
72 #error Multiple id options selected
73 #endif
74 #include <openssl/rand.h>
75 #endif
76
77 #define _FORTIFY_SOURCE 3
78
79 #include <string.h>
80 #include <fcntl.h>
81 #include <sys/time.h>
82 #ifdef HAVE_STDINT_H
83 #include <stdint.h>
84 #endif
85 #include <stdlib.h>
86 #include <string.h>
87 #include <errno.h>
88 #include <assert.h>
89 #include <unistd.h>
90 #include <limits.h>
91 #include <sys/stat.h>
92 #include <ctype.h>
93 #include <stdio.h>
94 #include <stdarg.h>
95
96 #include "evdns.h"
97 #ifdef WIN32
98 #include <windows.h>
99 #include <winsock2.h>
100 #include <iphlpapi.h>
101 #else
102 #include <sys/socket.h>
103 #include <netinet/in.h>
104 #include <arpa/inet.h>
105 #endif
106
107 #ifdef HAVE_NETINET_IN6_H
108 #include <netinet/in6.h>
109 #endif
110
111 #ifdef WIN32
112 typedef int socklen_t;
113 #endif
114
115 #define EVDNS_LOG_DEBUG 0
116 #define EVDNS_LOG_WARN 1
117
118 #ifndef HOST_NAME_MAX
119 #define HOST_NAME_MAX 255
120 #endif
121
122 #ifndef NDEBUG
123 #include <stdio.h>
124 #endif
125
126 #undef MIN
127 #define MIN(a,b) ((a)<(b)?(a):(b))
128
129 #ifdef __USE_ISOC99B
130 /* libevent doesn't work without this */
131 typedef uint8_t u_char;
132 typedef unsigned int uint;
133 #endif
134 #include <event.h>
135
136 #define u64 uint64_t
137 #define u32 uint32_t
138 #define u16 uint16_t
139 #define u8  uint8_t
140
141 #define MAX_ADDRS 4  /* maximum number of addresses from a single packet */
142 /* which we bother recording */
143
144 #define TYPE_A         EVDNS_TYPE_A
145 #define TYPE_CNAME     5
146 #define TYPE_PTR       EVDNS_TYPE_PTR
147 #define TYPE_AAAA      EVDNS_TYPE_AAAA
148
149 #define CLASS_INET     EVDNS_CLASS_INET
150
151 struct request {
152         u8 *request;  /* the dns packet data */
153         unsigned int request_len;
154         int reissue_count;
155         int tx_count;  /* the number of times that this packet has been sent */
156         unsigned int request_type; /* TYPE_PTR or TYPE_A */
157         void *user_pointer;  /* the pointer given to us for this request */
158         evdns_callback_type user_callback;
159         struct nameserver *ns;  /* the server which we last sent it */
160
161         /* elements used by the searching code */
162         int search_index;
163         struct search_state *search_state;
164         char *search_origname;  /* needs to be free()ed */
165         int search_flags;
166
167         /* these objects are kept in a circular list */
168         struct request *next, *prev;
169
170         struct event timeout_event;
171
172         u16 trans_id;  /* the transaction id */
173         char request_appended;  /* true if the request pointer is data which follows this struct */
174         char transmit_me;  /* needs to be transmitted */
175 };
176
177 #ifndef HAVE_STRUCT_IN6_ADDR
178 struct in6_addr {
179         u8 s6_addr[16];
180 };
181 #endif
182
183 struct reply {
184         unsigned int type;
185         unsigned int have_answer;
186         union {
187                 struct {
188                         u32 addrcount;
189                         u32 addresses[MAX_ADDRS];
190                 } a;
191                 struct {
192                         u32 addrcount;
193                         struct in6_addr addresses[MAX_ADDRS];
194                 } aaaa;
195                 struct {
196                         char name[HOST_NAME_MAX];
197                 } ptr;
198         } data;
199 };
200
201 struct nameserver {
202         int socket;  /* a connected UDP socket */
203         u32 address;
204         int failed_times;  /* number of times which we have given this server a chance */
205         int timedout;  /* number of times in a row a request has timed out */
206         struct event event;
207         /* these objects are kept in a circular list */
208         struct nameserver *next, *prev;
209         struct event timeout_event;  /* used to keep the timeout for */
210                                      /* when we next probe this server. */
211                                      /* Valid if state == 0 */
212         char state;  /* zero if we think that this server is down */
213         char choked;  /* true if we have an EAGAIN from this server's socket */
214         char write_waiting;  /* true if we are waiting for EV_WRITE events */
215 };
216
217 static struct request *req_head = NULL, *req_waiting_head = NULL;
218 static struct nameserver *server_head = NULL;
219
220 /* Represents a local port where we're listening for DNS requests. Right now, */
221 /* only UDP is supported. */
222 struct evdns_server_port {
223         int socket; /* socket we use to read queries and write replies. */
224         int refcnt; /* reference count. */
225         char choked; /* Are we currently blocked from writing? */
226         char closing; /* Are we trying to close this port, pending writes? */
227         evdns_request_callback_fn_type user_callback; /* Fn to handle requests */
228         void *user_data; /* Opaque pointer passed to user_callback */
229         struct event event; /* Read/write event */
230         /* circular list of replies that we want to write. */
231         struct server_request *pending_replies;
232 };
233
234 /* Represents part of a reply being built.      (That is, a single RR.) */
235 struct server_reply_item {
236         struct server_reply_item *next; /* next item in sequence. */
237         char *name; /* name part of the RR */
238         u16 type : 16; /* The RR type */
239         u16 class : 16; /* The RR class (usually CLASS_INET) */
240         u32 ttl; /* The RR TTL */
241         char is_name; /* True iff data is a label */
242         u16 datalen; /* Length of data; -1 if data is a label */
243         void *data; /* The contents of the RR */
244 };
245
246 /* Represents a request that we've received as a DNS server, and holds */
247 /* the components of the reply as we're constructing it. */
248 struct server_request {
249         /* Pointers to the next and previous entries on the list of replies */
250         /* that we're waiting to write.  Only set if we have tried to respond */
251         /* and gotten EAGAIN. */
252         struct server_request *next_pending;
253         struct server_request *prev_pending;
254
255         u16 trans_id; /* Transaction id. */
256         struct evdns_server_port *port; /* Which port received this request on? */
257         struct sockaddr_storage addr; /* Where to send the response */
258         socklen_t addrlen; /* length of addr */
259
260         int n_answer; /* how many answer RRs have been set? */
261         int n_authority; /* how many authority RRs have been set? */
262         int n_additional; /* how many additional RRs have been set? */
263
264         struct server_reply_item *answer; /* linked list of answer RRs */
265         struct server_reply_item *authority; /* linked list of authority RRs */
266         struct server_reply_item *additional; /* linked list of additional RRs */
267
268         /* Constructed response.  Only set once we're ready to send a reply. */
269         /* Once this is set, the RR fields are cleared, and no more should be set. */
270         char *response;
271         size_t response_len;
272
273         /* Caller-visible fields: flags, questions. */
274         struct evdns_server_request base;
275 };
276
277 /* helper macro */
278 #define OFFSET_OF(st, member) ((off_t) (((char*)&((st*)0)->member)-(char*)0))
279
280 /* Given a pointer to an evdns_server_request, get the corresponding */
281 /* server_request. */
282 #define TO_SERVER_REQUEST(base_ptr)                                                                             \
283         ((struct server_request*)                                                                                       \
284          (((char*)(base_ptr) - OFFSET_OF(struct server_request, base))))
285
286 /* The number of good nameservers that we have */
287 static int global_good_nameservers = 0;
288
289 /* inflight requests are contained in the req_head list */
290 /* and are actually going out across the network */
291 static int global_requests_inflight = 0;
292 /* requests which aren't inflight are in the waiting list */
293 /* and are counted here */
294 static int global_requests_waiting = 0;
295
296 static int global_max_requests_inflight = 64;
297
298 static struct timeval global_timeout = {5, 0};  /* 5 seconds */
299 static int global_max_reissues = 1;  /* a reissue occurs when we get some errors from the server */
300 static int global_max_retransmits = 3;  /* number of times we'll retransmit a request which timed out */
301 /* number of timeouts in a row before we consider this server to be down */
302 static int global_max_nameserver_timeout = 3;
303
304 /* These are the timeout values for nameservers. If we find a nameserver is down */
305 /* we try to probe it at intervals as given below. Values are in seconds. */
306 static const struct timeval global_nameserver_timeouts[] = {{10, 0}, {60, 0}, {300, 0}, {900, 0}, {3600, 0}};
307 static const int global_nameserver_timeouts_length = sizeof(global_nameserver_timeouts)/sizeof(struct timeval);
308
309 static struct nameserver *nameserver_pick(void);
310 static void evdns_request_insert(struct request *req, struct request **head);
311 static void nameserver_ready_callback(int fd, short events, void *arg);
312 static int evdns_transmit(void);
313 static int evdns_request_transmit(struct request *req);
314 static void nameserver_send_probe(struct nameserver *const ns);
315 static void search_request_finished(struct request *const);
316 static int search_try_next(struct request *const req);
317 static int search_request_new(int type, const char *const name, int flags, evdns_callback_type user_callback, void *user_arg);
318 static void evdns_requests_pump_waiting_queue(void);
319 static u16 transaction_id_pick(void);
320 static struct request *request_new(int type, const char *name, int flags, evdns_callback_type callback, void *ptr);
321 static void request_submit(struct request *req);
322
323 static int server_request_free(struct server_request *req);
324 static void server_request_free_answers(struct server_request *req);
325 static void server_port_free(struct evdns_server_port *port);
326 static void server_port_ready_callback(int fd, short events, void *arg);
327
328 static int strtoint(const char *const str);
329
330 #ifdef WIN32
331 static int
332 last_error(int sock)
333 {
334         int optval, optvallen=sizeof(optval);
335         int err = WSAGetLastError();
336         if (err == WSAEWOULDBLOCK && sock >= 0) {
337                 if (getsockopt(sock, SOL_SOCKET, SO_ERROR, (void*)&optval,
338                                &optvallen))
339                         return err;
340                 if (optval)
341                         return optval;
342         }
343         return err;
344
345 }
346 static int
347 error_is_eagain(int err)
348 {
349         return err == EAGAIN || err == WSAEWOULDBLOCK;
350 }
351 static int
352 inet_aton(const char *c, struct in_addr *addr)
353 {
354         uint32_t r;
355         if (strcmp(c, "255.255.255.255") == 0) {
356                 addr->s_addr = 0xffffffffu;
357         } else {
358                 r = inet_addr(c);
359                 if (r == INADDR_NONE)
360                         return 0;
361                 addr->s_addr = r;
362         }
363         return 1;
364 }
365 #define CLOSE_SOCKET(x) closesocket(x)
366 #else
367 #define last_error(sock) (errno)
368 #define error_is_eagain(err) ((err) == EAGAIN)
369 #define CLOSE_SOCKET(x) close(x)
370 #endif
371
372 #define ISSPACE(c) isspace((int)(unsigned char)(c))
373 #define ISDIGIT(c) isdigit((int)(unsigned char)(c))
374
375 #ifndef NDEBUG
376 static const char *
377 debug_ntoa(u32 address)
378 {
379         static char buf[32];
380         u32 a = ntohl(address);
381         snprintf(buf, sizeof(buf), "%d.%d.%d.%d",
382                       (int)(u8)((a>>24)&0xff),
383                       (int)(u8)((a>>16)&0xff),
384                       (int)(u8)((a>>8 )&0xff),
385                       (int)(u8)((a    )&0xff));
386         return buf;
387 }
388 #endif
389
390 static evdns_debug_log_fn_type evdns_log_fn = NULL;
391
392 void
393 evdns_set_log_fn(evdns_debug_log_fn_type fn)
394 {
395   evdns_log_fn = fn;
396 }
397
398 #ifdef __GNUC__
399 #define EVDNS_LOG_CHECK  __attribute__ ((format(printf, 2, 3)))
400 #else
401 #define EVDNS_LOG_CHECK
402 #endif
403
404 static void _evdns_log(int warn, const char *fmt, ...) EVDNS_LOG_CHECK;
405 static void
406 _evdns_log(int warn, const char *fmt, ...)
407 {
408   va_list args;
409   static char buf[512];
410   if (!evdns_log_fn)
411     return;
412   va_start(args,fmt);
413 #ifdef WIN32
414   _vsnprintf(buf, sizeof(buf), fmt, args);
415 #else
416   vsnprintf(buf, sizeof(buf), fmt, args);
417 #endif
418   buf[sizeof(buf)-1] = '\0';
419   evdns_log_fn(warn, buf);
420   va_end(args);
421 }
422
423 #define log _evdns_log
424
425 /* This walks the list of inflight requests to find the */
426 /* one with a matching transaction id. Returns NULL on */
427 /* failure */
428 static struct request *
429 request_find_from_trans_id(u16 trans_id) {
430         struct request *req = req_head, *const started_at = req_head;
431
432         if (req) {
433                 do {
434                         if (req->trans_id == trans_id) return req;
435                         req = req->next;
436                 } while (req != started_at);
437         }
438
439         return NULL;
440 }
441
442 /* a libevent callback function which is called when a nameserver */
443 /* has gone down and we want to test if it has came back to life yet */
444 static void
445 nameserver_prod_callback(int fd, short events, void *arg) {
446         struct nameserver *const ns = (struct nameserver *) arg;
447         (void)fd;
448         (void)events;
449
450         nameserver_send_probe(ns);
451 }
452
453 /* a libevent callback which is called when a nameserver probe (to see if */
454 /* it has come back to life) times out. We increment the count of failed_times */
455 /* and wait longer to send the next probe packet. */
456 static void
457 nameserver_probe_failed(struct nameserver *const ns) {
458         const struct timeval * timeout;
459         (void) evtimer_del(&ns->timeout_event);
460         if (ns->state == 1) {
461                 /* This can happen if the nameserver acts in a way which makes us mark */
462                 /* it as bad and then starts sending good replies. */
463                 return;
464         }
465
466         timeout =
467           &global_nameserver_timeouts[MIN(ns->failed_times,
468                                           global_nameserver_timeouts_length - 1)];
469         ns->failed_times++;
470
471         evtimer_set(&ns->timeout_event, nameserver_prod_callback, ns);
472         if (evtimer_add(&ns->timeout_event, (struct timeval *) timeout) < 0) {
473           log(EVDNS_LOG_WARN,
474               "Error from libevent when adding timer event for %s",
475               debug_ntoa(ns->address));
476           /* ???? Do more? */
477         }
478 }
479
480 /* called when a nameserver has been deemed to have failed. For example, too */
481 /* many packets have timed out etc */
482 static void
483 nameserver_failed(struct nameserver *const ns, const char *msg) {
484         struct request *req, *started_at;
485         /* if this nameserver has already been marked as failed */
486         /* then don't do anything */
487         if (!ns->state) return;
488
489         log(EVDNS_LOG_WARN, "Nameserver %s has failed: %s",
490             debug_ntoa(ns->address), msg);
491         global_good_nameservers--;
492         assert(global_good_nameservers >= 0);
493         if (global_good_nameservers == 0) {
494                 log(EVDNS_LOG_WARN, "All nameservers have failed");
495         }
496
497         ns->state = 0;
498         ns->failed_times = 1;
499
500         evtimer_set(&ns->timeout_event, nameserver_prod_callback, ns);
501         if (evtimer_add(&ns->timeout_event, (struct timeval *) &global_nameserver_timeouts[0]) < 0) {
502                 log(EVDNS_LOG_WARN,
503                     "Error from libevent when adding timer event for %s",
504                     debug_ntoa(ns->address));
505                 /* ???? Do more? */
506         }
507
508         /* walk the list of inflight requests to see if any can be reassigned to */
509         /* a different server. Requests in the waiting queue don't have a */
510         /* nameserver assigned yet */
511
512         /* if we don't have *any* good nameservers then there's no point */
513         /* trying to reassign requests to one */
514         if (!global_good_nameservers) return;
515
516         req = req_head;
517         started_at = req_head;
518         if (req) {
519                 do {
520                         if (req->tx_count == 0 && req->ns == ns) {
521                                 /* still waiting to go out, can be moved */
522                                 /* to another server */
523                                 req->ns = nameserver_pick();
524                         }
525                         req = req->next;
526                 } while (req != started_at);
527         }
528 }
529
530 static void
531 nameserver_up(struct nameserver *const ns) {
532         if (ns->state) return;
533         log(EVDNS_LOG_WARN, "Nameserver %s is back up",
534             debug_ntoa(ns->address));
535         evtimer_del(&ns->timeout_event);
536         ns->state = 1;
537         ns->failed_times = 0;
538         ns->timedout = 0;
539         global_good_nameservers++;
540 }
541
542 static void
543 request_trans_id_set(struct request *const req, const u16 trans_id) {
544         req->trans_id = trans_id;
545         *((u16 *) req->request) = htons(trans_id);
546 }
547
548 /* Called to remove a request from a list and dealloc it. */
549 /* head is a pointer to the head of the list it should be */
550 /* removed from or NULL if the request isn't in a list. */
551 static void
552 request_finished(struct request *const req, struct request **head) {
553         if (head) {
554                 if (req->next == req) {
555                         /* only item in the list */
556                         *head = NULL;
557                 } else {
558                         req->next->prev = req->prev;
559                         req->prev->next = req->next;
560                         if (*head == req) *head = req->next;
561                 }
562         }
563
564         log(EVDNS_LOG_DEBUG, "Removing timeout for request %lx",
565             (unsigned long) req);
566         evtimer_del(&req->timeout_event);
567
568         search_request_finished(req);
569         global_requests_inflight--;
570
571         if (!req->request_appended) {
572                 /* need to free the request data on it's own */
573                 free(req->request);
574         } else {
575                 /* the request data is appended onto the header */
576                 /* so everything gets free()ed when we: */
577         }
578
579         free(req);
580
581         evdns_requests_pump_waiting_queue();
582 }
583
584 /* This is called when a server returns a funny error code. */
585 /* We try the request again with another server. */
586 /* */
587 /* return: */
588 /*   0 ok */
589 /*   1 failed/reissue is pointless */
590 static int
591 request_reissue(struct request *req) {
592         const struct nameserver *const last_ns = req->ns;
593         /* the last nameserver should have been marked as failing */
594         /* by the caller of this function, therefore pick will try */
595         /* not to return it */
596         req->ns = nameserver_pick();
597         if (req->ns == last_ns) {
598                 /* ... but pick did return it */
599                 /* not a lot of point in trying again with the */
600                 /* same server */
601                 return 1;
602         }
603
604         req->reissue_count++;
605         req->tx_count = 0;
606         req->transmit_me = 1;
607
608         return 0;
609 }
610
611 /* this function looks for space on the inflight queue and promotes */
612 /* requests from the waiting queue if it can. */
613 static void
614 evdns_requests_pump_waiting_queue(void) {
615         while (global_requests_inflight < global_max_requests_inflight &&
616             global_requests_waiting) {
617                 struct request *req;
618                 /* move a request from the waiting queue to the inflight queue */
619                 assert(req_waiting_head);
620                 if (req_waiting_head->next == req_waiting_head) {
621                         /* only one item in the queue */
622                         req = req_waiting_head;
623                         req_waiting_head = NULL;
624                 } else {
625                         req = req_waiting_head;
626                         req->next->prev = req->prev;
627                         req->prev->next = req->next;
628                         req_waiting_head = req->next;
629                 }
630
631                 global_requests_waiting--;
632                 global_requests_inflight++;
633
634                 req->ns = nameserver_pick();
635                 request_trans_id_set(req, transaction_id_pick());
636
637                 evdns_request_insert(req, &req_head);
638                 evdns_request_transmit(req);
639                 evdns_transmit();
640         }
641 }
642
643 static void
644 reply_callback(struct request *const req, u32 ttl, u32 err, struct reply *reply) {
645         switch (req->request_type) {
646         case TYPE_A:
647                 if (reply)
648                         req->user_callback(DNS_ERR_NONE, DNS_IPv4_A,
649                                                            reply->data.a.addrcount, ttl,
650                                                  reply->data.a.addresses,
651                                                            req->user_pointer);
652                 else
653                         req->user_callback(err, 0, 0, 0, NULL, req->user_pointer);
654                 return;
655         case TYPE_PTR:
656                 if (reply) {
657                         char *name = reply->data.ptr.name;
658                         req->user_callback(DNS_ERR_NONE, DNS_PTR, 1, ttl,
659                                                            &name, req->user_pointer);
660                 } else {
661                         req->user_callback(err, 0, 0, 0, NULL,
662                                                            req->user_pointer);
663                 }
664                 return;
665         case TYPE_AAAA:
666                 if (reply)
667                         req->user_callback(DNS_ERR_NONE, DNS_IPv6_AAAA,
668                                                            reply->data.aaaa.addrcount, ttl,
669                                                            reply->data.aaaa.addresses,
670                                                            req->user_pointer);
671                 else
672                         req->user_callback(err, 0, 0, 0, NULL, req->user_pointer);
673                 return;
674         }
675         assert(0);
676 }
677
678 /* this processes a parsed reply packet */
679 static void
680 reply_handle(struct request *const req, u16 flags, u32 ttl, struct reply *reply) {
681         int error;
682         static const int error_codes[] = {DNS_ERR_FORMAT, DNS_ERR_SERVERFAILED, DNS_ERR_NOTEXIST, DNS_ERR_NOTIMPL, DNS_ERR_REFUSED};
683
684         if (flags & 0x020f || !reply || !reply->have_answer) {
685                 /* there was an error */
686                 if (flags & 0x0200) {
687                         error = DNS_ERR_TRUNCATED;
688                 } else {
689                         u16 error_code = (flags & 0x000f) - 1;
690                         if (error_code > 4) {
691                                 error = DNS_ERR_UNKNOWN;
692                         } else {
693                                 error = error_codes[error_code];
694                         }
695                 }
696
697                 switch(error) {
698                 case DNS_ERR_NOTIMPL:
699                 case DNS_ERR_REFUSED:
700                         /* we regard these errors as marking a bad nameserver */
701                         if (req->reissue_count < global_max_reissues) {
702                                 char msg[64];
703                                 snprintf(msg, sizeof(msg), "Bad response %d (%s)",
704                                          error, evdns_err_to_string(error));
705                                 nameserver_failed(req->ns, msg);
706                                 if (!request_reissue(req)) return;
707                         }
708                         break;
709                 case DNS_ERR_SERVERFAILED:
710                         /* rcode 2 (servfailed) sometimes means "we are broken" and
711                          * sometimes (with some binds) means "that request was very
712                          * confusing."  Treat this as a timeout, not a failure. 
713                          */
714                         log(EVDNS_LOG_DEBUG, "Got a SERVERFAILED from nameserver %s; "
715                                 "will allow the request to time out.",
716                                 debug_ntoa(req->ns->address));
717                         break;
718                 default:
719                         /* we got a good reply from the nameserver */
720                         nameserver_up(req->ns);
721                 }
722
723                 if (req->search_state && req->request_type != TYPE_PTR) {
724                         /* if we have a list of domains to search in, try the next one */
725                         if (!search_try_next(req)) {
726                                 /* a new request was issued so this request is finished and */
727                                 /* the user callback will be made when that request (or a */
728                                 /* child of it) finishes. */
729                                 request_finished(req, &req_head);
730                                 return;
731                         }
732                 }
733
734                 /* all else failed. Pass the failure up */
735                 reply_callback(req, 0, error, NULL);
736                 request_finished(req, &req_head);
737         } else {
738                 /* all ok, tell the user */
739                 reply_callback(req, ttl, 0, reply);
740                 nameserver_up(req->ns);
741                 request_finished(req, &req_head);
742         }
743 }
744
745 static int
746 name_parse(u8 *packet, int length, int *idx, char *name_out, int name_out_len) {
747         int name_end = -1;
748         int j = *idx;
749         int ptr_count = 0;
750 #define GET32(x) do { if (j + 4 > length) goto err; memcpy(&_t32, packet + j, 4); j += 4; x = ntohl(_t32); } while(0)
751 #define GET16(x) do { if (j + 2 > length) goto err; memcpy(&_t, packet + j, 2); j += 2; x = ntohs(_t); } while(0)
752 #define GET8(x) do { if (j >= length) goto err; x = packet[j++]; } while(0)
753
754         char *cp = name_out;
755         const char *const end = name_out + name_out_len;
756
757         /* Normally, names are a series of length prefixed strings terminated */
758         /* with a length of 0 (the lengths are u8's < 63). */
759         /* However, the length can start with a pair of 1 bits and that */
760         /* means that the next 14 bits are a pointer within the current */
761         /* packet. */
762
763         for(;;) {
764                 u8 label_len;
765                 if (j >= length) return -1;
766                 GET8(label_len);
767                 if (!label_len) break;
768                 if (label_len & 0xc0) {
769                         u8 ptr_low;
770                         GET8(ptr_low);
771                         if (name_end < 0) name_end = j;
772                         j = (((int)label_len & 0x3f) << 8) + ptr_low;
773                         /* Make sure that the target offset is in-bounds. */
774                         if (j < 0 || j >= length) return -1;
775                         /* If we've jumped more times than there are characters in the
776                          * message, we must have a loop. */
777                         if (++ptr_count > length) return -1;
778                         continue;
779                 }
780                 if (label_len > 63) return -1;
781                 if (cp != name_out) {
782                         if (cp + 1 >= end) return -1;
783                         *cp++ = '.';
784                 }
785                 if (cp + label_len >= end) return -1;
786                 memcpy(cp, packet + j, label_len);
787                 cp += label_len;
788                 j += label_len;
789         }
790         if (cp >= end) return -1;
791         *cp = '\0';
792         if (name_end < 0)
793                 *idx = j;
794         else
795                 *idx = name_end;
796         return 0;
797  err:
798         return -1;
799 }
800
801 /* parses a raw request from a nameserver */
802 static int
803 reply_parse(u8 *packet, int length) {
804         int j = 0;  /* index into packet */
805         u16 _t;  /* used by the macros */
806         u32 _t32;  /* used by the macros */
807         char tmp_name[256]; /* used by the macros */
808
809         u16 trans_id, questions, answers, authority, additional, datalength;
810         u16 flags = 0;
811         u32 ttl, ttl_r = 0xffffffff;
812         struct reply reply;
813         struct request *req = NULL;
814         unsigned int i;
815
816         GET16(trans_id);
817         GET16(flags);
818         GET16(questions);
819         GET16(answers);
820         GET16(authority);
821         GET16(additional);
822         (void) authority; /* suppress "unused variable" warnings. */
823         (void) additional; /* suppress "unused variable" warnings. */
824
825         req = request_find_from_trans_id(trans_id);
826         if (!req) return -1;
827
828         memset(&reply, 0, sizeof(reply));
829
830         /* If it's not an answer, it doesn't correspond to any request. */
831         if (!(flags & 0x8000)) return -1;  /* must be an answer */
832         if (flags & 0x020f) {
833                 /* there was an error */
834                 goto err;
835         }
836         /* if (!answers) return; */  /* must have an answer of some form */
837
838         /* This macro skips a name in the DNS reply. */
839 #define SKIP_NAME \
840         do { tmp_name[0] = '\0';                                        \
841                 if (name_parse(packet, length, &j, tmp_name, sizeof(tmp_name))<0) \
842                         goto err;                                                                                                       \
843         } while(0);
844
845         reply.type = req->request_type;
846
847         /* skip over each question in the reply */
848         for (i = 0; i < questions; ++i) {
849                 /* the question looks like
850                  *   <label:name><u16:type><u16:class>
851                  */
852                 SKIP_NAME;
853                 j += 4;
854                 if (j >= length) goto err;
855         }
856
857         /* now we have the answer section which looks like
858          * <label:name><u16:type><u16:class><u32:ttl><u16:len><data...>
859          */
860
861         for (i = 0; i < answers; ++i) {
862                 u16 type, class;
863
864                 SKIP_NAME;
865                 GET16(type);
866                 GET16(class);
867                 GET32(ttl);
868                 GET16(datalength);
869
870                 if (type == TYPE_A && class == CLASS_INET) {
871                         int addrcount, addrtocopy;
872                         if (req->request_type != TYPE_A) {
873                                 j += datalength; continue;
874                         }
875                         if ((datalength & 3) != 0) /* not an even number of As. */
876                             goto err;
877                         addrcount = datalength >> 2;
878                         addrtocopy = MIN(MAX_ADDRS - reply.data.a.addrcount, (unsigned)addrcount);
879
880                         ttl_r = MIN(ttl_r, ttl);
881                         /* we only bother with the first four addresses. */
882                         if (j + 4*addrtocopy > length) goto err;
883                         memcpy(&reply.data.a.addresses[reply.data.a.addrcount],
884                                    packet + j, 4*addrtocopy);
885                         j += 4*addrtocopy;
886                         reply.data.a.addrcount += addrtocopy;
887                         reply.have_answer = 1;
888                         if (reply.data.a.addrcount == MAX_ADDRS) break;
889                 } else if (type == TYPE_PTR && class == CLASS_INET) {
890                         if (req->request_type != TYPE_PTR) {
891                                 j += datalength; continue;
892                         }
893                         if (name_parse(packet, length, &j, reply.data.ptr.name,
894                                                    sizeof(reply.data.ptr.name))<0)
895                                 goto err;
896                         ttl_r = MIN(ttl_r, ttl);
897                         reply.have_answer = 1;
898                         break;
899                 } else if (type == TYPE_AAAA && class == CLASS_INET) {
900                         int addrcount, addrtocopy;
901                         if (req->request_type != TYPE_AAAA) {
902                                 j += datalength; continue;
903                         }
904                         if ((datalength & 15) != 0) /* not an even number of AAAAs. */
905                                 goto err;
906                         addrcount = datalength >> 4;  /* each address is 16 bytes long */
907                         addrtocopy = MIN(MAX_ADDRS - reply.data.aaaa.addrcount, (unsigned)addrcount);
908                         ttl_r = MIN(ttl_r, ttl);
909
910                         /* we only bother with the first four addresses. */
911                         if (j + 16*addrtocopy > length) goto err;
912                         memcpy(&reply.data.aaaa.addresses[reply.data.aaaa.addrcount],
913                                    packet + j, 16*addrtocopy);
914                         reply.data.aaaa.addrcount += addrtocopy;
915                         j += 16*addrtocopy;
916                         reply.have_answer = 1;
917                         if (reply.data.aaaa.addrcount == MAX_ADDRS) break;
918                 } else {
919                         /* skip over any other type of resource */
920                         j += datalength;
921                 }
922         }
923
924         reply_handle(req, flags, ttl_r, &reply);
925         return 0;
926  err:
927         if (req)
928                 reply_handle(req, flags, 0, NULL);
929         return -1;
930 }
931
932 /* Parse a raw request (packet,length) sent to a nameserver port (port) from */
933 /* a DNS client (addr,addrlen), and if it's well-formed, call the corresponding */
934 /* callback. */
935 static int
936 request_parse(u8 *packet, int length, struct evdns_server_port *port, struct sockaddr *addr, socklen_t addrlen)
937 {
938         int j = 0;      /* index into packet */
939         u16 _t;  /* used by the macros */
940         char tmp_name[256]; /* used by the macros */
941
942         int i;
943         u16 trans_id, flags, questions, answers, authority, additional;
944         struct server_request *server_req = NULL;
945
946         /* Get the header fields */
947         GET16(trans_id);
948         GET16(flags);
949         GET16(questions);
950         GET16(answers);
951         GET16(authority);
952         GET16(additional);
953
954         if (flags & 0x8000) return -1; /* Must not be an answer. */
955         if (flags & 0x7800) return -1; /* only standard queries are supported */
956         flags &= 0x0300; /* Only TC and RD get preserved. */
957
958         server_req = malloc(sizeof(struct server_request));
959         if (server_req == NULL) return -1;
960         memset(server_req, 0, sizeof(struct server_request));
961
962         server_req->trans_id = trans_id;
963         memcpy(&server_req->addr, addr, addrlen);
964         server_req->addrlen = addrlen;
965
966         server_req->base.flags = flags;
967         server_req->base.nquestions = 0;
968         server_req->base.questions = malloc(sizeof(struct evdns_server_question *) * questions);
969         if (server_req->base.questions == NULL)
970                 goto err;
971
972         for (i = 0; i < questions; ++i) {
973                 u16 type, class;
974                 struct evdns_server_question *q;
975                 int namelen;
976                 if (name_parse(packet, length, &j, tmp_name, sizeof(tmp_name))<0)
977                         goto err;
978                 GET16(type);
979                 GET16(class);
980                 namelen = strlen(tmp_name);
981                 q = malloc(sizeof(struct evdns_server_question) + namelen);
982                 if (!q)
983                         goto err;
984                 q->type = type;
985                 q->class = class;
986                 memcpy(q->name, tmp_name, namelen+1);
987                 server_req->base.questions[server_req->base.nquestions++] = q;
988         }
989
990         /* Ignore answers, authority, and additional. */
991
992         server_req->port = port;
993         port->refcnt++;
994         port->user_callback(&(server_req->base), port->user_data);
995
996         return 0;
997 err:
998         if (server_req) {
999                 if (server_req->base.questions) {
1000                         for (i = 0; i < server_req->base.nquestions; ++i)
1001                                 free(server_req->base.questions[i]);
1002                         free(server_req->base.questions);
1003                 }
1004                 free(server_req);
1005         }
1006         return -1;
1007
1008 #undef SKIP_NAME
1009 #undef GET32
1010 #undef GET16
1011 #undef GET8
1012 }
1013
1014 /* Try to choose a strong transaction id which isn't already in flight */
1015 static u16
1016 transaction_id_pick(void) {
1017         for (;;) {
1018                 const struct request *req = req_head, *started_at;
1019 #ifdef DNS_USE_CPU_CLOCK_FOR_ID
1020                 struct timespec ts;
1021                 u16 trans_id;
1022 #ifdef CLOCK_MONOTONIC
1023                 if (clock_gettime(CLOCK_MONOTONIC, &ts) == -1)
1024 #else
1025                 if (clock_gettime(CLOCK_REALTIME, &ts) == -1)
1026 #endif
1027                         event_err(1, "clock_gettime");
1028                 trans_id = ts.tv_nsec & 0xffff;
1029 #endif
1030
1031 #ifdef DNS_USE_GETTIMEOFDAY_FOR_ID
1032                 struct timeval tv;
1033                 u16 trans_id;
1034                 gettimeofday(&tv, NULL);
1035                 trans_id = tv.tv_usec & 0xffff;
1036 #endif
1037
1038 #ifdef DNS_USE_OPENSSL_FOR_ID
1039                 u16 trans_id;
1040                 if (RAND_pseudo_bytes((u8 *) &trans_id, 2) == -1) {
1041                         /* in the case that the RAND call fails we back */
1042                         /* down to using gettimeofday. */
1043                         struct timeval tv;
1044                         gettimeofday(&tv, NULL);
1045                         trans_id = tv.tv_usec & 0xffff; */
1046                         abort();
1047                 }
1048 #endif
1049
1050                 if (trans_id == 0xffff) continue;
1051                 /* now check to see if that id is already inflight */
1052                 req = started_at = req_head;
1053                 if (req) {
1054                         do {
1055                                 if (req->trans_id == trans_id) break;
1056                                 req = req->next;
1057                         } while (req != started_at);
1058                 }
1059                 /* we didn't find it, so this is a good id */
1060                 if (req == started_at) return trans_id;
1061         }
1062 }
1063
1064 /* choose a namesever to use. This function will try to ignore */
1065 /* nameservers which we think are down and load balance across the rest */
1066 /* by updating the server_head global each time. */
1067 static struct nameserver *
1068 nameserver_pick(void) {
1069         struct nameserver *started_at = server_head, *picked;
1070         if (!server_head) return NULL;
1071
1072         /* if we don't have any good nameservers then there's no */
1073         /* point in trying to find one. */
1074         if (!global_good_nameservers) {
1075                 server_head = server_head->next;
1076                 return server_head;
1077         }
1078
1079         /* remember that nameservers are in a circular list */
1080         for (;;) {
1081                 if (server_head->state) {
1082                         /* we think this server is currently good */
1083                         picked = server_head;
1084                         server_head = server_head->next;
1085                         return picked;
1086                 }
1087
1088                 server_head = server_head->next;
1089                 if (server_head == started_at) {
1090                         /* all the nameservers seem to be down */
1091                         /* so we just return this one and hope for the */
1092                         /* best */
1093                         assert(global_good_nameservers == 0);
1094                         picked = server_head;
1095                         server_head = server_head->next;
1096                         return picked;
1097                 }
1098         }
1099 }
1100
1101 /* this is called when a namesever socket is ready for reading */
1102 static void
1103 nameserver_read(struct nameserver *ns) {
1104         u8 packet[1500];
1105
1106         for (;;) {
1107                 const int r = recv(ns->socket, packet, sizeof(packet), 0);
1108                 if (r < 0) {
1109                         int err = last_error(ns->socket);
1110                         if (error_is_eagain(err)) return;
1111                         nameserver_failed(ns, strerror(err));
1112                         return;
1113                 }
1114                 ns->timedout = 0;
1115                 reply_parse(packet, r);
1116         }
1117 }
1118
1119 /* Read a packet from a DNS client on a server port s, parse it, and */
1120 /* act accordingly. */
1121 static void
1122 server_port_read(struct evdns_server_port *s) {
1123         u8 packet[1500];
1124         struct sockaddr_storage addr;
1125         socklen_t addrlen;
1126         int r;
1127
1128         for (;;) {
1129                 addrlen = sizeof(struct sockaddr_storage);
1130                 r = recvfrom(s->socket, packet, sizeof(packet), 0,
1131                                          (struct sockaddr*) &addr, &addrlen);
1132                 if (r < 0) {
1133                         int err = last_error(s->socket);
1134                         if (error_is_eagain(err)) return;
1135                         log(EVDNS_LOG_WARN, "Error %s (%d) while reading request.",
1136                                 strerror(err), err);
1137                         return;
1138                 }
1139                 request_parse(packet, r, s, (struct sockaddr*) &addr, addrlen);
1140         }
1141 }
1142
1143 /* Try to write all pending replies on a given DNS server port. */
1144 static void
1145 server_port_flush(struct evdns_server_port *port)
1146 {
1147         while (port->pending_replies) {
1148                 struct server_request *req = port->pending_replies;
1149                 int r = sendto(port->socket, req->response, req->response_len, 0,
1150                            (struct sockaddr*) &req->addr, req->addrlen);
1151                 if (r < 0) {
1152                         int err = last_error(port->socket);
1153                         if (error_is_eagain(err))
1154                                 return;
1155                         log(EVDNS_LOG_WARN, "Error %s (%d) while writing response to port; dropping", strerror(err), err);
1156                 }
1157                 if (server_request_free(req)) {
1158                         /* we released the last reference to req->port. */
1159                         return;
1160                 }
1161         }
1162
1163         /* We have no more pending requests; stop listening for 'writeable' events. */
1164         (void) event_del(&port->event);
1165         event_set(&port->event, port->socket, EV_READ | EV_PERSIST,
1166                           server_port_ready_callback, port);
1167         if (event_add(&port->event, NULL) < 0) {
1168                 log(EVDNS_LOG_WARN, "Error from libevent when adding event for DNS server.");
1169                 /* ???? Do more? */
1170         }
1171 }
1172
1173 /* set if we are waiting for the ability to write to this server. */
1174 /* if waiting is true then we ask libevent for EV_WRITE events, otherwise */
1175 /* we stop these events. */
1176 static void
1177 nameserver_write_waiting(struct nameserver *ns, char waiting) {
1178         if (ns->write_waiting == waiting) return;
1179
1180         ns->write_waiting = waiting;
1181         (void) event_del(&ns->event);
1182         event_set(&ns->event, ns->socket, EV_READ | (waiting ? EV_WRITE : 0) | EV_PERSIST,
1183                         nameserver_ready_callback, ns);
1184         if (event_add(&ns->event, NULL) < 0) {
1185           log(EVDNS_LOG_WARN, "Error from libevent when adding event for %s",
1186               debug_ntoa(ns->address));
1187           /* ???? Do more? */
1188         }
1189 }
1190
1191 /* a callback function. Called by libevent when the kernel says that */
1192 /* a nameserver socket is ready for writing or reading */
1193 static void
1194 nameserver_ready_callback(int fd, short events, void *arg) {
1195         struct nameserver *ns = (struct nameserver *) arg;
1196         (void)fd;
1197
1198         if (events & EV_WRITE) {
1199                 ns->choked = 0;
1200                 if (!evdns_transmit()) {
1201                         nameserver_write_waiting(ns, 0);
1202                 }
1203         }
1204         if (events & EV_READ) {
1205                 nameserver_read(ns);
1206         }
1207 }
1208
1209 /* a callback function. Called by libevent when the kernel says that */
1210 /* a server socket is ready for writing or reading. */
1211 static void
1212 server_port_ready_callback(int fd, short events, void *arg) {
1213         struct evdns_server_port *port = (struct evdns_server_port *) arg;
1214         (void) fd;
1215
1216         if (events & EV_WRITE) {
1217                 port->choked = 0;
1218                 server_port_flush(port);
1219         }
1220         if (events & EV_READ) {
1221                 server_port_read(port);
1222         }
1223 }
1224
1225 /* This is an inefficient representation; only use it via the dnslabel_table_*
1226  * functions, so that is can be safely replaced with something smarter later. */
1227 #define MAX_LABELS 128
1228 /* Structures used to implement name compression */
1229 struct dnslabel_entry { char *v; off_t pos; };
1230 struct dnslabel_table {
1231         int n_labels; /* number of current entries */
1232         /* map from name to position in message */
1233         struct dnslabel_entry labels[MAX_LABELS];
1234 };
1235
1236 /* Initialize dnslabel_table. */
1237 static void
1238 dnslabel_table_init(struct dnslabel_table *table)
1239 {
1240         table->n_labels = 0;
1241 }
1242
1243 /* Free all storage held by table, but not the table itself. */
1244 static void
1245 dnslabel_clear(struct dnslabel_table *table)
1246 {
1247         int i;
1248         for (i = 0; i < table->n_labels; ++i)
1249                 free(table->labels[i].v);
1250         table->n_labels = 0;
1251 }
1252
1253 /* return the position of the label in the current message, or -1 if the label */
1254 /* hasn't been used yet. */
1255 static int
1256 dnslabel_table_get_pos(const struct dnslabel_table *table, const char *label)
1257 {
1258         int i;
1259         for (i = 0; i < table->n_labels; ++i) {
1260                 if (!strcmp(label, table->labels[i].v))
1261                         return table->labels[i].pos;
1262         }
1263         return -1;
1264 }
1265
1266 /* remember that we've used the label at position pos */
1267 static int
1268 dnslabel_table_add(struct dnslabel_table *table, const char *label, off_t pos)
1269 {
1270         char *v;
1271         int p;
1272         if (table->n_labels == MAX_LABELS)
1273                 return (-1);
1274         v = strdup(label);
1275         if (v == NULL)
1276                 return (-1);
1277         p = table->n_labels++;
1278         table->labels[p].v = v;
1279         table->labels[p].pos = pos;
1280
1281         return (0);
1282 }
1283
1284 /* Converts a string to a length-prefixed set of DNS labels, starting */
1285 /* at buf[j]. name and buf must not overlap. name_len should be the length */
1286 /* of name.      table is optional, and is used for compression. */
1287 /* */
1288 /* Input: abc.def */
1289 /* Output: <3>abc<3>def<0> */
1290 /* */
1291 /* Returns the first index after the encoded name, or negative on error. */
1292 /*       -1      label was > 63 bytes */
1293 /*       -2      name too long to fit in buffer. */
1294 /* */
1295 static off_t
1296 dnsname_to_labels(u8 *const buf, size_t buf_len, off_t j,
1297                                   const char *name, const int name_len,
1298                                   struct dnslabel_table *table) {
1299         const char *end = name + name_len;
1300         int ref = 0;
1301         u16 _t;
1302
1303 #define APPEND16(x) do {                                                   \
1304                 if (j + 2 > (off_t)buf_len)                                \
1305                         goto overflow;                                             \
1306                 _t = htons(x);                                                     \
1307                 memcpy(buf + j, &_t, 2);                                   \
1308                 j += 2;                                                                    \
1309         } while (0)
1310 #define APPEND32(x) do {                                                   \
1311                 if (j + 4 > (off_t)buf_len)                                \
1312                         goto overflow;                                             \
1313                 _t32 = htonl(x);                                                   \
1314                 memcpy(buf + j, &_t32, 4);                                 \
1315                 j += 4;                                                                    \
1316         } while (0)
1317
1318         if (name_len > 255) return -2;
1319
1320         for (;;) {
1321                 const char *const start = name;
1322                 if (table && (ref = dnslabel_table_get_pos(table, name)) >= 0) {
1323                         APPEND16(ref | 0xc000);
1324                         return j;
1325                 }
1326                 name = strchr(name, '.');
1327                 if (!name) {
1328                         const unsigned int label_len = end - start;
1329                         if (label_len > 63) return -1;
1330                         if ((size_t)(j+label_len+1) > buf_len) return -2;
1331                         if (table) dnslabel_table_add(table, start, j);
1332                         buf[j++] = label_len;
1333
1334                         memcpy(buf + j, start, end - start);
1335                         j += end - start;
1336                         break;
1337                 } else {
1338                         /* append length of the label. */
1339                         const unsigned int label_len = name - start;
1340                         if (label_len > 63) return -1;
1341                         if ((size_t)(j+label_len+1) > buf_len) return -2;
1342                         if (table) dnslabel_table_add(table, start, j);
1343                         buf[j++] = label_len;
1344
1345                         memcpy(buf + j, start, name - start);
1346                         j += name - start;
1347                         /* hop over the '.' */
1348                         name++;
1349                 }
1350         }
1351
1352         /* the labels must be terminated by a 0. */
1353         /* It's possible that the name ended in a . */
1354         /* in which case the zero is already there */
1355         if (!j || buf[j-1]) buf[j++] = 0;
1356         return j;
1357  overflow:
1358         return (-2);
1359 }
1360
1361 /* Finds the length of a dns request for a DNS name of the given */
1362 /* length. The actual request may be smaller than the value returned */
1363 /* here */
1364 static int
1365 evdns_request_len(const int name_len) {
1366         return 96 + /* length of the DNS standard header */
1367                 name_len + 2 +
1368                 4;  /* space for the resource type */
1369 }
1370
1371 /* build a dns request packet into buf. buf should be at least as long */
1372 /* as evdns_request_len told you it should be. */
1373 /* */
1374 /* Returns the amount of space used. Negative on error. */
1375 static int
1376 evdns_request_data_build(const char *const name, const int name_len,
1377     const u16 trans_id, const u16 type, const u16 class,
1378     u8 *const buf, size_t buf_len) {
1379         off_t j = 0;  /* current offset into buf */
1380         u16 _t;  /* used by the macros */
1381
1382         APPEND16(trans_id);
1383         APPEND16(0x0100);  /* standard query, recusion needed */
1384         APPEND16(1);  /* one question */
1385         APPEND16(0);  /* no answers */
1386         APPEND16(0);  /* no authority */
1387         APPEND16(0);  /* no additional */
1388
1389         j = dnsname_to_labels(buf, buf_len, j, name, name_len, NULL);
1390         if (j < 0) {
1391                 return (int)j;
1392         }
1393         
1394         APPEND16(type);
1395         APPEND16(class);
1396
1397         return (int)j;
1398  overflow:
1399         return (-1);
1400 }
1401
1402 /* exported function */
1403 struct evdns_server_port *
1404 evdns_add_server_port(int socket, int is_tcp, evdns_request_callback_fn_type cb, void *user_data)
1405 {
1406         struct evdns_server_port *port;
1407         if (!(port = malloc(sizeof(struct evdns_server_port))))
1408                 return NULL;
1409         memset(port, 0, sizeof(struct evdns_server_port));
1410
1411         assert(!is_tcp); /* TCP sockets not yet implemented */
1412         port->socket = socket;
1413         port->refcnt = 1;
1414         port->choked = 0;
1415         port->closing = 0;
1416         port->user_callback = cb;
1417         port->user_data = user_data;
1418         port->pending_replies = NULL;
1419
1420         event_set(&port->event, port->socket, EV_READ | EV_PERSIST,
1421                           server_port_ready_callback, port);
1422         event_add(&port->event, NULL); /* check return. */
1423         return port;
1424 }
1425
1426 /* exported function */
1427 void
1428 evdns_close_server_port(struct evdns_server_port *port)
1429 {
1430         if (--port->refcnt == 0)
1431                 server_port_free(port);
1432         port->closing = 1;
1433 }
1434
1435 /* exported function */
1436 int
1437 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)
1438 {
1439         struct server_request *req = TO_SERVER_REQUEST(_req);
1440         struct server_reply_item **itemp, *item;
1441         int *countp;
1442
1443         if (req->response) /* have we already answered? */
1444                 return (-1);
1445
1446         switch (section) {
1447         case EVDNS_ANSWER_SECTION:
1448                 itemp = &req->answer;
1449                 countp = &req->n_answer;
1450                 break;
1451         case EVDNS_AUTHORITY_SECTION:
1452                 itemp = &req->authority;
1453                 countp = &req->n_authority;
1454                 break;
1455         case EVDNS_ADDITIONAL_SECTION:
1456                 itemp = &req->additional;
1457                 countp = &req->n_additional;
1458                 break;
1459         default:
1460                 return (-1);
1461         }
1462         while (*itemp) {
1463                 itemp = &((*itemp)->next);
1464         }
1465         item = malloc(sizeof(struct server_reply_item));
1466         if (!item)
1467                 return -1;
1468         item->next = NULL;
1469         if (!(item->name = strdup(name))) {
1470                 free(item);
1471                 return -1;
1472         }
1473         item->type = type;
1474         item->class = class;
1475         item->ttl = ttl;
1476         item->is_name = is_name != 0;
1477         item->datalen = 0;
1478         item->data = NULL;
1479         if (data) {
1480                 if (item->is_name) {
1481                         if (!(item->data = strdup(data))) {
1482                                 free(item->name);
1483                                 free(item);
1484                                 return -1;
1485                         }
1486                         item->datalen = (u16)-1;
1487                 } else {
1488                         if (!(item->data = malloc(datalen))) {
1489                                 free(item->name);
1490                                 free(item);
1491                                 return -1;
1492                         }
1493                         item->datalen = datalen;
1494                         memcpy(item->data, data, datalen);
1495                 }
1496         }
1497
1498         *itemp = item;
1499         ++(*countp);
1500         return 0;
1501 }
1502
1503 /* exported function */
1504 int
1505 evdns_server_request_add_a_reply(struct evdns_server_request *req, const char *name, int n, void *addrs, int ttl)
1506 {
1507         return evdns_server_request_add_reply(
1508                   req, EVDNS_ANSWER_SECTION, name, TYPE_A, CLASS_INET,
1509                   ttl, n*4, 0, addrs);
1510 }
1511
1512 /* exported function */
1513 int
1514 evdns_server_request_add_aaaa_reply(struct evdns_server_request *req, const char *name, int n, void *addrs, int ttl)
1515 {
1516         return evdns_server_request_add_reply(
1517                   req, EVDNS_ANSWER_SECTION, name, TYPE_AAAA, CLASS_INET,
1518                   ttl, n*16, 0, addrs);
1519 }
1520
1521 /* exported function */
1522 int
1523 evdns_server_request_add_ptr_reply(struct evdns_server_request *req, struct in_addr *in, const char *inaddr_name, const char *hostname, int ttl)
1524 {
1525         u32 a;
1526         char buf[32];
1527         assert(in || inaddr_name);
1528         assert(!(in && inaddr_name));
1529         if (in) {
1530                 a = ntohl(in->s_addr);
1531                 snprintf(buf, sizeof(buf), "%d.%d.%d.%d.in-addr.arpa",
1532                                 (int)(u8)((a    )&0xff),
1533                                 (int)(u8)((a>>8 )&0xff),
1534                                 (int)(u8)((a>>16)&0xff),
1535                                 (int)(u8)((a>>24)&0xff));
1536                 inaddr_name = buf;
1537         }
1538         return evdns_server_request_add_reply(
1539                   req, EVDNS_ANSWER_SECTION, inaddr_name, TYPE_PTR, CLASS_INET,
1540                   ttl, -1, 1, hostname);
1541 }
1542
1543 /* exported function */
1544 int
1545 evdns_server_request_add_cname_reply(struct evdns_server_request *req, const char *name, const char *cname, int ttl)
1546 {
1547         return evdns_server_request_add_reply(
1548                   req, EVDNS_ANSWER_SECTION, name, TYPE_A, CLASS_INET,
1549                   ttl, -1, 1, cname);
1550 }
1551
1552
1553 static int
1554 evdns_server_request_format_response(struct server_request *req, int err)
1555 {
1556         unsigned char buf[1500];
1557         size_t buf_len = sizeof(buf);
1558         off_t j = 0, r;
1559         u16 _t;
1560         u32 _t32;
1561         int i;
1562         u16 flags;
1563         struct dnslabel_table table;
1564
1565         if (err < 0 || err > 15) return -1;
1566
1567         /* Set response bit and error code; copy OPCODE and RD fields from
1568          * question; copy RA and AA if set by caller. */
1569         flags = req->base.flags;
1570         flags |= (0x8000 | err);
1571
1572         dnslabel_table_init(&table);
1573         APPEND16(req->trans_id);
1574         APPEND16(flags);
1575         APPEND16(req->base.nquestions);
1576         APPEND16(req->n_answer);
1577         APPEND16(req->n_authority);
1578         APPEND16(req->n_additional);
1579
1580         /* Add questions. */
1581         for (i=0; i < req->base.nquestions; ++i) {
1582                 const char *s = req->base.questions[i]->name;
1583                 j = dnsname_to_labels(buf, buf_len, j, s, strlen(s), &table);
1584                 if (j < 0) {
1585                         dnslabel_clear(&table);
1586                         return (int) j;
1587                 }
1588                 APPEND16(req->base.questions[i]->type);
1589                 APPEND16(req->base.questions[i]->class);
1590         }
1591
1592         /* Add answer, authority, and additional sections. */
1593         for (i=0; i<3; ++i) {
1594                 struct server_reply_item *item;
1595                 if (i==0)
1596                         item = req->answer;
1597                 else if (i==1)
1598                         item = req->authority;
1599                 else
1600                         item = req->additional;
1601                 while (item) {
1602                         r = dnsname_to_labels(buf, buf_len, j, item->name, strlen(item->name), &table);
1603                         if (r < 0)
1604                                 goto overflow;
1605                         j = r;
1606
1607                         APPEND16(item->type);
1608                         APPEND16(item->class);
1609                         APPEND32(item->ttl);
1610                         if (item->is_name) {
1611                                 off_t len_idx = j, name_start;
1612                                 j += 2;
1613                                 name_start = j;
1614                                 r = dnsname_to_labels(buf, buf_len, j, item->data, strlen(item->data), &table);
1615                                 if (r < 0)
1616                                         goto overflow;
1617                                 j = r;
1618                                 _t = htons( (j-name_start) );
1619                                 memcpy(buf+len_idx, &_t, 2);
1620                         } else {
1621                                 APPEND16(item->datalen);
1622                                 if (j+item->datalen > (off_t)buf_len)
1623                                         goto overflow;
1624                                 memcpy(buf+j, item->data, item->datalen);
1625                                 j += item->datalen;
1626                         }
1627                         item = item->next;
1628                 }
1629         }
1630
1631         if (j > 512) {
1632 overflow:
1633                 j = 512;
1634                 buf[3] |= 0x02; /* set the truncated bit. */
1635         }
1636
1637         req->response_len = j;
1638
1639         if (!(req->response = malloc(req->response_len))) {
1640                 server_request_free_answers(req);
1641                 dnslabel_clear(&table);
1642                 return (-1);
1643         }
1644         memcpy(req->response, buf, req->response_len);
1645         server_request_free_answers(req);
1646         dnslabel_clear(&table);
1647         return (0);
1648 }
1649
1650 /* exported function */
1651 int
1652 evdns_server_request_respond(struct evdns_server_request *_req, int err)
1653 {
1654         struct server_request *req = TO_SERVER_REQUEST(_req);
1655         struct evdns_server_port *port = req->port;
1656         int r;
1657         if (!req->response) {
1658                 if ((r = evdns_server_request_format_response(req, err))<0)
1659                         return r;
1660         }
1661
1662         r = sendto(port->socket, req->response, req->response_len, 0,
1663                            (struct sockaddr*) &req->addr, req->addrlen);
1664         if (r<0) {
1665                 int err = last_error(port->socket);
1666                 if (! error_is_eagain(err))
1667                         return -1;
1668
1669                 if (port->pending_replies) {
1670                         req->prev_pending = port->pending_replies->prev_pending;
1671                         req->next_pending = port->pending_replies;
1672                         req->prev_pending->next_pending =
1673                                 req->next_pending->prev_pending = req;
1674                 } else {
1675                         req->prev_pending = req->next_pending = req;
1676                         port->pending_replies = req;
1677                         port->choked = 1;
1678
1679                         (void) event_del(&port->event);
1680                         event_set(&port->event, port->socket, (port->closing?0:EV_READ) | EV_WRITE | EV_PERSIST, server_port_ready_callback, port);
1681
1682                         if (event_add(&port->event, NULL) < 0) {
1683                                 log(EVDNS_LOG_WARN, "Error from libevent when adding event for DNS server");
1684                         }
1685
1686                 }
1687
1688                 return 1;
1689         }
1690         if (server_request_free(req))
1691                 return 0;
1692
1693         if (port->pending_replies)
1694                 server_port_flush(port);
1695
1696         return 0;
1697 }
1698
1699 /* Free all storage held by RRs in req. */
1700 static void
1701 server_request_free_answers(struct server_request *req)
1702 {
1703         struct server_reply_item *victim, *next, **list;
1704         int i;
1705         for (i = 0; i < 3; ++i) {
1706                 if (i==0)
1707                         list = &req->answer;
1708                 else if (i==1)
1709                         list = &req->authority;
1710                 else
1711                         list = &req->additional;
1712
1713                 victim = *list;
1714                 while (victim) {
1715                         next = victim->next;
1716                         free(victim->name);
1717                         if (victim->data)
1718                                 free(victim->data);
1719                         free(victim);
1720                         victim = next;
1721                 }
1722                 *list = NULL;
1723         }
1724 }
1725
1726 /* Free all storage held by req, and remove links to it. */
1727 /* return true iff we just wound up freeing the server_port. */
1728 static int
1729 server_request_free(struct server_request *req)
1730 {
1731         int i, rc=1;
1732         if (req->base.questions) {
1733                 for (i = 0; i < req->base.nquestions; ++i)
1734                         free(req->base.questions[i]);
1735                 free(req->base.questions);
1736         }
1737
1738         if (req->port) {
1739                 if (req->port->pending_replies == req) {
1740                         if (req->next_pending)
1741                                 req->port->pending_replies = req->next_pending;
1742                         else
1743                                 req->port->pending_replies = NULL;
1744                 }
1745                 rc = --req->port->refcnt;
1746         }
1747
1748         if (req->response) {
1749                 free(req->response);
1750         }
1751
1752         server_request_free_answers(req);
1753
1754         if (req->next_pending && req->next_pending != req) {
1755                 req->next_pending->prev_pending = req->prev_pending;
1756                 req->prev_pending->next_pending = req->next_pending;
1757         }
1758
1759         if (rc == 0) {
1760                 server_port_free(req->port);
1761                 free(req);
1762                 return (1);
1763         }
1764         free(req);
1765         return (0);
1766 }
1767
1768 /* Free all storage held by an evdns_server_port.  Only called when  */
1769 static void
1770 server_port_free(struct evdns_server_port *port)
1771 {
1772         assert(port);
1773         assert(!port->refcnt);
1774         assert(!port->pending_replies);
1775         if (port->socket > 0) {
1776                 CLOSE_SOCKET(port->socket);
1777                 port->socket = -1;
1778         }
1779         (void) event_del(&port->event);
1780         /* XXXX actually free the port? -NM */
1781 }
1782
1783 /* exported function */
1784 int
1785 evdns_server_request_drop(struct evdns_server_request *_req)
1786 {
1787         struct server_request *req = TO_SERVER_REQUEST(_req);
1788         server_request_free(req);
1789         return 0;
1790 }
1791
1792 /* exported function */
1793 int
1794 evdns_server_request_get_requesting_addr(struct evdns_server_request *_req, struct sockaddr *sa, int addr_len)
1795 {
1796         struct server_request *req = TO_SERVER_REQUEST(_req);
1797         if (addr_len < (int)req->addrlen)
1798                 return -1;
1799         memcpy(sa, &(req->addr), req->addrlen);
1800         return req->addrlen;
1801 }
1802
1803 #undef APPEND16
1804 #undef APPEND32
1805
1806 /* this is a libevent callback function which is called when a request */
1807 /* has timed out. */
1808 static void
1809 evdns_request_timeout_callback(int fd, short events, void *arg) {
1810         struct request *const req = (struct request *) arg;
1811         (void) fd;
1812         (void) events;
1813
1814         log(EVDNS_LOG_DEBUG, "Request %lx timed out", (unsigned long) arg);
1815
1816         req->ns->timedout++;
1817         if (req->ns->timedout > global_max_nameserver_timeout) {
1818                 req->ns->timedout = 0;
1819                 nameserver_failed(req->ns, "request timed out.");
1820         }
1821
1822         (void) evtimer_del(&req->timeout_event);
1823         if (req->tx_count >= global_max_retransmits) {
1824                 /* this request has failed */
1825                 reply_callback(req, 0, DNS_ERR_TIMEOUT, NULL);
1826                 request_finished(req, &req_head);
1827         } else {
1828                 /* retransmit it */
1829                 evdns_request_transmit(req);
1830         }
1831 }
1832
1833 /* try to send a request to a given server. */
1834 /* */
1835 /* return: */
1836 /*   0 ok */
1837 /*   1 temporary failure */
1838 /*   2 other failure */
1839 static int
1840 evdns_request_transmit_to(struct request *req, struct nameserver *server) {
1841         const int r = send(server->socket, req->request, req->request_len, 0);
1842         if (r < 0) {
1843                 int err = last_error(server->socket);
1844                 if (error_is_eagain(err)) return 1;
1845                 nameserver_failed(req->ns, strerror(err));
1846                 return 2;
1847         } else if (r != (int)req->request_len) {
1848                 return 1;  /* short write */
1849         } else {
1850                 return 0;
1851         }
1852 }
1853
1854 /* try to send a request, updating the fields of the request */
1855 /* as needed */
1856 /* */
1857 /* return: */
1858 /*   0 ok */
1859 /*   1 failed */
1860 static int
1861 evdns_request_transmit(struct request *req) {
1862         int retcode = 0, r;
1863
1864         /* if we fail to send this packet then this flag marks it */
1865         /* for evdns_transmit */
1866         req->transmit_me = 1;
1867         if (req->trans_id == 0xffff) abort();
1868
1869         if (req->ns->choked) {
1870                 /* don't bother trying to write to a socket */
1871                 /* which we have had EAGAIN from */
1872                 return 1;
1873         }
1874
1875         r = evdns_request_transmit_to(req, req->ns);
1876         switch (r) {
1877         case 1:
1878                 /* temp failure */
1879                 req->ns->choked = 1;
1880                 nameserver_write_waiting(req->ns, 1);
1881                 return 1;
1882         case 2:
1883                 /* failed in some other way */
1884                 retcode = 1;
1885                 /* fall through */
1886         default:
1887                 /* all ok */
1888                 log(EVDNS_LOG_DEBUG,
1889                     "Setting timeout for request %lx", (unsigned long) req);
1890                 evtimer_set(&req->timeout_event, evdns_request_timeout_callback, req);
1891                 if (evtimer_add(&req->timeout_event, &global_timeout) < 0) {
1892                   log(EVDNS_LOG_WARN,
1893                       "Error from libevent when adding timer for request %lx",
1894                       (unsigned long) req);
1895                   /* ???? Do more? */
1896                 }
1897                 req->tx_count++;
1898                 req->transmit_me = 0;
1899                 return retcode;
1900         }
1901 }
1902
1903 static void
1904 nameserver_probe_callback(int result, char type, int count, int ttl, void *addresses, void *arg) {
1905         struct nameserver *const ns = (struct nameserver *) arg;
1906         (void) type;
1907         (void) count;
1908         (void) ttl;
1909         (void) addresses;
1910
1911         if (result == DNS_ERR_NONE || result == DNS_ERR_NOTEXIST) {
1912                 /* this is a good reply */
1913                 nameserver_up(ns);
1914         } else nameserver_probe_failed(ns);
1915 }
1916
1917 static void
1918 nameserver_send_probe(struct nameserver *const ns) {
1919         struct request *req;
1920         /* here we need to send a probe to a given nameserver */
1921         /* in the hope that it is up now. */
1922
1923         log(EVDNS_LOG_DEBUG, "Sending probe to %s", debug_ntoa(ns->address));
1924
1925         req = request_new(TYPE_A, "www.google.com", DNS_QUERY_NO_SEARCH, nameserver_probe_callback, ns);
1926         if (!req) return;
1927         /* we force this into the inflight queue no matter what */
1928         request_trans_id_set(req, transaction_id_pick());
1929         req->ns = ns;
1930         request_submit(req);
1931 }
1932
1933 /* returns: */
1934 /*   0 didn't try to transmit anything */
1935 /*   1 tried to transmit something */
1936 static int
1937 evdns_transmit(void) {
1938         char did_try_to_transmit = 0;
1939
1940         if (req_head) {
1941                 struct request *const started_at = req_head, *req = req_head;
1942                 /* first transmit all the requests which are currently waiting */
1943                 do {
1944                         if (req->transmit_me) {
1945                                 did_try_to_transmit = 1;
1946                                 evdns_request_transmit(req);
1947                         }
1948
1949                         req = req->next;
1950                 } while (req != started_at);
1951         }
1952
1953         return did_try_to_transmit;
1954 }
1955
1956 /* exported function */
1957 int
1958 evdns_count_nameservers(void)
1959 {
1960         const struct nameserver *server = server_head;
1961         int n = 0;
1962         if (!server)
1963                 return 0;
1964         do {
1965                 ++n;
1966                 server = server->next;
1967         } while (server != server_head);
1968         return n;
1969 }
1970
1971 /* exported function */
1972 int
1973 evdns_clear_nameservers_and_suspend(void)
1974 {
1975         struct nameserver *server = server_head, *started_at = server_head;
1976         struct request *req = req_head, *req_started_at = req_head;
1977
1978         if (!server)
1979                 return 0;
1980         while (1) {
1981                 struct nameserver *next = server->next;
1982                 (void) event_del(&server->event);
1983                 (void) evtimer_del(&server->timeout_event);
1984                 if (server->socket >= 0)
1985                         CLOSE_SOCKET(server->socket);
1986                 free(server);
1987                 if (next == started_at)
1988                         break;
1989                 server = next;
1990         }
1991         server_head = NULL;
1992         global_good_nameservers = 0;
1993
1994         while (req) {
1995                 struct request *next = req->next;
1996                 req->tx_count = req->reissue_count = 0;
1997                 req->ns = NULL;
1998                 /* ???? What to do about searches? */
1999                 (void) evtimer_del(&req->timeout_event);
2000                 req->trans_id = 0;
2001                 req->transmit_me = 0;
2002
2003                 global_requests_waiting++;
2004                 evdns_request_insert(req, &req_waiting_head);
2005                 /* We want to insert these suspended elements at the front of
2006                  * the waiting queue, since they were pending before any of
2007                  * the waiting entries were added.  This is a circular list,
2008                  * so we can just shift the start back by one.*/
2009                 req_waiting_head = req_waiting_head->prev;
2010
2011                 if (next == req_started_at)
2012                         break;
2013                 req = next;
2014         }
2015         req_head = NULL;
2016         global_requests_inflight = 0;
2017
2018         return 0;
2019 }
2020
2021
2022 /* exported function */
2023 int
2024 evdns_resume(void)
2025 {
2026         evdns_requests_pump_waiting_queue();
2027         return 0;
2028 }
2029
2030 static int
2031 _evdns_nameserver_add_impl(unsigned long int address, int port) {
2032         /* first check to see if we already have this nameserver */
2033
2034         const struct nameserver *server = server_head, *const started_at = server_head;
2035         struct nameserver *ns;
2036         struct sockaddr_in sin;
2037         int err = 0;
2038         if (server) {
2039                 do {
2040                         if (server->address == address) return 3;
2041                         server = server->next;
2042                 } while (server != started_at);
2043         }
2044
2045         ns = (struct nameserver *) malloc(sizeof(struct nameserver));
2046         if (!ns) return -1;
2047
2048         memset(ns, 0, sizeof(struct nameserver));
2049
2050         ns->socket = socket(PF_INET, SOCK_DGRAM, 0);
2051         if (ns->socket < 0) { err = 1; goto out1; }
2052 #ifdef WIN32
2053         {
2054                 u_long nonblocking = 1;
2055                 ioctlsocket(ns->socket, FIONBIO, &nonblocking);
2056         }
2057 #else
2058         fcntl(ns->socket, F_SETFL, O_NONBLOCK);
2059 #endif
2060         sin.sin_addr.s_addr = address;
2061         sin.sin_port = htons(port);
2062         sin.sin_family = AF_INET;
2063         if (connect(ns->socket, (struct sockaddr *) &sin, sizeof(sin)) != 0) {
2064                 err = 2;
2065                 goto out2;
2066         }
2067
2068         ns->address = address;
2069         ns->state = 1;
2070         event_set(&ns->event, ns->socket, EV_READ | EV_PERSIST, nameserver_ready_callback, ns);
2071         if (event_add(&ns->event, NULL) < 0) {
2072           err = 2;
2073           goto out2;
2074         }
2075
2076         log(EVDNS_LOG_DEBUG, "Added nameserver %s", debug_ntoa(address));
2077
2078         /* insert this nameserver into the list of them */
2079         if (!server_head) {
2080                 ns->next = ns->prev = ns;
2081                 server_head = ns;
2082         } else {
2083                 ns->next = server_head->next;
2084                 ns->prev = server_head;
2085                 server_head->next = ns;
2086                 if (server_head->prev == server_head) {
2087                         server_head->prev = ns;
2088                 }
2089         }
2090
2091         global_good_nameservers++;
2092
2093         return 0;
2094
2095 out2:
2096         CLOSE_SOCKET(ns->socket);
2097 out1:
2098         free(ns);
2099         log(EVDNS_LOG_WARN, "Unable to add nameserver %s: error %d", debug_ntoa(address), err);
2100         return err;
2101 }
2102
2103 /* exported function */
2104 int
2105 evdns_nameserver_add(unsigned long int address) {
2106         return _evdns_nameserver_add_impl(address, 53);
2107 }
2108
2109 /* exported function */
2110 int
2111 evdns_nameserver_ip_add(const char *ip_as_string) {
2112         struct in_addr ina;
2113         int port;
2114         char buf[20];
2115         const char *cp;
2116         cp = strchr(ip_as_string, ':');
2117         if (! cp) {
2118                 cp = ip_as_string;
2119                 port = 53;
2120         } else {
2121                 port = strtoint(cp+1);
2122                 if (port < 0 || port > 65535) {
2123                         return 4;
2124                 }
2125                 if ((cp-ip_as_string) >= (int)sizeof(buf)) {
2126                         return 4;
2127                 }
2128                 memcpy(buf, ip_as_string, cp-ip_as_string);
2129                 buf[cp-ip_as_string] = '\0';
2130                 cp = buf;
2131         }
2132         if (!inet_aton(cp, &ina)) {
2133                 return 4;
2134         }
2135         return _evdns_nameserver_add_impl(ina.s_addr, port);
2136 }
2137
2138 /* insert into the tail of the queue */
2139 static void
2140 evdns_request_insert(struct request *req, struct request **head) {
2141         if (!*head) {
2142                 *head = req;
2143                 req->next = req->prev = req;
2144                 return;
2145         }
2146
2147         req->prev = (*head)->prev;
2148         req->prev->next = req;
2149         req->next = *head;
2150         (*head)->prev = req;
2151 }
2152
2153 static int
2154 string_num_dots(const char *s) {
2155         int count = 0;
2156         while ((s = strchr(s, '.'))) {
2157                 s++;
2158                 count++;
2159         }
2160         return count;
2161 }
2162
2163 static struct request *
2164 request_new(int type, const char *name, int flags,
2165     evdns_callback_type callback, void *user_ptr) {
2166         const char issuing_now =
2167             (global_requests_inflight < global_max_requests_inflight) ? 1 : 0;
2168
2169         const int name_len = strlen(name);
2170         const int request_max_len = evdns_request_len(name_len);
2171         const u16 trans_id = issuing_now ? transaction_id_pick() : 0xffff;
2172         /* the request data is alloced in a single block with the header */
2173         struct request *const req =
2174             (struct request *) malloc(sizeof(struct request) + request_max_len);
2175         int rlen;
2176         (void) flags;
2177
2178         if (!req) return NULL;
2179         memset(req, 0, sizeof(struct request));
2180
2181         /* request data lives just after the header */
2182         req->request = ((u8 *) req) + sizeof(struct request);
2183         /* denotes that the request data shouldn't be free()ed */
2184         req->request_appended = 1;
2185         rlen = evdns_request_data_build(name, name_len, trans_id,
2186             type, CLASS_INET, req->request, request_max_len);
2187         if (rlen < 0)
2188                 goto err1;
2189         req->request_len = rlen;
2190         req->trans_id = trans_id;
2191         req->tx_count = 0;
2192         req->request_type = type;
2193         req->user_pointer = user_ptr;
2194         req->user_callback = callback;
2195         req->ns = issuing_now ? nameserver_pick() : NULL;
2196         req->next = req->prev = NULL;
2197
2198         return req;
2199 err1:
2200         free(req);
2201         return NULL;
2202 }
2203
2204 static void
2205 request_submit(struct request *const req) {
2206         if (req->ns) {
2207                 /* if it has a nameserver assigned then this is going */
2208                 /* straight into the inflight queue */
2209                 evdns_request_insert(req, &req_head);
2210                 global_requests_inflight++;
2211                 evdns_request_transmit(req);
2212         } else {
2213                 evdns_request_insert(req, &req_waiting_head);
2214                 global_requests_waiting++;
2215         }
2216 }
2217
2218 /* exported function */
2219 int evdns_resolve_ipv4(const char *name, int flags,
2220     evdns_callback_type callback, void *ptr) {
2221         log(EVDNS_LOG_DEBUG, "Resolve requested for %s", name);
2222         if (flags & DNS_QUERY_NO_SEARCH) {
2223                 struct request *const req =
2224                         request_new(TYPE_A, name, flags, callback, ptr);
2225                 if (req == NULL)
2226                         return (1);
2227                 request_submit(req);
2228                 return (0);
2229         } else {
2230                 return (search_request_new(TYPE_A, name, flags, callback, ptr));
2231         }
2232 }
2233
2234 /* exported function */
2235 int evdns_resolve_ipv6(const char *name, int flags,
2236                                            evdns_callback_type callback, void *ptr) {
2237         log(EVDNS_LOG_DEBUG, "Resolve requested for %s", name);
2238         if (flags & DNS_QUERY_NO_SEARCH) {
2239                 struct request *const req =
2240                         request_new(TYPE_AAAA, name, flags, callback, ptr);
2241                 if (req == NULL)
2242                         return (1);
2243                 request_submit(req);
2244                 return (0);
2245         } else {
2246                 return (search_request_new(TYPE_AAAA, name, flags, callback, ptr));
2247         }
2248 }
2249
2250 int evdns_resolve_reverse(struct in_addr *in, int flags, evdns_callback_type callback, void *ptr) {
2251         char buf[32];
2252         struct request *req;
2253         u32 a;
2254         assert(in);
2255         a = ntohl(in->s_addr);
2256         snprintf(buf, sizeof(buf), "%d.%d.%d.%d.in-addr.arpa",
2257                         (int)(u8)((a    )&0xff),
2258                         (int)(u8)((a>>8 )&0xff),
2259                         (int)(u8)((a>>16)&0xff),
2260                         (int)(u8)((a>>24)&0xff));
2261         log(EVDNS_LOG_DEBUG, "Resolve requested for %s (reverse)", buf);
2262         req = request_new(TYPE_PTR, buf, flags, callback, ptr);
2263         if (!req) return 1;
2264         request_submit(req);
2265         return 0;
2266 }
2267
2268 int evdns_resolve_reverse_ipv6(struct in6_addr *in, int flags, evdns_callback_type callback, void *ptr) {
2269         char buf[96];
2270         char *cp;
2271         struct request *req;
2272         int i;
2273         assert(in);
2274         cp = buf;
2275         for (i=15; i >= 0; --i) {
2276                 u8 byte = in->s6_addr[i];
2277                 *cp++ = "0123456789abcdef"[byte & 0x0f];
2278                 *cp++ = '.';
2279                 *cp++ = "0123456789abcdef"[byte >> 4];
2280                 *cp++ = '.';
2281         }
2282         assert(cp + strlen(".ip6.arpa") < buf+sizeof(buf));
2283         memcpy(cp, ".ip6.arpa", strlen(".ip6.arpa")+1);
2284         log(EVDNS_LOG_DEBUG, "Resolve requested for %s (reverse)", buf);
2285         req = request_new(TYPE_PTR, buf, flags, callback, ptr);
2286         if (!req) return 1;
2287         request_submit(req);
2288         return 0;
2289 }
2290
2291 /*/////////////////////////////////////////////////////////////////// */
2292 /* Search support */
2293 /* */
2294 /* the libc resolver has support for searching a number of domains */
2295 /* to find a name. If nothing else then it takes the single domain */
2296 /* from the gethostname() call. */
2297 /* */
2298 /* It can also be configured via the domain and search options in a */
2299 /* resolv.conf. */
2300 /* */
2301 /* The ndots option controls how many dots it takes for the resolver */
2302 /* to decide that a name is non-local and so try a raw lookup first. */
2303
2304 struct search_domain {
2305         int len;
2306         struct search_domain *next;
2307         /* the text string is appended to this structure */
2308 };
2309
2310 struct search_state {
2311         int refcount;
2312         int ndots;
2313         int num_domains;
2314         struct search_domain *head;
2315 };
2316
2317 static struct search_state *global_search_state = NULL;
2318
2319 static void
2320 search_state_decref(struct search_state *const state) {
2321         if (!state) return;
2322         state->refcount--;
2323         if (!state->refcount) {
2324                 struct search_domain *next, *dom;
2325                 for (dom = state->head; dom; dom = next) {
2326                         next = dom->next;
2327                         free(dom);
2328                 }
2329                 free(state);
2330         }
2331 }
2332
2333 static struct search_state *
2334 search_state_new(void) {
2335         struct search_state *state = (struct search_state *) malloc(sizeof(struct search_state));
2336         if (!state) return NULL;
2337         memset(state, 0, sizeof(struct search_state));
2338         state->refcount = 1;
2339         state->ndots = 1;
2340
2341         return state;
2342 }
2343
2344 static void
2345 search_postfix_clear(void) {
2346         search_state_decref(global_search_state);
2347
2348         global_search_state = search_state_new();
2349 }
2350
2351 /* exported function */
2352 void
2353 evdns_search_clear(void) {
2354         search_postfix_clear();
2355 }
2356
2357 static void
2358 search_postfix_add(const char *domain) {
2359         int domain_len;
2360         struct search_domain *sdomain;
2361         while (domain[0] == '.') domain++;
2362         domain_len = strlen(domain);
2363
2364         if (!global_search_state) global_search_state = search_state_new();
2365         if (!global_search_state) return;
2366         global_search_state->num_domains++;
2367
2368         sdomain = (struct search_domain *) malloc(sizeof(struct search_domain) + domain_len);
2369         if (!sdomain) return;
2370         memcpy( ((u8 *) sdomain) + sizeof(struct search_domain), domain, domain_len);
2371         sdomain->next = global_search_state->head;
2372         sdomain->len = domain_len;
2373
2374         global_search_state->head = sdomain;
2375 }
2376
2377 /* reverse the order of members in the postfix list. This is needed because, */
2378 /* when parsing resolv.conf we push elements in the wrong order */
2379 static void
2380 search_reverse(void) {
2381         struct search_domain *cur, *prev = NULL, *next;
2382         cur = global_search_state->head;
2383         while (cur) {
2384                 next = cur->next;
2385                 cur->next = prev;
2386                 prev = cur;
2387                 cur = next;
2388         }
2389
2390         global_search_state->head = prev;
2391 }
2392
2393 /* exported function */
2394 void
2395 evdns_search_add(const char *domain) {
2396         search_postfix_add(domain);
2397 }
2398
2399 /* exported function */
2400 void
2401 evdns_search_ndots_set(const int ndots) {
2402         if (!global_search_state) global_search_state = search_state_new();
2403         if (!global_search_state) return;
2404         global_search_state->ndots = ndots;
2405 }
2406
2407 static void
2408 search_set_from_hostname(void) {
2409         char hostname[HOST_NAME_MAX + 1], *domainname;
2410
2411         search_postfix_clear();
2412         if (gethostname(hostname, sizeof(hostname))) return;
2413         domainname = strchr(hostname, '.');
2414         if (!domainname) return;
2415         search_postfix_add(domainname);
2416 }
2417
2418 /* warning: returns malloced string */
2419 static char *
2420 search_make_new(const struct search_state *const state, int n, const char *const base_name) {
2421         const int base_len = strlen(base_name);
2422         const char need_to_append_dot = base_name[base_len - 1] == '.' ? 0 : 1;
2423         struct search_domain *dom;
2424
2425         for (dom = state->head; dom; dom = dom->next) {
2426                 if (!n--) {
2427                         /* this is the postfix we want */
2428                         /* the actual postfix string is kept at the end of the structure */
2429                         const u8 *const postfix = ((u8 *) dom) + sizeof(struct search_domain);
2430                         const int postfix_len = dom->len;
2431                         char *const newname = (char *) malloc(base_len + need_to_append_dot + postfix_len + 1);
2432                         if (!newname) return NULL;
2433                         memcpy(newname, base_name, base_len);
2434                         if (need_to_append_dot) newname[base_len] = '.';
2435                         memcpy(newname + base_len + need_to_append_dot, postfix, postfix_len);
2436                         newname[base_len + need_to_append_dot + postfix_len] = 0;
2437                         return newname;
2438                 }
2439         }
2440
2441         /* we ran off the end of the list and still didn't find the requested string */
2442         abort();
2443         return NULL; /* unreachable; stops warnings in some compilers. */
2444 }
2445
2446 static int
2447 search_request_new(int type, const char *const name, int flags, evdns_callback_type user_callback, void *user_arg) {
2448         assert(type == TYPE_A || type == TYPE_AAAA);
2449         if ( ((flags & DNS_QUERY_NO_SEARCH) == 0) &&
2450              global_search_state &&
2451                  global_search_state->num_domains) {
2452                 /* we have some domains to search */
2453                 struct request *req;
2454                 if (string_num_dots(name) >= global_search_state->ndots) {
2455                         req = request_new(type, name, flags, user_callback, user_arg);
2456                         if (!req) return 1;
2457                         req->search_index = -1;
2458                 } else {
2459                         char *const new_name = search_make_new(global_search_state, 0, name);
2460                         if (!new_name) return 1;
2461                         req = request_new(type, new_name, flags, user_callback, user_arg);
2462                         free(new_name);
2463                         if (!req) return 1;
2464                         req->search_index = 0;
2465                 }
2466                 req->search_origname = strdup(name);
2467                 req->search_state = global_search_state;
2468                 req->search_flags = flags;
2469                 global_search_state->refcount++;
2470                 request_submit(req);
2471                 return 0;
2472         } else {
2473                 struct request *const req = request_new(type, name, flags, user_callback, user_arg);
2474                 if (!req) return 1;
2475                 request_submit(req);
2476                 return 0;
2477         }
2478 }
2479
2480 /* this is called when a request has failed to find a name. We need to check */
2481 /* if it is part of a search and, if so, try the next name in the list */
2482 /* returns: */
2483 /*   0 another request has been submitted */
2484 /*   1 no more requests needed */
2485 static int
2486 search_try_next(struct request *const req) {
2487         if (req->search_state) {
2488                 /* it is part of a search */
2489                 char *new_name;
2490                 struct request *newreq;
2491                 req->search_index++;
2492                 if (req->search_index >= req->search_state->num_domains) {
2493                         /* no more postfixes to try, however we may need to try */
2494                         /* this name without a postfix */
2495                         if (string_num_dots(req->search_origname) < req->search_state->ndots) {
2496                                 /* yep, we need to try it raw */
2497                                 struct request *const newreq = request_new(req->request_type, req->search_origname, req->search_flags, req->user_callback, req->user_pointer);
2498                                 log(EVDNS_LOG_DEBUG, "Search: trying raw query %s", req->search_origname);
2499                                 if (newreq) {
2500                                         request_submit(newreq);
2501                                         return 0;
2502                                 }
2503                         }
2504                         return 1;
2505                 }
2506
2507                 new_name = search_make_new(req->search_state, req->search_index, req->search_origname);
2508                 if (!new_name) return 1;
2509                 log(EVDNS_LOG_DEBUG, "Search: now trying %s (%d)", new_name, req->search_index);
2510                 newreq = request_new(req->request_type, new_name, req->search_flags, req->user_callback, req->user_pointer);
2511                 free(new_name);
2512                 if (!newreq) return 1;
2513                 newreq->search_origname = req->search_origname;
2514                 req->search_origname = NULL;
2515                 newreq->search_state = req->search_state;
2516                 newreq->search_flags = req->search_flags;
2517                 newreq->search_index = req->search_index;
2518                 newreq->search_state->refcount++;
2519                 request_submit(newreq);
2520                 return 0;
2521         }
2522         return 1;
2523 }
2524
2525 static void
2526 search_request_finished(struct request *const req) {
2527         if (req->search_state) {
2528                 search_state_decref(req->search_state);
2529                 req->search_state = NULL;
2530         }
2531         if (req->search_origname) {
2532                 free(req->search_origname);
2533                 req->search_origname = NULL;
2534         }
2535 }
2536
2537 /*/////////////////////////////////////////////////////////////////// */
2538 /* Parsing resolv.conf files */
2539
2540 static void
2541 evdns_resolv_set_defaults(int flags) {
2542         /* if the file isn't found then we assume a local resolver */
2543         if (flags & DNS_OPTION_SEARCH) search_set_from_hostname();
2544         if (flags & DNS_OPTION_NAMESERVERS) evdns_nameserver_ip_add("127.0.0.1");
2545 }
2546
2547 #ifndef HAVE_STRTOK_R
2548 static char *
2549 fake_strtok_r(char *s, const char *delim, char **state) {
2550         return strtok(s, delim);
2551 }
2552 #endif
2553
2554 /* helper version of atoi which returns -1 on error */
2555 static int
2556 strtoint(const char *const str) {
2557         char *endptr;
2558         const int r = strtol(str, &endptr, 10);
2559         if (*endptr) return -1;
2560         return r;
2561 }
2562
2563 /* helper version of atoi that returns -1 on error and clips to bounds. */
2564 static int
2565 strtoint_clipped(const char *const str, int min, int max)
2566 {
2567         int r = strtoint(str);
2568         if (r == -1)
2569                 return r;
2570         else if (r<min)
2571                 return min;
2572         else if (r>max)
2573                 return max;
2574         else
2575                 return r;
2576 }
2577
2578 /* exported function */
2579 int
2580 evdns_set_option(const char *option, const char *val, int flags)
2581 {
2582         if (!strncmp(option, "ndots:", 6)) {
2583                 const int ndots = strtoint(val);
2584                 if (ndots == -1) return -1;
2585                 if (!(flags & DNS_OPTION_SEARCH)) return 0;
2586                 log(EVDNS_LOG_DEBUG, "Setting ndots to %d", ndots);
2587                 if (!global_search_state) global_search_state = search_state_new();
2588                 if (!global_search_state) return -1;
2589                 global_search_state->ndots = ndots;
2590         } else if (!strncmp(option, "timeout:", 8)) {
2591                 const int timeout = strtoint(val);
2592                 if (timeout == -1) return -1;
2593                 if (!(flags & DNS_OPTION_MISC)) return 0;
2594                 log(EVDNS_LOG_DEBUG, "Setting timeout to %d", timeout);
2595                 global_timeout.tv_sec = timeout;
2596         } else if (!strncmp(option, "max-timeouts:", 12)) {
2597                 const int maxtimeout = strtoint_clipped(val, 1, 255);
2598                 if (maxtimeout == -1) return -1;
2599                 if (!(flags & DNS_OPTION_MISC)) return 0;
2600                 log(EVDNS_LOG_DEBUG, "Setting maximum allowed timeouts to %d",
2601                         maxtimeout);
2602                 global_max_nameserver_timeout = maxtimeout;
2603         } else if (!strncmp(option, "max-inflight:", 13)) {
2604                 const int maxinflight = strtoint_clipped(val, 1, 65000);
2605                 if (maxinflight == -1) return -1;
2606                 if (!(flags & DNS_OPTION_MISC)) return 0;
2607                 log(EVDNS_LOG_DEBUG, "Setting maximum inflight requests to %d",
2608                         maxinflight);
2609                 global_max_requests_inflight = maxinflight;
2610         } else if (!strncmp(option, "attempts:", 9)) {
2611                 int retries = strtoint(val);
2612                 if (retries == -1) return -1;
2613                 if (retries > 255) retries = 255;
2614                 if (!(flags & DNS_OPTION_MISC)) return 0;
2615                 log(EVDNS_LOG_DEBUG, "Setting retries to %d", retries);
2616                 global_max_retransmits = retries;
2617         }
2618         return 0;
2619 }
2620
2621 static void
2622 resolv_conf_parse_line(char *const start, int flags) {
2623         char *strtok_state;
2624         static const char *const delims = " \t";
2625 #define NEXT_TOKEN fake_strtok_r(NULL, delims, &strtok_state)
2626
2627         char *const first_token = fake_strtok_r(start, delims, &strtok_state);
2628         if (!first_token) return;
2629
2630         if (!strcmp(first_token, "nameserver") && (flags & DNS_OPTION_NAMESERVERS)) {
2631                 const char *const nameserver = NEXT_TOKEN;
2632                 struct in_addr ina;
2633
2634                 if (inet_aton(nameserver, &ina)) {
2635                         /* address is valid */
2636                         evdns_nameserver_add(ina.s_addr);
2637                 }
2638         } else if (!strcmp(first_token, "domain") && (flags & DNS_OPTION_SEARCH)) {
2639                 const char *const domain = NEXT_TOKEN;
2640                 if (domain) {
2641                         search_postfix_clear();
2642                         search_postfix_add(domain);
2643                 }
2644         } else if (!strcmp(first_token, "search") && (flags & DNS_OPTION_SEARCH)) {
2645                 const char *domain;
2646                 search_postfix_clear();
2647
2648                 while ((domain = NEXT_TOKEN)) {
2649                         search_postfix_add(domain);
2650                 }
2651                 search_reverse();
2652         } else if (!strcmp(first_token, "options")) {
2653                 const char *option;
2654                 while ((option = NEXT_TOKEN)) {
2655                         const char *val = strchr(option, ':');
2656                         evdns_set_option(option, val ? val+1 : "", flags);
2657                 }
2658         }
2659 #undef NEXT_TOKEN
2660 }
2661
2662 /* exported function */
2663 /* returns: */
2664 /*   0 no errors */
2665 /*   1 failed to open file */
2666 /*   2 failed to stat file */
2667 /*   3 file too large */
2668 /*   4 out of memory */
2669 /*   5 short read from file */
2670 int
2671 evdns_resolv_conf_parse(int flags, const char *const filename) {
2672         struct stat st;
2673         int fd, n, r;
2674         u8 *resolv;
2675         char *start;
2676         int err = 0;
2677
2678         log(EVDNS_LOG_DEBUG, "Parsing resolv.conf file %s", filename);
2679
2680         fd = open(filename, O_RDONLY);
2681         if (fd < 0) {
2682                 evdns_resolv_set_defaults(flags);
2683                 return 1;
2684         }
2685
2686         if (fstat(fd, &st)) { err = 2; goto out1; }
2687         if (!st.st_size) {
2688                 evdns_resolv_set_defaults(flags);
2689                 err = (flags & DNS_OPTION_NAMESERVERS) ? 6 : 0;
2690                 goto out1;
2691         }
2692         if (st.st_size > 65535) { err = 3; goto out1; }  /* no resolv.conf should be any bigger */
2693
2694         resolv = (u8 *) malloc((size_t)st.st_size + 1);
2695         if (!resolv) { err = 4; goto out1; }
2696
2697         n = 0;
2698         while ((r = read(fd, resolv+n, (size_t)st.st_size-n)) > 0) {
2699                 n += r;
2700                 if (n == st.st_size)
2701                         break;
2702                 assert(n < st.st_size);
2703         }
2704         if (r < 0) { err = 5; goto out2; }
2705         resolv[n] = 0;   /* we malloced an extra byte; this should be fine. */
2706
2707         start = (char *) resolv;
2708         for (;;) {
2709                 char *const newline = strchr(start, '\n');
2710                 if (!newline) {
2711                         resolv_conf_parse_line(start, flags);
2712                         break;
2713                 } else {
2714                         *newline = 0;
2715                         resolv_conf_parse_line(start, flags);
2716                         start = newline + 1;
2717                 }
2718         }
2719
2720         if (!server_head && (flags & DNS_OPTION_NAMESERVERS)) {
2721                 /* no nameservers were configured. */
2722                 evdns_nameserver_ip_add("127.0.0.1");
2723                 err = 6;
2724         }
2725         if (flags & DNS_OPTION_SEARCH && (!global_search_state || global_search_state->num_domains == 0)) {
2726                 search_set_from_hostname();
2727         }
2728
2729 out2:
2730         free(resolv);
2731 out1:
2732         close(fd);
2733         return err;
2734 }
2735
2736 #ifdef WIN32
2737 /* Add multiple nameservers from a space-or-comma-separated list. */
2738 static int
2739 evdns_nameserver_ip_add_line(const char *ips) {
2740         const char *addr;
2741         char *buf;
2742         int r;
2743         while (*ips) {
2744                 while (ISSPACE(*ips) || *ips == ',' || *ips == '\t')
2745                         ++ips;
2746                 addr = ips;
2747                 while (ISDIGIT(*ips) || *ips == '.' || *ips == ':')
2748                         ++ips;
2749                 buf = malloc(ips-addr+1);
2750                 if (!buf) return 4;
2751                 memcpy(buf, addr, ips-addr);
2752                 buf[ips-addr] = '\0';
2753                 r = evdns_nameserver_ip_add(buf);
2754                 free(buf);
2755                 if (r) return r;
2756         }
2757         return 0;
2758 }
2759
2760 typedef DWORD(WINAPI *GetNetworkParams_fn_t)(FIXED_INFO *, DWORD*);
2761
2762 /* Use the windows GetNetworkParams interface in iphlpapi.dll to */
2763 /* figure out what our nameservers are. */
2764 static int
2765 load_nameservers_with_getnetworkparams(void)
2766 {
2767         /* Based on MSDN examples and inspection of  c-ares code. */
2768         FIXED_INFO *fixed;
2769         HMODULE handle = 0;
2770         ULONG size = sizeof(FIXED_INFO);
2771         void *buf = NULL;
2772         int status = 0, r, added_any;
2773         IP_ADDR_STRING *ns;
2774         GetNetworkParams_fn_t fn;
2775
2776         if (!(handle = LoadLibrary("iphlpapi.dll"))) {
2777                 log(EVDNS_LOG_WARN, "Could not open iphlpapi.dll");
2778                 status = -1;
2779                 goto done;
2780         }
2781         if (!(fn = (GetNetworkParams_fn_t) GetProcAddress(handle, "GetNetworkParams"))) {
2782                 log(EVDNS_LOG_WARN, "Could not get address of function.");
2783                 status = -1;
2784                 goto done;
2785         }
2786
2787         buf = malloc(size);
2788         if (!buf) { status = 4; goto done; }
2789         fixed = buf;
2790         r = fn(fixed, &size);
2791         if (r != ERROR_SUCCESS && r != ERROR_BUFFER_OVERFLOW) {
2792                 status = -1;
2793                 goto done;
2794         }
2795         if (r != ERROR_SUCCESS) {
2796                 free(buf);
2797                 buf = malloc(size);
2798                 if (!buf) { status = 4; goto done; }
2799                 fixed = buf;
2800                 r = fn(fixed, &size);
2801                 if (r != ERROR_SUCCESS) {
2802                         log(EVDNS_LOG_DEBUG, "fn() failed.");
2803                         status = -1;
2804                         goto done;
2805                 }
2806         }
2807
2808         assert(fixed);
2809         added_any = 0;
2810         ns = &(fixed->DnsServerList);
2811         while (ns) {
2812                 r = evdns_nameserver_ip_add_line(ns->IpAddress.String);
2813                 if (r) {
2814                         log(EVDNS_LOG_DEBUG,"Could not add nameserver %s to list,error: %d",
2815                                 (ns->IpAddress.String),(int)GetLastError());
2816                         status = r;
2817                         goto done;
2818                 } else {
2819                         log(EVDNS_LOG_DEBUG,"Succesfully added %s as nameserver",ns->IpAddress.String);
2820                 }
2821
2822                 added_any++;
2823                 ns = ns->Next;
2824         }
2825
2826         if (!added_any) {
2827                 log(EVDNS_LOG_DEBUG, "No nameservers added.");
2828                 status = -1;
2829         }
2830
2831  done:
2832         if (buf)
2833                 free(buf);
2834         if (handle)
2835                 FreeLibrary(handle);
2836         return status;
2837 }
2838
2839 static int
2840 config_nameserver_from_reg_key(HKEY key, const char *subkey)
2841 {
2842         char *buf;
2843         DWORD bufsz = 0, type = 0;
2844         int status = 0;
2845
2846         if (RegQueryValueEx(key, subkey, 0, &type, NULL, &bufsz)
2847             != ERROR_MORE_DATA)
2848                 return -1;
2849         if (!(buf = malloc(bufsz)))
2850                 return -1;
2851
2852         if (RegQueryValueEx(key, subkey, 0, &type, (LPBYTE)buf, &bufsz)
2853             == ERROR_SUCCESS && bufsz > 1) {
2854                 status = evdns_nameserver_ip_add_line(buf);
2855         }
2856
2857         free(buf);
2858         return status;
2859 }
2860
2861 #define SERVICES_KEY "System\\CurrentControlSet\\Services\\"
2862 #define WIN_NS_9X_KEY  SERVICES_KEY "VxD\\MSTCP"
2863 #define WIN_NS_NT_KEY  SERVICES_KEY "Tcpip\\Parameters"
2864
2865 static int
2866 load_nameservers_from_registry(void)
2867 {
2868         int found = 0;
2869         int r;
2870 #define TRY(k, name) \
2871         if (!found && config_nameserver_from_reg_key(k,name) == 0) {    \
2872                 log(EVDNS_LOG_DEBUG,"Found nameservers in %s/%s",#k,name); \
2873                 found = 1;                                              \
2874         } else if (!found) {                                            \
2875                 log(EVDNS_LOG_DEBUG,"Didn't find nameservers in %s/%s", \
2876                     #k,#name);                                          \
2877         }
2878
2879         if (((int)GetVersion()) > 0) { /* NT */
2880                 HKEY nt_key = 0, interfaces_key = 0;
2881
2882                 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, WIN_NS_NT_KEY, 0,
2883                                  KEY_READ, &nt_key) != ERROR_SUCCESS) {
2884                         log(EVDNS_LOG_DEBUG,"Couldn't open nt key, %d",(int)GetLastError());
2885                         return -1;
2886                 }
2887                 r = RegOpenKeyEx(nt_key, "Interfaces", 0,
2888                              KEY_QUERY_VALUE|KEY_ENUMERATE_SUB_KEYS,
2889                              &interfaces_key);
2890                 if (r != ERROR_SUCCESS) {
2891                         log(EVDNS_LOG_DEBUG,"Couldn't open interfaces key, %d",(int)GetLastError());
2892                         return -1;
2893                 }
2894                 TRY(nt_key, "NameServer");
2895                 TRY(nt_key, "DhcpNameServer");
2896                 TRY(interfaces_key, "NameServer");
2897                 TRY(interfaces_key, "DhcpNameServer");
2898                 RegCloseKey(interfaces_key);
2899                 RegCloseKey(nt_key);
2900         } else {
2901                 HKEY win_key = 0;
2902                 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, WIN_NS_9X_KEY, 0,
2903                                  KEY_READ, &win_key) != ERROR_SUCCESS) {
2904                         log(EVDNS_LOG_DEBUG, "Couldn't open registry key, %d", (int)GetLastError());
2905                         return -1;
2906                 }
2907                 TRY(win_key, "NameServer");
2908                 RegCloseKey(win_key);
2909         }
2910
2911         if (found == 0) {
2912                 log(EVDNS_LOG_WARN,"Didn't find any nameservers.");
2913         }
2914
2915         return found ? 0 : -1;
2916 #undef TRY
2917 }
2918
2919 int
2920 evdns_config_windows_nameservers(void)
2921 {
2922         if (load_nameservers_with_getnetworkparams() == 0)
2923                 return 0;
2924         return load_nameservers_from_registry();
2925 }
2926 #endif
2927
2928 int
2929 evdns_init(void)
2930 {
2931         int res = 0;
2932 #ifdef WIN32
2933         evdns_config_windows_nameservers();
2934 #else
2935         res = evdns_resolv_conf_parse(DNS_OPTIONS_ALL, "/etc/resolv.conf");
2936 #endif
2937
2938         return (res);
2939 }
2940
2941 const char *
2942 evdns_err_to_string(int err)
2943 {
2944     switch (err) {
2945         case DNS_ERR_NONE: return "no error";
2946         case DNS_ERR_FORMAT: return "misformatted query";
2947         case DNS_ERR_SERVERFAILED: return "server failed";
2948         case DNS_ERR_NOTEXIST: return "name does not exist";
2949         case DNS_ERR_NOTIMPL: return "query not implemented";
2950         case DNS_ERR_REFUSED: return "refused";
2951
2952         case DNS_ERR_TRUNCATED: return "reply truncated or ill-formed";
2953         case DNS_ERR_UNKNOWN: return "unknown";
2954         case DNS_ERR_TIMEOUT: return "request timed out";
2955         case DNS_ERR_SHUTDOWN: return "dns subsystem shut down";
2956         default: return "[Unknown error code]";
2957     }
2958 }
2959
2960 void
2961 evdns_shutdown(int fail_requests)
2962 {
2963         struct nameserver *server, *server_next;
2964         struct search_domain *dom, *dom_next;
2965
2966         while (req_head) {
2967                 if (fail_requests)
2968                         reply_callback(req_head, 0, DNS_ERR_SHUTDOWN, NULL);
2969                 request_finished(req_head, &req_head);
2970         }
2971         while (req_waiting_head) {
2972                 if (fail_requests)
2973                         reply_callback(req_waiting_head, 0, DNS_ERR_SHUTDOWN, NULL);
2974                 request_finished(req_waiting_head, &req_waiting_head);
2975         }
2976         global_requests_inflight = global_requests_waiting = 0;
2977
2978         for (server = server_head; server; server = server_next) {
2979                 server_next = server->next;
2980                 if (server->socket >= 0)
2981                         CLOSE_SOCKET(server->socket);
2982                 (void) event_del(&server->event);
2983                 if (server->state == 0)
2984                         (void) event_del(&server->timeout_event);
2985                 free(server);
2986                 if (server_next == server_head)
2987                         break;
2988         }
2989         server_head = NULL;
2990         global_good_nameservers = 0;
2991
2992         if (global_search_state) {
2993                 for (dom = global_search_state->head; dom; dom = dom_next) {
2994                         dom_next = dom->next;
2995                         free(dom);
2996                 }
2997                 free(global_search_state);
2998                 global_search_state = NULL;
2999         }
3000         evdns_log_fn = NULL;
3001 }
3002
3003 #ifdef EVDNS_MAIN
3004 void
3005 main_callback(int result, char type, int count, int ttl,
3006                           void *addrs, void *orig) {
3007         char *n = (char*)orig;
3008         int i;
3009         for (i = 0; i < count; ++i) {
3010                 if (type == DNS_IPv4_A) {
3011                         printf("%s: %s\n", n, debug_ntoa(((u32*)addrs)[i]));
3012                 } else if (type == DNS_PTR) {
3013                         printf("%s: %s\n", n, ((char**)addrs)[i]);
3014                 }
3015         }
3016         if (!count) {
3017                 printf("%s: No answer (%d)\n", n, result);
3018         }
3019         fflush(stdout);
3020 }
3021 void
3022 evdns_server_callback(struct evdns_server_request *req, void *data)
3023 {
3024         int i, r;
3025         (void)data;
3026         /* dummy; give 192.168.11.11 as an answer for all A questions,
3027          *      give foo.bar.example.com as an answer for all PTR questions. */
3028         for (i = 0; i < req->nquestions; ++i) {
3029                 u32 ans = htonl(0xc0a80b0bUL);
3030                 if (req->questions[i]->type == EVDNS_TYPE_A &&
3031                         req->questions[i]->class == EVDNS_CLASS_INET) {
3032                         printf(" -- replying for %s (A)\n", req->questions[i]->name);
3033                         r = evdns_server_request_add_a_reply(req, req->questions[i]->name,
3034                                                                                   1, &ans, 10);
3035                         if (r<0)
3036                                 printf("eeep, didn't work.\n");
3037                 } else if (req->questions[i]->type == EVDNS_TYPE_PTR &&
3038                                    req->questions[i]->class == EVDNS_CLASS_INET) {
3039                         printf(" -- replying for %s (PTR)\n", req->questions[i]->name);
3040                         r = evdns_server_request_add_ptr_reply(req, NULL, req->questions[i]->name,
3041                                                                                         "foo.bar.example.com", 10);
3042                 } else {
3043                         printf(" -- skipping %s [%d %d]\n", req->questions[i]->name,
3044                                    req->questions[i]->type, req->questions[i]->class);
3045                 }
3046         }
3047
3048         r = evdns_request_respond(req, 0);
3049         if (r<0)
3050                 printf("eeek, couldn't send reply.\n");
3051 }
3052
3053 void
3054 logfn(int is_warn, const char *msg) {
3055   (void) is_warn;
3056   fprintf(stderr, "%s\n", msg);
3057 }
3058 int
3059 main(int c, char **v) {
3060         int idx;
3061         int reverse = 0, verbose = 1, servertest = 0;
3062         if (c<2) {
3063                 fprintf(stderr, "syntax: %s [-x] [-v] hostname\n", v[0]);
3064                 fprintf(stderr, "syntax: %s [-servertest]\n", v[0]);
3065                 return 1;
3066         }
3067         idx = 1;
3068         while (idx < c && v[idx][0] == '-') {
3069                 if (!strcmp(v[idx], "-x"))
3070                         reverse = 1;
3071                 else if (!strcmp(v[idx], "-v"))
3072                         verbose = 1;
3073                 else if (!strcmp(v[idx], "-servertest"))
3074                         servertest = 1;
3075                 else
3076                         fprintf(stderr, "Unknown option %s\n", v[idx]);
3077                 ++idx;
3078         }
3079         event_init();
3080         if (verbose)
3081                 evdns_set_log_fn(logfn);
3082         evdns_resolv_conf_parse(DNS_OPTION_NAMESERVERS, "/etc/resolv.conf");
3083         if (servertest) {
3084                 int sock;
3085                 struct sockaddr_in my_addr;
3086                 sock = socket(PF_INET, SOCK_DGRAM, 0);
3087                 fcntl(sock, F_SETFL, O_NONBLOCK);
3088                 my_addr.sin_family = AF_INET;
3089                 my_addr.sin_port = htons(10053);
3090                 my_addr.sin_addr.s_addr = INADDR_ANY;
3091                 if (bind(sock, (struct sockaddr*)&my_addr, sizeof(my_addr))<0) {
3092                         perror("bind");
3093                         exit(1);
3094                 }
3095                 evdns_add_server_port(sock, 0, evdns_server_callback, NULL);
3096         }
3097         for (; idx < c; ++idx) {
3098                 if (reverse) {
3099                         struct in_addr addr;
3100                         if (!inet_aton(v[idx], &addr)) {
3101                                 fprintf(stderr, "Skipping non-IP %s\n", v[idx]);
3102                                 continue;
3103                         }
3104                         fprintf(stderr, "resolving %s...\n",v[idx]);
3105                         evdns_resolve_reverse(&addr, 0, main_callback, v[idx]);
3106                 } else {
3107                         fprintf(stderr, "resolving (fwd) %s...\n",v[idx]);
3108                         evdns_resolve_ipv4(v[idx], 0, main_callback, v[idx]);
3109                 }
3110         }
3111         fflush(stdout);
3112         event_dispatch();
3113         return 0;
3114 }
3115 #endif