blob: 0364be41aef8cbf2e78ebf9aebe9e5c192c705ae [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
33/*
34 * Issues to be discussed:
35 * - Thread safe-ness must be checked.
36 * - Return values. There are nonstandard return values defined and used
37 * in the source code. This is because RFC2553 is silent about which error
38 * code must be returned for which situation.
39 * - IPv4 classful (shortened) form. RFC2553 is silent about it. XNET 5.2
40 * says to use inet_aton() to convert IPv4 numeric to binary (alows
41 * classful form as a result).
42 * current code - disallow classful form for IPv4 (due to use of inet_pton).
Bernie Innocenti55864192018-08-30 04:05:20 +090043 * Note:
44 * - We use getipnodebyname() just for thread-safeness. There's no intent
45 * to let it do PF_UNSPEC (actually we never pass PF_UNSPEC to
46 * getipnodebyname().
47 * - The code filters out AFs that are not supported by the kernel,
48 * when globbing NULL hostname (to loopback, or wildcard). Is it the right
49 * thing to do? What is the relationship with post-RFC2553 AI_ADDRCONFIG
50 * in ai_flags?
51 * - (post-2553) semantics of AI_ADDRCONFIG itself is too vague.
52 * (1) what should we do against numeric hostname (2) what should we do
53 * against NULL hostname (3) what is AI_ADDRCONFIG itself. AF not ready?
54 * non-loopback address configured? global address configured?
55 * - To avoid search order issue, we have a big amount of code duplicate
56 * from gethnamaddr.c and some other places. The issues that there's no
57 * lower layer function to lookup "IPv4 or IPv6" record. Calling
58 * gethostbyname2 from getaddrinfo will end up in wrong search order, as
59 * follows:
60 * - The code makes use of following calls when asked to resolver with
61 * ai_family = PF_UNSPEC:
62 * getipnodebyname(host, AF_INET6);
63 * getipnodebyname(host, AF_INET);
64 * This will result in the following queries if the node is configure to
65 * prefer /etc/hosts than DNS:
66 * lookup /etc/hosts for IPv6 address
67 * lookup DNS for IPv6 address
68 * lookup /etc/hosts for IPv4 address
69 * lookup DNS for IPv4 address
70 * which may not meet people's requirement.
71 * The right thing to happen is to have underlying layer which does
72 * PF_UNSPEC lookup (lookup both) and return chain of addrinfos.
73 * This would result in a bit of code duplicate with _dns_ghbyname() and
74 * friends.
75 */
76
Bernie Innocenti55864192018-08-30 04:05:20 +090077#include <arpa/inet.h>
78#include <arpa/nameser.h>
79#include <assert.h>
80#include <ctype.h>
81#include <errno.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090082#include <fcntl.h>
83#include <net/if.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090084#include <netdb.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090085#include <netinet/in.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090086#include <stdbool.h>
87#include <stddef.h>
88#include <stdio.h>
89#include <stdlib.h>
90#include <string.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090091#include <sys/param.h>
92#include <sys/socket.h>
93#include <sys/stat.h>
94#include <sys/types.h>
95#include <sys/un.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090096#include <syslog.h>
Bernie Innocenti189eb502018-10-01 23:10:18 +090097#include <unistd.h>
Bernie Innocentif89b3512018-08-30 07:34:37 +090098
Bernie Innocenti189eb502018-10-01 23:10:18 +090099#include "netd_resolv/resolv.h"
100#include "resolv_cache.h"
101#include "resolv_private.h"
Bernie Innocenti55864192018-08-30 04:05:20 +0900102
Bernie Innocenti55864192018-08-30 04:05:20 +0900103#define ANY 0
Bernie Innocenti55864192018-08-30 04:05:20 +0900104
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900105static const char in_addrany[] = {0, 0, 0, 0};
106static const char in_loopback[] = {127, 0, 0, 1};
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900107static const char in6_addrany[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
108static const 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 +0900109
Bernie Innocenti55864192018-08-30 04:05:20 +0900110static const struct afd {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900111 int a_af;
112 int a_addrlen;
113 int a_socklen;
114 int a_off;
115 const char* a_addrany;
116 const char* a_loopback;
117 int a_scoped;
118} afdl[] = {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900119 {PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
120 offsetof(struct sockaddr_in6, sin6_addr), in6_addrany, in6_loopback, 1},
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900121 {PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
122 offsetof(struct sockaddr_in, sin_addr), in_addrany, in_loopback, 0},
123 {0, 0, 0, 0, NULL, NULL, 0},
Bernie Innocenti55864192018-08-30 04:05:20 +0900124};
125
126struct explore {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900127 int e_af;
128 int e_socktype;
129 int e_protocol;
130 const char* e_protostr;
131 int e_wild;
132#define WILD_AF(ex) ((ex)->e_wild & 0x01)
133#define WILD_SOCKTYPE(ex) ((ex)->e_wild & 0x02)
134#define WILD_PROTOCOL(ex) ((ex)->e_wild & 0x04)
Bernie Innocenti55864192018-08-30 04:05:20 +0900135};
136
137static const struct explore explore[] = {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900138 {PF_INET6, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07},
139 {PF_INET6, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07},
140 {PF_INET6, SOCK_RAW, ANY, NULL, 0x05},
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900141 {PF_INET, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07},
142 {PF_INET, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07},
143 {PF_INET, SOCK_RAW, ANY, NULL, 0x05},
144 {PF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07},
145 {PF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07},
146 {PF_UNSPEC, SOCK_RAW, ANY, NULL, 0x05},
147 {-1, 0, 0, NULL, 0},
Bernie Innocenti55864192018-08-30 04:05:20 +0900148};
149
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900150#define PTON_MAX 16
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900151#define MAXPACKET (8 * 1024)
Bernie Innocenti55864192018-08-30 04:05:20 +0900152
153typedef union {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900154 HEADER hdr;
155 u_char buf[MAXPACKET];
Bernie Innocenti55864192018-08-30 04:05:20 +0900156} querybuf;
157
158struct res_target {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900159 struct res_target* next;
160 const char* name; /* domain name */
161 int qclass, qtype; /* class and type of query */
162 u_char* answer; /* buffer to put answer */
163 int anslen; /* size of answer buffer */
164 int n; /* result length */
Bernie Innocenti55864192018-08-30 04:05:20 +0900165};
166
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900167static int str2number(const char*);
168static int explore_fqdn(const struct addrinfo*, const char*, const char*, struct addrinfo**,
169 const struct android_net_context*);
170static int explore_null(const struct addrinfo*, const char*, struct addrinfo**);
171static int explore_numeric(const struct addrinfo*, const char*, const char*, struct addrinfo**,
172 const char*);
173static int explore_numeric_scope(const struct addrinfo*, const char*, const char*,
174 struct addrinfo**);
175static int get_canonname(const struct addrinfo*, struct addrinfo*, const char*);
176static struct addrinfo* get_ai(const struct addrinfo*, const struct afd*, const char*);
177static int get_portmatch(const struct addrinfo*, const char*);
178static int get_port(const struct addrinfo*, const char*, int);
179static const struct afd* find_afd(int);
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900180static int ip6_str2scopeid(const char*, struct sockaddr_in6*, u_int32_t*);
Bernie Innocenti55864192018-08-30 04:05:20 +0900181
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900182static struct addrinfo* getanswer(const querybuf*, int, const char*, int, const struct addrinfo*);
Bernie Innocenti948f6572018-09-12 21:32:42 +0900183static int dns_getaddrinfo(const char* name, const addrinfo* pai,
184 const android_net_context* netcontext, addrinfo** rv);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900185static void _sethtent(FILE**);
186static void _endhtent(FILE**);
187static struct addrinfo* _gethtent(FILE**, const char*, const struct addrinfo*);
Bernie Innocenti948f6572018-09-12 21:32:42 +0900188static bool files_getaddrinfo(const char* name, const addrinfo* pai, addrinfo** res);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900189static int _find_src_addr(const struct sockaddr*, struct sockaddr*, unsigned, uid_t);
Bernie Innocenti55864192018-08-30 04:05:20 +0900190
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900191static int res_queryN(const char*, struct res_target*, res_state);
192static int res_searchN(const char*, struct res_target*, res_state);
193static int res_querydomainN(const char*, const char*, struct res_target*, res_state);
Bernie Innocenti55864192018-08-30 04:05:20 +0900194
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900195static const char* const ai_errlist[] = {
196 "Success",
197 "Address family for hostname not supported", /* EAI_ADDRFAMILY */
198 "Temporary failure in name resolution", /* EAI_AGAIN */
199 "Invalid value for ai_flags", /* EAI_BADFLAGS */
200 "Non-recoverable failure in name resolution", /* EAI_FAIL */
201 "ai_family not supported", /* EAI_FAMILY */
202 "Memory allocation failure", /* EAI_MEMORY */
203 "No address associated with hostname", /* EAI_NODATA */
204 "hostname nor servname provided, or not known", /* EAI_NONAME */
205 "servname not supported for ai_socktype", /* EAI_SERVICE */
206 "ai_socktype not supported", /* EAI_SOCKTYPE */
207 "System error returned in errno", /* EAI_SYSTEM */
208 "Invalid value for hints", /* EAI_BADHINTS */
209 "Resolved protocol is unknown", /* EAI_PROTOCOL */
210 "Argument buffer overflow", /* EAI_OVERFLOW */
211 "Unknown error", /* EAI_MAX */
Bernie Innocenti55864192018-08-30 04:05:20 +0900212};
213
214/* XXX macros that make external reference is BAD. */
215
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900216#define GET_AI(ai, afd, addr) \
217 do { \
218 /* external reference: pai, error, and label free */ \
219 (ai) = get_ai(pai, (afd), (addr)); \
220 if ((ai) == NULL) { \
221 error = EAI_MEMORY; \
222 goto free; \
223 } \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900224 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900225
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900226#define GET_PORT(ai, serv) \
227 do { \
228 /* external reference: error and label free */ \
229 error = get_port((ai), (serv), 0); \
230 if (error != 0) goto free; \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900231 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900232
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900233#define GET_CANONNAME(ai, str) \
234 do { \
235 /* external reference: pai, error and label free */ \
236 error = get_canonname(pai, (ai), (str)); \
237 if (error != 0) goto free; \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900238 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900239
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900240#define ERR(err) \
241 do { \
242 /* external reference: error, and label bad */ \
243 error = (err); \
244 goto bad; \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900245 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900246
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900247#define MATCH_FAMILY(x, y, w) \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900248 ((x) == (y) || ((w) && ((x) == PF_UNSPEC || (y) == PF_UNSPEC)))
249#define MATCH(x, y, w) ((x) == (y) || ((w) && ((x) == ANY || (y) == ANY)))
Bernie Innocenti55864192018-08-30 04:05:20 +0900250
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900251const char* gai_strerror(int ecode) {
252 if (ecode < 0 || ecode > EAI_MAX) ecode = EAI_MAX;
253 return ai_errlist[ecode];
Bernie Innocenti55864192018-08-30 04:05:20 +0900254}
255
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900256void freeaddrinfo(struct addrinfo* ai) {
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900257 while (ai) {
258 struct addrinfo* next = ai->ai_next;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900259 if (ai->ai_canonname) free(ai->ai_canonname);
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900260 // Also frees ai->ai_addr which points to extra space beyond addrinfo
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900261 free(ai);
262 ai = next;
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900263 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900264}
265
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900266static int str2number(const char* p) {
267 char* ep;
268 unsigned long v;
Bernie Innocenti55864192018-08-30 04:05:20 +0900269
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900270 assert(p != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900271
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900272 if (*p == '\0') return -1;
273 ep = NULL;
274 errno = 0;
275 v = strtoul(p, &ep, 10);
276 if (errno == 0 && ep && *ep == '\0' && v <= UINT_MAX)
277 return v;
278 else
279 return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900280}
281
282/*
283 * The following functions determine whether IPv4 or IPv6 connectivity is
284 * available in order to implement AI_ADDRCONFIG.
285 *
286 * Strictly speaking, AI_ADDRCONFIG should not look at whether connectivity is
287 * available, but whether addresses of the specified family are "configured
288 * on the local system". However, bionic doesn't currently support getifaddrs,
289 * so checking for connectivity is the next best thing.
290 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900291static int _have_ipv6(unsigned mark, uid_t uid) {
292 static const struct sockaddr_in6 sin6_test = {
293 .sin6_family = AF_INET6,
294 .sin6_addr.s6_addr = {// 2000::
295 0x20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
nuccachene172a4e2018-10-23 17:10:58 +0800296 sockaddr_union addr = {.sin6 = sin6_test};
297 return _find_src_addr(&addr.sa, NULL, mark, uid) == 1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900298}
299
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900300static int _have_ipv4(unsigned mark, uid_t uid) {
301 static const struct sockaddr_in sin_test = {
302 .sin_family = AF_INET,
303 .sin_addr.s_addr = __constant_htonl(0x08080808L) // 8.8.8.8
304 };
nuccachene172a4e2018-10-23 17:10:58 +0800305 sockaddr_union addr = {.sin = sin_test};
306 return _find_src_addr(&addr.sa, NULL, mark, uid) == 1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900307}
308
309bool readBE32(FILE* fp, int32_t* result) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900310 int32_t tmp;
311 if (fread(&tmp, sizeof(tmp), 1, fp) != 1) {
312 return false;
313 }
314 *result = ntohl(tmp);
315 return true;
Bernie Innocenti55864192018-08-30 04:05:20 +0900316}
317
Bernie Innocentic165ce82018-10-16 23:35:28 +0900318// Internal version of getaddrinfo(), but limited to AI_NUMERICHOST.
319// NOTE: also called by resolv_set_nameservers_for_net().
320int getaddrinfo_numeric(const char* hostname, const char* servname, addrinfo hints,
321 addrinfo** result) {
322 hints.ai_flags = AI_NUMERICHOST;
323 const android_net_context netcontext = {
324 .app_netid = NETID_UNSET,
325 .app_mark = MARK_UNSET,
326 .dns_netid = NETID_UNSET,
327 .dns_mark = MARK_UNSET,
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900328 .uid = NET_CONTEXT_INVALID_UID,
329 };
Bernie Innocentic165ce82018-10-16 23:35:28 +0900330 return android_getaddrinfofornetcontext(hostname, servname, &hints, &netcontext, result);
Bernie Innocenti55864192018-08-30 04:05:20 +0900331}
332
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900333int android_getaddrinfofornetcontext(const char* hostname, const char* servname,
334 const struct addrinfo* hints,
335 const struct android_net_context* netcontext,
336 struct addrinfo** res) {
337 struct addrinfo sentinel;
338 struct addrinfo* cur;
339 int error = 0;
340 struct addrinfo ai;
341 struct addrinfo ai0;
342 struct addrinfo* pai;
343 const struct explore* ex;
Bernie Innocenti55864192018-08-30 04:05:20 +0900344
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900345 /* hostname is allowed to be NULL */
346 /* servname is allowed to be NULL */
347 /* hints is allowed to be NULL */
348 assert(res != NULL);
349 assert(netcontext != NULL);
350 memset(&sentinel, 0, sizeof(sentinel));
351 cur = &sentinel;
352 pai = &ai;
353 pai->ai_flags = 0;
354 pai->ai_family = PF_UNSPEC;
355 pai->ai_socktype = ANY;
356 pai->ai_protocol = ANY;
357 pai->ai_addrlen = 0;
358 pai->ai_canonname = NULL;
359 pai->ai_addr = NULL;
360 pai->ai_next = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900361
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900362 if (hostname == NULL && servname == NULL) return EAI_NONAME;
363 if (hints) {
364 /* error check for hints */
365 if (hints->ai_addrlen || hints->ai_canonname || hints->ai_addr || hints->ai_next)
366 ERR(EAI_BADHINTS); /* xxx */
367 if (hints->ai_flags & ~AI_MASK) ERR(EAI_BADFLAGS);
368 switch (hints->ai_family) {
369 case PF_UNSPEC:
370 case PF_INET:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900371 case PF_INET6:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900372 break;
373 default:
374 ERR(EAI_FAMILY);
375 }
376 memcpy(pai, hints, sizeof(*pai));
Bernie Innocenti55864192018-08-30 04:05:20 +0900377
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900378 /*
379 * if both socktype/protocol are specified, check if they
380 * are meaningful combination.
381 */
382 if (pai->ai_socktype != ANY && pai->ai_protocol != ANY) {
383 for (ex = explore; ex->e_af >= 0; ex++) {
384 if (pai->ai_family != ex->e_af) continue;
385 if (ex->e_socktype == ANY) continue;
386 if (ex->e_protocol == ANY) continue;
387 if (pai->ai_socktype == ex->e_socktype && pai->ai_protocol != ex->e_protocol) {
388 ERR(EAI_BADHINTS);
389 }
390 }
391 }
392 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900393
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900394 /*
395 * check for special cases. (1) numeric servname is disallowed if
396 * socktype/protocol are left unspecified. (2) servname is disallowed
397 * for raw and other inet{,6} sockets.
398 */
399 if (MATCH_FAMILY(pai->ai_family, PF_INET, 1)
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900400 || MATCH_FAMILY(pai->ai_family, PF_INET6, 1)
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900401 ) {
402 ai0 = *pai; /* backup *pai */
Bernie Innocenti55864192018-08-30 04:05:20 +0900403
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900404 if (pai->ai_family == PF_UNSPEC) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900405 pai->ai_family = PF_INET6;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900406 }
407 error = get_portmatch(pai, servname);
408 if (error) ERR(error);
Bernie Innocenti55864192018-08-30 04:05:20 +0900409
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900410 *pai = ai0;
411 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900412
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900413 ai0 = *pai;
Bernie Innocenti55864192018-08-30 04:05:20 +0900414
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900415 /* NULL hostname, or numeric hostname */
416 for (ex = explore; ex->e_af >= 0; ex++) {
417 *pai = ai0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900418
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900419 /* PF_UNSPEC entries are prepared for DNS queries only */
420 if (ex->e_af == PF_UNSPEC) continue;
Bernie Innocenti55864192018-08-30 04:05:20 +0900421
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900422 if (!MATCH_FAMILY(pai->ai_family, ex->e_af, WILD_AF(ex))) continue;
423 if (!MATCH(pai->ai_socktype, ex->e_socktype, WILD_SOCKTYPE(ex))) continue;
424 if (!MATCH(pai->ai_protocol, ex->e_protocol, WILD_PROTOCOL(ex))) continue;
Bernie Innocenti55864192018-08-30 04:05:20 +0900425
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900426 if (pai->ai_family == PF_UNSPEC) pai->ai_family = ex->e_af;
427 if (pai->ai_socktype == ANY && ex->e_socktype != ANY) pai->ai_socktype = ex->e_socktype;
428 if (pai->ai_protocol == ANY && ex->e_protocol != ANY) pai->ai_protocol = ex->e_protocol;
Bernie Innocenti55864192018-08-30 04:05:20 +0900429
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900430 if (hostname == NULL)
431 error = explore_null(pai, servname, &cur->ai_next);
432 else
433 error = explore_numeric_scope(pai, hostname, servname, &cur->ai_next);
Bernie Innocenti55864192018-08-30 04:05:20 +0900434
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900435 if (error) goto free;
Bernie Innocenti55864192018-08-30 04:05:20 +0900436
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900437 while (cur->ai_next) cur = cur->ai_next;
438 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900439
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900440 /*
441 * XXX
442 * If numeric representation of AF1 can be interpreted as FQDN
443 * representation of AF2, we need to think again about the code below.
444 */
445 if (sentinel.ai_next) goto good;
Bernie Innocenti55864192018-08-30 04:05:20 +0900446
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900447 if (hostname == NULL) ERR(EAI_NODATA);
448 if (pai->ai_flags & AI_NUMERICHOST) ERR(EAI_NONAME);
Bernie Innocenti55864192018-08-30 04:05:20 +0900449
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900450 /*
451 * hostname as alphabetical name.
452 * we would like to prefer AF_INET6 than AF_INET, so we'll make a
453 * outer loop by AFs.
454 */
455 for (ex = explore; ex->e_af >= 0; ex++) {
456 *pai = ai0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900457
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900458 /* require exact match for family field */
459 if (pai->ai_family != ex->e_af) continue;
Bernie Innocenti55864192018-08-30 04:05:20 +0900460
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900461 if (!MATCH(pai->ai_socktype, ex->e_socktype, WILD_SOCKTYPE(ex))) {
462 continue;
463 }
464 if (!MATCH(pai->ai_protocol, ex->e_protocol, WILD_PROTOCOL(ex))) {
465 continue;
466 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900467
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900468 if (pai->ai_socktype == ANY && ex->e_socktype != ANY) pai->ai_socktype = ex->e_socktype;
469 if (pai->ai_protocol == ANY && ex->e_protocol != ANY) pai->ai_protocol = ex->e_protocol;
Bernie Innocenti55864192018-08-30 04:05:20 +0900470
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900471 error = explore_fqdn(pai, hostname, servname, &cur->ai_next, netcontext);
Bernie Innocenti55864192018-08-30 04:05:20 +0900472
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900473 while (cur && cur->ai_next) cur = cur->ai_next;
474 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900475
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900476 /* XXX */
477 if (sentinel.ai_next) error = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900478
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900479 if (error) goto free;
480 if (error == 0) {
481 if (sentinel.ai_next) {
482 good:
483 *res = sentinel.ai_next;
Bernie Innocenti357339c2018-08-31 16:11:41 +0900484 return 0;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900485 } else
486 error = EAI_FAIL;
487 }
488free:
489bad:
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900490 freeaddrinfo(sentinel.ai_next);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900491 *res = NULL;
492 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900493}
494
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900495// FQDN hostname, DNS lookup
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900496static int explore_fqdn(const struct addrinfo* pai, const char* hostname, const char* servname,
497 struct addrinfo** res, const struct android_net_context* netcontext) {
498 struct addrinfo* result;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900499 int error = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900500
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900501 assert(pai != NULL);
502 /* hostname may be NULL */
503 /* servname may be NULL */
504 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900505
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900506 result = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900507
Bernie Innocenti948f6572018-09-12 21:32:42 +0900508 // If the servname does not match socktype/protocol, ignore it.
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900509 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900510
Bernie Innocenti948f6572018-09-12 21:32:42 +0900511 if (!files_getaddrinfo(hostname, pai, &result)) {
512 error = dns_getaddrinfo(hostname, pai, netcontext, &result);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900513 }
Bernie Innocenti948f6572018-09-12 21:32:42 +0900514 if (!error) {
515 struct addrinfo* cur;
516 for (cur = result; cur; cur = cur->ai_next) {
517 GET_PORT(cur, servname);
518 /* canonname should be filled already */
519 }
520 *res = result;
521 return 0;
522 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900523
524free:
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900525 freeaddrinfo(result);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900526 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900527}
528
529/*
530 * hostname == NULL.
531 * passive socket -> anyaddr (0.0.0.0 or ::)
532 * non-passive socket -> localhost (127.0.0.1 or ::1)
533 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900534static int explore_null(const struct addrinfo* pai, const char* servname, struct addrinfo** res) {
535 int s;
536 const struct afd* afd;
537 struct addrinfo* cur;
538 struct addrinfo sentinel;
539 int error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900540
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900541 assert(pai != NULL);
542 /* servname may be NULL */
543 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900544
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900545 *res = NULL;
546 sentinel.ai_next = NULL;
547 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +0900548
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900549 /*
550 * filter out AFs that are not supported by the kernel
551 * XXX errno?
552 */
553 s = socket(pai->ai_family, SOCK_DGRAM | SOCK_CLOEXEC, 0);
554 if (s < 0) {
555 if (errno != EMFILE) return 0;
556 } else
557 close(s);
Bernie Innocenti55864192018-08-30 04:05:20 +0900558
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900559 /*
560 * if the servname does not match socktype/protocol, ignore it.
561 */
562 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900563
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900564 afd = find_afd(pai->ai_family);
565 if (afd == NULL) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900566
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900567 if (pai->ai_flags & AI_PASSIVE) {
568 GET_AI(cur->ai_next, afd, afd->a_addrany);
569 /* xxx meaningless?
570 * GET_CANONNAME(cur->ai_next, "anyaddr");
571 */
572 GET_PORT(cur->ai_next, servname);
573 } else {
574 GET_AI(cur->ai_next, afd, afd->a_loopback);
575 /* xxx meaningless?
576 * GET_CANONNAME(cur->ai_next, "localhost");
577 */
578 GET_PORT(cur->ai_next, servname);
579 }
580 cur = cur->ai_next;
Bernie Innocenti55864192018-08-30 04:05:20 +0900581
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900582 *res = sentinel.ai_next;
583 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900584
585free:
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900586 freeaddrinfo(sentinel.ai_next);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900587 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900588}
589
590/*
591 * numeric hostname
592 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900593static int explore_numeric(const struct addrinfo* pai, const char* hostname, const char* servname,
594 struct addrinfo** res, const char* canonname) {
595 const struct afd* afd;
596 struct addrinfo* cur;
597 struct addrinfo sentinel;
598 int error;
599 char pton[PTON_MAX];
Bernie Innocenti55864192018-08-30 04:05:20 +0900600
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900601 assert(pai != NULL);
602 /* hostname may be NULL */
603 /* servname may be NULL */
604 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900605
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900606 *res = NULL;
607 sentinel.ai_next = NULL;
608 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +0900609
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900610 /*
611 * if the servname does not match socktype/protocol, ignore it.
612 */
613 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900614
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900615 afd = find_afd(pai->ai_family);
616 if (afd == NULL) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900617
Ken Chen15c805a2018-10-17 00:19:59 +0800618 if (inet_pton(afd->a_af, hostname, pton) == 1) {
619 if (pai->ai_family == afd->a_af || pai->ai_family == PF_UNSPEC /*?*/) {
620 GET_AI(cur->ai_next, afd, pton);
621 GET_PORT(cur->ai_next, servname);
622 if ((pai->ai_flags & AI_CANONNAME)) {
623 /*
624 * Set the numeric address itself as
625 * the canonical name, based on a
626 * clarification in rfc2553bis-03.
627 */
628 GET_CANONNAME(cur->ai_next, canonname);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900629 }
Ken Chen15c805a2018-10-17 00:19:59 +0800630 while (cur->ai_next) cur = cur->ai_next;
631 } else
632 ERR(EAI_FAMILY); /*xxx*/
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900633 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900634
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900635 *res = sentinel.ai_next;
636 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900637
638free:
639bad:
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900640 freeaddrinfo(sentinel.ai_next);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900641 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900642}
643
644/*
645 * numeric hostname with scope
646 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900647static int explore_numeric_scope(const struct addrinfo* pai, const char* hostname,
648 const char* servname, struct addrinfo** res) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900649 const struct afd* afd;
650 struct addrinfo* cur;
651 int error;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900652 const char *cp, *scope, *addr;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900653 struct sockaddr_in6* sin6;
Bernie Innocenti55864192018-08-30 04:05:20 +0900654
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900655 assert(pai != NULL);
656 /* hostname may be NULL */
657 /* servname may be NULL */
658 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900659
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900660 /*
661 * if the servname does not match socktype/protocol, ignore it.
662 */
663 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900664
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900665 afd = find_afd(pai->ai_family);
666 if (afd == NULL) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900667
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900668 if (!afd->a_scoped) return explore_numeric(pai, hostname, servname, res, hostname);
Bernie Innocenti55864192018-08-30 04:05:20 +0900669
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900670 cp = strchr(hostname, SCOPE_DELIMITER);
671 if (cp == NULL) return explore_numeric(pai, hostname, servname, res, hostname);
Bernie Innocenti55864192018-08-30 04:05:20 +0900672
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900673 /*
674 * Handle special case of <scoped_address><delimiter><scope id>
675 */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900676 char* hostname2 = strdup(hostname);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900677 if (hostname2 == NULL) return EAI_MEMORY;
678 /* terminate at the delimiter */
679 hostname2[cp - hostname] = '\0';
680 addr = hostname2;
681 scope = cp + 1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900682
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900683 error = explore_numeric(pai, addr, servname, res, hostname);
684 if (error == 0) {
685 u_int32_t scopeid;
Bernie Innocenti55864192018-08-30 04:05:20 +0900686
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900687 for (cur = *res; cur; cur = cur->ai_next) {
688 if (cur->ai_family != AF_INET6) continue;
689 sin6 = (struct sockaddr_in6*) (void*) cur->ai_addr;
690 if (ip6_str2scopeid(scope, sin6, &scopeid) == -1) {
691 free(hostname2);
692 return (EAI_NODATA); /* XXX: is return OK? */
693 }
694 sin6->sin6_scope_id = scopeid;
695 }
696 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900697
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900698 free(hostname2);
Bernie Innocenti55864192018-08-30 04:05:20 +0900699
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900700 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900701}
702
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900703static int get_canonname(const struct addrinfo* pai, struct addrinfo* ai, const char* str) {
704 assert(pai != NULL);
705 assert(ai != NULL);
706 assert(str != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900707
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900708 if ((pai->ai_flags & AI_CANONNAME) != 0) {
709 ai->ai_canonname = strdup(str);
710 if (ai->ai_canonname == NULL) return EAI_MEMORY;
711 }
712 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900713}
714
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900715static struct addrinfo* get_ai(const struct addrinfo* pai, const struct afd* afd,
716 const char* addr) {
717 char* p;
718 struct addrinfo* ai;
Bernie Innocenti55864192018-08-30 04:05:20 +0900719
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900720 assert(pai != NULL);
721 assert(afd != NULL);
722 assert(addr != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900723
nuccachene21023a2018-09-11 11:13:44 +0800724 ai = (struct addrinfo*) malloc(sizeof(struct addrinfo) + sizeof(sockaddr_union));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900725 if (ai == NULL) return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900726
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900727 memcpy(ai, pai, sizeof(struct addrinfo));
728 ai->ai_addr = (struct sockaddr*) (void*) (ai + 1);
nuccachene21023a2018-09-11 11:13:44 +0800729 memset(ai->ai_addr, 0, sizeof(sockaddr_union));
Bernie Innocenti55864192018-08-30 04:05:20 +0900730
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900731 ai->ai_addrlen = afd->a_socklen;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900732 ai->ai_addr->sa_family = ai->ai_family = afd->a_af;
733 p = (char*) (void*) (ai->ai_addr);
734 memcpy(p + afd->a_off, addr, (size_t) afd->a_addrlen);
735 return ai;
Bernie Innocenti55864192018-08-30 04:05:20 +0900736}
737
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900738static int get_portmatch(const struct addrinfo* ai, const char* servname) {
739 assert(ai != NULL);
740 /* servname may be NULL */
Bernie Innocenti55864192018-08-30 04:05:20 +0900741
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900742 return get_port(ai, servname, 1);
Bernie Innocenti55864192018-08-30 04:05:20 +0900743}
744
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900745static int get_port(const struct addrinfo* ai, const char* servname, int matchonly) {
746 const char* proto;
747 struct servent* sp;
748 int port;
749 int allownumeric;
Bernie Innocenti55864192018-08-30 04:05:20 +0900750
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900751 assert(ai != NULL);
752 /* servname may be NULL */
Bernie Innocenti55864192018-08-30 04:05:20 +0900753
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900754 if (servname == NULL) return 0;
755 switch (ai->ai_family) {
756 case AF_INET:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900757 case AF_INET6:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900758 break;
759 default:
760 return 0;
761 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900762
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900763 switch (ai->ai_socktype) {
764 case SOCK_RAW:
765 return EAI_SERVICE;
766 case SOCK_DGRAM:
767 case SOCK_STREAM:
768 allownumeric = 1;
769 break;
770 case ANY:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900771 allownumeric = 1;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900772 break;
773 default:
774 return EAI_SOCKTYPE;
775 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900776
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900777 port = str2number(servname);
778 if (port >= 0) {
779 if (!allownumeric) return EAI_SERVICE;
780 if (port < 0 || port > 65535) return EAI_SERVICE;
781 port = htons(port);
782 } else {
783 if (ai->ai_flags & AI_NUMERICSERV) return EAI_NONAME;
Bernie Innocenti55864192018-08-30 04:05:20 +0900784
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900785 switch (ai->ai_socktype) {
786 case SOCK_DGRAM:
787 proto = "udp";
788 break;
789 case SOCK_STREAM:
790 proto = "tcp";
791 break;
792 default:
793 proto = NULL;
794 break;
795 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900796
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900797 if ((sp = getservbyname(servname, proto)) == NULL) return EAI_SERVICE;
798 port = sp->s_port;
799 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900800
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900801 if (!matchonly) {
802 switch (ai->ai_family) {
803 case AF_INET:
804 ((struct sockaddr_in*) (void*) ai->ai_addr)->sin_port = port;
805 break;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900806 case AF_INET6:
807 ((struct sockaddr_in6*) (void*) ai->ai_addr)->sin6_port = port;
808 break;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900809 }
810 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900811
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900812 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900813}
814
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900815static const struct afd* find_afd(int af) {
816 const struct afd* afd;
Bernie Innocenti55864192018-08-30 04:05:20 +0900817
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900818 if (af == PF_UNSPEC) return NULL;
819 for (afd = afdl; afd->a_af; afd++) {
820 if (afd->a_af == af) return afd;
821 }
822 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900823}
824
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900825// Convert a string to a scope identifier.
826static int ip6_str2scopeid(const char* scope, struct sockaddr_in6* sin6, u_int32_t* scopeid) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900827 u_long lscopeid;
828 struct in6_addr* a6;
829 char* ep;
Bernie Innocenti55864192018-08-30 04:05:20 +0900830
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900831 assert(scope != NULL);
832 assert(sin6 != NULL);
833 assert(scopeid != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900834
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900835 a6 = &sin6->sin6_addr;
Bernie Innocenti55864192018-08-30 04:05:20 +0900836
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900837 /* empty scopeid portion is invalid */
838 if (*scope == '\0') return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900839
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900840 if (IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) {
841 /*
842 * We currently assume a one-to-one mapping between links
843 * and interfaces, so we simply use interface indices for
844 * like-local scopes.
845 */
846 *scopeid = if_nametoindex(scope);
847 if (*scopeid == 0) goto trynumeric;
848 return 0;
849 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900850
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900851 /* still unclear about literal, allow numeric only - placeholder */
852 if (IN6_IS_ADDR_SITELOCAL(a6) || IN6_IS_ADDR_MC_SITELOCAL(a6)) goto trynumeric;
853 if (IN6_IS_ADDR_MC_ORGLOCAL(a6))
854 goto trynumeric;
855 else
856 goto trynumeric; /* global */
Bernie Innocenti55864192018-08-30 04:05:20 +0900857
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900858 /* try to convert to a numeric id as a last resort */
859trynumeric:
860 errno = 0;
861 lscopeid = strtoul(scope, &ep, 10);
862 *scopeid = (u_int32_t)(lscopeid & 0xffffffffUL);
863 if (errno == 0 && ep && *ep == '\0' && *scopeid == lscopeid)
864 return 0;
865 else
866 return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900867}
Bernie Innocenti55864192018-08-30 04:05:20 +0900868
869/* code duplicate with gethnamaddr.c */
870
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900871static const char AskedForGot[] = "gethostby*.getanswer: asked for \"%s\", got \"%s\"";
Bernie Innocenti55864192018-08-30 04:05:20 +0900872
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900873#define BOUNDED_INCR(x) \
874 do { \
875 BOUNDS_CHECK(cp, x); \
876 cp += (x); \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900877 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900878
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900879#define BOUNDS_CHECK(ptr, count) \
880 do { \
881 if (eom - (ptr) < (count)) { \
882 h_errno = NO_RECOVERY; \
883 return NULL; \
884 } \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900885 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900886
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900887static struct addrinfo* getanswer(const querybuf* answer, int anslen, const char* qname, int qtype,
888 const struct addrinfo* pai) {
889 struct addrinfo sentinel, *cur;
890 struct addrinfo ai;
891 const struct afd* afd;
892 char* canonname;
893 const HEADER* hp;
894 const u_char* cp;
895 int n;
896 const u_char* eom;
897 char *bp, *ep;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900898 int type, ancount, qdcount;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900899 int haveanswer, had_error;
900 char tbuf[MAXDNAME];
901 int (*name_ok)(const char*);
902 char hostbuf[8 * 1024];
Bernie Innocenti55864192018-08-30 04:05:20 +0900903
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900904 assert(answer != NULL);
905 assert(qname != NULL);
906 assert(pai != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900907
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900908 memset(&sentinel, 0, sizeof(sentinel));
909 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +0900910
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900911 canonname = NULL;
912 eom = answer->buf + anslen;
913 switch (qtype) {
914 case T_A:
915 case T_AAAA:
916 case T_ANY: /*use T_ANY only for T_A/T_AAAA lookup*/
917 name_ok = res_hnok;
918 break;
919 default:
920 return NULL; /* XXX should be abort(); */
921 }
922 /*
923 * find first satisfactory answer
924 */
925 hp = &answer->hdr;
926 ancount = ntohs(hp->ancount);
927 qdcount = ntohs(hp->qdcount);
928 bp = hostbuf;
929 ep = hostbuf + sizeof hostbuf;
930 cp = answer->buf;
931 BOUNDED_INCR(HFIXEDSZ);
932 if (qdcount != 1) {
933 h_errno = NO_RECOVERY;
934 return (NULL);
935 }
936 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
937 if ((n < 0) || !(*name_ok)(bp)) {
938 h_errno = NO_RECOVERY;
939 return (NULL);
940 }
941 BOUNDED_INCR(n + QFIXEDSZ);
942 if (qtype == T_A || qtype == T_AAAA || qtype == T_ANY) {
943 /* res_send() has already verified that the query name is the
944 * same as the one we sent; this just gets the expanded name
945 * (i.e., with the succeeding search-domain tacked on).
946 */
947 n = strlen(bp) + 1; /* for the \0 */
948 if (n >= MAXHOSTNAMELEN) {
949 h_errno = NO_RECOVERY;
950 return (NULL);
951 }
952 canonname = bp;
953 bp += n;
954 /* The qname can be abbreviated, but h_name is now absolute. */
955 qname = canonname;
956 }
957 haveanswer = 0;
958 had_error = 0;
959 while (ancount-- > 0 && cp < eom && !had_error) {
960 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
961 if ((n < 0) || !(*name_ok)(bp)) {
962 had_error++;
963 continue;
964 }
965 cp += n; /* name */
966 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
Bernie Innocentiee1b85b2018-09-25 14:23:19 +0900967 type = ns_get16(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900968 cp += INT16SZ; /* type */
Bernie Innocentiee1b85b2018-09-25 14:23:19 +0900969 int cl = ns_get16(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900970 cp += INT16SZ + INT32SZ; /* class, TTL */
Bernie Innocentiee1b85b2018-09-25 14:23:19 +0900971 n = ns_get16(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900972 cp += INT16SZ; /* len */
973 BOUNDS_CHECK(cp, n);
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900974 if (cl != C_IN) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900975 /* XXX - debug? syslog? */
976 cp += n;
977 continue; /* XXX - had_error++ ? */
978 }
979 if ((qtype == T_A || qtype == T_AAAA || qtype == T_ANY) && type == T_CNAME) {
980 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
981 if ((n < 0) || !(*name_ok)(tbuf)) {
982 had_error++;
983 continue;
984 }
985 cp += n;
986 /* Get canonical name. */
987 n = strlen(tbuf) + 1; /* for the \0 */
988 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
989 had_error++;
990 continue;
991 }
992 strlcpy(bp, tbuf, (size_t)(ep - bp));
993 canonname = bp;
994 bp += n;
995 continue;
996 }
997 if (qtype == T_ANY) {
998 if (!(type == T_A || type == T_AAAA)) {
999 cp += n;
1000 continue;
1001 }
1002 } else if (type != qtype) {
1003 if (type != T_KEY && type != T_SIG)
1004 syslog(LOG_NOTICE | LOG_AUTH,
1005 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"", qname,
1006 p_class(C_IN), p_type(qtype), p_type(type));
1007 cp += n;
1008 continue; /* XXX - had_error++ ? */
1009 }
1010 switch (type) {
1011 case T_A:
1012 case T_AAAA:
1013 if (strcasecmp(canonname, bp) != 0) {
1014 syslog(LOG_NOTICE | LOG_AUTH, AskedForGot, canonname, bp);
1015 cp += n;
1016 continue; /* XXX - had_error++ ? */
1017 }
1018 if (type == T_A && n != INADDRSZ) {
1019 cp += n;
1020 continue;
1021 }
1022 if (type == T_AAAA && n != IN6ADDRSZ) {
1023 cp += n;
1024 continue;
1025 }
1026 if (type == T_AAAA) {
1027 struct in6_addr in6;
1028 memcpy(&in6, cp, IN6ADDRSZ);
1029 if (IN6_IS_ADDR_V4MAPPED(&in6)) {
1030 cp += n;
1031 continue;
1032 }
1033 }
1034 if (!haveanswer) {
1035 int nn;
Bernie Innocenti55864192018-08-30 04:05:20 +09001036
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001037 canonname = bp;
1038 nn = strlen(bp) + 1; /* for the \0 */
1039 bp += nn;
1040 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001041
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001042 /* don't overwrite pai */
1043 ai = *pai;
1044 ai.ai_family = (type == T_A) ? AF_INET : AF_INET6;
1045 afd = find_afd(ai.ai_family);
1046 if (afd == NULL) {
1047 cp += n;
1048 continue;
1049 }
1050 cur->ai_next = get_ai(&ai, afd, (const char*) cp);
1051 if (cur->ai_next == NULL) had_error++;
1052 while (cur && cur->ai_next) cur = cur->ai_next;
1053 cp += n;
1054 break;
1055 default:
1056 abort();
1057 }
1058 if (!had_error) haveanswer++;
1059 }
1060 if (haveanswer) {
1061 if (!canonname)
1062 (void) get_canonname(pai, sentinel.ai_next, qname);
1063 else
1064 (void) get_canonname(pai, sentinel.ai_next, canonname);
1065 h_errno = NETDB_SUCCESS;
1066 return sentinel.ai_next;
1067 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001068
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001069 h_errno = NO_RECOVERY;
1070 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001071}
1072
1073struct addrinfo_sort_elem {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001074 struct addrinfo* ai;
1075 int has_src_addr;
1076 sockaddr_union src_addr;
1077 int original_order;
Bernie Innocenti55864192018-08-30 04:05:20 +09001078};
1079
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001080static int _get_scope(const struct sockaddr* addr) {
1081 if (addr->sa_family == AF_INET6) {
1082 const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
1083 if (IN6_IS_ADDR_MULTICAST(&addr6->sin6_addr)) {
1084 return IPV6_ADDR_MC_SCOPE(&addr6->sin6_addr);
1085 } else if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr) ||
1086 IN6_IS_ADDR_LINKLOCAL(&addr6->sin6_addr)) {
1087 /*
1088 * RFC 4291 section 2.5.3 says loopback is to be treated as having
1089 * link-local scope.
1090 */
1091 return IPV6_ADDR_SCOPE_LINKLOCAL;
1092 } else if (IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr)) {
1093 return IPV6_ADDR_SCOPE_SITELOCAL;
1094 } else {
1095 return IPV6_ADDR_SCOPE_GLOBAL;
1096 }
1097 } else if (addr->sa_family == AF_INET) {
1098 const struct sockaddr_in* addr4 = (const struct sockaddr_in*) addr;
1099 unsigned long int na = ntohl(addr4->sin_addr.s_addr);
Bernie Innocenti55864192018-08-30 04:05:20 +09001100
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001101 if (IN_LOOPBACK(na) || /* 127.0.0.0/8 */
1102 (na & 0xffff0000) == 0xa9fe0000) { /* 169.254.0.0/16 */
1103 return IPV6_ADDR_SCOPE_LINKLOCAL;
1104 } else {
1105 /*
1106 * RFC 6724 section 3.2. Other IPv4 addresses, including private addresses
1107 * and shared addresses (100.64.0.0/10), are assigned global scope.
1108 */
1109 return IPV6_ADDR_SCOPE_GLOBAL;
1110 }
1111 } else {
1112 /*
1113 * This should never happen.
1114 * Return a scope with low priority as a last resort.
1115 */
1116 return IPV6_ADDR_SCOPE_NODELOCAL;
1117 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001118}
1119
1120/* These macros are modelled after the ones in <netinet/in6.h>. */
1121
1122/* RFC 4380, section 2.6 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001123#define IN6_IS_ADDR_TEREDO(a) \
1124 ((*(const uint32_t*) (const void*) (&(a)->s6_addr[0]) == ntohl(0x20010000)))
Bernie Innocenti55864192018-08-30 04:05:20 +09001125
1126/* RFC 3056, section 2. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001127#define IN6_IS_ADDR_6TO4(a) (((a)->s6_addr[0] == 0x20) && ((a)->s6_addr[1] == 0x02))
Bernie Innocenti55864192018-08-30 04:05:20 +09001128
1129/* 6bone testing address area (3ffe::/16), deprecated in RFC 3701. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001130#define IN6_IS_ADDR_6BONE(a) (((a)->s6_addr[0] == 0x3f) && ((a)->s6_addr[1] == 0xfe))
Bernie Innocenti55864192018-08-30 04:05:20 +09001131
1132/*
1133 * Get the label for a given IPv4/IPv6 address.
1134 * RFC 6724, section 2.1.
1135 */
1136
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001137static int _get_label(const struct sockaddr* addr) {
1138 if (addr->sa_family == AF_INET) {
1139 return 4;
1140 } else if (addr->sa_family == AF_INET6) {
1141 const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
1142 if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr)) {
1143 return 0;
1144 } else if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr)) {
1145 return 4;
1146 } else if (IN6_IS_ADDR_6TO4(&addr6->sin6_addr)) {
1147 return 2;
1148 } else if (IN6_IS_ADDR_TEREDO(&addr6->sin6_addr)) {
1149 return 5;
1150 } else if (IN6_IS_ADDR_ULA(&addr6->sin6_addr)) {
1151 return 13;
1152 } else if (IN6_IS_ADDR_V4COMPAT(&addr6->sin6_addr)) {
1153 return 3;
1154 } else if (IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr)) {
1155 return 11;
1156 } else if (IN6_IS_ADDR_6BONE(&addr6->sin6_addr)) {
1157 return 12;
1158 } else {
1159 /* All other IPv6 addresses, including global unicast addresses. */
1160 return 1;
1161 }
1162 } else {
1163 /*
1164 * This should never happen.
1165 * Return a semi-random label as a last resort.
1166 */
1167 return 1;
1168 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001169}
1170
1171/*
1172 * Get the precedence for a given IPv4/IPv6 address.
1173 * RFC 6724, section 2.1.
1174 */
1175
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001176static int _get_precedence(const struct sockaddr* addr) {
1177 if (addr->sa_family == AF_INET) {
1178 return 35;
1179 } else if (addr->sa_family == AF_INET6) {
1180 const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
1181 if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr)) {
1182 return 50;
1183 } else if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr)) {
1184 return 35;
1185 } else if (IN6_IS_ADDR_6TO4(&addr6->sin6_addr)) {
1186 return 30;
1187 } else if (IN6_IS_ADDR_TEREDO(&addr6->sin6_addr)) {
1188 return 5;
1189 } else if (IN6_IS_ADDR_ULA(&addr6->sin6_addr)) {
1190 return 3;
1191 } else if (IN6_IS_ADDR_V4COMPAT(&addr6->sin6_addr) ||
1192 IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr) ||
1193 IN6_IS_ADDR_6BONE(&addr6->sin6_addr)) {
1194 return 1;
1195 } else {
1196 /* All other IPv6 addresses, including global unicast addresses. */
1197 return 40;
1198 }
1199 } else {
1200 return 1;
1201 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001202}
1203
1204/*
1205 * Find number of matching initial bits between the two addresses a1 and a2.
1206 */
1207
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001208static int _common_prefix_len(const struct in6_addr* a1, const struct in6_addr* a2) {
1209 const char* p1 = (const char*) a1;
1210 const char* p2 = (const char*) a2;
1211 unsigned i;
Bernie Innocenti55864192018-08-30 04:05:20 +09001212
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001213 for (i = 0; i < sizeof(*a1); ++i) {
1214 int x, j;
Bernie Innocenti55864192018-08-30 04:05:20 +09001215
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001216 if (p1[i] == p2[i]) {
1217 continue;
1218 }
1219 x = p1[i] ^ p2[i];
1220 for (j = 0; j < CHAR_BIT; ++j) {
1221 if (x & (1 << (CHAR_BIT - 1))) {
1222 return i * CHAR_BIT + j;
1223 }
1224 x <<= 1;
1225 }
1226 }
1227 return sizeof(*a1) * CHAR_BIT;
Bernie Innocenti55864192018-08-30 04:05:20 +09001228}
1229
1230/*
1231 * Compare two source/destination address pairs.
1232 * RFC 6724, section 6.
1233 */
1234
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001235static int _rfc6724_compare(const void* ptr1, const void* ptr2) {
1236 const struct addrinfo_sort_elem* a1 = (const struct addrinfo_sort_elem*) ptr1;
1237 const struct addrinfo_sort_elem* a2 = (const struct addrinfo_sort_elem*) ptr2;
1238 int scope_src1, scope_dst1, scope_match1;
1239 int scope_src2, scope_dst2, scope_match2;
1240 int label_src1, label_dst1, label_match1;
1241 int label_src2, label_dst2, label_match2;
1242 int precedence1, precedence2;
1243 int prefixlen1, prefixlen2;
Bernie Innocenti55864192018-08-30 04:05:20 +09001244
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001245 /* Rule 1: Avoid unusable destinations. */
1246 if (a1->has_src_addr != a2->has_src_addr) {
1247 return a2->has_src_addr - a1->has_src_addr;
1248 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001249
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001250 /* Rule 2: Prefer matching scope. */
nuccachene172a4e2018-10-23 17:10:58 +08001251 scope_src1 = _get_scope(&a1->src_addr.sa);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001252 scope_dst1 = _get_scope(a1->ai->ai_addr);
1253 scope_match1 = (scope_src1 == scope_dst1);
Bernie Innocenti55864192018-08-30 04:05:20 +09001254
nuccachene172a4e2018-10-23 17:10:58 +08001255 scope_src2 = _get_scope(&a2->src_addr.sa);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001256 scope_dst2 = _get_scope(a2->ai->ai_addr);
1257 scope_match2 = (scope_src2 == scope_dst2);
Bernie Innocenti55864192018-08-30 04:05:20 +09001258
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001259 if (scope_match1 != scope_match2) {
1260 return scope_match2 - scope_match1;
1261 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001262
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001263 /*
1264 * Rule 3: Avoid deprecated addresses.
1265 * TODO(sesse): We don't currently have a good way of finding this.
1266 */
Bernie Innocenti55864192018-08-30 04:05:20 +09001267
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001268 /*
1269 * Rule 4: Prefer home addresses.
1270 * TODO(sesse): We don't currently have a good way of finding this.
1271 */
Bernie Innocenti55864192018-08-30 04:05:20 +09001272
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001273 /* Rule 5: Prefer matching label. */
nuccachene172a4e2018-10-23 17:10:58 +08001274 label_src1 = _get_label(&a1->src_addr.sa);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001275 label_dst1 = _get_label(a1->ai->ai_addr);
1276 label_match1 = (label_src1 == label_dst1);
Bernie Innocenti55864192018-08-30 04:05:20 +09001277
nuccachene172a4e2018-10-23 17:10:58 +08001278 label_src2 = _get_label(&a2->src_addr.sa);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001279 label_dst2 = _get_label(a2->ai->ai_addr);
1280 label_match2 = (label_src2 == label_dst2);
Bernie Innocenti55864192018-08-30 04:05:20 +09001281
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001282 if (label_match1 != label_match2) {
1283 return label_match2 - label_match1;
1284 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001285
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001286 /* Rule 6: Prefer higher precedence. */
1287 precedence1 = _get_precedence(a1->ai->ai_addr);
1288 precedence2 = _get_precedence(a2->ai->ai_addr);
1289 if (precedence1 != precedence2) {
1290 return precedence2 - precedence1;
1291 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001292
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001293 /*
1294 * Rule 7: Prefer native transport.
1295 * TODO(sesse): We don't currently have a good way of finding this.
1296 */
Bernie Innocenti55864192018-08-30 04:05:20 +09001297
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001298 /* Rule 8: Prefer smaller scope. */
1299 if (scope_dst1 != scope_dst2) {
1300 return scope_dst1 - scope_dst2;
1301 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001302
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001303 /*
1304 * Rule 9: Use longest matching prefix.
1305 * We implement this for IPv6 only, as the rules in RFC 6724 don't seem
1306 * to work very well directly applied to IPv4. (glibc uses information from
1307 * the routing table for a custom IPv4 implementation here.)
1308 */
1309 if (a1->has_src_addr && a1->ai->ai_addr->sa_family == AF_INET6 && a2->has_src_addr &&
1310 a2->ai->ai_addr->sa_family == AF_INET6) {
nuccachene172a4e2018-10-23 17:10:58 +08001311 const struct sockaddr_in6* a1_src = &a1->src_addr.sin6;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001312 const struct sockaddr_in6* a1_dst = (const struct sockaddr_in6*) a1->ai->ai_addr;
nuccachene172a4e2018-10-23 17:10:58 +08001313 const struct sockaddr_in6* a2_src = &a2->src_addr.sin6;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001314 const struct sockaddr_in6* a2_dst = (const struct sockaddr_in6*) a2->ai->ai_addr;
1315 prefixlen1 = _common_prefix_len(&a1_src->sin6_addr, &a1_dst->sin6_addr);
1316 prefixlen2 = _common_prefix_len(&a2_src->sin6_addr, &a2_dst->sin6_addr);
1317 if (prefixlen1 != prefixlen2) {
1318 return prefixlen2 - prefixlen1;
1319 }
1320 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001321
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001322 /*
1323 * Rule 10: Leave the order unchanged.
1324 * We need this since qsort() is not necessarily stable.
1325 */
1326 return a1->original_order - a2->original_order;
Bernie Innocenti55864192018-08-30 04:05:20 +09001327}
1328
1329/*
1330 * Find the source address that will be used if trying to connect to the given
1331 * address. src_addr must be large enough to hold a struct sockaddr_in6.
1332 *
1333 * Returns 1 if a source address was found, 0 if the address is unreachable,
1334 * and -1 if a fatal error occurred. If 0 or -1, the contents of src_addr are
1335 * undefined.
1336 */
1337
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001338static int _find_src_addr(const struct sockaddr* addr, struct sockaddr* src_addr, unsigned mark,
1339 uid_t uid) {
1340 int sock;
1341 int ret;
1342 socklen_t len;
Bernie Innocenti55864192018-08-30 04:05:20 +09001343
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001344 switch (addr->sa_family) {
1345 case AF_INET:
1346 len = sizeof(struct sockaddr_in);
1347 break;
1348 case AF_INET6:
1349 len = sizeof(struct sockaddr_in6);
1350 break;
1351 default:
1352 /* No known usable source address for non-INET families. */
1353 return 0;
1354 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001355
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001356 sock = socket(addr->sa_family, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP);
1357 if (sock == -1) {
1358 if (errno == EAFNOSUPPORT) {
1359 return 0;
1360 } else {
1361 return -1;
1362 }
1363 }
1364 if (mark != MARK_UNSET && setsockopt(sock, SOL_SOCKET, SO_MARK, &mark, sizeof(mark)) < 0) {
1365 close(sock);
1366 return 0;
1367 }
1368 if (uid > 0 && uid != NET_CONTEXT_INVALID_UID && fchown(sock, uid, (gid_t) -1) < 0) {
1369 close(sock);
1370 return 0;
1371 }
1372 do {
Bernie Innocentif89b3512018-08-30 07:34:37 +09001373 ret = connect(sock, addr, len);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001374 } while (ret == -1 && errno == EINTR);
Bernie Innocenti55864192018-08-30 04:05:20 +09001375
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001376 if (ret == -1) {
1377 close(sock);
1378 return 0;
1379 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001380
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001381 if (src_addr && getsockname(sock, src_addr, &len) == -1) {
1382 close(sock);
1383 return -1;
1384 }
1385 close(sock);
1386 return 1;
Bernie Innocenti55864192018-08-30 04:05:20 +09001387}
1388
1389/*
1390 * Sort the linked list starting at sentinel->ai_next in RFC6724 order.
1391 * Will leave the list unchanged if an error occurs.
1392 */
1393
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001394static void _rfc6724_sort(struct addrinfo* list_sentinel, unsigned mark, uid_t uid) {
1395 struct addrinfo* cur;
1396 int nelem = 0, i;
1397 struct addrinfo_sort_elem* elems;
Bernie Innocenti55864192018-08-30 04:05:20 +09001398
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001399 cur = list_sentinel->ai_next;
1400 while (cur) {
1401 ++nelem;
1402 cur = cur->ai_next;
1403 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001404
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001405 elems = (struct addrinfo_sort_elem*) malloc(nelem * sizeof(struct addrinfo_sort_elem));
1406 if (elems == NULL) {
1407 goto error;
1408 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001409
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001410 /*
1411 * Convert the linked list to an array that also contains the candidate
1412 * source address for each destination address.
1413 */
1414 for (i = 0, cur = list_sentinel->ai_next; i < nelem; ++i, cur = cur->ai_next) {
1415 int has_src_addr;
1416 assert(cur != NULL);
1417 elems[i].ai = cur;
1418 elems[i].original_order = i;
Bernie Innocenti55864192018-08-30 04:05:20 +09001419
nuccachene172a4e2018-10-23 17:10:58 +08001420 has_src_addr = _find_src_addr(cur->ai_addr, &elems[i].src_addr.sa, mark, uid);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001421 if (has_src_addr == -1) {
1422 goto error;
1423 }
1424 elems[i].has_src_addr = has_src_addr;
1425 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001426
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001427 /* Sort the addresses, and rearrange the linked list so it matches the sorted order. */
1428 qsort((void*) elems, nelem, sizeof(struct addrinfo_sort_elem), _rfc6724_compare);
Bernie Innocenti55864192018-08-30 04:05:20 +09001429
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001430 list_sentinel->ai_next = elems[0].ai;
1431 for (i = 0; i < nelem - 1; ++i) {
1432 elems[i].ai->ai_next = elems[i + 1].ai;
1433 }
1434 elems[nelem - 1].ai->ai_next = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001435
1436error:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001437 free(elems);
Bernie Innocenti55864192018-08-30 04:05:20 +09001438}
1439
Bernie Innocenti948f6572018-09-12 21:32:42 +09001440static int dns_getaddrinfo(const char* name, const addrinfo* pai,
1441 const android_net_context* netcontext, addrinfo** rv) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001442 struct addrinfo* ai;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001443 struct addrinfo sentinel, *cur;
1444 struct res_target q, q2;
1445 res_state res;
Bernie Innocenti55864192018-08-30 04:05:20 +09001446
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001447 memset(&q, 0, sizeof(q));
1448 memset(&q2, 0, sizeof(q2));
1449 memset(&sentinel, 0, sizeof(sentinel));
1450 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +09001451
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001452 querybuf* buf = (querybuf*) malloc(sizeof(*buf));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001453 if (buf == NULL) {
1454 h_errno = NETDB_INTERNAL;
Bernie Innocenti948f6572018-09-12 21:32:42 +09001455 return EAI_MEMORY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001456 }
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001457 querybuf* buf2 = (querybuf*) malloc(sizeof(*buf2));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001458 if (buf2 == NULL) {
1459 free(buf);
1460 h_errno = NETDB_INTERNAL;
Bernie Innocenti948f6572018-09-12 21:32:42 +09001461 return EAI_MEMORY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001462 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001463
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001464 switch (pai->ai_family) {
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001465 case AF_UNSPEC: {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001466 /* prefer IPv6 */
1467 q.name = name;
1468 q.qclass = C_IN;
1469 q.answer = buf->buf;
1470 q.anslen = sizeof(buf->buf);
1471 int query_ipv6 = 1, query_ipv4 = 1;
1472 if (pai->ai_flags & AI_ADDRCONFIG) {
1473 query_ipv6 = _have_ipv6(netcontext->app_mark, netcontext->uid);
1474 query_ipv4 = _have_ipv4(netcontext->app_mark, netcontext->uid);
1475 }
1476 if (query_ipv6) {
1477 q.qtype = T_AAAA;
1478 if (query_ipv4) {
1479 q.next = &q2;
1480 q2.name = name;
1481 q2.qclass = C_IN;
1482 q2.qtype = T_A;
1483 q2.answer = buf2->buf;
1484 q2.anslen = sizeof(buf2->buf);
1485 }
1486 } else if (query_ipv4) {
1487 q.qtype = T_A;
1488 } else {
1489 free(buf);
1490 free(buf2);
Bernie Innocenti948f6572018-09-12 21:32:42 +09001491 return EAI_NODATA;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001492 }
1493 break;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001494 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001495 case AF_INET:
1496 q.name = name;
1497 q.qclass = C_IN;
1498 q.qtype = T_A;
1499 q.answer = buf->buf;
1500 q.anslen = sizeof(buf->buf);
1501 break;
1502 case AF_INET6:
1503 q.name = name;
1504 q.qclass = C_IN;
1505 q.qtype = T_AAAA;
1506 q.answer = buf->buf;
1507 q.anslen = sizeof(buf->buf);
1508 break;
1509 default:
1510 free(buf);
1511 free(buf2);
Bernie Innocenti948f6572018-09-12 21:32:42 +09001512 return EAI_FAMILY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001513 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001514
Bernie Innocenti4acba1a2018-09-26 11:52:04 +09001515 res = res_get_state();
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001516 if (res == NULL) {
1517 free(buf);
1518 free(buf2);
Bernie Innocenti948f6572018-09-12 21:32:42 +09001519 return EAI_MEMORY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001520 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001521
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001522 /* this just sets our netid val in the thread private data so we don't have to
1523 * modify the api's all the way down to res_send.c's res_nsend. We could
1524 * fully populate the thread private data here, but if we get down there
1525 * and have a cache hit that would be wasted, so we do the rest there on miss
1526 */
1527 res_setnetcontext(res, netcontext);
1528 if (res_searchN(name, &q, res) < 0) {
Bernie Innocenti4acba1a2018-09-26 11:52:04 +09001529 res_put_state(res);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001530 free(buf);
1531 free(buf2);
Bernie Innocenti948f6572018-09-12 21:32:42 +09001532 return EAI_NODATA; // TODO: Decode error from h_errno like we do below
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001533 }
1534 ai = getanswer(buf, q.n, q.name, q.qtype, pai);
1535 if (ai) {
1536 cur->ai_next = ai;
1537 while (cur && cur->ai_next) cur = cur->ai_next;
1538 }
1539 if (q.next) {
1540 ai = getanswer(buf2, q2.n, q2.name, q2.qtype, pai);
1541 if (ai) cur->ai_next = ai;
1542 }
1543 free(buf);
1544 free(buf2);
1545 if (sentinel.ai_next == NULL) {
Bernie Innocenti4acba1a2018-09-26 11:52:04 +09001546 res_put_state(res);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001547 switch (h_errno) {
1548 case HOST_NOT_FOUND:
Bernie Innocenti948f6572018-09-12 21:32:42 +09001549 return EAI_NODATA;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001550 case TRY_AGAIN:
Bernie Innocenti948f6572018-09-12 21:32:42 +09001551 return EAI_AGAIN;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001552 default:
Bernie Innocenti948f6572018-09-12 21:32:42 +09001553 return EAI_FAIL;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001554 }
1555 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001556
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001557 _rfc6724_sort(&sentinel, netcontext->app_mark, netcontext->uid);
Bernie Innocenti55864192018-08-30 04:05:20 +09001558
Bernie Innocenti4acba1a2018-09-26 11:52:04 +09001559 res_put_state(res);
Bernie Innocenti55864192018-08-30 04:05:20 +09001560
Bernie Innocenti948f6572018-09-12 21:32:42 +09001561 *rv = sentinel.ai_next;
1562 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001563}
1564
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001565static void _sethtent(FILE** hostf) {
1566 if (!*hostf)
1567 *hostf = fopen(_PATH_HOSTS, "re");
1568 else
1569 rewind(*hostf);
Bernie Innocenti55864192018-08-30 04:05:20 +09001570}
1571
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001572static void _endhtent(FILE** hostf) {
1573 if (*hostf) {
1574 (void) fclose(*hostf);
1575 *hostf = NULL;
1576 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001577}
1578
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001579static struct addrinfo* _gethtent(FILE** hostf, const char* name, const struct addrinfo* pai) {
1580 char* p;
1581 char *cp, *tname, *cname;
Bernie Innocentic165ce82018-10-16 23:35:28 +09001582 struct addrinfo *res0, *res;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001583 int error;
1584 const char* addr;
1585 char hostbuf[8 * 1024];
Bernie Innocenti55864192018-08-30 04:05:20 +09001586
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001587 assert(name != NULL);
1588 assert(pai != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +09001589
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001590 if (!*hostf && !(*hostf = fopen(_PATH_HOSTS, "re"))) return (NULL);
1591again:
1592 if (!(p = fgets(hostbuf, sizeof hostbuf, *hostf))) return (NULL);
1593 if (*p == '#') goto again;
1594 if (!(cp = strpbrk(p, "#\n"))) goto again;
1595 *cp = '\0';
1596 if (!(cp = strpbrk(p, " \t"))) goto again;
1597 *cp++ = '\0';
1598 addr = p;
1599 /* if this is not something we're looking for, skip it. */
1600 cname = NULL;
1601 while (cp && *cp) {
1602 if (*cp == ' ' || *cp == '\t') {
1603 cp++;
1604 continue;
1605 }
1606 if (!cname) cname = cp;
1607 tname = cp;
1608 if ((cp = strpbrk(cp, " \t")) != NULL) *cp++ = '\0';
1609 // fprintf(stderr, "\ttname = '%s'", tname);
1610 if (strcasecmp(name, tname) == 0) goto found;
1611 }
1612 goto again;
Bernie Innocenti55864192018-08-30 04:05:20 +09001613
1614found:
Bernie Innocentic165ce82018-10-16 23:35:28 +09001615 error = getaddrinfo_numeric(addr, nullptr, *pai, &res0);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001616 if (error) goto again;
1617 for (res = res0; res; res = res->ai_next) {
1618 /* cover it up */
1619 res->ai_flags = pai->ai_flags;
Bernie Innocenti55864192018-08-30 04:05:20 +09001620
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001621 if (pai->ai_flags & AI_CANONNAME) {
1622 if (get_canonname(pai, res, cname) != 0) {
1623 freeaddrinfo(res0);
1624 goto again;
1625 }
1626 }
1627 }
1628 return res0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001629}
1630
Bernie Innocenti948f6572018-09-12 21:32:42 +09001631static bool files_getaddrinfo(const char* name, const addrinfo* pai, addrinfo** res) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001632 struct addrinfo sentinel, *cur;
1633 struct addrinfo* p;
1634 FILE* hostf = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001635
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001636 memset(&sentinel, 0, sizeof(sentinel));
1637 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +09001638
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001639 _sethtent(&hostf);
1640 while ((p = _gethtent(&hostf, name, pai)) != NULL) {
1641 cur->ai_next = p;
1642 while (cur && cur->ai_next) cur = cur->ai_next;
1643 }
1644 _endhtent(&hostf);
Bernie Innocenti55864192018-08-30 04:05:20 +09001645
Bernie Innocenti948f6572018-09-12 21:32:42 +09001646 *res = sentinel.ai_next;
1647 return sentinel.ai_next != NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001648}
1649
1650/* resolver logic */
1651
1652/*
1653 * Formulate a normal query, send, and await answer.
1654 * Returned answer is placed in supplied buffer "answer".
1655 * Perform preliminary check of answer, returning success only
1656 * if no error is indicated and the answer count is nonzero.
1657 * Return the size of the response on success, -1 on error.
1658 * Error number is left in h_errno.
1659 *
1660 * Caller must parse answer and determine whether it answers the question.
1661 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001662static int res_queryN(const char* name, /* domain name */ struct res_target* target,
1663 res_state res) {
1664 u_char buf[MAXPACKET];
1665 HEADER* hp;
1666 int n;
1667 struct res_target* t;
1668 int rcode;
1669 int ancount;
Bernie Innocenti55864192018-08-30 04:05:20 +09001670
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001671 assert(name != NULL);
1672 /* XXX: target may be NULL??? */
Bernie Innocenti55864192018-08-30 04:05:20 +09001673
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001674 rcode = NOERROR;
1675 ancount = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001676
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001677 for (t = target; t; t = t->next) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001678 u_char* answer;
1679 int anslen;
1680 u_int oflags;
Bernie Innocenti55864192018-08-30 04:05:20 +09001681
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001682 hp = (HEADER*) (void*) t->answer;
1683 oflags = res->_flags;
Bernie Innocenti55864192018-08-30 04:05:20 +09001684
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001685 again:
1686 hp->rcode = NOERROR; /* default */
Bernie Innocenti55864192018-08-30 04:05:20 +09001687
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001688 /* make it easier... */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001689 int cl = t->qclass;
1690 int type = t->qtype;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001691 answer = t->answer;
1692 anslen = t->anslen;
Bernie Innocenti55864192018-08-30 04:05:20 +09001693#ifdef DEBUG
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001694 if (res->options & RES_DEBUG) printf(";; res_nquery(%s, %d, %d)\n", name, cl, type);
Bernie Innocenti55864192018-08-30 04:05:20 +09001695#endif
1696
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001697 n = res_nmkquery(res, QUERY, name, cl, type, NULL, 0, NULL, buf, sizeof(buf));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001698 if (n > 0 && (res->_flags & RES_F_EDNS0ERR) == 0 &&
1699 (res->options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) != 0)
1700 n = res_nopt(res, n, buf, sizeof(buf), anslen);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001701 if (n <= 0) {
Bernie Innocenti55864192018-08-30 04:05:20 +09001702#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001703 if (res->options & RES_DEBUG) printf(";; res_nquery: mkquery failed\n");
Bernie Innocenti55864192018-08-30 04:05:20 +09001704#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001705 h_errno = NO_RECOVERY;
1706 return n;
1707 }
1708 n = res_nsend(res, buf, n, answer, anslen);
Bernie Innocenti55864192018-08-30 04:05:20 +09001709
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001710 if (n < 0 || hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
1711 rcode = hp->rcode; /* record most recent error */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001712 /* if the query choked with EDNS0, retry without EDNS0 */
1713 if ((res->options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) != 0 &&
1714 ((oflags ^ res->_flags) & RES_F_EDNS0ERR) != 0) {
1715 res->_flags |= RES_F_EDNS0ERR;
Bernie Innocenti55864192018-08-30 04:05:20 +09001716#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001717 if (res->options & RES_DEBUG) printf(";; res_nquery: retry without EDNS0\n");
Bernie Innocenti55864192018-08-30 04:05:20 +09001718#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001719 goto again;
1720 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001721#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001722 if (res->options & RES_DEBUG)
1723 printf(";; rcode = %u, ancount=%u\n", hp->rcode, ntohs(hp->ancount));
Bernie Innocenti55864192018-08-30 04:05:20 +09001724#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001725 continue;
1726 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001727
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001728 ancount += ntohs(hp->ancount);
Bernie Innocenti55864192018-08-30 04:05:20 +09001729
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001730 t->n = n;
1731 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001732
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001733 if (ancount == 0) {
1734 switch (rcode) {
1735 case NXDOMAIN:
1736 h_errno = HOST_NOT_FOUND;
1737 break;
1738 case SERVFAIL:
1739 h_errno = TRY_AGAIN;
1740 break;
1741 case NOERROR:
1742 h_errno = NO_DATA;
1743 break;
1744 case FORMERR:
1745 case NOTIMP:
1746 case REFUSED:
1747 default:
1748 h_errno = NO_RECOVERY;
1749 break;
1750 }
1751 return -1;
1752 }
1753 return ancount;
Bernie Innocenti55864192018-08-30 04:05:20 +09001754}
1755
1756/*
1757 * Formulate a normal query, send, and retrieve answer in supplied buffer.
1758 * Return the size of the response on success, -1 on error.
1759 * If enabled, implement search rules until answer or unrecoverable failure
1760 * is detected. Error code, if any, is left in h_errno.
1761 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001762static int res_searchN(const char* name, struct res_target* target, res_state res) {
1763 const char *cp, *const *domain;
1764 HEADER* hp;
1765 u_int dots;
1766 int trailing_dot, ret, saved_herrno;
1767 int got_nodata = 0, got_servfail = 0, tried_as_is = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001768
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001769 assert(name != NULL);
1770 assert(target != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +09001771
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001772 hp = (HEADER*) (void*) target->answer; /*XXX*/
Bernie Innocenti55864192018-08-30 04:05:20 +09001773
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001774 errno = 0;
1775 h_errno = HOST_NOT_FOUND; /* default, if we never query */
1776 dots = 0;
1777 for (cp = name; *cp; cp++) dots += (*cp == '.');
1778 trailing_dot = 0;
1779 if (cp > name && *--cp == '.') trailing_dot++;
Bernie Innocenti55864192018-08-30 04:05:20 +09001780
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001781 /*
1782 * If there are dots in the name already, let's just give it a try
1783 * 'as is'. The threshold can be set with the "ndots" option.
1784 */
1785 saved_herrno = -1;
1786 if (dots >= res->ndots) {
1787 ret = res_querydomainN(name, NULL, target, res);
1788 if (ret > 0) return (ret);
1789 saved_herrno = h_errno;
1790 tried_as_is++;
1791 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001792
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001793 /*
1794 * We do at least one level of search if
1795 * - there is no dot and RES_DEFNAME is set, or
1796 * - there is at least one dot, there is no trailing dot,
1797 * and RES_DNSRCH is set.
1798 */
1799 if ((!dots && (res->options & RES_DEFNAMES)) ||
1800 (dots && !trailing_dot && (res->options & RES_DNSRCH))) {
1801 int done = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001802
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001803 /* Unfortunately we need to set stuff up before
1804 * the domain stuff is tried. Will have a better
1805 * fix after thread pools are used.
1806 */
1807 _resolv_populate_res_for_net(res);
Bernie Innocenti55864192018-08-30 04:05:20 +09001808
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001809 for (domain = (const char* const*) res->dnsrch; *domain && !done; domain++) {
1810 ret = res_querydomainN(name, *domain, target, res);
1811 if (ret > 0) return ret;
Bernie Innocenti55864192018-08-30 04:05:20 +09001812
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001813 /*
1814 * If no server present, give up.
1815 * If name isn't found in this domain,
1816 * keep trying higher domains in the search list
1817 * (if that's enabled).
1818 * On a NO_DATA error, keep trying, otherwise
1819 * a wildcard entry of another type could keep us
1820 * from finding this entry higher in the domain.
1821 * If we get some other error (negative answer or
1822 * server failure), then stop searching up,
1823 * but try the input name below in case it's
1824 * fully-qualified.
1825 */
1826 if (errno == ECONNREFUSED) {
1827 h_errno = TRY_AGAIN;
1828 return -1;
1829 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001830
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001831 switch (h_errno) {
1832 case NO_DATA:
1833 got_nodata++;
Bernie Innocenti8bb94ba2018-10-10 22:30:12 +09001834 [[fallthrough]];
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001835 case HOST_NOT_FOUND:
1836 /* keep trying */
1837 break;
1838 case TRY_AGAIN:
1839 if (hp->rcode == SERVFAIL) {
1840 /* try next search element, if any */
1841 got_servfail++;
1842 break;
1843 }
Bernie Innocenti8bb94ba2018-10-10 22:30:12 +09001844 [[fallthrough]];
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001845 default:
1846 /* anything else implies that we're done */
1847 done++;
1848 }
1849 /*
1850 * if we got here for some reason other than DNSRCH,
1851 * we only wanted one iteration of the loop, so stop.
1852 */
1853 if (!(res->options & RES_DNSRCH)) done++;
1854 }
1855 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001856
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001857 /*
1858 * if we have not already tried the name "as is", do that now.
1859 * note that we do this regardless of how many dots were in the
1860 * name or whether it ends with a dot.
1861 */
1862 if (!tried_as_is) {
1863 ret = res_querydomainN(name, NULL, target, res);
1864 if (ret > 0) return ret;
1865 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001866
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001867 /*
1868 * if we got here, we didn't satisfy the search.
1869 * if we did an initial full query, return that query's h_errno
1870 * (note that we wouldn't be here if that query had succeeded).
1871 * else if we ever got a nodata, send that back as the reason.
1872 * else send back meaningless h_errno, that being the one from
1873 * the last DNSRCH we did.
1874 */
1875 if (saved_herrno != -1)
1876 h_errno = saved_herrno;
1877 else if (got_nodata)
1878 h_errno = NO_DATA;
1879 else if (got_servfail)
1880 h_errno = TRY_AGAIN;
1881 return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +09001882}
1883
1884/*
1885 * Perform a call on res_query on the concatenation of name and domain,
1886 * removing a trailing dot from name if domain is NULL.
1887 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001888static int res_querydomainN(const char* name, const char* domain, struct res_target* target,
1889 res_state res) {
1890 char nbuf[MAXDNAME];
1891 const char* longname = nbuf;
1892 size_t n, d;
Bernie Innocenti55864192018-08-30 04:05:20 +09001893
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001894 assert(name != NULL);
1895 /* XXX: target may be NULL??? */
Bernie Innocenti55864192018-08-30 04:05:20 +09001896
1897#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001898 if (res->options & RES_DEBUG)
1899 printf(";; res_querydomain(%s, %s)\n", name, domain ? domain : "<Nil>");
Bernie Innocenti55864192018-08-30 04:05:20 +09001900#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001901 if (domain == NULL) {
1902 /*
1903 * Check for trailing '.';
1904 * copy without '.' if present.
1905 */
1906 n = strlen(name);
1907 if (n + 1 > sizeof(nbuf)) {
1908 h_errno = NO_RECOVERY;
1909 return -1;
1910 }
1911 if (n > 0 && name[--n] == '.') {
1912 strncpy(nbuf, name, n);
1913 nbuf[n] = '\0';
1914 } else
1915 longname = name;
1916 } else {
1917 n = strlen(name);
1918 d = strlen(domain);
1919 if (n + 1 + d + 1 > sizeof(nbuf)) {
1920 h_errno = NO_RECOVERY;
1921 return -1;
1922 }
1923 snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain);
1924 }
1925 return res_queryN(longname, target, res);
Bernie Innocenti55864192018-08-30 04:05:20 +09001926}