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/types.h>
73 #include <sys/param.h>
75 #include <sys/socket.h>
77 #include <netinet/in.h>
78 #include <arpa/nameser.h>
79 #include <arpa/inet.h>
85 #if defined(BSD) && (BSD >= 199306)
90 # include "../conf/portability.h"
93 #if defined(USE_OPTIONS_H)
94 # include <../conf/options.h>
97 static int s
= -1; /* socket used for communications */
98 static int connected
= 0; /* is the socket connected */
99 static int vc
= 0; /* is the socket a virtual circuit? */
102 /* XXX - should be in portability.h */
104 #define FD_SETSIZE 32
105 #define FD_SET(n, p) ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS)))
106 #define FD_CLR(n, p) ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS)))
107 #define FD_ISSET(n, p) ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS)))
108 #define FD_ZERO(p) bzero((char *)(p), sizeof(*(p)))
111 /* XXX - this should be done in portability.h */
112 #if (defined(BSD) && (BSD >= 199103)) || defined(linux)
113 # define CAN_RECONNECT 1
115 # define CAN_RECONNECT 0
119 # define Dprint(cond, args) /*empty*/
120 # define DprintQ(cond, args, query, size) /*empty*/
121 # define Aerror(file, string, error, address) /*empty*/
122 # define Perror(file, string, error) /*empty*/
124 # define Dprint(cond, args) if (cond) {fprintf args;} else {}
125 # define DprintQ(cond, args, query, size) if (cond) {\
127 __fp_nquery(query, size, stdout);\
130 Aerror(file
, string
, error
, address
)
134 struct sockaddr_in address
;
138 if (_res
.options
& RES_DEBUG
) {
139 fprintf(file
, "res_send: %s ([%s].%u): %s\n",
141 inet_ntoa(address
.sin_addr
),
142 ntohs(address
.sin_port
),
148 Perror(file
, string
, error
)
155 if (_res
.options
& RES_DEBUG
) {
156 fprintf(file
, "res_send: %s: %s\n",
157 string
, strerror(error
));
163 static res_send_qhook Qhook
= NULL
;
164 static res_send_rhook Rhook
= NULL
;
167 res_send_setqhook(hook
)
175 res_send_setrhook(hook
)
183 * res_isourserver(ina)
184 * looks up "ina" in _res.ns_addr_list[]
189 * paul vixie, 29may94
193 const struct sockaddr_in
*inp
;
195 struct sockaddr_in ina
;
196 register int ns
, ret
;
200 for (ns
= 0; ns
< _res
.nscount
; ns
++) {
201 register const struct sockaddr_in
*srv
= &_res
.nsaddr_list
[ns
];
203 if (srv
->sin_family
== ina
.sin_family
&&
204 srv
->sin_port
== ina
.sin_port
&&
205 (srv
->sin_addr
.s_addr
== INADDR_ANY
||
206 srv
->sin_addr
.s_addr
== ina
.sin_addr
.s_addr
)) {
215 * res_nameinquery(name, type, class, buf, eom)
216 * look for (name,type,class) in the query section of packet (buf,eom)
222 * paul vixie, 29may94
225 res_nameinquery(name
, type
, class, buf
, eom
)
227 register int type
, class;
228 const u_char
*buf
, *eom
;
230 register const u_char
*cp
= buf
+ HFIXEDSZ
;
231 int qdcount
= ntohs(((HEADER
*)buf
)->qdcount
);
233 while (qdcount
-- > 0) {
234 char tname
[MAXDNAME
+1];
235 register int n
, ttype
, tclass
;
237 n
= dn_expand(buf
, eom
, cp
, tname
, sizeof tname
);
241 ttype
= _getshort(cp
); cp
+= INT16SZ
;
242 tclass
= _getshort(cp
); cp
+= INT16SZ
;
245 strcasecmp(tname
, name
) == 0)
252 * res_queriesmatch(buf1, eom1, buf2, eom2)
253 * is there a 1:1 mapping of (name,type,class)
254 * in (buf1,eom1) and (buf2,eom2)?
257 * 0 : not a 1:1 mapping
258 * >0 : is a 1:1 mapping
260 * paul vixie, 29may94
263 res_queriesmatch(buf1
, eom1
, buf2
, eom2
)
264 const u_char
*buf1
, *eom1
;
265 const u_char
*buf2
, *eom2
;
267 register const u_char
*cp
= buf1
+ HFIXEDSZ
;
268 int qdcount
= ntohs(((HEADER
*)buf1
)->qdcount
);
270 if (qdcount
!= ntohs(((HEADER
*)buf2
)->qdcount
))
272 while (qdcount
-- > 0) {
273 char tname
[MAXDNAME
+1];
274 register int n
, ttype
, tclass
;
276 n
= dn_expand(buf1
, eom1
, cp
, tname
, sizeof tname
);
280 ttype
= _getshort(cp
); cp
+= INT16SZ
;
281 tclass
= _getshort(cp
); cp
+= INT16SZ
;
282 if (!res_nameinquery(tname
, ttype
, tclass
, buf2
, eom2
))
289 res_send(buf
, buflen
, ans
, anssiz
)
295 HEADER
*hp
= (HEADER
*) buf
;
296 HEADER
*anhp
= (HEADER
*) ans
;
297 int gotsomewhere
, connreset
, terrno
, try, v_circuit
, resplen
, ns
;
299 u_int badns
; /* XXX NSMAX can't exceed #/bits in this var */
301 if ((_res
.options
& RES_INIT
) == 0 && res_init() == -1) {
302 /* errno should have been set by res_init() in this case. */
305 DprintQ((_res
.options
& RES_DEBUG
) || (_res
.pfcode
& RES_PRF_QUERY
),
306 (stdout
, ";; res_send()\n"), buf
, buflen
);
307 v_circuit
= (_res
.options
& RES_USEVC
) || buflen
> PACKETSZ
;
314 * Send request, RETRY times, or until successful
316 for (try = 0; try < _res
.retry
; try++) {
317 for (ns
= 0; ns
< _res
.nscount
; ns
++) {
318 struct sockaddr_in
*nsap
= &_res
.nsaddr_list
[ns
];
320 if (badns
& (1 << ns
)) {
326 int done
= 0, loops
= 0;
331 act
= (*Qhook
)(&nsap
, &buf
, &buflen
,
332 ans
, anssiz
, &resplen
);
343 /* give the hook another try */
344 if (++loops
< 42) /*doug adams*/
355 Dprint(_res
.options
& RES_DEBUG
,
356 (stdout
, ";; Querying server (# %d) address = %s\n",
357 ns
+ 1, inet_ntoa(nsap
->sin_addr
)));
366 * Use virtual circuit;
367 * at most one attempt per server.
371 if ((s
< 0) || (!vc
)) {
375 s
= socket(PF_INET
, SOCK_STREAM
, 0);
378 Perror(stderr
, "socket(vc)", errno
);
382 if (connect(s
, (struct sockaddr
*)nsap
,
383 sizeof(struct sockaddr
)) < 0) {
385 Aerror(stderr
, "connect/vc",
394 * Send length & message
396 putshort((u_short
)buflen
, (u_char
*)&len
);
397 iov
[0].iov_base
= (caddr_t
)&len
;
398 iov
[0].iov_len
= INT16SZ
;
399 iov
[1].iov_base
= (caddr_t
)buf
;
400 iov
[1].iov_len
= buflen
;
401 if (writev(s
, iov
, 2) != (INT16SZ
+ buflen
)) {
403 Perror(stderr
, "write failed", errno
);
409 * Receive length & response
414 while ((n
= read(s
, (char *)cp
, (int)len
)) > 0) {
421 Perror(stderr
, "read failed", errno
);
424 * A long running process might get its TCP
425 * connection reset if the remote server was
426 * restarted. Requery the server instead of
427 * trying a new one. When there is only one
428 * server, this means that a query might work
429 * instead of failing. We only allow one reset
430 * per query to prevent looping.
432 if (terrno
== ECONNRESET
&& !connreset
) {
440 resplen
= _getshort(ans
);
441 if (resplen
> anssiz
) {
442 Dprint(_res
.options
& RES_DEBUG
,
443 (stdout
, ";; response truncated\n")
451 (n
= read(s
, (char *)cp
, (int)len
)) > 0) {
457 Perror(stderr
, "read(vc)", errno
);
463 * Flush rest of answer
464 * so connection stays in synch.
467 len
= resplen
- anssiz
;
471 n
= (len
> sizeof(junk
)
474 if ((n
= read(s
, junk
, n
)) > 0)
481 * The calling applicating has bailed out of
482 * a previous call and failed to arrange to have
483 * the circuit closed or the server has got
484 * itself confused. Anyway drop the packet and
485 * wait for the correct one.
487 if (hp
->id
!= anhp
->id
) {
488 DprintQ((_res
.options
& RES_DEBUG
) ||
489 (_res
.pfcode
& RES_PRF_REPLY
),
490 (stdout
, ";; old answer (unexpected):\n"),
491 ans
, (resplen
>anssiz
)?anssiz
:resplen
);
498 struct timeval timeout
;
500 struct sockaddr_in from
;
506 s
= socket(PF_INET
, SOCK_DGRAM
, 0);
512 Perror(stderr
, "socket(dg)", errno
);
518 * On a 4.3BSD+ machine (client and server,
519 * actually), sending to a nameserver datagram
520 * port with no nameserver will cause an
521 * ICMP port unreachable message to be returned.
522 * If our datagram socket is "connected" to the
523 * server, we get an ECONNREFUSED error on the next
524 * socket operation, and select returns if the
525 * error message is received. We can thus detect
526 * the absence of a nameserver without timing out.
527 * If we have sent queries to at least two servers,
528 * however, we don't want to remain connected,
529 * as we wish to receive answers from the first
532 if (_res
.nscount
== 1 || (try == 0 && ns
== 0)) {
534 * Connect only if we are sure we won't
535 * receive a response from another server.
538 if (connect(s
, (struct sockaddr
*)nsap
,
539 sizeof(struct sockaddr
)
550 if (send(s
, (char*)buf
, buflen
, 0) != buflen
) {
551 Perror(stderr
, "send", errno
);
558 * Disconnect if we want to listen
559 * for responses from more than one server.
563 struct sockaddr_in no_addr
;
565 no_addr
.sin_family
= AF_INET
;
566 no_addr
.sin_addr
.s_addr
= INADDR_ANY
;
567 no_addr
.sin_port
= 0;
573 int s1
= socket(PF_INET
, SOCK_DGRAM
,0);
578 Dprint(_res
.options
& RES_DEBUG
,
579 (stdout
, ";; new DG socket\n"))
584 if (sendto(s
, (char*)buf
, buflen
, 0,
585 (struct sockaddr
*)nsap
,
586 sizeof(struct sockaddr
))
588 Aerror(stderr
, "sendto", errno
, *nsap
);
598 timeout
.tv_sec
= (_res
.retrans
<< try);
600 timeout
.tv_sec
/= _res
.nscount
;
601 if ((long) timeout
.tv_sec
<= 0)
604 if (s
+1 > FD_SETSIZE
) {
605 Perror(stderr
, "s+1 > FD_SETSIZE", EMFILE
);
612 n
= select(s
+1, &dsmask
, (fd_set
*)NULL
,
613 (fd_set
*)NULL
, &timeout
);
617 Perror(stderr
, "select", errno
);
625 Dprint(_res
.options
& RES_DEBUG
,
626 (stdout
, ";; timeout\n"));
632 fromlen
= sizeof(struct sockaddr_in
);
633 resplen
= recvfrom(s
, (char*)ans
, anssiz
, 0,
634 (struct sockaddr
*)&from
, &fromlen
);
636 Perror(stderr
, "recvfrom", errno
);
641 if (hp
->id
!= anhp
->id
) {
643 * response from old query, ignore it.
644 * XXX - potential security hazard could
647 DprintQ((_res
.options
& RES_DEBUG
) ||
648 (_res
.pfcode
& RES_PRF_REPLY
),
649 (stdout
, ";; old answer:\n"),
650 ans
, (resplen
>anssiz
)?anssiz
:resplen
);
654 if (!(_res
.options
& RES_INSECURE1
) &&
655 !res_isourserver(&from
)) {
657 * response from wrong server? ignore it.
658 * XXX - potential security hazard could
661 DprintQ((_res
.options
& RES_DEBUG
) ||
662 (_res
.pfcode
& RES_PRF_REPLY
),
663 (stdout
, ";; not our server:\n"),
664 ans
, (resplen
>anssiz
)?anssiz
:resplen
);
668 if (!(_res
.options
& RES_INSECURE2
) &&
669 !res_queriesmatch(buf
, buf
+ buflen
,
670 ans
, ans
+ anssiz
)) {
672 * response contains wrong query? ignore it.
673 * XXX - potential security hazard could
676 DprintQ((_res
.options
& RES_DEBUG
) ||
677 (_res
.pfcode
& RES_PRF_REPLY
),
678 (stdout
, ";; wrong query name:\n"),
679 ans
, (resplen
>anssiz
)?anssiz
:resplen
);
682 if (anhp
->rcode
== SERVFAIL
||
683 anhp
->rcode
== NOTIMP
||
684 anhp
->rcode
== REFUSED
) {
685 DprintQ(_res
.options
& RES_DEBUG
,
686 (stdout
, "server rejected query:\n"),
687 ans
, (resplen
>anssiz
)?anssiz
:resplen
);
690 /* don't retry if called from dig */
694 if (!(_res
.options
& RES_IGNTC
) && anhp
->tc
) {
696 * get rest of answer;
697 * use TCP with same server.
699 Dprint(_res
.options
& RES_DEBUG
,
700 (stdout
, ";; truncated answer\n"));
706 Dprint((_res
.options
& RES_DEBUG
) ||
707 ((_res
.pfcode
& RES_PRF_REPLY
) &&
708 (_res
.pfcode
& RES_PRF_HEAD1
)),
709 (stdout
, ";; got answer:\n"));
710 DprintQ((_res
.options
& RES_DEBUG
) ||
711 (_res
.pfcode
& RES_PRF_REPLY
),
713 ans
, (resplen
>anssiz
)?anssiz
:resplen
);
715 * If using virtual circuits, we assume that the first server
716 * is preferred over the rest (i.e. it is on the local
717 * machine) and only keep that one open.
718 * If we have temporarily opened a virtual circuit,
719 * or if we haven't been asked to keep a socket open,
722 if ((v_circuit
&& (!(_res
.options
& RES_USEVC
) || ns
!= 0)) ||
723 !(_res
.options
& RES_STAYOPEN
)) {
727 int done
= 0, loops
= 0;
732 act
= (*Rhook
)(nsap
, buf
, buflen
,
733 ans
, anssiz
, &resplen
);
743 /* give the hook another try */
744 if (++loops
< 42) /*doug adams*/
762 __set_errno (ECONNREFUSED
); /* no nameservers found */
764 __set_errno (ETIMEDOUT
); /* no answer obtained */
766 __set_errno (terrno
);
771 * This routine is for closing the socket if a virtual circuit is used and
772 * the program wants to close it. This provides support for endhostent()
773 * which expects to close the socket.
775 * This routine is not expected to be user visible.
789 /* ultrix 4.0 had some icky packaging in its libc.a. alias for it here.
790 * there is more gunk of this kind over in res_debug.c.
801 res_send(buf
, buflen
, ans
, anssiz
)
807 return (__res_send(buf
, buflen
, ans
, anssiz
));
809 #endif /* Ultrix 4.0 hackery */