+++ /dev/null
-/*\r
- * Copyright (c) 1985, 1989, 1993\r
- * The Regents of the University of California. All rights reserved.\r
- *\r
- * Portions copyright (c) 1999, 2000\r
- * Intel Corporation.\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- *\r
- * 1. Redistributions of source code must retain the above copyright\r
- * notice, this list of conditions and the following disclaimer.\r
- *\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- * notice, this list of conditions and the following disclaimer in the\r
- * documentation and/or other materials provided with the distribution.\r
- *\r
- * 3. All advertising materials mentioning features or use of this software\r
- * must display the following acknowledgement:\r
- *\r
- * This product includes software developed by the University of\r
- * California, Berkeley, Intel Corporation, and its contributors.\r
- *\r
- * 4. Neither the name of University, Intel Corporation, or their respective\r
- * contributors may be used to endorse or promote products derived from\r
- * this software without specific prior written permission.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS, INTEL CORPORATION AND\r
- * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,\r
- * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS\r
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS,\r
- * INTEL CORPORATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,\r
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\r
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\r
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\r
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF\r
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
- *\r
- */\r
-\r
-/*\r
- * Portions Copyright (c) 1993 by Digital Equipment Corporation.\r
- *\r
- * Permission to use, copy, modify, and distribute this software for any\r
- * purpose with or without fee is hereby granted, provided that the above\r
- * copyright notice and this permission notice appear in all copies, and that\r
- * the name of Digital Equipment Corporation not be used in advertising or\r
- * publicity pertaining to distribution of the document or software without\r
- * specific, written prior permission.\r
- *\r
- * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL\r
- * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES\r
- * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT\r
- * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL\r
- * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR\r
- * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS\r
- * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS\r
- * SOFTWARE.\r
- */\r
-\r
-/*\r
- * Portions Copyright (c) 1996 by Internet Software Consortium.\r
- *\r
- * Permission to use, copy, modify, and distribute this software for any\r
- * purpose with or without fee is hereby granted, provided that the above\r
- * copyright notice and this permission notice appear in all copies.\r
- *\r
- * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS\r
- * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES\r
- * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE\r
- * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL\r
- * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR\r
- * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS\r
- * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS\r
- * SOFTWARE.\r
- */\r
-\r
-#if defined(LIBC_SCCS) && !defined(lint)\r
-static char sccsid[] = "@(#)res_send.c 8.1 (Berkeley) 6/4/93";\r
-static char orig_rcsid[] = "From: Id: res_send.c,v 8.20 1998/04/06 23:27:51 halley Exp $";\r
-static char rcsid[] = "$Id: res_send.c,v 1.1.1.1 2003/11/19 01:51:39 kyu3 Exp $";\r
-#endif /* LIBC_SCCS and not lint */\r
-\r
-/*\r
- * Send query to name server and wait for reply.\r
- */\r
-\r
-#include <sys/types.h>\r
-#include <sys/param.h>\r
-#include <sys/select.h>\r
-#include <sys/socket.h>\r
-#include <sys/time.h>\r
-#include <sys/uio.h>\r
-\r
-#include <netinet/in.h>\r
-#include <arpa/nameser.h>\r
-#include <arpa/inet.h>\r
-\r
-#include <errno.h>\r
-#include <netdb.h>\r
-#include <resolv.h>\r
-#include <stdio.h>\r
-#include <stdlib.h>\r
-#include <string.h>\r
-#include <unistd.h>\r
-\r
-#include "res_config.h"\r
-\r
-#ifndef _ORG_FREEBSD_\r
-#define NOPOLL\r
-#endif\r
-\r
-#ifdef NOPOLL /* libc_r doesn't wrap poll yet() */\r
-#else\r
-#include <poll.h>\r
-static int use_poll = 1; /* adapt to poll() syscall availability */\r
- /* 0 = not present, 1 = try it, 2 = exists */\r
-#endif\r
-\r
-static int s = -1; /* socket used for communications */\r
-static int connected = 0; /* is the socket connected */\r
-static int vc = 0; /* is the socket a virtual circuit? */\r
-static res_send_qhook Qhook = NULL;\r
-static res_send_rhook Rhook = NULL;\r
-\r
-\r
-#define CAN_RECONNECT 1\r
-\r
-#ifndef DEBUG\r
-# define Dprint(cond, args) /*empty*/\r
-# define DprintQ(cond, args, query, size) /*empty*/\r
-# define Aerror(file, string, error, address) /*empty*/\r
-# define Perror(file, string, error) /*empty*/\r
-#else\r
-# define Dprint(cond, args) if (cond) {fprintf args;} else {}\r
-# define DprintQ(cond, args, query, size) if (cond) {\\r
- fprintf args;\\r
- __fp_nquery(query, size, stdout);\\r
- } else {}\r
-\r
-static void\r
-Aerror(\r
- FILE *file,\r
- char *string,\r
- int error,\r
- struct sockaddr_in address\r
- )\r
-{\r
- int save = errno;\r
-\r
- if (_res.options & RES_DEBUG) {\r
- fprintf(file, "res_send: %s ([%s].%u): %s\n",\r
- string,\r
- inet_ntoa(address.sin_addr),\r
- ntohs(address.sin_port),\r
- strerror(error));\r
- }\r
- errno = save;\r
-}\r
-\r
-\r
-static void\r
-Perror(\r
- FILE *file,\r
- char *string,\r
- int error\r
- )\r
-{\r
- int save = errno;\r
-\r
- if (_res.options & RES_DEBUG) {\r
- fprintf(file, "res_send: %s: %s\n",\r
- string, strerror(error));\r
- }\r
- errno = save;\r
-}\r
-#endif\r
-\r
-void\r
-res_send_setqhook(\r
- res_send_qhook hook\r
- )\r
-{\r
-\r
- Qhook = hook;\r
-}\r
-\r
-void\r
-res_send_setrhook(\r
- res_send_rhook hook\r
- )\r
-{\r
-\r
- Rhook = hook;\r
-}\r
-\r
-/* int\r
- * res_isourserver(ina)\r
- * looks up "ina" in _res.ns_addr_list[]\r
- * returns:\r
- * 0 : not found\r
- * >0 : found\r
- * author:\r
- * paul vixie, 29may94\r
- */\r
-int\r
-res_isourserver(\r
- const struct sockaddr_in *inp\r
- )\r
-{\r
- struct sockaddr_in ina;\r
- int ns, ret;\r
-\r
- ina = *inp;\r
- ret = 0;\r
- for (ns = 0; ns < _res.nscount; ns++) {\r
- const struct sockaddr_in *srv = &_res.nsaddr_list[ns];\r
-\r
- if (srv->sin_family == ina.sin_family &&\r
- srv->sin_port == ina.sin_port &&\r
- (srv->sin_addr.s_addr == INADDR_ANY ||\r
- srv->sin_addr.s_addr == ina.sin_addr.s_addr)) {\r
- ret++;\r
- break;\r
- }\r
- }\r
- return (ret);\r
-}\r
-\r
-/* int\r
- * res_nameinquery(name, type, class, buf, eom)\r
- * look for (name,type,class) in the query section of packet (buf,eom)\r
- * requires:\r
- * buf + HFIXEDSZ <= eom\r
- * returns:\r
- * -1 : format error\r
- * 0 : not found\r
- * >0 : found\r
- * author:\r
- * paul vixie, 29may94\r
- */\r
-int\r
-res_nameinquery(\r
- const char *name,\r
- int type,\r
- int class,\r
- const u_char *buf,\r
- const u_char *eom\r
- )\r
-{\r
- const u_char *cp = buf + HFIXEDSZ;\r
- int qdcount = ntohs(((HEADER*)buf)->qdcount);\r
-\r
- while (qdcount-- > 0) {\r
- char tname[MAXDNAME+1];\r
- int n, ttype, tclass;\r
-\r
- n = dn_expand(buf, eom, cp, tname, sizeof tname);\r
- if (n < 0)\r
- return (-1);\r
- cp += n;\r
- if (cp + 2 * INT16SZ > eom)\r
- return (-1);\r
- ttype = ns_get16(cp); cp += INT16SZ;\r
- tclass = ns_get16(cp); cp += INT16SZ;\r
- if (ttype == type &&\r
- tclass == class &&\r
- strcasecmp(tname, name) == 0)\r
- return (1);\r
- }\r
- return (0);\r
-}\r
-\r
-/* int\r
- * res_queriesmatch(buf1, eom1, buf2, eom2)\r
- * is there a 1:1 mapping of (name,type,class)\r
- * in (buf1,eom1) and (buf2,eom2)?\r
- * returns:\r
- * -1 : format error\r
- * 0 : not a 1:1 mapping\r
- * >0 : is a 1:1 mapping\r
- * author:\r
- * paul vixie, 29may94\r
- */\r
-int\r
-res_queriesmatch(\r
- const u_char *buf1,\r
- const u_char *eom1,\r
- const u_char *buf2,\r
- const u_char *eom2\r
- )\r
-{\r
- const u_char *cp = buf1 + HFIXEDSZ;\r
- int qdcount = ntohs(((HEADER*)buf1)->qdcount);\r
-\r
- if (buf1 + HFIXEDSZ > eom1 || buf2 + HFIXEDSZ > eom2)\r
- return (-1);\r
-\r
- /*\r
- * Only header section present in replies to\r
- * dynamic update packets.\r
- */\r
- if ( (((HEADER *)buf1)->opcode == ns_o_update) &&\r
- (((HEADER *)buf2)->opcode == ns_o_update) )\r
- return (1);\r
-\r
- if (qdcount != ntohs(((HEADER*)buf2)->qdcount))\r
- return (0);\r
- while (qdcount-- > 0) {\r
- char tname[MAXDNAME+1];\r
- int n, ttype, tclass;\r
-\r
- n = dn_expand(buf1, eom1, cp, tname, sizeof tname);\r
- if (n < 0)\r
- return (-1);\r
- cp += n;\r
- if (cp + 2 * INT16SZ > eom1)\r
- return (-1);\r
- ttype = ns_get16(cp); cp += INT16SZ;\r
- tclass = ns_get16(cp); cp += INT16SZ;\r
- if (!res_nameinquery(tname, ttype, tclass, buf2, eom2))\r
- return (0);\r
- }\r
- return (1);\r
-}\r
-\r
-int\r
-res_send(\r
- const u_char *buf,\r
- int buflen,\r
- u_char *ans,\r
- int anssiz\r
- )\r
-{\r
- HEADER *hp = (HEADER *) buf;\r
- HEADER *anhp = (HEADER *) ans;\r
- int gotsomewhere, connreset, terrno, try, v_circuit, resplen, ns;\r
- ssize_t n;\r
- u_int32_t badns; /* XXX NSMAX can't exceed #/bits in this variable */\r
-\r
- if ((_res.options & RES_INIT) == 0 && res_init() == -1) {\r
- /* errno should have been set by res_init() in this case. */\r
- return (-1);\r
- }\r
- if (anssiz < HFIXEDSZ) {\r
- errno = EINVAL;\r
- return (-1);\r
- }\r
- DprintQ((_res.options & RES_DEBUG) || (_res.pfcode & RES_PRF_QUERY),\r
- (stdout, ";; res_send()\n"), buf, buflen);\r
- v_circuit = (_res.options & RES_USEVC) || buflen > PACKETSZ;\r
- gotsomewhere = 0;\r
- connreset = 0;\r
- terrno = ETIMEDOUT;\r
- badns = 0;\r
-\r
- /*\r
- * Send request, RETRY times, or until successful\r
- */\r
- for (try = 0; try < _res.retry; try++) {\r
- for (ns = 0; ns < _res.nscount; ns++) {\r
- struct sockaddr_in *nsap = &_res.nsaddr_list[ns];\r
- same_ns:\r
- if (badns & (1 << ns)) {\r
- res_close();\r
- goto next_ns;\r
- }\r
-\r
- if (Qhook) {\r
- int done = 0, loops = 0;\r
-\r
- do {\r
- res_sendhookact act;\r
-\r
- act = (*Qhook)(&nsap, &buf, &buflen,\r
- ans, anssiz, &resplen);\r
- switch (act) {\r
- case res_goahead:\r
- done = 1;\r
- break;\r
- case res_nextns:\r
- res_close();\r
- goto next_ns;\r
- case res_done:\r
- return (resplen);\r
- case res_modified:\r
- /* give the hook another try */\r
- if (++loops < 42) /*doug adams*/\r
- break;\r
- /*FALLTHROUGH*/\r
- case res_error:\r
- /*FALLTHROUGH*/\r
- default:\r
- return (-1);\r
- }\r
- } while (!done);\r
- }\r
-\r
- Dprint(_res.options & RES_DEBUG,\r
- (stdout, ";; Querying server (# %d) address = %s\n",\r
- ns + 1, inet_ntoa(nsap->sin_addr)));\r
-\r
- if (v_circuit) {\r
- int truncated;\r
- struct iovec iov[2];\r
- u_short len;\r
- u_char *cp;\r
-\r
- /*\r
- * Use virtual circuit;\r
- * at most one attempt per server.\r
- */\r
- try = _res.retry;\r
- truncated = 0;\r
- if (s < 0 || !vc || hp->opcode == ns_o_update) {\r
- if (s >= 0)\r
- res_close();\r
-\r
- s = socket(PF_INET, SOCK_STREAM, 0);\r
- if (s < 0) {\r
- terrno = errno;\r
- Perror(stderr, "socket(vc)", errno);\r
- return (-1);\r
- }\r
- errno = 0;\r
- nsap->sin_len = sizeof ( *nsap );\r
- if (connect(s, (struct sockaddr *)nsap,\r
- sizeof *nsap) < 0) {\r
- terrno = errno;\r
- Aerror(stderr, "connect/vc",\r
- errno, *nsap);\r
- badns |= (1 << ns);\r
- res_close();\r
- goto next_ns;\r
- }\r
- vc = 1;\r
- }\r
- /*\r
- * Send length & message\r
- */\r
- putshort((u_short)buflen, (u_char*)&len);\r
- iov[0].iov_base = (caddr_t)&len;\r
- iov[0].iov_len = INT16SZ;\r
- iov[1].iov_base = (caddr_t)buf;\r
- iov[1].iov_len = buflen;\r
- if (writev(s, iov, 2) != (INT16SZ + buflen)) {\r
- terrno = errno;\r
- Perror(stderr, "write failed", errno);\r
- badns |= (1 << ns);\r
- res_close();\r
- goto next_ns;\r
- }\r
- /*\r
- * Receive length & response\r
- */\r
-read_len:\r
- cp = ans;\r
- len = INT16SZ;\r
- while ((n = read(s, (char *)cp, (int)len)) > 0) {\r
- cp += n;\r
- len = (u_short)( len - n );\r
- if (len <= 0)\r
- break;\r
- }\r
- if (n <= 0) {\r
- terrno = errno;\r
- Perror(stderr, "read failed", errno);\r
- res_close();\r
- /*\r
- * A long running process might get its TCP\r
- * connection reset if the remote server was\r
- * restarted. Requery the server instead of\r
- * trying a new one. When there is only one\r
- * server, this means that a query might work\r
- * instead of failing. We only allow one reset\r
- * per query to prevent looping.\r
- */\r
- if (terrno == ECONNRESET && !connreset) {\r
- connreset = 1;\r
- res_close();\r
- goto same_ns;\r
- }\r
- res_close();\r
- goto next_ns;\r
- }\r
- resplen = ns_get16(ans);\r
- if (resplen > anssiz) {\r
- Dprint(_res.options & RES_DEBUG,\r
- (stdout, ";; response truncated\n")\r
- );\r
- truncated = 1;\r
- len = (ushort)anssiz;\r
- } else\r
- len = (ushort)resplen;\r
- if (len < HFIXEDSZ) {\r
- /*\r
- * Undersized message.\r
- */\r
- Dprint(_res.options & RES_DEBUG,\r
- (stdout, ";; undersized: %d\n", len));\r
- terrno = EMSGSIZE;\r
- badns |= (1 << ns);\r
- res_close();\r
- goto next_ns;\r
- }\r
- cp = ans;\r
- while (len != 0 &&\r
- (n = read(s, (char *)cp, (int)len)) > 0) {\r
- cp += n;\r
- len = (u_short)( len - n );\r
- }\r
- if (n <= 0) {\r
- terrno = errno;\r
- Perror(stderr, "read(vc)", errno);\r
- res_close();\r
- goto next_ns;\r
- }\r
- if (truncated) {\r
- /*\r
- * Flush rest of answer\r
- * so connection stays in synch.\r
- */\r
- anhp->tc = 1;\r
- len = (ushort)( resplen - anssiz );\r
- while (len != 0) {\r
- char junk[PACKETSZ];\r
-\r
- n = (len > sizeof(junk)\r
- ? sizeof(junk)\r
- : len);\r
- if ((n = read(s, junk, n)) > 0)\r
- len = (u_short)( len - n );\r
- else\r
- break;\r
- }\r
- }\r
- /*\r
- * The calling applicating has bailed out of\r
- * a previous call and failed to arrange to have\r
- * the circuit closed or the server has got\r
- * itself confused. Anyway drop the packet and\r
- * wait for the correct one.\r
- */\r
- if (hp->id != anhp->id) {\r
- DprintQ((_res.options & RES_DEBUG) ||\r
- (_res.pfcode & RES_PRF_REPLY),\r
- (stdout, ";; old answer (unexpected):\n"),\r
- ans, (resplen>anssiz)?anssiz:resplen);\r
- goto read_len;\r
- }\r
- } else {\r
- /*\r
- * Use datagrams.\r
- */\r
-#ifndef NOPOLL\r
- struct pollfd pfd;\r
- int msec;\r
-#endif\r
- struct timeval timeout;\r
- fd_set dsmask, *dsmaskp;\r
- int dsmasklen;\r
- struct sockaddr_in from;\r
- int fromlen;\r
-\r
- if ((s < 0) || vc) {\r
- if (vc)\r
- res_close();\r
- s = socket(PF_INET, SOCK_DGRAM, 0);\r
- if (s < 0) {\r
-#ifndef CAN_RECONNECT\r
- bad_dg_sock:\r
-#endif\r
- terrno = errno;\r
- Perror(stderr, "socket(dg)", errno);\r
- return (-1);\r
- }\r
- connected = 0;\r
- }\r
-#ifndef CANNOT_CONNECT_DGRAM\r
- /*\r
- * On a 4.3BSD+ machine (client and server,\r
- * actually), sending to a nameserver datagram\r
- * port with no nameserver will cause an\r
- * ICMP port unreachable message to be returned.\r
- * If our datagram socket is "connected" to the\r
- * server, we get an ECONNREFUSED error on the next\r
- * socket operation, and select returns if the\r
- * error message is received. We can thus detect\r
- * the absence of a nameserver without timing out.\r
- * If we have sent queries to at least two servers,\r
- * however, we don't want to remain connected,\r
- * as we wish to receive answers from the first\r
- * server to respond.\r
- */\r
- if (_res.nscount == 1 || (try == 0 && ns == 0)) {\r
- /*\r
- * Connect only if we are sure we won't\r
- * receive a response from another server.\r
- */\r
- if (!connected) {\r
- nsap->sin_len = sizeof ( *nsap );\r
- if (connect(s, (struct sockaddr *)nsap,\r
- sizeof *nsap\r
- ) < 0) {\r
- Aerror(stderr,\r
- "connect(dg)",\r
- errno, *nsap);\r
- badns |= (1 << ns);\r
- res_close();\r
- goto next_ns;\r
- }\r
- connected = 1;\r
- }\r
- if (send(s, (char*)buf, buflen, 0) != buflen) {\r
- Perror(stderr, "send", errno);\r
- badns |= (1 << ns);\r
- res_close();\r
- goto next_ns;\r
- }\r
- } else {\r
- /*\r
- * Disconnect if we want to listen\r
- * for responses from more than one server.\r
- */\r
- if (connected) {\r
-#ifdef CAN_RECONNECT\r
- struct sockaddr_in no_addr;\r
-\r
- no_addr.sin_family = AF_INET;\r
- no_addr.sin_addr.s_addr = INADDR_ANY;\r
- no_addr.sin_port = 0;\r
- (void) connect(s,\r
- (struct sockaddr *)\r
- &no_addr,\r
- sizeof no_addr);\r
-#else\r
- int s1 = socket(PF_INET, SOCK_DGRAM,0);\r
- if (s1 < 0)\r
- goto bad_dg_sock;\r
- (void) dup2(s1, s);\r
- (void) close(s1);\r
- Dprint(_res.options & RES_DEBUG,\r
- (stdout, ";; new DG socket\n"))\r
-#endif /* CAN_RECONNECT */\r
- connected = 0;\r
- errno = 0;\r
- }\r
-#endif /* !CANNOT_CONNECT_DGRAM */\r
- if (sendto(s, (char*)buf, buflen, 0,\r
- (struct sockaddr *)nsap,\r
- sizeof *nsap)\r
- != buflen) {\r
- Aerror(stderr, "sendto", errno, *nsap);\r
- badns |= (1 << ns);\r
- res_close();\r
- goto next_ns;\r
- }\r
-#ifndef CANNOT_CONNECT_DGRAM\r
- }\r
-#endif /* !CANNOT_CONNECT_DGRAM */\r
-\r
- /*\r
- * Wait for reply\r
- */\r
-#ifndef NOPOLL\r
- othersyscall:\r
- if (use_poll) {\r
- msec = (_res.retrans << try) * 1000;\r
- if (try > 0)\r
- msec /= _res.nscount;\r
- if (msec <= 0)\r
- msec = 1000;\r
- } else {\r
-#endif\r
- timeout.tv_sec = (_res.retrans << try);\r
- if (try > 0)\r
- timeout.tv_sec /= _res.nscount;\r
- if ((long) timeout.tv_sec <= 0)\r
- timeout.tv_sec = 1;\r
- timeout.tv_usec = 0;\r
-#ifndef NOPOLL\r
- }\r
-#endif\r
- wait:\r
- if (s < 0) {\r
- Perror(stderr, "s out-of-bounds", EMFILE);\r
- res_close();\r
- goto next_ns;\r
- }\r
-#ifndef NOPOLL\r
- if (use_poll) {\r
- struct sigaction sa, osa;\r
- int sigsys_installed = 0;\r
-\r
- pfd.fd = s;\r
- pfd.events = POLLIN;\r
- if (use_poll == 1) {\r
- bzero(&sa, sizeof(sa));\r
- sa.sa_handler = SIG_IGN;\r
- if (sigaction(SIGSYS, &sa, &osa) >= 0)\r
- sigsys_installed = 1;\r
- }\r
- n = poll(&pfd, 1, msec);\r
- if (sigsys_installed == 1) {\r
- int oerrno = errno;\r
- sigaction(SIGSYS, &osa, NULL);\r
- errno = oerrno;\r
- }\r
- /* XXX why does nosys() return EINVAL? */\r
- if (n < 0 && (errno == ENOSYS ||\r
- errno == EINVAL)) {\r
- use_poll = 0;\r
- goto othersyscall;\r
- } else if (use_poll == 1)\r
- use_poll = 2;\r
- if (n < 0) {\r
- if (errno == EINTR)\r
- goto wait;\r
- Perror(stderr, "poll", errno);\r
- res_close();\r
- goto next_ns;\r
- }\r
- } else {\r
-#endif\r
- dsmasklen = howmany(s + 1, NFDBITS) *\r
- sizeof(fd_mask);\r
- if (dsmasklen > sizeof(fd_set)) {\r
- dsmaskp = (fd_set *)malloc(dsmasklen);\r
- if (dsmaskp == NULL) {\r
- res_close();\r
- goto next_ns;\r
- }\r
- } else\r
- dsmaskp = &dsmask;\r
- /* only zero what we need */\r
- memset((char *)dsmaskp, 0, dsmasklen);\r
- FD_SET(s, dsmaskp);\r
- n = select(s + 1, dsmaskp, (fd_set *)NULL,\r
- (fd_set *)NULL, &timeout);\r
- if (dsmaskp != &dsmask)\r
- free(dsmaskp);\r
- if (n < 0) {\r
- if (errno == EINTR)\r
- goto wait;\r
- Perror(stderr, "select", errno);\r
- res_close();\r
- goto next_ns;\r
- }\r
-#ifndef NOPOLL\r
- }\r
-#endif\r
-\r
- if (n == 0) {\r
- /*\r
- * timeout\r
- */\r
- Dprint(_res.options & RES_DEBUG,\r
- (stdout, ";; timeout\n"));\r
- gotsomewhere = 1;\r
- res_close();\r
- goto next_ns;\r
- }\r
- errno = 0;\r
- fromlen = sizeof(struct sockaddr_in);\r
- resplen = (int)recvfrom(s, (char*)ans, anssiz, 0,\r
- (struct sockaddr *)&from, (socklen_t *)&fromlen);\r
- if (resplen <= 0) {\r
- Perror(stderr, "recvfrom", errno);\r
- res_close();\r
- goto next_ns;\r
- }\r
- gotsomewhere = 1;\r
- if (resplen < HFIXEDSZ) {\r
- /*\r
- * Undersized message.\r
- */\r
- Dprint(_res.options & RES_DEBUG,\r
- (stdout, ";; undersized: %d\n",\r
- resplen));\r
- terrno = EMSGSIZE;\r
- badns |= (1 << ns);\r
- res_close();\r
- goto next_ns;\r
- }\r
- if (hp->id != anhp->id) {\r
- /*\r
- * response from old query, ignore it.\r
- * XXX - potential security hazard could\r
- * be detected here.\r
- */\r
- DprintQ((_res.options & RES_DEBUG) ||\r
- (_res.pfcode & RES_PRF_REPLY),\r
- (stdout, ";; old answer:\n"),\r
- ans, (resplen>anssiz)?anssiz:resplen);\r
- goto wait;\r
- }\r
-#ifdef CHECK_SRVR_ADDR\r
- if (!(_res.options & RES_INSECURE1) &&\r
- !res_isourserver(&from)) {\r
- /*\r
- * response from wrong server? ignore it.\r
- * XXX - potential security hazard could\r
- * be detected here.\r
- */\r
- DprintQ((_res.options & RES_DEBUG) ||\r
- (_res.pfcode & RES_PRF_REPLY),\r
- (stdout, ";; not our server:\n"),\r
- ans, (resplen>anssiz)?anssiz:resplen);\r
- goto wait;\r
- }\r
-#endif\r
- if (!(_res.options & RES_INSECURE2) &&\r
- !res_queriesmatch(buf, buf + buflen,\r
- ans, ans + anssiz)) {\r
- /*\r
- * response contains wrong query? ignore it.\r
- * XXX - potential security hazard could\r
- * be detected here.\r
- */\r
- DprintQ((_res.options & RES_DEBUG) ||\r
- (_res.pfcode & RES_PRF_REPLY),\r
- (stdout, ";; wrong query name:\n"),\r
- ans, (resplen>anssiz)?anssiz:resplen);\r
- goto wait;\r
- }\r
- if (anhp->rcode == SERVFAIL ||\r
- anhp->rcode == NOTIMP ||\r
- anhp->rcode == REFUSED) {\r
- DprintQ(_res.options & RES_DEBUG,\r
- (stdout, "server rejected query:\n"),\r
- ans, (resplen>anssiz)?anssiz:resplen);\r
- badns |= (1 << ns);\r
- res_close();\r
- /* don't retry if called from dig */\r
- if (!_res.pfcode)\r
- goto next_ns;\r
- }\r
- if (!(_res.options & RES_IGNTC) && anhp->tc) {\r
- /*\r
- * get rest of answer;\r
- * use TCP with same server.\r
- */\r
- Dprint(_res.options & RES_DEBUG,\r
- (stdout, ";; truncated answer\n"));\r
- v_circuit = 1;\r
- res_close();\r
- goto same_ns;\r
- }\r
- } /*if vc/dg*/\r
- Dprint((_res.options & RES_DEBUG) ||\r
- ((_res.pfcode & RES_PRF_REPLY) &&\r
- (_res.pfcode & RES_PRF_HEAD1)),\r
- (stdout, ";; got answer:\n"));\r
- if((_res.options & RES_DEBUG) ||\r
- (_res.pfcode & RES_PRF_REPLY)) {\r
- __fp_nquery(ans, (resplen>anssiz)?anssiz:resplen, stdout);\r
- }\r
- /*\r
- * If using virtual circuits, we assume that the first server\r
- * is preferred over the rest (i.e. it is on the local\r
- * machine) and only keep that one open.\r
- * If we have temporarily opened a virtual circuit,\r
- * or if we haven't been asked to keep a socket open,\r
- * close the socket.\r
- */\r
- if ((v_circuit && (!(_res.options & RES_USEVC) || ns != 0)) ||\r
- !(_res.options & RES_STAYOPEN)) {\r
- res_close();\r
- }\r
- if (Rhook) {\r
- int done = 0, loops = 0;\r
-\r
- do {\r
- res_sendhookact act;\r
-\r
- act = (*Rhook)(nsap, buf, buflen,\r
- ans, anssiz, &resplen);\r
- switch (act) {\r
- case res_goahead:\r
- case res_done:\r
- done = 1;\r
- break;\r
- case res_nextns:\r
- res_close();\r
- goto next_ns;\r
- case res_modified:\r
- /* give the hook another try */\r
- if (++loops < 42) /*doug adams*/\r
- break;\r
- /*FALLTHROUGH*/\r
- case res_error:\r
- /*FALLTHROUGH*/\r
- default:\r
- return (-1);\r
- }\r
- } while (!done);\r
-\r
- }\r
- return (resplen);\r
- next_ns: ;\r
- } /*foreach ns*/\r
- } /*foreach retry*/\r
- res_close();\r
- if (!v_circuit) {\r
- if (!gotsomewhere)\r
- errno = ECONNREFUSED; /* no nameservers found */\r
- else\r
- errno = ETIMEDOUT; /* no answer obtained */\r
- } else\r
- errno = terrno;\r
- return (-1);\r
-}\r
-\r
-/*\r
- * This routine is for closing the socket if a virtual circuit is used and\r
- * the program wants to close it. This provides support for endhostent()\r
- * which expects to close the socket.\r
- *\r
- * This routine is not expected to be user visible.\r
- */\r
-void\r
-res_close()\r
-{\r
- if (s >= 0) {\r
- (void) close(s);\r
- s = -1;\r
- connected = 0;\r
- vc = 0;\r
- }\r
-}\r