blob: c6c863bfa116d518533e2cb8ff456fc8f5ee6b1b [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
Elliott Hughes8f2a5a02013-03-15 15:30:25 -0700122#include "libc_logging.h"
Robert Greenwaltecd0e952012-01-11 10:04:48 -0800123
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) {
Elliott Hughes8f2a5a02013-03-15 15:30:25 -0700545 __libc_format_log(ANDROID_LOG_DEBUG, "libc",
Robert Greenwaltecd0e952012-01-11 10:04:48 -0800546 "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) {
Elliott Hughes8f2a5a02013-03-15 15:30:25 -0700557 __libc_format_log(ANDROID_LOG_DEBUG, "libc", "using send_dg\n");
Robert Greenwaltecd0e952012-01-11 10:04:48 -0800558 }
559
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800560 n = send_dg(statp, buf, buflen, ans, anssiz, &terrno,
561 ns, &v_circuit, &gotsomewhere);
Robert Greenwaltecd0e952012-01-11 10:04:48 -0800562 if (DBG) {
Elliott Hughes8f2a5a02013-03-15 15:30:25 -0700563 __libc_format_log(ANDROID_LOG_DEBUG, "libc", "used send_dg %d\n",n);
Robert Greenwaltecd0e952012-01-11 10:04:48 -0800564 }
565
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800566 if (n < 0)
567 goto fail;
568 if (n == 0)
569 goto next_ns;
Robert Greenwaltecd0e952012-01-11 10:04:48 -0800570 if (DBG) {
Elliott Hughes8f2a5a02013-03-15 15:30:25 -0700571 __libc_format_log(ANDROID_LOG_DEBUG, "libc",
Robert Greenwaltecd0e952012-01-11 10:04:48 -0800572 "time=%d, %d\n",time(NULL), time(NULL)%2);
573 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800574 if (v_circuit)
575 goto same_ns;
576 resplen = n;
577 }
578
579 Dprint((statp->options & RES_DEBUG) ||
580 ((statp->pfcode & RES_PRF_REPLY) &&
581 (statp->pfcode & RES_PRF_HEAD1)),
582 (stdout, ";; got answer:\n"));
583
584 DprintQ((statp->options & RES_DEBUG) ||
585 (statp->pfcode & RES_PRF_REPLY),
586 (stdout, "%s", ""),
587 ans, (resplen > anssiz) ? anssiz : resplen);
588
589#if USE_RESOLV_CACHE
590 if (cache_status == RESOLV_CACHE_NOTFOUND) {
591 _resolv_cache_add(cache, buf, buflen,
592 ans, resplen);
593 }
594#endif
595 /*
596 * If we have temporarily opened a virtual circuit,
597 * or if we haven't been asked to keep a socket open,
598 * close the socket.
599 */
600 if ((v_circuit && (statp->options & RES_USEVC) == 0U) ||
601 (statp->options & RES_STAYOPEN) == 0U) {
602 res_nclose(statp);
603 }
604 if (statp->rhook) {
605 int done = 0, loops = 0;
606
607 do {
608 res_sendhookact act;
609
610 act = (*statp->rhook)(nsap, buf, buflen,
611 ans, anssiz, &resplen);
612 switch (act) {
613 case res_goahead:
614 case res_done:
615 done = 1;
616 break;
617 case res_nextns:
618 res_nclose(statp);
619 goto next_ns;
620 case res_modified:
621 /* give the hook another try */
622 if (++loops < 42) /*doug adams*/
623 break;
624 /*FALLTHROUGH*/
625 case res_error:
626 /*FALLTHROUGH*/
627 default:
628 goto fail;
629 }
630 } while (!done);
631
632 }
633 return (resplen);
634 next_ns: ;
635 } /*foreach ns*/
636 } /*foreach retry*/
637 res_nclose(statp);
638 if (!v_circuit) {
639 if (!gotsomewhere)
640 errno = ECONNREFUSED; /* no nameservers found */
641 else
642 errno = ETIMEDOUT; /* no answer obtained */
643 } else
644 errno = terrno;
Henrik Engströmce5ba8b2012-06-20 08:47:52 +0200645
646#if USE_RESOLV_CACHE
647 _resolv_cache_query_failed(cache, buf, buflen);
648#endif
649
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800650 return (-1);
651 fail:
Mattias Falka59cfcf2011-09-06 15:15:06 +0200652#if USE_RESOLV_CACHE
653 _resolv_cache_query_failed(cache, buf, buflen);
654#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800655 res_nclose(statp);
656 return (-1);
657}
658
659/* Private */
660
661static int
662get_salen(sa)
663 const struct sockaddr *sa;
664{
665
666#ifdef HAVE_SA_LEN
667 /* There are people do not set sa_len. Be forgiving to them. */
668 if (sa->sa_len)
669 return (sa->sa_len);
670#endif
671
672 if (sa->sa_family == AF_INET)
673 return (sizeof(struct sockaddr_in));
674 else if (sa->sa_family == AF_INET6)
675 return (sizeof(struct sockaddr_in6));
676 else
677 return (0); /* unknown, die on connect */
678}
679
680/*
681 * pick appropriate nsaddr_list for use. see res_init() for initialization.
682 */
683static struct sockaddr *
684get_nsaddr(statp, n)
685 res_state statp;
686 size_t n;
687{
688
689 if (!statp->nsaddr_list[n].sin_family && EXT(statp).ext) {
690 /*
691 * - EXT(statp).ext->nsaddrs[n] holds an address that is larger
692 * than struct sockaddr, and
693 * - user code did not update statp->nsaddr_list[n].
694 */
695 return (struct sockaddr *)(void *)&EXT(statp).ext->nsaddrs[n];
696 } else {
697 /*
698 * - user code updated statp->nsaddr_list[n], or
699 * - statp->nsaddr_list[n] has the same content as
700 * EXT(statp).ext->nsaddrs[n].
701 */
702 return (struct sockaddr *)(void *)&statp->nsaddr_list[n];
703 }
704}
705
Robert Greenwaltecd0e952012-01-11 10:04:48 -0800706static int get_timeout(const res_state statp, const int ns)
707{
708 int timeout = (statp->retrans << ns);
709 if (ns > 0) {
710 timeout /= statp->nscount;
711 }
712 if (timeout <= 0) {
713 timeout = 1;
714 }
715 if (DBG) {
Elliott Hughes8f2a5a02013-03-15 15:30:25 -0700716 __libc_format_log(ANDROID_LOG_DEBUG, "libc", "using timeout of %d sec\n", timeout);
Robert Greenwaltecd0e952012-01-11 10:04:48 -0800717 }
718
719 return timeout;
720}
721
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800722static int
723send_vc(res_state statp,
724 const u_char *buf, int buflen, u_char *ans, int anssiz,
725 int *terrno, int ns)
726{
727 const HEADER *hp = (const HEADER *)(const void *)buf;
728 HEADER *anhp = (HEADER *)(void *)ans;
729 struct sockaddr *nsap;
730 int nsaplen;
731 int truncating, connreset, resplen, n;
732 struct iovec iov[2];
733 u_short len;
734 u_char *cp;
735 void *tmp;
736
Robert Greenwaltecd0e952012-01-11 10:04:48 -0800737 if (DBG) {
Elliott Hughes8f2a5a02013-03-15 15:30:25 -0700738 __libc_format_log(ANDROID_LOG_DEBUG, "libc", "using send_vc\n");
Robert Greenwaltecd0e952012-01-11 10:04:48 -0800739 }
740
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800741 nsap = get_nsaddr(statp, (size_t)ns);
742 nsaplen = get_salen(nsap);
743
744 connreset = 0;
745 same_ns:
746 truncating = 0;
747
748 /* Are we still talking to whom we want to talk to? */
749 if (statp->_vcsock >= 0 && (statp->_flags & RES_F_VC) != 0) {
750 struct sockaddr_storage peer;
751 socklen_t size = sizeof peer;
752
753 if (getpeername(statp->_vcsock,
754 (struct sockaddr *)(void *)&peer, &size) < 0 ||
755 !sock_eq((struct sockaddr *)(void *)&peer, nsap)) {
756 res_nclose(statp);
757 statp->_flags &= ~RES_F_VC;
758 }
759 }
760
761 if (statp->_vcsock < 0 || (statp->_flags & RES_F_VC) == 0) {
762 if (statp->_vcsock >= 0)
763 res_nclose(statp);
764
765 statp->_vcsock = socket(nsap->sa_family, SOCK_STREAM, 0);
766 if (statp->_vcsock > highestFD) {
767 res_nclose(statp);
768 errno = ENOTSOCK;
769 }
770 if (statp->_vcsock < 0) {
771 switch (errno) {
772 case EPROTONOSUPPORT:
773#ifdef EPFNOSUPPORT
774 case EPFNOSUPPORT:
775#endif
776 case EAFNOSUPPORT:
777 Perror(statp, stderr, "socket(vc)", errno);
778 return (0);
779 default:
780 *terrno = errno;
781 Perror(statp, stderr, "socket(vc)", errno);
782 return (-1);
783 }
784 }
785 errno = 0;
786 if (random_bind(statp->_vcsock,nsap->sa_family) < 0) {
787 *terrno = errno;
788 Aerror(statp, stderr, "bind/vc", errno, nsap,
789 nsaplen);
790 res_nclose(statp);
791 return (0);
792 }
Robert Greenwaltecd0e952012-01-11 10:04:48 -0800793 if (connect_with_timeout(statp->_vcsock, nsap, (socklen_t)nsaplen,
794 get_timeout(statp, ns)) < 0) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800795 *terrno = errno;
796 Aerror(statp, stderr, "connect/vc", errno, nsap,
797 nsaplen);
798 res_nclose(statp);
799 return (0);
800 }
801 statp->_flags |= RES_F_VC;
802 }
803
804 /*
805 * Send length & message
806 */
807 ns_put16((u_short)buflen, (u_char*)(void *)&len);
808 iov[0] = evConsIovec(&len, INT16SZ);
809 DE_CONST(buf, tmp);
810 iov[1] = evConsIovec(tmp, (size_t)buflen);
811 if (writev(statp->_vcsock, iov, 2) != (INT16SZ + buflen)) {
812 *terrno = errno;
813 Perror(statp, stderr, "write failed", errno);
814 res_nclose(statp);
815 return (0);
816 }
817 /*
818 * Receive length & response
819 */
820 read_len:
821 cp = ans;
822 len = INT16SZ;
823 while ((n = read(statp->_vcsock, (char *)cp, (size_t)len)) > 0) {
824 cp += n;
825 if ((len -= n) == 0)
826 break;
827 }
828 if (n <= 0) {
829 *terrno = errno;
830 Perror(statp, stderr, "read failed", errno);
831 res_nclose(statp);
832 /*
833 * A long running process might get its TCP
834 * connection reset if the remote server was
835 * restarted. Requery the server instead of
836 * trying a new one. When there is only one
837 * server, this means that a query might work
838 * instead of failing. We only allow one reset
839 * per query to prevent looping.
840 */
841 if (*terrno == ECONNRESET && !connreset) {
842 connreset = 1;
843 res_nclose(statp);
844 goto same_ns;
845 }
846 res_nclose(statp);
847 return (0);
848 }
849 resplen = ns_get16(ans);
850 if (resplen > anssiz) {
851 Dprint(statp->options & RES_DEBUG,
852 (stdout, ";; response truncated\n")
853 );
854 truncating = 1;
855 len = anssiz;
856 } else
857 len = resplen;
858 if (len < HFIXEDSZ) {
859 /*
860 * Undersized message.
861 */
862 Dprint(statp->options & RES_DEBUG,
863 (stdout, ";; undersized: %d\n", len));
864 *terrno = EMSGSIZE;
865 res_nclose(statp);
866 return (0);
867 }
868 cp = ans;
869 while (len != 0 && (n = read(statp->_vcsock, (char *)cp, (size_t)len)) > 0){
870 cp += n;
871 len -= n;
872 }
873 if (n <= 0) {
874 *terrno = errno;
875 Perror(statp, stderr, "read(vc)", errno);
876 res_nclose(statp);
877 return (0);
878 }
879 if (truncating) {
880 /*
881 * Flush rest of answer so connection stays in synch.
882 */
883 anhp->tc = 1;
884 len = resplen - anssiz;
885 while (len != 0) {
886 char junk[PACKETSZ];
887
888 n = read(statp->_vcsock, junk,
889 (len > sizeof junk) ? sizeof junk : len);
890 if (n > 0)
891 len -= n;
892 else
893 break;
894 }
895 }
896 /*
897 * If the calling applicating has bailed out of
898 * a previous call and failed to arrange to have
899 * the circuit closed or the server has got
900 * itself confused, then drop the packet and
901 * wait for the correct one.
902 */
903 if (hp->id != anhp->id) {
904 DprintQ((statp->options & RES_DEBUG) ||
905 (statp->pfcode & RES_PRF_REPLY),
906 (stdout, ";; old answer (unexpected):\n"),
907 ans, (resplen > anssiz) ? anssiz: resplen);
908 goto read_len;
909 }
910
911 /*
912 * All is well, or the error is fatal. Signal that the
913 * next nameserver ought not be tried.
914 */
915 return (resplen);
916}
917
Robert Greenwaltecd0e952012-01-11 10:04:48 -0800918/* return -1 on error (errno set), 0 on success */
919static int
920connect_with_timeout(int sock, const struct sockaddr *nsap, socklen_t salen, int sec)
921{
922 int res, origflags;
923 fd_set rset, wset;
924 struct timespec now, timeout, finish;
925
926 origflags = fcntl(sock, F_GETFL, 0);
927 fcntl(sock, F_SETFL, origflags | O_NONBLOCK);
928
929 res = connect(sock, nsap, salen);
930 if (res < 0 && errno != EINPROGRESS) {
931 res = -1;
932 goto done;
933 }
934 if (res != 0) {
935 now = evNowTime();
936 timeout = evConsTime((long)sec, 0L);
937 finish = evAddTime(now, timeout);
938 if (DBG) {
Elliott Hughes8f2a5a02013-03-15 15:30:25 -0700939 __libc_format_log(ANDROID_LOG_DEBUG, "libc", " %d send_vc\n", sock);
Robert Greenwaltecd0e952012-01-11 10:04:48 -0800940 }
941
942 res = retrying_select(sock, &rset, &wset, &finish);
943 if (res <= 0) {
944 res = -1;
945 }
946 }
947done:
948 fcntl(sock, F_SETFL, origflags);
949 if (DBG) {
Elliott Hughes8f2a5a02013-03-15 15:30:25 -0700950 __libc_format_log(ANDROID_LOG_DEBUG, "libc",
Robert Greenwaltecd0e952012-01-11 10:04:48 -0800951 " %d connect_with_timeout returning %s\n", sock, res);
952 }
953 return res;
954}
955
956static int
957retrying_select(const int sock, fd_set *readset, fd_set *writeset, const struct timespec *finish)
958{
959 struct timespec now, timeout;
960 int n, error;
961 socklen_t len;
962
963
964retry:
965 if (DBG) {
Elliott Hughes8f2a5a02013-03-15 15:30:25 -0700966 __libc_format_log(ANDROID_LOG_DEBUG, "libc", " %d retying_select\n", sock);
Robert Greenwaltecd0e952012-01-11 10:04:48 -0800967 }
968
969 now = evNowTime();
970 if (readset) {
971 FD_ZERO(readset);
972 FD_SET(sock, readset);
973 }
974 if (writeset) {
975 FD_ZERO(writeset);
976 FD_SET(sock, writeset);
977 }
978 if (evCmpTime(*finish, now) > 0)
979 timeout = evSubTime(*finish, now);
980 else
981 timeout = evConsTime(0L, 0L);
982
983 n = pselect(sock + 1, readset, writeset, NULL, &timeout, NULL);
984 if (n == 0) {
985 if (DBG) {
Elliott Hughes8f2a5a02013-03-15 15:30:25 -0700986 __libc_format_log(ANDROID_LOG_DEBUG, " libc",
Robert Greenwaltecd0e952012-01-11 10:04:48 -0800987 " %d retrying_select timeout\n", sock);
988 }
989 errno = ETIMEDOUT;
990 return 0;
991 }
992 if (n < 0) {
993 if (errno == EINTR)
994 goto retry;
995 if (DBG) {
Elliott Hughes8f2a5a02013-03-15 15:30:25 -0700996 __libc_format_log(ANDROID_LOG_DEBUG, "libc",
Robert Greenwaltecd0e952012-01-11 10:04:48 -0800997 " %d retrying_select got error %d\n",sock, n);
998 }
999 return n;
1000 }
1001 if ((readset && FD_ISSET(sock, readset)) || (writeset && FD_ISSET(sock, writeset))) {
1002 len = sizeof(error);
1003 if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, &len) < 0 || error) {
1004 errno = error;
1005 if (DBG) {
Elliott Hughes8f2a5a02013-03-15 15:30:25 -07001006 __libc_format_log(ANDROID_LOG_DEBUG, "libc",
Robert Greenwaltecd0e952012-01-11 10:04:48 -08001007 " %d retrying_select dot error2 %d\n", sock, errno);
1008 }
1009
1010 return -1;
1011 }
1012 }
1013 if (DBG) {
Elliott Hughes8f2a5a02013-03-15 15:30:25 -07001014 __libc_format_log(ANDROID_LOG_DEBUG, "libc",
Robert Greenwaltecd0e952012-01-11 10:04:48 -08001015 " %d retrying_select returning %d for %d\n",sock, n);
1016 }
1017
1018 return n;
1019}
1020
1021
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001022static int
1023send_dg(res_state statp,
1024 const u_char *buf, int buflen, u_char *ans, int anssiz,
1025 int *terrno, int ns, int *v_circuit, int *gotsomewhere)
1026{
1027 const HEADER *hp = (const HEADER *)(const void *)buf;
1028 HEADER *anhp = (HEADER *)(void *)ans;
1029 const struct sockaddr *nsap;
1030 int nsaplen;
1031 struct timespec now, timeout, finish;
1032 fd_set dsmask;
1033 struct sockaddr_storage from;
1034 socklen_t fromlen;
1035 int resplen, seconds, n, s;
1036
1037 nsap = get_nsaddr(statp, (size_t)ns);
1038 nsaplen = get_salen(nsap);
1039 if (EXT(statp).nssocks[ns] == -1) {
1040 EXT(statp).nssocks[ns] = socket(nsap->sa_family, SOCK_DGRAM, 0);
1041 if (EXT(statp).nssocks[ns] > highestFD) {
1042 res_nclose(statp);
1043 errno = ENOTSOCK;
1044 }
1045 if (EXT(statp).nssocks[ns] < 0) {
1046 switch (errno) {
1047 case EPROTONOSUPPORT:
1048#ifdef EPFNOSUPPORT
1049 case EPFNOSUPPORT:
1050#endif
1051 case EAFNOSUPPORT:
1052 Perror(statp, stderr, "socket(dg)", errno);
1053 return (0);
1054 default:
1055 *terrno = errno;
1056 Perror(statp, stderr, "socket(dg)", errno);
1057 return (-1);
1058 }
1059 }
1060#ifndef CANNOT_CONNECT_DGRAM
1061 /*
1062 * On a 4.3BSD+ machine (client and server,
1063 * actually), sending to a nameserver datagram
1064 * port with no nameserver will cause an
1065 * ICMP port unreachable message to be returned.
1066 * If our datagram socket is "connected" to the
1067 * server, we get an ECONNREFUSED error on the next
1068 * socket operation, and select returns if the
1069 * error message is received. We can thus detect
1070 * the absence of a nameserver without timing out.
1071 */
1072 if (random_bind(EXT(statp).nssocks[ns], nsap->sa_family) < 0) {
1073 Aerror(statp, stderr, "bind(dg)", errno, nsap,
1074 nsaplen);
1075 res_nclose(statp);
1076 return (0);
1077 }
1078 if (connect(EXT(statp).nssocks[ns], nsap, (socklen_t)nsaplen) < 0) {
1079 Aerror(statp, stderr, "connect(dg)", errno, nsap,
1080 nsaplen);
1081 res_nclose(statp);
1082 return (0);
1083 }
1084#endif /* !CANNOT_CONNECT_DGRAM */
1085 Dprint(statp->options & RES_DEBUG,
1086 (stdout, ";; new DG socket\n"))
1087 }
1088 s = EXT(statp).nssocks[ns];
1089#ifndef CANNOT_CONNECT_DGRAM
1090 if (send(s, (const char*)buf, (size_t)buflen, 0) != buflen) {
1091 Perror(statp, stderr, "send", errno);
1092 res_nclose(statp);
1093 return (0);
1094 }
1095#else /* !CANNOT_CONNECT_DGRAM */
1096 if (sendto(s, (const char*)buf, buflen, 0, nsap, nsaplen) != buflen)
1097 {
1098 Aerror(statp, stderr, "sendto", errno, nsap, nsaplen);
1099 res_nclose(statp);
1100 return (0);
1101 }
1102#endif /* !CANNOT_CONNECT_DGRAM */
1103
1104 /*
1105 * Wait for reply.
1106 */
Robert Greenwaltecd0e952012-01-11 10:04:48 -08001107 seconds = get_timeout(statp, ns);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001108 now = evNowTime();
1109 timeout = evConsTime((long)seconds, 0L);
1110 finish = evAddTime(now, timeout);
Robert Greenwaltecd0e952012-01-11 10:04:48 -08001111retry:
1112 n = retrying_select(s, &dsmask, NULL, &finish);
1113
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001114 if (n == 0) {
1115 Dprint(statp->options & RES_DEBUG, (stdout, ";; timeout\n"));
1116 *gotsomewhere = 1;
1117 return (0);
1118 }
1119 if (n < 0) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001120 Perror(statp, stderr, "select", errno);
1121 res_nclose(statp);
1122 return (0);
1123 }
1124 errno = 0;
1125 fromlen = sizeof(from);
1126 resplen = recvfrom(s, (char*)ans, (size_t)anssiz,0,
1127 (struct sockaddr *)(void *)&from, &fromlen);
1128 if (resplen <= 0) {
1129 Perror(statp, stderr, "recvfrom", errno);
1130 res_nclose(statp);
1131 return (0);
1132 }
1133 *gotsomewhere = 1;
1134 if (resplen < HFIXEDSZ) {
1135 /*
1136 * Undersized message.
1137 */
1138 Dprint(statp->options & RES_DEBUG,
1139 (stdout, ";; undersized: %d\n",
1140 resplen));
1141 *terrno = EMSGSIZE;
1142 res_nclose(statp);
1143 return (0);
1144 }
1145 if (hp->id != anhp->id) {
1146 /*
1147 * response from old query, ignore it.
1148 * XXX - potential security hazard could
1149 * be detected here.
1150 */
Geremy Condra524c87c2012-06-08 21:06:33 -07001151#ifdef ANDROID_CHANGES
1152 __libc_android_log_event_uid(BIONIC_EVENT_RESOLVER_OLD_RESPONSE);
1153#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001154 DprintQ((statp->options & RES_DEBUG) ||
1155 (statp->pfcode & RES_PRF_REPLY),
1156 (stdout, ";; old answer:\n"),
1157 ans, (resplen > anssiz) ? anssiz : resplen);
Robert Greenwaltecd0e952012-01-11 10:04:48 -08001158 goto retry;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001159 }
1160 if (!(statp->options & RES_INSECURE1) &&
1161 !res_ourserver_p(statp, (struct sockaddr *)(void *)&from)) {
1162 /*
1163 * response from wrong server? ignore it.
1164 * XXX - potential security hazard could
1165 * be detected here.
1166 */
Geremy Condra524c87c2012-06-08 21:06:33 -07001167#ifdef ANDROID_CHANGES
1168 __libc_android_log_event_uid(BIONIC_EVENT_RESOLVER_WRONG_SERVER);
1169#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001170 DprintQ((statp->options & RES_DEBUG) ||
1171 (statp->pfcode & RES_PRF_REPLY),
1172 (stdout, ";; not our server:\n"),
1173 ans, (resplen > anssiz) ? anssiz : resplen);
Robert Greenwaltecd0e952012-01-11 10:04:48 -08001174 goto retry;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001175 }
1176#ifdef RES_USE_EDNS0
1177 if (anhp->rcode == FORMERR && (statp->options & RES_USE_EDNS0) != 0U) {
1178 /*
1179 * Do not retry if the server do not understand EDNS0.
1180 * The case has to be captured here, as FORMERR packet do not
1181 * carry query section, hence res_queriesmatch() returns 0.
1182 */
1183 DprintQ(statp->options & RES_DEBUG,
1184 (stdout, "server rejected query with EDNS0:\n"),
1185 ans, (resplen > anssiz) ? anssiz : resplen);
1186 /* record the error */
1187 statp->_flags |= RES_F_EDNS0ERR;
1188 res_nclose(statp);
1189 return (0);
1190 }
1191#endif
1192 if (!(statp->options & RES_INSECURE2) &&
1193 !res_queriesmatch(buf, buf + buflen,
1194 ans, ans + anssiz)) {
1195 /*
1196 * response contains wrong query? ignore it.
1197 * XXX - potential security hazard could
1198 * be detected here.
1199 */
Geremy Condra524c87c2012-06-08 21:06:33 -07001200#ifdef ANDROID_CHANGES
1201 __libc_android_log_event_uid(BIONIC_EVENT_RESOLVER_WRONG_QUERY);
1202#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001203 DprintQ((statp->options & RES_DEBUG) ||
1204 (statp->pfcode & RES_PRF_REPLY),
1205 (stdout, ";; wrong query name:\n"),
1206 ans, (resplen > anssiz) ? anssiz : resplen);
Robert Greenwaltecd0e952012-01-11 10:04:48 -08001207 goto retry;;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001208 }
1209 if (anhp->rcode == SERVFAIL ||
1210 anhp->rcode == NOTIMP ||
1211 anhp->rcode == REFUSED) {
1212 DprintQ(statp->options & RES_DEBUG,
1213 (stdout, "server rejected query:\n"),
1214 ans, (resplen > anssiz) ? anssiz : resplen);
1215 res_nclose(statp);
1216 /* don't retry if called from dig */
1217 if (!statp->pfcode)
1218 return (0);
1219 }
1220 if (!(statp->options & RES_IGNTC) && anhp->tc) {
1221 /*
1222 * To get the rest of answer,
1223 * use TCP with same server.
1224 */
1225 Dprint(statp->options & RES_DEBUG,
1226 (stdout, ";; truncated answer\n"));
1227 *v_circuit = 1;
1228 res_nclose(statp);
1229 return (1);
1230 }
1231 /*
1232 * All is well, or the error is fatal. Signal that the
1233 * next nameserver ought not be tried.
1234 */
1235 return (resplen);
1236}
1237
1238static void
1239Aerror(const res_state statp, FILE *file, const char *string, int error,
1240 const struct sockaddr *address, int alen)
1241{
1242 int save = errno;
1243 char hbuf[NI_MAXHOST];
1244 char sbuf[NI_MAXSERV];
1245
1246 alen = alen;
1247
1248 if ((statp->options & RES_DEBUG) != 0U) {
1249 if (getnameinfo(address, (socklen_t)alen, hbuf, sizeof(hbuf),
1250 sbuf, sizeof(sbuf), niflags)) {
1251 strncpy(hbuf, "?", sizeof(hbuf) - 1);
1252 hbuf[sizeof(hbuf) - 1] = '\0';
1253 strncpy(sbuf, "?", sizeof(sbuf) - 1);
1254 sbuf[sizeof(sbuf) - 1] = '\0';
1255 }
1256 fprintf(file, "res_send: %s ([%s].%s): %s\n",
1257 string, hbuf, sbuf, strerror(error));
1258 }
1259 errno = save;
1260}
1261
1262static void
1263Perror(const res_state statp, FILE *file, const char *string, int error) {
1264 int save = errno;
1265
1266 if ((statp->options & RES_DEBUG) != 0U)
1267 fprintf(file, "res_send: %s: %s\n",
1268 string, strerror(error));
1269 errno = save;
1270}
1271
1272static int
1273sock_eq(struct sockaddr *a, struct sockaddr *b) {
1274 struct sockaddr_in *a4, *b4;
1275 struct sockaddr_in6 *a6, *b6;
1276
1277 if (a->sa_family != b->sa_family)
1278 return 0;
1279 switch (a->sa_family) {
1280 case AF_INET:
1281 a4 = (struct sockaddr_in *)(void *)a;
1282 b4 = (struct sockaddr_in *)(void *)b;
1283 return a4->sin_port == b4->sin_port &&
1284 a4->sin_addr.s_addr == b4->sin_addr.s_addr;
1285 case AF_INET6:
1286 a6 = (struct sockaddr_in6 *)(void *)a;
1287 b6 = (struct sockaddr_in6 *)(void *)b;
1288 return a6->sin6_port == b6->sin6_port &&
1289#ifdef HAVE_SIN6_SCOPE_ID
1290 a6->sin6_scope_id == b6->sin6_scope_id &&
1291#endif
1292 IN6_ARE_ADDR_EQUAL(&a6->sin6_addr, &b6->sin6_addr);
1293 default:
1294 return 0;
1295 }
1296}
1297
1298#ifdef NEED_PSELECT
1299/* XXX needs to move to the porting library. */
1300static int
1301pselect(int nfds, void *rfds, void *wfds, void *efds,
1302 struct timespec *tsp, const sigset_t *sigmask)
1303{
1304 struct timeval tv, *tvp;
1305 sigset_t sigs;
1306 int n;
1307
1308 if (tsp) {
1309 tvp = &tv;
1310 tv = evTimeVal(*tsp);
1311 } else
1312 tvp = NULL;
1313 if (sigmask)
1314 sigprocmask(SIG_SETMASK, sigmask, &sigs);
1315 n = select(nfds, rfds, wfds, efds, tvp);
1316 if (sigmask)
1317 sigprocmask(SIG_SETMASK, &sigs, NULL);
1318 if (tsp)
1319 *tsp = evTimeSpec(tv);
1320 return (n);
1321}
1322#endif