blob: 1fb8ddd1809cd4b207f374e72a0a4760b03a584a [file] [log] [blame]
Bernie Innocenti55864192018-08-30 04:05:20 +09001/* $NetBSD: res_send.c,v 1.9 2006/01/24 17:41:25 christos Exp $ */
2
3/*
4 * Copyright (c) 1985, 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36/*
37 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
38 *
39 * Permission to use, copy, modify, and distribute this software for any
40 * purpose with or without fee is hereby granted, provided that the above
41 * copyright notice and this permission notice appear in all copies, and that
42 * the name of Digital Equipment Corporation not be used in advertising or
43 * publicity pertaining to distribution of the document or software without
44 * specific, written prior permission.
45 *
46 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
47 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
48 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
49 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
50 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
51 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
52 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
53 * SOFTWARE.
54 */
55
56/*
57 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
58 * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
59 *
60 * Permission to use, copy, modify, and distribute this software for any
61 * purpose with or without fee is hereby granted, provided that the above
62 * copyright notice and this permission notice appear in all copies.
63 *
64 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
65 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
66 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
67 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
68 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
69 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
70 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
71 */
72
Bernie Innocenti55864192018-08-30 04:05:20 +090073/*
74 * Send query to name server and wait for reply.
75 */
76
Bernie Innocentie9ba09c2018-09-12 23:20:10 +090077constexpr bool kVerboseLogging = false;
78#define LOG_TAG "res_send"
79
Bernie Innocenti55864192018-08-30 04:05:20 +090080#include <sys/param.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090081#include <sys/socket.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090082#include <sys/time.h>
83#include <sys/types.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090084#include <sys/uio.h>
85
Bernie Innocenti55864192018-08-30 04:05:20 +090086#include <arpa/inet.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090087#include <arpa/nameser.h>
88#include <netinet/in.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090089
90#include <errno.h>
91#include <fcntl.h>
92#include <netdb.h>
93#include <poll.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090094#include <signal.h>
95#include <stdio.h>
96#include <stdlib.h>
97#include <string.h>
98#include <time.h>
99#include <unistd.h>
100
Bernie Innocentie9ba09c2018-09-12 23:20:10 +0900101#include <android-base/logging.h>
Bernie Innocenti55864192018-08-30 04:05:20 +0900102
Bernie Innocenti189eb502018-10-01 23:10:18 +0900103#include "netd_resolv/resolv.h"
104#include "netd_resolv/stats.h"
Bernie Innocenti2fd418e2018-08-30 12:04:03 +0900105#include "private/android_filesystem_config.h"
Bernie Innocenti189eb502018-10-01 23:10:18 +0900106#include "res_state_ext.h"
Bernie Innocenti2fd418e2018-08-30 12:04:03 +0900107#include "resolv_cache.h"
Bernie Innocenti2fd418e2018-08-30 12:04:03 +0900108#include "resolv_private.h"
Bernie Innocenti2fd418e2018-08-30 12:04:03 +0900109
Bernie Innocentie9ba09c2018-09-12 23:20:10 +0900110
111#define VLOG if (!kVerboseLogging) {} else LOG(INFO)
112
113#ifndef RESOLV_ALLOW_VERBOSE_LOGGING
114static_assert(kVerboseLogging == false,
115 "Verbose logging floods logs at high-rate and exposes privacy-sensitive information. "
116 "Do not enable in release builds.");
Bernie Innocenti55864192018-08-30 04:05:20 +0900117#endif
Bernie Innocenti2fd418e2018-08-30 12:04:03 +0900118
Bernie Innocentie9ba09c2018-09-12 23:20:10 +0900119#ifndef DEBUG
120#define Dprint(cond, args) /*empty*/
121#define DprintQ(cond, args, query, size) /*empty*/
122#else
123// TODO: convert to Android logging
124#define Dprint(cond, args) \
125 if (cond) { \
126 fprintf args; \
127 } else { \
128 }
129#define DprintQ(cond, args, query, size) \
130 if (cond) { \
131 fprintf args; \
132 res_pquery(statp, query, size, stdout); \
133 } else { \
134 }
135#endif // DEBUG
Bernie Innocenti55864192018-08-30 04:05:20 +0900136
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900137static int get_salen(const struct sockaddr*);
138static struct sockaddr* get_nsaddr(res_state, size_t);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900139static int send_vc(res_state, struct __res_params* params, const u_char*, int, u_char*, int, int*,
140 int, time_t*, int*, int*);
141static int send_dg(res_state, struct __res_params* params, const u_char*, int, u_char*, int, int*,
142 int, int*, int*, time_t*, int*, int*);
143static void Aerror(const res_state, FILE*, const char*, int, const struct sockaddr*, int);
144static void Perror(const res_state, FILE*, const char*, int);
145static int sock_eq(struct sockaddr*, struct sockaddr*);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900146static int connect_with_timeout(int sock, const struct sockaddr* nsap, socklen_t salen,
147 const struct timespec timeout);
Bernie Innocenti55864192018-08-30 04:05:20 +0900148static int retrying_poll(const int sock, short events, const struct timespec* finish);
149
150/* BIONIC-BEGIN: implement source port randomization */
151typedef union {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900152 struct sockaddr sa;
153 struct sockaddr_in sin;
154 struct sockaddr_in6 sin6;
Bernie Innocenti55864192018-08-30 04:05:20 +0900155} _sockaddr_union;
156
Bernie Innocentif89b3512018-08-30 07:34:37 +0900157// BEGIN: Code copied from ISC eventlib
158// TODO: move away from this code
159
160#define BILLION 1000000000
161
162static struct timespec evConsTime(time_t sec, long nsec) {
163 struct timespec x;
164
165 x.tv_sec = sec;
166 x.tv_nsec = nsec;
167 return (x);
168}
169
170static struct timespec evAddTime(struct timespec addend1, struct timespec addend2) {
171 struct timespec x;
172
173 x.tv_sec = addend1.tv_sec + addend2.tv_sec;
174 x.tv_nsec = addend1.tv_nsec + addend2.tv_nsec;
175 if (x.tv_nsec >= BILLION) {
176 x.tv_sec++;
177 x.tv_nsec -= BILLION;
178 }
179 return (x);
180}
181
182static struct timespec evSubTime(struct timespec minuend, struct timespec subtrahend) {
183 struct timespec x;
184
185 x.tv_sec = minuend.tv_sec - subtrahend.tv_sec;
186 if (minuend.tv_nsec >= subtrahend.tv_nsec)
187 x.tv_nsec = minuend.tv_nsec - subtrahend.tv_nsec;
188 else {
189 x.tv_nsec = BILLION - subtrahend.tv_nsec + minuend.tv_nsec;
190 x.tv_sec--;
191 }
192 return (x);
193}
194
195static int evCmpTime(struct timespec a, struct timespec b) {
196#define SGN(x) ((x) < 0 ? (-1) : (x) > 0 ? (1) : (0));
197 time_t s = a.tv_sec - b.tv_sec;
198 long n;
199
200 if (s != 0) return SGN(s);
201
202 n = a.tv_nsec - b.tv_nsec;
203 return SGN(n);
204}
205
Bernie Innocentif89b3512018-08-30 07:34:37 +0900206static struct timespec evNowTime(void) {
Bernie Innocentif89b3512018-08-30 07:34:37 +0900207 struct timespec tsnow;
Bernie Innocenti357339c2018-08-31 16:11:41 +0900208 clock_gettime(CLOCK_REALTIME, &tsnow);
209 return tsnow;
Bernie Innocentif89b3512018-08-30 07:34:37 +0900210}
211
212static struct iovec evConsIovec(void* buf, size_t cnt) {
213 struct iovec ret;
214
215 memset(&ret, 0xf5, sizeof ret);
216 ret.iov_base = buf;
217 ret.iov_len = cnt;
Bernie Innocenti357339c2018-08-31 16:11:41 +0900218 return ret;
Bernie Innocentif89b3512018-08-30 07:34:37 +0900219}
220
221// END: Code copied from ISC eventlib
222
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900223static int random_bind(int s, int family) {
224 _sockaddr_union u;
225 int j;
226 socklen_t slen;
Bernie Innocenti55864192018-08-30 04:05:20 +0900227
228 /* clear all, this also sets the IP4/6 address to 'any' */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900229 memset(&u, 0, sizeof u);
Bernie Innocenti55864192018-08-30 04:05:20 +0900230
231 switch (family) {
232 case AF_INET:
233 u.sin.sin_family = family;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900234 slen = sizeof u.sin;
Bernie Innocenti55864192018-08-30 04:05:20 +0900235 break;
236 case AF_INET6:
237 u.sin6.sin6_family = family;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900238 slen = sizeof u.sin6;
Bernie Innocenti55864192018-08-30 04:05:20 +0900239 break;
240 default:
241 errno = EPROTO;
242 return -1;
243 }
244
245 /* first try to bind to a random source port a few times */
246 for (j = 0; j < 10; j++) {
247 /* find a random port between 1025 .. 65534 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900248 int port = 1025 + (res_randomid() % (65535 - 1025));
Bernie Innocenti55864192018-08-30 04:05:20 +0900249 if (family == AF_INET)
250 u.sin.sin_port = htons(port);
251 else
252 u.sin6.sin6_port = htons(port);
253
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900254 if (!bind(s, &u.sa, slen)) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900255 }
256
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900257 // nothing after 10 attempts, our network table is probably busy
258 // let the system decide which port is best
Bernie Innocenti55864192018-08-30 04:05:20 +0900259 if (family == AF_INET)
260 u.sin.sin_port = 0;
261 else
262 u.sin6.sin6_port = 0;
263
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900264 return bind(s, &u.sa, slen);
Bernie Innocenti55864192018-08-30 04:05:20 +0900265}
266/* BIONIC-END */
267
Bernie Innocenti55864192018-08-30 04:05:20 +0900268/* int
269 * res_isourserver(ina)
270 * looks up "ina" in _res.ns_addr_list[]
271 * returns:
272 * 0 : not found
273 * >0 : found
274 * author:
275 * paul vixie, 29may94
276 */
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900277static int res_ourserver_p(const res_state statp, const sockaddr* sa) {
278 const sockaddr_in *inp, *srv;
279 const sockaddr_in6 *in6p, *srv6;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900280 int ns;
Bernie Innocenti55864192018-08-30 04:05:20 +0900281
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900282 switch (sa->sa_family) {
283 case AF_INET:
284 inp = (const struct sockaddr_in*) (const void*) sa;
285 for (ns = 0; ns < statp->nscount; ns++) {
286 srv = (struct sockaddr_in*) (void*) get_nsaddr(statp, (size_t) ns);
287 if (srv->sin_family == inp->sin_family && srv->sin_port == inp->sin_port &&
288 (srv->sin_addr.s_addr == INADDR_ANY ||
289 srv->sin_addr.s_addr == inp->sin_addr.s_addr))
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900290 return 1;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900291 }
292 break;
293 case AF_INET6:
Ken Chencf6ded62018-10-16 23:12:09 +0800294 if (statp->_u._ext.ext == NULL) break;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900295 in6p = (const struct sockaddr_in6*) (const void*) sa;
296 for (ns = 0; ns < statp->nscount; ns++) {
297 srv6 = (struct sockaddr_in6*) (void*) get_nsaddr(statp, (size_t) ns);
298 if (srv6->sin6_family == in6p->sin6_family && srv6->sin6_port == in6p->sin6_port &&
Bernie Innocenti55864192018-08-30 04:05:20 +0900299#ifdef HAVE_SIN6_SCOPE_ID
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900300 (srv6->sin6_scope_id == 0 || srv6->sin6_scope_id == in6p->sin6_scope_id) &&
Bernie Innocenti55864192018-08-30 04:05:20 +0900301#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900302 (IN6_IS_ADDR_UNSPECIFIED(&srv6->sin6_addr) ||
303 IN6_ARE_ADDR_EQUAL(&srv6->sin6_addr, &in6p->sin6_addr)))
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900304 return 1;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900305 }
306 break;
307 default:
308 break;
309 }
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900310 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900311}
312
313/* int
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900314 * res_nameinquery(name, type, cl, buf, eom)
315 * look for (name, type, cl) in the query section of packet (buf, eom)
Bernie Innocenti55864192018-08-30 04:05:20 +0900316 * requires:
317 * buf + HFIXEDSZ <= eom
318 * returns:
319 * -1 : format error
320 * 0 : not found
321 * >0 : found
322 * author:
323 * paul vixie, 29may94
324 */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900325int res_nameinquery(const char* name, int type, int cl, const u_char* buf, const u_char* eom) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900326 const u_char* cp = buf + HFIXEDSZ;
327 int qdcount = ntohs(((const HEADER*) (const void*) buf)->qdcount);
Bernie Innocenti55864192018-08-30 04:05:20 +0900328
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900329 while (qdcount-- > 0) {
330 char tname[MAXDNAME + 1];
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900331 int n = dn_expand(buf, eom, cp, tname, sizeof tname);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900332 if (n < 0) return (-1);
333 cp += n;
334 if (cp + 2 * INT16SZ > eom) return (-1);
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900335 int ttype = ns_get16(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900336 cp += INT16SZ;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900337 int tclass = ns_get16(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900338 cp += INT16SZ;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900339 if (ttype == type && tclass == cl && ns_samename(tname, name) == 1) return (1);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900340 }
341 return (0);
Bernie Innocenti55864192018-08-30 04:05:20 +0900342}
343
344/* int
345 * res_queriesmatch(buf1, eom1, buf2, eom2)
346 * is there a 1:1 mapping of (name,type,class)
347 * in (buf1,eom1) and (buf2,eom2)?
348 * returns:
349 * -1 : format error
350 * 0 : not a 1:1 mapping
351 * >0 : is a 1:1 mapping
352 * author:
353 * paul vixie, 29may94
354 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900355int res_queriesmatch(const u_char* buf1, const u_char* eom1, const u_char* buf2,
356 const u_char* eom2) {
357 const u_char* cp = buf1 + HFIXEDSZ;
358 int qdcount = ntohs(((const HEADER*) (const void*) buf1)->qdcount);
Bernie Innocenti55864192018-08-30 04:05:20 +0900359
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900360 if (buf1 + HFIXEDSZ > eom1 || buf2 + HFIXEDSZ > eom2) return (-1);
Bernie Innocenti55864192018-08-30 04:05:20 +0900361
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900362 /*
363 * Only header section present in replies to
364 * dynamic update packets.
365 */
366 if ((((const HEADER*) (const void*) buf1)->opcode == ns_o_update) &&
367 (((const HEADER*) (const void*) buf2)->opcode == ns_o_update))
368 return (1);
Bernie Innocenti55864192018-08-30 04:05:20 +0900369
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900370 if (qdcount != ntohs(((const HEADER*) (const void*) buf2)->qdcount)) return (0);
371 while (qdcount-- > 0) {
372 char tname[MAXDNAME + 1];
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900373 int n = dn_expand(buf1, eom1, cp, tname, sizeof tname);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900374 if (n < 0) return (-1);
375 cp += n;
376 if (cp + 2 * INT16SZ > eom1) return (-1);
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900377 int ttype = ns_get16(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900378 cp += INT16SZ;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900379 int tclass = ns_get16(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900380 cp += INT16SZ;
381 if (!res_nameinquery(tname, ttype, tclass, buf2, eom2)) return (0);
382 }
383 return (1);
Bernie Innocenti55864192018-08-30 04:05:20 +0900384}
385
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900386int res_nsend(res_state statp, const u_char* buf, int buflen, u_char* ans, int anssiz) {
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900387 int gotsomewhere, terrno, v_circuit, resplen, n;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900388 ResolvCacheStatus cache_status = RESOLV_CACHE_UNSUPPORTED;
Bernie Innocenti55864192018-08-30 04:05:20 +0900389
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900390 if (anssiz < HFIXEDSZ) {
391 errno = EINVAL;
392 return (-1);
393 }
394 DprintQ((statp->options & RES_DEBUG) || (statp->pfcode & RES_PRF_QUERY),
395 (stdout, ";; res_send()\n"), buf, buflen);
396 v_circuit = (statp->options & RES_USEVC) || buflen > PACKETSZ;
397 gotsomewhere = 0;
398 terrno = ETIMEDOUT;
Bernie Innocenti55864192018-08-30 04:05:20 +0900399
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900400 int anslen = 0;
401 cache_status = _resolv_cache_lookup(statp->netid, buf, buflen, ans, anssiz, &anslen);
Bernie Innocenti55864192018-08-30 04:05:20 +0900402
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900403 if (cache_status == RESOLV_CACHE_FOUND) {
404 return anslen;
405 } else if (cache_status != RESOLV_CACHE_UNSUPPORTED) {
406 // had a cache miss for a known network, so populate the thread private
407 // data so the normal resolve path can do its thing
408 _resolv_populate_res_for_net(statp);
409 }
410 if (statp->nscount == 0) {
411 // We have no nameservers configured, so there's no point trying.
412 // Tell the cache the query failed, or any retries and anyone else asking the same
413 // question will block for PENDING_REQUEST_TIMEOUT seconds instead of failing fast.
414 _resolv_cache_query_failed(statp->netid, buf, buflen);
415 errno = ESRCH;
416 return (-1);
417 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900418
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900419 /*
420 * If the ns_addr_list in the resolver context has changed, then
421 * invalidate our cached copy and the associated timing data.
422 */
Ken Chencf6ded62018-10-16 23:12:09 +0800423 if (statp->_u._ext.nscount != 0) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900424 int needclose = 0;
425 struct sockaddr_storage peer;
426 socklen_t peerlen;
Bernie Innocenti55864192018-08-30 04:05:20 +0900427
Ken Chencf6ded62018-10-16 23:12:09 +0800428 if (statp->_u._ext.nscount != statp->nscount) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900429 needclose++;
430 } else {
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900431 for (int ns = 0; ns < statp->nscount; ns++) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900432 if (statp->nsaddr_list[ns].sin_family &&
433 !sock_eq((struct sockaddr*) (void*) &statp->nsaddr_list[ns],
Ken Chencf6ded62018-10-16 23:12:09 +0800434 (struct sockaddr*) (void*) &statp->_u._ext.ext->nsaddrs[ns])) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900435 needclose++;
436 break;
437 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900438
Ken Chencf6ded62018-10-16 23:12:09 +0800439 if (statp->_u._ext.nssocks[ns] == -1) continue;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900440 peerlen = sizeof(peer);
Ken Chencf6ded62018-10-16 23:12:09 +0800441 if (getpeername(statp->_u._ext.nssocks[ns], (struct sockaddr*) (void*) &peer,
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900442 &peerlen) < 0) {
443 needclose++;
444 break;
445 }
446 if (!sock_eq((struct sockaddr*) (void*) &peer, get_nsaddr(statp, (size_t) ns))) {
447 needclose++;
448 break;
449 }
450 }
451 }
452 if (needclose) {
453 res_nclose(statp);
Ken Chencf6ded62018-10-16 23:12:09 +0800454 statp->_u._ext.nscount = 0;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900455 }
456 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900457
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900458 /*
459 * Maybe initialize our private copy of the ns_addr_list.
460 */
Ken Chencf6ded62018-10-16 23:12:09 +0800461 if (statp->_u._ext.nscount == 0) {
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900462 for (int ns = 0; ns < statp->nscount; ns++) {
Ken Chencf6ded62018-10-16 23:12:09 +0800463 statp->_u._ext.nstimes[ns] = RES_MAXTIME;
464 statp->_u._ext.nssocks[ns] = -1;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900465 if (!statp->nsaddr_list[ns].sin_family) continue;
Ken Chencf6ded62018-10-16 23:12:09 +0800466 statp->_u._ext.ext->nsaddrs[ns].sin = statp->nsaddr_list[ns];
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900467 }
Ken Chencf6ded62018-10-16 23:12:09 +0800468 statp->_u._ext.nscount = statp->nscount;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900469 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900470
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900471 /*
472 * Some resolvers want to even out the load on their nameservers.
473 * Note that RES_BLAST overrides RES_ROTATE.
474 */
475 if ((statp->options & RES_ROTATE) != 0U && (statp->options & RES_BLAST) == 0U) {
476 union res_sockaddr_union inu;
477 struct sockaddr_in ina;
478 int lastns = statp->nscount - 1;
479 int fd;
480 u_int16_t nstime;
Bernie Innocenti55864192018-08-30 04:05:20 +0900481
Ken Chencf6ded62018-10-16 23:12:09 +0800482 if (statp->_u._ext.ext != NULL) inu = statp->_u._ext.ext->nsaddrs[0];
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900483 ina = statp->nsaddr_list[0];
Ken Chencf6ded62018-10-16 23:12:09 +0800484 fd = statp->_u._ext.nssocks[0];
485 nstime = statp->_u._ext.nstimes[0];
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900486 for (int ns = 0; ns < lastns; ns++) {
Ken Chencf6ded62018-10-16 23:12:09 +0800487 if (statp->_u._ext.ext != NULL)
488 statp->_u._ext.ext->nsaddrs[ns] = statp->_u._ext.ext->nsaddrs[ns + 1];
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900489 statp->nsaddr_list[ns] = statp->nsaddr_list[ns + 1];
Ken Chencf6ded62018-10-16 23:12:09 +0800490 statp->_u._ext.nssocks[ns] = statp->_u._ext.nssocks[ns + 1];
491 statp->_u._ext.nstimes[ns] = statp->_u._ext.nstimes[ns + 1];
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900492 }
Ken Chencf6ded62018-10-16 23:12:09 +0800493 if (statp->_u._ext.ext != NULL) statp->_u._ext.ext->nsaddrs[lastns] = inu;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900494 statp->nsaddr_list[lastns] = ina;
Ken Chencf6ded62018-10-16 23:12:09 +0800495 statp->_u._ext.nssocks[lastns] = fd;
496 statp->_u._ext.nstimes[lastns] = nstime;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900497 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900498
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900499 /*
500 * Send request, RETRY times, or until successful.
501 */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900502 for (int attempt = 0; attempt < statp->retry; ++attempt) {
Bernie Innocenti189eb502018-10-01 23:10:18 +0900503 struct res_stats stats[MAXNS];
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900504 struct __res_params params;
Bernie Innocenti189eb502018-10-01 23:10:18 +0900505 int revision_id = resolv_cache_get_resolver_stats(statp->netid, &params, stats);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900506 bool usable_servers[MAXNS];
507 android_net_res_stats_get_usable_servers(&params, stats, statp->nscount, usable_servers);
Bernie Innocenti55864192018-08-30 04:05:20 +0900508
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900509 for (int ns = 0; ns < statp->nscount; ns++) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900510 if (!usable_servers[ns]) continue;
511 struct sockaddr* nsap;
512 int nsaplen;
513 time_t now = 0;
514 int rcode = RCODE_INTERNAL_ERROR;
515 int delay = 0;
516 nsap = get_nsaddr(statp, (size_t) ns);
517 nsaplen = get_salen(nsap);
518 statp->_flags &= ~RES_F_LASTMASK;
519 statp->_flags |= (ns << RES_F_LASTSHIFT);
Bernie Innocenti55864192018-08-30 04:05:20 +0900520
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900521 same_ns:
522 if (statp->qhook) {
523 int done = 0, loops = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900524
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900525 do {
526 res_sendhookact act;
Bernie Innocenti55864192018-08-30 04:05:20 +0900527
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900528 act = (*statp->qhook)(&nsap, &buf, &buflen, ans, anssiz, &resplen);
529 switch (act) {
530 case res_goahead:
531 done = 1;
532 break;
533 case res_nextns:
534 res_nclose(statp);
535 goto next_ns;
536 case res_done:
537 if (cache_status == RESOLV_CACHE_NOTFOUND) {
538 _resolv_cache_add(statp->netid, buf, buflen, ans, resplen);
539 }
540 return (resplen);
541 case res_modified:
542 /* give the hook another try */
543 if (++loops < 42) /*doug adams*/
544 break;
Bernie Innocenti8bb94ba2018-10-10 22:30:12 +0900545 [[fallthrough]];
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900546 case res_error:
Bernie Innocenti8bb94ba2018-10-10 22:30:12 +0900547 [[fallthrough]];
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900548 default:
549 goto fail;
550 }
551 } while (!done);
552 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900553
Bernie Innocentie9ba09c2018-09-12 23:20:10 +0900554 [[maybe_unused]] static const int niflags = NI_NUMERICHOST | NI_NUMERICSERV;
555 [[maybe_unused]] char abuf[NI_MAXHOST];
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900556 Dprint(((statp->options & RES_DEBUG) &&
557 getnameinfo(nsap, (socklen_t) nsaplen, abuf, sizeof(abuf), NULL, 0, niflags) ==
558 0),
559 (stdout, ";; Querying server (# %d) address = %s\n", ns + 1, abuf));
Bernie Innocenti55864192018-08-30 04:05:20 +0900560
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900561 if (v_circuit) {
562 /* Use VC; at most one attempt per server. */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900563 attempt = statp->retry;
Bernie Innocenti55864192018-08-30 04:05:20 +0900564
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900565 n = send_vc(statp, &params, buf, buflen, ans, anssiz, &terrno, ns, &now, &rcode,
566 &delay);
Bernie Innocenti55864192018-08-30 04:05:20 +0900567
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900568 /*
569 * Only record stats the first time we try a query. This ensures that
570 * queries that deterministically fail (e.g., a name that always returns
571 * SERVFAIL or times out) do not unduly affect the stats.
572 */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900573 if (attempt == 0) {
Bernie Innocenti189eb502018-10-01 23:10:18 +0900574 res_sample sample;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900575 _res_stats_set_sample(&sample, now, rcode, delay);
576 _resolv_cache_add_resolver_stats_sample(statp->netid, revision_id, ns, &sample,
577 params.max_samples);
578 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900579
Bernie Innocentie9ba09c2018-09-12 23:20:10 +0900580 VLOG << "used send_vc " << n;
Bernie Innocenti55864192018-08-30 04:05:20 +0900581
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900582 if (n < 0) goto fail;
583 if (n == 0) goto next_ns;
584 resplen = n;
585 } else {
586 /* Use datagrams. */
Bernie Innocentie9ba09c2018-09-12 23:20:10 +0900587 VLOG << "using send_dg";
Bernie Innocenti55864192018-08-30 04:05:20 +0900588
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900589 n = send_dg(statp, &params, buf, buflen, ans, anssiz, &terrno, ns, &v_circuit,
590 &gotsomewhere, &now, &rcode, &delay);
Bernie Innocenti55864192018-08-30 04:05:20 +0900591
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900592 /* Only record stats the first time we try a query. See above. */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900593 if (attempt == 0) {
Bernie Innocenti189eb502018-10-01 23:10:18 +0900594 res_sample sample;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900595 _res_stats_set_sample(&sample, now, rcode, delay);
596 _resolv_cache_add_resolver_stats_sample(statp->netid, revision_id, ns, &sample,
597 params.max_samples);
598 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900599
Bernie Innocentie9ba09c2018-09-12 23:20:10 +0900600 VLOG << "used send_dg " << n;
Bernie Innocenti55864192018-08-30 04:05:20 +0900601
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900602 if (n < 0) goto fail;
603 if (n == 0) goto next_ns;
Bernie Innocentie9ba09c2018-09-12 23:20:10 +0900604 VLOG << "time=" << time(NULL);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900605 if (v_circuit) goto same_ns;
606 resplen = n;
607 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900608
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900609 Dprint((statp->options & RES_DEBUG) ||
610 ((statp->pfcode & RES_PRF_REPLY) && (statp->pfcode & RES_PRF_HEAD1)),
611 (stdout, ";; got answer:\n"));
Bernie Innocenti55864192018-08-30 04:05:20 +0900612
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900613 DprintQ((statp->options & RES_DEBUG) || (statp->pfcode & RES_PRF_REPLY),
614 (stdout, "%s", ""), ans, (resplen > anssiz) ? anssiz : resplen);
Bernie Innocenti55864192018-08-30 04:05:20 +0900615
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900616 if (cache_status == RESOLV_CACHE_NOTFOUND) {
617 _resolv_cache_add(statp->netid, buf, buflen, ans, resplen);
618 }
619 /*
620 * If we have temporarily opened a virtual circuit,
621 * or if we haven't been asked to keep a socket open,
622 * close the socket.
623 */
624 if ((v_circuit && (statp->options & RES_USEVC) == 0U) ||
625 (statp->options & RES_STAYOPEN) == 0U) {
626 res_nclose(statp);
627 }
628 if (statp->rhook) {
629 int done = 0, loops = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900630
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900631 do {
632 res_sendhookact act;
Bernie Innocenti55864192018-08-30 04:05:20 +0900633
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900634 act = (*statp->rhook)(nsap, buf, buflen, ans, anssiz, &resplen);
635 switch (act) {
636 case res_goahead:
637 case res_done:
638 done = 1;
639 break;
640 case res_nextns:
641 res_nclose(statp);
642 goto next_ns;
643 case res_modified:
644 /* give the hook another try */
645 if (++loops < 42) /*doug adams*/
646 break;
Bernie Innocenti8bb94ba2018-10-10 22:30:12 +0900647 [[fallthrough]];
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900648 case res_error:
Bernie Innocenti8bb94ba2018-10-10 22:30:12 +0900649 [[fallthrough]];
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900650 default:
651 goto fail;
652 }
653 } while (!done);
654 }
655 return (resplen);
656 next_ns:;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900657 } // for each ns
658 } // for each retry
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900659 res_nclose(statp);
660 if (!v_circuit) {
661 if (!gotsomewhere)
662 errno = ECONNREFUSED; /* no nameservers found */
663 else
664 errno = ETIMEDOUT; /* no answer obtained */
665 } else
666 errno = terrno;
Bernie Innocenti55864192018-08-30 04:05:20 +0900667
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900668 _resolv_cache_query_failed(statp->netid, buf, buflen);
Bernie Innocenti55864192018-08-30 04:05:20 +0900669
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900670 return (-1);
671fail:
Bernie Innocenti55864192018-08-30 04:05:20 +0900672
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900673 _resolv_cache_query_failed(statp->netid, buf, buflen);
674 res_nclose(statp);
675 return (-1);
Bernie Innocenti55864192018-08-30 04:05:20 +0900676}
677
678/* Private */
679
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900680static int get_salen(const struct sockaddr* sa) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900681 if (sa->sa_family == AF_INET)
682 return (sizeof(struct sockaddr_in));
683 else if (sa->sa_family == AF_INET6)
684 return (sizeof(struct sockaddr_in6));
685 else
686 return (0); /* unknown, die on connect */
Bernie Innocenti55864192018-08-30 04:05:20 +0900687}
688
689/*
690 * pick appropriate nsaddr_list for use. see res_init() for initialization.
691 */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900692static struct sockaddr* get_nsaddr(res_state statp, size_t n) {
Ken Chencf6ded62018-10-16 23:12:09 +0800693 if (!statp->nsaddr_list[n].sin_family && statp->_u._ext.ext) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900694 /*
Ken Chencf6ded62018-10-16 23:12:09 +0800695 * - statp->_u._ext.ext->nsaddrs[n] holds an address that is larger
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900696 * than struct sockaddr, and
697 * - user code did not update statp->nsaddr_list[n].
698 */
Ken Chencf6ded62018-10-16 23:12:09 +0800699 return (struct sockaddr*) (void*) &statp->_u._ext.ext->nsaddrs[n];
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900700 } else {
701 /*
702 * - user code updated statp->nsaddr_list[n], or
703 * - statp->nsaddr_list[n] has the same content as
Ken Chencf6ded62018-10-16 23:12:09 +0800704 * statp->_u._ext.ext->nsaddrs[n].
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900705 */
706 return (struct sockaddr*) (void*) &statp->nsaddr_list[n];
707 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900708}
709
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900710static struct timespec get_timeout(const res_state statp, const struct __res_params* params,
711 const int ns) {
712 int msec;
713 if (params->base_timeout_msec != 0) {
714 // TODO: scale the timeout by retry attempt and maybe number of servers
715 msec = params->base_timeout_msec;
716 } else {
717 // Legacy algorithm which scales the timeout by nameserver number.
718 // For instance, with 4 nameservers: 5s, 2.5s, 5s, 10s
719 // This has no effect with 1 or 2 nameservers
720 msec = (statp->retrans * 1000) << ns;
721 if (ns > 0) {
722 msec /= statp->nscount;
723 }
724 if (msec < 1000) {
725 msec = 1000; // Use at least 100ms
726 }
727 }
Bernie Innocentie9ba09c2018-09-12 23:20:10 +0900728 VLOG << "using timeout of " << msec << " msec";
Bernie Innocenti55864192018-08-30 04:05:20 +0900729
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900730 struct timespec result;
731 result.tv_sec = msec / 1000;
732 result.tv_nsec = (msec % 1000) * 1000000;
733 return result;
Bernie Innocenti55864192018-08-30 04:05:20 +0900734}
735
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900736static int send_vc(res_state statp, struct __res_params* params, const u_char* buf, int buflen,
737 u_char* ans, int anssiz, int* terrno, int ns, time_t* at, int* rcode,
738 int* delay) {
739 *at = time(NULL);
740 *rcode = RCODE_INTERNAL_ERROR;
741 *delay = 0;
742 const HEADER* hp = (const HEADER*) (const void*) buf;
743 HEADER* anhp = (HEADER*) (void*) ans;
744 struct sockaddr* nsap;
745 int nsaplen;
746 int truncating, connreset, resplen, n;
747 struct iovec iov[2];
748 u_short len;
749 u_char* cp;
Bernie Innocenti55864192018-08-30 04:05:20 +0900750
Bernie Innocentie9ba09c2018-09-12 23:20:10 +0900751 VLOG << "using send_vc";
Bernie Innocenti55864192018-08-30 04:05:20 +0900752
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900753 nsap = get_nsaddr(statp, (size_t) ns);
754 nsaplen = get_salen(nsap);
Bernie Innocenti55864192018-08-30 04:05:20 +0900755
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900756 connreset = 0;
757same_ns:
758 truncating = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900759
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900760 struct timespec now = evNowTime();
Bernie Innocenti55864192018-08-30 04:05:20 +0900761
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900762 /* Are we still talking to whom we want to talk to? */
763 if (statp->_vcsock >= 0 && (statp->_flags & RES_F_VC) != 0) {
764 struct sockaddr_storage peer;
765 socklen_t size = sizeof peer;
766 unsigned old_mark;
767 socklen_t mark_size = sizeof(old_mark);
768 if (getpeername(statp->_vcsock, (struct sockaddr*) (void*) &peer, &size) < 0 ||
769 !sock_eq((struct sockaddr*) (void*) &peer, nsap) ||
770 getsockopt(statp->_vcsock, SOL_SOCKET, SO_MARK, &old_mark, &mark_size) < 0 ||
771 old_mark != statp->_mark) {
772 res_nclose(statp);
773 statp->_flags &= ~RES_F_VC;
774 }
775 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900776
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900777 if (statp->_vcsock < 0 || (statp->_flags & RES_F_VC) == 0) {
778 if (statp->_vcsock >= 0) res_nclose(statp);
Bernie Innocenti55864192018-08-30 04:05:20 +0900779
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900780 statp->_vcsock = socket(nsap->sa_family, SOCK_STREAM | SOCK_CLOEXEC, 0);
781 if (statp->_vcsock < 0) {
782 switch (errno) {
783 case EPROTONOSUPPORT:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900784 case EPFNOSUPPORT:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900785 case EAFNOSUPPORT:
786 Perror(statp, stderr, "socket(vc)", errno);
Bernie Innocentie9ba09c2018-09-12 23:20:10 +0900787 return 0;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900788 default:
789 *terrno = errno;
790 Perror(statp, stderr, "socket(vc)", errno);
Bernie Innocentie9ba09c2018-09-12 23:20:10 +0900791 return -1;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900792 }
793 }
794 fchown(statp->_vcsock, AID_DNS, -1);
795 if (statp->_mark != MARK_UNSET) {
796 if (setsockopt(statp->_vcsock, SOL_SOCKET, SO_MARK, &statp->_mark,
797 sizeof(statp->_mark)) < 0) {
798 *terrno = errno;
799 Perror(statp, stderr, "setsockopt", errno);
800 return -1;
801 }
802 }
803 errno = 0;
804 if (random_bind(statp->_vcsock, nsap->sa_family) < 0) {
805 *terrno = errno;
806 Aerror(statp, stderr, "bind/vc", errno, nsap, nsaplen);
807 res_nclose(statp);
808 return (0);
809 }
810 if (connect_with_timeout(statp->_vcsock, nsap, (socklen_t) nsaplen,
811 get_timeout(statp, params, ns)) < 0) {
812 *terrno = errno;
813 Aerror(statp, stderr, "connect/vc", errno, nsap, nsaplen);
814 res_nclose(statp);
815 /*
816 * The way connect_with_timeout() is implemented prevents us from reliably
817 * determining whether this was really a timeout or e.g. ECONNREFUSED. Since
818 * currently both cases are handled in the same way, there is no need to
819 * change this (yet). If we ever need to reliably distinguish between these
820 * cases, both connect_with_timeout() and retrying_poll() need to be
821 * modified, though.
822 */
823 *rcode = RCODE_TIMEOUT;
824 return (0);
825 }
826 statp->_flags |= RES_F_VC;
827 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900828
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900829 /*
830 * Send length & message
831 */
832 ns_put16((u_short) buflen, (u_char*) (void*) &len);
833 iov[0] = evConsIovec(&len, INT16SZ);
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900834 iov[1] = evConsIovec((void*) buf, (size_t) buflen);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900835 if (writev(statp->_vcsock, iov, 2) != (INT16SZ + buflen)) {
836 *terrno = errno;
837 Perror(statp, stderr, "write failed", errno);
838 res_nclose(statp);
839 return (0);
840 }
841 /*
842 * Receive length & response
843 */
844read_len:
845 cp = ans;
846 len = INT16SZ;
847 while ((n = read(statp->_vcsock, (char*) cp, (size_t) len)) > 0) {
848 cp += n;
849 if ((len -= n) == 0) break;
850 }
851 if (n <= 0) {
852 *terrno = errno;
853 Perror(statp, stderr, "read failed", errno);
854 res_nclose(statp);
855 /*
856 * A long running process might get its TCP
857 * connection reset if the remote server was
858 * restarted. Requery the server instead of
859 * trying a new one. When there is only one
860 * server, this means that a query might work
861 * instead of failing. We only allow one reset
862 * per query to prevent looping.
863 */
864 if (*terrno == ECONNRESET && !connreset) {
865 connreset = 1;
866 res_nclose(statp);
867 goto same_ns;
868 }
869 res_nclose(statp);
870 return (0);
871 }
872 resplen = ns_get16(ans);
873 if (resplen > anssiz) {
874 Dprint(statp->options & RES_DEBUG, (stdout, ";; response truncated\n"));
875 truncating = 1;
876 len = anssiz;
877 } else
878 len = resplen;
879 if (len < HFIXEDSZ) {
880 /*
881 * Undersized message.
882 */
883 Dprint(statp->options & RES_DEBUG, (stdout, ";; undersized: %d\n", len));
884 *terrno = EMSGSIZE;
885 res_nclose(statp);
886 return (0);
887 }
888 cp = ans;
889 while (len != 0 && (n = read(statp->_vcsock, (char*) cp, (size_t) len)) > 0) {
890 cp += n;
891 len -= n;
892 }
893 if (n <= 0) {
894 *terrno = errno;
895 Perror(statp, stderr, "read(vc)", errno);
896 res_nclose(statp);
897 return (0);
898 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900899
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900900 if (truncating) {
901 /*
902 * Flush rest of answer so connection stays in synch.
903 */
904 anhp->tc = 1;
905 len = resplen - anssiz;
906 while (len != 0) {
907 char junk[PACKETSZ];
Bernie Innocenti55864192018-08-30 04:05:20 +0900908
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900909 n = read(statp->_vcsock, junk, (len > sizeof junk) ? sizeof junk : len);
910 if (n > 0)
911 len -= n;
912 else
913 break;
914 }
915 }
916 /*
Bernie Innocentie9ba09c2018-09-12 23:20:10 +0900917 * If the calling application has bailed out of
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900918 * a previous call and failed to arrange to have
919 * the circuit closed or the server has got
920 * itself confused, then drop the packet and
921 * wait for the correct one.
922 */
923 if (hp->id != anhp->id) {
924 DprintQ((statp->options & RES_DEBUG) || (statp->pfcode & RES_PRF_REPLY),
925 (stdout, ";; old answer (unexpected):\n"), ans,
926 (resplen > anssiz) ? anssiz : resplen);
927 goto read_len;
928 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900929
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900930 /*
931 * All is well, or the error is fatal. Signal that the
932 * next nameserver ought not be tried.
933 */
934 if (resplen > 0) {
935 struct timespec done = evNowTime();
936 *delay = _res_stats_calculate_rtt(&done, &now);
937 *rcode = anhp->rcode;
938 }
939 return (resplen);
Bernie Innocenti55864192018-08-30 04:05:20 +0900940}
941
942/* return -1 on error (errno set), 0 on success */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900943static int connect_with_timeout(int sock, const struct sockaddr* nsap, socklen_t salen,
944 const struct timespec timeout) {
945 int res, origflags;
Bernie Innocenti55864192018-08-30 04:05:20 +0900946
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900947 origflags = fcntl(sock, F_GETFL, 0);
948 fcntl(sock, F_SETFL, origflags | O_NONBLOCK);
Bernie Innocenti55864192018-08-30 04:05:20 +0900949
Bernie Innocentif89b3512018-08-30 07:34:37 +0900950 res = connect(sock, nsap, salen);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900951 if (res < 0 && errno != EINPROGRESS) {
952 res = -1;
953 goto done;
954 }
955 if (res != 0) {
956 struct timespec now = evNowTime();
957 struct timespec finish = evAddTime(now, timeout);
Bernie Innocentie9ba09c2018-09-12 23:20:10 +0900958 VLOG << sock << " send_vc";
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900959 res = retrying_poll(sock, POLLIN | POLLOUT, &finish);
960 if (res <= 0) {
961 res = -1;
962 }
963 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900964done:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900965 fcntl(sock, F_SETFL, origflags);
Bernie Innocentie9ba09c2018-09-12 23:20:10 +0900966 VLOG << sock << " connect_with_const timeout returning " << res;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900967 return res;
Bernie Innocenti55864192018-08-30 04:05:20 +0900968}
969
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900970static int retrying_poll(const int sock, const short events, const struct timespec* finish) {
971 struct timespec now, timeout;
Bernie Innocenti55864192018-08-30 04:05:20 +0900972
973retry:
Bernie Innocentie9ba09c2018-09-12 23:20:10 +0900974 VLOG << " " << sock << " retrying_poll";
Bernie Innocenti55864192018-08-30 04:05:20 +0900975
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900976 now = evNowTime();
977 if (evCmpTime(*finish, now) > 0)
978 timeout = evSubTime(*finish, now);
979 else
980 timeout = evConsTime(0L, 0L);
981 struct pollfd fds = {.fd = sock, .events = events};
982 int n = ppoll(&fds, 1, &timeout, /*sigmask=*/NULL);
983 if (n == 0) {
Bernie Innocentie9ba09c2018-09-12 23:20:10 +0900984 VLOG << " " << sock << "retrying_poll timeout";
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900985 errno = ETIMEDOUT;
986 return 0;
987 }
988 if (n < 0) {
989 if (errno == EINTR) goto retry;
Bernie Innocentie9ba09c2018-09-12 23:20:10 +0900990 VLOG << " " << sock << " retrying_poll got error " << n;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900991 return n;
992 }
993 if (fds.revents & (POLLIN | POLLOUT | POLLERR)) {
994 int error;
995 socklen_t len = sizeof(error);
996 if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, &len) < 0 || error) {
997 errno = error;
Bernie Innocentie9ba09c2018-09-12 23:20:10 +0900998 VLOG << " " << sock << " retrying_poll dot error2 " << errno;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900999 return -1;
1000 }
1001 }
Bernie Innocentie9ba09c2018-09-12 23:20:10 +09001002 VLOG << " " << sock << " retrying_poll returning " << n;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001003 return n;
Bernie Innocenti55864192018-08-30 04:05:20 +09001004}
1005
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001006static int send_dg(res_state statp, struct __res_params* params, const u_char* buf, int buflen,
1007 u_char* ans, int anssiz, int* terrno, int ns, int* v_circuit, int* gotsomewhere,
1008 time_t* at, int* rcode, int* delay) {
1009 *at = time(NULL);
1010 *rcode = RCODE_INTERNAL_ERROR;
1011 *delay = 0;
1012 const HEADER* hp = (const HEADER*) (const void*) buf;
1013 HEADER* anhp = (HEADER*) (void*) ans;
1014 const struct sockaddr* nsap;
1015 int nsaplen;
1016 struct timespec now, timeout, finish, done;
1017 struct sockaddr_storage from;
1018 socklen_t fromlen;
1019 int resplen, n, s;
Bernie Innocenti55864192018-08-30 04:05:20 +09001020
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001021 nsap = get_nsaddr(statp, (size_t) ns);
1022 nsaplen = get_salen(nsap);
Ken Chencf6ded62018-10-16 23:12:09 +08001023 if (statp->_u._ext.nssocks[ns] == -1) {
1024 statp->_u._ext.nssocks[ns] = socket(nsap->sa_family, SOCK_DGRAM | SOCK_CLOEXEC, 0);
1025 if (statp->_u._ext.nssocks[ns] < 0) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001026 switch (errno) {
1027 case EPROTONOSUPPORT:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001028 case EPFNOSUPPORT:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001029 case EAFNOSUPPORT:
1030 Perror(statp, stderr, "socket(dg)", errno);
1031 return (0);
1032 default:
1033 *terrno = errno;
1034 Perror(statp, stderr, "socket(dg)", errno);
1035 return (-1);
1036 }
1037 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001038
Ken Chencf6ded62018-10-16 23:12:09 +08001039 fchown(statp->_u._ext.nssocks[ns], AID_DNS, -1);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001040 if (statp->_mark != MARK_UNSET) {
Ken Chencf6ded62018-10-16 23:12:09 +08001041 if (setsockopt(statp->_u._ext.nssocks[ns], SOL_SOCKET, SO_MARK, &(statp->_mark),
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001042 sizeof(statp->_mark)) < 0) {
1043 res_nclose(statp);
1044 return -1;
1045 }
1046 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001047#ifndef CANNOT_CONNECT_DGRAM
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001048 /*
1049 * On a 4.3BSD+ machine (client and server,
1050 * actually), sending to a nameserver datagram
1051 * port with no nameserver will cause an
1052 * ICMP port unreachable message to be returned.
1053 * If our datagram socket is "connected" to the
1054 * server, we get an ECONNREFUSED error on the next
1055 * socket operation, and select returns if the
1056 * error message is received. We can thus detect
1057 * the absence of a nameserver without timing out.
1058 */
Ken Chencf6ded62018-10-16 23:12:09 +08001059 if (random_bind(statp->_u._ext.nssocks[ns], nsap->sa_family) < 0) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001060 Aerror(statp, stderr, "bind(dg)", errno, nsap, nsaplen);
1061 res_nclose(statp);
1062 return (0);
1063 }
Ken Chencf6ded62018-10-16 23:12:09 +08001064 if (connect(statp->_u._ext.nssocks[ns], nsap, (socklen_t) nsaplen) < 0) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001065 Aerror(statp, stderr, "connect(dg)", errno, nsap, nsaplen);
1066 res_nclose(statp);
1067 return (0);
1068 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001069#endif /* !CANNOT_CONNECT_DGRAM */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001070 Dprint(statp->options & RES_DEBUG, (stdout, ";; new DG socket\n"))
1071 }
Ken Chencf6ded62018-10-16 23:12:09 +08001072 s = statp->_u._ext.nssocks[ns];
Bernie Innocenti55864192018-08-30 04:05:20 +09001073#ifndef CANNOT_CONNECT_DGRAM
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001074 if (send(s, (const char*) buf, (size_t) buflen, 0) != buflen) {
1075 Perror(statp, stderr, "send", errno);
1076 res_nclose(statp);
1077 return (0);
1078 }
1079#else /* !CANNOT_CONNECT_DGRAM */
1080 if (sendto(s, (const char*) buf, buflen, 0, nsap, nsaplen) != buflen) {
1081 Aerror(statp, stderr, "sendto", errno, nsap, nsaplen);
1082 res_nclose(statp);
1083 return (0);
1084 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001085#endif /* !CANNOT_CONNECT_DGRAM */
1086
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001087 /*
1088 * Wait for reply.
1089 */
1090 timeout = get_timeout(statp, params, ns);
1091 now = evNowTime();
1092 finish = evAddTime(now, timeout);
Bernie Innocenti55864192018-08-30 04:05:20 +09001093retry:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001094 n = retrying_poll(s, POLLIN, &finish);
Bernie Innocenti55864192018-08-30 04:05:20 +09001095
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001096 if (n == 0) {
1097 *rcode = RCODE_TIMEOUT;
1098 Dprint(statp->options & RES_DEBUG, (stdout, ";; timeout\n"));
1099 *gotsomewhere = 1;
1100 return (0);
1101 }
1102 if (n < 0) {
1103 Perror(statp, stderr, "poll", errno);
1104 res_nclose(statp);
1105 return (0);
1106 }
1107 errno = 0;
1108 fromlen = sizeof(from);
1109 resplen = recvfrom(s, (char*) ans, (size_t) anssiz, 0, (struct sockaddr*) (void*) &from,
1110 &fromlen);
1111 if (resplen <= 0) {
1112 Perror(statp, stderr, "recvfrom", errno);
1113 res_nclose(statp);
1114 return (0);
1115 }
1116 *gotsomewhere = 1;
1117 if (resplen < HFIXEDSZ) {
1118 /*
1119 * Undersized message.
1120 */
1121 Dprint(statp->options & RES_DEBUG, (stdout, ";; undersized: %d\n", resplen));
1122 *terrno = EMSGSIZE;
1123 res_nclose(statp);
1124 return (0);
1125 }
1126 if (hp->id != anhp->id) {
1127 /*
1128 * response from old query, ignore it.
1129 * XXX - potential security hazard could
1130 * be detected here.
1131 */
1132 DprintQ((statp->options & RES_DEBUG) || (statp->pfcode & RES_PRF_REPLY),
1133 (stdout, ";; old answer:\n"), ans, (resplen > anssiz) ? anssiz : resplen);
1134 goto retry;
1135 }
1136 if (!(statp->options & RES_INSECURE1) &&
1137 !res_ourserver_p(statp, (struct sockaddr*) (void*) &from)) {
1138 /*
1139 * response from wrong server? ignore it.
1140 * XXX - potential security hazard could
1141 * be detected here.
1142 */
1143 DprintQ((statp->options & RES_DEBUG) || (statp->pfcode & RES_PRF_REPLY),
1144 (stdout, ";; not our server:\n"), ans, (resplen > anssiz) ? anssiz : resplen);
1145 goto retry;
1146 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001147 if (anhp->rcode == FORMERR && (statp->options & RES_USE_EDNS0) != 0U) {
1148 /*
1149 * Do not retry if the server do not understand EDNS0.
1150 * The case has to be captured here, as FORMERR packet do not
1151 * carry query section, hence res_queriesmatch() returns 0.
1152 */
1153 DprintQ(statp->options & RES_DEBUG, (stdout, "server rejected query with EDNS0:\n"), ans,
1154 (resplen > anssiz) ? anssiz : resplen);
1155 /* record the error */
1156 statp->_flags |= RES_F_EDNS0ERR;
1157 res_nclose(statp);
1158 return (0);
1159 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001160 if (!(statp->options & RES_INSECURE2) &&
1161 !res_queriesmatch(buf, buf + buflen, ans, ans + anssiz)) {
1162 /*
1163 * response contains wrong query? ignore it.
1164 * XXX - potential security hazard could
1165 * be detected here.
1166 */
1167 DprintQ((statp->options & RES_DEBUG) || (statp->pfcode & RES_PRF_REPLY),
1168 (stdout, ";; wrong query name:\n"), ans, (resplen > anssiz) ? anssiz : resplen);
1169 goto retry;
1170 ;
1171 }
1172 done = evNowTime();
1173 *delay = _res_stats_calculate_rtt(&done, &now);
1174 if (anhp->rcode == SERVFAIL || anhp->rcode == NOTIMP || anhp->rcode == REFUSED) {
1175 DprintQ(statp->options & RES_DEBUG, (stdout, "server rejected query:\n"), ans,
1176 (resplen > anssiz) ? anssiz : resplen);
1177 res_nclose(statp);
1178 /* don't retry if called from dig */
1179 if (!statp->pfcode) {
1180 *rcode = anhp->rcode;
1181 return (0);
1182 }
1183 }
1184 if (!(statp->options & RES_IGNTC) && anhp->tc) {
1185 /*
1186 * To get the rest of answer,
1187 * use TCP with same server.
1188 */
1189 Dprint(statp->options & RES_DEBUG, (stdout, ";; truncated answer\n"));
1190 *v_circuit = 1;
1191 res_nclose(statp);
1192 return (1);
1193 }
1194 /*
1195 * All is well, or the error is fatal. Signal that the
1196 * next nameserver ought not be tried.
1197 */
1198 if (resplen > 0) {
1199 *rcode = anhp->rcode;
1200 }
1201 return (resplen);
Bernie Innocenti55864192018-08-30 04:05:20 +09001202}
1203
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001204static void Aerror(const res_state statp, FILE* file, const char* string, int error,
1205 const struct sockaddr* address, int alen) {
Bernie Innocentie9ba09c2018-09-12 23:20:10 +09001206 if (!kVerboseLogging) return;
1207
1208 const int save = errno;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001209 char hbuf[NI_MAXHOST];
1210 char sbuf[NI_MAXSERV];
Bernie Innocentie9ba09c2018-09-12 23:20:10 +09001211 constexpr int niflags = NI_NUMERICHOST | NI_NUMERICSERV;
Bernie Innocenti55864192018-08-30 04:05:20 +09001212
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001213 if ((statp->options & RES_DEBUG) != 0U) {
1214 if (getnameinfo(address, (socklen_t) alen, hbuf, sizeof(hbuf), sbuf, sizeof(sbuf),
1215 niflags)) {
1216 strncpy(hbuf, "?", sizeof(hbuf) - 1);
1217 hbuf[sizeof(hbuf) - 1] = '\0';
1218 strncpy(sbuf, "?", sizeof(sbuf) - 1);
1219 sbuf[sizeof(sbuf) - 1] = '\0';
1220 }
1221 fprintf(file, "res_send: %s ([%s].%s): %s\n", string, hbuf, sbuf, strerror(error));
1222 }
1223 errno = save;
Bernie Innocenti55864192018-08-30 04:05:20 +09001224}
1225
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001226static void Perror(const res_state statp, FILE* file, const char* string, int error) {
Bernie Innocentie9ba09c2018-09-12 23:20:10 +09001227 if (!kVerboseLogging) return;
Bernie Innocenti55864192018-08-30 04:05:20 +09001228
Bernie Innocentie9ba09c2018-09-12 23:20:10 +09001229 const int save = errno;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001230 if ((statp->options & RES_DEBUG) != 0U)
1231 fprintf(file, "res_send: %s: %s\n", string, strerror(error));
1232 errno = save;
Bernie Innocenti55864192018-08-30 04:05:20 +09001233}
1234
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001235static int sock_eq(struct sockaddr* a, struct sockaddr* b) {
1236 struct sockaddr_in *a4, *b4;
1237 struct sockaddr_in6 *a6, *b6;
Bernie Innocenti55864192018-08-30 04:05:20 +09001238
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001239 if (a->sa_family != b->sa_family) return 0;
1240 switch (a->sa_family) {
1241 case AF_INET:
1242 a4 = (struct sockaddr_in*) (void*) a;
1243 b4 = (struct sockaddr_in*) (void*) b;
1244 return a4->sin_port == b4->sin_port && a4->sin_addr.s_addr == b4->sin_addr.s_addr;
1245 case AF_INET6:
1246 a6 = (struct sockaddr_in6*) (void*) a;
1247 b6 = (struct sockaddr_in6*) (void*) b;
1248 return a6->sin6_port == b6->sin6_port &&
Bernie Innocenti55864192018-08-30 04:05:20 +09001249#ifdef HAVE_SIN6_SCOPE_ID
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001250 a6->sin6_scope_id == b6->sin6_scope_id &&
Bernie Innocenti55864192018-08-30 04:05:20 +09001251#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001252 IN6_ARE_ADDR_EQUAL(&a6->sin6_addr, &b6->sin6_addr);
1253 default:
1254 return 0;
1255 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001256}