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"
82 #include "fd_setsize.h"
84 #include <sys/types.h>
85 #include <sys/param.h>
87 #include <sys/socket.h>
90 #include <netinet/in.h>
91 #include <arpa/nameser.h>
92 #include <arpa/inet.h>
103 #include <isc/eventlib.h>
105 #include "port_after.h"
108 #ifdef HAVE_STROPTS_H
112 #endif /* USE_POLL */
114 /* Options. Leave them on. */
116 #include "res_debug.h"
117 #include "res_private.h"
119 #define EXT(res) ((res)->_u._ext)
122 static const int highestFD = FD_SETSIZE - 1;
124 static int highestFD = 0;
129 static int get_salen __P((const struct sockaddr *));
130 static struct sockaddr * get_nsaddr __P((res_state, size_t));
131 static int send_vc(res_state, const u_char *, int,
132 u_char *, int, int *, int);
133 static int send_dg(res_state, const u_char *, int,
134 u_char *, int, int *, int, int,
136 static void Aerror(const res_state, FILE *, const char *, int,
137 const struct sockaddr *, int);
138 static void Perror(const res_state, FILE *, const char *, int);
139 static int sock_eq(struct sockaddr *, struct sockaddr *);
140 #if defined(NEED_PSELECT) && !defined(USE_POLL)
141 static int pselect(int, void *, void *, void *,
145 void res_pquery(const res_state, const u_char *, int, FILE *);
147 static const int niflags = NI_NUMERICHOST | NI_NUMERICSERV;
152 * looks up "ina" in _res.ns_addr_list[]
159 *\li paul vixie, 29may94
162 res_ourserver_p(const res_state statp, const struct sockaddr *sa) {
163 const struct sockaddr_in *inp, *srv;
164 const struct sockaddr_in6 *in6p, *srv6;
167 switch (sa->sa_family) {
169 inp = (const struct sockaddr_in *)sa;
170 for (ns = 0; ns < statp->nscount; ns++) {
171 srv = (struct sockaddr_in *)get_nsaddr(statp, ns);
172 if (srv->sin_family == inp->sin_family &&
173 srv->sin_port == inp->sin_port &&
174 (srv->sin_addr.s_addr == INADDR_ANY ||
175 srv->sin_addr.s_addr == inp->sin_addr.s_addr))
180 if (EXT(statp).ext == NULL)
182 in6p = (const struct sockaddr_in6 *)sa;
183 for (ns = 0; ns < statp->nscount; ns++) {
184 srv6 = (struct sockaddr_in6 *)get_nsaddr(statp, ns);
185 if (srv6->sin6_family == in6p->sin6_family &&
186 srv6->sin6_port == in6p->sin6_port &&
187 #ifdef HAVE_SIN6_SCOPE_ID
188 (srv6->sin6_scope_id == 0 ||
189 srv6->sin6_scope_id == in6p->sin6_scope_id) &&
191 (IN6_IS_ADDR_UNSPECIFIED(&srv6->sin6_addr) ||
192 IN6_ARE_ADDR_EQUAL(&srv6->sin6_addr, &in6p->sin6_addr)))
203 * look for (name,type,class) in the query section of packet (buf,eom)
206 *\li buf + HFIXEDSZ <= eom
209 *\li -1 : format error
214 *\li paul vixie, 29may94
217 res_nameinquery(const char *name, int type, int class,
218 const u_char *buf, const u_char *eom)
220 const u_char *cp = buf + HFIXEDSZ;
221 int qdcount = ntohs(((const HEADER*)buf)->qdcount);
223 while (qdcount-- > 0) {
224 char tname[MAXDNAME+1];
225 int n, ttype, tclass;
227 n = dn_expand(buf, eom, cp, tname, sizeof tname);
231 if (cp + 2 * INT16SZ > eom)
233 ttype = ns_get16(cp); cp += INT16SZ;
234 tclass = ns_get16(cp); cp += INT16SZ;
235 if (ttype == type && tclass == class &&
236 ns_samename(tname, name) == 1)
243 * is there a 1:1 mapping of (name,type,class)
244 * in (buf1,eom1) and (buf2,eom2)?
247 *\li -1 : format error
248 *\li 0 : not a 1:1 mapping
249 *\li >0 : is a 1:1 mapping
252 *\li paul vixie, 29may94
255 res_queriesmatch(const u_char *buf1, const u_char *eom1,
256 const u_char *buf2, const u_char *eom2)
258 const u_char *cp = buf1 + HFIXEDSZ;
259 int qdcount = ntohs(((const HEADER*)buf1)->qdcount);
261 if (buf1 + HFIXEDSZ > eom1 || buf2 + HFIXEDSZ > eom2)
265 * Only header section present in replies to
266 * dynamic update packets.
268 if ((((const HEADER *)buf1)->opcode == ns_o_update) &&
269 (((const HEADER *)buf2)->opcode == ns_o_update))
272 if (qdcount != ntohs(((const HEADER*)buf2)->qdcount))
274 while (qdcount-- > 0) {
275 char tname[MAXDNAME+1];
276 int n, ttype, tclass;
278 n = dn_expand(buf1, eom1, cp, tname, sizeof tname);
282 if (cp + 2 * INT16SZ > eom1)
284 ttype = ns_get16(cp); cp += INT16SZ;
285 tclass = ns_get16(cp); cp += INT16SZ;
286 if (!res_nameinquery(tname, ttype, tclass, buf2, eom2))
293 res_nsend(res_state statp,
294 const u_char *buf, int buflen, u_char *ans, int anssiz)
296 int gotsomewhere, terrno, tries, v_circuit, resplen, ns, n;
297 char abuf[NI_MAXHOST];
300 highestFD = sysconf(_SC_OPEN_MAX) - 1;
303 /* No name servers or res_init() failure */
304 if (statp->nscount == 0 || EXT(statp).ext == NULL) {
308 if (anssiz < HFIXEDSZ) {
312 DprintQ((statp->options & RES_DEBUG) || (statp->pfcode & RES_PRF_QUERY),
313 (stdout, ";; res_send()\n"), buf, buflen);
314 v_circuit = (statp->options & RES_USEVC) || buflen > PACKETSZ;
319 * If the ns_addr_list in the resolver context has changed, then
320 * invalidate our cached copy and the associated timing data.
322 if (EXT(statp).nscount != 0) {
324 struct sockaddr_storage peer;
325 ISC_SOCKLEN_T peerlen;
327 if (EXT(statp).nscount != statp->nscount)
330 for (ns = 0; ns < statp->nscount; ns++) {
331 if (statp->nsaddr_list[ns].sin_family &&
332 !sock_eq((struct sockaddr *)&statp->nsaddr_list[ns],
333 (struct sockaddr *)&EXT(statp).ext->nsaddrs[ns])) {
338 if (EXT(statp).nssocks[ns] == -1)
340 peerlen = sizeof(peer);
341 if (getsockname(EXT(statp).nssocks[ns],
342 (struct sockaddr *)&peer, &peerlen) < 0) {
346 if (!sock_eq((struct sockaddr *)&peer,
347 get_nsaddr(statp, ns))) {
354 EXT(statp).nscount = 0;
359 * Maybe initialize our private copy of the ns_addr_list.
361 if (EXT(statp).nscount == 0) {
362 for (ns = 0; ns < statp->nscount; ns++) {
363 EXT(statp).nstimes[ns] = RES_MAXTIME;
364 EXT(statp).nssocks[ns] = -1;
365 if (!statp->nsaddr_list[ns].sin_family)
367 EXT(statp).ext->nsaddrs[ns].sin =
368 statp->nsaddr_list[ns];
370 EXT(statp).nscount = statp->nscount;
374 * Some resolvers want to even out the load on their nameservers.
375 * Note that RES_BLAST overrides RES_ROTATE.
377 if ((statp->options & RES_ROTATE) != 0U &&
378 (statp->options & RES_BLAST) == 0U) {
379 union res_sockaddr_union inu;
380 struct sockaddr_in ina;
381 int lastns = statp->nscount - 1;
385 if (EXT(statp).ext != NULL)
386 inu = EXT(statp).ext->nsaddrs[0];
387 ina = statp->nsaddr_list[0];
388 fd = EXT(statp).nssocks[0];
389 nstime = EXT(statp).nstimes[0];
390 for (ns = 0; ns < lastns; ns++) {
391 if (EXT(statp).ext != NULL)
392 EXT(statp).ext->nsaddrs[ns] =
393 EXT(statp).ext->nsaddrs[ns + 1];
394 statp->nsaddr_list[ns] = statp->nsaddr_list[ns + 1];
395 EXT(statp).nssocks[ns] = EXT(statp).nssocks[ns + 1];
396 EXT(statp).nstimes[ns] = EXT(statp).nstimes[ns + 1];
398 if (EXT(statp).ext != NULL)
399 EXT(statp).ext->nsaddrs[lastns] = inu;
400 statp->nsaddr_list[lastns] = ina;
401 EXT(statp).nssocks[lastns] = fd;
402 EXT(statp).nstimes[lastns] = nstime;
406 * Send request, RETRY times, or until successful.
408 for (tries = 0; tries < statp->retry; tries++) {
409 for (ns = 0; ns < statp->nscount; ns++) {
410 struct sockaddr *nsap;
412 nsap = get_nsaddr(statp, ns);
413 nsaplen = get_salen(nsap);
414 statp->_flags &= ~RES_F_LASTMASK;
415 statp->_flags |= (ns << RES_F_LASTSHIFT);
418 int done = 0, loops = 0;
423 act = (*statp->qhook)(&nsap, &buf, &buflen,
424 ans, anssiz, &resplen);
435 /* give the hook another try */
436 if (++loops < 42) /*doug adams*/
447 Dprint(((statp->options & RES_DEBUG) &&
448 getnameinfo(nsap, nsaplen, abuf, sizeof(abuf),
449 NULL, 0, niflags) == 0),
450 (stdout, ";; Querying server (# %d) address = %s\n",
455 /* Use VC; at most one attempt per server. */
456 tries = statp->retry;
457 n = send_vc(statp, buf, buflen, ans, anssiz, &terrno,
466 n = send_dg(statp, buf, buflen, ans, anssiz, &terrno,
467 ns, tries, &v_circuit, &gotsomewhere);
477 Dprint((statp->options & RES_DEBUG) ||
478 ((statp->pfcode & RES_PRF_REPLY) &&
479 (statp->pfcode & RES_PRF_HEAD1)),
480 (stdout, ";; got answer:\n"));
482 DprintQ((statp->options & RES_DEBUG) ||
483 (statp->pfcode & RES_PRF_REPLY),
485 ans, (resplen > anssiz) ? anssiz : resplen);
488 * If we have temporarily opened a virtual circuit,
489 * or if we haven't been asked to keep a socket open,
492 if ((v_circuit && (statp->options & RES_USEVC) == 0U) ||
493 (statp->options & RES_STAYOPEN) == 0U) {
497 int done = 0, loops = 0;
502 act = (*statp->rhook)(nsap, buf, buflen,
503 ans, anssiz, &resplen);
513 /* give the hook another try */
514 if (++loops < 42) /*doug adams*/
532 errno = ECONNREFUSED; /*%< no nameservers found */
534 errno = ETIMEDOUT; /*%< no answer obtained */
547 const struct sockaddr *sa;
551 /* There are people do not set sa_len. Be forgiving to them. */
556 if (sa->sa_family == AF_INET)
557 return (sizeof(struct sockaddr_in));
558 else if (sa->sa_family == AF_INET6)
559 return (sizeof(struct sockaddr_in6));
561 return (0); /*%< unknown, die on connect */
565 * pick appropriate nsaddr_list for use. see res_init() for initialization.
567 static struct sockaddr *
573 if (!statp->nsaddr_list[n].sin_family && EXT(statp).ext) {
575 * - EXT(statp).ext->nsaddrs[n] holds an address that is larger
576 * than struct sockaddr, and
577 * - user code did not update statp->nsaddr_list[n].
579 return (struct sockaddr *)(void *)&EXT(statp).ext->nsaddrs[n];
582 * - user code updated statp->nsaddr_list[n], or
583 * - statp->nsaddr_list[n] has the same content as
584 * EXT(statp).ext->nsaddrs[n].
586 return (struct sockaddr *)(void *)&statp->nsaddr_list[n];
591 send_vc(res_state statp,
592 const u_char *buf, int buflen, u_char *ans, int anssiz,
595 const HEADER *hp = (const HEADER *) buf;
596 HEADER *anhp = (HEADER *) ans;
597 struct sockaddr *nsap;
599 int truncating, connreset, resplen, n;
608 nsap = get_nsaddr(statp, ns);
609 nsaplen = get_salen(nsap);
615 /* Are we still talking to whom we want to talk to? */
616 if (statp->_vcsock >= 0 && (statp->_flags & RES_F_VC) != 0) {
617 struct sockaddr_storage peer;
618 ISC_SOCKLEN_T size = sizeof peer;
620 if (getpeername(statp->_vcsock,
621 (struct sockaddr *)&peer, &size) < 0 ||
622 !sock_eq((struct sockaddr *)&peer, nsap)) {
624 statp->_flags &= ~RES_F_VC;
628 if (statp->_vcsock < 0 || (statp->_flags & RES_F_VC) == 0) {
629 if (statp->_vcsock >= 0)
632 statp->_vcsock = socket(nsap->sa_family, SOCK_STREAM, 0);
633 if (statp->_vcsock > highestFD) {
637 if (statp->_vcsock < 0) {
639 case EPROTONOSUPPORT:
644 Perror(statp, stderr, "socket(vc)", errno);
648 Perror(statp, stderr, "socket(vc)", errno);
654 * Disable generation of SIGPIPE when writing to a closed
655 * socket. Write should return -1 and set errno to EPIPE
658 * Push on even if setsockopt(SO_NOSIGPIPE) fails.
660 (void)setsockopt(statp->_vcsock, SOL_SOCKET, SO_NOSIGPIPE, &on,
664 if (connect(statp->_vcsock, nsap, nsaplen) < 0) {
666 Aerror(statp, stderr, "connect/vc", errno, nsap,
671 statp->_flags |= RES_F_VC;
675 * Send length & message
677 ns_put16((u_short)buflen, (u_char*)&len);
678 iov[0] = evConsIovec(&len, INT16SZ);
680 iov[1] = evConsIovec(tmp, buflen);
681 if (writev(statp->_vcsock, iov, 2) != (INT16SZ + buflen)) {
683 Perror(statp, stderr, "write failed", errno);
688 * Receive length & response
693 while ((n = read(statp->_vcsock, (char *)cp, (int)len)) > 0) {
700 Perror(statp, stderr, "read failed", errno);
703 * A long running process might get its TCP
704 * connection reset if the remote server was
705 * restarted. Requery the server instead of
706 * trying a new one. When there is only one
707 * server, this means that a query might work
708 * instead of failing. We only allow one reset
709 * per query to prevent looping.
711 if (*terrno == ECONNRESET && !connreset) {
719 resplen = ns_get16(ans);
720 if (resplen > anssiz) {
721 Dprint(statp->options & RES_DEBUG,
722 (stdout, ";; response truncated\n")
728 if (len < HFIXEDSZ) {
730 * Undersized message.
732 Dprint(statp->options & RES_DEBUG,
733 (stdout, ";; undersized: %d\n", len));
739 while (len != 0 && (n = read(statp->_vcsock, (char *)cp, (int)len)) > 0){
745 Perror(statp, stderr, "read(vc)", errno);
751 * Flush rest of answer so connection stays in synch.
754 len = resplen - anssiz;
758 n = read(statp->_vcsock, junk,
759 (len > sizeof junk) ? sizeof junk : len);
767 * If the calling applicating has bailed out of
768 * a previous call and failed to arrange to have
769 * the circuit closed or the server has got
770 * itself confused, then drop the packet and
771 * wait for the correct one.
773 if (hp->id != anhp->id) {
774 DprintQ((statp->options & RES_DEBUG) ||
775 (statp->pfcode & RES_PRF_REPLY),
776 (stdout, ";; old answer (unexpected):\n"),
777 ans, (resplen > anssiz) ? anssiz: resplen);
782 * All is well, or the error is fatal. Signal that the
783 * next nameserver ought not be tried.
789 send_dg(res_state statp, const u_char *buf, int buflen, u_char *ans,
790 int anssiz, int *terrno, int ns, int tries, int *v_circuit,
793 const HEADER *hp = (const HEADER *) buf;
794 HEADER *anhp = (HEADER *) ans;
795 const struct sockaddr *nsap;
797 struct timespec now, timeout, finish;
798 struct sockaddr_storage from;
799 ISC_SOCKLEN_T fromlen;
800 int resplen, seconds, n, s;
803 struct pollfd pollfd;
808 nsap = get_nsaddr(statp, ns);
809 nsaplen = get_salen(nsap);
810 if (EXT(statp).nssocks[ns] == -1) {
811 EXT(statp).nssocks[ns] = socket(nsap->sa_family, SOCK_DGRAM, 0);
812 if (EXT(statp).nssocks[ns] > highestFD) {
816 if (EXT(statp).nssocks[ns] < 0) {
818 case EPROTONOSUPPORT:
823 Perror(statp, stderr, "socket(dg)", errno);
827 Perror(statp, stderr, "socket(dg)", errno);
831 #ifndef CANNOT_CONNECT_DGRAM
833 * On a 4.3BSD+ machine (client and server,
834 * actually), sending to a nameserver datagram
835 * port with no nameserver will cause an
836 * ICMP port unreachable message to be returned.
837 * If our datagram socket is "connected" to the
838 * server, we get an ECONNREFUSED error on the next
839 * socket operation, and select returns if the
840 * error message is received. We can thus detect
841 * the absence of a nameserver without timing out.
843 if (connect(EXT(statp).nssocks[ns], nsap, nsaplen) < 0) {
844 Aerror(statp, stderr, "connect(dg)", errno, nsap,
849 #endif /* !CANNOT_CONNECT_DGRAM */
850 Dprint(statp->options & RES_DEBUG,
851 (stdout, ";; new DG socket\n"))
853 s = EXT(statp).nssocks[ns];
854 #ifndef CANNOT_CONNECT_DGRAM
855 if (send(s, (const char*)buf, buflen, 0) != buflen) {
856 Perror(statp, stderr, "send", errno);
860 #else /* !CANNOT_CONNECT_DGRAM */
861 if (sendto(s, (const char*)buf, buflen, 0, nsap, nsaplen) != buflen)
863 Aerror(statp, stderr, "sendto", errno, nsap, nsaplen);
867 #endif /* !CANNOT_CONNECT_DGRAM */
872 seconds = (statp->retrans << tries);
874 seconds /= statp->nscount;
878 timeout = evConsTime(seconds, 0);
879 finish = evAddTime(now, timeout);
887 if (evCmpTime(finish, now) > 0)
888 timeout = evSubTime(finish, now);
890 timeout = evConsTime(0, 0);
891 n = pselect(s + 1, &dsmask, NULL, NULL, &timeout, NULL);
893 timeout = evSubTime(finish, now);
894 if (timeout.tv_sec < 0)
895 timeout = evConsTime(0, 0);
896 polltimeout = 1000*timeout.tv_sec +
897 timeout.tv_nsec/1000000;
899 pollfd.events = POLLRDNORM;
900 n = poll(&pollfd, 1, polltimeout);
901 #endif /* USE_POLL */
904 Dprint(statp->options & RES_DEBUG, (stdout, ";; timeout\n"));
912 Perror(statp, stderr, "select", errno);
914 Perror(statp, stderr, "poll", errno);
915 #endif /* USE_POLL */
920 fromlen = sizeof(from);
921 resplen = recvfrom(s, (char*)ans, anssiz,0,
922 (struct sockaddr *)&from, &fromlen);
924 Perror(statp, stderr, "recvfrom", errno);
929 if (resplen < HFIXEDSZ) {
931 * Undersized message.
933 Dprint(statp->options & RES_DEBUG,
934 (stdout, ";; undersized: %d\n",
940 if (hp->id != anhp->id) {
942 * response from old query, ignore it.
943 * XXX - potential security hazard could
946 DprintQ((statp->options & RES_DEBUG) ||
947 (statp->pfcode & RES_PRF_REPLY),
948 (stdout, ";; old answer:\n"),
949 ans, (resplen > anssiz) ? anssiz : resplen);
952 if (!(statp->options & RES_INSECURE1) &&
953 !res_ourserver_p(statp, (struct sockaddr *)&from)) {
955 * response from wrong server? ignore it.
956 * XXX - potential security hazard could
959 DprintQ((statp->options & RES_DEBUG) ||
960 (statp->pfcode & RES_PRF_REPLY),
961 (stdout, ";; not our server:\n"),
962 ans, (resplen > anssiz) ? anssiz : resplen);
966 if (anhp->rcode == FORMERR && (statp->options & RES_USE_EDNS0) != 0U) {
968 * Do not retry if the server do not understand EDNS0.
969 * The case has to be captured here, as FORMERR packet do not
970 * carry query section, hence res_queriesmatch() returns 0.
972 DprintQ(statp->options & RES_DEBUG,
973 (stdout, "server rejected query with EDNS0:\n"),
974 ans, (resplen > anssiz) ? anssiz : resplen);
975 /* record the error */
976 statp->_flags |= RES_F_EDNS0ERR;
981 if (!(statp->options & RES_INSECURE2) &&
982 !res_queriesmatch(buf, buf + buflen,
983 ans, ans + anssiz)) {
985 * response contains wrong query? ignore it.
986 * XXX - potential security hazard could
989 DprintQ((statp->options & RES_DEBUG) ||
990 (statp->pfcode & RES_PRF_REPLY),
991 (stdout, ";; wrong query name:\n"),
992 ans, (resplen > anssiz) ? anssiz : resplen);
995 if (anhp->rcode == SERVFAIL ||
996 anhp->rcode == NOTIMP ||
997 anhp->rcode == REFUSED) {
998 DprintQ(statp->options & RES_DEBUG,
999 (stdout, "server rejected query:\n"),
1000 ans, (resplen > anssiz) ? anssiz : resplen);
1002 /* don't retry if called from dig */
1006 if (!(statp->options & RES_IGNTC) && anhp->tc) {
1008 * To get the rest of answer,
1009 * use TCP with same server.
1011 Dprint(statp->options & RES_DEBUG,
1012 (stdout, ";; truncated answer\n"));
1018 * All is well, or the error is fatal. Signal that the
1019 * next nameserver ought not be tried.
1025 Aerror(const res_state statp, FILE *file, const char *string, int error,
1026 const struct sockaddr *address, int alen)
1029 char hbuf[NI_MAXHOST];
1030 char sbuf[NI_MAXSERV];
1034 if ((statp->options & RES_DEBUG) != 0U) {
1035 if (getnameinfo(address, alen, hbuf, sizeof(hbuf),
1036 sbuf, sizeof(sbuf), niflags)) {
1037 strncpy(hbuf, "?", sizeof(hbuf) - 1);
1038 hbuf[sizeof(hbuf) - 1] = '\0';
1039 strncpy(sbuf, "?", sizeof(sbuf) - 1);
1040 sbuf[sizeof(sbuf) - 1] = '\0';
1042 fprintf(file, "res_send: %s ([%s].%s): %s\n",
1043 string, hbuf, sbuf, strerror(error));
1049 Perror(const res_state statp, FILE *file, const char *string, int error) {
1052 if ((statp->options & RES_DEBUG) != 0U)
1053 fprintf(file, "res_send: %s: %s\n",
1054 string, strerror(error));
1059 sock_eq(struct sockaddr *a, struct sockaddr *b) {
1060 struct sockaddr_in *a4, *b4;
1061 struct sockaddr_in6 *a6, *b6;
1063 if (a->sa_family != b->sa_family)
1065 switch (a->sa_family) {
1067 a4 = (struct sockaddr_in *)a;
1068 b4 = (struct sockaddr_in *)b;
1069 return a4->sin_port == b4->sin_port &&
1070 a4->sin_addr.s_addr == b4->sin_addr.s_addr;
1072 a6 = (struct sockaddr_in6 *)a;
1073 b6 = (struct sockaddr_in6 *)b;
1074 return a6->sin6_port == b6->sin6_port &&
1075 #ifdef HAVE_SIN6_SCOPE_ID
1076 a6->sin6_scope_id == b6->sin6_scope_id &&
1078 IN6_ARE_ADDR_EQUAL(&a6->sin6_addr, &b6->sin6_addr);
1084 #if defined(NEED_PSELECT) && !defined(USE_POLL)
1085 /* XXX needs to move to the porting library. */
1087 pselect(int nfds, void *rfds, void *wfds, void *efds,
1088 struct timespec *tsp, const sigset_t *sigmask)
1090 struct timeval tv, *tvp;
1096 tv = evTimeVal(*tsp);
1100 sigprocmask(SIG_SETMASK, sigmask, &sigs);
1101 n = select(nfds, rfds, wfds, efds, tvp);
1103 sigprocmask(SIG_SETMASK, &sigs, NULL);
1105 *tsp = evTimeSpec(tv);