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) 2004 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.7 2004/08/10 02:27:36 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"
106 /* Options. Leave them on. */
108 #include "res_debug.h"
109 #include "res_private.h"
111 #define EXT(res) ((res)->_u._ext)
113 static const int highestFD = FD_SETSIZE - 1;
117 static int get_salen __P((const struct sockaddr *));
118 static struct sockaddr * get_nsaddr __P((res_state, size_t));
119 static int send_vc(res_state, const u_char *, int,
120 u_char *, int, int *, int);
121 static int send_dg(res_state, const u_char *, int,
122 u_char *, int, int *, int,
124 static void Aerror(const res_state, FILE *, const char *, int,
125 const struct sockaddr *, int);
126 static void Perror(const res_state, FILE *, const char *, int);
127 static int sock_eq(struct sockaddr *, struct sockaddr *);
129 static int pselect(int, void *, void *, void *,
133 void res_pquery(const res_state, const u_char *, int, FILE *);
135 static const int niflags = NI_NUMERICHOST | NI_NUMERICSERV;
140 * res_isourserver(ina)
141 * looks up "ina" in _res.ns_addr_list[]
146 * paul vixie, 29may94
149 res_ourserver_p(const res_state statp, const struct sockaddr *sa) {
150 const struct sockaddr_in *inp, *srv;
151 const struct sockaddr_in6 *in6p, *srv6;
154 switch (sa->sa_family) {
156 inp = (const struct sockaddr_in *)sa;
157 for (ns = 0; ns < statp->nscount; ns++) {
158 srv = (struct sockaddr_in *)get_nsaddr(statp, ns);
159 if (srv->sin_family == inp->sin_family &&
160 srv->sin_port == inp->sin_port &&
161 (srv->sin_addr.s_addr == INADDR_ANY ||
162 srv->sin_addr.s_addr == inp->sin_addr.s_addr))
167 if (EXT(statp).ext == NULL)
169 in6p = (const struct sockaddr_in6 *)sa;
170 for (ns = 0; ns < statp->nscount; ns++) {
171 srv6 = (struct sockaddr_in6 *)get_nsaddr(statp, ns);
172 if (srv6->sin6_family == in6p->sin6_family &&
173 srv6->sin6_port == in6p->sin6_port &&
174 #ifdef HAVE_SIN6_SCOPE_ID
175 (srv6->sin6_scope_id == 0 ||
176 srv6->sin6_scope_id == in6p->sin6_scope_id) &&
178 (IN6_IS_ADDR_UNSPECIFIED(&srv6->sin6_addr) ||
179 IN6_ARE_ADDR_EQUAL(&srv6->sin6_addr, &in6p->sin6_addr)))
190 * res_nameinquery(name, type, class, buf, eom)
191 * look for (name,type,class) in the query section of packet (buf,eom)
193 * buf + HFIXEDSZ <= eom
199 * paul vixie, 29may94
202 res_nameinquery(const char *name, int type, int class,
203 const u_char *buf, const u_char *eom)
205 const u_char *cp = buf + HFIXEDSZ;
206 int qdcount = ntohs(((const HEADER*)buf)->qdcount);
208 while (qdcount-- > 0) {
209 char tname[MAXDNAME+1];
210 int n, ttype, tclass;
212 n = dn_expand(buf, eom, cp, tname, sizeof tname);
216 if (cp + 2 * INT16SZ > eom)
218 ttype = ns_get16(cp); cp += INT16SZ;
219 tclass = ns_get16(cp); cp += INT16SZ;
220 if (ttype == type && tclass == class &&
221 ns_samename(tname, name) == 1)
228 * res_queriesmatch(buf1, eom1, buf2, eom2)
229 * is there a 1:1 mapping of (name,type,class)
230 * in (buf1,eom1) and (buf2,eom2)?
233 * 0 : not a 1:1 mapping
234 * >0 : is a 1:1 mapping
236 * paul vixie, 29may94
239 res_queriesmatch(const u_char *buf1, const u_char *eom1,
240 const u_char *buf2, const u_char *eom2)
242 const u_char *cp = buf1 + HFIXEDSZ;
243 int qdcount = ntohs(((const HEADER*)buf1)->qdcount);
245 if (buf1 + HFIXEDSZ > eom1 || buf2 + HFIXEDSZ > eom2)
249 * Only header section present in replies to
250 * dynamic update packets.
252 if ((((const HEADER *)buf1)->opcode == ns_o_update) &&
253 (((const HEADER *)buf2)->opcode == ns_o_update))
256 if (qdcount != ntohs(((const HEADER*)buf2)->qdcount))
258 while (qdcount-- > 0) {
259 char tname[MAXDNAME+1];
260 int n, ttype, tclass;
262 n = dn_expand(buf1, eom1, cp, tname, sizeof tname);
266 if (cp + 2 * INT16SZ > eom1)
268 ttype = ns_get16(cp); cp += INT16SZ;
269 tclass = ns_get16(cp); cp += INT16SZ;
270 if (!res_nameinquery(tname, ttype, tclass, buf2, eom2))
277 res_nsend(res_state statp,
278 const u_char *buf, int buflen, u_char *ans, int anssiz)
280 int gotsomewhere, terrno, try, v_circuit, resplen, ns, n;
281 char abuf[NI_MAXHOST];
283 if (statp->nscount == 0) {
287 if (anssiz < HFIXEDSZ) {
291 DprintQ((statp->options & RES_DEBUG) || (statp->pfcode & RES_PRF_QUERY),
292 (stdout, ";; res_send()\n"), buf, buflen);
293 v_circuit = (statp->options & RES_USEVC) || buflen > PACKETSZ;
298 * If the ns_addr_list in the resolver context has changed, then
299 * invalidate our cached copy and the associated timing data.
301 if (EXT(statp).nscount != 0) {
303 struct sockaddr_storage peer;
304 ISC_SOCKLEN_T peerlen;
306 if (EXT(statp).nscount != statp->nscount)
309 for (ns = 0; ns < statp->nscount; ns++) {
310 if (statp->nsaddr_list[ns].sin_family &&
311 !sock_eq((struct sockaddr *)&statp->nsaddr_list[ns],
312 (struct sockaddr *)&EXT(statp).ext->nsaddrs[ns])) {
317 if (EXT(statp).nssocks[ns] == -1)
319 peerlen = sizeof(peer);
320 if (getsockname(EXT(statp).nssocks[ns],
321 (struct sockaddr *)&peer, &peerlen) < 0) {
325 if (!sock_eq((struct sockaddr *)&peer,
326 get_nsaddr(statp, ns))) {
333 EXT(statp).nscount = 0;
338 * Maybe initialize our private copy of the ns_addr_list.
340 if (EXT(statp).nscount == 0) {
341 for (ns = 0; ns < statp->nscount; ns++) {
342 EXT(statp).nstimes[ns] = RES_MAXTIME;
343 EXT(statp).nssocks[ns] = -1;
344 if (!statp->nsaddr_list[ns].sin_family)
346 EXT(statp).ext->nsaddrs[ns].sin =
347 statp->nsaddr_list[ns];
349 EXT(statp).nscount = statp->nscount;
353 * Some resolvers want to even out the load on their nameservers.
354 * Note that RES_BLAST overrides RES_ROTATE.
356 if ((statp->options & RES_ROTATE) != 0U &&
357 (statp->options & RES_BLAST) == 0U) {
358 union res_sockaddr_union inu;
359 struct sockaddr_in ina;
360 int lastns = statp->nscount - 1;
364 if (EXT(statp).ext != NULL)
365 inu = EXT(statp).ext->nsaddrs[0];
366 ina = statp->nsaddr_list[0];
367 fd = EXT(statp).nssocks[0];
368 nstime = EXT(statp).nstimes[0];
369 for (ns = 0; ns < lastns; ns++) {
370 if (EXT(statp).ext != NULL)
371 EXT(statp).ext->nsaddrs[ns] =
372 EXT(statp).ext->nsaddrs[ns + 1];
373 statp->nsaddr_list[ns] = statp->nsaddr_list[ns + 1];
374 EXT(statp).nssocks[ns] = EXT(statp).nssocks[ns + 1];
375 EXT(statp).nstimes[ns] = EXT(statp).nstimes[ns + 1];
377 if (EXT(statp).ext != NULL)
378 EXT(statp).ext->nsaddrs[lastns] = inu;
379 statp->nsaddr_list[lastns] = ina;
380 EXT(statp).nssocks[lastns] = fd;
381 EXT(statp).nstimes[lastns] = nstime;
385 * Send request, RETRY times, or until successful.
387 for (try = 0; try < statp->retry; try++) {
388 for (ns = 0; ns < statp->nscount; ns++) {
389 struct sockaddr *nsap;
391 nsap = get_nsaddr(statp, ns);
392 nsaplen = get_salen(nsap);
393 statp->_flags &= ~RES_F_LASTMASK;
394 statp->_flags |= (ns << RES_F_LASTSHIFT);
397 int done = 0, loops = 0;
402 act = (*statp->qhook)(&nsap, &buf, &buflen,
403 ans, anssiz, &resplen);
414 /* give the hook another try */
415 if (++loops < 42) /*doug adams*/
426 Dprint(((statp->options & RES_DEBUG) &&
427 getnameinfo(nsap, nsaplen, abuf, sizeof(abuf),
428 NULL, 0, niflags) == 0),
429 (stdout, ";; Querying server (# %d) address = %s\n",
434 /* Use VC; at most one attempt per server. */
436 n = send_vc(statp, buf, buflen, ans, anssiz, &terrno,
445 n = send_dg(statp, buf, buflen, ans, anssiz, &terrno,
446 ns, &v_circuit, &gotsomewhere);
456 Dprint((statp->options & RES_DEBUG) ||
457 ((statp->pfcode & RES_PRF_REPLY) &&
458 (statp->pfcode & RES_PRF_HEAD1)),
459 (stdout, ";; got answer:\n"));
461 DprintQ((statp->options & RES_DEBUG) ||
462 (statp->pfcode & RES_PRF_REPLY),
464 ans, (resplen > anssiz) ? anssiz : resplen);
467 * If we have temporarily opened a virtual circuit,
468 * or if we haven't been asked to keep a socket open,
471 if ((v_circuit && (statp->options & RES_USEVC) == 0U) ||
472 (statp->options & RES_STAYOPEN) == 0U) {
476 int done = 0, loops = 0;
481 act = (*statp->rhook)(nsap, buf, buflen,
482 ans, anssiz, &resplen);
492 /* give the hook another try */
493 if (++loops < 42) /*doug adams*/
511 errno = ECONNREFUSED; /* no nameservers found */
513 errno = ETIMEDOUT; /* no answer obtained */
526 const struct sockaddr *sa;
530 /* There are people do not set sa_len. Be forgiving to them. */
535 if (sa->sa_family == AF_INET)
536 return (sizeof(struct sockaddr_in));
537 else if (sa->sa_family == AF_INET6)
538 return (sizeof(struct sockaddr_in6));
540 return (0); /* unknown, die on connect */
544 * pick appropriate nsaddr_list for use. see res_init() for initialization.
546 static struct sockaddr *
552 if (!statp->nsaddr_list[n].sin_family && EXT(statp).ext) {
554 * - EXT(statp).ext->nsaddrs[n] holds an address that is larger
555 * than struct sockaddr, and
556 * - user code did not update statp->nsaddr_list[n].
558 return (struct sockaddr *)(void *)&EXT(statp).ext->nsaddrs[n];
561 * - user code updated statp->nsaddr_list[n], or
562 * - statp->nsaddr_list[n] has the same content as
563 * EXT(statp).ext->nsaddrs[n].
565 return (struct sockaddr *)(void *)&statp->nsaddr_list[n];
570 send_vc(res_state statp,
571 const u_char *buf, int buflen, u_char *ans, int anssiz,
574 const HEADER *hp = (const HEADER *) buf;
575 HEADER *anhp = (HEADER *) ans;
576 struct sockaddr *nsap;
578 int truncating, connreset, resplen, n;
584 nsap = get_nsaddr(statp, ns);
585 nsaplen = get_salen(nsap);
591 /* Are we still talking to whom we want to talk to? */
592 if (statp->_vcsock >= 0 && (statp->_flags & RES_F_VC) != 0) {
593 struct sockaddr_storage peer;
594 ISC_SOCKLEN_T size = sizeof peer;
596 if (getpeername(statp->_vcsock,
597 (struct sockaddr *)&peer, &size) < 0 ||
598 !sock_eq((struct sockaddr *)&peer, nsap)) {
600 statp->_flags &= ~RES_F_VC;
604 if (statp->_vcsock < 0 || (statp->_flags & RES_F_VC) == 0) {
605 if (statp->_vcsock >= 0)
608 statp->_vcsock = socket(nsap->sa_family, SOCK_STREAM, 0);
609 if (statp->_vcsock > highestFD) {
613 if (statp->_vcsock < 0) {
615 case EPROTONOSUPPORT:
620 Perror(statp, stderr, "socket(vc)", errno);
624 Perror(statp, stderr, "socket(vc)", errno);
629 if (connect(statp->_vcsock, nsap, nsaplen) < 0) {
631 Aerror(statp, stderr, "connect/vc", errno, nsap,
636 statp->_flags |= RES_F_VC;
640 * Send length & message
642 ns_put16((u_short)buflen, (u_char*)&len);
643 iov[0] = evConsIovec(&len, INT16SZ);
645 iov[1] = evConsIovec(tmp, buflen);
646 if (writev(statp->_vcsock, iov, 2) != (INT16SZ + buflen)) {
648 Perror(statp, stderr, "write failed", errno);
653 * Receive length & response
658 while ((n = read(statp->_vcsock, (char *)cp, (int)len)) > 0) {
665 Perror(statp, stderr, "read failed", errno);
668 * A long running process might get its TCP
669 * connection reset if the remote server was
670 * restarted. Requery the server instead of
671 * trying a new one. When there is only one
672 * server, this means that a query might work
673 * instead of failing. We only allow one reset
674 * per query to prevent looping.
676 if (*terrno == ECONNRESET && !connreset) {
684 resplen = ns_get16(ans);
685 if (resplen > anssiz) {
686 Dprint(statp->options & RES_DEBUG,
687 (stdout, ";; response truncated\n")
693 if (len < HFIXEDSZ) {
695 * Undersized message.
697 Dprint(statp->options & RES_DEBUG,
698 (stdout, ";; undersized: %d\n", len));
704 while (len != 0 && (n = read(statp->_vcsock, (char *)cp, (int)len)) > 0){
710 Perror(statp, stderr, "read(vc)", errno);
716 * Flush rest of answer so connection stays in synch.
719 len = resplen - anssiz;
723 n = read(statp->_vcsock, junk,
724 (len > sizeof junk) ? sizeof junk : len);
732 * If the calling applicating has bailed out of
733 * a previous call and failed to arrange to have
734 * the circuit closed or the server has got
735 * itself confused, then drop the packet and
736 * wait for the correct one.
738 if (hp->id != anhp->id) {
739 DprintQ((statp->options & RES_DEBUG) ||
740 (statp->pfcode & RES_PRF_REPLY),
741 (stdout, ";; old answer (unexpected):\n"),
742 ans, (resplen > anssiz) ? anssiz: resplen);
747 * All is well, or the error is fatal. Signal that the
748 * next nameserver ought not be tried.
754 send_dg(res_state statp,
755 const u_char *buf, int buflen, u_char *ans, int anssiz,
756 int *terrno, int ns, int *v_circuit, int *gotsomewhere)
758 const HEADER *hp = (const HEADER *) buf;
759 HEADER *anhp = (HEADER *) ans;
760 const struct sockaddr *nsap;
762 struct timespec now, timeout, finish;
764 struct sockaddr_storage from;
765 ISC_SOCKLEN_T fromlen;
766 int resplen, seconds, n, s;
768 nsap = get_nsaddr(statp, ns);
769 nsaplen = get_salen(nsap);
770 if (EXT(statp).nssocks[ns] == -1) {
771 EXT(statp).nssocks[ns] = socket(nsap->sa_family, SOCK_DGRAM, 0);
772 if (EXT(statp).nssocks[ns] > highestFD) {
776 if (EXT(statp).nssocks[ns] < 0) {
778 case EPROTONOSUPPORT:
783 Perror(statp, stderr, "socket(dg)", errno);
787 Perror(statp, stderr, "socket(dg)", errno);
791 #ifndef CANNOT_CONNECT_DGRAM
793 * On a 4.3BSD+ machine (client and server,
794 * actually), sending to a nameserver datagram
795 * port with no nameserver will cause an
796 * ICMP port unreachable message to be returned.
797 * If our datagram socket is "connected" to the
798 * server, we get an ECONNREFUSED error on the next
799 * socket operation, and select returns if the
800 * error message is received. We can thus detect
801 * the absence of a nameserver without timing out.
803 if (connect(EXT(statp).nssocks[ns], nsap, nsaplen) < 0) {
804 Aerror(statp, stderr, "connect(dg)", errno, nsap,
809 #endif /* !CANNOT_CONNECT_DGRAM */
810 Dprint(statp->options & RES_DEBUG,
811 (stdout, ";; new DG socket\n"))
813 s = EXT(statp).nssocks[ns];
814 #ifndef CANNOT_CONNECT_DGRAM
815 if (send(s, (const char*)buf, buflen, 0) != buflen) {
816 Perror(statp, stderr, "send", errno);
820 #else /* !CANNOT_CONNECT_DGRAM */
821 if (sendto(s, (const char*)buf, buflen, 0, nsap, nsaplen) != buflen)
823 Aerror(statp, stderr, "sendto", errno, nsap, nsaplen);
827 #endif /* !CANNOT_CONNECT_DGRAM */
832 seconds = (statp->retrans << ns);
834 seconds /= statp->nscount;
838 timeout = evConsTime(seconds, 0);
839 finish = evAddTime(now, timeout);
846 if (evCmpTime(finish, now) > 0)
847 timeout = evSubTime(finish, now);
849 timeout = evConsTime(0, 0);
850 n = pselect(s + 1, &dsmask, NULL, NULL, &timeout, NULL);
852 Dprint(statp->options & RES_DEBUG, (stdout, ";; timeout\n"));
859 Perror(statp, stderr, "select", errno);
864 fromlen = sizeof(from);
865 resplen = recvfrom(s, (char*)ans, anssiz,0,
866 (struct sockaddr *)&from, &fromlen);
868 Perror(statp, stderr, "recvfrom", errno);
873 if (resplen < HFIXEDSZ) {
875 * Undersized message.
877 Dprint(statp->options & RES_DEBUG,
878 (stdout, ";; undersized: %d\n",
884 if (hp->id != anhp->id) {
886 * response from old query, ignore it.
887 * XXX - potential security hazard could
890 DprintQ((statp->options & RES_DEBUG) ||
891 (statp->pfcode & RES_PRF_REPLY),
892 (stdout, ";; old answer:\n"),
893 ans, (resplen > anssiz) ? anssiz : resplen);
896 if (!(statp->options & RES_INSECURE1) &&
897 !res_ourserver_p(statp, (struct sockaddr *)&from)) {
899 * response from wrong server? ignore it.
900 * XXX - potential security hazard could
903 DprintQ((statp->options & RES_DEBUG) ||
904 (statp->pfcode & RES_PRF_REPLY),
905 (stdout, ";; not our server:\n"),
906 ans, (resplen > anssiz) ? anssiz : resplen);
910 if (anhp->rcode == FORMERR && (statp->options & RES_USE_EDNS0) != 0U) {
912 * Do not retry if the server do not understand EDNS0.
913 * The case has to be captured here, as FORMERR packet do not
914 * carry query section, hence res_queriesmatch() returns 0.
916 DprintQ(statp->options & RES_DEBUG,
917 (stdout, "server rejected query with EDNS0:\n"),
918 ans, (resplen > anssiz) ? anssiz : resplen);
919 /* record the error */
920 statp->_flags |= RES_F_EDNS0ERR;
925 if (!(statp->options & RES_INSECURE2) &&
926 !res_queriesmatch(buf, buf + buflen,
927 ans, ans + anssiz)) {
929 * response contains wrong query? ignore it.
930 * XXX - potential security hazard could
933 DprintQ((statp->options & RES_DEBUG) ||
934 (statp->pfcode & RES_PRF_REPLY),
935 (stdout, ";; wrong query name:\n"),
936 ans, (resplen > anssiz) ? anssiz : resplen);
939 if (anhp->rcode == SERVFAIL ||
940 anhp->rcode == NOTIMP ||
941 anhp->rcode == REFUSED) {
942 DprintQ(statp->options & RES_DEBUG,
943 (stdout, "server rejected query:\n"),
944 ans, (resplen > anssiz) ? anssiz : resplen);
946 /* don't retry if called from dig */
950 if (!(statp->options & RES_IGNTC) && anhp->tc) {
952 * To get the rest of answer,
953 * use TCP with same server.
955 Dprint(statp->options & RES_DEBUG,
956 (stdout, ";; truncated answer\n"));
962 * All is well, or the error is fatal. Signal that the
963 * next nameserver ought not be tried.
969 Aerror(const res_state statp, FILE *file, const char *string, int error,
970 const struct sockaddr *address, int alen)
973 char hbuf[NI_MAXHOST];
974 char sbuf[NI_MAXSERV];
978 if ((statp->options & RES_DEBUG) != 0U) {
979 if (getnameinfo(address, alen, hbuf, sizeof(hbuf),
980 sbuf, sizeof(sbuf), niflags)) {
981 strncpy(hbuf, "?", sizeof(hbuf) - 1);
982 hbuf[sizeof(hbuf) - 1] = '\0';
983 strncpy(sbuf, "?", sizeof(sbuf) - 1);
984 sbuf[sizeof(sbuf) - 1] = '\0';
986 fprintf(file, "res_send: %s ([%s].%s): %s\n",
987 string, hbuf, sbuf, strerror(error));
993 Perror(const res_state statp, FILE *file, const char *string, int error) {
996 if ((statp->options & RES_DEBUG) != 0U)
997 fprintf(file, "res_send: %s: %s\n",
998 string, strerror(error));
1003 sock_eq(struct sockaddr *a, struct sockaddr *b) {
1004 struct sockaddr_in *a4, *b4;
1005 struct sockaddr_in6 *a6, *b6;
1007 if (a->sa_family != b->sa_family)
1009 switch (a->sa_family) {
1011 a4 = (struct sockaddr_in *)a;
1012 b4 = (struct sockaddr_in *)b;
1013 return a4->sin_port == b4->sin_port &&
1014 a4->sin_addr.s_addr == b4->sin_addr.s_addr;
1016 a6 = (struct sockaddr_in6 *)a;
1017 b6 = (struct sockaddr_in6 *)b;
1018 return a6->sin6_port == b6->sin6_port &&
1019 #ifdef HAVE_SIN6_SCOPE_ID
1020 a6->sin6_scope_id == b6->sin6_scope_id &&
1022 IN6_ARE_ADDR_EQUAL(&a6->sin6_addr, &b6->sin6_addr);
1029 /* XXX needs to move to the porting library. */
1031 pselect(int nfds, void *rfds, void *wfds, void *efds,
1032 struct timespec *tsp, const sigset_t *sigmask)
1034 struct timeval tv, *tvp;
1040 tv = evTimeVal(*tsp);
1044 sigprocmask(SIG_SETMASK, sigmask, &sigs);
1045 n = select(nfds, rfds, wfds, efds, tvp);
1047 sigprocmask(SIG_SETMASK, &sigs, NULL);
1049 *tsp = evTimeSpec(tv);