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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)res_send.c 8.1 (Berkeley) 6/4/93
30 * $From: Id: res_send.c,v 8.20 1998/04/06 23:27:51 halley Exp $
31 * $FreeBSD: src/lib/libc/net/res_send.c,v 1.31.2.9 2002/04/11 17:30:24 ume Exp $
32 * $DragonFly: src/lib/libc/net/res_send.c,v 1.6 2005/11/13 02:04:47 swildner Exp $
36 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
38 * Permission to use, copy, modify, and distribute this software for any
39 * purpose with or without fee is hereby granted, provided that the above
40 * copyright notice and this permission notice appear in all copies, and that
41 * the name of Digital Equipment Corporation not be used in advertising or
42 * publicity pertaining to distribution of the document or software without
43 * specific, written prior permission.
45 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
46 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
47 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
48 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
49 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
50 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
51 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
56 * Portions Copyright (c) 1996 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 INTERNET SOFTWARE CONSORTIUM DISCLAIMS
63 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
64 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
65 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
66 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
67 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
68 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
73 * Send query to name server and wait for reply.
76 #include "namespace.h"
77 #include <sys/types.h>
78 #include <sys/event.h>
79 #include <sys/param.h>
81 #include <sys/socket.h>
84 #include <netinet/in.h>
85 #include <arpa/nameser.h>
86 #include <arpa/inet.h>
95 #include "un-namespace.h"
97 #include "res_config.h"
99 static int s = -1; /* socket used for communications */
100 static int connected = 0; /* is the socket connected */
101 static int vc = 0; /* is the socket a virtual circuit? */
102 static int af = 0; /* address family of socket */
103 static res_send_qhook Qhook = NULL;
104 static res_send_rhook Rhook = NULL;
107 #define CAN_RECONNECT 1
110 # define Dprint(cond, args) /*empty*/
111 # define DprintQ(cond, args, query, size) /*empty*/
112 # define Aerror(file, string, error, address) /*empty*/
113 # define Perror(file, string, error) /*empty*/
115 # define Dprint(cond, args) if (cond) {fprintf args;} else {}
116 # define DprintQ(cond, args, query, size) if (cond) {\
118 __fp_nquery(query, size, stdout);\
120 static char abuf[NI_MAXHOST];
121 static char pbuf[NI_MAXSERV];
122 static void Aerror (FILE *, char *, int, struct sockaddr *);
123 static void Perror (FILE *, char *, int);
126 Aerror(FILE *file, char *string, int error, struct sockaddr *address)
130 if (_res.options & RES_DEBUG) {
131 if (getnameinfo(address, address->sa_len, abuf, sizeof(abuf),
133 NI_NUMERICHOST|NI_NUMERICSERV|NI_WITHSCOPEID) != 0) {
134 strncpy(abuf, "?", sizeof(abuf));
135 strncpy(pbuf, "?", sizeof(pbuf));
137 fprintf(file, "res_send: %s ([%s].%s): %s\n",
138 string, abuf, pbuf, strerror(error));
144 Perror(FILE *file, char *string, int error)
148 if (_res.options & RES_DEBUG) {
149 fprintf(file, "res_send: %s: %s\n",
150 string, strerror(error));
157 res_send_setqhook(res_send_qhook hook)
164 res_send_setrhook(res_send_rhook hook)
170 static struct sockaddr * get_nsaddr (size_t);
173 * pick appropriate nsaddr_list for use. see res_init() for initialization.
175 static struct sockaddr *
179 if (!_res.nsaddr_list[n].sin_family) {
181 * - _res_ext.nsaddr_list[n] holds an address that is larger
182 * than struct sockaddr, and
183 * - user code did not update _res.nsaddr_list[n].
185 return (struct sockaddr *)&_res_ext.nsaddr_list[n];
188 * - user code updated _res.nsaddr_list[n], or
189 * - _res.nsaddr_list[n] has the same content as
190 * _res_ext.nsaddr_list[n].
192 return (struct sockaddr *)&_res.nsaddr_list[n];
197 * res_isourserver(ina)
198 * looks up "ina" in _res.ns_addr_list[]
203 * paul vixie, 29may94
206 res_isourserver(const struct sockaddr_in *inp)
208 const struct sockaddr_in6 *in6p = (const struct sockaddr_in6 *)inp;
209 const struct sockaddr_in6 *srv6;
210 const struct sockaddr_in *srv;
214 switch (inp->sin_family) {
216 for (ns = 0; ns < _res.nscount; ns++) {
217 srv6 = (struct sockaddr_in6 *)get_nsaddr(ns);
218 if (srv6->sin6_family == in6p->sin6_family &&
219 srv6->sin6_port == in6p->sin6_port &&
220 srv6->sin6_scope_id == in6p->sin6_scope_id &&
221 (IN6_IS_ADDR_UNSPECIFIED(&srv6->sin6_addr) ||
222 IN6_ARE_ADDR_EQUAL(&srv6->sin6_addr,
223 &in6p->sin6_addr))) {
230 for (ns = 0; ns < _res.nscount; ns++) {
231 srv = (struct sockaddr_in *)get_nsaddr(ns);
232 if (srv->sin_family == inp->sin_family &&
233 srv->sin_port == inp->sin_port &&
234 (srv->sin_addr.s_addr == INADDR_ANY ||
235 srv->sin_addr.s_addr == inp->sin_addr.s_addr)) {
246 * res_nameinquery(name, type, class, buf, eom)
247 * look for (name,type,class) in the query section of packet (buf,eom)
249 * buf + HFIXEDSZ <= eom
255 * paul vixie, 29may94
258 res_nameinquery(const char *name, int type, int class, const u_char *buf,
261 const u_char *cp = buf + HFIXEDSZ;
262 int qdcount = ntohs(((HEADER*)buf)->qdcount);
264 while (qdcount-- > 0) {
265 char tname[MAXDNAME+1];
266 int n, ttype, tclass;
268 n = dn_expand(buf, eom, cp, tname, sizeof tname);
272 if (cp + 2 * INT16SZ > eom)
274 ttype = ns_get16(cp); cp += INT16SZ;
275 tclass = ns_get16(cp); cp += INT16SZ;
278 strcasecmp(tname, name) == 0)
285 * res_queriesmatch(buf1, eom1, buf2, eom2)
286 * is there a 1:1 mapping of (name,type,class)
287 * in (buf1,eom1) and (buf2,eom2)?
290 * 0 : not a 1:1 mapping
291 * >0 : is a 1:1 mapping
293 * paul vixie, 29may94
296 res_queriesmatch(const u_char *buf1, const u_char *eom1,
297 const u_char *buf2, const u_char *eom2)
299 const u_char *cp = buf1 + HFIXEDSZ;
300 int qdcount = ntohs(((HEADER*)buf1)->qdcount);
302 if (buf1 + HFIXEDSZ > eom1 || buf2 + HFIXEDSZ > eom2)
306 * Only header section present in replies to
307 * dynamic update packets.
309 if ( (((HEADER *)buf1)->opcode == ns_o_update) &&
310 (((HEADER *)buf2)->opcode == ns_o_update) )
313 if (qdcount != ntohs(((HEADER*)buf2)->qdcount))
315 while (qdcount-- > 0) {
316 char tname[MAXDNAME+1];
317 int n, ttype, tclass;
319 n = dn_expand(buf1, eom1, cp, tname, sizeof tname);
323 if (cp + 2 * INT16SZ > eom1)
325 ttype = ns_get16(cp); cp += INT16SZ;
326 tclass = ns_get16(cp); cp += INT16SZ;
327 if (!res_nameinquery(tname, ttype, tclass, buf2, eom2))
334 res_send(const u_char *buf, int buflen, u_char *ans, int anssiz)
336 HEADER *hp = (HEADER *) buf;
337 HEADER *anhp = (HEADER *) ans;
338 int gotsomewhere, connreset, terrno, try, v_circuit, resplen, ns, n;
340 u_int badns; /* XXX NSMAX can't exceed #/bits in this variable */
342 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
343 /* errno should have been set by res_init() in this case. */
346 if (anssiz < HFIXEDSZ) {
350 DprintQ((_res.options & RES_DEBUG) || (_res.pfcode & RES_PRF_QUERY),
351 (stdout, ";; res_send()\n"), buf, buflen);
352 v_circuit = (_res.options & RES_USEVC) || buflen > PACKETSZ;
358 if ((kq = kqueue()) < 0) {
359 Perror(stderr, "kqueue", errno);
364 * Send request, RETRY times, or until successful
366 for (try = 0; try < _res.retry; try++) {
367 for (ns = 0; ns < _res.nscount; ns++) {
368 struct sockaddr *nsap = get_nsaddr(ns);
372 salen = nsap->sa_len;
373 else if (nsap->sa_family == AF_INET6)
374 salen = sizeof(struct sockaddr_in6);
375 else if (nsap->sa_family == AF_INET)
376 salen = sizeof(struct sockaddr_in);
378 salen = 0; /*unknown, die on connect*/
381 if (badns & (1 << ns)) {
387 int done = 0, loops = 0;
392 act = (*Qhook)((struct sockaddr_in **)&nsap,
394 ans, anssiz, &resplen);
406 /* give the hook another try */
407 if (++loops < 42) /*doug adams*/
419 Dprint((_res.options & RES_DEBUG) &&
420 getnameinfo(nsap, salen, abuf, sizeof(abuf),
421 NULL, 0, NI_NUMERICHOST | NI_WITHSCOPEID) == 0,
422 (stdout, ";; Querying server (# %d) address = %s\n",
432 * Use virtual circuit;
433 * at most one attempt per server.
437 if (s < 0 || !vc || hp->opcode == ns_o_update ||
438 af != nsap->sa_family) {
442 af = nsap->sa_family;
443 s = _socket(af, SOCK_STREAM, 0);
446 Perror(stderr, "socket(vc)", errno);
452 if (_connect(s, nsap, salen) < 0) {
454 Aerror(stderr, "connect/vc",
463 * Send length & message
465 putshort((u_short)buflen, (u_char*)&len);
466 iov[0].iov_base = (caddr_t)&len;
467 iov[0].iov_len = INT16SZ;
468 iov[1].iov_base = (caddr_t)buf;
469 iov[1].iov_len = buflen;
470 if (_writev(s, iov, 2) != (INT16SZ + buflen)) {
472 Perror(stderr, "write failed", errno);
478 * Receive length & response
483 while ((n = _read(s, (char *)cp, (int)len)) > 0) {
490 Perror(stderr, "read failed", errno);
493 * A long running process might get its TCP
494 * connection reset if the remote server was
495 * restarted. Requery the server instead of
496 * trying a new one. When there is only one
497 * server, this means that a query might work
498 * instead of failing. We only allow one reset
499 * per query to prevent looping.
501 if (terrno == ECONNRESET && !connreset) {
509 resplen = ns_get16(ans);
510 if (resplen > anssiz) {
511 Dprint(_res.options & RES_DEBUG,
512 (stdout, ";; response truncated\n")
518 if (len < HFIXEDSZ) {
520 * Undersized message.
522 Dprint(_res.options & RES_DEBUG,
523 (stdout, ";; undersized: %d\n", len));
531 (n = _read(s, (char *)cp, (int)len)) > 0) {
537 Perror(stderr, "read(vc)", errno);
543 * Flush rest of answer
544 * so connection stays in synch.
547 len = resplen - anssiz;
551 n = (len > sizeof(junk)
554 if ((n = _read(s, junk, n)) > 0)
561 * The calling applicating has bailed out of
562 * a previous call and failed to arrange to have
563 * the circuit closed or the server has got
564 * itself confused. Anyway drop the packet and
565 * wait for the correct one.
567 if (hp->id != anhp->id) {
568 DprintQ((_res.options & RES_DEBUG) ||
569 (_res.pfcode & RES_PRF_REPLY),
570 (stdout, ";; old answer (unexpected):\n"),
571 ans, (resplen>anssiz)?anssiz:resplen);
580 struct timeval timeout, ctv;
581 struct sockaddr_storage from;
584 if (s < 0 || vc || af != nsap->sa_family) {
587 af = nsap->sa_family;
588 s = _socket(af, SOCK_DGRAM, 0);
590 #ifndef CAN_RECONNECT
594 Perror(stderr, "socket(dg)", errno);
601 #ifndef CANNOT_CONNECT_DGRAM
603 * On a 4.3BSD+ machine (client and server,
604 * actually), sending to a nameserver datagram
605 * port with no nameserver will cause an
606 * ICMP port unreachable message to be returned.
607 * If our datagram socket is "connected" to the
608 * server, we get an ECONNREFUSED error on the next
609 * socket operation, and select returns if the
610 * error message is received. We can thus detect
611 * the absence of a nameserver without timing out.
612 * If we have sent queries to at least two servers,
613 * however, we don't want to remain connected,
614 * as we wish to receive answers from the first
617 * When the option "insecure1" is specified, we'd
618 * rather expect to see responses from an "unknown"
619 * address. In order to let the kernel accept such
620 * responses, do not connect the socket here.
621 * XXX: or do we need an explicit option to disable
624 if (!(_res.options & RES_INSECURE1) &&
625 (_res.nscount == 1 || (try == 0 && ns == 0))) {
627 * Connect only if we are sure we won't
628 * receive a response from another server.
631 if (_connect(s, nsap, salen) < 0) {
641 if (send(s, (char*)buf, buflen, 0) != buflen) {
642 Perror(stderr, "send", errno);
649 * Disconnect if we want to listen
650 * for responses from more than one server.
654 /* XXX: any errornous address */
655 struct sockaddr_in no_addr;
657 no_addr.sin_family = AF_INET;
658 no_addr.sin_addr.s_addr = INADDR_ANY;
659 no_addr.sin_port = 0;
661 (struct sockaddr *)&no_addr,
664 int s1 = _socket(af, SOCK_DGRAM,0);
669 Dprint(_res.options & RES_DEBUG,
670 (stdout, ";; new DG socket\n"))
671 #endif /* CAN_RECONNECT */
675 #endif /* !CANNOT_CONNECT_DGRAM */
676 if (_sendto(s, (char*)buf, buflen, 0,
677 nsap, salen) != buflen) {
678 Aerror(stderr, "sendto", errno, nsap);
683 #ifndef CANNOT_CONNECT_DGRAM
685 #endif /* !CANNOT_CONNECT_DGRAM */
691 timeout.tv_sec = (_res.retrans << try);
693 timeout.tv_sec /= _res.nscount;
694 if ((long) timeout.tv_sec <= 0)
697 TIMEVAL_TO_TIMESPEC(&timeout, &ts);
698 gettimeofday(&ctv, NULL);
699 timeradd(&timeout, &ctv, &timeout);
702 Perror(stderr, "s out-of-bounds", EMFILE);
707 EV_SET(&kv, s, EVFILT_READ, EV_ADD | EV_ONESHOT, 0,0,0);
709 n = _kevent(kq, &kv, 1, &kv, 1, &ts);
711 if (errno == EINTR) {
712 gettimeofday(&ctv, NULL);
713 if (timercmp(&ctv, &timeout, <)) {
714 timersub(&timeout, &ctv, &ctv);
715 TIMEVAL_TO_TIMESPEC(&ctv, &ts);
719 Perror(stderr, "kevent", errno);
728 Dprint(_res.options & RES_DEBUG,
729 (stdout, ";; timeout\n"));
735 fromlen = sizeof(from);
736 resplen = _recvfrom(s, (char*)ans, anssiz, 0,
737 (struct sockaddr *)&from, &fromlen);
739 Perror(stderr, "recvfrom", errno);
744 if (resplen < HFIXEDSZ) {
746 * Undersized message.
748 Dprint(_res.options & RES_DEBUG,
749 (stdout, ";; undersized: %d\n",
756 if (hp->id != anhp->id) {
758 * response from old query, ignore it.
759 * XXX - potential security hazard could
762 DprintQ((_res.options & RES_DEBUG) ||
763 (_res.pfcode & RES_PRF_REPLY),
764 (stdout, ";; old answer:\n"),
765 ans, (resplen>anssiz)?anssiz:resplen);
768 #ifdef CHECK_SRVR_ADDR
769 if (!(_res.options & RES_INSECURE1) &&
770 !res_isourserver((struct sockaddr_in *)&from)) {
772 * response from wrong server? ignore it.
773 * XXX - potential security hazard could
776 DprintQ((_res.options & RES_DEBUG) ||
777 (_res.pfcode & RES_PRF_REPLY),
778 (stdout, ";; not our server:\n"),
779 ans, (resplen>anssiz)?anssiz:resplen);
783 if (!(_res.options & RES_INSECURE2) &&
784 !res_queriesmatch(buf, buf + buflen,
785 ans, ans + anssiz)) {
787 * response contains wrong query? ignore it.
788 * XXX - potential security hazard could
791 DprintQ((_res.options & RES_DEBUG) ||
792 (_res.pfcode & RES_PRF_REPLY),
793 (stdout, ";; wrong query name:\n"),
794 ans, (resplen>anssiz)?anssiz:resplen);
797 if (anhp->rcode == SERVFAIL ||
798 anhp->rcode == NOTIMP ||
799 anhp->rcode == REFUSED) {
800 DprintQ(_res.options & RES_DEBUG,
801 (stdout, "server rejected query:\n"),
802 ans, (resplen>anssiz)?anssiz:resplen);
805 /* don't retry if called from dig */
809 if (!(_res.options & RES_IGNTC) && anhp->tc) {
811 * get rest of answer;
812 * use TCP with same server.
814 Dprint(_res.options & RES_DEBUG,
815 (stdout, ";; truncated answer\n"));
821 Dprint((_res.options & RES_DEBUG) ||
822 ((_res.pfcode & RES_PRF_REPLY) &&
823 (_res.pfcode & RES_PRF_HEAD1)),
824 (stdout, ";; got answer:\n"));
825 DprintQ((_res.options & RES_DEBUG) ||
826 (_res.pfcode & RES_PRF_REPLY),
828 ans, (resplen>anssiz)?anssiz:resplen);
830 * If using virtual circuits, we assume that the first server
831 * is preferred over the rest (i.e. it is on the local
832 * machine) and only keep that one open.
833 * If we have temporarily opened a virtual circuit,
834 * or if we haven't been asked to keep a socket open,
837 if ((v_circuit && (!(_res.options & RES_USEVC) || ns != 0)) ||
838 !(_res.options & RES_STAYOPEN)) {
842 int done = 0, loops = 0;
847 act = (*Rhook)((struct sockaddr_in *)nsap,
849 ans, anssiz, &resplen);
859 /* give the hook another try */
860 if (++loops < 42) /*doug adams*/
881 errno = ECONNREFUSED; /* no nameservers found */
883 errno = ETIMEDOUT; /* no answer obtained */
890 * This routine is for closing the socket if a virtual circuit is used and
891 * the program wants to close it. This provides support for endhostent()
892 * which expects to close the socket.
894 * This routine is not expected to be user visible.
909 * Weak aliases for applications that use certain private entry points,
910 * and fail to include <resolv.h>.
913 __weak_reference(__res_close, _res_close);
915 __weak_reference(__res_send, res_send);