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