blob: 53c492f3371ccdd4013910b039fd06f6403b6c10 [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:
649 res_nclose(statp);
650 return (-1);
651}
652
653/* Private */
654
655static int
656get_salen(sa)
657 const struct sockaddr *sa;
658{
659
660#ifdef HAVE_SA_LEN
661 /* There are people do not set sa_len. Be forgiving to them. */
662 if (sa->sa_len)
663 return (sa->sa_len);
664#endif
665
666 if (sa->sa_family == AF_INET)
667 return (sizeof(struct sockaddr_in));
668 else if (sa->sa_family == AF_INET6)
669 return (sizeof(struct sockaddr_in6));
670 else
671 return (0); /* unknown, die on connect */
672}
673
674/*
675 * pick appropriate nsaddr_list for use. see res_init() for initialization.
676 */
677static struct sockaddr *
678get_nsaddr(statp, n)
679 res_state statp;
680 size_t n;
681{
682
683 if (!statp->nsaddr_list[n].sin_family && EXT(statp).ext) {
684 /*
685 * - EXT(statp).ext->nsaddrs[n] holds an address that is larger
686 * than struct sockaddr, and
687 * - user code did not update statp->nsaddr_list[n].
688 */
689 return (struct sockaddr *)(void *)&EXT(statp).ext->nsaddrs[n];
690 } else {
691 /*
692 * - user code updated statp->nsaddr_list[n], or
693 * - statp->nsaddr_list[n] has the same content as
694 * EXT(statp).ext->nsaddrs[n].
695 */
696 return (struct sockaddr *)(void *)&statp->nsaddr_list[n];
697 }
698}
699
Robert Greenwaltecd0e952012-01-11 10:04:48 -0800700static int get_timeout(const res_state statp, const int ns)
701{
702 int timeout = (statp->retrans << ns);
703 if (ns > 0) {
704 timeout /= statp->nscount;
705 }
706 if (timeout <= 0) {
707 timeout = 1;
708 }
709 if (DBG) {
710 __libc_android_log_print(ANDROID_LOG_DEBUG, "libc",
711 "using timeout of %d sec\n", timeout);
712 }
713
714 return timeout;
715}
716
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800717static int
718send_vc(res_state statp,
719 const u_char *buf, int buflen, u_char *ans, int anssiz,
720 int *terrno, int ns)
721{
722 const HEADER *hp = (const HEADER *)(const void *)buf;
723 HEADER *anhp = (HEADER *)(void *)ans;
724 struct sockaddr *nsap;
725 int nsaplen;
726 int truncating, connreset, resplen, n;
727 struct iovec iov[2];
728 u_short len;
729 u_char *cp;
730 void *tmp;
731
Robert Greenwaltecd0e952012-01-11 10:04:48 -0800732 if (DBG) {
733 __libc_android_log_print(ANDROID_LOG_DEBUG, "libc", "using send_vc\n");
734 }
735
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800736 nsap = get_nsaddr(statp, (size_t)ns);
737 nsaplen = get_salen(nsap);
738
739 connreset = 0;
740 same_ns:
741 truncating = 0;
742
743 /* Are we still talking to whom we want to talk to? */
744 if (statp->_vcsock >= 0 && (statp->_flags & RES_F_VC) != 0) {
745 struct sockaddr_storage peer;
746 socklen_t size = sizeof peer;
747
748 if (getpeername(statp->_vcsock,
749 (struct sockaddr *)(void *)&peer, &size) < 0 ||
750 !sock_eq((struct sockaddr *)(void *)&peer, nsap)) {
751 res_nclose(statp);
752 statp->_flags &= ~RES_F_VC;
753 }
754 }
755
756 if (statp->_vcsock < 0 || (statp->_flags & RES_F_VC) == 0) {
757 if (statp->_vcsock >= 0)
758 res_nclose(statp);
759
760 statp->_vcsock = socket(nsap->sa_family, SOCK_STREAM, 0);
761 if (statp->_vcsock > highestFD) {
762 res_nclose(statp);
763 errno = ENOTSOCK;
764 }
765 if (statp->_vcsock < 0) {
766 switch (errno) {
767 case EPROTONOSUPPORT:
768#ifdef EPFNOSUPPORT
769 case EPFNOSUPPORT:
770#endif
771 case EAFNOSUPPORT:
772 Perror(statp, stderr, "socket(vc)", errno);
773 return (0);
774 default:
775 *terrno = errno;
776 Perror(statp, stderr, "socket(vc)", errno);
777 return (-1);
778 }
779 }
780 errno = 0;
781 if (random_bind(statp->_vcsock,nsap->sa_family) < 0) {
782 *terrno = errno;
783 Aerror(statp, stderr, "bind/vc", errno, nsap,
784 nsaplen);
785 res_nclose(statp);
786 return (0);
787 }
Robert Greenwaltecd0e952012-01-11 10:04:48 -0800788 if (connect_with_timeout(statp->_vcsock, nsap, (socklen_t)nsaplen,
789 get_timeout(statp, ns)) < 0) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800790 *terrno = errno;
791 Aerror(statp, stderr, "connect/vc", errno, nsap,
792 nsaplen);
793 res_nclose(statp);
794 return (0);
795 }
796 statp->_flags |= RES_F_VC;
797 }
798
799 /*
800 * Send length & message
801 */
802 ns_put16((u_short)buflen, (u_char*)(void *)&len);
803 iov[0] = evConsIovec(&len, INT16SZ);
804 DE_CONST(buf, tmp);
805 iov[1] = evConsIovec(tmp, (size_t)buflen);
806 if (writev(statp->_vcsock, iov, 2) != (INT16SZ + buflen)) {
807 *terrno = errno;
808 Perror(statp, stderr, "write failed", errno);
809 res_nclose(statp);
810 return (0);
811 }
812 /*
813 * Receive length & response
814 */
815 read_len:
816 cp = ans;
817 len = INT16SZ;
818 while ((n = read(statp->_vcsock, (char *)cp, (size_t)len)) > 0) {
819 cp += n;
820 if ((len -= n) == 0)
821 break;
822 }
823 if (n <= 0) {
824 *terrno = errno;
825 Perror(statp, stderr, "read failed", errno);
826 res_nclose(statp);
827 /*
828 * A long running process might get its TCP
829 * connection reset if the remote server was
830 * restarted. Requery the server instead of
831 * trying a new one. When there is only one
832 * server, this means that a query might work
833 * instead of failing. We only allow one reset
834 * per query to prevent looping.
835 */
836 if (*terrno == ECONNRESET && !connreset) {
837 connreset = 1;
838 res_nclose(statp);
839 goto same_ns;
840 }
841 res_nclose(statp);
842 return (0);
843 }
844 resplen = ns_get16(ans);
845 if (resplen > anssiz) {
846 Dprint(statp->options & RES_DEBUG,
847 (stdout, ";; response truncated\n")
848 );
849 truncating = 1;
850 len = anssiz;
851 } else
852 len = resplen;
853 if (len < HFIXEDSZ) {
854 /*
855 * Undersized message.
856 */
857 Dprint(statp->options & RES_DEBUG,
858 (stdout, ";; undersized: %d\n", len));
859 *terrno = EMSGSIZE;
860 res_nclose(statp);
861 return (0);
862 }
863 cp = ans;
864 while (len != 0 && (n = read(statp->_vcsock, (char *)cp, (size_t)len)) > 0){
865 cp += n;
866 len -= n;
867 }
868 if (n <= 0) {
869 *terrno = errno;
870 Perror(statp, stderr, "read(vc)", errno);
871 res_nclose(statp);
872 return (0);
873 }
874 if (truncating) {
875 /*
876 * Flush rest of answer so connection stays in synch.
877 */
878 anhp->tc = 1;
879 len = resplen - anssiz;
880 while (len != 0) {
881 char junk[PACKETSZ];
882
883 n = read(statp->_vcsock, junk,
884 (len > sizeof junk) ? sizeof junk : len);
885 if (n > 0)
886 len -= n;
887 else
888 break;
889 }
890 }
891 /*
892 * If the calling applicating has bailed out of
893 * a previous call and failed to arrange to have
894 * the circuit closed or the server has got
895 * itself confused, then drop the packet and
896 * wait for the correct one.
897 */
898 if (hp->id != anhp->id) {
899 DprintQ((statp->options & RES_DEBUG) ||
900 (statp->pfcode & RES_PRF_REPLY),
901 (stdout, ";; old answer (unexpected):\n"),
902 ans, (resplen > anssiz) ? anssiz: resplen);
903 goto read_len;
904 }
905
906 /*
907 * All is well, or the error is fatal. Signal that the
908 * next nameserver ought not be tried.
909 */
910 return (resplen);
911}
912
Robert Greenwaltecd0e952012-01-11 10:04:48 -0800913/* return -1 on error (errno set), 0 on success */
914static int
915connect_with_timeout(int sock, const struct sockaddr *nsap, socklen_t salen, int sec)
916{
917 int res, origflags;
918 fd_set rset, wset;
919 struct timespec now, timeout, finish;
920
921 origflags = fcntl(sock, F_GETFL, 0);
922 fcntl(sock, F_SETFL, origflags | O_NONBLOCK);
923
924 res = connect(sock, nsap, salen);
925 if (res < 0 && errno != EINPROGRESS) {
926 res = -1;
927 goto done;
928 }
929 if (res != 0) {
930 now = evNowTime();
931 timeout = evConsTime((long)sec, 0L);
932 finish = evAddTime(now, timeout);
933 if (DBG) {
934 __libc_android_log_print(ANDROID_LOG_DEBUG, "libc",
935 " %d send_vc\n", sock);
936 }
937
938 res = retrying_select(sock, &rset, &wset, &finish);
939 if (res <= 0) {
940 res = -1;
941 }
942 }
943done:
944 fcntl(sock, F_SETFL, origflags);
945 if (DBG) {
946 __libc_android_log_print(ANDROID_LOG_DEBUG, "libc",
947 " %d connect_with_timeout returning %s\n", sock, res);
948 }
949 return res;
950}
951
952static int
953retrying_select(const int sock, fd_set *readset, fd_set *writeset, const struct timespec *finish)
954{
955 struct timespec now, timeout;
956 int n, error;
957 socklen_t len;
958
959
960retry:
961 if (DBG) {
962 __libc_android_log_print(ANDROID_LOG_DEBUG, "libc", " %d retying_select\n", sock);
963 }
964
965 now = evNowTime();
966 if (readset) {
967 FD_ZERO(readset);
968 FD_SET(sock, readset);
969 }
970 if (writeset) {
971 FD_ZERO(writeset);
972 FD_SET(sock, writeset);
973 }
974 if (evCmpTime(*finish, now) > 0)
975 timeout = evSubTime(*finish, now);
976 else
977 timeout = evConsTime(0L, 0L);
978
979 n = pselect(sock + 1, readset, writeset, NULL, &timeout, NULL);
980 if (n == 0) {
981 if (DBG) {
982 __libc_android_log_print(ANDROID_LOG_DEBUG, " libc",
983 " %d retrying_select timeout\n", sock);
984 }
985 errno = ETIMEDOUT;
986 return 0;
987 }
988 if (n < 0) {
989 if (errno == EINTR)
990 goto retry;
991 if (DBG) {
992 __libc_android_log_print(ANDROID_LOG_DEBUG, "libc",
993 " %d retrying_select got error %d\n",sock, n);
994 }
995 return n;
996 }
997 if ((readset && FD_ISSET(sock, readset)) || (writeset && FD_ISSET(sock, writeset))) {
998 len = sizeof(error);
999 if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, &len) < 0 || error) {
1000 errno = error;
1001 if (DBG) {
1002 __libc_android_log_print(ANDROID_LOG_DEBUG, "libc",
1003 " %d retrying_select dot error2 %d\n", sock, errno);
1004 }
1005
1006 return -1;
1007 }
1008 }
1009 if (DBG) {
1010 __libc_android_log_print(ANDROID_LOG_DEBUG, "libc",
1011 " %d retrying_select returning %d for %d\n",sock, n);
1012 }
1013
1014 return n;
1015}
1016
1017
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001018static int
1019send_dg(res_state statp,
1020 const u_char *buf, int buflen, u_char *ans, int anssiz,
1021 int *terrno, int ns, int *v_circuit, int *gotsomewhere)
1022{
1023 const HEADER *hp = (const HEADER *)(const void *)buf;
1024 HEADER *anhp = (HEADER *)(void *)ans;
1025 const struct sockaddr *nsap;
1026 int nsaplen;
1027 struct timespec now, timeout, finish;
1028 fd_set dsmask;
1029 struct sockaddr_storage from;
1030 socklen_t fromlen;
1031 int resplen, seconds, n, s;
1032
1033 nsap = get_nsaddr(statp, (size_t)ns);
1034 nsaplen = get_salen(nsap);
1035 if (EXT(statp).nssocks[ns] == -1) {
1036 EXT(statp).nssocks[ns] = socket(nsap->sa_family, SOCK_DGRAM, 0);
1037 if (EXT(statp).nssocks[ns] > highestFD) {
1038 res_nclose(statp);
1039 errno = ENOTSOCK;
1040 }
1041 if (EXT(statp).nssocks[ns] < 0) {
1042 switch (errno) {
1043 case EPROTONOSUPPORT:
1044#ifdef EPFNOSUPPORT
1045 case EPFNOSUPPORT:
1046#endif
1047 case EAFNOSUPPORT:
1048 Perror(statp, stderr, "socket(dg)", errno);
1049 return (0);
1050 default:
1051 *terrno = errno;
1052 Perror(statp, stderr, "socket(dg)", errno);
1053 return (-1);
1054 }
1055 }
1056#ifndef CANNOT_CONNECT_DGRAM
1057 /*
1058 * On a 4.3BSD+ machine (client and server,
1059 * actually), sending to a nameserver datagram
1060 * port with no nameserver will cause an
1061 * ICMP port unreachable message to be returned.
1062 * If our datagram socket is "connected" to the
1063 * server, we get an ECONNREFUSED error on the next
1064 * socket operation, and select returns if the
1065 * error message is received. We can thus detect
1066 * the absence of a nameserver without timing out.
1067 */
1068 if (random_bind(EXT(statp).nssocks[ns], nsap->sa_family) < 0) {
1069 Aerror(statp, stderr, "bind(dg)", errno, nsap,
1070 nsaplen);
1071 res_nclose(statp);
1072 return (0);
1073 }
1074 if (connect(EXT(statp).nssocks[ns], nsap, (socklen_t)nsaplen) < 0) {
1075 Aerror(statp, stderr, "connect(dg)", errno, nsap,
1076 nsaplen);
1077 res_nclose(statp);
1078 return (0);
1079 }
1080#endif /* !CANNOT_CONNECT_DGRAM */
1081 Dprint(statp->options & RES_DEBUG,
1082 (stdout, ";; new DG socket\n"))
1083 }
1084 s = EXT(statp).nssocks[ns];
1085#ifndef CANNOT_CONNECT_DGRAM
1086 if (send(s, (const char*)buf, (size_t)buflen, 0) != buflen) {
1087 Perror(statp, stderr, "send", errno);
1088 res_nclose(statp);
1089 return (0);
1090 }
1091#else /* !CANNOT_CONNECT_DGRAM */
1092 if (sendto(s, (const char*)buf, buflen, 0, nsap, nsaplen) != buflen)
1093 {
1094 Aerror(statp, stderr, "sendto", errno, nsap, nsaplen);
1095 res_nclose(statp);
1096 return (0);
1097 }
1098#endif /* !CANNOT_CONNECT_DGRAM */
1099
1100 /*
1101 * Wait for reply.
1102 */
Robert Greenwaltecd0e952012-01-11 10:04:48 -08001103 seconds = get_timeout(statp, ns);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001104 now = evNowTime();
1105 timeout = evConsTime((long)seconds, 0L);
1106 finish = evAddTime(now, timeout);
Robert Greenwaltecd0e952012-01-11 10:04:48 -08001107retry:
1108 n = retrying_select(s, &dsmask, NULL, &finish);
1109
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001110 if (n == 0) {
1111 Dprint(statp->options & RES_DEBUG, (stdout, ";; timeout\n"));
1112 *gotsomewhere = 1;
1113 return (0);
1114 }
1115 if (n < 0) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001116 Perror(statp, stderr, "select", errno);
1117 res_nclose(statp);
1118 return (0);
1119 }
1120 errno = 0;
1121 fromlen = sizeof(from);
1122 resplen = recvfrom(s, (char*)ans, (size_t)anssiz,0,
1123 (struct sockaddr *)(void *)&from, &fromlen);
1124 if (resplen <= 0) {
1125 Perror(statp, stderr, "recvfrom", errno);
1126 res_nclose(statp);
1127 return (0);
1128 }
1129 *gotsomewhere = 1;
1130 if (resplen < HFIXEDSZ) {
1131 /*
1132 * Undersized message.
1133 */
1134 Dprint(statp->options & RES_DEBUG,
1135 (stdout, ";; undersized: %d\n",
1136 resplen));
1137 *terrno = EMSGSIZE;
1138 res_nclose(statp);
1139 return (0);
1140 }
1141 if (hp->id != anhp->id) {
1142 /*
1143 * response from old query, ignore it.
1144 * XXX - potential security hazard could
1145 * be detected here.
1146 */
Geremy Condra524c87c2012-06-08 21:06:33 -07001147#ifdef ANDROID_CHANGES
1148 __libc_android_log_event_uid(BIONIC_EVENT_RESOLVER_OLD_RESPONSE);
1149#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001150 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 */
Geremy Condra524c87c2012-06-08 21:06:33 -07001163#ifdef ANDROID_CHANGES
1164 __libc_android_log_event_uid(BIONIC_EVENT_RESOLVER_WRONG_SERVER);
1165#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001166 DprintQ((statp->options & RES_DEBUG) ||
1167 (statp->pfcode & RES_PRF_REPLY),
1168 (stdout, ";; not our server:\n"),
1169 ans, (resplen > anssiz) ? anssiz : resplen);
Robert Greenwaltecd0e952012-01-11 10:04:48 -08001170 goto retry;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001171 }
1172#ifdef RES_USE_EDNS0
1173 if (anhp->rcode == FORMERR && (statp->options & RES_USE_EDNS0) != 0U) {
1174 /*
1175 * Do not retry if the server do not understand EDNS0.
1176 * The case has to be captured here, as FORMERR packet do not
1177 * carry query section, hence res_queriesmatch() returns 0.
1178 */
1179 DprintQ(statp->options & RES_DEBUG,
1180 (stdout, "server rejected query with EDNS0:\n"),
1181 ans, (resplen > anssiz) ? anssiz : resplen);
1182 /* record the error */
1183 statp->_flags |= RES_F_EDNS0ERR;
1184 res_nclose(statp);
1185 return (0);
1186 }
1187#endif
1188 if (!(statp->options & RES_INSECURE2) &&
1189 !res_queriesmatch(buf, buf + buflen,
1190 ans, ans + anssiz)) {
1191 /*
1192 * response contains wrong query? ignore it.
1193 * XXX - potential security hazard could
1194 * be detected here.
1195 */
Geremy Condra524c87c2012-06-08 21:06:33 -07001196#ifdef ANDROID_CHANGES
1197 __libc_android_log_event_uid(BIONIC_EVENT_RESOLVER_WRONG_QUERY);
1198#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001199 DprintQ((statp->options & RES_DEBUG) ||
1200 (statp->pfcode & RES_PRF_REPLY),
1201 (stdout, ";; wrong query name:\n"),
1202 ans, (resplen > anssiz) ? anssiz : resplen);
Robert Greenwaltecd0e952012-01-11 10:04:48 -08001203 goto retry;;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001204 }
1205 if (anhp->rcode == SERVFAIL ||
1206 anhp->rcode == NOTIMP ||
1207 anhp->rcode == REFUSED) {
1208 DprintQ(statp->options & RES_DEBUG,
1209 (stdout, "server rejected query:\n"),
1210 ans, (resplen > anssiz) ? anssiz : resplen);
1211 res_nclose(statp);
1212 /* don't retry if called from dig */
1213 if (!statp->pfcode)
1214 return (0);
1215 }
1216 if (!(statp->options & RES_IGNTC) && anhp->tc) {
1217 /*
1218 * To get the rest of answer,
1219 * use TCP with same server.
1220 */
1221 Dprint(statp->options & RES_DEBUG,
1222 (stdout, ";; truncated answer\n"));
1223 *v_circuit = 1;
1224 res_nclose(statp);
1225 return (1);
1226 }
1227 /*
1228 * All is well, or the error is fatal. Signal that the
1229 * next nameserver ought not be tried.
1230 */
1231 return (resplen);
1232}
1233
1234static void
1235Aerror(const res_state statp, FILE *file, const char *string, int error,
1236 const struct sockaddr *address, int alen)
1237{
1238 int save = errno;
1239 char hbuf[NI_MAXHOST];
1240 char sbuf[NI_MAXSERV];
1241
1242 alen = alen;
1243
1244 if ((statp->options & RES_DEBUG) != 0U) {
1245 if (getnameinfo(address, (socklen_t)alen, hbuf, sizeof(hbuf),
1246 sbuf, sizeof(sbuf), niflags)) {
1247 strncpy(hbuf, "?", sizeof(hbuf) - 1);
1248 hbuf[sizeof(hbuf) - 1] = '\0';
1249 strncpy(sbuf, "?", sizeof(sbuf) - 1);
1250 sbuf[sizeof(sbuf) - 1] = '\0';
1251 }
1252 fprintf(file, "res_send: %s ([%s].%s): %s\n",
1253 string, hbuf, sbuf, strerror(error));
1254 }
1255 errno = save;
1256}
1257
1258static void
1259Perror(const res_state statp, FILE *file, const char *string, int error) {
1260 int save = errno;
1261
1262 if ((statp->options & RES_DEBUG) != 0U)
1263 fprintf(file, "res_send: %s: %s\n",
1264 string, strerror(error));
1265 errno = save;
1266}
1267
1268static int
1269sock_eq(struct sockaddr *a, struct sockaddr *b) {
1270 struct sockaddr_in *a4, *b4;
1271 struct sockaddr_in6 *a6, *b6;
1272
1273 if (a->sa_family != b->sa_family)
1274 return 0;
1275 switch (a->sa_family) {
1276 case AF_INET:
1277 a4 = (struct sockaddr_in *)(void *)a;
1278 b4 = (struct sockaddr_in *)(void *)b;
1279 return a4->sin_port == b4->sin_port &&
1280 a4->sin_addr.s_addr == b4->sin_addr.s_addr;
1281 case AF_INET6:
1282 a6 = (struct sockaddr_in6 *)(void *)a;
1283 b6 = (struct sockaddr_in6 *)(void *)b;
1284 return a6->sin6_port == b6->sin6_port &&
1285#ifdef HAVE_SIN6_SCOPE_ID
1286 a6->sin6_scope_id == b6->sin6_scope_id &&
1287#endif
1288 IN6_ARE_ADDR_EQUAL(&a6->sin6_addr, &b6->sin6_addr);
1289 default:
1290 return 0;
1291 }
1292}
1293
1294#ifdef NEED_PSELECT
1295/* XXX needs to move to the porting library. */
1296static int
1297pselect(int nfds, void *rfds, void *wfds, void *efds,
1298 struct timespec *tsp, const sigset_t *sigmask)
1299{
1300 struct timeval tv, *tvp;
1301 sigset_t sigs;
1302 int n;
1303
1304 if (tsp) {
1305 tvp = &tv;
1306 tv = evTimeVal(*tsp);
1307 } else
1308 tvp = NULL;
1309 if (sigmask)
1310 sigprocmask(SIG_SETMASK, sigmask, &sigs);
1311 n = select(nfds, rfds, wfds, efds, tvp);
1312 if (sigmask)
1313 sigprocmask(SIG_SETMASK, &sigs, NULL);
1314 if (tsp)
1315 *tsp = evTimeSpec(tv);
1316 return (n);
1317}
1318#endif