1 /* Copyright (C) 2016 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
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
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
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
85 * Send query to name server and wait for reply.
89 #include <sys/types.h>
90 #include <sys/param.h>
92 #include <sys/socket.h>
96 #include <netinet/in.h>
97 #include <arpa/nameser.h>
98 #include <arpa/inet.h>
99 #include <sys/ioctl.h>
110 #include <kernel-features.h>
111 #include <libc-internal.h>
114 #define MAXPACKET PACKETSZ
116 #define MAXPACKET 65536
119 /* From ev_streams.c. */
122 __attribute ((always_inline
))
123 evConsIovec(void *buf
, size_t cnt
, struct iovec
*vec
) {
124 memset(vec
, 0xf5, sizeof (*vec
));
129 /* From ev_timers.c. */
131 #define BILLION 1000000000
134 evConsTime(struct timespec
*res
, time_t sec
, long nsec
) {
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
) {
146 res
->tv_nsec
-= BILLION
;
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
;
157 res
->tv_nsec
= (BILLION
158 - subtrahend
->tv_nsec
+ minuend
->tv_nsec
);
164 evCmpTime(struct timespec a
, struct timespec b
) {
165 long x
= a
.tv_sec
- b
.tv_sec
;
168 x
= a
.tv_nsec
- b
.tv_nsec
;
169 return (x
< 0L ? (-1) : x
> 0L ? (1) : (0));
173 evNowTime(struct timespec
*res
) {
176 if (gettimeofday(&now
, NULL
) < 0)
177 evConsTime(res
, 0, 0);
179 TIMEVAL_TO_TIMESPEC (&now
, res
);
183 /* Options. Leave them on. */
185 #include "res_debug.h"
187 #define EXT(res) ((res)->_u._ext)
191 static struct sockaddr
*get_nsaddr (res_state
, int);
192 static int send_vc(res_state
, 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,
198 u_char
**, int *, int *, int,
199 int *, int *, u_char
**,
200 u_char
**, int *, int *, int *);
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);
206 static int sock_eq(struct sockaddr_in6
*, struct sockaddr_in6
*);
211 * res_isourserver(ina)
212 * looks up "ina" in _res.ns_addr_list[]
217 * paul vixie, 29may94
220 res_ourserver_p(const res_state statp
, const struct sockaddr_in6
*inp
)
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
))
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
))))
256 * res_nameinquery(name, type, class, buf, eom)
257 * look for (name,type,class) in the query section of packet (buf,eom)
259 * buf + HFIXEDSZ <= eom
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
);
282 if (cp
+ 2 * INT16SZ
> eom
)
285 NS_GET16(tclass
, cp
);
286 if (ttype
== type
&& tclass
== class &&
287 ns_samename(tname
, name
) == 1)
292 libresolv_hidden_def (res_nameinquery
)
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)?
300 * 0 : not a 1:1 mapping
301 * >0 : is a 1:1 mapping
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
)
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
))
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
)
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
);
338 if (cp
+ 2 * INT16SZ
> eom1
)
341 NS_GET16(tclass
, cp
);
342 if (!res_nameinquery(tname
, ttype
, tclass
, buf2
, eom2
))
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) {
362 if (anssiz
< (buf2
== NULL
? 1 : 2) * HFIXEDSZ
) {
363 __set_errno (EINVAL
);
368 if (__glibc_unlikely (statp
->qhook
|| statp
->rhook
)) {
369 if (anssiz
< MAXPACKET
&& ansp
) {
370 /* Always allocate MAXPACKET, callers expect
371 this specific size. */
372 u_char
*buf
= malloc (MAXPACKET
);
375 memcpy (buf
, ans
, HFIXEDSZ
);
383 DprintQ((statp
->options
& RES_DEBUG
) || (statp
->pfcode
& RES_PRF_QUERY
),
384 (stdout
, ";; res_send()\n"), buf
, buflen
);
385 v_circuit
= ((statp
->options
& RES_USEVC
)
387 || buflen2
> PACKETSZ
);
392 * If the ns_addr_list in the resolver context has changed, then
393 * invalidate our cached copy and the associated timing data.
395 if (EXT(statp
).nscount
!= 0) {
398 if (EXT(statp
).nscount
!= statp
->nscount
)
401 for (ns
= 0; ns
< statp
->nscount
; ns
++) {
402 if (statp
->nsaddr_list
[ns
].sin_family
!= 0
403 && !sock_eq((struct sockaddr_in6
*)
404 &statp
->nsaddr_list
[ns
],
405 EXT(statp
).nsaddrs
[ns
]))
412 __res_iclose(statp
, false);
413 EXT(statp
).nscount
= 0;
418 * Maybe initialize our private copy of the ns_addr_list.
420 if (EXT(statp
).nscount
== 0) {
421 for (ns
= 0; ns
< statp
->nscount
; ns
++) {
422 EXT(statp
).nssocks
[ns
] = -1;
423 if (statp
->nsaddr_list
[ns
].sin_family
== 0)
425 if (EXT(statp
).nsaddrs
[ns
] == NULL
)
426 EXT(statp
).nsaddrs
[ns
] =
427 malloc(sizeof (struct sockaddr_in6
));
428 if (EXT(statp
).nsaddrs
[ns
] != NULL
)
429 memset (mempcpy(EXT(statp
).nsaddrs
[ns
],
430 &statp
->nsaddr_list
[ns
],
431 sizeof (struct sockaddr_in
)),
433 sizeof (struct sockaddr_in6
)
434 - sizeof (struct sockaddr_in
));
436 EXT(statp
).nscount
= statp
->nscount
;
440 * Some resolvers want to even out the load on their nameservers.
441 * Note that RES_BLAST overrides RES_ROTATE.
443 if (__builtin_expect ((statp
->options
& RES_ROTATE
) != 0, 0) &&
444 (statp
->options
& RES_BLAST
) == 0) {
445 struct sockaddr_in ina
;
446 struct sockaddr_in6
*inp
;
447 int lastns
= statp
->nscount
- 1;
450 inp
= EXT(statp
).nsaddrs
[0];
451 ina
= statp
->nsaddr_list
[0];
452 fd
= EXT(statp
).nssocks
[0];
453 for (ns
= 0; ns
< lastns
; ns
++) {
454 EXT(statp
).nsaddrs
[ns
] = EXT(statp
).nsaddrs
[ns
+ 1];
455 statp
->nsaddr_list
[ns
] = statp
->nsaddr_list
[ns
+ 1];
456 EXT(statp
).nssocks
[ns
] = EXT(statp
).nssocks
[ns
+ 1];
458 EXT(statp
).nsaddrs
[lastns
] = inp
;
459 statp
->nsaddr_list
[lastns
] = ina
;
460 EXT(statp
).nssocks
[lastns
] = fd
;
464 * Send request, RETRY times, or until successful.
466 for (try = 0; try < statp
->retry
; try++) {
467 for (ns
= 0; ns
< statp
->nscount
; ns
++)
472 #if defined USE_HOOKS || defined DEBUG
473 struct sockaddr
*nsap
= get_nsaddr (statp
, ns
);
478 if (__glibc_unlikely (statp
->qhook
!= NULL
)) {
479 int done
= 0, loops
= 0;
484 struct sockaddr_in
*nsap4
;
485 nsap4
= (struct sockaddr_in
*) nsap
;
486 act
= (*statp
->qhook
)(&nsap4
, &buf
, &buflen
,
487 ans
, anssiz
, &resplen
);
488 nsap
= (struct sockaddr_in6
*) nsap4
;
494 __res_iclose(statp
, false);
499 /* give the hook another try */
500 if (++loops
< 42) /*doug adams*/
512 Dprint(statp
->options
& RES_DEBUG
,
513 (stdout
, ";; Querying server (# %d) address = %s\n",
514 ns
+ 1, inet_ntop(nsap
->sa_family
,
515 (nsap
->sa_family
== AF_INET6
516 ? (void *) &((struct sockaddr_in6
*) nsap
)->sin6_addr
517 : (void *) &((struct sockaddr_in
*) nsap
)->sin_addr
),
518 tmpbuf
, sizeof (tmpbuf
))));
520 if (__glibc_unlikely (v_circuit
)) {
521 /* Use VC; at most one attempt per server. */
523 n
= send_vc(statp
, buf
, buflen
, buf2
, buflen2
,
524 &ans
, &anssiz
, &terrno
,
525 ns
, ansp
, ansp2
, nansp2
, resplen2
,
529 if (n
== 0 && (buf2
== NULL
|| *resplen2
== 0))
533 n
= send_dg(statp
, buf
, buflen
, buf2
, buflen2
,
534 &ans
, &anssiz
, &terrno
,
535 ns
, &v_circuit
, &gotsomewhere
, ansp
,
536 ansp2
, nansp2
, resplen2
, ansp2_malloced
);
539 if (n
== 0 && (buf2
== NULL
|| *resplen2
== 0))
542 // XXX Check whether both requests failed or
543 // XXX whether one has been answered successfully
549 Dprint((statp
->options
& RES_DEBUG
) ||
550 ((statp
->pfcode
& RES_PRF_REPLY
) &&
551 (statp
->pfcode
& RES_PRF_HEAD1
)),
552 (stdout
, ";; got answer:\n"));
554 DprintQ((statp
->options
& RES_DEBUG
) ||
555 (statp
->pfcode
& RES_PRF_REPLY
),
557 ans
, (resplen
> anssiz
) ? anssiz
: resplen
);
559 DprintQ((statp
->options
& RES_DEBUG
) ||
560 (statp
->pfcode
& RES_PRF_REPLY
),
562 *ansp2
, (*resplen2
> *nansp2
) ? *nansp2
: *resplen2
);
566 * If we have temporarily opened a virtual circuit,
567 * or if we haven't been asked to keep a socket open,
570 if ((v_circuit
&& (statp
->options
& RES_USEVC
) == 0) ||
571 (statp
->options
& RES_STAYOPEN
) == 0) {
572 __res_iclose(statp
, false);
575 if (__glibc_unlikely (statp
->rhook
)) {
576 int done
= 0, loops
= 0;
581 act
= (*statp
->rhook
)((struct sockaddr_in
*)
583 ans
, anssiz
, &resplen
);
590 __res_iclose(statp
, false);
593 /* give the hook another try */
594 if (++loops
< 42) /*doug adams*/
610 __res_iclose(statp
, false);
613 __set_errno (ECONNREFUSED
); /* no nameservers found */
615 __set_errno (ETIMEDOUT
); /* no answer obtained */
617 __set_errno (terrno
);
622 res_nsend(res_state statp
,
623 const u_char
*buf
, int buflen
, u_char
*ans
, int anssiz
)
625 return __libc_res_nsend(statp
, buf
, buflen
, NULL
, 0, ans
, anssiz
,
626 NULL
, NULL
, NULL
, NULL
, NULL
);
628 libresolv_hidden_def (res_nsend
)
632 static struct sockaddr
*
633 get_nsaddr (res_state statp
, int n
)
636 if (statp
->nsaddr_list
[n
].sin_family
== 0 && EXT(statp
).nsaddrs
[n
] != NULL
)
637 /* EXT(statp).nsaddrs[n] holds an address that is larger than
638 struct sockaddr, and user code did not update
639 statp->nsaddr_list[n]. */
640 return (struct sockaddr
*) EXT(statp
).nsaddrs
[n
];
642 /* User code updated statp->nsaddr_list[n], or statp->nsaddr_list[n]
643 has the same content as EXT(statp).nsaddrs[n]. */
644 return (struct sockaddr
*) (void *) &statp
->nsaddr_list
[n
];
647 /* Close the resolver structure, assign zero to *RESPLEN2 if RESPLEN2
648 is not NULL, and return zero. */
650 __attribute__ ((warn_unused_result
))
651 close_and_return_error (res_state statp
, int *resplen2
)
653 __res_iclose(statp
, false);
654 if (resplen2
!= NULL
)
659 /* The send_vc function is responsible for sending a DNS query over TCP
660 to the nameserver numbered NS from the res_state STATP i.e.
661 EXT(statp).nssocks[ns]. The function supports sending both IPv4 and
662 IPv6 queries at the same serially on the same socket.
664 Please note that for TCP there is no way to disable sending both
665 queries, unlike UDP, which honours RES_SNGLKUP and RES_SNGLKUPREOP
666 and sends the queries serially and waits for the result after each
667 sent query. This implemetnation should be corrected to honour these
670 Please also note that for TCP we send both queries over the same
671 socket one after another. This technically violates best practice
672 since the server is allowed to read the first query, respond, and
673 then close the socket (to service another client). If the server
674 does this, then the remaining second query in the socket data buffer
675 will cause the server to send the client an RST which will arrive
676 asynchronously and the client's OS will likely tear down the socket
677 receive buffer resulting in a potentially short read and lost
678 response data. This will force the client to retry the query again,
679 and this process may repeat until all servers and connection resets
680 are exhausted and then the query will fail. It's not known if this
681 happens with any frequency in real DNS server implementations. This
682 implementation should be corrected to use two sockets by default for
685 The query stored in BUF of BUFLEN length is sent first followed by
686 the query stored in BUF2 of BUFLEN2 length. Queries are sent
687 serially on the same socket.
689 Answers to the query are stored firstly in *ANSP up to a max of
690 *ANSSIZP bytes. If more than *ANSSIZP bytes are needed and ANSCP
691 is non-NULL (to indicate that modifying the answer buffer is allowed)
692 then malloc is used to allocate a new response buffer and ANSCP and
693 ANSP will both point to the new buffer. If more than *ANSSIZP bytes
694 are needed but ANSCP is NULL, then as much of the response as
695 possible is read into the buffer, but the results will be truncated.
696 When truncation happens because of a small answer buffer the DNS
697 packets header field TC will bet set to 1, indicating a truncated
698 message and the rest of the socket data will be read and discarded.
700 Answers to the query are stored secondly in *ANSP2 up to a max of
701 *ANSSIZP2 bytes, with the actual response length stored in
702 *RESPLEN2. If more than *ANSSIZP bytes are needed and ANSP2
703 is non-NULL (required for a second query) then malloc is used to
704 allocate a new response buffer, *ANSSIZP2 is set to the new buffer
705 size and *ANSP2_MALLOCED is set to 1.
707 The ANSP2_MALLOCED argument will eventually be removed as the
708 change in buffer pointer can be used to detect the buffer has
709 changed and that the caller should use free on the new buffer.
711 Note that the answers may arrive in any order from the server and
712 therefore the first and second answer buffers may not correspond to
713 the first and second queries.
715 It is not supported to call this function with a non-NULL ANSP2
716 but a NULL ANSCP. Put another way, you can call send_vc with a
717 single unmodifiable buffer or two modifiable buffers, but no other
718 combination is supported.
720 It is the caller's responsibility to free the malloc allocated
721 buffers by detecting that the pointers have changed from their
722 original values i.e. *ANSCP or *ANSP2 has changed.
724 If errors are encountered then *TERRNO is set to an appropriate
725 errno value and a zero result is returned for a recoverable error,
726 and a less-than zero result is returned for a non-recoverable error.
728 If no errors are encountered then *TERRNO is left unmodified and
729 a the length of the first response in bytes is returned. */
731 send_vc(res_state statp
,
732 const u_char
*buf
, int buflen
, const u_char
*buf2
, int buflen2
,
733 u_char
**ansp
, int *anssizp
,
734 int *terrno
, int ns
, u_char
**anscp
, u_char
**ansp2
, int *anssizp2
,
735 int *resplen2
, int *ansp2_malloced
)
737 const HEADER
*hp
= (HEADER
*) buf
;
738 const HEADER
*hp2
= (HEADER
*) buf2
;
739 HEADER
*anhp
= (HEADER
*) *ansp
;
740 struct sockaddr
*nsap
= get_nsaddr (statp
, ns
);
741 int truncating
, connreset
, n
;
742 /* On some architectures compiler might emit a warning indicating
743 'resplen' may be used uninitialized. However if buf2 == NULL
744 then this code won't be executed; if buf2 != NULL, then first
745 time round the loop recvresp1 and recvresp2 will be 0 so this
746 code won't be executed but "thisresplenp = &resplen;" followed
747 by "*thisresplenp = rlen;" will be executed so that subsequent
748 times round the loop resplen has been initialized. So this is
751 DIAG_PUSH_NEEDS_COMMENT
;
752 DIAG_IGNORE_NEEDS_COMMENT (5, "-Wmaybe-uninitialized");
754 DIAG_POP_NEEDS_COMMENT
;
764 /* Are we still talking to whom we want to talk to? */
765 if (statp
->_vcsock
>= 0 && (statp
->_flags
& RES_F_VC
) != 0) {
766 struct sockaddr_in6 peer
;
767 socklen_t size
= sizeof peer
;
769 if (getpeername(statp
->_vcsock
,
770 (struct sockaddr
*)&peer
, &size
) < 0 ||
771 !sock_eq(&peer
, (struct sockaddr_in6
*) nsap
)) {
772 __res_iclose(statp
, false);
773 statp
->_flags
&= ~RES_F_VC
;
777 if (statp
->_vcsock
< 0 || (statp
->_flags
& RES_F_VC
) == 0) {
778 if (statp
->_vcsock
>= 0)
779 __res_iclose(statp
, false);
781 statp
->_vcsock
= socket(nsap
->sa_family
, SOCK_STREAM
, 0);
782 if (statp
->_vcsock
< 0) {
784 Perror(statp
, stderr
, "socket(vc)", errno
);
785 if (resplen2
!= NULL
)
790 if (connect(statp
->_vcsock
, nsap
,
791 nsap
->sa_family
== AF_INET
792 ? sizeof (struct sockaddr_in
)
793 : sizeof (struct sockaddr_in6
)) < 0) {
795 Aerror(statp
, stderr
, "connect/vc", errno
, nsap
);
796 return close_and_return_error (statp
, resplen2
);
798 statp
->_flags
|= RES_F_VC
;
802 * Send length & message
804 len
= htons ((u_short
) buflen
);
805 evConsIovec(&len
, INT16SZ
, &iov
[0]);
806 evConsIovec((void*)buf
, buflen
, &iov
[1]);
808 ssize_t explen
= INT16SZ
+ buflen
;
810 len2
= htons ((u_short
) buflen2
);
811 evConsIovec(&len2
, INT16SZ
, &iov
[2]);
812 evConsIovec((void*)buf2
, buflen2
, &iov
[3]);
814 explen
+= INT16SZ
+ buflen2
;
816 if (TEMP_FAILURE_RETRY (writev(statp
->_vcsock
, iov
, niov
)) != explen
) {
818 Perror(statp
, stderr
, "write failed", errno
);
819 return close_and_return_error (statp
, resplen2
);
822 * Receive length & response
825 /* Skip the second response if there is no second query.
826 To do that we mark the second response as received. */
827 int recvresp2
= buf2
== NULL
;
830 cp
= (u_char
*)&rlen16
;
831 len
= sizeof(rlen16
);
832 while ((n
= TEMP_FAILURE_RETRY (read(statp
->_vcsock
, cp
,
840 Perror(statp
, stderr
, "read failed", errno
);
842 * A long running process might get its TCP
843 * connection reset if the remote server was
844 * restarted. Requery the server instead of
845 * trying a new one. When there is only one
846 * server, this means that a query might work
847 * instead of failing. We only allow one reset
848 * per query to prevent looping.
850 if (*terrno
== ECONNRESET
&& !connreset
)
852 __res_iclose (statp
, false);
856 return close_and_return_error (statp
, resplen2
);
858 int rlen
= ntohs (rlen16
);
863 if ((recvresp1
| recvresp2
) == 0 || buf2
== NULL
) {
864 /* We have not received any responses
865 yet or we only have one response to
867 thisanssizp
= anssizp
;
868 thisansp
= anscp
?: ansp
;
869 assert (anscp
!= NULL
|| ansp2
== NULL
);
870 thisresplenp
= &resplen
;
872 thisanssizp
= anssizp2
;
874 thisresplenp
= resplen2
;
876 anhp
= (HEADER
*) *thisansp
;
878 *thisresplenp
= rlen
;
879 /* Is the answer buffer too small? */
880 if (*thisanssizp
< rlen
) {
881 /* If the current buffer is not the the static
882 user-supplied buffer then we can reallocate
884 if (thisansp
!= NULL
&& thisansp
!= ansp
) {
885 /* Always allocate MAXPACKET, callers expect
886 this specific size. */
887 u_char
*newp
= malloc (MAXPACKET
);
891 return close_and_return_error (statp
, resplen2
);
893 *thisanssizp
= MAXPACKET
;
895 if (thisansp
== ansp2
)
897 anhp
= (HEADER
*) newp
;
898 /* A uint16_t can't be larger than MAXPACKET
899 thus it's safe to allocate MAXPACKET but
900 read RLEN bytes instead. */
903 Dprint(statp
->options
& RES_DEBUG
,
904 (stdout
, ";; response truncated\n")
912 if (__glibc_unlikely (len
< HFIXEDSZ
)) {
914 * Undersized message.
916 Dprint(statp
->options
& RES_DEBUG
,
917 (stdout
, ";; undersized: %d\n", len
));
919 return close_and_return_error (statp
, resplen2
);
923 while (len
!= 0 && (n
= read(statp
->_vcsock
, (char *)cp
, (int)len
)) > 0){
927 if (__glibc_unlikely (n
<= 0)) {
929 Perror(statp
, stderr
, "read(vc)", errno
);
930 return close_and_return_error (statp
, resplen2
);
932 if (__glibc_unlikely (truncating
)) {
934 * Flush rest of answer so connection stays in synch.
937 len
= rlen
- *thisanssizp
;
941 n
= read(statp
->_vcsock
, junk
,
942 (len
> sizeof junk
) ? sizeof junk
: len
);
950 * If the calling application has bailed out of
951 * a previous call and failed to arrange to have
952 * the circuit closed or the server has got
953 * itself confused, then drop the packet and
954 * wait for the correct one.
956 if ((recvresp1
|| hp
->id
!= anhp
->id
)
957 && (recvresp2
|| hp2
->id
!= anhp
->id
)) {
958 DprintQ((statp
->options
& RES_DEBUG
) ||
959 (statp
->pfcode
& RES_PRF_REPLY
),
960 (stdout
, ";; old answer (unexpected):\n"),
962 (rlen
> *thisanssizp
) ? *thisanssizp
: rlen
);
966 /* Mark which reply we received. */
967 if (recvresp1
== 0 && hp
->id
== anhp
->id
)
971 /* Repeat waiting if we have a second answer to arrive. */
972 if ((recvresp1
& recvresp2
) == 0)
976 * All is well, or the error is fatal. Signal that the
977 * next nameserver ought not be tried.
983 reopen (res_state statp
, int *terrno
, int ns
)
985 if (EXT(statp
).nssocks
[ns
] == -1) {
986 struct sockaddr
*nsap
= get_nsaddr (statp
, ns
);
989 /* only try IPv6 if IPv6 NS and if not failed before */
990 if (nsap
->sa_family
== AF_INET6
&& !statp
->ipv6_unavail
) {
991 EXT(statp
).nssocks
[ns
]
992 = socket(PF_INET6
, SOCK_DGRAM
|SOCK_NONBLOCK
, 0);
993 if (EXT(statp
).nssocks
[ns
] < 0)
994 statp
->ipv6_unavail
= errno
== EAFNOSUPPORT
;
995 slen
= sizeof (struct sockaddr_in6
);
996 } else if (nsap
->sa_family
== AF_INET
) {
997 EXT(statp
).nssocks
[ns
]
998 = socket(PF_INET
, SOCK_DGRAM
|SOCK_NONBLOCK
, 0);
999 slen
= sizeof (struct sockaddr_in
);
1001 if (EXT(statp
).nssocks
[ns
] < 0) {
1003 Perror(statp
, stderr
, "socket(dg)", errno
);
1008 * On a 4.3BSD+ machine (client and server,
1009 * actually), sending to a nameserver datagram
1010 * port with no nameserver will cause an
1011 * ICMP port unreachable message to be returned.
1012 * If our datagram socket is "connected" to the
1013 * server, we get an ECONNREFUSED error on the next
1014 * socket operation, and select returns if the
1015 * error message is received. We can thus detect
1016 * the absence of a nameserver without timing out.
1018 if (connect(EXT(statp
).nssocks
[ns
], nsap
, slen
) < 0) {
1019 Aerror(statp
, stderr
, "connect(dg)", errno
, nsap
);
1020 __res_iclose(statp
, false);
1028 /* The send_dg function is responsible for sending a DNS query over UDP
1029 to the nameserver numbered NS from the res_state STATP i.e.
1030 EXT(statp).nssocks[ns]. The function supports IPv4 and IPv6 queries
1031 along with the ability to send the query in parallel for both stacks
1032 (default) or serially (RES_SINGLKUP). It also supports serial lookup
1033 with a close and reopen of the socket used to talk to the server
1034 (RES_SNGLKUPREOP) to work around broken name servers.
1036 The query stored in BUF of BUFLEN length is sent first followed by
1037 the query stored in BUF2 of BUFLEN2 length. Queries are sent
1038 in parallel (default) or serially (RES_SINGLKUP or RES_SNGLKUPREOP).
1040 Answers to the query are stored firstly in *ANSP up to a max of
1041 *ANSSIZP bytes. If more than *ANSSIZP bytes are needed and ANSCP
1042 is non-NULL (to indicate that modifying the answer buffer is allowed)
1043 then malloc is used to allocate a new response buffer and ANSCP and
1044 ANSP will both point to the new buffer. If more than *ANSSIZP bytes
1045 are needed but ANSCP is NULL, then as much of the response as
1046 possible is read into the buffer, but the results will be truncated.
1047 When truncation happens because of a small answer buffer the DNS
1048 packets header field TC will bet set to 1, indicating a truncated
1049 message, while the rest of the UDP packet is discarded.
1051 Answers to the query are stored secondly in *ANSP2 up to a max of
1052 *ANSSIZP2 bytes, with the actual response length stored in
1053 *RESPLEN2. If more than *ANSSIZP bytes are needed and ANSP2
1054 is non-NULL (required for a second query) then malloc is used to
1055 allocate a new response buffer, *ANSSIZP2 is set to the new buffer
1056 size and *ANSP2_MALLOCED is set to 1.
1058 The ANSP2_MALLOCED argument will eventually be removed as the
1059 change in buffer pointer can be used to detect the buffer has
1060 changed and that the caller should use free on the new buffer.
1062 Note that the answers may arrive in any order from the server and
1063 therefore the first and second answer buffers may not correspond to
1064 the first and second queries.
1066 It is not supported to call this function with a non-NULL ANSP2
1067 but a NULL ANSCP. Put another way, you can call send_vc with a
1068 single unmodifiable buffer or two modifiable buffers, but no other
1069 combination is supported.
1071 It is the caller's responsibility to free the malloc allocated
1072 buffers by detecting that the pointers have changed from their
1073 original values i.e. *ANSCP or *ANSP2 has changed.
1075 If an answer is truncated because of UDP datagram DNS limits then
1076 *V_CIRCUIT is set to 1 and the return value non-zero to indicate to
1077 the caller to retry with TCP. The value *GOTSOMEWHERE is set to 1
1078 if any progress was made reading a response from the nameserver and
1079 is used by the caller to distinguish between ECONNREFUSED and
1080 ETIMEDOUT (the latter if *GOTSOMEWHERE is 1).
1082 If errors are encountered then *TERRNO is set to an appropriate
1083 errno value and a zero result is returned for a recoverable error,
1084 and a less-than zero result is returned for a non-recoverable error.
1086 If no errors are encountered then *TERRNO is left unmodified and
1087 a the length of the first response in bytes is returned. */
1089 send_dg(res_state statp
,
1090 const u_char
*buf
, int buflen
, const u_char
*buf2
, int buflen2
,
1091 u_char
**ansp
, int *anssizp
,
1092 int *terrno
, int ns
, int *v_circuit
, int *gotsomewhere
, u_char
**anscp
,
1093 u_char
**ansp2
, int *anssizp2
, int *resplen2
, int *ansp2_malloced
)
1095 const HEADER
*hp
= (HEADER
*) buf
;
1096 const HEADER
*hp2
= (HEADER
*) buf2
;
1097 struct timespec now
, timeout
, finish
;
1098 struct pollfd pfd
[1];
1100 struct sockaddr_in6 from
;
1105 * Compute time for the total operation.
1107 int seconds
= (statp
->retrans
<< ns
);
1109 seconds
/= statp
->nscount
;
1112 bool single_request_reopen
= (statp
->options
& RES_SNGLKUPREOP
) != 0;
1113 bool single_request
= (((statp
->options
& RES_SNGLKUP
) != 0)
1114 | single_request_reopen
);
1115 int save_gotsomewhere
= *gotsomewhere
;
1119 retval
= reopen (statp
, terrno
, ns
);
1122 if (resplen2
!= NULL
)
1128 evConsTime(&timeout
, seconds
, 0);
1129 evAddTime(&finish
, &now
, &timeout
);
1130 int need_recompute
= 0;
1133 /* Skip the second response if there is no second query.
1134 To do that we mark the second response as received. */
1135 int recvresp2
= buf2
== NULL
;
1136 pfd
[0].fd
= EXT(statp
).nssocks
[ns
];
1137 pfd
[0].events
= POLLOUT
;
1139 if (need_recompute
) {
1142 if (evCmpTime(finish
, now
) <= 0) {
1144 Perror(statp
, stderr
, "poll", errno
);
1145 return close_and_return_error (statp
, resplen2
);
1147 evSubTime(&timeout
, &finish
, &now
);
1150 /* Convert struct timespec in milliseconds. */
1151 ptimeout
= timeout
.tv_sec
* 1000 + timeout
.tv_nsec
/ 1000000;
1155 n
= __poll (pfd
, 1, 0);
1156 if (__glibc_unlikely (n
== 0)) {
1157 n
= __poll (pfd
, 1, ptimeout
);
1161 Dprint(statp
->options
& RES_DEBUG
, (stdout
, ";; timeout\n"));
1162 if (resplen
> 1 && (recvresp1
|| (buf2
!= NULL
&& recvresp2
)))
1164 /* There are quite a few broken name servers out
1165 there which don't handle two outstanding
1166 requests from the same source. There are also
1167 broken firewall settings. If we time out after
1168 having received one answer switch to the mode
1169 where we send the second request only once we
1170 have received the first answer. */
1171 if (!single_request
)
1173 statp
->options
|= RES_SNGLKUP
;
1174 single_request
= true;
1175 *gotsomewhere
= save_gotsomewhere
;
1178 else if (!single_request_reopen
)
1180 statp
->options
|= RES_SNGLKUPREOP
;
1181 single_request_reopen
= true;
1182 *gotsomewhere
= save_gotsomewhere
;
1183 __res_iclose (statp
, false);
1192 if (resplen2
!= NULL
)
1198 goto recompute_resend
;
1203 if (pfd
[0].revents
& POLLOUT
) {
1204 #ifndef __ASSUME_SENDMMSG
1205 static int have_sendmmsg
;
1207 # define have_sendmmsg 1
1209 if (have_sendmmsg
>= 0 && nwritten
== 0 && buf2
!= NULL
1212 struct iovec iov
[2];
1213 struct mmsghdr reqs
[2];
1214 reqs
[0].msg_hdr
.msg_name
= NULL
;
1215 reqs
[0].msg_hdr
.msg_namelen
= 0;
1216 reqs
[0].msg_hdr
.msg_iov
= &iov
[0];
1217 reqs
[0].msg_hdr
.msg_iovlen
= 1;
1218 iov
[0].iov_base
= (void *) buf
;
1219 iov
[0].iov_len
= buflen
;
1220 reqs
[0].msg_hdr
.msg_control
= NULL
;
1221 reqs
[0].msg_hdr
.msg_controllen
= 0;
1223 reqs
[1].msg_hdr
.msg_name
= NULL
;
1224 reqs
[1].msg_hdr
.msg_namelen
= 0;
1225 reqs
[1].msg_hdr
.msg_iov
= &iov
[1];
1226 reqs
[1].msg_hdr
.msg_iovlen
= 1;
1227 iov
[1].iov_base
= (void *) buf2
;
1228 iov
[1].iov_len
= buflen2
;
1229 reqs
[1].msg_hdr
.msg_control
= NULL
;
1230 reqs
[1].msg_hdr
.msg_controllen
= 0;
1232 int ndg
= __sendmmsg (pfd
[0].fd
, reqs
, 2, MSG_NOSIGNAL
);
1233 if (__glibc_likely (ndg
== 2))
1235 if (reqs
[0].msg_len
!= buflen
1236 || reqs
[1].msg_len
!= buflen2
)
1239 pfd
[0].events
= POLLIN
;
1242 else if (ndg
== 1 && reqs
[0].msg_len
== buflen
)
1244 else if (ndg
< 0 && (errno
== EINTR
|| errno
== EAGAIN
))
1245 goto recompute_resend
;
1248 #ifndef __ASSUME_SENDMMSG
1249 if (__glibc_unlikely (have_sendmmsg
== 0))
1251 if (ndg
< 0 && errno
== ENOSYS
)
1261 Perror(statp
, stderr
, "sendmmsg", errno
);
1262 return close_and_return_error (statp
, resplen2
);
1268 #ifndef __ASSUME_SENDMMSG
1272 sr
= send (pfd
[0].fd
, buf2
, buflen2
, MSG_NOSIGNAL
);
1274 sr
= send (pfd
[0].fd
, buf
, buflen
, MSG_NOSIGNAL
);
1276 if (sr
!= (nwritten
!= 0 ? buflen2
: buflen
)) {
1277 if (errno
== EINTR
|| errno
== EAGAIN
)
1278 goto recompute_resend
;
1279 Perror(statp
, stderr
, "send", errno
);
1280 return close_and_return_error (statp
, resplen2
);
1283 if (nwritten
!= 0 || buf2
== NULL
|| single_request
)
1284 pfd
[0].events
= POLLIN
;
1286 pfd
[0].events
= POLLIN
| POLLOUT
;
1290 } else if (pfd
[0].revents
& POLLIN
) {
1295 if ((recvresp1
| recvresp2
) == 0 || buf2
== NULL
) {
1296 /* We have not received any responses
1297 yet or we only have one response to
1299 thisanssizp
= anssizp
;
1300 thisansp
= anscp
?: ansp
;
1301 assert (anscp
!= NULL
|| ansp2
== NULL
);
1302 thisresplenp
= &resplen
;
1304 thisanssizp
= anssizp2
;
1306 thisresplenp
= resplen2
;
1309 if (*thisanssizp
< MAXPACKET
1310 /* If the current buffer is not the the static
1311 user-supplied buffer then we can reallocate
1313 && (thisansp
!= NULL
&& thisansp
!= ansp
)
1315 /* Is the size too small? */
1316 && (ioctl (pfd
[0].fd
, FIONREAD
, thisresplenp
) < 0
1317 || *thisanssizp
< *thisresplenp
)
1320 /* Always allocate MAXPACKET, callers expect
1321 this specific size. */
1322 u_char
*newp
= malloc (MAXPACKET
);
1324 *thisanssizp
= MAXPACKET
;
1326 if (thisansp
== ansp2
)
1327 *ansp2_malloced
= 1;
1330 /* We could end up with truncation if anscp was NULL
1331 (not allowed to change caller's buffer) and the
1332 response buffer size is too small. This isn't a
1333 reliable way to detect truncation because the ioctl
1334 may be an inaccurate report of the UDP message size.
1335 Therefore we use this only to issue debug output.
1336 To do truncation accurately with UDP we need
1337 MSG_TRUNC which is only available on Linux. We
1338 can abstract out the Linux-specific feature in the
1339 future to detect truncation. */
1340 if (__glibc_unlikely (*thisanssizp
< *thisresplenp
)) {
1341 Dprint(statp
->options
& RES_DEBUG
,
1342 (stdout
, ";; response may be truncated (UDP)\n")
1346 HEADER
*anhp
= (HEADER
*) *thisansp
;
1347 socklen_t fromlen
= sizeof(struct sockaddr_in6
);
1348 assert (sizeof(from
) <= fromlen
);
1349 *thisresplenp
= recvfrom(pfd
[0].fd
, (char*)*thisansp
,
1351 (struct sockaddr
*)&from
, &fromlen
);
1352 if (__glibc_unlikely (*thisresplenp
<= 0)) {
1353 if (errno
== EINTR
|| errno
== EAGAIN
) {
1357 Perror(statp
, stderr
, "recvfrom", errno
);
1358 return close_and_return_error (statp
, resplen2
);
1361 if (__glibc_unlikely (*thisresplenp
< HFIXEDSZ
)) {
1363 * Undersized message.
1365 Dprint(statp
->options
& RES_DEBUG
,
1366 (stdout
, ";; undersized: %d\n",
1369 return close_and_return_error (statp
, resplen2
);
1371 if ((recvresp1
|| hp
->id
!= anhp
->id
)
1372 && (recvresp2
|| hp2
->id
!= anhp
->id
)) {
1374 * response from old query, ignore it.
1375 * XXX - potential security hazard could
1378 DprintQ((statp
->options
& RES_DEBUG
) ||
1379 (statp
->pfcode
& RES_PRF_REPLY
),
1380 (stdout
, ";; old answer:\n"),
1382 (*thisresplenp
> *thisanssizp
)
1383 ? *thisanssizp
: *thisresplenp
);
1386 if (!(statp
->options
& RES_INSECURE1
) &&
1387 !res_ourserver_p(statp
, &from
)) {
1389 * response from wrong server? ignore it.
1390 * XXX - potential security hazard could
1393 DprintQ((statp
->options
& RES_DEBUG
) ||
1394 (statp
->pfcode
& RES_PRF_REPLY
),
1395 (stdout
, ";; not our server:\n"),
1397 (*thisresplenp
> *thisanssizp
)
1398 ? *thisanssizp
: *thisresplenp
);
1401 #ifdef RES_USE_EDNS0
1402 if (anhp
->rcode
== FORMERR
1403 && (statp
->options
& RES_USE_EDNS0
) != 0U) {
1405 * Do not retry if the server does not understand
1406 * EDNS0. The case has to be captured here, as
1407 * FORMERR packet do not carry query section, hence
1408 * res_queriesmatch() returns 0.
1410 DprintQ(statp
->options
& RES_DEBUG
,
1412 "server rejected query with EDNS0:\n"),
1414 (*thisresplenp
> *thisanssizp
)
1415 ? *thisanssizp
: *thisresplenp
);
1416 /* record the error */
1417 statp
->_flags
|= RES_F_EDNS0ERR
;
1418 return close_and_return_error (statp
, resplen2
);
1421 if (!(statp
->options
& RES_INSECURE2
)
1422 && (recvresp1
|| !res_queriesmatch(buf
, buf
+ buflen
,
1426 && (recvresp2
|| !res_queriesmatch(buf2
, buf2
+ buflen2
,
1431 * response contains wrong query? ignore it.
1432 * XXX - potential security hazard could
1435 DprintQ((statp
->options
& RES_DEBUG
) ||
1436 (statp
->pfcode
& RES_PRF_REPLY
),
1437 (stdout
, ";; wrong query name:\n"),
1439 (*thisresplenp
> *thisanssizp
)
1440 ? *thisanssizp
: *thisresplenp
);
1443 if (anhp
->rcode
== SERVFAIL
||
1444 anhp
->rcode
== NOTIMP
||
1445 anhp
->rcode
== REFUSED
) {
1446 DprintQ(statp
->options
& RES_DEBUG
,
1447 (stdout
, "server rejected query:\n"),
1449 (*thisresplenp
> *thisanssizp
)
1450 ? *thisanssizp
: *thisresplenp
);
1453 if (recvresp1
|| (buf2
!= NULL
&& recvresp2
)) {
1459 /* No data from the first reply. */
1461 /* We are waiting for a possible second reply. */
1462 if (hp
->id
== anhp
->id
)
1470 /* don't retry if called from dig */
1472 return close_and_return_error (statp
, resplen2
);
1473 __res_iclose(statp
, false);
1475 if (anhp
->rcode
== NOERROR
&& anhp
->ancount
== 0
1476 && anhp
->aa
== 0 && anhp
->ra
== 0 && anhp
->arcount
== 0) {
1477 DprintQ(statp
->options
& RES_DEBUG
,
1478 (stdout
, "referred query:\n"),
1480 (*thisresplenp
> *thisanssizp
)
1481 ? *thisanssizp
: *thisresplenp
);
1484 if (!(statp
->options
& RES_IGNTC
) && anhp
->tc
) {
1486 * To get the rest of answer,
1487 * use TCP with same server.
1489 Dprint(statp
->options
& RES_DEBUG
,
1490 (stdout
, ";; truncated answer\n"));
1492 __res_iclose(statp
, false);
1493 // XXX if we have received one reply we could
1494 // XXX use it and not repeat it over TCP...
1495 if (resplen2
!= NULL
)
1499 /* Mark which reply we received. */
1500 if (recvresp1
== 0 && hp
->id
== anhp
->id
)
1504 /* Repeat waiting if we have a second answer to arrive. */
1505 if ((recvresp1
& recvresp2
) == 0) {
1506 if (single_request
) {
1507 pfd
[0].events
= POLLOUT
;
1508 if (single_request_reopen
) {
1509 __res_iclose (statp
, false);
1510 retval
= reopen (statp
, terrno
, ns
);
1513 if (resplen2
!= NULL
)
1517 pfd
[0].fd
= EXT(statp
).nssocks
[ns
];
1522 /* All is well. We have received both responses (if
1523 two responses were requested). */
1525 } else if (pfd
[0].revents
& (POLLERR
| POLLHUP
| POLLNVAL
))
1526 /* Something went wrong. We can stop trying. */
1527 return close_and_return_error (statp
, resplen2
);
1529 /* poll should not have returned > 0 in this case. */
1536 Aerror(const res_state statp
, FILE *file
, const char *string
, int error
,
1537 const struct sockaddr
*address
)
1541 if ((statp
->options
& RES_DEBUG
) != 0) {
1542 char tmp
[sizeof "xxxx.xxxx.xxxx.255.255.255.255"];
1544 fprintf(file
, "res_send: %s ([%s].%u): %s\n",
1546 (address
->sa_family
== AF_INET
1547 ? inet_ntop(address
->sa_family
,
1548 &((const struct sockaddr_in
*) address
)->sin_addr
,
1550 : inet_ntop(address
->sa_family
,
1551 &((const struct sockaddr_in6
*) address
)->sin6_addr
,
1553 (address
->sa_family
== AF_INET
1554 ? ntohs(((struct sockaddr_in
*) address
)->sin_port
)
1555 : address
->sa_family
== AF_INET6
1556 ? ntohs(((struct sockaddr_in6
*) address
)->sin6_port
)
1564 Perror(const res_state statp
, FILE *file
, const char *string
, int error
) {
1567 if ((statp
->options
& RES_DEBUG
) != 0)
1568 fprintf(file
, "res_send: %s: %s\n",
1569 string
, strerror(error
));
1575 sock_eq(struct sockaddr_in6
*a1
, struct sockaddr_in6
*a2
) {
1576 if (a1
->sin6_family
== a2
->sin6_family
) {
1577 if (a1
->sin6_family
== AF_INET
)
1578 return ((((struct sockaddr_in
*)a1
)->sin_port
==
1579 ((struct sockaddr_in
*)a2
)->sin_port
) &&
1580 (((struct sockaddr_in
*)a1
)->sin_addr
.s_addr
==
1581 ((struct sockaddr_in
*)a2
)->sin_addr
.s_addr
));
1583 return ((a1
->sin6_port
== a2
->sin6_port
) &&
1584 !memcmp(&a1
->sin6_addr
, &a2
->sin6_addr
,
1585 sizeof (struct in6_addr
)));
1587 if (a1
->sin6_family
== AF_INET
) {
1588 struct sockaddr_in6
*sap
= a1
;
1591 } /* assumes that AF_INET and AF_INET6 are the only possibilities */
1592 return ((a1
->sin6_port
== ((struct sockaddr_in
*)a2
)->sin_port
) &&
1593 IN6_IS_ADDR_V4MAPPED(&a1
->sin6_addr
) &&
1594 (a1
->sin6_addr
.s6_addr32
[3] ==
1595 ((struct sockaddr_in
*)a2
)->sin_addr
.s_addr
));