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