1 /* $Id: evdns.c,v 1.18 2007-11-06 17:20:42 root Exp $ */
3 /* The original version of this module was written by Adam Langley; for
4 * a history of modifications, check out the subversion logs.
6 * When editing this module, try to keep it re-mergeable by Adam. Don't
7 * reformat the whitespace, add Tor dependencies, or so on.
10 * - Support IPv6 and PTR records.
11 * - Replace all externally visible magic numbers with #defined constants.
12 * - Write doccumentation for APIs of all external functions.
16 * Adam Langley <agl@imperialviolet.org>
17 * http://www.imperialviolet.org/eventdns.html
20 * This software is Public Domain. To view a copy of the public domain dedication,
21 * visit http://creativecommons.org/licenses/publicdomain/ or send a letter to
22 * Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
24 * I ask and expect, but do not require, that all derivative works contain an
25 * attribution similar to:
26 * Parts developed by Adam Langley <agl@imperialviolet.org>
28 * You may wish to replace the word "Parts" with something else depending on
29 * the amount of original code.
31 * (Derivative works does not include programs which link against, run or include
32 * the source verbatim in their source distributions)
37 #include <sys/types.h>
50 #ifndef DNS_USE_CPU_CLOCK_FOR_ID
51 #ifndef DNS_USE_GETTIMEOFDAY_FOR_ID
52 #ifndef DNS_USE_OPENSSL_FOR_ID
53 #error Must configure at least one id generation method.
54 #error Please see the documentation.
59 /* #define _POSIX_C_SOURCE 200507 */
62 #ifdef DNS_USE_CPU_CLOCK_FOR_ID
63 #ifdef DNS_USE_OPENSSL_FOR_ID
64 #error Multiple id options selected
66 #ifdef DNS_USE_GETTIMEOFDAY_FOR_ID
67 #error Multiple id options selected
72 #ifdef DNS_USE_OPENSSL_FOR_ID
73 #ifdef DNS_USE_GETTIMEOFDAY_FOR_ID
74 #error Multiple id options selected
76 #include <openssl/rand.h>
79 #define _FORTIFY_SOURCE 3
101 #include <winsock2.h>
102 #include <iphlpapi.h>
104 #include <sys/socket.h>
105 #include <netinet/in.h>
106 #include <arpa/inet.h>
109 #ifdef HAVE_NETINET_IN6_H
110 #include <netinet/in6.h>
114 typedef int socklen_t;
117 #define EVDNS_LOG_DEBUG 0
118 #define EVDNS_LOG_WARN 1
120 #ifndef HOST_NAME_MAX
121 #define HOST_NAME_MAX 255
129 #define MIN(a,b) ((a)<(b)?(a):(b))
132 /* libevent doesn't work without this */
133 typedef uint8_t u_char;
134 typedef unsigned int uint;
143 #define MAX_ADDRS 4 /* maximum number of addresses from a single packet */
144 /* which we bother recording */
146 #define TYPE_A EVDNS_TYPE_A
148 #define TYPE_PTR EVDNS_TYPE_PTR
149 #define TYPE_AAAA EVDNS_TYPE_AAAA
151 #define CLASS_INET EVDNS_CLASS_INET
154 u8 *request; /* the dns packet data */
155 unsigned int request_len;
157 int tx_count; /* the number of times that this packet has been sent */
158 unsigned int request_type; /* TYPE_PTR or TYPE_A */
159 void *user_pointer; /* the pointer given to us for this request */
160 evdns_callback_type user_callback;
161 struct nameserver *ns; /* the server which we last sent it */
163 /* elements used by the searching code */
165 struct search_state *search_state;
166 char *search_origname; /* needs to be free()ed */
169 /* these objects are kept in a circular list */
170 struct request *next, *prev;
172 struct event timeout_event;
174 u16 trans_id; /* the transaction id */
175 char request_appended; /* true if the request pointer is data which follows this struct */
176 char transmit_me; /* needs to be transmitted */
179 #ifndef HAVE_STRUCT_IN6_ADDR
187 unsigned int have_answer;
191 u32 addresses[MAX_ADDRS];
195 struct in6_addr addresses[MAX_ADDRS];
198 char name[HOST_NAME_MAX];
204 int socket; /* a connected UDP socket */
206 int failed_times; /* number of times which we have given this server a chance */
207 int timedout; /* number of times in a row a request has timed out */
209 /* these objects are kept in a circular list */
210 struct nameserver *next, *prev;
211 struct event timeout_event; /* used to keep the timeout for */
212 /* when we next probe this server. */
213 /* Valid if state == 0 */
214 char state; /* zero if we think that this server is down */
215 char choked; /* true if we have an EAGAIN from this server's socket */
216 char write_waiting; /* true if we are waiting for EV_WRITE events */
219 static struct request *req_head = NULL, *req_waiting_head = NULL;
220 static struct nameserver *server_head = NULL;
222 /* Represents a local port where we're listening for DNS requests. Right now, */
223 /* only UDP is supported. */
224 struct evdns_server_port {
225 int socket; /* socket we use to read queries and write replies. */
226 int refcnt; /* reference count. */
227 char choked; /* Are we currently blocked from writing? */
228 char closing; /* Are we trying to close this port, pending writes? */
229 evdns_request_callback_fn_type user_callback; /* Fn to handle requests */
230 void *user_data; /* Opaque pointer passed to user_callback */
231 struct event event; /* Read/write event */
232 /* circular list of replies that we want to write. */
233 struct server_request *pending_replies;
236 /* Represents part of a reply being built. (That is, a single RR.) */
237 struct server_reply_item {
238 struct server_reply_item *next; /* next item in sequence. */
239 char *name; /* name part of the RR */
240 u16 type : 16; /* The RR type */
241 u16 class : 16; /* The RR class (usually CLASS_INET) */
242 u32 ttl; /* The RR TTL */
243 char is_name; /* True iff data is a label */
244 u16 datalen; /* Length of data; -1 if data is a label */
245 void *data; /* The contents of the RR */
248 /* Represents a request that we've received as a DNS server, and holds */
249 /* the components of the reply as we're constructing it. */
250 struct server_request {
251 /* Pointers to the next and previous entries on the list of replies */
252 /* that we're waiting to write. Only set if we have tried to respond */
253 /* and gotten EAGAIN. */
254 struct server_request *next_pending;
255 struct server_request *prev_pending;
257 u16 trans_id; /* Transaction id. */
258 struct evdns_server_port *port; /* Which port received this request on? */
259 struct sockaddr_storage addr; /* Where to send the response */
260 socklen_t addrlen; /* length of addr */
262 int n_answer; /* how many answer RRs have been set? */
263 int n_authority; /* how many authority RRs have been set? */
264 int n_additional; /* how many additional RRs have been set? */
266 struct server_reply_item *answer; /* linked list of answer RRs */
267 struct server_reply_item *authority; /* linked list of authority RRs */
268 struct server_reply_item *additional; /* linked list of additional RRs */
270 /* Constructed response. Only set once we're ready to send a reply. */
271 /* Once this is set, the RR fields are cleared, and no more should be set. */
275 /* Caller-visible fields: flags, questions. */
276 struct evdns_server_request base;
280 #define OFFSET_OF(st, member) ((off_t) (((char*)&((st*)0)->member)-(char*)0))
282 /* Given a pointer to an evdns_server_request, get the corresponding */
283 /* server_request. */
284 #define TO_SERVER_REQUEST(base_ptr) \
285 ((struct server_request*) \
286 (((char*)(base_ptr) - OFFSET_OF(struct server_request, base))))
288 /* The number of good nameservers that we have */
289 static int global_good_nameservers = 0;
291 /* inflight requests are contained in the req_head list */
292 /* and are actually going out across the network */
293 static int global_requests_inflight = 0;
294 /* requests which aren't inflight are in the waiting list */
295 /* and are counted here */
296 static int global_requests_waiting = 0;
298 static int global_max_requests_inflight = 64;
300 static struct timeval global_timeout = {5, 0}; /* 5 seconds */
301 static int global_max_reissues = 1; /* a reissue occurs when we get some errors from the server */
302 static int global_max_retransmits = 3; /* number of times we'll retransmit a request which timed out */
303 /* number of timeouts in a row before we consider this server to be down */
304 static int global_max_nameserver_timeout = 3;
306 /* These are the timeout values for nameservers. If we find a nameserver is down */
307 /* we try to probe it at intervals as given below. Values are in seconds. */
308 static const struct timeval global_nameserver_timeouts[] = {{10, 0}, {60, 0}, {300, 0}, {900, 0}, {3600, 0}};
309 static const int global_nameserver_timeouts_length = sizeof(global_nameserver_timeouts)/sizeof(struct timeval);
311 static struct nameserver *nameserver_pick(void);
312 static void evdns_request_insert(struct request *req, struct request **head);
313 static void nameserver_ready_callback(int fd, short events, void *arg);
314 static int evdns_transmit(void);
315 static int evdns_request_transmit(struct request *req);
316 static void nameserver_send_probe(struct nameserver *const ns);
317 static void search_request_finished(struct request *const);
318 static int search_try_next(struct request *const req);
319 static int search_request_new(int type, const char *const name, int flags, evdns_callback_type user_callback, void *user_arg);
320 static void evdns_requests_pump_waiting_queue(void);
321 static u16 transaction_id_pick(void);
322 static struct request *request_new(int type, const char *name, int flags, evdns_callback_type callback, void *ptr);
323 static void request_submit(struct request *req);
325 static int server_request_free(struct server_request *req);
326 static void server_request_free_answers(struct server_request *req);
327 static void server_port_free(struct evdns_server_port *port);
328 static void server_port_ready_callback(int fd, short events, void *arg);
330 static int strtoint(const char *const str);
336 int optval, optvallen=sizeof(optval);
337 int err = WSAGetLastError();
338 if (err == WSAEWOULDBLOCK && sock >= 0) {
339 if (getsockopt(sock, SOL_SOCKET, SO_ERROR, (void*)&optval,
349 error_is_eagain(int err)
351 return err == EAGAIN || err == WSAEWOULDBLOCK;
354 inet_aton(const char *c, struct in_addr *addr)
357 if (strcmp(c, "255.255.255.255") == 0) {
358 addr->s_addr = 0xffffffffu;
361 if (r == INADDR_NONE)
367 #define CLOSE_SOCKET(x) closesocket(x)
369 #define last_error(sock) (errno)
370 #define error_is_eagain(err) ((err) == EAGAIN)
371 #define CLOSE_SOCKET(x) close(x)
374 #define ISSPACE(c) isspace((int)(unsigned char)(c))
375 #define ISDIGIT(c) isdigit((int)(unsigned char)(c))
379 debug_ntoa(u32 address)
382 u32 a = ntohl(address);
383 snprintf(buf, sizeof(buf), "%d.%d.%d.%d",
384 (int)(u8)((a>>24)&0xff),
385 (int)(u8)((a>>16)&0xff),
386 (int)(u8)((a>>8 )&0xff),
387 (int)(u8)((a )&0xff));
392 static evdns_debug_log_fn_type evdns_log_fn = NULL;
395 evdns_set_log_fn(evdns_debug_log_fn_type fn)
401 #define EVDNS_LOG_CHECK __attribute__ ((format(printf, 2, 3)))
403 #define EVDNS_LOG_CHECK
406 static void _evdns_log(int warn, const char *fmt, ...) EVDNS_LOG_CHECK;
408 _evdns_log(int warn, const char *fmt, ...)
411 static char buf[512];
416 _vsnprintf(buf, sizeof(buf), fmt, args);
418 vsnprintf(buf, sizeof(buf), fmt, args);
420 buf[sizeof(buf)-1] = '\0';
421 evdns_log_fn(warn, buf);
425 #define log _evdns_log
427 /* This walks the list of inflight requests to find the */
428 /* one with a matching transaction id. Returns NULL on */
430 static struct request *
431 request_find_from_trans_id(u16 trans_id) {
432 struct request *req = req_head, *const started_at = req_head;
436 if (req->trans_id == trans_id) return req;
438 } while (req != started_at);
444 /* a libevent callback function which is called when a nameserver */
445 /* has gone down and we want to test if it has came back to life yet */
447 nameserver_prod_callback(int fd, short events, void *arg) {
448 struct nameserver *const ns = (struct nameserver *) arg;
452 nameserver_send_probe(ns);
455 /* a libevent callback which is called when a nameserver probe (to see if */
456 /* it has come back to life) times out. We increment the count of failed_times */
457 /* and wait longer to send the next probe packet. */
459 nameserver_probe_failed(struct nameserver *const ns) {
460 const struct timeval * timeout;
461 (void) evtimer_del(&ns->timeout_event);
462 if (ns->state == 1) {
463 /* This can happen if the nameserver acts in a way which makes us mark */
464 /* it as bad and then starts sending good replies. */
469 &global_nameserver_timeouts[MIN(ns->failed_times,
470 global_nameserver_timeouts_length - 1)];
473 evtimer_set(&ns->timeout_event, nameserver_prod_callback, ns);
474 if (evtimer_add(&ns->timeout_event, (struct timeval *) timeout) < 0) {
476 "Error from libevent when adding timer event for %s",
477 debug_ntoa(ns->address));
482 /* called when a nameserver has been deemed to have failed. For example, too */
483 /* many packets have timed out etc */
485 nameserver_failed(struct nameserver *const ns, const char *msg) {
486 struct request *req, *started_at;
487 /* if this nameserver has already been marked as failed */
488 /* then don't do anything */
489 if (!ns->state) return;
491 log(EVDNS_LOG_WARN, "Nameserver %s has failed: %s",
492 debug_ntoa(ns->address), msg);
493 global_good_nameservers--;
494 assert(global_good_nameservers >= 0);
495 if (global_good_nameservers == 0) {
496 log(EVDNS_LOG_WARN, "All nameservers have failed");
500 ns->failed_times = 1;
502 evtimer_set(&ns->timeout_event, nameserver_prod_callback, ns);
503 if (evtimer_add(&ns->timeout_event, (struct timeval *) &global_nameserver_timeouts[0]) < 0) {
505 "Error from libevent when adding timer event for %s",
506 debug_ntoa(ns->address));
510 /* walk the list of inflight requests to see if any can be reassigned to */
511 /* a different server. Requests in the waiting queue don't have a */
512 /* nameserver assigned yet */
514 /* if we don't have *any* good nameservers then there's no point */
515 /* trying to reassign requests to one */
516 if (!global_good_nameservers) return;
519 started_at = req_head;
522 if (req->tx_count == 0 && req->ns == ns) {
523 /* still waiting to go out, can be moved */
524 /* to another server */
525 req->ns = nameserver_pick();
528 } while (req != started_at);
533 nameserver_up(struct nameserver *const ns) {
534 if (ns->state) return;
535 log(EVDNS_LOG_WARN, "Nameserver %s is back up",
536 debug_ntoa(ns->address));
537 evtimer_del(&ns->timeout_event);
539 ns->failed_times = 0;
541 global_good_nameservers++;
545 request_trans_id_set(struct request *const req, const u16 trans_id) {
546 req->trans_id = trans_id;
547 *((u16 *) req->request) = htons(trans_id);
550 /* Called to remove a request from a list and dealloc it. */
551 /* head is a pointer to the head of the list it should be */
552 /* removed from or NULL if the request isn't in a list. */
554 request_finished(struct request *const req, struct request **head) {
556 if (req->next == req) {
557 /* only item in the list */
560 req->next->prev = req->prev;
561 req->prev->next = req->next;
562 if (*head == req) *head = req->next;
566 log(EVDNS_LOG_DEBUG, "Removing timeout for request %lx",
567 (unsigned long) req);
568 evtimer_del(&req->timeout_event);
570 search_request_finished(req);
571 global_requests_inflight--;
573 if (!req->request_appended) {
574 /* need to free the request data on it's own */
577 /* the request data is appended onto the header */
578 /* so everything gets free()ed when we: */
583 evdns_requests_pump_waiting_queue();
586 /* This is called when a server returns a funny error code. */
587 /* We try the request again with another server. */
591 /* 1 failed/reissue is pointless */
593 request_reissue(struct request *req) {
594 const struct nameserver *const last_ns = req->ns;
595 /* the last nameserver should have been marked as failing */
596 /* by the caller of this function, therefore pick will try */
597 /* not to return it */
598 req->ns = nameserver_pick();
599 if (req->ns == last_ns) {
600 /* ... but pick did return it */
601 /* not a lot of point in trying again with the */
606 req->reissue_count++;
608 req->transmit_me = 1;
613 /* this function looks for space on the inflight queue and promotes */
614 /* requests from the waiting queue if it can. */
616 evdns_requests_pump_waiting_queue(void) {
617 while (global_requests_inflight < global_max_requests_inflight &&
618 global_requests_waiting) {
620 /* move a request from the waiting queue to the inflight queue */
621 assert(req_waiting_head);
622 if (req_waiting_head->next == req_waiting_head) {
623 /* only one item in the queue */
624 req = req_waiting_head;
625 req_waiting_head = NULL;
627 req = req_waiting_head;
628 req->next->prev = req->prev;
629 req->prev->next = req->next;
630 req_waiting_head = req->next;
633 global_requests_waiting--;
634 global_requests_inflight++;
636 req->ns = nameserver_pick();
637 request_trans_id_set(req, transaction_id_pick());
639 evdns_request_insert(req, &req_head);
640 evdns_request_transmit(req);
646 reply_callback(struct request *const req, u32 ttl, u32 err, struct reply *reply) {
647 switch (req->request_type) {
650 req->user_callback(DNS_ERR_NONE, DNS_IPv4_A,
651 reply->data.a.addrcount, ttl,
652 reply->data.a.addresses,
655 req->user_callback(err, 0, 0, 0, NULL, req->user_pointer);
659 char *name = reply->data.ptr.name;
660 req->user_callback(DNS_ERR_NONE, DNS_PTR, 1, ttl,
661 &name, req->user_pointer);
663 req->user_callback(err, 0, 0, 0, NULL,
669 req->user_callback(DNS_ERR_NONE, DNS_IPv6_AAAA,
670 reply->data.aaaa.addrcount, ttl,
671 reply->data.aaaa.addresses,
674 req->user_callback(err, 0, 0, 0, NULL, req->user_pointer);
680 /* this processes a parsed reply packet */
682 reply_handle(struct request *const req, u16 flags, u32 ttl, struct reply *reply) {
684 static const int error_codes[] = {DNS_ERR_FORMAT, DNS_ERR_SERVERFAILED, DNS_ERR_NOTEXIST, DNS_ERR_NOTIMPL, DNS_ERR_REFUSED};
686 if (flags & 0x020f || !reply || !reply->have_answer) {
687 /* there was an error */
688 if (flags & 0x0200) {
689 error = DNS_ERR_TRUNCATED;
691 u16 error_code = (flags & 0x000f) - 1;
692 if (error_code > 4) {
693 error = DNS_ERR_UNKNOWN;
695 error = error_codes[error_code];
700 case DNS_ERR_NOTIMPL:
701 case DNS_ERR_REFUSED:
702 /* we regard these errors as marking a bad nameserver */
703 if (req->reissue_count < global_max_reissues) {
705 snprintf(msg, sizeof(msg), "Bad response %d (%s)",
706 error, evdns_err_to_string(error));
707 nameserver_failed(req->ns, msg);
708 if (!request_reissue(req)) return;
711 case DNS_ERR_SERVERFAILED:
712 /* rcode 2 (servfailed) sometimes means "we are broken" and
713 * sometimes (with some binds) means "that request was very
714 * confusing." Treat this as a timeout, not a failure.
716 log(EVDNS_LOG_DEBUG, "Got a SERVERFAILED from nameserver %s; "
717 "will allow the request to time out.",
718 debug_ntoa(req->ns->address));
721 /* we got a good reply from the nameserver */
722 nameserver_up(req->ns);
725 if (req->search_state && req->request_type != TYPE_PTR) {
726 /* if we have a list of domains to search in, try the next one */
727 if (!search_try_next(req)) {
728 /* a new request was issued so this request is finished and */
729 /* the user callback will be made when that request (or a */
730 /* child of it) finishes. */
731 request_finished(req, &req_head);
736 /* all else failed. Pass the failure up */
737 reply_callback(req, 0, error, NULL);
738 request_finished(req, &req_head);
740 /* all ok, tell the user */
741 reply_callback(req, ttl, 0, reply);
742 nameserver_up(req->ns);
743 request_finished(req, &req_head);
748 name_parse(u8 *packet, int length, int *idx, char *name_out, int name_out_len) {
752 #define GET32(x) do { if (j + 4 > length) goto err; memcpy(&_t32, packet + j, 4); j += 4; x = ntohl(_t32); } while(0)
753 #define GET16(x) do { if (j + 2 > length) goto err; memcpy(&_t, packet + j, 2); j += 2; x = ntohs(_t); } while(0)
754 #define GET8(x) do { if (j >= length) goto err; x = packet[j++]; } while(0)
757 const char *const end = name_out + name_out_len;
759 /* Normally, names are a series of length prefixed strings terminated */
760 /* with a length of 0 (the lengths are u8's < 63). */
761 /* However, the length can start with a pair of 1 bits and that */
762 /* means that the next 14 bits are a pointer within the current */
767 if (j >= length) return -1;
769 if (!label_len) break;
770 if (label_len & 0xc0) {
773 if (name_end < 0) name_end = j;
774 j = (((int)label_len & 0x3f) << 8) + ptr_low;
775 /* Make sure that the target offset is in-bounds. */
776 if (j < 0 || j >= length) return -1;
777 /* If we've jumped more times than there are characters in the
778 * message, we must have a loop. */
779 if (++ptr_count > length) return -1;
782 if (label_len > 63) return -1;
783 if (cp != name_out) {
784 if (cp + 1 >= end) return -1;
787 if (cp + label_len >= end) return -1;
788 memcpy(cp, packet + j, label_len);
792 if (cp >= end) return -1;
803 /* parses a raw request from a nameserver */
805 reply_parse(u8 *packet, int length) {
806 int j = 0; /* index into packet */
807 u16 _t; /* used by the macros */
808 u32 _t32; /* used by the macros */
809 char tmp_name[256]; /* used by the macros */
811 u16 trans_id, questions, answers, authority, additional, datalength;
813 u32 ttl, ttl_r = 0xffffffff;
815 struct request *req = NULL;
824 (void) authority; /* suppress "unused variable" warnings. */
825 (void) additional; /* suppress "unused variable" warnings. */
827 req = request_find_from_trans_id(trans_id);
830 memset(&reply, 0, sizeof(reply));
832 /* If it's not an answer, it doesn't correspond to any request. */
833 if (!(flags & 0x8000)) return -1; /* must be an answer */
834 if (flags & 0x020f) {
835 /* there was an error */
838 /* if (!answers) return; */ /* must have an answer of some form */
840 /* This macro skips a name in the DNS reply. */
842 do { tmp_name[0] = '\0'; \
843 if (name_parse(packet, length, &j, tmp_name, sizeof(tmp_name))<0) \
847 reply.type = req->request_type;
849 /* skip over each question in the reply */
850 for (i = 0; i < questions; ++i) {
851 /* the question looks like
852 * <label:name><u16:type><u16:class>
856 if (j >= length) goto err;
859 /* now we have the answer section which looks like
860 * <label:name><u16:type><u16:class><u32:ttl><u16:len><data...>
863 for (i = 0; i < answers; ++i) {
872 if (type == TYPE_A && class == CLASS_INET) {
873 int addrcount, addrtocopy;
874 if (req->request_type != TYPE_A) {
875 j += datalength; continue;
877 if ((datalength & 3) != 0) /* not an even number of As. */
879 addrcount = datalength >> 2;
880 addrtocopy = MIN(MAX_ADDRS - reply.data.a.addrcount, (unsigned)addrcount);
882 ttl_r = MIN(ttl_r, ttl);
883 /* we only bother with the first four addresses. */
884 if (j + 4*addrtocopy > length) goto err;
885 memcpy(&reply.data.a.addresses[reply.data.a.addrcount],
886 packet + j, 4*addrtocopy);
888 reply.data.a.addrcount += addrtocopy;
889 reply.have_answer = 1;
890 if (reply.data.a.addrcount == MAX_ADDRS) break;
891 } else if (type == TYPE_PTR && class == CLASS_INET) {
892 if (req->request_type != TYPE_PTR) {
893 j += datalength; continue;
895 if (name_parse(packet, length, &j, reply.data.ptr.name,
896 sizeof(reply.data.ptr.name))<0)
898 ttl_r = MIN(ttl_r, ttl);
899 reply.have_answer = 1;
901 } else if (type == TYPE_AAAA && class == CLASS_INET) {
902 int addrcount, addrtocopy;
903 if (req->request_type != TYPE_AAAA) {
904 j += datalength; continue;
906 if ((datalength & 15) != 0) /* not an even number of AAAAs. */
908 addrcount = datalength >> 4; /* each address is 16 bytes long */
909 addrtocopy = MIN(MAX_ADDRS - reply.data.aaaa.addrcount, (unsigned)addrcount);
910 ttl_r = MIN(ttl_r, ttl);
912 /* we only bother with the first four addresses. */
913 if (j + 16*addrtocopy > length) goto err;
914 memcpy(&reply.data.aaaa.addresses[reply.data.aaaa.addrcount],
915 packet + j, 16*addrtocopy);
916 reply.data.aaaa.addrcount += addrtocopy;
918 reply.have_answer = 1;
919 if (reply.data.aaaa.addrcount == MAX_ADDRS) break;
921 /* skip over any other type of resource */
926 reply_handle(req, flags, ttl_r, &reply);
930 reply_handle(req, flags, 0, NULL);
934 /* Parse a raw request (packet,length) sent to a nameserver port (port) from */
935 /* a DNS client (addr,addrlen), and if it's well-formed, call the corresponding */
938 request_parse(u8 *packet, int length, struct evdns_server_port *port, struct sockaddr *addr, socklen_t addrlen)
940 int j = 0; /* index into packet */
941 u16 _t; /* used by the macros */
942 char tmp_name[256]; /* used by the macros */
945 u16 trans_id, flags, questions, answers, authority, additional;
946 struct server_request *server_req = NULL;
948 /* Get the header fields */
956 if (flags & 0x8000) return -1; /* Must not be an answer. */
957 if (flags & 0x7800) return -1; /* only standard queries are supported */
958 flags &= 0x0300; /* Only TC and RD get preserved. */
960 server_req = malloc(sizeof(struct server_request));
961 if (server_req == NULL) return -1;
962 memset(server_req, 0, sizeof(struct server_request));
964 server_req->trans_id = trans_id;
965 memcpy(&server_req->addr, addr, addrlen);
966 server_req->addrlen = addrlen;
968 server_req->base.flags = flags;
969 server_req->base.nquestions = 0;
970 server_req->base.questions = malloc(sizeof(struct evdns_server_question *) * questions);
971 if (server_req->base.questions == NULL)
974 for (i = 0; i < questions; ++i) {
976 struct evdns_server_question *q;
978 if (name_parse(packet, length, &j, tmp_name, sizeof(tmp_name))<0)
982 namelen = strlen(tmp_name);
983 q = malloc(sizeof(struct evdns_server_question) + namelen);
988 memcpy(q->name, tmp_name, namelen+1);
989 server_req->base.questions[server_req->base.nquestions++] = q;
992 /* Ignore answers, authority, and additional. */
994 server_req->port = port;
996 port->user_callback(&(server_req->base), port->user_data);
1001 if (server_req->base.questions) {
1002 for (i = 0; i < server_req->base.nquestions; ++i)
1003 free(server_req->base.questions[i]);
1004 free(server_req->base.questions);
1016 /* Try to choose a strong transaction id which isn't already in flight */
1018 transaction_id_pick(void) {
1020 const struct request *req = req_head, *started_at;
1021 #ifdef DNS_USE_CPU_CLOCK_FOR_ID
1024 #ifdef CLOCK_MONOTONIC
1025 if (clock_gettime(CLOCK_MONOTONIC, &ts) == -1)
1027 if (clock_gettime(CLOCK_REALTIME, &ts) == -1)
1029 event_err(1, "clock_gettime");
1030 trans_id = ts.tv_nsec & 0xffff;
1033 #ifdef DNS_USE_GETTIMEOFDAY_FOR_ID
1036 gettimeofday(&tv, NULL);
1037 trans_id = tv.tv_usec & 0xffff;
1040 #ifdef DNS_USE_OPENSSL_FOR_ID
1042 if (RAND_pseudo_bytes((u8 *) &trans_id, 2) == -1) {
1043 /* in the case that the RAND call fails we back */
1044 /* down to using gettimeofday. */
1046 gettimeofday(&tv, NULL);
1047 trans_id = tv.tv_usec & 0xffff; */
1052 if (trans_id == 0xffff) continue;
1053 /* now check to see if that id is already inflight */
1054 req = started_at = req_head;
1057 if (req->trans_id == trans_id) break;
1059 } while (req != started_at);
1061 /* we didn't find it, so this is a good id */
1062 if (req == started_at) return trans_id;
1066 /* choose a namesever to use. This function will try to ignore */
1067 /* nameservers which we think are down and load balance across the rest */
1068 /* by updating the server_head global each time. */
1069 static struct nameserver *
1070 nameserver_pick(void) {
1071 struct nameserver *started_at = server_head, *picked;
1072 if (!server_head) return NULL;
1074 /* if we don't have any good nameservers then there's no */
1075 /* point in trying to find one. */
1076 if (!global_good_nameservers) {
1077 server_head = server_head->next;
1081 /* remember that nameservers are in a circular list */
1083 if (server_head->state) {
1084 /* we think this server is currently good */
1085 picked = server_head;
1086 server_head = server_head->next;
1090 server_head = server_head->next;
1091 if (server_head == started_at) {
1092 /* all the nameservers seem to be down */
1093 /* so we just return this one and hope for the */
1095 assert(global_good_nameservers == 0);
1096 picked = server_head;
1097 server_head = server_head->next;
1103 /* this is called when a namesever socket is ready for reading */
1105 nameserver_read(struct nameserver *ns) {
1109 const int r = recv(ns->socket, packet, sizeof(packet), 0);
1111 int err = last_error(ns->socket);
1112 if (error_is_eagain(err)) return;
1113 nameserver_failed(ns, strerror(err));
1117 reply_parse(packet, r);
1121 /* Read a packet from a DNS client on a server port s, parse it, and */
1122 /* act accordingly. */
1124 server_port_read(struct evdns_server_port *s) {
1126 struct sockaddr_storage addr;
1131 addrlen = sizeof(struct sockaddr_storage);
1132 r = recvfrom(s->socket, packet, sizeof(packet), 0,
1133 (struct sockaddr*) &addr, &addrlen);
1135 int err = last_error(s->socket);
1136 if (error_is_eagain(err)) return;
1137 log(EVDNS_LOG_WARN, "Error %s (%d) while reading request.",
1138 strerror(err), err);
1141 request_parse(packet, r, s, (struct sockaddr*) &addr, addrlen);
1145 /* Try to write all pending replies on a given DNS server port. */
1147 server_port_flush(struct evdns_server_port *port)
1149 while (port->pending_replies) {
1150 struct server_request *req = port->pending_replies;
1151 int r = sendto(port->socket, req->response, req->response_len, 0,
1152 (struct sockaddr*) &req->addr, req->addrlen);
1154 int err = last_error(port->socket);
1155 if (error_is_eagain(err))
1157 log(EVDNS_LOG_WARN, "Error %s (%d) while writing response to port; dropping", strerror(err), err);
1159 if (server_request_free(req)) {
1160 /* we released the last reference to req->port. */
1165 /* We have no more pending requests; stop listening for 'writeable' events. */
1166 (void) event_del(&port->event);
1167 event_set(&port->event, port->socket, EV_READ | EV_PERSIST,
1168 server_port_ready_callback, port);
1169 if (event_add(&port->event, NULL) < 0) {
1170 log(EVDNS_LOG_WARN, "Error from libevent when adding event for DNS server.");
1175 /* set if we are waiting for the ability to write to this server. */
1176 /* if waiting is true then we ask libevent for EV_WRITE events, otherwise */
1177 /* we stop these events. */
1179 nameserver_write_waiting(struct nameserver *ns, char waiting) {
1180 if (ns->write_waiting == waiting) return;
1182 ns->write_waiting = waiting;
1183 (void) event_del(&ns->event);
1184 event_set(&ns->event, ns->socket, EV_READ | (waiting ? EV_WRITE : 0) | EV_PERSIST,
1185 nameserver_ready_callback, ns);
1186 if (event_add(&ns->event, NULL) < 0) {
1187 log(EVDNS_LOG_WARN, "Error from libevent when adding event for %s",
1188 debug_ntoa(ns->address));
1193 /* a callback function. Called by libevent when the kernel says that */
1194 /* a nameserver socket is ready for writing or reading */
1196 nameserver_ready_callback(int fd, short events, void *arg) {
1197 struct nameserver *ns = (struct nameserver *) arg;
1200 if (events & EV_WRITE) {
1202 if (!evdns_transmit()) {
1203 nameserver_write_waiting(ns, 0);
1206 if (events & EV_READ) {
1207 nameserver_read(ns);
1211 /* a callback function. Called by libevent when the kernel says that */
1212 /* a server socket is ready for writing or reading. */
1214 server_port_ready_callback(int fd, short events, void *arg) {
1215 struct evdns_server_port *port = (struct evdns_server_port *) arg;
1218 if (events & EV_WRITE) {
1220 server_port_flush(port);
1222 if (events & EV_READ) {
1223 server_port_read(port);
1227 /* This is an inefficient representation; only use it via the dnslabel_table_*
1228 * functions, so that is can be safely replaced with something smarter later. */
1229 #define MAX_LABELS 128
1230 /* Structures used to implement name compression */
1231 struct dnslabel_entry { char *v; off_t pos; };
1232 struct dnslabel_table {
1233 int n_labels; /* number of current entries */
1234 /* map from name to position in message */
1235 struct dnslabel_entry labels[MAX_LABELS];
1238 /* Initialize dnslabel_table. */
1240 dnslabel_table_init(struct dnslabel_table *table)
1242 table->n_labels = 0;
1245 /* Free all storage held by table, but not the table itself. */
1247 dnslabel_clear(struct dnslabel_table *table)
1250 for (i = 0; i < table->n_labels; ++i)
1251 free(table->labels[i].v);
1252 table->n_labels = 0;
1255 /* return the position of the label in the current message, or -1 if the label */
1256 /* hasn't been used yet. */
1258 dnslabel_table_get_pos(const struct dnslabel_table *table, const char *label)
1261 for (i = 0; i < table->n_labels; ++i) {
1262 if (!strcmp(label, table->labels[i].v))
1263 return table->labels[i].pos;
1268 /* remember that we've used the label at position pos */
1270 dnslabel_table_add(struct dnslabel_table *table, const char *label, off_t pos)
1274 if (table->n_labels == MAX_LABELS)
1279 p = table->n_labels++;
1280 table->labels[p].v = v;
1281 table->labels[p].pos = pos;
1286 /* Converts a string to a length-prefixed set of DNS labels, starting */
1287 /* at buf[j]. name and buf must not overlap. name_len should be the length */
1288 /* of name. table is optional, and is used for compression. */
1290 /* Input: abc.def */
1291 /* Output: <3>abc<3>def<0> */
1293 /* Returns the first index after the encoded name, or negative on error. */
1294 /* -1 label was > 63 bytes */
1295 /* -2 name too long to fit in buffer. */
1298 dnsname_to_labels(u8 *const buf, size_t buf_len, off_t j,
1299 const char *name, const int name_len,
1300 struct dnslabel_table *table) {
1301 const char *end = name + name_len;
1305 #define APPEND16(x) do { \
1306 if (j + 2 > (off_t)buf_len) \
1309 memcpy(buf + j, &_t, 2); \
1312 #define APPEND32(x) do { \
1313 if (j + 4 > (off_t)buf_len) \
1316 memcpy(buf + j, &_t32, 4); \
1320 if (name_len > 255) return -2;
1323 const char *const start = name;
1324 if (table && (ref = dnslabel_table_get_pos(table, name)) >= 0) {
1325 APPEND16(ref | 0xc000);
1328 name = strchr(name, '.');
1330 const unsigned int label_len = end - start;
1331 if (label_len > 63) return -1;
1332 if ((size_t)(j+label_len+1) > buf_len) return -2;
1333 if (table) dnslabel_table_add(table, start, j);
1334 buf[j++] = label_len;
1336 memcpy(buf + j, start, end - start);
1340 /* append length of the label. */
1341 const unsigned int label_len = name - start;
1342 if (label_len > 63) return -1;
1343 if ((size_t)(j+label_len+1) > buf_len) return -2;
1344 if (table) dnslabel_table_add(table, start, j);
1345 buf[j++] = label_len;
1347 memcpy(buf + j, start, name - start);
1349 /* hop over the '.' */
1354 /* the labels must be terminated by a 0. */
1355 /* It's possible that the name ended in a . */
1356 /* in which case the zero is already there */
1357 if (!j || buf[j-1]) buf[j++] = 0;
1363 /* Finds the length of a dns request for a DNS name of the given */
1364 /* length. The actual request may be smaller than the value returned */
1367 evdns_request_len(const int name_len) {
1368 return 96 + /* length of the DNS standard header */
1370 4; /* space for the resource type */
1373 /* build a dns request packet into buf. buf should be at least as long */
1374 /* as evdns_request_len told you it should be. */
1376 /* Returns the amount of space used. Negative on error. */
1378 evdns_request_data_build(const char *const name, const int name_len,
1379 const u16 trans_id, const u16 type, const u16 class,
1380 u8 *const buf, size_t buf_len) {
1381 off_t j = 0; /* current offset into buf */
1382 u16 _t; /* used by the macros */
1385 APPEND16(0x0100); /* standard query, recusion needed */
1386 APPEND16(1); /* one question */
1387 APPEND16(0); /* no answers */
1388 APPEND16(0); /* no authority */
1389 APPEND16(0); /* no additional */
1391 j = dnsname_to_labels(buf, buf_len, j, name, name_len, NULL);
1404 /* exported function */
1405 struct evdns_server_port *
1406 evdns_add_server_port(int socket, int is_tcp, evdns_request_callback_fn_type cb, void *user_data)
1408 struct evdns_server_port *port;
1409 if (!(port = malloc(sizeof(struct evdns_server_port))))
1411 memset(port, 0, sizeof(struct evdns_server_port));
1413 assert(!is_tcp); /* TCP sockets not yet implemented */
1414 port->socket = socket;
1418 port->user_callback = cb;
1419 port->user_data = user_data;
1420 port->pending_replies = NULL;
1422 event_set(&port->event, port->socket, EV_READ | EV_PERSIST,
1423 server_port_ready_callback, port);
1424 event_add(&port->event, NULL); /* check return. */
1428 /* exported function */
1430 evdns_close_server_port(struct evdns_server_port *port)
1432 if (--port->refcnt == 0)
1433 server_port_free(port);
1437 /* exported function */
1439 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)
1441 struct server_request *req = TO_SERVER_REQUEST(_req);
1442 struct server_reply_item **itemp, *item;
1445 if (req->response) /* have we already answered? */
1449 case EVDNS_ANSWER_SECTION:
1450 itemp = &req->answer;
1451 countp = &req->n_answer;
1453 case EVDNS_AUTHORITY_SECTION:
1454 itemp = &req->authority;
1455 countp = &req->n_authority;
1457 case EVDNS_ADDITIONAL_SECTION:
1458 itemp = &req->additional;
1459 countp = &req->n_additional;
1465 itemp = &((*itemp)->next);
1467 item = malloc(sizeof(struct server_reply_item));
1471 if (!(item->name = strdup(name))) {
1476 item->class = class;
1478 item->is_name = is_name != 0;
1482 if (item->is_name) {
1483 if (!(item->data = strdup(data))) {
1488 item->datalen = (u16)-1;
1490 if (!(item->data = malloc(datalen))) {
1495 item->datalen = datalen;
1496 memcpy(item->data, data, datalen);
1505 /* exported function */
1507 evdns_server_request_add_a_reply(struct evdns_server_request *req, const char *name, int n, void *addrs, int ttl)
1509 return evdns_server_request_add_reply(
1510 req, EVDNS_ANSWER_SECTION, name, TYPE_A, CLASS_INET,
1511 ttl, n*4, 0, addrs);
1514 /* exported function */
1516 evdns_server_request_add_aaaa_reply(struct evdns_server_request *req, const char *name, int n, void *addrs, int ttl)
1518 return evdns_server_request_add_reply(
1519 req, EVDNS_ANSWER_SECTION, name, TYPE_AAAA, CLASS_INET,
1520 ttl, n*16, 0, addrs);
1523 /* exported function */
1525 evdns_server_request_add_ptr_reply(struct evdns_server_request *req, struct in_addr *in, const char *inaddr_name, const char *hostname, int ttl)
1529 assert(in || inaddr_name);
1530 assert(!(in && inaddr_name));
1532 a = ntohl(in->s_addr);
1533 snprintf(buf, sizeof(buf), "%d.%d.%d.%d.in-addr.arpa",
1534 (int)(u8)((a )&0xff),
1535 (int)(u8)((a>>8 )&0xff),
1536 (int)(u8)((a>>16)&0xff),
1537 (int)(u8)((a>>24)&0xff));
1540 return evdns_server_request_add_reply(
1541 req, EVDNS_ANSWER_SECTION, inaddr_name, TYPE_PTR, CLASS_INET,
1542 ttl, -1, 1, hostname);
1545 /* exported function */
1547 evdns_server_request_add_cname_reply(struct evdns_server_request *req, const char *name, const char *cname, int ttl)
1549 return evdns_server_request_add_reply(
1550 req, EVDNS_ANSWER_SECTION, name, TYPE_A, CLASS_INET,
1556 evdns_server_request_format_response(struct server_request *req, int err)
1558 unsigned char buf[1500];
1559 size_t buf_len = sizeof(buf);
1565 struct dnslabel_table table;
1567 if (err < 0 || err > 15) return -1;
1569 /* Set response bit and error code; copy OPCODE and RD fields from
1570 * question; copy RA and AA if set by caller. */
1571 flags = req->base.flags;
1572 flags |= (0x8000 | err);
1574 dnslabel_table_init(&table);
1575 APPEND16(req->trans_id);
1577 APPEND16(req->base.nquestions);
1578 APPEND16(req->n_answer);
1579 APPEND16(req->n_authority);
1580 APPEND16(req->n_additional);
1582 /* Add questions. */
1583 for (i=0; i < req->base.nquestions; ++i) {
1584 const char *s = req->base.questions[i]->name;
1585 j = dnsname_to_labels(buf, buf_len, j, s, strlen(s), &table);
1587 dnslabel_clear(&table);
1590 APPEND16(req->base.questions[i]->type);
1591 APPEND16(req->base.questions[i]->class);
1594 /* Add answer, authority, and additional sections. */
1595 for (i=0; i<3; ++i) {
1596 struct server_reply_item *item;
1600 item = req->authority;
1602 item = req->additional;
1604 r = dnsname_to_labels(buf, buf_len, j, item->name, strlen(item->name), &table);
1609 APPEND16(item->type);
1610 APPEND16(item->class);
1611 APPEND32(item->ttl);
1612 if (item->is_name) {
1613 off_t len_idx = j, name_start;
1616 r = dnsname_to_labels(buf, buf_len, j, item->data, strlen(item->data), &table);
1620 _t = htons( (j-name_start) );
1621 memcpy(buf+len_idx, &_t, 2);
1623 APPEND16(item->datalen);
1624 if (j+item->datalen > (off_t)buf_len)
1626 memcpy(buf+j, item->data, item->datalen);
1636 buf[3] |= 0x02; /* set the truncated bit. */
1639 req->response_len = j;
1641 if (!(req->response = malloc(req->response_len))) {
1642 server_request_free_answers(req);
1643 dnslabel_clear(&table);
1646 memcpy(req->response, buf, req->response_len);
1647 server_request_free_answers(req);
1648 dnslabel_clear(&table);
1652 /* exported function */
1654 evdns_server_request_respond(struct evdns_server_request *_req, int err)
1656 struct server_request *req = TO_SERVER_REQUEST(_req);
1657 struct evdns_server_port *port = req->port;
1659 if (!req->response) {
1660 if ((r = evdns_server_request_format_response(req, err))<0)
1664 r = sendto(port->socket, req->response, req->response_len, 0,
1665 (struct sockaddr*) &req->addr, req->addrlen);
1667 int err = last_error(port->socket);
1668 if (! error_is_eagain(err))
1671 if (port->pending_replies) {
1672 req->prev_pending = port->pending_replies->prev_pending;
1673 req->next_pending = port->pending_replies;
1674 req->prev_pending->next_pending =
1675 req->next_pending->prev_pending = req;
1677 req->prev_pending = req->next_pending = req;
1678 port->pending_replies = req;
1681 (void) event_del(&port->event);
1682 event_set(&port->event, port->socket, (port->closing?0:EV_READ) | EV_WRITE | EV_PERSIST, server_port_ready_callback, port);
1684 if (event_add(&port->event, NULL) < 0) {
1685 log(EVDNS_LOG_WARN, "Error from libevent when adding event for DNS server");
1692 if (server_request_free(req))
1695 if (port->pending_replies)
1696 server_port_flush(port);
1701 /* Free all storage held by RRs in req. */
1703 server_request_free_answers(struct server_request *req)
1705 struct server_reply_item *victim, *next, **list;
1707 for (i = 0; i < 3; ++i) {
1709 list = &req->answer;
1711 list = &req->authority;
1713 list = &req->additional;
1717 next = victim->next;
1728 /* Free all storage held by req, and remove links to it. */
1729 /* return true iff we just wound up freeing the server_port. */
1731 server_request_free(struct server_request *req)
1734 if (req->base.questions) {
1735 for (i = 0; i < req->base.nquestions; ++i)
1736 free(req->base.questions[i]);
1737 free(req->base.questions);
1741 if (req->port->pending_replies == req) {
1742 if (req->next_pending)
1743 req->port->pending_replies = req->next_pending;
1745 req->port->pending_replies = NULL;
1747 rc = --req->port->refcnt;
1750 if (req->response) {
1751 free(req->response);
1754 server_request_free_answers(req);
1756 if (req->next_pending && req->next_pending != req) {
1757 req->next_pending->prev_pending = req->prev_pending;
1758 req->prev_pending->next_pending = req->next_pending;
1762 server_port_free(req->port);
1770 /* Free all storage held by an evdns_server_port. Only called when */
1772 server_port_free(struct evdns_server_port *port)
1775 assert(!port->refcnt);
1776 assert(!port->pending_replies);
1777 if (port->socket > 0) {
1778 CLOSE_SOCKET(port->socket);
1781 (void) event_del(&port->event);
1782 /* XXXX actually free the port? -NM */
1785 /* exported function */
1787 evdns_server_request_drop(struct evdns_server_request *_req)
1789 struct server_request *req = TO_SERVER_REQUEST(_req);
1790 server_request_free(req);
1794 /* exported function */
1796 evdns_server_request_get_requesting_addr(struct evdns_server_request *_req, struct sockaddr *sa, int addr_len)
1798 struct server_request *req = TO_SERVER_REQUEST(_req);
1799 if (addr_len < (int)req->addrlen)
1801 memcpy(sa, &(req->addr), req->addrlen);
1802 return req->addrlen;
1808 /* this is a libevent callback function which is called when a request */
1809 /* has timed out. */
1811 evdns_request_timeout_callback(int fd, short events, void *arg) {
1812 struct request *const req = (struct request *) arg;
1816 log(EVDNS_LOG_DEBUG, "Request %lx timed out", (unsigned long) arg);
1818 req->ns->timedout++;
1819 if (req->ns->timedout > global_max_nameserver_timeout) {
1820 req->ns->timedout = 0;
1821 nameserver_failed(req->ns, "request timed out.");
1824 (void) evtimer_del(&req->timeout_event);
1825 if (req->tx_count >= global_max_retransmits) {
1826 /* this request has failed */
1827 reply_callback(req, 0, DNS_ERR_TIMEOUT, NULL);
1828 request_finished(req, &req_head);
1831 evdns_request_transmit(req);
1835 /* try to send a request to a given server. */
1839 /* 1 temporary failure */
1840 /* 2 other failure */
1842 evdns_request_transmit_to(struct request *req, struct nameserver *server) {
1843 const int r = send(server->socket, req->request, req->request_len, 0);
1845 int err = last_error(server->socket);
1846 if (error_is_eagain(err)) return 1;
1847 nameserver_failed(req->ns, strerror(err));
1849 } else if (r != (int)req->request_len) {
1850 return 1; /* short write */
1856 /* try to send a request, updating the fields of the request */
1863 evdns_request_transmit(struct request *req) {
1866 /* if we fail to send this packet then this flag marks it */
1867 /* for evdns_transmit */
1868 req->transmit_me = 1;
1869 if (req->trans_id == 0xffff) abort();
1871 if (req->ns->choked) {
1872 /* don't bother trying to write to a socket */
1873 /* which we have had EAGAIN from */
1877 r = evdns_request_transmit_to(req, req->ns);
1881 req->ns->choked = 1;
1882 nameserver_write_waiting(req->ns, 1);
1885 /* failed in some other way */
1890 log(EVDNS_LOG_DEBUG,
1891 "Setting timeout for request %lx", (unsigned long) req);
1892 evtimer_set(&req->timeout_event, evdns_request_timeout_callback, req);
1893 if (evtimer_add(&req->timeout_event, &global_timeout) < 0) {
1895 "Error from libevent when adding timer for request %lx",
1896 (unsigned long) req);
1900 req->transmit_me = 0;
1906 nameserver_probe_callback(int result, char type, int count, int ttl, void *addresses, void *arg) {
1907 struct nameserver *const ns = (struct nameserver *) arg;
1913 if (result == DNS_ERR_NONE || result == DNS_ERR_NOTEXIST) {
1914 /* this is a good reply */
1916 } else nameserver_probe_failed(ns);
1920 nameserver_send_probe(struct nameserver *const ns) {
1921 struct request *req;
1922 /* here we need to send a probe to a given nameserver */
1923 /* in the hope that it is up now. */
1925 log(EVDNS_LOG_DEBUG, "Sending probe to %s", debug_ntoa(ns->address));
1927 req = request_new(TYPE_A, "www.google.com", DNS_QUERY_NO_SEARCH, nameserver_probe_callback, ns);
1929 /* we force this into the inflight queue no matter what */
1930 request_trans_id_set(req, transaction_id_pick());
1932 request_submit(req);
1936 /* 0 didn't try to transmit anything */
1937 /* 1 tried to transmit something */
1939 evdns_transmit(void) {
1940 char did_try_to_transmit = 0;
1943 struct request *const started_at = req_head, *req = req_head;
1944 /* first transmit all the requests which are currently waiting */
1946 if (req->transmit_me) {
1947 did_try_to_transmit = 1;
1948 evdns_request_transmit(req);
1952 } while (req != started_at);
1955 return did_try_to_transmit;
1958 /* exported function */
1960 evdns_count_nameservers(void)
1962 const struct nameserver *server = server_head;
1968 server = server->next;
1969 } while (server != server_head);
1973 /* exported function */
1975 evdns_clear_nameservers_and_suspend(void)
1977 struct nameserver *server = server_head, *started_at = server_head;
1978 struct request *req = req_head, *req_started_at = req_head;
1983 struct nameserver *next = server->next;
1984 (void) event_del(&server->event);
1985 (void) evtimer_del(&server->timeout_event);
1986 if (server->socket >= 0)
1987 CLOSE_SOCKET(server->socket);
1989 if (next == started_at)
1994 global_good_nameservers = 0;
1997 struct request *next = req->next;
1998 req->tx_count = req->reissue_count = 0;
2000 /* ???? What to do about searches? */
2001 (void) evtimer_del(&req->timeout_event);
2003 req->transmit_me = 0;
2005 global_requests_waiting++;
2006 evdns_request_insert(req, &req_waiting_head);
2007 /* We want to insert these suspended elements at the front of
2008 * the waiting queue, since they were pending before any of
2009 * the waiting entries were added. This is a circular list,
2010 * so we can just shift the start back by one.*/
2011 req_waiting_head = req_waiting_head->prev;
2013 if (next == req_started_at)
2018 global_requests_inflight = 0;
2024 /* exported function */
2028 evdns_requests_pump_waiting_queue();
2033 _evdns_nameserver_add_impl(unsigned long int address, int port) {
2034 /* first check to see if we already have this nameserver */
2036 const struct nameserver *server = server_head, *const started_at = server_head;
2037 struct nameserver *ns;
2038 struct sockaddr_in sin;
2042 if (server->address == address) return 3;
2043 server = server->next;
2044 } while (server != started_at);
2047 ns = (struct nameserver *) malloc(sizeof(struct nameserver));
2050 memset(ns, 0, sizeof(struct nameserver));
2052 ns->socket = socket(PF_INET, SOCK_DGRAM, 0);
2053 if (ns->socket < 0) { err = 1; goto out1; }
2056 u_long nonblocking = 1;
2057 ioctlsocket(ns->socket, FIONBIO, &nonblocking);
2060 fcntl(ns->socket, F_SETFL, O_NONBLOCK);
2062 sin.sin_addr.s_addr = address;
2063 sin.sin_port = htons(port);
2064 sin.sin_family = AF_INET;
2065 if (connect(ns->socket, (struct sockaddr *) &sin, sizeof(sin)) != 0) {
2070 ns->address = address;
2072 event_set(&ns->event, ns->socket, EV_READ | EV_PERSIST, nameserver_ready_callback, ns);
2073 if (event_add(&ns->event, NULL) < 0) {
2078 log(EVDNS_LOG_DEBUG, "Added nameserver %s", debug_ntoa(address));
2080 /* insert this nameserver into the list of them */
2082 ns->next = ns->prev = ns;
2085 ns->next = server_head->next;
2086 ns->prev = server_head;
2087 server_head->next = ns;
2088 if (server_head->prev == server_head) {
2089 server_head->prev = ns;
2093 global_good_nameservers++;
2098 CLOSE_SOCKET(ns->socket);
2101 log(EVDNS_LOG_WARN, "Unable to add nameserver %s: error %d", debug_ntoa(address), err);
2105 /* exported function */
2107 evdns_nameserver_add(unsigned long int address) {
2108 return _evdns_nameserver_add_impl(address, 53);
2111 /* exported function */
2113 evdns_nameserver_ip_add(const char *ip_as_string) {
2118 cp = strchr(ip_as_string, ':');
2123 port = strtoint(cp+1);
2124 if (port < 0 || port > 65535) {
2127 if ((cp-ip_as_string) >= (int)sizeof(buf)) {
2130 memcpy(buf, ip_as_string, cp-ip_as_string);
2131 buf[cp-ip_as_string] = '\0';
2134 if (!inet_aton(cp, &ina)) {
2137 return _evdns_nameserver_add_impl(ina.s_addr, port);
2140 /* insert into the tail of the queue */
2142 evdns_request_insert(struct request *req, struct request **head) {
2145 req->next = req->prev = req;
2149 req->prev = (*head)->prev;
2150 req->prev->next = req;
2152 (*head)->prev = req;
2156 string_num_dots(const char *s) {
2158 while ((s = strchr(s, '.'))) {
2165 static struct request *
2166 request_new(int type, const char *name, int flags,
2167 evdns_callback_type callback, void *user_ptr) {
2168 const char issuing_now =
2169 (global_requests_inflight < global_max_requests_inflight) ? 1 : 0;
2171 const int name_len = strlen(name);
2172 const int request_max_len = evdns_request_len(name_len);
2173 const u16 trans_id = issuing_now ? transaction_id_pick() : 0xffff;
2174 /* the request data is alloced in a single block with the header */
2175 struct request *const req =
2176 (struct request *) malloc(sizeof(struct request) + request_max_len);
2180 if (!req) return NULL;
2181 memset(req, 0, sizeof(struct request));
2183 /* request data lives just after the header */
2184 req->request = ((u8 *) req) + sizeof(struct request);
2185 /* denotes that the request data shouldn't be free()ed */
2186 req->request_appended = 1;
2187 rlen = evdns_request_data_build(name, name_len, trans_id,
2188 type, CLASS_INET, req->request, request_max_len);
2191 req->request_len = rlen;
2192 req->trans_id = trans_id;
2194 req->request_type = type;
2195 req->user_pointer = user_ptr;
2196 req->user_callback = callback;
2197 req->ns = issuing_now ? nameserver_pick() : NULL;
2198 req->next = req->prev = NULL;
2207 request_submit(struct request *const req) {
2209 /* if it has a nameserver assigned then this is going */
2210 /* straight into the inflight queue */
2211 evdns_request_insert(req, &req_head);
2212 global_requests_inflight++;
2213 evdns_request_transmit(req);
2215 evdns_request_insert(req, &req_waiting_head);
2216 global_requests_waiting++;
2220 /* exported function */
2221 int evdns_resolve_ipv4(const char *name, int flags,
2222 evdns_callback_type callback, void *ptr) {
2223 log(EVDNS_LOG_DEBUG, "Resolve requested for %s", name);
2224 if (flags & DNS_QUERY_NO_SEARCH) {
2225 struct request *const req =
2226 request_new(TYPE_A, name, flags, callback, ptr);
2229 request_submit(req);
2232 return (search_request_new(TYPE_A, name, flags, callback, ptr));
2236 /* exported function */
2237 int evdns_resolve_ipv6(const char *name, int flags,
2238 evdns_callback_type callback, void *ptr) {
2239 log(EVDNS_LOG_DEBUG, "Resolve requested for %s", name);
2240 if (flags & DNS_QUERY_NO_SEARCH) {
2241 struct request *const req =
2242 request_new(TYPE_AAAA, name, flags, callback, ptr);
2245 request_submit(req);
2248 return (search_request_new(TYPE_AAAA, name, flags, callback, ptr));
2252 int evdns_resolve_reverse(struct in_addr *in, int flags, evdns_callback_type callback, void *ptr) {
2254 struct request *req;
2257 a = ntohl(in->s_addr);
2258 snprintf(buf, sizeof(buf), "%d.%d.%d.%d.in-addr.arpa",
2259 (int)(u8)((a )&0xff),
2260 (int)(u8)((a>>8 )&0xff),
2261 (int)(u8)((a>>16)&0xff),
2262 (int)(u8)((a>>24)&0xff));
2263 log(EVDNS_LOG_DEBUG, "Resolve requested for %s (reverse)", buf);
2264 req = request_new(TYPE_PTR, buf, flags, callback, ptr);
2266 request_submit(req);
2270 int evdns_resolve_reverse_ipv6(struct in6_addr *in, int flags, evdns_callback_type callback, void *ptr) {
2273 struct request *req;
2277 for (i=15; i >= 0; --i) {
2278 u8 byte = in->s6_addr[i];
2279 *cp++ = "0123456789abcdef"[byte & 0x0f];
2281 *cp++ = "0123456789abcdef"[byte >> 4];
2284 assert(cp + strlen(".ip6.arpa") < buf+sizeof(buf));
2285 memcpy(cp, ".ip6.arpa", strlen(".ip6.arpa")+1);
2286 log(EVDNS_LOG_DEBUG, "Resolve requested for %s (reverse)", buf);
2287 req = request_new(TYPE_PTR, buf, flags, callback, ptr);
2289 request_submit(req);
2293 /*/////////////////////////////////////////////////////////////////// */
2294 /* Search support */
2296 /* the libc resolver has support for searching a number of domains */
2297 /* to find a name. If nothing else then it takes the single domain */
2298 /* from the gethostname() call. */
2300 /* It can also be configured via the domain and search options in a */
2303 /* The ndots option controls how many dots it takes for the resolver */
2304 /* to decide that a name is non-local and so try a raw lookup first. */
2306 struct search_domain {
2308 struct search_domain *next;
2309 /* the text string is appended to this structure */
2312 struct search_state {
2316 struct search_domain *head;
2319 static struct search_state *global_search_state = NULL;
2322 search_state_decref(struct search_state *const state) {
2325 if (!state->refcount) {
2326 struct search_domain *next, *dom;
2327 for (dom = state->head; dom; dom = next) {
2335 static struct search_state *
2336 search_state_new(void) {
2337 struct search_state *state = (struct search_state *) malloc(sizeof(struct search_state));
2338 if (!state) return NULL;
2339 memset(state, 0, sizeof(struct search_state));
2340 state->refcount = 1;
2347 search_postfix_clear(void) {
2348 search_state_decref(global_search_state);
2350 global_search_state = search_state_new();
2353 /* exported function */
2355 evdns_search_clear(void) {
2356 search_postfix_clear();
2360 search_postfix_add(const char *domain) {
2362 struct search_domain *sdomain;
2363 while (domain[0] == '.') domain++;
2364 domain_len = strlen(domain);
2366 if (!global_search_state) global_search_state = search_state_new();
2367 if (!global_search_state) return;
2368 global_search_state->num_domains++;
2370 sdomain = (struct search_domain *) malloc(sizeof(struct search_domain) + domain_len);
2371 if (!sdomain) return;
2372 memcpy( ((u8 *) sdomain) + sizeof(struct search_domain), domain, domain_len);
2373 sdomain->next = global_search_state->head;
2374 sdomain->len = domain_len;
2376 global_search_state->head = sdomain;
2379 /* reverse the order of members in the postfix list. This is needed because, */
2380 /* when parsing resolv.conf we push elements in the wrong order */
2382 search_reverse(void) {
2383 struct search_domain *cur, *prev = NULL, *next;
2384 cur = global_search_state->head;
2392 global_search_state->head = prev;
2395 /* exported function */
2397 evdns_search_add(const char *domain) {
2398 search_postfix_add(domain);
2401 /* exported function */
2403 evdns_search_ndots_set(const int ndots) {
2404 if (!global_search_state) global_search_state = search_state_new();
2405 if (!global_search_state) return;
2406 global_search_state->ndots = ndots;
2410 search_set_from_hostname(void) {
2411 char hostname[HOST_NAME_MAX + 1], *domainname;
2413 search_postfix_clear();
2414 if (gethostname(hostname, sizeof(hostname))) return;
2415 domainname = strchr(hostname, '.');
2416 if (!domainname) return;
2417 search_postfix_add(domainname);
2420 /* warning: returns malloced string */
2422 search_make_new(const struct search_state *const state, int n, const char *const base_name) {
2423 const int base_len = strlen(base_name);
2424 const char need_to_append_dot = base_name[base_len - 1] == '.' ? 0 : 1;
2425 struct search_domain *dom;
2427 for (dom = state->head; dom; dom = dom->next) {
2429 /* this is the postfix we want */
2430 /* the actual postfix string is kept at the end of the structure */
2431 const u8 *const postfix = ((u8 *) dom) + sizeof(struct search_domain);
2432 const int postfix_len = dom->len;
2433 char *const newname = (char *) malloc(base_len + need_to_append_dot + postfix_len + 1);
2434 if (!newname) return NULL;
2435 memcpy(newname, base_name, base_len);
2436 if (need_to_append_dot) newname[base_len] = '.';
2437 memcpy(newname + base_len + need_to_append_dot, postfix, postfix_len);
2438 newname[base_len + need_to_append_dot + postfix_len] = 0;
2443 /* we ran off the end of the list and still didn't find the requested string */
2445 return NULL; /* unreachable; stops warnings in some compilers. */
2449 search_request_new(int type, const char *const name, int flags, evdns_callback_type user_callback, void *user_arg) {
2450 assert(type == TYPE_A || type == TYPE_AAAA);
2451 if ( ((flags & DNS_QUERY_NO_SEARCH) == 0) &&
2452 global_search_state &&
2453 global_search_state->num_domains) {
2454 /* we have some domains to search */
2455 struct request *req;
2456 if (string_num_dots(name) >= global_search_state->ndots) {
2457 req = request_new(type, name, flags, user_callback, user_arg);
2459 req->search_index = -1;
2461 char *const new_name = search_make_new(global_search_state, 0, name);
2462 if (!new_name) return 1;
2463 req = request_new(type, new_name, flags, user_callback, user_arg);
2466 req->search_index = 0;
2468 req->search_origname = strdup(name);
2469 req->search_state = global_search_state;
2470 req->search_flags = flags;
2471 global_search_state->refcount++;
2472 request_submit(req);
2475 struct request *const req = request_new(type, name, flags, user_callback, user_arg);
2477 request_submit(req);
2482 /* this is called when a request has failed to find a name. We need to check */
2483 /* if it is part of a search and, if so, try the next name in the list */
2485 /* 0 another request has been submitted */
2486 /* 1 no more requests needed */
2488 search_try_next(struct request *const req) {
2489 if (req->search_state) {
2490 /* it is part of a search */
2492 struct request *newreq;
2493 req->search_index++;
2494 if (req->search_index >= req->search_state->num_domains) {
2495 /* no more postfixes to try, however we may need to try */
2496 /* this name without a postfix */
2497 if (string_num_dots(req->search_origname) < req->search_state->ndots) {
2498 /* yep, we need to try it raw */
2499 struct request *const newreq = request_new(req->request_type, req->search_origname, req->search_flags, req->user_callback, req->user_pointer);
2500 log(EVDNS_LOG_DEBUG, "Search: trying raw query %s", req->search_origname);
2502 request_submit(newreq);
2509 new_name = search_make_new(req->search_state, req->search_index, req->search_origname);
2510 if (!new_name) return 1;
2511 log(EVDNS_LOG_DEBUG, "Search: now trying %s (%d)", new_name, req->search_index);
2512 newreq = request_new(req->request_type, new_name, req->search_flags, req->user_callback, req->user_pointer);
2514 if (!newreq) return 1;
2515 newreq->search_origname = req->search_origname;
2516 req->search_origname = NULL;
2517 newreq->search_state = req->search_state;
2518 newreq->search_flags = req->search_flags;
2519 newreq->search_index = req->search_index;
2520 newreq->search_state->refcount++;
2521 request_submit(newreq);
2528 search_request_finished(struct request *const req) {
2529 if (req->search_state) {
2530 search_state_decref(req->search_state);
2531 req->search_state = NULL;
2533 if (req->search_origname) {
2534 free(req->search_origname);
2535 req->search_origname = NULL;
2539 /*/////////////////////////////////////////////////////////////////// */
2540 /* Parsing resolv.conf files */
2543 evdns_resolv_set_defaults(int flags) {
2544 /* if the file isn't found then we assume a local resolver */
2545 if (flags & DNS_OPTION_SEARCH) search_set_from_hostname();
2546 if (flags & DNS_OPTION_NAMESERVERS) evdns_nameserver_ip_add("127.0.0.1");
2549 #ifndef HAVE_STRTOK_R
2551 strtok_r(char *s, const char *delim, char **state) {
2552 return strtok(s, delim);
2556 /* helper version of atoi which returns -1 on error */
2558 strtoint(const char *const str) {
2560 const int r = strtol(str, &endptr, 10);
2561 if (*endptr) return -1;
2565 /* helper version of atoi that returns -1 on error and clips to bounds. */
2567 strtoint_clipped(const char *const str, int min, int max)
2569 int r = strtoint(str);
2580 /* exported function */
2582 evdns_set_option(const char *option, const char *val, int flags)
2584 if (!strncmp(option, "ndots:", 6)) {
2585 const int ndots = strtoint(val);
2586 if (ndots == -1) return -1;
2587 if (!(flags & DNS_OPTION_SEARCH)) return 0;
2588 log(EVDNS_LOG_DEBUG, "Setting ndots to %d", ndots);
2589 if (!global_search_state) global_search_state = search_state_new();
2590 if (!global_search_state) return -1;
2591 global_search_state->ndots = ndots;
2592 } else if (!strncmp(option, "timeout:", 8)) {
2593 const int timeout = strtoint(val);
2594 if (timeout == -1) return -1;
2595 if (!(flags & DNS_OPTION_MISC)) return 0;
2596 log(EVDNS_LOG_DEBUG, "Setting timeout to %d", timeout);
2597 global_timeout.tv_sec = timeout;
2598 } else if (!strncmp(option, "max-timeouts:", 12)) {
2599 const int maxtimeout = strtoint_clipped(val, 1, 255);
2600 if (maxtimeout == -1) return -1;
2601 if (!(flags & DNS_OPTION_MISC)) return 0;
2602 log(EVDNS_LOG_DEBUG, "Setting maximum allowed timeouts to %d",
2604 global_max_nameserver_timeout = maxtimeout;
2605 } else if (!strncmp(option, "max-inflight:", 13)) {
2606 const int maxinflight = strtoint_clipped(val, 1, 65000);
2607 if (maxinflight == -1) return -1;
2608 if (!(flags & DNS_OPTION_MISC)) return 0;
2609 log(EVDNS_LOG_DEBUG, "Setting maximum inflight requests to %d",
2611 global_max_requests_inflight = maxinflight;
2612 } else if (!strncmp(option, "attempts:", 9)) {
2613 int retries = strtoint(val);
2614 if (retries == -1) return -1;
2615 if (retries > 255) retries = 255;
2616 if (!(flags & DNS_OPTION_MISC)) return 0;
2617 log(EVDNS_LOG_DEBUG, "Setting retries to %d", retries);
2618 global_max_retransmits = retries;
2624 resolv_conf_parse_line(char *const start, int flags) {
2626 static const char *const delims = " \t";
2627 #define NEXT_TOKEN strtok_r(NULL, delims, &strtok_state)
2629 char *const first_token = strtok_r(start, delims, &strtok_state);
2630 if (!first_token) return;
2632 if (!strcmp(first_token, "nameserver") && (flags & DNS_OPTION_NAMESERVERS)) {
2633 const char *const nameserver = NEXT_TOKEN;
2636 if (inet_aton(nameserver, &ina)) {
2637 /* address is valid */
2638 evdns_nameserver_add(ina.s_addr);
2640 } else if (!strcmp(first_token, "domain") && (flags & DNS_OPTION_SEARCH)) {
2641 const char *const domain = NEXT_TOKEN;
2643 search_postfix_clear();
2644 search_postfix_add(domain);
2646 } else if (!strcmp(first_token, "search") && (flags & DNS_OPTION_SEARCH)) {
2648 search_postfix_clear();
2650 while ((domain = NEXT_TOKEN)) {
2651 search_postfix_add(domain);
2654 } else if (!strcmp(first_token, "options")) {
2656 while ((option = NEXT_TOKEN)) {
2657 const char *val = strchr(option, ':');
2658 evdns_set_option(option, val ? val+1 : "", flags);
2664 /* exported function */
2667 /* 1 failed to open file */
2668 /* 2 failed to stat file */
2669 /* 3 file too large */
2670 /* 4 out of memory */
2671 /* 5 short read from file */
2673 evdns_resolv_conf_parse(int flags, const char *const filename) {
2680 log(EVDNS_LOG_DEBUG, "Parsing resolv.conf file %s", filename);
2682 fd = open(filename, O_RDONLY);
2684 evdns_resolv_set_defaults(flags);
2688 if (fstat(fd, &st)) { err = 2; goto out1; }
2690 evdns_resolv_set_defaults(flags);
2691 err = (flags & DNS_OPTION_NAMESERVERS) ? 6 : 0;
2694 if (st.st_size > 65535) { err = 3; goto out1; } /* no resolv.conf should be any bigger */
2696 resolv = (u8 *) malloc((size_t)st.st_size + 1);
2697 if (!resolv) { err = 4; goto out1; }
2700 while ((r = read(fd, resolv+n, (size_t)st.st_size-n)) > 0) {
2702 if (n == st.st_size)
2704 assert(n < st.st_size);
2706 if (r < 0) { err = 5; goto out2; }
2707 resolv[n] = 0; /* we malloced an extra byte; this should be fine. */
2709 start = (char *) resolv;
2711 char *const newline = strchr(start, '\n');
2713 resolv_conf_parse_line(start, flags);
2717 resolv_conf_parse_line(start, flags);
2718 start = newline + 1;
2722 if (!server_head && (flags & DNS_OPTION_NAMESERVERS)) {
2723 /* no nameservers were configured. */
2724 evdns_nameserver_ip_add("127.0.0.1");
2727 if (flags & DNS_OPTION_SEARCH && (!global_search_state || global_search_state->num_domains == 0)) {
2728 search_set_from_hostname();
2739 /* Add multiple nameservers from a space-or-comma-separated list. */
2741 evdns_nameserver_ip_add_line(const char *ips) {
2746 while (ISSPACE(*ips) || *ips == ',' || *ips == '\t')
2749 while (ISDIGIT(*ips) || *ips == '.' || *ips == ':')
2751 buf = malloc(ips-addr+1);
2753 memcpy(buf, addr, ips-addr);
2754 buf[ips-addr] = '\0';
2755 r = evdns_nameserver_ip_add(buf);
2762 typedef DWORD(WINAPI *GetNetworkParams_fn_t)(FIXED_INFO *, DWORD*);
2764 /* Use the windows GetNetworkParams interface in iphlpapi.dll to */
2765 /* figure out what our nameservers are. */
2767 load_nameservers_with_getnetworkparams(void)
2769 /* Based on MSDN examples and inspection of c-ares code. */
2772 ULONG size = sizeof(FIXED_INFO);
2774 int status = 0, r, added_any;
2776 GetNetworkParams_fn_t fn;
2778 if (!(handle = LoadLibrary("iphlpapi.dll"))) {
2779 log(EVDNS_LOG_WARN, "Could not open iphlpapi.dll");
2783 if (!(fn = (GetNetworkParams_fn_t) GetProcAddress(handle, "GetNetworkParams"))) {
2784 log(EVDNS_LOG_WARN, "Could not get address of function.");
2790 if (!buf) { status = 4; goto done; }
2792 r = fn(fixed, &size);
2793 if (r != ERROR_SUCCESS && r != ERROR_BUFFER_OVERFLOW) {
2797 if (r != ERROR_SUCCESS) {
2800 if (!buf) { status = 4; goto done; }
2802 r = fn(fixed, &size);
2803 if (r != ERROR_SUCCESS) {
2804 log(EVDNS_LOG_DEBUG, "fn() failed.");
2812 ns = &(fixed->DnsServerList);
2814 r = evdns_nameserver_ip_add_line(ns->IpAddress.String);
2816 log(EVDNS_LOG_DEBUG,"Could not add nameserver %s to list,error: %d",
2817 (ns->IpAddress.String),(int)GetLastError());
2821 log(EVDNS_LOG_DEBUG,"Succesfully added %s as nameserver",ns->IpAddress.String);
2829 log(EVDNS_LOG_DEBUG, "No nameservers added.");
2837 FreeLibrary(handle);
2842 config_nameserver_from_reg_key(HKEY key, const char *subkey)
2845 DWORD bufsz = 0, type = 0;
2848 if (RegQueryValueEx(key, subkey, 0, &type, NULL, &bufsz)
2851 if (!(buf = malloc(bufsz)))
2854 if (RegQueryValueEx(key, subkey, 0, &type, (LPBYTE)buf, &bufsz)
2855 == ERROR_SUCCESS && bufsz > 1) {
2856 status = evdns_nameserver_ip_add_line(buf);
2863 #define SERVICES_KEY "System\\CurrentControlSet\\Services\\"
2864 #define WIN_NS_9X_KEY SERVICES_KEY "VxD\\MSTCP"
2865 #define WIN_NS_NT_KEY SERVICES_KEY "Tcpip\\Parameters"
2868 load_nameservers_from_registry(void)
2872 #define TRY(k, name) \
2873 if (!found && config_nameserver_from_reg_key(k,name) == 0) { \
2874 log(EVDNS_LOG_DEBUG,"Found nameservers in %s/%s",#k,name); \
2876 } else if (!found) { \
2877 log(EVDNS_LOG_DEBUG,"Didn't find nameservers in %s/%s", \
2881 if (((int)GetVersion()) > 0) { /* NT */
2882 HKEY nt_key = 0, interfaces_key = 0;
2884 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, WIN_NS_NT_KEY, 0,
2885 KEY_READ, &nt_key) != ERROR_SUCCESS) {
2886 log(EVDNS_LOG_DEBUG,"Couldn't open nt key, %d",(int)GetLastError());
2889 r = RegOpenKeyEx(nt_key, "Interfaces", 0,
2890 KEY_QUERY_VALUE|KEY_ENUMERATE_SUB_KEYS,
2892 if (r != ERROR_SUCCESS) {
2893 log(EVDNS_LOG_DEBUG,"Couldn't open interfaces key, %d",(int)GetLastError());
2896 TRY(nt_key, "NameServer");
2897 TRY(nt_key, "DhcpNameServer");
2898 TRY(interfaces_key, "NameServer");
2899 TRY(interfaces_key, "DhcpNameServer");
2900 RegCloseKey(interfaces_key);
2901 RegCloseKey(nt_key);
2904 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, WIN_NS_9X_KEY, 0,
2905 KEY_READ, &win_key) != ERROR_SUCCESS) {
2906 log(EVDNS_LOG_DEBUG, "Couldn't open registry key, %d", (int)GetLastError());
2909 TRY(win_key, "NameServer");
2910 RegCloseKey(win_key);
2914 log(EVDNS_LOG_WARN,"Didn't find any nameservers.");
2917 return found ? 0 : -1;
2922 evdns_config_windows_nameservers(void)
2924 if (load_nameservers_with_getnetworkparams() == 0)
2926 return load_nameservers_from_registry();
2935 evdns_config_windows_nameservers();
2937 res = evdns_resolv_conf_parse(DNS_OPTIONS_ALL, "/etc/resolv.conf");
2944 evdns_err_to_string(int err)
2947 case DNS_ERR_NONE: return "no error";
2948 case DNS_ERR_FORMAT: return "misformatted query";
2949 case DNS_ERR_SERVERFAILED: return "server failed";
2950 case DNS_ERR_NOTEXIST: return "name does not exist";
2951 case DNS_ERR_NOTIMPL: return "query not implemented";
2952 case DNS_ERR_REFUSED: return "refused";
2954 case DNS_ERR_TRUNCATED: return "reply truncated or ill-formed";
2955 case DNS_ERR_UNKNOWN: return "unknown";
2956 case DNS_ERR_TIMEOUT: return "request timed out";
2957 case DNS_ERR_SHUTDOWN: return "dns subsystem shut down";
2958 default: return "[Unknown error code]";
2963 evdns_shutdown(int fail_requests)
2965 struct nameserver *server, *server_next;
2966 struct search_domain *dom, *dom_next;
2970 reply_callback(req_head, 0, DNS_ERR_SHUTDOWN, NULL);
2971 request_finished(req_head, &req_head);
2973 while (req_waiting_head) {
2975 reply_callback(req_waiting_head, 0, DNS_ERR_SHUTDOWN, NULL);
2976 request_finished(req_waiting_head, &req_waiting_head);
2978 global_requests_inflight = global_requests_waiting = 0;
2980 for (server = server_head; server; server = server_next) {
2981 server_next = server->next;
2982 if (server->socket >= 0)
2983 CLOSE_SOCKET(server->socket);
2984 (void) event_del(&server->event);
2985 if (server->state == 0)
2986 (void) event_del(&server->timeout_event);
2988 if (server_next == server_head)
2992 global_good_nameservers = 0;
2994 if (global_search_state) {
2995 for (dom = global_search_state->head; dom; dom = dom_next) {
2996 dom_next = dom->next;
2999 free(global_search_state);
3000 global_search_state = NULL;
3002 evdns_log_fn = NULL;
3007 main_callback(int result, char type, int count, int ttl,
3008 void *addrs, void *orig) {
3009 char *n = (char*)orig;
3011 for (i = 0; i < count; ++i) {
3012 if (type == DNS_IPv4_A) {
3013 printf("%s: %s\n", n, debug_ntoa(((u32*)addrs)[i]));
3014 } else if (type == DNS_PTR) {
3015 printf("%s: %s\n", n, ((char**)addrs)[i]);
3019 printf("%s: No answer (%d)\n", n, result);
3024 evdns_server_callback(struct evdns_server_request *req, void *data)
3028 /* dummy; give 192.168.11.11 as an answer for all A questions,
3029 * give foo.bar.example.com as an answer for all PTR questions. */
3030 for (i = 0; i < req->nquestions; ++i) {
3031 u32 ans = htonl(0xc0a80b0bUL);
3032 if (req->questions[i]->type == EVDNS_TYPE_A &&
3033 req->questions[i]->class == EVDNS_CLASS_INET) {
3034 printf(" -- replying for %s (A)\n", req->questions[i]->name);
3035 r = evdns_server_request_add_a_reply(req, req->questions[i]->name,
3038 printf("eeep, didn't work.\n");
3039 } else if (req->questions[i]->type == EVDNS_TYPE_PTR &&
3040 req->questions[i]->class == EVDNS_CLASS_INET) {
3041 printf(" -- replying for %s (PTR)\n", req->questions[i]->name);
3042 r = evdns_server_request_add_ptr_reply(req, NULL, req->questions[i]->name,
3043 "foo.bar.example.com", 10);
3045 printf(" -- skipping %s [%d %d]\n", req->questions[i]->name,
3046 req->questions[i]->type, req->questions[i]->class);
3050 r = evdns_request_respond(req, 0);
3052 printf("eeek, couldn't send reply.\n");
3056 logfn(int is_warn, const char *msg) {
3058 fprintf(stderr, "%s\n", msg);
3061 main(int c, char **v) {
3063 int reverse = 0, verbose = 1, servertest = 0;
3065 fprintf(stderr, "syntax: %s [-x] [-v] hostname\n", v[0]);
3066 fprintf(stderr, "syntax: %s [-servertest]\n", v[0]);
3070 while (idx < c && v[idx][0] == '-') {
3071 if (!strcmp(v[idx], "-x"))
3073 else if (!strcmp(v[idx], "-v"))
3075 else if (!strcmp(v[idx], "-servertest"))
3078 fprintf(stderr, "Unknown option %s\n", v[idx]);
3083 evdns_set_log_fn(logfn);
3084 evdns_resolv_conf_parse(DNS_OPTION_NAMESERVERS, "/etc/resolv.conf");
3087 struct sockaddr_in my_addr;
3088 sock = socket(PF_INET, SOCK_DGRAM, 0);
3089 fcntl(sock, F_SETFL, O_NONBLOCK);
3090 my_addr.sin_family = AF_INET;
3091 my_addr.sin_port = htons(10053);
3092 my_addr.sin_addr.s_addr = INADDR_ANY;
3093 if (bind(sock, (struct sockaddr*)&my_addr, sizeof(my_addr))<0) {
3097 evdns_add_server_port(sock, 0, evdns_server_callback, NULL);
3099 for (; idx < c; ++idx) {
3101 struct in_addr addr;
3102 if (!inet_aton(v[idx], &addr)) {
3103 fprintf(stderr, "Skipping non-IP %s\n", v[idx]);
3106 fprintf(stderr, "resolving %s...\n",v[idx]);
3107 evdns_resolve_reverse(&addr, 0, main_callback, v[idx]);
3109 fprintf(stderr, "resolving (fwd) %s...\n",v[idx]);
3110 evdns_resolve_ipv4(v[idx], 0, main_callback, v[idx]);