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.
76 #include <sys/types.h>
77 #include <sys/param.h>
79 #include <sys/socket.h>
85 #include <netinet/in.h>
86 #include <arpa/nameser.h>
87 #include <arpa/inet.h>
88 #include <sys/ioctl.h>
100 #define MAXPACKET PACKETSZ
102 #define MAXPACKET 65536
106 #include <isc/eventlib.h>
109 /* From ev_streams.c. */
112 __attribute ((always_inline
))
113 evConsIovec(void *buf
, size_t cnt
, struct iovec
*vec
) {
114 memset(vec
, 0xf5, sizeof (*vec
));
119 /* From ev_timers.c. */
121 #define BILLION 1000000000
124 evConsTime(struct timespec
*res
, time_t sec
, long nsec
) {
130 evAddTime(struct timespec
*res
, const struct timespec
*addend1
,
131 const struct timespec
*addend2
) {
132 res
->tv_sec
= addend1
->tv_sec
+ addend2
->tv_sec
;
133 res
->tv_nsec
= addend1
->tv_nsec
+ addend2
->tv_nsec
;
134 if (res
->tv_nsec
>= BILLION
) {
136 res
->tv_nsec
-= BILLION
;
141 evSubTime(struct timespec
*res
, const struct timespec
*minuend
,
142 const struct timespec
*subtrahend
) {
143 res
->tv_sec
= minuend
->tv_sec
- subtrahend
->tv_sec
;
144 if (minuend
->tv_nsec
>= subtrahend
->tv_nsec
)
145 res
->tv_nsec
= minuend
->tv_nsec
- subtrahend
->tv_nsec
;
147 res
->tv_nsec
= (BILLION
148 - subtrahend
->tv_nsec
+ minuend
->tv_nsec
);
154 evCmpTime(struct timespec a
, struct timespec b
) {
155 long x
= a
.tv_sec
- b
.tv_sec
;
158 x
= a
.tv_nsec
- b
.tv_nsec
;
159 return (x
< 0L ? (-1) : x
> 0L ? (1) : (0));
163 evNowTime(struct timespec
*res
) {
166 if (gettimeofday(&now
, NULL
) < 0)
167 evConsTime(res
, 0, 0);
169 TIMEVAL_TO_TIMESPEC (&now
, res
);
174 /* Options. Leave them on. */
176 #include "res_debug.h"
178 #define EXT(res) ((res)->_u._ext)
181 static const int highestFD
= FD_SETSIZE
- 1;
186 static int send_vc(res_state
, const u_char
*, int,
187 u_char
**, int *, int *, int, u_char
**);
188 static int send_dg(res_state
, const u_char
*, int,
189 u_char
**, int *, int *, int,
190 int *, int *, u_char
**);
192 static void Aerror(const res_state
, FILE *, const char *, int,
194 static void Perror(const res_state
, FILE *, const char *, int);
197 static int sock_eq(struct sockaddr_in6
*, struct sockaddr_in6
*);
199 static int sock_eq(struct sockaddr_in
*, struct sockaddr_in
*);
202 static int pselect(int, void *, void *, void *,
210 static void convaddr4to6(struct sockaddr_in6
*sa
);
212 void res_pquery(const res_state
, const u_char
*, int, FILE *);
217 * res_isourserver(ina)
218 * looks up "ina" in _res.ns_addr_list[]
223 * paul vixie, 29may94
227 res_ourserver_p(const res_state statp
, const struct sockaddr_in6
*inp
)
229 res_ourserver_p(const res_state statp
, const struct sockaddr_in
*inp
)
235 if (inp
->sin6_family
== AF_INET
) {
236 struct sockaddr_in
*in4p
= (struct sockaddr_in
*) inp
;
237 in_port_t port
= in4p
->sin_port
;
238 in_addr_t addr
= in4p
->sin_addr
.s_addr
;
240 for (ns
= 0; ns
< MAXNS
; ns
++) {
241 const struct sockaddr_in
*srv
=
242 (struct sockaddr_in
*)EXT(statp
).nsaddrs
[ns
];
244 if ((srv
!= NULL
) && (srv
->sin_family
== AF_INET
) &&
245 (srv
->sin_port
== port
) &&
246 (srv
->sin_addr
.s_addr
== INADDR_ANY
||
247 srv
->sin_addr
.s_addr
== addr
))
250 } else if (inp
->sin6_family
== AF_INET6
) {
251 for (ns
= 0; ns
< MAXNS
; ns
++) {
252 const struct sockaddr_in6
*srv
= EXT(statp
).nsaddrs
[ns
];
253 if ((srv
!= NULL
) && (srv
->sin6_family
== AF_INET6
) &&
254 (srv
->sin6_port
== inp
->sin6_port
) &&
255 !(memcmp(&srv
->sin6_addr
, &in6addr_any
,
256 sizeof (struct in6_addr
)) &&
257 memcmp(&srv
->sin6_addr
, &inp
->sin6_addr
,
258 sizeof (struct in6_addr
))))
263 struct sockaddr_in ina
= *inp
;
264 for (ns
= 0; ns
< statp
->nscount
; ns
++) {
265 const struct sockaddr_in
*srv
= &statp
->nsaddr_list
[ns
];
267 if (srv
->sin_family
== ina
.sin_family
&&
268 srv
->sin_port
== ina
.sin_port
&&
269 (srv
->sin_addr
.s_addr
== INADDR_ANY
||
270 srv
->sin_addr
.s_addr
== ina
.sin_addr
.s_addr
))
278 * res_nameinquery(name, type, class, buf, eom)
279 * look for (name,type,class) in the query section of packet (buf,eom)
281 * buf + HFIXEDSZ <= eom
287 * paul vixie, 29may94
290 res_nameinquery(const char *name
, int type
, int class,
291 const u_char
*buf
, const u_char
*eom
)
293 const u_char
*cp
= buf
+ HFIXEDSZ
;
294 int qdcount
= ntohs(((HEADER
*)buf
)->qdcount
);
296 while (qdcount
-- > 0) {
297 char tname
[MAXDNAME
+1];
298 int n
, ttype
, tclass
;
300 n
= dn_expand(buf
, eom
, cp
, tname
, sizeof tname
);
304 if (cp
+ 2 * INT16SZ
> eom
)
306 ttype
= ns_get16(cp
); cp
+= INT16SZ
;
307 tclass
= ns_get16(cp
); cp
+= INT16SZ
;
308 if (ttype
== type
&& tclass
== class &&
309 ns_samename(tname
, name
) == 1)
316 * res_queriesmatch(buf1, eom1, buf2, eom2)
317 * is there a 1:1 mapping of (name,type,class)
318 * in (buf1,eom1) and (buf2,eom2)?
321 * 0 : not a 1:1 mapping
322 * >0 : is a 1:1 mapping
324 * paul vixie, 29may94
327 res_queriesmatch(const u_char
*buf1
, const u_char
*eom1
,
328 const u_char
*buf2
, const u_char
*eom2
)
330 const u_char
*cp
= buf1
+ HFIXEDSZ
;
331 int qdcount
= ntohs(((HEADER
*)buf1
)->qdcount
);
333 if (buf1
+ HFIXEDSZ
> eom1
|| buf2
+ HFIXEDSZ
> eom2
)
337 * Only header section present in replies to
338 * dynamic update packets.
340 if ((((HEADER
*)buf1
)->opcode
== ns_o_update
) &&
341 (((HEADER
*)buf2
)->opcode
== ns_o_update
))
344 if (qdcount
!= ntohs(((HEADER
*)buf2
)->qdcount
))
346 while (qdcount
-- > 0) {
347 char tname
[MAXDNAME
+1];
348 int n
, ttype
, tclass
;
350 n
= dn_expand(buf1
, eom1
, cp
, tname
, sizeof tname
);
354 if (cp
+ 2 * INT16SZ
> eom1
)
356 ttype
= ns_get16(cp
); cp
+= INT16SZ
;
357 tclass
= ns_get16(cp
); cp
+= INT16SZ
;
358 if (!res_nameinquery(tname
, ttype
, tclass
, buf2
, eom2
))
365 __libc_res_nsend(res_state statp
, const u_char
*buf
, int buflen
,
366 u_char
*ans
, int anssiz
, u_char
**ansp
)
368 int gotsomewhere
, terrno
, try, v_circuit
, resplen
, ns
, n
;
370 if (statp
->nscount
== 0) {
375 if (anssiz
< HFIXEDSZ
) {
376 __set_errno (EINVAL
);
380 if ((statp
->qhook
|| statp
->rhook
) && anssiz
< MAXPACKET
&& ansp
) {
381 u_char
*buf
= malloc (MAXPACKET
);
384 memcpy (buf
, ans
, HFIXEDSZ
);
390 DprintQ((statp
->options
& RES_DEBUG
) || (statp
->pfcode
& RES_PRF_QUERY
),
391 (stdout
, ";; res_send()\n"), buf
, buflen
);
392 v_circuit
= (statp
->options
& RES_USEVC
) || buflen
> 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
).nsinit
) {
403 if (EXT(statp
).nscount
!= statp
->nscount
)
407 for (ns
= 0; ns
< MAXNS
; ns
++) {
408 unsigned int map
= EXT(statp
).nsmap
[ns
];
410 && !sock_eq((struct sockaddr_in6
*)
411 &statp
->nsaddr_list
[map
],
412 EXT(statp
).nsaddrs
[ns
]))
414 for (ns
= 0; ns
< statp
->nscount
; ns
++) {
415 if (!sock_eq(&statp
->nsaddr_list
[ns
],
416 &EXT(statp
).nsaddrs
[ns
]))
428 * Maybe initialize our private copy of the ns_addr_list.
430 if (EXT(statp
).nsinit
== 0) {
432 unsigned char map
[MAXNS
];
434 memset (map
, MAXNS
, sizeof (map
));
435 for (n
= 0; n
< MAXNS
; n
++) {
436 ns
= EXT(statp
).nsmap
[n
];
437 if (ns
< statp
->nscount
)
439 else if (ns
< MAXNS
) {
440 free(EXT(statp
).nsaddrs
[n
]);
441 EXT(statp
).nsaddrs
[n
] = NULL
;
442 EXT(statp
).nsmap
[n
] = MAXNS
;
446 if (statp
->nscount
> EXT(statp
).nscount
)
447 for (n
= EXT(statp
).nscount
, ns
= 0;
448 n
< statp
->nscount
; n
++) {
450 && EXT(statp
).nsmap
[ns
] != MAXNS
)
454 EXT(statp
).nsmap
[ns
] = n
;
457 EXT(statp
).nscount
= n
;
458 for (ns
= 0; ns
< EXT(statp
).nscount
; ns
++) {
460 if (EXT(statp
).nsaddrs
[n
] == NULL
)
461 EXT(statp
).nsaddrs
[n
] =
462 malloc(sizeof (struct sockaddr_in6
));
463 if (EXT(statp
).nsaddrs
[n
] != NULL
) {
464 memcpy(EXT(statp
).nsaddrs
[n
],
465 &statp
->nsaddr_list
[ns
],
466 sizeof (struct sockaddr_in
));
467 EXT(statp
).nssocks
[n
] = -1;
472 for (ns
= 0; ns
< statp
->nscount
; ns
++) {
473 EXT(statp
).nsaddrs
[ns
] = statp
->nsaddr_list
[ns
];
474 EXT(statp
).nssocks
[ns
] = -1;
476 EXT(statp
).nscount
= ns
;
478 EXT(statp
).nsinit
= 1;
482 * Some resolvers want to even out the load on their nameservers.
483 * Note that RES_BLAST overrides RES_ROTATE.
485 if ((statp
->options
& RES_ROTATE
) != 0 &&
486 (statp
->options
& RES_BLAST
) == 0) {
488 struct sockaddr_in6
*ina
;
492 while (n
< MAXNS
&& EXT(statp
).nsmap
[n
] == MAXNS
)
495 ina
= EXT(statp
).nsaddrs
[n
];
496 map
= EXT(statp
).nsmap
[n
];
500 && EXT(statp
).nsmap
[ns
] == MAXNS
)
504 EXT(statp
).nsaddrs
[n
] = EXT(statp
).nsaddrs
[ns
];
505 EXT(statp
).nsmap
[n
] = EXT(statp
).nsmap
[ns
];
508 EXT(statp
).nsaddrs
[n
] = ina
;
509 EXT(statp
).nsmap
[n
] = map
;
512 struct sockaddr_in ina
;
513 int lastns
= statp
->nscount
- 1;
515 ina
= statp
->nsaddr_list
[0];
516 for (ns
= 0; ns
< lastns
; ns
++)
517 statp
->nsaddr_list
[ns
] = statp
->nsaddr_list
[ns
+ 1];
518 statp
->nsaddr_list
[lastns
] = ina
;
523 * Send request, RETRY times, or until successful.
525 for (try = 0; try < statp
->retry
; try++) {
527 for (ns
= 0; ns
< MAXNS
; ns
++)
529 for (ns
= 0; ns
< statp
->nscount
; ns
++)
533 struct sockaddr_in6
*nsap
= EXT(statp
).nsaddrs
[ns
];
538 struct sockaddr_in
*nsap
= &statp
->nsaddr_list
[ns
];
542 int done
= 0, loops
= 0;
548 struct sockaddr_in
*nsap4
;
549 nsap4
= (struct sockaddr_in
*) nsap
;
550 act
= (*statp
->qhook
)(&nsap4
, &buf
, &buflen
,
551 ans
, anssiz
, &resplen
);
552 nsap
= (struct sockaddr_in6
*) nsap4
;
554 act
= (*statp
->qhook
)(&nsap
, &buf
, &buflen
,
555 ans
, anssiz
, &resplen
);
567 /* give the hook another try */
568 if (++loops
< 42) /*doug adams*/
579 Dprint(statp
->options
& RES_DEBUG
,
580 (stdout
, ";; Querying server (# %d) address = %s\n",
581 ns
+ 1, inet_ntoa(nsap
->sin_addr
)));
584 /* Use VC; at most one attempt per server. */
586 n
= send_vc(statp
, buf
, buflen
, &ans
, &anssiz
, &terrno
,
595 n
= send_dg(statp
, buf
, buflen
, &ans
, &anssiz
, &terrno
,
596 ns
, &v_circuit
, &gotsomewhere
, ansp
);
606 Dprint((statp
->options
& RES_DEBUG
) ||
607 ((statp
->pfcode
& RES_PRF_REPLY
) &&
608 (statp
->pfcode
& RES_PRF_HEAD1
)),
609 (stdout
, ";; got answer:\n"));
611 DprintQ((statp
->options
& RES_DEBUG
) ||
612 (statp
->pfcode
& RES_PRF_REPLY
),
614 ans
, (resplen
> anssiz
) ? anssiz
: resplen
);
617 * If we have temporarily opened a virtual circuit,
618 * or if we haven't been asked to keep a socket open,
621 if ((v_circuit
&& (statp
->options
& RES_USEVC
) == 0) ||
622 (statp
->options
& RES_STAYOPEN
) == 0) {
626 int done
= 0, loops
= 0;
632 act
= (*statp
->rhook
)((struct sockaddr_in
*)
634 ans
, anssiz
, &resplen
);
636 act
= (*statp
->rhook
)(nsap
, buf
, buflen
,
637 ans
, anssiz
, &resplen
);
648 /* give the hook another try */
649 if (++loops
< 42) /*doug adams*/
667 __set_errno (ECONNREFUSED
); /* no nameservers found */
669 __set_errno (ETIMEDOUT
); /* no answer obtained */
671 __set_errno (terrno
);
676 res_nsend(res_state statp
,
677 const u_char
*buf
, int buflen
, u_char
*ans
, int anssiz
)
679 return __libc_res_nsend(statp
, buf
, buflen
, ans
, anssiz
, NULL
);
685 send_vc(res_state statp
,
686 const u_char
*buf
, int buflen
, u_char
**ansp
, int *anssizp
,
687 int *terrno
, int ns
, u_char
**anscp
)
689 const HEADER
*hp
= (HEADER
*) buf
;
691 int anssiz
= *anssizp
;
692 HEADER
*anhp
= (HEADER
*) ans
;
694 struct sockaddr_in6
*nsap
= EXT(statp
).nsaddrs
[ns
];
696 struct sockaddr_in
*nsap
= &statp
->nsaddr_list
[ns
];
698 int truncating
, connreset
, resplen
, n
;
707 /* Are we still talking to whom we want to talk to? */
708 if (statp
->_vcsock
>= 0 && (statp
->_flags
& RES_F_VC
) != 0) {
710 struct sockaddr_in6 peer
;
712 struct sockaddr_in peer
;
714 int size
= sizeof peer
;
716 if (getpeername(statp
->_vcsock
,
717 (struct sockaddr
*)&peer
, &size
) < 0 ||
718 !sock_eq(&peer
, nsap
)) {
720 statp
->_flags
&= ~RES_F_VC
;
724 if (statp
->_vcsock
< 0 || (statp
->_flags
& RES_F_VC
) == 0) {
725 if (statp
->_vcsock
>= 0)
729 statp
->_vcsock
= socket(nsap
->sin6_family
, SOCK_STREAM
, 0);
731 statp
->_vcsock
= socket(PF_INET
, SOCK_STREAM
, 0);
732 if (statp
->_vcsock
> highestFD
) {
734 __set_errno (ENOTSOCK
);
737 if (statp
->_vcsock
< 0) {
739 Perror(statp
, stderr
, "socket(vc)", errno
);
743 if (connect(statp
->_vcsock
, (struct sockaddr
*)nsap
,
746 Aerror(statp
, stderr
, "connect/vc", errno
, *nsap
);
750 statp
->_flags
|= RES_F_VC
;
754 * Send length & message
756 putshort((u_short
)buflen
, (u_char
*)&len
);
757 evConsIovec(&len
, INT16SZ
, &iov
[0]);
758 evConsIovec((void*)buf
, buflen
, &iov
[1]);
759 if (TEMP_FAILURE_RETRY (writev(statp
->_vcsock
, iov
, 2))
760 != (INT16SZ
+ buflen
)) {
762 Perror(statp
, stderr
, "write failed", errno
);
767 * Receive length & response
772 while ((n
= TEMP_FAILURE_RETRY (read(statp
->_vcsock
, (char *)cp
,
780 Perror(statp
, stderr
, "read failed", errno
);
783 * A long running process might get its TCP
784 * connection reset if the remote server was
785 * restarted. Requery the server instead of
786 * trying a new one. When there is only one
787 * server, this means that a query might work
788 * instead of failing. We only allow one reset
789 * per query to prevent looping.
791 if (*terrno
== ECONNRESET
&& !connreset
) {
799 resplen
= ns_get16(ans
);
800 if (resplen
> anssiz
) {
802 ans
= malloc (MAXPACKET
);
809 *anssizp
= MAXPACKET
;
812 anhp
= (HEADER
*) ans
;
815 Dprint(statp
->options
& RES_DEBUG
,
816 (stdout
, ";; response truncated\n")
823 if (len
< HFIXEDSZ
) {
825 * Undersized message.
827 Dprint(statp
->options
& RES_DEBUG
,
828 (stdout
, ";; undersized: %d\n", len
));
834 while (len
!= 0 && (n
= read(statp
->_vcsock
, (char *)cp
, (int)len
)) > 0){
840 Perror(statp
, stderr
, "read(vc)", errno
);
846 * Flush rest of answer so connection stays in synch.
849 len
= resplen
- anssiz
;
853 n
= read(statp
->_vcsock
, junk
,
854 (len
> sizeof junk
) ? sizeof junk
: len
);
862 * If the calling applicating has bailed out of
863 * a previous call and failed to arrange to have
864 * the circuit closed or the server has got
865 * itself confused, then drop the packet and
866 * wait for the correct one.
868 if (hp
->id
!= anhp
->id
) {
869 DprintQ((statp
->options
& RES_DEBUG
) ||
870 (statp
->pfcode
& RES_PRF_REPLY
),
871 (stdout
, ";; old answer (unexpected):\n"),
872 ans
, (resplen
> anssiz
) ? anssiz
: resplen
);
877 * All is well, or the error is fatal. Signal that the
878 * next nameserver ought not be tried.
884 send_dg(res_state statp
,
885 const u_char
*buf
, int buflen
, u_char
**ansp
, int *anssizp
,
886 int *terrno
, int ns
, int *v_circuit
, int *gotsomewhere
, u_char
**anscp
)
888 const HEADER
*hp
= (HEADER
*) buf
;
890 int anssiz
= *anssizp
;
891 HEADER
*anhp
= (HEADER
*) ans
;
893 struct sockaddr_in6
*nsap
= EXT(statp
).nsaddrs
[ns
];
895 const struct sockaddr_in
*nsap
= &statp
->nsaddr_list
[ns
];
897 struct timespec now
, timeout
, finish
;
899 struct pollfd pfd
[1];
901 struct sockaddr_in6 from
;
902 static int socket_pf
= 0;
905 struct sockaddr_in from
;
907 int fromlen
, resplen
, seconds
, n
, s
;
909 if (EXT(statp
).nssocks
[ns
] == -1) {
911 /* only try IPv6 if IPv6 NS and if not failed before */
912 if ((EXT(statp
).nscount6
> 0) && (socket_pf
!= PF_INET
)) {
913 EXT(statp
).nssocks
[ns
] =
914 socket(PF_INET6
, SOCK_DGRAM
, 0);
915 socket_pf
= EXT(statp
).nssocks
[ns
] < 0 ? PF_INET
918 if (EXT(statp
).nssocks
[ns
] < 0)
919 EXT(statp
).nssocks
[ns
] = socket(PF_INET
, SOCK_DGRAM
, 0);
921 EXT(statp
).nssocks
[ns
] = socket(PF_INET
, SOCK_DGRAM
, 0);
922 if (EXT(statp
).nssocks
[ns
] > highestFD
) {
924 __set_errno (ENOTSOCK
);
927 if (EXT(statp
).nssocks
[ns
] < 0) {
929 Perror(statp
, stderr
, "socket(dg)", errno
);
932 #ifndef CANNOT_CONNECT_DGRAM
934 /* If IPv6 socket and nsap is IPv4, make it IPv4-mapped */
935 if ((socket_pf
== PF_INET6
) && (nsap
->sin6_family
== AF_INET
))
939 * On a 4.3BSD+ machine (client and server,
940 * actually), sending to a nameserver datagram
941 * port with no nameserver will cause an
942 * ICMP port unreachable message to be returned.
943 * If our datagram socket is "connected" to the
944 * server, we get an ECONNREFUSED error on the next
945 * socket operation, and select returns if the
946 * error message is received. We can thus detect
947 * the absence of a nameserver without timing out.
949 if (connect(EXT(statp
).nssocks
[ns
], (struct sockaddr
*)nsap
,
951 Aerror(statp
, stderr
, "connect(dg)", errno
, *nsap
);
955 #endif /* !CANNOT_CONNECT_DGRAM */
956 Dprint(statp
->options
& RES_DEBUG
,
957 (stdout
, ";; new DG socket\n"))
959 s
= EXT(statp
).nssocks
[ns
];
960 #ifndef CANNOT_CONNECT_DGRAM
961 if (send(s
, (char*)buf
, buflen
, 0) != buflen
) {
962 Perror(statp
, stderr
, "send", errno
);
966 #else /* !CANNOT_CONNECT_DGRAM */
968 /* If IPv6 socket and nsap is IPv4, make it IPv4-mapped */
969 if ((socket_pf
== PF_INET6
) && (nsap
->sin6_family
== AF_INET
))
972 if (sendto(s
, (char*)buf
, buflen
, 0,
973 (struct sockaddr
*)nsap
, sizeof *nsap
) != buflen
)
975 Aerror(statp
, stderr
, "sendto", errno
, *nsap
);
979 #endif /* !CANNOT_CONNECT_DGRAM */
984 seconds
= (statp
->retrans
<< ns
);
986 seconds
/= statp
->nscount
;
990 evConsTime(&timeout
, seconds
, 0);
991 evAddTime(&finish
, &now
, &timeout
);
994 /* Convert struct timespec in milliseconds. */
995 ptimeout
= timeout
.tv_sec
* 1000 + timeout
.tv_nsec
/ 1000000;
998 pfd
[0].events
= POLLIN
;
999 n
= __poll (pfd
, 1, ptimeout
);
1003 n
= pselect(s
+ 1, &dsmask
, NULL
, NULL
, &timeout
, NULL
);
1006 Dprint(statp
->options
& RES_DEBUG
, (stdout
, ";; timeout\n"));
1011 if (errno
== EINTR
) {
1013 if (evCmpTime(finish
, now
) > 0) {
1014 evSubTime(&timeout
, &finish
, &now
);
1018 Perror(statp
, stderr
, "select", errno
);
1024 fromlen
= sizeof(struct sockaddr_in6
);
1026 fromlen
= sizeof(struct sockaddr_in
);
1028 if (anssiz
< MAXPACKET
1030 && (ioctl (s
, FIONREAD
, &resplen
) < 0
1031 || anssiz
< resplen
)) {
1032 ans
= malloc (MAXPACKET
);
1037 *anssizp
= MAXPACKET
;
1040 anhp
= (HEADER
*) ans
;
1043 resplen
= recvfrom(s
, (char*)ans
, anssiz
,0,
1044 (struct sockaddr
*)&from
, &fromlen
);
1046 Perror(statp
, stderr
, "recvfrom", errno
);
1051 if (resplen
< HFIXEDSZ
) {
1053 * Undersized message.
1055 Dprint(statp
->options
& RES_DEBUG
,
1056 (stdout
, ";; undersized: %d\n",
1062 if (hp
->id
!= anhp
->id
) {
1064 * response from old query, ignore it.
1065 * XXX - potential security hazard could
1068 DprintQ((statp
->options
& RES_DEBUG
) ||
1069 (statp
->pfcode
& RES_PRF_REPLY
),
1070 (stdout
, ";; old answer:\n"),
1071 ans
, (resplen
> anssiz
) ? anssiz
: resplen
);
1074 if (!(statp
->options
& RES_INSECURE1
) &&
1075 !res_ourserver_p(statp
, &from
)) {
1077 * response from wrong server? ignore it.
1078 * XXX - potential security hazard could
1081 DprintQ((statp
->options
& RES_DEBUG
) ||
1082 (statp
->pfcode
& RES_PRF_REPLY
),
1083 (stdout
, ";; not our server:\n"),
1084 ans
, (resplen
> anssiz
) ? anssiz
: resplen
);
1087 if (!(statp
->options
& RES_INSECURE2
) &&
1088 !res_queriesmatch(buf
, buf
+ buflen
,
1089 ans
, ans
+ anssiz
)) {
1091 * response contains wrong query? ignore it.
1092 * XXX - potential security hazard could
1095 DprintQ((statp
->options
& RES_DEBUG
) ||
1096 (statp
->pfcode
& RES_PRF_REPLY
),
1097 (stdout
, ";; wrong query name:\n"),
1098 ans
, (resplen
> anssiz
) ? anssiz
: resplen
);
1101 if (anhp
->rcode
== SERVFAIL
||
1102 anhp
->rcode
== NOTIMP
||
1103 anhp
->rcode
== REFUSED
) {
1104 DprintQ(statp
->options
& RES_DEBUG
,
1105 (stdout
, "server rejected query:\n"),
1106 ans
, (resplen
> anssiz
) ? anssiz
: resplen
);
1108 /* don't retry if called from dig */
1112 if (!(statp
->options
& RES_IGNTC
) && anhp
->tc
) {
1114 * To get the rest of answer,
1115 * use TCP with same server.
1117 Dprint(statp
->options
& RES_DEBUG
,
1118 (stdout
, ";; truncated answer\n"));
1124 * All is well, or the error is fatal. Signal that the
1125 * next nameserver ought not be tried.
1132 Aerror(const res_state statp
, FILE *file
, const char *string
, int error
,
1133 struct sockaddr_in address
)
1137 if ((statp
->options
& RES_DEBUG
) != 0) {
1138 char tmp
[sizeof "255.255.255.255"];
1140 fprintf(file
, "res_send: %s ([%s].%u): %s\n",
1142 inet_ntop(address
.sin_family
, &address
.sin_addr
,
1144 ntohs(address
.sin_port
),
1151 Perror(const res_state statp
, FILE *file
, const char *string
, int error
) {
1154 if ((statp
->options
& RES_DEBUG
) != 0)
1155 fprintf(file
, "res_send: %s: %s\n",
1156 string
, strerror(error
));
1163 sock_eq(struct sockaddr_in6
*a1
, struct sockaddr_in6
*a2
) {
1164 if (a1
->sin6_family
== a2
->sin6_family
) {
1165 if (a1
->sin6_family
== AF_INET
)
1166 return ((((struct sockaddr_in
*)a1
)->sin_port
==
1167 ((struct sockaddr_in
*)a2
)->sin_port
) &&
1168 (((struct sockaddr_in
*)a1
)->sin_addr
.s_addr
==
1169 ((struct sockaddr_in
*)a2
)->sin_addr
.s_addr
));
1171 return ((a1
->sin6_port
== a2
->sin6_port
) &&
1172 !memcmp(&a1
->sin6_addr
, &a2
->sin6_addr
,
1173 sizeof (struct in6_addr
)));
1175 if (a1
->sin6_family
== AF_INET
) {
1176 struct sockaddr_in6
*sap
= a1
;
1179 } /* assumes that AF_INET and AF_INET6 are the only possibilities */
1180 return ((a1
->sin6_port
== ((struct sockaddr_in
*)a2
)->sin_port
) &&
1181 IN6_IS_ADDR_V4MAPPED(&a1
->sin6_addr
) &&
1182 (a1
->sin6_addr
.s6_addr32
[3] ==
1183 ((struct sockaddr_in
*)a2
)->sin_addr
.s_addr
));
1186 sock_eq(struct sockaddr_in
*a1
, struct sockaddr_in
*a2
) {
1187 return ((a1
->sin_family
== a2
->sin_family
) &&
1188 (a1
->sin_port
== a2
->sin_port
) &&
1189 (a1
->sin_addr
.s_addr
== a2
->sin_addr
.s_addr
));
1195 * Converts IPv4 family, address and port to
1196 * IPv6 family, IPv4-mapped IPv6 address and port.
1199 convaddr4to6(struct sockaddr_in6
*sa
)
1201 struct sockaddr_in
*sa4p
= (struct sockaddr_in
*) sa
;
1202 in_port_t port
= sa4p
->sin_port
;
1203 in_addr_t addr
= sa4p
->sin_addr
.s_addr
;
1205 sa
->sin6_family
= AF_INET6
;
1206 sa
->sin6_port
= port
;
1207 sa
->sin6_addr
.s6_addr32
[0] = 0;
1208 sa
->sin6_addr
.s6_addr32
[1] = 0;
1209 sa
->sin6_addr
.s6_addr32
[2] = htonl(0xFFFF);
1210 sa
->sin6_addr
.s6_addr32
[3] = addr
;
1215 /* XXX needs to move to the porting library. */
1217 pselect(int nfds
, void *rfds
, void *wfds
, void *efds
,
1218 struct timespec
*tsp
, const sigset_t
*sigmask
)
1220 struct timeval tv
, *tvp
;
1226 tv
= evTimeVal(*tsp
);
1230 sigprocmask(SIG_SETMASK
, sigmask
, &sigs
);
1231 n
= select(nfds
, rfds
, wfds
, efds
, tvp
);
1233 sigprocmask(SIG_SETMASK
, &sigs
, NULL
);
1235 TIMEVAL_TO_TIMESPEC (tv
, *tsp
);