powerpc: Add optimized version of [l]lrintf
[glibc.git] / resolv / res_send.c
blobb7b8ecdfc4244c4a60a38658ad3694caa258f95e
1 /* Copyright (C) 2016-2017 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
19 * Copyright (c) 1985, 1989, 1993
20 * The Regents of the University of California. All rights reserved.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the above copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 4. Neither the name of the University nor the names of its contributors
31 * may be used to endorse or promote products derived from this software
32 * without specific prior written permission.
34 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
35 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
38 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44 * SUCH DAMAGE.
48 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
50 * Permission to use, copy, modify, and distribute this software for any
51 * purpose with or without fee is hereby granted, provided that the above
52 * copyright notice and this permission notice appear in all copies, and that
53 * the name of Digital Equipment Corporation not be used in advertising or
54 * publicity pertaining to distribution of the document or software without
55 * specific, written prior permission.
57 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
58 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
59 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
60 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
61 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
62 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
63 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
64 * SOFTWARE.
68 * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
70 * Permission to use, copy, modify, and distribute this software for any
71 * purpose with or without fee is hereby granted, provided that the above
72 * copyright notice and this permission notice appear in all copies.
74 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
75 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
76 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
77 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
78 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
79 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
80 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
81 * SOFTWARE.
85 * Send query to name server and wait for reply.
88 #include <assert.h>
89 #include <sys/types.h>
90 #include <sys/param.h>
91 #include <sys/time.h>
92 #include <sys/socket.h>
93 #include <sys/uio.h>
94 #include <sys/poll.h>
96 #include <netinet/in.h>
97 #include <arpa/nameser.h>
98 #include <arpa/inet.h>
99 #include <sys/ioctl.h>
101 #include <errno.h>
102 #include <fcntl.h>
103 #include <netdb.h>
104 #include <resolv/resolv-internal.h>
105 #include <signal.h>
106 #include <stdio.h>
107 #include <stdlib.h>
108 #include <string.h>
109 #include <unistd.h>
110 #include <kernel-features.h>
111 #include <libc-diag.h>
113 #if PACKETSZ > 65536
114 #define MAXPACKET PACKETSZ
115 #else
116 #define MAXPACKET 65536
117 #endif
119 /* From ev_streams.c. */
121 static inline void
122 __attribute ((always_inline))
123 evConsIovec(void *buf, size_t cnt, struct iovec *vec) {
124 memset(vec, 0xf5, sizeof (*vec));
125 vec->iov_base = buf;
126 vec->iov_len = cnt;
129 /* From ev_timers.c. */
131 #define BILLION 1000000000
133 static inline void
134 evConsTime(struct timespec *res, time_t sec, long nsec) {
135 res->tv_sec = sec;
136 res->tv_nsec = nsec;
139 static inline void
140 evAddTime(struct timespec *res, const struct timespec *addend1,
141 const struct timespec *addend2) {
142 res->tv_sec = addend1->tv_sec + addend2->tv_sec;
143 res->tv_nsec = addend1->tv_nsec + addend2->tv_nsec;
144 if (res->tv_nsec >= BILLION) {
145 res->tv_sec++;
146 res->tv_nsec -= BILLION;
150 static inline void
151 evSubTime(struct timespec *res, const struct timespec *minuend,
152 const struct timespec *subtrahend) {
153 res->tv_sec = minuend->tv_sec - subtrahend->tv_sec;
154 if (minuend->tv_nsec >= subtrahend->tv_nsec)
155 res->tv_nsec = minuend->tv_nsec - subtrahend->tv_nsec;
156 else {
157 res->tv_nsec = (BILLION
158 - subtrahend->tv_nsec + minuend->tv_nsec);
159 res->tv_sec--;
163 static int
164 evCmpTime(struct timespec a, struct timespec b) {
165 long x = a.tv_sec - b.tv_sec;
167 if (x == 0L)
168 x = a.tv_nsec - b.tv_nsec;
169 return (x < 0L ? (-1) : x > 0L ? (1) : (0));
172 static void
173 evNowTime(struct timespec *res) {
174 struct timeval now;
176 if (gettimeofday(&now, NULL) < 0)
177 evConsTime(res, 0, 0);
178 else
179 TIMEVAL_TO_TIMESPEC (&now, res);
183 /* Options. Leave them on. */
184 /* #undef DEBUG */
185 #include "res_debug.h"
187 #define EXT(res) ((res)->_u._ext)
189 /* Forward. */
191 static struct sockaddr *get_nsaddr (res_state, int);
192 static int send_vc(res_state, const u_char *, int,
193 const u_char *, int,
194 u_char **, int *, int *, int, u_char **,
195 u_char **, int *, int *, int *);
196 static int send_dg(res_state, const u_char *, int,
197 const u_char *, int,
198 u_char **, int *, int *, int,
199 int *, int *, u_char **,
200 u_char **, int *, int *, int *);
201 #ifdef DEBUG
202 static void Aerror(const res_state, FILE *, const char *, int,
203 const struct sockaddr *);
204 static void Perror(const res_state, FILE *, const char *, int);
205 #endif
206 static int sock_eq(struct sockaddr_in6 *, struct sockaddr_in6 *);
208 /* Public. */
210 /* int
211 * res_isourserver(ina)
212 * looks up "ina" in _res.ns_addr_list[]
213 * returns:
214 * 0 : not found
215 * >0 : found
216 * author:
217 * paul vixie, 29may94
220 res_ourserver_p(const res_state statp, const struct sockaddr_in6 *inp)
222 int ns;
224 if (inp->sin6_family == AF_INET) {
225 struct sockaddr_in *in4p = (struct sockaddr_in *) inp;
226 in_port_t port = in4p->sin_port;
227 in_addr_t addr = in4p->sin_addr.s_addr;
229 for (ns = 0; ns < statp->nscount; ns++) {
230 const struct sockaddr_in *srv =
231 (struct sockaddr_in *) get_nsaddr (statp, ns);
233 if ((srv->sin_family == AF_INET) &&
234 (srv->sin_port == port) &&
235 (srv->sin_addr.s_addr == INADDR_ANY ||
236 srv->sin_addr.s_addr == addr))
237 return (1);
239 } else if (inp->sin6_family == AF_INET6) {
240 for (ns = 0; ns < statp->nscount; ns++) {
241 const struct sockaddr_in6 *srv
242 = (struct sockaddr_in6 *) get_nsaddr (statp, ns);
243 if ((srv->sin6_family == AF_INET6) &&
244 (srv->sin6_port == inp->sin6_port) &&
245 !(memcmp(&srv->sin6_addr, &in6addr_any,
246 sizeof (struct in6_addr)) &&
247 memcmp(&srv->sin6_addr, &inp->sin6_addr,
248 sizeof (struct in6_addr))))
249 return (1);
252 return (0);
255 /* int
256 * res_nameinquery(name, type, class, buf, eom)
257 * look for (name,type,class) in the query section of packet (buf,eom)
258 * requires:
259 * buf + HFIXEDSZ <= eom
260 * returns:
261 * -1 : format error
262 * 0 : not found
263 * >0 : found
264 * author:
265 * paul vixie, 29may94
268 res_nameinquery(const char *name, int type, int class,
269 const u_char *buf, const u_char *eom)
271 const u_char *cp = buf + HFIXEDSZ;
272 int qdcount = ntohs(((HEADER*)buf)->qdcount);
274 while (qdcount-- > 0) {
275 char tname[MAXDNAME+1];
276 int n, ttype, tclass;
278 n = dn_expand(buf, eom, cp, tname, sizeof tname);
279 if (n < 0)
280 return (-1);
281 cp += n;
282 if (cp + 2 * INT16SZ > eom)
283 return (-1);
284 NS_GET16(ttype, cp);
285 NS_GET16(tclass, cp);
286 if (ttype == type && tclass == class &&
287 ns_samename(tname, name) == 1)
288 return (1);
290 return (0);
292 libresolv_hidden_def (res_nameinquery)
294 /* int
295 * res_queriesmatch(buf1, eom1, buf2, eom2)
296 * is there a 1:1 mapping of (name,type,class)
297 * in (buf1,eom1) and (buf2,eom2)?
298 * returns:
299 * -1 : format error
300 * 0 : not a 1:1 mapping
301 * >0 : is a 1:1 mapping
302 * author:
303 * paul vixie, 29may94
306 res_queriesmatch(const u_char *buf1, const u_char *eom1,
307 const u_char *buf2, const u_char *eom2)
309 if (buf1 + HFIXEDSZ > eom1 || buf2 + HFIXEDSZ > eom2)
310 return (-1);
313 * Only header section present in replies to
314 * dynamic update packets.
316 if ((((HEADER *)buf1)->opcode == ns_o_update) &&
317 (((HEADER *)buf2)->opcode == ns_o_update))
318 return (1);
320 /* Note that we initially do not convert QDCOUNT to the host byte
321 order. We can compare it with the second buffer's QDCOUNT
322 value without doing this. */
323 int qdcount = ((HEADER*)buf1)->qdcount;
324 if (qdcount != ((HEADER*)buf2)->qdcount)
325 return (0);
327 qdcount = htons (qdcount);
328 const u_char *cp = buf1 + HFIXEDSZ;
330 while (qdcount-- > 0) {
331 char tname[MAXDNAME+1];
332 int n, ttype, tclass;
334 n = dn_expand(buf1, eom1, cp, tname, sizeof tname);
335 if (n < 0)
336 return (-1);
337 cp += n;
338 if (cp + 2 * INT16SZ > eom1)
339 return (-1);
340 NS_GET16(ttype, cp);
341 NS_GET16(tclass, cp);
342 if (!res_nameinquery(tname, ttype, tclass, buf2, eom2))
343 return (0);
345 return (1);
347 libresolv_hidden_def (res_queriesmatch)
350 __libc_res_nsend(res_state statp, const u_char *buf, int buflen,
351 const u_char *buf2, int buflen2,
352 u_char *ans, int anssiz, u_char **ansp, u_char **ansp2,
353 int *nansp2, int *resplen2, int *ansp2_malloced)
355 int gotsomewhere, terrno, try, v_circuit, resplen, ns, n;
357 if (statp->nscount == 0) {
358 __set_errno (ESRCH);
359 return (-1);
362 if (anssiz < (buf2 == NULL ? 1 : 2) * HFIXEDSZ) {
363 __set_errno (EINVAL);
364 return (-1);
367 DprintQ((statp->options & RES_DEBUG) || (statp->pfcode & RES_PRF_QUERY),
368 (stdout, ";; res_send()\n"), buf, buflen);
369 v_circuit = ((statp->options & RES_USEVC)
370 || buflen > PACKETSZ
371 || buflen2 > PACKETSZ);
372 gotsomewhere = 0;
373 terrno = ETIMEDOUT;
376 * If the ns_addr_list in the resolver context has changed, then
377 * invalidate our cached copy and the associated timing data.
379 if (EXT(statp).nscount != 0) {
380 int needclose = 0;
382 if (EXT(statp).nscount != statp->nscount)
383 needclose++;
384 else
385 for (ns = 0; ns < statp->nscount; ns++) {
386 if (statp->nsaddr_list[ns].sin_family != 0
387 && !sock_eq((struct sockaddr_in6 *)
388 &statp->nsaddr_list[ns],
389 EXT(statp).nsaddrs[ns]))
391 needclose++;
392 break;
395 if (needclose) {
396 __res_iclose(statp, false);
397 EXT(statp).nscount = 0;
402 * Maybe initialize our private copy of the ns_addr_list.
404 if (EXT(statp).nscount == 0) {
405 for (ns = 0; ns < statp->nscount; ns++) {
406 EXT(statp).nssocks[ns] = -1;
407 if (statp->nsaddr_list[ns].sin_family == 0)
408 continue;
409 if (EXT(statp).nsaddrs[ns] == NULL)
410 EXT(statp).nsaddrs[ns] =
411 malloc(sizeof (struct sockaddr_in6));
412 if (EXT(statp).nsaddrs[ns] != NULL)
413 memset (mempcpy(EXT(statp).nsaddrs[ns],
414 &statp->nsaddr_list[ns],
415 sizeof (struct sockaddr_in)),
416 '\0',
417 sizeof (struct sockaddr_in6)
418 - sizeof (struct sockaddr_in));
420 EXT(statp).nscount = statp->nscount;
424 * Some resolvers want to even out the load on their nameservers.
425 * Note that RES_BLAST overrides RES_ROTATE.
427 if (__glibc_unlikely ((statp->options & RES_ROTATE) != 0)) {
428 struct sockaddr_in ina;
429 struct sockaddr_in6 *inp;
430 int lastns = statp->nscount - 1;
431 int fd;
433 inp = EXT(statp).nsaddrs[0];
434 ina = statp->nsaddr_list[0];
435 fd = EXT(statp).nssocks[0];
436 for (ns = 0; ns < lastns; ns++) {
437 EXT(statp).nsaddrs[ns] = EXT(statp).nsaddrs[ns + 1];
438 statp->nsaddr_list[ns] = statp->nsaddr_list[ns + 1];
439 EXT(statp).nssocks[ns] = EXT(statp).nssocks[ns + 1];
441 EXT(statp).nsaddrs[lastns] = inp;
442 statp->nsaddr_list[lastns] = ina;
443 EXT(statp).nssocks[lastns] = fd;
447 * Send request, RETRY times, or until successful.
449 for (try = 0; try < statp->retry; try++) {
450 for (ns = 0; ns < statp->nscount; ns++)
452 #ifdef DEBUG
453 char tmpbuf[40];
454 struct sockaddr *nsap = get_nsaddr (statp, ns);
455 #endif
457 same_ns:
458 Dprint(statp->options & RES_DEBUG,
459 (stdout, ";; Querying server (# %d) address = %s\n",
460 ns + 1, inet_ntop(nsap->sa_family,
461 (nsap->sa_family == AF_INET6
462 ? (void *) &((struct sockaddr_in6 *) nsap)->sin6_addr
463 : (void *) &((struct sockaddr_in *) nsap)->sin_addr),
464 tmpbuf, sizeof (tmpbuf))));
466 if (__glibc_unlikely (v_circuit)) {
467 /* Use VC; at most one attempt per server. */
468 try = statp->retry;
469 n = send_vc(statp, buf, buflen, buf2, buflen2,
470 &ans, &anssiz, &terrno,
471 ns, ansp, ansp2, nansp2, resplen2,
472 ansp2_malloced);
473 if (n < 0)
474 return (-1);
475 if (n == 0 && (buf2 == NULL || *resplen2 == 0))
476 goto next_ns;
477 } else {
478 /* Use datagrams. */
479 n = send_dg(statp, buf, buflen, buf2, buflen2,
480 &ans, &anssiz, &terrno,
481 ns, &v_circuit, &gotsomewhere, ansp,
482 ansp2, nansp2, resplen2, ansp2_malloced);
483 if (n < 0)
484 return (-1);
485 if (n == 0 && (buf2 == NULL || *resplen2 == 0))
486 goto next_ns;
487 if (v_circuit)
488 // XXX Check whether both requests failed or
489 // XXX whether one has been answered successfully
490 goto same_ns;
493 resplen = n;
495 Dprint((statp->options & RES_DEBUG) ||
496 ((statp->pfcode & RES_PRF_REPLY) &&
497 (statp->pfcode & RES_PRF_HEAD1)),
498 (stdout, ";; got answer:\n"));
500 DprintQ((statp->options & RES_DEBUG) ||
501 (statp->pfcode & RES_PRF_REPLY),
502 (stdout, "%s", ""),
503 ans, (resplen > anssiz) ? anssiz : resplen);
504 if (buf2 != NULL) {
505 DprintQ((statp->options & RES_DEBUG) ||
506 (statp->pfcode & RES_PRF_REPLY),
507 (stdout, "%s", ""),
508 *ansp2, (*resplen2 > *nansp2) ? *nansp2 : *resplen2);
512 * If we have temporarily opened a virtual circuit,
513 * or if we haven't been asked to keep a socket open,
514 * close the socket.
516 if ((v_circuit && (statp->options & RES_USEVC) == 0) ||
517 (statp->options & RES_STAYOPEN) == 0) {
518 __res_iclose(statp, false);
520 return (resplen);
521 next_ns: ;
522 } /*foreach ns*/
523 } /*foreach retry*/
524 __res_iclose(statp, false);
525 if (!v_circuit) {
526 if (!gotsomewhere)
527 __set_errno (ECONNREFUSED); /* no nameservers found */
528 else
529 __set_errno (ETIMEDOUT); /* no answer obtained */
530 } else
531 __set_errno (terrno);
532 return (-1);
536 res_nsend(res_state statp,
537 const u_char *buf, int buflen, u_char *ans, int anssiz)
539 return __libc_res_nsend(statp, buf, buflen, NULL, 0, ans, anssiz,
540 NULL, NULL, NULL, NULL, NULL);
542 libresolv_hidden_def (res_nsend)
544 /* Private */
546 static struct sockaddr *
547 get_nsaddr (res_state statp, int n)
550 if (statp->nsaddr_list[n].sin_family == 0 && EXT(statp).nsaddrs[n] != NULL)
551 /* EXT(statp).nsaddrs[n] holds an address that is larger than
552 struct sockaddr, and user code did not update
553 statp->nsaddr_list[n]. */
554 return (struct sockaddr *) EXT(statp).nsaddrs[n];
555 else
556 /* User code updated statp->nsaddr_list[n], or statp->nsaddr_list[n]
557 has the same content as EXT(statp).nsaddrs[n]. */
558 return (struct sockaddr *) (void *) &statp->nsaddr_list[n];
561 /* Close the resolver structure, assign zero to *RESPLEN2 if RESPLEN2
562 is not NULL, and return zero. */
563 static int
564 __attribute__ ((warn_unused_result))
565 close_and_return_error (res_state statp, int *resplen2)
567 __res_iclose(statp, false);
568 if (resplen2 != NULL)
569 *resplen2 = 0;
570 return 0;
573 /* The send_vc function is responsible for sending a DNS query over TCP
574 to the nameserver numbered NS from the res_state STATP i.e.
575 EXT(statp).nssocks[ns]. The function supports sending both IPv4 and
576 IPv6 queries at the same serially on the same socket.
578 Please note that for TCP there is no way to disable sending both
579 queries, unlike UDP, which honours RES_SNGLKUP and RES_SNGLKUPREOP
580 and sends the queries serially and waits for the result after each
581 sent query. This implementation should be corrected to honour these
582 options.
584 Please also note that for TCP we send both queries over the same
585 socket one after another. This technically violates best practice
586 since the server is allowed to read the first query, respond, and
587 then close the socket (to service another client). If the server
588 does this, then the remaining second query in the socket data buffer
589 will cause the server to send the client an RST which will arrive
590 asynchronously and the client's OS will likely tear down the socket
591 receive buffer resulting in a potentially short read and lost
592 response data. This will force the client to retry the query again,
593 and this process may repeat until all servers and connection resets
594 are exhausted and then the query will fail. It's not known if this
595 happens with any frequency in real DNS server implementations. This
596 implementation should be corrected to use two sockets by default for
597 parallel queries.
599 The query stored in BUF of BUFLEN length is sent first followed by
600 the query stored in BUF2 of BUFLEN2 length. Queries are sent
601 serially on the same socket.
603 Answers to the query are stored firstly in *ANSP up to a max of
604 *ANSSIZP bytes. If more than *ANSSIZP bytes are needed and ANSCP
605 is non-NULL (to indicate that modifying the answer buffer is allowed)
606 then malloc is used to allocate a new response buffer and ANSCP and
607 ANSP will both point to the new buffer. If more than *ANSSIZP bytes
608 are needed but ANSCP is NULL, then as much of the response as
609 possible is read into the buffer, but the results will be truncated.
610 When truncation happens because of a small answer buffer the DNS
611 packets header field TC will bet set to 1, indicating a truncated
612 message and the rest of the socket data will be read and discarded.
614 Answers to the query are stored secondly in *ANSP2 up to a max of
615 *ANSSIZP2 bytes, with the actual response length stored in
616 *RESPLEN2. If more than *ANSSIZP bytes are needed and ANSP2
617 is non-NULL (required for a second query) then malloc is used to
618 allocate a new response buffer, *ANSSIZP2 is set to the new buffer
619 size and *ANSP2_MALLOCED is set to 1.
621 The ANSP2_MALLOCED argument will eventually be removed as the
622 change in buffer pointer can be used to detect the buffer has
623 changed and that the caller should use free on the new buffer.
625 Note that the answers may arrive in any order from the server and
626 therefore the first and second answer buffers may not correspond to
627 the first and second queries.
629 It is not supported to call this function with a non-NULL ANSP2
630 but a NULL ANSCP. Put another way, you can call send_vc with a
631 single unmodifiable buffer or two modifiable buffers, but no other
632 combination is supported.
634 It is the caller's responsibility to free the malloc allocated
635 buffers by detecting that the pointers have changed from their
636 original values i.e. *ANSCP or *ANSP2 has changed.
638 If errors are encountered then *TERRNO is set to an appropriate
639 errno value and a zero result is returned for a recoverable error,
640 and a less-than zero result is returned for a non-recoverable error.
642 If no errors are encountered then *TERRNO is left unmodified and
643 a the length of the first response in bytes is returned. */
644 static int
645 send_vc(res_state statp,
646 const u_char *buf, int buflen, const u_char *buf2, int buflen2,
647 u_char **ansp, int *anssizp,
648 int *terrno, int ns, u_char **anscp, u_char **ansp2, int *anssizp2,
649 int *resplen2, int *ansp2_malloced)
651 const HEADER *hp = (HEADER *) buf;
652 const HEADER *hp2 = (HEADER *) buf2;
653 HEADER *anhp = (HEADER *) *ansp;
654 struct sockaddr *nsap = get_nsaddr (statp, ns);
655 int truncating, connreset, n;
656 /* On some architectures compiler might emit a warning indicating
657 'resplen' may be used uninitialized. However if buf2 == NULL
658 then this code won't be executed; if buf2 != NULL, then first
659 time round the loop recvresp1 and recvresp2 will be 0 so this
660 code won't be executed but "thisresplenp = &resplen;" followed
661 by "*thisresplenp = rlen;" will be executed so that subsequent
662 times round the loop resplen has been initialized. So this is
663 a false-positive.
665 DIAG_PUSH_NEEDS_COMMENT;
666 DIAG_IGNORE_NEEDS_COMMENT (5, "-Wmaybe-uninitialized");
667 int resplen;
668 DIAG_POP_NEEDS_COMMENT;
669 struct iovec iov[4];
670 u_short len;
671 u_short len2;
672 u_char *cp;
674 connreset = 0;
675 same_ns:
676 truncating = 0;
678 /* Are we still talking to whom we want to talk to? */
679 if (statp->_vcsock >= 0 && (statp->_flags & RES_F_VC) != 0) {
680 struct sockaddr_in6 peer;
681 socklen_t size = sizeof peer;
683 if (getpeername(statp->_vcsock,
684 (struct sockaddr *)&peer, &size) < 0 ||
685 !sock_eq(&peer, (struct sockaddr_in6 *) nsap)) {
686 __res_iclose(statp, false);
687 statp->_flags &= ~RES_F_VC;
691 if (statp->_vcsock < 0 || (statp->_flags & RES_F_VC) == 0) {
692 if (statp->_vcsock >= 0)
693 __res_iclose(statp, false);
695 statp->_vcsock = socket
696 (nsap->sa_family, SOCK_STREAM | SOCK_CLOEXEC, 0);
697 if (statp->_vcsock < 0) {
698 *terrno = errno;
699 Perror(statp, stderr, "socket(vc)", errno);
700 if (resplen2 != NULL)
701 *resplen2 = 0;
702 return (-1);
704 __set_errno (0);
705 if (connect(statp->_vcsock, nsap,
706 nsap->sa_family == AF_INET
707 ? sizeof (struct sockaddr_in)
708 : sizeof (struct sockaddr_in6)) < 0) {
709 *terrno = errno;
710 Aerror(statp, stderr, "connect/vc", errno, nsap);
711 return close_and_return_error (statp, resplen2);
713 statp->_flags |= RES_F_VC;
717 * Send length & message
719 len = htons ((u_short) buflen);
720 evConsIovec(&len, INT16SZ, &iov[0]);
721 evConsIovec((void*)buf, buflen, &iov[1]);
722 int niov = 2;
723 ssize_t explen = INT16SZ + buflen;
724 if (buf2 != NULL) {
725 len2 = htons ((u_short) buflen2);
726 evConsIovec(&len2, INT16SZ, &iov[2]);
727 evConsIovec((void*)buf2, buflen2, &iov[3]);
728 niov = 4;
729 explen += INT16SZ + buflen2;
731 if (TEMP_FAILURE_RETRY (writev(statp->_vcsock, iov, niov)) != explen) {
732 *terrno = errno;
733 Perror(statp, stderr, "write failed", errno);
734 return close_and_return_error (statp, resplen2);
737 * Receive length & response
739 int recvresp1 = 0;
740 /* Skip the second response if there is no second query.
741 To do that we mark the second response as received. */
742 int recvresp2 = buf2 == NULL;
743 uint16_t rlen16;
744 read_len:
745 cp = (u_char *)&rlen16;
746 len = sizeof(rlen16);
747 while ((n = TEMP_FAILURE_RETRY (read(statp->_vcsock, cp,
748 (int)len))) > 0) {
749 cp += n;
750 if ((len -= n) <= 0)
751 break;
753 if (n <= 0) {
754 *terrno = errno;
755 Perror(statp, stderr, "read failed", errno);
757 * A long running process might get its TCP
758 * connection reset if the remote server was
759 * restarted. Requery the server instead of
760 * trying a new one. When there is only one
761 * server, this means that a query might work
762 * instead of failing. We only allow one reset
763 * per query to prevent looping.
765 if (*terrno == ECONNRESET && !connreset)
767 __res_iclose (statp, false);
768 connreset = 1;
769 goto same_ns;
771 return close_and_return_error (statp, resplen2);
773 int rlen = ntohs (rlen16);
775 int *thisanssizp;
776 u_char **thisansp;
777 int *thisresplenp;
778 if ((recvresp1 | recvresp2) == 0 || buf2 == NULL) {
779 /* We have not received any responses
780 yet or we only have one response to
781 receive. */
782 thisanssizp = anssizp;
783 thisansp = anscp ?: ansp;
784 assert (anscp != NULL || ansp2 == NULL);
785 thisresplenp = &resplen;
786 } else {
787 thisanssizp = anssizp2;
788 thisansp = ansp2;
789 thisresplenp = resplen2;
791 anhp = (HEADER *) *thisansp;
793 *thisresplenp = rlen;
794 /* Is the answer buffer too small? */
795 if (*thisanssizp < rlen) {
796 /* If the current buffer is not the the static
797 user-supplied buffer then we can reallocate
798 it. */
799 if (thisansp != NULL && thisansp != ansp) {
800 /* Always allocate MAXPACKET, callers expect
801 this specific size. */
802 u_char *newp = malloc (MAXPACKET);
803 if (newp == NULL)
805 *terrno = ENOMEM;
806 return close_and_return_error (statp, resplen2);
808 *thisanssizp = MAXPACKET;
809 *thisansp = newp;
810 if (thisansp == ansp2)
811 *ansp2_malloced = 1;
812 anhp = (HEADER *) newp;
813 /* A uint16_t can't be larger than MAXPACKET
814 thus it's safe to allocate MAXPACKET but
815 read RLEN bytes instead. */
816 len = rlen;
817 } else {
818 Dprint(statp->options & RES_DEBUG,
819 (stdout, ";; response truncated\n")
821 truncating = 1;
822 len = *thisanssizp;
824 } else
825 len = rlen;
827 if (__glibc_unlikely (len < HFIXEDSZ)) {
829 * Undersized message.
831 Dprint(statp->options & RES_DEBUG,
832 (stdout, ";; undersized: %d\n", len));
833 *terrno = EMSGSIZE;
834 return close_and_return_error (statp, resplen2);
837 cp = *thisansp;
838 while (len != 0 && (n = read(statp->_vcsock, (char *)cp, (int)len)) > 0){
839 cp += n;
840 len -= n;
842 if (__glibc_unlikely (n <= 0)) {
843 *terrno = errno;
844 Perror(statp, stderr, "read(vc)", errno);
845 return close_and_return_error (statp, resplen2);
847 if (__glibc_unlikely (truncating)) {
849 * Flush rest of answer so connection stays in synch.
851 anhp->tc = 1;
852 len = rlen - *thisanssizp;
853 while (len != 0) {
854 char junk[PACKETSZ];
856 n = read(statp->_vcsock, junk,
857 (len > sizeof junk) ? sizeof junk : len);
858 if (n > 0)
859 len -= n;
860 else
861 break;
865 * If the calling application has bailed out of
866 * a previous call and failed to arrange to have
867 * the circuit closed or the server has got
868 * itself confused, then drop the packet and
869 * wait for the correct one.
871 if ((recvresp1 || hp->id != anhp->id)
872 && (recvresp2 || hp2->id != anhp->id)) {
873 DprintQ((statp->options & RES_DEBUG) ||
874 (statp->pfcode & RES_PRF_REPLY),
875 (stdout, ";; old answer (unexpected):\n"),
876 *thisansp,
877 (rlen > *thisanssizp) ? *thisanssizp: rlen);
878 goto read_len;
881 /* Mark which reply we received. */
882 if (recvresp1 == 0 && hp->id == anhp->id)
883 recvresp1 = 1;
884 else
885 recvresp2 = 1;
886 /* Repeat waiting if we have a second answer to arrive. */
887 if ((recvresp1 & recvresp2) == 0)
888 goto read_len;
891 * All is well, or the error is fatal. Signal that the
892 * next nameserver ought not be tried.
894 return resplen;
897 static int
898 reopen (res_state statp, int *terrno, int ns)
900 if (EXT(statp).nssocks[ns] == -1) {
901 struct sockaddr *nsap = get_nsaddr (statp, ns);
902 socklen_t slen;
904 /* only try IPv6 if IPv6 NS and if not failed before */
905 if (nsap->sa_family == AF_INET6 && !statp->ipv6_unavail) {
906 EXT(statp).nssocks[ns] = socket
907 (PF_INET6,
908 SOCK_DGRAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
909 if (EXT(statp).nssocks[ns] < 0)
910 statp->ipv6_unavail = errno == EAFNOSUPPORT;
911 slen = sizeof (struct sockaddr_in6);
912 } else if (nsap->sa_family == AF_INET) {
913 EXT(statp).nssocks[ns] = socket
914 (PF_INET,
915 SOCK_DGRAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
916 slen = sizeof (struct sockaddr_in);
918 if (EXT(statp).nssocks[ns] < 0) {
919 *terrno = errno;
920 Perror(statp, stderr, "socket(dg)", errno);
921 return (-1);
925 * On a 4.3BSD+ machine (client and server,
926 * actually), sending to a nameserver datagram
927 * port with no nameserver will cause an
928 * ICMP port unreachable message to be returned.
929 * If our datagram socket is "connected" to the
930 * server, we get an ECONNREFUSED error on the next
931 * socket operation, and select returns if the
932 * error message is received. We can thus detect
933 * the absence of a nameserver without timing out.
935 /* With GCC 5.3 when compiling with -Os the compiler
936 emits a warning that slen may be used uninitialized,
937 but that is never true. Both slen and
938 EXT(statp).nssocks[ns] are initialized together or
939 the function return -1 before control flow reaches
940 the call to connect with slen. */
941 DIAG_PUSH_NEEDS_COMMENT;
942 DIAG_IGNORE_Os_NEEDS_COMMENT (5, "-Wmaybe-uninitialized");
943 if (connect(EXT(statp).nssocks[ns], nsap, slen) < 0) {
944 DIAG_POP_NEEDS_COMMENT;
945 Aerror(statp, stderr, "connect(dg)", errno, nsap);
946 __res_iclose(statp, false);
947 return (0);
951 return 1;
954 /* The send_dg function is responsible for sending a DNS query over UDP
955 to the nameserver numbered NS from the res_state STATP i.e.
956 EXT(statp).nssocks[ns]. The function supports IPv4 and IPv6 queries
957 along with the ability to send the query in parallel for both stacks
958 (default) or serially (RES_SINGLKUP). It also supports serial lookup
959 with a close and reopen of the socket used to talk to the server
960 (RES_SNGLKUPREOP) to work around broken name servers.
962 The query stored in BUF of BUFLEN length is sent first followed by
963 the query stored in BUF2 of BUFLEN2 length. Queries are sent
964 in parallel (default) or serially (RES_SINGLKUP or RES_SNGLKUPREOP).
966 Answers to the query are stored firstly in *ANSP up to a max of
967 *ANSSIZP bytes. If more than *ANSSIZP bytes are needed and ANSCP
968 is non-NULL (to indicate that modifying the answer buffer is allowed)
969 then malloc is used to allocate a new response buffer and ANSCP and
970 ANSP will both point to the new buffer. If more than *ANSSIZP bytes
971 are needed but ANSCP is NULL, then as much of the response as
972 possible is read into the buffer, but the results will be truncated.
973 When truncation happens because of a small answer buffer the DNS
974 packets header field TC will bet set to 1, indicating a truncated
975 message, while the rest of the UDP packet is discarded.
977 Answers to the query are stored secondly in *ANSP2 up to a max of
978 *ANSSIZP2 bytes, with the actual response length stored in
979 *RESPLEN2. If more than *ANSSIZP bytes are needed and ANSP2
980 is non-NULL (required for a second query) then malloc is used to
981 allocate a new response buffer, *ANSSIZP2 is set to the new buffer
982 size and *ANSP2_MALLOCED is set to 1.
984 The ANSP2_MALLOCED argument will eventually be removed as the
985 change in buffer pointer can be used to detect the buffer has
986 changed and that the caller should use free on the new buffer.
988 Note that the answers may arrive in any order from the server and
989 therefore the first and second answer buffers may not correspond to
990 the first and second queries.
992 It is not supported to call this function with a non-NULL ANSP2
993 but a NULL ANSCP. Put another way, you can call send_vc with a
994 single unmodifiable buffer or two modifiable buffers, but no other
995 combination is supported.
997 It is the caller's responsibility to free the malloc allocated
998 buffers by detecting that the pointers have changed from their
999 original values i.e. *ANSCP or *ANSP2 has changed.
1001 If an answer is truncated because of UDP datagram DNS limits then
1002 *V_CIRCUIT is set to 1 and the return value non-zero to indicate to
1003 the caller to retry with TCP. The value *GOTSOMEWHERE is set to 1
1004 if any progress was made reading a response from the nameserver and
1005 is used by the caller to distinguish between ECONNREFUSED and
1006 ETIMEDOUT (the latter if *GOTSOMEWHERE is 1).
1008 If errors are encountered then *TERRNO is set to an appropriate
1009 errno value and a zero result is returned for a recoverable error,
1010 and a less-than zero result is returned for a non-recoverable error.
1012 If no errors are encountered then *TERRNO is left unmodified and
1013 a the length of the first response in bytes is returned. */
1014 static int
1015 send_dg(res_state statp,
1016 const u_char *buf, int buflen, const u_char *buf2, int buflen2,
1017 u_char **ansp, int *anssizp,
1018 int *terrno, int ns, int *v_circuit, int *gotsomewhere, u_char **anscp,
1019 u_char **ansp2, int *anssizp2, int *resplen2, int *ansp2_malloced)
1021 const HEADER *hp = (HEADER *) buf;
1022 const HEADER *hp2 = (HEADER *) buf2;
1023 struct timespec now, timeout, finish;
1024 struct pollfd pfd[1];
1025 int ptimeout;
1026 struct sockaddr_in6 from;
1027 int resplen = 0;
1028 int n;
1031 * Compute time for the total operation.
1033 int seconds = (statp->retrans << ns);
1034 if (ns > 0)
1035 seconds /= statp->nscount;
1036 if (seconds <= 0)
1037 seconds = 1;
1038 bool single_request_reopen = (statp->options & RES_SNGLKUPREOP) != 0;
1039 bool single_request = (((statp->options & RES_SNGLKUP) != 0)
1040 | single_request_reopen);
1041 int save_gotsomewhere = *gotsomewhere;
1043 int retval;
1044 retry_reopen:
1045 retval = reopen (statp, terrno, ns);
1046 if (retval <= 0)
1048 if (resplen2 != NULL)
1049 *resplen2 = 0;
1050 return retval;
1052 retry:
1053 evNowTime(&now);
1054 evConsTime(&timeout, seconds, 0);
1055 evAddTime(&finish, &now, &timeout);
1056 int need_recompute = 0;
1057 int nwritten = 0;
1058 int recvresp1 = 0;
1059 /* Skip the second response if there is no second query.
1060 To do that we mark the second response as received. */
1061 int recvresp2 = buf2 == NULL;
1062 pfd[0].fd = EXT(statp).nssocks[ns];
1063 pfd[0].events = POLLOUT;
1064 wait:
1065 if (need_recompute) {
1066 recompute_resend:
1067 evNowTime(&now);
1068 if (evCmpTime(finish, now) <= 0) {
1069 poll_err_out:
1070 Perror(statp, stderr, "poll", errno);
1071 return close_and_return_error (statp, resplen2);
1073 evSubTime(&timeout, &finish, &now);
1074 need_recompute = 0;
1076 /* Convert struct timespec in milliseconds. */
1077 ptimeout = timeout.tv_sec * 1000 + timeout.tv_nsec / 1000000;
1079 n = 0;
1080 if (nwritten == 0)
1081 n = __poll (pfd, 1, 0);
1082 if (__glibc_unlikely (n == 0)) {
1083 n = __poll (pfd, 1, ptimeout);
1084 need_recompute = 1;
1086 if (n == 0) {
1087 Dprint(statp->options & RES_DEBUG, (stdout, ";; timeout\n"));
1088 if (resplen > 1 && (recvresp1 || (buf2 != NULL && recvresp2)))
1090 /* There are quite a few broken name servers out
1091 there which don't handle two outstanding
1092 requests from the same source. There are also
1093 broken firewall settings. If we time out after
1094 having received one answer switch to the mode
1095 where we send the second request only once we
1096 have received the first answer. */
1097 if (!single_request)
1099 statp->options |= RES_SNGLKUP;
1100 single_request = true;
1101 *gotsomewhere = save_gotsomewhere;
1102 goto retry;
1104 else if (!single_request_reopen)
1106 statp->options |= RES_SNGLKUPREOP;
1107 single_request_reopen = true;
1108 *gotsomewhere = save_gotsomewhere;
1109 __res_iclose (statp, false);
1110 goto retry_reopen;
1113 *resplen2 = 1;
1114 return resplen;
1117 *gotsomewhere = 1;
1118 if (resplen2 != NULL)
1119 *resplen2 = 0;
1120 return 0;
1122 if (n < 0) {
1123 if (errno == EINTR)
1124 goto recompute_resend;
1126 goto poll_err_out;
1128 __set_errno (0);
1129 if (pfd[0].revents & POLLOUT) {
1130 #ifndef __ASSUME_SENDMMSG
1131 static int have_sendmmsg;
1132 #else
1133 # define have_sendmmsg 1
1134 #endif
1135 if (have_sendmmsg >= 0 && nwritten == 0 && buf2 != NULL
1136 && !single_request)
1138 struct iovec iov[2];
1139 struct mmsghdr reqs[2];
1140 reqs[0].msg_hdr.msg_name = NULL;
1141 reqs[0].msg_hdr.msg_namelen = 0;
1142 reqs[0].msg_hdr.msg_iov = &iov[0];
1143 reqs[0].msg_hdr.msg_iovlen = 1;
1144 iov[0].iov_base = (void *) buf;
1145 iov[0].iov_len = buflen;
1146 reqs[0].msg_hdr.msg_control = NULL;
1147 reqs[0].msg_hdr.msg_controllen = 0;
1149 reqs[1].msg_hdr.msg_name = NULL;
1150 reqs[1].msg_hdr.msg_namelen = 0;
1151 reqs[1].msg_hdr.msg_iov = &iov[1];
1152 reqs[1].msg_hdr.msg_iovlen = 1;
1153 iov[1].iov_base = (void *) buf2;
1154 iov[1].iov_len = buflen2;
1155 reqs[1].msg_hdr.msg_control = NULL;
1156 reqs[1].msg_hdr.msg_controllen = 0;
1158 int ndg = __sendmmsg (pfd[0].fd, reqs, 2, MSG_NOSIGNAL);
1159 if (__glibc_likely (ndg == 2))
1161 if (reqs[0].msg_len != buflen
1162 || reqs[1].msg_len != buflen2)
1163 goto fail_sendmmsg;
1165 pfd[0].events = POLLIN;
1166 nwritten += 2;
1168 else if (ndg == 1 && reqs[0].msg_len == buflen)
1169 goto just_one;
1170 else if (ndg < 0 && (errno == EINTR || errno == EAGAIN))
1171 goto recompute_resend;
1172 else
1174 #ifndef __ASSUME_SENDMMSG
1175 if (__glibc_unlikely (have_sendmmsg == 0))
1177 if (ndg < 0 && errno == ENOSYS)
1179 have_sendmmsg = -1;
1180 goto try_send;
1182 have_sendmmsg = 1;
1184 #endif
1186 fail_sendmmsg:
1187 Perror(statp, stderr, "sendmmsg", errno);
1188 return close_and_return_error (statp, resplen2);
1191 else
1193 ssize_t sr;
1194 #ifndef __ASSUME_SENDMMSG
1195 try_send:
1196 #endif
1197 if (nwritten != 0)
1198 sr = send (pfd[0].fd, buf2, buflen2, MSG_NOSIGNAL);
1199 else
1200 sr = send (pfd[0].fd, buf, buflen, MSG_NOSIGNAL);
1202 if (sr != (nwritten != 0 ? buflen2 : buflen)) {
1203 if (errno == EINTR || errno == EAGAIN)
1204 goto recompute_resend;
1205 Perror(statp, stderr, "send", errno);
1206 return close_and_return_error (statp, resplen2);
1208 just_one:
1209 if (nwritten != 0 || buf2 == NULL || single_request)
1210 pfd[0].events = POLLIN;
1211 else
1212 pfd[0].events = POLLIN | POLLOUT;
1213 ++nwritten;
1215 goto wait;
1216 } else if (pfd[0].revents & POLLIN) {
1217 int *thisanssizp;
1218 u_char **thisansp;
1219 int *thisresplenp;
1221 if ((recvresp1 | recvresp2) == 0 || buf2 == NULL) {
1222 /* We have not received any responses
1223 yet or we only have one response to
1224 receive. */
1225 thisanssizp = anssizp;
1226 thisansp = anscp ?: ansp;
1227 assert (anscp != NULL || ansp2 == NULL);
1228 thisresplenp = &resplen;
1229 } else {
1230 thisanssizp = anssizp2;
1231 thisansp = ansp2;
1232 thisresplenp = resplen2;
1235 if (*thisanssizp < MAXPACKET
1236 /* If the current buffer is not the the static
1237 user-supplied buffer then we can reallocate
1238 it. */
1239 && (thisansp != NULL && thisansp != ansp)
1240 #ifdef FIONREAD
1241 /* Is the size too small? */
1242 && (ioctl (pfd[0].fd, FIONREAD, thisresplenp) < 0
1243 || *thisanssizp < *thisresplenp)
1244 #endif
1246 /* Always allocate MAXPACKET, callers expect
1247 this specific size. */
1248 u_char *newp = malloc (MAXPACKET);
1249 if (newp != NULL) {
1250 *thisanssizp = MAXPACKET;
1251 *thisansp = newp;
1252 if (thisansp == ansp2)
1253 *ansp2_malloced = 1;
1256 /* We could end up with truncation if anscp was NULL
1257 (not allowed to change caller's buffer) and the
1258 response buffer size is too small. This isn't a
1259 reliable way to detect truncation because the ioctl
1260 may be an inaccurate report of the UDP message size.
1261 Therefore we use this only to issue debug output.
1262 To do truncation accurately with UDP we need
1263 MSG_TRUNC which is only available on Linux. We
1264 can abstract out the Linux-specific feature in the
1265 future to detect truncation. */
1266 if (__glibc_unlikely (*thisanssizp < *thisresplenp)) {
1267 Dprint(statp->options & RES_DEBUG,
1268 (stdout, ";; response may be truncated (UDP)\n")
1272 HEADER *anhp = (HEADER *) *thisansp;
1273 socklen_t fromlen = sizeof(struct sockaddr_in6);
1274 assert (sizeof(from) <= fromlen);
1275 *thisresplenp = recvfrom(pfd[0].fd, (char*)*thisansp,
1276 *thisanssizp, 0,
1277 (struct sockaddr *)&from, &fromlen);
1278 if (__glibc_unlikely (*thisresplenp <= 0)) {
1279 if (errno == EINTR || errno == EAGAIN) {
1280 need_recompute = 1;
1281 goto wait;
1283 Perror(statp, stderr, "recvfrom", errno);
1284 return close_and_return_error (statp, resplen2);
1286 *gotsomewhere = 1;
1287 if (__glibc_unlikely (*thisresplenp < HFIXEDSZ)) {
1289 * Undersized message.
1291 Dprint(statp->options & RES_DEBUG,
1292 (stdout, ";; undersized: %d\n",
1293 *thisresplenp));
1294 *terrno = EMSGSIZE;
1295 return close_and_return_error (statp, resplen2);
1297 if ((recvresp1 || hp->id != anhp->id)
1298 && (recvresp2 || hp2->id != anhp->id)) {
1300 * response from old query, ignore it.
1301 * XXX - potential security hazard could
1302 * be detected here.
1304 DprintQ((statp->options & RES_DEBUG) ||
1305 (statp->pfcode & RES_PRF_REPLY),
1306 (stdout, ";; old answer:\n"),
1307 *thisansp,
1308 (*thisresplenp > *thisanssizp)
1309 ? *thisanssizp : *thisresplenp);
1310 goto wait;
1312 if (!(statp->options & RES_INSECURE1) &&
1313 !res_ourserver_p(statp, &from)) {
1315 * response from wrong server? ignore it.
1316 * XXX - potential security hazard could
1317 * be detected here.
1319 DprintQ((statp->options & RES_DEBUG) ||
1320 (statp->pfcode & RES_PRF_REPLY),
1321 (stdout, ";; not our server:\n"),
1322 *thisansp,
1323 (*thisresplenp > *thisanssizp)
1324 ? *thisanssizp : *thisresplenp);
1325 goto wait;
1327 if (!(statp->options & RES_INSECURE2)
1328 && (recvresp1 || !res_queriesmatch(buf, buf + buflen,
1329 *thisansp,
1330 *thisansp
1331 + *thisanssizp))
1332 && (recvresp2 || !res_queriesmatch(buf2, buf2 + buflen2,
1333 *thisansp,
1334 *thisansp
1335 + *thisanssizp))) {
1337 * response contains wrong query? ignore it.
1338 * XXX - potential security hazard could
1339 * be detected here.
1341 DprintQ((statp->options & RES_DEBUG) ||
1342 (statp->pfcode & RES_PRF_REPLY),
1343 (stdout, ";; wrong query name:\n"),
1344 *thisansp,
1345 (*thisresplenp > *thisanssizp)
1346 ? *thisanssizp : *thisresplenp);
1347 goto wait;
1349 if (anhp->rcode == SERVFAIL ||
1350 anhp->rcode == NOTIMP ||
1351 anhp->rcode == REFUSED) {
1352 DprintQ(statp->options & RES_DEBUG,
1353 (stdout, "server rejected query:\n"),
1354 *thisansp,
1355 (*thisresplenp > *thisanssizp)
1356 ? *thisanssizp : *thisresplenp);
1358 next_ns:
1359 if (recvresp1 || (buf2 != NULL && recvresp2)) {
1360 *resplen2 = 0;
1361 return resplen;
1363 if (buf2 != NULL)
1365 /* No data from the first reply. */
1366 resplen = 0;
1367 /* We are waiting for a possible second reply. */
1368 if (hp->id == anhp->id)
1369 recvresp1 = 1;
1370 else
1371 recvresp2 = 1;
1373 goto wait;
1376 /* don't retry if called from dig */
1377 if (!statp->pfcode)
1378 return close_and_return_error (statp, resplen2);
1379 __res_iclose(statp, false);
1381 if (anhp->rcode == NOERROR && anhp->ancount == 0
1382 && anhp->aa == 0 && anhp->ra == 0 && anhp->arcount == 0) {
1383 DprintQ(statp->options & RES_DEBUG,
1384 (stdout, "referred query:\n"),
1385 *thisansp,
1386 (*thisresplenp > *thisanssizp)
1387 ? *thisanssizp : *thisresplenp);
1388 goto next_ns;
1390 if (!(statp->options & RES_IGNTC) && anhp->tc) {
1392 * To get the rest of answer,
1393 * use TCP with same server.
1395 Dprint(statp->options & RES_DEBUG,
1396 (stdout, ";; truncated answer\n"));
1397 *v_circuit = 1;
1398 __res_iclose(statp, false);
1399 // XXX if we have received one reply we could
1400 // XXX use it and not repeat it over TCP...
1401 if (resplen2 != NULL)
1402 *resplen2 = 0;
1403 return (1);
1405 /* Mark which reply we received. */
1406 if (recvresp1 == 0 && hp->id == anhp->id)
1407 recvresp1 = 1;
1408 else
1409 recvresp2 = 1;
1410 /* Repeat waiting if we have a second answer to arrive. */
1411 if ((recvresp1 & recvresp2) == 0) {
1412 if (single_request) {
1413 pfd[0].events = POLLOUT;
1414 if (single_request_reopen) {
1415 __res_iclose (statp, false);
1416 retval = reopen (statp, terrno, ns);
1417 if (retval <= 0)
1419 if (resplen2 != NULL)
1420 *resplen2 = 0;
1421 return retval;
1423 pfd[0].fd = EXT(statp).nssocks[ns];
1426 goto wait;
1428 /* All is well. We have received both responses (if
1429 two responses were requested). */
1430 return (resplen);
1431 } else if (pfd[0].revents & (POLLERR | POLLHUP | POLLNVAL))
1432 /* Something went wrong. We can stop trying. */
1433 return close_and_return_error (statp, resplen2);
1434 else {
1435 /* poll should not have returned > 0 in this case. */
1436 abort ();
1440 #ifdef DEBUG
1441 static void
1442 Aerror(const res_state statp, FILE *file, const char *string, int error,
1443 const struct sockaddr *address)
1445 int save = errno;
1447 if ((statp->options & RES_DEBUG) != 0) {
1448 char tmp[sizeof "xxxx.xxxx.xxxx.255.255.255.255"];
1450 fprintf(file, "res_send: %s ([%s].%u): %s\n",
1451 string,
1452 (address->sa_family == AF_INET
1453 ? inet_ntop(address->sa_family,
1454 &((const struct sockaddr_in *) address)->sin_addr,
1455 tmp, sizeof tmp)
1456 : inet_ntop(address->sa_family,
1457 &((const struct sockaddr_in6 *) address)->sin6_addr,
1458 tmp, sizeof tmp)),
1459 (address->sa_family == AF_INET
1460 ? ntohs(((struct sockaddr_in *) address)->sin_port)
1461 : address->sa_family == AF_INET6
1462 ? ntohs(((struct sockaddr_in6 *) address)->sin6_port)
1463 : 0),
1464 strerror(error));
1466 __set_errno (save);
1469 static void
1470 Perror(const res_state statp, FILE *file, const char *string, int error) {
1471 int save = errno;
1473 if ((statp->options & RES_DEBUG) != 0)
1474 fprintf(file, "res_send: %s: %s\n",
1475 string, strerror(error));
1476 __set_errno (save);
1478 #endif
1480 static int
1481 sock_eq(struct sockaddr_in6 *a1, struct sockaddr_in6 *a2) {
1482 if (a1->sin6_family == a2->sin6_family) {
1483 if (a1->sin6_family == AF_INET)
1484 return ((((struct sockaddr_in *)a1)->sin_port ==
1485 ((struct sockaddr_in *)a2)->sin_port) &&
1486 (((struct sockaddr_in *)a1)->sin_addr.s_addr ==
1487 ((struct sockaddr_in *)a2)->sin_addr.s_addr));
1488 else
1489 return ((a1->sin6_port == a2->sin6_port) &&
1490 !memcmp(&a1->sin6_addr, &a2->sin6_addr,
1491 sizeof (struct in6_addr)));
1493 if (a1->sin6_family == AF_INET) {
1494 struct sockaddr_in6 *sap = a1;
1495 a1 = a2;
1496 a2 = sap;
1497 } /* assumes that AF_INET and AF_INET6 are the only possibilities */
1498 return ((a1->sin6_port == ((struct sockaddr_in *)a2)->sin_port) &&
1499 IN6_IS_ADDR_V4MAPPED(&a1->sin6_addr) &&
1500 (a1->sin6_addr.s6_addr32[3] ==
1501 ((struct sockaddr_in *)a2)->sin_addr.s_addr));