blob: 2f131f19713b80bac0194f5387065709456e20a9 [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
373 if (statp->nscount == 0) {
374 errno = ESRCH;
375 return (-1);
376 }
377 if (anssiz < HFIXEDSZ) {
378 errno = EINVAL;
379 return (-1);
380 }
381 DprintQ((statp->options & RES_DEBUG) || (statp->pfcode & RES_PRF_QUERY),
382 (stdout, ";; res_send()\n"), buf, buflen);
383 v_circuit = (statp->options & RES_USEVC) || buflen > PACKETSZ;
384 gotsomewhere = 0;
385 terrno = ETIMEDOUT;
386
387#if USE_RESOLV_CACHE
388 cache = __get_res_cache();
389 if (cache != NULL) {
390 int anslen = 0;
391 cache_status = _resolv_cache_lookup(
392 cache, buf, buflen,
393 ans, anssiz, &anslen);
394
395 if (cache_status == RESOLV_CACHE_FOUND) {
396 return anslen;
397 }
398 }
399#endif
400
401 /*
402 * If the ns_addr_list in the resolver context has changed, then
403 * invalidate our cached copy and the associated timing data.
404 */
405 if (EXT(statp).nscount != 0) {
406 int needclose = 0;
407 struct sockaddr_storage peer;
408 socklen_t peerlen;
409
410 if (EXT(statp).nscount != statp->nscount)
411 needclose++;
412 else
413 for (ns = 0; ns < statp->nscount; ns++) {
414 if (statp->nsaddr_list[ns].sin_family &&
415 !sock_eq((struct sockaddr *)(void *)&statp->nsaddr_list[ns],
416 (struct sockaddr *)(void *)&EXT(statp).ext->nsaddrs[ns])) {
417 needclose++;
418 break;
419 }
420
421 if (EXT(statp).nssocks[ns] == -1)
422 continue;
423 peerlen = sizeof(peer);
Jim Huang87043f92011-12-12 16:32:56 +0800424 if (getpeername(EXT(statp).nssocks[ns],
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800425 (struct sockaddr *)(void *)&peer, &peerlen) < 0) {
426 needclose++;
427 break;
428 }
429 if (!sock_eq((struct sockaddr *)(void *)&peer,
430 get_nsaddr(statp, (size_t)ns))) {
431 needclose++;
432 break;
433 }
434 }
435 if (needclose) {
436 res_nclose(statp);
437 EXT(statp).nscount = 0;
438 }
439 }
440
441 /*
442 * Maybe initialize our private copy of the ns_addr_list.
443 */
444 if (EXT(statp).nscount == 0) {
445 for (ns = 0; ns < statp->nscount; ns++) {
446 EXT(statp).nstimes[ns] = RES_MAXTIME;
447 EXT(statp).nssocks[ns] = -1;
448 if (!statp->nsaddr_list[ns].sin_family)
449 continue;
450 EXT(statp).ext->nsaddrs[ns].sin =
451 statp->nsaddr_list[ns];
452 }
453 EXT(statp).nscount = statp->nscount;
454 }
455
456 /*
457 * Some resolvers want to even out the load on their nameservers.
458 * Note that RES_BLAST overrides RES_ROTATE.
459 */
460 if ((statp->options & RES_ROTATE) != 0U &&
461 (statp->options & RES_BLAST) == 0U) {
462 union res_sockaddr_union inu;
463 struct sockaddr_in ina;
464 int lastns = statp->nscount - 1;
465 int fd;
466 u_int16_t nstime;
467
468 if (EXT(statp).ext != NULL)
469 inu = EXT(statp).ext->nsaddrs[0];
470 ina = statp->nsaddr_list[0];
471 fd = EXT(statp).nssocks[0];
472 nstime = EXT(statp).nstimes[0];
473 for (ns = 0; ns < lastns; ns++) {
474 if (EXT(statp).ext != NULL)
475 EXT(statp).ext->nsaddrs[ns] =
476 EXT(statp).ext->nsaddrs[ns + 1];
477 statp->nsaddr_list[ns] = statp->nsaddr_list[ns + 1];
478 EXT(statp).nssocks[ns] = EXT(statp).nssocks[ns + 1];
479 EXT(statp).nstimes[ns] = EXT(statp).nstimes[ns + 1];
480 }
481 if (EXT(statp).ext != NULL)
482 EXT(statp).ext->nsaddrs[lastns] = inu;
483 statp->nsaddr_list[lastns] = ina;
484 EXT(statp).nssocks[lastns] = fd;
485 EXT(statp).nstimes[lastns] = nstime;
486 }
487
488 /*
489 * Send request, RETRY times, or until successful.
490 */
491 for (try = 0; try < statp->retry; try++) {
492 for (ns = 0; ns < statp->nscount; ns++) {
493 struct sockaddr *nsap;
494 int nsaplen;
495 nsap = get_nsaddr(statp, (size_t)ns);
496 nsaplen = get_salen(nsap);
497 statp->_flags &= ~RES_F_LASTMASK;
498 statp->_flags |= (ns << RES_F_LASTSHIFT);
499 same_ns:
500 if (statp->qhook) {
501 int done = 0, loops = 0;
502
503 do {
504 res_sendhookact act;
505
506 act = (*statp->qhook)(&nsap, &buf, &buflen,
507 ans, anssiz, &resplen);
508 switch (act) {
509 case res_goahead:
510 done = 1;
511 break;
512 case res_nextns:
513 res_nclose(statp);
514 goto next_ns;
515 case res_done:
516 return (resplen);
517 case res_modified:
518 /* give the hook another try */
519 if (++loops < 42) /*doug adams*/
520 break;
521 /*FALLTHROUGH*/
522 case res_error:
523 /*FALLTHROUGH*/
524 default:
525 goto fail;
526 }
527 } while (!done);
528 }
529
530 Dprint(((statp->options & RES_DEBUG) &&
531 getnameinfo(nsap, (socklen_t)nsaplen, abuf, sizeof(abuf),
Robert Greenwaltecd0e952012-01-11 10:04:48 -0800532 NULL, 0, niflags) == 0),
533 (stdout, ";; Querying server (# %d) address = %s\n",
534 ns + 1, abuf));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800535
536
537 if (v_circuit) {
538 /* Use VC; at most one attempt per server. */
539 try = statp->retry;
Robert Greenwaltecd0e952012-01-11 10:04:48 -0800540
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800541 n = send_vc(statp, buf, buflen, ans, anssiz, &terrno,
542 ns);
Robert Greenwaltecd0e952012-01-11 10:04:48 -0800543
544 if (DBG) {
545 __libc_android_log_print(ANDROID_LOG_DEBUG, "libc",
546 "used send_vc %d\n", n);
547 }
548
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800549 if (n < 0)
550 goto fail;
551 if (n == 0)
552 goto next_ns;
553 resplen = n;
554 } else {
555 /* Use datagrams. */
Robert Greenwaltecd0e952012-01-11 10:04:48 -0800556 if (DBG) {
557 __libc_android_log_print(ANDROID_LOG_DEBUG, "libc",
558 "using send_dg\n");
559 }
560
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800561 n = send_dg(statp, buf, buflen, ans, anssiz, &terrno,
562 ns, &v_circuit, &gotsomewhere);
Robert Greenwaltecd0e952012-01-11 10:04:48 -0800563 if (DBG) {
564 __libc_android_log_print(ANDROID_LOG_DEBUG, "libc",
565 "used send_dg %d\n",n);
566 }
567
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800568 if (n < 0)
569 goto fail;
570 if (n == 0)
571 goto next_ns;
Robert Greenwaltecd0e952012-01-11 10:04:48 -0800572 if (DBG) {
573 __libc_android_log_print(ANDROID_LOG_DEBUG, "libc",
574 "time=%d, %d\n",time(NULL), time(NULL)%2);
575 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800576 if (v_circuit)
577 goto same_ns;
578 resplen = n;
579 }
580
581 Dprint((statp->options & RES_DEBUG) ||
582 ((statp->pfcode & RES_PRF_REPLY) &&
583 (statp->pfcode & RES_PRF_HEAD1)),
584 (stdout, ";; got answer:\n"));
585
586 DprintQ((statp->options & RES_DEBUG) ||
587 (statp->pfcode & RES_PRF_REPLY),
588 (stdout, "%s", ""),
589 ans, (resplen > anssiz) ? anssiz : resplen);
590
591#if USE_RESOLV_CACHE
592 if (cache_status == RESOLV_CACHE_NOTFOUND) {
593 _resolv_cache_add(cache, buf, buflen,
594 ans, resplen);
595 }
596#endif
597 /*
598 * If we have temporarily opened a virtual circuit,
599 * or if we haven't been asked to keep a socket open,
600 * close the socket.
601 */
602 if ((v_circuit && (statp->options & RES_USEVC) == 0U) ||
603 (statp->options & RES_STAYOPEN) == 0U) {
604 res_nclose(statp);
605 }
606 if (statp->rhook) {
607 int done = 0, loops = 0;
608
609 do {
610 res_sendhookact act;
611
612 act = (*statp->rhook)(nsap, buf, buflen,
613 ans, anssiz, &resplen);
614 switch (act) {
615 case res_goahead:
616 case res_done:
617 done = 1;
618 break;
619 case res_nextns:
620 res_nclose(statp);
621 goto next_ns;
622 case res_modified:
623 /* give the hook another try */
624 if (++loops < 42) /*doug adams*/
625 break;
626 /*FALLTHROUGH*/
627 case res_error:
628 /*FALLTHROUGH*/
629 default:
630 goto fail;
631 }
632 } while (!done);
633
634 }
635 return (resplen);
636 next_ns: ;
637 } /*foreach ns*/
638 } /*foreach retry*/
639 res_nclose(statp);
640 if (!v_circuit) {
641 if (!gotsomewhere)
642 errno = ECONNREFUSED; /* no nameservers found */
643 else
644 errno = ETIMEDOUT; /* no answer obtained */
645 } else
646 errno = terrno;
647 return (-1);
648 fail:
Mattias Falka59cfcf2011-09-06 15:15:06 +0200649#if USE_RESOLV_CACHE
650 _resolv_cache_query_failed(cache, buf, buflen);
651#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800652 res_nclose(statp);
653 return (-1);
654}
655
656/* Private */
657
658static int
659get_salen(sa)
660 const struct sockaddr *sa;
661{
662
663#ifdef HAVE_SA_LEN
664 /* There are people do not set sa_len. Be forgiving to them. */
665 if (sa->sa_len)
666 return (sa->sa_len);
667#endif
668
669 if (sa->sa_family == AF_INET)
670 return (sizeof(struct sockaddr_in));
671 else if (sa->sa_family == AF_INET6)
672 return (sizeof(struct sockaddr_in6));
673 else
674 return (0); /* unknown, die on connect */
675}
676
677/*
678 * pick appropriate nsaddr_list for use. see res_init() for initialization.
679 */
680static struct sockaddr *
681get_nsaddr(statp, n)
682 res_state statp;
683 size_t n;
684{
685
686 if (!statp->nsaddr_list[n].sin_family && EXT(statp).ext) {
687 /*
688 * - EXT(statp).ext->nsaddrs[n] holds an address that is larger
689 * than struct sockaddr, and
690 * - user code did not update statp->nsaddr_list[n].
691 */
692 return (struct sockaddr *)(void *)&EXT(statp).ext->nsaddrs[n];
693 } else {
694 /*
695 * - user code updated statp->nsaddr_list[n], or
696 * - statp->nsaddr_list[n] has the same content as
697 * EXT(statp).ext->nsaddrs[n].
698 */
699 return (struct sockaddr *)(void *)&statp->nsaddr_list[n];
700 }
701}
702
Robert Greenwaltecd0e952012-01-11 10:04:48 -0800703static int get_timeout(const res_state statp, const int ns)
704{
705 int timeout = (statp->retrans << ns);
706 if (ns > 0) {
707 timeout /= statp->nscount;
708 }
709 if (timeout <= 0) {
710 timeout = 1;
711 }
712 if (DBG) {
713 __libc_android_log_print(ANDROID_LOG_DEBUG, "libc",
714 "using timeout of %d sec\n", timeout);
715 }
716
717 return timeout;
718}
719
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800720static int
721send_vc(res_state statp,
722 const u_char *buf, int buflen, u_char *ans, int anssiz,
723 int *terrno, int ns)
724{
725 const HEADER *hp = (const HEADER *)(const void *)buf;
726 HEADER *anhp = (HEADER *)(void *)ans;
727 struct sockaddr *nsap;
728 int nsaplen;
729 int truncating, connreset, resplen, n;
730 struct iovec iov[2];
731 u_short len;
732 u_char *cp;
733 void *tmp;
734
Robert Greenwaltecd0e952012-01-11 10:04:48 -0800735 if (DBG) {
736 __libc_android_log_print(ANDROID_LOG_DEBUG, "libc", "using send_vc\n");
737 }
738
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800739 nsap = get_nsaddr(statp, (size_t)ns);
740 nsaplen = get_salen(nsap);
741
742 connreset = 0;
743 same_ns:
744 truncating = 0;
745
746 /* Are we still talking to whom we want to talk to? */
747 if (statp->_vcsock >= 0 && (statp->_flags & RES_F_VC) != 0) {
748 struct sockaddr_storage peer;
749 socklen_t size = sizeof peer;
750
751 if (getpeername(statp->_vcsock,
752 (struct sockaddr *)(void *)&peer, &size) < 0 ||
753 !sock_eq((struct sockaddr *)(void *)&peer, nsap)) {
754 res_nclose(statp);
755 statp->_flags &= ~RES_F_VC;
756 }
757 }
758
759 if (statp->_vcsock < 0 || (statp->_flags & RES_F_VC) == 0) {
760 if (statp->_vcsock >= 0)
761 res_nclose(statp);
762
763 statp->_vcsock = socket(nsap->sa_family, SOCK_STREAM, 0);
764 if (statp->_vcsock > highestFD) {
765 res_nclose(statp);
766 errno = ENOTSOCK;
767 }
768 if (statp->_vcsock < 0) {
769 switch (errno) {
770 case EPROTONOSUPPORT:
771#ifdef EPFNOSUPPORT
772 case EPFNOSUPPORT:
773#endif
774 case EAFNOSUPPORT:
775 Perror(statp, stderr, "socket(vc)", errno);
776 return (0);
777 default:
778 *terrno = errno;
779 Perror(statp, stderr, "socket(vc)", errno);
780 return (-1);
781 }
782 }
783 errno = 0;
784 if (random_bind(statp->_vcsock,nsap->sa_family) < 0) {
785 *terrno = errno;
786 Aerror(statp, stderr, "bind/vc", errno, nsap,
787 nsaplen);
788 res_nclose(statp);
789 return (0);
790 }
Robert Greenwaltecd0e952012-01-11 10:04:48 -0800791 if (connect_with_timeout(statp->_vcsock, nsap, (socklen_t)nsaplen,
792 get_timeout(statp, ns)) < 0) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800793 *terrno = errno;
794 Aerror(statp, stderr, "connect/vc", errno, nsap,
795 nsaplen);
796 res_nclose(statp);
797 return (0);
798 }
799 statp->_flags |= RES_F_VC;
800 }
801
802 /*
803 * Send length & message
804 */
805 ns_put16((u_short)buflen, (u_char*)(void *)&len);
806 iov[0] = evConsIovec(&len, INT16SZ);
807 DE_CONST(buf, tmp);
808 iov[1] = evConsIovec(tmp, (size_t)buflen);
809 if (writev(statp->_vcsock, iov, 2) != (INT16SZ + buflen)) {
810 *terrno = errno;
811 Perror(statp, stderr, "write failed", errno);
812 res_nclose(statp);
813 return (0);
814 }
815 /*
816 * Receive length & response
817 */
818 read_len:
819 cp = ans;
820 len = INT16SZ;
821 while ((n = read(statp->_vcsock, (char *)cp, (size_t)len)) > 0) {
822 cp += n;
823 if ((len -= n) == 0)
824 break;
825 }
826 if (n <= 0) {
827 *terrno = errno;
828 Perror(statp, stderr, "read failed", errno);
829 res_nclose(statp);
830 /*
831 * A long running process might get its TCP
832 * connection reset if the remote server was
833 * restarted. Requery the server instead of
834 * trying a new one. When there is only one
835 * server, this means that a query might work
836 * instead of failing. We only allow one reset
837 * per query to prevent looping.
838 */
839 if (*terrno == ECONNRESET && !connreset) {
840 connreset = 1;
841 res_nclose(statp);
842 goto same_ns;
843 }
844 res_nclose(statp);
845 return (0);
846 }
847 resplen = ns_get16(ans);
848 if (resplen > anssiz) {
849 Dprint(statp->options & RES_DEBUG,
850 (stdout, ";; response truncated\n")
851 );
852 truncating = 1;
853 len = anssiz;
854 } else
855 len = resplen;
856 if (len < HFIXEDSZ) {
857 /*
858 * Undersized message.
859 */
860 Dprint(statp->options & RES_DEBUG,
861 (stdout, ";; undersized: %d\n", len));
862 *terrno = EMSGSIZE;
863 res_nclose(statp);
864 return (0);
865 }
866 cp = ans;
867 while (len != 0 && (n = read(statp->_vcsock, (char *)cp, (size_t)len)) > 0){
868 cp += n;
869 len -= n;
870 }
871 if (n <= 0) {
872 *terrno = errno;
873 Perror(statp, stderr, "read(vc)", errno);
874 res_nclose(statp);
875 return (0);
876 }
877 if (truncating) {
878 /*
879 * Flush rest of answer so connection stays in synch.
880 */
881 anhp->tc = 1;
882 len = resplen - anssiz;
883 while (len != 0) {
884 char junk[PACKETSZ];
885
886 n = read(statp->_vcsock, junk,
887 (len > sizeof junk) ? sizeof junk : len);
888 if (n > 0)
889 len -= n;
890 else
891 break;
892 }
893 }
894 /*
895 * If the calling applicating has bailed out of
896 * a previous call and failed to arrange to have
897 * the circuit closed or the server has got
898 * itself confused, then drop the packet and
899 * wait for the correct one.
900 */
901 if (hp->id != anhp->id) {
902 DprintQ((statp->options & RES_DEBUG) ||
903 (statp->pfcode & RES_PRF_REPLY),
904 (stdout, ";; old answer (unexpected):\n"),
905 ans, (resplen > anssiz) ? anssiz: resplen);
906 goto read_len;
907 }
908
909 /*
910 * All is well, or the error is fatal. Signal that the
911 * next nameserver ought not be tried.
912 */
913 return (resplen);
914}
915
Robert Greenwaltecd0e952012-01-11 10:04:48 -0800916/* return -1 on error (errno set), 0 on success */
917static int
918connect_with_timeout(int sock, const struct sockaddr *nsap, socklen_t salen, int sec)
919{
920 int res, origflags;
921 fd_set rset, wset;
922 struct timespec now, timeout, finish;
923
924 origflags = fcntl(sock, F_GETFL, 0);
925 fcntl(sock, F_SETFL, origflags | O_NONBLOCK);
926
927 res = connect(sock, nsap, salen);
928 if (res < 0 && errno != EINPROGRESS) {
929 res = -1;
930 goto done;
931 }
932 if (res != 0) {
933 now = evNowTime();
934 timeout = evConsTime((long)sec, 0L);
935 finish = evAddTime(now, timeout);
936 if (DBG) {
937 __libc_android_log_print(ANDROID_LOG_DEBUG, "libc",
938 " %d send_vc\n", sock);
939 }
940
941 res = retrying_select(sock, &rset, &wset, &finish);
942 if (res <= 0) {
943 res = -1;
944 }
945 }
946done:
947 fcntl(sock, F_SETFL, origflags);
948 if (DBG) {
949 __libc_android_log_print(ANDROID_LOG_DEBUG, "libc",
950 " %d connect_with_timeout returning %s\n", sock, res);
951 }
952 return res;
953}
954
955static int
956retrying_select(const int sock, fd_set *readset, fd_set *writeset, const struct timespec *finish)
957{
958 struct timespec now, timeout;
959 int n, error;
960 socklen_t len;
961
962
963retry:
964 if (DBG) {
965 __libc_android_log_print(ANDROID_LOG_DEBUG, "libc", " %d retying_select\n", sock);
966 }
967
968 now = evNowTime();
969 if (readset) {
970 FD_ZERO(readset);
971 FD_SET(sock, readset);
972 }
973 if (writeset) {
974 FD_ZERO(writeset);
975 FD_SET(sock, writeset);
976 }
977 if (evCmpTime(*finish, now) > 0)
978 timeout = evSubTime(*finish, now);
979 else
980 timeout = evConsTime(0L, 0L);
981
982 n = pselect(sock + 1, readset, writeset, NULL, &timeout, NULL);
983 if (n == 0) {
984 if (DBG) {
985 __libc_android_log_print(ANDROID_LOG_DEBUG, " libc",
986 " %d retrying_select timeout\n", sock);
987 }
988 errno = ETIMEDOUT;
989 return 0;
990 }
991 if (n < 0) {
992 if (errno == EINTR)
993 goto retry;
994 if (DBG) {
995 __libc_android_log_print(ANDROID_LOG_DEBUG, "libc",
996 " %d retrying_select got error %d\n",sock, n);
997 }
998 return n;
999 }
1000 if ((readset && FD_ISSET(sock, readset)) || (writeset && FD_ISSET(sock, writeset))) {
1001 len = sizeof(error);
1002 if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, &len) < 0 || error) {
1003 errno = error;
1004 if (DBG) {
1005 __libc_android_log_print(ANDROID_LOG_DEBUG, "libc",
1006 " %d retrying_select dot error2 %d\n", sock, errno);
1007 }
1008
1009 return -1;
1010 }
1011 }
1012 if (DBG) {
1013 __libc_android_log_print(ANDROID_LOG_DEBUG, "libc",
1014 " %d retrying_select returning %d for %d\n",sock, n);
1015 }
1016
1017 return n;
1018}
1019
1020
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001021static int
1022send_dg(res_state statp,
1023 const u_char *buf, int buflen, u_char *ans, int anssiz,
1024 int *terrno, int ns, int *v_circuit, int *gotsomewhere)
1025{
1026 const HEADER *hp = (const HEADER *)(const void *)buf;
1027 HEADER *anhp = (HEADER *)(void *)ans;
1028 const struct sockaddr *nsap;
1029 int nsaplen;
1030 struct timespec now, timeout, finish;
1031 fd_set dsmask;
1032 struct sockaddr_storage from;
1033 socklen_t fromlen;
1034 int resplen, seconds, n, s;
1035
1036 nsap = get_nsaddr(statp, (size_t)ns);
1037 nsaplen = get_salen(nsap);
1038 if (EXT(statp).nssocks[ns] == -1) {
1039 EXT(statp).nssocks[ns] = socket(nsap->sa_family, SOCK_DGRAM, 0);
1040 if (EXT(statp).nssocks[ns] > highestFD) {
1041 res_nclose(statp);
1042 errno = ENOTSOCK;
1043 }
1044 if (EXT(statp).nssocks[ns] < 0) {
1045 switch (errno) {
1046 case EPROTONOSUPPORT:
1047#ifdef EPFNOSUPPORT
1048 case EPFNOSUPPORT:
1049#endif
1050 case EAFNOSUPPORT:
1051 Perror(statp, stderr, "socket(dg)", errno);
1052 return (0);
1053 default:
1054 *terrno = errno;
1055 Perror(statp, stderr, "socket(dg)", errno);
1056 return (-1);
1057 }
1058 }
1059#ifndef CANNOT_CONNECT_DGRAM
1060 /*
1061 * On a 4.3BSD+ machine (client and server,
1062 * actually), sending to a nameserver datagram
1063 * port with no nameserver will cause an
1064 * ICMP port unreachable message to be returned.
1065 * If our datagram socket is "connected" to the
1066 * server, we get an ECONNREFUSED error on the next
1067 * socket operation, and select returns if the
1068 * error message is received. We can thus detect
1069 * the absence of a nameserver without timing out.
1070 */
1071 if (random_bind(EXT(statp).nssocks[ns], nsap->sa_family) < 0) {
1072 Aerror(statp, stderr, "bind(dg)", errno, nsap,
1073 nsaplen);
1074 res_nclose(statp);
1075 return (0);
1076 }
1077 if (connect(EXT(statp).nssocks[ns], nsap, (socklen_t)nsaplen) < 0) {
1078 Aerror(statp, stderr, "connect(dg)", errno, nsap,
1079 nsaplen);
1080 res_nclose(statp);
1081 return (0);
1082 }
1083#endif /* !CANNOT_CONNECT_DGRAM */
1084 Dprint(statp->options & RES_DEBUG,
1085 (stdout, ";; new DG socket\n"))
1086 }
1087 s = EXT(statp).nssocks[ns];
1088#ifndef CANNOT_CONNECT_DGRAM
1089 if (send(s, (const char*)buf, (size_t)buflen, 0) != buflen) {
1090 Perror(statp, stderr, "send", errno);
1091 res_nclose(statp);
1092 return (0);
1093 }
1094#else /* !CANNOT_CONNECT_DGRAM */
1095 if (sendto(s, (const char*)buf, buflen, 0, nsap, nsaplen) != buflen)
1096 {
1097 Aerror(statp, stderr, "sendto", errno, nsap, nsaplen);
1098 res_nclose(statp);
1099 return (0);
1100 }
1101#endif /* !CANNOT_CONNECT_DGRAM */
1102
1103 /*
1104 * Wait for reply.
1105 */
Robert Greenwaltecd0e952012-01-11 10:04:48 -08001106 seconds = get_timeout(statp, ns);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001107 now = evNowTime();
1108 timeout = evConsTime((long)seconds, 0L);
1109 finish = evAddTime(now, timeout);
Robert Greenwaltecd0e952012-01-11 10:04:48 -08001110retry:
1111 n = retrying_select(s, &dsmask, NULL, &finish);
1112
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001113 if (n == 0) {
1114 Dprint(statp->options & RES_DEBUG, (stdout, ";; timeout\n"));
1115 *gotsomewhere = 1;
1116 return (0);
1117 }
1118 if (n < 0) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001119 Perror(statp, stderr, "select", errno);
1120 res_nclose(statp);
1121 return (0);
1122 }
1123 errno = 0;
1124 fromlen = sizeof(from);
1125 resplen = recvfrom(s, (char*)ans, (size_t)anssiz,0,
1126 (struct sockaddr *)(void *)&from, &fromlen);
1127 if (resplen <= 0) {
1128 Perror(statp, stderr, "recvfrom", errno);
1129 res_nclose(statp);
1130 return (0);
1131 }
1132 *gotsomewhere = 1;
1133 if (resplen < HFIXEDSZ) {
1134 /*
1135 * Undersized message.
1136 */
1137 Dprint(statp->options & RES_DEBUG,
1138 (stdout, ";; undersized: %d\n",
1139 resplen));
1140 *terrno = EMSGSIZE;
1141 res_nclose(statp);
1142 return (0);
1143 }
1144 if (hp->id != anhp->id) {
1145 /*
1146 * response from old query, ignore it.
1147 * XXX - potential security hazard could
1148 * be detected here.
1149 */
1150 DprintQ((statp->options & RES_DEBUG) ||
1151 (statp->pfcode & RES_PRF_REPLY),
1152 (stdout, ";; old answer:\n"),
1153 ans, (resplen > anssiz) ? anssiz : resplen);
Robert Greenwaltecd0e952012-01-11 10:04:48 -08001154 goto retry;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001155 }
1156 if (!(statp->options & RES_INSECURE1) &&
1157 !res_ourserver_p(statp, (struct sockaddr *)(void *)&from)) {
1158 /*
1159 * response from wrong server? ignore it.
1160 * XXX - potential security hazard could
1161 * be detected here.
1162 */
1163 DprintQ((statp->options & RES_DEBUG) ||
1164 (statp->pfcode & RES_PRF_REPLY),
1165 (stdout, ";; not our server:\n"),
1166 ans, (resplen > anssiz) ? anssiz : resplen);
Robert Greenwaltecd0e952012-01-11 10:04:48 -08001167 goto retry;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001168 }
1169#ifdef RES_USE_EDNS0
1170 if (anhp->rcode == FORMERR && (statp->options & RES_USE_EDNS0) != 0U) {
1171 /*
1172 * Do not retry if the server do not understand EDNS0.
1173 * The case has to be captured here, as FORMERR packet do not
1174 * carry query section, hence res_queriesmatch() returns 0.
1175 */
1176 DprintQ(statp->options & RES_DEBUG,
1177 (stdout, "server rejected query with EDNS0:\n"),
1178 ans, (resplen > anssiz) ? anssiz : resplen);
1179 /* record the error */
1180 statp->_flags |= RES_F_EDNS0ERR;
1181 res_nclose(statp);
1182 return (0);
1183 }
1184#endif
1185 if (!(statp->options & RES_INSECURE2) &&
1186 !res_queriesmatch(buf, buf + buflen,
1187 ans, ans + anssiz)) {
1188 /*
1189 * response contains wrong query? ignore it.
1190 * XXX - potential security hazard could
1191 * be detected here.
1192 */
1193 DprintQ((statp->options & RES_DEBUG) ||
1194 (statp->pfcode & RES_PRF_REPLY),
1195 (stdout, ";; wrong query name:\n"),
1196 ans, (resplen > anssiz) ? anssiz : resplen);
Robert Greenwaltecd0e952012-01-11 10:04:48 -08001197 goto retry;;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001198 }
1199 if (anhp->rcode == SERVFAIL ||
1200 anhp->rcode == NOTIMP ||
1201 anhp->rcode == REFUSED) {
1202 DprintQ(statp->options & RES_DEBUG,
1203 (stdout, "server rejected query:\n"),
1204 ans, (resplen > anssiz) ? anssiz : resplen);
1205 res_nclose(statp);
1206 /* don't retry if called from dig */
1207 if (!statp->pfcode)
1208 return (0);
1209 }
1210 if (!(statp->options & RES_IGNTC) && anhp->tc) {
1211 /*
1212 * To get the rest of answer,
1213 * use TCP with same server.
1214 */
1215 Dprint(statp->options & RES_DEBUG,
1216 (stdout, ";; truncated answer\n"));
1217 *v_circuit = 1;
1218 res_nclose(statp);
1219 return (1);
1220 }
1221 /*
1222 * All is well, or the error is fatal. Signal that the
1223 * next nameserver ought not be tried.
1224 */
1225 return (resplen);
1226}
1227
1228static void
1229Aerror(const res_state statp, FILE *file, const char *string, int error,
1230 const struct sockaddr *address, int alen)
1231{
1232 int save = errno;
1233 char hbuf[NI_MAXHOST];
1234 char sbuf[NI_MAXSERV];
1235
1236 alen = alen;
1237
1238 if ((statp->options & RES_DEBUG) != 0U) {
1239 if (getnameinfo(address, (socklen_t)alen, hbuf, sizeof(hbuf),
1240 sbuf, sizeof(sbuf), niflags)) {
1241 strncpy(hbuf, "?", sizeof(hbuf) - 1);
1242 hbuf[sizeof(hbuf) - 1] = '\0';
1243 strncpy(sbuf, "?", sizeof(sbuf) - 1);
1244 sbuf[sizeof(sbuf) - 1] = '\0';
1245 }
1246 fprintf(file, "res_send: %s ([%s].%s): %s\n",
1247 string, hbuf, sbuf, strerror(error));
1248 }
1249 errno = save;
1250}
1251
1252static void
1253Perror(const res_state statp, FILE *file, const char *string, int error) {
1254 int save = errno;
1255
1256 if ((statp->options & RES_DEBUG) != 0U)
1257 fprintf(file, "res_send: %s: %s\n",
1258 string, strerror(error));
1259 errno = save;
1260}
1261
1262static int
1263sock_eq(struct sockaddr *a, struct sockaddr *b) {
1264 struct sockaddr_in *a4, *b4;
1265 struct sockaddr_in6 *a6, *b6;
1266
1267 if (a->sa_family != b->sa_family)
1268 return 0;
1269 switch (a->sa_family) {
1270 case AF_INET:
1271 a4 = (struct sockaddr_in *)(void *)a;
1272 b4 = (struct sockaddr_in *)(void *)b;
1273 return a4->sin_port == b4->sin_port &&
1274 a4->sin_addr.s_addr == b4->sin_addr.s_addr;
1275 case AF_INET6:
1276 a6 = (struct sockaddr_in6 *)(void *)a;
1277 b6 = (struct sockaddr_in6 *)(void *)b;
1278 return a6->sin6_port == b6->sin6_port &&
1279#ifdef HAVE_SIN6_SCOPE_ID
1280 a6->sin6_scope_id == b6->sin6_scope_id &&
1281#endif
1282 IN6_ARE_ADDR_EQUAL(&a6->sin6_addr, &b6->sin6_addr);
1283 default:
1284 return 0;
1285 }
1286}
1287
1288#ifdef NEED_PSELECT
1289/* XXX needs to move to the porting library. */
1290static int
1291pselect(int nfds, void *rfds, void *wfds, void *efds,
1292 struct timespec *tsp, const sigset_t *sigmask)
1293{
1294 struct timeval tv, *tvp;
1295 sigset_t sigs;
1296 int n;
1297
1298 if (tsp) {
1299 tvp = &tv;
1300 tv = evTimeVal(*tsp);
1301 } else
1302 tvp = NULL;
1303 if (sigmask)
1304 sigprocmask(SIG_SETMASK, sigmask, &sigs);
1305 n = select(nfds, rfds, wfds, efds, tvp);
1306 if (sigmask)
1307 sigprocmask(SIG_SETMASK, &sigs, NULL);
1308 if (tsp)
1309 *tsp = evTimeSpec(tv);
1310 return (n);
1311}
1312#endif