blob: 5e9d821fa0f6ce00e7250758ec848adc484aa057 [file] [log] [blame]
Bernie Innocenti55864192018-08-30 04:05:20 +09001/* $NetBSD: getaddrinfo.c,v 1.82 2006/03/25 12:09:40 rpaulo Exp $ */
2/* $KAME: getaddrinfo.c,v 1.29 2000/08/31 17:26:57 itojun Exp $ */
3
4/*
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 * 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. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
Bernie Innocentid017e972019-03-03 19:39:53 +090033#define LOG_TAG "getaddrinfo"
34
Bernie Innocenti55864192018-08-30 04:05:20 +090035#include <arpa/inet.h>
36#include <arpa/nameser.h>
37#include <assert.h>
38#include <ctype.h>
39#include <errno.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090040#include <fcntl.h>
41#include <net/if.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090042#include <netdb.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090043#include <netinet/in.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090044#include <stdbool.h>
45#include <stddef.h>
46#include <stdio.h>
47#include <stdlib.h>
48#include <string.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090049#include <sys/param.h>
50#include <sys/socket.h>
51#include <sys/stat.h>
52#include <sys/types.h>
53#include <sys/un.h>
Bernie Innocenti189eb502018-10-01 23:10:18 +090054#include <unistd.h>
Bernie Innocentif89b3512018-08-30 07:34:37 +090055
chenbruce16adee42019-02-20 19:45:50 +080056#include <android-base/logging.h>
57
Bernie Innocenti189eb502018-10-01 23:10:18 +090058#include "netd_resolv/resolv.h"
59#include "resolv_cache.h"
60#include "resolv_private.h"
Bernie Innocenti55864192018-08-30 04:05:20 +090061
Bernie Innocenti55864192018-08-30 04:05:20 +090062#define ANY 0
Bernie Innocenti55864192018-08-30 04:05:20 +090063
Bernie Innocenti93a31342018-12-12 00:43:02 +090064const char in_addrany[] = {0, 0, 0, 0};
65const char in_loopback[] = {127, 0, 0, 1};
66const char in6_addrany[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
67const char in6_loopback[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
Bernie Innocenti55864192018-08-30 04:05:20 +090068
Bernie Innocenti93a31342018-12-12 00:43:02 +090069const struct afd {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090070 int a_af;
71 int a_addrlen;
72 int a_socklen;
73 int a_off;
74 const char* a_addrany;
75 const char* a_loopback;
76 int a_scoped;
77} afdl[] = {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090078 {PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
79 offsetof(struct sockaddr_in6, sin6_addr), in6_addrany, in6_loopback, 1},
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090080 {PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
81 offsetof(struct sockaddr_in, sin_addr), in_addrany, in_loopback, 0},
82 {0, 0, 0, 0, NULL, NULL, 0},
Bernie Innocenti55864192018-08-30 04:05:20 +090083};
84
Bernie Innocenti9ddc10d2019-02-20 18:21:24 +090085struct Explore {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090086 int e_af;
87 int e_socktype;
88 int e_protocol;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090089 int e_wild;
Bernie Innocenti9ddc10d2019-02-20 18:21:24 +090090#define WILD_AF(ex) ((ex).e_wild & 0x01)
91#define WILD_SOCKTYPE(ex) ((ex).e_wild & 0x02)
92#define WILD_PROTOCOL(ex) ((ex).e_wild & 0x04)
Bernie Innocenti55864192018-08-30 04:05:20 +090093};
94
Bernie Innocenti9ddc10d2019-02-20 18:21:24 +090095const Explore explore_options[] = {
Ken Chen3270cf52018-11-07 01:20:48 +080096 {PF_INET6, SOCK_DGRAM, IPPROTO_UDP, 0x07},
97 {PF_INET6, SOCK_STREAM, IPPROTO_TCP, 0x07},
98 {PF_INET6, SOCK_RAW, ANY, 0x05},
99 {PF_INET, SOCK_DGRAM, IPPROTO_UDP, 0x07},
100 {PF_INET, SOCK_STREAM, IPPROTO_TCP, 0x07},
101 {PF_INET, SOCK_RAW, ANY, 0x05},
102 {PF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, 0x07},
103 {PF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, 0x07},
104 {PF_UNSPEC, SOCK_RAW, ANY, 0x05},
Bernie Innocenti55864192018-08-30 04:05:20 +0900105};
106
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900107#define PTON_MAX 16
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900108#define MAXPACKET (8 * 1024)
Bernie Innocenti55864192018-08-30 04:05:20 +0900109
110typedef union {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900111 HEADER hdr;
112 u_char buf[MAXPACKET];
Bernie Innocenti55864192018-08-30 04:05:20 +0900113} querybuf;
114
115struct res_target {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900116 struct res_target* next;
117 const char* name; /* domain name */
118 int qclass, qtype; /* class and type of query */
119 u_char* answer; /* buffer to put answer */
120 int anslen; /* size of answer buffer */
121 int n; /* result length */
Bernie Innocenti55864192018-08-30 04:05:20 +0900122};
123
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900124static int str2number(const char*);
125static int explore_fqdn(const struct addrinfo*, const char*, const char*, struct addrinfo**,
126 const struct android_net_context*);
127static int explore_null(const struct addrinfo*, const char*, struct addrinfo**);
128static int explore_numeric(const struct addrinfo*, const char*, const char*, struct addrinfo**,
129 const char*);
130static int explore_numeric_scope(const struct addrinfo*, const char*, const char*,
131 struct addrinfo**);
132static int get_canonname(const struct addrinfo*, struct addrinfo*, const char*);
133static struct addrinfo* get_ai(const struct addrinfo*, const struct afd*, const char*);
134static int get_portmatch(const struct addrinfo*, const char*);
135static int get_port(const struct addrinfo*, const char*, int);
136static const struct afd* find_afd(int);
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900137static int ip6_str2scopeid(const char*, struct sockaddr_in6*, u_int32_t*);
Bernie Innocenti55864192018-08-30 04:05:20 +0900138
Hungming Chend57ade02018-12-25 15:47:47 +0800139static struct addrinfo* getanswer(const querybuf*, int, const char*, int, const struct addrinfo*,
140 int* herrno);
Bernie Innocenti948f6572018-09-12 21:32:42 +0900141static int dns_getaddrinfo(const char* name, const addrinfo* pai,
142 const android_net_context* netcontext, addrinfo** rv);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900143static void _sethtent(FILE**);
144static void _endhtent(FILE**);
145static struct addrinfo* _gethtent(FILE**, const char*, const struct addrinfo*);
Bernie Innocenti948f6572018-09-12 21:32:42 +0900146static bool files_getaddrinfo(const char* name, const addrinfo* pai, addrinfo** res);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900147static int _find_src_addr(const struct sockaddr*, struct sockaddr*, unsigned, uid_t);
Bernie Innocenti55864192018-08-30 04:05:20 +0900148
Hungming Chen7f0d3292018-12-27 18:33:19 +0800149static int res_queryN(const char* name, res_target* target, res_state res, int* herrno);
150static int res_searchN(const char* name, res_target* target, res_state res, int* herrno);
Mike Yu69615f62018-11-06 15:42:36 +0800151static int res_querydomainN(const char* name, const char* domain, res_target* target, res_state res,
Hungming Chen7f0d3292018-12-27 18:33:19 +0800152 int* herrno);
Bernie Innocenti55864192018-08-30 04:05:20 +0900153
Bernie Innocenti93a31342018-12-12 00:43:02 +0900154const char* const ai_errlist[] = {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900155 "Success",
156 "Address family for hostname not supported", /* EAI_ADDRFAMILY */
157 "Temporary failure in name resolution", /* EAI_AGAIN */
158 "Invalid value for ai_flags", /* EAI_BADFLAGS */
159 "Non-recoverable failure in name resolution", /* EAI_FAIL */
160 "ai_family not supported", /* EAI_FAMILY */
161 "Memory allocation failure", /* EAI_MEMORY */
162 "No address associated with hostname", /* EAI_NODATA */
163 "hostname nor servname provided, or not known", /* EAI_NONAME */
164 "servname not supported for ai_socktype", /* EAI_SERVICE */
165 "ai_socktype not supported", /* EAI_SOCKTYPE */
166 "System error returned in errno", /* EAI_SYSTEM */
167 "Invalid value for hints", /* EAI_BADHINTS */
168 "Resolved protocol is unknown", /* EAI_PROTOCOL */
169 "Argument buffer overflow", /* EAI_OVERFLOW */
170 "Unknown error", /* EAI_MAX */
Bernie Innocenti55864192018-08-30 04:05:20 +0900171};
172
173/* XXX macros that make external reference is BAD. */
174
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900175#define GET_AI(ai, afd, addr) \
176 do { \
177 /* external reference: pai, error, and label free */ \
178 (ai) = get_ai(pai, (afd), (addr)); \
179 if ((ai) == NULL) { \
180 error = EAI_MEMORY; \
181 goto free; \
182 } \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900183 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900184
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900185#define GET_PORT(ai, serv) \
186 do { \
187 /* external reference: error and label free */ \
188 error = get_port((ai), (serv), 0); \
189 if (error != 0) goto free; \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900190 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900191
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900192#define MATCH_FAMILY(x, y, w) \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900193 ((x) == (y) || ((w) && ((x) == PF_UNSPEC || (y) == PF_UNSPEC)))
194#define MATCH(x, y, w) ((x) == (y) || ((w) && ((x) == ANY || (y) == ANY)))
Bernie Innocenti55864192018-08-30 04:05:20 +0900195
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900196const char* gai_strerror(int ecode) {
197 if (ecode < 0 || ecode > EAI_MAX) ecode = EAI_MAX;
198 return ai_errlist[ecode];
Bernie Innocenti55864192018-08-30 04:05:20 +0900199}
200
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900201void freeaddrinfo(struct addrinfo* ai) {
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900202 while (ai) {
203 struct addrinfo* next = ai->ai_next;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900204 if (ai->ai_canonname) free(ai->ai_canonname);
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900205 // Also frees ai->ai_addr which points to extra space beyond addrinfo
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900206 free(ai);
207 ai = next;
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900208 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900209}
210
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900211static int str2number(const char* p) {
212 char* ep;
213 unsigned long v;
Bernie Innocenti55864192018-08-30 04:05:20 +0900214
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900215 assert(p != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900216
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900217 if (*p == '\0') return -1;
218 ep = NULL;
219 errno = 0;
220 v = strtoul(p, &ep, 10);
221 if (errno == 0 && ep && *ep == '\0' && v <= UINT_MAX)
222 return v;
223 else
224 return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900225}
226
227/*
228 * The following functions determine whether IPv4 or IPv6 connectivity is
229 * available in order to implement AI_ADDRCONFIG.
230 *
231 * Strictly speaking, AI_ADDRCONFIG should not look at whether connectivity is
232 * available, but whether addresses of the specified family are "configured
233 * on the local system". However, bionic doesn't currently support getifaddrs,
234 * so checking for connectivity is the next best thing.
235 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900236static int _have_ipv6(unsigned mark, uid_t uid) {
237 static const struct sockaddr_in6 sin6_test = {
238 .sin6_family = AF_INET6,
239 .sin6_addr.s6_addr = {// 2000::
240 0x20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
nuccachene172a4e2018-10-23 17:10:58 +0800241 sockaddr_union addr = {.sin6 = sin6_test};
242 return _find_src_addr(&addr.sa, NULL, mark, uid) == 1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900243}
244
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900245static int _have_ipv4(unsigned mark, uid_t uid) {
246 static const struct sockaddr_in sin_test = {
247 .sin_family = AF_INET,
248 .sin_addr.s_addr = __constant_htonl(0x08080808L) // 8.8.8.8
249 };
nuccachene172a4e2018-10-23 17:10:58 +0800250 sockaddr_union addr = {.sin = sin_test};
251 return _find_src_addr(&addr.sa, NULL, mark, uid) == 1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900252}
253
Bernie Innocentic165ce82018-10-16 23:35:28 +0900254// Internal version of getaddrinfo(), but limited to AI_NUMERICHOST.
255// NOTE: also called by resolv_set_nameservers_for_net().
256int getaddrinfo_numeric(const char* hostname, const char* servname, addrinfo hints,
257 addrinfo** result) {
258 hints.ai_flags = AI_NUMERICHOST;
259 const android_net_context netcontext = {
260 .app_netid = NETID_UNSET,
261 .app_mark = MARK_UNSET,
262 .dns_netid = NETID_UNSET,
263 .dns_mark = MARK_UNSET,
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900264 .uid = NET_CONTEXT_INVALID_UID,
265 };
Bernie Innocentic165ce82018-10-16 23:35:28 +0900266 return android_getaddrinfofornetcontext(hostname, servname, &hints, &netcontext, result);
Bernie Innocenti55864192018-08-30 04:05:20 +0900267}
268
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900269int android_getaddrinfofornetcontext(const char* hostname, const char* servname,
270 const struct addrinfo* hints,
271 const struct android_net_context* netcontext,
272 struct addrinfo** res) {
Ken Chen3270cf52018-11-07 01:20:48 +0800273 struct addrinfo sentinel = {};
Bernie Innocentib47552e2019-02-20 18:39:35 +0900274 struct addrinfo* cur = &sentinel;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900275 int error = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900276
Bernie Innocentib47552e2019-02-20 18:39:35 +0900277 // hostname is allowed to be nullptr
278 // servname is allowed to be nullptr
279 // hints is allowed to be nullptr
280 assert(res != nullptr);
281 assert(netcontext != nullptr);
Bernie Innocenti2319a772019-02-20 17:50:38 +0900282
Bernie Innocentib47552e2019-02-20 18:39:35 +0900283 struct addrinfo ai = {
284 .ai_flags = 0,
285 .ai_family = PF_UNSPEC,
286 .ai_socktype = ANY,
287 .ai_protocol = ANY,
288 .ai_addrlen = 0,
289 .ai_canonname = nullptr,
290 .ai_addr = nullptr,
291 .ai_next = nullptr,
292 };
Bernie Innocenti2319a772019-02-20 17:50:38 +0900293
Ken Chen3270cf52018-11-07 01:20:48 +0800294 do {
295 if (hostname == NULL && servname == NULL) {
296 error = EAI_NONAME;
297 break;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900298 }
Ken Chen3270cf52018-11-07 01:20:48 +0800299 if (hints) {
300 /* error check for hints */
301 if (hints->ai_addrlen || hints->ai_canonname || hints->ai_addr || hints->ai_next) {
302 error = EAI_BADHINTS;
303 break;
304 }
305 if (hints->ai_flags & ~AI_MASK) {
306 error = EAI_BADFLAGS;
307 break;
308 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900309
Ken Chen3270cf52018-11-07 01:20:48 +0800310 if (!(hints->ai_family == PF_UNSPEC || hints->ai_family == PF_INET ||
311 hints->ai_family == PF_INET6)) {
312 error = EAI_FAMILY;
313 break;
314 }
Bernie Innocenti2319a772019-02-20 17:50:38 +0900315
316 ai = *hints;
Ken Chen3270cf52018-11-07 01:20:48 +0800317
318 /*
319 * if both socktype/protocol are specified, check if they
320 * are meaningful combination.
321 */
Bernie Innocenti2319a772019-02-20 17:50:38 +0900322 if (ai.ai_socktype != ANY && ai.ai_protocol != ANY) {
Bernie Innocenti9ddc10d2019-02-20 18:21:24 +0900323 for (const Explore& ex : explore_options) {
324 if (ai.ai_family != ex.e_af) continue;
325 if (ex.e_socktype == ANY) continue;
326 if (ex.e_protocol == ANY) continue;
327 if (ai.ai_socktype == ex.e_socktype && ai.ai_protocol != ex.e_protocol) {
Ken Chen3270cf52018-11-07 01:20:48 +0800328 error = EAI_BADHINTS;
329 break;
330 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900331 }
Ken Chen3270cf52018-11-07 01:20:48 +0800332 if (error) break;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900333 }
334 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900335
Ken Chen3270cf52018-11-07 01:20:48 +0800336 /*
Bernie Innocentib47552e2019-02-20 18:39:35 +0900337 * Check for special cases:
338 * (1) numeric servname is disallowed if socktype/protocol are left unspecified.
339 * (2) servname is disallowed for raw and other inet{,6} sockets.
Ken Chen3270cf52018-11-07 01:20:48 +0800340 */
Bernie Innocenti2319a772019-02-20 17:50:38 +0900341 if (MATCH_FAMILY(ai.ai_family, PF_INET, 1) || MATCH_FAMILY(ai.ai_family, PF_INET6, 1)) {
Bernie Innocentib47552e2019-02-20 18:39:35 +0900342 struct addrinfo tmp = ai;
343 if (tmp.ai_family == PF_UNSPEC) {
344 tmp.ai_family = PF_INET6;
Ken Chen3270cf52018-11-07 01:20:48 +0800345 }
Bernie Innocentib47552e2019-02-20 18:39:35 +0900346 error = get_portmatch(&tmp, servname);
Ken Chen3270cf52018-11-07 01:20:48 +0800347 if (error) break;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900348 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900349
Bernie Innocenti9ddc10d2019-02-20 18:21:24 +0900350 // NULL hostname, or numeric hostname
351 for (const Explore& ex : explore_options) {
Ken Chen3270cf52018-11-07 01:20:48 +0800352 /* PF_UNSPEC entries are prepared for DNS queries only */
Bernie Innocenti9ddc10d2019-02-20 18:21:24 +0900353 if (ex.e_af == PF_UNSPEC) continue;
Bernie Innocenti55864192018-08-30 04:05:20 +0900354
Bernie Innocenti9ddc10d2019-02-20 18:21:24 +0900355 if (!MATCH_FAMILY(ai.ai_family, ex.e_af, WILD_AF(ex))) continue;
356 if (!MATCH(ai.ai_socktype, ex.e_socktype, WILD_SOCKTYPE(ex))) continue;
357 if (!MATCH(ai.ai_protocol, ex.e_protocol, WILD_PROTOCOL(ex))) continue;
Bernie Innocenti55864192018-08-30 04:05:20 +0900358
Bernie Innocentib47552e2019-02-20 18:39:35 +0900359 struct addrinfo tmp = ai;
360 if (tmp.ai_family == PF_UNSPEC) tmp.ai_family = ex.e_af;
361 if (tmp.ai_socktype == ANY && ex.e_socktype != ANY) tmp.ai_socktype = ex.e_socktype;
362 if (tmp.ai_protocol == ANY && ex.e_protocol != ANY) tmp.ai_protocol = ex.e_protocol;
Ken Chen3270cf52018-11-07 01:20:48 +0800363
Bernie Innocentid017e972019-03-03 19:39:53 +0900364 LOG(DEBUG) << "explore_numeric: ai_family=" << tmp.ai_family
365 << " ai_socktype=" << tmp.ai_socktype << " ai_protocol=" << tmp.ai_protocol;
Bernie Innocentib47552e2019-02-20 18:39:35 +0900366 if (hostname == nullptr)
367 error = explore_null(&tmp, servname, &cur->ai_next);
Ken Chen3270cf52018-11-07 01:20:48 +0800368 else
Bernie Innocentib47552e2019-02-20 18:39:35 +0900369 error = explore_numeric_scope(&tmp, hostname, servname, &cur->ai_next);
Ken Chen3270cf52018-11-07 01:20:48 +0800370
371 if (error) break;
372
373 while (cur->ai_next) cur = cur->ai_next;
374 }
375 if (error) break;
376
377 /*
378 * XXX
379 * If numeric representation of AF1 can be interpreted as FQDN
380 * representation of AF2, we need to think again about the code below.
381 */
382 if (sentinel.ai_next) break;
383
Bernie Innocenti9ddc10d2019-02-20 18:21:24 +0900384 if (hostname == nullptr) {
Ken Chen3270cf52018-11-07 01:20:48 +0800385 error = EAI_NODATA;
386 break;
387 }
Bernie Innocenti2319a772019-02-20 17:50:38 +0900388 if (ai.ai_flags & AI_NUMERICHOST) {
Ken Chen3270cf52018-11-07 01:20:48 +0800389 error = EAI_NONAME;
390 break;
391 }
392
393 /*
394 * hostname as alphabetical name.
Bernie Innocenti9ddc10d2019-02-20 18:21:24 +0900395 * We would like to prefer AF_INET6 over AF_INET, so we'll make a outer loop by AFs.
Ken Chen3270cf52018-11-07 01:20:48 +0800396 */
Bernie Innocenti9ddc10d2019-02-20 18:21:24 +0900397 for (const Explore& ex : explore_options) {
Bernie Innocenti9ddc10d2019-02-20 18:21:24 +0900398 // Require exact match for family field
399 if (ai.ai_family != ex.e_af) continue;
Ken Chen3270cf52018-11-07 01:20:48 +0800400
Bernie Innocenti9ddc10d2019-02-20 18:21:24 +0900401 if (!MATCH(ai.ai_socktype, ex.e_socktype, WILD_SOCKTYPE(ex))) {
Ken Chen3270cf52018-11-07 01:20:48 +0800402 continue;
403 }
Bernie Innocenti9ddc10d2019-02-20 18:21:24 +0900404 if (!MATCH(ai.ai_protocol, ex.e_protocol, WILD_PROTOCOL(ex))) {
Ken Chen3270cf52018-11-07 01:20:48 +0800405 continue;
406 }
407
Bernie Innocentib47552e2019-02-20 18:39:35 +0900408 struct addrinfo tmp = ai;
409 if (tmp.ai_socktype == ANY && ex.e_socktype != ANY) tmp.ai_socktype = ex.e_socktype;
410 if (tmp.ai_protocol == ANY && ex.e_protocol != ANY) tmp.ai_protocol = ex.e_protocol;
Ken Chen3270cf52018-11-07 01:20:48 +0800411
Bernie Innocentid017e972019-03-03 19:39:53 +0900412 LOG(DEBUG) << "explore_fqdn(): ai_family=" << tmp.ai_family
413 << " ai_socktype=" << tmp.ai_socktype << " ai_protocol=" << tmp.ai_protocol;
Bernie Innocentib47552e2019-02-20 18:39:35 +0900414 error = explore_fqdn(&tmp, hostname, servname, &cur->ai_next, netcontext);
Ken Chen3270cf52018-11-07 01:20:48 +0800415
416 while (cur->ai_next) cur = cur->ai_next;
417 }
418
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900419 if (sentinel.ai_next) {
Ken Chen3270cf52018-11-07 01:20:48 +0800420 error = 0;
421 } else if (error == 0) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900422 error = EAI_FAIL;
Ken Chen3270cf52018-11-07 01:20:48 +0800423 }
424 } while (0);
425
426 if (error) {
427 freeaddrinfo(sentinel.ai_next);
428 *res = NULL;
429 } else {
430 *res = sentinel.ai_next;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900431 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900432 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900433}
434
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900435// FQDN hostname, DNS lookup
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900436static int explore_fqdn(const struct addrinfo* pai, const char* hostname, const char* servname,
437 struct addrinfo** res, const struct android_net_context* netcontext) {
438 struct addrinfo* result;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900439 int error = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900440
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900441 assert(pai != NULL);
442 /* hostname may be NULL */
443 /* servname may be NULL */
444 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900445
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900446 result = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900447
Bernie Innocenti948f6572018-09-12 21:32:42 +0900448 // If the servname does not match socktype/protocol, ignore it.
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900449 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900450
Bernie Innocenti948f6572018-09-12 21:32:42 +0900451 if (!files_getaddrinfo(hostname, pai, &result)) {
452 error = dns_getaddrinfo(hostname, pai, netcontext, &result);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900453 }
Bernie Innocenti948f6572018-09-12 21:32:42 +0900454 if (!error) {
455 struct addrinfo* cur;
456 for (cur = result; cur; cur = cur->ai_next) {
457 GET_PORT(cur, servname);
458 /* canonname should be filled already */
459 }
460 *res = result;
461 return 0;
462 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900463
464free:
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900465 freeaddrinfo(result);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900466 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900467}
468
469/*
470 * hostname == NULL.
471 * passive socket -> anyaddr (0.0.0.0 or ::)
472 * non-passive socket -> localhost (127.0.0.1 or ::1)
473 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900474static int explore_null(const struct addrinfo* pai, const char* servname, struct addrinfo** res) {
475 int s;
476 const struct afd* afd;
477 struct addrinfo* cur;
478 struct addrinfo sentinel;
479 int error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900480
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900481 assert(pai != NULL);
482 /* servname may be NULL */
483 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900484
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900485 *res = NULL;
486 sentinel.ai_next = NULL;
487 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +0900488
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900489 /*
490 * filter out AFs that are not supported by the kernel
491 * XXX errno?
492 */
493 s = socket(pai->ai_family, SOCK_DGRAM | SOCK_CLOEXEC, 0);
494 if (s < 0) {
495 if (errno != EMFILE) return 0;
496 } else
497 close(s);
Bernie Innocenti55864192018-08-30 04:05:20 +0900498
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900499 /*
500 * if the servname does not match socktype/protocol, ignore it.
501 */
502 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900503
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900504 afd = find_afd(pai->ai_family);
505 if (afd == NULL) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900506
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900507 if (pai->ai_flags & AI_PASSIVE) {
508 GET_AI(cur->ai_next, afd, afd->a_addrany);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900509 GET_PORT(cur->ai_next, servname);
510 } else {
511 GET_AI(cur->ai_next, afd, afd->a_loopback);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900512 GET_PORT(cur->ai_next, servname);
513 }
514 cur = cur->ai_next;
Bernie Innocenti55864192018-08-30 04:05:20 +0900515
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900516 *res = sentinel.ai_next;
517 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900518
519free:
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900520 freeaddrinfo(sentinel.ai_next);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900521 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900522}
523
524/*
525 * numeric hostname
526 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900527static int explore_numeric(const struct addrinfo* pai, const char* hostname, const char* servname,
528 struct addrinfo** res, const char* canonname) {
529 const struct afd* afd;
530 struct addrinfo* cur;
531 struct addrinfo sentinel;
532 int error;
533 char pton[PTON_MAX];
Bernie Innocenti55864192018-08-30 04:05:20 +0900534
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900535 assert(pai != NULL);
536 /* hostname may be NULL */
537 /* servname may be NULL */
538 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900539
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900540 *res = NULL;
541 sentinel.ai_next = NULL;
542 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +0900543
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900544 /*
545 * if the servname does not match socktype/protocol, ignore it.
546 */
547 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900548
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900549 afd = find_afd(pai->ai_family);
550 if (afd == NULL) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900551
Ken Chen15c805a2018-10-17 00:19:59 +0800552 if (inet_pton(afd->a_af, hostname, pton) == 1) {
553 if (pai->ai_family == afd->a_af || pai->ai_family == PF_UNSPEC /*?*/) {
554 GET_AI(cur->ai_next, afd, pton);
555 GET_PORT(cur->ai_next, servname);
556 if ((pai->ai_flags & AI_CANONNAME)) {
557 /*
558 * Set the numeric address itself as
559 * the canonical name, based on a
560 * clarification in rfc2553bis-03.
561 */
Ken Chen3270cf52018-11-07 01:20:48 +0800562 error = get_canonname(pai, cur->ai_next, canonname);
563 if (error != 0) {
564 freeaddrinfo(sentinel.ai_next);
565 return error;
566 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900567 }
Ken Chen15c805a2018-10-17 00:19:59 +0800568 while (cur->ai_next) cur = cur->ai_next;
569 } else
Ken Chen3270cf52018-11-07 01:20:48 +0800570 return EAI_FAMILY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900571 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900572
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900573 *res = sentinel.ai_next;
574 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900575
576free:
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900577 freeaddrinfo(sentinel.ai_next);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900578 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900579}
580
581/*
582 * numeric hostname with scope
583 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900584static int explore_numeric_scope(const struct addrinfo* pai, const char* hostname,
585 const char* servname, struct addrinfo** res) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900586 const struct afd* afd;
587 struct addrinfo* cur;
588 int error;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900589 const char *cp, *scope, *addr;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900590 struct sockaddr_in6* sin6;
Bernie Innocenti55864192018-08-30 04:05:20 +0900591
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900592 assert(pai != NULL);
593 /* hostname may be NULL */
594 /* servname may be NULL */
595 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900596
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900597 /*
598 * if the servname does not match socktype/protocol, ignore it.
599 */
600 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900601
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900602 afd = find_afd(pai->ai_family);
603 if (afd == NULL) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900604
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900605 if (!afd->a_scoped) return explore_numeric(pai, hostname, servname, res, hostname);
Bernie Innocenti55864192018-08-30 04:05:20 +0900606
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900607 cp = strchr(hostname, SCOPE_DELIMITER);
608 if (cp == NULL) return explore_numeric(pai, hostname, servname, res, hostname);
Bernie Innocenti55864192018-08-30 04:05:20 +0900609
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900610 /*
611 * Handle special case of <scoped_address><delimiter><scope id>
612 */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900613 char* hostname2 = strdup(hostname);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900614 if (hostname2 == NULL) return EAI_MEMORY;
615 /* terminate at the delimiter */
616 hostname2[cp - hostname] = '\0';
617 addr = hostname2;
618 scope = cp + 1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900619
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900620 error = explore_numeric(pai, addr, servname, res, hostname);
621 if (error == 0) {
622 u_int32_t scopeid;
Bernie Innocenti55864192018-08-30 04:05:20 +0900623
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900624 for (cur = *res; cur; cur = cur->ai_next) {
625 if (cur->ai_family != AF_INET6) continue;
626 sin6 = (struct sockaddr_in6*) (void*) cur->ai_addr;
627 if (ip6_str2scopeid(scope, sin6, &scopeid) == -1) {
628 free(hostname2);
629 return (EAI_NODATA); /* XXX: is return OK? */
630 }
631 sin6->sin6_scope_id = scopeid;
632 }
633 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900634
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900635 free(hostname2);
Bernie Innocenti55864192018-08-30 04:05:20 +0900636
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900637 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900638}
639
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900640static int get_canonname(const struct addrinfo* pai, struct addrinfo* ai, const char* str) {
641 assert(pai != NULL);
642 assert(ai != NULL);
643 assert(str != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900644
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900645 if ((pai->ai_flags & AI_CANONNAME) != 0) {
646 ai->ai_canonname = strdup(str);
647 if (ai->ai_canonname == NULL) return EAI_MEMORY;
648 }
649 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900650}
651
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900652static struct addrinfo* get_ai(const struct addrinfo* pai, const struct afd* afd,
653 const char* addr) {
654 char* p;
655 struct addrinfo* ai;
Bernie Innocenti55864192018-08-30 04:05:20 +0900656
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900657 assert(pai != NULL);
658 assert(afd != NULL);
659 assert(addr != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900660
nuccachene21023a2018-09-11 11:13:44 +0800661 ai = (struct addrinfo*) malloc(sizeof(struct addrinfo) + sizeof(sockaddr_union));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900662 if (ai == NULL) return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900663
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900664 memcpy(ai, pai, sizeof(struct addrinfo));
665 ai->ai_addr = (struct sockaddr*) (void*) (ai + 1);
nuccachene21023a2018-09-11 11:13:44 +0800666 memset(ai->ai_addr, 0, sizeof(sockaddr_union));
Bernie Innocenti55864192018-08-30 04:05:20 +0900667
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900668 ai->ai_addrlen = afd->a_socklen;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900669 ai->ai_addr->sa_family = ai->ai_family = afd->a_af;
670 p = (char*) (void*) (ai->ai_addr);
671 memcpy(p + afd->a_off, addr, (size_t) afd->a_addrlen);
672 return ai;
Bernie Innocenti55864192018-08-30 04:05:20 +0900673}
674
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900675static int get_portmatch(const struct addrinfo* ai, const char* servname) {
676 assert(ai != NULL);
677 /* servname may be NULL */
Bernie Innocenti55864192018-08-30 04:05:20 +0900678
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900679 return get_port(ai, servname, 1);
Bernie Innocenti55864192018-08-30 04:05:20 +0900680}
681
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900682static int get_port(const struct addrinfo* ai, const char* servname, int matchonly) {
683 const char* proto;
684 struct servent* sp;
685 int port;
686 int allownumeric;
Bernie Innocenti55864192018-08-30 04:05:20 +0900687
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900688 assert(ai != NULL);
689 /* servname may be NULL */
Bernie Innocenti55864192018-08-30 04:05:20 +0900690
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900691 if (servname == NULL) return 0;
692 switch (ai->ai_family) {
693 case AF_INET:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900694 case AF_INET6:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900695 break;
696 default:
697 return 0;
698 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900699
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900700 switch (ai->ai_socktype) {
701 case SOCK_RAW:
702 return EAI_SERVICE;
703 case SOCK_DGRAM:
704 case SOCK_STREAM:
705 allownumeric = 1;
706 break;
707 case ANY:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900708 allownumeric = 1;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900709 break;
710 default:
711 return EAI_SOCKTYPE;
712 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900713
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900714 port = str2number(servname);
715 if (port >= 0) {
716 if (!allownumeric) return EAI_SERVICE;
717 if (port < 0 || port > 65535) return EAI_SERVICE;
718 port = htons(port);
719 } else {
720 if (ai->ai_flags & AI_NUMERICSERV) return EAI_NONAME;
Bernie Innocenti55864192018-08-30 04:05:20 +0900721
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900722 switch (ai->ai_socktype) {
723 case SOCK_DGRAM:
724 proto = "udp";
725 break;
726 case SOCK_STREAM:
727 proto = "tcp";
728 break;
729 default:
730 proto = NULL;
731 break;
732 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900733
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900734 if ((sp = getservbyname(servname, proto)) == NULL) return EAI_SERVICE;
735 port = sp->s_port;
736 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900737
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900738 if (!matchonly) {
739 switch (ai->ai_family) {
740 case AF_INET:
741 ((struct sockaddr_in*) (void*) ai->ai_addr)->sin_port = port;
742 break;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900743 case AF_INET6:
744 ((struct sockaddr_in6*) (void*) ai->ai_addr)->sin6_port = port;
745 break;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900746 }
747 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900748
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900749 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900750}
751
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900752static const struct afd* find_afd(int af) {
753 const struct afd* afd;
Bernie Innocenti55864192018-08-30 04:05:20 +0900754
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900755 if (af == PF_UNSPEC) return NULL;
756 for (afd = afdl; afd->a_af; afd++) {
757 if (afd->a_af == af) return afd;
758 }
759 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900760}
761
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900762// Convert a string to a scope identifier.
763static int ip6_str2scopeid(const char* scope, struct sockaddr_in6* sin6, u_int32_t* scopeid) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900764 u_long lscopeid;
765 struct in6_addr* a6;
766 char* ep;
Bernie Innocenti55864192018-08-30 04:05:20 +0900767
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900768 assert(scope != NULL);
769 assert(sin6 != NULL);
770 assert(scopeid != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900771
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900772 a6 = &sin6->sin6_addr;
Bernie Innocenti55864192018-08-30 04:05:20 +0900773
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900774 /* empty scopeid portion is invalid */
775 if (*scope == '\0') return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900776
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900777 if (IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) {
778 /*
779 * We currently assume a one-to-one mapping between links
780 * and interfaces, so we simply use interface indices for
781 * like-local scopes.
782 */
783 *scopeid = if_nametoindex(scope);
784 if (*scopeid == 0) goto trynumeric;
785 return 0;
786 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900787
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900788 /* still unclear about literal, allow numeric only - placeholder */
789 if (IN6_IS_ADDR_SITELOCAL(a6) || IN6_IS_ADDR_MC_SITELOCAL(a6)) goto trynumeric;
790 if (IN6_IS_ADDR_MC_ORGLOCAL(a6))
791 goto trynumeric;
792 else
793 goto trynumeric; /* global */
Bernie Innocenti55864192018-08-30 04:05:20 +0900794
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900795 /* try to convert to a numeric id as a last resort */
796trynumeric:
797 errno = 0;
798 lscopeid = strtoul(scope, &ep, 10);
799 *scopeid = (u_int32_t)(lscopeid & 0xffffffffUL);
800 if (errno == 0 && ep && *ep == '\0' && *scopeid == lscopeid)
801 return 0;
802 else
803 return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900804}
Bernie Innocenti55864192018-08-30 04:05:20 +0900805
806/* code duplicate with gethnamaddr.c */
807
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900808#define BOUNDED_INCR(x) \
809 do { \
810 BOUNDS_CHECK(cp, x); \
811 cp += (x); \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900812 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900813
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900814#define BOUNDS_CHECK(ptr, count) \
815 do { \
816 if (eom - (ptr) < (count)) { \
Hungming Chend57ade02018-12-25 15:47:47 +0800817 *herrno = NO_RECOVERY; \
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900818 return NULL; \
819 } \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900820 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900821
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900822static struct addrinfo* getanswer(const querybuf* answer, int anslen, const char* qname, int qtype,
Hungming Chend57ade02018-12-25 15:47:47 +0800823 const struct addrinfo* pai, int* herrno) {
Ken Chen3270cf52018-11-07 01:20:48 +0800824 struct addrinfo sentinel = {};
825 struct addrinfo *cur;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900826 struct addrinfo ai;
827 const struct afd* afd;
828 char* canonname;
829 const HEADER* hp;
830 const u_char* cp;
831 int n;
832 const u_char* eom;
833 char *bp, *ep;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900834 int type, ancount, qdcount;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900835 int haveanswer, had_error;
836 char tbuf[MAXDNAME];
837 int (*name_ok)(const char*);
838 char hostbuf[8 * 1024];
Bernie Innocenti55864192018-08-30 04:05:20 +0900839
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900840 assert(answer != NULL);
841 assert(qname != NULL);
842 assert(pai != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900843
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900844 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +0900845
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900846 canonname = NULL;
847 eom = answer->buf + anslen;
848 switch (qtype) {
849 case T_A:
850 case T_AAAA:
851 case T_ANY: /*use T_ANY only for T_A/T_AAAA lookup*/
852 name_ok = res_hnok;
853 break;
854 default:
855 return NULL; /* XXX should be abort(); */
856 }
857 /*
858 * find first satisfactory answer
859 */
860 hp = &answer->hdr;
861 ancount = ntohs(hp->ancount);
862 qdcount = ntohs(hp->qdcount);
863 bp = hostbuf;
864 ep = hostbuf + sizeof hostbuf;
865 cp = answer->buf;
866 BOUNDED_INCR(HFIXEDSZ);
867 if (qdcount != 1) {
Hungming Chend57ade02018-12-25 15:47:47 +0800868 *herrno = NO_RECOVERY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900869 return (NULL);
870 }
871 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
872 if ((n < 0) || !(*name_ok)(bp)) {
Hungming Chend57ade02018-12-25 15:47:47 +0800873 *herrno = NO_RECOVERY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900874 return (NULL);
875 }
876 BOUNDED_INCR(n + QFIXEDSZ);
877 if (qtype == T_A || qtype == T_AAAA || qtype == T_ANY) {
878 /* res_send() has already verified that the query name is the
879 * same as the one we sent; this just gets the expanded name
880 * (i.e., with the succeeding search-domain tacked on).
881 */
882 n = strlen(bp) + 1; /* for the \0 */
883 if (n >= MAXHOSTNAMELEN) {
Hungming Chend57ade02018-12-25 15:47:47 +0800884 *herrno = NO_RECOVERY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900885 return (NULL);
886 }
887 canonname = bp;
888 bp += n;
889 /* The qname can be abbreviated, but h_name is now absolute. */
890 qname = canonname;
891 }
892 haveanswer = 0;
893 had_error = 0;
894 while (ancount-- > 0 && cp < eom && !had_error) {
895 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
896 if ((n < 0) || !(*name_ok)(bp)) {
897 had_error++;
898 continue;
899 }
900 cp += n; /* name */
901 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
Bernie Innocentiee1b85b2018-09-25 14:23:19 +0900902 type = ns_get16(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900903 cp += INT16SZ; /* type */
Bernie Innocentiee1b85b2018-09-25 14:23:19 +0900904 int cl = ns_get16(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900905 cp += INT16SZ + INT32SZ; /* class, TTL */
Bernie Innocentiee1b85b2018-09-25 14:23:19 +0900906 n = ns_get16(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900907 cp += INT16SZ; /* len */
908 BOUNDS_CHECK(cp, n);
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900909 if (cl != C_IN) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900910 /* XXX - debug? syslog? */
911 cp += n;
912 continue; /* XXX - had_error++ ? */
913 }
914 if ((qtype == T_A || qtype == T_AAAA || qtype == T_ANY) && type == T_CNAME) {
915 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
916 if ((n < 0) || !(*name_ok)(tbuf)) {
917 had_error++;
918 continue;
919 }
920 cp += n;
921 /* Get canonical name. */
922 n = strlen(tbuf) + 1; /* for the \0 */
923 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
924 had_error++;
925 continue;
926 }
927 strlcpy(bp, tbuf, (size_t)(ep - bp));
928 canonname = bp;
929 bp += n;
930 continue;
931 }
932 if (qtype == T_ANY) {
933 if (!(type == T_A || type == T_AAAA)) {
934 cp += n;
935 continue;
936 }
937 } else if (type != qtype) {
938 if (type != T_KEY && type != T_SIG)
chenbruce16adee42019-02-20 19:45:50 +0800939 LOG(DEBUG) << __func__ << "(getanswer): asked for \"" << qname << " "
940 << p_class(C_IN) << " " << p_type(qtype) << "\", got type \""
941 << p_type(type) << "\"";
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900942 cp += n;
943 continue; /* XXX - had_error++ ? */
944 }
945 switch (type) {
946 case T_A:
947 case T_AAAA:
948 if (strcasecmp(canonname, bp) != 0) {
chenbruce16adee42019-02-20 19:45:50 +0800949 LOG(DEBUG) << __func__ << "(getanswer): asked for \"" << canonname
950 << "\", got \"" << bp << "\"";
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900951 cp += n;
952 continue; /* XXX - had_error++ ? */
953 }
954 if (type == T_A && n != INADDRSZ) {
955 cp += n;
956 continue;
957 }
958 if (type == T_AAAA && n != IN6ADDRSZ) {
959 cp += n;
960 continue;
961 }
962 if (type == T_AAAA) {
963 struct in6_addr in6;
964 memcpy(&in6, cp, IN6ADDRSZ);
965 if (IN6_IS_ADDR_V4MAPPED(&in6)) {
966 cp += n;
967 continue;
968 }
969 }
970 if (!haveanswer) {
971 int nn;
Bernie Innocenti55864192018-08-30 04:05:20 +0900972
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900973 canonname = bp;
974 nn = strlen(bp) + 1; /* for the \0 */
975 bp += nn;
976 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900977
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900978 /* don't overwrite pai */
979 ai = *pai;
980 ai.ai_family = (type == T_A) ? AF_INET : AF_INET6;
981 afd = find_afd(ai.ai_family);
982 if (afd == NULL) {
983 cp += n;
984 continue;
985 }
986 cur->ai_next = get_ai(&ai, afd, (const char*) cp);
987 if (cur->ai_next == NULL) had_error++;
988 while (cur && cur->ai_next) cur = cur->ai_next;
989 cp += n;
990 break;
991 default:
992 abort();
993 }
994 if (!had_error) haveanswer++;
995 }
996 if (haveanswer) {
997 if (!canonname)
998 (void) get_canonname(pai, sentinel.ai_next, qname);
999 else
1000 (void) get_canonname(pai, sentinel.ai_next, canonname);
Hungming Chend57ade02018-12-25 15:47:47 +08001001 *herrno = NETDB_SUCCESS;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001002 return sentinel.ai_next;
1003 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001004
Hungming Chend57ade02018-12-25 15:47:47 +08001005 *herrno = NO_RECOVERY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001006 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001007}
1008
1009struct addrinfo_sort_elem {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001010 struct addrinfo* ai;
1011 int has_src_addr;
1012 sockaddr_union src_addr;
1013 int original_order;
Bernie Innocenti55864192018-08-30 04:05:20 +09001014};
1015
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001016static int _get_scope(const struct sockaddr* addr) {
1017 if (addr->sa_family == AF_INET6) {
1018 const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
1019 if (IN6_IS_ADDR_MULTICAST(&addr6->sin6_addr)) {
1020 return IPV6_ADDR_MC_SCOPE(&addr6->sin6_addr);
1021 } else if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr) ||
1022 IN6_IS_ADDR_LINKLOCAL(&addr6->sin6_addr)) {
1023 /*
1024 * RFC 4291 section 2.5.3 says loopback is to be treated as having
1025 * link-local scope.
1026 */
1027 return IPV6_ADDR_SCOPE_LINKLOCAL;
1028 } else if (IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr)) {
1029 return IPV6_ADDR_SCOPE_SITELOCAL;
1030 } else {
1031 return IPV6_ADDR_SCOPE_GLOBAL;
1032 }
1033 } else if (addr->sa_family == AF_INET) {
1034 const struct sockaddr_in* addr4 = (const struct sockaddr_in*) addr;
1035 unsigned long int na = ntohl(addr4->sin_addr.s_addr);
Bernie Innocenti55864192018-08-30 04:05:20 +09001036
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001037 if (IN_LOOPBACK(na) || /* 127.0.0.0/8 */
1038 (na & 0xffff0000) == 0xa9fe0000) { /* 169.254.0.0/16 */
1039 return IPV6_ADDR_SCOPE_LINKLOCAL;
1040 } else {
1041 /*
1042 * RFC 6724 section 3.2. Other IPv4 addresses, including private addresses
1043 * and shared addresses (100.64.0.0/10), are assigned global scope.
1044 */
1045 return IPV6_ADDR_SCOPE_GLOBAL;
1046 }
1047 } else {
1048 /*
1049 * This should never happen.
1050 * Return a scope with low priority as a last resort.
1051 */
1052 return IPV6_ADDR_SCOPE_NODELOCAL;
1053 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001054}
1055
1056/* These macros are modelled after the ones in <netinet/in6.h>. */
1057
1058/* RFC 4380, section 2.6 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001059#define IN6_IS_ADDR_TEREDO(a) \
1060 ((*(const uint32_t*) (const void*) (&(a)->s6_addr[0]) == ntohl(0x20010000)))
Bernie Innocenti55864192018-08-30 04:05:20 +09001061
1062/* RFC 3056, section 2. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001063#define IN6_IS_ADDR_6TO4(a) (((a)->s6_addr[0] == 0x20) && ((a)->s6_addr[1] == 0x02))
Bernie Innocenti55864192018-08-30 04:05:20 +09001064
1065/* 6bone testing address area (3ffe::/16), deprecated in RFC 3701. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001066#define IN6_IS_ADDR_6BONE(a) (((a)->s6_addr[0] == 0x3f) && ((a)->s6_addr[1] == 0xfe))
Bernie Innocenti55864192018-08-30 04:05:20 +09001067
1068/*
1069 * Get the label for a given IPv4/IPv6 address.
1070 * RFC 6724, section 2.1.
1071 */
1072
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001073static int _get_label(const struct sockaddr* addr) {
1074 if (addr->sa_family == AF_INET) {
1075 return 4;
1076 } else if (addr->sa_family == AF_INET6) {
1077 const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
1078 if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr)) {
1079 return 0;
1080 } else if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr)) {
1081 return 4;
1082 } else if (IN6_IS_ADDR_6TO4(&addr6->sin6_addr)) {
1083 return 2;
1084 } else if (IN6_IS_ADDR_TEREDO(&addr6->sin6_addr)) {
1085 return 5;
1086 } else if (IN6_IS_ADDR_ULA(&addr6->sin6_addr)) {
1087 return 13;
1088 } else if (IN6_IS_ADDR_V4COMPAT(&addr6->sin6_addr)) {
1089 return 3;
1090 } else if (IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr)) {
1091 return 11;
1092 } else if (IN6_IS_ADDR_6BONE(&addr6->sin6_addr)) {
1093 return 12;
1094 } else {
1095 /* All other IPv6 addresses, including global unicast addresses. */
1096 return 1;
1097 }
1098 } else {
1099 /*
1100 * This should never happen.
1101 * Return a semi-random label as a last resort.
1102 */
1103 return 1;
1104 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001105}
1106
1107/*
1108 * Get the precedence for a given IPv4/IPv6 address.
1109 * RFC 6724, section 2.1.
1110 */
1111
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001112static int _get_precedence(const struct sockaddr* addr) {
1113 if (addr->sa_family == AF_INET) {
1114 return 35;
1115 } else if (addr->sa_family == AF_INET6) {
1116 const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
1117 if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr)) {
1118 return 50;
1119 } else if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr)) {
1120 return 35;
1121 } else if (IN6_IS_ADDR_6TO4(&addr6->sin6_addr)) {
1122 return 30;
1123 } else if (IN6_IS_ADDR_TEREDO(&addr6->sin6_addr)) {
1124 return 5;
1125 } else if (IN6_IS_ADDR_ULA(&addr6->sin6_addr)) {
1126 return 3;
1127 } else if (IN6_IS_ADDR_V4COMPAT(&addr6->sin6_addr) ||
1128 IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr) ||
1129 IN6_IS_ADDR_6BONE(&addr6->sin6_addr)) {
1130 return 1;
1131 } else {
1132 /* All other IPv6 addresses, including global unicast addresses. */
1133 return 40;
1134 }
1135 } else {
1136 return 1;
1137 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001138}
1139
1140/*
1141 * Find number of matching initial bits between the two addresses a1 and a2.
1142 */
1143
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001144static int _common_prefix_len(const struct in6_addr* a1, const struct in6_addr* a2) {
1145 const char* p1 = (const char*) a1;
1146 const char* p2 = (const char*) a2;
1147 unsigned i;
Bernie Innocenti55864192018-08-30 04:05:20 +09001148
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001149 for (i = 0; i < sizeof(*a1); ++i) {
1150 int x, j;
Bernie Innocenti55864192018-08-30 04:05:20 +09001151
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001152 if (p1[i] == p2[i]) {
1153 continue;
1154 }
1155 x = p1[i] ^ p2[i];
1156 for (j = 0; j < CHAR_BIT; ++j) {
1157 if (x & (1 << (CHAR_BIT - 1))) {
1158 return i * CHAR_BIT + j;
1159 }
1160 x <<= 1;
1161 }
1162 }
1163 return sizeof(*a1) * CHAR_BIT;
Bernie Innocenti55864192018-08-30 04:05:20 +09001164}
1165
1166/*
1167 * Compare two source/destination address pairs.
1168 * RFC 6724, section 6.
1169 */
1170
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001171static int _rfc6724_compare(const void* ptr1, const void* ptr2) {
1172 const struct addrinfo_sort_elem* a1 = (const struct addrinfo_sort_elem*) ptr1;
1173 const struct addrinfo_sort_elem* a2 = (const struct addrinfo_sort_elem*) ptr2;
1174 int scope_src1, scope_dst1, scope_match1;
1175 int scope_src2, scope_dst2, scope_match2;
1176 int label_src1, label_dst1, label_match1;
1177 int label_src2, label_dst2, label_match2;
1178 int precedence1, precedence2;
1179 int prefixlen1, prefixlen2;
Bernie Innocenti55864192018-08-30 04:05:20 +09001180
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001181 /* Rule 1: Avoid unusable destinations. */
1182 if (a1->has_src_addr != a2->has_src_addr) {
1183 return a2->has_src_addr - a1->has_src_addr;
1184 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001185
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001186 /* Rule 2: Prefer matching scope. */
nuccachene172a4e2018-10-23 17:10:58 +08001187 scope_src1 = _get_scope(&a1->src_addr.sa);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001188 scope_dst1 = _get_scope(a1->ai->ai_addr);
1189 scope_match1 = (scope_src1 == scope_dst1);
Bernie Innocenti55864192018-08-30 04:05:20 +09001190
nuccachene172a4e2018-10-23 17:10:58 +08001191 scope_src2 = _get_scope(&a2->src_addr.sa);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001192 scope_dst2 = _get_scope(a2->ai->ai_addr);
1193 scope_match2 = (scope_src2 == scope_dst2);
Bernie Innocenti55864192018-08-30 04:05:20 +09001194
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001195 if (scope_match1 != scope_match2) {
1196 return scope_match2 - scope_match1;
1197 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001198
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001199 /*
1200 * Rule 3: Avoid deprecated addresses.
1201 * TODO(sesse): We don't currently have a good way of finding this.
1202 */
Bernie Innocenti55864192018-08-30 04:05:20 +09001203
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001204 /*
1205 * Rule 4: Prefer home addresses.
1206 * TODO(sesse): We don't currently have a good way of finding this.
1207 */
Bernie Innocenti55864192018-08-30 04:05:20 +09001208
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001209 /* Rule 5: Prefer matching label. */
nuccachene172a4e2018-10-23 17:10:58 +08001210 label_src1 = _get_label(&a1->src_addr.sa);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001211 label_dst1 = _get_label(a1->ai->ai_addr);
1212 label_match1 = (label_src1 == label_dst1);
Bernie Innocenti55864192018-08-30 04:05:20 +09001213
nuccachene172a4e2018-10-23 17:10:58 +08001214 label_src2 = _get_label(&a2->src_addr.sa);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001215 label_dst2 = _get_label(a2->ai->ai_addr);
1216 label_match2 = (label_src2 == label_dst2);
Bernie Innocenti55864192018-08-30 04:05:20 +09001217
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001218 if (label_match1 != label_match2) {
1219 return label_match2 - label_match1;
1220 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001221
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001222 /* Rule 6: Prefer higher precedence. */
1223 precedence1 = _get_precedence(a1->ai->ai_addr);
1224 precedence2 = _get_precedence(a2->ai->ai_addr);
1225 if (precedence1 != precedence2) {
1226 return precedence2 - precedence1;
1227 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001228
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001229 /*
1230 * Rule 7: Prefer native transport.
1231 * TODO(sesse): We don't currently have a good way of finding this.
1232 */
Bernie Innocenti55864192018-08-30 04:05:20 +09001233
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001234 /* Rule 8: Prefer smaller scope. */
1235 if (scope_dst1 != scope_dst2) {
1236 return scope_dst1 - scope_dst2;
1237 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001238
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001239 /*
1240 * Rule 9: Use longest matching prefix.
1241 * We implement this for IPv6 only, as the rules in RFC 6724 don't seem
1242 * to work very well directly applied to IPv4. (glibc uses information from
1243 * the routing table for a custom IPv4 implementation here.)
1244 */
1245 if (a1->has_src_addr && a1->ai->ai_addr->sa_family == AF_INET6 && a2->has_src_addr &&
1246 a2->ai->ai_addr->sa_family == AF_INET6) {
nuccachene172a4e2018-10-23 17:10:58 +08001247 const struct sockaddr_in6* a1_src = &a1->src_addr.sin6;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001248 const struct sockaddr_in6* a1_dst = (const struct sockaddr_in6*) a1->ai->ai_addr;
nuccachene172a4e2018-10-23 17:10:58 +08001249 const struct sockaddr_in6* a2_src = &a2->src_addr.sin6;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001250 const struct sockaddr_in6* a2_dst = (const struct sockaddr_in6*) a2->ai->ai_addr;
1251 prefixlen1 = _common_prefix_len(&a1_src->sin6_addr, &a1_dst->sin6_addr);
1252 prefixlen2 = _common_prefix_len(&a2_src->sin6_addr, &a2_dst->sin6_addr);
1253 if (prefixlen1 != prefixlen2) {
1254 return prefixlen2 - prefixlen1;
1255 }
1256 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001257
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001258 /*
1259 * Rule 10: Leave the order unchanged.
1260 * We need this since qsort() is not necessarily stable.
1261 */
1262 return a1->original_order - a2->original_order;
Bernie Innocenti55864192018-08-30 04:05:20 +09001263}
1264
1265/*
1266 * Find the source address that will be used if trying to connect to the given
1267 * address. src_addr must be large enough to hold a struct sockaddr_in6.
1268 *
1269 * Returns 1 if a source address was found, 0 if the address is unreachable,
1270 * and -1 if a fatal error occurred. If 0 or -1, the contents of src_addr are
1271 * undefined.
1272 */
1273
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001274static int _find_src_addr(const struct sockaddr* addr, struct sockaddr* src_addr, unsigned mark,
1275 uid_t uid) {
1276 int sock;
1277 int ret;
1278 socklen_t len;
Bernie Innocenti55864192018-08-30 04:05:20 +09001279
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001280 switch (addr->sa_family) {
1281 case AF_INET:
1282 len = sizeof(struct sockaddr_in);
1283 break;
1284 case AF_INET6:
1285 len = sizeof(struct sockaddr_in6);
1286 break;
1287 default:
1288 /* No known usable source address for non-INET families. */
1289 return 0;
1290 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001291
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001292 sock = socket(addr->sa_family, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP);
1293 if (sock == -1) {
1294 if (errno == EAFNOSUPPORT) {
1295 return 0;
1296 } else {
1297 return -1;
1298 }
1299 }
1300 if (mark != MARK_UNSET && setsockopt(sock, SOL_SOCKET, SO_MARK, &mark, sizeof(mark)) < 0) {
1301 close(sock);
1302 return 0;
1303 }
1304 if (uid > 0 && uid != NET_CONTEXT_INVALID_UID && fchown(sock, uid, (gid_t) -1) < 0) {
1305 close(sock);
1306 return 0;
1307 }
1308 do {
Bernie Innocentif89b3512018-08-30 07:34:37 +09001309 ret = connect(sock, addr, len);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001310 } while (ret == -1 && errno == EINTR);
Bernie Innocenti55864192018-08-30 04:05:20 +09001311
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001312 if (ret == -1) {
1313 close(sock);
1314 return 0;
1315 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001316
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001317 if (src_addr && getsockname(sock, src_addr, &len) == -1) {
1318 close(sock);
1319 return -1;
1320 }
1321 close(sock);
1322 return 1;
Bernie Innocenti55864192018-08-30 04:05:20 +09001323}
1324
1325/*
1326 * Sort the linked list starting at sentinel->ai_next in RFC6724 order.
1327 * Will leave the list unchanged if an error occurs.
1328 */
1329
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001330static void _rfc6724_sort(struct addrinfo* list_sentinel, unsigned mark, uid_t uid) {
1331 struct addrinfo* cur;
1332 int nelem = 0, i;
1333 struct addrinfo_sort_elem* elems;
Bernie Innocenti55864192018-08-30 04:05:20 +09001334
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001335 cur = list_sentinel->ai_next;
1336 while (cur) {
1337 ++nelem;
1338 cur = cur->ai_next;
1339 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001340
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001341 elems = (struct addrinfo_sort_elem*) malloc(nelem * sizeof(struct addrinfo_sort_elem));
1342 if (elems == NULL) {
1343 goto error;
1344 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001345
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001346 /*
1347 * Convert the linked list to an array that also contains the candidate
1348 * source address for each destination address.
1349 */
1350 for (i = 0, cur = list_sentinel->ai_next; i < nelem; ++i, cur = cur->ai_next) {
1351 int has_src_addr;
1352 assert(cur != NULL);
1353 elems[i].ai = cur;
1354 elems[i].original_order = i;
Bernie Innocenti55864192018-08-30 04:05:20 +09001355
nuccachene172a4e2018-10-23 17:10:58 +08001356 has_src_addr = _find_src_addr(cur->ai_addr, &elems[i].src_addr.sa, mark, uid);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001357 if (has_src_addr == -1) {
1358 goto error;
1359 }
1360 elems[i].has_src_addr = has_src_addr;
1361 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001362
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001363 /* Sort the addresses, and rearrange the linked list so it matches the sorted order. */
1364 qsort((void*) elems, nelem, sizeof(struct addrinfo_sort_elem), _rfc6724_compare);
Bernie Innocenti55864192018-08-30 04:05:20 +09001365
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001366 list_sentinel->ai_next = elems[0].ai;
1367 for (i = 0; i < nelem - 1; ++i) {
1368 elems[i].ai->ai_next = elems[i + 1].ai;
1369 }
1370 elems[nelem - 1].ai->ai_next = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001371
1372error:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001373 free(elems);
Bernie Innocenti55864192018-08-30 04:05:20 +09001374}
1375
Bernie Innocenti948f6572018-09-12 21:32:42 +09001376static int dns_getaddrinfo(const char* name, const addrinfo* pai,
1377 const android_net_context* netcontext, addrinfo** rv) {
Ken Chen3270cf52018-11-07 01:20:48 +08001378 struct addrinfo *ai, *cur;
1379 struct addrinfo sentinel = {};
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001380 struct res_target q, q2;
1381 res_state res;
Bernie Innocenti55864192018-08-30 04:05:20 +09001382
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001383 memset(&q, 0, sizeof(q));
1384 memset(&q2, 0, sizeof(q2));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001385 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +09001386
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001387 querybuf* buf = (querybuf*) malloc(sizeof(*buf));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001388 if (buf == NULL) {
Bernie Innocenti948f6572018-09-12 21:32:42 +09001389 return EAI_MEMORY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001390 }
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001391 querybuf* buf2 = (querybuf*) malloc(sizeof(*buf2));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001392 if (buf2 == NULL) {
1393 free(buf);
Bernie Innocenti948f6572018-09-12 21:32:42 +09001394 return EAI_MEMORY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001395 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001396
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001397 switch (pai->ai_family) {
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001398 case AF_UNSPEC: {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001399 /* prefer IPv6 */
1400 q.name = name;
1401 q.qclass = C_IN;
1402 q.answer = buf->buf;
1403 q.anslen = sizeof(buf->buf);
1404 int query_ipv6 = 1, query_ipv4 = 1;
1405 if (pai->ai_flags & AI_ADDRCONFIG) {
1406 query_ipv6 = _have_ipv6(netcontext->app_mark, netcontext->uid);
1407 query_ipv4 = _have_ipv4(netcontext->app_mark, netcontext->uid);
1408 }
1409 if (query_ipv6) {
1410 q.qtype = T_AAAA;
1411 if (query_ipv4) {
1412 q.next = &q2;
1413 q2.name = name;
1414 q2.qclass = C_IN;
1415 q2.qtype = T_A;
1416 q2.answer = buf2->buf;
1417 q2.anslen = sizeof(buf2->buf);
1418 }
1419 } else if (query_ipv4) {
1420 q.qtype = T_A;
1421 } else {
1422 free(buf);
1423 free(buf2);
Bernie Innocenti948f6572018-09-12 21:32:42 +09001424 return EAI_NODATA;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001425 }
1426 break;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001427 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001428 case AF_INET:
1429 q.name = name;
1430 q.qclass = C_IN;
1431 q.qtype = T_A;
1432 q.answer = buf->buf;
1433 q.anslen = sizeof(buf->buf);
1434 break;
1435 case AF_INET6:
1436 q.name = name;
1437 q.qclass = C_IN;
1438 q.qtype = T_AAAA;
1439 q.answer = buf->buf;
1440 q.anslen = sizeof(buf->buf);
1441 break;
1442 default:
1443 free(buf);
1444 free(buf2);
Bernie Innocenti948f6572018-09-12 21:32:42 +09001445 return EAI_FAMILY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001446 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001447
Bernie Innocenti4acba1a2018-09-26 11:52:04 +09001448 res = res_get_state();
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001449 if (res == NULL) {
1450 free(buf);
1451 free(buf2);
Bernie Innocenti948f6572018-09-12 21:32:42 +09001452 return EAI_MEMORY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001453 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001454
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001455 /* this just sets our netid val in the thread private data so we don't have to
1456 * modify the api's all the way down to res_send.c's res_nsend. We could
1457 * fully populate the thread private data here, but if we get down there
1458 * and have a cache hit that would be wasted, so we do the rest there on miss
1459 */
1460 res_setnetcontext(res, netcontext);
Mike Yu69615f62018-11-06 15:42:36 +08001461
Hungming Chend57ade02018-12-25 15:47:47 +08001462 int herrno = NETDB_INTERNAL;
Hungming Chen7f0d3292018-12-27 18:33:19 +08001463 if (res_searchN(name, &q, res, &herrno) < 0) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001464 free(buf);
1465 free(buf2);
Hungming Chen7f0d3292018-12-27 18:33:19 +08001466 // Pass herrno to catch more detailed errors rather than EAI_NODATA.
1467 return herrnoToAiErrno(herrno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001468 }
Hungming Chend57ade02018-12-25 15:47:47 +08001469 ai = getanswer(buf, q.n, q.name, q.qtype, pai, &herrno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001470 if (ai) {
1471 cur->ai_next = ai;
1472 while (cur && cur->ai_next) cur = cur->ai_next;
1473 }
1474 if (q.next) {
Hungming Chend57ade02018-12-25 15:47:47 +08001475 ai = getanswer(buf2, q2.n, q2.name, q2.qtype, pai, &herrno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001476 if (ai) cur->ai_next = ai;
1477 }
1478 free(buf);
1479 free(buf2);
1480 if (sentinel.ai_next == NULL) {
Hungming Chen7f0d3292018-12-27 18:33:19 +08001481 return herrnoToAiErrno(herrno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001482 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001483
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001484 _rfc6724_sort(&sentinel, netcontext->app_mark, netcontext->uid);
Bernie Innocenti55864192018-08-30 04:05:20 +09001485
Bernie Innocenti948f6572018-09-12 21:32:42 +09001486 *rv = sentinel.ai_next;
1487 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001488}
1489
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001490static void _sethtent(FILE** hostf) {
1491 if (!*hostf)
1492 *hostf = fopen(_PATH_HOSTS, "re");
1493 else
1494 rewind(*hostf);
Bernie Innocenti55864192018-08-30 04:05:20 +09001495}
1496
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001497static void _endhtent(FILE** hostf) {
1498 if (*hostf) {
1499 (void) fclose(*hostf);
1500 *hostf = NULL;
1501 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001502}
1503
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001504static struct addrinfo* _gethtent(FILE** hostf, const char* name, const struct addrinfo* pai) {
1505 char* p;
1506 char *cp, *tname, *cname;
Bernie Innocentic165ce82018-10-16 23:35:28 +09001507 struct addrinfo *res0, *res;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001508 int error;
1509 const char* addr;
1510 char hostbuf[8 * 1024];
Bernie Innocenti55864192018-08-30 04:05:20 +09001511
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001512 assert(name != NULL);
1513 assert(pai != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +09001514
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001515 if (!*hostf && !(*hostf = fopen(_PATH_HOSTS, "re"))) return (NULL);
1516again:
1517 if (!(p = fgets(hostbuf, sizeof hostbuf, *hostf))) return (NULL);
1518 if (*p == '#') goto again;
1519 if (!(cp = strpbrk(p, "#\n"))) goto again;
1520 *cp = '\0';
1521 if (!(cp = strpbrk(p, " \t"))) goto again;
1522 *cp++ = '\0';
1523 addr = p;
1524 /* if this is not something we're looking for, skip it. */
1525 cname = NULL;
1526 while (cp && *cp) {
1527 if (*cp == ' ' || *cp == '\t') {
1528 cp++;
1529 continue;
1530 }
1531 if (!cname) cname = cp;
1532 tname = cp;
1533 if ((cp = strpbrk(cp, " \t")) != NULL) *cp++ = '\0';
1534 // fprintf(stderr, "\ttname = '%s'", tname);
1535 if (strcasecmp(name, tname) == 0) goto found;
1536 }
1537 goto again;
Bernie Innocenti55864192018-08-30 04:05:20 +09001538
1539found:
Bernie Innocentic165ce82018-10-16 23:35:28 +09001540 error = getaddrinfo_numeric(addr, nullptr, *pai, &res0);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001541 if (error) goto again;
1542 for (res = res0; res; res = res->ai_next) {
1543 /* cover it up */
1544 res->ai_flags = pai->ai_flags;
Bernie Innocenti55864192018-08-30 04:05:20 +09001545
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001546 if (pai->ai_flags & AI_CANONNAME) {
1547 if (get_canonname(pai, res, cname) != 0) {
1548 freeaddrinfo(res0);
1549 goto again;
1550 }
1551 }
1552 }
1553 return res0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001554}
1555
Bernie Innocenti948f6572018-09-12 21:32:42 +09001556static bool files_getaddrinfo(const char* name, const addrinfo* pai, addrinfo** res) {
Ken Chen3270cf52018-11-07 01:20:48 +08001557 struct addrinfo sentinel = {};
1558 struct addrinfo *p, *cur;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001559 FILE* hostf = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001560
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001561 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +09001562
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001563 _sethtent(&hostf);
1564 while ((p = _gethtent(&hostf, name, pai)) != NULL) {
1565 cur->ai_next = p;
1566 while (cur && cur->ai_next) cur = cur->ai_next;
1567 }
1568 _endhtent(&hostf);
Bernie Innocenti55864192018-08-30 04:05:20 +09001569
Bernie Innocenti948f6572018-09-12 21:32:42 +09001570 *res = sentinel.ai_next;
1571 return sentinel.ai_next != NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001572}
1573
1574/* resolver logic */
1575
1576/*
1577 * Formulate a normal query, send, and await answer.
1578 * Returned answer is placed in supplied buffer "answer".
1579 * Perform preliminary check of answer, returning success only
1580 * if no error is indicated and the answer count is nonzero.
1581 * Return the size of the response on success, -1 on error.
Hungming Chend57ade02018-12-25 15:47:47 +08001582 * Error number is left in *herrno.
Bernie Innocenti55864192018-08-30 04:05:20 +09001583 *
1584 * Caller must parse answer and determine whether it answers the question.
1585 */
Hungming Chen7f0d3292018-12-27 18:33:19 +08001586static int res_queryN(const char* name, res_target* target, res_state res, int* herrno) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001587 u_char buf[MAXPACKET];
1588 HEADER* hp;
1589 int n;
1590 struct res_target* t;
1591 int rcode;
1592 int ancount;
Bernie Innocenti55864192018-08-30 04:05:20 +09001593
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001594 assert(name != NULL);
1595 /* XXX: target may be NULL??? */
Bernie Innocenti55864192018-08-30 04:05:20 +09001596
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001597 rcode = NOERROR;
1598 ancount = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001599
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001600 for (t = target; t; t = t->next) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001601 u_char* answer;
1602 int anslen;
Bernie Innocenti55864192018-08-30 04:05:20 +09001603
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001604 hp = (HEADER*) (void*) t->answer;
Ken Chenbfd32022019-01-02 14:59:38 +08001605 bool retried = false;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001606 again:
1607 hp->rcode = NOERROR; /* default */
Bernie Innocenti55864192018-08-30 04:05:20 +09001608
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001609 /* make it easier... */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001610 int cl = t->qclass;
1611 int type = t->qtype;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001612 answer = t->answer;
1613 anslen = t->anslen;
chenbruce16adee42019-02-20 19:45:50 +08001614
Bernie Innocentid017e972019-03-03 19:39:53 +09001615 LOG(DEBUG) << __func__ << "(" << name << ", " << cl << ", " << type << ")";
Bernie Innocenti55864192018-08-30 04:05:20 +09001616
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001617 n = res_nmkquery(res, QUERY, name, cl, type, NULL, 0, NULL, buf, sizeof(buf));
Ken Chenbfd32022019-01-02 14:59:38 +08001618 if (n > 0 && (res->options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) != 0 && !retried)
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001619 n = res_nopt(res, n, buf, sizeof(buf), anslen);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001620 if (n <= 0) {
Bernie Innocentid017e972019-03-03 19:39:53 +09001621 LOG(ERROR) << __func__ << ": res_nmkquery failed";
Hungming Chend57ade02018-12-25 15:47:47 +08001622 *herrno = NO_RECOVERY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001623 return n;
1624 }
Mike Yu69615f62018-11-06 15:42:36 +08001625
Luke Huang952d0942018-12-26 16:53:03 +08001626 n = res_nsend(res, buf, n, answer, anslen, &rcode, 0);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001627 if (n < 0 || hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
Hungming Chen7f0d3292018-12-27 18:33:19 +08001628 // Record rcode from DNS response header only if no timeout.
1629 // Keep rcode timeout for reporting later if any.
1630 if (rcode != RCODE_TIMEOUT) rcode = hp->rcode; /* record most recent error */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001631 /* if the query choked with EDNS0, retry without EDNS0 */
1632 if ((res->options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) != 0 &&
Ken Chenbfd32022019-01-02 14:59:38 +08001633 (res->_flags & RES_F_EDNS0ERR) && !retried) {
Bernie Innocentid017e972019-03-03 19:39:53 +09001634 LOG(DEBUG) << __func__ << ": retry without EDNS0";
Ken Chenbfd32022019-01-02 14:59:38 +08001635 retried = true;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001636 goto again;
1637 }
Bernie Innocentid017e972019-03-03 19:39:53 +09001638 LOG(DEBUG) << __func__ << ": rcode=" << hp->rcode << ", ancount=" << ntohs(hp->ancount);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001639 continue;
1640 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001641
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001642 ancount += ntohs(hp->ancount);
Bernie Innocenti55864192018-08-30 04:05:20 +09001643
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001644 t->n = n;
1645 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001646
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001647 if (ancount == 0) {
1648 switch (rcode) {
Hungming Chen7f0d3292018-12-27 18:33:19 +08001649 // Not defined in RFC.
1650 case RCODE_TIMEOUT:
1651 // DNS metrics monitors DNS query timeout.
1652 *herrno = NETD_RESOLV_H_ERRNO_EXT_TIMEOUT; // extended h_errno.
1653 break;
1654 // Defined in RFC 1035 section 4.1.1.
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001655 case NXDOMAIN:
Hungming Chend57ade02018-12-25 15:47:47 +08001656 *herrno = HOST_NOT_FOUND;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001657 break;
1658 case SERVFAIL:
Hungming Chend57ade02018-12-25 15:47:47 +08001659 *herrno = TRY_AGAIN;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001660 break;
1661 case NOERROR:
Hungming Chend57ade02018-12-25 15:47:47 +08001662 *herrno = NO_DATA;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001663 break;
1664 case FORMERR:
1665 case NOTIMP:
1666 case REFUSED:
1667 default:
Hungming Chend57ade02018-12-25 15:47:47 +08001668 *herrno = NO_RECOVERY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001669 break;
1670 }
1671 return -1;
1672 }
1673 return ancount;
Bernie Innocenti55864192018-08-30 04:05:20 +09001674}
1675
1676/*
1677 * Formulate a normal query, send, and retrieve answer in supplied buffer.
1678 * Return the size of the response on success, -1 on error.
1679 * If enabled, implement search rules until answer or unrecoverable failure
Hungming Chend57ade02018-12-25 15:47:47 +08001680 * is detected. Error code, if any, is left in *herrno.
Bernie Innocenti55864192018-08-30 04:05:20 +09001681 */
Hungming Chen7f0d3292018-12-27 18:33:19 +08001682static int res_searchN(const char* name, res_target* target, res_state res, int* herrno) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001683 const char *cp, *const *domain;
1684 HEADER* hp;
1685 u_int dots;
1686 int trailing_dot, ret, saved_herrno;
1687 int got_nodata = 0, got_servfail = 0, tried_as_is = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001688
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001689 assert(name != NULL);
1690 assert(target != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +09001691
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001692 hp = (HEADER*) (void*) target->answer; /*XXX*/
Bernie Innocenti55864192018-08-30 04:05:20 +09001693
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001694 errno = 0;
Hungming Chend57ade02018-12-25 15:47:47 +08001695 *herrno = HOST_NOT_FOUND; /* default, if we never query */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001696 dots = 0;
1697 for (cp = name; *cp; cp++) dots += (*cp == '.');
1698 trailing_dot = 0;
1699 if (cp > name && *--cp == '.') trailing_dot++;
Bernie Innocenti55864192018-08-30 04:05:20 +09001700
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001701 /*
1702 * If there are dots in the name already, let's just give it a try
1703 * 'as is'. The threshold can be set with the "ndots" option.
1704 */
1705 saved_herrno = -1;
1706 if (dots >= res->ndots) {
Hungming Chen7f0d3292018-12-27 18:33:19 +08001707 ret = res_querydomainN(name, NULL, target, res, herrno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001708 if (ret > 0) return (ret);
Hungming Chend57ade02018-12-25 15:47:47 +08001709 saved_herrno = *herrno;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001710 tried_as_is++;
1711 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001712
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001713 /*
1714 * We do at least one level of search if
1715 * - there is no dot and RES_DEFNAME is set, or
1716 * - there is at least one dot, there is no trailing dot,
1717 * and RES_DNSRCH is set.
1718 */
1719 if ((!dots && (res->options & RES_DEFNAMES)) ||
1720 (dots && !trailing_dot && (res->options & RES_DNSRCH))) {
1721 int done = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001722
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001723 /* Unfortunately we need to set stuff up before
1724 * the domain stuff is tried. Will have a better
1725 * fix after thread pools are used.
1726 */
1727 _resolv_populate_res_for_net(res);
Bernie Innocenti55864192018-08-30 04:05:20 +09001728
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001729 for (domain = (const char* const*) res->dnsrch; *domain && !done; domain++) {
Hungming Chen7f0d3292018-12-27 18:33:19 +08001730 ret = res_querydomainN(name, *domain, target, res, herrno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001731 if (ret > 0) return ret;
Bernie Innocenti55864192018-08-30 04:05:20 +09001732
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001733 /*
1734 * If no server present, give up.
1735 * If name isn't found in this domain,
1736 * keep trying higher domains in the search list
1737 * (if that's enabled).
1738 * On a NO_DATA error, keep trying, otherwise
1739 * a wildcard entry of another type could keep us
1740 * from finding this entry higher in the domain.
1741 * If we get some other error (negative answer or
1742 * server failure), then stop searching up,
1743 * but try the input name below in case it's
1744 * fully-qualified.
1745 */
1746 if (errno == ECONNREFUSED) {
Hungming Chend57ade02018-12-25 15:47:47 +08001747 *herrno = TRY_AGAIN;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001748 return -1;
1749 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001750
Hungming Chend57ade02018-12-25 15:47:47 +08001751 switch (*herrno) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001752 case NO_DATA:
1753 got_nodata++;
Bernie Innocenti8bb94ba2018-10-10 22:30:12 +09001754 [[fallthrough]];
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001755 case HOST_NOT_FOUND:
1756 /* keep trying */
1757 break;
1758 case TRY_AGAIN:
1759 if (hp->rcode == SERVFAIL) {
1760 /* try next search element, if any */
1761 got_servfail++;
1762 break;
1763 }
Bernie Innocenti8bb94ba2018-10-10 22:30:12 +09001764 [[fallthrough]];
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001765 default:
1766 /* anything else implies that we're done */
1767 done++;
1768 }
1769 /*
1770 * if we got here for some reason other than DNSRCH,
1771 * we only wanted one iteration of the loop, so stop.
1772 */
1773 if (!(res->options & RES_DNSRCH)) done++;
1774 }
1775 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001776
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001777 /*
1778 * if we have not already tried the name "as is", do that now.
1779 * note that we do this regardless of how many dots were in the
1780 * name or whether it ends with a dot.
1781 */
1782 if (!tried_as_is) {
Hungming Chen7f0d3292018-12-27 18:33:19 +08001783 ret = res_querydomainN(name, NULL, target, res, herrno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001784 if (ret > 0) return ret;
1785 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001786
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001787 /*
1788 * if we got here, we didn't satisfy the search.
1789 * if we did an initial full query, return that query's h_errno
1790 * (note that we wouldn't be here if that query had succeeded).
1791 * else if we ever got a nodata, send that back as the reason.
1792 * else send back meaningless h_errno, that being the one from
1793 * the last DNSRCH we did.
1794 */
1795 if (saved_herrno != -1)
Hungming Chend57ade02018-12-25 15:47:47 +08001796 *herrno = saved_herrno;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001797 else if (got_nodata)
Hungming Chend57ade02018-12-25 15:47:47 +08001798 *herrno = NO_DATA;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001799 else if (got_servfail)
Hungming Chend57ade02018-12-25 15:47:47 +08001800 *herrno = TRY_AGAIN;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001801 return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +09001802}
1803
1804/*
1805 * Perform a call on res_query on the concatenation of name and domain,
1806 * removing a trailing dot from name if domain is NULL.
1807 */
Mike Yu69615f62018-11-06 15:42:36 +08001808static int res_querydomainN(const char* name, const char* domain, res_target* target, res_state res,
Hungming Chen7f0d3292018-12-27 18:33:19 +08001809 int* herrno) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001810 char nbuf[MAXDNAME];
1811 const char* longname = nbuf;
1812 size_t n, d;
Bernie Innocenti55864192018-08-30 04:05:20 +09001813
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001814 assert(name != NULL);
Bernie Innocentid017e972019-03-03 19:39:53 +09001815
1816 LOG(DEBUG) << __func__ << "(\"" << name << "\", " << (domain ? domain : "<null>") << ")";
1817
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001818 if (domain == NULL) {
Bernie Innocentid017e972019-03-03 19:39:53 +09001819 // Check for trailing '.'; copy without '.' if present.
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001820 n = strlen(name);
1821 if (n + 1 > sizeof(nbuf)) {
Hungming Chend57ade02018-12-25 15:47:47 +08001822 *herrno = NO_RECOVERY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001823 return -1;
1824 }
1825 if (n > 0 && name[--n] == '.') {
1826 strncpy(nbuf, name, n);
1827 nbuf[n] = '\0';
1828 } else
1829 longname = name;
1830 } else {
1831 n = strlen(name);
1832 d = strlen(domain);
1833 if (n + 1 + d + 1 > sizeof(nbuf)) {
Hungming Chend57ade02018-12-25 15:47:47 +08001834 *herrno = NO_RECOVERY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001835 return -1;
1836 }
1837 snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain);
1838 }
Hungming Chen7f0d3292018-12-27 18:33:19 +08001839 return res_queryN(longname, target, res, herrno);
Bernie Innocenti55864192018-08-30 04:05:20 +09001840}