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 /* The send_vc function is responsible for sending a DNS query over TCP
653 to the nameserver numbered NS from the res_state STATP i.e.
654 EXT(statp).nssocks[ns]. The function supports sending both IPv4 and
655 IPv6 queries at the same serially on the same socket.
657 Please note that for TCP there is no way to disable sending both
658 queries, unlike UDP, which honours RES_SNGLKUP and RES_SNGLKUPREOP
659 and sends the queries serially and waits for the result after each
660 sent query. This implemetnation should be corrected to honour these
663 Please also note that for TCP we send both queries over the same
664 socket one after another. This technically violates best practice
665 since the server is allowed to read the first query, respond, and
666 then close the socket (to service another client). If the server
667 does this, then the remaining second query in the socket data buffer
668 will cause the server to send the client an RST which will arrive
669 asynchronously and the client's OS will likely tear down the socket
670 receive buffer resulting in a potentially short read and lost
671 response data. This will force the client to retry the query again,
672 and this process may repeat until all servers and connection resets
673 are exhausted and then the query will fail. It's not known if this
674 happens with any frequency in real DNS server implementations. This
675 implementation should be corrected to use two sockets by default for
678 The query stored in BUF of BUFLEN length is sent first followed by
679 the query stored in BUF2 of BUFLEN2 length. Queries are sent
680 serially on the same socket.
682 Answers to the query are stored firstly in *ANSP up to a max of
683 *ANSSIZP bytes. If more than *ANSSIZP bytes are needed and ANSCP
684 is non-NULL (to indicate that modifying the answer buffer is allowed)
685 then malloc is used to allocate a new response buffer and ANSCP and
686 ANSP will both point to the new buffer. If more than *ANSSIZP bytes
687 are needed but ANSCP is NULL, then as much of the response as
688 possible is read into the buffer, but the results will be truncated.
689 When truncation happens because of a small answer buffer the DNS
690 packets header field TC will bet set to 1, indicating a truncated
691 message and the rest of the socket data will be read and discarded.
693 Answers to the query are stored secondly in *ANSP2 up to a max of
694 *ANSSIZP2 bytes, with the actual response length stored in
695 *RESPLEN2. If more than *ANSSIZP bytes are needed and ANSP2
696 is non-NULL (required for a second query) then malloc is used to
697 allocate a new response buffer, *ANSSIZP2 is set to the new buffer
698 size and *ANSP2_MALLOCED is set to 1.
700 The ANSP2_MALLOCED argument will eventually be removed as the
701 change in buffer pointer can be used to detect the buffer has
702 changed and that the caller should use free on the new buffer.
704 Note that the answers may arrive in any order from the server and
705 therefore the first and second answer buffers may not correspond to
706 the first and second queries.
708 It is not supported to call this function with a non-NULL ANSP2
709 but a NULL ANSCP. Put another way, you can call send_vc with a
710 single unmodifiable buffer or two modifiable buffers, but no other
711 combination is supported.
713 It is the caller's responsibility to free the malloc allocated
714 buffers by detecting that the pointers have changed from their
715 original values i.e. *ANSCP or *ANSP2 has changed.
717 If errors are encountered then *TERRNO is set to an appropriate
718 errno value and a zero result is returned for a recoverable error,
719 and a less-than zero result is returned for a non-recoverable error.
721 If no errors are encountered then *TERRNO is left unmodified and
722 a the length of the first response in bytes is returned. */
724 send_vc(res_state statp
,
725 const u_char
*buf
, int buflen
, const u_char
*buf2
, int buflen2
,
726 u_char
**ansp
, int *anssizp
,
727 int *terrno
, int ns
, u_char
**anscp
, u_char
**ansp2
, int *anssizp2
,
728 int *resplen2
, int *ansp2_malloced
)
730 const HEADER
*hp
= (HEADER
*) buf
;
731 const HEADER
*hp2
= (HEADER
*) buf2
;
732 HEADER
*anhp
= (HEADER
*) *ansp
;
733 struct sockaddr
*nsap
= get_nsaddr (statp
, ns
);
734 int truncating
, connreset
, n
;
735 /* On some architectures compiler might emit a warning indicating
736 'resplen' may be used uninitialized. However if buf2 == NULL
737 then this code won't be executed; if buf2 != NULL, then first
738 time round the loop recvresp1 and recvresp2 will be 0 so this
739 code won't be executed but "thisresplenp = &resplen;" followed
740 by "*thisresplenp = rlen;" will be executed so that subsequent
741 times round the loop resplen has been initialized. So this is
744 DIAG_PUSH_NEEDS_COMMENT
;
745 DIAG_IGNORE_NEEDS_COMMENT (5, "-Wmaybe-uninitialized");
747 DIAG_POP_NEEDS_COMMENT
;
753 if (resplen2
!= NULL
)
759 /* Are we still talking to whom we want to talk to? */
760 if (statp
->_vcsock
>= 0 && (statp
->_flags
& RES_F_VC
) != 0) {
761 struct sockaddr_in6 peer
;
762 socklen_t size
= sizeof peer
;
764 if (getpeername(statp
->_vcsock
,
765 (struct sockaddr
*)&peer
, &size
) < 0 ||
766 !sock_eq(&peer
, (struct sockaddr_in6
*) nsap
)) {
767 __res_iclose(statp
, false);
768 statp
->_flags
&= ~RES_F_VC
;
772 if (statp
->_vcsock
< 0 || (statp
->_flags
& RES_F_VC
) == 0) {
773 if (statp
->_vcsock
>= 0)
774 __res_iclose(statp
, false);
776 statp
->_vcsock
= socket(nsap
->sa_family
, SOCK_STREAM
, 0);
777 if (statp
->_vcsock
< 0) {
779 Perror(statp
, stderr
, "socket(vc)", errno
);
783 if (connect(statp
->_vcsock
, nsap
,
784 nsap
->sa_family
== AF_INET
785 ? sizeof (struct sockaddr_in
)
786 : sizeof (struct sockaddr_in6
)) < 0) {
788 Aerror(statp
, stderr
, "connect/vc", errno
, nsap
);
789 __res_iclose(statp
, false);
792 statp
->_flags
|= RES_F_VC
;
796 * Send length & message
798 len
= htons ((u_short
) buflen
);
799 evConsIovec(&len
, INT16SZ
, &iov
[0]);
800 evConsIovec((void*)buf
, buflen
, &iov
[1]);
802 ssize_t explen
= INT16SZ
+ buflen
;
804 len2
= htons ((u_short
) buflen2
);
805 evConsIovec(&len2
, INT16SZ
, &iov
[2]);
806 evConsIovec((void*)buf2
, buflen2
, &iov
[3]);
808 explen
+= INT16SZ
+ buflen2
;
810 if (TEMP_FAILURE_RETRY (writev(statp
->_vcsock
, iov
, niov
)) != explen
) {
812 Perror(statp
, stderr
, "write failed", errno
);
813 __res_iclose(statp
, false);
817 * Receive length & response
820 /* Skip the second response if there is no second query.
821 To do that we mark the second response as received. */
822 int recvresp2
= buf2
== NULL
;
825 cp
= (u_char
*)&rlen16
;
826 len
= sizeof(rlen16
);
827 while ((n
= TEMP_FAILURE_RETRY (read(statp
->_vcsock
, cp
,
835 Perror(statp
, stderr
, "read failed", errno
);
836 __res_iclose(statp
, false);
838 * A long running process might get its TCP
839 * connection reset if the remote server was
840 * restarted. Requery the server instead of
841 * trying a new one. When there is only one
842 * server, this means that a query might work
843 * instead of failing. We only allow one reset
844 * per query to prevent looping.
846 if (*terrno
== ECONNRESET
&& !connreset
) {
852 int rlen
= ntohs (rlen16
);
857 if ((recvresp1
| recvresp2
) == 0 || buf2
== NULL
) {
858 /* We have not received any responses
859 yet or we only have one response to
861 thisanssizp
= anssizp
;
862 thisansp
= anscp
?: ansp
;
863 assert (anscp
!= NULL
|| ansp2
== NULL
);
864 thisresplenp
= &resplen
;
866 thisanssizp
= anssizp2
;
868 thisresplenp
= resplen2
;
870 anhp
= (HEADER
*) *thisansp
;
872 *thisresplenp
= rlen
;
873 /* Is the answer buffer too small? */
874 if (*thisanssizp
< rlen
) {
875 /* If the current buffer is not the the static
876 user-supplied buffer then we can reallocate
878 if (thisansp
!= NULL
&& thisansp
!= ansp
) {
879 /* Always allocate MAXPACKET, callers expect
880 this specific size. */
881 u_char
*newp
= malloc (MAXPACKET
);
884 __res_iclose(statp
, false);
887 *thisanssizp
= MAXPACKET
;
889 if (thisansp
== ansp2
)
891 anhp
= (HEADER
*) newp
;
892 /* A uint16_t can't be larger than MAXPACKET
893 thus it's safe to allocate MAXPACKET but
894 read RLEN bytes instead. */
897 Dprint(statp
->options
& RES_DEBUG
,
898 (stdout
, ";; response truncated\n")
906 if (__glibc_unlikely (len
< HFIXEDSZ
)) {
908 * Undersized message.
910 Dprint(statp
->options
& RES_DEBUG
,
911 (stdout
, ";; undersized: %d\n", len
));
913 __res_iclose(statp
, false);
918 while (len
!= 0 && (n
= read(statp
->_vcsock
, (char *)cp
, (int)len
)) > 0){
922 if (__glibc_unlikely (n
<= 0)) {
924 Perror(statp
, stderr
, "read(vc)", errno
);
925 __res_iclose(statp
, false);
928 if (__glibc_unlikely (truncating
)) {
930 * Flush rest of answer so connection stays in synch.
933 len
= rlen
- *thisanssizp
;
937 n
= read(statp
->_vcsock
, junk
,
938 (len
> sizeof junk
) ? sizeof junk
: len
);
946 * If the calling application has bailed out of
947 * a previous call and failed to arrange to have
948 * the circuit closed or the server has got
949 * itself confused, then drop the packet and
950 * wait for the correct one.
952 if ((recvresp1
|| hp
->id
!= anhp
->id
)
953 && (recvresp2
|| hp2
->id
!= anhp
->id
)) {
954 DprintQ((statp
->options
& RES_DEBUG
) ||
955 (statp
->pfcode
& RES_PRF_REPLY
),
956 (stdout
, ";; old answer (unexpected):\n"),
958 (rlen
> *thisanssizp
) ? *thisanssizp
: rlen
);
962 /* Mark which reply we received. */
963 if (recvresp1
== 0 && hp
->id
== anhp
->id
)
967 /* Repeat waiting if we have a second answer to arrive. */
968 if ((recvresp1
& recvresp2
) == 0)
972 * All is well, or the error is fatal. Signal that the
973 * next nameserver ought not be tried.
979 reopen (res_state statp
, int *terrno
, int ns
)
981 if (EXT(statp
).nssocks
[ns
] == -1) {
982 struct sockaddr
*nsap
= get_nsaddr (statp
, ns
);
985 /* only try IPv6 if IPv6 NS and if not failed before */
986 if (nsap
->sa_family
== AF_INET6
&& !statp
->ipv6_unavail
) {
987 EXT(statp
).nssocks
[ns
]
988 = socket(PF_INET6
, SOCK_DGRAM
|SOCK_NONBLOCK
, 0);
989 if (EXT(statp
).nssocks
[ns
] < 0)
990 statp
->ipv6_unavail
= errno
== EAFNOSUPPORT
;
991 slen
= sizeof (struct sockaddr_in6
);
992 } else if (nsap
->sa_family
== AF_INET
) {
993 EXT(statp
).nssocks
[ns
]
994 = socket(PF_INET
, SOCK_DGRAM
|SOCK_NONBLOCK
, 0);
995 slen
= sizeof (struct sockaddr_in
);
997 if (EXT(statp
).nssocks
[ns
] < 0) {
999 Perror(statp
, stderr
, "socket(dg)", errno
);
1004 * On a 4.3BSD+ machine (client and server,
1005 * actually), sending to a nameserver datagram
1006 * port with no nameserver will cause an
1007 * ICMP port unreachable message to be returned.
1008 * If our datagram socket is "connected" to the
1009 * server, we get an ECONNREFUSED error on the next
1010 * socket operation, and select returns if the
1011 * error message is received. We can thus detect
1012 * the absence of a nameserver without timing out.
1014 if (connect(EXT(statp
).nssocks
[ns
], nsap
, slen
) < 0) {
1015 Aerror(statp
, stderr
, "connect(dg)", errno
, nsap
);
1016 __res_iclose(statp
, false);
1024 /* The send_dg function is responsible for sending a DNS query over UDP
1025 to the nameserver numbered NS from the res_state STATP i.e.
1026 EXT(statp).nssocks[ns]. The function supports IPv4 and IPv6 queries
1027 along with the ability to send the query in parallel for both stacks
1028 (default) or serially (RES_SINGLKUP). It also supports serial lookup
1029 with a close and reopen of the socket used to talk to the server
1030 (RES_SNGLKUPREOP) to work around broken name servers.
1032 The query stored in BUF of BUFLEN length is sent first followed by
1033 the query stored in BUF2 of BUFLEN2 length. Queries are sent
1034 in parallel (default) or serially (RES_SINGLKUP or RES_SNGLKUPREOP).
1036 Answers to the query are stored firstly in *ANSP up to a max of
1037 *ANSSIZP bytes. If more than *ANSSIZP bytes are needed and ANSCP
1038 is non-NULL (to indicate that modifying the answer buffer is allowed)
1039 then malloc is used to allocate a new response buffer and ANSCP and
1040 ANSP will both point to the new buffer. If more than *ANSSIZP bytes
1041 are needed but ANSCP is NULL, then as much of the response as
1042 possible is read into the buffer, but the results will be truncated.
1043 When truncation happens because of a small answer buffer the DNS
1044 packets header field TC will bet set to 1, indicating a truncated
1045 message, while the rest of the UDP packet is discarded.
1047 Answers to the query are stored secondly in *ANSP2 up to a max of
1048 *ANSSIZP2 bytes, with the actual response length stored in
1049 *RESPLEN2. If more than *ANSSIZP bytes are needed and ANSP2
1050 is non-NULL (required for a second query) then malloc is used to
1051 allocate a new response buffer, *ANSSIZP2 is set to the new buffer
1052 size and *ANSP2_MALLOCED is set to 1.
1054 The ANSP2_MALLOCED argument will eventually be removed as the
1055 change in buffer pointer can be used to detect the buffer has
1056 changed and that the caller should use free on the new buffer.
1058 Note that the answers may arrive in any order from the server and
1059 therefore the first and second answer buffers may not correspond to
1060 the first and second queries.
1062 It is not supported to call this function with a non-NULL ANSP2
1063 but a NULL ANSCP. Put another way, you can call send_vc with a
1064 single unmodifiable buffer or two modifiable buffers, but no other
1065 combination is supported.
1067 It is the caller's responsibility to free the malloc allocated
1068 buffers by detecting that the pointers have changed from their
1069 original values i.e. *ANSCP or *ANSP2 has changed.
1071 If an answer is truncated because of UDP datagram DNS limits then
1072 *V_CIRCUIT is set to 1 and the return value non-zero to indicate to
1073 the caller to retry with TCP. The value *GOTSOMEWHERE is set to 1
1074 if any progress was made reading a response from the nameserver and
1075 is used by the caller to distinguish between ECONNREFUSED and
1076 ETIMEDOUT (the latter if *GOTSOMEWHERE is 1).
1078 If errors are encountered then *TERRNO is set to an appropriate
1079 errno value and a zero result is returned for a recoverable error,
1080 and a less-than zero result is returned for a non-recoverable error.
1082 If no errors are encountered then *TERRNO is left unmodified and
1083 a the length of the first response in bytes is returned. */
1085 send_dg(res_state statp
,
1086 const u_char
*buf
, int buflen
, const u_char
*buf2
, int buflen2
,
1087 u_char
**ansp
, int *anssizp
,
1088 int *terrno
, int ns
, int *v_circuit
, int *gotsomewhere
, u_char
**anscp
,
1089 u_char
**ansp2
, int *anssizp2
, int *resplen2
, int *ansp2_malloced
)
1091 const HEADER
*hp
= (HEADER
*) buf
;
1092 const HEADER
*hp2
= (HEADER
*) buf2
;
1093 struct timespec now
, timeout
, finish
;
1094 struct pollfd pfd
[1];
1096 struct sockaddr_in6 from
;
1101 * Compute time for the total operation.
1103 int seconds
= (statp
->retrans
<< ns
);
1105 seconds
/= statp
->nscount
;
1108 bool single_request_reopen
= (statp
->options
& RES_SNGLKUPREOP
) != 0;
1109 bool single_request
= (((statp
->options
& RES_SNGLKUP
) != 0)
1110 | single_request_reopen
);
1111 int save_gotsomewhere
= *gotsomewhere
;
1115 retval
= reopen (statp
, terrno
, ns
);
1120 evConsTime(&timeout
, seconds
, 0);
1121 evAddTime(&finish
, &now
, &timeout
);
1122 int need_recompute
= 0;
1125 /* Skip the second response if there is no second query.
1126 To do that we mark the second response as received. */
1127 int recvresp2
= buf2
== NULL
;
1128 pfd
[0].fd
= EXT(statp
).nssocks
[ns
];
1129 pfd
[0].events
= POLLOUT
;
1130 if (resplen2
!= NULL
)
1133 if (need_recompute
) {
1136 if (evCmpTime(finish
, now
) <= 0) {
1138 Perror(statp
, stderr
, "poll", errno
);
1140 __res_iclose(statp
, false);
1143 evSubTime(&timeout
, &finish
, &now
);
1146 /* Convert struct timespec in milliseconds. */
1147 ptimeout
= timeout
.tv_sec
* 1000 + timeout
.tv_nsec
/ 1000000;
1151 n
= __poll (pfd
, 1, 0);
1152 if (__glibc_unlikely (n
== 0)) {
1153 n
= __poll (pfd
, 1, ptimeout
);
1157 Dprint(statp
->options
& RES_DEBUG
, (stdout
, ";; timeout\n"));
1158 if (resplen
> 1 && (recvresp1
|| (buf2
!= NULL
&& recvresp2
)))
1160 /* There are quite a few broken name servers out
1161 there which don't handle two outstanding
1162 requests from the same source. There are also
1163 broken firewall settings. If we time out after
1164 having received one answer switch to the mode
1165 where we send the second request only once we
1166 have received the first answer. */
1167 if (!single_request
)
1169 statp
->options
|= RES_SNGLKUP
;
1170 single_request
= true;
1171 *gotsomewhere
= save_gotsomewhere
;
1174 else if (!single_request_reopen
)
1176 statp
->options
|= RES_SNGLKUPREOP
;
1177 single_request_reopen
= true;
1178 *gotsomewhere
= save_gotsomewhere
;
1179 __res_iclose (statp
, false);
1192 goto recompute_resend
;
1197 if (pfd
[0].revents
& POLLOUT
) {
1198 #ifndef __ASSUME_SENDMMSG
1199 static int have_sendmmsg
;
1201 # define have_sendmmsg 1
1203 if (have_sendmmsg
>= 0 && nwritten
== 0 && buf2
!= NULL
1206 struct iovec iov
[2];
1207 struct mmsghdr reqs
[2];
1208 reqs
[0].msg_hdr
.msg_name
= NULL
;
1209 reqs
[0].msg_hdr
.msg_namelen
= 0;
1210 reqs
[0].msg_hdr
.msg_iov
= &iov
[0];
1211 reqs
[0].msg_hdr
.msg_iovlen
= 1;
1212 iov
[0].iov_base
= (void *) buf
;
1213 iov
[0].iov_len
= buflen
;
1214 reqs
[0].msg_hdr
.msg_control
= NULL
;
1215 reqs
[0].msg_hdr
.msg_controllen
= 0;
1217 reqs
[1].msg_hdr
.msg_name
= NULL
;
1218 reqs
[1].msg_hdr
.msg_namelen
= 0;
1219 reqs
[1].msg_hdr
.msg_iov
= &iov
[1];
1220 reqs
[1].msg_hdr
.msg_iovlen
= 1;
1221 iov
[1].iov_base
= (void *) buf2
;
1222 iov
[1].iov_len
= buflen2
;
1223 reqs
[1].msg_hdr
.msg_control
= NULL
;
1224 reqs
[1].msg_hdr
.msg_controllen
= 0;
1226 int ndg
= __sendmmsg (pfd
[0].fd
, reqs
, 2, MSG_NOSIGNAL
);
1227 if (__glibc_likely (ndg
== 2))
1229 if (reqs
[0].msg_len
!= buflen
1230 || reqs
[1].msg_len
!= buflen2
)
1233 pfd
[0].events
= POLLIN
;
1236 else if (ndg
== 1 && reqs
[0].msg_len
== buflen
)
1238 else if (ndg
< 0 && (errno
== EINTR
|| errno
== EAGAIN
))
1239 goto recompute_resend
;
1242 #ifndef __ASSUME_SENDMMSG
1243 if (__glibc_unlikely (have_sendmmsg
== 0))
1245 if (ndg
< 0 && errno
== ENOSYS
)
1255 Perror(statp
, stderr
, "sendmmsg", errno
);
1262 #ifndef __ASSUME_SENDMMSG
1266 sr
= send (pfd
[0].fd
, buf2
, buflen2
, MSG_NOSIGNAL
);
1268 sr
= send (pfd
[0].fd
, buf
, buflen
, MSG_NOSIGNAL
);
1270 if (sr
!= (nwritten
!= 0 ? buflen2
: buflen
)) {
1271 if (errno
== EINTR
|| errno
== EAGAIN
)
1272 goto recompute_resend
;
1273 Perror(statp
, stderr
, "send", errno
);
1277 if (nwritten
!= 0 || buf2
== NULL
|| single_request
)
1278 pfd
[0].events
= POLLIN
;
1280 pfd
[0].events
= POLLIN
| POLLOUT
;
1284 } else if (pfd
[0].revents
& POLLIN
) {
1289 if ((recvresp1
| recvresp2
) == 0 || buf2
== NULL
) {
1290 /* We have not received any responses
1291 yet or we only have one response to
1293 thisanssizp
= anssizp
;
1294 thisansp
= anscp
?: ansp
;
1295 assert (anscp
!= NULL
|| ansp2
== NULL
);
1296 thisresplenp
= &resplen
;
1298 thisanssizp
= anssizp2
;
1300 thisresplenp
= resplen2
;
1303 if (*thisanssizp
< MAXPACKET
1304 /* If the current buffer is not the the static
1305 user-supplied buffer then we can reallocate
1307 && (thisansp
!= NULL
&& thisansp
!= ansp
)
1309 /* Is the size too small? */
1310 && (ioctl (pfd
[0].fd
, FIONREAD
, thisresplenp
) < 0
1311 || *thisanssizp
< *thisresplenp
)
1314 /* Always allocate MAXPACKET, callers expect
1315 this specific size. */
1316 u_char
*newp
= malloc (MAXPACKET
);
1318 *thisanssizp
= MAXPACKET
;
1320 if (thisansp
== ansp2
)
1321 *ansp2_malloced
= 1;
1324 /* We could end up with truncation if anscp was NULL
1325 (not allowed to change caller's buffer) and the
1326 response buffer size is too small. This isn't a
1327 reliable way to detect truncation because the ioctl
1328 may be an inaccurate report of the UDP message size.
1329 Therefore we use this only to issue debug output.
1330 To do truncation accurately with UDP we need
1331 MSG_TRUNC which is only available on Linux. We
1332 can abstract out the Linux-specific feature in the
1333 future to detect truncation. */
1334 if (__glibc_unlikely (*thisanssizp
< *thisresplenp
)) {
1335 Dprint(statp
->options
& RES_DEBUG
,
1336 (stdout
, ";; response may be truncated (UDP)\n")
1340 HEADER
*anhp
= (HEADER
*) *thisansp
;
1341 socklen_t fromlen
= sizeof(struct sockaddr_in6
);
1342 assert (sizeof(from
) <= fromlen
);
1343 *thisresplenp
= recvfrom(pfd
[0].fd
, (char*)*thisansp
,
1345 (struct sockaddr
*)&from
, &fromlen
);
1346 if (__glibc_unlikely (*thisresplenp
<= 0)) {
1347 if (errno
== EINTR
|| errno
== EAGAIN
) {
1351 Perror(statp
, stderr
, "recvfrom", errno
);
1355 if (__glibc_unlikely (*thisresplenp
< HFIXEDSZ
)) {
1357 * Undersized message.
1359 Dprint(statp
->options
& RES_DEBUG
,
1360 (stdout
, ";; undersized: %d\n",
1365 if ((recvresp1
|| hp
->id
!= anhp
->id
)
1366 && (recvresp2
|| hp2
->id
!= anhp
->id
)) {
1368 * response from old query, ignore it.
1369 * XXX - potential security hazard could
1372 DprintQ((statp
->options
& RES_DEBUG
) ||
1373 (statp
->pfcode
& RES_PRF_REPLY
),
1374 (stdout
, ";; old answer:\n"),
1376 (*thisresplenp
> *thisanssizp
)
1377 ? *thisanssizp
: *thisresplenp
);
1380 if (!(statp
->options
& RES_INSECURE1
) &&
1381 !res_ourserver_p(statp
, &from
)) {
1383 * response from wrong server? ignore it.
1384 * XXX - potential security hazard could
1387 DprintQ((statp
->options
& RES_DEBUG
) ||
1388 (statp
->pfcode
& RES_PRF_REPLY
),
1389 (stdout
, ";; not our server:\n"),
1391 (*thisresplenp
> *thisanssizp
)
1392 ? *thisanssizp
: *thisresplenp
);
1395 #ifdef RES_USE_EDNS0
1396 if (anhp
->rcode
== FORMERR
1397 && (statp
->options
& RES_USE_EDNS0
) != 0U) {
1399 * Do not retry if the server does not understand
1400 * EDNS0. The case has to be captured here, as
1401 * FORMERR packet do not carry query section, hence
1402 * res_queriesmatch() returns 0.
1404 DprintQ(statp
->options
& RES_DEBUG
,
1406 "server rejected query with EDNS0:\n"),
1408 (*thisresplenp
> *thisanssizp
)
1409 ? *thisanssizp
: *thisresplenp
);
1410 /* record the error */
1411 statp
->_flags
|= RES_F_EDNS0ERR
;
1415 if (!(statp
->options
& RES_INSECURE2
)
1416 && (recvresp1
|| !res_queriesmatch(buf
, buf
+ buflen
,
1420 && (recvresp2
|| !res_queriesmatch(buf2
, buf2
+ buflen2
,
1425 * response contains wrong query? ignore it.
1426 * XXX - potential security hazard could
1429 DprintQ((statp
->options
& RES_DEBUG
) ||
1430 (statp
->pfcode
& RES_PRF_REPLY
),
1431 (stdout
, ";; wrong query name:\n"),
1433 (*thisresplenp
> *thisanssizp
)
1434 ? *thisanssizp
: *thisresplenp
);
1437 if (anhp
->rcode
== SERVFAIL
||
1438 anhp
->rcode
== NOTIMP
||
1439 anhp
->rcode
== REFUSED
) {
1440 DprintQ(statp
->options
& RES_DEBUG
,
1441 (stdout
, "server rejected query:\n"),
1443 (*thisresplenp
> *thisanssizp
)
1444 ? *thisanssizp
: *thisresplenp
);
1447 if (recvresp1
|| (buf2
!= NULL
&& recvresp2
)) {
1453 /* No data from the first reply. */
1455 /* We are waiting for a possible second reply. */
1456 if (hp
->id
== anhp
->id
)
1464 __res_iclose(statp
, false);
1465 /* don't retry if called from dig */
1469 if (anhp
->rcode
== NOERROR
&& anhp
->ancount
== 0
1470 && anhp
->aa
== 0 && anhp
->ra
== 0 && anhp
->arcount
== 0) {
1471 DprintQ(statp
->options
& RES_DEBUG
,
1472 (stdout
, "referred query:\n"),
1474 (*thisresplenp
> *thisanssizp
)
1475 ? *thisanssizp
: *thisresplenp
);
1478 if (!(statp
->options
& RES_IGNTC
) && anhp
->tc
) {
1480 * To get the rest of answer,
1481 * use TCP with same server.
1483 Dprint(statp
->options
& RES_DEBUG
,
1484 (stdout
, ";; truncated answer\n"));
1486 __res_iclose(statp
, false);
1487 // XXX if we have received one reply we could
1488 // XXX use it and not repeat it over TCP...
1491 /* Mark which reply we received. */
1492 if (recvresp1
== 0 && hp
->id
== anhp
->id
)
1496 /* Repeat waiting if we have a second answer to arrive. */
1497 if ((recvresp1
& recvresp2
) == 0) {
1498 if (single_request
) {
1499 pfd
[0].events
= POLLOUT
;
1500 if (single_request_reopen
) {
1501 __res_iclose (statp
, false);
1502 retval
= reopen (statp
, terrno
, ns
);
1505 pfd
[0].fd
= EXT(statp
).nssocks
[ns
];
1511 * All is well, or the error is fatal. Signal that the
1512 * next nameserver ought not be tried.
1515 } else if (pfd
[0].revents
& (POLLERR
| POLLHUP
| POLLNVAL
)) {
1516 /* Something went wrong. We can stop trying. */
1520 /* poll should not have returned > 0 in this case. */
1527 Aerror(const res_state statp
, FILE *file
, const char *string
, int error
,
1528 const struct sockaddr
*address
)
1532 if ((statp
->options
& RES_DEBUG
) != 0) {
1533 char tmp
[sizeof "xxxx.xxxx.xxxx.255.255.255.255"];
1535 fprintf(file
, "res_send: %s ([%s].%u): %s\n",
1537 (address
->sa_family
== AF_INET
1538 ? inet_ntop(address
->sa_family
,
1539 &((const struct sockaddr_in
*) address
)->sin_addr
,
1541 : inet_ntop(address
->sa_family
,
1542 &((const struct sockaddr_in6
*) address
)->sin6_addr
,
1544 (address
->sa_family
== AF_INET
1545 ? ntohs(((struct sockaddr_in
*) address
)->sin_port
)
1546 : address
->sa_family
== AF_INET6
1547 ? ntohs(((struct sockaddr_in6
*) address
)->sin6_port
)
1555 Perror(const res_state statp
, FILE *file
, const char *string
, int error
) {
1558 if ((statp
->options
& RES_DEBUG
) != 0)
1559 fprintf(file
, "res_send: %s: %s\n",
1560 string
, strerror(error
));
1566 sock_eq(struct sockaddr_in6
*a1
, struct sockaddr_in6
*a2
) {
1567 if (a1
->sin6_family
== a2
->sin6_family
) {
1568 if (a1
->sin6_family
== AF_INET
)
1569 return ((((struct sockaddr_in
*)a1
)->sin_port
==
1570 ((struct sockaddr_in
*)a2
)->sin_port
) &&
1571 (((struct sockaddr_in
*)a1
)->sin_addr
.s_addr
==
1572 ((struct sockaddr_in
*)a2
)->sin_addr
.s_addr
));
1574 return ((a1
->sin6_port
== a2
->sin6_port
) &&
1575 !memcmp(&a1
->sin6_addr
, &a2
->sin6_addr
,
1576 sizeof (struct in6_addr
)));
1578 if (a1
->sin6_family
== AF_INET
) {
1579 struct sockaddr_in6
*sap
= a1
;
1582 } /* assumes that AF_INET and AF_INET6 are the only possibilities */
1583 return ((a1
->sin6_port
== ((struct sockaddr_in
*)a2
)->sin_port
) &&
1584 IN6_IS_ADDR_V4MAPPED(&a1
->sin6_addr
) &&
1585 (a1
->sin6_addr
.s6_addr32
[3] ==
1586 ((struct sockaddr_in
*)a2
)->sin_addr
.s_addr
));