revert between 56095 -> 55830 in arch
[AROS.git] / workbench / network / stacks / AROSTCP / dhcp / minires / res_send.c
blob2363214f77ec17826173f5660a4d3b88e557bae8
1 /*
2 * Copyright (c) 1985, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
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
51 * SOFTWARE.
55 * Portions Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
56 * Portions Copyright (c) 1996-2003 by Internet Software Consortium
58 * Permission to use, copy, modify, and distribute this software for any
59 * purpose with or without fee is hereby granted, provided that the above
60 * copyright notice and this permission notice appear in all copies.
62 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
63 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
64 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
65 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
66 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
67 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
68 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
70 * Internet Systems Consortium, Inc.
71 * 950 Charter Street
72 * Redwood City, CA 94063
73 * <info@isc.org>
74 * http://www.isc.org/
77 #if defined(LIBC_SCCS) && !defined(lint) && !defined(__AROS__)
78 static const char sccsid[] = "@(#)res_send.c 8.1 (Berkeley) 6/4/93";
79 static const char rcsid[] = "$Id$";
80 #endif /* LIBC_SCCS and not lint */
83 * Send query to name server and wait for reply.
86 #include <sys/types.h>
87 #include <sys/param.h>
88 #include <sys/time.h>
89 #include <sys/socket.h>
90 #include <sys/uio.h>
92 #include <netinet/in.h>
93 #include <arpa/inet.h>
95 #include <errno.h>
96 #include <netdb.h>
97 #include <signal.h>
98 #include <stdio.h>
99 #include <stdlib.h>
100 #include <string.h>
101 #include <unistd.h>
103 #include "minires/minires.h"
104 #include "arpa/nameser.h"
106 #ifdef __AROS__
107 #include <aros/debug.h>
108 #endif
110 /* Prototypes */
111 int trace_mr_socket (int, int, int);
112 int trace_mr_connect (int s, struct sockaddr *, SOCKLEN_T);
113 ssize_t trace_mr_read (int, void *, size_t);
114 ssize_t trace_mr_send (int, const void *, size_t, int);
115 int trace_mr_close (int);
116 int trace_mr_bind (int, struct sockaddr *, SOCKLEN_T);
117 int trace_mr_select (int, fd_set *, fd_set *, fd_set *, struct timeval *);
118 ssize_t trace_mr_recvfrom (int s, void *, size_t, int,
119 struct sockaddr *, SOCKLEN_T *);
121 /* Rename the I/O functions in case we're tracing. */
122 #undef send
123 #undef recvfrom
124 #undef read
125 #undef connect
126 #undef socket
127 #undef bind
128 #undef close
129 #undef select
130 #undef time
131 #define send trace_mr_send
132 #define recvfrom trace_mr_recvfrom
133 #define read trace_mr_read
134 #define connect trace_mr_connect
135 #define socket trace_mr_socket
136 #define bind trace_mr_bind
137 #define close trace_mr_close
138 #define select trace_mr_select
139 #define time trace_mr_time
141 #define CHECK_SRVR_ADDR
143 static int cmpsock(struct sockaddr_in *a1, struct sockaddr_in *a2);
144 void res_pquery(const res_state, const u_char *, int, FILE *);
146 /* int
147 * res_isourserver(ina)
148 * looks up "ina" in _res.ns_addr_list[]
149 * returns:
150 * 0 : not found
151 * >0 : found
152 * author:
153 * paul vixie, 29may94
156 res_ourserver_p(const res_state statp, const struct sockaddr_in *inp) {
157 struct sockaddr_in ina;
158 int ns;
160 ina = *inp;
161 for (ns = 0; ns < statp->nscount; ns++) {
162 const struct sockaddr_in *srv = &statp->nsaddr_list[ns];
164 if (srv->sin_family == ina.sin_family &&
165 srv->sin_port == ina.sin_port &&
166 (srv->sin_addr.s_addr == INADDR_ANY ||
167 srv->sin_addr.s_addr == ina.sin_addr.s_addr))
168 return (1);
170 return (0);
173 /* int
174 * res_nameinquery(name, type, class, buf, eom)
175 * look for (name,type,class) in the query section of packet (buf,eom)
176 * requires:
177 * buf + HFIXEDSZ <= eom
178 * returns:
179 * -1 : format error
180 * 0 : not found
181 * >0 : found
182 * author:
183 * paul vixie, 29may94
186 res_nameinquery(const char *name, int type, int class,
187 const u_char *buf, const u_char *eom)
189 const u_char *cp = buf + HFIXEDSZ;
190 int qdcount = ntohs(((const HEADER *)buf)->qdcount);
192 while (qdcount-- > 0) {
193 char tname[MAXDNAME+1];
194 int n, ttype, tclass;
196 n = dn_expand(buf, eom, cp, tname, sizeof tname);
197 if (n < 0)
198 return (-1);
199 cp += n;
200 if (cp + 2 * INT16SZ > eom)
201 return (-1);
202 ttype = getUShort(cp); cp += INT16SZ;
203 tclass = getUShort(cp); cp += INT16SZ;
204 if (ttype == type && tclass == class &&
205 ns_samename(tname, name) == 1)
206 return (1);
208 return (0);
211 /* int
212 * res_queriesmatch(buf1, eom1, buf2, eom2)
213 * is there a 1:1 mapping of (name,type,class)
214 * in (buf1,eom1) and (buf2,eom2)?
215 * returns:
216 * -1 : format error
217 * 0 : not a 1:1 mapping
218 * >0 : is a 1:1 mapping
219 * author:
220 * paul vixie, 29may94
223 res_queriesmatch(const u_char *buf1, const u_char *eom1,
224 const u_char *buf2, const u_char *eom2)
226 const u_char *cp = buf1 + HFIXEDSZ;
227 int qdcount = ntohs(((const HEADER *)buf1)->qdcount);
229 if (buf1 + HFIXEDSZ > eom1 || buf2 + HFIXEDSZ > eom2)
230 return (-1);
233 * Only header section present in replies to
234 * dynamic update packets.
236 if ( (((const HEADER *)buf1)->opcode == ns_o_update) &&
237 (((const HEADER *)buf2)->opcode == ns_o_update) )
238 return (1);
240 if (qdcount != ntohs(((const HEADER*)buf2)->qdcount))
241 return (0);
242 while (qdcount-- > 0) {
243 char tname[MAXDNAME+1];
244 int n, ttype, tclass;
246 n = dn_expand(buf1, eom1, cp, tname, sizeof tname);
247 if (n < 0)
248 return (-1);
249 cp += n;
250 if (cp + 2 * INT16SZ > eom1)
251 return (-1);
252 ttype = getUShort(cp); cp += INT16SZ;
253 tclass = getUShort(cp); cp += INT16SZ;
254 if (!res_nameinquery(tname, ttype, tclass, buf2, eom2))
255 return (0);
257 return (1);
260 isc_result_t
261 res_nsend(res_state statp,
262 double *buf, unsigned buflen,
263 double *ans, unsigned anssiz, unsigned *ansret)
265 HEADER *hp = (HEADER *) buf;
266 HEADER *anhp = (HEADER *) ans;
267 int gotsomewhere, connreset, terrno, try, v_circuit, resplen, ns, n;
268 u_int badns; /* XXX NSMAX can't exceed #/bits in this variable */
269 static int highestFD = FD_SETSIZE - 1;
271 if (anssiz < HFIXEDSZ) {
272 return ISC_R_INVALIDARG;
274 DprintQ((statp->options & RES_DEBUG) ||
275 (statp->pfcode & RES_PRF_QUERY),
276 (stdout, ";; res_send()\n"), buf, buflen);
277 v_circuit = (statp->options & RES_USEVC) || buflen > PACKETSZ;
278 gotsomewhere = 0;
279 connreset = 0;
280 terrno = ISC_R_TIMEDOUT;
281 badns = 0;
284 * Some callers want to even out the load on their resolver list.
286 if (statp->nscount > 0 && (statp->options & RES_ROTATE) != 0) {
287 struct sockaddr_in ina;
288 int lastns = statp->nscount - 1;
290 ina = statp->nsaddr_list[0];
291 for (ns = 0; ns < lastns; ns++)
292 statp->nsaddr_list[ns] = statp->nsaddr_list[ns + 1];
293 statp->nsaddr_list[lastns] = ina;
296 #if defined (TRACING)
297 trace_mr_statp_setup (statp);
298 #endif
301 * Send request, RETRY times, or until successful
303 for (try = 0; try < statp->retry; try++) {
304 for (ns = 0; ns < statp->nscount; ns++) {
305 struct sockaddr_in *nsap = &statp->nsaddr_list[ns];
306 same_ns:
307 if (badns & (1 << ns)) {
308 res_nclose(statp);
309 goto next_ns;
312 if (statp->qhook) {
313 int done = 0, loops = 0;
315 do {
316 res_sendhookact act;
318 act = (*statp->qhook)(&nsap, &buf, &buflen,
319 ans, anssiz, &resplen);
320 switch (act) {
321 case res_goahead:
322 done = 1;
323 break;
324 case res_nextns:
325 res_nclose(statp);
326 goto next_ns;
327 case res_done:
328 return (resplen);
329 case res_modified:
330 /* give the hook another try */
331 if (++loops < 42) /*doug adams*/
332 break;
333 /*FALLTHROUGH*/
334 case res_error:
335 /*FALLTHROUGH*/
336 default:
337 return ISC_R_UNEXPECTED;
339 } while (!done);
342 Dprint(statp->options & RES_DEBUG,
343 (stdout, ";; Querying server (# %d) address = %s\n",
344 ns + 1, inet_ntoa(nsap->sin_addr)));
346 if (v_circuit) {
347 int truncated;
348 struct iovec iov[2];
349 u_short len;
350 u_char *cp;
352 /* Use VC; at most one attempt per server. */
353 try = statp->retry;
354 truncated = 0;
356 /* Are we still talking to whom we want to talk to? */
357 if (statp->_sock >= 0 &&
358 (statp->_flags & RES_F_VC) != 0) {
359 struct sockaddr_in peer;
360 SOCKLEN_T size = sizeof(peer);
362 if (getpeername(statp->_sock,
363 (struct sockaddr *)&peer,
364 &size) < 0) {
365 res_nclose(statp);
366 statp->_flags &= ~RES_F_VC;
367 } else if (!cmpsock(&peer, nsap)) {
368 res_nclose(statp);
369 statp->_flags &= ~RES_F_VC;
373 if (statp->_sock < 0 ||
374 (statp->_flags & RES_F_VC) == 0) {
375 if (statp->_sock >= 0)
376 res_nclose(statp);
378 statp->_sock = socket(PF_INET,
379 SOCK_STREAM, 0);
380 if (statp->_sock < 0 ||
381 statp->_sock > highestFD) {
382 terrno = uerr2isc (errno);
383 Perror(statp, stderr,
384 "socket(vc)", errno);
385 return (-1);
387 errno = 0;
388 if (connect(statp->_sock,
389 (struct sockaddr *)nsap,
390 sizeof *nsap) < 0) {
391 terrno = uerr2isc (errno);
392 Aerror(statp, stderr, "connect/vc",
393 errno, *nsap);
394 badns |= (1 << ns);
395 res_nclose(statp);
396 goto next_ns;
398 statp->_flags |= RES_F_VC;
401 * Send length & message
403 putUShort((u_char*)&len, buflen);
404 iov[0].iov_base = (caddr_t)&len;
405 iov[0].iov_len = INT16SZ;
406 iov[1].iov_base = (const caddr_t)buf;
407 iov[1].iov_len = buflen;
408 #ifdef __AROS__
409 bug("DONT HAVE writev\n");
410 (void)iov;
411 #else
412 if (writev(statp->_sock, iov, 2) !=
413 (INT16SZ + buflen)) {
414 terrno = uerr2isc (errno);
415 Perror(statp, stderr, "write failed", errno);
416 badns |= (1 << ns);
417 res_nclose(statp);
418 goto next_ns;
420 #endif
422 * Receive length & response
424 read_len:
425 cp = (u_char *)ans;
426 len = INT16SZ;
427 while ((n = read(statp->_sock,
428 (char *)cp, (unsigned)len)) > 0) {
429 cp += n;
430 if ((len -= n) <= 0)
431 break;
433 if (n <= 0) {
434 terrno = uerr2isc (errno);
435 Perror(statp, stderr, "read failed", errno);
436 res_nclose(statp);
438 * A long running process might get its TCP
439 * connection reset if the remote server was
440 * restarted. Requery the server instead of
441 * trying a new one. When there is only one
442 * server, this means that a query might work
443 * instead of failing. We only allow one reset
444 * per query to prevent looping.
446 if (terrno == ISC_R_CONNREFUSED &&
447 !connreset) {
448 connreset = 1;
449 res_nclose(statp);
450 goto same_ns;
452 res_nclose(statp);
453 goto next_ns;
455 resplen = getUShort ((unsigned char *)ans);
456 if (resplen > anssiz) {
457 Dprint(statp->options & RES_DEBUG,
458 (stdout, ";; response truncated\n")
460 truncated = 1;
461 len = anssiz;
462 } else
463 len = resplen;
464 if (len < HFIXEDSZ) {
466 * Undersized message.
468 Dprint(statp->options & RES_DEBUG,
469 (stdout, ";; undersized: %d\n", len));
470 terrno = ISC_R_NOSPACE;
471 badns |= (1 << ns);
472 res_nclose(statp);
473 goto next_ns;
475 cp = (u_char *)ans;
476 while (len != 0 &&
477 (n = read(statp->_sock,
478 (char *)cp, (unsigned)len))
479 > 0) {
480 cp += n;
481 len -= n;
483 if (n <= 0) {
484 terrno = uerr2isc (errno);
485 Perror(statp, stderr, "read(vc)", errno);
486 res_nclose(statp);
487 goto next_ns;
489 if (truncated) {
491 * Flush rest of answer
492 * so connection stays in synch.
494 anhp->tc = 1;
495 len = resplen - anssiz;
496 while (len != 0) {
497 char junk[PACKETSZ];
499 n = (len > sizeof(junk)
500 ? sizeof(junk)
501 : len);
502 n = read(statp->_sock,
503 junk, (unsigned)n);
504 if (n > 0)
505 len -= n;
506 else
507 break;
511 * The calling applicating has bailed out of
512 * a previous call and failed to arrange to have
513 * the circuit closed or the server has got
514 * itself confused. Anyway drop the packet and
515 * wait for the correct one.
517 if (hp->id != anhp->id) {
518 DprintQ((statp->options & RES_DEBUG) ||
519 (statp->pfcode & RES_PRF_REPLY),
520 (stdout,
521 ";; old answer (unexpected):\n"),
522 ans, (resplen>anssiz)?anssiz:resplen);
523 goto read_len;
525 } else {
527 * Use datagrams.
529 int start, timeout, finish;
530 fd_set dsmask;
531 struct sockaddr_in from;
532 SOCKLEN_T fromlen;
533 int seconds;
535 if (statp->_sock < 0 ||
536 (statp->_flags & RES_F_VC) != 0) {
537 if ((statp->_flags & RES_F_VC) != 0)
538 res_nclose(statp);
539 statp->_sock = socket(PF_INET, SOCK_DGRAM, 0);
540 if (statp->_sock < 0 ||
541 statp->_sock > highestFD) {
542 #ifndef CAN_RECONNECT
543 bad_dg_sock:
544 #endif
545 terrno = uerr2isc (errno);
546 Perror(statp, stderr,
547 "socket(dg)", errno);
548 return terrno;
550 statp->_flags &= ~RES_F_CONN;
552 #ifndef CANNOT_CONNECT_DGRAM
554 * On a 4.3BSD+ machine (client and server,
555 * actually), sending to a nameserver datagram
556 * port with no nameserver will cause an
557 * ICMP port unreachable message to be returned.
558 * If our datagram socket is "connected" to the
559 * server, we get an ECONNREFUSED error on the next
560 * socket operation, and select returns if the
561 * error message is received. We can thus detect
562 * the absence of a nameserver without timing out.
563 * If we have sent queries to at least two servers,
564 * however, we don't want to remain connected,
565 * as we wish to receive answers from the first
566 * server to respond.
568 if (statp->nscount == 1 || (try == 0 && ns == 0)) {
570 * Connect only if we are sure we won't
571 * receive a response from another server.
573 if ((statp->_flags & RES_F_CONN) == 0) {
574 if (connect(statp->_sock,
575 (struct sockaddr *)nsap,
576 sizeof *nsap) < 0) {
577 Aerror(statp, stderr,
578 "connect(dg)",
579 errno, *nsap);
580 badns |= (1 << ns);
581 res_nclose(statp);
582 goto next_ns;
584 statp->_flags |= RES_F_CONN;
586 if (send(statp->_sock,
587 buf, (unsigned)buflen, 0)
588 != buflen) {
589 Perror(statp, stderr, "send", errno);
590 badns |= (1 << ns);
591 res_nclose(statp);
592 goto next_ns;
594 } else {
596 * Disconnect if we want to listen
597 * for responses from more than one server.
599 if ((statp->_flags & RES_F_CONN) != 0) {
600 #ifdef CAN_RECONNECT
601 struct sockaddr_in no_addr;
603 no_addr.sin_family = AF_INET;
604 no_addr.sin_addr.s_addr = INADDR_ANY;
605 no_addr.sin_port = 0;
606 (void) connect(statp->_sock,
607 (struct sockaddr *)
608 &no_addr,
609 sizeof no_addr);
610 #else
611 struct sockaddr_in local_addr;
612 SOCKLEN_T len;
613 int result, s1;
615 len = sizeof(local_addr);
616 s1 = socket(PF_INET, SOCK_DGRAM, 0);
617 result = getsockname(statp->_sock,
618 (struct sockaddr *)&local_addr,
619 &len);
620 if (s1 < 0)
621 goto bad_dg_sock;
622 #ifdef SOCKET_IS_NOT_A_FILE
623 Dup2Socket(s1, statp->_sock);
624 #else
625 (void) dup2(s1, statp->_sock);
626 #endif
627 (void) close(s1);
628 if (result == 0) {
630 * Attempt to rebind to old
631 * port. Note connected socket
632 * has an sin_addr set.
634 local_addr.sin_addr.s_addr =
635 htonl(0);
636 (void)bind(statp->_sock,
637 (struct sockaddr *)
638 &local_addr,
639 (unsigned)len);
641 Dprint(statp->options & RES_DEBUG,
642 (stdout, ";; new DG socket\n"));
643 #endif /* CAN_RECONNECT */
644 statp->_flags &= ~RES_F_CONN;
645 errno = 0;
647 #endif /* !CANNOT_CONNECT_DGRAM */
648 if (sendto(statp->_sock,
649 (const char *)buf, buflen, 0,
650 (struct sockaddr *)nsap,
651 sizeof *nsap)
652 != buflen) {
653 Aerror(statp, stderr, "sendto", errno, *nsap);
654 badns |= (1 << ns);
655 res_nclose(statp);
656 goto next_ns;
658 #ifndef CANNOT_CONNECT_DGRAM
660 #endif /* !CANNOT_CONNECT_DGRAM */
662 if (statp->_sock < 0 || statp->_sock > highestFD) {
663 Perror(statp, stderr,
664 "fd out-of-bounds", EMFILE);
665 res_nclose(statp);
666 goto next_ns;
670 * Wait for reply
672 seconds = (statp->retrans << try);
673 if (try > 0)
674 seconds /= statp->nscount;
675 if (seconds <= 0)
676 seconds = 1;
677 start = cur_time;
678 timeout = seconds;
679 finish = start + timeout;
680 wait:
681 FD_ZERO(&dsmask);
682 FD_SET(statp->_sock, &dsmask);
684 struct timeval t;
685 t.tv_sec = timeout;
686 t.tv_usec = 0;
687 n = select(statp->_sock + 1,
688 &dsmask, NULL, NULL, &t);
690 if (n == 0) {
691 Dprint(statp->options & RES_DEBUG,
692 (stdout, ";; timeout\n"));
693 gotsomewhere = 1;
694 goto next_ns;
696 if (n < 0) {
697 if (errno == EINTR) {
698 if (finish >= cur_time) {
699 timeout = finish - cur_time;
700 goto wait;
703 Perror(statp, stderr, "select", errno);
704 res_nclose(statp);
705 goto next_ns;
707 errno = 0;
708 fromlen = sizeof(struct sockaddr_in);
709 resplen = recvfrom(statp->_sock,
710 (char *)ans, anssiz, 0,
711 (struct sockaddr *)&from, &fromlen);
712 if (resplen <= 0) {
713 Perror(statp, stderr, "recvfrom", errno);
714 res_nclose(statp);
715 goto next_ns;
717 gotsomewhere = 1;
718 if (resplen < HFIXEDSZ) {
720 * Undersized message.
722 Dprint(statp->options & RES_DEBUG,
723 (stdout, ";; undersized: %d\n",
724 resplen));
725 terrno = ISC_R_NOSPACE;
726 badns |= (1 << ns);
727 res_nclose(statp);
728 goto next_ns;
730 if (hp->id != anhp->id) {
732 * response from old query, ignore it.
733 * XXX - potential security hazard could
734 * be detected here.
736 DprintQ((statp->options & RES_DEBUG) ||
737 (statp->pfcode & RES_PRF_REPLY),
738 (stdout, ";; old answer:\n"),
739 ans, (resplen>anssiz)?anssiz:resplen);
740 goto wait;
742 #ifdef CHECK_SRVR_ADDR
743 if (!(statp->options & RES_INSECURE1) &&
744 !res_ourserver_p(statp, &from)) {
746 * response from wrong server? ignore it.
747 * XXX - potential security hazard could
748 * be detected here.
750 DprintQ((statp->options & RES_DEBUG) ||
751 (statp->pfcode & RES_PRF_REPLY),
752 (stdout, ";; not our server:\n"),
753 ans, (resplen>anssiz)?anssiz:resplen);
754 goto wait;
756 #endif
757 if (!(statp->options & RES_INSECURE2) &&
758 !res_queriesmatch((u_char *)buf,
759 ((u_char *)buf) + buflen,
760 (u_char *)ans,
761 ((u_char *)ans) + anssiz)) {
763 * response contains wrong query? ignore it.
764 * XXX - potential security hazard could
765 * be detected here.
767 DprintQ((statp->options & RES_DEBUG) ||
768 (statp->pfcode & RES_PRF_REPLY),
769 (stdout, ";; wrong query name:\n"),
770 ans, (resplen>anssiz)?anssiz:resplen);
771 goto wait;
773 if (anhp->rcode == SERVFAIL ||
774 anhp->rcode == NOTIMP ||
775 anhp->rcode == REFUSED) {
776 DprintQ(statp->options & RES_DEBUG,
777 (stdout, "server rejected query:\n"),
778 ans, (resplen>anssiz)?anssiz:resplen);
779 badns |= (1 << ns);
780 res_nclose(statp);
781 /* don't retry if called from dig */
782 if (!statp->pfcode)
783 goto next_ns;
785 if (!(statp->options & RES_IGNTC) && anhp->tc) {
787 * get rest of answer;
788 * use TCP with same server.
790 Dprint(statp->options & RES_DEBUG,
791 (stdout, ";; truncated answer\n"));
792 v_circuit = 1;
793 res_nclose(statp);
794 goto same_ns;
796 } /*if vc/dg*/
797 Dprint((statp->options & RES_DEBUG) ||
798 ((statp->pfcode & RES_PRF_REPLY) &&
799 (statp->pfcode & RES_PRF_HEAD1)),
800 (stdout, ";; got answer:\n"));
801 DprintQ((statp->options & RES_DEBUG) ||
802 (statp->pfcode & RES_PRF_REPLY),
803 (stdout, ""),
804 ans, (resplen>anssiz)?anssiz:resplen);
806 * If using virtual circuits, we assume that the first server
807 * is preferred over the rest (i.e. it is on the local
808 * machine) and only keep that one open.
809 * If we have temporarily opened a virtual circuit,
810 * or if we haven't been asked to keep a socket open,
811 * close the socket.
813 if ((v_circuit && (!(statp->options & RES_USEVC) || ns != 0)) ||
814 !(statp->options & RES_STAYOPEN)) {
815 res_nclose(statp);
817 if (statp->rhook) {
818 int done = 0, loops = 0;
820 do {
821 res_sendhookact act;
823 act = (*statp->rhook)(nsap, buf, buflen,
824 ans, anssiz, &resplen);
825 switch (act) {
826 case res_goahead:
827 case res_done:
828 done = 1;
829 break;
830 case res_nextns:
831 res_nclose(statp);
832 goto next_ns;
833 case res_modified:
834 /* give the hook another try */
835 if (++loops < 42) /*doug adams*/
836 break;
837 /*FALLTHROUGH*/
838 case res_error:
839 /*FALLTHROUGH*/
840 default:
841 return ISC_R_UNEXPECTED;
843 } while (!done);
846 *ansret = resplen;
847 return ISC_R_SUCCESS;
848 next_ns: ;
849 } /*foreach ns*/
850 } /*foreach retry*/
851 res_nclose(statp);
852 if (!v_circuit) {
853 if (!gotsomewhere)
854 terrno = ISC_R_CONNREFUSED; /* no nameservers found */
855 else
856 errno = ISC_R_TIMEDOUT; /* no answer obtained */
858 return terrno;
862 * This routine is for closing the socket if a virtual circuit is used and
863 * the program wants to close it. This provides support for endhostent()
864 * which expects to close the socket.
866 * This routine is not expected to be user visible.
868 void
869 res_nclose(res_state statp) {
870 if (statp->_sock >= 0) {
871 (void) close(statp->_sock);
872 statp->_sock = -1;
873 statp->_flags &= ~(RES_F_VC | RES_F_CONN);
877 /* Private */
878 static int
879 cmpsock(struct sockaddr_in *a1, struct sockaddr_in *a2) {
880 return ((a1->sin_family == a2->sin_family) &&
881 (a1->sin_port == a2->sin_port) &&
882 (a1->sin_addr.s_addr == a2->sin_addr.s_addr));
885 #ifdef NEED_PSELECT
886 /* XXX needs to move to the porting library. */
887 static int
888 pselect(int nfds, void *rfds, void *wfds, void *efds,
889 struct timespec *tsp,
890 const sigset_t *sigmask)
892 struct timeval tv, *tvp;
893 sigset_t sigs;
894 int n;
896 if (tsp) {
897 tvp = &tv;
898 tv = evTimeVal(*tsp);
899 } else
900 tvp = NULL;
901 if (sigmask)
902 sigprocmask(SIG_SETMASK, sigmask, &sigs);
903 n = select(nfds, rfds, wfds, efds, tvp);
904 if (sigmask)
905 sigprocmask(SIG_SETMASK, &sigs, NULL);
906 if (tsp)
907 *tsp = evTimeSpec(tv);
908 return (n);
910 #endif