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 * 3. 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 * Copyright (c) 2005 by Internet Systems Consortium, Inc. ("ISC")
52 * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
54 * Permission to use, copy, modify, and distribute this software for any
55 * purpose with or without fee is hereby granted, provided that the above
56 * copyright notice and this permission notice appear in all copies.
58 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
59 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
60 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
61 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
62 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
63 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
64 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
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
[] = "$Id: res_send.c,v 1.18.10.1 2008/01/27 02:06:46 marka Exp $";
70 #endif /* LIBC_SCCS and not lint */
74 * Send query to name server and wait for reply.
77 #include "port_before.h"
79 #include "fd_setsize.h"
83 #include "namespace.h"
85 #include <sys/types.h>
86 #include <sys/param.h>
88 #include <sys/socket.h>
91 #include <netinet/in.h>
92 #include <arpa/nameser.h>
93 #include <arpa/inet.h>
104 #include "isc/eventlib.h"
106 #include "port_after.h"
108 #include <sys/event.h>
112 #ifdef HAVE_STROPTS_H
116 #endif /* USE_POLL */
120 #include "un-namespace.h"
123 /* Options. Leave them on. */
125 #include "res_debug.h"
126 #include "res_private.h"
128 #define EXT(res) ((res)->_u._ext)
130 #if !defined(USE_POLL) && !defined(USE_KQUEUE)
131 static const int highestFD
= FD_SETSIZE
- 1;
136 static int get_salen
__P((const struct sockaddr
*));
137 static struct sockaddr
* get_nsaddr
__P((res_state
, size_t));
138 static int send_vc(res_state
, const u_char
*, int,
139 u_char
*, int, int *, int);
140 static int send_dg(res_state
,
145 u_char
*, int, int *, int, int,
147 static void Aerror(const res_state
, FILE *, const char *, int,
148 const struct sockaddr
*, int);
149 static void Perror(const res_state
, FILE *, const char *, int);
150 static int sock_eq(struct sockaddr
*, struct sockaddr
*);
151 #if defined(NEED_PSELECT) && !defined(USE_POLL) && !defined(USE_KQUEUE)
152 static int pselect(int, void *, void *, void *,
156 void res_pquery(const res_state
, const u_char
*, int, FILE *);
158 static const int niflags
= NI_NUMERICHOST
| NI_NUMERICSERV
;
163 * looks up "ina" in _res.ns_addr_list[]
170 *\li paul vixie, 29may94
173 res_ourserver_p(const res_state statp
, const struct sockaddr
*sa
) {
174 const struct sockaddr_in
*inp
, *srv
;
175 const struct sockaddr_in6
*in6p
, *srv6
;
178 switch (sa
->sa_family
) {
180 inp
= (const struct sockaddr_in
*)sa
;
181 for (ns
= 0; ns
< statp
->nscount
; ns
++) {
182 srv
= (struct sockaddr_in
*)get_nsaddr(statp
, ns
);
183 if (srv
->sin_family
== inp
->sin_family
&&
184 srv
->sin_port
== inp
->sin_port
&&
185 (srv
->sin_addr
.s_addr
== INADDR_ANY
||
186 srv
->sin_addr
.s_addr
== inp
->sin_addr
.s_addr
))
191 if (EXT(statp
).ext
== NULL
)
193 in6p
= (const struct sockaddr_in6
*)sa
;
194 for (ns
= 0; ns
< statp
->nscount
; ns
++) {
195 srv6
= (struct sockaddr_in6
*)get_nsaddr(statp
, ns
);
196 if (srv6
->sin6_family
== in6p
->sin6_family
&&
197 srv6
->sin6_port
== in6p
->sin6_port
&&
198 #ifdef HAVE_SIN6_SCOPE_ID
199 (srv6
->sin6_scope_id
== 0 ||
200 srv6
->sin6_scope_id
== in6p
->sin6_scope_id
) &&
202 (IN6_IS_ADDR_UNSPECIFIED(&srv6
->sin6_addr
) ||
203 IN6_ARE_ADDR_EQUAL(&srv6
->sin6_addr
, &in6p
->sin6_addr
)))
214 * look for (name,type,class) in the query section of packet (buf,eom)
217 *\li buf + HFIXEDSZ <= eom
220 *\li -1 : format error
225 *\li paul vixie, 29may94
228 res_nameinquery(const char *name
, int type
, int class,
229 const u_char
*buf
, const u_char
*eom
)
231 const u_char
*cp
= buf
+ HFIXEDSZ
;
232 int qdcount
= ntohs(((const HEADER
*)buf
)->qdcount
);
234 while (qdcount
-- > 0) {
235 char tname
[MAXDNAME
+1];
236 int n
, ttype
, tclass
;
238 n
= dn_expand(buf
, eom
, cp
, tname
, sizeof tname
);
242 if (cp
+ 2 * INT16SZ
> eom
)
244 ttype
= ns_get16(cp
); cp
+= INT16SZ
;
245 tclass
= ns_get16(cp
); cp
+= INT16SZ
;
246 if (ttype
== type
&& tclass
== class &&
247 ns_samename(tname
, name
) == 1)
254 * is there a 1:1 mapping of (name,type,class)
255 * in (buf1,eom1) and (buf2,eom2)?
258 *\li -1 : format error
259 *\li 0 : not a 1:1 mapping
260 *\li >0 : is a 1:1 mapping
263 *\li paul vixie, 29may94
266 res_queriesmatch(const u_char
*buf1
, const u_char
*eom1
,
267 const u_char
*buf2
, const u_char
*eom2
)
269 const u_char
*cp
= buf1
+ HFIXEDSZ
;
270 int qdcount
= ntohs(((const HEADER
*)buf1
)->qdcount
);
272 if (buf1
+ HFIXEDSZ
> eom1
|| buf2
+ HFIXEDSZ
> eom2
)
276 * Only header section present in replies to
277 * dynamic update packets.
279 if ((((const HEADER
*)buf1
)->opcode
== ns_o_update
) &&
280 (((const HEADER
*)buf2
)->opcode
== ns_o_update
))
283 if (qdcount
!= ntohs(((const HEADER
*)buf2
)->qdcount
))
285 while (qdcount
-- > 0) {
286 char tname
[MAXDNAME
+1];
287 int n
, ttype
, tclass
;
289 n
= dn_expand(buf1
, eom1
, cp
, tname
, sizeof tname
);
293 if (cp
+ 2 * INT16SZ
> eom1
)
295 ttype
= ns_get16(cp
); cp
+= INT16SZ
;
296 tclass
= ns_get16(cp
); cp
+= INT16SZ
;
297 if (!res_nameinquery(tname
, ttype
, tclass
, buf2
, eom2
))
304 res_nsend(res_state statp
,
305 const u_char
*buf
, int buflen
, u_char
*ans
, int anssiz
)
307 int gotsomewhere
, terrno
, tries
, v_circuit
, resplen
, ns
, n
;
311 char abuf
[NI_MAXHOST
];
313 /* No name servers or res_init() failure */
314 if (statp
->nscount
== 0 || EXT(statp
).ext
== NULL
) {
318 if (anssiz
< HFIXEDSZ
) {
322 DprintQ((statp
->options
& RES_DEBUG
) || (statp
->pfcode
& RES_PRF_QUERY
),
323 (stdout
, ";; res_send()\n"), buf
, buflen
);
324 v_circuit
= (statp
->options
& RES_USEVC
) || buflen
> PACKETSZ
;
329 if ((kq
= kqueue()) < 0) {
330 Perror(statp
, stderr
, "kqueue", errno
);
336 * If the ns_addr_list in the resolver context has changed, then
337 * invalidate our cached copy and the associated timing data.
339 if (EXT(statp
).nscount
!= 0) {
341 struct sockaddr_storage peer
;
342 ISC_SOCKLEN_T peerlen
;
344 if (EXT(statp
).nscount
!= statp
->nscount
)
347 for (ns
= 0; ns
< statp
->nscount
; ns
++) {
348 if (statp
->nsaddr_list
[ns
].sin_family
&&
349 !sock_eq((struct sockaddr
*)&statp
->nsaddr_list
[ns
],
350 (struct sockaddr
*)&EXT(statp
).ext
->nsaddrs
[ns
])) {
355 if (EXT(statp
).nssocks
[ns
] == -1)
357 peerlen
= sizeof(peer
);
359 if (getsockname(EXT(statp
).nssocks
[ns
],
361 if (_getsockname(EXT(statp
).nssocks
[ns
],
363 (struct sockaddr
*)&peer
, &peerlen
) < 0) {
367 if (!sock_eq((struct sockaddr
*)&peer
,
368 get_nsaddr(statp
, ns
))) {
375 EXT(statp
).nscount
= 0;
380 * Maybe initialize our private copy of the ns_addr_list.
382 if (EXT(statp
).nscount
== 0) {
383 for (ns
= 0; ns
< statp
->nscount
; ns
++) {
384 EXT(statp
).nstimes
[ns
] = RES_MAXTIME
;
385 EXT(statp
).nssocks
[ns
] = -1;
386 if (!statp
->nsaddr_list
[ns
].sin_family
)
388 EXT(statp
).ext
->nsaddrs
[ns
].sin
=
389 statp
->nsaddr_list
[ns
];
391 EXT(statp
).nscount
= statp
->nscount
;
395 * Some resolvers want to even out the load on their nameservers.
396 * Note that RES_BLAST overrides RES_ROTATE.
398 if ((statp
->options
& RES_ROTATE
) != 0U &&
399 (statp
->options
& RES_BLAST
) == 0U) {
400 union res_sockaddr_union inu
;
401 struct sockaddr_in ina
;
402 int lastns
= statp
->nscount
- 1;
406 if (EXT(statp
).ext
!= NULL
)
407 inu
= EXT(statp
).ext
->nsaddrs
[0];
408 ina
= statp
->nsaddr_list
[0];
409 fd
= EXT(statp
).nssocks
[0];
410 nstime
= EXT(statp
).nstimes
[0];
411 for (ns
= 0; ns
< lastns
; ns
++) {
412 if (EXT(statp
).ext
!= NULL
)
413 EXT(statp
).ext
->nsaddrs
[ns
] =
414 EXT(statp
).ext
->nsaddrs
[ns
+ 1];
415 statp
->nsaddr_list
[ns
] = statp
->nsaddr_list
[ns
+ 1];
416 EXT(statp
).nssocks
[ns
] = EXT(statp
).nssocks
[ns
+ 1];
417 EXT(statp
).nstimes
[ns
] = EXT(statp
).nstimes
[ns
+ 1];
419 if (EXT(statp
).ext
!= NULL
)
420 EXT(statp
).ext
->nsaddrs
[lastns
] = inu
;
421 statp
->nsaddr_list
[lastns
] = ina
;
422 EXT(statp
).nssocks
[lastns
] = fd
;
423 EXT(statp
).nstimes
[lastns
] = nstime
;
427 * Send request, RETRY times, or until successful.
429 for (tries
= 0; tries
< statp
->retry
; tries
++) {
430 for (ns
= 0; ns
< statp
->nscount
; ns
++) {
431 struct sockaddr
*nsap
;
433 nsap
= get_nsaddr(statp
, ns
);
434 nsaplen
= get_salen(nsap
);
435 statp
->_flags
&= ~RES_F_LASTMASK
;
436 statp
->_flags
|= (ns
<< RES_F_LASTSHIFT
);
439 int done
= 0, loops
= 0;
444 act
= (*statp
->qhook
)(&nsap
, &buf
, &buflen
,
445 ans
, anssiz
, &resplen
);
459 /* give the hook another try */
460 if (++loops
< 42) /*doug adams*/
471 Dprint(((statp
->options
& RES_DEBUG
) &&
472 getnameinfo(nsap
, nsaplen
, abuf
, sizeof(abuf
),
473 NULL
, 0, niflags
) == 0),
474 (stdout
, ";; Querying server (# %d) address = %s\n",
479 /* Use VC; at most one attempt per server. */
480 tries
= statp
->retry
;
481 n
= send_vc(statp
, buf
, buflen
, ans
, anssiz
, &terrno
,
494 buf
, buflen
, ans
, anssiz
, &terrno
,
495 ns
, tries
, &v_circuit
, &gotsomewhere
);
505 Dprint((statp
->options
& RES_DEBUG
) ||
506 ((statp
->pfcode
& RES_PRF_REPLY
) &&
507 (statp
->pfcode
& RES_PRF_HEAD1
)),
508 (stdout
, ";; got answer:\n"));
510 DprintQ((statp
->options
& RES_DEBUG
) ||
511 (statp
->pfcode
& RES_PRF_REPLY
),
513 ans
, (resplen
> anssiz
) ? anssiz
: resplen
);
516 * If we have temporarily opened a virtual circuit,
517 * or if we haven't been asked to keep a socket open,
520 if ((v_circuit
&& (statp
->options
& RES_USEVC
) == 0U) ||
521 (statp
->options
& RES_STAYOPEN
) == 0U) {
525 int done
= 0, loops
= 0;
530 act
= (*statp
->rhook
)(nsap
, buf
, buflen
,
531 ans
, anssiz
, &resplen
);
541 /* give the hook another try */
542 if (++loops
< 42) /*doug adams*/
566 errno
= ECONNREFUSED
; /*%< no nameservers found */
568 errno
= ETIMEDOUT
; /*%< no answer obtained */
580 get_salen(const struct sockaddr
*sa
)
584 /* There are people do not set sa_len. Be forgiving to them. */
589 if (sa
->sa_family
== AF_INET
)
590 return (sizeof(struct sockaddr_in
));
591 else if (sa
->sa_family
== AF_INET6
)
592 return (sizeof(struct sockaddr_in6
));
594 return (0); /*%< unknown, die on connect */
598 * pick appropriate nsaddr_list for use. see res_init() for initialization.
600 static struct sockaddr
*
601 get_nsaddr(res_state statp
, size_t n
)
604 if (!statp
->nsaddr_list
[n
].sin_family
&& EXT(statp
).ext
) {
606 * - EXT(statp).ext->nsaddrs[n] holds an address that is larger
607 * than struct sockaddr, and
608 * - user code did not update statp->nsaddr_list[n].
610 return (struct sockaddr
*)(void *)&EXT(statp
).ext
->nsaddrs
[n
];
613 * - user code updated statp->nsaddr_list[n], or
614 * - statp->nsaddr_list[n] has the same content as
615 * EXT(statp).ext->nsaddrs[n].
617 return (struct sockaddr
*)(void *)&statp
->nsaddr_list
[n
];
622 send_vc(res_state statp
,
623 const u_char
*buf
, int buflen
, u_char
*ans
, int anssiz
,
626 const HEADER
*hp
= (const HEADER
*) buf
;
627 HEADER
*anhp
= (HEADER
*) ans
;
628 struct sockaddr
*nsap
;
630 int truncating
, connreset
, resplen
, n
;
639 nsap
= get_nsaddr(statp
, ns
);
640 nsaplen
= get_salen(nsap
);
646 /* Are we still talking to whom we want to talk to? */
647 if (statp
->_vcsock
>= 0 && (statp
->_flags
& RES_F_VC
) != 0) {
648 struct sockaddr_storage peer
;
649 ISC_SOCKLEN_T size
= sizeof peer
;
651 if (getpeername(statp
->_vcsock
,
653 if (_getpeername(statp
->_vcsock
,
655 (struct sockaddr
*)&peer
, &size
) < 0 ||
656 !sock_eq((struct sockaddr
*)&peer
, nsap
)) {
658 statp
->_flags
&= ~RES_F_VC
;
662 if (statp
->_vcsock
< 0 || (statp
->_flags
& RES_F_VC
) == 0) {
663 if (statp
->_vcsock
>= 0)
666 statp
->_vcsock
= socket(nsap
->sa_family
, SOCK_STREAM
, 0);
668 statp
->_vcsock
= _socket(nsap
->sa_family
, SOCK_STREAM
, 0);
670 #if !defined(USE_POLL) && !defined(USE_KQUEUE)
671 if (statp
->_vcsock
> highestFD
) {
676 if (statp
->_vcsock
< 0) {
678 case EPROTONOSUPPORT
:
683 Perror(statp
, stderr
, "socket(vc)", errno
);
687 Perror(statp
, stderr
, "socket(vc)", errno
);
693 * Disable generation of SIGPIPE when writing to a closed
694 * socket. Write should return -1 and set errno to EPIPE
697 * Push on even if setsockopt(SO_NOSIGPIPE) fails.
699 (void)_setsockopt(statp
->_vcsock
, SOL_SOCKET
, SO_NOSIGPIPE
, &on
,
704 if (connect(statp
->_vcsock
, nsap
, nsaplen
) < 0) {
706 if (_connect(statp
->_vcsock
, nsap
, nsaplen
) < 0) {
709 Aerror(statp
, stderr
, "connect/vc", errno
, nsap
,
714 statp
->_flags
|= RES_F_VC
;
718 * Send length & message
720 ns_put16((u_short
)buflen
, (u_char
*)&len
);
721 iov
[0] = evConsIovec(&len
, INT16SZ
);
723 iov
[1] = evConsIovec(tmp
, buflen
);
725 if (writev(statp
->_vcsock
, iov
, 2) != (INT16SZ
+ buflen
)) {
727 if (_writev(statp
->_vcsock
, iov
, 2) != (INT16SZ
+ buflen
)) {
730 Perror(statp
, stderr
, "write failed", errno
);
735 * Receive length & response
741 while ((n
= read(statp
->_vcsock
, (char *)cp
, (int)len
)) > 0) {
743 while ((n
= _read(statp
->_vcsock
, (char *)cp
, (int)len
)) > 0) {
751 Perror(statp
, stderr
, "read failed", errno
);
754 * A long running process might get its TCP
755 * connection reset if the remote server was
756 * restarted. Requery the server instead of
757 * trying a new one. When there is only one
758 * server, this means that a query might work
759 * instead of failing. We only allow one reset
760 * per query to prevent looping.
762 if (*terrno
== ECONNRESET
&& !connreset
) {
770 resplen
= ns_get16(ans
);
771 if (resplen
> anssiz
) {
772 Dprint(statp
->options
& RES_DEBUG
,
773 (stdout
, ";; response truncated\n")
779 if (len
< HFIXEDSZ
) {
781 * Undersized message.
783 Dprint(statp
->options
& RES_DEBUG
,
784 (stdout
, ";; undersized: %d\n", len
));
791 while (len
!= 0 && (n
= read(statp
->_vcsock
, (char *)cp
, (int)len
)) > 0){
793 while (len
!= 0 && (n
= _read(statp
->_vcsock
, (char *)cp
, (int)len
)) > 0){
800 Perror(statp
, stderr
, "read(vc)", errno
);
806 * Flush rest of answer so connection stays in synch.
809 len
= resplen
- anssiz
;
814 n
= read(statp
->_vcsock
, junk
,
816 n
= _read(statp
->_vcsock
, junk
,
818 (len
> sizeof junk
) ? sizeof junk
: len
);
826 * If the calling applicating has bailed out of
827 * a previous call and failed to arrange to have
828 * the circuit closed or the server has got
829 * itself confused, then drop the packet and
830 * wait for the correct one.
832 if (hp
->id
!= anhp
->id
) {
833 DprintQ((statp
->options
& RES_DEBUG
) ||
834 (statp
->pfcode
& RES_PRF_REPLY
),
835 (stdout
, ";; old answer (unexpected):\n"),
836 ans
, (resplen
> anssiz
) ? anssiz
: resplen
);
841 * All is well, or the error is fatal. Signal that the
842 * next nameserver ought not be tried.
848 send_dg(res_state statp
,
852 const u_char
*buf
, int buflen
, u_char
*ans
,
853 int anssiz
, int *terrno
, int ns
, int tries
, int *v_circuit
,
856 const HEADER
*hp
= (const HEADER
*) buf
;
857 HEADER
*anhp
= (HEADER
*) ans
;
858 const struct sockaddr
*nsap
;
860 struct timespec now
, timeout
, finish
;
861 struct sockaddr_storage from
;
862 ISC_SOCKLEN_T fromlen
;
863 int resplen
, seconds
, n
, s
;
869 struct pollfd pollfd
;
875 nsap
= get_nsaddr(statp
, ns
);
876 nsaplen
= get_salen(nsap
);
877 if (EXT(statp
).nssocks
[ns
] == -1) {
879 EXT(statp
).nssocks
[ns
] = socket(nsap
->sa_family
, SOCK_DGRAM
, 0);
881 EXT(statp
).nssocks
[ns
] = _socket(nsap
->sa_family
, SOCK_DGRAM
, 0);
883 #if !defined(USE_POLL) && !defined(USE_KQUEUE)
884 if (EXT(statp
).nssocks
[ns
] > highestFD
) {
889 if (EXT(statp
).nssocks
[ns
] < 0) {
891 case EPROTONOSUPPORT
:
896 Perror(statp
, stderr
, "socket(dg)", errno
);
900 Perror(statp
, stderr
, "socket(dg)", errno
);
904 #ifndef CANNOT_CONNECT_DGRAM
906 * On a 4.3BSD+ machine (client and server,
907 * actually), sending to a nameserver datagram
908 * port with no nameserver will cause an
909 * ICMP port unreachable message to be returned.
910 * If our datagram socket is "connected" to the
911 * server, we get an ECONNREFUSED error on the next
912 * socket operation, and select returns if the
913 * error message is received. We can thus detect
914 * the absence of a nameserver without timing out.
917 * When the option "insecure1" is specified, we'd
918 * rather expect to see responses from an "unknown"
919 * address. In order to let the kernel accept such
920 * responses, do not connect the socket here.
921 * XXX: or do we need an explicit option to disable
924 if (!(statp
->options
& RES_INSECURE1
) &&
926 connect(EXT(statp
).nssocks
[ns
], nsap
, nsaplen
) < 0) {
928 _connect(EXT(statp
).nssocks
[ns
], nsap
, nsaplen
) < 0) {
930 Aerror(statp
, stderr
, "connect(dg)", errno
, nsap
,
935 #endif /* !CANNOT_CONNECT_DGRAM */
936 Dprint(statp
->options
& RES_DEBUG
,
937 (stdout
, ";; new DG socket\n"))
939 s
= EXT(statp
).nssocks
[ns
];
940 #ifndef CANNOT_CONNECT_DGRAM
941 if (statp
->options
& RES_INSECURE1
) {
947 (const char*)buf
, buflen
, 0, nsap
, nsaplen
) != buflen
) {
948 Aerror(statp
, stderr
, "sendto", errno
, nsap
, nsaplen
);
952 } else if (send(s
, (const char*)buf
, buflen
, 0) != buflen
) {
953 Perror(statp
, stderr
, "send", errno
);
957 #else /* !CANNOT_CONNECT_DGRAM */
959 if (sendto(s
, (const char*)buf
, buflen
, 0, nsap
, nsaplen
) != buflen
)
961 if (_sendto(s
, (const char*)buf
, buflen
, 0, nsap
, nsaplen
) != buflen
)
964 Aerror(statp
, stderr
, "sendto", errno
, nsap
, nsaplen
);
968 #endif /* !CANNOT_CONNECT_DGRAM */
973 seconds
= (statp
->retrans
<< tries
);
975 seconds
/= statp
->nscount
;
979 timeout
= evConsTime(seconds
, 0);
980 finish
= evAddTime(now
, timeout
);
986 if (evCmpTime(finish
, now
) > 0)
987 timeout
= evSubTime(finish
, now
);
989 timeout
= evConsTime(0, 0);
991 EV_SET(&kv
, s
, EVFILT_READ
, EV_ADD
| EV_ONESHOT
, 0, 0, 0);
992 n
= _kevent(kq
, &kv
, 1, &kv
, 1, &timeout
);
996 n
= pselect(s
+ 1, &dsmask
, NULL
, NULL
, &timeout
, NULL
);
999 timeout
= evSubTime(finish
, now
);
1000 if (timeout
.tv_sec
< 0)
1001 timeout
= evConsTime(0, 0);
1002 polltimeout
= 1000*timeout
.tv_sec
+
1003 timeout
.tv_nsec
/1000000;
1005 pollfd
.events
= POLLRDNORM
;
1006 n
= poll(&pollfd
, 1, polltimeout
);
1007 #endif /* USE_POLL */
1010 Dprint(statp
->options
& RES_DEBUG
, (stdout
, ";; timeout\n"));
1018 Perror(statp
, stderr
, "kevent", errno
);
1021 Perror(statp
, stderr
, "select", errno
);
1023 Perror(statp
, stderr
, "poll", errno
);
1024 #endif /* USE_POLL */
1034 fromlen
= sizeof(from
);
1036 resplen
= recvfrom(s
, (char*)ans
, anssiz
,0,
1038 resplen
= _recvfrom(s
, (char*)ans
, anssiz
,0,
1040 (struct sockaddr
*)&from
, &fromlen
);
1042 Perror(statp
, stderr
, "recvfrom", errno
);
1047 if (resplen
< HFIXEDSZ
) {
1049 * Undersized message.
1051 Dprint(statp
->options
& RES_DEBUG
,
1052 (stdout
, ";; undersized: %d\n",
1058 if (hp
->id
!= anhp
->id
) {
1060 * response from old query, ignore it.
1061 * XXX - potential security hazard could
1064 DprintQ((statp
->options
& RES_DEBUG
) ||
1065 (statp
->pfcode
& RES_PRF_REPLY
),
1066 (stdout
, ";; old answer:\n"),
1067 ans
, (resplen
> anssiz
) ? anssiz
: resplen
);
1070 if (!(statp
->options
& RES_INSECURE1
) &&
1071 !res_ourserver_p(statp
, (struct sockaddr
*)&from
)) {
1073 * response from wrong server? ignore it.
1074 * XXX - potential security hazard could
1077 DprintQ((statp
->options
& RES_DEBUG
) ||
1078 (statp
->pfcode
& RES_PRF_REPLY
),
1079 (stdout
, ";; not our server:\n"),
1080 ans
, (resplen
> anssiz
) ? anssiz
: resplen
);
1083 #ifdef RES_USE_EDNS0
1084 if (anhp
->rcode
== FORMERR
&& (statp
->options
& RES_USE_EDNS0
) != 0U) {
1086 * Do not retry if the server do not understand EDNS0.
1087 * The case has to be captured here, as FORMERR packet do not
1088 * carry query section, hence res_queriesmatch() returns 0.
1090 DprintQ(statp
->options
& RES_DEBUG
,
1091 (stdout
, "server rejected query with EDNS0:\n"),
1092 ans
, (resplen
> anssiz
) ? anssiz
: resplen
);
1093 /* record the error */
1094 statp
->_flags
|= RES_F_EDNS0ERR
;
1099 if (!(statp
->options
& RES_INSECURE2
) &&
1100 !res_queriesmatch(buf
, buf
+ buflen
,
1101 ans
, ans
+ anssiz
)) {
1103 * response contains wrong query? ignore it.
1104 * XXX - potential security hazard could
1107 DprintQ((statp
->options
& RES_DEBUG
) ||
1108 (statp
->pfcode
& RES_PRF_REPLY
),
1109 (stdout
, ";; wrong query name:\n"),
1110 ans
, (resplen
> anssiz
) ? anssiz
: resplen
);
1113 if (anhp
->rcode
== SERVFAIL
||
1114 anhp
->rcode
== NOTIMP
||
1115 anhp
->rcode
== REFUSED
) {
1116 DprintQ(statp
->options
& RES_DEBUG
,
1117 (stdout
, "server rejected query:\n"),
1118 ans
, (resplen
> anssiz
) ? anssiz
: resplen
);
1120 /* don't retry if called from dig */
1124 if (!(statp
->options
& RES_IGNTC
) && anhp
->tc
) {
1126 * To get the rest of answer,
1127 * use TCP with same server.
1129 Dprint(statp
->options
& RES_DEBUG
,
1130 (stdout
, ";; truncated answer\n"));
1136 * All is well, or the error is fatal. Signal that the
1137 * next nameserver ought not be tried.
1143 Aerror(const res_state statp
, FILE *file
, const char *string
, int error
,
1144 const struct sockaddr
*address
, int alen
)
1147 char hbuf
[NI_MAXHOST
];
1148 char sbuf
[NI_MAXSERV
];
1152 if ((statp
->options
& RES_DEBUG
) != 0U) {
1153 if (getnameinfo(address
, alen
, hbuf
, sizeof(hbuf
),
1154 sbuf
, sizeof(sbuf
), niflags
)) {
1155 strncpy(hbuf
, "?", sizeof(hbuf
) - 1);
1156 hbuf
[sizeof(hbuf
) - 1] = '\0';
1157 strncpy(sbuf
, "?", sizeof(sbuf
) - 1);
1158 sbuf
[sizeof(sbuf
) - 1] = '\0';
1160 fprintf(file
, "res_send: %s ([%s].%s): %s\n",
1161 string
, hbuf
, sbuf
, strerror(error
));
1167 Perror(const res_state statp
, FILE *file
, const char *string
, int error
) {
1170 if ((statp
->options
& RES_DEBUG
) != 0U)
1171 fprintf(file
, "res_send: %s: %s\n",
1172 string
, strerror(error
));
1177 sock_eq(struct sockaddr
*a
, struct sockaddr
*b
) {
1178 struct sockaddr_in
*a4
, *b4
;
1179 struct sockaddr_in6
*a6
, *b6
;
1181 if (a
->sa_family
!= b
->sa_family
)
1183 switch (a
->sa_family
) {
1185 a4
= (struct sockaddr_in
*)a
;
1186 b4
= (struct sockaddr_in
*)b
;
1187 return a4
->sin_port
== b4
->sin_port
&&
1188 a4
->sin_addr
.s_addr
== b4
->sin_addr
.s_addr
;
1190 a6
= (struct sockaddr_in6
*)a
;
1191 b6
= (struct sockaddr_in6
*)b
;
1192 return a6
->sin6_port
== b6
->sin6_port
&&
1193 #ifdef HAVE_SIN6_SCOPE_ID
1194 a6
->sin6_scope_id
== b6
->sin6_scope_id
&&
1196 IN6_ARE_ADDR_EQUAL(&a6
->sin6_addr
, &b6
->sin6_addr
);
1202 #if defined(NEED_PSELECT) && !defined(USE_POLL) && !defined(USE_KQUEUE)
1203 /* XXX needs to move to the porting library. */
1205 pselect(int nfds
, void *rfds
, void *wfds
, void *efds
,
1206 struct timespec
*tsp
, const sigset_t
*sigmask
)
1208 struct timeval tv
, *tvp
;
1214 tv
= evTimeVal(*tsp
);
1218 sigprocmask(SIG_SETMASK
, sigmask
, &sigs
);
1219 n
= select(nfds
, rfds
, wfds
, efds
, tvp
);
1221 sigprocmask(SIG_SETMASK
, &sigs
, NULL
);
1223 *tsp
= evTimeSpec(tv
);