blob: 6edff5a61ccddc3cd0d0e54f8b36e4bab04a8ed4 [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>
91#include <strings.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090092#include <sys/param.h>
93#include <sys/socket.h>
94#include <sys/stat.h>
95#include <sys/types.h>
96#include <sys/un.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090097#include <syslog.h>
Bernie Innocenti189eb502018-10-01 23:10:18 +090098#include <unistd.h>
Bernie Innocentif89b3512018-08-30 07:34:37 +090099
Bernie Innocenti189eb502018-10-01 23:10:18 +0900100#include "netd_resolv/resolv.h"
101#include "resolv_cache.h"
102#include "resolv_private.h"
Bernie Innocenti55864192018-08-30 04:05:20 +0900103
104typedef union sockaddr_union {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900105 struct sockaddr generic;
106 struct sockaddr_in in;
Bernie Innocenti55864192018-08-30 04:05:20 +0900107 struct sockaddr_in6 in6;
108} sockaddr_union;
109
Bernie Innocenti55864192018-08-30 04:05:20 +0900110#define ANY 0
Bernie Innocenti55864192018-08-30 04:05:20 +0900111
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900112static const char in_addrany[] = {0, 0, 0, 0};
113static const char in_loopback[] = {127, 0, 0, 1};
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900114static const char in6_addrany[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
115static 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 +0900116
Bernie Innocenti55864192018-08-30 04:05:20 +0900117static const struct afd {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900118 int a_af;
119 int a_addrlen;
120 int a_socklen;
121 int a_off;
122 const char* a_addrany;
123 const char* a_loopback;
124 int a_scoped;
125} afdl[] = {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900126 {PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
127 offsetof(struct sockaddr_in6, sin6_addr), in6_addrany, in6_loopback, 1},
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900128 {PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
129 offsetof(struct sockaddr_in, sin_addr), in_addrany, in_loopback, 0},
130 {0, 0, 0, 0, NULL, NULL, 0},
Bernie Innocenti55864192018-08-30 04:05:20 +0900131};
132
133struct explore {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900134 int e_af;
135 int e_socktype;
136 int e_protocol;
137 const char* e_protostr;
138 int e_wild;
139#define WILD_AF(ex) ((ex)->e_wild & 0x01)
140#define WILD_SOCKTYPE(ex) ((ex)->e_wild & 0x02)
141#define WILD_PROTOCOL(ex) ((ex)->e_wild & 0x04)
Bernie Innocenti55864192018-08-30 04:05:20 +0900142};
143
144static const struct explore explore[] = {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900145 {PF_INET6, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07},
146 {PF_INET6, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07},
147 {PF_INET6, SOCK_RAW, ANY, NULL, 0x05},
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900148 {PF_INET, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07},
149 {PF_INET, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07},
150 {PF_INET, SOCK_RAW, ANY, NULL, 0x05},
151 {PF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07},
152 {PF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07},
153 {PF_UNSPEC, SOCK_RAW, ANY, NULL, 0x05},
154 {-1, 0, 0, NULL, 0},
Bernie Innocenti55864192018-08-30 04:05:20 +0900155};
156
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900157#define PTON_MAX 16
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900158#define MAXPACKET (8 * 1024)
Bernie Innocenti55864192018-08-30 04:05:20 +0900159
160typedef union {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900161 HEADER hdr;
162 u_char buf[MAXPACKET];
Bernie Innocenti55864192018-08-30 04:05:20 +0900163} querybuf;
164
165struct res_target {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900166 struct res_target* next;
167 const char* name; /* domain name */
168 int qclass, qtype; /* class and type of query */
169 u_char* answer; /* buffer to put answer */
170 int anslen; /* size of answer buffer */
171 int n; /* result length */
Bernie Innocenti55864192018-08-30 04:05:20 +0900172};
173
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900174static int str2number(const char*);
175static int explore_fqdn(const struct addrinfo*, const char*, const char*, struct addrinfo**,
176 const struct android_net_context*);
177static int explore_null(const struct addrinfo*, const char*, struct addrinfo**);
178static int explore_numeric(const struct addrinfo*, const char*, const char*, struct addrinfo**,
179 const char*);
180static int explore_numeric_scope(const struct addrinfo*, const char*, const char*,
181 struct addrinfo**);
182static int get_canonname(const struct addrinfo*, struct addrinfo*, const char*);
183static struct addrinfo* get_ai(const struct addrinfo*, const struct afd*, const char*);
184static int get_portmatch(const struct addrinfo*, const char*);
185static int get_port(const struct addrinfo*, const char*, int);
186static const struct afd* find_afd(int);
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900187static int ip6_str2scopeid(const char*, struct sockaddr_in6*, u_int32_t*);
Bernie Innocenti55864192018-08-30 04:05:20 +0900188
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900189static struct addrinfo* getanswer(const querybuf*, int, const char*, int, const struct addrinfo*);
Bernie Innocenti948f6572018-09-12 21:32:42 +0900190static int dns_getaddrinfo(const char* name, const addrinfo* pai,
191 const android_net_context* netcontext, addrinfo** rv);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900192static void _sethtent(FILE**);
193static void _endhtent(FILE**);
194static struct addrinfo* _gethtent(FILE**, const char*, const struct addrinfo*);
Bernie Innocenti948f6572018-09-12 21:32:42 +0900195static bool files_getaddrinfo(const char* name, const addrinfo* pai, addrinfo** res);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900196static int _find_src_addr(const struct sockaddr*, struct sockaddr*, unsigned, uid_t);
Bernie Innocenti55864192018-08-30 04:05:20 +0900197
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900198static int res_queryN(const char*, struct res_target*, res_state);
199static int res_searchN(const char*, struct res_target*, res_state);
200static int res_querydomainN(const char*, const char*, struct res_target*, res_state);
Bernie Innocenti55864192018-08-30 04:05:20 +0900201
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900202static const char* const ai_errlist[] = {
203 "Success",
204 "Address family for hostname not supported", /* EAI_ADDRFAMILY */
205 "Temporary failure in name resolution", /* EAI_AGAIN */
206 "Invalid value for ai_flags", /* EAI_BADFLAGS */
207 "Non-recoverable failure in name resolution", /* EAI_FAIL */
208 "ai_family not supported", /* EAI_FAMILY */
209 "Memory allocation failure", /* EAI_MEMORY */
210 "No address associated with hostname", /* EAI_NODATA */
211 "hostname nor servname provided, or not known", /* EAI_NONAME */
212 "servname not supported for ai_socktype", /* EAI_SERVICE */
213 "ai_socktype not supported", /* EAI_SOCKTYPE */
214 "System error returned in errno", /* EAI_SYSTEM */
215 "Invalid value for hints", /* EAI_BADHINTS */
216 "Resolved protocol is unknown", /* EAI_PROTOCOL */
217 "Argument buffer overflow", /* EAI_OVERFLOW */
218 "Unknown error", /* EAI_MAX */
Bernie Innocenti55864192018-08-30 04:05:20 +0900219};
220
221/* XXX macros that make external reference is BAD. */
222
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900223#define GET_AI(ai, afd, addr) \
224 do { \
225 /* external reference: pai, error, and label free */ \
226 (ai) = get_ai(pai, (afd), (addr)); \
227 if ((ai) == NULL) { \
228 error = EAI_MEMORY; \
229 goto free; \
230 } \
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_PORT(ai, serv) \
234 do { \
235 /* external reference: error and label free */ \
236 error = get_port((ai), (serv), 0); \
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 GET_CANONNAME(ai, str) \
241 do { \
242 /* external reference: pai, error and label free */ \
243 error = get_canonname(pai, (ai), (str)); \
244 if (error != 0) goto free; \
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 ERR(err) \
248 do { \
249 /* external reference: error, and label bad */ \
250 error = (err); \
251 goto bad; \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900252 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900253
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900254#define MATCH_FAMILY(x, y, w) \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900255 ((x) == (y) || ((w) && ((x) == PF_UNSPEC || (y) == PF_UNSPEC)))
256#define MATCH(x, y, w) ((x) == (y) || ((w) && ((x) == ANY || (y) == ANY)))
Bernie Innocenti55864192018-08-30 04:05:20 +0900257
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900258const char* gai_strerror(int ecode) {
259 if (ecode < 0 || ecode > EAI_MAX) ecode = EAI_MAX;
260 return ai_errlist[ecode];
Bernie Innocenti55864192018-08-30 04:05:20 +0900261}
262
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900263void freeaddrinfo(struct addrinfo* ai) {
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900264 while (ai) {
265 struct addrinfo* next = ai->ai_next;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900266 if (ai->ai_canonname) free(ai->ai_canonname);
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900267 // Also frees ai->ai_addr which points to extra space beyond addrinfo
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900268 free(ai);
269 ai = next;
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900270 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900271}
272
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900273static int str2number(const char* p) {
274 char* ep;
275 unsigned long v;
Bernie Innocenti55864192018-08-30 04:05:20 +0900276
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900277 assert(p != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900278
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900279 if (*p == '\0') return -1;
280 ep = NULL;
281 errno = 0;
282 v = strtoul(p, &ep, 10);
283 if (errno == 0 && ep && *ep == '\0' && v <= UINT_MAX)
284 return v;
285 else
286 return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900287}
288
289/*
290 * The following functions determine whether IPv4 or IPv6 connectivity is
291 * available in order to implement AI_ADDRCONFIG.
292 *
293 * Strictly speaking, AI_ADDRCONFIG should not look at whether connectivity is
294 * available, but whether addresses of the specified family are "configured
295 * on the local system". However, bionic doesn't currently support getifaddrs,
296 * so checking for connectivity is the next best thing.
297 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900298static int _have_ipv6(unsigned mark, uid_t uid) {
299 static const struct sockaddr_in6 sin6_test = {
300 .sin6_family = AF_INET6,
301 .sin6_addr.s6_addr = {// 2000::
302 0x20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
303 sockaddr_union addr = {.in6 = sin6_test};
304 return _find_src_addr(&addr.generic, NULL, mark, uid) == 1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900305}
306
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900307static int _have_ipv4(unsigned mark, uid_t uid) {
308 static const struct sockaddr_in sin_test = {
309 .sin_family = AF_INET,
310 .sin_addr.s_addr = __constant_htonl(0x08080808L) // 8.8.8.8
311 };
312 sockaddr_union addr = {.in = sin_test};
313 return _find_src_addr(&addr.generic, NULL, mark, uid) == 1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900314}
315
316bool readBE32(FILE* fp, int32_t* result) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900317 int32_t tmp;
318 if (fread(&tmp, sizeof(tmp), 1, fp) != 1) {
319 return false;
320 }
321 *result = ntohl(tmp);
322 return true;
Bernie Innocenti55864192018-08-30 04:05:20 +0900323}
324
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900325int getaddrinfo(const char* hostname, const char* servname, const struct addrinfo* hints,
326 struct addrinfo** res) {
327 return android_getaddrinfofornet(hostname, servname, hints, NETID_UNSET, MARK_UNSET, res);
Bernie Innocenti55864192018-08-30 04:05:20 +0900328}
329
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900330int android_getaddrinfofornet(const char* hostname, const char* servname,
331 const struct addrinfo* hints, unsigned netid, unsigned mark,
332 struct addrinfo** res) {
333 struct android_net_context netcontext = {
334 .app_netid = netid,
335 .app_mark = mark,
336 .dns_netid = netid,
337 .dns_mark = mark,
338 .uid = NET_CONTEXT_INVALID_UID,
339 };
340 return android_getaddrinfofornetcontext(hostname, servname, hints, &netcontext, res);
Bernie Innocenti55864192018-08-30 04:05:20 +0900341}
342
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900343int android_getaddrinfofornetcontext(const char* hostname, const char* servname,
344 const struct addrinfo* hints,
345 const struct android_net_context* netcontext,
346 struct addrinfo** res) {
347 struct addrinfo sentinel;
348 struct addrinfo* cur;
349 int error = 0;
350 struct addrinfo ai;
351 struct addrinfo ai0;
352 struct addrinfo* pai;
353 const struct explore* ex;
Bernie Innocenti55864192018-08-30 04:05:20 +0900354
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900355 /* hostname is allowed to be NULL */
356 /* servname is allowed to be NULL */
357 /* hints is allowed to be NULL */
358 assert(res != NULL);
359 assert(netcontext != NULL);
360 memset(&sentinel, 0, sizeof(sentinel));
361 cur = &sentinel;
362 pai = &ai;
363 pai->ai_flags = 0;
364 pai->ai_family = PF_UNSPEC;
365 pai->ai_socktype = ANY;
366 pai->ai_protocol = ANY;
367 pai->ai_addrlen = 0;
368 pai->ai_canonname = NULL;
369 pai->ai_addr = NULL;
370 pai->ai_next = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900371
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900372 if (hostname == NULL && servname == NULL) return EAI_NONAME;
373 if (hints) {
374 /* error check for hints */
375 if (hints->ai_addrlen || hints->ai_canonname || hints->ai_addr || hints->ai_next)
376 ERR(EAI_BADHINTS); /* xxx */
377 if (hints->ai_flags & ~AI_MASK) ERR(EAI_BADFLAGS);
378 switch (hints->ai_family) {
379 case PF_UNSPEC:
380 case PF_INET:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900381 case PF_INET6:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900382 break;
383 default:
384 ERR(EAI_FAMILY);
385 }
386 memcpy(pai, hints, sizeof(*pai));
Bernie Innocenti55864192018-08-30 04:05:20 +0900387
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900388 /*
389 * if both socktype/protocol are specified, check if they
390 * are meaningful combination.
391 */
392 if (pai->ai_socktype != ANY && pai->ai_protocol != ANY) {
393 for (ex = explore; ex->e_af >= 0; ex++) {
394 if (pai->ai_family != ex->e_af) continue;
395 if (ex->e_socktype == ANY) continue;
396 if (ex->e_protocol == ANY) continue;
397 if (pai->ai_socktype == ex->e_socktype && pai->ai_protocol != ex->e_protocol) {
398 ERR(EAI_BADHINTS);
399 }
400 }
401 }
402 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900403
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900404 /*
405 * check for special cases. (1) numeric servname is disallowed if
406 * socktype/protocol are left unspecified. (2) servname is disallowed
407 * for raw and other inet{,6} sockets.
408 */
409 if (MATCH_FAMILY(pai->ai_family, PF_INET, 1)
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900410 || MATCH_FAMILY(pai->ai_family, PF_INET6, 1)
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900411 ) {
412 ai0 = *pai; /* backup *pai */
Bernie Innocenti55864192018-08-30 04:05:20 +0900413
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900414 if (pai->ai_family == PF_UNSPEC) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900415 pai->ai_family = PF_INET6;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900416 }
417 error = get_portmatch(pai, servname);
418 if (error) ERR(error);
Bernie Innocenti55864192018-08-30 04:05:20 +0900419
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900420 *pai = ai0;
421 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900422
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900423 ai0 = *pai;
Bernie Innocenti55864192018-08-30 04:05:20 +0900424
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900425 /* NULL hostname, or numeric hostname */
426 for (ex = explore; ex->e_af >= 0; ex++) {
427 *pai = ai0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900428
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900429 /* PF_UNSPEC entries are prepared for DNS queries only */
430 if (ex->e_af == PF_UNSPEC) continue;
Bernie Innocenti55864192018-08-30 04:05:20 +0900431
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900432 if (!MATCH_FAMILY(pai->ai_family, ex->e_af, WILD_AF(ex))) continue;
433 if (!MATCH(pai->ai_socktype, ex->e_socktype, WILD_SOCKTYPE(ex))) continue;
434 if (!MATCH(pai->ai_protocol, ex->e_protocol, WILD_PROTOCOL(ex))) continue;
Bernie Innocenti55864192018-08-30 04:05:20 +0900435
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900436 if (pai->ai_family == PF_UNSPEC) pai->ai_family = ex->e_af;
437 if (pai->ai_socktype == ANY && ex->e_socktype != ANY) pai->ai_socktype = ex->e_socktype;
438 if (pai->ai_protocol == ANY && ex->e_protocol != ANY) pai->ai_protocol = ex->e_protocol;
Bernie Innocenti55864192018-08-30 04:05:20 +0900439
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900440 if (hostname == NULL)
441 error = explore_null(pai, servname, &cur->ai_next);
442 else
443 error = explore_numeric_scope(pai, hostname, servname, &cur->ai_next);
Bernie Innocenti55864192018-08-30 04:05:20 +0900444
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900445 if (error) goto free;
Bernie Innocenti55864192018-08-30 04:05:20 +0900446
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900447 while (cur->ai_next) cur = cur->ai_next;
448 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900449
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900450 /*
451 * XXX
452 * If numeric representation of AF1 can be interpreted as FQDN
453 * representation of AF2, we need to think again about the code below.
454 */
455 if (sentinel.ai_next) goto good;
Bernie Innocenti55864192018-08-30 04:05:20 +0900456
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900457 if (hostname == NULL) ERR(EAI_NODATA);
458 if (pai->ai_flags & AI_NUMERICHOST) ERR(EAI_NONAME);
Bernie Innocenti55864192018-08-30 04:05:20 +0900459
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900460 /*
461 * hostname as alphabetical name.
462 * we would like to prefer AF_INET6 than AF_INET, so we'll make a
463 * outer loop by AFs.
464 */
465 for (ex = explore; ex->e_af >= 0; ex++) {
466 *pai = ai0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900467
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900468 /* require exact match for family field */
469 if (pai->ai_family != ex->e_af) continue;
Bernie Innocenti55864192018-08-30 04:05:20 +0900470
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900471 if (!MATCH(pai->ai_socktype, ex->e_socktype, WILD_SOCKTYPE(ex))) {
472 continue;
473 }
474 if (!MATCH(pai->ai_protocol, ex->e_protocol, WILD_PROTOCOL(ex))) {
475 continue;
476 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900477
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900478 if (pai->ai_socktype == ANY && ex->e_socktype != ANY) pai->ai_socktype = ex->e_socktype;
479 if (pai->ai_protocol == ANY && ex->e_protocol != ANY) pai->ai_protocol = ex->e_protocol;
Bernie Innocenti55864192018-08-30 04:05:20 +0900480
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900481 error = explore_fqdn(pai, hostname, servname, &cur->ai_next, netcontext);
Bernie Innocenti55864192018-08-30 04:05:20 +0900482
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900483 while (cur && cur->ai_next) cur = cur->ai_next;
484 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900485
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900486 /* XXX */
487 if (sentinel.ai_next) error = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900488
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900489 if (error) goto free;
490 if (error == 0) {
491 if (sentinel.ai_next) {
492 good:
493 *res = sentinel.ai_next;
Bernie Innocenti357339c2018-08-31 16:11:41 +0900494 return 0;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900495 } else
496 error = EAI_FAIL;
497 }
498free:
499bad:
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900500 freeaddrinfo(sentinel.ai_next);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900501 *res = NULL;
502 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900503}
504
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900505// FQDN hostname, DNS lookup
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900506static int explore_fqdn(const struct addrinfo* pai, const char* hostname, const char* servname,
507 struct addrinfo** res, const struct android_net_context* netcontext) {
508 struct addrinfo* result;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900509 int error = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900510
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900511 assert(pai != NULL);
512 /* hostname may be NULL */
513 /* servname may be NULL */
514 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900515
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900516 result = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900517
Bernie Innocenti948f6572018-09-12 21:32:42 +0900518 // If the servname does not match socktype/protocol, ignore it.
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900519 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900520
Bernie Innocenti948f6572018-09-12 21:32:42 +0900521 if (!files_getaddrinfo(hostname, pai, &result)) {
522 error = dns_getaddrinfo(hostname, pai, netcontext, &result);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900523 }
Bernie Innocenti948f6572018-09-12 21:32:42 +0900524 if (!error) {
525 struct addrinfo* cur;
526 for (cur = result; cur; cur = cur->ai_next) {
527 GET_PORT(cur, servname);
528 /* canonname should be filled already */
529 }
530 *res = result;
531 return 0;
532 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900533
534free:
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900535 freeaddrinfo(result);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900536 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900537}
538
539/*
540 * hostname == NULL.
541 * passive socket -> anyaddr (0.0.0.0 or ::)
542 * non-passive socket -> localhost (127.0.0.1 or ::1)
543 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900544static int explore_null(const struct addrinfo* pai, const char* servname, struct addrinfo** res) {
545 int s;
546 const struct afd* afd;
547 struct addrinfo* cur;
548 struct addrinfo sentinel;
549 int error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900550
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900551 assert(pai != NULL);
552 /* servname may be NULL */
553 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900554
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900555 *res = NULL;
556 sentinel.ai_next = NULL;
557 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +0900558
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900559 /*
560 * filter out AFs that are not supported by the kernel
561 * XXX errno?
562 */
563 s = socket(pai->ai_family, SOCK_DGRAM | SOCK_CLOEXEC, 0);
564 if (s < 0) {
565 if (errno != EMFILE) return 0;
566 } else
567 close(s);
Bernie Innocenti55864192018-08-30 04:05:20 +0900568
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900569 /*
570 * if the servname does not match socktype/protocol, ignore it.
571 */
572 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900573
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900574 afd = find_afd(pai->ai_family);
575 if (afd == NULL) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900576
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900577 if (pai->ai_flags & AI_PASSIVE) {
578 GET_AI(cur->ai_next, afd, afd->a_addrany);
579 /* xxx meaningless?
580 * GET_CANONNAME(cur->ai_next, "anyaddr");
581 */
582 GET_PORT(cur->ai_next, servname);
583 } else {
584 GET_AI(cur->ai_next, afd, afd->a_loopback);
585 /* xxx meaningless?
586 * GET_CANONNAME(cur->ai_next, "localhost");
587 */
588 GET_PORT(cur->ai_next, servname);
589 }
590 cur = cur->ai_next;
Bernie Innocenti55864192018-08-30 04:05:20 +0900591
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900592 *res = sentinel.ai_next;
593 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900594
595free:
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900596 freeaddrinfo(sentinel.ai_next);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900597 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900598}
599
600/*
601 * numeric hostname
602 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900603static int explore_numeric(const struct addrinfo* pai, const char* hostname, const char* servname,
604 struct addrinfo** res, const char* canonname) {
605 const struct afd* afd;
606 struct addrinfo* cur;
607 struct addrinfo sentinel;
608 int error;
609 char pton[PTON_MAX];
Bernie Innocenti55864192018-08-30 04:05:20 +0900610
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900611 assert(pai != NULL);
612 /* hostname may be NULL */
613 /* servname may be NULL */
614 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900615
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900616 *res = NULL;
617 sentinel.ai_next = NULL;
618 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +0900619
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900620 /*
621 * if the servname does not match socktype/protocol, ignore it.
622 */
623 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900624
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900625 afd = find_afd(pai->ai_family);
626 if (afd == NULL) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900627
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900628 switch (afd->a_af) {
Bernie Innocenti55864192018-08-30 04:05:20 +0900629#if 0 /*X/Open spec*/
630 case AF_INET:
631 if (inet_aton(hostname, (struct in_addr *)pton) == 1) {
632 if (pai->ai_family == afd->a_af ||
633 pai->ai_family == PF_UNSPEC /*?*/) {
634 GET_AI(cur->ai_next, afd, pton);
635 GET_PORT(cur->ai_next, servname);
636 if ((pai->ai_flags & AI_CANONNAME)) {
637 /*
638 * Set the numeric address itself as
639 * the canonical name, based on a
640 * clarification in rfc2553bis-03.
641 */
642 GET_CANONNAME(cur->ai_next, canonname);
643 }
644 while (cur && cur->ai_next)
645 cur = cur->ai_next;
646 } else
647 ERR(EAI_FAMILY); /*xxx*/
648 }
649 break;
650#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900651 default:
652 if (inet_pton(afd->a_af, hostname, pton) == 1) {
653 if (pai->ai_family == afd->a_af || pai->ai_family == PF_UNSPEC /*?*/) {
654 GET_AI(cur->ai_next, afd, pton);
655 GET_PORT(cur->ai_next, servname);
656 if ((pai->ai_flags & AI_CANONNAME)) {
657 /*
658 * Set the numeric address itself as
659 * the canonical name, based on a
660 * clarification in rfc2553bis-03.
661 */
662 GET_CANONNAME(cur->ai_next, canonname);
663 }
664 while (cur->ai_next) cur = cur->ai_next;
665 } else
666 ERR(EAI_FAMILY); /*xxx*/
667 }
668 break;
669 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900670
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900671 *res = sentinel.ai_next;
672 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900673
674free:
675bad:
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900676 freeaddrinfo(sentinel.ai_next);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900677 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900678}
679
680/*
681 * numeric hostname with scope
682 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900683static int explore_numeric_scope(const struct addrinfo* pai, const char* hostname,
684 const char* servname, struct addrinfo** res) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900685 const struct afd* afd;
686 struct addrinfo* cur;
687 int error;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900688 const char *cp, *scope, *addr;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900689 struct sockaddr_in6* sin6;
Bernie Innocenti55864192018-08-30 04:05:20 +0900690
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900691 assert(pai != NULL);
692 /* hostname may be NULL */
693 /* servname may be NULL */
694 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900695
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900696 /*
697 * if the servname does not match socktype/protocol, ignore it.
698 */
699 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900700
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900701 afd = find_afd(pai->ai_family);
702 if (afd == NULL) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900703
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900704 if (!afd->a_scoped) return explore_numeric(pai, hostname, servname, res, hostname);
Bernie Innocenti55864192018-08-30 04:05:20 +0900705
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900706 cp = strchr(hostname, SCOPE_DELIMITER);
707 if (cp == NULL) return explore_numeric(pai, hostname, servname, res, hostname);
Bernie Innocenti55864192018-08-30 04:05:20 +0900708
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900709 /*
710 * Handle special case of <scoped_address><delimiter><scope id>
711 */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900712 char* hostname2 = strdup(hostname);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900713 if (hostname2 == NULL) return EAI_MEMORY;
714 /* terminate at the delimiter */
715 hostname2[cp - hostname] = '\0';
716 addr = hostname2;
717 scope = cp + 1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900718
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900719 error = explore_numeric(pai, addr, servname, res, hostname);
720 if (error == 0) {
721 u_int32_t scopeid;
Bernie Innocenti55864192018-08-30 04:05:20 +0900722
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900723 for (cur = *res; cur; cur = cur->ai_next) {
724 if (cur->ai_family != AF_INET6) continue;
725 sin6 = (struct sockaddr_in6*) (void*) cur->ai_addr;
726 if (ip6_str2scopeid(scope, sin6, &scopeid) == -1) {
727 free(hostname2);
728 return (EAI_NODATA); /* XXX: is return OK? */
729 }
730 sin6->sin6_scope_id = scopeid;
731 }
732 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900733
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900734 free(hostname2);
Bernie Innocenti55864192018-08-30 04:05:20 +0900735
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900736 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900737}
738
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900739static int get_canonname(const struct addrinfo* pai, struct addrinfo* ai, const char* str) {
740 assert(pai != NULL);
741 assert(ai != NULL);
742 assert(str != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900743
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900744 if ((pai->ai_flags & AI_CANONNAME) != 0) {
745 ai->ai_canonname = strdup(str);
746 if (ai->ai_canonname == NULL) return EAI_MEMORY;
747 }
748 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900749}
750
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900751static struct addrinfo* get_ai(const struct addrinfo* pai, const struct afd* afd,
752 const char* addr) {
753 char* p;
754 struct addrinfo* ai;
Bernie Innocenti55864192018-08-30 04:05:20 +0900755
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900756 assert(pai != NULL);
757 assert(afd != NULL);
758 assert(addr != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900759
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900760 ai = (struct addrinfo*) malloc(sizeof(struct addrinfo) + (afd->a_socklen));
761 if (ai == NULL) return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900762
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900763 memcpy(ai, pai, sizeof(struct addrinfo));
764 ai->ai_addr = (struct sockaddr*) (void*) (ai + 1);
765 memset(ai->ai_addr, 0, (size_t) afd->a_socklen);
Bernie Innocenti55864192018-08-30 04:05:20 +0900766
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900767 ai->ai_addrlen = afd->a_socklen;
768#if defined(__alpha__) || (defined(__i386__) && defined(_LP64)) || defined(__sparc64__)
769 ai->__ai_pad0 = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900770#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900771 ai->ai_addr->sa_family = ai->ai_family = afd->a_af;
772 p = (char*) (void*) (ai->ai_addr);
773 memcpy(p + afd->a_off, addr, (size_t) afd->a_addrlen);
774 return ai;
Bernie Innocenti55864192018-08-30 04:05:20 +0900775}
776
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900777static int get_portmatch(const struct addrinfo* ai, const char* servname) {
778 assert(ai != NULL);
779 /* servname may be NULL */
Bernie Innocenti55864192018-08-30 04:05:20 +0900780
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900781 return get_port(ai, servname, 1);
Bernie Innocenti55864192018-08-30 04:05:20 +0900782}
783
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900784static int get_port(const struct addrinfo* ai, const char* servname, int matchonly) {
785 const char* proto;
786 struct servent* sp;
787 int port;
788 int allownumeric;
Bernie Innocenti55864192018-08-30 04:05:20 +0900789
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900790 assert(ai != NULL);
791 /* servname may be NULL */
Bernie Innocenti55864192018-08-30 04:05:20 +0900792
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900793 if (servname == NULL) return 0;
794 switch (ai->ai_family) {
795 case AF_INET:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900796 case AF_INET6:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900797 break;
798 default:
799 return 0;
800 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900801
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900802 switch (ai->ai_socktype) {
803 case SOCK_RAW:
804 return EAI_SERVICE;
805 case SOCK_DGRAM:
806 case SOCK_STREAM:
807 allownumeric = 1;
808 break;
809 case ANY:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900810 allownumeric = 1;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900811 break;
812 default:
813 return EAI_SOCKTYPE;
814 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900815
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900816 port = str2number(servname);
817 if (port >= 0) {
818 if (!allownumeric) return EAI_SERVICE;
819 if (port < 0 || port > 65535) return EAI_SERVICE;
820 port = htons(port);
821 } else {
822 if (ai->ai_flags & AI_NUMERICSERV) return EAI_NONAME;
Bernie Innocenti55864192018-08-30 04:05:20 +0900823
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900824 switch (ai->ai_socktype) {
825 case SOCK_DGRAM:
826 proto = "udp";
827 break;
828 case SOCK_STREAM:
829 proto = "tcp";
830 break;
831 default:
832 proto = NULL;
833 break;
834 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900835
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900836 if ((sp = getservbyname(servname, proto)) == NULL) return EAI_SERVICE;
837 port = sp->s_port;
838 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900839
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900840 if (!matchonly) {
841 switch (ai->ai_family) {
842 case AF_INET:
843 ((struct sockaddr_in*) (void*) ai->ai_addr)->sin_port = port;
844 break;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900845 case AF_INET6:
846 ((struct sockaddr_in6*) (void*) ai->ai_addr)->sin6_port = port;
847 break;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900848 }
849 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900850
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900851 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900852}
853
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900854static const struct afd* find_afd(int af) {
855 const struct afd* afd;
Bernie Innocenti55864192018-08-30 04:05:20 +0900856
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900857 if (af == PF_UNSPEC) return NULL;
858 for (afd = afdl; afd->a_af; afd++) {
859 if (afd->a_af == af) return afd;
860 }
861 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900862}
863
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900864// Convert a string to a scope identifier.
865static int ip6_str2scopeid(const char* scope, struct sockaddr_in6* sin6, u_int32_t* scopeid) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900866 u_long lscopeid;
867 struct in6_addr* a6;
868 char* ep;
Bernie Innocenti55864192018-08-30 04:05:20 +0900869
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900870 assert(scope != NULL);
871 assert(sin6 != NULL);
872 assert(scopeid != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900873
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900874 a6 = &sin6->sin6_addr;
Bernie Innocenti55864192018-08-30 04:05:20 +0900875
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900876 /* empty scopeid portion is invalid */
877 if (*scope == '\0') return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900878
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900879 if (IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) {
880 /*
881 * We currently assume a one-to-one mapping between links
882 * and interfaces, so we simply use interface indices for
883 * like-local scopes.
884 */
885 *scopeid = if_nametoindex(scope);
886 if (*scopeid == 0) goto trynumeric;
887 return 0;
888 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900889
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900890 /* still unclear about literal, allow numeric only - placeholder */
891 if (IN6_IS_ADDR_SITELOCAL(a6) || IN6_IS_ADDR_MC_SITELOCAL(a6)) goto trynumeric;
892 if (IN6_IS_ADDR_MC_ORGLOCAL(a6))
893 goto trynumeric;
894 else
895 goto trynumeric; /* global */
Bernie Innocenti55864192018-08-30 04:05:20 +0900896
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900897 /* try to convert to a numeric id as a last resort */
898trynumeric:
899 errno = 0;
900 lscopeid = strtoul(scope, &ep, 10);
901 *scopeid = (u_int32_t)(lscopeid & 0xffffffffUL);
902 if (errno == 0 && ep && *ep == '\0' && *scopeid == lscopeid)
903 return 0;
904 else
905 return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900906}
Bernie Innocenti55864192018-08-30 04:05:20 +0900907
908/* code duplicate with gethnamaddr.c */
909
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900910static const char AskedForGot[] = "gethostby*.getanswer: asked for \"%s\", got \"%s\"";
Bernie Innocenti55864192018-08-30 04:05:20 +0900911
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900912#define BOUNDED_INCR(x) \
913 do { \
914 BOUNDS_CHECK(cp, x); \
915 cp += (x); \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900916 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900917
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900918#define BOUNDS_CHECK(ptr, count) \
919 do { \
920 if (eom - (ptr) < (count)) { \
921 h_errno = NO_RECOVERY; \
922 return NULL; \
923 } \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900924 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900925
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900926static struct addrinfo* getanswer(const querybuf* answer, int anslen, const char* qname, int qtype,
927 const struct addrinfo* pai) {
928 struct addrinfo sentinel, *cur;
929 struct addrinfo ai;
930 const struct afd* afd;
931 char* canonname;
932 const HEADER* hp;
933 const u_char* cp;
934 int n;
935 const u_char* eom;
936 char *bp, *ep;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900937 int type, ancount, qdcount;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900938 int haveanswer, had_error;
939 char tbuf[MAXDNAME];
940 int (*name_ok)(const char*);
941 char hostbuf[8 * 1024];
Bernie Innocenti55864192018-08-30 04:05:20 +0900942
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900943 assert(answer != NULL);
944 assert(qname != NULL);
945 assert(pai != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900946
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900947 memset(&sentinel, 0, sizeof(sentinel));
948 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +0900949
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900950 canonname = NULL;
951 eom = answer->buf + anslen;
952 switch (qtype) {
953 case T_A:
954 case T_AAAA:
955 case T_ANY: /*use T_ANY only for T_A/T_AAAA lookup*/
956 name_ok = res_hnok;
957 break;
958 default:
959 return NULL; /* XXX should be abort(); */
960 }
961 /*
962 * find first satisfactory answer
963 */
964 hp = &answer->hdr;
965 ancount = ntohs(hp->ancount);
966 qdcount = ntohs(hp->qdcount);
967 bp = hostbuf;
968 ep = hostbuf + sizeof hostbuf;
969 cp = answer->buf;
970 BOUNDED_INCR(HFIXEDSZ);
971 if (qdcount != 1) {
972 h_errno = NO_RECOVERY;
973 return (NULL);
974 }
975 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
976 if ((n < 0) || !(*name_ok)(bp)) {
977 h_errno = NO_RECOVERY;
978 return (NULL);
979 }
980 BOUNDED_INCR(n + QFIXEDSZ);
981 if (qtype == T_A || qtype == T_AAAA || qtype == T_ANY) {
982 /* res_send() has already verified that the query name is the
983 * same as the one we sent; this just gets the expanded name
984 * (i.e., with the succeeding search-domain tacked on).
985 */
986 n = strlen(bp) + 1; /* for the \0 */
987 if (n >= MAXHOSTNAMELEN) {
988 h_errno = NO_RECOVERY;
989 return (NULL);
990 }
991 canonname = bp;
992 bp += n;
993 /* The qname can be abbreviated, but h_name is now absolute. */
994 qname = canonname;
995 }
996 haveanswer = 0;
997 had_error = 0;
998 while (ancount-- > 0 && cp < eom && !had_error) {
999 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
1000 if ((n < 0) || !(*name_ok)(bp)) {
1001 had_error++;
1002 continue;
1003 }
1004 cp += n; /* name */
1005 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
Bernie Innocentiee1b85b2018-09-25 14:23:19 +09001006 type = ns_get16(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001007 cp += INT16SZ; /* type */
Bernie Innocentiee1b85b2018-09-25 14:23:19 +09001008 int cl = ns_get16(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001009 cp += INT16SZ + INT32SZ; /* class, TTL */
Bernie Innocentiee1b85b2018-09-25 14:23:19 +09001010 n = ns_get16(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001011 cp += INT16SZ; /* len */
1012 BOUNDS_CHECK(cp, n);
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001013 if (cl != C_IN) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001014 /* XXX - debug? syslog? */
1015 cp += n;
1016 continue; /* XXX - had_error++ ? */
1017 }
1018 if ((qtype == T_A || qtype == T_AAAA || qtype == T_ANY) && type == T_CNAME) {
1019 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
1020 if ((n < 0) || !(*name_ok)(tbuf)) {
1021 had_error++;
1022 continue;
1023 }
1024 cp += n;
1025 /* Get canonical name. */
1026 n = strlen(tbuf) + 1; /* for the \0 */
1027 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
1028 had_error++;
1029 continue;
1030 }
1031 strlcpy(bp, tbuf, (size_t)(ep - bp));
1032 canonname = bp;
1033 bp += n;
1034 continue;
1035 }
1036 if (qtype == T_ANY) {
1037 if (!(type == T_A || type == T_AAAA)) {
1038 cp += n;
1039 continue;
1040 }
1041 } else if (type != qtype) {
1042 if (type != T_KEY && type != T_SIG)
1043 syslog(LOG_NOTICE | LOG_AUTH,
1044 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"", qname,
1045 p_class(C_IN), p_type(qtype), p_type(type));
1046 cp += n;
1047 continue; /* XXX - had_error++ ? */
1048 }
1049 switch (type) {
1050 case T_A:
1051 case T_AAAA:
1052 if (strcasecmp(canonname, bp) != 0) {
1053 syslog(LOG_NOTICE | LOG_AUTH, AskedForGot, canonname, bp);
1054 cp += n;
1055 continue; /* XXX - had_error++ ? */
1056 }
1057 if (type == T_A && n != INADDRSZ) {
1058 cp += n;
1059 continue;
1060 }
1061 if (type == T_AAAA && n != IN6ADDRSZ) {
1062 cp += n;
1063 continue;
1064 }
1065 if (type == T_AAAA) {
1066 struct in6_addr in6;
1067 memcpy(&in6, cp, IN6ADDRSZ);
1068 if (IN6_IS_ADDR_V4MAPPED(&in6)) {
1069 cp += n;
1070 continue;
1071 }
1072 }
1073 if (!haveanswer) {
1074 int nn;
Bernie Innocenti55864192018-08-30 04:05:20 +09001075
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001076 canonname = bp;
1077 nn = strlen(bp) + 1; /* for the \0 */
1078 bp += nn;
1079 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001080
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001081 /* don't overwrite pai */
1082 ai = *pai;
1083 ai.ai_family = (type == T_A) ? AF_INET : AF_INET6;
1084 afd = find_afd(ai.ai_family);
1085 if (afd == NULL) {
1086 cp += n;
1087 continue;
1088 }
1089 cur->ai_next = get_ai(&ai, afd, (const char*) cp);
1090 if (cur->ai_next == NULL) had_error++;
1091 while (cur && cur->ai_next) cur = cur->ai_next;
1092 cp += n;
1093 break;
1094 default:
1095 abort();
1096 }
1097 if (!had_error) haveanswer++;
1098 }
1099 if (haveanswer) {
1100 if (!canonname)
1101 (void) get_canonname(pai, sentinel.ai_next, qname);
1102 else
1103 (void) get_canonname(pai, sentinel.ai_next, canonname);
1104 h_errno = NETDB_SUCCESS;
1105 return sentinel.ai_next;
1106 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001107
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001108 h_errno = NO_RECOVERY;
1109 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001110}
1111
1112struct addrinfo_sort_elem {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001113 struct addrinfo* ai;
1114 int has_src_addr;
1115 sockaddr_union src_addr;
1116 int original_order;
Bernie Innocenti55864192018-08-30 04:05:20 +09001117};
1118
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001119static int _get_scope(const struct sockaddr* addr) {
1120 if (addr->sa_family == AF_INET6) {
1121 const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
1122 if (IN6_IS_ADDR_MULTICAST(&addr6->sin6_addr)) {
1123 return IPV6_ADDR_MC_SCOPE(&addr6->sin6_addr);
1124 } else if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr) ||
1125 IN6_IS_ADDR_LINKLOCAL(&addr6->sin6_addr)) {
1126 /*
1127 * RFC 4291 section 2.5.3 says loopback is to be treated as having
1128 * link-local scope.
1129 */
1130 return IPV6_ADDR_SCOPE_LINKLOCAL;
1131 } else if (IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr)) {
1132 return IPV6_ADDR_SCOPE_SITELOCAL;
1133 } else {
1134 return IPV6_ADDR_SCOPE_GLOBAL;
1135 }
1136 } else if (addr->sa_family == AF_INET) {
1137 const struct sockaddr_in* addr4 = (const struct sockaddr_in*) addr;
1138 unsigned long int na = ntohl(addr4->sin_addr.s_addr);
Bernie Innocenti55864192018-08-30 04:05:20 +09001139
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001140 if (IN_LOOPBACK(na) || /* 127.0.0.0/8 */
1141 (na & 0xffff0000) == 0xa9fe0000) { /* 169.254.0.0/16 */
1142 return IPV6_ADDR_SCOPE_LINKLOCAL;
1143 } else {
1144 /*
1145 * RFC 6724 section 3.2. Other IPv4 addresses, including private addresses
1146 * and shared addresses (100.64.0.0/10), are assigned global scope.
1147 */
1148 return IPV6_ADDR_SCOPE_GLOBAL;
1149 }
1150 } else {
1151 /*
1152 * This should never happen.
1153 * Return a scope with low priority as a last resort.
1154 */
1155 return IPV6_ADDR_SCOPE_NODELOCAL;
1156 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001157}
1158
1159/* These macros are modelled after the ones in <netinet/in6.h>. */
1160
1161/* RFC 4380, section 2.6 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001162#define IN6_IS_ADDR_TEREDO(a) \
1163 ((*(const uint32_t*) (const void*) (&(a)->s6_addr[0]) == ntohl(0x20010000)))
Bernie Innocenti55864192018-08-30 04:05:20 +09001164
1165/* RFC 3056, section 2. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001166#define IN6_IS_ADDR_6TO4(a) (((a)->s6_addr[0] == 0x20) && ((a)->s6_addr[1] == 0x02))
Bernie Innocenti55864192018-08-30 04:05:20 +09001167
1168/* 6bone testing address area (3ffe::/16), deprecated in RFC 3701. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001169#define IN6_IS_ADDR_6BONE(a) (((a)->s6_addr[0] == 0x3f) && ((a)->s6_addr[1] == 0xfe))
Bernie Innocenti55864192018-08-30 04:05:20 +09001170
1171/*
1172 * Get the label for a given IPv4/IPv6 address.
1173 * RFC 6724, section 2.1.
1174 */
1175
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001176static int _get_label(const struct sockaddr* addr) {
1177 if (addr->sa_family == AF_INET) {
1178 return 4;
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 0;
1183 } else if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr)) {
1184 return 4;
1185 } else if (IN6_IS_ADDR_6TO4(&addr6->sin6_addr)) {
1186 return 2;
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 13;
1191 } else if (IN6_IS_ADDR_V4COMPAT(&addr6->sin6_addr)) {
1192 return 3;
1193 } else if (IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr)) {
1194 return 11;
1195 } else if (IN6_IS_ADDR_6BONE(&addr6->sin6_addr)) {
1196 return 12;
1197 } else {
1198 /* All other IPv6 addresses, including global unicast addresses. */
1199 return 1;
1200 }
1201 } else {
1202 /*
1203 * This should never happen.
1204 * Return a semi-random label as a last resort.
1205 */
1206 return 1;
1207 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001208}
1209
1210/*
1211 * Get the precedence for a given IPv4/IPv6 address.
1212 * RFC 6724, section 2.1.
1213 */
1214
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001215static int _get_precedence(const struct sockaddr* addr) {
1216 if (addr->sa_family == AF_INET) {
1217 return 35;
1218 } else if (addr->sa_family == AF_INET6) {
1219 const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
1220 if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr)) {
1221 return 50;
1222 } else if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr)) {
1223 return 35;
1224 } else if (IN6_IS_ADDR_6TO4(&addr6->sin6_addr)) {
1225 return 30;
1226 } else if (IN6_IS_ADDR_TEREDO(&addr6->sin6_addr)) {
1227 return 5;
1228 } else if (IN6_IS_ADDR_ULA(&addr6->sin6_addr)) {
1229 return 3;
1230 } else if (IN6_IS_ADDR_V4COMPAT(&addr6->sin6_addr) ||
1231 IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr) ||
1232 IN6_IS_ADDR_6BONE(&addr6->sin6_addr)) {
1233 return 1;
1234 } else {
1235 /* All other IPv6 addresses, including global unicast addresses. */
1236 return 40;
1237 }
1238 } else {
1239 return 1;
1240 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001241}
1242
1243/*
1244 * Find number of matching initial bits between the two addresses a1 and a2.
1245 */
1246
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001247static int _common_prefix_len(const struct in6_addr* a1, const struct in6_addr* a2) {
1248 const char* p1 = (const char*) a1;
1249 const char* p2 = (const char*) a2;
1250 unsigned i;
Bernie Innocenti55864192018-08-30 04:05:20 +09001251
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001252 for (i = 0; i < sizeof(*a1); ++i) {
1253 int x, j;
Bernie Innocenti55864192018-08-30 04:05:20 +09001254
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001255 if (p1[i] == p2[i]) {
1256 continue;
1257 }
1258 x = p1[i] ^ p2[i];
1259 for (j = 0; j < CHAR_BIT; ++j) {
1260 if (x & (1 << (CHAR_BIT - 1))) {
1261 return i * CHAR_BIT + j;
1262 }
1263 x <<= 1;
1264 }
1265 }
1266 return sizeof(*a1) * CHAR_BIT;
Bernie Innocenti55864192018-08-30 04:05:20 +09001267}
1268
1269/*
1270 * Compare two source/destination address pairs.
1271 * RFC 6724, section 6.
1272 */
1273
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001274static int _rfc6724_compare(const void* ptr1, const void* ptr2) {
1275 const struct addrinfo_sort_elem* a1 = (const struct addrinfo_sort_elem*) ptr1;
1276 const struct addrinfo_sort_elem* a2 = (const struct addrinfo_sort_elem*) ptr2;
1277 int scope_src1, scope_dst1, scope_match1;
1278 int scope_src2, scope_dst2, scope_match2;
1279 int label_src1, label_dst1, label_match1;
1280 int label_src2, label_dst2, label_match2;
1281 int precedence1, precedence2;
1282 int prefixlen1, prefixlen2;
Bernie Innocenti55864192018-08-30 04:05:20 +09001283
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001284 /* Rule 1: Avoid unusable destinations. */
1285 if (a1->has_src_addr != a2->has_src_addr) {
1286 return a2->has_src_addr - a1->has_src_addr;
1287 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001288
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001289 /* Rule 2: Prefer matching scope. */
1290 scope_src1 = _get_scope(&a1->src_addr.generic);
1291 scope_dst1 = _get_scope(a1->ai->ai_addr);
1292 scope_match1 = (scope_src1 == scope_dst1);
Bernie Innocenti55864192018-08-30 04:05:20 +09001293
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001294 scope_src2 = _get_scope(&a2->src_addr.generic);
1295 scope_dst2 = _get_scope(a2->ai->ai_addr);
1296 scope_match2 = (scope_src2 == scope_dst2);
Bernie Innocenti55864192018-08-30 04:05:20 +09001297
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001298 if (scope_match1 != scope_match2) {
1299 return scope_match2 - scope_match1;
1300 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001301
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001302 /*
1303 * Rule 3: Avoid deprecated addresses.
1304 * TODO(sesse): We don't currently have a good way of finding this.
1305 */
Bernie Innocenti55864192018-08-30 04:05:20 +09001306
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001307 /*
1308 * Rule 4: Prefer home addresses.
1309 * TODO(sesse): We don't currently have a good way of finding this.
1310 */
Bernie Innocenti55864192018-08-30 04:05:20 +09001311
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001312 /* Rule 5: Prefer matching label. */
1313 label_src1 = _get_label(&a1->src_addr.generic);
1314 label_dst1 = _get_label(a1->ai->ai_addr);
1315 label_match1 = (label_src1 == label_dst1);
Bernie Innocenti55864192018-08-30 04:05:20 +09001316
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001317 label_src2 = _get_label(&a2->src_addr.generic);
1318 label_dst2 = _get_label(a2->ai->ai_addr);
1319 label_match2 = (label_src2 == label_dst2);
Bernie Innocenti55864192018-08-30 04:05:20 +09001320
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001321 if (label_match1 != label_match2) {
1322 return label_match2 - label_match1;
1323 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001324
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001325 /* Rule 6: Prefer higher precedence. */
1326 precedence1 = _get_precedence(a1->ai->ai_addr);
1327 precedence2 = _get_precedence(a2->ai->ai_addr);
1328 if (precedence1 != precedence2) {
1329 return precedence2 - precedence1;
1330 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001331
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001332 /*
1333 * Rule 7: Prefer native transport.
1334 * TODO(sesse): We don't currently have a good way of finding this.
1335 */
Bernie Innocenti55864192018-08-30 04:05:20 +09001336
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001337 /* Rule 8: Prefer smaller scope. */
1338 if (scope_dst1 != scope_dst2) {
1339 return scope_dst1 - scope_dst2;
1340 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001341
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001342 /*
1343 * Rule 9: Use longest matching prefix.
1344 * We implement this for IPv6 only, as the rules in RFC 6724 don't seem
1345 * to work very well directly applied to IPv4. (glibc uses information from
1346 * the routing table for a custom IPv4 implementation here.)
1347 */
1348 if (a1->has_src_addr && a1->ai->ai_addr->sa_family == AF_INET6 && a2->has_src_addr &&
1349 a2->ai->ai_addr->sa_family == AF_INET6) {
1350 const struct sockaddr_in6* a1_src = &a1->src_addr.in6;
1351 const struct sockaddr_in6* a1_dst = (const struct sockaddr_in6*) a1->ai->ai_addr;
1352 const struct sockaddr_in6* a2_src = &a2->src_addr.in6;
1353 const struct sockaddr_in6* a2_dst = (const struct sockaddr_in6*) a2->ai->ai_addr;
1354 prefixlen1 = _common_prefix_len(&a1_src->sin6_addr, &a1_dst->sin6_addr);
1355 prefixlen2 = _common_prefix_len(&a2_src->sin6_addr, &a2_dst->sin6_addr);
1356 if (prefixlen1 != prefixlen2) {
1357 return prefixlen2 - prefixlen1;
1358 }
1359 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001360
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001361 /*
1362 * Rule 10: Leave the order unchanged.
1363 * We need this since qsort() is not necessarily stable.
1364 */
1365 return a1->original_order - a2->original_order;
Bernie Innocenti55864192018-08-30 04:05:20 +09001366}
1367
1368/*
1369 * Find the source address that will be used if trying to connect to the given
1370 * address. src_addr must be large enough to hold a struct sockaddr_in6.
1371 *
1372 * Returns 1 if a source address was found, 0 if the address is unreachable,
1373 * and -1 if a fatal error occurred. If 0 or -1, the contents of src_addr are
1374 * undefined.
1375 */
1376
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001377static int _find_src_addr(const struct sockaddr* addr, struct sockaddr* src_addr, unsigned mark,
1378 uid_t uid) {
1379 int sock;
1380 int ret;
1381 socklen_t len;
Bernie Innocenti55864192018-08-30 04:05:20 +09001382
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001383 switch (addr->sa_family) {
1384 case AF_INET:
1385 len = sizeof(struct sockaddr_in);
1386 break;
1387 case AF_INET6:
1388 len = sizeof(struct sockaddr_in6);
1389 break;
1390 default:
1391 /* No known usable source address for non-INET families. */
1392 return 0;
1393 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001394
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001395 sock = socket(addr->sa_family, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP);
1396 if (sock == -1) {
1397 if (errno == EAFNOSUPPORT) {
1398 return 0;
1399 } else {
1400 return -1;
1401 }
1402 }
1403 if (mark != MARK_UNSET && setsockopt(sock, SOL_SOCKET, SO_MARK, &mark, sizeof(mark)) < 0) {
1404 close(sock);
1405 return 0;
1406 }
1407 if (uid > 0 && uid != NET_CONTEXT_INVALID_UID && fchown(sock, uid, (gid_t) -1) < 0) {
1408 close(sock);
1409 return 0;
1410 }
1411 do {
Bernie Innocentif89b3512018-08-30 07:34:37 +09001412 ret = connect(sock, addr, len);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001413 } while (ret == -1 && errno == EINTR);
Bernie Innocenti55864192018-08-30 04:05:20 +09001414
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001415 if (ret == -1) {
1416 close(sock);
1417 return 0;
1418 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001419
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001420 if (src_addr && getsockname(sock, src_addr, &len) == -1) {
1421 close(sock);
1422 return -1;
1423 }
1424 close(sock);
1425 return 1;
Bernie Innocenti55864192018-08-30 04:05:20 +09001426}
1427
1428/*
1429 * Sort the linked list starting at sentinel->ai_next in RFC6724 order.
1430 * Will leave the list unchanged if an error occurs.
1431 */
1432
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001433static void _rfc6724_sort(struct addrinfo* list_sentinel, unsigned mark, uid_t uid) {
1434 struct addrinfo* cur;
1435 int nelem = 0, i;
1436 struct addrinfo_sort_elem* elems;
Bernie Innocenti55864192018-08-30 04:05:20 +09001437
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001438 cur = list_sentinel->ai_next;
1439 while (cur) {
1440 ++nelem;
1441 cur = cur->ai_next;
1442 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001443
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001444 elems = (struct addrinfo_sort_elem*) malloc(nelem * sizeof(struct addrinfo_sort_elem));
1445 if (elems == NULL) {
1446 goto error;
1447 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001448
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001449 /*
1450 * Convert the linked list to an array that also contains the candidate
1451 * source address for each destination address.
1452 */
1453 for (i = 0, cur = list_sentinel->ai_next; i < nelem; ++i, cur = cur->ai_next) {
1454 int has_src_addr;
1455 assert(cur != NULL);
1456 elems[i].ai = cur;
1457 elems[i].original_order = i;
Bernie Innocenti55864192018-08-30 04:05:20 +09001458
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001459 has_src_addr = _find_src_addr(cur->ai_addr, &elems[i].src_addr.generic, mark, uid);
1460 if (has_src_addr == -1) {
1461 goto error;
1462 }
1463 elems[i].has_src_addr = has_src_addr;
1464 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001465
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001466 /* Sort the addresses, and rearrange the linked list so it matches the sorted order. */
1467 qsort((void*) elems, nelem, sizeof(struct addrinfo_sort_elem), _rfc6724_compare);
Bernie Innocenti55864192018-08-30 04:05:20 +09001468
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001469 list_sentinel->ai_next = elems[0].ai;
1470 for (i = 0; i < nelem - 1; ++i) {
1471 elems[i].ai->ai_next = elems[i + 1].ai;
1472 }
1473 elems[nelem - 1].ai->ai_next = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001474
1475error:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001476 free(elems);
Bernie Innocenti55864192018-08-30 04:05:20 +09001477}
1478
Bernie Innocenti948f6572018-09-12 21:32:42 +09001479static int dns_getaddrinfo(const char* name, const addrinfo* pai,
1480 const android_net_context* netcontext, addrinfo** rv) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001481 struct addrinfo* ai;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001482 struct addrinfo sentinel, *cur;
1483 struct res_target q, q2;
1484 res_state res;
Bernie Innocenti55864192018-08-30 04:05:20 +09001485
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001486 memset(&q, 0, sizeof(q));
1487 memset(&q2, 0, sizeof(q2));
1488 memset(&sentinel, 0, sizeof(sentinel));
1489 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +09001490
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001491 querybuf* buf = (querybuf*) malloc(sizeof(*buf));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001492 if (buf == NULL) {
1493 h_errno = NETDB_INTERNAL;
Bernie Innocenti948f6572018-09-12 21:32:42 +09001494 return EAI_MEMORY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001495 }
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001496 querybuf* buf2 = (querybuf*) malloc(sizeof(*buf2));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001497 if (buf2 == NULL) {
1498 free(buf);
1499 h_errno = NETDB_INTERNAL;
Bernie Innocenti948f6572018-09-12 21:32:42 +09001500 return EAI_MEMORY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001501 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001502
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001503 switch (pai->ai_family) {
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001504 case AF_UNSPEC: {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001505 /* prefer IPv6 */
1506 q.name = name;
1507 q.qclass = C_IN;
1508 q.answer = buf->buf;
1509 q.anslen = sizeof(buf->buf);
1510 int query_ipv6 = 1, query_ipv4 = 1;
1511 if (pai->ai_flags & AI_ADDRCONFIG) {
1512 query_ipv6 = _have_ipv6(netcontext->app_mark, netcontext->uid);
1513 query_ipv4 = _have_ipv4(netcontext->app_mark, netcontext->uid);
1514 }
1515 if (query_ipv6) {
1516 q.qtype = T_AAAA;
1517 if (query_ipv4) {
1518 q.next = &q2;
1519 q2.name = name;
1520 q2.qclass = C_IN;
1521 q2.qtype = T_A;
1522 q2.answer = buf2->buf;
1523 q2.anslen = sizeof(buf2->buf);
1524 }
1525 } else if (query_ipv4) {
1526 q.qtype = T_A;
1527 } else {
1528 free(buf);
1529 free(buf2);
Bernie Innocenti948f6572018-09-12 21:32:42 +09001530 return EAI_NODATA;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001531 }
1532 break;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001533 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001534 case AF_INET:
1535 q.name = name;
1536 q.qclass = C_IN;
1537 q.qtype = T_A;
1538 q.answer = buf->buf;
1539 q.anslen = sizeof(buf->buf);
1540 break;
1541 case AF_INET6:
1542 q.name = name;
1543 q.qclass = C_IN;
1544 q.qtype = T_AAAA;
1545 q.answer = buf->buf;
1546 q.anslen = sizeof(buf->buf);
1547 break;
1548 default:
1549 free(buf);
1550 free(buf2);
Bernie Innocenti948f6572018-09-12 21:32:42 +09001551 return EAI_FAMILY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001552 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001553
Bernie Innocenti4acba1a2018-09-26 11:52:04 +09001554 res = res_get_state();
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001555 if (res == NULL) {
1556 free(buf);
1557 free(buf2);
Bernie Innocenti948f6572018-09-12 21:32:42 +09001558 return EAI_MEMORY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001559 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001560
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001561 /* this just sets our netid val in the thread private data so we don't have to
1562 * modify the api's all the way down to res_send.c's res_nsend. We could
1563 * fully populate the thread private data here, but if we get down there
1564 * and have a cache hit that would be wasted, so we do the rest there on miss
1565 */
1566 res_setnetcontext(res, netcontext);
1567 if (res_searchN(name, &q, res) < 0) {
Bernie Innocenti4acba1a2018-09-26 11:52:04 +09001568 res_put_state(res);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001569 free(buf);
1570 free(buf2);
Bernie Innocenti948f6572018-09-12 21:32:42 +09001571 return EAI_NODATA; // TODO: Decode error from h_errno like we do below
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001572 }
1573 ai = getanswer(buf, q.n, q.name, q.qtype, pai);
1574 if (ai) {
1575 cur->ai_next = ai;
1576 while (cur && cur->ai_next) cur = cur->ai_next;
1577 }
1578 if (q.next) {
1579 ai = getanswer(buf2, q2.n, q2.name, q2.qtype, pai);
1580 if (ai) cur->ai_next = ai;
1581 }
1582 free(buf);
1583 free(buf2);
1584 if (sentinel.ai_next == NULL) {
Bernie Innocenti4acba1a2018-09-26 11:52:04 +09001585 res_put_state(res);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001586 switch (h_errno) {
1587 case HOST_NOT_FOUND:
Bernie Innocenti948f6572018-09-12 21:32:42 +09001588 return EAI_NODATA;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001589 case TRY_AGAIN:
Bernie Innocenti948f6572018-09-12 21:32:42 +09001590 return EAI_AGAIN;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001591 default:
Bernie Innocenti948f6572018-09-12 21:32:42 +09001592 return EAI_FAIL;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001593 }
1594 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001595
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001596 _rfc6724_sort(&sentinel, netcontext->app_mark, netcontext->uid);
Bernie Innocenti55864192018-08-30 04:05:20 +09001597
Bernie Innocenti4acba1a2018-09-26 11:52:04 +09001598 res_put_state(res);
Bernie Innocenti55864192018-08-30 04:05:20 +09001599
Bernie Innocenti948f6572018-09-12 21:32:42 +09001600 *rv = sentinel.ai_next;
1601 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001602}
1603
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001604static void _sethtent(FILE** hostf) {
1605 if (!*hostf)
1606 *hostf = fopen(_PATH_HOSTS, "re");
1607 else
1608 rewind(*hostf);
Bernie Innocenti55864192018-08-30 04:05:20 +09001609}
1610
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001611static void _endhtent(FILE** hostf) {
1612 if (*hostf) {
1613 (void) fclose(*hostf);
1614 *hostf = NULL;
1615 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001616}
1617
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001618static struct addrinfo* _gethtent(FILE** hostf, const char* name, const struct addrinfo* pai) {
1619 char* p;
1620 char *cp, *tname, *cname;
1621 struct addrinfo hints, *res0, *res;
1622 int error;
1623 const char* addr;
1624 char hostbuf[8 * 1024];
Bernie Innocenti55864192018-08-30 04:05:20 +09001625
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001626 assert(name != NULL);
1627 assert(pai != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +09001628
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001629 if (!*hostf && !(*hostf = fopen(_PATH_HOSTS, "re"))) return (NULL);
1630again:
1631 if (!(p = fgets(hostbuf, sizeof hostbuf, *hostf))) return (NULL);
1632 if (*p == '#') goto again;
1633 if (!(cp = strpbrk(p, "#\n"))) goto again;
1634 *cp = '\0';
1635 if (!(cp = strpbrk(p, " \t"))) goto again;
1636 *cp++ = '\0';
1637 addr = p;
1638 /* if this is not something we're looking for, skip it. */
1639 cname = NULL;
1640 while (cp && *cp) {
1641 if (*cp == ' ' || *cp == '\t') {
1642 cp++;
1643 continue;
1644 }
1645 if (!cname) cname = cp;
1646 tname = cp;
1647 if ((cp = strpbrk(cp, " \t")) != NULL) *cp++ = '\0';
1648 // fprintf(stderr, "\ttname = '%s'", tname);
1649 if (strcasecmp(name, tname) == 0) goto found;
1650 }
1651 goto again;
Bernie Innocenti55864192018-08-30 04:05:20 +09001652
1653found:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001654 hints = *pai;
1655 hints.ai_flags = AI_NUMERICHOST;
1656 error = getaddrinfo(addr, NULL, &hints, &res0);
1657 if (error) goto again;
1658 for (res = res0; res; res = res->ai_next) {
1659 /* cover it up */
1660 res->ai_flags = pai->ai_flags;
Bernie Innocenti55864192018-08-30 04:05:20 +09001661
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001662 if (pai->ai_flags & AI_CANONNAME) {
1663 if (get_canonname(pai, res, cname) != 0) {
1664 freeaddrinfo(res0);
1665 goto again;
1666 }
1667 }
1668 }
1669 return res0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001670}
1671
Bernie Innocenti948f6572018-09-12 21:32:42 +09001672static bool files_getaddrinfo(const char* name, const addrinfo* pai, addrinfo** res) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001673 struct addrinfo sentinel, *cur;
1674 struct addrinfo* p;
1675 FILE* hostf = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001676
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001677 memset(&sentinel, 0, sizeof(sentinel));
1678 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +09001679
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001680 _sethtent(&hostf);
1681 while ((p = _gethtent(&hostf, name, pai)) != NULL) {
1682 cur->ai_next = p;
1683 while (cur && cur->ai_next) cur = cur->ai_next;
1684 }
1685 _endhtent(&hostf);
Bernie Innocenti55864192018-08-30 04:05:20 +09001686
Bernie Innocenti948f6572018-09-12 21:32:42 +09001687 *res = sentinel.ai_next;
1688 return sentinel.ai_next != NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001689}
1690
1691/* resolver logic */
1692
1693/*
1694 * Formulate a normal query, send, and await answer.
1695 * Returned answer is placed in supplied buffer "answer".
1696 * Perform preliminary check of answer, returning success only
1697 * if no error is indicated and the answer count is nonzero.
1698 * Return the size of the response on success, -1 on error.
1699 * Error number is left in h_errno.
1700 *
1701 * Caller must parse answer and determine whether it answers the question.
1702 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001703static int res_queryN(const char* name, /* domain name */ struct res_target* target,
1704 res_state res) {
1705 u_char buf[MAXPACKET];
1706 HEADER* hp;
1707 int n;
1708 struct res_target* t;
1709 int rcode;
1710 int ancount;
Bernie Innocenti55864192018-08-30 04:05:20 +09001711
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001712 assert(name != NULL);
1713 /* XXX: target may be NULL??? */
Bernie Innocenti55864192018-08-30 04:05:20 +09001714
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001715 rcode = NOERROR;
1716 ancount = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001717
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001718 for (t = target; t; t = t->next) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001719 u_char* answer;
1720 int anslen;
1721 u_int oflags;
Bernie Innocenti55864192018-08-30 04:05:20 +09001722
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001723 hp = (HEADER*) (void*) t->answer;
1724 oflags = res->_flags;
Bernie Innocenti55864192018-08-30 04:05:20 +09001725
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001726 again:
1727 hp->rcode = NOERROR; /* default */
Bernie Innocenti55864192018-08-30 04:05:20 +09001728
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001729 /* make it easier... */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001730 int cl = t->qclass;
1731 int type = t->qtype;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001732 answer = t->answer;
1733 anslen = t->anslen;
Bernie Innocenti55864192018-08-30 04:05:20 +09001734#ifdef DEBUG
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001735 if (res->options & RES_DEBUG) printf(";; res_nquery(%s, %d, %d)\n", name, cl, type);
Bernie Innocenti55864192018-08-30 04:05:20 +09001736#endif
1737
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001738 n = res_nmkquery(res, QUERY, name, cl, type, NULL, 0, NULL, buf, sizeof(buf));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001739 if (n > 0 && (res->_flags & RES_F_EDNS0ERR) == 0 &&
1740 (res->options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) != 0)
1741 n = res_nopt(res, n, buf, sizeof(buf), anslen);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001742 if (n <= 0) {
Bernie Innocenti55864192018-08-30 04:05:20 +09001743#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001744 if (res->options & RES_DEBUG) printf(";; res_nquery: mkquery failed\n");
Bernie Innocenti55864192018-08-30 04:05:20 +09001745#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001746 h_errno = NO_RECOVERY;
1747 return n;
1748 }
1749 n = res_nsend(res, buf, n, answer, anslen);
Bernie Innocenti55864192018-08-30 04:05:20 +09001750
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001751 if (n < 0 || hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
1752 rcode = hp->rcode; /* record most recent error */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001753 /* if the query choked with EDNS0, retry without EDNS0 */
1754 if ((res->options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) != 0 &&
1755 ((oflags ^ res->_flags) & RES_F_EDNS0ERR) != 0) {
1756 res->_flags |= RES_F_EDNS0ERR;
Bernie Innocenti55864192018-08-30 04:05:20 +09001757#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001758 if (res->options & RES_DEBUG) printf(";; res_nquery: retry without EDNS0\n");
Bernie Innocenti55864192018-08-30 04:05:20 +09001759#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001760 goto again;
1761 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001762#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001763 if (res->options & RES_DEBUG)
1764 printf(";; rcode = %u, ancount=%u\n", hp->rcode, ntohs(hp->ancount));
Bernie Innocenti55864192018-08-30 04:05:20 +09001765#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001766 continue;
1767 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001768
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001769 ancount += ntohs(hp->ancount);
Bernie Innocenti55864192018-08-30 04:05:20 +09001770
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001771 t->n = n;
1772 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001773
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001774 if (ancount == 0) {
1775 switch (rcode) {
1776 case NXDOMAIN:
1777 h_errno = HOST_NOT_FOUND;
1778 break;
1779 case SERVFAIL:
1780 h_errno = TRY_AGAIN;
1781 break;
1782 case NOERROR:
1783 h_errno = NO_DATA;
1784 break;
1785 case FORMERR:
1786 case NOTIMP:
1787 case REFUSED:
1788 default:
1789 h_errno = NO_RECOVERY;
1790 break;
1791 }
1792 return -1;
1793 }
1794 return ancount;
Bernie Innocenti55864192018-08-30 04:05:20 +09001795}
1796
1797/*
1798 * Formulate a normal query, send, and retrieve answer in supplied buffer.
1799 * Return the size of the response on success, -1 on error.
1800 * If enabled, implement search rules until answer or unrecoverable failure
1801 * is detected. Error code, if any, is left in h_errno.
1802 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001803static int res_searchN(const char* name, struct res_target* target, res_state res) {
1804 const char *cp, *const *domain;
1805 HEADER* hp;
1806 u_int dots;
1807 int trailing_dot, ret, saved_herrno;
1808 int got_nodata = 0, got_servfail = 0, tried_as_is = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001809
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001810 assert(name != NULL);
1811 assert(target != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +09001812
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001813 hp = (HEADER*) (void*) target->answer; /*XXX*/
Bernie Innocenti55864192018-08-30 04:05:20 +09001814
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001815 errno = 0;
1816 h_errno = HOST_NOT_FOUND; /* default, if we never query */
1817 dots = 0;
1818 for (cp = name; *cp; cp++) dots += (*cp == '.');
1819 trailing_dot = 0;
1820 if (cp > name && *--cp == '.') trailing_dot++;
Bernie Innocenti55864192018-08-30 04:05:20 +09001821
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001822 /*
1823 * If there are dots in the name already, let's just give it a try
1824 * 'as is'. The threshold can be set with the "ndots" option.
1825 */
1826 saved_herrno = -1;
1827 if (dots >= res->ndots) {
1828 ret = res_querydomainN(name, NULL, target, res);
1829 if (ret > 0) return (ret);
1830 saved_herrno = h_errno;
1831 tried_as_is++;
1832 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001833
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001834 /*
1835 * We do at least one level of search if
1836 * - there is no dot and RES_DEFNAME is set, or
1837 * - there is at least one dot, there is no trailing dot,
1838 * and RES_DNSRCH is set.
1839 */
1840 if ((!dots && (res->options & RES_DEFNAMES)) ||
1841 (dots && !trailing_dot && (res->options & RES_DNSRCH))) {
1842 int done = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001843
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001844 /* Unfortunately we need to set stuff up before
1845 * the domain stuff is tried. Will have a better
1846 * fix after thread pools are used.
1847 */
1848 _resolv_populate_res_for_net(res);
Bernie Innocenti55864192018-08-30 04:05:20 +09001849
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001850 for (domain = (const char* const*) res->dnsrch; *domain && !done; domain++) {
1851 ret = res_querydomainN(name, *domain, target, res);
1852 if (ret > 0) return ret;
Bernie Innocenti55864192018-08-30 04:05:20 +09001853
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001854 /*
1855 * If no server present, give up.
1856 * If name isn't found in this domain,
1857 * keep trying higher domains in the search list
1858 * (if that's enabled).
1859 * On a NO_DATA error, keep trying, otherwise
1860 * a wildcard entry of another type could keep us
1861 * from finding this entry higher in the domain.
1862 * If we get some other error (negative answer or
1863 * server failure), then stop searching up,
1864 * but try the input name below in case it's
1865 * fully-qualified.
1866 */
1867 if (errno == ECONNREFUSED) {
1868 h_errno = TRY_AGAIN;
1869 return -1;
1870 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001871
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001872 switch (h_errno) {
1873 case NO_DATA:
1874 got_nodata++;
Bernie Innocenti8bb94ba2018-10-10 22:30:12 +09001875 [[fallthrough]];
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001876 case HOST_NOT_FOUND:
1877 /* keep trying */
1878 break;
1879 case TRY_AGAIN:
1880 if (hp->rcode == SERVFAIL) {
1881 /* try next search element, if any */
1882 got_servfail++;
1883 break;
1884 }
Bernie Innocenti8bb94ba2018-10-10 22:30:12 +09001885 [[fallthrough]];
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001886 default:
1887 /* anything else implies that we're done */
1888 done++;
1889 }
1890 /*
1891 * if we got here for some reason other than DNSRCH,
1892 * we only wanted one iteration of the loop, so stop.
1893 */
1894 if (!(res->options & RES_DNSRCH)) done++;
1895 }
1896 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001897
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001898 /*
1899 * if we have not already tried the name "as is", do that now.
1900 * note that we do this regardless of how many dots were in the
1901 * name or whether it ends with a dot.
1902 */
1903 if (!tried_as_is) {
1904 ret = res_querydomainN(name, NULL, target, res);
1905 if (ret > 0) return ret;
1906 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001907
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001908 /*
1909 * if we got here, we didn't satisfy the search.
1910 * if we did an initial full query, return that query's h_errno
1911 * (note that we wouldn't be here if that query had succeeded).
1912 * else if we ever got a nodata, send that back as the reason.
1913 * else send back meaningless h_errno, that being the one from
1914 * the last DNSRCH we did.
1915 */
1916 if (saved_herrno != -1)
1917 h_errno = saved_herrno;
1918 else if (got_nodata)
1919 h_errno = NO_DATA;
1920 else if (got_servfail)
1921 h_errno = TRY_AGAIN;
1922 return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +09001923}
1924
1925/*
1926 * Perform a call on res_query on the concatenation of name and domain,
1927 * removing a trailing dot from name if domain is NULL.
1928 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001929static int res_querydomainN(const char* name, const char* domain, struct res_target* target,
1930 res_state res) {
1931 char nbuf[MAXDNAME];
1932 const char* longname = nbuf;
1933 size_t n, d;
Bernie Innocenti55864192018-08-30 04:05:20 +09001934
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001935 assert(name != NULL);
1936 /* XXX: target may be NULL??? */
Bernie Innocenti55864192018-08-30 04:05:20 +09001937
1938#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001939 if (res->options & RES_DEBUG)
1940 printf(";; res_querydomain(%s, %s)\n", name, domain ? domain : "<Nil>");
Bernie Innocenti55864192018-08-30 04:05:20 +09001941#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001942 if (domain == NULL) {
1943 /*
1944 * Check for trailing '.';
1945 * copy without '.' if present.
1946 */
1947 n = strlen(name);
1948 if (n + 1 > sizeof(nbuf)) {
1949 h_errno = NO_RECOVERY;
1950 return -1;
1951 }
1952 if (n > 0 && name[--n] == '.') {
1953 strncpy(nbuf, name, n);
1954 nbuf[n] = '\0';
1955 } else
1956 longname = name;
1957 } else {
1958 n = strlen(name);
1959 d = strlen(domain);
1960 if (n + 1 + d + 1 > sizeof(nbuf)) {
1961 h_errno = NO_RECOVERY;
1962 return -1;
1963 }
1964 snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain);
1965 }
1966 return res_queryN(longname, target, res);
Bernie Innocenti55864192018-08-30 04:05:20 +09001967}