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.5.2.2.4.9 2006/10/16 23:00:50 marka Exp $";
74 #endif /* LIBC_SCCS and not lint */
77 * Send query to name server and wait for reply.
80 #include "port_before.h"
82 #include "fd_setsize.h"
86 #include "namespace.h"
88 #include <sys/types.h>
89 #include <sys/param.h>
91 #include <sys/socket.h>
94 #include <netinet/in.h>
95 #include <arpa/nameser.h>
96 #include <arpa/inet.h>
107 #include "isc/eventlib.h"
109 #include "port_after.h"
111 #include <sys/event.h>
114 #ifdef HAVE_STROPTS_H
118 #endif /* USE_POLL */
122 #include "un-namespace.h"
125 /* Options. Leave them on. */
127 #include "res_debug.h"
128 #include "res_private.h"
130 #define EXT(res) ((res)->_u._ext)
132 #if !defined(USE_POLL) && !defined(USE_KQUEUE)
133 static const int highestFD = FD_SETSIZE - 1;
138 static int get_salen __P((const struct sockaddr *));
139 static struct sockaddr * get_nsaddr __P((res_state, size_t));
140 static int send_vc(res_state, const u_char *, int,
141 u_char *, int, int *, int);
142 static int send_dg(res_state,
147 u_char *, int, int *, int, int,
149 static void Aerror(const res_state, FILE *, const char *, int,
150 const struct sockaddr *, int);
151 static void Perror(const res_state, FILE *, const char *, int);
152 static int sock_eq(struct sockaddr *, struct sockaddr *);
153 #if defined(NEED_PSELECT) && !defined(USE_POLL) && !defined(USE_KQUEUE)
154 static int pselect(int, void *, void *, void *,
158 void res_pquery(const res_state, const u_char *, int, FILE *);
160 static const int niflags = NI_NUMERICHOST | NI_NUMERICSERV;
165 * res_isourserver(ina)
166 * looks up "ina" in _res.ns_addr_list[]
171 * paul vixie, 29may94
174 res_ourserver_p(const res_state statp, const struct sockaddr *sa) {
175 const struct sockaddr_in *inp, *srv;
176 const struct sockaddr_in6 *in6p, *srv6;
179 switch (sa->sa_family) {
181 inp = (const struct sockaddr_in *)sa;
182 for (ns = 0; ns < statp->nscount; ns++) {
183 srv = (struct sockaddr_in *)get_nsaddr(statp, ns);
184 if (srv->sin_family == inp->sin_family &&
185 srv->sin_port == inp->sin_port &&
186 (srv->sin_addr.s_addr == INADDR_ANY ||
187 srv->sin_addr.s_addr == inp->sin_addr.s_addr))
192 if (EXT(statp).ext == NULL)
194 in6p = (const struct sockaddr_in6 *)sa;
195 for (ns = 0; ns < statp->nscount; ns++) {
196 srv6 = (struct sockaddr_in6 *)get_nsaddr(statp, ns);
197 if (srv6->sin6_family == in6p->sin6_family &&
198 srv6->sin6_port == in6p->sin6_port &&
199 #ifdef HAVE_SIN6_SCOPE_ID
200 (srv6->sin6_scope_id == 0 ||
201 srv6->sin6_scope_id == in6p->sin6_scope_id) &&
203 (IN6_IS_ADDR_UNSPECIFIED(&srv6->sin6_addr) ||
204 IN6_ARE_ADDR_EQUAL(&srv6->sin6_addr, &in6p->sin6_addr)))
215 * res_nameinquery(name, type, class, buf, eom)
216 * look for (name,type,class) in the query section of packet (buf,eom)
218 * buf + HFIXEDSZ <= eom
224 * paul vixie, 29may94
227 res_nameinquery(const char *name, int type, int class,
228 const u_char *buf, const u_char *eom)
230 const u_char *cp = buf + HFIXEDSZ;
231 int qdcount = ntohs(((const HEADER*)buf)->qdcount);
233 while (qdcount-- > 0) {
234 char tname[MAXDNAME+1];
235 int n, ttype, tclass;
237 n = dn_expand(buf, eom, cp, tname, sizeof tname);
241 if (cp + 2 * INT16SZ > eom)
243 ttype = ns_get16(cp); cp += INT16SZ;
244 tclass = ns_get16(cp); cp += INT16SZ;
245 if (ttype == type && tclass == class &&
246 ns_samename(tname, name) == 1)
253 * res_queriesmatch(buf1, eom1, buf2, eom2)
254 * is there a 1:1 mapping of (name,type,class)
255 * in (buf1,eom1) and (buf2,eom2)?
258 * 0 : not a 1:1 mapping
259 * >0 : is a 1:1 mapping
261 * 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, try, 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);
357 if (getsockname(EXT(statp).nssocks[ns],
359 if (_getsockname(EXT(statp).nssocks[ns],
361 (struct sockaddr *)&peer, &peerlen) < 0) {
365 if (!sock_eq((struct sockaddr *)&peer,
366 get_nsaddr(statp, ns))) {
373 EXT(statp).nscount = 0;
378 * Maybe initialize our private copy of the ns_addr_list.
380 if (EXT(statp).nscount == 0) {
381 for (ns = 0; ns < statp->nscount; ns++) {
382 EXT(statp).nstimes[ns] = RES_MAXTIME;
383 EXT(statp).nssocks[ns] = -1;
384 if (!statp->nsaddr_list[ns].sin_family)
386 EXT(statp).ext->nsaddrs[ns].sin =
387 statp->nsaddr_list[ns];
389 EXT(statp).nscount = statp->nscount;
393 * Some resolvers want to even out the load on their nameservers.
394 * Note that RES_BLAST overrides RES_ROTATE.
396 if ((statp->options & RES_ROTATE) != 0U &&
397 (statp->options & RES_BLAST) == 0U) {
398 union res_sockaddr_union inu;
399 struct sockaddr_in ina;
400 int lastns = statp->nscount - 1;
404 if (EXT(statp).ext != NULL)
405 inu = EXT(statp).ext->nsaddrs[0];
406 ina = statp->nsaddr_list[0];
407 fd = EXT(statp).nssocks[0];
408 nstime = EXT(statp).nstimes[0];
409 for (ns = 0; ns < lastns; ns++) {
410 if (EXT(statp).ext != NULL)
411 EXT(statp).ext->nsaddrs[ns] =
412 EXT(statp).ext->nsaddrs[ns + 1];
413 statp->nsaddr_list[ns] = statp->nsaddr_list[ns + 1];
414 EXT(statp).nssocks[ns] = EXT(statp).nssocks[ns + 1];
415 EXT(statp).nstimes[ns] = EXT(statp).nstimes[ns + 1];
417 if (EXT(statp).ext != NULL)
418 EXT(statp).ext->nsaddrs[lastns] = inu;
419 statp->nsaddr_list[lastns] = ina;
420 EXT(statp).nssocks[lastns] = fd;
421 EXT(statp).nstimes[lastns] = nstime;
425 * Send request, RETRY times, or until successful.
427 for (try = 0; try < statp->retry; try++) {
428 for (ns = 0; ns < statp->nscount; ns++) {
429 struct sockaddr *nsap;
431 nsap = get_nsaddr(statp, ns);
432 nsaplen = get_salen(nsap);
433 statp->_flags &= ~RES_F_LASTMASK;
434 statp->_flags |= (ns << RES_F_LASTSHIFT);
437 int done = 0, loops = 0;
442 act = (*statp->qhook)(&nsap, &buf, &buflen,
443 ans, anssiz, &resplen);
457 /* give the hook another try */
458 if (++loops < 42) /*doug adams*/
469 Dprint(((statp->options & RES_DEBUG) &&
470 getnameinfo(nsap, nsaplen, abuf, sizeof(abuf),
471 NULL, 0, niflags) == 0),
472 (stdout, ";; Querying server (# %d) address = %s\n",
477 /* Use VC; at most one attempt per server. */
479 n = send_vc(statp, buf, buflen, ans, anssiz, &terrno,
492 buf, buflen, ans, anssiz, &terrno,
493 ns, try, &v_circuit, &gotsomewhere);
503 Dprint((statp->options & RES_DEBUG) ||
504 ((statp->pfcode & RES_PRF_REPLY) &&
505 (statp->pfcode & RES_PRF_HEAD1)),
506 (stdout, ";; got answer:\n"));
508 DprintQ((statp->options & RES_DEBUG) ||
509 (statp->pfcode & RES_PRF_REPLY),
511 ans, (resplen > anssiz) ? anssiz : resplen);
514 * If we have temporarily opened a virtual circuit,
515 * or if we haven't been asked to keep a socket open,
518 if ((v_circuit && (statp->options & RES_USEVC) == 0U) ||
519 (statp->options & RES_STAYOPEN) == 0U) {
523 int done = 0, loops = 0;
528 act = (*statp->rhook)(nsap, buf, buflen,
529 ans, anssiz, &resplen);
539 /* give the hook another try */
540 if (++loops < 42) /*doug adams*/
564 errno = ECONNREFUSED; /* no nameservers found */
566 errno = ETIMEDOUT; /* no answer obtained */
579 const struct sockaddr *sa;
583 /* There are people do not set sa_len. Be forgiving to them. */
588 if (sa->sa_family == AF_INET)
589 return (sizeof(struct sockaddr_in));
590 else if (sa->sa_family == AF_INET6)
591 return (sizeof(struct sockaddr_in6));
593 return (0); /* unknown, die on connect */
597 * pick appropriate nsaddr_list for use. see res_init() for initialization.
599 static struct sockaddr *
605 if (!statp->nsaddr_list[n].sin_family && EXT(statp).ext) {
607 * - EXT(statp).ext->nsaddrs[n] holds an address that is larger
608 * than struct sockaddr, and
609 * - user code did not update statp->nsaddr_list[n].
611 return (struct sockaddr *)(void *)&EXT(statp).ext->nsaddrs[n];
614 * - user code updated statp->nsaddr_list[n], or
615 * - statp->nsaddr_list[n] has the same content as
616 * EXT(statp).ext->nsaddrs[n].
618 return (struct sockaddr *)(void *)&statp->nsaddr_list[n];
623 send_vc(res_state statp,
624 const u_char *buf, int buflen, u_char *ans, int anssiz,
627 const HEADER *hp = (const HEADER *) buf;
628 HEADER *anhp = (HEADER *) ans;
629 struct sockaddr *nsap;
631 int truncating, connreset, resplen, n;
637 nsap = get_nsaddr(statp, ns);
638 nsaplen = get_salen(nsap);
644 /* Are we still talking to whom we want to talk to? */
645 if (statp->_vcsock >= 0 && (statp->_flags & RES_F_VC) != 0) {
646 struct sockaddr_storage peer;
647 ISC_SOCKLEN_T size = sizeof peer;
650 if (getpeername(statp->_vcsock,
652 if (_getpeername(statp->_vcsock,
654 (struct sockaddr *)&peer, &size) < 0 ||
655 !sock_eq((struct sockaddr *)&peer, nsap)) {
657 statp->_flags &= ~RES_F_VC;
661 if (statp->_vcsock < 0 || (statp->_flags & RES_F_VC) == 0) {
662 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 if (connect(statp->_vcsock, nsap, nsaplen) < 0) {
695 if (_connect(statp->_vcsock, nsap, nsaplen) < 0) {
698 Aerror(statp, stderr, "connect/vc", errno, nsap,
703 statp->_flags |= RES_F_VC;
707 * Send length & message
709 ns_put16((u_short)buflen, (u_char*)&len);
710 iov[0] = evConsIovec(&len, INT16SZ);
712 iov[1] = evConsIovec(tmp, buflen);
714 if (writev(statp->_vcsock, iov, 2) != (INT16SZ + buflen)) {
716 if (_writev(statp->_vcsock, iov, 2) != (INT16SZ + buflen)) {
719 Perror(statp, stderr, "write failed", errno);
724 * Receive length & response
730 while ((n = read(statp->_vcsock, (char *)cp, (int)len)) > 0) {
732 while ((n = _read(statp->_vcsock, (char *)cp, (int)len)) > 0) {
740 Perror(statp, stderr, "read failed", errno);
743 * A long running process might get its TCP
744 * connection reset if the remote server was
745 * restarted. Requery the server instead of
746 * trying a new one. When there is only one
747 * server, this means that a query might work
748 * instead of failing. We only allow one reset
749 * per query to prevent looping.
751 if (*terrno == ECONNRESET && !connreset) {
759 resplen = ns_get16(ans);
760 if (resplen > anssiz) {
761 Dprint(statp->options & RES_DEBUG,
762 (stdout, ";; response truncated\n")
768 if (len < HFIXEDSZ) {
770 * Undersized message.
772 Dprint(statp->options & RES_DEBUG,
773 (stdout, ";; undersized: %d\n", len));
780 while (len != 0 && (n = read(statp->_vcsock, (char *)cp, (int)len)) > 0){
782 while (len != 0 && (n = _read(statp->_vcsock, (char *)cp, (int)len)) > 0){
789 Perror(statp, stderr, "read(vc)", errno);
795 * Flush rest of answer so connection stays in synch.
798 len = resplen - anssiz;
803 n = read(statp->_vcsock, junk,
805 n = _read(statp->_vcsock, junk,
807 (len > sizeof junk) ? sizeof junk : len);
815 * If the calling applicating has bailed out of
816 * a previous call and failed to arrange to have
817 * the circuit closed or the server has got
818 * itself confused, then drop the packet and
819 * wait for the correct one.
821 if (hp->id != anhp->id) {
822 DprintQ((statp->options & RES_DEBUG) ||
823 (statp->pfcode & RES_PRF_REPLY),
824 (stdout, ";; old answer (unexpected):\n"),
825 ans, (resplen > anssiz) ? anssiz: resplen);
830 * All is well, or the error is fatal. Signal that the
831 * next nameserver ought not be tried.
837 send_dg(res_state statp,
841 const u_char *buf, int buflen, u_char *ans,
842 int anssiz, int *terrno, int ns, int try, int *v_circuit,
845 const HEADER *hp = (const HEADER *) buf;
846 HEADER *anhp = (HEADER *) ans;
847 const struct sockaddr *nsap;
849 struct timespec now, timeout, finish;
850 struct sockaddr_storage from;
851 ISC_SOCKLEN_T fromlen;
852 int resplen, seconds, n, s;
858 struct pollfd pollfd;
864 nsap = get_nsaddr(statp, ns);
865 nsaplen = get_salen(nsap);
866 if (EXT(statp).nssocks[ns] == -1) {
868 EXT(statp).nssocks[ns] = socket(nsap->sa_family, SOCK_DGRAM, 0);
870 EXT(statp).nssocks[ns] = _socket(nsap->sa_family, SOCK_DGRAM, 0);
872 #if !defined(USE_POLL) && !defined(USE_KQUEUE)
873 if (EXT(statp).nssocks[ns] > highestFD) {
878 if (EXT(statp).nssocks[ns] < 0) {
880 case EPROTONOSUPPORT:
885 Perror(statp, stderr, "socket(dg)", errno);
889 Perror(statp, stderr, "socket(dg)", errno);
893 #ifndef CANNOT_CONNECT_DGRAM
895 * On a 4.3BSD+ machine (client and server,
896 * actually), sending to a nameserver datagram
897 * port with no nameserver will cause an
898 * ICMP port unreachable message to be returned.
899 * If our datagram socket is "connected" to the
900 * server, we get an ECONNREFUSED error on the next
901 * socket operation, and select returns if the
902 * error message is received. We can thus detect
903 * the absence of a nameserver without timing out.
906 * When the option "insecure1" is specified, we'd
907 * rather expect to see responses from an "unknown"
908 * address. In order to let the kernel accept such
909 * responses, do not connect the socket here.
910 * XXX: or do we need an explicit option to disable
913 if (!(statp->options & RES_INSECURE1) &&
915 connect(EXT(statp).nssocks[ns], nsap, nsaplen) < 0) {
917 _connect(EXT(statp).nssocks[ns], nsap, nsaplen) < 0) {
919 Aerror(statp, stderr, "connect(dg)", errno, nsap,
924 #endif /* !CANNOT_CONNECT_DGRAM */
925 Dprint(statp->options & RES_DEBUG,
926 (stdout, ";; new DG socket\n"))
928 s = EXT(statp).nssocks[ns];
929 #ifndef CANNOT_CONNECT_DGRAM
930 if (statp->options & RES_INSECURE1) {
936 (const char*)buf, buflen, 0, nsap, nsaplen) != buflen) {
937 Aerror(statp, stderr, "sendto", errno, nsap, nsaplen);
941 } else if (send(s, (const char*)buf, buflen, 0) != buflen) {
942 Perror(statp, stderr, "send", errno);
946 #else /* !CANNOT_CONNECT_DGRAM */
948 if (sendto(s, (const char*)buf, buflen, 0, nsap, nsaplen) != buflen)
950 if (_sendto(s, (const char*)buf, buflen, 0, nsap, nsaplen) != buflen)
953 Aerror(statp, stderr, "sendto", errno, nsap, nsaplen);
957 #endif /* !CANNOT_CONNECT_DGRAM */
962 seconds = (statp->retrans << try);
964 seconds /= statp->nscount;
968 timeout = evConsTime(seconds, 0);
969 finish = evAddTime(now, timeout);
975 if (evCmpTime(finish, now) > 0)
976 timeout = evSubTime(finish, now);
978 timeout = evConsTime(0, 0);
980 EV_SET(&kv, s, EVFILT_READ, EV_ADD | EV_ONESHOT, 0, 0, 0);
981 n = _kevent(kq, &kv, 1, &kv, 1, &timeout);
985 n = pselect(s + 1, &dsmask, NULL, NULL, &timeout, NULL);
988 timeout = evSubTime(finish, now);
989 if (timeout.tv_sec < 0)
990 timeout = evConsTime(0, 0);
991 polltimeout = 1000*timeout.tv_sec +
992 timeout.tv_nsec/1000000;
994 pollfd.events = POLLRDNORM;
995 n = poll(&pollfd, 1, polltimeout);
996 #endif /* USE_POLL */
999 Dprint(statp->options & RES_DEBUG, (stdout, ";; timeout\n"));
1007 Perror(statp, stderr, "kevent", errno);
1010 Perror(statp, stderr, "select", errno);
1012 Perror(statp, stderr, "poll", errno);
1013 #endif /* USE_POLL */
1023 fromlen = sizeof(from);
1025 resplen = recvfrom(s, (char*)ans, anssiz,0,
1027 resplen = _recvfrom(s, (char*)ans, anssiz,0,
1029 (struct sockaddr *)&from, &fromlen);
1031 Perror(statp, stderr, "recvfrom", errno);
1036 if (resplen < HFIXEDSZ) {
1038 * Undersized message.
1040 Dprint(statp->options & RES_DEBUG,
1041 (stdout, ";; undersized: %d\n",
1047 if (hp->id != anhp->id) {
1049 * response from old query, ignore it.
1050 * XXX - potential security hazard could
1053 DprintQ((statp->options & RES_DEBUG) ||
1054 (statp->pfcode & RES_PRF_REPLY),
1055 (stdout, ";; old answer:\n"),
1056 ans, (resplen > anssiz) ? anssiz : resplen);
1059 if (!(statp->options & RES_INSECURE1) &&
1060 !res_ourserver_p(statp, (struct sockaddr *)&from)) {
1062 * response from wrong server? ignore it.
1063 * XXX - potential security hazard could
1066 DprintQ((statp->options & RES_DEBUG) ||
1067 (statp->pfcode & RES_PRF_REPLY),
1068 (stdout, ";; not our server:\n"),
1069 ans, (resplen > anssiz) ? anssiz : resplen);
1072 #ifdef RES_USE_EDNS0
1073 if (anhp->rcode == FORMERR && (statp->options & RES_USE_EDNS0) != 0U) {
1075 * Do not retry if the server do not understand EDNS0.
1076 * The case has to be captured here, as FORMERR packet do not
1077 * carry query section, hence res_queriesmatch() returns 0.
1079 DprintQ(statp->options & RES_DEBUG,
1080 (stdout, "server rejected query with EDNS0:\n"),
1081 ans, (resplen > anssiz) ? anssiz : resplen);
1082 /* record the error */
1083 statp->_flags |= RES_F_EDNS0ERR;
1088 if (!(statp->options & RES_INSECURE2) &&
1089 !res_queriesmatch(buf, buf + buflen,
1090 ans, ans + anssiz)) {
1092 * response contains wrong query? ignore it.
1093 * XXX - potential security hazard could
1096 DprintQ((statp->options & RES_DEBUG) ||
1097 (statp->pfcode & RES_PRF_REPLY),
1098 (stdout, ";; wrong query name:\n"),
1099 ans, (resplen > anssiz) ? anssiz : resplen);
1102 if (anhp->rcode == SERVFAIL ||
1103 anhp->rcode == NOTIMP ||
1104 anhp->rcode == REFUSED) {
1105 DprintQ(statp->options & RES_DEBUG,
1106 (stdout, "server rejected query:\n"),
1107 ans, (resplen > anssiz) ? anssiz : resplen);
1109 /* don't retry if called from dig */
1113 if (!(statp->options & RES_IGNTC) && anhp->tc) {
1115 * To get the rest of answer,
1116 * use TCP with same server.
1118 Dprint(statp->options & RES_DEBUG,
1119 (stdout, ";; truncated answer\n"));
1125 * All is well, or the error is fatal. Signal that the
1126 * next nameserver ought not be tried.
1132 Aerror(const res_state statp, FILE *file, const char *string, int error,
1133 const struct sockaddr *address, int alen)
1136 char hbuf[NI_MAXHOST];
1137 char sbuf[NI_MAXSERV];
1141 if ((statp->options & RES_DEBUG) != 0U) {
1142 if (getnameinfo(address, alen, hbuf, sizeof(hbuf),
1143 sbuf, sizeof(sbuf), niflags)) {
1144 strncpy(hbuf, "?", sizeof(hbuf) - 1);
1145 hbuf[sizeof(hbuf) - 1] = '\0';
1146 strncpy(sbuf, "?", sizeof(sbuf) - 1);
1147 sbuf[sizeof(sbuf) - 1] = '\0';
1149 fprintf(file, "res_send: %s ([%s].%s): %s\n",
1150 string, hbuf, sbuf, strerror(error));
1156 Perror(const res_state statp, FILE *file, const char *string, int error) {
1159 if ((statp->options & RES_DEBUG) != 0U)
1160 fprintf(file, "res_send: %s: %s\n",
1161 string, strerror(error));
1166 sock_eq(struct sockaddr *a, struct sockaddr *b) {
1167 struct sockaddr_in *a4, *b4;
1168 struct sockaddr_in6 *a6, *b6;
1170 if (a->sa_family != b->sa_family)
1172 switch (a->sa_family) {
1174 a4 = (struct sockaddr_in *)a;
1175 b4 = (struct sockaddr_in *)b;
1176 return a4->sin_port == b4->sin_port &&
1177 a4->sin_addr.s_addr == b4->sin_addr.s_addr;
1179 a6 = (struct sockaddr_in6 *)a;
1180 b6 = (struct sockaddr_in6 *)b;
1181 return a6->sin6_port == b6->sin6_port &&
1182 #ifdef HAVE_SIN6_SCOPE_ID
1183 a6->sin6_scope_id == b6->sin6_scope_id &&
1185 IN6_ARE_ADDR_EQUAL(&a6->sin6_addr, &b6->sin6_addr);
1191 #if defined(NEED_PSELECT) && !defined(USE_POLL) && !defined(USE_KQUEUE)
1192 /* XXX needs to move to the porting library. */
1194 pselect(int nfds, void *rfds, void *wfds, void *efds,
1195 struct timespec *tsp, const sigset_t *sigmask)
1197 struct timeval tv, *tvp;
1203 tv = evTimeVal(*tsp);
1207 sigprocmask(SIG_SETMASK, sigmask, &sigs);
1208 n = select(nfds, rfds, wfds, efds, tvp);
1210 sigprocmask(SIG_SETMASK, &sigs, NULL);
1212 *tsp = evTimeSpec(tv);