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
84 #if defined(LIBC_SCCS) && !defined(lint)
85 static const char sccsid
[] = "@(#)res_send.c 8.1 (Berkeley) 6/4/93";
86 static const char rcsid
[] = "$BINDId: res_send.c,v 8.38 2000/03/30 20:16:51 vixie Exp $";
87 #endif /* LIBC_SCCS and not lint */
90 * Send query to name server and wait for reply.
94 #include <sys/types.h>
95 #include <sys/param.h>
97 #include <sys/socket.h>
101 #include <netinet/in.h>
102 #include <arpa/nameser.h>
103 #include <arpa/inet.h>
104 #include <sys/ioctl.h>
115 #include <kernel-features.h>
116 #include <libc-internal.h>
119 #define MAXPACKET PACKETSZ
121 #define MAXPACKET 65536
124 /* From ev_streams.c. */
127 __attribute ((always_inline
))
128 evConsIovec(void *buf
, size_t cnt
, struct iovec
*vec
) {
129 memset(vec
, 0xf5, sizeof (*vec
));
134 /* From ev_timers.c. */
136 #define BILLION 1000000000
139 evConsTime(struct timespec
*res
, time_t sec
, long nsec
) {
145 evAddTime(struct timespec
*res
, const struct timespec
*addend1
,
146 const struct timespec
*addend2
) {
147 res
->tv_sec
= addend1
->tv_sec
+ addend2
->tv_sec
;
148 res
->tv_nsec
= addend1
->tv_nsec
+ addend2
->tv_nsec
;
149 if (res
->tv_nsec
>= BILLION
) {
151 res
->tv_nsec
-= BILLION
;
156 evSubTime(struct timespec
*res
, const struct timespec
*minuend
,
157 const struct timespec
*subtrahend
) {
158 res
->tv_sec
= minuend
->tv_sec
- subtrahend
->tv_sec
;
159 if (minuend
->tv_nsec
>= subtrahend
->tv_nsec
)
160 res
->tv_nsec
= minuend
->tv_nsec
- subtrahend
->tv_nsec
;
162 res
->tv_nsec
= (BILLION
163 - subtrahend
->tv_nsec
+ minuend
->tv_nsec
);
169 evCmpTime(struct timespec a
, struct timespec b
) {
170 long x
= a
.tv_sec
- b
.tv_sec
;
173 x
= a
.tv_nsec
- b
.tv_nsec
;
174 return (x
< 0L ? (-1) : x
> 0L ? (1) : (0));
178 evNowTime(struct timespec
*res
) {
181 if (gettimeofday(&now
, NULL
) < 0)
182 evConsTime(res
, 0, 0);
184 TIMEVAL_TO_TIMESPEC (&now
, res
);
188 /* Options. Leave them on. */
190 #include "res_debug.h"
192 #define EXT(res) ((res)->_u._ext)
196 static struct sockaddr
*get_nsaddr (res_state
, int);
197 static int send_vc(res_state
, const u_char
*, int,
199 u_char
**, int *, int *, int, u_char
**,
200 u_char
**, int *, int *, int *);
201 static int send_dg(res_state
, const u_char
*, int,
203 u_char
**, int *, int *, int,
204 int *, int *, u_char
**,
205 u_char
**, int *, int *, int *);
207 static void Aerror(const res_state
, FILE *, const char *, int,
208 const struct sockaddr
*);
209 static void Perror(const res_state
, FILE *, const char *, int);
211 static int sock_eq(struct sockaddr_in6
*, struct sockaddr_in6
*);
216 * res_isourserver(ina)
217 * looks up "ina" in _res.ns_addr_list[]
222 * paul vixie, 29may94
225 res_ourserver_p(const res_state statp
, const struct sockaddr_in6
*inp
)
229 if (inp
->sin6_family
== AF_INET
) {
230 struct sockaddr_in
*in4p
= (struct sockaddr_in
*) inp
;
231 in_port_t port
= in4p
->sin_port
;
232 in_addr_t addr
= in4p
->sin_addr
.s_addr
;
234 for (ns
= 0; ns
< statp
->nscount
; ns
++) {
235 const struct sockaddr_in
*srv
=
236 (struct sockaddr_in
*) get_nsaddr (statp
, ns
);
238 if ((srv
->sin_family
== AF_INET
) &&
239 (srv
->sin_port
== port
) &&
240 (srv
->sin_addr
.s_addr
== INADDR_ANY
||
241 srv
->sin_addr
.s_addr
== addr
))
244 } else if (inp
->sin6_family
== AF_INET6
) {
245 for (ns
= 0; ns
< statp
->nscount
; ns
++) {
246 const struct sockaddr_in6
*srv
247 = (struct sockaddr_in6
*) get_nsaddr (statp
, ns
);
248 if ((srv
->sin6_family
== AF_INET6
) &&
249 (srv
->sin6_port
== inp
->sin6_port
) &&
250 !(memcmp(&srv
->sin6_addr
, &in6addr_any
,
251 sizeof (struct in6_addr
)) &&
252 memcmp(&srv
->sin6_addr
, &inp
->sin6_addr
,
253 sizeof (struct in6_addr
))))
261 * res_nameinquery(name, type, class, buf, eom)
262 * look for (name,type,class) in the query section of packet (buf,eom)
264 * buf + HFIXEDSZ <= eom
270 * paul vixie, 29may94
273 res_nameinquery(const char *name
, int type
, int class,
274 const u_char
*buf
, const u_char
*eom
)
276 const u_char
*cp
= buf
+ HFIXEDSZ
;
277 int qdcount
= ntohs(((HEADER
*)buf
)->qdcount
);
279 while (qdcount
-- > 0) {
280 char tname
[MAXDNAME
+1];
281 int n
, ttype
, tclass
;
283 n
= dn_expand(buf
, eom
, cp
, tname
, sizeof tname
);
287 if (cp
+ 2 * INT16SZ
> eom
)
290 NS_GET16(tclass
, cp
);
291 if (ttype
== type
&& tclass
== class &&
292 ns_samename(tname
, name
) == 1)
297 libresolv_hidden_def (res_nameinquery
)
300 * res_queriesmatch(buf1, eom1, buf2, eom2)
301 * is there a 1:1 mapping of (name,type,class)
302 * in (buf1,eom1) and (buf2,eom2)?
305 * 0 : not a 1:1 mapping
306 * >0 : is a 1:1 mapping
308 * paul vixie, 29may94
311 res_queriesmatch(const u_char
*buf1
, const u_char
*eom1
,
312 const u_char
*buf2
, const u_char
*eom2
)
314 if (buf1
+ HFIXEDSZ
> eom1
|| buf2
+ HFIXEDSZ
> eom2
)
318 * Only header section present in replies to
319 * dynamic update packets.
321 if ((((HEADER
*)buf1
)->opcode
== ns_o_update
) &&
322 (((HEADER
*)buf2
)->opcode
== ns_o_update
))
325 /* Note that we initially do not convert QDCOUNT to the host byte
326 order. We can compare it with the second buffer's QDCOUNT
327 value without doing this. */
328 int qdcount
= ((HEADER
*)buf1
)->qdcount
;
329 if (qdcount
!= ((HEADER
*)buf2
)->qdcount
)
332 qdcount
= htons (qdcount
);
333 const u_char
*cp
= buf1
+ HFIXEDSZ
;
335 while (qdcount
-- > 0) {
336 char tname
[MAXDNAME
+1];
337 int n
, ttype
, tclass
;
339 n
= dn_expand(buf1
, eom1
, cp
, tname
, sizeof tname
);
343 if (cp
+ 2 * INT16SZ
> eom1
)
346 NS_GET16(tclass
, cp
);
347 if (!res_nameinquery(tname
, ttype
, tclass
, buf2
, eom2
))
352 libresolv_hidden_def (res_queriesmatch
)
355 __libc_res_nsend(res_state statp
, const u_char
*buf
, int buflen
,
356 const u_char
*buf2
, int buflen2
,
357 u_char
*ans
, int anssiz
, u_char
**ansp
, u_char
**ansp2
,
358 int *nansp2
, int *resplen2
, int *ansp2_malloced
)
360 int gotsomewhere
, terrno
, try, v_circuit
, resplen
, ns
, n
;
362 if (statp
->nscount
== 0) {
367 if (anssiz
< (buf2
== NULL
? 1 : 2) * HFIXEDSZ
) {
368 __set_errno (EINVAL
);
373 if (__glibc_unlikely (statp
->qhook
|| statp
->rhook
)) {
374 if (anssiz
< MAXPACKET
&& ansp
) {
375 /* Always allocate MAXPACKET, callers expect
376 this specific size. */
377 u_char
*buf
= malloc (MAXPACKET
);
380 memcpy (buf
, ans
, HFIXEDSZ
);
388 DprintQ((statp
->options
& RES_DEBUG
) || (statp
->pfcode
& RES_PRF_QUERY
),
389 (stdout
, ";; res_send()\n"), buf
, buflen
);
390 v_circuit
= ((statp
->options
& RES_USEVC
)
392 || buflen2
> PACKETSZ
);
397 * If the ns_addr_list in the resolver context has changed, then
398 * invalidate our cached copy and the associated timing data.
400 if (EXT(statp
).nscount
!= 0) {
403 if (EXT(statp
).nscount
!= statp
->nscount
)
406 for (ns
= 0; ns
< statp
->nscount
; ns
++) {
407 if (statp
->nsaddr_list
[ns
].sin_family
!= 0
408 && !sock_eq((struct sockaddr_in6
*)
409 &statp
->nsaddr_list
[ns
],
410 EXT(statp
).nsaddrs
[ns
]))
417 __res_iclose(statp
, false);
418 EXT(statp
).nscount
= 0;
423 * Maybe initialize our private copy of the ns_addr_list.
425 if (EXT(statp
).nscount
== 0) {
426 for (ns
= 0; ns
< statp
->nscount
; ns
++) {
427 EXT(statp
).nssocks
[ns
] = -1;
428 if (statp
->nsaddr_list
[ns
].sin_family
== 0)
430 if (EXT(statp
).nsaddrs
[ns
] == NULL
)
431 EXT(statp
).nsaddrs
[ns
] =
432 malloc(sizeof (struct sockaddr_in6
));
433 if (EXT(statp
).nsaddrs
[ns
] != NULL
)
434 memset (mempcpy(EXT(statp
).nsaddrs
[ns
],
435 &statp
->nsaddr_list
[ns
],
436 sizeof (struct sockaddr_in
)),
438 sizeof (struct sockaddr_in6
)
439 - sizeof (struct sockaddr_in
));
441 EXT(statp
).nscount
= statp
->nscount
;
445 * Some resolvers want to even out the load on their nameservers.
446 * Note that RES_BLAST overrides RES_ROTATE.
448 if (__builtin_expect ((statp
->options
& RES_ROTATE
) != 0, 0) &&
449 (statp
->options
& RES_BLAST
) == 0) {
450 struct sockaddr_in ina
;
451 struct sockaddr_in6
*inp
;
452 int lastns
= statp
->nscount
- 1;
455 inp
= EXT(statp
).nsaddrs
[0];
456 ina
= statp
->nsaddr_list
[0];
457 fd
= EXT(statp
).nssocks
[0];
458 for (ns
= 0; ns
< lastns
; ns
++) {
459 EXT(statp
).nsaddrs
[ns
] = EXT(statp
).nsaddrs
[ns
+ 1];
460 statp
->nsaddr_list
[ns
] = statp
->nsaddr_list
[ns
+ 1];
461 EXT(statp
).nssocks
[ns
] = EXT(statp
).nssocks
[ns
+ 1];
463 EXT(statp
).nsaddrs
[lastns
] = inp
;
464 statp
->nsaddr_list
[lastns
] = ina
;
465 EXT(statp
).nssocks
[lastns
] = fd
;
469 * Send request, RETRY times, or until successful.
471 for (try = 0; try < statp
->retry
; try++) {
472 for (ns
= 0; ns
< statp
->nscount
; ns
++)
477 #if defined USE_HOOKS || defined DEBUG
478 struct sockaddr
*nsap
= get_nsaddr (statp
, ns
);
483 if (__glibc_unlikely (statp
->qhook
!= NULL
)) {
484 int done
= 0, loops
= 0;
489 struct sockaddr_in
*nsap4
;
490 nsap4
= (struct sockaddr_in
*) nsap
;
491 act
= (*statp
->qhook
)(&nsap4
, &buf
, &buflen
,
492 ans
, anssiz
, &resplen
);
493 nsap
= (struct sockaddr_in6
*) nsap4
;
499 __res_iclose(statp
, false);
504 /* give the hook another try */
505 if (++loops
< 42) /*doug adams*/
517 Dprint(statp
->options
& RES_DEBUG
,
518 (stdout
, ";; Querying server (# %d) address = %s\n",
519 ns
+ 1, inet_ntop(nsap
->sa_family
,
520 (nsap
->sa_family
== AF_INET6
521 ? (void *) &((struct sockaddr_in6
*) nsap
)->sin6_addr
522 : (void *) &((struct sockaddr_in
*) nsap
)->sin_addr
),
523 tmpbuf
, sizeof (tmpbuf
))));
525 if (__glibc_unlikely (v_circuit
)) {
526 /* Use VC; at most one attempt per server. */
528 n
= send_vc(statp
, buf
, buflen
, buf2
, buflen2
,
529 &ans
, &anssiz
, &terrno
,
530 ns
, ansp
, ansp2
, nansp2
, resplen2
,
534 if (n
== 0 && (buf2
== NULL
|| *resplen2
== 0))
538 n
= send_dg(statp
, buf
, buflen
, buf2
, buflen2
,
539 &ans
, &anssiz
, &terrno
,
540 ns
, &v_circuit
, &gotsomewhere
, ansp
,
541 ansp2
, nansp2
, resplen2
, ansp2_malloced
);
544 if (n
== 0 && (buf2
== NULL
|| *resplen2
== 0))
547 // XXX Check whether both requests failed or
548 // XXX whether one has been answered successfully
554 Dprint((statp
->options
& RES_DEBUG
) ||
555 ((statp
->pfcode
& RES_PRF_REPLY
) &&
556 (statp
->pfcode
& RES_PRF_HEAD1
)),
557 (stdout
, ";; got answer:\n"));
559 DprintQ((statp
->options
& RES_DEBUG
) ||
560 (statp
->pfcode
& RES_PRF_REPLY
),
562 ans
, (resplen
> anssiz
) ? anssiz
: resplen
);
564 DprintQ((statp
->options
& RES_DEBUG
) ||
565 (statp
->pfcode
& RES_PRF_REPLY
),
567 *ansp2
, (*resplen2
> *nansp2
) ? *nansp2
: *resplen2
);
571 * If we have temporarily opened a virtual circuit,
572 * or if we haven't been asked to keep a socket open,
575 if ((v_circuit
&& (statp
->options
& RES_USEVC
) == 0) ||
576 (statp
->options
& RES_STAYOPEN
) == 0) {
577 __res_iclose(statp
, false);
580 if (__glibc_unlikely (statp
->rhook
)) {
581 int done
= 0, loops
= 0;
586 act
= (*statp
->rhook
)((struct sockaddr_in
*)
588 ans
, anssiz
, &resplen
);
595 __res_iclose(statp
, false);
598 /* give the hook another try */
599 if (++loops
< 42) /*doug adams*/
615 __res_iclose(statp
, false);
618 __set_errno (ECONNREFUSED
); /* no nameservers found */
620 __set_errno (ETIMEDOUT
); /* no answer obtained */
622 __set_errno (terrno
);
627 res_nsend(res_state statp
,
628 const u_char
*buf
, int buflen
, u_char
*ans
, int anssiz
)
630 return __libc_res_nsend(statp
, buf
, buflen
, NULL
, 0, ans
, anssiz
,
631 NULL
, NULL
, NULL
, NULL
, NULL
);
633 libresolv_hidden_def (res_nsend
)
637 static struct sockaddr
*
638 get_nsaddr (res_state statp
, int n
)
641 if (statp
->nsaddr_list
[n
].sin_family
== 0 && EXT(statp
).nsaddrs
[n
] != NULL
)
642 /* EXT(statp).nsaddrs[n] holds an address that is larger than
643 struct sockaddr, and user code did not update
644 statp->nsaddr_list[n]. */
645 return (struct sockaddr
*) EXT(statp
).nsaddrs
[n
];
647 /* User code updated statp->nsaddr_list[n], or statp->nsaddr_list[n]
648 has the same content as EXT(statp).nsaddrs[n]. */
649 return (struct sockaddr
*) (void *) &statp
->nsaddr_list
[n
];
652 /* Close the resolver structure, assign zero to *RESPLEN2 if RESPLEN2
653 is not NULL, and return zero. */
655 __attribute__ ((warn_unused_result
))
656 close_and_return_error (res_state statp
, int *resplen2
)
658 __res_iclose(statp
, false);
659 if (resplen2
!= NULL
)
664 /* The send_vc function is responsible for sending a DNS query over TCP
665 to the nameserver numbered NS from the res_state STATP i.e.
666 EXT(statp).nssocks[ns]. The function supports sending both IPv4 and
667 IPv6 queries at the same serially on the same socket.
669 Please note that for TCP there is no way to disable sending both
670 queries, unlike UDP, which honours RES_SNGLKUP and RES_SNGLKUPREOP
671 and sends the queries serially and waits for the result after each
672 sent query. This implemetnation should be corrected to honour these
675 Please also note that for TCP we send both queries over the same
676 socket one after another. This technically violates best practice
677 since the server is allowed to read the first query, respond, and
678 then close the socket (to service another client). If the server
679 does this, then the remaining second query in the socket data buffer
680 will cause the server to send the client an RST which will arrive
681 asynchronously and the client's OS will likely tear down the socket
682 receive buffer resulting in a potentially short read and lost
683 response data. This will force the client to retry the query again,
684 and this process may repeat until all servers and connection resets
685 are exhausted and then the query will fail. It's not known if this
686 happens with any frequency in real DNS server implementations. This
687 implementation should be corrected to use two sockets by default for
690 The query stored in BUF of BUFLEN length is sent first followed by
691 the query stored in BUF2 of BUFLEN2 length. Queries are sent
692 serially on the same socket.
694 Answers to the query are stored firstly in *ANSP up to a max of
695 *ANSSIZP bytes. If more than *ANSSIZP bytes are needed and ANSCP
696 is non-NULL (to indicate that modifying the answer buffer is allowed)
697 then malloc is used to allocate a new response buffer and ANSCP and
698 ANSP will both point to the new buffer. If more than *ANSSIZP bytes
699 are needed but ANSCP is NULL, then as much of the response as
700 possible is read into the buffer, but the results will be truncated.
701 When truncation happens because of a small answer buffer the DNS
702 packets header field TC will bet set to 1, indicating a truncated
703 message and the rest of the socket data will be read and discarded.
705 Answers to the query are stored secondly in *ANSP2 up to a max of
706 *ANSSIZP2 bytes, with the actual response length stored in
707 *RESPLEN2. If more than *ANSSIZP bytes are needed and ANSP2
708 is non-NULL (required for a second query) then malloc is used to
709 allocate a new response buffer, *ANSSIZP2 is set to the new buffer
710 size and *ANSP2_MALLOCED is set to 1.
712 The ANSP2_MALLOCED argument will eventually be removed as the
713 change in buffer pointer can be used to detect the buffer has
714 changed and that the caller should use free on the new buffer.
716 Note that the answers may arrive in any order from the server and
717 therefore the first and second answer buffers may not correspond to
718 the first and second queries.
720 It is not supported to call this function with a non-NULL ANSP2
721 but a NULL ANSCP. Put another way, you can call send_vc with a
722 single unmodifiable buffer or two modifiable buffers, but no other
723 combination is supported.
725 It is the caller's responsibility to free the malloc allocated
726 buffers by detecting that the pointers have changed from their
727 original values i.e. *ANSCP or *ANSP2 has changed.
729 If errors are encountered then *TERRNO is set to an appropriate
730 errno value and a zero result is returned for a recoverable error,
731 and a less-than zero result is returned for a non-recoverable error.
733 If no errors are encountered then *TERRNO is left unmodified and
734 a the length of the first response in bytes is returned. */
736 send_vc(res_state statp
,
737 const u_char
*buf
, int buflen
, const u_char
*buf2
, int buflen2
,
738 u_char
**ansp
, int *anssizp
,
739 int *terrno
, int ns
, u_char
**anscp
, u_char
**ansp2
, int *anssizp2
,
740 int *resplen2
, int *ansp2_malloced
)
742 const HEADER
*hp
= (HEADER
*) buf
;
743 const HEADER
*hp2
= (HEADER
*) buf2
;
744 HEADER
*anhp
= (HEADER
*) *ansp
;
745 struct sockaddr
*nsap
= get_nsaddr (statp
, ns
);
746 int truncating
, connreset
, n
;
747 /* On some architectures compiler might emit a warning indicating
748 'resplen' may be used uninitialized. However if buf2 == NULL
749 then this code won't be executed; if buf2 != NULL, then first
750 time round the loop recvresp1 and recvresp2 will be 0 so this
751 code won't be executed but "thisresplenp = &resplen;" followed
752 by "*thisresplenp = rlen;" will be executed so that subsequent
753 times round the loop resplen has been initialized. So this is
756 DIAG_PUSH_NEEDS_COMMENT
;
757 DIAG_IGNORE_NEEDS_COMMENT (5, "-Wmaybe-uninitialized");
759 DIAG_POP_NEEDS_COMMENT
;
769 /* Are we still talking to whom we want to talk to? */
770 if (statp
->_vcsock
>= 0 && (statp
->_flags
& RES_F_VC
) != 0) {
771 struct sockaddr_in6 peer
;
772 socklen_t size
= sizeof peer
;
774 if (getpeername(statp
->_vcsock
,
775 (struct sockaddr
*)&peer
, &size
) < 0 ||
776 !sock_eq(&peer
, (struct sockaddr_in6
*) nsap
)) {
777 __res_iclose(statp
, false);
778 statp
->_flags
&= ~RES_F_VC
;
782 if (statp
->_vcsock
< 0 || (statp
->_flags
& RES_F_VC
) == 0) {
783 if (statp
->_vcsock
>= 0)
784 __res_iclose(statp
, false);
786 statp
->_vcsock
= socket(nsap
->sa_family
, SOCK_STREAM
, 0);
787 if (statp
->_vcsock
< 0) {
789 Perror(statp
, stderr
, "socket(vc)", errno
);
790 if (resplen2
!= NULL
)
795 if (connect(statp
->_vcsock
, nsap
,
796 nsap
->sa_family
== AF_INET
797 ? sizeof (struct sockaddr_in
)
798 : sizeof (struct sockaddr_in6
)) < 0) {
800 Aerror(statp
, stderr
, "connect/vc", errno
, nsap
);
801 return close_and_return_error (statp
, resplen2
);
803 statp
->_flags
|= RES_F_VC
;
807 * Send length & message
809 len
= htons ((u_short
) buflen
);
810 evConsIovec(&len
, INT16SZ
, &iov
[0]);
811 evConsIovec((void*)buf
, buflen
, &iov
[1]);
813 ssize_t explen
= INT16SZ
+ buflen
;
815 len2
= htons ((u_short
) buflen2
);
816 evConsIovec(&len2
, INT16SZ
, &iov
[2]);
817 evConsIovec((void*)buf2
, buflen2
, &iov
[3]);
819 explen
+= INT16SZ
+ buflen2
;
821 if (TEMP_FAILURE_RETRY (writev(statp
->_vcsock
, iov
, niov
)) != explen
) {
823 Perror(statp
, stderr
, "write failed", errno
);
824 return close_and_return_error (statp
, resplen2
);
827 * Receive length & response
830 /* Skip the second response if there is no second query.
831 To do that we mark the second response as received. */
832 int recvresp2
= buf2
== NULL
;
835 cp
= (u_char
*)&rlen16
;
836 len
= sizeof(rlen16
);
837 while ((n
= TEMP_FAILURE_RETRY (read(statp
->_vcsock
, cp
,
845 Perror(statp
, stderr
, "read failed", errno
);
847 * A long running process might get its TCP
848 * connection reset if the remote server was
849 * restarted. Requery the server instead of
850 * trying a new one. When there is only one
851 * server, this means that a query might work
852 * instead of failing. We only allow one reset
853 * per query to prevent looping.
855 if (*terrno
== ECONNRESET
&& !connreset
)
857 __res_iclose (statp
, false);
861 return close_and_return_error (statp
, resplen2
);
863 int rlen
= ntohs (rlen16
);
868 if ((recvresp1
| recvresp2
) == 0 || buf2
== NULL
) {
869 /* We have not received any responses
870 yet or we only have one response to
872 thisanssizp
= anssizp
;
873 thisansp
= anscp
?: ansp
;
874 assert (anscp
!= NULL
|| ansp2
== NULL
);
875 thisresplenp
= &resplen
;
877 thisanssizp
= anssizp2
;
879 thisresplenp
= resplen2
;
881 anhp
= (HEADER
*) *thisansp
;
883 *thisresplenp
= rlen
;
884 /* Is the answer buffer too small? */
885 if (*thisanssizp
< rlen
) {
886 /* If the current buffer is not the the static
887 user-supplied buffer then we can reallocate
889 if (thisansp
!= NULL
&& thisansp
!= ansp
) {
890 /* Always allocate MAXPACKET, callers expect
891 this specific size. */
892 u_char
*newp
= malloc (MAXPACKET
);
896 return close_and_return_error (statp
, resplen2
);
898 *thisanssizp
= MAXPACKET
;
900 if (thisansp
== ansp2
)
902 anhp
= (HEADER
*) newp
;
903 /* A uint16_t can't be larger than MAXPACKET
904 thus it's safe to allocate MAXPACKET but
905 read RLEN bytes instead. */
908 Dprint(statp
->options
& RES_DEBUG
,
909 (stdout
, ";; response truncated\n")
917 if (__glibc_unlikely (len
< HFIXEDSZ
)) {
919 * Undersized message.
921 Dprint(statp
->options
& RES_DEBUG
,
922 (stdout
, ";; undersized: %d\n", len
));
924 return close_and_return_error (statp
, resplen2
);
928 while (len
!= 0 && (n
= read(statp
->_vcsock
, (char *)cp
, (int)len
)) > 0){
932 if (__glibc_unlikely (n
<= 0)) {
934 Perror(statp
, stderr
, "read(vc)", errno
);
935 return close_and_return_error (statp
, resplen2
);
937 if (__glibc_unlikely (truncating
)) {
939 * Flush rest of answer so connection stays in synch.
942 len
= rlen
- *thisanssizp
;
946 n
= read(statp
->_vcsock
, junk
,
947 (len
> sizeof junk
) ? sizeof junk
: len
);
955 * If the calling application has bailed out of
956 * a previous call and failed to arrange to have
957 * the circuit closed or the server has got
958 * itself confused, then drop the packet and
959 * wait for the correct one.
961 if ((recvresp1
|| hp
->id
!= anhp
->id
)
962 && (recvresp2
|| hp2
->id
!= anhp
->id
)) {
963 DprintQ((statp
->options
& RES_DEBUG
) ||
964 (statp
->pfcode
& RES_PRF_REPLY
),
965 (stdout
, ";; old answer (unexpected):\n"),
967 (rlen
> *thisanssizp
) ? *thisanssizp
: rlen
);
971 /* Mark which reply we received. */
972 if (recvresp1
== 0 && hp
->id
== anhp
->id
)
976 /* Repeat waiting if we have a second answer to arrive. */
977 if ((recvresp1
& recvresp2
) == 0)
981 * All is well, or the error is fatal. Signal that the
982 * next nameserver ought not be tried.
988 reopen (res_state statp
, int *terrno
, int ns
)
990 if (EXT(statp
).nssocks
[ns
] == -1) {
991 struct sockaddr
*nsap
= get_nsaddr (statp
, ns
);
994 /* only try IPv6 if IPv6 NS and if not failed before */
995 if (nsap
->sa_family
== AF_INET6
&& !statp
->ipv6_unavail
) {
996 EXT(statp
).nssocks
[ns
]
997 = socket(PF_INET6
, SOCK_DGRAM
|SOCK_NONBLOCK
, 0);
998 if (EXT(statp
).nssocks
[ns
] < 0)
999 statp
->ipv6_unavail
= errno
== EAFNOSUPPORT
;
1000 slen
= sizeof (struct sockaddr_in6
);
1001 } else if (nsap
->sa_family
== AF_INET
) {
1002 EXT(statp
).nssocks
[ns
]
1003 = socket(PF_INET
, SOCK_DGRAM
|SOCK_NONBLOCK
, 0);
1004 slen
= sizeof (struct sockaddr_in
);
1006 if (EXT(statp
).nssocks
[ns
] < 0) {
1008 Perror(statp
, stderr
, "socket(dg)", errno
);
1013 * On a 4.3BSD+ machine (client and server,
1014 * actually), sending to a nameserver datagram
1015 * port with no nameserver will cause an
1016 * ICMP port unreachable message to be returned.
1017 * If our datagram socket is "connected" to the
1018 * server, we get an ECONNREFUSED error on the next
1019 * socket operation, and select returns if the
1020 * error message is received. We can thus detect
1021 * the absence of a nameserver without timing out.
1023 if (connect(EXT(statp
).nssocks
[ns
], nsap
, slen
) < 0) {
1024 Aerror(statp
, stderr
, "connect(dg)", errno
, nsap
);
1025 __res_iclose(statp
, false);
1033 /* The send_dg function is responsible for sending a DNS query over UDP
1034 to the nameserver numbered NS from the res_state STATP i.e.
1035 EXT(statp).nssocks[ns]. The function supports IPv4 and IPv6 queries
1036 along with the ability to send the query in parallel for both stacks
1037 (default) or serially (RES_SINGLKUP). It also supports serial lookup
1038 with a close and reopen of the socket used to talk to the server
1039 (RES_SNGLKUPREOP) to work around broken name servers.
1041 The query stored in BUF of BUFLEN length is sent first followed by
1042 the query stored in BUF2 of BUFLEN2 length. Queries are sent
1043 in parallel (default) or serially (RES_SINGLKUP or RES_SNGLKUPREOP).
1045 Answers to the query are stored firstly in *ANSP up to a max of
1046 *ANSSIZP bytes. If more than *ANSSIZP bytes are needed and ANSCP
1047 is non-NULL (to indicate that modifying the answer buffer is allowed)
1048 then malloc is used to allocate a new response buffer and ANSCP and
1049 ANSP will both point to the new buffer. If more than *ANSSIZP bytes
1050 are needed but ANSCP is NULL, then as much of the response as
1051 possible is read into the buffer, but the results will be truncated.
1052 When truncation happens because of a small answer buffer the DNS
1053 packets header field TC will bet set to 1, indicating a truncated
1054 message, while the rest of the UDP packet is discarded.
1056 Answers to the query are stored secondly in *ANSP2 up to a max of
1057 *ANSSIZP2 bytes, with the actual response length stored in
1058 *RESPLEN2. If more than *ANSSIZP bytes are needed and ANSP2
1059 is non-NULL (required for a second query) then malloc is used to
1060 allocate a new response buffer, *ANSSIZP2 is set to the new buffer
1061 size and *ANSP2_MALLOCED is set to 1.
1063 The ANSP2_MALLOCED argument will eventually be removed as the
1064 change in buffer pointer can be used to detect the buffer has
1065 changed and that the caller should use free on the new buffer.
1067 Note that the answers may arrive in any order from the server and
1068 therefore the first and second answer buffers may not correspond to
1069 the first and second queries.
1071 It is not supported to call this function with a non-NULL ANSP2
1072 but a NULL ANSCP. Put another way, you can call send_vc with a
1073 single unmodifiable buffer or two modifiable buffers, but no other
1074 combination is supported.
1076 It is the caller's responsibility to free the malloc allocated
1077 buffers by detecting that the pointers have changed from their
1078 original values i.e. *ANSCP or *ANSP2 has changed.
1080 If an answer is truncated because of UDP datagram DNS limits then
1081 *V_CIRCUIT is set to 1 and the return value non-zero to indicate to
1082 the caller to retry with TCP. The value *GOTSOMEWHERE is set to 1
1083 if any progress was made reading a response from the nameserver and
1084 is used by the caller to distinguish between ECONNREFUSED and
1085 ETIMEDOUT (the latter if *GOTSOMEWHERE is 1).
1087 If errors are encountered then *TERRNO is set to an appropriate
1088 errno value and a zero result is returned for a recoverable error,
1089 and a less-than zero result is returned for a non-recoverable error.
1091 If no errors are encountered then *TERRNO is left unmodified and
1092 a the length of the first response in bytes is returned. */
1094 send_dg(res_state statp
,
1095 const u_char
*buf
, int buflen
, const u_char
*buf2
, int buflen2
,
1096 u_char
**ansp
, int *anssizp
,
1097 int *terrno
, int ns
, int *v_circuit
, int *gotsomewhere
, u_char
**anscp
,
1098 u_char
**ansp2
, int *anssizp2
, int *resplen2
, int *ansp2_malloced
)
1100 const HEADER
*hp
= (HEADER
*) buf
;
1101 const HEADER
*hp2
= (HEADER
*) buf2
;
1102 struct timespec now
, timeout
, finish
;
1103 struct pollfd pfd
[1];
1105 struct sockaddr_in6 from
;
1110 * Compute time for the total operation.
1112 int seconds
= (statp
->retrans
<< ns
);
1114 seconds
/= statp
->nscount
;
1117 bool single_request_reopen
= (statp
->options
& RES_SNGLKUPREOP
) != 0;
1118 bool single_request
= (((statp
->options
& RES_SNGLKUP
) != 0)
1119 | single_request_reopen
);
1120 int save_gotsomewhere
= *gotsomewhere
;
1124 retval
= reopen (statp
, terrno
, ns
);
1127 if (resplen2
!= NULL
)
1133 evConsTime(&timeout
, seconds
, 0);
1134 evAddTime(&finish
, &now
, &timeout
);
1135 int need_recompute
= 0;
1138 /* Skip the second response if there is no second query.
1139 To do that we mark the second response as received. */
1140 int recvresp2
= buf2
== NULL
;
1141 pfd
[0].fd
= EXT(statp
).nssocks
[ns
];
1142 pfd
[0].events
= POLLOUT
;
1144 if (need_recompute
) {
1147 if (evCmpTime(finish
, now
) <= 0) {
1149 Perror(statp
, stderr
, "poll", errno
);
1150 return close_and_return_error (statp
, resplen2
);
1152 evSubTime(&timeout
, &finish
, &now
);
1155 /* Convert struct timespec in milliseconds. */
1156 ptimeout
= timeout
.tv_sec
* 1000 + timeout
.tv_nsec
/ 1000000;
1160 n
= __poll (pfd
, 1, 0);
1161 if (__glibc_unlikely (n
== 0)) {
1162 n
= __poll (pfd
, 1, ptimeout
);
1166 Dprint(statp
->options
& RES_DEBUG
, (stdout
, ";; timeout\n"));
1167 if (resplen
> 1 && (recvresp1
|| (buf2
!= NULL
&& recvresp2
)))
1169 /* There are quite a few broken name servers out
1170 there which don't handle two outstanding
1171 requests from the same source. There are also
1172 broken firewall settings. If we time out after
1173 having received one answer switch to the mode
1174 where we send the second request only once we
1175 have received the first answer. */
1176 if (!single_request
)
1178 statp
->options
|= RES_SNGLKUP
;
1179 single_request
= true;
1180 *gotsomewhere
= save_gotsomewhere
;
1183 else if (!single_request_reopen
)
1185 statp
->options
|= RES_SNGLKUPREOP
;
1186 single_request_reopen
= true;
1187 *gotsomewhere
= save_gotsomewhere
;
1188 __res_iclose (statp
, false);
1197 if (resplen2
!= NULL
)
1203 goto recompute_resend
;
1208 if (pfd
[0].revents
& POLLOUT
) {
1209 #ifndef __ASSUME_SENDMMSG
1210 static int have_sendmmsg
;
1212 # define have_sendmmsg 1
1214 if (have_sendmmsg
>= 0 && nwritten
== 0 && buf2
!= NULL
1217 struct iovec iov
[2];
1218 struct mmsghdr reqs
[2];
1219 reqs
[0].msg_hdr
.msg_name
= NULL
;
1220 reqs
[0].msg_hdr
.msg_namelen
= 0;
1221 reqs
[0].msg_hdr
.msg_iov
= &iov
[0];
1222 reqs
[0].msg_hdr
.msg_iovlen
= 1;
1223 iov
[0].iov_base
= (void *) buf
;
1224 iov
[0].iov_len
= buflen
;
1225 reqs
[0].msg_hdr
.msg_control
= NULL
;
1226 reqs
[0].msg_hdr
.msg_controllen
= 0;
1228 reqs
[1].msg_hdr
.msg_name
= NULL
;
1229 reqs
[1].msg_hdr
.msg_namelen
= 0;
1230 reqs
[1].msg_hdr
.msg_iov
= &iov
[1];
1231 reqs
[1].msg_hdr
.msg_iovlen
= 1;
1232 iov
[1].iov_base
= (void *) buf2
;
1233 iov
[1].iov_len
= buflen2
;
1234 reqs
[1].msg_hdr
.msg_control
= NULL
;
1235 reqs
[1].msg_hdr
.msg_controllen
= 0;
1237 int ndg
= __sendmmsg (pfd
[0].fd
, reqs
, 2, MSG_NOSIGNAL
);
1238 if (__glibc_likely (ndg
== 2))
1240 if (reqs
[0].msg_len
!= buflen
1241 || reqs
[1].msg_len
!= buflen2
)
1244 pfd
[0].events
= POLLIN
;
1247 else if (ndg
== 1 && reqs
[0].msg_len
== buflen
)
1249 else if (ndg
< 0 && (errno
== EINTR
|| errno
== EAGAIN
))
1250 goto recompute_resend
;
1253 #ifndef __ASSUME_SENDMMSG
1254 if (__glibc_unlikely (have_sendmmsg
== 0))
1256 if (ndg
< 0 && errno
== ENOSYS
)
1266 Perror(statp
, stderr
, "sendmmsg", errno
);
1267 return close_and_return_error (statp
, resplen2
);
1273 #ifndef __ASSUME_SENDMMSG
1277 sr
= send (pfd
[0].fd
, buf2
, buflen2
, MSG_NOSIGNAL
);
1279 sr
= send (pfd
[0].fd
, buf
, buflen
, MSG_NOSIGNAL
);
1281 if (sr
!= (nwritten
!= 0 ? buflen2
: buflen
)) {
1282 if (errno
== EINTR
|| errno
== EAGAIN
)
1283 goto recompute_resend
;
1284 Perror(statp
, stderr
, "send", errno
);
1285 return close_and_return_error (statp
, resplen2
);
1288 if (nwritten
!= 0 || buf2
== NULL
|| single_request
)
1289 pfd
[0].events
= POLLIN
;
1291 pfd
[0].events
= POLLIN
| POLLOUT
;
1295 } else if (pfd
[0].revents
& POLLIN
) {
1300 if ((recvresp1
| recvresp2
) == 0 || buf2
== NULL
) {
1301 /* We have not received any responses
1302 yet or we only have one response to
1304 thisanssizp
= anssizp
;
1305 thisansp
= anscp
?: ansp
;
1306 assert (anscp
!= NULL
|| ansp2
== NULL
);
1307 thisresplenp
= &resplen
;
1309 thisanssizp
= anssizp2
;
1311 thisresplenp
= resplen2
;
1314 if (*thisanssizp
< MAXPACKET
1315 /* If the current buffer is not the the static
1316 user-supplied buffer then we can reallocate
1318 && (thisansp
!= NULL
&& thisansp
!= ansp
)
1320 /* Is the size too small? */
1321 && (ioctl (pfd
[0].fd
, FIONREAD
, thisresplenp
) < 0
1322 || *thisanssizp
< *thisresplenp
)
1325 /* Always allocate MAXPACKET, callers expect
1326 this specific size. */
1327 u_char
*newp
= malloc (MAXPACKET
);
1329 *thisanssizp
= MAXPACKET
;
1331 if (thisansp
== ansp2
)
1332 *ansp2_malloced
= 1;
1335 /* We could end up with truncation if anscp was NULL
1336 (not allowed to change caller's buffer) and the
1337 response buffer size is too small. This isn't a
1338 reliable way to detect truncation because the ioctl
1339 may be an inaccurate report of the UDP message size.
1340 Therefore we use this only to issue debug output.
1341 To do truncation accurately with UDP we need
1342 MSG_TRUNC which is only available on Linux. We
1343 can abstract out the Linux-specific feature in the
1344 future to detect truncation. */
1345 if (__glibc_unlikely (*thisanssizp
< *thisresplenp
)) {
1346 Dprint(statp
->options
& RES_DEBUG
,
1347 (stdout
, ";; response may be truncated (UDP)\n")
1351 HEADER
*anhp
= (HEADER
*) *thisansp
;
1352 socklen_t fromlen
= sizeof(struct sockaddr_in6
);
1353 assert (sizeof(from
) <= fromlen
);
1354 *thisresplenp
= recvfrom(pfd
[0].fd
, (char*)*thisansp
,
1356 (struct sockaddr
*)&from
, &fromlen
);
1357 if (__glibc_unlikely (*thisresplenp
<= 0)) {
1358 if (errno
== EINTR
|| errno
== EAGAIN
) {
1362 Perror(statp
, stderr
, "recvfrom", errno
);
1363 return close_and_return_error (statp
, resplen2
);
1366 if (__glibc_unlikely (*thisresplenp
< HFIXEDSZ
)) {
1368 * Undersized message.
1370 Dprint(statp
->options
& RES_DEBUG
,
1371 (stdout
, ";; undersized: %d\n",
1374 return close_and_return_error (statp
, resplen2
);
1376 if ((recvresp1
|| hp
->id
!= anhp
->id
)
1377 && (recvresp2
|| hp2
->id
!= anhp
->id
)) {
1379 * response from old query, ignore it.
1380 * XXX - potential security hazard could
1383 DprintQ((statp
->options
& RES_DEBUG
) ||
1384 (statp
->pfcode
& RES_PRF_REPLY
),
1385 (stdout
, ";; old answer:\n"),
1387 (*thisresplenp
> *thisanssizp
)
1388 ? *thisanssizp
: *thisresplenp
);
1391 if (!(statp
->options
& RES_INSECURE1
) &&
1392 !res_ourserver_p(statp
, &from
)) {
1394 * response from wrong server? ignore it.
1395 * XXX - potential security hazard could
1398 DprintQ((statp
->options
& RES_DEBUG
) ||
1399 (statp
->pfcode
& RES_PRF_REPLY
),
1400 (stdout
, ";; not our server:\n"),
1402 (*thisresplenp
> *thisanssizp
)
1403 ? *thisanssizp
: *thisresplenp
);
1406 #ifdef RES_USE_EDNS0
1407 if (anhp
->rcode
== FORMERR
1408 && (statp
->options
& RES_USE_EDNS0
) != 0U) {
1410 * Do not retry if the server does not understand
1411 * EDNS0. The case has to be captured here, as
1412 * FORMERR packet do not carry query section, hence
1413 * res_queriesmatch() returns 0.
1415 DprintQ(statp
->options
& RES_DEBUG
,
1417 "server rejected query with EDNS0:\n"),
1419 (*thisresplenp
> *thisanssizp
)
1420 ? *thisanssizp
: *thisresplenp
);
1421 /* record the error */
1422 statp
->_flags
|= RES_F_EDNS0ERR
;
1423 return close_and_return_error (statp
, resplen2
);
1426 if (!(statp
->options
& RES_INSECURE2
)
1427 && (recvresp1
|| !res_queriesmatch(buf
, buf
+ buflen
,
1431 && (recvresp2
|| !res_queriesmatch(buf2
, buf2
+ buflen2
,
1436 * response contains wrong query? ignore it.
1437 * XXX - potential security hazard could
1440 DprintQ((statp
->options
& RES_DEBUG
) ||
1441 (statp
->pfcode
& RES_PRF_REPLY
),
1442 (stdout
, ";; wrong query name:\n"),
1444 (*thisresplenp
> *thisanssizp
)
1445 ? *thisanssizp
: *thisresplenp
);
1448 if (anhp
->rcode
== SERVFAIL
||
1449 anhp
->rcode
== NOTIMP
||
1450 anhp
->rcode
== REFUSED
) {
1451 DprintQ(statp
->options
& RES_DEBUG
,
1452 (stdout
, "server rejected query:\n"),
1454 (*thisresplenp
> *thisanssizp
)
1455 ? *thisanssizp
: *thisresplenp
);
1458 if (recvresp1
|| (buf2
!= NULL
&& recvresp2
)) {
1464 /* No data from the first reply. */
1466 /* We are waiting for a possible second reply. */
1467 if (hp
->id
== anhp
->id
)
1475 /* don't retry if called from dig */
1477 return close_and_return_error (statp
, resplen2
);
1478 __res_iclose(statp
, false);
1480 if (anhp
->rcode
== NOERROR
&& anhp
->ancount
== 0
1481 && anhp
->aa
== 0 && anhp
->ra
== 0 && anhp
->arcount
== 0) {
1482 DprintQ(statp
->options
& RES_DEBUG
,
1483 (stdout
, "referred query:\n"),
1485 (*thisresplenp
> *thisanssizp
)
1486 ? *thisanssizp
: *thisresplenp
);
1489 if (!(statp
->options
& RES_IGNTC
) && anhp
->tc
) {
1491 * To get the rest of answer,
1492 * use TCP with same server.
1494 Dprint(statp
->options
& RES_DEBUG
,
1495 (stdout
, ";; truncated answer\n"));
1497 __res_iclose(statp
, false);
1498 // XXX if we have received one reply we could
1499 // XXX use it and not repeat it over TCP...
1500 if (resplen2
!= NULL
)
1504 /* Mark which reply we received. */
1505 if (recvresp1
== 0 && hp
->id
== anhp
->id
)
1509 /* Repeat waiting if we have a second answer to arrive. */
1510 if ((recvresp1
& recvresp2
) == 0) {
1511 if (single_request
) {
1512 pfd
[0].events
= POLLOUT
;
1513 if (single_request_reopen
) {
1514 __res_iclose (statp
, false);
1515 retval
= reopen (statp
, terrno
, ns
);
1518 if (resplen2
!= NULL
)
1522 pfd
[0].fd
= EXT(statp
).nssocks
[ns
];
1527 /* All is well. We have received both responses (if
1528 two responses were requested). */
1530 } else if (pfd
[0].revents
& (POLLERR
| POLLHUP
| POLLNVAL
))
1531 /* Something went wrong. We can stop trying. */
1532 return close_and_return_error (statp
, resplen2
);
1534 /* poll should not have returned > 0 in this case. */
1541 Aerror(const res_state statp
, FILE *file
, const char *string
, int error
,
1542 const struct sockaddr
*address
)
1546 if ((statp
->options
& RES_DEBUG
) != 0) {
1547 char tmp
[sizeof "xxxx.xxxx.xxxx.255.255.255.255"];
1549 fprintf(file
, "res_send: %s ([%s].%u): %s\n",
1551 (address
->sa_family
== AF_INET
1552 ? inet_ntop(address
->sa_family
,
1553 &((const struct sockaddr_in
*) address
)->sin_addr
,
1555 : inet_ntop(address
->sa_family
,
1556 &((const struct sockaddr_in6
*) address
)->sin6_addr
,
1558 (address
->sa_family
== AF_INET
1559 ? ntohs(((struct sockaddr_in
*) address
)->sin_port
)
1560 : address
->sa_family
== AF_INET6
1561 ? ntohs(((struct sockaddr_in6
*) address
)->sin6_port
)
1569 Perror(const res_state statp
, FILE *file
, const char *string
, int error
) {
1572 if ((statp
->options
& RES_DEBUG
) != 0)
1573 fprintf(file
, "res_send: %s: %s\n",
1574 string
, strerror(error
));
1580 sock_eq(struct sockaddr_in6
*a1
, struct sockaddr_in6
*a2
) {
1581 if (a1
->sin6_family
== a2
->sin6_family
) {
1582 if (a1
->sin6_family
== AF_INET
)
1583 return ((((struct sockaddr_in
*)a1
)->sin_port
==
1584 ((struct sockaddr_in
*)a2
)->sin_port
) &&
1585 (((struct sockaddr_in
*)a1
)->sin_addr
.s_addr
==
1586 ((struct sockaddr_in
*)a2
)->sin_addr
.s_addr
));
1588 return ((a1
->sin6_port
== a2
->sin6_port
) &&
1589 !memcmp(&a1
->sin6_addr
, &a2
->sin6_addr
,
1590 sizeof (struct in6_addr
)));
1592 if (a1
->sin6_family
== AF_INET
) {
1593 struct sockaddr_in6
*sap
= a1
;
1596 } /* assumes that AF_INET and AF_INET6 are the only possibilities */
1597 return ((a1
->sin6_port
== ((struct sockaddr_in
*)a2
)->sin_port
) &&
1598 IN6_IS_ADDR_V4MAPPED(&a1
->sin6_addr
) &&
1599 (a1
->sin6_addr
.s6_addr32
[3] ==
1600 ((struct sockaddr_in
*)a2
)->sin_addr
.s_addr
));