blob: ceb2c7782b66cb5d1baa20fba5b17805a1fba5de [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/* $NetBSD: res_send.c,v 1.9 2006/01/24 17:41:25 christos Exp $ */
2
3/*
4 * Copyright 2008 Android Open Source Project (source port randomization)
5 * Copyright (c) 1985, 1989, 1993
6 * The Regents of the University of California. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37/*
38 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
39 *
40 * Permission to use, copy, modify, and distribute this software for any
41 * purpose with or without fee is hereby granted, provided that the above
42 * copyright notice and this permission notice appear in all copies, and that
43 * the name of Digital Equipment Corporation not be used in advertising or
44 * publicity pertaining to distribution of the document or software without
45 * specific, written prior permission.
46 *
47 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
48 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
49 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
50 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
51 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
52 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
53 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
54 * SOFTWARE.
55 */
56
57/*
58 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
59 * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
60 *
61 * Permission to use, copy, modify, and distribute this software for any
62 * purpose with or without fee is hereby granted, provided that the above
63 * copyright notice and this permission notice appear in all copies.
64 *
65 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
66 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
67 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
68 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
69 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
70 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
71 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
72 */
73
74#include <sys/cdefs.h>
75#if defined(LIBC_SCCS) && !defined(lint)
76#ifdef notdef
77static const char sccsid[] = "@(#)res_send.c 8.1 (Berkeley) 6/4/93";
78static const char rcsid[] = "Id: res_send.c,v 1.5.2.2.4.5 2004/08/10 02:19:56 marka Exp";
79#else
80__RCSID("$NetBSD: res_send.c,v 1.9 2006/01/24 17:41:25 christos Exp $");
81#endif
82#endif /* LIBC_SCCS and not lint */
83
84/* set to 1 to use our small/simple/limited DNS cache */
85#define USE_RESOLV_CACHE 1
86
87/*
88 * Send query to name server and wait for reply.
89 */
90
91#include <sys/types.h>
92#include <sys/param.h>
93#include <sys/time.h>
94#include <sys/socket.h>
95#include <sys/uio.h>
96
97#include <netinet/in.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080098#include "arpa_nameser.h"
99#include <arpa/inet.h>
100
101#include <errno.h>
Robert Greenwaltecd0e952012-01-11 10:04:48 -0800102#include <fcntl.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800103#include <netdb.h>
104#ifdef ANDROID_CHANGES
105#include "resolv_private.h"
106#else
107#include <resolv.h>
108#endif
109#include <signal.h>
110#include <stdio.h>
111#include <stdlib.h>
112#include <string.h>
Robert Greenwaltecd0e952012-01-11 10:04:48 -0800113#include <time.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800114#include <unistd.h>
115
116#include <isc/eventlib.h>
117
118#if USE_RESOLV_CACHE
119# include <resolv_cache.h>
120#endif
121
Robert Greenwaltecd0e952012-01-11 10:04:48 -0800122#include "logd.h"
123
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800124#ifndef DE_CONST
125#define DE_CONST(c,v) v = ((c) ? \
126 strchr((const void *)(c), *(const char *)(const void *)(c)) : NULL)
127#endif
128
129/* Options. Leave them on. */
130#ifndef DEBUG
131#define DEBUG
132#endif
133#include "res_debug.h"
134#include "res_private.h"
135
136#define EXT(res) ((res)->_u._ext)
Robert Greenwaltecd0e952012-01-11 10:04:48 -0800137#define DBG 0
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800138
139static const int highestFD = FD_SETSIZE - 1;
140
141/* Forward. */
142
143static int get_salen __P((const struct sockaddr *));
144static struct sockaddr * get_nsaddr __P((res_state, size_t));
145static int send_vc(res_state, const u_char *, int,
146 u_char *, int, int *, int);
147static int send_dg(res_state, const u_char *, int,
148 u_char *, int, int *, int,
149 int *, int *);
150static void Aerror(const res_state, FILE *, const char *, int,
151 const struct sockaddr *, int);
152static void Perror(const res_state, FILE *, const char *, int);
153static int sock_eq(struct sockaddr *, struct sockaddr *);
154#ifdef NEED_PSELECT
155static int pselect(int, void *, void *, void *,
156 struct timespec *,
157 const sigset_t *);
158#endif
159void res_pquery(const res_state, const u_char *, int, FILE *);
Robert Greenwaltecd0e952012-01-11 10:04:48 -0800160static int connect_with_timeout(int sock, const struct sockaddr *nsap,
161 socklen_t salen, int sec);
162static int retrying_select(const int sock, fd_set *readset, fd_set *writeset,
163 const struct timespec *finish);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800164
165/* BIONIC-BEGIN: implement source port randomization */
166typedef union {
167 struct sockaddr sa;
168 struct sockaddr_in sin;
169 struct sockaddr_in6 sin6;
170} _sockaddr_union;
171
172static int
173random_bind( int s, int family )
174{
175 _sockaddr_union u;
176 int j;
177 socklen_t slen;
178
179 /* clear all, this also sets the IP4/6 address to 'any' */
180 memset( &u, 0, sizeof u );
181
182 switch (family) {
183 case AF_INET:
184 u.sin.sin_family = family;
185 slen = sizeof u.sin;
186 break;
187 case AF_INET6:
188 u.sin6.sin6_family = family;
189 slen = sizeof u.sin6;
190 break;
191 default:
192 errno = EPROTO;
193 return -1;
194 }
195
196 /* first try to bind to a random source port a few times */
197 for (j = 0; j < 10; j++) {
198 /* find a random port between 1025 .. 65534 */
199 int port = 1025 + (res_randomid() % (65535-1025));
200 if (family == AF_INET)
201 u.sin.sin_port = htons(port);
202 else
203 u.sin6.sin6_port = htons(port);
204
205 if ( !bind( s, &u.sa, slen ) )
206 return 0;
207 }
208
209 /* nothing after 10 tries, our network table is probably busy */
210 /* let the system decide which port is best */
211 if (family == AF_INET)
212 u.sin.sin_port = 0;
213 else
214 u.sin6.sin6_port = 0;
215
216 return bind( s, &u.sa, slen );
217}
218/* BIONIC-END */
219
220static const int niflags = NI_NUMERICHOST | NI_NUMERICSERV;
221
222/* Public. */
223
224/* int
225 * res_isourserver(ina)
226 * looks up "ina" in _res.ns_addr_list[]
227 * returns:
228 * 0 : not found
229 * >0 : found
230 * author:
231 * paul vixie, 29may94
232 */
Jim Huang7cc56662010-10-15 02:02:57 +0800233__LIBC_HIDDEN__ int
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800234res_ourserver_p(const res_state statp, const struct sockaddr *sa) {
235 const struct sockaddr_in *inp, *srv;
236 const struct sockaddr_in6 *in6p, *srv6;
237 int ns;
238
239 switch (sa->sa_family) {
240 case AF_INET:
241 inp = (const struct sockaddr_in *)(const void *)sa;
242 for (ns = 0; ns < statp->nscount; ns++) {
243 srv = (struct sockaddr_in *)(void *)get_nsaddr(statp, (size_t)ns);
244 if (srv->sin_family == inp->sin_family &&
245 srv->sin_port == inp->sin_port &&
246 (srv->sin_addr.s_addr == INADDR_ANY ||
247 srv->sin_addr.s_addr == inp->sin_addr.s_addr))
248 return (1);
249 }
250 break;
251 case AF_INET6:
252 if (EXT(statp).ext == NULL)
253 break;
254 in6p = (const struct sockaddr_in6 *)(const void *)sa;
255 for (ns = 0; ns < statp->nscount; ns++) {
256 srv6 = (struct sockaddr_in6 *)(void *)get_nsaddr(statp, (size_t)ns);
257 if (srv6->sin6_family == in6p->sin6_family &&
258 srv6->sin6_port == in6p->sin6_port &&
259#ifdef HAVE_SIN6_SCOPE_ID
260 (srv6->sin6_scope_id == 0 ||
261 srv6->sin6_scope_id == in6p->sin6_scope_id) &&
262#endif
263 (IN6_IS_ADDR_UNSPECIFIED(&srv6->sin6_addr) ||
264 IN6_ARE_ADDR_EQUAL(&srv6->sin6_addr, &in6p->sin6_addr)))
265 return (1);
266 }
267 break;
268 default:
269 break;
270 }
271 return (0);
272}
273
274/* int
275 * res_nameinquery(name, type, class, buf, eom)
276 * look for (name,type,class) in the query section of packet (buf,eom)
277 * requires:
278 * buf + HFIXEDSZ <= eom
279 * returns:
280 * -1 : format error
281 * 0 : not found
282 * >0 : found
283 * author:
284 * paul vixie, 29may94
285 */
286int
287res_nameinquery(const char *name, int type, int class,
288 const u_char *buf, const u_char *eom)
289{
290 const u_char *cp = buf + HFIXEDSZ;
291 int qdcount = ntohs(((const HEADER*)(const void *)buf)->qdcount);
292
293 while (qdcount-- > 0) {
294 char tname[MAXDNAME+1];
295 int n, ttype, tclass;
296
297 n = dn_expand(buf, eom, cp, tname, sizeof tname);
298 if (n < 0)
299 return (-1);
300 cp += n;
301 if (cp + 2 * INT16SZ > eom)
302 return (-1);
303 ttype = ns_get16(cp); cp += INT16SZ;
304 tclass = ns_get16(cp); cp += INT16SZ;
305 if (ttype == type && tclass == class &&
306 ns_samename(tname, name) == 1)
307 return (1);
308 }
309 return (0);
310}
311
312/* int
313 * res_queriesmatch(buf1, eom1, buf2, eom2)
314 * is there a 1:1 mapping of (name,type,class)
315 * in (buf1,eom1) and (buf2,eom2)?
316 * returns:
317 * -1 : format error
318 * 0 : not a 1:1 mapping
319 * >0 : is a 1:1 mapping
320 * author:
321 * paul vixie, 29may94
322 */
323int
324res_queriesmatch(const u_char *buf1, const u_char *eom1,
325 const u_char *buf2, const u_char *eom2)
326{
327 const u_char *cp = buf1 + HFIXEDSZ;
328 int qdcount = ntohs(((const HEADER*)(const void *)buf1)->qdcount);
329
330 if (buf1 + HFIXEDSZ > eom1 || buf2 + HFIXEDSZ > eom2)
331 return (-1);
332
333 /*
334 * Only header section present in replies to
335 * dynamic update packets.
336 */
337 if ((((const HEADER *)(const void *)buf1)->opcode == ns_o_update) &&
338 (((const HEADER *)(const void *)buf2)->opcode == ns_o_update))
339 return (1);
340
341 if (qdcount != ntohs(((const HEADER*)(const void *)buf2)->qdcount))
342 return (0);
343 while (qdcount-- > 0) {
344 char tname[MAXDNAME+1];
345 int n, ttype, tclass;
346
347 n = dn_expand(buf1, eom1, cp, tname, sizeof tname);
348 if (n < 0)
349 return (-1);
350 cp += n;
351 if (cp + 2 * INT16SZ > eom1)
352 return (-1);
353 ttype = ns_get16(cp); cp += INT16SZ;
354 tclass = ns_get16(cp); cp += INT16SZ;
355 if (!res_nameinquery(tname, ttype, tclass, buf2, eom2))
356 return (0);
357 }
358 return (1);
359}
360
361
362int
363res_nsend(res_state statp,
364 const u_char *buf, int buflen, u_char *ans, int anssiz)
365{
366 int gotsomewhere, terrno, try, v_circuit, resplen, ns, n;
367 char abuf[NI_MAXHOST];
368#if USE_RESOLV_CACHE
369 struct resolv_cache* cache;
370 ResolvCacheStatus cache_status = RESOLV_CACHE_UNSUPPORTED;
371#endif
372
Mattias Falkc63e5902011-08-23 14:34:14 +0200373#if !USE_RESOLV_CACHE
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800374 if (statp->nscount == 0) {
375 errno = ESRCH;
376 return (-1);
377 }
Mattias Falkc63e5902011-08-23 14:34:14 +0200378#endif
379
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800380 if (anssiz < HFIXEDSZ) {
381 errno = EINVAL;
382 return (-1);
383 }
384 DprintQ((statp->options & RES_DEBUG) || (statp->pfcode & RES_PRF_QUERY),
385 (stdout, ";; res_send()\n"), buf, buflen);
386 v_circuit = (statp->options & RES_USEVC) || buflen > PACKETSZ;
387 gotsomewhere = 0;
388 terrno = ETIMEDOUT;
389
390#if USE_RESOLV_CACHE
Mattias Falkc63e5902011-08-23 14:34:14 +0200391 // get the cache associated with the interface
392 cache = __get_res_cache(statp->iface);
393 if (cache != NULL) {
394 int anslen = 0;
395 cache_status = _resolv_cache_lookup(
396 cache, buf, buflen,
397 ans, anssiz, &anslen);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800398
Mattias Falkc63e5902011-08-23 14:34:14 +0200399 if (cache_status == RESOLV_CACHE_FOUND) {
400 return anslen;
401 } else {
402 // had a cache miss for a known interface, so populate the thread private
403 // data so the normal resolve path can do its thing
404 _resolv_populate_res_for_iface(statp);
405 }
406 }
407
408 if (statp->nscount == 0) {
409 errno = ESRCH;
410 return (-1);
411 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800412#endif
413
414 /*
415 * If the ns_addr_list in the resolver context has changed, then
416 * invalidate our cached copy and the associated timing data.
417 */
418 if (EXT(statp).nscount != 0) {
419 int needclose = 0;
420 struct sockaddr_storage peer;
421 socklen_t peerlen;
422
423 if (EXT(statp).nscount != statp->nscount)
424 needclose++;
425 else
426 for (ns = 0; ns < statp->nscount; ns++) {
427 if (statp->nsaddr_list[ns].sin_family &&
428 !sock_eq((struct sockaddr *)(void *)&statp->nsaddr_list[ns],
429 (struct sockaddr *)(void *)&EXT(statp).ext->nsaddrs[ns])) {
430 needclose++;
431 break;
432 }
433
434 if (EXT(statp).nssocks[ns] == -1)
435 continue;
436 peerlen = sizeof(peer);
Jim Huang87043f92011-12-12 16:32:56 +0800437 if (getpeername(EXT(statp).nssocks[ns],
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800438 (struct sockaddr *)(void *)&peer, &peerlen) < 0) {
439 needclose++;
440 break;
441 }
442 if (!sock_eq((struct sockaddr *)(void *)&peer,
443 get_nsaddr(statp, (size_t)ns))) {
444 needclose++;
445 break;
446 }
447 }
448 if (needclose) {
449 res_nclose(statp);
450 EXT(statp).nscount = 0;
451 }
452 }
453
454 /*
455 * Maybe initialize our private copy of the ns_addr_list.
456 */
457 if (EXT(statp).nscount == 0) {
458 for (ns = 0; ns < statp->nscount; ns++) {
459 EXT(statp).nstimes[ns] = RES_MAXTIME;
460 EXT(statp).nssocks[ns] = -1;
461 if (!statp->nsaddr_list[ns].sin_family)
462 continue;
463 EXT(statp).ext->nsaddrs[ns].sin =
464 statp->nsaddr_list[ns];
465 }
466 EXT(statp).nscount = statp->nscount;
467 }
468
469 /*
470 * Some resolvers want to even out the load on their nameservers.
471 * Note that RES_BLAST overrides RES_ROTATE.
472 */
473 if ((statp->options & RES_ROTATE) != 0U &&
474 (statp->options & RES_BLAST) == 0U) {
475 union res_sockaddr_union inu;
476 struct sockaddr_in ina;
477 int lastns = statp->nscount - 1;
478 int fd;
479 u_int16_t nstime;
480
481 if (EXT(statp).ext != NULL)
482 inu = EXT(statp).ext->nsaddrs[0];
483 ina = statp->nsaddr_list[0];
484 fd = EXT(statp).nssocks[0];
485 nstime = EXT(statp).nstimes[0];
486 for (ns = 0; ns < lastns; ns++) {
487 if (EXT(statp).ext != NULL)
488 EXT(statp).ext->nsaddrs[ns] =
489 EXT(statp).ext->nsaddrs[ns + 1];
490 statp->nsaddr_list[ns] = statp->nsaddr_list[ns + 1];
491 EXT(statp).nssocks[ns] = EXT(statp).nssocks[ns + 1];
492 EXT(statp).nstimes[ns] = EXT(statp).nstimes[ns + 1];
493 }
494 if (EXT(statp).ext != NULL)
495 EXT(statp).ext->nsaddrs[lastns] = inu;
496 statp->nsaddr_list[lastns] = ina;
497 EXT(statp).nssocks[lastns] = fd;
498 EXT(statp).nstimes[lastns] = nstime;
499 }
500
501 /*
502 * Send request, RETRY times, or until successful.
503 */
504 for (try = 0; try < statp->retry; try++) {
505 for (ns = 0; ns < statp->nscount; ns++) {
506 struct sockaddr *nsap;
507 int nsaplen;
508 nsap = get_nsaddr(statp, (size_t)ns);
509 nsaplen = get_salen(nsap);
510 statp->_flags &= ~RES_F_LASTMASK;
511 statp->_flags |= (ns << RES_F_LASTSHIFT);
512 same_ns:
513 if (statp->qhook) {
514 int done = 0, loops = 0;
515
516 do {
517 res_sendhookact act;
518
519 act = (*statp->qhook)(&nsap, &buf, &buflen,
520 ans, anssiz, &resplen);
521 switch (act) {
522 case res_goahead:
523 done = 1;
524 break;
525 case res_nextns:
526 res_nclose(statp);
527 goto next_ns;
528 case res_done:
529 return (resplen);
530 case res_modified:
531 /* give the hook another try */
532 if (++loops < 42) /*doug adams*/
533 break;
534 /*FALLTHROUGH*/
535 case res_error:
536 /*FALLTHROUGH*/
537 default:
538 goto fail;
539 }
540 } while (!done);
541 }
542
543 Dprint(((statp->options & RES_DEBUG) &&
544 getnameinfo(nsap, (socklen_t)nsaplen, abuf, sizeof(abuf),
Robert Greenwaltecd0e952012-01-11 10:04:48 -0800545 NULL, 0, niflags) == 0),
546 (stdout, ";; Querying server (# %d) address = %s\n",
547 ns + 1, abuf));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800548
549
550 if (v_circuit) {
551 /* Use VC; at most one attempt per server. */
552 try = statp->retry;
Robert Greenwaltecd0e952012-01-11 10:04:48 -0800553
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800554 n = send_vc(statp, buf, buflen, ans, anssiz, &terrno,
555 ns);
Robert Greenwaltecd0e952012-01-11 10:04:48 -0800556
557 if (DBG) {
558 __libc_android_log_print(ANDROID_LOG_DEBUG, "libc",
559 "used send_vc %d\n", n);
560 }
561
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800562 if (n < 0)
563 goto fail;
564 if (n == 0)
565 goto next_ns;
566 resplen = n;
567 } else {
568 /* Use datagrams. */
Robert Greenwaltecd0e952012-01-11 10:04:48 -0800569 if (DBG) {
570 __libc_android_log_print(ANDROID_LOG_DEBUG, "libc",
571 "using send_dg\n");
572 }
573
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800574 n = send_dg(statp, buf, buflen, ans, anssiz, &terrno,
575 ns, &v_circuit, &gotsomewhere);
Robert Greenwaltecd0e952012-01-11 10:04:48 -0800576 if (DBG) {
577 __libc_android_log_print(ANDROID_LOG_DEBUG, "libc",
578 "used send_dg %d\n",n);
579 }
580
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800581 if (n < 0)
582 goto fail;
583 if (n == 0)
584 goto next_ns;
Robert Greenwaltecd0e952012-01-11 10:04:48 -0800585 if (DBG) {
586 __libc_android_log_print(ANDROID_LOG_DEBUG, "libc",
587 "time=%d, %d\n",time(NULL), time(NULL)%2);
588 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800589 if (v_circuit)
590 goto same_ns;
591 resplen = n;
592 }
593
594 Dprint((statp->options & RES_DEBUG) ||
595 ((statp->pfcode & RES_PRF_REPLY) &&
596 (statp->pfcode & RES_PRF_HEAD1)),
597 (stdout, ";; got answer:\n"));
598
599 DprintQ((statp->options & RES_DEBUG) ||
600 (statp->pfcode & RES_PRF_REPLY),
601 (stdout, "%s", ""),
602 ans, (resplen > anssiz) ? anssiz : resplen);
603
604#if USE_RESOLV_CACHE
605 if (cache_status == RESOLV_CACHE_NOTFOUND) {
606 _resolv_cache_add(cache, buf, buflen,
607 ans, resplen);
608 }
609#endif
610 /*
611 * If we have temporarily opened a virtual circuit,
612 * or if we haven't been asked to keep a socket open,
613 * close the socket.
614 */
615 if ((v_circuit && (statp->options & RES_USEVC) == 0U) ||
616 (statp->options & RES_STAYOPEN) == 0U) {
617 res_nclose(statp);
618 }
619 if (statp->rhook) {
620 int done = 0, loops = 0;
621
622 do {
623 res_sendhookact act;
624
625 act = (*statp->rhook)(nsap, buf, buflen,
626 ans, anssiz, &resplen);
627 switch (act) {
628 case res_goahead:
629 case res_done:
630 done = 1;
631 break;
632 case res_nextns:
633 res_nclose(statp);
634 goto next_ns;
635 case res_modified:
636 /* give the hook another try */
637 if (++loops < 42) /*doug adams*/
638 break;
639 /*FALLTHROUGH*/
640 case res_error:
641 /*FALLTHROUGH*/
642 default:
643 goto fail;
644 }
645 } while (!done);
646
647 }
648 return (resplen);
649 next_ns: ;
650 } /*foreach ns*/
651 } /*foreach retry*/
652 res_nclose(statp);
653 if (!v_circuit) {
654 if (!gotsomewhere)
655 errno = ECONNREFUSED; /* no nameservers found */
656 else
657 errno = ETIMEDOUT; /* no answer obtained */
658 } else
659 errno = terrno;
Henrik Engströmce5ba8b2012-06-20 08:47:52 +0200660
661#if USE_RESOLV_CACHE
662 _resolv_cache_query_failed(cache, buf, buflen);
663#endif
664
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800665 return (-1);
666 fail:
Mattias Falka59cfcf2011-09-06 15:15:06 +0200667#if USE_RESOLV_CACHE
668 _resolv_cache_query_failed(cache, buf, buflen);
669#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800670 res_nclose(statp);
671 return (-1);
672}
673
674/* Private */
675
676static int
677get_salen(sa)
678 const struct sockaddr *sa;
679{
680
681#ifdef HAVE_SA_LEN
682 /* There are people do not set sa_len. Be forgiving to them. */
683 if (sa->sa_len)
684 return (sa->sa_len);
685#endif
686
687 if (sa->sa_family == AF_INET)
688 return (sizeof(struct sockaddr_in));
689 else if (sa->sa_family == AF_INET6)
690 return (sizeof(struct sockaddr_in6));
691 else
692 return (0); /* unknown, die on connect */
693}
694
695/*
696 * pick appropriate nsaddr_list for use. see res_init() for initialization.
697 */
698static struct sockaddr *
699get_nsaddr(statp, n)
700 res_state statp;
701 size_t n;
702{
703
704 if (!statp->nsaddr_list[n].sin_family && EXT(statp).ext) {
705 /*
706 * - EXT(statp).ext->nsaddrs[n] holds an address that is larger
707 * than struct sockaddr, and
708 * - user code did not update statp->nsaddr_list[n].
709 */
710 return (struct sockaddr *)(void *)&EXT(statp).ext->nsaddrs[n];
711 } else {
712 /*
713 * - user code updated statp->nsaddr_list[n], or
714 * - statp->nsaddr_list[n] has the same content as
715 * EXT(statp).ext->nsaddrs[n].
716 */
717 return (struct sockaddr *)(void *)&statp->nsaddr_list[n];
718 }
719}
720
Robert Greenwaltecd0e952012-01-11 10:04:48 -0800721static int get_timeout(const res_state statp, const int ns)
722{
723 int timeout = (statp->retrans << ns);
724 if (ns > 0) {
725 timeout /= statp->nscount;
726 }
727 if (timeout <= 0) {
728 timeout = 1;
729 }
730 if (DBG) {
731 __libc_android_log_print(ANDROID_LOG_DEBUG, "libc",
732 "using timeout of %d sec\n", timeout);
733 }
734
735 return timeout;
736}
737
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800738static int
739send_vc(res_state statp,
740 const u_char *buf, int buflen, u_char *ans, int anssiz,
741 int *terrno, int ns)
742{
743 const HEADER *hp = (const HEADER *)(const void *)buf;
744 HEADER *anhp = (HEADER *)(void *)ans;
745 struct sockaddr *nsap;
746 int nsaplen;
747 int truncating, connreset, resplen, n;
748 struct iovec iov[2];
749 u_short len;
750 u_char *cp;
751 void *tmp;
752
Robert Greenwaltecd0e952012-01-11 10:04:48 -0800753 if (DBG) {
754 __libc_android_log_print(ANDROID_LOG_DEBUG, "libc", "using send_vc\n");
755 }
756
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800757 nsap = get_nsaddr(statp, (size_t)ns);
758 nsaplen = get_salen(nsap);
759
760 connreset = 0;
761 same_ns:
762 truncating = 0;
763
764 /* Are we still talking to whom we want to talk to? */
765 if (statp->_vcsock >= 0 && (statp->_flags & RES_F_VC) != 0) {
766 struct sockaddr_storage peer;
767 socklen_t size = sizeof peer;
768
769 if (getpeername(statp->_vcsock,
770 (struct sockaddr *)(void *)&peer, &size) < 0 ||
771 !sock_eq((struct sockaddr *)(void *)&peer, nsap)) {
772 res_nclose(statp);
773 statp->_flags &= ~RES_F_VC;
774 }
775 }
776
777 if (statp->_vcsock < 0 || (statp->_flags & RES_F_VC) == 0) {
778 if (statp->_vcsock >= 0)
779 res_nclose(statp);
780
781 statp->_vcsock = socket(nsap->sa_family, SOCK_STREAM, 0);
782 if (statp->_vcsock > highestFD) {
783 res_nclose(statp);
784 errno = ENOTSOCK;
785 }
786 if (statp->_vcsock < 0) {
787 switch (errno) {
788 case EPROTONOSUPPORT:
789#ifdef EPFNOSUPPORT
790 case EPFNOSUPPORT:
791#endif
792 case EAFNOSUPPORT:
793 Perror(statp, stderr, "socket(vc)", errno);
794 return (0);
795 default:
796 *terrno = errno;
797 Perror(statp, stderr, "socket(vc)", errno);
798 return (-1);
799 }
800 }
801 errno = 0;
802 if (random_bind(statp->_vcsock,nsap->sa_family) < 0) {
803 *terrno = errno;
804 Aerror(statp, stderr, "bind/vc", errno, nsap,
805 nsaplen);
806 res_nclose(statp);
807 return (0);
808 }
Robert Greenwaltecd0e952012-01-11 10:04:48 -0800809 if (connect_with_timeout(statp->_vcsock, nsap, (socklen_t)nsaplen,
810 get_timeout(statp, ns)) < 0) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800811 *terrno = errno;
812 Aerror(statp, stderr, "connect/vc", errno, nsap,
813 nsaplen);
814 res_nclose(statp);
815 return (0);
816 }
817 statp->_flags |= RES_F_VC;
818 }
819
820 /*
821 * Send length & message
822 */
823 ns_put16((u_short)buflen, (u_char*)(void *)&len);
824 iov[0] = evConsIovec(&len, INT16SZ);
825 DE_CONST(buf, tmp);
826 iov[1] = evConsIovec(tmp, (size_t)buflen);
827 if (writev(statp->_vcsock, iov, 2) != (INT16SZ + buflen)) {
828 *terrno = errno;
829 Perror(statp, stderr, "write failed", errno);
830 res_nclose(statp);
831 return (0);
832 }
833 /*
834 * Receive length & response
835 */
836 read_len:
837 cp = ans;
838 len = INT16SZ;
839 while ((n = read(statp->_vcsock, (char *)cp, (size_t)len)) > 0) {
840 cp += n;
841 if ((len -= n) == 0)
842 break;
843 }
844 if (n <= 0) {
845 *terrno = errno;
846 Perror(statp, stderr, "read failed", errno);
847 res_nclose(statp);
848 /*
849 * A long running process might get its TCP
850 * connection reset if the remote server was
851 * restarted. Requery the server instead of
852 * trying a new one. When there is only one
853 * server, this means that a query might work
854 * instead of failing. We only allow one reset
855 * per query to prevent looping.
856 */
857 if (*terrno == ECONNRESET && !connreset) {
858 connreset = 1;
859 res_nclose(statp);
860 goto same_ns;
861 }
862 res_nclose(statp);
863 return (0);
864 }
865 resplen = ns_get16(ans);
866 if (resplen > anssiz) {
867 Dprint(statp->options & RES_DEBUG,
868 (stdout, ";; response truncated\n")
869 );
870 truncating = 1;
871 len = anssiz;
872 } else
873 len = resplen;
874 if (len < HFIXEDSZ) {
875 /*
876 * Undersized message.
877 */
878 Dprint(statp->options & RES_DEBUG,
879 (stdout, ";; undersized: %d\n", len));
880 *terrno = EMSGSIZE;
881 res_nclose(statp);
882 return (0);
883 }
884 cp = ans;
885 while (len != 0 && (n = read(statp->_vcsock, (char *)cp, (size_t)len)) > 0){
886 cp += n;
887 len -= n;
888 }
889 if (n <= 0) {
890 *terrno = errno;
891 Perror(statp, stderr, "read(vc)", errno);
892 res_nclose(statp);
893 return (0);
894 }
895 if (truncating) {
896 /*
897 * Flush rest of answer so connection stays in synch.
898 */
899 anhp->tc = 1;
900 len = resplen - anssiz;
901 while (len != 0) {
902 char junk[PACKETSZ];
903
904 n = read(statp->_vcsock, junk,
905 (len > sizeof junk) ? sizeof junk : len);
906 if (n > 0)
907 len -= n;
908 else
909 break;
910 }
911 }
912 /*
913 * If the calling applicating has bailed out of
914 * a previous call and failed to arrange to have
915 * the circuit closed or the server has got
916 * itself confused, then drop the packet and
917 * wait for the correct one.
918 */
919 if (hp->id != anhp->id) {
920 DprintQ((statp->options & RES_DEBUG) ||
921 (statp->pfcode & RES_PRF_REPLY),
922 (stdout, ";; old answer (unexpected):\n"),
923 ans, (resplen > anssiz) ? anssiz: resplen);
924 goto read_len;
925 }
926
927 /*
928 * All is well, or the error is fatal. Signal that the
929 * next nameserver ought not be tried.
930 */
931 return (resplen);
932}
933
Robert Greenwaltecd0e952012-01-11 10:04:48 -0800934/* return -1 on error (errno set), 0 on success */
935static int
936connect_with_timeout(int sock, const struct sockaddr *nsap, socklen_t salen, int sec)
937{
938 int res, origflags;
939 fd_set rset, wset;
940 struct timespec now, timeout, finish;
941
942 origflags = fcntl(sock, F_GETFL, 0);
943 fcntl(sock, F_SETFL, origflags | O_NONBLOCK);
944
945 res = connect(sock, nsap, salen);
946 if (res < 0 && errno != EINPROGRESS) {
947 res = -1;
948 goto done;
949 }
950 if (res != 0) {
951 now = evNowTime();
952 timeout = evConsTime((long)sec, 0L);
953 finish = evAddTime(now, timeout);
954 if (DBG) {
955 __libc_android_log_print(ANDROID_LOG_DEBUG, "libc",
956 " %d send_vc\n", sock);
957 }
958
959 res = retrying_select(sock, &rset, &wset, &finish);
960 if (res <= 0) {
961 res = -1;
962 }
963 }
964done:
965 fcntl(sock, F_SETFL, origflags);
966 if (DBG) {
967 __libc_android_log_print(ANDROID_LOG_DEBUG, "libc",
968 " %d connect_with_timeout returning %s\n", sock, res);
969 }
970 return res;
971}
972
973static int
974retrying_select(const int sock, fd_set *readset, fd_set *writeset, const struct timespec *finish)
975{
976 struct timespec now, timeout;
977 int n, error;
978 socklen_t len;
979
980
981retry:
982 if (DBG) {
983 __libc_android_log_print(ANDROID_LOG_DEBUG, "libc", " %d retying_select\n", sock);
984 }
985
986 now = evNowTime();
987 if (readset) {
988 FD_ZERO(readset);
989 FD_SET(sock, readset);
990 }
991 if (writeset) {
992 FD_ZERO(writeset);
993 FD_SET(sock, writeset);
994 }
995 if (evCmpTime(*finish, now) > 0)
996 timeout = evSubTime(*finish, now);
997 else
998 timeout = evConsTime(0L, 0L);
999
1000 n = pselect(sock + 1, readset, writeset, NULL, &timeout, NULL);
1001 if (n == 0) {
1002 if (DBG) {
1003 __libc_android_log_print(ANDROID_LOG_DEBUG, " libc",
1004 " %d retrying_select timeout\n", sock);
1005 }
1006 errno = ETIMEDOUT;
1007 return 0;
1008 }
1009 if (n < 0) {
1010 if (errno == EINTR)
1011 goto retry;
1012 if (DBG) {
1013 __libc_android_log_print(ANDROID_LOG_DEBUG, "libc",
1014 " %d retrying_select got error %d\n",sock, n);
1015 }
1016 return n;
1017 }
1018 if ((readset && FD_ISSET(sock, readset)) || (writeset && FD_ISSET(sock, writeset))) {
1019 len = sizeof(error);
1020 if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, &len) < 0 || error) {
1021 errno = error;
1022 if (DBG) {
1023 __libc_android_log_print(ANDROID_LOG_DEBUG, "libc",
1024 " %d retrying_select dot error2 %d\n", sock, errno);
1025 }
1026
1027 return -1;
1028 }
1029 }
1030 if (DBG) {
1031 __libc_android_log_print(ANDROID_LOG_DEBUG, "libc",
1032 " %d retrying_select returning %d for %d\n",sock, n);
1033 }
1034
1035 return n;
1036}
1037
1038
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001039static int
1040send_dg(res_state statp,
1041 const u_char *buf, int buflen, u_char *ans, int anssiz,
1042 int *terrno, int ns, int *v_circuit, int *gotsomewhere)
1043{
1044 const HEADER *hp = (const HEADER *)(const void *)buf;
1045 HEADER *anhp = (HEADER *)(void *)ans;
1046 const struct sockaddr *nsap;
1047 int nsaplen;
1048 struct timespec now, timeout, finish;
1049 fd_set dsmask;
1050 struct sockaddr_storage from;
1051 socklen_t fromlen;
1052 int resplen, seconds, n, s;
1053
1054 nsap = get_nsaddr(statp, (size_t)ns);
1055 nsaplen = get_salen(nsap);
1056 if (EXT(statp).nssocks[ns] == -1) {
1057 EXT(statp).nssocks[ns] = socket(nsap->sa_family, SOCK_DGRAM, 0);
1058 if (EXT(statp).nssocks[ns] > highestFD) {
1059 res_nclose(statp);
1060 errno = ENOTSOCK;
1061 }
1062 if (EXT(statp).nssocks[ns] < 0) {
1063 switch (errno) {
1064 case EPROTONOSUPPORT:
1065#ifdef EPFNOSUPPORT
1066 case EPFNOSUPPORT:
1067#endif
1068 case EAFNOSUPPORT:
1069 Perror(statp, stderr, "socket(dg)", errno);
1070 return (0);
1071 default:
1072 *terrno = errno;
1073 Perror(statp, stderr, "socket(dg)", errno);
1074 return (-1);
1075 }
1076 }
1077#ifndef CANNOT_CONNECT_DGRAM
1078 /*
1079 * On a 4.3BSD+ machine (client and server,
1080 * actually), sending to a nameserver datagram
1081 * port with no nameserver will cause an
1082 * ICMP port unreachable message to be returned.
1083 * If our datagram socket is "connected" to the
1084 * server, we get an ECONNREFUSED error on the next
1085 * socket operation, and select returns if the
1086 * error message is received. We can thus detect
1087 * the absence of a nameserver without timing out.
1088 */
1089 if (random_bind(EXT(statp).nssocks[ns], nsap->sa_family) < 0) {
1090 Aerror(statp, stderr, "bind(dg)", errno, nsap,
1091 nsaplen);
1092 res_nclose(statp);
1093 return (0);
1094 }
1095 if (connect(EXT(statp).nssocks[ns], nsap, (socklen_t)nsaplen) < 0) {
1096 Aerror(statp, stderr, "connect(dg)", errno, nsap,
1097 nsaplen);
1098 res_nclose(statp);
1099 return (0);
1100 }
1101#endif /* !CANNOT_CONNECT_DGRAM */
1102 Dprint(statp->options & RES_DEBUG,
1103 (stdout, ";; new DG socket\n"))
1104 }
1105 s = EXT(statp).nssocks[ns];
1106#ifndef CANNOT_CONNECT_DGRAM
1107 if (send(s, (const char*)buf, (size_t)buflen, 0) != buflen) {
1108 Perror(statp, stderr, "send", errno);
1109 res_nclose(statp);
1110 return (0);
1111 }
1112#else /* !CANNOT_CONNECT_DGRAM */
1113 if (sendto(s, (const char*)buf, buflen, 0, nsap, nsaplen) != buflen)
1114 {
1115 Aerror(statp, stderr, "sendto", errno, nsap, nsaplen);
1116 res_nclose(statp);
1117 return (0);
1118 }
1119#endif /* !CANNOT_CONNECT_DGRAM */
1120
1121 /*
1122 * Wait for reply.
1123 */
Robert Greenwaltecd0e952012-01-11 10:04:48 -08001124 seconds = get_timeout(statp, ns);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001125 now = evNowTime();
1126 timeout = evConsTime((long)seconds, 0L);
1127 finish = evAddTime(now, timeout);
Robert Greenwaltecd0e952012-01-11 10:04:48 -08001128retry:
1129 n = retrying_select(s, &dsmask, NULL, &finish);
1130
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001131 if (n == 0) {
1132 Dprint(statp->options & RES_DEBUG, (stdout, ";; timeout\n"));
1133 *gotsomewhere = 1;
1134 return (0);
1135 }
1136 if (n < 0) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001137 Perror(statp, stderr, "select", errno);
1138 res_nclose(statp);
1139 return (0);
1140 }
1141 errno = 0;
1142 fromlen = sizeof(from);
1143 resplen = recvfrom(s, (char*)ans, (size_t)anssiz,0,
1144 (struct sockaddr *)(void *)&from, &fromlen);
1145 if (resplen <= 0) {
1146 Perror(statp, stderr, "recvfrom", errno);
1147 res_nclose(statp);
1148 return (0);
1149 }
1150 *gotsomewhere = 1;
1151 if (resplen < HFIXEDSZ) {
1152 /*
1153 * Undersized message.
1154 */
1155 Dprint(statp->options & RES_DEBUG,
1156 (stdout, ";; undersized: %d\n",
1157 resplen));
1158 *terrno = EMSGSIZE;
1159 res_nclose(statp);
1160 return (0);
1161 }
1162 if (hp->id != anhp->id) {
1163 /*
1164 * response from old query, ignore it.
1165 * XXX - potential security hazard could
1166 * be detected here.
1167 */
Geremy Condra524c87c2012-06-08 21:06:33 -07001168#ifdef ANDROID_CHANGES
1169 __libc_android_log_event_uid(BIONIC_EVENT_RESOLVER_OLD_RESPONSE);
1170#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001171 DprintQ((statp->options & RES_DEBUG) ||
1172 (statp->pfcode & RES_PRF_REPLY),
1173 (stdout, ";; old answer:\n"),
1174 ans, (resplen > anssiz) ? anssiz : resplen);
Robert Greenwaltecd0e952012-01-11 10:04:48 -08001175 goto retry;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001176 }
1177 if (!(statp->options & RES_INSECURE1) &&
1178 !res_ourserver_p(statp, (struct sockaddr *)(void *)&from)) {
1179 /*
1180 * response from wrong server? ignore it.
1181 * XXX - potential security hazard could
1182 * be detected here.
1183 */
Geremy Condra524c87c2012-06-08 21:06:33 -07001184#ifdef ANDROID_CHANGES
1185 __libc_android_log_event_uid(BIONIC_EVENT_RESOLVER_WRONG_SERVER);
1186#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001187 DprintQ((statp->options & RES_DEBUG) ||
1188 (statp->pfcode & RES_PRF_REPLY),
1189 (stdout, ";; not our server:\n"),
1190 ans, (resplen > anssiz) ? anssiz : resplen);
Robert Greenwaltecd0e952012-01-11 10:04:48 -08001191 goto retry;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001192 }
1193#ifdef RES_USE_EDNS0
1194 if (anhp->rcode == FORMERR && (statp->options & RES_USE_EDNS0) != 0U) {
1195 /*
1196 * Do not retry if the server do not understand EDNS0.
1197 * The case has to be captured here, as FORMERR packet do not
1198 * carry query section, hence res_queriesmatch() returns 0.
1199 */
1200 DprintQ(statp->options & RES_DEBUG,
1201 (stdout, "server rejected query with EDNS0:\n"),
1202 ans, (resplen > anssiz) ? anssiz : resplen);
1203 /* record the error */
1204 statp->_flags |= RES_F_EDNS0ERR;
1205 res_nclose(statp);
1206 return (0);
1207 }
1208#endif
1209 if (!(statp->options & RES_INSECURE2) &&
1210 !res_queriesmatch(buf, buf + buflen,
1211 ans, ans + anssiz)) {
1212 /*
1213 * response contains wrong query? ignore it.
1214 * XXX - potential security hazard could
1215 * be detected here.
1216 */
Geremy Condra524c87c2012-06-08 21:06:33 -07001217#ifdef ANDROID_CHANGES
1218 __libc_android_log_event_uid(BIONIC_EVENT_RESOLVER_WRONG_QUERY);
1219#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001220 DprintQ((statp->options & RES_DEBUG) ||
1221 (statp->pfcode & RES_PRF_REPLY),
1222 (stdout, ";; wrong query name:\n"),
1223 ans, (resplen > anssiz) ? anssiz : resplen);
Robert Greenwaltecd0e952012-01-11 10:04:48 -08001224 goto retry;;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001225 }
1226 if (anhp->rcode == SERVFAIL ||
1227 anhp->rcode == NOTIMP ||
1228 anhp->rcode == REFUSED) {
1229 DprintQ(statp->options & RES_DEBUG,
1230 (stdout, "server rejected query:\n"),
1231 ans, (resplen > anssiz) ? anssiz : resplen);
1232 res_nclose(statp);
1233 /* don't retry if called from dig */
1234 if (!statp->pfcode)
1235 return (0);
1236 }
1237 if (!(statp->options & RES_IGNTC) && anhp->tc) {
1238 /*
1239 * To get the rest of answer,
1240 * use TCP with same server.
1241 */
1242 Dprint(statp->options & RES_DEBUG,
1243 (stdout, ";; truncated answer\n"));
1244 *v_circuit = 1;
1245 res_nclose(statp);
1246 return (1);
1247 }
1248 /*
1249 * All is well, or the error is fatal. Signal that the
1250 * next nameserver ought not be tried.
1251 */
1252 return (resplen);
1253}
1254
1255static void
1256Aerror(const res_state statp, FILE *file, const char *string, int error,
1257 const struct sockaddr *address, int alen)
1258{
1259 int save = errno;
1260 char hbuf[NI_MAXHOST];
1261 char sbuf[NI_MAXSERV];
1262
1263 alen = alen;
1264
1265 if ((statp->options & RES_DEBUG) != 0U) {
1266 if (getnameinfo(address, (socklen_t)alen, hbuf, sizeof(hbuf),
1267 sbuf, sizeof(sbuf), niflags)) {
1268 strncpy(hbuf, "?", sizeof(hbuf) - 1);
1269 hbuf[sizeof(hbuf) - 1] = '\0';
1270 strncpy(sbuf, "?", sizeof(sbuf) - 1);
1271 sbuf[sizeof(sbuf) - 1] = '\0';
1272 }
1273 fprintf(file, "res_send: %s ([%s].%s): %s\n",
1274 string, hbuf, sbuf, strerror(error));
1275 }
1276 errno = save;
1277}
1278
1279static void
1280Perror(const res_state statp, FILE *file, const char *string, int error) {
1281 int save = errno;
1282
1283 if ((statp->options & RES_DEBUG) != 0U)
1284 fprintf(file, "res_send: %s: %s\n",
1285 string, strerror(error));
1286 errno = save;
1287}
1288
1289static int
1290sock_eq(struct sockaddr *a, struct sockaddr *b) {
1291 struct sockaddr_in *a4, *b4;
1292 struct sockaddr_in6 *a6, *b6;
1293
1294 if (a->sa_family != b->sa_family)
1295 return 0;
1296 switch (a->sa_family) {
1297 case AF_INET:
1298 a4 = (struct sockaddr_in *)(void *)a;
1299 b4 = (struct sockaddr_in *)(void *)b;
1300 return a4->sin_port == b4->sin_port &&
1301 a4->sin_addr.s_addr == b4->sin_addr.s_addr;
1302 case AF_INET6:
1303 a6 = (struct sockaddr_in6 *)(void *)a;
1304 b6 = (struct sockaddr_in6 *)(void *)b;
1305 return a6->sin6_port == b6->sin6_port &&
1306#ifdef HAVE_SIN6_SCOPE_ID
1307 a6->sin6_scope_id == b6->sin6_scope_id &&
1308#endif
1309 IN6_ARE_ADDR_EQUAL(&a6->sin6_addr, &b6->sin6_addr);
1310 default:
1311 return 0;
1312 }
1313}
1314
1315#ifdef NEED_PSELECT
1316/* XXX needs to move to the porting library. */
1317static int
1318pselect(int nfds, void *rfds, void *wfds, void *efds,
1319 struct timespec *tsp, const sigset_t *sigmask)
1320{
1321 struct timeval tv, *tvp;
1322 sigset_t sigs;
1323 int n;
1324
1325 if (tsp) {
1326 tvp = &tv;
1327 tv = evTimeVal(*tsp);
1328 } else
1329 tvp = NULL;
1330 if (sigmask)
1331 sigprocmask(SIG_SETMASK, sigmask, &sigs);
1332 n = select(nfds, rfds, wfds, efds, tvp);
1333 if (sigmask)
1334 sigprocmask(SIG_SETMASK, &sigs, NULL);
1335 if (tsp)
1336 *tsp = evTimeSpec(tv);
1337 return (n);
1338}
1339#endif