]>
git.proxmox.com Git - mirror_edk2.git/blob - StdLib/BsdSocketLib/res_send.c
2 * Copyright (c) 1985, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Portions copyright (c) 1999, 2000
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
23 * This product includes software developed by the University of
24 * California, Berkeley, Intel Corporation, and its contributors.
26 * 4. Neither the name of University, Intel Corporation, or their respective
27 * contributors may be used to endorse or promote products derived from
28 * this software without specific prior written permission.
30 * THIS SOFTWARE IS PROVIDED BY THE REGENTS, INTEL CORPORATION AND
31 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
32 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
33 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS,
34 * INTEL CORPORATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
35 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
40 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
47 * Permission to use, copy, modify, and distribute this software for any
48 * purpose with or without fee is hereby granted, provided that the above
49 * copyright notice and this permission notice appear in all copies, and that
50 * the name of Digital Equipment Corporation not be used in advertising or
51 * publicity pertaining to distribution of the document or software without
52 * specific, written prior permission.
54 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
55 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
56 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
57 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
58 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
59 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
60 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
65 * Portions Copyright (c) 1996 by Internet Software Consortium.
67 * Permission to use, copy, modify, and distribute this software for any
68 * purpose with or without fee is hereby granted, provided that the above
69 * copyright notice and this permission notice appear in all copies.
71 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
72 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
73 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
74 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
75 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
76 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
77 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
81 #if defined(LIBC_SCCS) && !defined(lint)
82 static char sccsid
[] = "@(#)res_send.c 8.1 (Berkeley) 6/4/93";
83 static char orig_rcsid
[] = "From: Id: res_send.c,v 8.20 1998/04/06 23:27:51 halley Exp $";
84 static char rcsid
[] = "$Id: res_send.c,v 1.1.1.1 2003/11/19 01:51:39 kyu3 Exp $";
85 #endif /* LIBC_SCCS and not lint */
88 * Send query to name server and wait for reply.
91 #include <sys/types.h>
92 #include <sys/param.h>
93 #include <sys/select.h>
94 #include <sys/socket.h>
98 #include <netinet/in.h>
99 #include <arpa/nameser.h>
100 #include <arpa/inet.h>
110 #include "res_config.h"
112 #ifndef _ORG_FREEBSD_
116 #ifdef NOPOLL /* libc_r doesn't wrap poll yet() */
119 static int use_poll
= 1; /* adapt to poll() syscall availability */
120 /* 0 = not present, 1 = try it, 2 = exists */
123 static int s
= -1; /* socket used for communications */
124 static int connected
= 0; /* is the socket connected */
125 static int vc
= 0; /* is the socket a virtual circuit? */
126 static res_send_qhook Qhook
= NULL
;
127 static res_send_rhook Rhook
= NULL
;
130 #define CAN_RECONNECT 1
133 # define Dprint(cond, args) /*empty*/
134 # define DprintQ(cond, args, query, size) /*empty*/
135 # define Aerror(file, string, error, address) /*empty*/
136 # define Perror(file, string, error) /*empty*/
138 # define Dprint(cond, args) if (cond) {fprintf args;} else {}
139 # define DprintQ(cond, args, query, size) if (cond) {\
141 __fp_nquery(query, size, stdout);\
149 struct sockaddr_in address
154 if (_res
.options
& RES_DEBUG
) {
155 fprintf(file
, "res_send: %s ([%s].%u): %s\n",
157 inet_ntoa(address
.sin_addr
),
158 ntohs(address
.sin_port
),
174 if (_res
.options
& RES_DEBUG
) {
175 fprintf(file
, "res_send: %s: %s\n",
176 string
, strerror(error
));
201 * res_isourserver(ina)
202 * looks up "ina" in _res.ns_addr_list[]
207 * paul vixie, 29may94
211 const struct sockaddr_in
*inp
214 struct sockaddr_in ina
;
219 for (ns
= 0; ns
< _res
.nscount
; ns
++) {
220 const struct sockaddr_in
*srv
= &_res
.nsaddr_list
[ns
];
222 if (srv
->sin_family
== ina
.sin_family
&&
223 srv
->sin_port
== ina
.sin_port
&&
224 (srv
->sin_addr
.s_addr
== INADDR_ANY
||
225 srv
->sin_addr
.s_addr
== ina
.sin_addr
.s_addr
)) {
234 * res_nameinquery(name, type, class, buf, eom)
235 * look for (name,type,class) in the query section of packet (buf,eom)
237 * buf + HFIXEDSZ <= eom
243 * paul vixie, 29may94
254 const u_char
*cp
= buf
+ HFIXEDSZ
;
255 int qdcount
= ntohs(((HEADER
*)buf
)->qdcount
);
257 while (qdcount
-- > 0) {
258 char tname
[MAXDNAME
+1];
259 int n
, ttype
, tclass
;
261 n
= dn_expand(buf
, eom
, cp
, tname
, sizeof tname
);
265 if (cp
+ 2 * INT16SZ
> eom
)
267 ttype
= ns_get16(cp
); cp
+= INT16SZ
;
268 tclass
= ns_get16(cp
); cp
+= INT16SZ
;
271 strcasecmp(tname
, name
) == 0)
278 * res_queriesmatch(buf1, eom1, buf2, eom2)
279 * is there a 1:1 mapping of (name,type,class)
280 * in (buf1,eom1) and (buf2,eom2)?
283 * 0 : not a 1:1 mapping
284 * >0 : is a 1:1 mapping
286 * paul vixie, 29may94
296 const u_char
*cp
= buf1
+ HFIXEDSZ
;
297 int qdcount
= ntohs(((HEADER
*)buf1
)->qdcount
);
299 if (buf1
+ HFIXEDSZ
> eom1
|| buf2
+ HFIXEDSZ
> eom2
)
303 * Only header section present in replies to
304 * dynamic update packets.
306 if ( (((HEADER
*)buf1
)->opcode
== ns_o_update
) &&
307 (((HEADER
*)buf2
)->opcode
== ns_o_update
) )
310 if (qdcount
!= ntohs(((HEADER
*)buf2
)->qdcount
))
312 while (qdcount
-- > 0) {
313 char tname
[MAXDNAME
+1];
314 int n
, ttype
, tclass
;
316 n
= dn_expand(buf1
, eom1
, cp
, tname
, sizeof tname
);
320 if (cp
+ 2 * INT16SZ
> eom1
)
322 ttype
= ns_get16(cp
); cp
+= INT16SZ
;
323 tclass
= ns_get16(cp
); cp
+= INT16SZ
;
324 if (!res_nameinquery(tname
, ttype
, tclass
, buf2
, eom2
))
338 HEADER
*hp
= (HEADER
*) buf
;
339 HEADER
*anhp
= (HEADER
*) ans
;
340 int gotsomewhere
, connreset
, terrno
, try, v_circuit
, resplen
, ns
;
342 u_int32_t badns
; /* XXX NSMAX can't exceed #/bits in this variable */
344 if ((_res
.options
& RES_INIT
) == 0 && res_init() == -1) {
345 /* errno should have been set by res_init() in this case. */
348 if (anssiz
< HFIXEDSZ
) {
352 DprintQ((_res
.options
& RES_DEBUG
) || (_res
.pfcode
& RES_PRF_QUERY
),
353 (stdout
, ";; res_send()\n"), buf
, buflen
);
354 v_circuit
= (_res
.options
& RES_USEVC
) || buflen
> PACKETSZ
;
361 * Send request, RETRY times, or until successful
363 for (try = 0; try < _res
.retry
; try++) {
364 for (ns
= 0; ns
< _res
.nscount
; ns
++) {
365 struct sockaddr_in
*nsap
= &_res
.nsaddr_list
[ns
];
367 if (badns
& (1 << ns
)) {
373 int done
= 0, loops
= 0;
378 act
= (*Qhook
)(&nsap
, &buf
, &buflen
,
379 ans
, anssiz
, &resplen
);
390 /* give the hook another try */
391 if (++loops
< 42) /*doug adams*/
402 Dprint(_res
.options
& RES_DEBUG
,
403 (stdout
, ";; Querying server (# %d) address = %s\n",
404 ns
+ 1, inet_ntoa(nsap
->sin_addr
)));
413 * Use virtual circuit;
414 * at most one attempt per server.
418 if (s
< 0 || !vc
|| hp
->opcode
== ns_o_update
) {
422 s
= socket(PF_INET
, SOCK_STREAM
, 0);
425 Perror(stderr
, "socket(vc)", errno
);
429 nsap
->sin_len
= sizeof ( *nsap
);
430 if (connect(s
, (struct sockaddr
*)nsap
,
433 Aerror(stderr
, "connect/vc",
442 * Send length & message
444 putshort((u_short
)buflen
, (u_char
*)&len
);
445 iov
[0].iov_base
= (caddr_t
)&len
;
446 iov
[0].iov_len
= INT16SZ
;
447 iov
[1].iov_base
= (caddr_t
)buf
;
448 iov
[1].iov_len
= buflen
;
449 if (writev(s
, iov
, 2) != (INT16SZ
+ buflen
)) {
451 Perror(stderr
, "write failed", errno
);
457 * Receive length & response
462 while ((n
= read(s
, (char *)cp
, (int)len
)) > 0) {
464 len
= (u_short
)( len
- n
);
470 Perror(stderr
, "read failed", errno
);
473 * A long running process might get its TCP
474 * connection reset if the remote server was
475 * restarted. Requery the server instead of
476 * trying a new one. When there is only one
477 * server, this means that a query might work
478 * instead of failing. We only allow one reset
479 * per query to prevent looping.
481 if (terrno
== ECONNRESET
&& !connreset
) {
489 resplen
= ns_get16(ans
);
490 if (resplen
> anssiz
) {
491 Dprint(_res
.options
& RES_DEBUG
,
492 (stdout
, ";; response truncated\n")
495 len
= (ushort
)anssiz
;
497 len
= (ushort
)resplen
;
498 if (len
< HFIXEDSZ
) {
500 * Undersized message.
502 Dprint(_res
.options
& RES_DEBUG
,
503 (stdout
, ";; undersized: %d\n", len
));
511 (n
= read(s
, (char *)cp
, (int)len
)) > 0) {
513 len
= (u_short
)( len
- n
);
517 Perror(stderr
, "read(vc)", errno
);
523 * Flush rest of answer
524 * so connection stays in synch.
527 len
= (ushort
)( resplen
- anssiz
);
531 n
= (len
> sizeof(junk
)
534 if ((n
= read(s
, junk
, n
)) > 0)
535 len
= (u_short
)( len
- n
);
541 * The calling applicating has bailed out of
542 * a previous call and failed to arrange to have
543 * the circuit closed or the server has got
544 * itself confused. Anyway drop the packet and
545 * wait for the correct one.
547 if (hp
->id
!= anhp
->id
) {
548 DprintQ((_res
.options
& RES_DEBUG
) ||
549 (_res
.pfcode
& RES_PRF_REPLY
),
550 (stdout
, ";; old answer (unexpected):\n"),
551 ans
, (resplen
>anssiz
)?anssiz
:resplen
);
562 struct timeval timeout
;
563 fd_set dsmask
, *dsmaskp
;
565 struct sockaddr_in from
;
571 s
= socket(PF_INET
, SOCK_DGRAM
, 0);
573 #ifndef CAN_RECONNECT
577 Perror(stderr
, "socket(dg)", errno
);
582 #ifndef CANNOT_CONNECT_DGRAM
584 * On a 4.3BSD+ machine (client and server,
585 * actually), sending to a nameserver datagram
586 * port with no nameserver will cause an
587 * ICMP port unreachable message to be returned.
588 * If our datagram socket is "connected" to the
589 * server, we get an ECONNREFUSED error on the next
590 * socket operation, and select returns if the
591 * error message is received. We can thus detect
592 * the absence of a nameserver without timing out.
593 * If we have sent queries to at least two servers,
594 * however, we don't want to remain connected,
595 * as we wish to receive answers from the first
598 if (_res
.nscount
== 1 || (try == 0 && ns
== 0)) {
600 * Connect only if we are sure we won't
601 * receive a response from another server.
604 nsap
->sin_len
= sizeof ( *nsap
);
605 if (connect(s
, (struct sockaddr
*)nsap
,
617 if (send(s
, (char*)buf
, buflen
, 0) != buflen
) {
618 Perror(stderr
, "send", errno
);
625 * Disconnect if we want to listen
626 * for responses from more than one server.
630 struct sockaddr_in no_addr
;
632 no_addr
.sin_family
= AF_INET
;
633 no_addr
.sin_addr
.s_addr
= INADDR_ANY
;
634 no_addr
.sin_port
= 0;
640 int s1
= socket(PF_INET
, SOCK_DGRAM
,0);
645 Dprint(_res
.options
& RES_DEBUG
,
646 (stdout
, ";; new DG socket\n"))
647 #endif /* CAN_RECONNECT */
651 #endif /* !CANNOT_CONNECT_DGRAM */
652 if (sendto(s
, (char*)buf
, buflen
, 0,
653 (struct sockaddr
*)nsap
,
656 Aerror(stderr
, "sendto", errno
, *nsap
);
661 #ifndef CANNOT_CONNECT_DGRAM
663 #endif /* !CANNOT_CONNECT_DGRAM */
671 msec
= (_res
.retrans
<< try) * 1000;
673 msec
/= _res
.nscount
;
678 timeout
.tv_sec
= (_res
.retrans
<< try);
680 timeout
.tv_sec
/= _res
.nscount
;
681 if ((long) timeout
.tv_sec
<= 0)
689 Perror(stderr
, "s out-of-bounds", EMFILE
);
695 struct sigaction sa
, osa
;
696 int sigsys_installed
= 0;
701 bzero(&sa
, sizeof(sa
));
702 sa
.sa_handler
= SIG_IGN
;
703 if (sigaction(SIGSYS
, &sa
, &osa
) >= 0)
704 sigsys_installed
= 1;
706 n
= poll(&pfd
, 1, msec
);
707 if (sigsys_installed
== 1) {
709 sigaction(SIGSYS
, &osa
, NULL
);
712 /* XXX why does nosys() return EINVAL? */
713 if (n
< 0 && (errno
== ENOSYS
||
717 } else if (use_poll
== 1)
722 Perror(stderr
, "poll", errno
);
728 dsmasklen
= howmany(s
+ 1, NFDBITS
) *
730 if (dsmasklen
> sizeof(fd_set
)) {
731 dsmaskp
= (fd_set
*)malloc(dsmasklen
);
732 if (dsmaskp
== NULL
) {
738 /* only zero what we need */
739 memset((char *)dsmaskp
, 0, dsmasklen
);
741 n
= select(s
+ 1, dsmaskp
, (fd_set
*)NULL
,
742 (fd_set
*)NULL
, &timeout
);
743 if (dsmaskp
!= &dsmask
)
748 Perror(stderr
, "select", errno
);
760 Dprint(_res
.options
& RES_DEBUG
,
761 (stdout
, ";; timeout\n"));
767 fromlen
= sizeof(struct sockaddr_in
);
768 resplen
= (int)recvfrom(s
, (char*)ans
, anssiz
, 0,
769 (struct sockaddr
*)&from
, (socklen_t
*)&fromlen
);
771 Perror(stderr
, "recvfrom", errno
);
776 if (resplen
< HFIXEDSZ
) {
778 * Undersized message.
780 Dprint(_res
.options
& RES_DEBUG
,
781 (stdout
, ";; undersized: %d\n",
788 if (hp
->id
!= anhp
->id
) {
790 * response from old query, ignore it.
791 * XXX - potential security hazard could
794 DprintQ((_res
.options
& RES_DEBUG
) ||
795 (_res
.pfcode
& RES_PRF_REPLY
),
796 (stdout
, ";; old answer:\n"),
797 ans
, (resplen
>anssiz
)?anssiz
:resplen
);
800 #ifdef CHECK_SRVR_ADDR
801 if (!(_res
.options
& RES_INSECURE1
) &&
802 !res_isourserver(&from
)) {
804 * response from wrong server? ignore it.
805 * XXX - potential security hazard could
808 DprintQ((_res
.options
& RES_DEBUG
) ||
809 (_res
.pfcode
& RES_PRF_REPLY
),
810 (stdout
, ";; not our server:\n"),
811 ans
, (resplen
>anssiz
)?anssiz
:resplen
);
815 if (!(_res
.options
& RES_INSECURE2
) &&
816 !res_queriesmatch(buf
, buf
+ buflen
,
817 ans
, ans
+ anssiz
)) {
819 * response contains wrong query? ignore it.
820 * XXX - potential security hazard could
823 DprintQ((_res
.options
& RES_DEBUG
) ||
824 (_res
.pfcode
& RES_PRF_REPLY
),
825 (stdout
, ";; wrong query name:\n"),
826 ans
, (resplen
>anssiz
)?anssiz
:resplen
);
829 if (anhp
->rcode
== SERVFAIL
||
830 anhp
->rcode
== NOTIMP
||
831 anhp
->rcode
== REFUSED
) {
832 DprintQ(_res
.options
& RES_DEBUG
,
833 (stdout
, "server rejected query:\n"),
834 ans
, (resplen
>anssiz
)?anssiz
:resplen
);
837 /* don't retry if called from dig */
841 if (!(_res
.options
& RES_IGNTC
) && anhp
->tc
) {
843 * get rest of answer;
844 * use TCP with same server.
846 Dprint(_res
.options
& RES_DEBUG
,
847 (stdout
, ";; truncated answer\n"));
853 Dprint((_res
.options
& RES_DEBUG
) ||
854 ((_res
.pfcode
& RES_PRF_REPLY
) &&
855 (_res
.pfcode
& RES_PRF_HEAD1
)),
856 (stdout
, ";; got answer:\n"));
857 if((_res
.options
& RES_DEBUG
) ||
858 (_res
.pfcode
& RES_PRF_REPLY
)) {
859 __fp_nquery(ans
, (resplen
>anssiz
)?anssiz
:resplen
, stdout
);
862 * If using virtual circuits, we assume that the first server
863 * is preferred over the rest (i.e. it is on the local
864 * machine) and only keep that one open.
865 * If we have temporarily opened a virtual circuit,
866 * or if we haven't been asked to keep a socket open,
869 if ((v_circuit
&& (!(_res
.options
& RES_USEVC
) || ns
!= 0)) ||
870 !(_res
.options
& RES_STAYOPEN
)) {
874 int done
= 0, loops
= 0;
879 act
= (*Rhook
)(nsap
, buf
, buflen
,
880 ans
, anssiz
, &resplen
);
890 /* give the hook another try */
891 if (++loops
< 42) /*doug adams*/
909 errno
= ECONNREFUSED
; /* no nameservers found */
911 errno
= ETIMEDOUT
; /* no answer obtained */
918 * This routine is for closing the socket if a virtual circuit is used and
919 * the program wants to close it. This provides support for endhostent()
920 * which expects to close the socket.
922 * This routine is not expected to be user visible.