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