2 * Copyright (c) 1985, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
33 * Permission to use, copy, modify, and distribute this software for any
34 * purpose with or without fee is hereby granted, provided that the above
35 * copyright notice and this permission notice appear in all copies, and that
36 * the name of Digital Equipment Corporation not be used in advertising or
37 * publicity pertaining to distribution of the document or software without
38 * specific, written prior permission.
40 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
41 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
42 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
43 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
44 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
45 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
46 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
51 * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
53 * Permission to use, copy, modify, and distribute this software for any
54 * purpose with or without fee is hereby granted, provided that the above
55 * copyright notice and this permission notice appear in all copies.
57 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
58 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
59 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
60 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
61 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
62 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
63 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
67 #if defined(LIBC_SCCS) && !defined(lint)
68 static const char sccsid
[] = "@(#)res_send.c 8.1 (Berkeley) 6/4/93";
69 static const char rcsid
[] = "$Id$";
70 #endif /* LIBC_SCCS and not lint */
73 * Send query to name server and wait for reply.
76 #include <sys/types.h>
77 #include <sys/param.h>
79 #include <sys/socket.h>
82 #include <netinet/in.h>
83 #include <arpa/nameser.h>
84 #include <arpa/inet.h>
97 /* Options. Leave them on. */
99 #include "res_debug.h"
102 static int pselect(int, void *, void *, void *,
107 #define CHECK_SRVR_ADDR
109 /* From bind lib/isc/ev_timers.c: */
110 #define BILLION 1000000000
111 static struct timespec
112 evTimeSpec(struct timeval tv
) {
115 ts
.tv_sec
= tv
.tv_sec
;
116 ts
.tv_nsec
= tv
.tv_usec
* 1000;
120 static struct timespec
121 evConsTime(time_t sec
, long nsec
) {
129 static struct timespec
130 evAddTime(struct timespec addend1
, struct timespec addend2
) {
133 x
.tv_sec
= addend1
.tv_sec
+ addend2
.tv_sec
;
134 x
.tv_nsec
= addend1
.tv_nsec
+ addend2
.tv_nsec
;
135 if (x
.tv_nsec
>= BILLION
) {
137 x
.tv_nsec
-= BILLION
;
142 static struct timespec
143 evSubTime(struct timespec minuend
, struct timespec subtrahend
) {
146 x
.tv_sec
= minuend
.tv_sec
- subtrahend
.tv_sec
;
147 if (minuend
.tv_nsec
>= subtrahend
.tv_nsec
)
148 x
.tv_nsec
= minuend
.tv_nsec
- subtrahend
.tv_nsec
;
150 x
.tv_nsec
= BILLION
- subtrahend
.tv_nsec
+ minuend
.tv_nsec
;
157 evCmpTime(struct timespec a
, struct timespec b
) {
158 long x
= a
.tv_sec
- b
.tv_sec
;
161 x
= a
.tv_nsec
- b
.tv_nsec
;
162 return (x
< 0L ? (-1) : x
> 0L ? (1) : (0));
165 static struct timespec
169 if (gettimeofday(&now
, NULL
) < 0)
170 return (evConsTime(0, 0));
171 return (evTimeSpec(now
));
173 /* End of code from bind lib/isc/ev_timers.c. */
177 Aerror(const res_state statp
, FILE *file
, const char *string
, int error
,
178 struct sockaddr_in address
)
182 if ((statp
->options
& RES_DEBUG
) != 0) {
183 char tmp
[sizeof "255.255.255.255"];
185 fprintf(file
, "res_send: %s ([%s].%u): %s\n",
187 inet_ntop(address
.sin_family
, &address
.sin_addr
,
189 ntohs(address
.sin_port
),
195 Perror(const res_state statp
, FILE *file
, const char *string
, int error
) {
198 if ((statp
->options
& RES_DEBUG
) != 0)
199 fprintf(file
, "res_send: %s: %s\n",
200 string
, strerror(error
));
205 static int cmpsock(struct sockaddr_in
*a1
, struct sockaddr_in
*a2
);
206 void res_pquery(const res_state
, const u_char
*, int, FILE *);
209 * res_isourserver(ina)
210 * looks up "ina" in _res.ns_addr_list[]
215 * paul vixie, 29may94
218 res_ourserver_p(const res_state statp
, const struct sockaddr_in
*inp
) {
219 struct sockaddr_in ina
;
223 for (ns
= 0; ns
< statp
->nscount
; ns
++) {
224 const struct sockaddr_in
*srv
= &statp
->nsaddr_list
[ns
];
226 if (srv
->sin_family
== ina
.sin_family
&&
227 srv
->sin_port
== ina
.sin_port
&&
228 (srv
->sin_addr
.s_addr
== INADDR_ANY
||
229 srv
->sin_addr
.s_addr
== ina
.sin_addr
.s_addr
))
236 * res_nameinquery(name, type, class, buf, eom)
237 * look for (name,type,class) in the query section of packet (buf,eom)
239 * buf + HFIXEDSZ <= eom
245 * paul vixie, 29may94
248 res_nameinquery(const char *name
, int type
, int class,
249 const u_char
*buf
, const u_char
*eom
)
251 const u_char
*cp
= buf
+ HFIXEDSZ
;
252 int qdcount
= ntohs(((HEADER
*)buf
)->qdcount
);
254 while (qdcount
-- > 0) {
255 char tname
[MAXDNAME
+1];
256 int n
, ttype
, tclass
;
258 n
= dn_expand(buf
, eom
, cp
, tname
, sizeof tname
);
262 if (cp
+ 2 * INT16SZ
> eom
)
264 ttype
= ns_get16(cp
); cp
+= INT16SZ
;
265 tclass
= ns_get16(cp
); cp
+= INT16SZ
;
266 if (ttype
== type
&& tclass
== class &&
267 ns_samename(tname
, name
) == 1)
274 * res_queriesmatch(buf1, eom1, buf2, eom2)
275 * is there a 1:1 mapping of (name,type,class)
276 * in (buf1,eom1) and (buf2,eom2)?
279 * 0 : not a 1:1 mapping
280 * >0 : is a 1:1 mapping
282 * paul vixie, 29may94
285 res_queriesmatch(const u_char
*buf1
, const u_char
*eom1
,
286 const u_char
*buf2
, const u_char
*eom2
)
288 const u_char
*cp
= buf1
+ HFIXEDSZ
;
289 int qdcount
= ntohs(((HEADER
*)buf1
)->qdcount
);
291 if (buf1
+ HFIXEDSZ
> eom1
|| buf2
+ HFIXEDSZ
> eom2
)
295 * Only header section present in replies to
296 * dynamic update packets.
298 if ( (((HEADER
*)buf1
)->opcode
== ns_o_update
) &&
299 (((HEADER
*)buf2
)->opcode
== ns_o_update
) )
302 if (qdcount
!= ntohs(((HEADER
*)buf2
)->qdcount
))
304 while (qdcount
-- > 0) {
305 char tname
[MAXDNAME
+1];
306 int n
, ttype
, tclass
;
308 n
= dn_expand(buf1
, eom1
, cp
, tname
, sizeof tname
);
312 if (cp
+ 2 * INT16SZ
> eom1
)
314 ttype
= ns_get16(cp
); cp
+= INT16SZ
;
315 tclass
= ns_get16(cp
); cp
+= INT16SZ
;
316 if (!res_nameinquery(tname
, ttype
, tclass
, buf2
, eom2
))
323 res_nsend(res_state statp
,
324 const u_char
*buf
, int buflen
, u_char
*ans
, int anssiz
)
326 HEADER
*hp
= (HEADER
*) buf
;
327 HEADER
*anhp
= (HEADER
*) ans
;
328 int gotsomewhere
, connreset
, terrno
, try, v_circuit
, resplen
, ns
, n
;
329 u_int badns
; /* XXX NSMAX can't exceed #/bits in this variable */
330 static int highestFD
= FD_SETSIZE
- 1;
332 if (anssiz
< HFIXEDSZ
) {
333 __set_errno (EINVAL
);
336 DprintQ((statp
->options
& RES_DEBUG
) || (statp
->pfcode
& RES_PRF_QUERY
),
337 (stdout
, ";; res_send()\n"), buf
, buflen
);
338 v_circuit
= (statp
->options
& RES_USEVC
) || buflen
> PACKETSZ
;
345 * Some callers want to even out the load on their resolver list.
347 if (statp
->nscount
> 0 && (statp
->options
& RES_ROTATE
) != 0) {
348 struct sockaddr_in ina
;
349 int lastns
= statp
->nscount
- 1;
351 ina
= statp
->nsaddr_list
[0];
352 for (ns
= 0; ns
< lastns
; ns
++)
353 statp
->nsaddr_list
[ns
] = statp
->nsaddr_list
[ns
+ 1];
354 statp
->nsaddr_list
[lastns
] = ina
;
358 * Send request, RETRY times, or until successful
360 for (try = 0; try < statp
->retry
; try++) {
361 for (ns
= 0; ns
< statp
->nscount
; ns
++) {
362 struct sockaddr_in
*nsap
= &statp
->nsaddr_list
[ns
];
364 if (badns
& (1 << ns
)) {
370 int done
= 0, loops
= 0;
375 act
= (*statp
->qhook
)(&nsap
, &buf
, &buflen
,
376 ans
, anssiz
, &resplen
);
387 /* give the hook another try */
388 if (++loops
< 42) /*doug adams*/
399 Dprint(statp
->options
& RES_DEBUG
,
400 (stdout
, ";; Querying server (# %d) address = %s\n",
401 ns
+ 1, inet_ntoa(nsap
->sin_addr
)));
409 /* Use VC; at most one attempt per server. */
413 /* Are we still talking to whom we want to talk to? */
414 if (statp
->_sock
>= 0 &&
415 (statp
->_flags
& RES_F_VC
) != 0) {
416 struct sockaddr_in peer
;
417 int size
= sizeof(peer
);
419 if (getpeername(statp
->_sock
,
420 (struct sockaddr
*)&peer
,
423 statp
->_flags
&= ~RES_F_VC
;
424 } else if (!cmpsock(&peer
, nsap
)) {
426 statp
->_flags
&= ~RES_F_VC
;
430 if (statp
->_sock
< 0 ||
431 (statp
->_flags
& RES_F_VC
) == 0) {
432 if (statp
->_sock
>= 0)
435 statp
->_sock
= socket(PF_INET
,
437 if (statp
->_sock
< 0 ||
438 statp
->_sock
> highestFD
) {
440 Perror(statp
, stderr
,
441 "socket(vc)", errno
);
445 if (connect(statp
->_sock
,
446 (struct sockaddr
*)nsap
,
449 Aerror(statp
, stderr
, "connect/vc",
455 statp
->_flags
|= RES_F_VC
;
458 * Send length & message
460 putshort((u_short
)buflen
, (u_char
*)&len
);
461 iov
[0].iov_base
= (caddr_t
)&len
;
462 iov
[0].iov_len
= INT16SZ
;
463 iov
[1].iov_base
= (caddr_t
)buf
;
464 iov
[1].iov_len
= buflen
;
465 if (writev(statp
->_sock
, iov
, 2) !=
466 (INT16SZ
+ buflen
)) {
468 Perror(statp
, stderr
, "write failed", errno
);
474 * Receive length & response
479 while ((n
= read(statp
->_sock
,
480 (char *)cp
, (int)len
)) > 0) {
487 Perror(statp
, stderr
, "read failed", errno
);
490 * A long running process might get its TCP
491 * connection reset if the remote server was
492 * restarted. Requery the server instead of
493 * trying a new one. When there is only one
494 * server, this means that a query might work
495 * instead of failing. We only allow one reset
496 * per query to prevent looping.
498 if (terrno
== ECONNRESET
&& !connreset
) {
506 resplen
= ns_get16(ans
);
507 if (resplen
> anssiz
) {
508 Dprint(statp
->options
& RES_DEBUG
,
509 (stdout
, ";; response truncated\n")
515 if (len
< HFIXEDSZ
) {
517 * Undersized message.
519 Dprint(statp
->options
& RES_DEBUG
,
520 (stdout
, ";; undersized: %d\n", len
));
528 (n
= read(statp
->_sock
, (char *)cp
, (int)len
))
535 Perror(statp
, stderr
, "read(vc)", errno
);
541 * Flush rest of answer
542 * so connection stays in synch.
545 len
= resplen
- anssiz
;
549 n
= ((size_t)len
> sizeof(junk
)
552 n
= read(statp
->_sock
, junk
, n
);
560 * The calling applicating has bailed out of
561 * a previous call and failed to arrange to have
562 * the circuit closed or the server has got
563 * itself confused. Anyway drop the packet and
564 * wait for the correct one.
566 if (hp
->id
!= anhp
->id
) {
567 DprintQ((statp
->options
& RES_DEBUG
) ||
568 (statp
->pfcode
& RES_PRF_REPLY
),
569 (stdout
, ";; old answer (unexpected):\n"),
570 ans
, (resplen
>anssiz
)?anssiz
:resplen
);
577 struct timespec start
, timeout
, finish
;
579 struct pollfd pfd
[1];
584 struct sockaddr_in from
;
585 int fromlen
, seconds
;
587 if (statp
->_sock
< 0 ||
588 (statp
->_flags
& RES_F_VC
) != 0) {
589 if ((statp
->_flags
& RES_F_VC
) != 0)
591 statp
->_sock
= socket(PF_INET
, SOCK_DGRAM
, 0);
592 if (statp
->_sock
< 0 ||
593 statp
->_sock
> highestFD
) {
594 #ifndef CAN_RECONNECT
598 Perror(statp
, stderr
,
599 "socket(dg)", errno
);
602 statp
->_flags
&= ~RES_F_CONN
;
604 #ifndef CANNOT_CONNECT_DGRAM
606 * On a 4.3BSD+ machine (client and server,
607 * actually), sending to a nameserver datagram
608 * port with no nameserver will cause an
609 * ICMP port unreachable message to be returned.
610 * If our datagram socket is "connected" to the
611 * server, we get an ECONNREFUSED error on the next
612 * socket operation, and select returns if the
613 * error message is received. We can thus detect
614 * the absence of a nameserver without timing out.
615 * If we have sent queries to at least two servers,
616 * however, we don't want to remain connected,
617 * as we wish to receive answers from the first
620 if (statp
->nscount
== 1 || (try == 0 && ns
== 0)) {
622 * Connect only if we are sure we won't
623 * receive a response from another server.
625 if ((statp
->_flags
& RES_F_CONN
) == 0) {
626 if (connect(statp
->_sock
,
627 (struct sockaddr
*)nsap
,
629 Aerror(statp
, stderr
,
636 statp
->_flags
|= RES_F_CONN
;
638 if (send(statp
->_sock
, (char*)buf
, buflen
, 0)
640 Perror(statp
, stderr
, "send", errno
);
647 * Disconnect if we want to listen
648 * for responses from more than one server.
650 if ((statp
->_flags
& RES_F_CONN
) != 0) {
652 struct sockaddr_in no_addr
;
654 no_addr
.sin_family
= AF_INET
;
655 no_addr
.sin_addr
.s_addr
= INADDR_ANY
;
656 no_addr
.sin_port
= 0;
657 (void) connect(statp
->_sock
,
662 struct sockaddr_in local_addr
;
665 len
= sizeof(local_addr
);
666 s1
= socket(PF_INET
, SOCK_DGRAM
, 0);
667 result
= getsockname(statp
->_sock
,
668 (struct sockaddr
*)&local_addr
,
672 (void) dup2(s1
, statp
->_sock
);
676 * Attempt to rebind to old
677 * port. Note connected socket
678 * has an sin_addr set.
680 local_addr
.sin_addr
.s_addr
=
682 (void)bind(statp
->_sock
,
686 Dprint(statp
->options
& RES_DEBUG
,
687 (stdout
, ";; new DG socket\n"))
688 #endif /* CAN_RECONNECT */
689 statp
->_flags
&= ~RES_F_CONN
;
692 #endif /* !CANNOT_CONNECT_DGRAM */
693 if (sendto(statp
->_sock
,
694 (char*)buf
, buflen
, 0,
695 (struct sockaddr
*)nsap
,
698 Aerror(statp
, stderr
, "sendto", errno
, *nsap
);
703 #ifndef CANNOT_CONNECT_DGRAM
705 #endif /* !CANNOT_CONNECT_DGRAM */
707 if (statp
->_sock
< 0 || statp
->_sock
> highestFD
) {
708 Perror(statp
, stderr
,
709 "fd out-of-bounds", EMFILE
);
717 seconds
= (statp
->retrans
<< try);
719 seconds
/= statp
->nscount
;
724 timeout
= evConsTime(seconds
, 0);
725 finish
= evAddTime(start
, timeout
);
728 /* Convert struct timespec in milliseconds. */
729 ptimeout
= timeout
.tv_sec
* 1000
730 + timeout
.tv_nsec
/ 1000000;
732 pfd
[0].fd
= statp
->_sock
;
733 pfd
[0].events
= POLLIN
;
734 n
= __poll (pfd
, 1, ptimeout
);
737 FD_SET(statp
->_sock
, &dsmask
);
738 n
= pselect(statp
->_sock
+ 1,
743 Dprint(statp
->options
& RES_DEBUG
,
744 (stdout
, ";; timeout\n"));
749 if (errno
== EINTR
) {
753 if (evCmpTime(finish
, now
) >= 0) {
754 timeout
= evSubTime(finish
,
760 Perror(statp
, stderr
, "poll", errno
);
762 Perror(statp
, stderr
, "select", errno
);
768 fromlen
= sizeof(struct sockaddr_in
);
769 resplen
= recvfrom(statp
->_sock
, (char*)ans
, anssiz
,0,
770 (struct sockaddr
*)&from
, &fromlen
);
772 Perror(statp
, stderr
, "recvfrom", errno
);
777 if (resplen
< HFIXEDSZ
) {
779 * Undersized message.
781 Dprint(statp
->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((statp
->options
& RES_DEBUG
) ||
796 (statp
->pfcode
& RES_PRF_REPLY
),
797 (stdout
, ";; old answer:\n"),
798 ans
, (resplen
>anssiz
)?anssiz
:resplen
);
801 #ifdef CHECK_SRVR_ADDR
802 if (!(statp
->options
& RES_INSECURE1
) &&
803 !res_ourserver_p(statp
, &from
)) {
805 * response from wrong server? ignore it.
806 * XXX - potential security hazard could
809 DprintQ((statp
->options
& RES_DEBUG
) ||
810 (statp
->pfcode
& RES_PRF_REPLY
),
811 (stdout
, ";; not our server:\n"),
812 ans
, (resplen
>anssiz
)?anssiz
:resplen
);
816 if (!(statp
->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((statp
->options
& RES_DEBUG
) ||
825 (statp
->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(statp
->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 (!(statp
->options
& RES_IGNTC
) && anhp
->tc
) {
844 * get rest of answer;
845 * use TCP with same server.
847 Dprint(statp
->options
& RES_DEBUG
,
848 (stdout
, ";; truncated answer\n"));
854 Dprint((statp
->options
& RES_DEBUG
) ||
855 ((statp
->pfcode
& RES_PRF_REPLY
) &&
856 (statp
->pfcode
& RES_PRF_HEAD1
)),
857 (stdout
, ";; got answer:\n"));
858 DprintQ((statp
->options
& RES_DEBUG
) ||
859 (statp
->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
&& (!(statp
->options
& RES_USEVC
) || ns
!= 0)) ||
871 !(statp
->options
& RES_STAYOPEN
)) {
875 int done
= 0, loops
= 0;
880 act
= (*statp
->rhook
)(nsap
, buf
, buflen
,
881 ans
, anssiz
, &resplen
);
891 /* give the hook another try */
892 if (++loops
< 42) /*doug adams*/
910 __set_errno (ECONNREFUSED
); /* no nameservers found */
912 __set_errno (ETIMEDOUT
); /* no answer obtained */
914 __set_errno (terrno
);
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.
926 res_nclose(res_state statp
) {
927 if (statp
->_sock
>= 0) {
928 (void) close(statp
->_sock
);
930 statp
->_flags
&= ~(RES_F_VC
| RES_F_CONN
);
936 cmpsock(struct sockaddr_in
*a1
, struct sockaddr_in
*a2
) {
937 return ((a1
->sin_family
== a2
->sin_family
) &&
938 (a1
->sin_port
== a2
->sin_port
) &&
939 (a1
->sin_addr
.s_addr
== a2
->sin_addr
.s_addr
));
943 /* XXX needs to move to the porting library. */
945 pselect(int nfds
, void *rfds
, void *wfds
, void *efds
,
946 struct timespec
*tsp
,
947 const sigset_t
*sigmask
)
949 struct timeval tv
, *tvp
;
955 tv
= evTimeVal(*tsp
);
959 sigprocmask(SIG_SETMASK
, sigmask
, &sigs
);
960 n
= select(nfds
, rfds
, wfds
, efds
, tvp
);
962 sigprocmask(SIG_SETMASK
, &sigs
, NULL
);
964 *tsp
= evTimeSpec(tv
);