]>
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() */
117 static int use_poll
= 0;
120 static int use_poll
= 1; /* adapt to poll() syscall availability */
121 /* 0 = not present, 1 = try it, 2 = exists */
124 static int s
= -1; /* socket used for communications */
125 static int connected
= 0; /* is the socket connected */
126 static int vc
= 0; /* is the socket a virtual circuit? */
127 static res_send_qhook Qhook
= NULL
;
128 static res_send_rhook Rhook
= NULL
;
131 #define CAN_RECONNECT 1
134 # define Dprint(cond, args) /*empty*/
135 # define DprintQ(cond, args, query, size) /*empty*/
136 # define Aerror(file, string, error, address) /*empty*/
137 # define Perror(file, string, error) /*empty*/
139 # define Dprint(cond, args) if (cond) {fprintf args;} else {}
140 # define DprintQ(cond, args, query, size) if (cond) {\
142 __fp_nquery(query, size, stdout);\
150 struct sockaddr_in address
155 if (_res
.options
& RES_DEBUG
) {
156 fprintf(file
, "res_send: %s ([%s].%u): %s\n",
158 inet_ntoa(address
.sin_addr
),
159 ntohs(address
.sin_port
),
175 if (_res
.options
& RES_DEBUG
) {
176 fprintf(file
, "res_send: %s: %s\n",
177 string
, strerror(error
));
202 * res_isourserver(ina)
203 * looks up "ina" in _res.ns_addr_list[]
208 * paul vixie, 29may94
212 const struct sockaddr_in
*inp
215 struct sockaddr_in ina
;
220 for (ns
= 0; ns
< _res
.nscount
; ns
++) {
221 const struct sockaddr_in
*srv
= &_res
.nsaddr_list
[ns
];
223 if (srv
->sin_family
== ina
.sin_family
&&
224 srv
->sin_port
== ina
.sin_port
&&
225 (srv
->sin_addr
.s_addr
== INADDR_ANY
||
226 srv
->sin_addr
.s_addr
== ina
.sin_addr
.s_addr
)) {
235 * res_nameinquery(name, type, class, buf, eom)
236 * look for (name,type,class) in the query section of packet (buf,eom)
238 * buf + HFIXEDSZ <= eom
244 * paul vixie, 29may94
255 const u_char
*cp
= buf
+ HFIXEDSZ
;
256 int qdcount
= ntohs(((HEADER
*)buf
)->qdcount
);
258 while (qdcount
-- > 0) {
259 char tname
[MAXDNAME
+1];
260 int n
, ttype
, tclass
;
262 n
= dn_expand(buf
, eom
, cp
, tname
, sizeof tname
);
266 if (cp
+ 2 * INT16SZ
> eom
)
268 ttype
= ns_get16(cp
); cp
+= INT16SZ
;
269 tclass
= ns_get16(cp
); cp
+= INT16SZ
;
272 strcasecmp(tname
, name
) == 0)
279 * res_queriesmatch(buf1, eom1, buf2, eom2)
280 * is there a 1:1 mapping of (name,type,class)
281 * in (buf1,eom1) and (buf2,eom2)?
284 * 0 : not a 1:1 mapping
285 * >0 : is a 1:1 mapping
287 * paul vixie, 29may94
297 const u_char
*cp
= buf1
+ HFIXEDSZ
;
298 int qdcount
= ntohs(((HEADER
*)buf1
)->qdcount
);
300 if (buf1
+ HFIXEDSZ
> eom1
|| buf2
+ HFIXEDSZ
> eom2
)
304 * Only header section present in replies to
305 * dynamic update packets.
307 if ( (((HEADER
*)buf1
)->opcode
== ns_o_update
) &&
308 (((HEADER
*)buf2
)->opcode
== ns_o_update
) )
311 if (qdcount
!= ntohs(((HEADER
*)buf2
)->qdcount
))
313 while (qdcount
-- > 0) {
314 char tname
[MAXDNAME
+1];
315 int n
, ttype
, tclass
;
317 n
= dn_expand(buf1
, eom1
, cp
, tname
, sizeof tname
);
321 if (cp
+ 2 * INT16SZ
> eom1
)
323 ttype
= ns_get16(cp
); cp
+= INT16SZ
;
324 tclass
= ns_get16(cp
); cp
+= INT16SZ
;
325 if (!res_nameinquery(tname
, ttype
, tclass
, buf2
, eom2
))
339 HEADER
*hp
= (HEADER
*) buf
;
340 HEADER
*anhp
= (HEADER
*) ans
;
341 int gotsomewhere
, connreset
, terrno
, try, v_circuit
, resplen
, ns
;
343 u_int32_t badns
; /* XXX NSMAX can't exceed #/bits in this variable */
345 if ((_res
.options
& RES_INIT
) == 0 && res_init() == -1) {
346 /* errno should have been set by res_init() in this case. */
349 if (anssiz
< HFIXEDSZ
) {
353 DprintQ((_res
.options
& RES_DEBUG
) || (_res
.pfcode
& RES_PRF_QUERY
),
354 (stdout
, ";; res_send()\n"), buf
, buflen
);
355 v_circuit
= (_res
.options
& RES_USEVC
) || buflen
> PACKETSZ
;
362 * Send request, RETRY times, or until successful
364 for (try = 0; try < _res
.retry
; try++) {
365 for (ns
= 0; ns
< _res
.nscount
; ns
++) {
366 struct sockaddr_in
*nsap
= &_res
.nsaddr_list
[ns
];
368 if (badns
& (1 << ns
)) {
374 int done
= 0, loops
= 0;
379 act
= (*Qhook
)(&nsap
, &buf
, &buflen
,
380 ans
, anssiz
, &resplen
);
391 /* give the hook another try */
392 if (++loops
< 42) /*doug adams*/
403 Dprint(_res
.options
& RES_DEBUG
,
404 (stdout
, ";; Querying server (# %d) address = %s\n",
405 ns
+ 1, inet_ntoa(nsap
->sin_addr
)));
414 * Use virtual circuit;
415 * at most one attempt per server.
419 if (s
< 0 || !vc
|| hp
->opcode
== ns_o_update
) {
423 s
= socket(PF_INET
, SOCK_STREAM
, 0);
426 Perror(stderr
, "socket(vc)", errno
);
430 nsap
->sin_len
= sizeof ( *nsap
);
431 if (connect(s
, (struct sockaddr
*)nsap
,
434 Aerror(stderr
, "connect/vc",
443 * Send length & message
445 putshort((u_short
)buflen
, (u_char
*)&len
);
446 iov
[0].iov_base
= (caddr_t
)&len
;
447 iov
[0].iov_len
= INT16SZ
;
448 iov
[1].iov_base
= (caddr_t
)buf
;
449 iov
[1].iov_len
= buflen
;
450 if (writev(s
, iov
, 2) != (INT16SZ
+ buflen
)) {
452 Perror(stderr
, "write failed", errno
);
458 * Receive length & response
463 while ((n
= read(s
, (char *)cp
, (int)len
)) > 0) {
465 len
= (u_short
)( len
- n
);
471 Perror(stderr
, "read failed", errno
);
474 * A long running process might get its TCP
475 * connection reset if the remote server was
476 * restarted. Requery the server instead of
477 * trying a new one. When there is only one
478 * server, this means that a query might work
479 * instead of failing. We only allow one reset
480 * per query to prevent looping.
482 if (terrno
== ECONNRESET
&& !connreset
) {
490 resplen
= ns_get16(ans
);
491 if (resplen
> anssiz
) {
492 Dprint(_res
.options
& RES_DEBUG
,
493 (stdout
, ";; response truncated\n")
496 len
= (ushort
)anssiz
;
498 len
= (ushort
)resplen
;
499 if (len
< HFIXEDSZ
) {
501 * Undersized message.
503 Dprint(_res
.options
& RES_DEBUG
,
504 (stdout
, ";; undersized: %d\n", len
));
512 (n
= read(s
, (char *)cp
, (int)len
)) > 0) {
514 len
= (u_short
)( len
- n
);
518 Perror(stderr
, "read(vc)", errno
);
524 * Flush rest of answer
525 * so connection stays in synch.
528 len
= (ushort
)( resplen
- anssiz
);
532 n
= (len
> sizeof(junk
)
535 if ((n
= read(s
, junk
, n
)) > 0)
536 len
= (u_short
)( len
- n
);
542 * The calling applicating has bailed out of
543 * a previous call and failed to arrange to have
544 * the circuit closed or the server has got
545 * itself confused. Anyway drop the packet and
546 * wait for the correct one.
548 if (hp
->id
!= anhp
->id
) {
549 DprintQ((_res
.options
& RES_DEBUG
) ||
550 (_res
.pfcode
& RES_PRF_REPLY
),
551 (stdout
, ";; old answer (unexpected):\n"),
552 ans
, (resplen
>anssiz
)?anssiz
:resplen
);
563 struct timeval timeout
;
564 fd_set dsmask
, *dsmaskp
;
566 struct sockaddr_in from
;
572 s
= socket(PF_INET
, SOCK_DGRAM
, 0);
574 #ifndef CAN_RECONNECT
578 Perror(stderr
, "socket(dg)", errno
);
583 #ifndef CANNOT_CONNECT_DGRAM
585 * On a 4.3BSD+ machine (client and server,
586 * actually), sending to a nameserver datagram
587 * port with no nameserver will cause an
588 * ICMP port unreachable message to be returned.
589 * If our datagram socket is "connected" to the
590 * server, we get an ECONNREFUSED error on the next
591 * socket operation, and select returns if the
592 * error message is received. We can thus detect
593 * the absence of a nameserver without timing out.
594 * If we have sent queries to at least two servers,
595 * however, we don't want to remain connected,
596 * as we wish to receive answers from the first
599 if (_res
.nscount
== 1 || (try == 0 && ns
== 0)) {
601 * Connect only if we are sure we won't
602 * receive a response from another server.
605 nsap
->sin_len
= sizeof ( *nsap
);
606 if (connect(s
, (struct sockaddr
*)nsap
,
618 if (send(s
, (char*)buf
, buflen
, 0) != buflen
) {
619 Perror(stderr
, "send", errno
);
626 * Disconnect if we want to listen
627 * for responses from more than one server.
631 struct sockaddr_in no_addr
;
633 no_addr
.sin_family
= AF_INET
;
634 no_addr
.sin_addr
.s_addr
= INADDR_ANY
;
635 no_addr
.sin_port
= 0;
641 int s1
= socket(PF_INET
, SOCK_DGRAM
,0);
646 Dprint(_res
.options
& RES_DEBUG
,
647 (stdout
, ";; new DG socket\n"))
648 #endif /* CAN_RECONNECT */
652 #endif /* !CANNOT_CONNECT_DGRAM */
653 if (sendto(s
, (char*)buf
, buflen
, 0,
654 (struct sockaddr
*)nsap
,
657 Aerror(stderr
, "sendto", errno
, *nsap
);
662 #ifndef CANNOT_CONNECT_DGRAM
664 #endif /* !CANNOT_CONNECT_DGRAM */
672 msec
= (_res
.retrans
<< try) * 1000;
674 msec
/= _res
.nscount
;
679 timeout
.tv_sec
= (_res
.retrans
<< try);
681 timeout
.tv_sec
/= _res
.nscount
;
682 if ((long) timeout
.tv_sec
<= 0)
690 Perror(stderr
, "s out-of-bounds", EMFILE
);
696 struct sigaction sa
, osa
;
697 int sigsys_installed
= 0;
702 bzero(&sa
, sizeof(sa
));
703 sa
.sa_handler
= SIG_IGN
;
704 if (sigaction(SIGSYS
, &sa
, &osa
) >= 0)
705 sigsys_installed
= 1;
707 n
= poll(&pfd
, 1, msec
);
708 if (sigsys_installed
== 1) {
710 sigaction(SIGSYS
, &osa
, NULL
);
713 /* XXX why does nosys() return EINVAL? */
714 if (n
< 0 && (errno
== ENOSYS
||
718 } else if (use_poll
== 1)
723 Perror(stderr
, "poll", errno
);
729 dsmasklen
= howmany(s
+ 1, NFDBITS
) *
731 if (dsmasklen
> sizeof(fd_set
)) {
732 dsmaskp
= (fd_set
*)malloc(dsmasklen
);
733 if (dsmaskp
== NULL
) {
739 /* only zero what we need */
740 memset((char *)dsmaskp
, 0, dsmasklen
);
742 n
= select(s
+ 1, dsmaskp
, (fd_set
*)NULL
,
743 (fd_set
*)NULL
, &timeout
);
744 if (dsmaskp
!= &dsmask
)
749 Perror(stderr
, "select", errno
);
761 Dprint(_res
.options
& RES_DEBUG
,
762 (stdout
, ";; timeout\n"));
768 fromlen
= sizeof(struct sockaddr_in
);
769 resplen
= (int)recvfrom(s
, (char*)ans
, anssiz
, 0,
770 (struct sockaddr
*)&from
, &fromlen
);
772 Perror(stderr
, "recvfrom", errno
);
777 if (resplen
< HFIXEDSZ
) {
779 * Undersized message.
781 Dprint(_res
.options
& RES_DEBUG
,
782 (stdout
, ";; undersized: %d\n",
789 if (hp
->id
!= anhp
->id
) {
791 * response from old query, ignore it.
792 * XXX - potential security hazard could
795 DprintQ((_res
.options
& RES_DEBUG
) ||
796 (_res
.pfcode
& RES_PRF_REPLY
),
797 (stdout
, ";; old answer:\n"),
798 ans
, (resplen
>anssiz
)?anssiz
:resplen
);
801 #ifdef CHECK_SRVR_ADDR
802 if (!(_res
.options
& RES_INSECURE1
) &&
803 !res_isourserver(&from
)) {
805 * response from wrong server? ignore it.
806 * XXX - potential security hazard could
809 DprintQ((_res
.options
& RES_DEBUG
) ||
810 (_res
.pfcode
& RES_PRF_REPLY
),
811 (stdout
, ";; not our server:\n"),
812 ans
, (resplen
>anssiz
)?anssiz
:resplen
);
816 if (!(_res
.options
& RES_INSECURE2
) &&
817 !res_queriesmatch(buf
, buf
+ buflen
,
818 ans
, ans
+ anssiz
)) {
820 * response contains wrong query? ignore it.
821 * XXX - potential security hazard could
824 DprintQ((_res
.options
& RES_DEBUG
) ||
825 (_res
.pfcode
& RES_PRF_REPLY
),
826 (stdout
, ";; wrong query name:\n"),
827 ans
, (resplen
>anssiz
)?anssiz
:resplen
);
830 if (anhp
->rcode
== SERVFAIL
||
831 anhp
->rcode
== NOTIMP
||
832 anhp
->rcode
== REFUSED
) {
833 DprintQ(_res
.options
& RES_DEBUG
,
834 (stdout
, "server rejected query:\n"),
835 ans
, (resplen
>anssiz
)?anssiz
:resplen
);
838 /* don't retry if called from dig */
842 if (!(_res
.options
& RES_IGNTC
) && anhp
->tc
) {
844 * get rest of answer;
845 * use TCP with same server.
847 Dprint(_res
.options
& RES_DEBUG
,
848 (stdout
, ";; truncated answer\n"));
854 Dprint((_res
.options
& RES_DEBUG
) ||
855 ((_res
.pfcode
& RES_PRF_REPLY
) &&
856 (_res
.pfcode
& RES_PRF_HEAD1
)),
857 (stdout
, ";; got answer:\n"));
858 DprintQ((_res
.options
& RES_DEBUG
) ||
859 (_res
.pfcode
& RES_PRF_REPLY
),
861 ans
, (resplen
>anssiz
)?anssiz
:resplen
);
863 * If using virtual circuits, we assume that the first server
864 * is preferred over the rest (i.e. it is on the local
865 * machine) and only keep that one open.
866 * If we have temporarily opened a virtual circuit,
867 * or if we haven't been asked to keep a socket open,
870 if ((v_circuit
&& (!(_res
.options
& RES_USEVC
) || ns
!= 0)) ||
871 !(_res
.options
& RES_STAYOPEN
)) {
875 int done
= 0, loops
= 0;
880 act
= (*Rhook
)(nsap
, buf
, buflen
,
881 ans
, anssiz
, &resplen
);
891 /* give the hook another try */
892 if (++loops
< 42) /*doug adams*/
910 errno
= ECONNREFUSED
; /* no nameservers found */
912 errno
= ETIMEDOUT
; /* no answer obtained */
919 * This routine is for closing the socket if a virtual circuit is used and
920 * the program wants to close it. This provides support for endhostent()
921 * which expects to close the socket.
923 * This routine is not expected to be user visible.