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;
140 static int get_salen __P((const struct sockaddr *));
141 static struct sockaddr * get_nsaddr __P((res_state, size_t));
142 static int send_vc(res_state, const u_char *, int,
143 u_char *, int, int *, int);
144 static int send_dg(res_state,
149 u_char *, int, int *, int, int,
151 static void Aerror(const res_state, FILE *, const char *, int,
152 const struct sockaddr *, int);
153 static void Perror(const res_state, FILE *, const char *, int);
154 static int sock_eq(struct sockaddr *, struct sockaddr *);
155 #if defined(NEED_PSELECT) && !defined(USE_POLL) && !defined(USE_KQUEUE)
156 static int pselect(int, void *, void *, void *,
160 void res_pquery(const res_state, const u_char *, int, FILE *);
162 static const int niflags = NI_NUMERICHOST | NI_NUMERICSERV;
167 * looks up "ina" in _res.ns_addr_list[]
174 *\li paul vixie, 29may94
177 res_ourserver_p(const res_state statp, const struct sockaddr *sa) {
178 const struct sockaddr_in *inp, *srv;
179 const struct sockaddr_in6 *in6p, *srv6;
182 switch (sa->sa_family) {
184 inp = (const struct sockaddr_in *)sa;
185 for (ns = 0; ns < statp->nscount; ns++) {
186 srv = (struct sockaddr_in *)get_nsaddr(statp, ns);
187 if (srv->sin_family == inp->sin_family &&
188 srv->sin_port == inp->sin_port &&
189 (srv->sin_addr.s_addr == INADDR_ANY ||
190 srv->sin_addr.s_addr == inp->sin_addr.s_addr))
195 if (EXT(statp).ext == NULL)
197 in6p = (const struct sockaddr_in6 *)sa;
198 for (ns = 0; ns < statp->nscount; ns++) {
199 srv6 = (struct sockaddr_in6 *)get_nsaddr(statp, ns);
200 if (srv6->sin6_family == in6p->sin6_family &&
201 srv6->sin6_port == in6p->sin6_port &&
202 #ifdef HAVE_SIN6_SCOPE_ID
203 (srv6->sin6_scope_id == 0 ||
204 srv6->sin6_scope_id == in6p->sin6_scope_id) &&
206 (IN6_IS_ADDR_UNSPECIFIED(&srv6->sin6_addr) ||
207 IN6_ARE_ADDR_EQUAL(&srv6->sin6_addr, &in6p->sin6_addr)))
218 * look for (name,type,class) in the query section of packet (buf,eom)
221 *\li buf + HFIXEDSZ <= eom
224 *\li -1 : format error
229 *\li paul vixie, 29may94
232 res_nameinquery(const char *name, int type, int class,
233 const u_char *buf, const u_char *eom)
235 const u_char *cp = buf + HFIXEDSZ;
236 int qdcount = ntohs(((const HEADER*)buf)->qdcount);
238 while (qdcount-- > 0) {
239 char tname[MAXDNAME+1];
240 int n, ttype, tclass;
242 n = dn_expand(buf, eom, cp, tname, sizeof tname);
246 if (cp + 2 * INT16SZ > eom)
248 ttype = ns_get16(cp); cp += INT16SZ;
249 tclass = ns_get16(cp); cp += INT16SZ;
250 if (ttype == type && tclass == class &&
251 ns_samename(tname, name) == 1)
258 * is there a 1:1 mapping of (name,type,class)
259 * in (buf1,eom1) and (buf2,eom2)?
262 *\li -1 : format error
263 *\li 0 : not a 1:1 mapping
264 *\li >0 : is a 1:1 mapping
267 *\li paul vixie, 29may94
270 res_queriesmatch(const u_char *buf1, const u_char *eom1,
271 const u_char *buf2, const u_char *eom2)
273 const u_char *cp = buf1 + HFIXEDSZ;
274 int qdcount = ntohs(((const HEADER*)buf1)->qdcount);
276 if (buf1 + HFIXEDSZ > eom1 || buf2 + HFIXEDSZ > eom2)
280 * Only header section present in replies to
281 * dynamic update packets.
283 if ((((const HEADER *)buf1)->opcode == ns_o_update) &&
284 (((const HEADER *)buf2)->opcode == ns_o_update))
287 if (qdcount != ntohs(((const HEADER*)buf2)->qdcount))
289 while (qdcount-- > 0) {
290 char tname[MAXDNAME+1];
291 int n, ttype, tclass;
293 n = dn_expand(buf1, eom1, cp, tname, sizeof tname);
297 if (cp + 2 * INT16SZ > eom1)
299 ttype = ns_get16(cp); cp += INT16SZ;
300 tclass = ns_get16(cp); cp += INT16SZ;
301 if (!res_nameinquery(tname, ttype, tclass, buf2, eom2))
308 res_nsend(res_state statp,
309 const u_char *buf, int buflen, u_char *ans, int anssiz)
311 int gotsomewhere, terrno, tries, v_circuit, resplen, ns, n;
315 char abuf[NI_MAXHOST];
317 /* No name servers or res_init() failure */
318 if (statp->nscount == 0 || EXT(statp).ext == NULL) {
322 if (anssiz < HFIXEDSZ) {
326 DprintQ((statp->options & RES_DEBUG) || (statp->pfcode & RES_PRF_QUERY),
327 (stdout, ";; res_send()\n"), buf, buflen);
328 v_circuit = (statp->options & RES_USEVC) || buflen > PACKETSZ;
333 if ((kq = kqueue()) < 0) {
334 Perror(statp, stderr, "kqueue", errno);
340 * If the ns_addr_list in the resolver context has changed, then
341 * invalidate our cached copy and the associated timing data.
343 if (EXT(statp).nscount != 0) {
345 struct sockaddr_storage peer;
346 ISC_SOCKLEN_T peerlen;
348 if (EXT(statp).nscount != statp->nscount)
351 for (ns = 0; ns < statp->nscount; ns++) {
352 if (statp->nsaddr_list[ns].sin_family &&
353 !sock_eq((struct sockaddr *)&statp->nsaddr_list[ns],
354 (struct sockaddr *)&EXT(statp).ext->nsaddrs[ns])) {
359 if (EXT(statp).nssocks[ns] == -1)
361 peerlen = sizeof(peer);
363 if (getsockname(EXT(statp).nssocks[ns],
365 if (_getsockname(EXT(statp).nssocks[ns],
367 (struct sockaddr *)&peer, &peerlen) < 0) {
371 if (!sock_eq((struct sockaddr *)&peer,
372 get_nsaddr(statp, ns))) {
379 EXT(statp).nscount = 0;
384 * Maybe initialize our private copy of the ns_addr_list.
386 if (EXT(statp).nscount == 0) {
387 for (ns = 0; ns < statp->nscount; ns++) {
388 EXT(statp).nstimes[ns] = RES_MAXTIME;
389 EXT(statp).nssocks[ns] = -1;
390 if (!statp->nsaddr_list[ns].sin_family)
392 EXT(statp).ext->nsaddrs[ns].sin =
393 statp->nsaddr_list[ns];
395 EXT(statp).nscount = statp->nscount;
399 * Some resolvers want to even out the load on their nameservers.
400 * Note that RES_BLAST overrides RES_ROTATE.
402 if ((statp->options & RES_ROTATE) != 0U &&
403 (statp->options & RES_BLAST) == 0U) {
404 union res_sockaddr_union inu;
405 struct sockaddr_in ina;
406 int lastns = statp->nscount - 1;
410 if (EXT(statp).ext != NULL)
411 inu = EXT(statp).ext->nsaddrs[0];
412 ina = statp->nsaddr_list[0];
413 fd = EXT(statp).nssocks[0];
414 nstime = EXT(statp).nstimes[0];
415 for (ns = 0; ns < lastns; ns++) {
416 if (EXT(statp).ext != NULL)
417 EXT(statp).ext->nsaddrs[ns] =
418 EXT(statp).ext->nsaddrs[ns + 1];
419 statp->nsaddr_list[ns] = statp->nsaddr_list[ns + 1];
420 EXT(statp).nssocks[ns] = EXT(statp).nssocks[ns + 1];
421 EXT(statp).nstimes[ns] = EXT(statp).nstimes[ns + 1];
423 if (EXT(statp).ext != NULL)
424 EXT(statp).ext->nsaddrs[lastns] = inu;
425 statp->nsaddr_list[lastns] = ina;
426 EXT(statp).nssocks[lastns] = fd;
427 EXT(statp).nstimes[lastns] = nstime;
431 * Send request, RETRY times, or until successful.
433 for (tries = 0; tries < statp->retry; tries++) {
434 for (ns = 0; ns < statp->nscount; ns++) {
435 struct sockaddr *nsap;
437 nsap = get_nsaddr(statp, ns);
438 nsaplen = get_salen(nsap);
439 statp->_flags &= ~RES_F_LASTMASK;
440 statp->_flags |= (ns << RES_F_LASTSHIFT);
443 int done = 0, loops = 0;
448 act = (*statp->qhook)(&nsap, &buf, &buflen,
449 ans, anssiz, &resplen);
463 /* give the hook another try */
464 if (++loops < 42) /*doug adams*/
475 Dprint(((statp->options & RES_DEBUG) &&
476 getnameinfo(nsap, nsaplen, abuf, sizeof(abuf),
477 NULL, 0, niflags) == 0),
478 (stdout, ";; Querying server (# %d) address = %s\n",
483 /* Use VC; at most one attempt per server. */
484 tries = statp->retry;
485 n = send_vc(statp, buf, buflen, ans, anssiz, &terrno,
498 buf, buflen, ans, anssiz, &terrno,
499 ns, tries, &v_circuit, &gotsomewhere);
509 Dprint((statp->options & RES_DEBUG) ||
510 ((statp->pfcode & RES_PRF_REPLY) &&
511 (statp->pfcode & RES_PRF_HEAD1)),
512 (stdout, ";; got answer:\n"));
514 DprintQ((statp->options & RES_DEBUG) ||
515 (statp->pfcode & RES_PRF_REPLY),
517 ans, (resplen > anssiz) ? anssiz : resplen);
520 * If we have temporarily opened a virtual circuit,
521 * or if we haven't been asked to keep a socket open,
524 if ((v_circuit && (statp->options & RES_USEVC) == 0U) ||
525 (statp->options & RES_STAYOPEN) == 0U) {
529 int done = 0, loops = 0;
534 act = (*statp->rhook)(nsap, buf, buflen,
535 ans, anssiz, &resplen);
545 /* give the hook another try */
546 if (++loops < 42) /*doug adams*/
570 errno = ECONNREFUSED; /*%< no nameservers found */
572 errno = ETIMEDOUT; /*%< no answer obtained */
584 get_salen(const struct sockaddr *sa)
588 /* There are people do not set sa_len. Be forgiving to them. */
593 if (sa->sa_family == AF_INET)
594 return (sizeof(struct sockaddr_in));
595 else if (sa->sa_family == AF_INET6)
596 return (sizeof(struct sockaddr_in6));
598 return (0); /*%< unknown, die on connect */
602 * pick appropriate nsaddr_list for use. see res_init() for initialization.
604 static struct sockaddr *
605 get_nsaddr(res_state statp, size_t n)
608 if (!statp->nsaddr_list[n].sin_family && EXT(statp).ext) {
610 * - EXT(statp).ext->nsaddrs[n] holds an address that is larger
611 * than struct sockaddr, and
612 * - user code did not update statp->nsaddr_list[n].
614 return (struct sockaddr *)(void *)&EXT(statp).ext->nsaddrs[n];
617 * - user code updated statp->nsaddr_list[n], or
618 * - statp->nsaddr_list[n] has the same content as
619 * EXT(statp).ext->nsaddrs[n].
621 return (struct sockaddr *)(void *)&statp->nsaddr_list[n];
626 send_vc(res_state statp,
627 const u_char *buf, int buflen, u_char *ans, int anssiz,
630 const HEADER *hp = (const HEADER *) buf;
631 HEADER *anhp = (HEADER *) ans;
632 struct sockaddr *nsap;
634 int truncating, connreset, resplen, n;
643 nsap = get_nsaddr(statp, ns);
644 nsaplen = get_salen(nsap);
650 /* Are we still talking to whom we want to talk to? */
651 if (statp->_vcsock >= 0 && (statp->_flags & RES_F_VC) != 0) {
652 struct sockaddr_storage peer;
653 ISC_SOCKLEN_T size = sizeof peer;
655 if (getpeername(statp->_vcsock,
657 if (_getpeername(statp->_vcsock,
659 (struct sockaddr *)&peer, &size) < 0 ||
660 !sock_eq((struct sockaddr *)&peer, nsap)) {
662 statp->_flags &= ~RES_F_VC;
666 if (statp->_vcsock < 0 || (statp->_flags & RES_F_VC) == 0) {
667 if (statp->_vcsock >= 0)
670 statp->_vcsock = socket(nsap->sa_family, SOCK_STREAM, 0);
672 statp->_vcsock = _socket(nsap->sa_family, SOCK_STREAM, 0);
674 #if !defined(USE_POLL) && !defined(USE_KQUEUE)
675 if (statp->_vcsock > highestFD) {
680 if (statp->_vcsock < 0) {
682 case EPROTONOSUPPORT:
687 Perror(statp, stderr, "socket(vc)", errno);
691 Perror(statp, stderr, "socket(vc)", errno);
697 * Disable generation of SIGPIPE when writing to a closed
698 * socket. Write should return -1 and set errno to EPIPE
701 * Push on even if setsockopt(SO_NOSIGPIPE) fails.
703 (void)setsockopt(statp->_vcsock, SOL_SOCKET, SO_NOSIGPIPE, &on,
708 if (connect(statp->_vcsock, nsap, nsaplen) < 0) {
710 if (_connect(statp->_vcsock, nsap, nsaplen) < 0) {
713 Aerror(statp, stderr, "connect/vc", errno, nsap,
718 statp->_flags |= RES_F_VC;
722 * Send length & message
724 ns_put16((u_short)buflen, (u_char*)&len);
725 iov[0] = evConsIovec(&len, INT16SZ);
727 iov[1] = evConsIovec(tmp, buflen);
729 if (writev(statp->_vcsock, iov, 2) != (INT16SZ + buflen)) {
731 if (_writev(statp->_vcsock, iov, 2) != (INT16SZ + buflen)) {
734 Perror(statp, stderr, "write failed", errno);
739 * Receive length & response
745 while ((n = read(statp->_vcsock, (char *)cp, (int)len)) > 0) {
747 while ((n = _read(statp->_vcsock, (char *)cp, (int)len)) > 0) {
755 Perror(statp, stderr, "read failed", errno);
758 * A long running process might get its TCP
759 * connection reset if the remote server was
760 * restarted. Requery the server instead of
761 * trying a new one. When there is only one
762 * server, this means that a query might work
763 * instead of failing. We only allow one reset
764 * per query to prevent looping.
766 if (*terrno == ECONNRESET && !connreset) {
774 resplen = ns_get16(ans);
775 if (resplen > anssiz) {
776 Dprint(statp->options & RES_DEBUG,
777 (stdout, ";; response truncated\n")
783 if (len < HFIXEDSZ) {
785 * Undersized message.
787 Dprint(statp->options & RES_DEBUG,
788 (stdout, ";; undersized: %d\n", len));
795 while (len != 0 && (n = read(statp->_vcsock, (char *)cp, (int)len)) > 0){
797 while (len != 0 && (n = _read(statp->_vcsock, (char *)cp, (int)len)) > 0){
804 Perror(statp, stderr, "read(vc)", errno);
810 * Flush rest of answer so connection stays in synch.
813 len = resplen - anssiz;
818 n = read(statp->_vcsock, junk,
820 n = _read(statp->_vcsock, junk,
822 (len > sizeof junk) ? sizeof junk : len);
830 * If the calling applicating has bailed out of
831 * a previous call and failed to arrange to have
832 * the circuit closed or the server has got
833 * itself confused, then drop the packet and
834 * wait for the correct one.
836 if (hp->id != anhp->id) {
837 DprintQ((statp->options & RES_DEBUG) ||
838 (statp->pfcode & RES_PRF_REPLY),
839 (stdout, ";; old answer (unexpected):\n"),
840 ans, (resplen > anssiz) ? anssiz: resplen);
845 * All is well, or the error is fatal. Signal that the
846 * next nameserver ought not be tried.
852 send_dg(res_state statp,
856 const u_char *buf, int buflen, u_char *ans,
857 int anssiz, int *terrno, int ns, int tries, int *v_circuit,
860 const HEADER *hp = (const HEADER *) buf;
861 HEADER *anhp = (HEADER *) ans;
862 const struct sockaddr *nsap;
864 struct timespec now, timeout, finish;
865 struct sockaddr_storage from;
866 ISC_SOCKLEN_T fromlen;
867 int resplen, seconds, n, s;
873 struct pollfd pollfd;
879 nsap = get_nsaddr(statp, ns);
880 nsaplen = get_salen(nsap);
881 if (EXT(statp).nssocks[ns] == -1) {
883 EXT(statp).nssocks[ns] = socket(nsap->sa_family, SOCK_DGRAM, 0);
885 EXT(statp).nssocks[ns] = _socket(nsap->sa_family, SOCK_DGRAM, 0);
887 #if !defined(USE_POLL) && !defined(USE_KQUEUE)
888 if (EXT(statp).nssocks[ns] > highestFD) {
893 if (EXT(statp).nssocks[ns] < 0) {
895 case EPROTONOSUPPORT:
900 Perror(statp, stderr, "socket(dg)", errno);
904 Perror(statp, stderr, "socket(dg)", errno);
908 #ifndef CANNOT_CONNECT_DGRAM
910 * On a 4.3BSD+ machine (client and server,
911 * actually), sending to a nameserver datagram
912 * port with no nameserver will cause an
913 * ICMP port unreachable message to be returned.
914 * If our datagram socket is "connected" to the
915 * server, we get an ECONNREFUSED error on the next
916 * socket operation, and select returns if the
917 * error message is received. We can thus detect
918 * the absence of a nameserver without timing out.
921 * When the option "insecure1" is specified, we'd
922 * rather expect to see responses from an "unknown"
923 * address. In order to let the kernel accept such
924 * responses, do not connect the socket here.
925 * XXX: or do we need an explicit option to disable
928 if (!(statp->options & RES_INSECURE1) &&
930 connect(EXT(statp).nssocks[ns], nsap, nsaplen) < 0) {
932 _connect(EXT(statp).nssocks[ns], nsap, nsaplen) < 0) {
934 Aerror(statp, stderr, "connect(dg)", errno, nsap,
939 #endif /* !CANNOT_CONNECT_DGRAM */
940 Dprint(statp->options & RES_DEBUG,
941 (stdout, ";; new DG socket\n"))
943 s = EXT(statp).nssocks[ns];
944 #ifndef CANNOT_CONNECT_DGRAM
945 if (statp->options & RES_INSECURE1) {
951 (const char*)buf, buflen, 0, nsap, nsaplen) != buflen) {
952 Aerror(statp, stderr, "sendto", errno, nsap, nsaplen);
956 } else if (send(s, (const char*)buf, buflen, 0) != buflen) {
957 Perror(statp, stderr, "send", errno);
961 #else /* !CANNOT_CONNECT_DGRAM */
963 if (sendto(s, (const char*)buf, buflen, 0, nsap, nsaplen) != buflen)
965 if (_sendto(s, (const char*)buf, buflen, 0, nsap, nsaplen) != buflen)
968 Aerror(statp, stderr, "sendto", errno, nsap, nsaplen);
972 #endif /* !CANNOT_CONNECT_DGRAM */
977 seconds = (statp->retrans << tries);
979 seconds /= statp->nscount;
983 timeout = evConsTime(seconds, 0);
984 finish = evAddTime(now, timeout);
990 if (evCmpTime(finish, now) > 0)
991 timeout = evSubTime(finish, now);
993 timeout = evConsTime(0, 0);
995 EV_SET(&kv, s, EVFILT_READ, EV_ADD | EV_ONESHOT, 0, 0, 0);
996 n = _kevent(kq, &kv, 1, &kv, 1, &timeout);
1000 n = pselect(s + 1, &dsmask, NULL, NULL, &timeout, NULL);
1003 timeout = evSubTime(finish, now);
1004 if (timeout.tv_sec < 0)
1005 timeout = evConsTime(0, 0);
1006 polltimeout = 1000*timeout.tv_sec +
1007 timeout.tv_nsec/1000000;
1009 pollfd.events = POLLRDNORM;
1010 n = poll(&pollfd, 1, polltimeout);
1011 #endif /* USE_POLL */
1014 Dprint(statp->options & RES_DEBUG, (stdout, ";; timeout\n"));
1022 Perror(statp, stderr, "kevent", errno);
1025 Perror(statp, stderr, "select", errno);
1027 Perror(statp, stderr, "poll", errno);
1028 #endif /* USE_POLL */
1038 fromlen = sizeof(from);
1040 resplen = recvfrom(s, (char*)ans, anssiz,0,
1042 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, (struct sockaddr *)&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 #ifdef RES_USE_EDNS0
1088 if (anhp->rcode == FORMERR && (statp->options & RES_USE_EDNS0) != 0U) {
1090 * Do not retry if the server do not understand EDNS0.
1091 * The case has to be captured here, as FORMERR packet do not
1092 * carry query section, hence res_queriesmatch() returns 0.
1094 DprintQ(statp->options & RES_DEBUG,
1095 (stdout, "server rejected query with EDNS0:\n"),
1096 ans, (resplen > anssiz) ? anssiz : resplen);
1097 /* record the error */
1098 statp->_flags |= RES_F_EDNS0ERR;
1103 if (!(statp->options & RES_INSECURE2) &&
1104 !res_queriesmatch(buf, buf + buflen,
1105 ans, ans + anssiz)) {
1107 * response contains wrong query? ignore it.
1108 * XXX - potential security hazard could
1111 DprintQ((statp->options & RES_DEBUG) ||
1112 (statp->pfcode & RES_PRF_REPLY),
1113 (stdout, ";; wrong query name:\n"),
1114 ans, (resplen > anssiz) ? anssiz : resplen);
1117 if (anhp->rcode == SERVFAIL ||
1118 anhp->rcode == NOTIMP ||
1119 anhp->rcode == REFUSED) {
1120 DprintQ(statp->options & RES_DEBUG,
1121 (stdout, "server rejected query:\n"),
1122 ans, (resplen > anssiz) ? anssiz : resplen);
1124 /* don't retry if called from dig */
1128 if (!(statp->options & RES_IGNTC) && anhp->tc) {
1130 * To get the rest of answer,
1131 * use TCP with same server.
1133 Dprint(statp->options & RES_DEBUG,
1134 (stdout, ";; truncated answer\n"));
1140 * All is well, or the error is fatal. Signal that the
1141 * next nameserver ought not be tried.
1147 Aerror(const res_state statp, FILE *file, const char *string, int error,
1148 const struct sockaddr *address, int alen)
1151 char hbuf[NI_MAXHOST];
1152 char sbuf[NI_MAXSERV];
1156 if ((statp->options & RES_DEBUG) != 0U) {
1157 if (getnameinfo(address, alen, hbuf, sizeof(hbuf),
1158 sbuf, sizeof(sbuf), niflags)) {
1159 strncpy(hbuf, "?", sizeof(hbuf) - 1);
1160 hbuf[sizeof(hbuf) - 1] = '\0';
1161 strncpy(sbuf, "?", sizeof(sbuf) - 1);
1162 sbuf[sizeof(sbuf) - 1] = '\0';
1164 fprintf(file, "res_send: %s ([%s].%s): %s\n",
1165 string, hbuf, sbuf, strerror(error));
1171 Perror(const res_state statp, FILE *file, const char *string, int error) {
1174 if ((statp->options & RES_DEBUG) != 0U)
1175 fprintf(file, "res_send: %s: %s\n",
1176 string, strerror(error));
1181 sock_eq(struct sockaddr *a, struct sockaddr *b) {
1182 struct sockaddr_in *a4, *b4;
1183 struct sockaddr_in6 *a6, *b6;
1185 if (a->sa_family != b->sa_family)
1187 switch (a->sa_family) {
1189 a4 = (struct sockaddr_in *)a;
1190 b4 = (struct sockaddr_in *)b;
1191 return a4->sin_port == b4->sin_port &&
1192 a4->sin_addr.s_addr == b4->sin_addr.s_addr;
1194 a6 = (struct sockaddr_in6 *)a;
1195 b6 = (struct sockaddr_in6 *)b;
1196 return a6->sin6_port == b6->sin6_port &&
1197 #ifdef HAVE_SIN6_SCOPE_ID
1198 a6->sin6_scope_id == b6->sin6_scope_id &&
1200 IN6_ARE_ADDR_EQUAL(&a6->sin6_addr, &b6->sin6_addr);
1206 #if defined(NEED_PSELECT) && !defined(USE_POLL) && !defined(USE_KQUEUE)
1207 /* XXX needs to move to the porting library. */
1209 pselect(int nfds, void *rfds, void *wfds, void *efds,
1210 struct timespec *tsp, const sigset_t *sigmask)
1212 struct timeval tv, *tvp;
1218 tv = evTimeVal(*tsp);
1222 sigprocmask(SIG_SETMASK, sigmask, &sigs);
1223 n = select(nfds, rfds, wfds, efds, tvp);
1225 sigprocmask(SIG_SETMASK, &sigs, NULL);
1227 *tsp = evTimeSpec(tv);