1
2 /* Copyright 1998 by the Massachusetts Institute of Technology.
3 * Copyright (C) 2004-2017 by Daniel Stenberg
4 *
5 * Permission to use, copy, modify, and distribute this
6 * software and its documentation for any purpose and without
7 * fee is hereby granted, provided that the above copyright
8 * notice appear in all copies and that both that copyright
9 * notice and this permission notice appear in supporting
10 * documentation, and that the name of M.I.T. not be used in
11 * advertising or publicity pertaining to distribution of the
12 * software without specific, written prior permission.
13 * M.I.T. makes no representations about the suitability of
14 * this software for any purpose. It is provided "as is"
15 * without express or implied warranty.
16 */
17
18 #include "ares_setup.h"
19
20 #ifdef HAVE_SYS_UIO_H
21 # include <sys/uio.h>
22 #endif
23 #ifdef HAVE_NETINET_IN_H
24 # include <netinet/in.h>
25 #endif
26 #ifdef HAVE_NETINET_TCP_H
27 # include <netinet/tcp.h>
28 #endif
29 #ifdef HAVE_NETDB_H
30 # include <netdb.h>
31 #endif
32 #ifdef HAVE_ARPA_INET_H
33 # include <arpa/inet.h>
34 #endif
35 #ifdef HAVE_ARPA_NAMESER_H
36 # include <arpa/nameser.h>
37 #else
38 # include "nameser.h"
39 #endif
40 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
41 # include <arpa/nameser_compat.h>
42 #endif
43
44 #ifdef HAVE_STRINGS_H
45 # include <strings.h>
46 #endif
47 #ifdef HAVE_SYS_IOCTL_H
48 # include <sys/ioctl.h>
49 #endif
50 #ifdef NETWARE
51 # include <sys/filio.h>
52 #endif
53
54 #include <assert.h>
55 #include <fcntl.h>
56 #include <limits.h>
57
58 #include "ares.h"
59 #include "ares_dns.h"
60 #include "ares_nowarn.h"
61 #include "ares_private.h"
62
63
64 static int try_again(int errnum);
65 static void write_tcp_data(ares_channel channel, fd_set *write_fds,
66 ares_socket_t write_fd, struct timeval *now);
67 static void read_tcp_data(ares_channel channel, fd_set *read_fds,
68 ares_socket_t read_fd, struct timeval *now);
69 static void read_udp_packets(ares_channel channel, fd_set *read_fds,
70 ares_socket_t read_fd, struct timeval *now);
71 static void advance_tcp_send_queue(ares_channel channel, int whichserver,
72 ares_ssize_t num_bytes);
73 static void process_timeouts(ares_channel channel, struct timeval *now);
74 static void process_broken_connections(ares_channel channel,
75 struct timeval *now);
76 static void process_answer(ares_channel channel, unsigned char *abuf,
77 int alen, int whichserver, int tcp,
78 struct timeval *now);
79 static void handle_error(ares_channel channel, int whichserver,
80 struct timeval *now);
81 static void skip_server(ares_channel channel, struct query *query,
82 int whichserver);
83 static void next_server(ares_channel channel, struct query *query,
84 struct timeval *now);
85 static int open_tcp_socket(ares_channel channel, struct server_state *server);
86 static int open_udp_socket(ares_channel channel, struct server_state *server);
87 static int same_questions(const unsigned char *qbuf, int qlen,
88 const unsigned char *abuf, int alen);
89 static int same_address(struct sockaddr *sa, struct ares_addr *aa);
90 static void end_query(ares_channel channel, struct query *query, int status,
91 unsigned char *abuf, int alen);
92
93 /* return true if now is exactly check time or later */
ares__timedout(struct timeval * now,struct timeval * check)94 int ares__timedout(struct timeval *now,
95 struct timeval *check)
96 {
97 long secs = (now->tv_sec - check->tv_sec);
98
99 if(secs > 0)
100 return 1; /* yes, timed out */
101 if(secs < 0)
102 return 0; /* nope, not timed out */
103
104 /* if the full seconds were identical, check the sub second parts */
105 return (now->tv_usec - check->tv_usec >= 0);
106 }
107
108 /* add the specific number of milliseconds to the time in the first argument */
timeadd(struct timeval * now,int millisecs)109 static void timeadd(struct timeval *now, int millisecs)
110 {
111 now->tv_sec += millisecs/1000;
112 now->tv_usec += (millisecs%1000)*1000;
113
114 if(now->tv_usec >= 1000000) {
115 ++(now->tv_sec);
116 now->tv_usec -= 1000000;
117 }
118 }
119
120 /*
121 * generic process function
122 */
processfds(ares_channel channel,fd_set * read_fds,ares_socket_t read_fd,fd_set * write_fds,ares_socket_t write_fd)123 static void processfds(ares_channel channel,
124 fd_set *read_fds, ares_socket_t read_fd,
125 fd_set *write_fds, ares_socket_t write_fd)
126 {
127 struct timeval now = ares__tvnow();
128
129 write_tcp_data(channel, write_fds, write_fd, &now);
130 read_tcp_data(channel, read_fds, read_fd, &now);
131 read_udp_packets(channel, read_fds, read_fd, &now);
132 process_timeouts(channel, &now);
133 process_broken_connections(channel, &now);
134 }
135
136 /* Something interesting happened on the wire, or there was a timeout.
137 * See what's up and respond accordingly.
138 */
ares_process(ares_channel channel,fd_set * read_fds,fd_set * write_fds)139 void ares_process(ares_channel channel, fd_set *read_fds, fd_set *write_fds)
140 {
141 processfds(channel, read_fds, ARES_SOCKET_BAD, write_fds, ARES_SOCKET_BAD);
142 }
143
144 /* Something interesting happened on the wire, or there was a timeout.
145 * See what's up and respond accordingly.
146 */
ares_process_fd(ares_channel channel,ares_socket_t read_fd,ares_socket_t write_fd)147 void ares_process_fd(ares_channel channel,
148 ares_socket_t read_fd, /* use ARES_SOCKET_BAD or valid
149 file descriptors */
150 ares_socket_t write_fd)
151 {
152 processfds(channel, NULL, read_fd, NULL, write_fd);
153 }
154
155
156 /* Return 1 if the specified error number describes a readiness error, or 0
157 * otherwise. This is mostly for HP-UX, which could return EAGAIN or
158 * EWOULDBLOCK. See this man page
159 *
160 * http://devrsrc1.external.hp.com/STKS/cgi-bin/man2html?
161 * manpage=/usr/share/man/man2.Z/send.2
162 */
try_again(int errnum)163 static int try_again(int errnum)
164 {
165 #if !defined EWOULDBLOCK && !defined EAGAIN
166 #error "Neither EWOULDBLOCK nor EAGAIN defined"
167 #endif
168 switch (errnum)
169 {
170 #ifdef EWOULDBLOCK
171 case EWOULDBLOCK:
172 return 1;
173 #endif
174 #if defined EAGAIN && EAGAIN != EWOULDBLOCK
175 case EAGAIN:
176 return 1;
177 #endif
178 }
179 return 0;
180 }
181
socket_writev(ares_channel channel,ares_socket_t s,const struct iovec * vec,int len)182 static ares_ssize_t socket_writev(ares_channel channel, ares_socket_t s, const struct iovec * vec, int len)
183 {
184 if (channel->sock_funcs)
185 return channel->sock_funcs->asendv(s, vec, len, channel->sock_func_cb_data);
186
187 return writev(s, vec, len);
188 }
189
socket_write(ares_channel channel,ares_socket_t s,const void * data,size_t len)190 static ares_ssize_t socket_write(ares_channel channel, ares_socket_t s, const void * data, size_t len)
191 {
192 if (channel->sock_funcs)
193 {
194 struct iovec vec;
195 vec.iov_base = (void*)data;
196 vec.iov_len = len;
197 return channel->sock_funcs->asendv(s, &vec, 1, channel->sock_func_cb_data);
198 }
199 return swrite(s, data, len);
200 }
201
202 /* If any TCP sockets select true for writing, write out queued data
203 * we have for them.
204 */
write_tcp_data(ares_channel channel,fd_set * write_fds,ares_socket_t write_fd,struct timeval * now)205 static void write_tcp_data(ares_channel channel,
206 fd_set *write_fds,
207 ares_socket_t write_fd,
208 struct timeval *now)
209 {
210 struct server_state *server;
211 struct send_request *sendreq;
212 struct iovec *vec;
213 int i;
214 ares_ssize_t scount;
215 ares_ssize_t wcount;
216 size_t n;
217
218 if(!write_fds && (write_fd == ARES_SOCKET_BAD))
219 /* no possible action */
220 return;
221
222 for (i = 0; i < channel->nservers; i++)
223 {
224 /* Make sure server has data to send and is selected in write_fds or
225 write_fd. */
226 server = &channel->servers[i];
227 if (!server->qhead || server->tcp_socket == ARES_SOCKET_BAD ||
228 server->is_broken)
229 continue;
230
231 if(write_fds) {
232 if(!FD_ISSET(server->tcp_socket, write_fds))
233 continue;
234 }
235 else {
236 if(server->tcp_socket != write_fd)
237 continue;
238 }
239
240 if(write_fds)
241 /* If there's an error and we close this socket, then open
242 * another with the same fd to talk to another server, then we
243 * don't want to think that it was the new socket that was
244 * ready. This is not disastrous, but is likely to result in
245 * extra system calls and confusion. */
246 FD_CLR(server->tcp_socket, write_fds);
247
248 /* Count the number of send queue items. */
249 n = 0;
250 for (sendreq = server->qhead; sendreq; sendreq = sendreq->next)
251 n++;
252
253 /* Allocate iovecs so we can send all our data at once. */
254 vec = ares_malloc(n * sizeof(struct iovec));
255 if (vec)
256 {
257 /* Fill in the iovecs and send. */
258 n = 0;
259 for (sendreq = server->qhead; sendreq; sendreq = sendreq->next)
260 {
261 vec[n].iov_base = (char *) sendreq->data;
262 vec[n].iov_len = sendreq->len;
263 n++;
264 }
265 wcount = socket_writev(channel, server->tcp_socket, vec, (int)n);
266 ares_free(vec);
267 if (wcount < 0)
268 {
269 if (!try_again(SOCKERRNO))
270 handle_error(channel, i, now);
271 continue;
272 }
273
274 /* Advance the send queue by as many bytes as we sent. */
275 advance_tcp_send_queue(channel, i, wcount);
276 }
277 else
278 {
279 /* Can't allocate iovecs; just send the first request. */
280 sendreq = server->qhead;
281
282 scount = socket_write(channel, server->tcp_socket, sendreq->data, sendreq->len);
283 if (scount < 0)
284 {
285 if (!try_again(SOCKERRNO))
286 handle_error(channel, i, now);
287 continue;
288 }
289
290 /* Advance the send queue by as many bytes as we sent. */
291 advance_tcp_send_queue(channel, i, scount);
292 }
293 }
294 }
295
296 /* Consume the given number of bytes from the head of the TCP send queue. */
advance_tcp_send_queue(ares_channel channel,int whichserver,ares_ssize_t num_bytes)297 static void advance_tcp_send_queue(ares_channel channel, int whichserver,
298 ares_ssize_t num_bytes)
299 {
300 struct send_request *sendreq;
301 struct server_state *server = &channel->servers[whichserver];
302 while (num_bytes > 0) {
303 sendreq = server->qhead;
304 if ((size_t)num_bytes >= sendreq->len) {
305 num_bytes -= sendreq->len;
306 server->qhead = sendreq->next;
307 if (sendreq->data_storage)
308 ares_free(sendreq->data_storage);
309 ares_free(sendreq);
310 if (server->qhead == NULL) {
311 SOCK_STATE_CALLBACK(channel, server->tcp_socket, 1, 0);
312 server->qtail = NULL;
313
314 /* qhead is NULL so we cannot continue this loop */
315 break;
316 }
317 }
318 else {
319 sendreq->data += num_bytes;
320 sendreq->len -= num_bytes;
321 num_bytes = 0;
322 }
323 }
324 }
325
socket_recvfrom(ares_channel channel,ares_socket_t s,void * data,size_t data_len,int flags,struct sockaddr * from,ares_socklen_t * from_len)326 static ares_ssize_t socket_recvfrom(ares_channel channel,
327 ares_socket_t s,
328 void * data,
329 size_t data_len,
330 int flags,
331 struct sockaddr *from,
332 ares_socklen_t *from_len)
333 {
334 if (channel->sock_funcs)
335 return channel->sock_funcs->arecvfrom(s, data, data_len,
336 flags, from, from_len,
337 channel->sock_func_cb_data);
338
339 #ifdef HAVE_RECVFROM
340 return recvfrom(s, data, data_len, flags, from, from_len);
341 #else
342 return sread(s, data, data_len);
343 #endif
344 }
345
socket_recv(ares_channel channel,ares_socket_t s,void * data,size_t data_len)346 static ares_ssize_t socket_recv(ares_channel channel,
347 ares_socket_t s,
348 void * data,
349 size_t data_len)
350 {
351 if (channel->sock_funcs)
352 return channel->sock_funcs->arecvfrom(s, data, data_len, 0, 0, 0,
353 channel->sock_func_cb_data);
354
355 return sread(s, data, data_len);
356 }
357
358 /* If any TCP socket selects true for reading, read some data,
359 * allocate a buffer if we finish reading the length word, and process
360 * a packet if we finish reading one.
361 */
read_tcp_data(ares_channel channel,fd_set * read_fds,ares_socket_t read_fd,struct timeval * now)362 static void read_tcp_data(ares_channel channel, fd_set *read_fds,
363 ares_socket_t read_fd, struct timeval *now)
364 {
365 struct server_state *server;
366 int i;
367 ares_ssize_t count;
368
369 if(!read_fds && (read_fd == ARES_SOCKET_BAD))
370 /* no possible action */
371 return;
372
373 for (i = 0; i < channel->nservers; i++)
374 {
375 /* Make sure the server has a socket and is selected in read_fds. */
376 server = &channel->servers[i];
377 if (server->tcp_socket == ARES_SOCKET_BAD || server->is_broken)
378 continue;
379
380 if(read_fds) {
381 if(!FD_ISSET(server->tcp_socket, read_fds))
382 continue;
383 }
384 else {
385 if(server->tcp_socket != read_fd)
386 continue;
387 }
388
389 if(read_fds)
390 /* If there's an error and we close this socket, then open another
391 * with the same fd to talk to another server, then we don't want to
392 * think that it was the new socket that was ready. This is not
393 * disastrous, but is likely to result in extra system calls and
394 * confusion. */
395 FD_CLR(server->tcp_socket, read_fds);
396
397 if (server->tcp_lenbuf_pos != 2)
398 {
399 /* We haven't yet read a length word, so read that (or
400 * what's left to read of it).
401 */
402 count = socket_recv(channel, server->tcp_socket,
403 server->tcp_lenbuf + server->tcp_lenbuf_pos,
404 2 - server->tcp_lenbuf_pos);
405 if (count <= 0)
406 {
407 if (!(count == -1 && try_again(SOCKERRNO)))
408 handle_error(channel, i, now);
409 continue;
410 }
411
412 server->tcp_lenbuf_pos += (int)count;
413 if (server->tcp_lenbuf_pos == 2)
414 {
415 /* We finished reading the length word. Decode the
416 * length and allocate a buffer for the data.
417 */
418 server->tcp_length = server->tcp_lenbuf[0] << 8
419 | server->tcp_lenbuf[1];
420 server->tcp_buffer = ares_malloc(server->tcp_length);
421 if (!server->tcp_buffer) {
422 handle_error(channel, i, now);
423 return; /* bail out on malloc failure. TODO: make this
424 function return error codes */
425 }
426 server->tcp_buffer_pos = 0;
427 }
428 }
429 else
430 {
431 /* Read data into the allocated buffer. */
432 count = socket_recv(channel, server->tcp_socket,
433 server->tcp_buffer + server->tcp_buffer_pos,
434 server->tcp_length - server->tcp_buffer_pos);
435 if (count <= 0)
436 {
437 if (!(count == -1 && try_again(SOCKERRNO)))
438 handle_error(channel, i, now);
439 continue;
440 }
441
442 server->tcp_buffer_pos += (int)count;
443 if (server->tcp_buffer_pos == server->tcp_length)
444 {
445 /* We finished reading this answer; process it and
446 * prepare to read another length word.
447 */
448 process_answer(channel, server->tcp_buffer, server->tcp_length,
449 i, 1, now);
450 ares_free(server->tcp_buffer);
451 server->tcp_buffer = NULL;
452 server->tcp_lenbuf_pos = 0;
453 server->tcp_buffer_pos = 0;
454 }
455 }
456 }
457 }
458
459 /* If any UDP sockets select true for reading, process them. */
read_udp_packets(ares_channel channel,fd_set * read_fds,ares_socket_t read_fd,struct timeval * now)460 static void read_udp_packets(ares_channel channel, fd_set *read_fds,
461 ares_socket_t read_fd, struct timeval *now)
462 {
463 struct server_state *server;
464 int i;
465 ares_ssize_t count;
466 unsigned char buf[MAXENDSSZ + 1];
467 #ifdef HAVE_RECVFROM
468 ares_socklen_t fromlen;
469 union {
470 struct sockaddr sa;
471 struct sockaddr_in sa4;
472 struct sockaddr_in6 sa6;
473 } from;
474 #endif
475
476 if(!read_fds && (read_fd == ARES_SOCKET_BAD))
477 /* no possible action */
478 return;
479
480 for (i = 0; i < channel->nservers; i++)
481 {
482 /* Make sure the server has a socket and is selected in read_fds. */
483 server = &channel->servers[i];
484
485 if (server->udp_socket == ARES_SOCKET_BAD || server->is_broken)
486 continue;
487
488 if(read_fds) {
489 if(!FD_ISSET(server->udp_socket, read_fds))
490 continue;
491 }
492 else {
493 if(server->udp_socket != read_fd)
494 continue;
495 }
496
497 if(read_fds)
498 /* If there's an error and we close this socket, then open
499 * another with the same fd to talk to another server, then we
500 * don't want to think that it was the new socket that was
501 * ready. This is not disastrous, but is likely to result in
502 * extra system calls and confusion. */
503 FD_CLR(server->udp_socket, read_fds);
504
505 /* To reduce event loop overhead, read and process as many
506 * packets as we can. */
507 do {
508 if (server->udp_socket == ARES_SOCKET_BAD)
509 count = 0;
510
511 else {
512 if (server->addr.family == AF_INET)
513 fromlen = sizeof(from.sa4);
514 else
515 fromlen = sizeof(from.sa6);
516 count = socket_recvfrom(channel, server->udp_socket, (void *)buf,
517 sizeof(buf), 0, &from.sa, &fromlen);
518 }
519
520 if (count == -1 && try_again(SOCKERRNO))
521 continue;
522 else if (count <= 0)
523 handle_error(channel, i, now);
524 #ifdef HAVE_RECVFROM
525 else if (!same_address(&from.sa, &server->addr))
526 /* The address the response comes from does not match the address we
527 * sent the request to. Someone may be attempting to perform a cache
528 * poisoning attack. */
529 break;
530 #endif
531 else
532 process_answer(channel, buf, (int)count, i, 0, now);
533 } while (count > 0);
534 }
535 }
536
537 /* If any queries have timed out, note the timeout and move them on. */
process_timeouts(ares_channel channel,struct timeval * now)538 static void process_timeouts(ares_channel channel, struct timeval *now)
539 {
540 time_t t; /* the time of the timeouts we're processing */
541 struct query *query;
542 struct list_node* list_head;
543 struct list_node* list_node;
544
545 /* Process all the timeouts that have fired since the last time we processed
546 * timeouts. If things are going well, then we'll have hundreds/thousands of
547 * queries that fall into future buckets, and only a handful of requests
548 * that fall into the "now" bucket, so this should be quite quick.
549 */
550 for (t = channel->last_timeout_processed; t <= now->tv_sec; t++)
551 {
552 list_head = &(channel->queries_by_timeout[t % ARES_TIMEOUT_TABLE_SIZE]);
553 for (list_node = list_head->next; list_node != list_head; )
554 {
555 query = list_node->data;
556 list_node = list_node->next; /* in case the query gets deleted */
557 if (query->timeout.tv_sec && ares__timedout(now, &query->timeout))
558 {
559 query->error_status = ARES_ETIMEOUT;
560 ++query->timeouts;
561 next_server(channel, query, now);
562 }
563 }
564 }
565 channel->last_timeout_processed = now->tv_sec;
566 }
567
568 /* Handle an answer from a server. */
process_answer(ares_channel channel,unsigned char * abuf,int alen,int whichserver,int tcp,struct timeval * now)569 static void process_answer(ares_channel channel, unsigned char *abuf,
570 int alen, int whichserver, int tcp,
571 struct timeval *now)
572 {
573 int tc, rcode, packetsz;
574 unsigned short id;
575 struct query *query;
576 struct list_node* list_head;
577 struct list_node* list_node;
578
579 /* If there's no room in the answer for a header, we can't do much
580 * with it. */
581 if (alen < HFIXEDSZ)
582 return;
583
584 /* Grab the query ID, truncate bit, and response code from the packet. */
585 id = DNS_HEADER_QID(abuf);
586 tc = DNS_HEADER_TC(abuf);
587 rcode = DNS_HEADER_RCODE(abuf);
588
589 /* Find the query corresponding to this packet. The queries are
590 * hashed/bucketed by query id, so this lookup should be quick. Note that
591 * both the query id and the questions must be the same; when the query id
592 * wraps around we can have multiple outstanding queries with the same query
593 * id, so we need to check both the id and question.
594 */
595 query = NULL;
596 list_head = &(channel->queries_by_qid[id % ARES_QID_TABLE_SIZE]);
597 for (list_node = list_head->next; list_node != list_head;
598 list_node = list_node->next)
599 {
600 struct query *q = list_node->data;
601 if ((q->qid == id) && same_questions(q->qbuf, q->qlen, abuf, alen))
602 {
603 query = q;
604 break;
605 }
606 }
607 if (!query)
608 return;
609
610 packetsz = PACKETSZ;
611 /* If we use EDNS and server answers with one of these RCODES, the protocol
612 * extension is not understood by the responder. We must retry the query
613 * without EDNS enabled.
614 */
615 if (channel->flags & ARES_FLAG_EDNS)
616 {
617 packetsz = channel->ednspsz;
618 if (rcode == NOTIMP || rcode == FORMERR || rcode == SERVFAIL)
619 {
620 int qlen = (query->tcplen - 2) - EDNSFIXEDSZ;
621 channel->flags ^= ARES_FLAG_EDNS;
622 query->tcplen -= EDNSFIXEDSZ;
623 query->qlen -= EDNSFIXEDSZ;
624 query->tcpbuf[0] = (unsigned char)((qlen >> 8) & 0xff);
625 query->tcpbuf[1] = (unsigned char)(qlen & 0xff);
626 DNS_HEADER_SET_ARCOUNT(query->tcpbuf + 2, 0);
627 query->tcpbuf = ares_realloc(query->tcpbuf, query->tcplen);
628 query->qbuf = query->tcpbuf + 2;
629 ares__send_query(channel, query, now);
630 return;
631 }
632 }
633
634 /* If we got a truncated UDP packet and are not ignoring truncation,
635 * don't accept the packet, and switch the query to TCP if we hadn't
636 * done so already.
637 */
638 if ((tc || alen > packetsz) && !tcp && !(channel->flags & ARES_FLAG_IGNTC))
639 {
640 if (!query->using_tcp)
641 {
642 query->using_tcp = 1;
643 ares__send_query(channel, query, now);
644 }
645 return;
646 }
647
648 /* Limit alen to PACKETSZ if we aren't using TCP (only relevant if we
649 * are ignoring truncation.
650 */
651 if (alen > packetsz && !tcp)
652 alen = packetsz;
653
654 /* If we aren't passing through all error packets, discard packets
655 * with SERVFAIL, NOTIMP, or REFUSED response codes.
656 */
657 if (!(channel->flags & ARES_FLAG_NOCHECKRESP))
658 {
659 if (rcode == SERVFAIL || rcode == NOTIMP || rcode == REFUSED)
660 {
661 skip_server(channel, query, whichserver);
662 if (query->server == whichserver)
663 next_server(channel, query, now);
664 return;
665 }
666 }
667
668 end_query(channel, query, ARES_SUCCESS, abuf, alen);
669 }
670
671 /* Close all the connections that are no longer usable. */
process_broken_connections(ares_channel channel,struct timeval * now)672 static void process_broken_connections(ares_channel channel,
673 struct timeval *now)
674 {
675 int i;
676 for (i = 0; i < channel->nservers; i++)
677 {
678 struct server_state *server = &channel->servers[i];
679 if (server->is_broken)
680 {
681 handle_error(channel, i, now);
682 }
683 }
684 }
685
686 /* Swap the contents of two lists */
swap_lists(struct list_node * head_a,struct list_node * head_b)687 static void swap_lists(struct list_node* head_a,
688 struct list_node* head_b)
689 {
690 int is_a_empty = ares__is_list_empty(head_a);
691 int is_b_empty = ares__is_list_empty(head_b);
692 struct list_node old_a = *head_a;
693 struct list_node old_b = *head_b;
694
695 if (is_a_empty) {
696 ares__init_list_head(head_b);
697 } else {
698 *head_b = old_a;
699 old_a.next->prev = head_b;
700 old_a.prev->next = head_b;
701 }
702 if (is_b_empty) {
703 ares__init_list_head(head_a);
704 } else {
705 *head_a = old_b;
706 old_b.next->prev = head_a;
707 old_b.prev->next = head_a;
708 }
709 }
710
handle_error(ares_channel channel,int whichserver,struct timeval * now)711 static void handle_error(ares_channel channel, int whichserver,
712 struct timeval *now)
713 {
714 struct server_state *server;
715 struct query *query;
716 struct list_node list_head;
717 struct list_node* list_node;
718
719 server = &channel->servers[whichserver];
720
721 /* Reset communications with this server. */
722 ares__close_sockets(channel, server);
723
724 /* Tell all queries talking to this server to move on and not try this
725 * server again. We steal the current list of queries that were in-flight to
726 * this server, since when we call next_server this can cause the queries to
727 * be re-sent to this server, which will re-insert these queries in that
728 * same server->queries_to_server list.
729 */
730 ares__init_list_head(&list_head);
731 swap_lists(&list_head, &(server->queries_to_server));
732 for (list_node = list_head.next; list_node != &list_head; )
733 {
734 query = list_node->data;
735 list_node = list_node->next; /* in case the query gets deleted */
736 assert(query->server == whichserver);
737 skip_server(channel, query, whichserver);
738 next_server(channel, query, now);
739 }
740 /* Each query should have removed itself from our temporary list as
741 * it re-sent itself or finished up...
742 */
743 assert(ares__is_list_empty(&list_head));
744 }
745
skip_server(ares_channel channel,struct query * query,int whichserver)746 static void skip_server(ares_channel channel, struct query *query,
747 int whichserver)
748 {
749 /* The given server gave us problems with this query, so if we have the
750 * luxury of using other servers, then let's skip the potentially broken
751 * server and just use the others. If we only have one server and we need to
752 * retry then we should just go ahead and re-use that server, since it's our
753 * only hope; perhaps we just got unlucky, and retrying will work (eg, the
754 * server timed out our TCP connection just as we were sending another
755 * request).
756 */
757 if (channel->nservers > 1)
758 {
759 query->server_info[whichserver].skip_server = 1;
760 }
761 }
762
next_server(ares_channel channel,struct query * query,struct timeval * now)763 static void next_server(ares_channel channel, struct query *query,
764 struct timeval *now)
765 {
766 /* We need to try each server channel->tries times. We have channel->nservers
767 * servers to try. In total, we need to do channel->nservers * channel->tries
768 * attempts. Use query->try to remember how many times we already attempted
769 * this query. Use modular arithmetic to find the next server to try. */
770 while (++(query->try_count) < (channel->nservers * channel->tries))
771 {
772 struct server_state *server;
773
774 /* Move on to the next server. */
775 query->server = (query->server + 1) % channel->nservers;
776 server = &channel->servers[query->server];
777
778 /* We don't want to use this server if (1) we decided this connection is
779 * broken, and thus about to be closed, (2) we've decided to skip this
780 * server because of earlier errors we encountered, or (3) we already
781 * sent this query over this exact connection.
782 */
783 if (!server->is_broken &&
784 !query->server_info[query->server].skip_server &&
785 !(query->using_tcp &&
786 (query->server_info[query->server].tcp_connection_generation ==
787 server->tcp_connection_generation)))
788 {
789 ares__send_query(channel, query, now);
790 return;
791 }
792
793 /* You might think that with TCP we only need one try. However, even
794 * when using TCP, servers can time-out our connection just as we're
795 * sending a request, or close our connection because they die, or never
796 * send us a reply because they get wedged or tickle a bug that drops
797 * our request.
798 */
799 }
800
801 /* If we are here, all attempts to perform query failed. */
802 end_query(channel, query, query->error_status, NULL, 0);
803 }
804
ares__send_query(ares_channel channel,struct query * query,struct timeval * now)805 void ares__send_query(ares_channel channel, struct query *query,
806 struct timeval *now)
807 {
808 struct send_request *sendreq;
809 struct server_state *server;
810 int timeplus;
811
812 server = &channel->servers[query->server];
813 if (query->using_tcp)
814 {
815 /* Make sure the TCP socket for this server is set up and queue
816 * a send request.
817 */
818 if (server->tcp_socket == ARES_SOCKET_BAD)
819 {
820 if (open_tcp_socket(channel, server) == -1)
821 {
822 skip_server(channel, query, query->server);
823 next_server(channel, query, now);
824 return;
825 }
826 }
827 sendreq = ares_malloc(sizeof(struct send_request));
828 if (!sendreq)
829 {
830 end_query(channel, query, ARES_ENOMEM, NULL, 0);
831 return;
832 }
833 memset(sendreq, 0, sizeof(struct send_request));
834 /* To make the common case fast, we avoid copies by using the query's
835 * tcpbuf for as long as the query is alive. In the rare case where the
836 * query ends while it's queued for transmission, then we give the
837 * sendreq its own copy of the request packet and put it in
838 * sendreq->data_storage.
839 */
840 sendreq->data_storage = NULL;
841 sendreq->data = query->tcpbuf;
842 sendreq->len = query->tcplen;
843 sendreq->owner_query = query;
844 sendreq->next = NULL;
845 if (server->qtail)
846 server->qtail->next = sendreq;
847 else
848 {
849 SOCK_STATE_CALLBACK(channel, server->tcp_socket, 1, 1);
850 server->qhead = sendreq;
851 }
852 server->qtail = sendreq;
853 query->server_info[query->server].tcp_connection_generation =
854 server->tcp_connection_generation;
855 }
856 else
857 {
858 if (server->udp_socket == ARES_SOCKET_BAD)
859 {
860 if (open_udp_socket(channel, server) == -1)
861 {
862 skip_server(channel, query, query->server);
863 next_server(channel, query, now);
864 return;
865 }
866 }
867 if (socket_write(channel, server->udp_socket, query->qbuf, query->qlen) == -1)
868 {
869 /* FIXME: Handle EAGAIN here since it likely can happen. */
870 skip_server(channel, query, query->server);
871 next_server(channel, query, now);
872 return;
873 }
874 }
875
876 /* For each trip through the entire server list, double the channel's
877 * assigned timeout, avoiding overflow. If channel->timeout is negative,
878 * leave it as-is, even though that should be impossible here.
879 */
880 timeplus = channel->timeout;
881 {
882 /* How many times do we want to double it? Presume sane values here. */
883 const int shift = query->try_count / channel->nservers;
884
885 /* Is there enough room to shift timeplus left that many times?
886 *
887 * To find out, confirm that all of the bits we'll shift away are zero.
888 * Stop considering a shift if we get to the point where we could shift
889 * a 1 into the sign bit (i.e. when shift is within two of the bit
890 * count).
891 *
892 * This has the side benefit of leaving negative numbers unchanged.
893 */
894 if(shift <= (int)(sizeof(int) * CHAR_BIT - 1)
895 && (timeplus >> (sizeof(int) * CHAR_BIT - 1 - shift)) == 0)
896 {
897 timeplus <<= shift;
898 }
899 }
900
901 query->timeout = *now;
902 timeadd(&query->timeout, timeplus);
903 /* Keep track of queries bucketed by timeout, so we can process
904 * timeout events quickly.
905 */
906 ares__remove_from_list(&(query->queries_by_timeout));
907 ares__insert_in_list(
908 &(query->queries_by_timeout),
909 &(channel->queries_by_timeout[query->timeout.tv_sec %
910 ARES_TIMEOUT_TABLE_SIZE]));
911
912 /* Keep track of queries bucketed by server, so we can process server
913 * errors quickly.
914 */
915 ares__remove_from_list(&(query->queries_to_server));
916 ares__insert_in_list(&(query->queries_to_server),
917 &(server->queries_to_server));
918 }
919
920 /*
921 * setsocknonblock sets the given socket to either blocking or non-blocking
922 * mode based on the 'nonblock' boolean argument. This function is highly
923 * portable.
924 */
setsocknonblock(ares_socket_t sockfd,int nonblock)925 static int setsocknonblock(ares_socket_t sockfd, /* operate on this */
926 int nonblock /* TRUE or FALSE */)
927 {
928 #if defined(USE_BLOCKING_SOCKETS)
929
930 return 0; /* returns success */
931
932 #elif defined(HAVE_FCNTL_O_NONBLOCK)
933
934 /* most recent unix versions */
935 int flags;
936 flags = fcntl(sockfd, F_GETFL, 0);
937 if (FALSE != nonblock)
938 return fcntl(sockfd, F_SETFL, flags | O_NONBLOCK);
939 else
940 return fcntl(sockfd, F_SETFL, flags & (~O_NONBLOCK)); /* LCOV_EXCL_LINE */
941
942 #elif defined(HAVE_IOCTL_FIONBIO)
943
944 /* older unix versions */
945 int flags = nonblock ? 1 : 0;
946 return ioctl(sockfd, FIONBIO, &flags);
947
948 #elif defined(HAVE_IOCTLSOCKET_FIONBIO)
949
950 #ifdef WATT32
951 char flags = nonblock ? 1 : 0;
952 #else
953 /* Windows */
954 unsigned long flags = nonblock ? 1UL : 0UL;
955 #endif
956 return ioctlsocket(sockfd, FIONBIO, &flags);
957
958 #elif defined(HAVE_IOCTLSOCKET_CAMEL_FIONBIO)
959
960 /* Amiga */
961 long flags = nonblock ? 1L : 0L;
962 return IoctlSocket(sockfd, FIONBIO, flags);
963
964 #elif defined(HAVE_SETSOCKOPT_SO_NONBLOCK)
965
966 /* BeOS */
967 long b = nonblock ? 1L : 0L;
968 return setsockopt(sockfd, SOL_SOCKET, SO_NONBLOCK, &b, sizeof(b));
969
970 #else
971 # error "no non-blocking method was found/used/set"
972 #endif
973 }
974
configure_socket(ares_socket_t s,int family,ares_channel channel)975 static int configure_socket(ares_socket_t s, int family, ares_channel channel)
976 {
977 union {
978 struct sockaddr sa;
979 struct sockaddr_in sa4;
980 struct sockaddr_in6 sa6;
981 } local;
982
983 /* do not set options for user-managed sockets */
984 if (channel->sock_funcs)
985 return 0;
986
987 (void)setsocknonblock(s, TRUE);
988
989 #if defined(FD_CLOEXEC) && !defined(MSDOS)
990 /* Configure the socket fd as close-on-exec. */
991 if (fcntl(s, F_SETFD, FD_CLOEXEC) == -1)
992 return -1; /* LCOV_EXCL_LINE */
993 #endif
994
995 /* Set the socket's send and receive buffer sizes. */
996 if ((channel->socket_send_buffer_size > 0) &&
997 setsockopt(s, SOL_SOCKET, SO_SNDBUF,
998 (void *)&channel->socket_send_buffer_size,
999 sizeof(channel->socket_send_buffer_size)) == -1)
1000 return -1;
1001
1002 if ((channel->socket_receive_buffer_size > 0) &&
1003 setsockopt(s, SOL_SOCKET, SO_RCVBUF,
1004 (void *)&channel->socket_receive_buffer_size,
1005 sizeof(channel->socket_receive_buffer_size)) == -1)
1006 return -1;
1007
1008 #ifdef SO_BINDTODEVICE
1009 if (channel->local_dev_name[0]) {
1010 if (setsockopt(s, SOL_SOCKET, SO_BINDTODEVICE,
1011 channel->local_dev_name, sizeof(channel->local_dev_name))) {
1012 /* Only root can do this, and usually not fatal if it doesn't work, so */
1013 /* just continue on. */
1014 }
1015 }
1016 #endif
1017
1018 if (family == AF_INET) {
1019 if (channel->local_ip4) {
1020 memset(&local.sa4, 0, sizeof(local.sa4));
1021 local.sa4.sin_family = AF_INET;
1022 local.sa4.sin_addr.s_addr = htonl(channel->local_ip4);
1023 if (bind(s, &local.sa, sizeof(local.sa4)) < 0)
1024 return -1;
1025 }
1026 }
1027 else if (family == AF_INET6) {
1028 if (memcmp(channel->local_ip6, &ares_in6addr_any,
1029 sizeof(channel->local_ip6)) != 0) {
1030 memset(&local.sa6, 0, sizeof(local.sa6));
1031 local.sa6.sin6_family = AF_INET6;
1032 memcpy(&local.sa6.sin6_addr, channel->local_ip6,
1033 sizeof(channel->local_ip6));
1034 if (bind(s, &local.sa, sizeof(local.sa6)) < 0)
1035 return -1;
1036 }
1037 }
1038
1039 return 0;
1040 }
1041
open_tcp_socket(ares_channel channel,struct server_state * server)1042 static int open_tcp_socket(ares_channel channel, struct server_state *server)
1043 {
1044 ares_socket_t s;
1045 int opt;
1046 ares_socklen_t salen;
1047 union {
1048 struct sockaddr_in sa4;
1049 struct sockaddr_in6 sa6;
1050 } saddr;
1051 struct sockaddr *sa;
1052
1053 switch (server->addr.family)
1054 {
1055 case AF_INET:
1056 sa = (void *)&saddr.sa4;
1057 salen = sizeof(saddr.sa4);
1058 memset(sa, 0, salen);
1059 saddr.sa4.sin_family = AF_INET;
1060 if (server->addr.tcp_port) {
1061 saddr.sa4.sin_port = aresx_sitous(server->addr.tcp_port);
1062 } else {
1063 saddr.sa4.sin_port = aresx_sitous(channel->tcp_port);
1064 }
1065 memcpy(&saddr.sa4.sin_addr, &server->addr.addrV4,
1066 sizeof(server->addr.addrV4));
1067 break;
1068 case AF_INET6:
1069 sa = (void *)&saddr.sa6;
1070 salen = sizeof(saddr.sa6);
1071 memset(sa, 0, salen);
1072 saddr.sa6.sin6_family = AF_INET6;
1073 if (server->addr.tcp_port) {
1074 saddr.sa6.sin6_port = aresx_sitous(server->addr.tcp_port);
1075 } else {
1076 saddr.sa6.sin6_port = aresx_sitous(channel->tcp_port);
1077 }
1078 memcpy(&saddr.sa6.sin6_addr, &server->addr.addrV6,
1079 sizeof(server->addr.addrV6));
1080 break;
1081 default:
1082 return -1; /* LCOV_EXCL_LINE */
1083 }
1084
1085 /* Acquire a socket. */
1086 s = ares__open_socket(channel, server->addr.family, SOCK_STREAM, 0);
1087 if (s == ARES_SOCKET_BAD)
1088 return -1;
1089
1090 /* Configure it. */
1091 if (configure_socket(s, server->addr.family, channel) < 0)
1092 {
1093 ares__close_socket(channel, s);
1094 return -1;
1095 }
1096
1097 #ifdef TCP_NODELAY
1098 /*
1099 * Disable the Nagle algorithm (only relevant for TCP sockets, and thus not
1100 * in configure_socket). In general, in DNS lookups we're pretty much
1101 * interested in firing off a single request and then waiting for a reply,
1102 * so batching isn't very interesting.
1103 */
1104 opt = 1;
1105 if (channel->sock_funcs == 0
1106 &&
1107 setsockopt(s, IPPROTO_TCP, TCP_NODELAY,
1108 (void *)&opt, sizeof(opt)) == -1)
1109 {
1110 ares__close_socket(channel, s);
1111 return -1;
1112 }
1113 #endif
1114
1115 if (channel->sock_config_cb)
1116 {
1117 int err = channel->sock_config_cb(s, SOCK_STREAM,
1118 channel->sock_config_cb_data);
1119 if (err < 0)
1120 {
1121 ares__close_socket(channel, s);
1122 return err;
1123 }
1124 }
1125
1126 /* Connect to the server. */
1127 if (ares__connect_socket(channel, s, sa, salen) == -1)
1128 {
1129 int err = SOCKERRNO;
1130
1131 if (err != EINPROGRESS && err != EWOULDBLOCK)
1132 {
1133 ares__close_socket(channel, s);
1134 return -1;
1135 }
1136 }
1137
1138 if (channel->sock_create_cb)
1139 {
1140 int err = channel->sock_create_cb(s, SOCK_STREAM,
1141 channel->sock_create_cb_data);
1142 if (err < 0)
1143 {
1144 ares__close_socket(channel, s);
1145 return err;
1146 }
1147 }
1148
1149 SOCK_STATE_CALLBACK(channel, s, 1, 0);
1150 server->tcp_buffer_pos = 0;
1151 server->tcp_socket = s;
1152 server->tcp_connection_generation = ++channel->tcp_connection_generation;
1153 return 0;
1154 }
1155
open_udp_socket(ares_channel channel,struct server_state * server)1156 static int open_udp_socket(ares_channel channel, struct server_state *server)
1157 {
1158 ares_socket_t s;
1159 ares_socklen_t salen;
1160 union {
1161 struct sockaddr_in sa4;
1162 struct sockaddr_in6 sa6;
1163 } saddr;
1164 struct sockaddr *sa;
1165
1166 switch (server->addr.family)
1167 {
1168 case AF_INET:
1169 sa = (void *)&saddr.sa4;
1170 salen = sizeof(saddr.sa4);
1171 memset(sa, 0, salen);
1172 saddr.sa4.sin_family = AF_INET;
1173 if (server->addr.udp_port) {
1174 saddr.sa4.sin_port = aresx_sitous(server->addr.udp_port);
1175 } else {
1176 saddr.sa4.sin_port = aresx_sitous(channel->udp_port);
1177 }
1178 memcpy(&saddr.sa4.sin_addr, &server->addr.addrV4,
1179 sizeof(server->addr.addrV4));
1180 break;
1181 case AF_INET6:
1182 sa = (void *)&saddr.sa6;
1183 salen = sizeof(saddr.sa6);
1184 memset(sa, 0, salen);
1185 saddr.sa6.sin6_family = AF_INET6;
1186 if (server->addr.udp_port) {
1187 saddr.sa6.sin6_port = aresx_sitous(server->addr.udp_port);
1188 } else {
1189 saddr.sa6.sin6_port = aresx_sitous(channel->udp_port);
1190 }
1191 memcpy(&saddr.sa6.sin6_addr, &server->addr.addrV6,
1192 sizeof(server->addr.addrV6));
1193 break;
1194 default:
1195 return -1; /* LCOV_EXCL_LINE */
1196 }
1197
1198 /* Acquire a socket. */
1199 s = ares__open_socket(channel, server->addr.family, SOCK_DGRAM, 0);
1200 if (s == ARES_SOCKET_BAD)
1201 return -1;
1202
1203 /* Set the socket non-blocking. */
1204 if (configure_socket(s, server->addr.family, channel) < 0)
1205 {
1206 ares__close_socket(channel, s);
1207 return -1;
1208 }
1209
1210 if (channel->sock_config_cb)
1211 {
1212 int err = channel->sock_config_cb(s, SOCK_DGRAM,
1213 channel->sock_config_cb_data);
1214 if (err < 0)
1215 {
1216 ares__close_socket(channel, s);
1217 return err;
1218 }
1219 }
1220
1221 /* Connect to the server. */
1222 if (ares__connect_socket(channel, s, sa, salen) == -1)
1223 {
1224 int err = SOCKERRNO;
1225
1226 if (err != EINPROGRESS && err != EWOULDBLOCK)
1227 {
1228 ares__close_socket(channel, s);
1229 return -1;
1230 }
1231 }
1232
1233 if (channel->sock_create_cb)
1234 {
1235 int err = channel->sock_create_cb(s, SOCK_DGRAM,
1236 channel->sock_create_cb_data);
1237 if (err < 0)
1238 {
1239 ares__close_socket(channel, s);
1240 return err;
1241 }
1242 }
1243
1244 SOCK_STATE_CALLBACK(channel, s, 1, 0);
1245
1246 server->udp_socket = s;
1247 return 0;
1248 }
1249
same_questions(const unsigned char * qbuf,int qlen,const unsigned char * abuf,int alen)1250 static int same_questions(const unsigned char *qbuf, int qlen,
1251 const unsigned char *abuf, int alen)
1252 {
1253 struct {
1254 const unsigned char *p;
1255 int qdcount;
1256 char *name;
1257 long namelen;
1258 int type;
1259 int dnsclass;
1260 } q, a;
1261 int i, j;
1262
1263 if (qlen < HFIXEDSZ || alen < HFIXEDSZ)
1264 return 0;
1265
1266 /* Extract qdcount from the request and reply buffers and compare them. */
1267 q.qdcount = DNS_HEADER_QDCOUNT(qbuf);
1268 a.qdcount = DNS_HEADER_QDCOUNT(abuf);
1269 if (q.qdcount != a.qdcount)
1270 return 0;
1271
1272 /* For each question in qbuf, find it in abuf. */
1273 q.p = qbuf + HFIXEDSZ;
1274 for (i = 0; i < q.qdcount; i++)
1275 {
1276 /* Decode the question in the query. */
1277 if (ares_expand_name(q.p, qbuf, qlen, &q.name, &q.namelen)
1278 != ARES_SUCCESS)
1279 return 0;
1280 q.p += q.namelen;
1281 if (q.p + QFIXEDSZ > qbuf + qlen)
1282 {
1283 ares_free(q.name);
1284 return 0;
1285 }
1286 q.type = DNS_QUESTION_TYPE(q.p);
1287 q.dnsclass = DNS_QUESTION_CLASS(q.p);
1288 q.p += QFIXEDSZ;
1289
1290 /* Search for this question in the answer. */
1291 a.p = abuf + HFIXEDSZ;
1292 for (j = 0; j < a.qdcount; j++)
1293 {
1294 /* Decode the question in the answer. */
1295 if (ares_expand_name(a.p, abuf, alen, &a.name, &a.namelen)
1296 != ARES_SUCCESS)
1297 {
1298 ares_free(q.name);
1299 return 0;
1300 }
1301 a.p += a.namelen;
1302 if (a.p + QFIXEDSZ > abuf + alen)
1303 {
1304 ares_free(q.name);
1305 ares_free(a.name);
1306 return 0;
1307 }
1308 a.type = DNS_QUESTION_TYPE(a.p);
1309 a.dnsclass = DNS_QUESTION_CLASS(a.p);
1310 a.p += QFIXEDSZ;
1311
1312 /* Compare the decoded questions. */
1313 if (strcasecmp(q.name, a.name) == 0 && q.type == a.type
1314 && q.dnsclass == a.dnsclass)
1315 {
1316 ares_free(a.name);
1317 break;
1318 }
1319 ares_free(a.name);
1320 }
1321
1322 ares_free(q.name);
1323 if (j == a.qdcount)
1324 return 0;
1325 }
1326 return 1;
1327 }
1328
same_address(struct sockaddr * sa,struct ares_addr * aa)1329 static int same_address(struct sockaddr *sa, struct ares_addr *aa)
1330 {
1331 void *addr1;
1332 void *addr2;
1333
1334 if (sa->sa_family == aa->family)
1335 {
1336 switch (aa->family)
1337 {
1338 case AF_INET:
1339 addr1 = &aa->addrV4;
1340 addr2 = &(CARES_INADDR_CAST(struct sockaddr_in *, sa))->sin_addr;
1341 if (memcmp(addr1, addr2, sizeof(aa->addrV4)) == 0)
1342 return 1; /* match */
1343 break;
1344 case AF_INET6:
1345 addr1 = &aa->addrV6;
1346 addr2 = &(CARES_INADDR_CAST(struct sockaddr_in6 *, sa))->sin6_addr;
1347 if (memcmp(addr1, addr2, sizeof(aa->addrV6)) == 0)
1348 return 1; /* match */
1349 break;
1350 default:
1351 break; /* LCOV_EXCL_LINE */
1352 }
1353 }
1354 return 0; /* different */
1355 }
1356
end_query(ares_channel channel,struct query * query,int status,unsigned char * abuf,int alen)1357 static void end_query (ares_channel channel, struct query *query, int status,
1358 unsigned char *abuf, int alen)
1359 {
1360 int i;
1361
1362 /* First we check to see if this query ended while one of our send
1363 * queues still has pointers to it.
1364 */
1365 for (i = 0; i < channel->nservers; i++)
1366 {
1367 struct server_state *server = &channel->servers[i];
1368 struct send_request *sendreq;
1369 for (sendreq = server->qhead; sendreq; sendreq = sendreq->next)
1370 if (sendreq->owner_query == query)
1371 {
1372 sendreq->owner_query = NULL;
1373 assert(sendreq->data_storage == NULL);
1374 if (status == ARES_SUCCESS)
1375 {
1376 /* We got a reply for this query, but this queued sendreq
1377 * points into this soon-to-be-gone query's tcpbuf. Probably
1378 * this means we timed out and queued the query for
1379 * retransmission, then received a response before actually
1380 * retransmitting. This is perfectly fine, so we want to keep
1381 * the connection running smoothly if we can. But in the worst
1382 * case we may have sent only some prefix of the query, with
1383 * some suffix of the query left to send. Also, the buffer may
1384 * be queued on multiple queues. To prevent dangling pointers
1385 * to the query's tcpbuf and handle these cases, we just give
1386 * such sendreqs their own copy of the query packet.
1387 */
1388 sendreq->data_storage = ares_malloc(sendreq->len);
1389 if (sendreq->data_storage != NULL)
1390 {
1391 memcpy(sendreq->data_storage, sendreq->data, sendreq->len);
1392 sendreq->data = sendreq->data_storage;
1393 }
1394 }
1395 if ((status != ARES_SUCCESS) || (sendreq->data_storage == NULL))
1396 {
1397 /* We encountered an error (probably a timeout, suggesting the
1398 * DNS server we're talking to is probably unreachable,
1399 * wedged, or severely overloaded) or we couldn't copy the
1400 * request, so mark the connection as broken. When we get to
1401 * process_broken_connections() we'll close the connection and
1402 * try to re-send requests to another server.
1403 */
1404 server->is_broken = 1;
1405 /* Just to be paranoid, zero out this sendreq... */
1406 sendreq->data = NULL;
1407 sendreq->len = 0;
1408 }
1409 }
1410 }
1411
1412 /* Invoke the callback */
1413 query->callback(query->arg, status, query->timeouts, abuf, alen);
1414 ares__free_query(query);
1415
1416 /* Simple cleanup policy: if no queries are remaining, close all network
1417 * sockets unless STAYOPEN is set.
1418 */
1419 if (!(channel->flags & ARES_FLAG_STAYOPEN) &&
1420 ares__is_list_empty(&(channel->all_queries)))
1421 {
1422 for (i = 0; i < channel->nservers; i++)
1423 ares__close_sockets(channel, &channel->servers[i]);
1424 }
1425 }
1426
ares__free_query(struct query * query)1427 void ares__free_query(struct query *query)
1428 {
1429 /* Remove the query from all the lists in which it is linked */
1430 ares__remove_from_list(&(query->queries_by_qid));
1431 ares__remove_from_list(&(query->queries_by_timeout));
1432 ares__remove_from_list(&(query->queries_to_server));
1433 ares__remove_from_list(&(query->all_queries));
1434 /* Zero out some important stuff, to help catch bugs */
1435 query->callback = NULL;
1436 query->arg = NULL;
1437 /* Deallocate the memory associated with the query */
1438 ares_free(query->tcpbuf);
1439 ares_free(query->server_info);
1440 ares_free(query);
1441 }
1442
ares__open_socket(ares_channel channel,int af,int type,int protocol)1443 ares_socket_t ares__open_socket(ares_channel channel,
1444 int af, int type, int protocol)
1445 {
1446 if (channel->sock_funcs)
1447 return channel->sock_funcs->asocket(af,
1448 type,
1449 protocol,
1450 channel->sock_func_cb_data);
1451 else
1452 return socket(af, type, protocol);
1453 }
1454
ares__connect_socket(ares_channel channel,ares_socket_t sockfd,const struct sockaddr * addr,ares_socklen_t addrlen)1455 int ares__connect_socket(ares_channel channel,
1456 ares_socket_t sockfd,
1457 const struct sockaddr *addr,
1458 ares_socklen_t addrlen)
1459 {
1460 if (channel->sock_funcs)
1461 return channel->sock_funcs->aconnect(sockfd,
1462 addr,
1463 addrlen,
1464 channel->sock_func_cb_data);
1465 else
1466 return connect(sockfd, addr, addrlen);
1467 }
1468
ares__close_socket(ares_channel channel,ares_socket_t s)1469 void ares__close_socket(ares_channel channel, ares_socket_t s)
1470 {
1471 if (channel->sock_funcs)
1472 channel->sock_funcs->aclose(s, channel->sock_func_cb_data);
1473 else
1474 sclose(s);
1475 }
1476