2 * Copyright (c) 1985, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
33 * Permission to use, copy, modify, and distribute this software for any
34 * purpose with or without fee is hereby granted, provided that the above
35 * copyright notice and this permission notice appear in all copies, and that
36 * the name of Digital Equipment Corporation not be used in advertising or
37 * publicity pertaining to distribution of the document or software without
38 * specific, written prior permission.
40 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
41 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
42 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
43 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
44 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
45 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
46 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
51 * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
53 * Permission to use, copy, modify, and distribute this software for any
54 * purpose with or without fee is hereby granted, provided that the above
55 * copyright notice and this permission notice appear in all copies.
57 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
58 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
59 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
60 * CONSORTIUM 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
67 #if defined(LIBC_SCCS) && !defined(lint)
68 static const char sccsid
[] = "@(#)res_send.c 8.1 (Berkeley) 6/4/93";
69 static const char rcsid
[] = "$BINDId: res_send.c,v 8.38 2000/03/30 20:16:51 vixie Exp $";
70 #endif /* LIBC_SCCS and not lint */
73 * Send query to name server and wait for reply.
77 #include <sys/types.h>
78 #include <sys/param.h>
80 #include <sys/socket.h>
84 #include <netinet/in.h>
85 #include <arpa/nameser.h>
86 #include <arpa/inet.h>
87 #include <sys/ioctl.h>
98 #include <kernel-features.h>
101 #define MAXPACKET PACKETSZ
103 #define MAXPACKET 65536
107 #ifndef __ASSUME_SOCK_CLOEXEC
108 static int __have_o_nonblock
;
110 # define __have_o_nonblock 0
114 /* From ev_streams.c. */
117 __attribute ((always_inline
))
118 evConsIovec(void *buf
, size_t cnt
, struct iovec
*vec
) {
119 memset(vec
, 0xf5, sizeof (*vec
));
124 /* From ev_timers.c. */
126 #define BILLION 1000000000
129 evConsTime(struct timespec
*res
, time_t sec
, long nsec
) {
135 evAddTime(struct timespec
*res
, const struct timespec
*addend1
,
136 const struct timespec
*addend2
) {
137 res
->tv_sec
= addend1
->tv_sec
+ addend2
->tv_sec
;
138 res
->tv_nsec
= addend1
->tv_nsec
+ addend2
->tv_nsec
;
139 if (res
->tv_nsec
>= BILLION
) {
141 res
->tv_nsec
-= BILLION
;
146 evSubTime(struct timespec
*res
, const struct timespec
*minuend
,
147 const struct timespec
*subtrahend
) {
148 res
->tv_sec
= minuend
->tv_sec
- subtrahend
->tv_sec
;
149 if (minuend
->tv_nsec
>= subtrahend
->tv_nsec
)
150 res
->tv_nsec
= minuend
->tv_nsec
- subtrahend
->tv_nsec
;
152 res
->tv_nsec
= (BILLION
153 - subtrahend
->tv_nsec
+ minuend
->tv_nsec
);
159 evCmpTime(struct timespec a
, struct timespec b
) {
160 long x
= a
.tv_sec
- b
.tv_sec
;
163 x
= a
.tv_nsec
- b
.tv_nsec
;
164 return (x
< 0L ? (-1) : x
> 0L ? (1) : (0));
168 evNowTime(struct timespec
*res
) {
171 if (gettimeofday(&now
, NULL
) < 0)
172 evConsTime(res
, 0, 0);
174 TIMEVAL_TO_TIMESPEC (&now
, res
);
178 /* Options. Leave them on. */
180 #include "res_debug.h"
182 #define EXT(res) ((res)->_u._ext)
186 static int send_vc(res_state
, const u_char
*, int,
188 u_char
**, int *, int *, int, u_char
**,
189 u_char
**, int *, int *);
190 static int send_dg(res_state
, const u_char
*, int,
192 u_char
**, int *, int *, int,
193 int *, int *, u_char
**,
194 u_char
**, int *, int *);
196 static void Aerror(const res_state
, FILE *, const char *, int,
197 const struct sockaddr
*);
198 static void Perror(const res_state
, FILE *, const char *, int);
200 static int sock_eq(struct sockaddr_in6
*, struct sockaddr_in6
*);
204 static void convaddr4to6(struct sockaddr_in6
*sa
);
209 * res_isourserver(ina)
210 * looks up "ina" in _res.ns_addr_list[]
215 * paul vixie, 29may94
218 res_ourserver_p(const res_state statp
, const struct sockaddr_in6
*inp
)
222 if (inp
->sin6_family
== AF_INET
) {
223 struct sockaddr_in
*in4p
= (struct sockaddr_in
*) inp
;
224 in_port_t port
= in4p
->sin_port
;
225 in_addr_t addr
= in4p
->sin_addr
.s_addr
;
227 for (ns
= 0; ns
< MAXNS
; ns
++) {
228 const struct sockaddr_in
*srv
=
229 (struct sockaddr_in
*)EXT(statp
).nsaddrs
[ns
];
231 if ((srv
!= NULL
) && (srv
->sin_family
== AF_INET
) &&
232 (srv
->sin_port
== port
) &&
233 (srv
->sin_addr
.s_addr
== INADDR_ANY
||
234 srv
->sin_addr
.s_addr
== addr
))
237 } else if (inp
->sin6_family
== AF_INET6
) {
238 for (ns
= 0; ns
< MAXNS
; ns
++) {
239 const struct sockaddr_in6
*srv
= EXT(statp
).nsaddrs
[ns
];
240 if ((srv
!= NULL
) && (srv
->sin6_family
== AF_INET6
) &&
241 (srv
->sin6_port
== inp
->sin6_port
) &&
242 !(memcmp(&srv
->sin6_addr
, &in6addr_any
,
243 sizeof (struct in6_addr
)) &&
244 memcmp(&srv
->sin6_addr
, &inp
->sin6_addr
,
245 sizeof (struct in6_addr
))))
253 * res_nameinquery(name, type, class, buf, eom)
254 * look for (name,type,class) in the query section of packet (buf,eom)
256 * buf + HFIXEDSZ <= eom
262 * paul vixie, 29may94
265 res_nameinquery(const char *name
, int type
, int class,
266 const u_char
*buf
, const u_char
*eom
)
268 const u_char
*cp
= buf
+ HFIXEDSZ
;
269 int qdcount
= ntohs(((HEADER
*)buf
)->qdcount
);
271 while (qdcount
-- > 0) {
272 char tname
[MAXDNAME
+1];
273 int n
, ttype
, tclass
;
275 n
= dn_expand(buf
, eom
, cp
, tname
, sizeof tname
);
279 if (cp
+ 2 * INT16SZ
> eom
)
282 NS_GET16(tclass
, cp
);
283 if (ttype
== type
&& tclass
== class &&
284 ns_samename(tname
, name
) == 1)
289 libresolv_hidden_def (res_nameinquery
)
292 * res_queriesmatch(buf1, eom1, buf2, eom2)
293 * is there a 1:1 mapping of (name,type,class)
294 * in (buf1,eom1) and (buf2,eom2)?
297 * 0 : not a 1:1 mapping
298 * >0 : is a 1:1 mapping
300 * paul vixie, 29may94
303 res_queriesmatch(const u_char
*buf1
, const u_char
*eom1
,
304 const u_char
*buf2
, const u_char
*eom2
)
306 if (buf1
+ HFIXEDSZ
> eom1
|| buf2
+ HFIXEDSZ
> eom2
)
310 * Only header section present in replies to
311 * dynamic update packets.
313 if ((((HEADER
*)buf1
)->opcode
== ns_o_update
) &&
314 (((HEADER
*)buf2
)->opcode
== ns_o_update
))
317 /* Note that we initially do not convert QDCOUNT to the host byte
318 order. We can compare it with the second buffer's QDCOUNT
319 value without doing this. */
320 int qdcount
= ((HEADER
*)buf1
)->qdcount
;
321 if (qdcount
!= ((HEADER
*)buf2
)->qdcount
)
324 qdcount
= htons (qdcount
);
325 const u_char
*cp
= buf1
+ HFIXEDSZ
;
327 while (qdcount
-- > 0) {
328 char tname
[MAXDNAME
+1];
329 int n
, ttype
, tclass
;
331 n
= dn_expand(buf1
, eom1
, cp
, tname
, sizeof tname
);
335 if (cp
+ 2 * INT16SZ
> eom1
)
338 NS_GET16(tclass
, cp
);
339 if (!res_nameinquery(tname
, ttype
, tclass
, buf2
, eom2
))
344 libresolv_hidden_def (res_queriesmatch
)
347 __libc_res_nsend(res_state statp
, const u_char
*buf
, int buflen
,
348 const u_char
*buf2
, int buflen2
,
349 u_char
*ans
, int anssiz
, u_char
**ansp
, u_char
**ansp2
,
350 int *nansp2
, int *resplen2
)
352 int gotsomewhere
, terrno
, try, v_circuit
, resplen
, ns
, n
;
354 if (statp
->nscount
== 0) {
359 if (anssiz
< (buf2
== NULL
? 1 : 2) * HFIXEDSZ
) {
360 __set_errno (EINVAL
);
365 if (__builtin_expect (statp
->qhook
|| statp
->rhook
, 0)) {
366 if (anssiz
< MAXPACKET
&& ansp
) {
367 u_char
*buf
= malloc (MAXPACKET
);
370 memcpy (buf
, ans
, HFIXEDSZ
);
378 DprintQ((statp
->options
& RES_DEBUG
) || (statp
->pfcode
& RES_PRF_QUERY
),
379 (stdout
, ";; res_send()\n"), buf
, buflen
);
380 v_circuit
= ((statp
->options
& RES_USEVC
)
382 || buflen2
> PACKETSZ
);
387 * If the ns_addr_list in the resolver context has changed, then
388 * invalidate our cached copy and the associated timing data.
390 if (EXT(statp
).nsinit
) {
393 if (EXT(statp
).nscount
!= statp
->nscount
)
396 for (ns
= 0; ns
< MAXNS
; ns
++) {
397 unsigned int map
= EXT(statp
).nsmap
[ns
];
399 && !sock_eq((struct sockaddr_in6
*)
400 &statp
->nsaddr_list
[map
],
401 EXT(statp
).nsaddrs
[ns
]))
408 __res_iclose(statp
, false);
412 * Maybe initialize our private copy of the ns_addr_list.
414 if (EXT(statp
).nsinit
== 0) {
415 unsigned char map
[MAXNS
];
417 memset (map
, MAXNS
, sizeof (map
));
418 for (n
= 0; n
< MAXNS
; n
++) {
419 ns
= EXT(statp
).nsmap
[n
];
420 if (ns
< statp
->nscount
)
422 else if (ns
< MAXNS
) {
423 free(EXT(statp
).nsaddrs
[n
]);
424 EXT(statp
).nsaddrs
[n
] = NULL
;
425 EXT(statp
).nsmap
[n
] = MAXNS
;
429 if (statp
->nscount
> EXT(statp
).nscount
)
430 for (n
= EXT(statp
).nscount
, ns
= 0;
431 n
< statp
->nscount
; n
++) {
433 && EXT(statp
).nsmap
[ns
] != MAXNS
)
437 EXT(statp
).nsmap
[ns
] = n
;
440 EXT(statp
).nscount
= n
;
441 for (ns
= 0; ns
< EXT(statp
).nscount
; ns
++) {
443 if (EXT(statp
).nsaddrs
[n
] == NULL
)
444 EXT(statp
).nsaddrs
[n
] =
445 malloc(sizeof (struct sockaddr_in6
));
446 if (EXT(statp
).nsaddrs
[n
] != NULL
) {
447 memset (mempcpy(EXT(statp
).nsaddrs
[n
],
448 &statp
->nsaddr_list
[ns
],
449 sizeof (struct sockaddr_in
)),
451 sizeof (struct sockaddr_in6
)
452 - sizeof (struct sockaddr_in
));
453 EXT(statp
).nssocks
[n
] = -1;
457 EXT(statp
).nsinit
= 1;
461 * Some resolvers want to even out the load on their nameservers.
462 * Note that RES_BLAST overrides RES_ROTATE.
464 if (__builtin_expect ((statp
->options
& RES_ROTATE
) != 0, 0) &&
465 (statp
->options
& RES_BLAST
) == 0) {
466 struct sockaddr_in6
*ina
;
470 while (n
< MAXNS
&& EXT(statp
).nsmap
[n
] == MAXNS
)
473 ina
= EXT(statp
).nsaddrs
[n
];
474 map
= EXT(statp
).nsmap
[n
];
478 && EXT(statp
).nsmap
[ns
] == MAXNS
)
482 EXT(statp
).nsaddrs
[n
] = EXT(statp
).nsaddrs
[ns
];
483 EXT(statp
).nsmap
[n
] = EXT(statp
).nsmap
[ns
];
486 EXT(statp
).nsaddrs
[n
] = ina
;
487 EXT(statp
).nsmap
[n
] = map
;
492 * Send request, RETRY times, or until successful.
494 for (try = 0; try < statp
->retry
; try++) {
495 for (ns
= 0; ns
< MAXNS
; ns
++)
497 struct sockaddr_in6
*nsap
= EXT(statp
).nsaddrs
[ns
];
503 if (__builtin_expect (statp
->qhook
!= NULL
, 0)) {
504 int done
= 0, loops
= 0;
509 struct sockaddr_in
*nsap4
;
510 nsap4
= (struct sockaddr_in
*) nsap
;
511 act
= (*statp
->qhook
)(&nsap4
, &buf
, &buflen
,
512 ans
, anssiz
, &resplen
);
513 nsap
= (struct sockaddr_in6
*) nsap4
;
519 __res_iclose(statp
, false);
524 /* give the hook another try */
525 if (++loops
< 42) /*doug adams*/
540 Dprint(statp
->options
& RES_DEBUG
,
541 (stdout
, ";; Querying server (# %d) address = %s\n",
542 ns
+ 1, inet_ntop(AF_INET6
, &nsap
->sin6_addr
,
543 tmpbuf
, sizeof (tmpbuf
))));
545 if (__builtin_expect (v_circuit
, 0)) {
546 /* Use VC; at most one attempt per server. */
548 n
= send_vc(statp
, buf
, buflen
, buf2
, buflen2
,
549 &ans
, &anssiz
, &terrno
,
550 ns
, ansp
, ansp2
, nansp2
, resplen2
);
557 n
= send_dg(statp
, buf
, buflen
, buf2
, buflen2
,
558 &ans
, &anssiz
, &terrno
,
559 ns
, &v_circuit
, &gotsomewhere
, ansp
,
560 ansp2
, nansp2
, resplen2
);
566 // XXX Check whether both requests failed or
567 // XXX whether one has been answered successfully
573 Dprint((statp
->options
& RES_DEBUG
) ||
574 ((statp
->pfcode
& RES_PRF_REPLY
) &&
575 (statp
->pfcode
& RES_PRF_HEAD1
)),
576 (stdout
, ";; got answer:\n"));
578 DprintQ((statp
->options
& RES_DEBUG
) ||
579 (statp
->pfcode
& RES_PRF_REPLY
),
581 ans
, (resplen
> anssiz
) ? anssiz
: resplen
);
583 DprintQ((statp
->options
& RES_DEBUG
) ||
584 (statp
->pfcode
& RES_PRF_REPLY
),
586 *ansp2
, (*resplen2
> *nansp2
) ? *nansp2
: *resplen2
);
589 * If we have temporarily opened a virtual circuit,
590 * or if we haven't been asked to keep a socket open,
593 if ((v_circuit
&& (statp
->options
& RES_USEVC
) == 0) ||
594 (statp
->options
& RES_STAYOPEN
) == 0) {
595 __res_iclose(statp
, false);
598 if (__builtin_expect (statp
->rhook
, 0)) {
599 int done
= 0, loops
= 0;
604 act
= (*statp
->rhook
)((struct sockaddr_in
*)
606 ans
, anssiz
, &resplen
);
613 __res_iclose(statp
, false);
616 /* give the hook another try */
617 if (++loops
< 42) /*doug adams*/
633 __res_iclose(statp
, false);
636 __set_errno (ECONNREFUSED
); /* no nameservers found */
638 __set_errno (ETIMEDOUT
); /* no answer obtained */
640 __set_errno (terrno
);
645 res_nsend(res_state statp
,
646 const u_char
*buf
, int buflen
, u_char
*ans
, int anssiz
)
648 return __libc_res_nsend(statp
, buf
, buflen
, NULL
, 0, ans
, anssiz
,
649 NULL
, NULL
, NULL
, NULL
);
651 libresolv_hidden_def (res_nsend
)
656 send_vc(res_state statp
,
657 const u_char
*buf
, int buflen
, const u_char
*buf2
, int buflen2
,
658 u_char
**ansp
, int *anssizp
,
659 int *terrno
, int ns
, u_char
**anscp
, u_char
**ansp2
, int *anssizp2
,
662 const HEADER
*hp
= (HEADER
*) buf
;
663 const HEADER
*hp2
= (HEADER
*) buf2
;
665 int orig_anssizp
= *anssizp
;
667 // int anssiz = *anssizp;
668 HEADER
*anhp
= (HEADER
*) ans
;
669 struct sockaddr_in6
*nsap
= EXT(statp
).nsaddrs
[ns
];
670 int truncating
, connreset
, resplen
, n
;
676 if (resplen2
!= NULL
)
682 /* Are we still talking to whom we want to talk to? */
683 if (statp
->_vcsock
>= 0 && (statp
->_flags
& RES_F_VC
) != 0) {
684 struct sockaddr_in6 peer
;
685 socklen_t size
= sizeof peer
;
687 if (getpeername(statp
->_vcsock
,
688 (struct sockaddr
*)&peer
, &size
) < 0 ||
689 !sock_eq(&peer
, nsap
)) {
690 __res_iclose(statp
, false);
691 statp
->_flags
&= ~RES_F_VC
;
695 if (statp
->_vcsock
< 0 || (statp
->_flags
& RES_F_VC
) == 0) {
696 if (statp
->_vcsock
>= 0)
697 __res_iclose(statp
, false);
699 statp
->_vcsock
= socket(nsap
->sin6_family
, SOCK_STREAM
, 0);
700 if (statp
->_vcsock
< 0) {
702 Perror(statp
, stderr
, "socket(vc)", errno
);
706 if (connect(statp
->_vcsock
, (struct sockaddr
*)nsap
,
707 nsap
->sin6_family
== AF_INET
708 ? sizeof (struct sockaddr_in
)
709 : sizeof (struct sockaddr_in6
)) < 0) {
711 Aerror(statp
, stderr
, "connect/vc", errno
,
712 (struct sockaddr
*) nsap
);
713 __res_iclose(statp
, false);
716 statp
->_flags
|= RES_F_VC
;
720 * Send length & message
722 len
= htons ((u_short
) buflen
);
723 evConsIovec(&len
, INT16SZ
, &iov
[0]);
724 evConsIovec((void*)buf
, buflen
, &iov
[1]);
726 ssize_t explen
= INT16SZ
+ buflen
;
728 len2
= htons ((u_short
) buflen2
);
729 evConsIovec(&len2
, INT16SZ
, &iov
[2]);
730 evConsIovec((void*)buf2
, buflen2
, &iov
[3]);
732 explen
+= INT16SZ
+ buflen2
;
734 if (TEMP_FAILURE_RETRY (writev(statp
->_vcsock
, iov
, niov
)) != explen
) {
736 Perror(statp
, stderr
, "write failed", errno
);
737 __res_iclose(statp
, false);
741 * Receive length & response
744 int recvresp2
= buf2
== NULL
;
747 cp
= (u_char
*)&rlen16
;
748 len
= sizeof(rlen16
);
749 while ((n
= TEMP_FAILURE_RETRY (read(statp
->_vcsock
, cp
,
757 Perror(statp
, stderr
, "read failed", errno
);
758 __res_iclose(statp
, false);
760 * A long running process might get its TCP
761 * connection reset if the remote server was
762 * restarted. Requery the server instead of
763 * trying a new one. When there is only one
764 * server, this means that a query might work
765 * instead of failing. We only allow one reset
766 * per query to prevent looping.
768 if (*terrno
== ECONNRESET
&& !connreset
) {
774 int rlen
= ntohs (rlen16
);
779 if ((recvresp1
| recvresp2
) == 0 || buf2
== NULL
) {
780 thisanssizp
= anssizp
;
781 thisansp
= anscp
?: ansp
;
782 assert (anscp
!= NULL
|| ansp2
== NULL
);
783 thisresplenp
= &resplen
;
785 if (*anssizp
!= MAXPACKET
) {
786 /* No buffer allocated for the first
787 reply. We can try to use the rest
788 of the user-provided buffer. */
789 #ifdef _STRING_ARCH_unaligned
790 *anssizp2
= orig_anssizp
- resplen
;
791 *ansp2
= *ansp
+ resplen
;
794 = ((resplen
+ __alignof__ (HEADER
) - 1)
795 & ~(__alignof__ (HEADER
) - 1));
796 *anssizp2
= orig_anssizp
- aligned_resplen
;
797 *ansp2
= *ansp
+ aligned_resplen
;
800 /* The first reply did not fit into the
801 user-provided buffer. Maybe the second
803 *anssizp2
= orig_anssizp
;
807 thisanssizp
= anssizp2
;
809 thisresplenp
= resplen2
;
811 anhp
= (HEADER
*) *thisansp
;
813 *thisresplenp
= rlen
;
814 if (rlen
> *thisanssizp
) {
815 /* Yes, we test ANSCP here. If we have two buffers
816 both will be allocatable. */
817 if (__builtin_expect (anscp
!= NULL
, 1)) {
818 u_char
*newp
= malloc (MAXPACKET
);
821 __res_iclose(statp
, false);
824 *thisanssizp
= MAXPACKET
;
826 anhp
= (HEADER
*) newp
;
829 Dprint(statp
->options
& RES_DEBUG
,
830 (stdout
, ";; response truncated\n")
838 if (__builtin_expect (len
< HFIXEDSZ
, 0)) {
840 * Undersized message.
842 Dprint(statp
->options
& RES_DEBUG
,
843 (stdout
, ";; undersized: %d\n", len
));
845 __res_iclose(statp
, false);
850 while (len
!= 0 && (n
= read(statp
->_vcsock
, (char *)cp
, (int)len
)) > 0){
854 if (__builtin_expect (n
<= 0, 0)) {
856 Perror(statp
, stderr
, "read(vc)", errno
);
857 __res_iclose(statp
, false);
860 if (__builtin_expect (truncating
, 0)) {
862 * Flush rest of answer so connection stays in synch.
865 len
= rlen
- *thisanssizp
;
869 n
= read(statp
->_vcsock
, junk
,
870 (len
> sizeof junk
) ? sizeof junk
: len
);
878 * If the calling applicating has bailed out of
879 * a previous call and failed to arrange to have
880 * the circuit closed or the server has got
881 * itself confused, then drop the packet and
882 * wait for the correct one.
884 if ((recvresp1
|| hp
->id
!= anhp
->id
)
885 && (recvresp2
|| hp2
->id
!= anhp
->id
)) {
886 DprintQ((statp
->options
& RES_DEBUG
) ||
887 (statp
->pfcode
& RES_PRF_REPLY
),
888 (stdout
, ";; old answer (unexpected):\n"),
890 (rlen
> *thisanssiz
) ? *thisanssiz
: rlen
);
894 /* Mark which reply we received. */
895 if (recvresp1
== 0 && hp
->id
== anhp
->id
)
899 /* Repeat waiting if we have a second answer to arrive. */
900 if ((recvresp1
& recvresp2
) == 0)
904 * All is well, or the error is fatal. Signal that the
905 * next nameserver ought not be tried.
911 send_dg(res_state statp
,
912 const u_char
*buf
, int buflen
, const u_char
*buf2
, int buflen2
,
913 u_char
**ansp
, int *anssizp
,
914 int *terrno
, int ns
, int *v_circuit
, int *gotsomewhere
, u_char
**anscp
,
915 u_char
**ansp2
, int *anssizp2
, int *resplen2
)
917 const HEADER
*hp
= (HEADER
*) buf
;
918 const HEADER
*hp2
= (HEADER
*) buf2
;
920 int orig_anssizp
= *anssizp
;
921 struct sockaddr_in6
*nsap
= EXT(statp
).nsaddrs
[ns
];
922 struct timespec now
, timeout
, finish
;
923 struct pollfd pfd
[1];
925 struct sockaddr_in6 from
;
928 if (EXT(statp
).nssocks
[ns
] == -1) {
929 /* only try IPv6 if IPv6 NS and if not failed before */
930 if ((EXT(statp
).nscount6
> 0) && !statp
->ipv6_unavail
) {
931 if (__builtin_expect (__have_o_nonblock
>= 0, 1)) {
932 EXT(statp
).nssocks
[ns
] =
933 socket(PF_INET6
, SOCK_DGRAM
|SOCK_NONBLOCK
,
935 #ifndef __ASSUME_SOCK_CLOEXEC
936 if (__have_o_nonblock
== 0)
938 = (EXT(statp
).nssocks
[ns
] == -1
939 && errno
== EINVAL
? -1 : 1);
942 if (__builtin_expect (__have_o_nonblock
< 0, 0))
943 EXT(statp
).nssocks
[ns
] =
944 socket(PF_INET6
, SOCK_DGRAM
, 0);
945 if (EXT(statp
).nssocks
[ns
] < 0)
946 statp
->ipv6_unavail
= errno
== EAFNOSUPPORT
;
947 /* If IPv6 socket and nsap is IPv4, make it
949 else if (nsap
->sin6_family
== AF_INET
)
952 if (EXT(statp
).nssocks
[ns
] < 0) {
953 if (__builtin_expect (__have_o_nonblock
>= 0, 1)) {
954 EXT(statp
).nssocks
[ns
]
955 = socket(PF_INET
, SOCK_DGRAM
|SOCK_NONBLOCK
,
957 #ifndef __ASSUME_SOCK_CLOEXEC
958 if (__have_o_nonblock
== 0)
960 = (EXT(statp
).nssocks
[ns
] == -1
961 && errno
== EINVAL
? -1 : 1);
964 if (__builtin_expect (__have_o_nonblock
< 0, 0))
965 EXT(statp
).nssocks
[ns
]
966 = socket(PF_INET
, SOCK_DGRAM
, 0);
968 if (EXT(statp
).nssocks
[ns
] < 0) {
970 Perror(statp
, stderr
, "socket(dg)", errno
);
975 * On a 4.3BSD+ machine (client and server,
976 * actually), sending to a nameserver datagram
977 * port with no nameserver will cause an
978 * ICMP port unreachable message to be returned.
979 * If our datagram socket is "connected" to the
980 * server, we get an ECONNREFUSED error on the next
981 * socket operation, and select returns if the
982 * error message is received. We can thus detect
983 * the absence of a nameserver without timing out.
985 if (connect(EXT(statp
).nssocks
[ns
], (struct sockaddr
*)nsap
,
987 Aerror(statp
, stderr
, "connect(dg)", errno
,
988 (struct sockaddr
*) nsap
);
989 __res_iclose(statp
, false);
992 if (__builtin_expect (__have_o_nonblock
< 0, 0)) {
993 /* Make socket non-blocking. */
994 int fl
= __fcntl (EXT(statp
).nssocks
[ns
], F_GETFL
);
996 __fcntl (EXT(statp
).nssocks
[ns
], F_SETFL
,
998 Dprint(statp
->options
& RES_DEBUG
,
999 (stdout
, ";; new DG socket\n"))
1004 * Compute time for the total operation.
1006 int seconds
= (statp
->retrans
<< ns
);
1008 seconds
/= statp
->nscount
;
1011 bool single_request
= ((statp
->options
) & RES_SNGLKUP
) != 0;// XXX
1012 int save_gotsomewhere
= *gotsomewhere
;
1015 evConsTime(&timeout
, seconds
, 0);
1016 evAddTime(&finish
, &now
, &timeout
);
1017 int need_recompute
= 0;
1020 int recvresp2
= buf2
== NULL
;
1021 pfd
[0].fd
= EXT(statp
).nssocks
[ns
];
1022 pfd
[0].events
= POLLOUT
;
1023 if (resplen2
!= NULL
)
1026 if (need_recompute
) {
1029 if (evCmpTime(finish
, now
) <= 0) {
1031 Perror(statp
, stderr
, "poll", errno
);
1033 __res_iclose(statp
, false);
1036 evSubTime(&timeout
, &finish
, &now
);
1039 /* Convert struct timespec in milliseconds. */
1040 ptimeout
= timeout
.tv_sec
* 1000 + timeout
.tv_nsec
/ 1000000;
1044 n
= __poll (pfd
, 1, 0);
1045 if (__builtin_expect (n
== 0, 0)) {
1046 n
= __poll (pfd
, 1, ptimeout
);
1050 Dprint(statp
->options
& RES_DEBUG
, (stdout
, ";; timeout\n"));
1051 if (resplen
> 1 && (recvresp1
|| (buf2
!= NULL
&& recvresp2
)))
1053 /* There are quite a few broken name servers out
1054 there which don't handle two outstanding
1055 requests from the same source. There are also
1056 broken firewall settings. If we time out after
1057 having received one answer switch to the mode
1058 where we send the second request only once we
1059 have received the first answer. */
1060 if (!single_request
)
1062 single_request
= true;
1063 *gotsomewhere
= save_gotsomewhere
;
1076 goto recompute_resend
;
1081 if (pfd
[0].revents
& POLLOUT
) {
1084 sr
= send (pfd
[0].fd
, buf2
, buflen2
, MSG_NOSIGNAL
);
1086 sr
= send (pfd
[0].fd
, buf
, buflen
, MSG_NOSIGNAL
);
1089 if (errno
== EINTR
|| errno
== EAGAIN
)
1090 goto recompute_resend
;
1091 Perror(statp
, stderr
, "send", errno
);
1094 if (nwritten
!= 0 || buf2
== NULL
|| single_request
)
1095 pfd
[0].events
= POLLIN
;
1097 pfd
[0].events
= POLLIN
| POLLOUT
;
1100 } else if (pfd
[0].revents
& POLLIN
) {
1105 if ((recvresp1
| recvresp2
) == 0 || buf2
== NULL
) {
1106 thisanssizp
= anssizp
;
1107 thisansp
= anscp
?: ansp
;
1108 assert (anscp
!= NULL
|| ansp2
== NULL
);
1109 thisresplenp
= &resplen
;
1111 if (*anssizp
!= MAXPACKET
) {
1112 /* No buffer allocated for the first
1113 reply. We can try to use the rest
1114 of the user-provided buffer. */
1115 #ifdef _STRING_ARCH_unaligned
1116 *anssizp2
= orig_anssizp
- resplen
;
1117 *ansp2
= *ansp
+ resplen
;
1120 = ((resplen
+ __alignof__ (HEADER
) - 1)
1121 & ~(__alignof__ (HEADER
) - 1));
1122 *anssizp2
= orig_anssizp
- aligned_resplen
;
1123 *ansp2
= *ansp
+ aligned_resplen
;
1126 /* The first reply did not fit into the
1127 user-provided buffer. Maybe the second
1129 *anssizp2
= orig_anssizp
;
1133 thisanssizp
= anssizp2
;
1135 thisresplenp
= resplen2
;
1138 if (*thisanssizp
< MAXPACKET
1139 /* Yes, we test ANSCP here. If we have two buffers
1140 both will be allocatable. */
1142 && (ioctl (pfd
[0].fd
, FIONREAD
, thisresplenp
) < 0
1143 || *thisanssizp
< *thisresplenp
)) {
1144 u_char
*newp
= malloc (MAXPACKET
);
1146 *anssizp
= MAXPACKET
;
1147 *thisansp
= ans
= newp
;
1150 HEADER
*anhp
= (HEADER
*) *thisansp
;
1151 socklen_t fromlen
= sizeof(struct sockaddr_in6
);
1152 assert (sizeof(from
) <= fromlen
);
1153 *thisresplenp
= recvfrom(pfd
[0].fd
, (char*)*thisansp
,
1155 (struct sockaddr
*)&from
, &fromlen
);
1156 if (__builtin_expect (*thisresplenp
<= 0, 0)) {
1157 if (errno
== EINTR
|| errno
== EAGAIN
) {
1161 Perror(statp
, stderr
, "recvfrom", errno
);
1165 if (__builtin_expect (*thisresplenp
< HFIXEDSZ
, 0)) {
1167 * Undersized message.
1169 Dprint(statp
->options
& RES_DEBUG
,
1170 (stdout
, ";; undersized: %d\n",
1175 if ((recvresp1
|| hp
->id
!= anhp
->id
)
1176 && (recvresp2
|| hp2
->id
!= anhp
->id
)) {
1178 * response from old query, ignore it.
1179 * XXX - potential security hazard could
1182 DprintQ((statp
->options
& RES_DEBUG
) ||
1183 (statp
->pfcode
& RES_PRF_REPLY
),
1184 (stdout
, ";; old answer:\n"),
1186 (*thisresplen
> *thisanssiz
)
1187 ? *thisanssiz
: *thisresplen
);
1190 if (!(statp
->options
& RES_INSECURE1
) &&
1191 !res_ourserver_p(statp
, &from
)) {
1193 * response from wrong server? ignore it.
1194 * XXX - potential security hazard could
1197 DprintQ((statp
->options
& RES_DEBUG
) ||
1198 (statp
->pfcode
& RES_PRF_REPLY
),
1199 (stdout
, ";; not our server:\n"),
1201 (*thisresplen
> *thisanssiz
)
1202 ? *thisanssiz
: *thisresplen
);
1205 #ifdef RES_USE_EDNS0
1206 if (anhp
->rcode
== FORMERR
1207 && (statp
->options
& RES_USE_EDNS0
) != 0U) {
1209 * Do not retry if the server does not understand
1210 * EDNS0. The case has to be captured here, as
1211 * FORMERR packet do not carry query section, hence
1212 * res_queriesmatch() returns 0.
1214 DprintQ(statp
->options
& RES_DEBUG
,
1216 "server rejected query with EDNS0:\n"),
1218 (*thisresplen
> *thisanssiz
)
1219 ? *thisanssiz
: *thisresplen
);
1220 /* record the error */
1221 statp
->_flags
|= RES_F_EDNS0ERR
;
1225 if (!(statp
->options
& RES_INSECURE2
)
1226 && (recvresp1
|| !res_queriesmatch(buf
, buf
+ buflen
,
1230 && (recvresp2
|| !res_queriesmatch(buf2
, buf2
+ buflen2
,
1235 * response contains wrong query? ignore it.
1236 * XXX - potential security hazard could
1239 DprintQ((statp
->options
& RES_DEBUG
) ||
1240 (statp
->pfcode
& RES_PRF_REPLY
),
1241 (stdout
, ";; wrong query name:\n"),
1243 (*thisresplen
> *thisanssiz
)
1244 ? *thisanssiz
: *thisresplen
);
1247 if (anhp
->rcode
== SERVFAIL
||
1248 anhp
->rcode
== NOTIMP
||
1249 anhp
->rcode
== REFUSED
) {
1250 DprintQ(statp
->options
& RES_DEBUG
,
1251 (stdout
, "server rejected query:\n"),
1253 (*thisresplen
> *thisanssiz
)
1254 ? *thisanssiz
: *thisresplen
);
1256 if (recvresp1
|| (buf2
!= NULL
&& recvresp2
))
1263 /* We are waiting for a possible second reply. */
1265 if (hp
->id
== anhp
->id
)
1274 __res_iclose(statp
, false);
1275 /* don't retry if called from dig */
1279 if (anhp
->rcode
== NOERROR
&& anhp
->ancount
== 0
1280 && anhp
->aa
== 0 && anhp
->ra
== 0 && anhp
->arcount
== 0) {
1281 DprintQ(statp
->options
& RES_DEBUG
,
1282 (stdout
, "referred query:\n"),
1284 (*thisresplen
> *thisanssiz
)
1285 ? *thisanssiz
: *thisresplen
);
1288 if (!(statp
->options
& RES_IGNTC
) && anhp
->tc
) {
1290 * To get the rest of answer,
1291 * use TCP with same server.
1293 Dprint(statp
->options
& RES_DEBUG
,
1294 (stdout
, ";; truncated answer\n"));
1296 __res_iclose(statp
, false);
1297 // XXX if we have received one reply we could
1298 // XXX use it and not repeat it over TCP...
1301 /* Mark which reply we received. */
1302 if (recvresp1
== 0 && hp
->id
== anhp
->id
)
1306 /* Repeat waiting if we have a second answer to arrive. */
1307 if ((recvresp1
& recvresp2
) == 0) {
1309 pfd
[0].events
= POLLOUT
;
1313 * All is well, or the error is fatal. Signal that the
1314 * next nameserver ought not be tried.
1317 } else if (pfd
[0].revents
& (POLLERR
| POLLHUP
| POLLNVAL
)) {
1318 /* Something went wrong. We can stop trying. */
1322 /* poll should not have returned > 0 in this case. */
1329 Aerror(const res_state statp
, FILE *file
, const char *string
, int error
,
1330 const struct sockaddr
*address
)
1334 if ((statp
->options
& RES_DEBUG
) != 0) {
1335 char tmp
[sizeof "xxxx.xxxx.xxxx.255.255.255.255"];
1337 fprintf(file
, "res_send: %s ([%s].%u): %s\n",
1339 (address
->sa_family
== AF_INET
1340 ? inet_ntop(address
->sa_family
,
1341 &((const struct sockaddr_in
*) address
)->sin_addr
,
1343 : inet_ntop(address
->sa_family
,
1344 &((const struct sockaddr_in6
*) address
)->sin6_addr
,
1346 (address
->sa_family
== AF_INET
1347 ? ntohs(((struct sockaddr_in
*) address
)->sin_port
)
1348 : address
->sa_family
== AF_INET6
1349 ? ntohs(((struct sockaddr_in6
*) address
)->sin6_port
)
1357 Perror(const res_state statp
, FILE *file
, const char *string
, int error
) {
1360 if ((statp
->options
& RES_DEBUG
) != 0)
1361 fprintf(file
, "res_send: %s: %s\n",
1362 string
, strerror(error
));
1368 sock_eq(struct sockaddr_in6
*a1
, struct sockaddr_in6
*a2
) {
1369 if (a1
->sin6_family
== a2
->sin6_family
) {
1370 if (a1
->sin6_family
== AF_INET
)
1371 return ((((struct sockaddr_in
*)a1
)->sin_port
==
1372 ((struct sockaddr_in
*)a2
)->sin_port
) &&
1373 (((struct sockaddr_in
*)a1
)->sin_addr
.s_addr
==
1374 ((struct sockaddr_in
*)a2
)->sin_addr
.s_addr
));
1376 return ((a1
->sin6_port
== a2
->sin6_port
) &&
1377 !memcmp(&a1
->sin6_addr
, &a2
->sin6_addr
,
1378 sizeof (struct in6_addr
)));
1380 if (a1
->sin6_family
== AF_INET
) {
1381 struct sockaddr_in6
*sap
= a1
;
1384 } /* assumes that AF_INET and AF_INET6 are the only possibilities */
1385 return ((a1
->sin6_port
== ((struct sockaddr_in
*)a2
)->sin_port
) &&
1386 IN6_IS_ADDR_V4MAPPED(&a1
->sin6_addr
) &&
1387 (a1
->sin6_addr
.s6_addr32
[3] ==
1388 ((struct sockaddr_in
*)a2
)->sin_addr
.s_addr
));
1392 * Converts IPv4 family, address and port to
1393 * IPv6 family, IPv4-mapped IPv6 address and port.
1396 convaddr4to6(struct sockaddr_in6
*sa
)
1398 struct sockaddr_in
*sa4p
= (struct sockaddr_in
*) sa
;
1399 in_port_t port
= sa4p
->sin_port
;
1400 in_addr_t addr
= sa4p
->sin_addr
.s_addr
;
1402 sa
->sin6_family
= AF_INET6
;
1403 sa
->sin6_port
= port
;
1404 sa
->sin6_addr
.s6_addr32
[0] = 0;
1405 sa
->sin6_addr
.s6_addr32
[1] = 0;
1406 sa
->sin6_addr
.s6_addr32
[2] = htonl(0xFFFF);
1407 sa
->sin6_addr
.s6_addr32
[3] = addr
;