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 * 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.9.18.10 2008/01/27 02:06:26 marka Exp $";
70 #endif /* LIBC_SCCS and not lint */
71 #include <sys/cdefs.h>
72 __FBSDID("$FreeBSD$");
76 * Send query to name server and wait for reply.
79 #include "port_before.h"
81 #include "fd_setsize.h"
84 #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"
109 #include <sys/event.h>
112 #ifdef HAVE_STROPTS_H
116 #endif /* USE_POLL */
119 #include "un-namespace.h"
121 /* Options. Leave them on. */
123 #include "res_debug.h"
124 #include "res_private.h"
126 #define EXT(res) ((res)->_u._ext)
128 #if !defined(USE_POLL) && !defined(USE_KQUEUE)
129 static const int highestFD = FD_SETSIZE - 1;
134 static int get_salen(const struct sockaddr *);
135 static struct sockaddr * get_nsaddr(res_state, size_t);
136 static int send_vc(res_state, const u_char *, int,
137 u_char *, int, int *, int);
138 static int send_dg(res_state,
143 u_char *, int, int *, int, int,
145 static void Aerror(const res_state, FILE *, const char *, int,
146 const struct sockaddr *, int);
147 static void Perror(const res_state, FILE *, const char *, int);
148 static int sock_eq(struct sockaddr *, struct sockaddr *);
149 #if defined(NEED_PSELECT) && !defined(USE_POLL) && !defined(USE_KQUEUE)
150 static int pselect(int, void *, void *, void *,
154 void res_pquery(const res_state, const u_char *, int, FILE *);
156 static const int niflags = NI_NUMERICHOST | NI_NUMERICSERV;
161 * looks up "ina" in _res.ns_addr_list[]
168 *\li paul vixie, 29may94
171 res_ourserver_p(const res_state statp, const struct sockaddr *sa) {
172 const struct sockaddr_in *inp, *srv;
173 const struct sockaddr_in6 *in6p, *srv6;
176 switch (sa->sa_family) {
178 inp = (const struct sockaddr_in *)sa;
179 for (ns = 0; ns < statp->nscount; ns++) {
180 srv = (struct sockaddr_in *)get_nsaddr(statp, ns);
181 if (srv->sin_family == inp->sin_family &&
182 srv->sin_port == inp->sin_port &&
183 (srv->sin_addr.s_addr == INADDR_ANY ||
184 srv->sin_addr.s_addr == inp->sin_addr.s_addr))
189 if (EXT(statp).ext == NULL)
191 in6p = (const struct sockaddr_in6 *)sa;
192 for (ns = 0; ns < statp->nscount; ns++) {
193 srv6 = (struct sockaddr_in6 *)get_nsaddr(statp, ns);
194 if (srv6->sin6_family == in6p->sin6_family &&
195 srv6->sin6_port == in6p->sin6_port &&
196 #ifdef HAVE_SIN6_SCOPE_ID
197 (srv6->sin6_scope_id == 0 ||
198 srv6->sin6_scope_id == in6p->sin6_scope_id) &&
200 (IN6_IS_ADDR_UNSPECIFIED(&srv6->sin6_addr) ||
201 IN6_ARE_ADDR_EQUAL(&srv6->sin6_addr, &in6p->sin6_addr)))
212 * look for (name,type,class) in the query section of packet (buf,eom)
215 *\li buf + HFIXEDSZ <= eom
218 *\li -1 : format error
223 *\li paul vixie, 29may94
226 res_nameinquery(const char *name, int type, int class,
227 const u_char *buf, const u_char *eom)
229 const u_char *cp = buf + HFIXEDSZ;
230 int qdcount = ntohs(((const HEADER*)buf)->qdcount);
232 while (qdcount-- > 0) {
233 char tname[MAXDNAME+1];
234 int n, ttype, tclass;
236 n = dn_expand(buf, eom, cp, tname, sizeof tname);
240 if (cp + 2 * INT16SZ > eom)
242 ttype = ns_get16(cp); cp += INT16SZ;
243 tclass = ns_get16(cp); cp += INT16SZ;
244 if (ttype == type && tclass == class &&
245 ns_samename(tname, name) == 1)
252 * is there a 1:1 mapping of (name,type,class)
253 * in (buf1,eom1) and (buf2,eom2)?
256 *\li -1 : format error
257 *\li 0 : not a 1:1 mapping
258 *\li >0 : is a 1:1 mapping
261 *\li paul vixie, 29may94
264 res_queriesmatch(const u_char *buf1, const u_char *eom1,
265 const u_char *buf2, const u_char *eom2)
267 const u_char *cp = buf1 + HFIXEDSZ;
268 int qdcount = ntohs(((const HEADER*)buf1)->qdcount);
270 if (buf1 + HFIXEDSZ > eom1 || buf2 + HFIXEDSZ > eom2)
274 * Only header section present in replies to
275 * dynamic update packets.
277 if ((((const HEADER *)buf1)->opcode == ns_o_update) &&
278 (((const HEADER *)buf2)->opcode == ns_o_update))
281 if (qdcount != ntohs(((const HEADER*)buf2)->qdcount))
283 while (qdcount-- > 0) {
284 char tname[MAXDNAME+1];
285 int n, ttype, tclass;
287 n = dn_expand(buf1, eom1, cp, tname, sizeof tname);
291 if (cp + 2 * INT16SZ > eom1)
293 ttype = ns_get16(cp); cp += INT16SZ;
294 tclass = ns_get16(cp); cp += INT16SZ;
295 if (!res_nameinquery(tname, ttype, tclass, buf2, eom2))
302 res_nsend(res_state statp,
303 const u_char *buf, int buflen, u_char *ans, int anssiz)
305 int gotsomewhere, terrno, tries, v_circuit, resplen, ns, n;
309 char abuf[NI_MAXHOST];
311 /* No name servers or res_init() failure */
312 if (statp->nscount == 0 || EXT(statp).ext == NULL) {
316 if (anssiz < HFIXEDSZ) {
320 DprintQ((statp->options & RES_DEBUG) || (statp->pfcode & RES_PRF_QUERY),
321 (stdout, ";; res_send()\n"), buf, buflen);
322 v_circuit = (statp->options & RES_USEVC) || buflen > PACKETSZ;
327 if ((kq = kqueue()) < 0) {
328 Perror(statp, stderr, "kqueue", errno);
334 * If the ns_addr_list in the resolver context has changed, then
335 * invalidate our cached copy and the associated timing data.
337 if (EXT(statp).nscount != 0) {
339 struct sockaddr_storage peer;
340 ISC_SOCKLEN_T peerlen;
342 if (EXT(statp).nscount != statp->nscount)
345 for (ns = 0; ns < statp->nscount; ns++) {
346 if (statp->nsaddr_list[ns].sin_family &&
347 !sock_eq((struct sockaddr *)&statp->nsaddr_list[ns],
348 (struct sockaddr *)&EXT(statp).ext->nsaddrs[ns])) {
353 if (EXT(statp).nssocks[ns] == -1)
355 peerlen = sizeof(peer);
356 if (_getsockname(EXT(statp).nssocks[ns],
357 (struct sockaddr *)&peer, &peerlen) < 0) {
361 if (!sock_eq((struct sockaddr *)&peer,
362 get_nsaddr(statp, ns))) {
369 EXT(statp).nscount = 0;
374 * Maybe initialize our private copy of the ns_addr_list.
376 if (EXT(statp).nscount == 0) {
377 for (ns = 0; ns < statp->nscount; ns++) {
378 EXT(statp).nstimes[ns] = RES_MAXTIME;
379 EXT(statp).nssocks[ns] = -1;
380 if (!statp->nsaddr_list[ns].sin_family)
382 EXT(statp).ext->nsaddrs[ns].sin =
383 statp->nsaddr_list[ns];
385 EXT(statp).nscount = statp->nscount;
389 * Some resolvers want to even out the load on their nameservers.
390 * Note that RES_BLAST overrides RES_ROTATE.
392 if ((statp->options & RES_ROTATE) != 0U &&
393 (statp->options & RES_BLAST) == 0U) {
394 union res_sockaddr_union inu;
395 struct sockaddr_in ina;
396 int lastns = statp->nscount - 1;
400 if (EXT(statp).ext != NULL)
401 inu = EXT(statp).ext->nsaddrs[0];
402 ina = statp->nsaddr_list[0];
403 fd = EXT(statp).nssocks[0];
404 nstime = EXT(statp).nstimes[0];
405 for (ns = 0; ns < lastns; ns++) {
406 if (EXT(statp).ext != NULL)
407 EXT(statp).ext->nsaddrs[ns] =
408 EXT(statp).ext->nsaddrs[ns + 1];
409 statp->nsaddr_list[ns] = statp->nsaddr_list[ns + 1];
410 EXT(statp).nssocks[ns] = EXT(statp).nssocks[ns + 1];
411 EXT(statp).nstimes[ns] = EXT(statp).nstimes[ns + 1];
413 if (EXT(statp).ext != NULL)
414 EXT(statp).ext->nsaddrs[lastns] = inu;
415 statp->nsaddr_list[lastns] = ina;
416 EXT(statp).nssocks[lastns] = fd;
417 EXT(statp).nstimes[lastns] = nstime;
421 * Send request, RETRY times, or until successful.
423 for (tries = 0; tries < statp->retry; tries++) {
424 for (ns = 0; ns < statp->nscount; ns++) {
425 struct sockaddr *nsap;
427 nsap = get_nsaddr(statp, ns);
428 nsaplen = get_salen(nsap);
429 statp->_flags &= ~RES_F_LASTMASK;
430 statp->_flags |= (ns << RES_F_LASTSHIFT);
433 int done = 0, loops = 0;
438 act = (*statp->qhook)(&nsap, &buf, &buflen,
439 ans, anssiz, &resplen);
453 /* give the hook another try */
454 if (++loops < 42) /*doug adams*/
465 Dprint(((statp->options & RES_DEBUG) &&
466 getnameinfo(nsap, nsaplen, abuf, sizeof(abuf),
467 NULL, 0, niflags) == 0),
468 (stdout, ";; Querying server (# %d) address = %s\n",
473 /* Use VC; at most one attempt per server. */
474 tries = statp->retry;
475 n = send_vc(statp, buf, buflen, ans, anssiz, &terrno,
488 buf, buflen, ans, anssiz, &terrno,
489 ns, tries, &v_circuit, &gotsomewhere);
499 Dprint((statp->options & RES_DEBUG) ||
500 ((statp->pfcode & RES_PRF_REPLY) &&
501 (statp->pfcode & RES_PRF_HEAD1)),
502 (stdout, ";; got answer:\n"));
504 DprintQ((statp->options & RES_DEBUG) ||
505 (statp->pfcode & RES_PRF_REPLY),
507 ans, (resplen > anssiz) ? anssiz : resplen);
510 * If we have temporarily opened a virtual circuit,
511 * or if we haven't been asked to keep a socket open,
514 if ((v_circuit && (statp->options & RES_USEVC) == 0U) ||
515 (statp->options & RES_STAYOPEN) == 0U) {
519 int done = 0, loops = 0;
524 act = (*statp->rhook)(nsap, buf, buflen,
525 ans, anssiz, &resplen);
535 /* give the hook another try */
536 if (++loops < 42) /*doug adams*/
560 errno = ECONNREFUSED; /*%< no nameservers found */
562 errno = ETIMEDOUT; /*%< no answer obtained */
578 const struct sockaddr *sa;
582 /* There are people do not set sa_len. Be forgiving to them. */
587 if (sa->sa_family == AF_INET)
588 return (sizeof(struct sockaddr_in));
589 else if (sa->sa_family == AF_INET6)
590 return (sizeof(struct sockaddr_in6));
592 return (0); /*%< unknown, die on connect */
596 * pick appropriate nsaddr_list for use. see res_init() for initialization.
598 static struct sockaddr *
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,
652 (struct sockaddr *)&peer, &size) < 0 ||
653 !sock_eq((struct sockaddr *)&peer, nsap)) {
655 statp->_flags &= ~RES_F_VC;
659 if (statp->_vcsock < 0 || (statp->_flags & RES_F_VC) == 0) {
660 if (statp->_vcsock >= 0)
663 statp->_vcsock = _socket(nsap->sa_family, SOCK_STREAM, 0);
664 #if !defined(USE_POLL) && !defined(USE_KQUEUE)
665 if (statp->_vcsock > highestFD) {
670 if (statp->_vcsock < 0) {
672 case EPROTONOSUPPORT:
677 Perror(statp, stderr, "socket(vc)", errno);
681 Perror(statp, stderr, "socket(vc)", errno);
687 * Disable generation of SIGPIPE when writing to a closed
688 * socket. Write should return -1 and set errno to EPIPE
691 * Push on even if setsockopt(SO_NOSIGPIPE) fails.
693 (void)_setsockopt(statp->_vcsock, SOL_SOCKET, SO_NOSIGPIPE, &on,
697 if (_connect(statp->_vcsock, nsap, nsaplen) < 0) {
699 Aerror(statp, stderr, "connect/vc", errno, nsap,
704 statp->_flags |= RES_F_VC;
708 * Send length & message
710 ns_put16((u_short)buflen, (u_char*)&len);
711 iov[0] = evConsIovec(&len, INT16SZ);
713 iov[1] = evConsIovec(tmp, buflen);
714 if (_writev(statp->_vcsock, iov, 2) != (INT16SZ + buflen)) {
716 Perror(statp, stderr, "write failed", errno);
721 * Receive length & response
726 while ((n = _read(statp->_vcsock, (char *)cp, (int)len)) > 0) {
733 Perror(statp, stderr, "read failed", errno);
736 * A long running process might get its TCP
737 * connection reset if the remote server was
738 * restarted. Requery the server instead of
739 * trying a new one. When there is only one
740 * server, this means that a query might work
741 * instead of failing. We only allow one reset
742 * per query to prevent looping.
744 if (*terrno == ECONNRESET && !connreset) {
752 resplen = ns_get16(ans);
753 if (resplen > anssiz) {
754 Dprint(statp->options & RES_DEBUG,
755 (stdout, ";; response truncated\n")
761 if (len < HFIXEDSZ) {
763 * Undersized message.
765 Dprint(statp->options & RES_DEBUG,
766 (stdout, ";; undersized: %d\n", len));
773 (n = _read(statp->_vcsock, (char *)cp, (int)len)) > 0) {
779 Perror(statp, stderr, "read(vc)", errno);
785 * Flush rest of answer so connection stays in synch.
788 len = resplen - anssiz;
792 n = _read(statp->_vcsock, junk,
793 (len > sizeof junk) ? sizeof junk : len);
801 * If the calling applicating has bailed out of
802 * a previous call and failed to arrange to have
803 * the circuit closed or the server has got
804 * itself confused, then drop the packet and
805 * wait for the correct one.
807 if (hp->id != anhp->id) {
808 DprintQ((statp->options & RES_DEBUG) ||
809 (statp->pfcode & RES_PRF_REPLY),
810 (stdout, ";; old answer (unexpected):\n"),
811 ans, (resplen > anssiz) ? anssiz: resplen);
816 * All is well, or the error is fatal. Signal that the
817 * next nameserver ought not be tried.
823 send_dg(res_state statp,
827 const u_char *buf, int buflen, u_char *ans,
828 int anssiz, int *terrno, int ns, int tries, int *v_circuit,
831 const HEADER *hp = (const HEADER *) buf;
832 HEADER *anhp = (HEADER *) ans;
833 const struct sockaddr *nsap;
835 struct timespec now, timeout, finish;
836 struct sockaddr_storage from;
837 ISC_SOCKLEN_T fromlen;
838 int resplen, seconds, n, s;
844 struct pollfd pollfd;
850 nsap = get_nsaddr(statp, ns);
851 nsaplen = get_salen(nsap);
852 if (EXT(statp).nssocks[ns] == -1) {
853 EXT(statp).nssocks[ns] = _socket(nsap->sa_family,
855 #if !defined(USE_POLL) && !defined(USE_KQUEUE)
856 if (EXT(statp).nssocks[ns] > highestFD) {
861 if (EXT(statp).nssocks[ns] < 0) {
863 case EPROTONOSUPPORT:
868 Perror(statp, stderr, "socket(dg)", errno);
872 Perror(statp, stderr, "socket(dg)", errno);
876 #ifndef CANNOT_CONNECT_DGRAM
878 * On a 4.3BSD+ machine (client and server,
879 * actually), sending to a nameserver datagram
880 * port with no nameserver will cause an
881 * ICMP port unreachable message to be returned.
882 * If our datagram socket is "connected" to the
883 * server, we get an ECONNREFUSED error on the next
884 * socket operation, and select returns if the
885 * error message is received. We can thus detect
886 * the absence of a nameserver without timing out.
888 * When the option "insecure1" is specified, we'd
889 * rather expect to see responses from an "unknown"
890 * address. In order to let the kernel accept such
891 * responses, do not connect the socket here.
892 * XXX: or do we need an explicit option to disable
895 if (!(statp->options & RES_INSECURE1) &&
896 _connect(EXT(statp).nssocks[ns], nsap, nsaplen) < 0) {
897 Aerror(statp, stderr, "connect(dg)", errno, nsap,
902 #endif /* !CANNOT_CONNECT_DGRAM */
903 Dprint(statp->options & RES_DEBUG,
904 (stdout, ";; new DG socket\n"))
906 s = EXT(statp).nssocks[ns];
907 #ifndef CANNOT_CONNECT_DGRAM
908 if (statp->options & RES_INSECURE1) {
910 (const char*)buf, buflen, 0, nsap, nsaplen) != buflen) {
911 Aerror(statp, stderr, "sendto", errno, nsap, nsaplen);
915 } else if (send(s, (const char*)buf, buflen, 0) != buflen) {
916 Perror(statp, stderr, "send", errno);
920 #else /* !CANNOT_CONNECT_DGRAM */
921 if (_sendto(s, (const char*)buf, buflen, 0, nsap, nsaplen) != buflen)
923 Aerror(statp, stderr, "sendto", errno, nsap, nsaplen);
927 #endif /* !CANNOT_CONNECT_DGRAM */
932 seconds = (statp->retrans << tries);
934 seconds /= statp->nscount;
938 timeout = evConsTime(seconds, 0);
939 finish = evAddTime(now, timeout);
945 if (evCmpTime(finish, now) > 0)
946 timeout = evSubTime(finish, now);
948 timeout = evConsTime(0, 0);
950 EV_SET(&kv, s, EVFILT_READ, EV_ADD | EV_ONESHOT, 0, 0, 0);
951 n = _kevent(kq, &kv, 1, &kv, 1, &timeout);
955 n = pselect(s + 1, &dsmask, NULL, NULL, &timeout, NULL);
958 timeout = evSubTime(finish, now);
959 if (timeout.tv_sec < 0)
960 timeout = evConsTime(0, 0);
961 polltimeout = 1000*timeout.tv_sec +
962 timeout.tv_nsec/1000000;
964 pollfd.events = POLLRDNORM;
965 n = poll(&pollfd, 1, polltimeout);
966 #endif /* USE_POLL */
969 Dprint(statp->options & RES_DEBUG, (stdout, ";; timeout\n"));
977 Perror(statp, stderr, "kevent", errno);
980 Perror(statp, stderr, "select", errno);
982 Perror(statp, stderr, "poll", errno);
983 #endif /* USE_POLL */
993 fromlen = sizeof(from);
994 resplen = _recvfrom(s, (char*)ans, anssiz,0,
995 (struct sockaddr *)&from, &fromlen);
997 Perror(statp, stderr, "recvfrom", errno);
1002 if (resplen < HFIXEDSZ) {
1004 * Undersized message.
1006 Dprint(statp->options & RES_DEBUG,
1007 (stdout, ";; undersized: %d\n",
1013 if (hp->id != anhp->id) {
1015 * response from old query, ignore it.
1016 * XXX - potential security hazard could
1019 DprintQ((statp->options & RES_DEBUG) ||
1020 (statp->pfcode & RES_PRF_REPLY),
1021 (stdout, ";; old answer:\n"),
1022 ans, (resplen > anssiz) ? anssiz : resplen);
1025 if (!(statp->options & RES_INSECURE1) &&
1026 !res_ourserver_p(statp, (struct sockaddr *)&from)) {
1028 * response from wrong server? ignore it.
1029 * XXX - potential security hazard could
1032 DprintQ((statp->options & RES_DEBUG) ||
1033 (statp->pfcode & RES_PRF_REPLY),
1034 (stdout, ";; not our server:\n"),
1035 ans, (resplen > anssiz) ? anssiz : resplen);
1038 #ifdef RES_USE_EDNS0
1039 if (anhp->rcode == FORMERR && (statp->options & RES_USE_EDNS0) != 0U) {
1041 * Do not retry if the server do not understand EDNS0.
1042 * The case has to be captured here, as FORMERR packet do not
1043 * carry query section, hence res_queriesmatch() returns 0.
1045 DprintQ(statp->options & RES_DEBUG,
1046 (stdout, "server rejected query with EDNS0:\n"),
1047 ans, (resplen > anssiz) ? anssiz : resplen);
1048 /* record the error */
1049 statp->_flags |= RES_F_EDNS0ERR;
1054 if (!(statp->options & RES_INSECURE2) &&
1055 !res_queriesmatch(buf, buf + buflen,
1056 ans, ans + anssiz)) {
1058 * response contains wrong query? ignore it.
1059 * XXX - potential security hazard could
1062 DprintQ((statp->options & RES_DEBUG) ||
1063 (statp->pfcode & RES_PRF_REPLY),
1064 (stdout, ";; wrong query name:\n"),
1065 ans, (resplen > anssiz) ? anssiz : resplen);
1068 if (anhp->rcode == SERVFAIL ||
1069 anhp->rcode == NOTIMP ||
1070 anhp->rcode == REFUSED) {
1071 DprintQ(statp->options & RES_DEBUG,
1072 (stdout, "server rejected query:\n"),
1073 ans, (resplen > anssiz) ? anssiz : resplen);
1075 /* don't retry if called from dig */
1079 if (!(statp->options & RES_IGNTC) && anhp->tc) {
1081 * To get the rest of answer,
1082 * use TCP with same server.
1084 Dprint(statp->options & RES_DEBUG,
1085 (stdout, ";; truncated answer\n"));
1091 * All is well, or the error is fatal. Signal that the
1092 * next nameserver ought not be tried.
1098 Aerror(const res_state statp, FILE *file, const char *string, int error,
1099 const struct sockaddr *address, int alen)
1102 char hbuf[NI_MAXHOST];
1103 char sbuf[NI_MAXSERV];
1107 if ((statp->options & RES_DEBUG) != 0U) {
1108 if (getnameinfo(address, alen, hbuf, sizeof(hbuf),
1109 sbuf, sizeof(sbuf), niflags)) {
1110 strncpy(hbuf, "?", sizeof(hbuf) - 1);
1111 hbuf[sizeof(hbuf) - 1] = '\0';
1112 strncpy(sbuf, "?", sizeof(sbuf) - 1);
1113 sbuf[sizeof(sbuf) - 1] = '\0';
1115 fprintf(file, "res_send: %s ([%s].%s): %s\n",
1116 string, hbuf, sbuf, strerror(error));
1122 Perror(const res_state statp, FILE *file, const char *string, int error) {
1125 if ((statp->options & RES_DEBUG) != 0U)
1126 fprintf(file, "res_send: %s: %s\n",
1127 string, strerror(error));
1132 sock_eq(struct sockaddr *a, struct sockaddr *b) {
1133 struct sockaddr_in *a4, *b4;
1134 struct sockaddr_in6 *a6, *b6;
1136 if (a->sa_family != b->sa_family)
1138 switch (a->sa_family) {
1140 a4 = (struct sockaddr_in *)a;
1141 b4 = (struct sockaddr_in *)b;
1142 return a4->sin_port == b4->sin_port &&
1143 a4->sin_addr.s_addr == b4->sin_addr.s_addr;
1145 a6 = (struct sockaddr_in6 *)a;
1146 b6 = (struct sockaddr_in6 *)b;
1147 return a6->sin6_port == b6->sin6_port &&
1148 #ifdef HAVE_SIN6_SCOPE_ID
1149 a6->sin6_scope_id == b6->sin6_scope_id &&
1151 IN6_ARE_ADDR_EQUAL(&a6->sin6_addr, &b6->sin6_addr);
1157 #if defined(NEED_PSELECT) && !defined(USE_POLL) && !defined(USE_KQUEUE)
1158 /* XXX needs to move to the porting library. */
1160 pselect(int nfds, void *rfds, void *wfds, void *efds,
1161 struct timespec *tsp, const sigset_t *sigmask)
1163 struct timeval tv, *tvp;
1169 tv = evTimeVal(*tsp);
1173 sigprocmask(SIG_SETMASK, sigmask, &sigs);
1174 n = select(nfds, rfds, wfds, efds, tvp);
1176 sigprocmask(SIG_SETMASK, &sigs, NULL);
1178 *tsp = evTimeSpec(tv);