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>
100 #define MAXPACKET PACKETSZ
102 #define MAXPACKET 65536
106 /* From ev_streams.c. */
109 __attribute ((always_inline
))
110 evConsIovec(void *buf
, size_t cnt
, struct iovec
*vec
) {
111 memset(vec
, 0xf5, sizeof (*vec
));
116 /* From ev_timers.c. */
118 #define BILLION 1000000000
121 evConsTime(struct timespec
*res
, time_t sec
, long nsec
) {
127 evAddTime(struct timespec
*res
, const struct timespec
*addend1
,
128 const struct timespec
*addend2
) {
129 res
->tv_sec
= addend1
->tv_sec
+ addend2
->tv_sec
;
130 res
->tv_nsec
= addend1
->tv_nsec
+ addend2
->tv_nsec
;
131 if (res
->tv_nsec
>= BILLION
) {
133 res
->tv_nsec
-= BILLION
;
138 evSubTime(struct timespec
*res
, const struct timespec
*minuend
,
139 const struct timespec
*subtrahend
) {
140 res
->tv_sec
= minuend
->tv_sec
- subtrahend
->tv_sec
;
141 if (minuend
->tv_nsec
>= subtrahend
->tv_nsec
)
142 res
->tv_nsec
= minuend
->tv_nsec
- subtrahend
->tv_nsec
;
144 res
->tv_nsec
= (BILLION
145 - subtrahend
->tv_nsec
+ minuend
->tv_nsec
);
151 evCmpTime(struct timespec a
, struct timespec b
) {
152 long x
= a
.tv_sec
- b
.tv_sec
;
155 x
= a
.tv_nsec
- b
.tv_nsec
;
156 return (x
< 0L ? (-1) : x
> 0L ? (1) : (0));
160 evNowTime(struct timespec
*res
) {
163 if (gettimeofday(&now
, NULL
) < 0)
164 evConsTime(res
, 0, 0);
166 TIMEVAL_TO_TIMESPEC (&now
, res
);
170 /* Options. Leave them on. */
172 #include "res_debug.h"
174 #define EXT(res) ((res)->_u._ext)
178 static int send_vc(res_state
, const u_char
*, int,
179 u_char
**, int *, int *, int, u_char
**);
180 static int send_dg(res_state
, const u_char
*, int,
181 u_char
**, int *, int *, int,
182 int *, int *, u_char
**);
184 static void Aerror(const res_state
, FILE *, const char *, int,
185 const struct sockaddr
*);
186 static void Perror(const res_state
, FILE *, const char *, int);
188 static int sock_eq(struct sockaddr_in6
*, struct sockaddr_in6
*);
192 static void convaddr4to6(struct sockaddr_in6
*sa
);
193 void res_pquery(const res_state
, const u_char
*, int, FILE *);
198 * res_isourserver(ina)
199 * looks up "ina" in _res.ns_addr_list[]
204 * paul vixie, 29may94
207 res_ourserver_p(const res_state statp
, const struct sockaddr_in6
*inp
)
211 if (inp
->sin6_family
== AF_INET
) {
212 struct sockaddr_in
*in4p
= (struct sockaddr_in
*) inp
;
213 in_port_t port
= in4p
->sin_port
;
214 in_addr_t addr
= in4p
->sin_addr
.s_addr
;
216 for (ns
= 0; ns
< MAXNS
; ns
++) {
217 const struct sockaddr_in
*srv
=
218 (struct sockaddr_in
*)EXT(statp
).nsaddrs
[ns
];
220 if ((srv
!= NULL
) && (srv
->sin_family
== AF_INET
) &&
221 (srv
->sin_port
== port
) &&
222 (srv
->sin_addr
.s_addr
== INADDR_ANY
||
223 srv
->sin_addr
.s_addr
== addr
))
226 } else if (inp
->sin6_family
== AF_INET6
) {
227 for (ns
= 0; ns
< MAXNS
; ns
++) {
228 const struct sockaddr_in6
*srv
= EXT(statp
).nsaddrs
[ns
];
229 if ((srv
!= NULL
) && (srv
->sin6_family
== AF_INET6
) &&
230 (srv
->sin6_port
== inp
->sin6_port
) &&
231 !(memcmp(&srv
->sin6_addr
, &in6addr_any
,
232 sizeof (struct in6_addr
)) &&
233 memcmp(&srv
->sin6_addr
, &inp
->sin6_addr
,
234 sizeof (struct in6_addr
))))
242 * res_nameinquery(name, type, class, buf, eom)
243 * look for (name,type,class) in the query section of packet (buf,eom)
245 * buf + HFIXEDSZ <= eom
251 * paul vixie, 29may94
254 res_nameinquery(const char *name
, int type
, int class,
255 const u_char
*buf
, const u_char
*eom
)
257 const u_char
*cp
= buf
+ HFIXEDSZ
;
258 int qdcount
= ntohs(((HEADER
*)buf
)->qdcount
);
260 while (qdcount
-- > 0) {
261 char tname
[MAXDNAME
+1];
262 int n
, ttype
, tclass
;
264 n
= dn_expand(buf
, eom
, cp
, tname
, sizeof tname
);
268 if (cp
+ 2 * INT16SZ
> eom
)
270 ttype
= ns_get16(cp
); cp
+= INT16SZ
;
271 tclass
= ns_get16(cp
); cp
+= INT16SZ
;
272 if (ttype
== type
&& tclass
== class &&
273 ns_samename(tname
, name
) == 1)
278 libresolv_hidden_def (res_nameinquery
)
281 * res_queriesmatch(buf1, eom1, buf2, eom2)
282 * is there a 1:1 mapping of (name,type,class)
283 * in (buf1,eom1) and (buf2,eom2)?
286 * 0 : not a 1:1 mapping
287 * >0 : is a 1:1 mapping
289 * paul vixie, 29may94
292 res_queriesmatch(const u_char
*buf1
, const u_char
*eom1
,
293 const u_char
*buf2
, const u_char
*eom2
)
295 const u_char
*cp
= buf1
+ HFIXEDSZ
;
296 int qdcount
= ntohs(((HEADER
*)buf1
)->qdcount
);
298 if (buf1
+ HFIXEDSZ
> eom1
|| buf2
+ HFIXEDSZ
> eom2
)
302 * Only header section present in replies to
303 * dynamic update packets.
305 if ((((HEADER
*)buf1
)->opcode
== ns_o_update
) &&
306 (((HEADER
*)buf2
)->opcode
== ns_o_update
))
309 if (qdcount
!= ntohs(((HEADER
*)buf2
)->qdcount
))
311 while (qdcount
-- > 0) {
312 char tname
[MAXDNAME
+1];
313 int n
, ttype
, tclass
;
315 n
= dn_expand(buf1
, eom1
, cp
, tname
, sizeof tname
);
319 if (cp
+ 2 * INT16SZ
> eom1
)
321 ttype
= ns_get16(cp
); cp
+= INT16SZ
;
322 tclass
= ns_get16(cp
); cp
+= INT16SZ
;
323 if (!res_nameinquery(tname
, ttype
, tclass
, buf2
, eom2
))
328 libresolv_hidden_def (res_queriesmatch
)
331 __libc_res_nsend(res_state statp
, const u_char
*buf
, int buflen
,
332 u_char
*ans
, int anssiz
, u_char
**ansp
)
334 int gotsomewhere
, terrno
, try, v_circuit
, resplen
, ns
, n
;
336 if (statp
->nscount
== 0) {
341 if (anssiz
< HFIXEDSZ
) {
342 __set_errno (EINVAL
);
346 if ((statp
->qhook
|| statp
->rhook
) && anssiz
< MAXPACKET
&& ansp
) {
347 u_char
*buf
= malloc (MAXPACKET
);
350 memcpy (buf
, ans
, HFIXEDSZ
);
356 DprintQ((statp
->options
& RES_DEBUG
) || (statp
->pfcode
& RES_PRF_QUERY
),
357 (stdout
, ";; res_send()\n"), buf
, buflen
);
358 v_circuit
= (statp
->options
& RES_USEVC
) || buflen
> PACKETSZ
;
363 * If the ns_addr_list in the resolver context has changed, then
364 * invalidate our cached copy and the associated timing data.
366 if (EXT(statp
).nsinit
) {
369 if (EXT(statp
).nscount
!= statp
->nscount
)
372 for (ns
= 0; ns
< MAXNS
; ns
++) {
373 unsigned int map
= EXT(statp
).nsmap
[ns
];
375 && !sock_eq((struct sockaddr_in6
*)
376 &statp
->nsaddr_list
[map
],
377 EXT(statp
).nsaddrs
[ns
]))
388 * Maybe initialize our private copy of the ns_addr_list.
390 if (EXT(statp
).nsinit
== 0) {
391 unsigned char map
[MAXNS
];
393 memset (map
, MAXNS
, sizeof (map
));
394 for (n
= 0; n
< MAXNS
; n
++) {
395 ns
= EXT(statp
).nsmap
[n
];
396 if (ns
< statp
->nscount
)
398 else if (ns
< MAXNS
) {
399 free(EXT(statp
).nsaddrs
[n
]);
400 EXT(statp
).nsaddrs
[n
] = NULL
;
401 EXT(statp
).nsmap
[n
] = MAXNS
;
405 if (statp
->nscount
> EXT(statp
).nscount
)
406 for (n
= EXT(statp
).nscount
, ns
= 0;
407 n
< statp
->nscount
; n
++) {
409 && EXT(statp
).nsmap
[ns
] != MAXNS
)
413 EXT(statp
).nsmap
[ns
] = n
;
416 EXT(statp
).nscount
= n
;
417 for (ns
= 0; ns
< EXT(statp
).nscount
; ns
++) {
419 if (EXT(statp
).nsaddrs
[n
] == NULL
)
420 EXT(statp
).nsaddrs
[n
] =
421 malloc(sizeof (struct sockaddr_in6
));
422 if (EXT(statp
).nsaddrs
[n
] != NULL
) {
423 memcpy(EXT(statp
).nsaddrs
[n
],
424 &statp
->nsaddr_list
[ns
],
425 sizeof (struct sockaddr_in
));
426 EXT(statp
).nssocks
[n
] = -1;
430 EXT(statp
).nsinit
= 1;
434 * Some resolvers want to even out the load on their nameservers.
435 * Note that RES_BLAST overrides RES_ROTATE.
437 if ((statp
->options
& RES_ROTATE
) != 0 &&
438 (statp
->options
& RES_BLAST
) == 0) {
439 struct sockaddr_in6
*ina
;
443 while (n
< MAXNS
&& EXT(statp
).nsmap
[n
] == MAXNS
)
446 ina
= EXT(statp
).nsaddrs
[n
];
447 map
= EXT(statp
).nsmap
[n
];
451 && EXT(statp
).nsmap
[ns
] == MAXNS
)
455 EXT(statp
).nsaddrs
[n
] = EXT(statp
).nsaddrs
[ns
];
456 EXT(statp
).nsmap
[n
] = EXT(statp
).nsmap
[ns
];
459 EXT(statp
).nsaddrs
[n
] = ina
;
460 EXT(statp
).nsmap
[n
] = map
;
465 * Send request, RETRY times, or until successful.
467 for (try = 0; try < statp
->retry
; try++) {
468 for (ns
= 0; ns
< MAXNS
; ns
++)
470 struct sockaddr_in6
*nsap
= EXT(statp
).nsaddrs
[ns
];
476 int done
= 0, loops
= 0;
481 struct sockaddr_in
*nsap4
;
482 nsap4
= (struct sockaddr_in
*) nsap
;
483 act
= (*statp
->qhook
)(&nsap4
, &buf
, &buflen
,
484 ans
, anssiz
, &resplen
);
485 nsap
= (struct sockaddr_in6
*) nsap4
;
496 /* give the hook another try */
497 if (++loops
< 42) /*doug adams*/
511 Dprint(statp
->options
& RES_DEBUG
,
512 (stdout
, ";; Querying server (# %d) address = %s\n",
513 ns
+ 1, inet_ntop(AF_INET6
, &nsap
->sin6_addr
,
514 tmpbuf
, sizeof (tmpbuf
))));
517 /* Use VC; at most one attempt per server. */
519 n
= send_vc(statp
, buf
, buflen
, &ans
, &anssiz
, &terrno
,
528 n
= send_dg(statp
, buf
, buflen
, &ans
, &anssiz
, &terrno
,
529 ns
, &v_circuit
, &gotsomewhere
, ansp
);
539 Dprint((statp
->options
& RES_DEBUG
) ||
540 ((statp
->pfcode
& RES_PRF_REPLY
) &&
541 (statp
->pfcode
& RES_PRF_HEAD1
)),
542 (stdout
, ";; got answer:\n"));
544 DprintQ((statp
->options
& RES_DEBUG
) ||
545 (statp
->pfcode
& RES_PRF_REPLY
),
547 ans
, (resplen
> anssiz
) ? anssiz
: resplen
);
550 * If we have temporarily opened a virtual circuit,
551 * or if we haven't been asked to keep a socket open,
554 if ((v_circuit
&& (statp
->options
& RES_USEVC
) == 0) ||
555 (statp
->options
& RES_STAYOPEN
) == 0) {
559 int done
= 0, loops
= 0;
564 act
= (*statp
->rhook
)((struct sockaddr_in
*)
566 ans
, anssiz
, &resplen
);
576 /* give the hook another try */
577 if (++loops
< 42) /*doug adams*/
595 __set_errno (ECONNREFUSED
); /* no nameservers found */
597 __set_errno (ETIMEDOUT
); /* no answer obtained */
599 __set_errno (terrno
);
604 res_nsend(res_state statp
,
605 const u_char
*buf
, int buflen
, u_char
*ans
, int anssiz
)
607 return __libc_res_nsend(statp
, buf
, buflen
, ans
, anssiz
, NULL
);
609 libresolv_hidden_def (res_nsend
)
614 send_vc(res_state statp
,
615 const u_char
*buf
, int buflen
, u_char
**ansp
, int *anssizp
,
616 int *terrno
, int ns
, u_char
**anscp
)
618 const HEADER
*hp
= (HEADER
*) buf
;
620 int anssiz
= *anssizp
;
621 HEADER
*anhp
= (HEADER
*) ans
;
622 struct sockaddr_in6
*nsap
= EXT(statp
).nsaddrs
[ns
];
623 int truncating
, connreset
, resplen
, n
;
632 /* Are we still talking to whom we want to talk to? */
633 if (statp
->_vcsock
>= 0 && (statp
->_flags
& RES_F_VC
) != 0) {
634 struct sockaddr_in6 peer
;
635 socklen_t size
= sizeof peer
;
637 if (getpeername(statp
->_vcsock
,
638 (struct sockaddr
*)&peer
, &size
) < 0 ||
639 !sock_eq(&peer
, nsap
)) {
641 statp
->_flags
&= ~RES_F_VC
;
645 if (statp
->_vcsock
< 0 || (statp
->_flags
& RES_F_VC
) == 0) {
646 if (statp
->_vcsock
>= 0)
649 statp
->_vcsock
= socket(nsap
->sin6_family
, SOCK_STREAM
, 0);
650 if (statp
->_vcsock
< 0) {
652 Perror(statp
, stderr
, "socket(vc)", errno
);
656 if (connect(statp
->_vcsock
, (struct sockaddr
*)nsap
,
657 nsap
->sin6_family
== AF_INET
658 ? sizeof (struct sockaddr_in
)
659 : sizeof (struct sockaddr_in6
)) < 0) {
661 Aerror(statp
, stderr
, "connect/vc", errno
,
662 (struct sockaddr
*) nsap
);
666 statp
->_flags
|= RES_F_VC
;
670 * Send length & message
672 putshort((u_short
)buflen
, (u_char
*)&len
);
673 evConsIovec(&len
, INT16SZ
, &iov
[0]);
674 evConsIovec((void*)buf
, buflen
, &iov
[1]);
675 if (TEMP_FAILURE_RETRY (writev(statp
->_vcsock
, iov
, 2))
676 != (INT16SZ
+ buflen
)) {
678 Perror(statp
, stderr
, "write failed", errno
);
683 * Receive length & response
688 while ((n
= TEMP_FAILURE_RETRY (read(statp
->_vcsock
, (char *)cp
,
696 Perror(statp
, stderr
, "read failed", errno
);
699 * A long running process might get its TCP
700 * connection reset if the remote server was
701 * restarted. Requery the server instead of
702 * trying a new one. When there is only one
703 * server, this means that a query might work
704 * instead of failing. We only allow one reset
705 * per query to prevent looping.
707 if (*terrno
== ECONNRESET
&& !connreset
) {
715 resplen
= ns_get16(ans
);
716 if (resplen
> anssiz
) {
718 ans
= malloc (MAXPACKET
);
725 *anssizp
= MAXPACKET
;
728 anhp
= (HEADER
*) ans
;
731 Dprint(statp
->options
& RES_DEBUG
,
732 (stdout
, ";; response truncated\n")
739 if (len
< HFIXEDSZ
) {
741 * Undersized message.
743 Dprint(statp
->options
& RES_DEBUG
,
744 (stdout
, ";; undersized: %d\n", len
));
750 while (len
!= 0 && (n
= read(statp
->_vcsock
, (char *)cp
, (int)len
)) > 0){
756 Perror(statp
, stderr
, "read(vc)", errno
);
762 * Flush rest of answer so connection stays in synch.
765 len
= resplen
- anssiz
;
769 n
= read(statp
->_vcsock
, junk
,
770 (len
> sizeof junk
) ? sizeof junk
: len
);
778 * If the calling applicating has bailed out of
779 * a previous call and failed to arrange to have
780 * the circuit closed or the server has got
781 * itself confused, then drop the packet and
782 * wait for the correct one.
784 if (hp
->id
!= anhp
->id
) {
785 DprintQ((statp
->options
& RES_DEBUG
) ||
786 (statp
->pfcode
& RES_PRF_REPLY
),
787 (stdout
, ";; old answer (unexpected):\n"),
788 ans
, (resplen
> anssiz
) ? anssiz
: resplen
);
793 * All is well, or the error is fatal. Signal that the
794 * next nameserver ought not be tried.
800 send_dg(res_state statp
,
801 const u_char
*buf
, int buflen
, u_char
**ansp
, int *anssizp
,
802 int *terrno
, int ns
, int *v_circuit
, int *gotsomewhere
, u_char
**anscp
)
804 const HEADER
*hp
= (HEADER
*) buf
;
806 int anssiz
= *anssizp
;
807 HEADER
*anhp
= (HEADER
*) ans
;
808 struct sockaddr_in6
*nsap
= EXT(statp
).nsaddrs
[ns
];
809 struct timespec now
, timeout
, finish
;
810 struct pollfd pfd
[1];
812 struct sockaddr_in6 from
;
813 static int socket_pf
= 0;
815 int resplen
, seconds
, n
;
817 if (EXT(statp
).nssocks
[ns
] == -1) {
818 /* only try IPv6 if IPv6 NS and if not failed before */
819 if ((EXT(statp
).nscount6
> 0) && (socket_pf
!= PF_INET
)) {
820 EXT(statp
).nssocks
[ns
] =
821 socket(PF_INET6
, SOCK_DGRAM
, 0);
822 socket_pf
= EXT(statp
).nssocks
[ns
] < 0 ? PF_INET
825 if (EXT(statp
).nssocks
[ns
] < 0)
826 EXT(statp
).nssocks
[ns
] = socket(PF_INET
, SOCK_DGRAM
, 0);
827 if (EXT(statp
).nssocks
[ns
] < 0) {
829 Perror(statp
, stderr
, "socket(dg)", errno
);
832 /* If IPv6 socket and nsap is IPv4, make it IPv4-mapped */
833 if ((socket_pf
== PF_INET6
) && (nsap
->sin6_family
== AF_INET
))
836 * On a 4.3BSD+ machine (client and server,
837 * actually), sending to a nameserver datagram
838 * port with no nameserver will cause an
839 * ICMP port unreachable message to be returned.
840 * If our datagram socket is "connected" to the
841 * server, we get an ECONNREFUSED error on the next
842 * socket operation, and select returns if the
843 * error message is received. We can thus detect
844 * the absence of a nameserver without timing out.
846 if (connect(EXT(statp
).nssocks
[ns
], (struct sockaddr
*)nsap
,
848 Aerror(statp
, stderr
, "connect(dg)", errno
,
849 (struct sockaddr
*) nsap
);
853 /* Make socket non-blocking. */
854 int fl
= __fcntl (EXT(statp
).nssocks
[ns
], F_GETFL
);
856 __fcntl (EXT(statp
).nssocks
[ns
], F_SETFL
,
858 Dprint(statp
->options
& RES_DEBUG
,
859 (stdout
, ";; new DG socket\n"))
863 * Compute time for the total operation.
865 seconds
= (statp
->retrans
<< ns
);
867 seconds
/= statp
->nscount
;
871 evConsTime(&timeout
, seconds
, 0);
872 evAddTime(&finish
, &now
, &timeout
);
873 int need_recompute
= 0;
875 pfd
[0].fd
= EXT(statp
).nssocks
[ns
];
876 pfd
[0].events
= POLLOUT
;
878 if (need_recompute
) {
880 if (evCmpTime(finish
, now
) <= 0) {
881 Perror(statp
, stderr
, "select", errno
);
885 evSubTime(&timeout
, &finish
, &now
);
887 /* Convert struct timespec in milliseconds. */
888 ptimeout
= timeout
.tv_sec
* 1000 + timeout
.tv_nsec
/ 1000000;
892 n
= __poll (pfd
, 1, 0);
893 if (__builtin_expect (n
== 0, 0)) {
894 n
= __poll (pfd
, 1, ptimeout
);
898 Dprint(statp
->options
& RES_DEBUG
, (stdout
,
899 ";; timeout sending\n"));
904 if (errno
== EINTR
) {
907 if (evCmpTime(finish
, now
) > 0) {
908 evSubTime(&timeout
, &finish
, &now
);
912 Perror(statp
, stderr
, "poll", errno
);
917 if (pfd
[0].revents
& POLLOUT
) {
918 if (send(pfd
[0].fd
, (char*)buf
, buflen
, 0) != buflen
) {
919 if (errno
== EINTR
|| errno
== EAGAIN
)
920 goto recompute_resend
;
921 Perror(statp
, stderr
, "send", errno
);
925 pfd
[0].events
= POLLIN
;
928 } else if (pfd
[0].revents
& POLLIN
) {
929 fromlen
= sizeof(struct sockaddr_in6
);
930 if (anssiz
< MAXPACKET
932 && (ioctl (pfd
[0].fd
, FIONREAD
, &resplen
) < 0
933 || anssiz
< resplen
)) {
934 ans
= malloc (MAXPACKET
);
939 *anssizp
= MAXPACKET
;
942 anhp
= (HEADER
*) ans
;
945 resplen
= recvfrom(pfd
[0].fd
, (char*)ans
, anssiz
,0,
946 (struct sockaddr
*)&from
, &fromlen
);
948 if (errno
== EINTR
|| errno
== EAGAIN
) {
952 Perror(statp
, stderr
, "recvfrom", errno
);
957 if (resplen
< HFIXEDSZ
) {
959 * Undersized message.
961 Dprint(statp
->options
& RES_DEBUG
,
962 (stdout
, ";; undersized: %d\n",
968 if (hp
->id
!= anhp
->id
) {
970 * response from old query, ignore it.
971 * XXX - potential security hazard could
974 DprintQ((statp
->options
& RES_DEBUG
) ||
975 (statp
->pfcode
& RES_PRF_REPLY
),
976 (stdout
, ";; old answer:\n"),
977 ans
, (resplen
> anssiz
) ? anssiz
: resplen
);
980 if (!(statp
->options
& RES_INSECURE1
) &&
981 !res_ourserver_p(statp
, &from
)) {
983 * response from wrong server? ignore it.
984 * XXX - potential security hazard could
987 DprintQ((statp
->options
& RES_DEBUG
) ||
988 (statp
->pfcode
& RES_PRF_REPLY
),
989 (stdout
, ";; not our server:\n"),
990 ans
, (resplen
> anssiz
) ? anssiz
: resplen
);
993 if (!(statp
->options
& RES_INSECURE2
) &&
994 !res_queriesmatch(buf
, buf
+ buflen
,
995 ans
, ans
+ anssiz
)) {
997 * response contains wrong query? ignore it.
998 * XXX - potential security hazard could
1001 DprintQ((statp
->options
& RES_DEBUG
) ||
1002 (statp
->pfcode
& RES_PRF_REPLY
),
1003 (stdout
, ";; wrong query name:\n"),
1004 ans
, (resplen
> anssiz
) ? anssiz
: resplen
);
1007 if (anhp
->rcode
== SERVFAIL
||
1008 anhp
->rcode
== NOTIMP
||
1009 anhp
->rcode
== REFUSED
) {
1010 DprintQ(statp
->options
& RES_DEBUG
,
1011 (stdout
, "server rejected query:\n"),
1012 ans
, (resplen
> anssiz
) ? anssiz
: resplen
);
1015 /* don't retry if called from dig */
1019 if (anhp
->rcode
== NOERROR
&& anhp
->ancount
== 0
1020 && anhp
->aa
== 0 && anhp
->ra
== 0 && anhp
->arcount
== 0) {
1021 DprintQ(statp
->options
& RES_DEBUG
,
1022 (stdout
, "referred query:\n"),
1023 ans
, (resplen
> anssiz
) ? anssiz
: resplen
);
1026 if (!(statp
->options
& RES_IGNTC
) && anhp
->tc
) {
1028 * To get the rest of answer,
1029 * use TCP with same server.
1031 Dprint(statp
->options
& RES_DEBUG
,
1032 (stdout
, ";; truncated answer\n"));
1038 * All is well, or the error is fatal. Signal that the
1039 * next nameserver ought not be tried.
1042 } else if (pfd
[0].revents
& (POLLERR
| POLLHUP
| POLLNVAL
)) {
1043 /* Something went wrong. We can stop trying. */
1048 /* poll should not have returned > 0 in this case. */
1055 Aerror(const res_state statp
, FILE *file
, const char *string
, int error
,
1056 const struct sockaddr
*address
)
1060 if ((statp
->options
& RES_DEBUG
) != 0) {
1061 char tmp
[sizeof "xxxx.xxxx.xxxx.255.255.255.255"];
1063 fprintf(file
, "res_send: %s ([%s].%u): %s\n",
1065 (address
->sa_family
== AF_INET
1066 ? inet_ntop(address
->sa_family
,
1067 &((const struct sockaddr_in
*) address
)->sin_addr
,
1069 : inet_ntop(address
->sa_family
,
1070 &((const struct sockaddr_in6
*) address
)->sin6_addr
,
1072 (address
->sa_family
== AF_INET
1073 ? ntohs(((struct sockaddr_in
*) address
)->sin_port
)
1074 : address
->sa_family
== AF_INET6
1075 ? ntohs(((struct sockaddr_in6
*) address
)->sin6_port
)
1083 Perror(const res_state statp
, FILE *file
, const char *string
, int error
) {
1086 if ((statp
->options
& RES_DEBUG
) != 0)
1087 fprintf(file
, "res_send: %s: %s\n",
1088 string
, strerror(error
));
1094 sock_eq(struct sockaddr_in6
*a1
, struct sockaddr_in6
*a2
) {
1095 if (a1
->sin6_family
== a2
->sin6_family
) {
1096 if (a1
->sin6_family
== AF_INET
)
1097 return ((((struct sockaddr_in
*)a1
)->sin_port
==
1098 ((struct sockaddr_in
*)a2
)->sin_port
) &&
1099 (((struct sockaddr_in
*)a1
)->sin_addr
.s_addr
==
1100 ((struct sockaddr_in
*)a2
)->sin_addr
.s_addr
));
1102 return ((a1
->sin6_port
== a2
->sin6_port
) &&
1103 !memcmp(&a1
->sin6_addr
, &a2
->sin6_addr
,
1104 sizeof (struct in6_addr
)));
1106 if (a1
->sin6_family
== AF_INET
) {
1107 struct sockaddr_in6
*sap
= a1
;
1110 } /* assumes that AF_INET and AF_INET6 are the only possibilities */
1111 return ((a1
->sin6_port
== ((struct sockaddr_in
*)a2
)->sin_port
) &&
1112 IN6_IS_ADDR_V4MAPPED(&a1
->sin6_addr
) &&
1113 (a1
->sin6_addr
.s6_addr32
[3] ==
1114 ((struct sockaddr_in
*)a2
)->sin_addr
.s_addr
));
1118 * Converts IPv4 family, address and port to
1119 * IPv6 family, IPv4-mapped IPv6 address and port.
1122 convaddr4to6(struct sockaddr_in6
*sa
)
1124 struct sockaddr_in
*sa4p
= (struct sockaddr_in
*) sa
;
1125 in_port_t port
= sa4p
->sin_port
;
1126 in_addr_t addr
= sa4p
->sin_addr
.s_addr
;
1128 sa
->sin6_family
= AF_INET6
;
1129 sa
->sin6_port
= port
;
1130 sa
->sin6_addr
.s6_addr32
[0] = 0;
1131 sa
->sin6_addr
.s6_addr32
[1] = 0;
1132 sa
->sin6_addr
.s6_addr32
[2] = htonl(0xFFFF);
1133 sa
->sin6_addr
.s6_addr32
[3] = addr
;