(obstack_free): Explicitly convert __obj to
[kopensolaris-gnu/glibc.git] / resolv / res_send.c
1 /*
2  * ++Copyright++ 1985, 1989, 1993
3  * -
4  * Copyright (c) 1985, 1989, 1993
5  *    The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *      This product includes software developed by the University of
18  *      California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  * -
35  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
36  *
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.
43  *
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.
52  * -
53  * --Copyright--
54  */
55
56 #if defined(LIBC_SCCS) && !defined(lint)
57 static char sccsid[] = "@(#)res_send.c  8.1 (Berkeley) 6/4/93";
58 static char rcsid[] = "$Id$";
59 #endif /* LIBC_SCCS and not lint */
60
61         /* change this to "0"
62          * if you talk to a lot
63          * of multi-homed SunOS
64          * ("broken") name servers.
65          */
66 #define CHECK_SRVR_ADDR 1       /* XXX - should be in options.h */
67
68 /*
69  * Send query to name server and wait for reply.
70  */
71
72 #include <sys/types.h>
73 #include <sys/param.h>
74 #include <sys/poll.h>
75 #include <sys/time.h>
76 #include <sys/socket.h>
77 #include <sys/uio.h>
78 #include <netinet/in.h>
79 #include <arpa/nameser.h>
80 #include <arpa/inet.h>
81
82 #include <stdio.h>
83 #include <netdb.h>
84 #include <errno.h>
85 #include <resolv.h>
86 #if defined(BSD) && (BSD >= 199306)
87 # include <stdlib.h>
88 # include <string.h>
89 # include <unistd.h>
90 #else
91 # include "../conf/portability.h"
92 #endif
93
94 #if defined(USE_OPTIONS_H)
95 # include <../conf/options.h>
96 #endif
97
98 static int s = -1;      /* socket used for communications */
99 static int connected = 0;       /* is the socket connected */
100 static int vc = 0;      /* is the socket a virtual circuit? */
101
102 /* XXX - this should be done in portability.h */
103 #if (defined(BSD) && (BSD >= 199103)) || defined(linux)
104 # define CAN_RECONNECT 1
105 #else
106 # define CAN_RECONNECT 0
107 #endif
108
109 #ifndef DEBUG
110 #   define Dprint(cond, args) /*empty*/
111 #   define DprintQ(cond, args, query, size) /*empty*/
112 #   define Aerror(file, string, error, address) /*empty*/
113 #   define Perror(file, string, error) /*empty*/
114 #else
115 #   define Dprint(cond, args) if (cond) {fprintf args;} else {}
116 #   define DprintQ(cond, args, query, size) if (cond) {\
117                         fprintf args;\
118                         __fp_nquery(query, size, stdout);\
119                 } else {}
120     static void
121     Aerror(file, string, error, address)
122         FILE *file;
123         char *string;
124         int error;
125         struct sockaddr_in address;
126     {
127         int save = errno;
128
129         if (_res.options & RES_DEBUG) {
130                 fprintf(file, "res_send: %s ([%s].%u): %s\n",
131                         string,
132                         inet_ntoa(address.sin_addr),
133                         ntohs(address.sin_port),
134                         strerror(error));
135         }
136         __set_errno (save);
137     }
138     static void
139     Perror(file, string, error)
140         FILE *file;
141         char *string;
142         int error;
143     {
144         int save = errno;
145
146         if (_res.options & RES_DEBUG) {
147                 fprintf(file, "res_send: %s: %s\n",
148                         string, strerror(error));
149         }
150         __set_errno (save);
151     }
152 #endif
153
154 static res_send_qhook Qhook = NULL;
155 static res_send_rhook Rhook = NULL;
156
157 void
158 res_send_setqhook(hook)
159         res_send_qhook hook;
160 {
161
162         Qhook = hook;
163 }
164
165 void
166 res_send_setrhook(hook)
167         res_send_rhook hook;
168 {
169
170         Rhook = hook;
171 }
172
173 /* int
174  * res_isourserver(ina)
175  *      looks up "ina" in _res.ns_addr_list[]
176  * returns:
177  *      0  : not found
178  *      >0 : found
179  * author:
180  *      paul vixie, 29may94
181  */
182 int
183 res_isourserver(inp)
184         const struct sockaddr_in *inp;
185 {
186         struct sockaddr_in ina;
187         register int ns, ret;
188
189         ina = *inp;
190         ret = 0;
191         for (ns = 0;  ns < _res.nscount;  ns++) {
192                 register const struct sockaddr_in *srv = &_res.nsaddr_list[ns];
193
194                 if (srv->sin_family == ina.sin_family &&
195                     srv->sin_port == ina.sin_port &&
196                     (srv->sin_addr.s_addr == INADDR_ANY ||
197                      srv->sin_addr.s_addr == ina.sin_addr.s_addr)) {
198                         ret++;
199                         break;
200                 }
201         }
202         return (ret);
203 }
204
205 /* int
206  * res_nameinquery(name, type, class, buf, eom)
207  *      look for (name,type,class) in the query section of packet (buf,eom)
208  * requires:
209  *      buf + HFIXESDZ <= eom
210  * returns:
211  *      -1 : format error
212  *      0  : not found
213  *      >0 : found
214  * author:
215  *      paul vixie, 29may94
216  */
217 int
218 res_nameinquery(name, type, class, buf, eom)
219         const char *name;
220         register int type, class;
221         const u_char *buf, *eom;
222 {
223         register const u_char *cp = buf + HFIXEDSZ;
224         int qdcount = ntohs(((HEADER*)buf)->qdcount);
225
226         while (qdcount-- > 0) {
227                 char tname[MAXDNAME+1];
228                 register int n, ttype, tclass;
229
230                 n = dn_expand(buf, eom, cp, tname, sizeof tname);
231                 if (n < 0)
232                         return (-1);
233                 cp += n;
234                 if (cp + 2 * INT16SZ > eom)
235                         return (-1);
236                 ttype = _getshort(cp); cp += INT16SZ;
237                 tclass = _getshort(cp); cp += INT16SZ;
238                 if (ttype == type &&
239                     tclass == class &&
240                     strcasecmp(tname, name) == 0)
241                         return (1);
242         }
243         return (0);
244 }
245
246 /* int
247  * res_queriesmatch(buf1, eom1, buf2, eom2)
248  *      is there a 1:1 mapping of (name,type,class)
249  *      in (buf1,eom1) and (buf2,eom2)?
250  * returns:
251  *      -1 : format error
252  *      0  : not a 1:1 mapping
253  *      >0 : is a 1:1 mapping
254  * author:
255  *      paul vixie, 29may94
256  */
257 int
258 res_queriesmatch(buf1, eom1, buf2, eom2)
259         const u_char *buf1, *eom1;
260         const u_char *buf2, *eom2;
261 {
262         register const u_char *cp = buf1 + HFIXEDSZ;
263         int qdcount = ntohs(((HEADER*)buf1)->qdcount);
264
265         if (buf1 + HFIXEDSZ > eom1 || buf2 + HFIXEDSZ > eom2)
266                 return (-1);
267
268         if (qdcount != ntohs(((HEADER*)buf2)->qdcount))
269                 return (0);
270         while (qdcount-- > 0) {
271                 char tname[MAXDNAME+1];
272                 register int n, ttype, tclass;
273
274                 n = dn_expand(buf1, eom1, cp, tname, sizeof tname);
275                 if (n < 0)
276                         return (-1);
277                 cp += n;
278                 if (cp + 2 * INT16SZ > eom1)
279                         return (-1);
280                 ttype = _getshort(cp);  cp += INT16SZ;
281                 tclass = _getshort(cp); cp += INT16SZ;
282                 if (!res_nameinquery(tname, ttype, tclass, buf2, eom2))
283                         return (0);
284         }
285         return (1);
286 }
287
288 int
289 res_send(buf, buflen, ans, anssiz)
290         const u_char *buf;
291         int buflen;
292         u_char *ans;
293         int anssiz;
294 {
295         HEADER *hp = (HEADER *) buf;
296         HEADER *anhp = (HEADER *) ans;
297         int gotsomewhere, connreset, terrno, try, v_circuit, resplen, ns;
298         register int n;
299         u_int badns;    /* XXX NSMAX can't exceed #/bits in this var */
300
301         if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
302                 /* errno should have been set by res_init() in this case. */
303                 return (-1);
304         }
305         if (anssiz < HFIXEDSZ) {
306                 __set_errno (EINVAL);
307                 return (-1);
308         }
309         DprintQ((_res.options & RES_DEBUG) || (_res.pfcode & RES_PRF_QUERY),
310                 (stdout, ";; res_send()\n"), buf, buflen);
311         v_circuit = (_res.options & RES_USEVC) || buflen > PACKETSZ;
312         gotsomewhere = 0;
313         connreset = 0;
314         terrno = ETIMEDOUT;
315         badns = 0;
316
317         /*
318          * Send request, RETRY times, or until successful
319          */
320         for (try = 0; try < _res.retry; try++) {
321             for (ns = 0; ns < _res.nscount; ns++) {
322                 struct sockaddr_in *nsap = &_res.nsaddr_list[ns];
323     same_ns:
324                 if (badns & (1 << ns)) {
325                         res_close();
326                         goto next_ns;
327                 }
328
329                 if (Qhook) {
330                         int done = 0, loops = 0;
331
332                         do {
333                                 res_sendhookact act;
334
335                                 act = (*Qhook)(&nsap, &buf, &buflen,
336                                                ans, anssiz, &resplen);
337                                 switch (act) {
338                                 case res_goahead:
339                                         done = 1;
340                                         break;
341                                 case res_nextns:
342                                         res_close();
343                                         goto next_ns;
344                                 case res_done:
345                                         return (resplen);
346                                 case res_modified:
347                                         /* give the hook another try */
348                                         if (++loops < 42) /*doug adams*/
349                                                 break;
350                                         /*FALLTHROUGH*/
351                                 case res_error:
352                                         /*FALLTHROUGH*/
353                                 default:
354                                         return (-1);
355                                 }
356                         } while (!done);
357                 }
358
359                 Dprint(_res.options & RES_DEBUG,
360                        (stdout, ";; Querying server (# %d) address = %s\n",
361                         ns + 1, inet_ntoa(nsap->sin_addr)));
362
363                 if (v_circuit) {
364                         int truncated;
365                         struct iovec iov[2];
366                         u_short len;
367                         u_char *cp;
368
369                         /*
370                          * Use virtual circuit;
371                          * at most one attempt per server.
372                          */
373                         try = _res.retry;
374                         truncated = 0;
375                         if ((s < 0) || (!vc)) {
376                                 if (s >= 0)
377                                         res_close();
378
379                                 s = socket(PF_INET, SOCK_STREAM, 0);
380                                 if (s < 0) {
381                                         terrno = errno;
382                                         Perror(stderr, "socket(vc)", errno);
383                                         return (-1);
384                                 }
385                                 __set_errno (0);
386                                 if (connect(s, (struct sockaddr *)nsap,
387                                             sizeof(struct sockaddr)) < 0) {
388                                         terrno = errno;
389                                         Aerror(stderr, "connect/vc",
390                                                errno, *nsap);
391                                         badns |= (1 << ns);
392                                         res_close();
393                                         goto next_ns;
394                                 }
395                                 vc = 1;
396                         }
397                         /*
398                          * Send length & message
399                          */
400                         putshort((u_short)buflen, (u_char*)&len);
401                         iov[0].iov_base = (caddr_t)&len;
402                         iov[0].iov_len = INT16SZ;
403                         iov[1].iov_base = (caddr_t)buf;
404                         iov[1].iov_len = buflen;
405                         if (writev(s, iov, 2) != (INT16SZ + buflen)) {
406                                 terrno = errno;
407                                 Perror(stderr, "write failed", errno);
408                                 badns |= (1 << ns);
409                                 res_close();
410                                 goto next_ns;
411                         }
412                         /*
413                          * Receive length & response
414                          */
415 read_len:
416                         cp = ans;
417                         len = INT16SZ;
418                         while ((n = read(s, (char *)cp, (int)len)) > 0) {
419                                 cp += n;
420                                 if ((len -= n) <= 0)
421                                         break;
422                         }
423                         if (n <= 0) {
424                                 terrno = errno;
425                                 Perror(stderr, "read failed", errno);
426                                 res_close();
427                                 /*
428                                  * A long running process might get its TCP
429                                  * connection reset if the remote server was
430                                  * restarted.  Requery the server instead of
431                                  * trying a new one.  When there is only one
432                                  * server, this means that a query might work
433                                  * instead of failing.  We only allow one reset
434                                  * per query to prevent looping.
435                                  */
436                                 if (terrno == ECONNRESET && !connreset) {
437                                         connreset = 1;
438                                         res_close();
439                                         goto same_ns;
440                                 }
441                                 res_close();
442                                 goto next_ns;
443                         }
444                         resplen = _getshort(ans);
445                         if (resplen > anssiz) {
446                                 Dprint(_res.options & RES_DEBUG,
447                                        (stdout, ";; response truncated\n")
448                                        );
449                                 truncated = 1;
450                                 len = anssiz;
451                         } else
452                                 len = resplen;
453                         if (len < HFIXEDSZ) {
454                                 /*
455                                  * Undersized message.
456                                  */
457                                 Dprint(_res.options & RES_DEBUG,
458                                        (stdout, ";; undersized: %d\n", len));
459                                 terrno = EMSGSIZE;
460                                 badns |= (1 << ns);
461                                 res_close();
462                                 goto next_ns;
463                         }
464                         cp = ans;
465                         while (len != 0 &&
466                                (n = read(s, (char *)cp, (int)len)) > 0) {
467                                 cp += n;
468                                 len -= n;
469                         }
470                         if (n <= 0) {
471                                 terrno = errno;
472                                 Perror(stderr, "read(vc)", errno);
473                                 res_close();
474                                 goto next_ns;
475                         }
476                         if (truncated) {
477                                 /*
478                                  * Flush rest of answer
479                                  * so connection stays in synch.
480                                  */
481                                 anhp->tc = 1;
482                                 len = resplen - anssiz;
483                                 while (len != 0) {
484                                         char junk[PACKETSZ];
485
486                                         n = ((size_t) len > sizeof(junk)
487                                              ? sizeof(junk)
488                                              : len);
489                                         if ((n = read(s, junk, n)) > 0)
490                                                 len -= n;
491                                         else
492                                                 break;
493                                 }
494                         }
495                         /*
496                          * The calling applicating has bailed out of
497                          * a previous call and failed to arrange to have
498                          * the circuit closed or the server has got
499                          * itself confused. Anyway drop the packet and
500                          * wait for the correct one.
501                          */
502                         if (hp->id != anhp->id) {
503                                 DprintQ((_res.options & RES_DEBUG) ||
504                                         (_res.pfcode & RES_PRF_REPLY),
505                                         (stdout, ";; old answer (unexpected):\n"),
506                                         ans, (resplen>anssiz)?anssiz:resplen);
507                                 goto read_len;
508                         }
509                 } else {
510                         /*
511                          * Use datagrams.
512                          */
513                         int timeout;
514                         struct pollfd pfd[1];
515                         struct sockaddr_in from;
516                         socklen_t fromlen;
517
518                         if ((s < 0) || vc) {
519                                 if (vc)
520                                         res_close();
521                                 s = socket(PF_INET, SOCK_DGRAM, 0);
522                                 if (s < 0) {
523 #if !CAN_RECONNECT
524  bad_dg_sock:
525 #endif
526                                         terrno = errno;
527                                         Perror(stderr, "socket(dg)", errno);
528                                         return (-1);
529                                 }
530                                 connected = 0;
531                         }
532                         /*
533                          * On a 4.3BSD+ machine (client and server,
534                          * actually), sending to a nameserver datagram
535                          * port with no nameserver will cause an
536                          * ICMP port unreachable message to be returned.
537                          * If our datagram socket is "connected" to the
538                          * server, we get an ECONNREFUSED error on the next
539                          * socket operation, and select returns if the
540                          * error message is received.  We can thus detect
541                          * the absence of a nameserver without timing out.
542                          * If we have sent queries to at least two servers,
543                          * however, we don't want to remain connected,
544                          * as we wish to receive answers from the first
545                          * server to respond.
546                          */
547                         if (_res.nscount == 1 || (try == 0 && ns == 0)) {
548                                 /*
549                                  * Connect only if we are sure we won't
550                                  * receive a response from another server.
551                                  */
552                                 if (!connected) {
553                                         if (connect(s, (struct sockaddr *)nsap,
554                                                     sizeof(struct sockaddr)
555                                                     ) < 0) {
556                                                 Aerror(stderr,
557                                                        "connect(dg)",
558                                                        errno, *nsap);
559                                                 badns |= (1 << ns);
560                                                 res_close();
561                                                 goto next_ns;
562                                         }
563                                         connected = 1;
564                                 }
565                                 if (send(s, (char*)buf, buflen, 0) != buflen) {
566                                         Perror(stderr, "send", errno);
567                                         badns |= (1 << ns);
568                                         res_close();
569                                         goto next_ns;
570                                 }
571                         } else {
572                                 /*
573                                  * Disconnect if we want to listen
574                                  * for responses from more than one server.
575                                  */
576                                 if (connected) {
577 #if CAN_RECONNECT
578                                         struct sockaddr_in no_addr;
579
580                                         no_addr.sin_family = AF_INET;
581                                         no_addr.sin_addr.s_addr = INADDR_ANY;
582                                         no_addr.sin_port = 0;
583                                         (void) connect(s,
584                                                        (struct sockaddr *)
585                                                         &no_addr,
586                                                        sizeof(no_addr));
587 #else
588                                         int s1 = socket(PF_INET, SOCK_DGRAM,0);
589                                         if (s1 < 0)
590                                                 goto bad_dg_sock;
591                                         (void) dup2(s1, s);
592                                         (void) close(s1);
593                                         Dprint(_res.options & RES_DEBUG,
594                                                (stdout, ";; new DG socket\n"))
595 #endif
596                                         connected = 0;
597                                         __set_errno (0);
598                                 }
599                                 if (sendto(s, (char*)buf, buflen, 0,
600                                            (struct sockaddr *)nsap,
601                                            sizeof(struct sockaddr))
602                                     != buflen) {
603                                         Aerror(stderr, "sendto", errno, *nsap);
604                                         badns |= (1 << ns);
605                                         res_close();
606                                         goto next_ns;
607                                 }
608                         }
609
610                         /*
611                          * Wait for reply
612                          */
613                         timeout = (_res.retrans << try) * 1000;
614                         if (try > 0)
615                                 timeout /= _res.nscount;
616                         if (timeout <= 0)
617                                 timeout = 1000;
618     wait:
619                         if (s < 0 || s >= FD_SETSIZE) {
620                                 Perror(stderr, "s out-of-bounds", EMFILE);
621                                 res_close();
622                                 goto next_ns;
623                         }
624                         pfd[0].fd = s;
625                         pfd[0].events = POLLIN;
626                         n = __poll(pfd, 1, timeout);
627                         if (n < 0) {
628                                 if (errno == EINTR)
629                                         goto wait;
630                                 Perror(stderr, "poll", errno);
631                                 res_close();
632                                 goto next_ns;
633                         }
634                         if (n == 0) {
635                                 /*
636                                  * timeout
637                                  */
638                                 Dprint(_res.options & RES_DEBUG,
639                                        (stdout, ";; timeout\n"));
640                                 gotsomewhere = 1;
641                                 res_close();
642                                 goto next_ns;
643                         }
644                         __set_errno (0);
645                         fromlen = sizeof(struct sockaddr_in);
646                         resplen = recvfrom(s, (char*)ans, anssiz, 0,
647                                            (struct sockaddr *)&from, &fromlen);
648                         if (resplen <= 0) {
649                                 Perror(stderr, "recvfrom", errno);
650                                 res_close();
651                                 goto next_ns;
652                         }
653                         gotsomewhere = 1;
654                         if (resplen < HFIXEDSZ) {
655                                 /*
656                                  * Undersized message.
657                                  */
658                                 Dprint(_res.options & RES_DEBUG,
659                                        (stdout, ";; undersized: %d\n",
660                                         resplen));
661                                 terrno = EMSGSIZE;
662                                 badns |= (1 << ns);
663                                 res_close();
664                                 goto next_ns;
665                         }
666                         if (hp->id != anhp->id) {
667                                 /*
668                                  * response from old query, ignore it.
669                                  * XXX - potential security hazard could
670                                  *       be detected here.
671                                  */
672                                 DprintQ((_res.options & RES_DEBUG) ||
673                                         (_res.pfcode & RES_PRF_REPLY),
674                                         (stdout, ";; old answer:\n"),
675                                         ans, (resplen>anssiz)?anssiz:resplen);
676                                 goto wait;
677                         }
678 #if CHECK_SRVR_ADDR
679                         if (!(_res.options & RES_INSECURE1) &&
680                             !res_isourserver(&from)) {
681                                 /*
682                                  * response from wrong server? ignore it.
683                                  * XXX - potential security hazard could
684                                  *       be detected here.
685                                  */
686                                 DprintQ((_res.options & RES_DEBUG) ||
687                                         (_res.pfcode & RES_PRF_REPLY),
688                                         (stdout, ";; not our server:\n"),
689                                         ans, (resplen>anssiz)?anssiz:resplen);
690                                 goto wait;
691                         }
692 #endif
693                         if (!(_res.options & RES_INSECURE2) &&
694                             !res_queriesmatch(buf, buf + buflen,
695                                               ans, ans + anssiz)) {
696                                 /*
697                                  * response contains wrong query? ignore it.
698                                  * XXX - potential security hazard could
699                                  *       be detected here.
700                                  */
701                                 DprintQ((_res.options & RES_DEBUG) ||
702                                         (_res.pfcode & RES_PRF_REPLY),
703                                         (stdout, ";; wrong query name:\n"),
704                                         ans, (resplen>anssiz)?anssiz:resplen);
705                                 goto wait;
706                         }
707                         if (anhp->rcode == SERVFAIL ||
708                             anhp->rcode == NOTIMP ||
709                             anhp->rcode == REFUSED) {
710                                 DprintQ(_res.options & RES_DEBUG,
711                                         (stdout, "server rejected query:\n"),
712                                         ans, (resplen>anssiz)?anssiz:resplen);
713                                 badns |= (1 << ns);
714                                 res_close();
715                                 /* don't retry if called from dig */
716                                 if (!_res.pfcode)
717                                         goto next_ns;
718                         }
719                         if (!(_res.options & RES_IGNTC) && anhp->tc) {
720                                 /*
721                                  * get rest of answer;
722                                  * use TCP with same server.
723                                  */
724                                 Dprint(_res.options & RES_DEBUG,
725                                        (stdout, ";; truncated answer\n"));
726                                 v_circuit = 1;
727                                 res_close();
728                                 goto same_ns;
729                         }
730                 } /*if vc/dg*/
731                 Dprint((_res.options & RES_DEBUG) ||
732                        ((_res.pfcode & RES_PRF_REPLY) &&
733                         (_res.pfcode & RES_PRF_HEAD1)),
734                        (stdout, ";; got answer:\n"));
735                 DprintQ((_res.options & RES_DEBUG) ||
736                         (_res.pfcode & RES_PRF_REPLY),
737                         (stdout, ""),
738                         ans, (resplen>anssiz)?anssiz:resplen);
739                 /*
740                  * If using virtual circuits, we assume that the first server
741                  * is preferred over the rest (i.e. it is on the local
742                  * machine) and only keep that one open.
743                  * If we have temporarily opened a virtual circuit,
744                  * or if we haven't been asked to keep a socket open,
745                  * close the socket.
746                  */
747                 if ((v_circuit && (!(_res.options & RES_USEVC) || ns != 0)) ||
748                     !(_res.options & RES_STAYOPEN)) {
749                         res_close();
750                 }
751                 if (Rhook) {
752                         int done = 0, loops = 0;
753
754                         do {
755                                 res_sendhookact act;
756
757                                 act = (*Rhook)(nsap, buf, buflen,
758                                                ans, anssiz, &resplen);
759                                 switch (act) {
760                                 case res_goahead:
761                                 case res_done:
762                                         done = 1;
763                                         break;
764                                 case res_nextns:
765                                         res_close();
766                                         goto next_ns;
767                                 case res_modified:
768                                         /* give the hook another try */
769                                         if (++loops < 42) /*doug adams*/
770                                                 break;
771                                         /*FALLTHROUGH*/
772                                 case res_error:
773                                         /*FALLTHROUGH*/
774                                 default:
775                                         return (-1);
776                                 }
777                         } while (!done);
778
779                 }
780                 return (resplen);
781     next_ns: ;
782            } /*foreach ns*/
783         } /*foreach retry*/
784         res_close();
785         if (!v_circuit) {
786                 if (!gotsomewhere)
787                         __set_errno (ECONNREFUSED); /* no nameservers found */
788                 else
789                         __set_errno (ETIMEDOUT);    /* no answer obtained */
790         } else
791                 __set_errno (terrno);
792         return (-1);
793 }
794
795 /*
796  * This routine is for closing the socket if a virtual circuit is used and
797  * the program wants to close it.  This provides support for endhostent()
798  * which expects to close the socket.
799  *
800  * This routine is not expected to be user visible.
801  */
802 void
803 res_close()
804 {
805         if (s >= 0) {
806                 (void) close(s);
807                 s = -1;
808                 connected = 0;
809                 vc = 0;
810         }
811 }
812
813 #ifdef ultrix
814 /* ultrix 4.0 had some icky packaging in its libc.a.  alias for it here.
815  * there is more gunk of this kind over in res_debug.c.
816  */
817
818 void
819 _res_close()
820 {
821         res_close();
822 }
823
824 #undef res_send
825 int
826 res_send(buf, buflen, ans, anssiz)
827         const u_char *buf;
828         int buflen;
829         u_char *ans;
830         int anssiz;
831 {
832         return (__res_send(buf, buflen, ans, anssiz));
833 }
834 #endif /* Ultrix 4.0 hackery */