2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.1 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
25 * ++Copyright++ 1985, 1989, 1993
27 * Copyright (c) 1985, 1989, 1993
28 * The Regents of the University of California. All rights reserved.
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
33 * 1. Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
38 * 3. All advertising materials mentioning features or use of this software
39 * must display the following acknowledgement:
40 * This product includes software developed by the University of
41 * California, Berkeley and its contributors.
42 * 4. Neither the name of the University nor the names of its contributors
43 * may be used to endorse or promote products derived from this software
44 * without specific prior written permission.
46 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
60 * Permission to use, copy, modify, and distribute this software for any
61 * purpose with or without fee is hereby granted, provided that the above
62 * copyright notice and this permission notice appear in all copies, and that
63 * the name of Digital Equipment Corporation not be used in advertising or
64 * publicity pertaining to distribution of the document or software without
65 * specific, written prior permission.
67 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
68 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
69 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
70 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
71 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
72 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
73 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
79 #if defined(LIBC_SCCS) && !defined(lint)
80 static char sccsid
[] = "@(#)res_send.c 8.1 (Berkeley) 6/4/93";
81 static char rcsid
[] = "$Id: res_send.c,v 1.2 1999/10/14 21:56:45 wsanchez Exp $";
82 #endif /* LIBC_SCCS and not lint */
85 * if you talk to a lot
86 * of multi-homed SunOS
87 * ("broken") name servers.
89 #define CHECK_SRVR_ADDR 1 /* XXX - should be in options.h */
92 * Send query to name server and wait for reply.
95 #include <sys/param.h>
97 #include <sys/socket.h>
99 #include <netinet/in.h>
100 #include <arpa/nameser.h>
101 #include <arpa/inet.h>
107 #if defined(BSD) && (BSD >= 199306)
112 # include "portability.h"
115 #if defined(USE_OPTIONS_H)
116 # include "options.h"
119 void _res_close
__P((void));
121 static int s
= -1; /* socket used for communications */
122 static int connected
= 0; /* is the socket connected */
123 static int vc
= 0; /* is the socket a virtual ciruit? */
126 /* XXX - should be in portability.h */
128 #define FD_SETSIZE 32
129 #define FD_SET(n, p) ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS)))
130 #define FD_CLR(n, p) ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS)))
131 #define FD_ISSET(n, p) ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS)))
132 #define FD_ZERO(p) bzero((char *)(p), sizeof(*(p)))
135 /* XXX - this should be done in portability.h */
136 #if (defined(BSD) && (BSD >= 199103)) || defined(linux)
137 # define CAN_RECONNECT 1
139 # define CAN_RECONNECT 0
143 # define Dprint(cond, args) /*empty*/
144 # define DprintQ(cond, args, query, size) /*empty*/
145 # define Aerror(file, string, error, address) /*empty*/
146 # define Perror(file, string, error) /*empty*/
148 # define Dprint(cond, args) if (cond) {fprintf args;} else {}
149 # define DprintQ(cond, args, query, size) if (cond) {\
151 __fp_nquery(query, size, stdout);\
154 Aerror(file
, string
, error
, address
)
158 struct sockaddr_in address
;
162 if (_res
.options
& RES_DEBUG
) {
163 fprintf(file
, "res_send: %s ([%s].%u): %s\n",
165 inet_ntoa(address
.sin_addr
),
166 ntohs(address
.sin_port
),
172 Perror(file
, string
, error
)
179 if (_res
.options
& RES_DEBUG
) {
180 fprintf(file
, "res_send: %s: %s\n",
181 string
, strerror(error
));
187 static res_send_qhook Qhook
= NULL
;
188 static res_send_rhook Rhook
= NULL
;
191 res_send_setqhook(hook
)
199 res_send_setrhook(hook
)
207 * res_isourserver(ina)
208 * looks up "ina" in _res.ns_addr_list[]
213 * paul vixie, 29may94
217 const struct sockaddr_in
*inp
;
219 struct sockaddr_in ina
;
220 register int ns
, ret
;
224 for (ns
= 0; ns
< _res
.nscount
; ns
++) {
225 register const struct sockaddr_in
*srv
= &_res
.nsaddr_list
[ns
];
227 if (srv
->sin_family
== ina
.sin_family
&&
228 srv
->sin_port
== ina
.sin_port
&&
229 (srv
->sin_addr
.s_addr
== INADDR_ANY
||
230 srv
->sin_addr
.s_addr
== ina
.sin_addr
.s_addr
)) {
239 * res_nameinquery(name, type, class, buf, eom)
240 * look for (name,type,class) in the query section of packet (buf,eom)
246 * paul vixie, 29may94
249 res_nameinquery(name
, type
, class, buf
, eom
)
251 register int type
, class;
252 const u_char
*buf
, *eom
;
254 register const u_char
*cp
= buf
+ HFIXEDSZ
;
255 int qdcount
= ntohs(((HEADER
*)buf
)->qdcount
);
257 while (qdcount
-- > 0) {
258 char tname
[MAXDNAME
+1];
259 register int n
, ttype
, tclass
;
261 n
= dn_expand(buf
, eom
, cp
, tname
, sizeof tname
);
265 ttype
= _getshort(cp
); cp
+= INT16SZ
;
266 tclass
= _getshort(cp
); cp
+= INT16SZ
;
269 strcasecmp(tname
, name
) == 0)
276 * res_queriesmatch(buf1, eom1, buf2, eom2)
277 * is there a 1:1 mapping of (name,type,class)
278 * in (buf1,eom1) and (buf2,eom2)?
281 * 0 : not a 1:1 mapping
282 * >0 : is a 1:1 mapping
284 * paul vixie, 29may94
287 res_queriesmatch(buf1
, eom1
, buf2
, eom2
)
288 const u_char
*buf1
, *eom1
;
289 const u_char
*buf2
, *eom2
;
291 register const u_char
*cp
= buf1
+ HFIXEDSZ
;
292 int qdcount
= ntohs(((HEADER
*)buf1
)->qdcount
);
294 if (qdcount
!= ntohs(((HEADER
*)buf2
)->qdcount
))
296 while (qdcount
-- > 0) {
297 char tname
[MAXDNAME
+1];
298 register int n
, ttype
, tclass
;
300 n
= dn_expand(buf1
, eom1
, cp
, tname
, sizeof tname
);
304 ttype
= _getshort(cp
); cp
+= INT16SZ
;
305 tclass
= _getshort(cp
); cp
+= INT16SZ
;
306 if (!res_nameinquery(tname
, ttype
, tclass
, buf2
, eom2
))
313 res_send(buf
, buflen
, ans
, anssiz
)
319 HEADER
*hp
= (HEADER
*) buf
;
320 HEADER
*anhp
= (HEADER
*) ans
;
321 int gotsomewhere
, connreset
, terrno
, try, v_circuit
, resplen
, ns
;
323 u_int badns
; /* XXX NSMAX can't exceed #/bits in this var */
325 if ((_res
.options
& RES_INIT
) == 0 && res_init() == -1) {
326 /* errno should have been set by res_init() in this case. */
329 DprintQ((_res
.options
& RES_DEBUG
) || (_res
.pfcode
& RES_PRF_QUERY
),
330 (stdout
, ";; res_send()\n"), buf
, buflen
);
331 v_circuit
= (_res
.options
& RES_USEVC
) || buflen
> PACKETSZ
;
338 * Send request, RETRY times, or until successful
340 for (try = 0; try < _res
.retry
; try++) {
341 for (ns
= 0; ns
< _res
.nscount
; ns
++) {
342 struct sockaddr_in
*nsap
= &_res
.nsaddr_list
[ns
];
344 if (badns
& (1 << ns
)) {
350 int done
= 0, loops
= 0;
355 act
= (*Qhook
)(&nsap
, &buf
, &buflen
,
356 ans
, anssiz
, &resplen
);
367 /* give the hook another try */
368 if (++loops
< 42) /*doug adams*/
379 Dprint(_res
.options
& RES_DEBUG
,
380 (stdout
, ";; Querying server (# %d) address = %s\n",
381 ns
+ 1, inet_ntoa(nsap
->sin_addr
)));
390 * Use virtual circuit;
391 * at most one attempt per server.
395 if ((s
< 0) || (!vc
)) {
399 s
= socket(PF_INET
, SOCK_STREAM
, 0);
402 Perror(stderr
, "socket(vc)", errno
);
406 if (connect(s
, (struct sockaddr
*)nsap
,
407 sizeof(struct sockaddr
)) < 0) {
409 Aerror(stderr
, "connect/vc",
418 * Send length & message
420 putshort((u_short
)buflen
, (u_char
*)&len
);
421 iov
[0].iov_base
= (caddr_t
)&len
;
422 iov
[0].iov_len
= INT16SZ
;
423 iov
[1].iov_base
= (caddr_t
)buf
;
424 iov
[1].iov_len
= buflen
;
425 if (writev(s
, iov
, 2) != (INT16SZ
+ buflen
)) {
427 Perror(stderr
, "write failed", errno
);
433 * Receive length & response
437 while ((n
= read(s
, (char *)cp
, (int)len
)) > 0) {
444 Perror(stderr
, "read failed", errno
);
447 * A long running process might get its TCP
448 * connection reset if the remote server was
449 * restarted. Requery the server instead of
450 * trying a new one. When there is only one
451 * server, this means that a query might work
452 * instead of failing. We only allow one reset
453 * per query to prevent looping.
455 if (terrno
== ECONNRESET
&& !connreset
) {
463 resplen
= _getshort(ans
);
464 if (resplen
> anssiz
) {
465 Dprint(_res
.options
& RES_DEBUG
,
466 (stdout
, ";; response truncated\n")
474 (n
= read(s
, (char *)cp
, (int)len
)) > 0) {
480 Perror(stderr
, "read(vc)", errno
);
486 * Flush rest of answer
487 * so connection stays in synch.
490 len
= resplen
- anssiz
;
494 n
= (len
> sizeof(junk
)
497 if ((n
= read(s
, junk
, n
)) > 0)
507 struct timeval timeout
;
509 struct sockaddr_in from
;
515 s
= socket(PF_INET
, SOCK_DGRAM
, 0);
521 Perror(stderr
, "socket(dg)", errno
);
527 * On a 4.3BSD+ machine (client and server,
528 * actually), sending to a nameserver datagram
529 * port with no nameserver will cause an
530 * ICMP port unreachable message to be returned.
531 * If our datagram socket is "connected" to the
532 * server, we get an ECONNREFUSED error on the next
533 * socket operation, and select returns if the
534 * error message is received. We can thus detect
535 * the absence of a nameserver without timing out.
536 * If we have sent queries to at least two servers,
537 * however, we don't want to remain connected,
538 * as we wish to receive answers from the first
541 if (_res
.nscount
== 1 || (try == 0 && ns
== 0)) {
543 * Connect only if we are sure we won't
544 * receive a response from another server.
547 if (connect(s
, (struct sockaddr
*)nsap
,
548 sizeof(struct sockaddr
)
559 if (send(s
, (char*)buf
, buflen
, 0) != buflen
) {
560 Perror(stderr
, "send", errno
);
567 * Disconnect if we want to listen
568 * for responses from more than one server.
572 struct sockaddr_in no_addr
;
574 no_addr
.sin_family
= AF_INET
;
575 no_addr
.sin_addr
.s_addr
= INADDR_ANY
;
576 no_addr
.sin_port
= 0;
582 int s1
= socket(PF_INET
, SOCK_DGRAM
,0);
587 Dprint(_res
.options
& RES_DEBUG
,
588 (stdout
, ";; new DG socket\n"))
593 if (sendto(s
, (char*)buf
, buflen
, 0,
594 (struct sockaddr
*)nsap
,
595 sizeof(struct sockaddr
))
597 Aerror(stderr
, "sendto", errno
, *nsap
);
607 timeout
.tv_sec
= (_res
.retrans
<< try);
609 timeout
.tv_sec
/= _res
.nscount
;
610 if ((long) timeout
.tv_sec
<= 0)
616 n
= select(s
+1, &dsmask
, (fd_set
*)NULL
,
617 (fd_set
*)NULL
, &timeout
);
619 Perror(stderr
, "select", errno
);
627 Dprint(_res
.options
& RES_DEBUG
,
628 (stdout
, ";; timeout\n"));
634 fromlen
= sizeof(struct sockaddr_in
);
635 resplen
= recvfrom(s
, (char*)ans
, anssiz
, 0,
636 (struct sockaddr
*)&from
, &fromlen
);
638 Perror(stderr
, "recvfrom", errno
);
643 if (hp
->id
!= anhp
->id
) {
645 * response from old query, ignore it.
646 * XXX - potential security hazard could
649 DprintQ((_res
.options
& RES_DEBUG
) ||
650 (_res
.pfcode
& RES_PRF_REPLY
),
651 (stdout
, ";; old answer:\n"),
656 if (!(_res
.options
& RES_INSECURE1
) &&
657 !res_isourserver(&from
)) {
659 * response from wrong server? ignore it.
660 * XXX - potential security hazard could
663 DprintQ((_res
.options
& RES_DEBUG
) ||
664 (_res
.pfcode
& RES_PRF_REPLY
),
665 (stdout
, ";; not our server:\n"),
670 if (!(_res
.options
& RES_INSECURE2
) &&
671 !res_queriesmatch(buf
, buf
+ buflen
,
672 ans
, ans
+ anssiz
)) {
674 * response contains wrong query? ignore it.
675 * XXX - potential security hazard could
678 DprintQ((_res
.options
& RES_DEBUG
) ||
679 (_res
.pfcode
& RES_PRF_REPLY
),
680 (stdout
, ";; wrong query name:\n"),
684 if (anhp
->rcode
== SERVFAIL
||
685 anhp
->rcode
== NOTIMP
||
686 anhp
->rcode
== REFUSED
) {
687 DprintQ(_res
.options
& RES_DEBUG
,
688 (stdout
, "server rejected query:\n"),
692 /* don't retry if called from dig */
696 if (!(_res
.options
& RES_IGNTC
) && anhp
->tc
) {
698 * get rest of answer;
699 * use TCP with same server.
701 Dprint(_res
.options
& RES_DEBUG
,
702 (stdout
, ";; truncated answer\n"));
708 DprintQ((_res
.options
& RES_DEBUG
) ||
709 (_res
.pfcode
& RES_PRF_REPLY
),
710 (stdout
, ";; got answer:\n"),
713 * If using virtual circuits, we assume that the first server
714 * is preferred over the rest (i.e. it is on the local
715 * machine) and only keep that one open.
716 * If we have temporarily opened a virtual circuit,
717 * or if we haven't been asked to keep a socket open,
720 if ((v_circuit
&& (!(_res
.options
& RES_USEVC
) || ns
!= 0)) ||
721 !(_res
.options
& RES_STAYOPEN
)) {
725 int done
= 0, loops
= 0;
730 act
= (*Rhook
)(nsap
, buf
, buflen
,
731 ans
, anssiz
, &resplen
);
741 /* give the hook another try */
742 if (++loops
< 42) /*doug adams*/
760 errno
= ECONNREFUSED
; /* no nameservers found */
762 errno
= ETIMEDOUT
; /* no answer obtained */
769 * This routine is for closing the socket if a virtual circuit is used and
770 * the program wants to close it. This provides support for endhostent()
771 * which expects to close the socket.
773 * This routine is not expected to be user visible.