blob: fbf69a0b0ac54ec8935b0f7d0e8379ba513c153a [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
Ken Chen15c805a2018-10-17 00:19:59 +0800628 if (inet_pton(afd->a_af, hostname, pton) == 1) {
629 if (pai->ai_family == afd->a_af || pai->ai_family == PF_UNSPEC /*?*/) {
630 GET_AI(cur->ai_next, afd, pton);
631 GET_PORT(cur->ai_next, servname);
632 if ((pai->ai_flags & AI_CANONNAME)) {
633 /*
634 * Set the numeric address itself as
635 * the canonical name, based on a
636 * clarification in rfc2553bis-03.
637 */
638 GET_CANONNAME(cur->ai_next, canonname);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900639 }
Ken Chen15c805a2018-10-17 00:19:59 +0800640 while (cur->ai_next) cur = cur->ai_next;
641 } else
642 ERR(EAI_FAMILY); /*xxx*/
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900643 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900644
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900645 *res = sentinel.ai_next;
646 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900647
648free:
649bad:
Bernie Innocentib050f9b2018-10-02 12:53:39 +0900650 freeaddrinfo(sentinel.ai_next);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900651 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900652}
653
654/*
655 * numeric hostname with scope
656 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900657static int explore_numeric_scope(const struct addrinfo* pai, const char* hostname,
658 const char* servname, struct addrinfo** res) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900659 const struct afd* afd;
660 struct addrinfo* cur;
661 int error;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900662 const char *cp, *scope, *addr;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900663 struct sockaddr_in6* sin6;
Bernie Innocenti55864192018-08-30 04:05:20 +0900664
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900665 assert(pai != NULL);
666 /* hostname may be NULL */
667 /* servname may be NULL */
668 assert(res != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900669
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900670 /*
671 * if the servname does not match socktype/protocol, ignore it.
672 */
673 if (get_portmatch(pai, servname) != 0) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900674
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900675 afd = find_afd(pai->ai_family);
676 if (afd == NULL) return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900677
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900678 if (!afd->a_scoped) return explore_numeric(pai, hostname, servname, res, hostname);
Bernie Innocenti55864192018-08-30 04:05:20 +0900679
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900680 cp = strchr(hostname, SCOPE_DELIMITER);
681 if (cp == NULL) return explore_numeric(pai, hostname, servname, res, hostname);
Bernie Innocenti55864192018-08-30 04:05:20 +0900682
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900683 /*
684 * Handle special case of <scoped_address><delimiter><scope id>
685 */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900686 char* hostname2 = strdup(hostname);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900687 if (hostname2 == NULL) return EAI_MEMORY;
688 /* terminate at the delimiter */
689 hostname2[cp - hostname] = '\0';
690 addr = hostname2;
691 scope = cp + 1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900692
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900693 error = explore_numeric(pai, addr, servname, res, hostname);
694 if (error == 0) {
695 u_int32_t scopeid;
Bernie Innocenti55864192018-08-30 04:05:20 +0900696
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900697 for (cur = *res; cur; cur = cur->ai_next) {
698 if (cur->ai_family != AF_INET6) continue;
699 sin6 = (struct sockaddr_in6*) (void*) cur->ai_addr;
700 if (ip6_str2scopeid(scope, sin6, &scopeid) == -1) {
701 free(hostname2);
702 return (EAI_NODATA); /* XXX: is return OK? */
703 }
704 sin6->sin6_scope_id = scopeid;
705 }
706 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900707
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900708 free(hostname2);
Bernie Innocenti55864192018-08-30 04:05:20 +0900709
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900710 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900711}
712
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900713static int get_canonname(const struct addrinfo* pai, struct addrinfo* ai, const char* str) {
714 assert(pai != NULL);
715 assert(ai != NULL);
716 assert(str != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900717
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900718 if ((pai->ai_flags & AI_CANONNAME) != 0) {
719 ai->ai_canonname = strdup(str);
720 if (ai->ai_canonname == NULL) return EAI_MEMORY;
721 }
722 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900723}
724
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900725static struct addrinfo* get_ai(const struct addrinfo* pai, const struct afd* afd,
726 const char* addr) {
727 char* p;
728 struct addrinfo* ai;
Bernie Innocenti55864192018-08-30 04:05:20 +0900729
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900730 assert(pai != NULL);
731 assert(afd != NULL);
732 assert(addr != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900733
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900734 ai = (struct addrinfo*) malloc(sizeof(struct addrinfo) + (afd->a_socklen));
735 if (ai == NULL) return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900736
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900737 memcpy(ai, pai, sizeof(struct addrinfo));
738 ai->ai_addr = (struct sockaddr*) (void*) (ai + 1);
739 memset(ai->ai_addr, 0, (size_t) afd->a_socklen);
Bernie Innocenti55864192018-08-30 04:05:20 +0900740
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900741 ai->ai_addrlen = afd->a_socklen;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900742 ai->ai_addr->sa_family = ai->ai_family = afd->a_af;
743 p = (char*) (void*) (ai->ai_addr);
744 memcpy(p + afd->a_off, addr, (size_t) afd->a_addrlen);
745 return ai;
Bernie Innocenti55864192018-08-30 04:05:20 +0900746}
747
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900748static int get_portmatch(const struct addrinfo* ai, const char* servname) {
749 assert(ai != NULL);
750 /* servname may be NULL */
Bernie Innocenti55864192018-08-30 04:05:20 +0900751
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900752 return get_port(ai, servname, 1);
Bernie Innocenti55864192018-08-30 04:05:20 +0900753}
754
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900755static int get_port(const struct addrinfo* ai, const char* servname, int matchonly) {
756 const char* proto;
757 struct servent* sp;
758 int port;
759 int allownumeric;
Bernie Innocenti55864192018-08-30 04:05:20 +0900760
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900761 assert(ai != NULL);
762 /* servname may be NULL */
Bernie Innocenti55864192018-08-30 04:05:20 +0900763
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900764 if (servname == NULL) return 0;
765 switch (ai->ai_family) {
766 case AF_INET:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900767 case AF_INET6:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900768 break;
769 default:
770 return 0;
771 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900772
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900773 switch (ai->ai_socktype) {
774 case SOCK_RAW:
775 return EAI_SERVICE;
776 case SOCK_DGRAM:
777 case SOCK_STREAM:
778 allownumeric = 1;
779 break;
780 case ANY:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900781 allownumeric = 1;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900782 break;
783 default:
784 return EAI_SOCKTYPE;
785 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900786
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900787 port = str2number(servname);
788 if (port >= 0) {
789 if (!allownumeric) return EAI_SERVICE;
790 if (port < 0 || port > 65535) return EAI_SERVICE;
791 port = htons(port);
792 } else {
793 if (ai->ai_flags & AI_NUMERICSERV) return EAI_NONAME;
Bernie Innocenti55864192018-08-30 04:05:20 +0900794
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900795 switch (ai->ai_socktype) {
796 case SOCK_DGRAM:
797 proto = "udp";
798 break;
799 case SOCK_STREAM:
800 proto = "tcp";
801 break;
802 default:
803 proto = NULL;
804 break;
805 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900806
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900807 if ((sp = getservbyname(servname, proto)) == NULL) return EAI_SERVICE;
808 port = sp->s_port;
809 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900810
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900811 if (!matchonly) {
812 switch (ai->ai_family) {
813 case AF_INET:
814 ((struct sockaddr_in*) (void*) ai->ai_addr)->sin_port = port;
815 break;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900816 case AF_INET6:
817 ((struct sockaddr_in6*) (void*) ai->ai_addr)->sin6_port = port;
818 break;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900819 }
820 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900821
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900822 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900823}
824
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900825static const struct afd* find_afd(int af) {
826 const struct afd* afd;
Bernie Innocenti55864192018-08-30 04:05:20 +0900827
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900828 if (af == PF_UNSPEC) return NULL;
829 for (afd = afdl; afd->a_af; afd++) {
830 if (afd->a_af == af) return afd;
831 }
832 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900833}
834
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900835// Convert a string to a scope identifier.
836static int ip6_str2scopeid(const char* scope, struct sockaddr_in6* sin6, u_int32_t* scopeid) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900837 u_long lscopeid;
838 struct in6_addr* a6;
839 char* ep;
Bernie Innocenti55864192018-08-30 04:05:20 +0900840
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900841 assert(scope != NULL);
842 assert(sin6 != NULL);
843 assert(scopeid != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900844
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900845 a6 = &sin6->sin6_addr;
Bernie Innocenti55864192018-08-30 04:05:20 +0900846
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900847 /* empty scopeid portion is invalid */
848 if (*scope == '\0') return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900849
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900850 if (IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) {
851 /*
852 * We currently assume a one-to-one mapping between links
853 * and interfaces, so we simply use interface indices for
854 * like-local scopes.
855 */
856 *scopeid = if_nametoindex(scope);
857 if (*scopeid == 0) goto trynumeric;
858 return 0;
859 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900860
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900861 /* still unclear about literal, allow numeric only - placeholder */
862 if (IN6_IS_ADDR_SITELOCAL(a6) || IN6_IS_ADDR_MC_SITELOCAL(a6)) goto trynumeric;
863 if (IN6_IS_ADDR_MC_ORGLOCAL(a6))
864 goto trynumeric;
865 else
866 goto trynumeric; /* global */
Bernie Innocenti55864192018-08-30 04:05:20 +0900867
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900868 /* try to convert to a numeric id as a last resort */
869trynumeric:
870 errno = 0;
871 lscopeid = strtoul(scope, &ep, 10);
872 *scopeid = (u_int32_t)(lscopeid & 0xffffffffUL);
873 if (errno == 0 && ep && *ep == '\0' && *scopeid == lscopeid)
874 return 0;
875 else
876 return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900877}
Bernie Innocenti55864192018-08-30 04:05:20 +0900878
879/* code duplicate with gethnamaddr.c */
880
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900881static const char AskedForGot[] = "gethostby*.getanswer: asked for \"%s\", got \"%s\"";
Bernie Innocenti55864192018-08-30 04:05:20 +0900882
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900883#define BOUNDED_INCR(x) \
884 do { \
885 BOUNDS_CHECK(cp, x); \
886 cp += (x); \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900887 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900888
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900889#define BOUNDS_CHECK(ptr, count) \
890 do { \
891 if (eom - (ptr) < (count)) { \
892 h_errno = NO_RECOVERY; \
893 return NULL; \
894 } \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900895 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900896
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900897static struct addrinfo* getanswer(const querybuf* answer, int anslen, const char* qname, int qtype,
898 const struct addrinfo* pai) {
899 struct addrinfo sentinel, *cur;
900 struct addrinfo ai;
901 const struct afd* afd;
902 char* canonname;
903 const HEADER* hp;
904 const u_char* cp;
905 int n;
906 const u_char* eom;
907 char *bp, *ep;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900908 int type, ancount, qdcount;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900909 int haveanswer, had_error;
910 char tbuf[MAXDNAME];
911 int (*name_ok)(const char*);
912 char hostbuf[8 * 1024];
Bernie Innocenti55864192018-08-30 04:05:20 +0900913
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900914 assert(answer != NULL);
915 assert(qname != NULL);
916 assert(pai != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900917
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900918 memset(&sentinel, 0, sizeof(sentinel));
919 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +0900920
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900921 canonname = NULL;
922 eom = answer->buf + anslen;
923 switch (qtype) {
924 case T_A:
925 case T_AAAA:
926 case T_ANY: /*use T_ANY only for T_A/T_AAAA lookup*/
927 name_ok = res_hnok;
928 break;
929 default:
930 return NULL; /* XXX should be abort(); */
931 }
932 /*
933 * find first satisfactory answer
934 */
935 hp = &answer->hdr;
936 ancount = ntohs(hp->ancount);
937 qdcount = ntohs(hp->qdcount);
938 bp = hostbuf;
939 ep = hostbuf + sizeof hostbuf;
940 cp = answer->buf;
941 BOUNDED_INCR(HFIXEDSZ);
942 if (qdcount != 1) {
943 h_errno = NO_RECOVERY;
944 return (NULL);
945 }
946 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
947 if ((n < 0) || !(*name_ok)(bp)) {
948 h_errno = NO_RECOVERY;
949 return (NULL);
950 }
951 BOUNDED_INCR(n + QFIXEDSZ);
952 if (qtype == T_A || qtype == T_AAAA || qtype == T_ANY) {
953 /* res_send() has already verified that the query name is the
954 * same as the one we sent; this just gets the expanded name
955 * (i.e., with the succeeding search-domain tacked on).
956 */
957 n = strlen(bp) + 1; /* for the \0 */
958 if (n >= MAXHOSTNAMELEN) {
959 h_errno = NO_RECOVERY;
960 return (NULL);
961 }
962 canonname = bp;
963 bp += n;
964 /* The qname can be abbreviated, but h_name is now absolute. */
965 qname = canonname;
966 }
967 haveanswer = 0;
968 had_error = 0;
969 while (ancount-- > 0 && cp < eom && !had_error) {
970 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
971 if ((n < 0) || !(*name_ok)(bp)) {
972 had_error++;
973 continue;
974 }
975 cp += n; /* name */
976 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
Bernie Innocentiee1b85b2018-09-25 14:23:19 +0900977 type = ns_get16(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900978 cp += INT16SZ; /* type */
Bernie Innocentiee1b85b2018-09-25 14:23:19 +0900979 int cl = ns_get16(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900980 cp += INT16SZ + INT32SZ; /* class, TTL */
Bernie Innocentiee1b85b2018-09-25 14:23:19 +0900981 n = ns_get16(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900982 cp += INT16SZ; /* len */
983 BOUNDS_CHECK(cp, n);
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900984 if (cl != C_IN) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900985 /* XXX - debug? syslog? */
986 cp += n;
987 continue; /* XXX - had_error++ ? */
988 }
989 if ((qtype == T_A || qtype == T_AAAA || qtype == T_ANY) && type == T_CNAME) {
990 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
991 if ((n < 0) || !(*name_ok)(tbuf)) {
992 had_error++;
993 continue;
994 }
995 cp += n;
996 /* Get canonical name. */
997 n = strlen(tbuf) + 1; /* for the \0 */
998 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
999 had_error++;
1000 continue;
1001 }
1002 strlcpy(bp, tbuf, (size_t)(ep - bp));
1003 canonname = bp;
1004 bp += n;
1005 continue;
1006 }
1007 if (qtype == T_ANY) {
1008 if (!(type == T_A || type == T_AAAA)) {
1009 cp += n;
1010 continue;
1011 }
1012 } else if (type != qtype) {
1013 if (type != T_KEY && type != T_SIG)
1014 syslog(LOG_NOTICE | LOG_AUTH,
1015 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"", qname,
1016 p_class(C_IN), p_type(qtype), p_type(type));
1017 cp += n;
1018 continue; /* XXX - had_error++ ? */
1019 }
1020 switch (type) {
1021 case T_A:
1022 case T_AAAA:
1023 if (strcasecmp(canonname, bp) != 0) {
1024 syslog(LOG_NOTICE | LOG_AUTH, AskedForGot, canonname, bp);
1025 cp += n;
1026 continue; /* XXX - had_error++ ? */
1027 }
1028 if (type == T_A && n != INADDRSZ) {
1029 cp += n;
1030 continue;
1031 }
1032 if (type == T_AAAA && n != IN6ADDRSZ) {
1033 cp += n;
1034 continue;
1035 }
1036 if (type == T_AAAA) {
1037 struct in6_addr in6;
1038 memcpy(&in6, cp, IN6ADDRSZ);
1039 if (IN6_IS_ADDR_V4MAPPED(&in6)) {
1040 cp += n;
1041 continue;
1042 }
1043 }
1044 if (!haveanswer) {
1045 int nn;
Bernie Innocenti55864192018-08-30 04:05:20 +09001046
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001047 canonname = bp;
1048 nn = strlen(bp) + 1; /* for the \0 */
1049 bp += nn;
1050 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001051
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001052 /* don't overwrite pai */
1053 ai = *pai;
1054 ai.ai_family = (type == T_A) ? AF_INET : AF_INET6;
1055 afd = find_afd(ai.ai_family);
1056 if (afd == NULL) {
1057 cp += n;
1058 continue;
1059 }
1060 cur->ai_next = get_ai(&ai, afd, (const char*) cp);
1061 if (cur->ai_next == NULL) had_error++;
1062 while (cur && cur->ai_next) cur = cur->ai_next;
1063 cp += n;
1064 break;
1065 default:
1066 abort();
1067 }
1068 if (!had_error) haveanswer++;
1069 }
1070 if (haveanswer) {
1071 if (!canonname)
1072 (void) get_canonname(pai, sentinel.ai_next, qname);
1073 else
1074 (void) get_canonname(pai, sentinel.ai_next, canonname);
1075 h_errno = NETDB_SUCCESS;
1076 return sentinel.ai_next;
1077 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001078
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001079 h_errno = NO_RECOVERY;
1080 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001081}
1082
1083struct addrinfo_sort_elem {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001084 struct addrinfo* ai;
1085 int has_src_addr;
1086 sockaddr_union src_addr;
1087 int original_order;
Bernie Innocenti55864192018-08-30 04:05:20 +09001088};
1089
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001090static int _get_scope(const struct sockaddr* addr) {
1091 if (addr->sa_family == AF_INET6) {
1092 const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
1093 if (IN6_IS_ADDR_MULTICAST(&addr6->sin6_addr)) {
1094 return IPV6_ADDR_MC_SCOPE(&addr6->sin6_addr);
1095 } else if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr) ||
1096 IN6_IS_ADDR_LINKLOCAL(&addr6->sin6_addr)) {
1097 /*
1098 * RFC 4291 section 2.5.3 says loopback is to be treated as having
1099 * link-local scope.
1100 */
1101 return IPV6_ADDR_SCOPE_LINKLOCAL;
1102 } else if (IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr)) {
1103 return IPV6_ADDR_SCOPE_SITELOCAL;
1104 } else {
1105 return IPV6_ADDR_SCOPE_GLOBAL;
1106 }
1107 } else if (addr->sa_family == AF_INET) {
1108 const struct sockaddr_in* addr4 = (const struct sockaddr_in*) addr;
1109 unsigned long int na = ntohl(addr4->sin_addr.s_addr);
Bernie Innocenti55864192018-08-30 04:05:20 +09001110
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001111 if (IN_LOOPBACK(na) || /* 127.0.0.0/8 */
1112 (na & 0xffff0000) == 0xa9fe0000) { /* 169.254.0.0/16 */
1113 return IPV6_ADDR_SCOPE_LINKLOCAL;
1114 } else {
1115 /*
1116 * RFC 6724 section 3.2. Other IPv4 addresses, including private addresses
1117 * and shared addresses (100.64.0.0/10), are assigned global scope.
1118 */
1119 return IPV6_ADDR_SCOPE_GLOBAL;
1120 }
1121 } else {
1122 /*
1123 * This should never happen.
1124 * Return a scope with low priority as a last resort.
1125 */
1126 return IPV6_ADDR_SCOPE_NODELOCAL;
1127 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001128}
1129
1130/* These macros are modelled after the ones in <netinet/in6.h>. */
1131
1132/* RFC 4380, section 2.6 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001133#define IN6_IS_ADDR_TEREDO(a) \
1134 ((*(const uint32_t*) (const void*) (&(a)->s6_addr[0]) == ntohl(0x20010000)))
Bernie Innocenti55864192018-08-30 04:05:20 +09001135
1136/* RFC 3056, section 2. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001137#define IN6_IS_ADDR_6TO4(a) (((a)->s6_addr[0] == 0x20) && ((a)->s6_addr[1] == 0x02))
Bernie Innocenti55864192018-08-30 04:05:20 +09001138
1139/* 6bone testing address area (3ffe::/16), deprecated in RFC 3701. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001140#define IN6_IS_ADDR_6BONE(a) (((a)->s6_addr[0] == 0x3f) && ((a)->s6_addr[1] == 0xfe))
Bernie Innocenti55864192018-08-30 04:05:20 +09001141
1142/*
1143 * Get the label for a given IPv4/IPv6 address.
1144 * RFC 6724, section 2.1.
1145 */
1146
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001147static int _get_label(const struct sockaddr* addr) {
1148 if (addr->sa_family == AF_INET) {
1149 return 4;
1150 } else if (addr->sa_family == AF_INET6) {
1151 const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
1152 if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr)) {
1153 return 0;
1154 } else if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr)) {
1155 return 4;
1156 } else if (IN6_IS_ADDR_6TO4(&addr6->sin6_addr)) {
1157 return 2;
1158 } else if (IN6_IS_ADDR_TEREDO(&addr6->sin6_addr)) {
1159 return 5;
1160 } else if (IN6_IS_ADDR_ULA(&addr6->sin6_addr)) {
1161 return 13;
1162 } else if (IN6_IS_ADDR_V4COMPAT(&addr6->sin6_addr)) {
1163 return 3;
1164 } else if (IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr)) {
1165 return 11;
1166 } else if (IN6_IS_ADDR_6BONE(&addr6->sin6_addr)) {
1167 return 12;
1168 } else {
1169 /* All other IPv6 addresses, including global unicast addresses. */
1170 return 1;
1171 }
1172 } else {
1173 /*
1174 * This should never happen.
1175 * Return a semi-random label as a last resort.
1176 */
1177 return 1;
1178 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001179}
1180
1181/*
1182 * Get the precedence for a given IPv4/IPv6 address.
1183 * RFC 6724, section 2.1.
1184 */
1185
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001186static int _get_precedence(const struct sockaddr* addr) {
1187 if (addr->sa_family == AF_INET) {
1188 return 35;
1189 } else if (addr->sa_family == AF_INET6) {
1190 const struct sockaddr_in6* addr6 = (const struct sockaddr_in6*) addr;
1191 if (IN6_IS_ADDR_LOOPBACK(&addr6->sin6_addr)) {
1192 return 50;
1193 } else if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr)) {
1194 return 35;
1195 } else if (IN6_IS_ADDR_6TO4(&addr6->sin6_addr)) {
1196 return 30;
1197 } else if (IN6_IS_ADDR_TEREDO(&addr6->sin6_addr)) {
1198 return 5;
1199 } else if (IN6_IS_ADDR_ULA(&addr6->sin6_addr)) {
1200 return 3;
1201 } else if (IN6_IS_ADDR_V4COMPAT(&addr6->sin6_addr) ||
1202 IN6_IS_ADDR_SITELOCAL(&addr6->sin6_addr) ||
1203 IN6_IS_ADDR_6BONE(&addr6->sin6_addr)) {
1204 return 1;
1205 } else {
1206 /* All other IPv6 addresses, including global unicast addresses. */
1207 return 40;
1208 }
1209 } else {
1210 return 1;
1211 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001212}
1213
1214/*
1215 * Find number of matching initial bits between the two addresses a1 and a2.
1216 */
1217
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001218static int _common_prefix_len(const struct in6_addr* a1, const struct in6_addr* a2) {
1219 const char* p1 = (const char*) a1;
1220 const char* p2 = (const char*) a2;
1221 unsigned i;
Bernie Innocenti55864192018-08-30 04:05:20 +09001222
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001223 for (i = 0; i < sizeof(*a1); ++i) {
1224 int x, j;
Bernie Innocenti55864192018-08-30 04:05:20 +09001225
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001226 if (p1[i] == p2[i]) {
1227 continue;
1228 }
1229 x = p1[i] ^ p2[i];
1230 for (j = 0; j < CHAR_BIT; ++j) {
1231 if (x & (1 << (CHAR_BIT - 1))) {
1232 return i * CHAR_BIT + j;
1233 }
1234 x <<= 1;
1235 }
1236 }
1237 return sizeof(*a1) * CHAR_BIT;
Bernie Innocenti55864192018-08-30 04:05:20 +09001238}
1239
1240/*
1241 * Compare two source/destination address pairs.
1242 * RFC 6724, section 6.
1243 */
1244
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001245static int _rfc6724_compare(const void* ptr1, const void* ptr2) {
1246 const struct addrinfo_sort_elem* a1 = (const struct addrinfo_sort_elem*) ptr1;
1247 const struct addrinfo_sort_elem* a2 = (const struct addrinfo_sort_elem*) ptr2;
1248 int scope_src1, scope_dst1, scope_match1;
1249 int scope_src2, scope_dst2, scope_match2;
1250 int label_src1, label_dst1, label_match1;
1251 int label_src2, label_dst2, label_match2;
1252 int precedence1, precedence2;
1253 int prefixlen1, prefixlen2;
Bernie Innocenti55864192018-08-30 04:05:20 +09001254
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001255 /* Rule 1: Avoid unusable destinations. */
1256 if (a1->has_src_addr != a2->has_src_addr) {
1257 return a2->has_src_addr - a1->has_src_addr;
1258 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001259
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001260 /* Rule 2: Prefer matching scope. */
1261 scope_src1 = _get_scope(&a1->src_addr.generic);
1262 scope_dst1 = _get_scope(a1->ai->ai_addr);
1263 scope_match1 = (scope_src1 == scope_dst1);
Bernie Innocenti55864192018-08-30 04:05:20 +09001264
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001265 scope_src2 = _get_scope(&a2->src_addr.generic);
1266 scope_dst2 = _get_scope(a2->ai->ai_addr);
1267 scope_match2 = (scope_src2 == scope_dst2);
Bernie Innocenti55864192018-08-30 04:05:20 +09001268
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001269 if (scope_match1 != scope_match2) {
1270 return scope_match2 - scope_match1;
1271 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001272
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001273 /*
1274 * Rule 3: Avoid deprecated addresses.
1275 * TODO(sesse): We don't currently have a good way of finding this.
1276 */
Bernie Innocenti55864192018-08-30 04:05:20 +09001277
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001278 /*
1279 * Rule 4: Prefer home addresses.
1280 * TODO(sesse): We don't currently have a good way of finding this.
1281 */
Bernie Innocenti55864192018-08-30 04:05:20 +09001282
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001283 /* Rule 5: Prefer matching label. */
1284 label_src1 = _get_label(&a1->src_addr.generic);
1285 label_dst1 = _get_label(a1->ai->ai_addr);
1286 label_match1 = (label_src1 == label_dst1);
Bernie Innocenti55864192018-08-30 04:05:20 +09001287
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001288 label_src2 = _get_label(&a2->src_addr.generic);
1289 label_dst2 = _get_label(a2->ai->ai_addr);
1290 label_match2 = (label_src2 == label_dst2);
Bernie Innocenti55864192018-08-30 04:05:20 +09001291
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001292 if (label_match1 != label_match2) {
1293 return label_match2 - label_match1;
1294 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001295
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001296 /* Rule 6: Prefer higher precedence. */
1297 precedence1 = _get_precedence(a1->ai->ai_addr);
1298 precedence2 = _get_precedence(a2->ai->ai_addr);
1299 if (precedence1 != precedence2) {
1300 return precedence2 - precedence1;
1301 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001302
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001303 /*
1304 * Rule 7: Prefer native transport.
1305 * TODO(sesse): We don't currently have a good way of finding this.
1306 */
Bernie Innocenti55864192018-08-30 04:05:20 +09001307
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001308 /* Rule 8: Prefer smaller scope. */
1309 if (scope_dst1 != scope_dst2) {
1310 return scope_dst1 - scope_dst2;
1311 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001312
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001313 /*
1314 * Rule 9: Use longest matching prefix.
1315 * We implement this for IPv6 only, as the rules in RFC 6724 don't seem
1316 * to work very well directly applied to IPv4. (glibc uses information from
1317 * the routing table for a custom IPv4 implementation here.)
1318 */
1319 if (a1->has_src_addr && a1->ai->ai_addr->sa_family == AF_INET6 && a2->has_src_addr &&
1320 a2->ai->ai_addr->sa_family == AF_INET6) {
1321 const struct sockaddr_in6* a1_src = &a1->src_addr.in6;
1322 const struct sockaddr_in6* a1_dst = (const struct sockaddr_in6*) a1->ai->ai_addr;
1323 const struct sockaddr_in6* a2_src = &a2->src_addr.in6;
1324 const struct sockaddr_in6* a2_dst = (const struct sockaddr_in6*) a2->ai->ai_addr;
1325 prefixlen1 = _common_prefix_len(&a1_src->sin6_addr, &a1_dst->sin6_addr);
1326 prefixlen2 = _common_prefix_len(&a2_src->sin6_addr, &a2_dst->sin6_addr);
1327 if (prefixlen1 != prefixlen2) {
1328 return prefixlen2 - prefixlen1;
1329 }
1330 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001331
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001332 /*
1333 * Rule 10: Leave the order unchanged.
1334 * We need this since qsort() is not necessarily stable.
1335 */
1336 return a1->original_order - a2->original_order;
Bernie Innocenti55864192018-08-30 04:05:20 +09001337}
1338
1339/*
1340 * Find the source address that will be used if trying to connect to the given
1341 * address. src_addr must be large enough to hold a struct sockaddr_in6.
1342 *
1343 * Returns 1 if a source address was found, 0 if the address is unreachable,
1344 * and -1 if a fatal error occurred. If 0 or -1, the contents of src_addr are
1345 * undefined.
1346 */
1347
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001348static int _find_src_addr(const struct sockaddr* addr, struct sockaddr* src_addr, unsigned mark,
1349 uid_t uid) {
1350 int sock;
1351 int ret;
1352 socklen_t len;
Bernie Innocenti55864192018-08-30 04:05:20 +09001353
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001354 switch (addr->sa_family) {
1355 case AF_INET:
1356 len = sizeof(struct sockaddr_in);
1357 break;
1358 case AF_INET6:
1359 len = sizeof(struct sockaddr_in6);
1360 break;
1361 default:
1362 /* No known usable source address for non-INET families. */
1363 return 0;
1364 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001365
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001366 sock = socket(addr->sa_family, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP);
1367 if (sock == -1) {
1368 if (errno == EAFNOSUPPORT) {
1369 return 0;
1370 } else {
1371 return -1;
1372 }
1373 }
1374 if (mark != MARK_UNSET && setsockopt(sock, SOL_SOCKET, SO_MARK, &mark, sizeof(mark)) < 0) {
1375 close(sock);
1376 return 0;
1377 }
1378 if (uid > 0 && uid != NET_CONTEXT_INVALID_UID && fchown(sock, uid, (gid_t) -1) < 0) {
1379 close(sock);
1380 return 0;
1381 }
1382 do {
Bernie Innocentif89b3512018-08-30 07:34:37 +09001383 ret = connect(sock, addr, len);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001384 } while (ret == -1 && errno == EINTR);
Bernie Innocenti55864192018-08-30 04:05:20 +09001385
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001386 if (ret == -1) {
1387 close(sock);
1388 return 0;
1389 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001390
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001391 if (src_addr && getsockname(sock, src_addr, &len) == -1) {
1392 close(sock);
1393 return -1;
1394 }
1395 close(sock);
1396 return 1;
Bernie Innocenti55864192018-08-30 04:05:20 +09001397}
1398
1399/*
1400 * Sort the linked list starting at sentinel->ai_next in RFC6724 order.
1401 * Will leave the list unchanged if an error occurs.
1402 */
1403
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001404static void _rfc6724_sort(struct addrinfo* list_sentinel, unsigned mark, uid_t uid) {
1405 struct addrinfo* cur;
1406 int nelem = 0, i;
1407 struct addrinfo_sort_elem* elems;
Bernie Innocenti55864192018-08-30 04:05:20 +09001408
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001409 cur = list_sentinel->ai_next;
1410 while (cur) {
1411 ++nelem;
1412 cur = cur->ai_next;
1413 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001414
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001415 elems = (struct addrinfo_sort_elem*) malloc(nelem * sizeof(struct addrinfo_sort_elem));
1416 if (elems == NULL) {
1417 goto error;
1418 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001419
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001420 /*
1421 * Convert the linked list to an array that also contains the candidate
1422 * source address for each destination address.
1423 */
1424 for (i = 0, cur = list_sentinel->ai_next; i < nelem; ++i, cur = cur->ai_next) {
1425 int has_src_addr;
1426 assert(cur != NULL);
1427 elems[i].ai = cur;
1428 elems[i].original_order = i;
Bernie Innocenti55864192018-08-30 04:05:20 +09001429
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001430 has_src_addr = _find_src_addr(cur->ai_addr, &elems[i].src_addr.generic, mark, uid);
1431 if (has_src_addr == -1) {
1432 goto error;
1433 }
1434 elems[i].has_src_addr = has_src_addr;
1435 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001436
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001437 /* Sort the addresses, and rearrange the linked list so it matches the sorted order. */
1438 qsort((void*) elems, nelem, sizeof(struct addrinfo_sort_elem), _rfc6724_compare);
Bernie Innocenti55864192018-08-30 04:05:20 +09001439
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001440 list_sentinel->ai_next = elems[0].ai;
1441 for (i = 0; i < nelem - 1; ++i) {
1442 elems[i].ai->ai_next = elems[i + 1].ai;
1443 }
1444 elems[nelem - 1].ai->ai_next = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001445
1446error:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001447 free(elems);
Bernie Innocenti55864192018-08-30 04:05:20 +09001448}
1449
Bernie Innocenti948f6572018-09-12 21:32:42 +09001450static int dns_getaddrinfo(const char* name, const addrinfo* pai,
1451 const android_net_context* netcontext, addrinfo** rv) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001452 struct addrinfo* ai;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001453 struct addrinfo sentinel, *cur;
1454 struct res_target q, q2;
1455 res_state res;
Bernie Innocenti55864192018-08-30 04:05:20 +09001456
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001457 memset(&q, 0, sizeof(q));
1458 memset(&q2, 0, sizeof(q2));
1459 memset(&sentinel, 0, sizeof(sentinel));
1460 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +09001461
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001462 querybuf* buf = (querybuf*) malloc(sizeof(*buf));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001463 if (buf == NULL) {
1464 h_errno = NETDB_INTERNAL;
Bernie Innocenti948f6572018-09-12 21:32:42 +09001465 return EAI_MEMORY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001466 }
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001467 querybuf* buf2 = (querybuf*) malloc(sizeof(*buf2));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001468 if (buf2 == NULL) {
1469 free(buf);
1470 h_errno = NETDB_INTERNAL;
Bernie Innocenti948f6572018-09-12 21:32:42 +09001471 return EAI_MEMORY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001472 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001473
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001474 switch (pai->ai_family) {
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001475 case AF_UNSPEC: {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001476 /* prefer IPv6 */
1477 q.name = name;
1478 q.qclass = C_IN;
1479 q.answer = buf->buf;
1480 q.anslen = sizeof(buf->buf);
1481 int query_ipv6 = 1, query_ipv4 = 1;
1482 if (pai->ai_flags & AI_ADDRCONFIG) {
1483 query_ipv6 = _have_ipv6(netcontext->app_mark, netcontext->uid);
1484 query_ipv4 = _have_ipv4(netcontext->app_mark, netcontext->uid);
1485 }
1486 if (query_ipv6) {
1487 q.qtype = T_AAAA;
1488 if (query_ipv4) {
1489 q.next = &q2;
1490 q2.name = name;
1491 q2.qclass = C_IN;
1492 q2.qtype = T_A;
1493 q2.answer = buf2->buf;
1494 q2.anslen = sizeof(buf2->buf);
1495 }
1496 } else if (query_ipv4) {
1497 q.qtype = T_A;
1498 } else {
1499 free(buf);
1500 free(buf2);
Bernie Innocenti948f6572018-09-12 21:32:42 +09001501 return EAI_NODATA;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001502 }
1503 break;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001504 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001505 case AF_INET:
1506 q.name = name;
1507 q.qclass = C_IN;
1508 q.qtype = T_A;
1509 q.answer = buf->buf;
1510 q.anslen = sizeof(buf->buf);
1511 break;
1512 case AF_INET6:
1513 q.name = name;
1514 q.qclass = C_IN;
1515 q.qtype = T_AAAA;
1516 q.answer = buf->buf;
1517 q.anslen = sizeof(buf->buf);
1518 break;
1519 default:
1520 free(buf);
1521 free(buf2);
Bernie Innocenti948f6572018-09-12 21:32:42 +09001522 return EAI_FAMILY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001523 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001524
Bernie Innocenti4acba1a2018-09-26 11:52:04 +09001525 res = res_get_state();
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001526 if (res == NULL) {
1527 free(buf);
1528 free(buf2);
Bernie Innocenti948f6572018-09-12 21:32:42 +09001529 return EAI_MEMORY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001530 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001531
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001532 /* this just sets our netid val in the thread private data so we don't have to
1533 * modify the api's all the way down to res_send.c's res_nsend. We could
1534 * fully populate the thread private data here, but if we get down there
1535 * and have a cache hit that would be wasted, so we do the rest there on miss
1536 */
1537 res_setnetcontext(res, netcontext);
1538 if (res_searchN(name, &q, res) < 0) {
Bernie Innocenti4acba1a2018-09-26 11:52:04 +09001539 res_put_state(res);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001540 free(buf);
1541 free(buf2);
Bernie Innocenti948f6572018-09-12 21:32:42 +09001542 return EAI_NODATA; // TODO: Decode error from h_errno like we do below
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001543 }
1544 ai = getanswer(buf, q.n, q.name, q.qtype, pai);
1545 if (ai) {
1546 cur->ai_next = ai;
1547 while (cur && cur->ai_next) cur = cur->ai_next;
1548 }
1549 if (q.next) {
1550 ai = getanswer(buf2, q2.n, q2.name, q2.qtype, pai);
1551 if (ai) cur->ai_next = ai;
1552 }
1553 free(buf);
1554 free(buf2);
1555 if (sentinel.ai_next == NULL) {
Bernie Innocenti4acba1a2018-09-26 11:52:04 +09001556 res_put_state(res);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001557 switch (h_errno) {
1558 case HOST_NOT_FOUND:
Bernie Innocenti948f6572018-09-12 21:32:42 +09001559 return EAI_NODATA;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001560 case TRY_AGAIN:
Bernie Innocenti948f6572018-09-12 21:32:42 +09001561 return EAI_AGAIN;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001562 default:
Bernie Innocenti948f6572018-09-12 21:32:42 +09001563 return EAI_FAIL;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001564 }
1565 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001566
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001567 _rfc6724_sort(&sentinel, netcontext->app_mark, netcontext->uid);
Bernie Innocenti55864192018-08-30 04:05:20 +09001568
Bernie Innocenti4acba1a2018-09-26 11:52:04 +09001569 res_put_state(res);
Bernie Innocenti55864192018-08-30 04:05:20 +09001570
Bernie Innocenti948f6572018-09-12 21:32:42 +09001571 *rv = sentinel.ai_next;
1572 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001573}
1574
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001575static void _sethtent(FILE** hostf) {
1576 if (!*hostf)
1577 *hostf = fopen(_PATH_HOSTS, "re");
1578 else
1579 rewind(*hostf);
Bernie Innocenti55864192018-08-30 04:05:20 +09001580}
1581
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001582static void _endhtent(FILE** hostf) {
1583 if (*hostf) {
1584 (void) fclose(*hostf);
1585 *hostf = NULL;
1586 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001587}
1588
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001589static struct addrinfo* _gethtent(FILE** hostf, const char* name, const struct addrinfo* pai) {
1590 char* p;
1591 char *cp, *tname, *cname;
1592 struct addrinfo hints, *res0, *res;
1593 int error;
1594 const char* addr;
1595 char hostbuf[8 * 1024];
Bernie Innocenti55864192018-08-30 04:05:20 +09001596
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001597 assert(name != NULL);
1598 assert(pai != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +09001599
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001600 if (!*hostf && !(*hostf = fopen(_PATH_HOSTS, "re"))) return (NULL);
1601again:
1602 if (!(p = fgets(hostbuf, sizeof hostbuf, *hostf))) return (NULL);
1603 if (*p == '#') goto again;
1604 if (!(cp = strpbrk(p, "#\n"))) goto again;
1605 *cp = '\0';
1606 if (!(cp = strpbrk(p, " \t"))) goto again;
1607 *cp++ = '\0';
1608 addr = p;
1609 /* if this is not something we're looking for, skip it. */
1610 cname = NULL;
1611 while (cp && *cp) {
1612 if (*cp == ' ' || *cp == '\t') {
1613 cp++;
1614 continue;
1615 }
1616 if (!cname) cname = cp;
1617 tname = cp;
1618 if ((cp = strpbrk(cp, " \t")) != NULL) *cp++ = '\0';
1619 // fprintf(stderr, "\ttname = '%s'", tname);
1620 if (strcasecmp(name, tname) == 0) goto found;
1621 }
1622 goto again;
Bernie Innocenti55864192018-08-30 04:05:20 +09001623
1624found:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001625 hints = *pai;
1626 hints.ai_flags = AI_NUMERICHOST;
1627 error = getaddrinfo(addr, NULL, &hints, &res0);
1628 if (error) goto again;
1629 for (res = res0; res; res = res->ai_next) {
1630 /* cover it up */
1631 res->ai_flags = pai->ai_flags;
Bernie Innocenti55864192018-08-30 04:05:20 +09001632
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001633 if (pai->ai_flags & AI_CANONNAME) {
1634 if (get_canonname(pai, res, cname) != 0) {
1635 freeaddrinfo(res0);
1636 goto again;
1637 }
1638 }
1639 }
1640 return res0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001641}
1642
Bernie Innocenti948f6572018-09-12 21:32:42 +09001643static bool files_getaddrinfo(const char* name, const addrinfo* pai, addrinfo** res) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001644 struct addrinfo sentinel, *cur;
1645 struct addrinfo* p;
1646 FILE* hostf = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001647
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001648 memset(&sentinel, 0, sizeof(sentinel));
1649 cur = &sentinel;
Bernie Innocenti55864192018-08-30 04:05:20 +09001650
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001651 _sethtent(&hostf);
1652 while ((p = _gethtent(&hostf, name, pai)) != NULL) {
1653 cur->ai_next = p;
1654 while (cur && cur->ai_next) cur = cur->ai_next;
1655 }
1656 _endhtent(&hostf);
Bernie Innocenti55864192018-08-30 04:05:20 +09001657
Bernie Innocenti948f6572018-09-12 21:32:42 +09001658 *res = sentinel.ai_next;
1659 return sentinel.ai_next != NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +09001660}
1661
1662/* resolver logic */
1663
1664/*
1665 * Formulate a normal query, send, and await answer.
1666 * Returned answer is placed in supplied buffer "answer".
1667 * Perform preliminary check of answer, returning success only
1668 * if no error is indicated and the answer count is nonzero.
1669 * Return the size of the response on success, -1 on error.
1670 * Error number is left in h_errno.
1671 *
1672 * Caller must parse answer and determine whether it answers the question.
1673 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001674static int res_queryN(const char* name, /* domain name */ struct res_target* target,
1675 res_state res) {
1676 u_char buf[MAXPACKET];
1677 HEADER* hp;
1678 int n;
1679 struct res_target* t;
1680 int rcode;
1681 int ancount;
Bernie Innocenti55864192018-08-30 04:05:20 +09001682
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001683 assert(name != NULL);
1684 /* XXX: target may be NULL??? */
Bernie Innocenti55864192018-08-30 04:05:20 +09001685
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001686 rcode = NOERROR;
1687 ancount = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001688
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001689 for (t = target; t; t = t->next) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001690 u_char* answer;
1691 int anslen;
1692 u_int oflags;
Bernie Innocenti55864192018-08-30 04:05:20 +09001693
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001694 hp = (HEADER*) (void*) t->answer;
1695 oflags = res->_flags;
Bernie Innocenti55864192018-08-30 04:05:20 +09001696
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001697 again:
1698 hp->rcode = NOERROR; /* default */
Bernie Innocenti55864192018-08-30 04:05:20 +09001699
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001700 /* make it easier... */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001701 int cl = t->qclass;
1702 int type = t->qtype;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001703 answer = t->answer;
1704 anslen = t->anslen;
Bernie Innocenti55864192018-08-30 04:05:20 +09001705#ifdef DEBUG
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001706 if (res->options & RES_DEBUG) printf(";; res_nquery(%s, %d, %d)\n", name, cl, type);
Bernie Innocenti55864192018-08-30 04:05:20 +09001707#endif
1708
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +09001709 n = res_nmkquery(res, QUERY, name, cl, type, NULL, 0, NULL, buf, sizeof(buf));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001710 if (n > 0 && (res->_flags & RES_F_EDNS0ERR) == 0 &&
1711 (res->options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) != 0)
1712 n = res_nopt(res, n, buf, sizeof(buf), anslen);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001713 if (n <= 0) {
Bernie Innocenti55864192018-08-30 04:05:20 +09001714#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001715 if (res->options & RES_DEBUG) printf(";; res_nquery: mkquery failed\n");
Bernie Innocenti55864192018-08-30 04:05:20 +09001716#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001717 h_errno = NO_RECOVERY;
1718 return n;
1719 }
1720 n = res_nsend(res, buf, n, answer, anslen);
Bernie Innocenti55864192018-08-30 04:05:20 +09001721
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001722 if (n < 0 || hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
1723 rcode = hp->rcode; /* record most recent error */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001724 /* if the query choked with EDNS0, retry without EDNS0 */
1725 if ((res->options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) != 0 &&
1726 ((oflags ^ res->_flags) & RES_F_EDNS0ERR) != 0) {
1727 res->_flags |= RES_F_EDNS0ERR;
Bernie Innocenti55864192018-08-30 04:05:20 +09001728#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001729 if (res->options & RES_DEBUG) printf(";; res_nquery: retry without EDNS0\n");
Bernie Innocenti55864192018-08-30 04:05:20 +09001730#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001731 goto again;
1732 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001733#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001734 if (res->options & RES_DEBUG)
1735 printf(";; rcode = %u, ancount=%u\n", hp->rcode, ntohs(hp->ancount));
Bernie Innocenti55864192018-08-30 04:05:20 +09001736#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001737 continue;
1738 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001739
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001740 ancount += ntohs(hp->ancount);
Bernie Innocenti55864192018-08-30 04:05:20 +09001741
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001742 t->n = n;
1743 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001744
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001745 if (ancount == 0) {
1746 switch (rcode) {
1747 case NXDOMAIN:
1748 h_errno = HOST_NOT_FOUND;
1749 break;
1750 case SERVFAIL:
1751 h_errno = TRY_AGAIN;
1752 break;
1753 case NOERROR:
1754 h_errno = NO_DATA;
1755 break;
1756 case FORMERR:
1757 case NOTIMP:
1758 case REFUSED:
1759 default:
1760 h_errno = NO_RECOVERY;
1761 break;
1762 }
1763 return -1;
1764 }
1765 return ancount;
Bernie Innocenti55864192018-08-30 04:05:20 +09001766}
1767
1768/*
1769 * Formulate a normal query, send, and retrieve answer in supplied buffer.
1770 * Return the size of the response on success, -1 on error.
1771 * If enabled, implement search rules until answer or unrecoverable failure
1772 * is detected. Error code, if any, is left in h_errno.
1773 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001774static int res_searchN(const char* name, struct res_target* target, res_state res) {
1775 const char *cp, *const *domain;
1776 HEADER* hp;
1777 u_int dots;
1778 int trailing_dot, ret, saved_herrno;
1779 int got_nodata = 0, got_servfail = 0, tried_as_is = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001780
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001781 assert(name != NULL);
1782 assert(target != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +09001783
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001784 hp = (HEADER*) (void*) target->answer; /*XXX*/
Bernie Innocenti55864192018-08-30 04:05:20 +09001785
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001786 errno = 0;
1787 h_errno = HOST_NOT_FOUND; /* default, if we never query */
1788 dots = 0;
1789 for (cp = name; *cp; cp++) dots += (*cp == '.');
1790 trailing_dot = 0;
1791 if (cp > name && *--cp == '.') trailing_dot++;
Bernie Innocenti55864192018-08-30 04:05:20 +09001792
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001793 /*
1794 * If there are dots in the name already, let's just give it a try
1795 * 'as is'. The threshold can be set with the "ndots" option.
1796 */
1797 saved_herrno = -1;
1798 if (dots >= res->ndots) {
1799 ret = res_querydomainN(name, NULL, target, res);
1800 if (ret > 0) return (ret);
1801 saved_herrno = h_errno;
1802 tried_as_is++;
1803 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001804
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001805 /*
1806 * We do at least one level of search if
1807 * - there is no dot and RES_DEFNAME is set, or
1808 * - there is at least one dot, there is no trailing dot,
1809 * and RES_DNSRCH is set.
1810 */
1811 if ((!dots && (res->options & RES_DEFNAMES)) ||
1812 (dots && !trailing_dot && (res->options & RES_DNSRCH))) {
1813 int done = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +09001814
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001815 /* Unfortunately we need to set stuff up before
1816 * the domain stuff is tried. Will have a better
1817 * fix after thread pools are used.
1818 */
1819 _resolv_populate_res_for_net(res);
Bernie Innocenti55864192018-08-30 04:05:20 +09001820
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001821 for (domain = (const char* const*) res->dnsrch; *domain && !done; domain++) {
1822 ret = res_querydomainN(name, *domain, target, res);
1823 if (ret > 0) return ret;
Bernie Innocenti55864192018-08-30 04:05:20 +09001824
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001825 /*
1826 * If no server present, give up.
1827 * If name isn't found in this domain,
1828 * keep trying higher domains in the search list
1829 * (if that's enabled).
1830 * On a NO_DATA error, keep trying, otherwise
1831 * a wildcard entry of another type could keep us
1832 * from finding this entry higher in the domain.
1833 * If we get some other error (negative answer or
1834 * server failure), then stop searching up,
1835 * but try the input name below in case it's
1836 * fully-qualified.
1837 */
1838 if (errno == ECONNREFUSED) {
1839 h_errno = TRY_AGAIN;
1840 return -1;
1841 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001842
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001843 switch (h_errno) {
1844 case NO_DATA:
1845 got_nodata++;
Bernie Innocenti8bb94ba2018-10-10 22:30:12 +09001846 [[fallthrough]];
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001847 case HOST_NOT_FOUND:
1848 /* keep trying */
1849 break;
1850 case TRY_AGAIN:
1851 if (hp->rcode == SERVFAIL) {
1852 /* try next search element, if any */
1853 got_servfail++;
1854 break;
1855 }
Bernie Innocenti8bb94ba2018-10-10 22:30:12 +09001856 [[fallthrough]];
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001857 default:
1858 /* anything else implies that we're done */
1859 done++;
1860 }
1861 /*
1862 * if we got here for some reason other than DNSRCH,
1863 * we only wanted one iteration of the loop, so stop.
1864 */
1865 if (!(res->options & RES_DNSRCH)) done++;
1866 }
1867 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001868
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001869 /*
1870 * if we have not already tried the name "as is", do that now.
1871 * note that we do this regardless of how many dots were in the
1872 * name or whether it ends with a dot.
1873 */
1874 if (!tried_as_is) {
1875 ret = res_querydomainN(name, NULL, target, res);
1876 if (ret > 0) return ret;
1877 }
Bernie Innocenti55864192018-08-30 04:05:20 +09001878
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001879 /*
1880 * if we got here, we didn't satisfy the search.
1881 * if we did an initial full query, return that query's h_errno
1882 * (note that we wouldn't be here if that query had succeeded).
1883 * else if we ever got a nodata, send that back as the reason.
1884 * else send back meaningless h_errno, that being the one from
1885 * the last DNSRCH we did.
1886 */
1887 if (saved_herrno != -1)
1888 h_errno = saved_herrno;
1889 else if (got_nodata)
1890 h_errno = NO_DATA;
1891 else if (got_servfail)
1892 h_errno = TRY_AGAIN;
1893 return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +09001894}
1895
1896/*
1897 * Perform a call on res_query on the concatenation of name and domain,
1898 * removing a trailing dot from name if domain is NULL.
1899 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001900static int res_querydomainN(const char* name, const char* domain, struct res_target* target,
1901 res_state res) {
1902 char nbuf[MAXDNAME];
1903 const char* longname = nbuf;
1904 size_t n, d;
Bernie Innocenti55864192018-08-30 04:05:20 +09001905
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001906 assert(name != NULL);
1907 /* XXX: target may be NULL??? */
Bernie Innocenti55864192018-08-30 04:05:20 +09001908
1909#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001910 if (res->options & RES_DEBUG)
1911 printf(";; res_querydomain(%s, %s)\n", name, domain ? domain : "<Nil>");
Bernie Innocenti55864192018-08-30 04:05:20 +09001912#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +09001913 if (domain == NULL) {
1914 /*
1915 * Check for trailing '.';
1916 * copy without '.' if present.
1917 */
1918 n = strlen(name);
1919 if (n + 1 > sizeof(nbuf)) {
1920 h_errno = NO_RECOVERY;
1921 return -1;
1922 }
1923 if (n > 0 && name[--n] == '.') {
1924 strncpy(nbuf, name, n);
1925 nbuf[n] = '\0';
1926 } else
1927 longname = name;
1928 } else {
1929 n = strlen(name);
1930 d = strlen(domain);
1931 if (n + 1 + d + 1 > sizeof(nbuf)) {
1932 h_errno = NO_RECOVERY;
1933 return -1;
1934 }
1935 snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain);
1936 }
1937 return res_queryN(longname, target, res);
Bernie Innocenti55864192018-08-30 04:05:20 +09001938}