1ee65b806SJan Lentfer /*
2*fbfb85d2SSascha Wildner * @(#)res_send.c 8.1 (Berkeley) 6/4/93
3*fbfb85d2SSascha Wildner * $Id: res_send.c,v 1.18.10.1 2008/01/27 02:06:46 marka Exp $
4*fbfb85d2SSascha Wildner */
5*fbfb85d2SSascha Wildner
6*fbfb85d2SSascha Wildner /*
7ee65b806SJan Lentfer * Copyright (c) 1985, 1989, 1993
8ee65b806SJan Lentfer * The Regents of the University of California. All rights reserved.
9ee65b806SJan Lentfer *
10ee65b806SJan Lentfer * Redistribution and use in source and binary forms, with or without
11ee65b806SJan Lentfer * modification, are permitted provided that the following conditions
12ee65b806SJan Lentfer * are met:
13ee65b806SJan Lentfer * 1. Redistributions of source code must retain the above copyright
14ee65b806SJan Lentfer * notice, this list of conditions and the following disclaimer.
15ee65b806SJan Lentfer * 2. Redistributions in binary form must reproduce the above copyright
16ee65b806SJan Lentfer * notice, this list of conditions and the following disclaimer in the
17ee65b806SJan Lentfer * documentation and/or other materials provided with the distribution.
180f0f9bbdSzrj * 3. Neither the name of the University nor the names of its contributors
19ee65b806SJan Lentfer * may be used to endorse or promote products derived from this software
20ee65b806SJan Lentfer * without specific prior written permission.
21ee65b806SJan Lentfer *
22ee65b806SJan Lentfer * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23ee65b806SJan Lentfer * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24ee65b806SJan Lentfer * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25ee65b806SJan Lentfer * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26ee65b806SJan Lentfer * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27ee65b806SJan Lentfer * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28ee65b806SJan Lentfer * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29ee65b806SJan Lentfer * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30ee65b806SJan Lentfer * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31ee65b806SJan Lentfer * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32ee65b806SJan Lentfer * SUCH DAMAGE.
33ee65b806SJan Lentfer */
34ee65b806SJan Lentfer
35ee65b806SJan Lentfer /*
36ee65b806SJan Lentfer * Portions Copyright (c) 1993 by Digital Equipment Corporation.
37ee65b806SJan Lentfer *
38ee65b806SJan Lentfer * Permission to use, copy, modify, and distribute this software for any
39ee65b806SJan Lentfer * purpose with or without fee is hereby granted, provided that the above
40ee65b806SJan Lentfer * copyright notice and this permission notice appear in all copies, and that
41ee65b806SJan Lentfer * the name of Digital Equipment Corporation not be used in advertising or
42ee65b806SJan Lentfer * publicity pertaining to distribution of the document or software without
43ee65b806SJan Lentfer * specific, written prior permission.
44ee65b806SJan Lentfer *
45ee65b806SJan Lentfer * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
46ee65b806SJan Lentfer * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
47ee65b806SJan Lentfer * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
48ee65b806SJan Lentfer * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
49ee65b806SJan Lentfer * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
50ee65b806SJan Lentfer * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
51ee65b806SJan Lentfer * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
52ee65b806SJan Lentfer * SOFTWARE.
53ee65b806SJan Lentfer */
54ee65b806SJan Lentfer
55ee65b806SJan Lentfer /*
56ee65b806SJan Lentfer * Copyright (c) 2005 by Internet Systems Consortium, Inc. ("ISC")
57ee65b806SJan Lentfer * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
58ee65b806SJan Lentfer *
59ee65b806SJan Lentfer * Permission to use, copy, modify, and distribute this software for any
60ee65b806SJan Lentfer * purpose with or without fee is hereby granted, provided that the above
61ee65b806SJan Lentfer * copyright notice and this permission notice appear in all copies.
62ee65b806SJan Lentfer *
63ee65b806SJan Lentfer * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
64ee65b806SJan Lentfer * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
65ee65b806SJan Lentfer * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
66ee65b806SJan Lentfer * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
67ee65b806SJan Lentfer * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
68ee65b806SJan Lentfer * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
69ee65b806SJan Lentfer * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
70ee65b806SJan Lentfer */
71ee65b806SJan Lentfer
72ee65b806SJan Lentfer /*! \file
73ee65b806SJan Lentfer * \brief
74ee65b806SJan Lentfer * Send query to name server and wait for reply.
75ee65b806SJan Lentfer */
76ee65b806SJan Lentfer
77ee65b806SJan Lentfer #include "port_before.h"
78ee65b806SJan Lentfer #ifndef USE_KQUEUE
79ee65b806SJan Lentfer #include "fd_setsize.h"
80ee65b806SJan Lentfer #endif
81ee65b806SJan Lentfer
82ee65b806SJan Lentfer #ifdef _LIBC
83ee65b806SJan Lentfer #include "namespace.h"
84ee65b806SJan Lentfer #endif
85ee65b806SJan Lentfer #include <sys/types.h>
86ee65b806SJan Lentfer #include <sys/param.h>
87ee65b806SJan Lentfer #include <sys/time.h>
88ee65b806SJan Lentfer #include <sys/socket.h>
89ee65b806SJan Lentfer #include <sys/uio.h>
90ee65b806SJan Lentfer
91ee65b806SJan Lentfer #include <netinet/in.h>
92ee65b806SJan Lentfer #include <arpa/nameser.h>
93ee65b806SJan Lentfer #include <arpa/inet.h>
94ee65b806SJan Lentfer
95ee65b806SJan Lentfer #include <errno.h>
96ee65b806SJan Lentfer #include <netdb.h>
97ee65b806SJan Lentfer #include <resolv.h>
98ee65b806SJan Lentfer #include <signal.h>
99ee65b806SJan Lentfer #include <stdio.h>
100ee65b806SJan Lentfer #include <stdlib.h>
101ee65b806SJan Lentfer #include <string.h>
102ee65b806SJan Lentfer #include <unistd.h>
103ee65b806SJan Lentfer
104ee65b806SJan Lentfer #include "isc/eventlib.h"
105ee65b806SJan Lentfer
106ee65b806SJan Lentfer #include "port_after.h"
107ee65b806SJan Lentfer #ifdef USE_KQUEUE
108ee65b806SJan Lentfer #include <sys/event.h>
109ee65b806SJan Lentfer #else
110ee65b806SJan Lentfer
111ee65b806SJan Lentfer #ifdef USE_POLL
112ee65b806SJan Lentfer #ifdef HAVE_STROPTS_H
113ee65b806SJan Lentfer #include <stropts.h>
114ee65b806SJan Lentfer #endif
115ee65b806SJan Lentfer #include <poll.h>
116ee65b806SJan Lentfer #endif /* USE_POLL */
117ee65b806SJan Lentfer #endif
118ee65b806SJan Lentfer
119ee65b806SJan Lentfer #ifdef _LIBC
120ee65b806SJan Lentfer #include "un-namespace.h"
121ee65b806SJan Lentfer #endif
122ee65b806SJan Lentfer
123ee65b806SJan Lentfer /* Options. Leave them on. */
124ee65b806SJan Lentfer #define DEBUG
125ee65b806SJan Lentfer #include "res_debug.h"
126ee65b806SJan Lentfer #include "res_private.h"
127ee65b806SJan Lentfer
128ee65b806SJan Lentfer #define EXT(res) ((res)->_u._ext)
129ee65b806SJan Lentfer
130ee65b806SJan Lentfer #if !defined(USE_POLL) && !defined(USE_KQUEUE)
131ee65b806SJan Lentfer static const int highestFD = FD_SETSIZE - 1;
132ee65b806SJan Lentfer #endif
133ee65b806SJan Lentfer
134ee65b806SJan Lentfer /* Forward. */
135ee65b806SJan Lentfer
136ee65b806SJan Lentfer static int get_salen __P((const struct sockaddr *));
137ee65b806SJan Lentfer static struct sockaddr * get_nsaddr __P((res_state, size_t));
138ee65b806SJan Lentfer static int send_vc(res_state, const u_char *, int,
139ee65b806SJan Lentfer u_char *, int, int *, int);
140ee65b806SJan Lentfer static int send_dg(res_state,
141ee65b806SJan Lentfer #ifdef USE_KQUEUE
142ee65b806SJan Lentfer int,
143ee65b806SJan Lentfer #endif
144ee65b806SJan Lentfer const u_char *, int,
145ee65b806SJan Lentfer u_char *, int, int *, int, int,
146ee65b806SJan Lentfer int *, int *);
147ee65b806SJan Lentfer static void Aerror(const res_state, FILE *, const char *, int,
148ee65b806SJan Lentfer const struct sockaddr *, int);
149ee65b806SJan Lentfer static void Perror(const res_state, FILE *, const char *, int);
150ee65b806SJan Lentfer static int sock_eq(struct sockaddr *, struct sockaddr *);
151ee65b806SJan Lentfer #if defined(NEED_PSELECT) && !defined(USE_POLL) && !defined(USE_KQUEUE)
152ee65b806SJan Lentfer static int pselect(int, void *, void *, void *,
153ee65b806SJan Lentfer struct timespec *,
154ee65b806SJan Lentfer const sigset_t *);
155ee65b806SJan Lentfer #endif
156ee65b806SJan Lentfer void res_pquery(const res_state, const u_char *, int, FILE *);
157ee65b806SJan Lentfer
158ee65b806SJan Lentfer static const int niflags = NI_NUMERICHOST | NI_NUMERICSERV;
159ee65b806SJan Lentfer
160ee65b806SJan Lentfer /* Public. */
161ee65b806SJan Lentfer
162ee65b806SJan Lentfer /*%
163ee65b806SJan Lentfer * looks up "ina" in _res.ns_addr_list[]
164ee65b806SJan Lentfer *
165ee65b806SJan Lentfer * returns:
166ee65b806SJan Lentfer *\li 0 : not found
167ee65b806SJan Lentfer *\li >0 : found
168ee65b806SJan Lentfer *
169ee65b806SJan Lentfer * author:
170ee65b806SJan Lentfer *\li paul vixie, 29may94
171ee65b806SJan Lentfer */
172ee65b806SJan Lentfer int
res_ourserver_p(const res_state statp,const struct sockaddr * sa)173ee65b806SJan Lentfer res_ourserver_p(const res_state statp, const struct sockaddr *sa) {
174ee65b806SJan Lentfer const struct sockaddr_in *inp, *srv;
175ee65b806SJan Lentfer const struct sockaddr_in6 *in6p, *srv6;
176ee65b806SJan Lentfer int ns;
177ee65b806SJan Lentfer
178ee65b806SJan Lentfer switch (sa->sa_family) {
179ee65b806SJan Lentfer case AF_INET:
180ee65b806SJan Lentfer inp = (const struct sockaddr_in *)sa;
181ee65b806SJan Lentfer for (ns = 0; ns < statp->nscount; ns++) {
182ee65b806SJan Lentfer srv = (struct sockaddr_in *)get_nsaddr(statp, ns);
183ee65b806SJan Lentfer if (srv->sin_family == inp->sin_family &&
184ee65b806SJan Lentfer srv->sin_port == inp->sin_port &&
185ee65b806SJan Lentfer (srv->sin_addr.s_addr == INADDR_ANY ||
186ee65b806SJan Lentfer srv->sin_addr.s_addr == inp->sin_addr.s_addr))
187ee65b806SJan Lentfer return (1);
188ee65b806SJan Lentfer }
189ee65b806SJan Lentfer break;
190ee65b806SJan Lentfer case AF_INET6:
191ee65b806SJan Lentfer if (EXT(statp).ext == NULL)
192ee65b806SJan Lentfer break;
193ee65b806SJan Lentfer in6p = (const struct sockaddr_in6 *)sa;
194ee65b806SJan Lentfer for (ns = 0; ns < statp->nscount; ns++) {
195ee65b806SJan Lentfer srv6 = (struct sockaddr_in6 *)get_nsaddr(statp, ns);
196ee65b806SJan Lentfer if (srv6->sin6_family == in6p->sin6_family &&
197ee65b806SJan Lentfer srv6->sin6_port == in6p->sin6_port &&
198ee65b806SJan Lentfer #ifdef HAVE_SIN6_SCOPE_ID
199ee65b806SJan Lentfer (srv6->sin6_scope_id == 0 ||
200ee65b806SJan Lentfer srv6->sin6_scope_id == in6p->sin6_scope_id) &&
201ee65b806SJan Lentfer #endif
202ee65b806SJan Lentfer (IN6_IS_ADDR_UNSPECIFIED(&srv6->sin6_addr) ||
203ee65b806SJan Lentfer IN6_ARE_ADDR_EQUAL(&srv6->sin6_addr, &in6p->sin6_addr)))
204ee65b806SJan Lentfer return (1);
205ee65b806SJan Lentfer }
206ee65b806SJan Lentfer break;
207ee65b806SJan Lentfer default:
208ee65b806SJan Lentfer break;
209ee65b806SJan Lentfer }
210ee65b806SJan Lentfer return (0);
211ee65b806SJan Lentfer }
212ee65b806SJan Lentfer
213ee65b806SJan Lentfer /*%
214ee65b806SJan Lentfer * look for (name,type,class) in the query section of packet (buf,eom)
215ee65b806SJan Lentfer *
216ee65b806SJan Lentfer * requires:
217ee65b806SJan Lentfer *\li buf + HFIXEDSZ <= eom
218ee65b806SJan Lentfer *
219ee65b806SJan Lentfer * returns:
220ee65b806SJan Lentfer *\li -1 : format error
221ee65b806SJan Lentfer *\li 0 : not found
222ee65b806SJan Lentfer *\li >0 : found
223ee65b806SJan Lentfer *
224ee65b806SJan Lentfer * author:
225ee65b806SJan Lentfer *\li paul vixie, 29may94
226ee65b806SJan Lentfer */
227ee65b806SJan Lentfer int
res_nameinquery(const char * name,int type,int class,const u_char * buf,const u_char * eom)228ee65b806SJan Lentfer res_nameinquery(const char *name, int type, int class,
229ee65b806SJan Lentfer const u_char *buf, const u_char *eom)
230ee65b806SJan Lentfer {
231ee65b806SJan Lentfer const u_char *cp = buf + HFIXEDSZ;
232ee65b806SJan Lentfer int qdcount = ntohs(((const HEADER*)buf)->qdcount);
233ee65b806SJan Lentfer
234ee65b806SJan Lentfer while (qdcount-- > 0) {
235ee65b806SJan Lentfer char tname[MAXDNAME+1];
236ee65b806SJan Lentfer int n, ttype, tclass;
237ee65b806SJan Lentfer
238ee65b806SJan Lentfer n = dn_expand(buf, eom, cp, tname, sizeof tname);
239ee65b806SJan Lentfer if (n < 0)
240ee65b806SJan Lentfer return (-1);
241ee65b806SJan Lentfer cp += n;
242ee65b806SJan Lentfer if (cp + 2 * INT16SZ > eom)
243ee65b806SJan Lentfer return (-1);
244ee65b806SJan Lentfer ttype = ns_get16(cp); cp += INT16SZ;
245ee65b806SJan Lentfer tclass = ns_get16(cp); cp += INT16SZ;
246ee65b806SJan Lentfer if (ttype == type && tclass == class &&
247ee65b806SJan Lentfer ns_samename(tname, name) == 1)
248ee65b806SJan Lentfer return (1);
249ee65b806SJan Lentfer }
250ee65b806SJan Lentfer return (0);
251ee65b806SJan Lentfer }
252ee65b806SJan Lentfer
253ee65b806SJan Lentfer /*%
254ee65b806SJan Lentfer * is there a 1:1 mapping of (name,type,class)
255ee65b806SJan Lentfer * in (buf1,eom1) and (buf2,eom2)?
256ee65b806SJan Lentfer *
257ee65b806SJan Lentfer * returns:
258ee65b806SJan Lentfer *\li -1 : format error
259ee65b806SJan Lentfer *\li 0 : not a 1:1 mapping
260ee65b806SJan Lentfer *\li >0 : is a 1:1 mapping
261ee65b806SJan Lentfer *
262ee65b806SJan Lentfer * author:
263ee65b806SJan Lentfer *\li paul vixie, 29may94
264ee65b806SJan Lentfer */
265ee65b806SJan Lentfer int
res_queriesmatch(const u_char * buf1,const u_char * eom1,const u_char * buf2,const u_char * eom2)266ee65b806SJan Lentfer res_queriesmatch(const u_char *buf1, const u_char *eom1,
267ee65b806SJan Lentfer const u_char *buf2, const u_char *eom2)
268ee65b806SJan Lentfer {
269ee65b806SJan Lentfer const u_char *cp = buf1 + HFIXEDSZ;
270ee65b806SJan Lentfer int qdcount = ntohs(((const HEADER*)buf1)->qdcount);
271ee65b806SJan Lentfer
272ee65b806SJan Lentfer if (buf1 + HFIXEDSZ > eom1 || buf2 + HFIXEDSZ > eom2)
273ee65b806SJan Lentfer return (-1);
274ee65b806SJan Lentfer
275ee65b806SJan Lentfer /*
276ee65b806SJan Lentfer * Only header section present in replies to
277ee65b806SJan Lentfer * dynamic update packets.
278ee65b806SJan Lentfer */
279ee65b806SJan Lentfer if ((((const HEADER *)buf1)->opcode == ns_o_update) &&
280ee65b806SJan Lentfer (((const HEADER *)buf2)->opcode == ns_o_update))
281ee65b806SJan Lentfer return (1);
282ee65b806SJan Lentfer
283ee65b806SJan Lentfer if (qdcount != ntohs(((const HEADER*)buf2)->qdcount))
284ee65b806SJan Lentfer return (0);
285ee65b806SJan Lentfer while (qdcount-- > 0) {
286ee65b806SJan Lentfer char tname[MAXDNAME+1];
287ee65b806SJan Lentfer int n, ttype, tclass;
288ee65b806SJan Lentfer
289ee65b806SJan Lentfer n = dn_expand(buf1, eom1, cp, tname, sizeof tname);
290ee65b806SJan Lentfer if (n < 0)
291ee65b806SJan Lentfer return (-1);
292ee65b806SJan Lentfer cp += n;
293ee65b806SJan Lentfer if (cp + 2 * INT16SZ > eom1)
294ee65b806SJan Lentfer return (-1);
295ee65b806SJan Lentfer ttype = ns_get16(cp); cp += INT16SZ;
296ee65b806SJan Lentfer tclass = ns_get16(cp); cp += INT16SZ;
297ee65b806SJan Lentfer if (!res_nameinquery(tname, ttype, tclass, buf2, eom2))
298ee65b806SJan Lentfer return (0);
299ee65b806SJan Lentfer }
300ee65b806SJan Lentfer return (1);
301ee65b806SJan Lentfer }
302ee65b806SJan Lentfer
303ee65b806SJan Lentfer int
res_nsend(res_state statp,const u_char * buf,int buflen,u_char * ans,int anssiz)304ee65b806SJan Lentfer res_nsend(res_state statp,
305ee65b806SJan Lentfer const u_char *buf, int buflen, u_char *ans, int anssiz)
306ee65b806SJan Lentfer {
307ee65b806SJan Lentfer int gotsomewhere, terrno, tries, v_circuit, resplen, ns, n;
308ee65b806SJan Lentfer #ifdef USE_KQUEUE
309ee65b806SJan Lentfer int kq;
310ee65b806SJan Lentfer #endif
311ee65b806SJan Lentfer char abuf[NI_MAXHOST];
312ee65b806SJan Lentfer
313ee65b806SJan Lentfer /* No name servers or res_init() failure */
314ee65b806SJan Lentfer if (statp->nscount == 0 || EXT(statp).ext == NULL) {
315ee65b806SJan Lentfer errno = ESRCH;
316ee65b806SJan Lentfer return (-1);
317ee65b806SJan Lentfer }
318ee65b806SJan Lentfer if (anssiz < HFIXEDSZ) {
319ee65b806SJan Lentfer errno = EINVAL;
320ee65b806SJan Lentfer return (-1);
321ee65b806SJan Lentfer }
322ee65b806SJan Lentfer DprintQ((statp->options & RES_DEBUG) || (statp->pfcode & RES_PRF_QUERY),
323ee65b806SJan Lentfer (stdout, ";; res_send()\n"), buf, buflen);
324ee65b806SJan Lentfer v_circuit = (statp->options & RES_USEVC) || buflen > PACKETSZ;
325ee65b806SJan Lentfer gotsomewhere = 0;
326ee65b806SJan Lentfer terrno = ETIMEDOUT;
327ee65b806SJan Lentfer
328ee65b806SJan Lentfer #ifdef USE_KQUEUE
329ee65b806SJan Lentfer if ((kq = kqueue()) < 0) {
330ee65b806SJan Lentfer Perror(statp, stderr, "kqueue", errno);
331ee65b806SJan Lentfer return (-1);
332ee65b806SJan Lentfer }
333ee65b806SJan Lentfer #endif
334ee65b806SJan Lentfer
335ee65b806SJan Lentfer /*
336ee65b806SJan Lentfer * If the ns_addr_list in the resolver context has changed, then
337ee65b806SJan Lentfer * invalidate our cached copy and the associated timing data.
338ee65b806SJan Lentfer */
339ee65b806SJan Lentfer if (EXT(statp).nscount != 0) {
340ee65b806SJan Lentfer int needclose = 0;
341ee65b806SJan Lentfer struct sockaddr_storage peer;
342ee65b806SJan Lentfer ISC_SOCKLEN_T peerlen;
343ee65b806SJan Lentfer
344ee65b806SJan Lentfer if (EXT(statp).nscount != statp->nscount)
345ee65b806SJan Lentfer needclose++;
346ee65b806SJan Lentfer else
347ee65b806SJan Lentfer for (ns = 0; ns < statp->nscount; ns++) {
348ee65b806SJan Lentfer if (statp->nsaddr_list[ns].sin_family &&
349ee65b806SJan Lentfer !sock_eq((struct sockaddr *)&statp->nsaddr_list[ns],
350ee65b806SJan Lentfer (struct sockaddr *)&EXT(statp).ext->nsaddrs[ns])) {
351ee65b806SJan Lentfer needclose++;
352ee65b806SJan Lentfer break;
353ee65b806SJan Lentfer }
354ee65b806SJan Lentfer
355ee65b806SJan Lentfer if (EXT(statp).nssocks[ns] == -1)
356ee65b806SJan Lentfer continue;
357ee65b806SJan Lentfer peerlen = sizeof(peer);
358ee65b806SJan Lentfer #ifndef _LIBC
359ee65b806SJan Lentfer if (getsockname(EXT(statp).nssocks[ns],
360ee65b806SJan Lentfer #else
361ee65b806SJan Lentfer if (_getsockname(EXT(statp).nssocks[ns],
362ee65b806SJan Lentfer #endif
363ee65b806SJan Lentfer (struct sockaddr *)&peer, &peerlen) < 0) {
364ee65b806SJan Lentfer needclose++;
365ee65b806SJan Lentfer break;
366ee65b806SJan Lentfer }
367ee65b806SJan Lentfer if (!sock_eq((struct sockaddr *)&peer,
368ee65b806SJan Lentfer get_nsaddr(statp, ns))) {
369ee65b806SJan Lentfer needclose++;
370ee65b806SJan Lentfer break;
371ee65b806SJan Lentfer }
372ee65b806SJan Lentfer }
373ee65b806SJan Lentfer if (needclose) {
374ee65b806SJan Lentfer res_nclose(statp);
375ee65b806SJan Lentfer EXT(statp).nscount = 0;
376ee65b806SJan Lentfer }
377ee65b806SJan Lentfer }
378ee65b806SJan Lentfer
379ee65b806SJan Lentfer /*
380ee65b806SJan Lentfer * Maybe initialize our private copy of the ns_addr_list.
381ee65b806SJan Lentfer */
382ee65b806SJan Lentfer if (EXT(statp).nscount == 0) {
383ee65b806SJan Lentfer for (ns = 0; ns < statp->nscount; ns++) {
384ee65b806SJan Lentfer EXT(statp).nstimes[ns] = RES_MAXTIME;
385ee65b806SJan Lentfer EXT(statp).nssocks[ns] = -1;
386ee65b806SJan Lentfer if (!statp->nsaddr_list[ns].sin_family)
387ee65b806SJan Lentfer continue;
388ee65b806SJan Lentfer EXT(statp).ext->nsaddrs[ns].sin =
389ee65b806SJan Lentfer statp->nsaddr_list[ns];
390ee65b806SJan Lentfer }
391ee65b806SJan Lentfer EXT(statp).nscount = statp->nscount;
392ee65b806SJan Lentfer }
393ee65b806SJan Lentfer
394ee65b806SJan Lentfer /*
395ee65b806SJan Lentfer * Some resolvers want to even out the load on their nameservers.
396ee65b806SJan Lentfer * Note that RES_BLAST overrides RES_ROTATE.
397ee65b806SJan Lentfer */
398ee65b806SJan Lentfer if ((statp->options & RES_ROTATE) != 0U &&
399ee65b806SJan Lentfer (statp->options & RES_BLAST) == 0U) {
400ee65b806SJan Lentfer union res_sockaddr_union inu;
401ee65b806SJan Lentfer struct sockaddr_in ina;
402ee65b806SJan Lentfer int lastns = statp->nscount - 1;
403ee65b806SJan Lentfer int fd;
404ee65b806SJan Lentfer u_int16_t nstime;
405ee65b806SJan Lentfer
406ee65b806SJan Lentfer if (EXT(statp).ext != NULL)
407ee65b806SJan Lentfer inu = EXT(statp).ext->nsaddrs[0];
408ee65b806SJan Lentfer ina = statp->nsaddr_list[0];
409ee65b806SJan Lentfer fd = EXT(statp).nssocks[0];
410ee65b806SJan Lentfer nstime = EXT(statp).nstimes[0];
411ee65b806SJan Lentfer for (ns = 0; ns < lastns; ns++) {
412ee65b806SJan Lentfer if (EXT(statp).ext != NULL)
413ee65b806SJan Lentfer EXT(statp).ext->nsaddrs[ns] =
414ee65b806SJan Lentfer EXT(statp).ext->nsaddrs[ns + 1];
415ee65b806SJan Lentfer statp->nsaddr_list[ns] = statp->nsaddr_list[ns + 1];
416ee65b806SJan Lentfer EXT(statp).nssocks[ns] = EXT(statp).nssocks[ns + 1];
417ee65b806SJan Lentfer EXT(statp).nstimes[ns] = EXT(statp).nstimes[ns + 1];
418ee65b806SJan Lentfer }
419ee65b806SJan Lentfer if (EXT(statp).ext != NULL)
420ee65b806SJan Lentfer EXT(statp).ext->nsaddrs[lastns] = inu;
421ee65b806SJan Lentfer statp->nsaddr_list[lastns] = ina;
422ee65b806SJan Lentfer EXT(statp).nssocks[lastns] = fd;
423ee65b806SJan Lentfer EXT(statp).nstimes[lastns] = nstime;
424ee65b806SJan Lentfer }
425ee65b806SJan Lentfer
426ee65b806SJan Lentfer /*
427ee65b806SJan Lentfer * Send request, RETRY times, or until successful.
428ee65b806SJan Lentfer */
429ee65b806SJan Lentfer for (tries = 0; tries < statp->retry; tries++) {
430ee65b806SJan Lentfer for (ns = 0; ns < statp->nscount; ns++) {
431ee65b806SJan Lentfer struct sockaddr *nsap;
432ee65b806SJan Lentfer int nsaplen;
433ee65b806SJan Lentfer nsap = get_nsaddr(statp, ns);
434ee65b806SJan Lentfer nsaplen = get_salen(nsap);
435ee65b806SJan Lentfer statp->_flags &= ~RES_F_LASTMASK;
436ee65b806SJan Lentfer statp->_flags |= (ns << RES_F_LASTSHIFT);
437ee65b806SJan Lentfer same_ns:
438ee65b806SJan Lentfer if (statp->qhook) {
439ee65b806SJan Lentfer int done = 0, loops = 0;
440ee65b806SJan Lentfer
441ee65b806SJan Lentfer do {
442ee65b806SJan Lentfer res_sendhookact act;
443ee65b806SJan Lentfer
444ee65b806SJan Lentfer act = (*statp->qhook)(&nsap, &buf, &buflen,
445ee65b806SJan Lentfer ans, anssiz, &resplen);
446ee65b806SJan Lentfer switch (act) {
447ee65b806SJan Lentfer case res_goahead:
448ee65b806SJan Lentfer done = 1;
449ee65b806SJan Lentfer break;
450ee65b806SJan Lentfer case res_nextns:
451ee65b806SJan Lentfer res_nclose(statp);
452ee65b806SJan Lentfer goto next_ns;
453ee65b806SJan Lentfer case res_done:
454ee65b806SJan Lentfer #ifdef USE_KQUEUE
455ee65b806SJan Lentfer _close(kq);
456ee65b806SJan Lentfer #endif
457ee65b806SJan Lentfer return (resplen);
458ee65b806SJan Lentfer case res_modified:
459ee65b806SJan Lentfer /* give the hook another try */
460ee65b806SJan Lentfer if (++loops < 42) /*doug adams*/
461ee65b806SJan Lentfer break;
462ee65b806SJan Lentfer /*FALLTHROUGH*/
463ee65b806SJan Lentfer case res_error:
464ee65b806SJan Lentfer /*FALLTHROUGH*/
465ee65b806SJan Lentfer default:
466ee65b806SJan Lentfer goto fail;
467ee65b806SJan Lentfer }
468ee65b806SJan Lentfer } while (!done);
469ee65b806SJan Lentfer }
470ee65b806SJan Lentfer
471ee65b806SJan Lentfer Dprint(((statp->options & RES_DEBUG) &&
472ee65b806SJan Lentfer getnameinfo(nsap, nsaplen, abuf, sizeof(abuf),
473ee65b806SJan Lentfer NULL, 0, niflags) == 0),
474ee65b806SJan Lentfer (stdout, ";; Querying server (# %d) address = %s\n",
475ee65b806SJan Lentfer ns + 1, abuf));
476ee65b806SJan Lentfer
477ee65b806SJan Lentfer
478ee65b806SJan Lentfer if (v_circuit) {
479ee65b806SJan Lentfer /* Use VC; at most one attempt per server. */
480ee65b806SJan Lentfer tries = statp->retry;
481ee65b806SJan Lentfer n = send_vc(statp, buf, buflen, ans, anssiz, &terrno,
482ee65b806SJan Lentfer ns);
483ee65b806SJan Lentfer if (n < 0)
484ee65b806SJan Lentfer goto fail;
485ee65b806SJan Lentfer if (n == 0)
486ee65b806SJan Lentfer goto next_ns;
487ee65b806SJan Lentfer resplen = n;
488ee65b806SJan Lentfer } else {
489ee65b806SJan Lentfer /* Use datagrams. */
490ee65b806SJan Lentfer n = send_dg(statp,
491ee65b806SJan Lentfer #ifdef USE_KQUEUE
492ee65b806SJan Lentfer kq,
493ee65b806SJan Lentfer #endif
494ee65b806SJan Lentfer buf, buflen, ans, anssiz, &terrno,
495ee65b806SJan Lentfer ns, tries, &v_circuit, &gotsomewhere);
496ee65b806SJan Lentfer if (n < 0)
497ee65b806SJan Lentfer goto fail;
498ee65b806SJan Lentfer if (n == 0)
499ee65b806SJan Lentfer goto next_ns;
500ee65b806SJan Lentfer if (v_circuit)
501ee65b806SJan Lentfer goto same_ns;
502ee65b806SJan Lentfer resplen = n;
503ee65b806SJan Lentfer }
504ee65b806SJan Lentfer
505ee65b806SJan Lentfer Dprint((statp->options & RES_DEBUG) ||
506ee65b806SJan Lentfer ((statp->pfcode & RES_PRF_REPLY) &&
507ee65b806SJan Lentfer (statp->pfcode & RES_PRF_HEAD1)),
508ee65b806SJan Lentfer (stdout, ";; got answer:\n"));
509ee65b806SJan Lentfer
510ee65b806SJan Lentfer DprintQ((statp->options & RES_DEBUG) ||
511ee65b806SJan Lentfer (statp->pfcode & RES_PRF_REPLY),
512ee65b806SJan Lentfer (stdout, "%s", ""),
513ee65b806SJan Lentfer ans, (resplen > anssiz) ? anssiz : resplen);
514ee65b806SJan Lentfer
515ee65b806SJan Lentfer /*
516ee65b806SJan Lentfer * If we have temporarily opened a virtual circuit,
517ee65b806SJan Lentfer * or if we haven't been asked to keep a socket open,
518ee65b806SJan Lentfer * close the socket.
519ee65b806SJan Lentfer */
520ee65b806SJan Lentfer if ((v_circuit && (statp->options & RES_USEVC) == 0U) ||
521ee65b806SJan Lentfer (statp->options & RES_STAYOPEN) == 0U) {
522ee65b806SJan Lentfer res_nclose(statp);
523ee65b806SJan Lentfer }
524ee65b806SJan Lentfer if (statp->rhook) {
525ee65b806SJan Lentfer int done = 0, loops = 0;
526ee65b806SJan Lentfer
527ee65b806SJan Lentfer do {
528ee65b806SJan Lentfer res_sendhookact act;
529ee65b806SJan Lentfer
530ee65b806SJan Lentfer act = (*statp->rhook)(nsap, buf, buflen,
531ee65b806SJan Lentfer ans, anssiz, &resplen);
532ee65b806SJan Lentfer switch (act) {
533ee65b806SJan Lentfer case res_goahead:
534ee65b806SJan Lentfer case res_done:
535ee65b806SJan Lentfer done = 1;
536ee65b806SJan Lentfer break;
537ee65b806SJan Lentfer case res_nextns:
538ee65b806SJan Lentfer res_nclose(statp);
539ee65b806SJan Lentfer goto next_ns;
540ee65b806SJan Lentfer case res_modified:
541ee65b806SJan Lentfer /* give the hook another try */
542ee65b806SJan Lentfer if (++loops < 42) /*doug adams*/
543ee65b806SJan Lentfer break;
544ee65b806SJan Lentfer /*FALLTHROUGH*/
545ee65b806SJan Lentfer case res_error:
546ee65b806SJan Lentfer /*FALLTHROUGH*/
547ee65b806SJan Lentfer default:
548ee65b806SJan Lentfer goto fail;
549ee65b806SJan Lentfer }
550ee65b806SJan Lentfer } while (!done);
551ee65b806SJan Lentfer
552ee65b806SJan Lentfer }
553ee65b806SJan Lentfer #ifdef USE_KQUEUE
554ee65b806SJan Lentfer _close(kq);
555ee65b806SJan Lentfer #endif
556ee65b806SJan Lentfer return (resplen);
557ee65b806SJan Lentfer next_ns: ;
558ee65b806SJan Lentfer } /*foreach ns*/
559ee65b806SJan Lentfer } /*foreach retry*/
560ee65b806SJan Lentfer res_nclose(statp);
561ee65b806SJan Lentfer #ifdef USE_KQUEUE
562ee65b806SJan Lentfer _close(kq);
563ee65b806SJan Lentfer #endif
564ee65b806SJan Lentfer if (!v_circuit) {
565ee65b806SJan Lentfer if (!gotsomewhere)
566ee65b806SJan Lentfer errno = ECONNREFUSED; /*%< no nameservers found */
567ee65b806SJan Lentfer else
568ee65b806SJan Lentfer errno = ETIMEDOUT; /*%< no answer obtained */
569ee65b806SJan Lentfer } else
570ee65b806SJan Lentfer errno = terrno;
571ee65b806SJan Lentfer return (-1);
572ee65b806SJan Lentfer fail:
573ee65b806SJan Lentfer res_nclose(statp);
574ee65b806SJan Lentfer return (-1);
575ee65b806SJan Lentfer }
576ee65b806SJan Lentfer
577ee65b806SJan Lentfer /* Private */
578ee65b806SJan Lentfer
579ee65b806SJan Lentfer static int
get_salen(const struct sockaddr * sa)5805d7d35b1SSascha Wildner get_salen(const struct sockaddr *sa)
581ee65b806SJan Lentfer {
582ee65b806SJan Lentfer
583ee65b806SJan Lentfer #ifdef HAVE_SA_LEN
584ee65b806SJan Lentfer /* There are people do not set sa_len. Be forgiving to them. */
585ee65b806SJan Lentfer if (sa->sa_len)
586ee65b806SJan Lentfer return (sa->sa_len);
587ee65b806SJan Lentfer #endif
588ee65b806SJan Lentfer
589ee65b806SJan Lentfer if (sa->sa_family == AF_INET)
590ee65b806SJan Lentfer return (sizeof(struct sockaddr_in));
591ee65b806SJan Lentfer else if (sa->sa_family == AF_INET6)
592ee65b806SJan Lentfer return (sizeof(struct sockaddr_in6));
593ee65b806SJan Lentfer else
594ee65b806SJan Lentfer return (0); /*%< unknown, die on connect */
595ee65b806SJan Lentfer }
596ee65b806SJan Lentfer
597ee65b806SJan Lentfer /*%
598ee65b806SJan Lentfer * pick appropriate nsaddr_list for use. see res_init() for initialization.
599ee65b806SJan Lentfer */
600ee65b806SJan Lentfer static struct sockaddr *
get_nsaddr(res_state statp,size_t n)6015d7d35b1SSascha Wildner get_nsaddr(res_state statp, size_t n)
602ee65b806SJan Lentfer {
603ee65b806SJan Lentfer
604ee65b806SJan Lentfer if (!statp->nsaddr_list[n].sin_family && EXT(statp).ext) {
605ee65b806SJan Lentfer /*
606ee65b806SJan Lentfer * - EXT(statp).ext->nsaddrs[n] holds an address that is larger
607ee65b806SJan Lentfer * than struct sockaddr, and
608ee65b806SJan Lentfer * - user code did not update statp->nsaddr_list[n].
609ee65b806SJan Lentfer */
610ee65b806SJan Lentfer return (struct sockaddr *)(void *)&EXT(statp).ext->nsaddrs[n];
611ee65b806SJan Lentfer } else {
612ee65b806SJan Lentfer /*
613ee65b806SJan Lentfer * - user code updated statp->nsaddr_list[n], or
614ee65b806SJan Lentfer * - statp->nsaddr_list[n] has the same content as
615ee65b806SJan Lentfer * EXT(statp).ext->nsaddrs[n].
616ee65b806SJan Lentfer */
617ee65b806SJan Lentfer return (struct sockaddr *)(void *)&statp->nsaddr_list[n];
618ee65b806SJan Lentfer }
619ee65b806SJan Lentfer }
620ee65b806SJan Lentfer
621ee65b806SJan Lentfer static int
send_vc(res_state statp,const u_char * buf,int buflen,u_char * ans,int anssiz,int * terrno,int ns)622ee65b806SJan Lentfer send_vc(res_state statp,
623ee65b806SJan Lentfer const u_char *buf, int buflen, u_char *ans, int anssiz,
624ee65b806SJan Lentfer int *terrno, int ns)
625ee65b806SJan Lentfer {
626ee65b806SJan Lentfer const HEADER *hp = (const HEADER *) buf;
627ee65b806SJan Lentfer HEADER *anhp = (HEADER *) ans;
628ee65b806SJan Lentfer struct sockaddr *nsap;
629ee65b806SJan Lentfer int nsaplen;
630ee65b806SJan Lentfer int truncating, connreset, resplen, n;
631ee65b806SJan Lentfer struct iovec iov[2];
632ee65b806SJan Lentfer u_short len;
633ee65b806SJan Lentfer u_char *cp;
634ee65b806SJan Lentfer void *tmp;
635ee65b806SJan Lentfer #ifdef SO_NOSIGPIPE
636ee65b806SJan Lentfer int on = 1;
637ee65b806SJan Lentfer #endif
638ee65b806SJan Lentfer
639ee65b806SJan Lentfer nsap = get_nsaddr(statp, ns);
640ee65b806SJan Lentfer nsaplen = get_salen(nsap);
641ee65b806SJan Lentfer
642ee65b806SJan Lentfer connreset = 0;
643ee65b806SJan Lentfer same_ns:
644ee65b806SJan Lentfer truncating = 0;
645ee65b806SJan Lentfer
646ee65b806SJan Lentfer /* Are we still talking to whom we want to talk to? */
647ee65b806SJan Lentfer if (statp->_vcsock >= 0 && (statp->_flags & RES_F_VC) != 0) {
648ee65b806SJan Lentfer struct sockaddr_storage peer;
649ee65b806SJan Lentfer ISC_SOCKLEN_T size = sizeof peer;
650ee65b806SJan Lentfer #ifndef _LIBC
651ee65b806SJan Lentfer if (getpeername(statp->_vcsock,
652ee65b806SJan Lentfer #else
653ee65b806SJan Lentfer if (_getpeername(statp->_vcsock,
654ee65b806SJan Lentfer #endif
655ee65b806SJan Lentfer (struct sockaddr *)&peer, &size) < 0 ||
656ee65b806SJan Lentfer !sock_eq((struct sockaddr *)&peer, nsap)) {
657ee65b806SJan Lentfer res_nclose(statp);
658ee65b806SJan Lentfer statp->_flags &= ~RES_F_VC;
659ee65b806SJan Lentfer }
660ee65b806SJan Lentfer }
661ee65b806SJan Lentfer
662ee65b806SJan Lentfer if (statp->_vcsock < 0 || (statp->_flags & RES_F_VC) == 0) {
663ee65b806SJan Lentfer if (statp->_vcsock >= 0)
664ee65b806SJan Lentfer res_nclose(statp);
665ee65b806SJan Lentfer #ifndef _LIBC
666ee65b806SJan Lentfer statp->_vcsock = socket(nsap->sa_family, SOCK_STREAM, 0);
667ee65b806SJan Lentfer #else
668ee65b806SJan Lentfer statp->_vcsock = _socket(nsap->sa_family, SOCK_STREAM, 0);
669ee65b806SJan Lentfer #endif
670ee65b806SJan Lentfer #if !defined(USE_POLL) && !defined(USE_KQUEUE)
671ee65b806SJan Lentfer if (statp->_vcsock > highestFD) {
672ee65b806SJan Lentfer res_nclose(statp);
673ee65b806SJan Lentfer errno = ENOTSOCK;
674ee65b806SJan Lentfer }
675ee65b806SJan Lentfer #endif
676ee65b806SJan Lentfer if (statp->_vcsock < 0) {
677ee65b806SJan Lentfer switch (errno) {
678ee65b806SJan Lentfer case EPROTONOSUPPORT:
679ee65b806SJan Lentfer #ifdef EPFNOSUPPORT
680ee65b806SJan Lentfer case EPFNOSUPPORT:
681ee65b806SJan Lentfer #endif
682ee65b806SJan Lentfer case EAFNOSUPPORT:
683ee65b806SJan Lentfer Perror(statp, stderr, "socket(vc)", errno);
684ee65b806SJan Lentfer return (0);
685ee65b806SJan Lentfer default:
686ee65b806SJan Lentfer *terrno = errno;
687ee65b806SJan Lentfer Perror(statp, stderr, "socket(vc)", errno);
688ee65b806SJan Lentfer return (-1);
689ee65b806SJan Lentfer }
690ee65b806SJan Lentfer }
691ee65b806SJan Lentfer #ifdef SO_NOSIGPIPE
692ee65b806SJan Lentfer /*
693ee65b806SJan Lentfer * Disable generation of SIGPIPE when writing to a closed
694ee65b806SJan Lentfer * socket. Write should return -1 and set errno to EPIPE
695ee65b806SJan Lentfer * instead.
696ee65b806SJan Lentfer *
697ee65b806SJan Lentfer * Push on even if setsockopt(SO_NOSIGPIPE) fails.
698ee65b806SJan Lentfer */
699e3b4bb34SSascha Wildner (void)_setsockopt(statp->_vcsock, SOL_SOCKET, SO_NOSIGPIPE, &on,
700ee65b806SJan Lentfer sizeof(on));
701ee65b806SJan Lentfer #endif
702ee65b806SJan Lentfer errno = 0;
703ee65b806SJan Lentfer #ifndef _LIBC
704ee65b806SJan Lentfer if (connect(statp->_vcsock, nsap, nsaplen) < 0) {
705ee65b806SJan Lentfer #else
706ee65b806SJan Lentfer if (_connect(statp->_vcsock, nsap, nsaplen) < 0) {
707ee65b806SJan Lentfer #endif
708ee65b806SJan Lentfer *terrno = errno;
709ee65b806SJan Lentfer Aerror(statp, stderr, "connect/vc", errno, nsap,
710ee65b806SJan Lentfer nsaplen);
711ee65b806SJan Lentfer res_nclose(statp);
712ee65b806SJan Lentfer return (0);
713ee65b806SJan Lentfer }
714ee65b806SJan Lentfer statp->_flags |= RES_F_VC;
715ee65b806SJan Lentfer }
716ee65b806SJan Lentfer
717ee65b806SJan Lentfer /*
718ee65b806SJan Lentfer * Send length & message
719ee65b806SJan Lentfer */
720ee65b806SJan Lentfer ns_put16((u_short)buflen, (u_char*)&len);
721ee65b806SJan Lentfer iov[0] = evConsIovec(&len, INT16SZ);
722ee65b806SJan Lentfer DE_CONST(buf, tmp);
723ee65b806SJan Lentfer iov[1] = evConsIovec(tmp, buflen);
724ee65b806SJan Lentfer #ifndef _LIBC
725ee65b806SJan Lentfer if (writev(statp->_vcsock, iov, 2) != (INT16SZ + buflen)) {
726ee65b806SJan Lentfer #else
727ee65b806SJan Lentfer if (_writev(statp->_vcsock, iov, 2) != (INT16SZ + buflen)) {
728ee65b806SJan Lentfer #endif
729ee65b806SJan Lentfer *terrno = errno;
730ee65b806SJan Lentfer Perror(statp, stderr, "write failed", errno);
731ee65b806SJan Lentfer res_nclose(statp);
732ee65b806SJan Lentfer return (0);
733ee65b806SJan Lentfer }
734ee65b806SJan Lentfer /*
735ee65b806SJan Lentfer * Receive length & response
736ee65b806SJan Lentfer */
737ee65b806SJan Lentfer read_len:
738ee65b806SJan Lentfer cp = ans;
739ee65b806SJan Lentfer len = INT16SZ;
740ee65b806SJan Lentfer #ifndef _LIBC
741ee65b806SJan Lentfer while ((n = read(statp->_vcsock, (char *)cp, (int)len)) > 0) {
742ee65b806SJan Lentfer #else
743ee65b806SJan Lentfer while ((n = _read(statp->_vcsock, (char *)cp, (int)len)) > 0) {
744ee65b806SJan Lentfer #endif
745ee65b806SJan Lentfer cp += n;
746ee65b806SJan Lentfer if ((len -= n) == 0)
747ee65b806SJan Lentfer break;
748ee65b806SJan Lentfer }
749ee65b806SJan Lentfer if (n <= 0) {
750ee65b806SJan Lentfer *terrno = errno;
751ee65b806SJan Lentfer Perror(statp, stderr, "read failed", errno);
752ee65b806SJan Lentfer res_nclose(statp);
753ee65b806SJan Lentfer /*
754ee65b806SJan Lentfer * A long running process might get its TCP
755ee65b806SJan Lentfer * connection reset if the remote server was
756ee65b806SJan Lentfer * restarted. Requery the server instead of
757ee65b806SJan Lentfer * trying a new one. When there is only one
758ee65b806SJan Lentfer * server, this means that a query might work
759ee65b806SJan Lentfer * instead of failing. We only allow one reset
760ee65b806SJan Lentfer * per query to prevent looping.
761ee65b806SJan Lentfer */
762ee65b806SJan Lentfer if (*terrno == ECONNRESET && !connreset) {
763ee65b806SJan Lentfer connreset = 1;
764ee65b806SJan Lentfer res_nclose(statp);
765ee65b806SJan Lentfer goto same_ns;
766ee65b806SJan Lentfer }
767ee65b806SJan Lentfer res_nclose(statp);
768ee65b806SJan Lentfer return (0);
769ee65b806SJan Lentfer }
770ee65b806SJan Lentfer resplen = ns_get16(ans);
771ee65b806SJan Lentfer if (resplen > anssiz) {
772ee65b806SJan Lentfer Dprint(statp->options & RES_DEBUG,
773ee65b806SJan Lentfer (stdout, ";; response truncated\n")
774ee65b806SJan Lentfer );
775ee65b806SJan Lentfer truncating = 1;
776ee65b806SJan Lentfer len = anssiz;
777ee65b806SJan Lentfer } else
778ee65b806SJan Lentfer len = resplen;
779ee65b806SJan Lentfer if (len < HFIXEDSZ) {
780ee65b806SJan Lentfer /*
781ee65b806SJan Lentfer * Undersized message.
782ee65b806SJan Lentfer */
783ee65b806SJan Lentfer Dprint(statp->options & RES_DEBUG,
784ee65b806SJan Lentfer (stdout, ";; undersized: %d\n", len));
785ee65b806SJan Lentfer *terrno = EMSGSIZE;
786ee65b806SJan Lentfer res_nclose(statp);
787ee65b806SJan Lentfer return (0);
788ee65b806SJan Lentfer }
789ee65b806SJan Lentfer cp = ans;
790ee65b806SJan Lentfer #ifndef _LIBC
791ee65b806SJan Lentfer while (len != 0 && (n = read(statp->_vcsock, (char *)cp, (int)len)) > 0){
792ee65b806SJan Lentfer #else
793ee65b806SJan Lentfer while (len != 0 && (n = _read(statp->_vcsock, (char *)cp, (int)len)) > 0){
794ee65b806SJan Lentfer #endif
795ee65b806SJan Lentfer cp += n;
796ee65b806SJan Lentfer len -= n;
797ee65b806SJan Lentfer }
798ee65b806SJan Lentfer if (n <= 0) {
799ee65b806SJan Lentfer *terrno = errno;
800ee65b806SJan Lentfer Perror(statp, stderr, "read(vc)", errno);
801ee65b806SJan Lentfer res_nclose(statp);
802ee65b806SJan Lentfer return (0);
803ee65b806SJan Lentfer }
804ee65b806SJan Lentfer if (truncating) {
805ee65b806SJan Lentfer /*
806ee65b806SJan Lentfer * Flush rest of answer so connection stays in synch.
807ee65b806SJan Lentfer */
808ee65b806SJan Lentfer anhp->tc = 1;
809ee65b806SJan Lentfer len = resplen - anssiz;
810ee65b806SJan Lentfer while (len != 0) {
811ee65b806SJan Lentfer char junk[PACKETSZ];
812ee65b806SJan Lentfer
813ee65b806SJan Lentfer #ifndef _LIBC
814ee65b806SJan Lentfer n = read(statp->_vcsock, junk,
815ee65b806SJan Lentfer #else
816ee65b806SJan Lentfer n = _read(statp->_vcsock, junk,
817ee65b806SJan Lentfer #endif
818ee65b806SJan Lentfer (len > sizeof junk) ? sizeof junk : len);
819ee65b806SJan Lentfer if (n > 0)
820ee65b806SJan Lentfer len -= n;
821ee65b806SJan Lentfer else
822ee65b806SJan Lentfer break;
823ee65b806SJan Lentfer }
824ee65b806SJan Lentfer }
825ee65b806SJan Lentfer /*
826ee65b806SJan Lentfer * If the calling applicating has bailed out of
827ee65b806SJan Lentfer * a previous call and failed to arrange to have
828ee65b806SJan Lentfer * the circuit closed or the server has got
829ee65b806SJan Lentfer * itself confused, then drop the packet and
830ee65b806SJan Lentfer * wait for the correct one.
831ee65b806SJan Lentfer */
832ee65b806SJan Lentfer if (hp->id != anhp->id) {
833ee65b806SJan Lentfer DprintQ((statp->options & RES_DEBUG) ||
834ee65b806SJan Lentfer (statp->pfcode & RES_PRF_REPLY),
835ee65b806SJan Lentfer (stdout, ";; old answer (unexpected):\n"),
836ee65b806SJan Lentfer ans, (resplen > anssiz) ? anssiz: resplen);
837ee65b806SJan Lentfer goto read_len;
838ee65b806SJan Lentfer }
839ee65b806SJan Lentfer
840ee65b806SJan Lentfer /*
841ee65b806SJan Lentfer * All is well, or the error is fatal. Signal that the
842ee65b806SJan Lentfer * next nameserver ought not be tried.
843ee65b806SJan Lentfer */
844ee65b806SJan Lentfer return (resplen);
845ee65b806SJan Lentfer }
846ee65b806SJan Lentfer
847ee65b806SJan Lentfer static int
848ee65b806SJan Lentfer send_dg(res_state statp,
849ee65b806SJan Lentfer #ifdef USE_KQUEUE
850ee65b806SJan Lentfer int kq,
851ee65b806SJan Lentfer #endif
852ee65b806SJan Lentfer const u_char *buf, int buflen, u_char *ans,
853ee65b806SJan Lentfer int anssiz, int *terrno, int ns, int tries, int *v_circuit,
854ee65b806SJan Lentfer int *gotsomewhere)
855ee65b806SJan Lentfer {
856ee65b806SJan Lentfer const HEADER *hp = (const HEADER *) buf;
857ee65b806SJan Lentfer HEADER *anhp = (HEADER *) ans;
858ee65b806SJan Lentfer const struct sockaddr *nsap;
859ee65b806SJan Lentfer int nsaplen;
860ee65b806SJan Lentfer struct timespec now, timeout, finish;
861ee65b806SJan Lentfer struct sockaddr_storage from;
862ee65b806SJan Lentfer ISC_SOCKLEN_T fromlen;
863ee65b806SJan Lentfer int resplen, seconds, n, s;
864ee65b806SJan Lentfer #ifdef USE_KQUEUE
865ee65b806SJan Lentfer struct kevent kv;
866ee65b806SJan Lentfer #else
867ee65b806SJan Lentfer #ifdef USE_POLL
868ee65b806SJan Lentfer int polltimeout;
869ee65b806SJan Lentfer struct pollfd pollfd;
870ee65b806SJan Lentfer #else
871ee65b806SJan Lentfer fd_set dsmask;
872ee65b806SJan Lentfer #endif
873ee65b806SJan Lentfer #endif
874ee65b806SJan Lentfer
875ee65b806SJan Lentfer nsap = get_nsaddr(statp, ns);
876ee65b806SJan Lentfer nsaplen = get_salen(nsap);
877ee65b806SJan Lentfer if (EXT(statp).nssocks[ns] == -1) {
878ee65b806SJan Lentfer #ifndef _LIBC
879ee65b806SJan Lentfer EXT(statp).nssocks[ns] = socket(nsap->sa_family, SOCK_DGRAM, 0);
880ee65b806SJan Lentfer #else
881ee65b806SJan Lentfer EXT(statp).nssocks[ns] = _socket(nsap->sa_family, SOCK_DGRAM, 0);
882ee65b806SJan Lentfer #endif
883ee65b806SJan Lentfer #if !defined(USE_POLL) && !defined(USE_KQUEUE)
884ee65b806SJan Lentfer if (EXT(statp).nssocks[ns] > highestFD) {
885ee65b806SJan Lentfer res_nclose(statp);
886ee65b806SJan Lentfer errno = ENOTSOCK;
887ee65b806SJan Lentfer }
888ee65b806SJan Lentfer #endif
889ee65b806SJan Lentfer if (EXT(statp).nssocks[ns] < 0) {
890ee65b806SJan Lentfer switch (errno) {
891ee65b806SJan Lentfer case EPROTONOSUPPORT:
892ee65b806SJan Lentfer #ifdef EPFNOSUPPORT
893ee65b806SJan Lentfer case EPFNOSUPPORT:
894ee65b806SJan Lentfer #endif
895ee65b806SJan Lentfer case EAFNOSUPPORT:
896ee65b806SJan Lentfer Perror(statp, stderr, "socket(dg)", errno);
897ee65b806SJan Lentfer return (0);
898ee65b806SJan Lentfer default:
899ee65b806SJan Lentfer *terrno = errno;
900ee65b806SJan Lentfer Perror(statp, stderr, "socket(dg)", errno);
901ee65b806SJan Lentfer return (-1);
902ee65b806SJan Lentfer }
903ee65b806SJan Lentfer }
904ee65b806SJan Lentfer #ifndef CANNOT_CONNECT_DGRAM
905ee65b806SJan Lentfer /*
906ee65b806SJan Lentfer * On a 4.3BSD+ machine (client and server,
907ee65b806SJan Lentfer * actually), sending to a nameserver datagram
908ee65b806SJan Lentfer * port with no nameserver will cause an
909ee65b806SJan Lentfer * ICMP port unreachable message to be returned.
910ee65b806SJan Lentfer * If our datagram socket is "connected" to the
911ee65b806SJan Lentfer * server, we get an ECONNREFUSED error on the next
912ee65b806SJan Lentfer * socket operation, and select returns if the
913ee65b806SJan Lentfer * error message is received. We can thus detect
914ee65b806SJan Lentfer * the absence of a nameserver without timing out.
915ee65b806SJan Lentfer *
916ee65b806SJan Lentfer *
917ee65b806SJan Lentfer * When the option "insecure1" is specified, we'd
918ee65b806SJan Lentfer * rather expect to see responses from an "unknown"
919ee65b806SJan Lentfer * address. In order to let the kernel accept such
920ee65b806SJan Lentfer * responses, do not connect the socket here.
921ee65b806SJan Lentfer * XXX: or do we need an explicit option to disable
922ee65b806SJan Lentfer * connecting?
923ee65b806SJan Lentfer */
924ee65b806SJan Lentfer if (!(statp->options & RES_INSECURE1) &&
925ee65b806SJan Lentfer #ifndef _LIBC
926ee65b806SJan Lentfer connect(EXT(statp).nssocks[ns], nsap, nsaplen) < 0) {
927ee65b806SJan Lentfer #else
928ee65b806SJan Lentfer _connect(EXT(statp).nssocks[ns], nsap, nsaplen) < 0) {
929ee65b806SJan Lentfer #endif
930ee65b806SJan Lentfer Aerror(statp, stderr, "connect(dg)", errno, nsap,
931ee65b806SJan Lentfer nsaplen);
932ee65b806SJan Lentfer res_nclose(statp);
933ee65b806SJan Lentfer return (0);
934ee65b806SJan Lentfer }
935ee65b806SJan Lentfer #endif /* !CANNOT_CONNECT_DGRAM */
936ee65b806SJan Lentfer Dprint(statp->options & RES_DEBUG,
937ee65b806SJan Lentfer (stdout, ";; new DG socket\n"))
938ee65b806SJan Lentfer }
939ee65b806SJan Lentfer s = EXT(statp).nssocks[ns];
940ee65b806SJan Lentfer #ifndef CANNOT_CONNECT_DGRAM
941ee65b806SJan Lentfer if (statp->options & RES_INSECURE1) {
942ee65b806SJan Lentfer #ifndef _LIBC
943ee65b806SJan Lentfer if (sendto(s,
944ee65b806SJan Lentfer #else
945ee65b806SJan Lentfer if (_sendto(s,
946ee65b806SJan Lentfer #endif
947ee65b806SJan Lentfer (const char*)buf, buflen, 0, nsap, nsaplen) != buflen) {
948ee65b806SJan Lentfer Aerror(statp, stderr, "sendto", errno, nsap, nsaplen);
949ee65b806SJan Lentfer res_nclose(statp);
950ee65b806SJan Lentfer return (0);
951ee65b806SJan Lentfer }
952ee65b806SJan Lentfer } else if (send(s, (const char*)buf, buflen, 0) != buflen) {
953ee65b806SJan Lentfer Perror(statp, stderr, "send", errno);
954ee65b806SJan Lentfer res_nclose(statp);
955ee65b806SJan Lentfer return (0);
956ee65b806SJan Lentfer }
957ee65b806SJan Lentfer #else /* !CANNOT_CONNECT_DGRAM */
958ee65b806SJan Lentfer #ifndef _LIBC
959ee65b806SJan Lentfer if (sendto(s, (const char*)buf, buflen, 0, nsap, nsaplen) != buflen)
960ee65b806SJan Lentfer #else
961ee65b806SJan Lentfer if (_sendto(s, (const char*)buf, buflen, 0, nsap, nsaplen) != buflen)
962ee65b806SJan Lentfer #endif
963ee65b806SJan Lentfer {
964ee65b806SJan Lentfer Aerror(statp, stderr, "sendto", errno, nsap, nsaplen);
965ee65b806SJan Lentfer res_nclose(statp);
966ee65b806SJan Lentfer return (0);
967ee65b806SJan Lentfer }
968ee65b806SJan Lentfer #endif /* !CANNOT_CONNECT_DGRAM */
969ee65b806SJan Lentfer
970ee65b806SJan Lentfer /*
971ee65b806SJan Lentfer * Wait for reply.
972ee65b806SJan Lentfer */
973ee65b806SJan Lentfer seconds = (statp->retrans << tries);
974ee65b806SJan Lentfer if (ns > 0)
975ee65b806SJan Lentfer seconds /= statp->nscount;
976ee65b806SJan Lentfer if (seconds <= 0)
977ee65b806SJan Lentfer seconds = 1;
978ee65b806SJan Lentfer now = evNowTime();
979ee65b806SJan Lentfer timeout = evConsTime(seconds, 0);
980ee65b806SJan Lentfer finish = evAddTime(now, timeout);
981ee65b806SJan Lentfer goto nonow;
982ee65b806SJan Lentfer wait:
983ee65b806SJan Lentfer now = evNowTime();
984ee65b806SJan Lentfer nonow:
985ee65b806SJan Lentfer #ifndef USE_POLL
986ee65b806SJan Lentfer if (evCmpTime(finish, now) > 0)
987ee65b806SJan Lentfer timeout = evSubTime(finish, now);
988ee65b806SJan Lentfer else
989ee65b806SJan Lentfer timeout = evConsTime(0, 0);
990ee65b806SJan Lentfer #ifdef USE_KQUEUE
991ee65b806SJan Lentfer EV_SET(&kv, s, EVFILT_READ, EV_ADD | EV_ONESHOT, 0, 0, 0);
992ee65b806SJan Lentfer n = _kevent(kq, &kv, 1, &kv, 1, &timeout);
993ee65b806SJan Lentfer #else
994ee65b806SJan Lentfer FD_ZERO(&dsmask);
995ee65b806SJan Lentfer FD_SET(s, &dsmask);
996ee65b806SJan Lentfer n = pselect(s + 1, &dsmask, NULL, NULL, &timeout, NULL);
997ee65b806SJan Lentfer #endif
998ee65b806SJan Lentfer #else
999ee65b806SJan Lentfer timeout = evSubTime(finish, now);
1000ee65b806SJan Lentfer if (timeout.tv_sec < 0)
1001ee65b806SJan Lentfer timeout = evConsTime(0, 0);
1002ee65b806SJan Lentfer polltimeout = 1000*timeout.tv_sec +
1003ee65b806SJan Lentfer timeout.tv_nsec/1000000;
1004ee65b806SJan Lentfer pollfd.fd = s;
1005ee65b806SJan Lentfer pollfd.events = POLLRDNORM;
1006ee65b806SJan Lentfer n = poll(&pollfd, 1, polltimeout);
1007ee65b806SJan Lentfer #endif /* USE_POLL */
1008ee65b806SJan Lentfer
1009ee65b806SJan Lentfer if (n == 0) {
1010ee65b806SJan Lentfer Dprint(statp->options & RES_DEBUG, (stdout, ";; timeout\n"));
1011ee65b806SJan Lentfer *gotsomewhere = 1;
1012ee65b806SJan Lentfer return (0);
1013ee65b806SJan Lentfer }
1014ee65b806SJan Lentfer if (n < 0) {
1015ee65b806SJan Lentfer if (errno == EINTR)
1016ee65b806SJan Lentfer goto wait;
1017ee65b806SJan Lentfer #ifdef USE_KQUEUE
1018ee65b806SJan Lentfer Perror(statp, stderr, "kevent", errno);
1019ee65b806SJan Lentfer #else
1020ee65b806SJan Lentfer #ifndef USE_POLL
1021ee65b806SJan Lentfer Perror(statp, stderr, "select", errno);
1022ee65b806SJan Lentfer #else
1023ee65b806SJan Lentfer Perror(statp, stderr, "poll", errno);
1024ee65b806SJan Lentfer #endif /* USE_POLL */
1025ee65b806SJan Lentfer #endif
1026ee65b806SJan Lentfer res_nclose(statp);
1027ee65b806SJan Lentfer return (0);
1028ee65b806SJan Lentfer }
1029ee65b806SJan Lentfer #ifdef USE_KQUEUE
1030ee65b806SJan Lentfer if (kv.ident != s)
1031ee65b806SJan Lentfer goto wait;
1032ee65b806SJan Lentfer #endif
1033ee65b806SJan Lentfer errno = 0;
1034ee65b806SJan Lentfer fromlen = sizeof(from);
1035ee65b806SJan Lentfer #ifndef _LIBC
1036ee65b806SJan Lentfer resplen = recvfrom(s, (char*)ans, anssiz,0,
1037ee65b806SJan Lentfer #else
1038ee65b806SJan Lentfer resplen = _recvfrom(s, (char*)ans, anssiz,0,
1039ee65b806SJan Lentfer #endif
1040ee65b806SJan Lentfer (struct sockaddr *)&from, &fromlen);
1041ee65b806SJan Lentfer if (resplen <= 0) {
1042ee65b806SJan Lentfer Perror(statp, stderr, "recvfrom", errno);
1043ee65b806SJan Lentfer res_nclose(statp);
1044ee65b806SJan Lentfer return (0);
1045ee65b806SJan Lentfer }
1046ee65b806SJan Lentfer *gotsomewhere = 1;
1047ee65b806SJan Lentfer if (resplen < HFIXEDSZ) {
1048ee65b806SJan Lentfer /*
1049ee65b806SJan Lentfer * Undersized message.
1050ee65b806SJan Lentfer */
1051ee65b806SJan Lentfer Dprint(statp->options & RES_DEBUG,
1052ee65b806SJan Lentfer (stdout, ";; undersized: %d\n",
1053ee65b806SJan Lentfer resplen));
1054ee65b806SJan Lentfer *terrno = EMSGSIZE;
1055ee65b806SJan Lentfer res_nclose(statp);
1056ee65b806SJan Lentfer return (0);
1057ee65b806SJan Lentfer }
1058ee65b806SJan Lentfer if (hp->id != anhp->id) {
1059ee65b806SJan Lentfer /*
1060ee65b806SJan Lentfer * response from old query, ignore it.
1061ee65b806SJan Lentfer * XXX - potential security hazard could
1062ee65b806SJan Lentfer * be detected here.
1063ee65b806SJan Lentfer */
1064ee65b806SJan Lentfer DprintQ((statp->options & RES_DEBUG) ||
1065ee65b806SJan Lentfer (statp->pfcode & RES_PRF_REPLY),
1066ee65b806SJan Lentfer (stdout, ";; old answer:\n"),
1067ee65b806SJan Lentfer ans, (resplen > anssiz) ? anssiz : resplen);
1068ee65b806SJan Lentfer goto wait;
1069ee65b806SJan Lentfer }
1070ee65b806SJan Lentfer if (!(statp->options & RES_INSECURE1) &&
1071ee65b806SJan Lentfer !res_ourserver_p(statp, (struct sockaddr *)&from)) {
1072ee65b806SJan Lentfer /*
1073ee65b806SJan Lentfer * response from wrong server? ignore it.
1074ee65b806SJan Lentfer * XXX - potential security hazard could
1075ee65b806SJan Lentfer * be detected here.
1076ee65b806SJan Lentfer */
1077ee65b806SJan Lentfer DprintQ((statp->options & RES_DEBUG) ||
1078ee65b806SJan Lentfer (statp->pfcode & RES_PRF_REPLY),
1079ee65b806SJan Lentfer (stdout, ";; not our server:\n"),
1080ee65b806SJan Lentfer ans, (resplen > anssiz) ? anssiz : resplen);
1081ee65b806SJan Lentfer goto wait;
1082ee65b806SJan Lentfer }
1083ee65b806SJan Lentfer #ifdef RES_USE_EDNS0
1084ee65b806SJan Lentfer if (anhp->rcode == FORMERR && (statp->options & RES_USE_EDNS0) != 0U) {
1085ee65b806SJan Lentfer /*
1086ee65b806SJan Lentfer * Do not retry if the server do not understand EDNS0.
1087ee65b806SJan Lentfer * The case has to be captured here, as FORMERR packet do not
1088ee65b806SJan Lentfer * carry query section, hence res_queriesmatch() returns 0.
1089ee65b806SJan Lentfer */
1090ee65b806SJan Lentfer DprintQ(statp->options & RES_DEBUG,
1091ee65b806SJan Lentfer (stdout, "server rejected query with EDNS0:\n"),
1092ee65b806SJan Lentfer ans, (resplen > anssiz) ? anssiz : resplen);
1093ee65b806SJan Lentfer /* record the error */
1094ee65b806SJan Lentfer statp->_flags |= RES_F_EDNS0ERR;
1095ee65b806SJan Lentfer res_nclose(statp);
1096ee65b806SJan Lentfer return (0);
1097ee65b806SJan Lentfer }
1098ee65b806SJan Lentfer #endif
1099ee65b806SJan Lentfer if (!(statp->options & RES_INSECURE2) &&
1100ee65b806SJan Lentfer !res_queriesmatch(buf, buf + buflen,
1101ee65b806SJan Lentfer ans, ans + anssiz)) {
1102ee65b806SJan Lentfer /*
1103ee65b806SJan Lentfer * response contains wrong query? ignore it.
1104ee65b806SJan Lentfer * XXX - potential security hazard could
1105ee65b806SJan Lentfer * be detected here.
1106ee65b806SJan Lentfer */
1107ee65b806SJan Lentfer DprintQ((statp->options & RES_DEBUG) ||
1108ee65b806SJan Lentfer (statp->pfcode & RES_PRF_REPLY),
1109ee65b806SJan Lentfer (stdout, ";; wrong query name:\n"),
1110ee65b806SJan Lentfer ans, (resplen > anssiz) ? anssiz : resplen);
1111ee65b806SJan Lentfer goto wait;
1112ee65b806SJan Lentfer }
1113ee65b806SJan Lentfer if (anhp->rcode == SERVFAIL ||
1114ee65b806SJan Lentfer anhp->rcode == NOTIMP ||
1115ee65b806SJan Lentfer anhp->rcode == REFUSED) {
1116ee65b806SJan Lentfer DprintQ(statp->options & RES_DEBUG,
1117ee65b806SJan Lentfer (stdout, "server rejected query:\n"),
1118ee65b806SJan Lentfer ans, (resplen > anssiz) ? anssiz : resplen);
1119ee65b806SJan Lentfer res_nclose(statp);
1120ee65b806SJan Lentfer /* don't retry if called from dig */
1121ee65b806SJan Lentfer if (!statp->pfcode)
1122ee65b806SJan Lentfer return (0);
1123ee65b806SJan Lentfer }
1124ee65b806SJan Lentfer if (!(statp->options & RES_IGNTC) && anhp->tc) {
1125ee65b806SJan Lentfer /*
1126ee65b806SJan Lentfer * To get the rest of answer,
1127ee65b806SJan Lentfer * use TCP with same server.
1128ee65b806SJan Lentfer */
1129ee65b806SJan Lentfer Dprint(statp->options & RES_DEBUG,
1130ee65b806SJan Lentfer (stdout, ";; truncated answer\n"));
1131ee65b806SJan Lentfer *v_circuit = 1;
1132ee65b806SJan Lentfer res_nclose(statp);
1133ee65b806SJan Lentfer return (1);
1134ee65b806SJan Lentfer }
1135ee65b806SJan Lentfer /*
1136ee65b806SJan Lentfer * All is well, or the error is fatal. Signal that the
1137ee65b806SJan Lentfer * next nameserver ought not be tried.
1138ee65b806SJan Lentfer */
1139ee65b806SJan Lentfer return (resplen);
1140ee65b806SJan Lentfer }
1141ee65b806SJan Lentfer
1142ee65b806SJan Lentfer static void
1143ee65b806SJan Lentfer Aerror(const res_state statp, FILE *file, const char *string, int error,
1144ee65b806SJan Lentfer const struct sockaddr *address, int alen)
1145ee65b806SJan Lentfer {
1146ee65b806SJan Lentfer int save = errno;
1147ee65b806SJan Lentfer char hbuf[NI_MAXHOST];
1148ee65b806SJan Lentfer char sbuf[NI_MAXSERV];
1149ee65b806SJan Lentfer
1150ee65b806SJan Lentfer alen = alen;
1151ee65b806SJan Lentfer
1152ee65b806SJan Lentfer if ((statp->options & RES_DEBUG) != 0U) {
1153ee65b806SJan Lentfer if (getnameinfo(address, alen, hbuf, sizeof(hbuf),
1154ee65b806SJan Lentfer sbuf, sizeof(sbuf), niflags)) {
1155ee65b806SJan Lentfer strncpy(hbuf, "?", sizeof(hbuf) - 1);
1156ee65b806SJan Lentfer hbuf[sizeof(hbuf) - 1] = '\0';
1157ee65b806SJan Lentfer strncpy(sbuf, "?", sizeof(sbuf) - 1);
1158ee65b806SJan Lentfer sbuf[sizeof(sbuf) - 1] = '\0';
1159ee65b806SJan Lentfer }
1160ee65b806SJan Lentfer fprintf(file, "res_send: %s ([%s].%s): %s\n",
1161ee65b806SJan Lentfer string, hbuf, sbuf, strerror(error));
1162ee65b806SJan Lentfer }
1163ee65b806SJan Lentfer errno = save;
1164ee65b806SJan Lentfer }
1165ee65b806SJan Lentfer
1166ee65b806SJan Lentfer static void
1167ee65b806SJan Lentfer Perror(const res_state statp, FILE *file, const char *string, int error) {
1168ee65b806SJan Lentfer int save = errno;
1169ee65b806SJan Lentfer
1170ee65b806SJan Lentfer if ((statp->options & RES_DEBUG) != 0U)
1171ee65b806SJan Lentfer fprintf(file, "res_send: %s: %s\n",
1172ee65b806SJan Lentfer string, strerror(error));
1173ee65b806SJan Lentfer errno = save;
1174ee65b806SJan Lentfer }
1175ee65b806SJan Lentfer
1176ee65b806SJan Lentfer static int
1177ee65b806SJan Lentfer sock_eq(struct sockaddr *a, struct sockaddr *b) {
1178ee65b806SJan Lentfer struct sockaddr_in *a4, *b4;
1179ee65b806SJan Lentfer struct sockaddr_in6 *a6, *b6;
1180ee65b806SJan Lentfer
1181ee65b806SJan Lentfer if (a->sa_family != b->sa_family)
1182ee65b806SJan Lentfer return 0;
1183ee65b806SJan Lentfer switch (a->sa_family) {
1184ee65b806SJan Lentfer case AF_INET:
1185ee65b806SJan Lentfer a4 = (struct sockaddr_in *)a;
1186ee65b806SJan Lentfer b4 = (struct sockaddr_in *)b;
1187ee65b806SJan Lentfer return a4->sin_port == b4->sin_port &&
1188ee65b806SJan Lentfer a4->sin_addr.s_addr == b4->sin_addr.s_addr;
1189ee65b806SJan Lentfer case AF_INET6:
1190ee65b806SJan Lentfer a6 = (struct sockaddr_in6 *)a;
1191ee65b806SJan Lentfer b6 = (struct sockaddr_in6 *)b;
1192ee65b806SJan Lentfer return a6->sin6_port == b6->sin6_port &&
1193ee65b806SJan Lentfer #ifdef HAVE_SIN6_SCOPE_ID
1194ee65b806SJan Lentfer a6->sin6_scope_id == b6->sin6_scope_id &&
1195ee65b806SJan Lentfer #endif
1196ee65b806SJan Lentfer IN6_ARE_ADDR_EQUAL(&a6->sin6_addr, &b6->sin6_addr);
1197ee65b806SJan Lentfer default:
1198ee65b806SJan Lentfer return 0;
1199ee65b806SJan Lentfer }
1200ee65b806SJan Lentfer }
1201ee65b806SJan Lentfer
1202ee65b806SJan Lentfer #if defined(NEED_PSELECT) && !defined(USE_POLL) && !defined(USE_KQUEUE)
1203ee65b806SJan Lentfer /* XXX needs to move to the porting library. */
1204ee65b806SJan Lentfer static int
1205ee65b806SJan Lentfer pselect(int nfds, void *rfds, void *wfds, void *efds,
1206ee65b806SJan Lentfer struct timespec *tsp, const sigset_t *sigmask)
1207ee65b806SJan Lentfer {
1208ee65b806SJan Lentfer struct timeval tv, *tvp;
1209ee65b806SJan Lentfer sigset_t sigs;
1210ee65b806SJan Lentfer int n;
1211ee65b806SJan Lentfer
1212ee65b806SJan Lentfer if (tsp) {
1213ee65b806SJan Lentfer tvp = &tv;
1214ee65b806SJan Lentfer tv = evTimeVal(*tsp);
1215ee65b806SJan Lentfer } else
1216ee65b806SJan Lentfer tvp = NULL;
1217ee65b806SJan Lentfer if (sigmask)
1218ee65b806SJan Lentfer sigprocmask(SIG_SETMASK, sigmask, &sigs);
1219ee65b806SJan Lentfer n = select(nfds, rfds, wfds, efds, tvp);
1220ee65b806SJan Lentfer if (sigmask)
1221ee65b806SJan Lentfer sigprocmask(SIG_SETMASK, &sigs, NULL);
1222ee65b806SJan Lentfer if (tsp)
1223ee65b806SJan Lentfer *tsp = evTimeSpec(tv);
1224ee65b806SJan Lentfer return (n);
1225ee65b806SJan Lentfer }
1226ee65b806SJan Lentfer #endif
1227