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"
81 #include "fd_setsize.h"
83 #include <sys/types.h>
84 #include <sys/param.h>
86 #include <sys/socket.h>
89 #include <netinet/in.h>
90 #include <arpa/nameser.h>
91 #include <arpa/inet.h>
102 #include <isc/eventlib.h>
104 #include "port_after.h"
107 #ifdef HAVE_STROPTS_H
111 #endif /* USE_POLL */
113 /* Options. Leave them on. */
115 #include "res_debug.h"
116 #include "res_private.h"
118 #define EXT(res) ((res)->_u._ext)
121 static const int highestFD = FD_SETSIZE - 1;
123 static int highestFD = 0;
128 static int get_salen __P((const struct sockaddr *));
129 static struct sockaddr * get_nsaddr __P((res_state, size_t));
130 static int send_vc(res_state, const u_char *, int,
131 u_char *, int, int *, int);
132 static int send_dg(res_state, const u_char *, int,
133 u_char *, int, int *, int, int,
135 static void Aerror(const res_state, FILE *, const char *, int,
136 const struct sockaddr *, int);
137 static void Perror(const res_state, FILE *, const char *, int);
138 static int sock_eq(struct sockaddr *, struct sockaddr *);
139 #if defined(NEED_PSELECT) && !defined(USE_POLL)
140 static int pselect(int, void *, void *, void *,
144 void res_pquery(const res_state, const u_char *, int, FILE *);
146 static const int niflags = NI_NUMERICHOST | NI_NUMERICSERV;
151 * res_isourserver(ina)
152 * looks up "ina" in _res.ns_addr_list[]
157 * paul vixie, 29may94
160 res_ourserver_p(const res_state statp, const struct sockaddr *sa) {
161 const struct sockaddr_in *inp, *srv;
162 const struct sockaddr_in6 *in6p, *srv6;
165 switch (sa->sa_family) {
167 inp = (const struct sockaddr_in *)sa;
168 for (ns = 0; ns < statp->nscount; ns++) {
169 srv = (struct sockaddr_in *)get_nsaddr(statp, ns);
170 if (srv->sin_family == inp->sin_family &&
171 srv->sin_port == inp->sin_port &&
172 (srv->sin_addr.s_addr == INADDR_ANY ||
173 srv->sin_addr.s_addr == inp->sin_addr.s_addr))
178 if (EXT(statp).ext == NULL)
180 in6p = (const struct sockaddr_in6 *)sa;
181 for (ns = 0; ns < statp->nscount; ns++) {
182 srv6 = (struct sockaddr_in6 *)get_nsaddr(statp, ns);
183 if (srv6->sin6_family == in6p->sin6_family &&
184 srv6->sin6_port == in6p->sin6_port &&
185 #ifdef HAVE_SIN6_SCOPE_ID
186 (srv6->sin6_scope_id == 0 ||
187 srv6->sin6_scope_id == in6p->sin6_scope_id) &&
189 (IN6_IS_ADDR_UNSPECIFIED(&srv6->sin6_addr) ||
190 IN6_ARE_ADDR_EQUAL(&srv6->sin6_addr, &in6p->sin6_addr)))
201 * res_nameinquery(name, type, class, buf, eom)
202 * look for (name,type,class) in the query section of packet (buf,eom)
204 * buf + HFIXEDSZ <= eom
210 * paul vixie, 29may94
213 res_nameinquery(const char *name, int type, int class,
214 const u_char *buf, const u_char *eom)
216 const u_char *cp = buf + HFIXEDSZ;
217 int qdcount = ntohs(((const HEADER*)buf)->qdcount);
219 while (qdcount-- > 0) {
220 char tname[MAXDNAME+1];
221 int n, ttype, tclass;
223 n = dn_expand(buf, eom, cp, tname, sizeof tname);
227 if (cp + 2 * INT16SZ > eom)
229 ttype = ns_get16(cp); cp += INT16SZ;
230 tclass = ns_get16(cp); cp += INT16SZ;
231 if (ttype == type && tclass == class &&
232 ns_samename(tname, name) == 1)
239 * res_queriesmatch(buf1, eom1, buf2, eom2)
240 * is there a 1:1 mapping of (name,type,class)
241 * in (buf1,eom1) and (buf2,eom2)?
244 * 0 : not a 1:1 mapping
245 * >0 : is a 1:1 mapping
247 * paul vixie, 29may94
250 res_queriesmatch(const u_char *buf1, const u_char *eom1,
251 const u_char *buf2, const u_char *eom2)
253 const u_char *cp = buf1 + HFIXEDSZ;
254 int qdcount = ntohs(((const HEADER*)buf1)->qdcount);
256 if (buf1 + HFIXEDSZ > eom1 || buf2 + HFIXEDSZ > eom2)
260 * Only header section present in replies to
261 * dynamic update packets.
263 if ((((const HEADER *)buf1)->opcode == ns_o_update) &&
264 (((const HEADER *)buf2)->opcode == ns_o_update))
267 if (qdcount != ntohs(((const HEADER*)buf2)->qdcount))
269 while (qdcount-- > 0) {
270 char tname[MAXDNAME+1];
271 int n, ttype, tclass;
273 n = dn_expand(buf1, eom1, cp, tname, sizeof tname);
277 if (cp + 2 * INT16SZ > eom1)
279 ttype = ns_get16(cp); cp += INT16SZ;
280 tclass = ns_get16(cp); cp += INT16SZ;
281 if (!res_nameinquery(tname, ttype, tclass, buf2, eom2))
288 res_nsend(res_state statp,
289 const u_char *buf, int buflen, u_char *ans, int anssiz)
291 int gotsomewhere, terrno, try, v_circuit, resplen, ns, n;
292 char abuf[NI_MAXHOST];
295 highestFD = sysconf(_SC_OPEN_MAX) - 1;
298 /* No name servers or res_init() failure */
299 if (statp->nscount == 0 || EXT(statp).ext == NULL) {
303 if (anssiz < HFIXEDSZ) {
307 DprintQ((statp->options & RES_DEBUG) || (statp->pfcode & RES_PRF_QUERY),
308 (stdout, ";; res_send()\n"), buf, buflen);
309 v_circuit = (statp->options & RES_USEVC) || buflen > PACKETSZ;
314 * If the ns_addr_list in the resolver context has changed, then
315 * invalidate our cached copy and the associated timing data.
317 if (EXT(statp).nscount != 0) {
319 struct sockaddr_storage peer;
320 ISC_SOCKLEN_T peerlen;
322 if (EXT(statp).nscount != statp->nscount)
325 for (ns = 0; ns < statp->nscount; ns++) {
326 if (statp->nsaddr_list[ns].sin_family &&
327 !sock_eq((struct sockaddr *)&statp->nsaddr_list[ns],
328 (struct sockaddr *)&EXT(statp).ext->nsaddrs[ns])) {
333 if (EXT(statp).nssocks[ns] == -1)
335 peerlen = sizeof(peer);
336 if (getsockname(EXT(statp).nssocks[ns],
337 (struct sockaddr *)&peer, &peerlen) < 0) {
341 if (!sock_eq((struct sockaddr *)&peer,
342 get_nsaddr(statp, ns))) {
349 EXT(statp).nscount = 0;
354 * Maybe initialize our private copy of the ns_addr_list.
356 if (EXT(statp).nscount == 0) {
357 for (ns = 0; ns < statp->nscount; ns++) {
358 EXT(statp).nstimes[ns] = RES_MAXTIME;
359 EXT(statp).nssocks[ns] = -1;
360 if (!statp->nsaddr_list[ns].sin_family)
362 EXT(statp).ext->nsaddrs[ns].sin =
363 statp->nsaddr_list[ns];
365 EXT(statp).nscount = statp->nscount;
369 * Some resolvers want to even out the load on their nameservers.
370 * Note that RES_BLAST overrides RES_ROTATE.
372 if ((statp->options & RES_ROTATE) != 0U &&
373 (statp->options & RES_BLAST) == 0U) {
374 union res_sockaddr_union inu;
375 struct sockaddr_in ina;
376 int lastns = statp->nscount - 1;
380 if (EXT(statp).ext != NULL)
381 inu = EXT(statp).ext->nsaddrs[0];
382 ina = statp->nsaddr_list[0];
383 fd = EXT(statp).nssocks[0];
384 nstime = EXT(statp).nstimes[0];
385 for (ns = 0; ns < lastns; ns++) {
386 if (EXT(statp).ext != NULL)
387 EXT(statp).ext->nsaddrs[ns] =
388 EXT(statp).ext->nsaddrs[ns + 1];
389 statp->nsaddr_list[ns] = statp->nsaddr_list[ns + 1];
390 EXT(statp).nssocks[ns] = EXT(statp).nssocks[ns + 1];
391 EXT(statp).nstimes[ns] = EXT(statp).nstimes[ns + 1];
393 if (EXT(statp).ext != NULL)
394 EXT(statp).ext->nsaddrs[lastns] = inu;
395 statp->nsaddr_list[lastns] = ina;
396 EXT(statp).nssocks[lastns] = fd;
397 EXT(statp).nstimes[lastns] = nstime;
401 * Send request, RETRY times, or until successful.
403 for (try = 0; try < statp->retry; try++) {
404 for (ns = 0; ns < statp->nscount; ns++) {
405 struct sockaddr *nsap;
407 nsap = get_nsaddr(statp, ns);
408 nsaplen = get_salen(nsap);
409 statp->_flags &= ~RES_F_LASTMASK;
410 statp->_flags |= (ns << RES_F_LASTSHIFT);
413 int done = 0, loops = 0;
418 act = (*statp->qhook)(&nsap, &buf, &buflen,
419 ans, anssiz, &resplen);
430 /* give the hook another try */
431 if (++loops < 42) /*doug adams*/
442 Dprint(((statp->options & RES_DEBUG) &&
443 getnameinfo(nsap, nsaplen, abuf, sizeof(abuf),
444 NULL, 0, niflags) == 0),
445 (stdout, ";; Querying server (# %d) address = %s\n",
450 /* Use VC; at most one attempt per server. */
452 n = send_vc(statp, buf, buflen, ans, anssiz, &terrno,
461 n = send_dg(statp, buf, buflen, ans, anssiz, &terrno,
462 ns, try, &v_circuit, &gotsomewhere);
472 Dprint((statp->options & RES_DEBUG) ||
473 ((statp->pfcode & RES_PRF_REPLY) &&
474 (statp->pfcode & RES_PRF_HEAD1)),
475 (stdout, ";; got answer:\n"));
477 DprintQ((statp->options & RES_DEBUG) ||
478 (statp->pfcode & RES_PRF_REPLY),
480 ans, (resplen > anssiz) ? anssiz : resplen);
483 * If we have temporarily opened a virtual circuit,
484 * or if we haven't been asked to keep a socket open,
487 if ((v_circuit && (statp->options & RES_USEVC) == 0U) ||
488 (statp->options & RES_STAYOPEN) == 0U) {
492 int done = 0, loops = 0;
497 act = (*statp->rhook)(nsap, buf, buflen,
498 ans, anssiz, &resplen);
508 /* give the hook another try */
509 if (++loops < 42) /*doug adams*/
527 errno = ECONNREFUSED; /* no nameservers found */
529 errno = ETIMEDOUT; /* no answer obtained */
542 const struct sockaddr *sa;
546 /* There are people do not set sa_len. Be forgiving to them. */
551 if (sa->sa_family == AF_INET)
552 return (sizeof(struct sockaddr_in));
553 else if (sa->sa_family == AF_INET6)
554 return (sizeof(struct sockaddr_in6));
556 return (0); /* unknown, die on connect */
560 * pick appropriate nsaddr_list for use. see res_init() for initialization.
562 static struct sockaddr *
568 if (!statp->nsaddr_list[n].sin_family && EXT(statp).ext) {
570 * - EXT(statp).ext->nsaddrs[n] holds an address that is larger
571 * than struct sockaddr, and
572 * - user code did not update statp->nsaddr_list[n].
574 return (struct sockaddr *)(void *)&EXT(statp).ext->nsaddrs[n];
577 * - user code updated statp->nsaddr_list[n], or
578 * - statp->nsaddr_list[n] has the same content as
579 * EXT(statp).ext->nsaddrs[n].
581 return (struct sockaddr *)(void *)&statp->nsaddr_list[n];
586 send_vc(res_state statp,
587 const u_char *buf, int buflen, u_char *ans, int anssiz,
590 const HEADER *hp = (const HEADER *) buf;
591 HEADER *anhp = (HEADER *) ans;
592 struct sockaddr *nsap;
594 int truncating, connreset, resplen, n;
600 nsap = get_nsaddr(statp, ns);
601 nsaplen = get_salen(nsap);
607 /* Are we still talking to whom we want to talk to? */
608 if (statp->_vcsock >= 0 && (statp->_flags & RES_F_VC) != 0) {
609 struct sockaddr_storage peer;
610 ISC_SOCKLEN_T size = sizeof peer;
612 if (getpeername(statp->_vcsock,
613 (struct sockaddr *)&peer, &size) < 0 ||
614 !sock_eq((struct sockaddr *)&peer, nsap)) {
616 statp->_flags &= ~RES_F_VC;
620 if (statp->_vcsock < 0 || (statp->_flags & RES_F_VC) == 0) {
621 if (statp->_vcsock >= 0)
624 statp->_vcsock = socket(nsap->sa_family, SOCK_STREAM, 0);
625 if (statp->_vcsock > highestFD) {
629 if (statp->_vcsock < 0) {
631 case EPROTONOSUPPORT:
636 Perror(statp, stderr, "socket(vc)", errno);
640 Perror(statp, stderr, "socket(vc)", errno);
645 if (connect(statp->_vcsock, nsap, nsaplen) < 0) {
647 Aerror(statp, stderr, "connect/vc", errno, nsap,
652 statp->_flags |= RES_F_VC;
656 * Send length & message
658 ns_put16((u_short)buflen, (u_char*)&len);
659 iov[0] = evConsIovec(&len, INT16SZ);
661 iov[1] = evConsIovec(tmp, buflen);
662 if (writev(statp->_vcsock, iov, 2) != (INT16SZ + buflen)) {
664 Perror(statp, stderr, "write failed", errno);
669 * Receive length & response
674 while ((n = read(statp->_vcsock, (char *)cp, (int)len)) > 0) {
681 Perror(statp, stderr, "read failed", errno);
684 * A long running process might get its TCP
685 * connection reset if the remote server was
686 * restarted. Requery the server instead of
687 * trying a new one. When there is only one
688 * server, this means that a query might work
689 * instead of failing. We only allow one reset
690 * per query to prevent looping.
692 if (*terrno == ECONNRESET && !connreset) {
700 resplen = ns_get16(ans);
701 if (resplen > anssiz) {
702 Dprint(statp->options & RES_DEBUG,
703 (stdout, ";; response truncated\n")
709 if (len < HFIXEDSZ) {
711 * Undersized message.
713 Dprint(statp->options & RES_DEBUG,
714 (stdout, ";; undersized: %d\n", len));
720 while (len != 0 && (n = read(statp->_vcsock, (char *)cp, (int)len)) > 0){
726 Perror(statp, stderr, "read(vc)", errno);
732 * Flush rest of answer so connection stays in synch.
735 len = resplen - anssiz;
739 n = read(statp->_vcsock, junk,
740 (len > sizeof junk) ? sizeof junk : len);
748 * If the calling applicating has bailed out of
749 * a previous call and failed to arrange to have
750 * the circuit closed or the server has got
751 * itself confused, then drop the packet and
752 * wait for the correct one.
754 if (hp->id != anhp->id) {
755 DprintQ((statp->options & RES_DEBUG) ||
756 (statp->pfcode & RES_PRF_REPLY),
757 (stdout, ";; old answer (unexpected):\n"),
758 ans, (resplen > anssiz) ? anssiz: resplen);
763 * All is well, or the error is fatal. Signal that the
764 * next nameserver ought not be tried.
770 send_dg(res_state statp, const u_char *buf, int buflen, u_char *ans,
771 int anssiz, int *terrno, int ns, int try, int *v_circuit,
774 const HEADER *hp = (const HEADER *) buf;
775 HEADER *anhp = (HEADER *) ans;
776 const struct sockaddr *nsap;
778 struct timespec now, timeout, finish;
779 struct sockaddr_storage from;
780 ISC_SOCKLEN_T fromlen;
781 int resplen, seconds, n, s;
784 struct pollfd pollfd;
789 nsap = get_nsaddr(statp, ns);
790 nsaplen = get_salen(nsap);
791 if (EXT(statp).nssocks[ns] == -1) {
792 EXT(statp).nssocks[ns] = socket(nsap->sa_family, SOCK_DGRAM, 0);
793 if (EXT(statp).nssocks[ns] > highestFD) {
797 if (EXT(statp).nssocks[ns] < 0) {
799 case EPROTONOSUPPORT:
804 Perror(statp, stderr, "socket(dg)", errno);
808 Perror(statp, stderr, "socket(dg)", errno);
812 #ifndef CANNOT_CONNECT_DGRAM
814 * On a 4.3BSD+ machine (client and server,
815 * actually), sending to a nameserver datagram
816 * port with no nameserver will cause an
817 * ICMP port unreachable message to be returned.
818 * If our datagram socket is "connected" to the
819 * server, we get an ECONNREFUSED error on the next
820 * socket operation, and select returns if the
821 * error message is received. We can thus detect
822 * the absence of a nameserver without timing out.
824 if (connect(EXT(statp).nssocks[ns], nsap, nsaplen) < 0) {
825 Aerror(statp, stderr, "connect(dg)", errno, nsap,
830 #endif /* !CANNOT_CONNECT_DGRAM */
831 Dprint(statp->options & RES_DEBUG,
832 (stdout, ";; new DG socket\n"))
834 s = EXT(statp).nssocks[ns];
835 #ifndef CANNOT_CONNECT_DGRAM
836 if (send(s, (const char*)buf, buflen, 0) != buflen) {
837 Perror(statp, stderr, "send", errno);
841 #else /* !CANNOT_CONNECT_DGRAM */
842 if (sendto(s, (const char*)buf, buflen, 0, nsap, nsaplen) != buflen)
844 Aerror(statp, stderr, "sendto", errno, nsap, nsaplen);
848 #endif /* !CANNOT_CONNECT_DGRAM */
853 seconds = (statp->retrans << try);
855 seconds /= statp->nscount;
859 timeout = evConsTime(seconds, 0);
860 finish = evAddTime(now, timeout);
868 if (evCmpTime(finish, now) > 0)
869 timeout = evSubTime(finish, now);
871 timeout = evConsTime(0, 0);
872 n = pselect(s + 1, &dsmask, NULL, NULL, &timeout, NULL);
874 timeout = evSubTime(finish, now);
875 if (timeout.tv_sec < 0)
876 timeout = evConsTime(0, 0);
877 polltimeout = 1000*timeout.tv_sec +
878 timeout.tv_nsec/1000000;
880 pollfd.events = POLLRDNORM;
881 n = poll(&pollfd, 1, polltimeout);
882 #endif /* USE_POLL */
885 Dprint(statp->options & RES_DEBUG, (stdout, ";; timeout\n"));
893 Perror(statp, stderr, "select", errno);
895 Perror(statp, stderr, "poll", errno);
896 #endif /* USE_POLL */
901 fromlen = sizeof(from);
902 resplen = recvfrom(s, (char*)ans, anssiz,0,
903 (struct sockaddr *)&from, &fromlen);
905 Perror(statp, stderr, "recvfrom", errno);
910 if (resplen < HFIXEDSZ) {
912 * Undersized message.
914 Dprint(statp->options & RES_DEBUG,
915 (stdout, ";; undersized: %d\n",
921 if (hp->id != anhp->id) {
923 * response from old query, ignore it.
924 * XXX - potential security hazard could
927 DprintQ((statp->options & RES_DEBUG) ||
928 (statp->pfcode & RES_PRF_REPLY),
929 (stdout, ";; old answer:\n"),
930 ans, (resplen > anssiz) ? anssiz : resplen);
933 if (!(statp->options & RES_INSECURE1) &&
934 !res_ourserver_p(statp, (struct sockaddr *)&from)) {
936 * response from wrong server? ignore it.
937 * XXX - potential security hazard could
940 DprintQ((statp->options & RES_DEBUG) ||
941 (statp->pfcode & RES_PRF_REPLY),
942 (stdout, ";; not our server:\n"),
943 ans, (resplen > anssiz) ? anssiz : resplen);
947 if (anhp->rcode == FORMERR && (statp->options & RES_USE_EDNS0) != 0U) {
949 * Do not retry if the server do not understand EDNS0.
950 * The case has to be captured here, as FORMERR packet do not
951 * carry query section, hence res_queriesmatch() returns 0.
953 DprintQ(statp->options & RES_DEBUG,
954 (stdout, "server rejected query with EDNS0:\n"),
955 ans, (resplen > anssiz) ? anssiz : resplen);
956 /* record the error */
957 statp->_flags |= RES_F_EDNS0ERR;
962 if (!(statp->options & RES_INSECURE2) &&
963 !res_queriesmatch(buf, buf + buflen,
964 ans, ans + anssiz)) {
966 * response contains wrong query? ignore it.
967 * XXX - potential security hazard could
970 DprintQ((statp->options & RES_DEBUG) ||
971 (statp->pfcode & RES_PRF_REPLY),
972 (stdout, ";; wrong query name:\n"),
973 ans, (resplen > anssiz) ? anssiz : resplen);
976 if (anhp->rcode == SERVFAIL ||
977 anhp->rcode == NOTIMP ||
978 anhp->rcode == REFUSED) {
979 DprintQ(statp->options & RES_DEBUG,
980 (stdout, "server rejected query:\n"),
981 ans, (resplen > anssiz) ? anssiz : resplen);
983 /* don't retry if called from dig */
987 if (!(statp->options & RES_IGNTC) && anhp->tc) {
989 * To get the rest of answer,
990 * use TCP with same server.
992 Dprint(statp->options & RES_DEBUG,
993 (stdout, ";; truncated answer\n"));
999 * All is well, or the error is fatal. Signal that the
1000 * next nameserver ought not be tried.
1006 Aerror(const res_state statp, FILE *file, const char *string, int error,
1007 const struct sockaddr *address, int alen)
1010 char hbuf[NI_MAXHOST];
1011 char sbuf[NI_MAXSERV];
1015 if ((statp->options & RES_DEBUG) != 0U) {
1016 if (getnameinfo(address, alen, hbuf, sizeof(hbuf),
1017 sbuf, sizeof(sbuf), niflags)) {
1018 strncpy(hbuf, "?", sizeof(hbuf) - 1);
1019 hbuf[sizeof(hbuf) - 1] = '\0';
1020 strncpy(sbuf, "?", sizeof(sbuf) - 1);
1021 sbuf[sizeof(sbuf) - 1] = '\0';
1023 fprintf(file, "res_send: %s ([%s].%s): %s\n",
1024 string, hbuf, sbuf, strerror(error));
1030 Perror(const res_state statp, FILE *file, const char *string, int error) {
1033 if ((statp->options & RES_DEBUG) != 0U)
1034 fprintf(file, "res_send: %s: %s\n",
1035 string, strerror(error));
1040 sock_eq(struct sockaddr *a, struct sockaddr *b) {
1041 struct sockaddr_in *a4, *b4;
1042 struct sockaddr_in6 *a6, *b6;
1044 if (a->sa_family != b->sa_family)
1046 switch (a->sa_family) {
1048 a4 = (struct sockaddr_in *)a;
1049 b4 = (struct sockaddr_in *)b;
1050 return a4->sin_port == b4->sin_port &&
1051 a4->sin_addr.s_addr == b4->sin_addr.s_addr;
1053 a6 = (struct sockaddr_in6 *)a;
1054 b6 = (struct sockaddr_in6 *)b;
1055 return a6->sin6_port == b6->sin6_port &&
1056 #ifdef HAVE_SIN6_SCOPE_ID
1057 a6->sin6_scope_id == b6->sin6_scope_id &&
1059 IN6_ARE_ADDR_EQUAL(&a6->sin6_addr, &b6->sin6_addr);
1065 #if defined(NEED_PSELECT) && !defined(USE_POLL)
1066 /* XXX needs to move to the porting library. */
1068 pselect(int nfds, void *rfds, void *wfds, void *efds,
1069 struct timespec *tsp, const sigset_t *sigmask)
1071 struct timeval tv, *tvp;
1077 tv = evTimeVal(*tsp);
1081 sigprocmask(SIG_SETMASK, sigmask, &sigs);
1082 n = select(nfds, rfds, wfds, efds, tvp);
1084 sigprocmask(SIG_SETMASK, &sigs, NULL);
1086 *tsp = evTimeSpec(tv);