2 * ++Copyright++ 1985, 1989, 1993
4 * Copyright (c) 1985, 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * 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
56 #if defined(LIBC_SCCS) && !defined(lint)
57 static char sccsid[] = "@(#)res_send.c 8.1 (Berkeley) 6/4/93";
58 static char rcsid[] = "$Id$";
59 #endif /* LIBC_SCCS and not lint */
62 * if you talk to a lot
63 * of multi-homed SunOS
64 * ("broken") name servers.
66 #define CHECK_SRVR_ADDR 1 /* XXX - should be in options.h */
69 * Send query to name server and wait for reply.
72 #include <sys/param.h>
74 #include <sys/socket.h>
76 #include <netinet/in.h>
77 #include <arpa/nameser.h>
78 #include <arpa/inet.h>
83 #if defined(BSD) && (BSD >= 199306)
87 # include "../conf/portability.h"
90 #if defined(USE_OPTIONS_H)
91 # include <../conf/options.h>
94 void _res_close __P((void));
96 static int s = -1; /* socket used for communications */
97 static int connected = 0; /* is the socket connected */
98 static int vc = 0; /* is the socket a virtual ciruit? */
101 /* XXX - should be in portability.h */
103 #define FD_SETSIZE 32
104 #define FD_SET(n, p) ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS)))
105 #define FD_CLR(n, p) ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS)))
106 #define FD_ISSET(n, p) ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS)))
107 #define FD_ZERO(p) bzero((char *)(p), sizeof(*(p)))
111 # define Dprint(cond, args) /*empty*/
112 # define DprintQ(cond, args, query) /*empty*/
113 # define Aerror(file, string, error, address) /*empty*/
114 # define Perror(file, string, error) /*empty*/
116 # define Dprint(cond, args) if (cond) {fprintf args;} else {}
117 # define DprintQ(cond, args, query) if (cond) {\
122 Aerror(file, string, error, address)
126 struct sockaddr_in address;
130 if (_res.options & RES_DEBUG) {
131 fprintf(file, "res_send: %s ([%s].%d): %s\n",
133 inet_ntoa(address.sin_addr),
140 Perror(file, string, error)
147 if (_res.options & RES_DEBUG) {
148 fprintf(file, "res_send: %s: %s\n",
149 string, strerror(error));
155 static res_send_qhook Qhook = NULL;
156 static res_send_rhook Rhook = NULL;
159 res_send_setqhook(hook)
166 res_send_setrhook(hook)
174 * looks up "ina" in _res.ns_addr_list[]
179 * paul vixie, 29may94
183 const struct sockaddr_in *inp;
185 struct sockaddr_in ina;
186 register int ns, ret;
190 for (ns = 0; ns < _res.nscount; ns++) {
191 register const struct sockaddr_in *srv = &_res.nsaddr_list[ns];
193 if (srv->sin_family == ina.sin_family &&
194 srv->sin_port == ina.sin_port &&
195 (srv->sin_addr.s_addr == INADDR_ANY ||
196 srv->sin_addr.s_addr == ina.sin_addr.s_addr)) {
205 * name_in_query(name, type, class, buf, eom)
206 * look for (name,type,class) in the query section of packet (buf,eom)
213 name_in_query(name, type, class, buf, eom)
215 register int type, class;
216 const u_char *buf, *eom;
218 register const u_char *cp = buf + HFIXEDSZ;
219 int qdcount = ntohs(((HEADER*)buf)->qdcount);
221 while (qdcount-- > 0) {
222 char tname[MAXDNAME+1];
223 register int n, ttype, tclass;
225 n = dn_expand(buf, eom, cp, tname, sizeof tname);
229 ttype = _getshort(cp); cp += INT16SZ;
230 tclass = _getshort(cp); cp += INT16SZ;
233 strcasecmp(tname, name) == 0)
240 * queries_match(buf1, eom1, buf2, eom2)
241 * is there a 1:1 mapping of (name,type,class)
242 * in (buf1,eom1) and (buf2,eom2)?
245 * 0 : not a 1:1 mapping
246 * >0 : is a 1:1 mapping
249 queries_match(buf1, eom1, buf2, eom2)
250 const u_char *buf1, *eom1;
251 const u_char *buf2, *eom2;
253 register const u_char *cp = buf1 + HFIXEDSZ;
254 int qdcount = ntohs(((HEADER*)buf1)->qdcount);
256 if (qdcount != ntohs(((HEADER*)buf2)->qdcount))
258 while (qdcount-- > 0) {
259 char tname[MAXDNAME+1];
260 register int n, ttype, tclass;
262 n = dn_expand(buf1, eom1, cp, tname, sizeof tname);
266 ttype = _getshort(cp); cp += INT16SZ;
267 tclass = _getshort(cp); cp += INT16SZ;
268 if (!name_in_query(tname, ttype, tclass, buf2, eom2))
275 res_send(buf, buflen, ans, anssiz)
281 HEADER *hp = (HEADER *) buf;
282 HEADER *anhp = (HEADER *) ans;
283 int gotsomewhere = 0,
288 int try, v_circuit, resplen, ns;
289 u_int badns; /* XXX NSMAX can't exceed #/bits in this var */
291 DprintQ((_res.options & RES_DEBUG) || (_res.pfcode & RES_PRF_QUERY),
292 (stdout, ";; res_send()\n"), buf);
293 if (!(_res.options & RES_INIT)) {
294 if (res_init() == -1)
297 v_circuit = (_res.options & RES_USEVC) || buflen > PACKETSZ;
301 * Send request, RETRY times, or until successful
303 for (try = 0; try < _res.retry; try++) {
304 for (ns = 0; ns < _res.nscount; ns++) {
305 struct sockaddr_in *nsap = &_res.nsaddr_list[ns];
307 if (badns & (1<<ns)) {
313 int done = 0, loops = 0;
318 act = (*Qhook)(&nsap,
334 /* give the hook another try */
335 if (++loops < 42) /*doug adams*/
346 Dprint(_res.options & RES_DEBUG,
347 (stdout, ";; Querying server (# %d) address = %s\n",
348 ns+1, inet_ntoa(nsap->sin_addr)));
357 * Use virtual circuit;
358 * at most one attempt per server.
362 if ((s < 0) || (!vc)) {
366 s = socket(AF_INET, SOCK_STREAM, PF_UNSPEC);
369 Perror(stderr, "socket(vc)", errno);
373 (struct sockaddr *)nsap,
374 sizeof(struct sockaddr))
377 Aerror(stderr, "connect/vc",
386 * Send length & message
388 putshort((u_short)buflen, (u_char*)&len);
389 iov[0].iov_base = (caddr_t)&len;
390 iov[0].iov_len = INT16SZ;
391 iov[1].iov_base = (caddr_t)buf;
392 iov[1].iov_len = buflen;
393 if (writev(s, iov, 2) != (INT16SZ + buflen)) {
395 Perror(stderr, "write failed", errno);
401 * Receive length & response
405 while ((n = read(s, (char *)cp, (int)len)) > 0) {
412 Perror(stderr, "read failed", errno);
415 * A long running process might get its TCP
416 * connection reset if the remote server was
417 * restarted. Requery the server instead of
418 * trying a new one. When there is only one
419 * server, this means that a query might work
420 * instead of failing. We only allow one reset
421 * per query to prevent looping.
423 if (terrno == ECONNRESET && !connreset) {
431 resplen = _getshort(ans);
432 if (resplen > anssiz) {
433 Dprint(_res.options & RES_DEBUG,
434 (stdout, ";; response truncated\n")
442 (n = read(s, (char *)cp, (int)len)) > 0
449 Perror(stderr, "read(vc)", errno);
455 * Flush rest of answer
456 * so connection stays in synch.
459 len = resplen - anssiz;
463 n = (len > sizeof(junk)
466 if ((n = read(s, junk, n)) > 0)
476 struct timeval timeout;
478 struct sockaddr_in from;
484 s = socket(AF_INET, SOCK_DGRAM, PF_UNSPEC);
486 bad_dg_sock: terrno = errno;
487 Perror(stderr, "socket(dg)", errno);
493 * On a 4.3BSD+ machine (client and server,
494 * actually), sending to a nameserver datagram
495 * port with no nameserver will cause an
496 * ICMP port unreachable message to be returned.
497 * If our datagram socket is "connected" to the
498 * server, we get an ECONNREFUSED error on the next
499 * socket operation, and select returns if the
500 * error message is received. We can thus detect
501 * the absence of a nameserver without timing out.
502 * If we have sent queries to at least two servers,
503 * however, we don't want to remain connected,
504 * as we wish to receive answers from the first
507 if (_res.nscount == 1 || (try == 0 && ns == 0)) {
509 * Connect only if we are sure we won't
510 * receive a response from another server.
514 (struct sockaddr *)nsap,
515 sizeof(struct sockaddr)
527 if (send(s, buf, buflen, 0) != buflen) {
528 Perror(stderr, "send", errno);
535 * Disconnect if we want to listen
536 * for responses from more than one server.
539 #if defined(BSD) && (BSD >= 199103)
540 struct sockaddr_in no_addr;
542 no_addr.sin_family = AF_INET;
543 no_addr.sin_addr.s_addr = INADDR_ANY;
544 no_addr.sin_port = 0;
550 int s1 = socket(AF_INET, SOCK_DGRAM,
556 Dprint(_res.options & RES_DEBUG,
557 (stdout, ";; new DG socket\n"))
562 if (sendto(s, buf, buflen, 0,
563 (struct sockaddr *)nsap,
564 sizeof(struct sockaddr))
566 Aerror(stderr, "sendto", errno, *nsap);
576 timeout.tv_sec = (_res.retrans << try);
578 timeout.tv_sec /= _res.nscount;
579 if ((long) timeout.tv_sec <= 0)
585 n = select(s+1, &dsmask, (fd_set *)NULL,
586 (fd_set *)NULL, &timeout);
588 Perror(stderr, "select", errno);
596 Dprint(_res.options & RES_DEBUG,
597 (stdout, ";; timeout\n")
603 fromlen = sizeof(struct sockaddr_in);
604 resplen = recvfrom(s, ans, anssiz, 0,
605 (struct sockaddr *)&from, &fromlen);
607 Perror(stderr, "recvfrom", errno);
612 if (hp->id != anhp->id) {
614 * response from old query, ignore it.
615 * XXX - potential security hazard could
618 DprintQ((_res.options & RES_DEBUG) ||
619 (_res.pfcode & RES_PRF_REPLY),
620 (stdout, ";; old answer:\n"),
625 if (!(_res.options & RES_INSECURE1) &&
626 !our_server(&from)) {
628 * response from wrong server? ignore it.
629 * XXX - potential security hazard could
632 DprintQ((_res.options & RES_DEBUG) ||
633 (_res.pfcode & RES_PRF_REPLY),
634 (stdout, ";; not our server:\n"),
639 if (!(_res.options & RES_INSECURE2) &&
640 !queries_match(buf, buf + buflen,
641 ans, ans + anssiz)) {
643 * response contains wrong query? ignore it.
644 * XXX - potential security hazard could
647 DprintQ((_res.options & RES_DEBUG) ||
648 (_res.pfcode & RES_PRF_REPLY),
649 (stdout, ";; wrong query name:\n"),
653 if (anhp->rcode == SERVFAIL ||
654 anhp->rcode == NOTIMP ||
655 anhp->rcode == REFUSED) {
656 DprintQ(_res.options & RES_DEBUG,
657 (stdout, "server rejected query:\n"),
663 if (!(_res.options & RES_IGNTC) && anhp->tc) {
665 * get rest of answer;
666 * use TCP with same server.
668 Dprint(_res.options & RES_DEBUG,
669 (stdout, ";; truncated answer\n")
676 DprintQ((_res.options & RES_DEBUG) ||
677 (_res.pfcode & RES_PRF_REPLY),
678 (stdout, ";; got answer:\n"),
681 * If using virtual circuits, we assume that the first server
682 * is preferred over the rest (i.e. it is on the local
683 * machine) and only keep that one open.
684 * If we have temporarily opened a virtual circuit,
685 * or if we haven't been asked to keep a socket open,
688 if ((v_circuit && (!(_res.options & RES_USEVC) || ns != 0)) ||
689 !(_res.options & RES_STAYOPEN)) {
693 int done = 0, loops = 0;
713 /* give the hook another try */
714 if (++loops < 42) /*doug adams*/
732 errno = ECONNREFUSED; /* no nameservers found */
734 errno = ETIMEDOUT; /* no answer obtained */
742 * This routine is for closing the socket if a virtual circuit is used and
743 * the program wants to close it. This provides support for endhostent()
744 * which expects to close the socket.
746 * This routine is not expected to be user visible.