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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
37 * Permission to use, copy, modify, and distribute this software for any
38 * purpose with or without fee is hereby granted, provided that the above
39 * copyright notice and this permission notice appear in all copies, and that
40 * the name of Digital Equipment Corporation not be used in advertising or
41 * publicity pertaining to distribution of the document or software without
42 * specific, written prior permission.
44 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
45 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
46 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
47 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
48 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
49 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
50 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
55 * Copyright (c) 2005 by Internet Systems Consortium, Inc. ("ISC")
56 * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
58 * Permission to use, copy, modify, and distribute this software for any
59 * purpose with or without fee is hereby granted, provided that the above
60 * copyright notice and this permission notice appear in all copies.
62 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
63 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
64 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
65 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
66 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
67 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
68 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
71 #if defined(LIBC_SCCS) && !defined(lint)
72 static const char sccsid
[] = "@(#)res_send.c 8.1 (Berkeley) 6/4/93";
73 static const char rcsid
[] = "$Id: res_send.c,v 1.18.10.1 2008/01/27 02:06:46 marka Exp $";
74 #endif /* LIBC_SCCS and not lint */
78 * Send query to name server and wait for reply.
81 #include "port_before.h"
83 #include "fd_setsize.h"
87 #include "namespace.h"
89 #include <sys/types.h>
90 #include <sys/param.h>
92 #include <sys/socket.h>
95 #include <netinet/in.h>
96 #include <arpa/nameser.h>
97 #include <arpa/inet.h>
108 #include "isc/eventlib.h"
110 #include "port_after.h"
112 #include <sys/event.h>
116 #ifdef HAVE_STROPTS_H
120 #endif /* USE_POLL */
124 #include "un-namespace.h"
127 /* Options. Leave them on. */
129 #include "res_debug.h"
130 #include "res_private.h"
132 #define EXT(res) ((res)->_u._ext)
134 #if !defined(USE_POLL) && !defined(USE_KQUEUE)
135 static const int highestFD
= FD_SETSIZE
- 1;
137 static int highestFD
= 0;
142 static int get_salen
__P((const struct sockaddr
*));
143 static struct sockaddr
* get_nsaddr
__P((res_state
, size_t));
144 static int send_vc(res_state
, const u_char
*, int,
145 u_char
*, int, int *, int);
146 static int send_dg(res_state
,
151 u_char
*, int, int *, int, int,
153 static void Aerror(const res_state
, FILE *, const char *, int,
154 const struct sockaddr
*, int);
155 static void Perror(const res_state
, FILE *, const char *, int);
156 static int sock_eq(struct sockaddr
*, struct sockaddr
*);
157 #if defined(NEED_PSELECT) && !defined(USE_POLL) && !defined(USE_KQUEUE)
158 static int pselect(int, void *, void *, void *,
162 void res_pquery(const res_state
, const u_char
*, int, FILE *);
164 static const int niflags
= NI_NUMERICHOST
| NI_NUMERICSERV
;
169 * looks up "ina" in _res.ns_addr_list[]
176 *\li paul vixie, 29may94
179 res_ourserver_p(const res_state statp
, const struct sockaddr
*sa
) {
180 const struct sockaddr_in
*inp
, *srv
;
181 const struct sockaddr_in6
*in6p
, *srv6
;
184 switch (sa
->sa_family
) {
186 inp
= (const struct sockaddr_in
*)sa
;
187 for (ns
= 0; ns
< statp
->nscount
; ns
++) {
188 srv
= (struct sockaddr_in
*)get_nsaddr(statp
, ns
);
189 if (srv
->sin_family
== inp
->sin_family
&&
190 srv
->sin_port
== inp
->sin_port
&&
191 (srv
->sin_addr
.s_addr
== INADDR_ANY
||
192 srv
->sin_addr
.s_addr
== inp
->sin_addr
.s_addr
))
197 if (EXT(statp
).ext
== NULL
)
199 in6p
= (const struct sockaddr_in6
*)sa
;
200 for (ns
= 0; ns
< statp
->nscount
; ns
++) {
201 srv6
= (struct sockaddr_in6
*)get_nsaddr(statp
, ns
);
202 if (srv6
->sin6_family
== in6p
->sin6_family
&&
203 srv6
->sin6_port
== in6p
->sin6_port
&&
204 #ifdef HAVE_SIN6_SCOPE_ID
205 (srv6
->sin6_scope_id
== 0 ||
206 srv6
->sin6_scope_id
== in6p
->sin6_scope_id
) &&
208 (IN6_IS_ADDR_UNSPECIFIED(&srv6
->sin6_addr
) ||
209 IN6_ARE_ADDR_EQUAL(&srv6
->sin6_addr
, &in6p
->sin6_addr
)))
220 * look for (name,type,class) in the query section of packet (buf,eom)
223 *\li buf + HFIXEDSZ <= eom
226 *\li -1 : format error
231 *\li paul vixie, 29may94
234 res_nameinquery(const char *name
, int type
, int class,
235 const u_char
*buf
, const u_char
*eom
)
237 const u_char
*cp
= buf
+ HFIXEDSZ
;
238 int qdcount
= ntohs(((const HEADER
*)buf
)->qdcount
);
240 while (qdcount
-- > 0) {
241 char tname
[MAXDNAME
+1];
242 int n
, ttype
, tclass
;
244 n
= dn_expand(buf
, eom
, cp
, tname
, sizeof tname
);
248 if (cp
+ 2 * INT16SZ
> eom
)
250 ttype
= ns_get16(cp
); cp
+= INT16SZ
;
251 tclass
= ns_get16(cp
); cp
+= INT16SZ
;
252 if (ttype
== type
&& tclass
== class &&
253 ns_samename(tname
, name
) == 1)
260 * is there a 1:1 mapping of (name,type,class)
261 * in (buf1,eom1) and (buf2,eom2)?
264 *\li -1 : format error
265 *\li 0 : not a 1:1 mapping
266 *\li >0 : is a 1:1 mapping
269 *\li paul vixie, 29may94
272 res_queriesmatch(const u_char
*buf1
, const u_char
*eom1
,
273 const u_char
*buf2
, const u_char
*eom2
)
275 const u_char
*cp
= buf1
+ HFIXEDSZ
;
276 int qdcount
= ntohs(((const HEADER
*)buf1
)->qdcount
);
278 if (buf1
+ HFIXEDSZ
> eom1
|| buf2
+ HFIXEDSZ
> eom2
)
282 * Only header section present in replies to
283 * dynamic update packets.
285 if ((((const HEADER
*)buf1
)->opcode
== ns_o_update
) &&
286 (((const HEADER
*)buf2
)->opcode
== ns_o_update
))
289 if (qdcount
!= ntohs(((const HEADER
*)buf2
)->qdcount
))
291 while (qdcount
-- > 0) {
292 char tname
[MAXDNAME
+1];
293 int n
, ttype
, tclass
;
295 n
= dn_expand(buf1
, eom1
, cp
, tname
, sizeof tname
);
299 if (cp
+ 2 * INT16SZ
> eom1
)
301 ttype
= ns_get16(cp
); cp
+= INT16SZ
;
302 tclass
= ns_get16(cp
); cp
+= INT16SZ
;
303 if (!res_nameinquery(tname
, ttype
, tclass
, buf2
, eom2
))
310 res_nsend(res_state statp
,
311 const u_char
*buf
, int buflen
, u_char
*ans
, int anssiz
)
313 int gotsomewhere
, terrno
, tries
, v_circuit
, resplen
, ns
, n
;
317 char abuf
[NI_MAXHOST
];
320 highestFD
= sysconf(_SC_OPEN_MAX
) - 1;
323 /* No name servers or res_init() failure */
324 if (statp
->nscount
== 0 || EXT(statp
).ext
== NULL
) {
328 if (anssiz
< HFIXEDSZ
) {
332 DprintQ((statp
->options
& RES_DEBUG
) || (statp
->pfcode
& RES_PRF_QUERY
),
333 (stdout
, ";; res_send()\n"), buf
, buflen
);
334 v_circuit
= (statp
->options
& RES_USEVC
) || buflen
> PACKETSZ
;
339 if ((kq
= kqueue()) < 0) {
340 Perror(statp
, stderr
, "kqueue", errno
);
346 * If the ns_addr_list in the resolver context has changed, then
347 * invalidate our cached copy and the associated timing data.
349 if (EXT(statp
).nscount
!= 0) {
351 struct sockaddr_storage peer
;
352 ISC_SOCKLEN_T peerlen
;
354 if (EXT(statp
).nscount
!= statp
->nscount
)
357 for (ns
= 0; ns
< statp
->nscount
; ns
++) {
358 if (statp
->nsaddr_list
[ns
].sin_family
&&
359 !sock_eq((struct sockaddr
*)&statp
->nsaddr_list
[ns
],
360 (struct sockaddr
*)&EXT(statp
).ext
->nsaddrs
[ns
])) {
365 if (EXT(statp
).nssocks
[ns
] == -1)
367 peerlen
= sizeof(peer
);
369 if (getsockname(EXT(statp
).nssocks
[ns
],
371 if (_getsockname(EXT(statp
).nssocks
[ns
],
373 (struct sockaddr
*)&peer
, &peerlen
) < 0) {
377 if (!sock_eq((struct sockaddr
*)&peer
,
378 get_nsaddr(statp
, ns
))) {
385 EXT(statp
).nscount
= 0;
390 * Maybe initialize our private copy of the ns_addr_list.
392 if (EXT(statp
).nscount
== 0) {
393 for (ns
= 0; ns
< statp
->nscount
; ns
++) {
394 EXT(statp
).nstimes
[ns
] = RES_MAXTIME
;
395 EXT(statp
).nssocks
[ns
] = -1;
396 if (!statp
->nsaddr_list
[ns
].sin_family
)
398 EXT(statp
).ext
->nsaddrs
[ns
].sin
=
399 statp
->nsaddr_list
[ns
];
401 EXT(statp
).nscount
= statp
->nscount
;
405 * Some resolvers want to even out the load on their nameservers.
406 * Note that RES_BLAST overrides RES_ROTATE.
408 if ((statp
->options
& RES_ROTATE
) != 0U &&
409 (statp
->options
& RES_BLAST
) == 0U) {
410 union res_sockaddr_union inu
;
411 struct sockaddr_in ina
;
412 int lastns
= statp
->nscount
- 1;
416 if (EXT(statp
).ext
!= NULL
)
417 inu
= EXT(statp
).ext
->nsaddrs
[0];
418 ina
= statp
->nsaddr_list
[0];
419 fd
= EXT(statp
).nssocks
[0];
420 nstime
= EXT(statp
).nstimes
[0];
421 for (ns
= 0; ns
< lastns
; ns
++) {
422 if (EXT(statp
).ext
!= NULL
)
423 EXT(statp
).ext
->nsaddrs
[ns
] =
424 EXT(statp
).ext
->nsaddrs
[ns
+ 1];
425 statp
->nsaddr_list
[ns
] = statp
->nsaddr_list
[ns
+ 1];
426 EXT(statp
).nssocks
[ns
] = EXT(statp
).nssocks
[ns
+ 1];
427 EXT(statp
).nstimes
[ns
] = EXT(statp
).nstimes
[ns
+ 1];
429 if (EXT(statp
).ext
!= NULL
)
430 EXT(statp
).ext
->nsaddrs
[lastns
] = inu
;
431 statp
->nsaddr_list
[lastns
] = ina
;
432 EXT(statp
).nssocks
[lastns
] = fd
;
433 EXT(statp
).nstimes
[lastns
] = nstime
;
437 * Send request, RETRY times, or until successful.
439 for (tries
= 0; tries
< statp
->retry
; tries
++) {
440 for (ns
= 0; ns
< statp
->nscount
; ns
++) {
441 struct sockaddr
*nsap
;
443 nsap
= get_nsaddr(statp
, ns
);
444 nsaplen
= get_salen(nsap
);
445 statp
->_flags
&= ~RES_F_LASTMASK
;
446 statp
->_flags
|= (ns
<< RES_F_LASTSHIFT
);
449 int done
= 0, loops
= 0;
454 act
= (*statp
->qhook
)(&nsap
, &buf
, &buflen
,
455 ans
, anssiz
, &resplen
);
469 /* give the hook another try */
470 if (++loops
< 42) /*doug adams*/
481 Dprint(((statp
->options
& RES_DEBUG
) &&
482 getnameinfo(nsap
, nsaplen
, abuf
, sizeof(abuf
),
483 NULL
, 0, niflags
) == 0),
484 (stdout
, ";; Querying server (# %d) address = %s\n",
489 /* Use VC; at most one attempt per server. */
490 tries
= statp
->retry
;
491 n
= send_vc(statp
, buf
, buflen
, ans
, anssiz
, &terrno
,
504 buf
, buflen
, ans
, anssiz
, &terrno
,
505 ns
, tries
, &v_circuit
, &gotsomewhere
);
515 Dprint((statp
->options
& RES_DEBUG
) ||
516 ((statp
->pfcode
& RES_PRF_REPLY
) &&
517 (statp
->pfcode
& RES_PRF_HEAD1
)),
518 (stdout
, ";; got answer:\n"));
520 DprintQ((statp
->options
& RES_DEBUG
) ||
521 (statp
->pfcode
& RES_PRF_REPLY
),
523 ans
, (resplen
> anssiz
) ? anssiz
: resplen
);
526 * If we have temporarily opened a virtual circuit,
527 * or if we haven't been asked to keep a socket open,
530 if ((v_circuit
&& (statp
->options
& RES_USEVC
) == 0U) ||
531 (statp
->options
& RES_STAYOPEN
) == 0U) {
535 int done
= 0, loops
= 0;
540 act
= (*statp
->rhook
)(nsap
, buf
, buflen
,
541 ans
, anssiz
, &resplen
);
551 /* give the hook another try */
552 if (++loops
< 42) /*doug adams*/
576 errno
= ECONNREFUSED
; /*%< no nameservers found */
578 errno
= ETIMEDOUT
; /*%< no answer obtained */
591 const struct sockaddr
*sa
;
595 /* There are people do not set sa_len. Be forgiving to them. */
600 if (sa
->sa_family
== AF_INET
)
601 return (sizeof(struct sockaddr_in
));
602 else if (sa
->sa_family
== AF_INET6
)
603 return (sizeof(struct sockaddr_in6
));
605 return (0); /*%< unknown, die on connect */
609 * pick appropriate nsaddr_list for use. see res_init() for initialization.
611 static struct sockaddr
*
617 if (!statp
->nsaddr_list
[n
].sin_family
&& EXT(statp
).ext
) {
619 * - EXT(statp).ext->nsaddrs[n] holds an address that is larger
620 * than struct sockaddr, and
621 * - user code did not update statp->nsaddr_list[n].
623 return (struct sockaddr
*)(void *)&EXT(statp
).ext
->nsaddrs
[n
];
626 * - user code updated statp->nsaddr_list[n], or
627 * - statp->nsaddr_list[n] has the same content as
628 * EXT(statp).ext->nsaddrs[n].
630 return (struct sockaddr
*)(void *)&statp
->nsaddr_list
[n
];
635 send_vc(res_state statp
,
636 const u_char
*buf
, int buflen
, u_char
*ans
, int anssiz
,
639 const HEADER
*hp
= (const HEADER
*) buf
;
640 HEADER
*anhp
= (HEADER
*) ans
;
641 struct sockaddr
*nsap
;
643 int truncating
, connreset
, resplen
, n
;
652 nsap
= get_nsaddr(statp
, ns
);
653 nsaplen
= get_salen(nsap
);
659 /* Are we still talking to whom we want to talk to? */
660 if (statp
->_vcsock
>= 0 && (statp
->_flags
& RES_F_VC
) != 0) {
661 struct sockaddr_storage peer
;
662 ISC_SOCKLEN_T size
= sizeof peer
;
664 if (getpeername(statp
->_vcsock
,
666 if (_getpeername(statp
->_vcsock
,
668 (struct sockaddr
*)&peer
, &size
) < 0 ||
669 !sock_eq((struct sockaddr
*)&peer
, nsap
)) {
671 statp
->_flags
&= ~RES_F_VC
;
675 if (statp
->_vcsock
< 0 || (statp
->_flags
& RES_F_VC
) == 0) {
676 if (statp
->_vcsock
>= 0)
679 statp
->_vcsock
= socket(nsap
->sa_family
, SOCK_STREAM
, 0);
681 statp
->_vcsock
= _socket(nsap
->sa_family
, SOCK_STREAM
, 0);
683 #if !defined(USE_POLL) && !defined(USE_KQUEUE)
684 if (statp
->_vcsock
> highestFD
) {
689 if (statp
->_vcsock
< 0) {
691 case EPROTONOSUPPORT
:
696 Perror(statp
, stderr
, "socket(vc)", errno
);
700 Perror(statp
, stderr
, "socket(vc)", errno
);
706 * Disable generation of SIGPIPE when writing to a closed
707 * socket. Write should return -1 and set errno to EPIPE
710 * Push on even if setsockopt(SO_NOSIGPIPE) fails.
712 (void)setsockopt(statp
->_vcsock
, SOL_SOCKET
, SO_NOSIGPIPE
, &on
,
717 if (connect(statp
->_vcsock
, nsap
, nsaplen
) < 0) {
719 if (_connect(statp
->_vcsock
, nsap
, nsaplen
) < 0) {
722 Aerror(statp
, stderr
, "connect/vc", errno
, nsap
,
727 statp
->_flags
|= RES_F_VC
;
731 * Send length & message
733 ns_put16((u_short
)buflen
, (u_char
*)&len
);
734 iov
[0] = evConsIovec(&len
, INT16SZ
);
736 iov
[1] = evConsIovec(tmp
, buflen
);
738 if (writev(statp
->_vcsock
, iov
, 2) != (INT16SZ
+ buflen
)) {
740 if (_writev(statp
->_vcsock
, iov
, 2) != (INT16SZ
+ buflen
)) {
743 Perror(statp
, stderr
, "write failed", errno
);
748 * Receive length & response
754 while ((n
= read(statp
->_vcsock
, (char *)cp
, (int)len
)) > 0) {
756 while ((n
= _read(statp
->_vcsock
, (char *)cp
, (int)len
)) > 0) {
764 Perror(statp
, stderr
, "read failed", errno
);
767 * A long running process might get its TCP
768 * connection reset if the remote server was
769 * restarted. Requery the server instead of
770 * trying a new one. When there is only one
771 * server, this means that a query might work
772 * instead of failing. We only allow one reset
773 * per query to prevent looping.
775 if (*terrno
== ECONNRESET
&& !connreset
) {
783 resplen
= ns_get16(ans
);
784 if (resplen
> anssiz
) {
785 Dprint(statp
->options
& RES_DEBUG
,
786 (stdout
, ";; response truncated\n")
792 if (len
< HFIXEDSZ
) {
794 * Undersized message.
796 Dprint(statp
->options
& RES_DEBUG
,
797 (stdout
, ";; undersized: %d\n", len
));
804 while (len
!= 0 && (n
= read(statp
->_vcsock
, (char *)cp
, (int)len
)) > 0){
806 while (len
!= 0 && (n
= _read(statp
->_vcsock
, (char *)cp
, (int)len
)) > 0){
813 Perror(statp
, stderr
, "read(vc)", errno
);
819 * Flush rest of answer so connection stays in synch.
822 len
= resplen
- anssiz
;
827 n
= read(statp
->_vcsock
, junk
,
829 n
= _read(statp
->_vcsock
, junk
,
831 (len
> sizeof junk
) ? sizeof junk
: len
);
839 * If the calling applicating has bailed out of
840 * a previous call and failed to arrange to have
841 * the circuit closed or the server has got
842 * itself confused, then drop the packet and
843 * wait for the correct one.
845 if (hp
->id
!= anhp
->id
) {
846 DprintQ((statp
->options
& RES_DEBUG
) ||
847 (statp
->pfcode
& RES_PRF_REPLY
),
848 (stdout
, ";; old answer (unexpected):\n"),
849 ans
, (resplen
> anssiz
) ? anssiz
: resplen
);
854 * All is well, or the error is fatal. Signal that the
855 * next nameserver ought not be tried.
861 send_dg(res_state statp
,
865 const u_char
*buf
, int buflen
, u_char
*ans
,
866 int anssiz
, int *terrno
, int ns
, int tries
, int *v_circuit
,
869 const HEADER
*hp
= (const HEADER
*) buf
;
870 HEADER
*anhp
= (HEADER
*) ans
;
871 const struct sockaddr
*nsap
;
873 struct timespec now
, timeout
, finish
;
874 struct sockaddr_storage from
;
875 ISC_SOCKLEN_T fromlen
;
876 int resplen
, seconds
, n
, s
;
882 struct pollfd pollfd
;
888 nsap
= get_nsaddr(statp
, ns
);
889 nsaplen
= get_salen(nsap
);
890 if (EXT(statp
).nssocks
[ns
] == -1) {
892 EXT(statp
).nssocks
[ns
] = socket(nsap
->sa_family
, SOCK_DGRAM
, 0);
894 EXT(statp
).nssocks
[ns
] = _socket(nsap
->sa_family
, SOCK_DGRAM
, 0);
896 #if !defined(USE_POLL) && !defined(USE_KQUEUE)
897 if (EXT(statp
).nssocks
[ns
] > highestFD
) {
902 if (EXT(statp
).nssocks
[ns
] < 0) {
904 case EPROTONOSUPPORT
:
909 Perror(statp
, stderr
, "socket(dg)", errno
);
913 Perror(statp
, stderr
, "socket(dg)", errno
);
917 #ifndef CANNOT_CONNECT_DGRAM
919 * On a 4.3BSD+ machine (client and server,
920 * actually), sending to a nameserver datagram
921 * port with no nameserver will cause an
922 * ICMP port unreachable message to be returned.
923 * If our datagram socket is "connected" to the
924 * server, we get an ECONNREFUSED error on the next
925 * socket operation, and select returns if the
926 * error message is received. We can thus detect
927 * the absence of a nameserver without timing out.
930 * When the option "insecure1" is specified, we'd
931 * rather expect to see responses from an "unknown"
932 * address. In order to let the kernel accept such
933 * responses, do not connect the socket here.
934 * XXX: or do we need an explicit option to disable
937 if (!(statp
->options
& RES_INSECURE1
) &&
939 connect(EXT(statp
).nssocks
[ns
], nsap
, nsaplen
) < 0) {
941 _connect(EXT(statp
).nssocks
[ns
], nsap
, nsaplen
) < 0) {
943 Aerror(statp
, stderr
, "connect(dg)", errno
, nsap
,
948 #endif /* !CANNOT_CONNECT_DGRAM */
949 Dprint(statp
->options
& RES_DEBUG
,
950 (stdout
, ";; new DG socket\n"))
952 s
= EXT(statp
).nssocks
[ns
];
953 #ifndef CANNOT_CONNECT_DGRAM
954 if (statp
->options
& RES_INSECURE1
) {
960 (const char*)buf
, buflen
, 0, nsap
, nsaplen
) != buflen
) {
961 Aerror(statp
, stderr
, "sendto", errno
, nsap
, nsaplen
);
965 } else if (send(s
, (const char*)buf
, buflen
, 0) != buflen
) {
966 Perror(statp
, stderr
, "send", errno
);
970 #else /* !CANNOT_CONNECT_DGRAM */
972 if (sendto(s
, (const char*)buf
, buflen
, 0, nsap
, nsaplen
) != buflen
)
974 if (_sendto(s
, (const char*)buf
, buflen
, 0, nsap
, nsaplen
) != buflen
)
977 Aerror(statp
, stderr
, "sendto", errno
, nsap
, nsaplen
);
981 #endif /* !CANNOT_CONNECT_DGRAM */
986 seconds
= (statp
->retrans
<< tries
);
988 seconds
/= statp
->nscount
;
992 timeout
= evConsTime(seconds
, 0);
993 finish
= evAddTime(now
, timeout
);
999 if (evCmpTime(finish
, now
) > 0)
1000 timeout
= evSubTime(finish
, now
);
1002 timeout
= evConsTime(0, 0);
1004 EV_SET(&kv
, s
, EVFILT_READ
, EV_ADD
| EV_ONESHOT
, 0, 0, 0);
1005 n
= _kevent(kq
, &kv
, 1, &kv
, 1, &timeout
);
1009 n
= pselect(s
+ 1, &dsmask
, NULL
, NULL
, &timeout
, NULL
);
1012 timeout
= evSubTime(finish
, now
);
1013 if (timeout
.tv_sec
< 0)
1014 timeout
= evConsTime(0, 0);
1015 polltimeout
= 1000*timeout
.tv_sec
+
1016 timeout
.tv_nsec
/1000000;
1018 pollfd
.events
= POLLRDNORM
;
1019 n
= poll(&pollfd
, 1, polltimeout
);
1020 #endif /* USE_POLL */
1023 Dprint(statp
->options
& RES_DEBUG
, (stdout
, ";; timeout\n"));
1031 Perror(statp
, stderr
, "kevent", errno
);
1034 Perror(statp
, stderr
, "select", errno
);
1036 Perror(statp
, stderr
, "poll", errno
);
1037 #endif /* USE_POLL */
1047 fromlen
= sizeof(from
);
1049 resplen
= recvfrom(s
, (char*)ans
, anssiz
,0,
1051 resplen
= _recvfrom(s
, (char*)ans
, anssiz
,0,
1053 (struct sockaddr
*)&from
, &fromlen
);
1055 Perror(statp
, stderr
, "recvfrom", errno
);
1060 if (resplen
< HFIXEDSZ
) {
1062 * Undersized message.
1064 Dprint(statp
->options
& RES_DEBUG
,
1065 (stdout
, ";; undersized: %d\n",
1071 if (hp
->id
!= anhp
->id
) {
1073 * response from old query, ignore it.
1074 * XXX - potential security hazard could
1077 DprintQ((statp
->options
& RES_DEBUG
) ||
1078 (statp
->pfcode
& RES_PRF_REPLY
),
1079 (stdout
, ";; old answer:\n"),
1080 ans
, (resplen
> anssiz
) ? anssiz
: resplen
);
1083 if (!(statp
->options
& RES_INSECURE1
) &&
1084 !res_ourserver_p(statp
, (struct sockaddr
*)&from
)) {
1086 * response from wrong server? ignore it.
1087 * XXX - potential security hazard could
1090 DprintQ((statp
->options
& RES_DEBUG
) ||
1091 (statp
->pfcode
& RES_PRF_REPLY
),
1092 (stdout
, ";; not our server:\n"),
1093 ans
, (resplen
> anssiz
) ? anssiz
: resplen
);
1096 #ifdef RES_USE_EDNS0
1097 if (anhp
->rcode
== FORMERR
&& (statp
->options
& RES_USE_EDNS0
) != 0U) {
1099 * Do not retry if the server do not understand EDNS0.
1100 * The case has to be captured here, as FORMERR packet do not
1101 * carry query section, hence res_queriesmatch() returns 0.
1103 DprintQ(statp
->options
& RES_DEBUG
,
1104 (stdout
, "server rejected query with EDNS0:\n"),
1105 ans
, (resplen
> anssiz
) ? anssiz
: resplen
);
1106 /* record the error */
1107 statp
->_flags
|= RES_F_EDNS0ERR
;
1112 if (!(statp
->options
& RES_INSECURE2
) &&
1113 !res_queriesmatch(buf
, buf
+ buflen
,
1114 ans
, ans
+ anssiz
)) {
1116 * response contains wrong query? ignore it.
1117 * XXX - potential security hazard could
1120 DprintQ((statp
->options
& RES_DEBUG
) ||
1121 (statp
->pfcode
& RES_PRF_REPLY
),
1122 (stdout
, ";; wrong query name:\n"),
1123 ans
, (resplen
> anssiz
) ? anssiz
: resplen
);
1126 if (anhp
->rcode
== SERVFAIL
||
1127 anhp
->rcode
== NOTIMP
||
1128 anhp
->rcode
== REFUSED
) {
1129 DprintQ(statp
->options
& RES_DEBUG
,
1130 (stdout
, "server rejected query:\n"),
1131 ans
, (resplen
> anssiz
) ? anssiz
: resplen
);
1133 /* don't retry if called from dig */
1137 if (!(statp
->options
& RES_IGNTC
) && anhp
->tc
) {
1139 * To get the rest of answer,
1140 * use TCP with same server.
1142 Dprint(statp
->options
& RES_DEBUG
,
1143 (stdout
, ";; truncated answer\n"));
1149 * All is well, or the error is fatal. Signal that the
1150 * next nameserver ought not be tried.
1156 Aerror(const res_state statp
, FILE *file
, const char *string
, int error
,
1157 const struct sockaddr
*address
, int alen
)
1160 char hbuf
[NI_MAXHOST
];
1161 char sbuf
[NI_MAXSERV
];
1165 if ((statp
->options
& RES_DEBUG
) != 0U) {
1166 if (getnameinfo(address
, alen
, hbuf
, sizeof(hbuf
),
1167 sbuf
, sizeof(sbuf
), niflags
)) {
1168 strncpy(hbuf
, "?", sizeof(hbuf
) - 1);
1169 hbuf
[sizeof(hbuf
) - 1] = '\0';
1170 strncpy(sbuf
, "?", sizeof(sbuf
) - 1);
1171 sbuf
[sizeof(sbuf
) - 1] = '\0';
1173 fprintf(file
, "res_send: %s ([%s].%s): %s\n",
1174 string
, hbuf
, sbuf
, strerror(error
));
1180 Perror(const res_state statp
, FILE *file
, const char *string
, int error
) {
1183 if ((statp
->options
& RES_DEBUG
) != 0U)
1184 fprintf(file
, "res_send: %s: %s\n",
1185 string
, strerror(error
));
1190 sock_eq(struct sockaddr
*a
, struct sockaddr
*b
) {
1191 struct sockaddr_in
*a4
, *b4
;
1192 struct sockaddr_in6
*a6
, *b6
;
1194 if (a
->sa_family
!= b
->sa_family
)
1196 switch (a
->sa_family
) {
1198 a4
= (struct sockaddr_in
*)a
;
1199 b4
= (struct sockaddr_in
*)b
;
1200 return a4
->sin_port
== b4
->sin_port
&&
1201 a4
->sin_addr
.s_addr
== b4
->sin_addr
.s_addr
;
1203 a6
= (struct sockaddr_in6
*)a
;
1204 b6
= (struct sockaddr_in6
*)b
;
1205 return a6
->sin6_port
== b6
->sin6_port
&&
1206 #ifdef HAVE_SIN6_SCOPE_ID
1207 a6
->sin6_scope_id
== b6
->sin6_scope_id
&&
1209 IN6_ARE_ADDR_EQUAL(&a6
->sin6_addr
, &b6
->sin6_addr
);
1215 #if defined(NEED_PSELECT) && !defined(USE_POLL) && !defined(USE_KQUEUE)
1216 /* XXX needs to move to the porting library. */
1218 pselect(int nfds
, void *rfds
, void *wfds
, void *efds
,
1219 struct timespec
*tsp
, const sigset_t
*sigmask
)
1221 struct timeval tv
, *tvp
;
1227 tv
= evTimeVal(*tsp
);
1231 sigprocmask(SIG_SETMASK
, sigmask
, &sigs
);
1232 n
= select(nfds
, rfds
, wfds
, efds
, tvp
);
1234 sigprocmask(SIG_SETMASK
, &sigs
, NULL
);
1236 *tsp
= evTimeSpec(tv
);