blob: 7fa6a1b59b4b436391e8c7d3a834e92716250a7c [file] [log] [blame]
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001/* $NetBSD: gethnamaddr.c,v 1.91 2014/06/19 15:08:18 christos Exp $ */
2
3/*
4 * ++Copyright++ 1985, 1988, 1993
5 * -
6 * Copyright (c) 1985, 1988, 1993
7 * The Regents of the University of California. All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 * -
33 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
34 *
35 * Permission to use, copy, modify, and distribute this software for any
36 * purpose with or without fee is hereby granted, provided that the above
37 * copyright notice and this permission notice appear in all copies, and that
38 * the name of Digital Equipment Corporation not be used in advertising or
39 * publicity pertaining to distribution of the document or software without
40 * specific, written prior permission.
41 *
42 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
43 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
44 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
45 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
46 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
47 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
48 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
49 * SOFTWARE.
50 * -
51 * --Copyright--
52 */
53
Bernie Innocentie71a28a2019-05-29 00:42:35 +090054#include "gethnamaddr.h"
55
chenbruceacb832c2019-02-20 19:45:50 +080056#include <android-base/logging.h>
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090057#include <arpa/inet.h>
58#include <arpa/nameser.h>
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090059#include <assert.h>
60#include <ctype.h>
61#include <errno.h>
62#include <netdb.h>
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090063#include <netinet/in.h>
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090064#include <stdarg.h>
65#include <stdbool.h>
Bernie Innocentie2bc46f2018-10-16 23:35:28 +090066#include <stdlib.h>
67#include <string.h>
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090068#include <sys/param.h>
69#include <sys/socket.h>
70#include <sys/un.h>
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090071#include <unistd.h>
nuccachen9227b7f2018-09-18 13:38:48 +080072#include <functional>
lifrbe7dfbf2019-01-09 14:41:15 +080073#include <vector>
Bernie Innocentiafaacf72018-08-30 07:34:37 +090074
chenbruceceb3c022021-06-15 21:36:19 +080075#include "Experiments.h"
Bernie Innocentie2bc46f2018-10-16 23:35:28 +090076#include "hostent.h"
Bernie Innocentiac18b122018-10-01 23:10:18 +090077#include "netd_resolv/resolv.h"
Bernie Innocenti10a90282020-01-23 23:28:00 +090078#include "res_comp.h"
79#include "res_debug.h" // p_class(), p_type()
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090080#include "resolv_cache.h"
Luke Huang1b3f4462020-12-04 17:48:39 +080081#include "resolv_private.h"
lifr94981782019-05-17 21:15:19 +080082#include "stats.pb.h"
83
84using android::net::NetworkDnsEventReported;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090085
Bernie Innocentibf0b3bc2019-10-11 19:45:56 +090086constexpr int MAXADDRS = 35;
87
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090088typedef union {
89 HEADER hdr;
chenbrucec51f1212019-09-12 16:59:33 +080090 uint8_t buf[MAXPACKET];
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090091} querybuf;
92
nuccachen9227b7f2018-09-18 13:38:48 +080093static void pad_v4v6_hostent(struct hostent* hp, char** bpp, char* ep);
waynema067e9842019-03-08 19:13:41 +080094static int dns_gethtbyaddr(const unsigned char* uaddr, int len, int af,
lifr94981782019-05-17 21:15:19 +080095 const android_net_context* netcontext, getnamaddr* info,
96 NetworkDnsEventReported* event);
Bernie Innocenti08487112019-10-11 21:14:13 +090097static int dns_gethtbyname(ResState* res, const char* name, int af, getnamaddr* info);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090098
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090099#define BOUNDED_INCR(x) \
100 do { \
101 BOUNDS_CHECK(cp, x); \
102 cp += (x); \
Bernie Innocenti9c575932018-09-07 21:10:25 +0900103 } while (0)
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900104
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900105#define BOUNDS_CHECK(ptr, count) \
106 do { \
107 if (eom - (ptr) < (count)) goto no_recovery; \
Bernie Innocenti9c575932018-09-07 21:10:25 +0900108 } while (0)
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900109
Bernie Innocentibc7d6c02021-06-22 16:59:52 +0900110static struct hostent* getanswer(const querybuf* _Nonnull answer, int anslen,
111 const char* _Nonnull qname, int qtype, struct hostent* hent,
112 char* buf, size_t buflen, int* he) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900113 const HEADER* hp;
chenbrucec51f1212019-09-12 16:59:33 +0800114 const uint8_t* cp;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900115 int n;
116 size_t qlen;
chenbrucec51f1212019-09-12 16:59:33 +0800117 const uint8_t *eom, *erdata;
lifrbe7dfbf2019-01-09 14:41:15 +0800118 char *bp, **hap, *ep;
Bernie Innocenti9c575932018-09-07 21:10:25 +0900119 int ancount, qdcount;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900120 int haveanswer, had_error;
121 int toobig = 0;
122 char tbuf[MAXDNAME];
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900123 char* addr_ptrs[MAXADDRS];
124 const char* tname;
lifrbe7dfbf2019-01-09 14:41:15 +0800125 std::vector<char*> aliases;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900126
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900127 tname = qname;
128 hent->h_name = NULL;
129 eom = answer->buf + anslen;
Bernie Innocenti10a90282020-01-23 23:28:00 +0900130
131 bool (*name_ok)(const char* dn);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900132 switch (qtype) {
133 case T_A:
134 case T_AAAA:
135 name_ok = res_hnok;
136 break;
137 case T_PTR:
138 name_ok = res_dnok;
139 break;
140 default:
141 *he = NO_RECOVERY;
142 return NULL; /* XXX should be abort(); */
143 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900144
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900145 /*
146 * find first satisfactory answer
147 */
148 hp = &answer->hdr;
149 ancount = ntohs(hp->ancount);
150 qdcount = ntohs(hp->qdcount);
151 bp = buf;
152 ep = buf + buflen;
153 cp = answer->buf;
154 BOUNDED_INCR(HFIXEDSZ);
155 if (qdcount != 1) goto no_recovery;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900156
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900157 n = dn_expand(answer->buf, eom, cp, bp, (int) (ep - bp));
Bernie Innocenti10a90282020-01-23 23:28:00 +0900158 if ((n < 0) || !name_ok(bp)) goto no_recovery;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900159
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900160 BOUNDED_INCR(n + QFIXEDSZ);
161 if (qtype == T_A || qtype == T_AAAA) {
162 /* res_send() has already verified that the query name is the
163 * same as the one we sent; this just gets the expanded name
164 * (i.e., with the succeeding search-domain tacked on).
165 */
166 n = (int) strlen(bp) + 1; /* for the \0 */
167 if (n >= MAXHOSTNAMELEN) goto no_recovery;
168 hent->h_name = bp;
169 bp += n;
170 /* The qname can be abbreviated, but h_name is now absolute. */
171 qname = hent->h_name;
172 }
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900173 hent->h_addr_list = hap = addr_ptrs;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900174 *hap = NULL;
175 haveanswer = 0;
176 had_error = 0;
177 while (ancount-- > 0 && cp < eom && !had_error) {
178 n = dn_expand(answer->buf, eom, cp, bp, (int) (ep - bp));
Bernie Innocenti10a90282020-01-23 23:28:00 +0900179 if ((n < 0) || !name_ok(bp)) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900180 had_error++;
181 continue;
182 }
183 cp += n; /* name */
184 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
chenbruce0d470422019-03-28 18:44:37 +0800185 int type = ntohs(*reinterpret_cast<const uint16_t*>(cp));
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900186 cp += INT16SZ; /* type */
chenbruce0d470422019-03-28 18:44:37 +0800187 int cl = ntohs(*reinterpret_cast<const uint16_t*>(cp));
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900188 cp += INT16SZ + INT32SZ; /* class, TTL */
chenbruce0d470422019-03-28 18:44:37 +0800189 n = ntohs(*reinterpret_cast<const uint16_t*>(cp));
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900190 cp += INT16SZ; /* len */
191 BOUNDS_CHECK(cp, n);
192 erdata = cp + n;
Bernie Innocenti9c575932018-09-07 21:10:25 +0900193 if (cl != C_IN) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900194 /* XXX - debug? syslog? */
195 cp += n;
196 continue; /* XXX - had_error++ ? */
197 }
198 if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) {
199 n = dn_expand(answer->buf, eom, cp, tbuf, (int) sizeof tbuf);
Bernie Innocenti10a90282020-01-23 23:28:00 +0900200 if ((n < 0) || !name_ok(tbuf)) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900201 had_error++;
202 continue;
203 }
204 cp += n;
205 if (cp != erdata) goto no_recovery;
206 /* Store alias. */
lifrbe7dfbf2019-01-09 14:41:15 +0800207 aliases.push_back(bp);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900208 n = (int) strlen(bp) + 1; /* for the \0 */
209 if (n >= MAXHOSTNAMELEN) {
210 had_error++;
211 continue;
212 }
213 bp += n;
214 /* Get canonical name. */
215 n = (int) strlen(tbuf) + 1; /* for the \0 */
216 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
217 had_error++;
218 continue;
219 }
220 strlcpy(bp, tbuf, (size_t)(ep - bp));
221 hent->h_name = bp;
222 bp += n;
223 continue;
224 }
225 if (qtype == T_PTR && type == T_CNAME) {
226 n = dn_expand(answer->buf, eom, cp, tbuf, (int) sizeof tbuf);
Bernie Innocenti10a90282020-01-23 23:28:00 +0900227 if (n < 0 || !res_dnok(tbuf)) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900228 had_error++;
229 continue;
230 }
231 cp += n;
232 if (cp != erdata) goto no_recovery;
233 /* Get canonical name. */
234 n = (int) strlen(tbuf) + 1; /* for the \0 */
235 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
236 had_error++;
237 continue;
238 }
239 strlcpy(bp, tbuf, (size_t)(ep - bp));
240 tname = bp;
241 bp += n;
242 continue;
243 }
244 if (type != qtype) {
245 if (type != T_KEY && type != T_SIG)
Ken Chenffc224a2019-03-19 17:41:28 +0800246 LOG(DEBUG) << __func__ << ": asked for \"" << qname << " " << p_class(C_IN) << " "
247 << p_type(qtype) << "\", got type \"" << p_type(type) << "\"";
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900248 cp += n;
249 continue; /* XXX - had_error++ ? */
250 }
251 switch (type) {
252 case T_PTR:
253 if (strcasecmp(tname, bp) != 0) {
Ken Chenffc224a2019-03-19 17:41:28 +0800254 LOG(DEBUG) << __func__ << ": asked for \"" << qname << "\", got \"" << bp
255 << "\"";
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900256 cp += n;
257 continue; /* XXX - had_error++ ? */
258 }
259 n = dn_expand(answer->buf, eom, cp, bp, (int) (ep - bp));
Bernie Innocenti10a90282020-01-23 23:28:00 +0900260 if ((n < 0) || !res_hnok(bp)) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900261 had_error++;
262 break;
263 }
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900264 cp += n;
265 if (cp != erdata) goto no_recovery;
266 if (!haveanswer)
267 hent->h_name = bp;
268 else
lifrbe7dfbf2019-01-09 14:41:15 +0800269 aliases.push_back(bp);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900270 if (n != -1) {
271 n = (int) strlen(bp) + 1; /* for the \0 */
272 if (n >= MAXHOSTNAMELEN) {
273 had_error++;
274 break;
275 }
276 bp += n;
277 }
278 break;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900279 case T_A:
280 case T_AAAA:
281 if (strcasecmp(hent->h_name, bp) != 0) {
Ken Chenffc224a2019-03-19 17:41:28 +0800282 LOG(DEBUG) << __func__ << ": asked for \"" << hent->h_name << "\", got \"" << bp
283 << "\"";
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900284 cp += n;
285 continue; /* XXX - had_error++ ? */
286 }
287 if (n != hent->h_length) {
288 cp += n;
289 continue;
290 }
291 if (type == T_AAAA) {
292 struct in6_addr in6;
293 memcpy(&in6, cp, NS_IN6ADDRSZ);
294 if (IN6_IS_ADDR_V4MAPPED(&in6)) {
295 cp += n;
296 continue;
297 }
298 }
299 if (!haveanswer) {
300 int nn;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900301
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900302 hent->h_name = bp;
303 nn = (int) strlen(bp) + 1; /* for the \0 */
304 bp += nn;
305 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900306
Bernie Innocentibc7d6c02021-06-22 16:59:52 +0900307 bp = align_ptr<sizeof(int32_t)>(bp);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900308
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900309 if (bp + n >= ep) {
Ken Chenffc224a2019-03-19 17:41:28 +0800310 LOG(DEBUG) << __func__ << ": size (" << n << ") too big";
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900311 had_error++;
312 continue;
313 }
314 if (hap >= &addr_ptrs[MAXADDRS - 1]) {
315 if (!toobig++) {
Ken Chenffc224a2019-03-19 17:41:28 +0800316 LOG(DEBUG) << __func__ << ": Too many addresses (" << MAXADDRS << ")";
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900317 }
318 cp += n;
319 continue;
320 }
321 (void) memcpy(*hap++ = bp, cp, (size_t) n);
322 bp += n;
323 cp += n;
324 if (cp != erdata) goto no_recovery;
325 break;
326 default:
327 abort();
328 }
329 if (!had_error) haveanswer++;
330 }
331 if (haveanswer) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900332 *hap = NULL;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900333 if (!hent->h_name) {
334 n = (int) strlen(qname) + 1; /* for the \0 */
335 if (n > ep - bp || n >= MAXHOSTNAMELEN) goto no_recovery;
336 strlcpy(bp, qname, (size_t)(ep - bp));
337 hent->h_name = bp;
338 bp += n;
339 }
nuccachen9227b7f2018-09-18 13:38:48 +0800340 if (hent->h_addrtype == AF_INET) pad_v4v6_hostent(hent, &bp, ep);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900341 goto success;
342 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900343no_recovery:
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900344 *he = NO_RECOVERY;
345 return NULL;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900346success:
Bernie Innocentibc7d6c02021-06-22 16:59:52 +0900347 bp = align_ptr(bp);
lifrbe7dfbf2019-01-09 14:41:15 +0800348 aliases.push_back(nullptr);
349 qlen = aliases.size() * sizeof(*hent->h_aliases);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900350 if ((size_t)(ep - bp) < qlen) goto nospc;
Bernie Innocenti9c575932018-09-07 21:10:25 +0900351 hent->h_aliases = (char**) bp;
lifrbe7dfbf2019-01-09 14:41:15 +0800352 memcpy(bp, aliases.data(), qlen);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900353
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900354 bp += qlen;
355 n = (int) (hap - addr_ptrs);
356 qlen = (n + 1) * sizeof(*hent->h_addr_list);
357 if ((size_t)(ep - bp) < qlen) goto nospc;
Bernie Innocenti9c575932018-09-07 21:10:25 +0900358 hent->h_addr_list = (char**) bp;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900359 memcpy(bp, addr_ptrs, qlen);
360 *he = NETDB_SUCCESS;
361 return hent;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900362nospc:
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900363 errno = ENOSPC;
364 *he = NETDB_INTERNAL;
365 return NULL;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900366}
367
Bernie Innocenti36db7732019-10-11 18:03:02 +0900368int resolv_gethostbyname(const char* name, int af, hostent* hp, char* buf, size_t buflen,
369 const android_net_context* netcontext, hostent** result,
370 NetworkDnsEventReported* event) {
Bernie Innocentic0d8ada2020-11-04 12:51:22 +0900371 if (name == nullptr || hp == nullptr) {
372 return EAI_SYSTEM;
373 }
Bernie Innocenti36db7732019-10-11 18:03:02 +0900374
Bernie Innocentic0d8ada2020-11-04 12:51:22 +0900375 getnamaddr info;
Luke Huang1b3f4462020-12-04 17:48:39 +0800376 ResState res(netcontext, event);
Bernie Innocenti36db7732019-10-11 18:03:02 +0900377
chenbruceceb3c022021-06-15 21:36:19 +0800378 setMdnsFlag(name, &(res._flags));
379
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900380 size_t size;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900381 switch (af) {
382 case AF_INET:
383 size = NS_INADDRSZ;
384 break;
385 case AF_INET6:
386 size = NS_IN6ADDRSZ;
387 break;
388 default:
Mike Yuc7d55102018-11-06 19:20:07 +0800389 return EAI_FAMILY;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900390 }
391 if (buflen < size) goto nospc;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900392
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900393 hp->h_addrtype = af;
394 hp->h_length = (int) size;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900395
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900396 /*
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900397 * disallow names consisting only of digits/dots, unless
398 * they end in a dot.
399 */
chenbrucec51f1212019-09-12 16:59:33 +0800400 if (isdigit((uint8_t)name[0])) {
Bernie Innocenti2e8540b2018-09-26 11:52:04 +0900401 for (const char* cp = name;; ++cp) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900402 if (!*cp) {
403 if (*--cp == '.') break;
404 /*
405 * All-numeric, no dot at the end.
406 * Fake up a hostent as if we'd actually
407 * done a lookup.
408 */
409 goto fake;
410 }
chenbrucec51f1212019-09-12 16:59:33 +0800411 if (!isdigit((uint8_t)*cp) && *cp != '.') break;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900412 }
Bernie Innocenti2e8540b2018-09-26 11:52:04 +0900413 }
chenbrucec51f1212019-09-12 16:59:33 +0800414 if ((isxdigit((uint8_t)name[0]) && strchr(name, ':') != NULL) || name[0] == ':') {
Bernie Innocenti2e8540b2018-09-26 11:52:04 +0900415 for (const char* cp = name;; ++cp) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900416 if (!*cp) {
417 if (*--cp == '.') break;
418 /*
419 * All-IPv6-legal, no dot at the end.
420 * Fake up a hostent as if we'd actually
421 * done a lookup.
422 */
423 goto fake;
424 }
chenbrucec51f1212019-09-12 16:59:33 +0800425 if (!isxdigit((uint8_t)*cp) && *cp != ':' && *cp != '.') break;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900426 }
Bernie Innocenti2e8540b2018-09-26 11:52:04 +0900427 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900428
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900429 info.hp = hp;
430 info.buf = buf;
431 info.buflen = buflen;
Hungming Chen6c84a3d2018-12-26 16:14:17 +0800432 if (_hf_gethtbyname2(name, af, &info)) {
Bernie Innocenti08487112019-10-11 21:14:13 +0900433 int error = dns_gethtbyname(&res, name, af, &info);
Hungming Chena6914a62019-01-19 15:07:04 +0800434 if (error != 0) return error;
Bernie Innocentic939de02018-09-12 17:59:17 +0900435 }
Bernie Innocenti36db7732019-10-11 18:03:02 +0900436 *result = hp;
Mike Yuc7d55102018-11-06 19:20:07 +0800437 return 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900438nospc:
Hungming Chen6c84a3d2018-12-26 16:14:17 +0800439 return EAI_MEMORY;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900440fake:
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900441 HENT_ARRAY(hp->h_addr_list, 1, buf, buflen);
442 HENT_ARRAY(hp->h_aliases, 0, buf, buflen);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900443
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900444 hp->h_aliases[0] = NULL;
445 if (size > buflen) goto nospc;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900446
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900447 if (inet_pton(af, name, buf) <= 0) {
Mike Yuc7d55102018-11-06 19:20:07 +0800448 return EAI_NODATA;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900449 }
450 hp->h_addr_list[0] = buf;
451 hp->h_addr_list[1] = NULL;
452 buf += size;
453 buflen -= size;
454 HENT_SCOPY(hp->h_name, name, buf, buflen);
Bernie Innocenti36db7732019-10-11 18:03:02 +0900455 *result = hp;
Mike Yuc7d55102018-11-06 19:20:07 +0800456 return 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900457}
458
Bernie Innocentibc7d6c02021-06-22 16:59:52 +0900459int resolv_gethostbyaddr(const void* _Nonnull addr, socklen_t len, int af, hostent* hp, char* buf,
Bernie Innocenti324f9f82019-10-11 18:50:20 +0900460 size_t buflen, const struct android_net_context* netcontext,
461 hostent** result, NetworkDnsEventReported* event) {
chenbrucec51f1212019-09-12 16:59:33 +0800462 const uint8_t* uaddr = (const uint8_t*)addr;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900463 socklen_t size;
464 struct getnamaddr info;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900465
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900466 if (af == AF_INET6 && len == NS_IN6ADDRSZ &&
467 (IN6_IS_ADDR_LINKLOCAL((const struct in6_addr*) addr) ||
468 IN6_IS_ADDR_SITELOCAL((const struct in6_addr*) addr))) {
Hungming Chen598202c2018-12-26 17:04:43 +0800469 return EAI_NODATA;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900470 }
471 if (af == AF_INET6 && len == NS_IN6ADDRSZ &&
472 (IN6_IS_ADDR_V4MAPPED((const struct in6_addr*) addr) ||
473 IN6_IS_ADDR_V4COMPAT((const struct in6_addr*) addr))) {
474 /* Unmap. */
475 uaddr += NS_IN6ADDRSZ - NS_INADDRSZ;
476 addr = uaddr;
477 af = AF_INET;
478 len = NS_INADDRSZ;
479 }
480 switch (af) {
481 case AF_INET:
482 size = NS_INADDRSZ;
483 break;
484 case AF_INET6:
485 size = NS_IN6ADDRSZ;
486 break;
487 default:
Hungming Chen598202c2018-12-26 17:04:43 +0800488 return EAI_FAMILY;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900489 }
490 if (size != len) {
Hungming Chena6914a62019-01-19 15:07:04 +0800491 // TODO: Consider converting to a private extended EAI_* error code.
492 // Currently, the EAI_* value has no corresponding error code for invalid argument socket
493 // length. In order to not rely on errno, convert the original error code pair, EAI_SYSTEM
494 // and EINVAL, to EAI_FAIL.
495 return EAI_FAIL;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900496 }
497 info.hp = hp;
498 info.buf = buf;
499 info.buflen = buflen;
Hungming Chen6c84a3d2018-12-26 16:14:17 +0800500 if (_hf_gethtbyaddr(uaddr, len, af, &info)) {
lifr94981782019-05-17 21:15:19 +0800501 int error = dns_gethtbyaddr(uaddr, len, af, netcontext, &info, event);
Hungming Chena6914a62019-01-19 15:07:04 +0800502 if (error != 0) return error;
Bernie Innocenti9f61dbd2018-09-12 20:03:11 +0900503 }
Bernie Innocenti324f9f82019-10-11 18:50:20 +0900504 *result = hp;
Hungming Chen598202c2018-12-26 17:04:43 +0800505 return 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900506}
507
Hungming Chena6914a62019-01-19 15:07:04 +0800508// TODO: Consider leaving function without returning error code as _gethtent() does because
509// the error code of the caller does not currently return to netd.
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900510struct hostent* netbsd_gethostent_r(FILE* hf, struct hostent* hent, char* buf, size_t buflen,
511 int* he) {
Bernie Innocenti9c575932018-09-07 21:10:25 +0900512 char *name;
lifrbe7dfbf2019-01-09 14:41:15 +0800513 char* cp;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900514 int af, len;
515 size_t anum;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900516 struct in6_addr host_addr;
lifrbe7dfbf2019-01-09 14:41:15 +0800517 std::vector<char*> aliases;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900518
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900519 if (hf == NULL) {
520 *he = NETDB_INTERNAL;
521 errno = EINVAL;
522 return NULL;
523 }
Bernie Innocenti9c575932018-09-07 21:10:25 +0900524 char* p = NULL;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900525
Bernie Innocenti36db7732019-10-11 18:03:02 +0900526 // Allocate a new space to read file lines like upstream does.
527 const size_t line_buf_size = MAXPACKET;
Bernie Innocenti9c575932018-09-07 21:10:25 +0900528 if ((p = (char*) malloc(line_buf_size)) == NULL) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900529 goto nospc;
530 }
531 for (;;) {
532 if (!fgets(p, line_buf_size, hf)) {
533 free(p);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900534 *he = HOST_NOT_FOUND;
535 return NULL;
536 }
537 if (*p == '#') {
538 continue;
539 }
540 if (!(cp = strpbrk(p, "#\n"))) {
541 continue;
542 }
543 *cp = '\0';
544 if (!(cp = strpbrk(p, " \t"))) continue;
545 *cp++ = '\0';
546 if (inet_pton(AF_INET6, p, &host_addr) > 0) {
547 af = AF_INET6;
548 len = NS_IN6ADDRSZ;
549 } else {
550 if (inet_pton(AF_INET, p, &host_addr) <= 0) continue;
chenbruce018fdb22019-06-12 18:08:04 +0800551 af = AF_INET;
552 len = NS_INADDRSZ;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900553 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900554
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900555 /* if this is not something we're looking for, skip it. */
556 if (hent->h_addrtype != 0 && hent->h_addrtype != af) continue;
557 if (hent->h_length != 0 && hent->h_length != len) continue;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900558
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900559 while (*cp == ' ' || *cp == '\t') cp++;
560 if ((cp = strpbrk(name = cp, " \t")) != NULL) *cp++ = '\0';
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900561 while (cp && *cp) {
562 if (*cp == ' ' || *cp == '\t') {
563 cp++;
564 continue;
565 }
lifrbe7dfbf2019-01-09 14:41:15 +0800566 aliases.push_back(cp);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900567 if ((cp = strpbrk(cp, " \t")) != NULL) *cp++ = '\0';
568 }
569 break;
570 }
571 hent->h_length = len;
572 hent->h_addrtype = af;
573 HENT_ARRAY(hent->h_addr_list, 1, buf, buflen);
lifrbe7dfbf2019-01-09 14:41:15 +0800574 anum = aliases.size();
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900575 HENT_ARRAY(hent->h_aliases, anum, buf, buflen);
576 HENT_COPY(hent->h_addr_list[0], &host_addr, hent->h_length, buf, buflen);
577 hent->h_addr_list[1] = NULL;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900578
nuccachenc28257d2018-09-11 11:20:00 +0800579 /* Reserve space for mapping IPv4 address to IPv6 address in place */
580 if (hent->h_addrtype == AF_INET) {
581 HENT_COPY(buf, NAT64_PAD, sizeof(NAT64_PAD), buf, buflen);
582 }
583
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900584 HENT_SCOPY(hent->h_name, name, buf, buflen);
585 for (size_t i = 0; i < anum; i++) HENT_SCOPY(hent->h_aliases[i], aliases[i], buf, buflen);
586 hent->h_aliases[anum] = NULL;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900587 *he = NETDB_SUCCESS;
588 free(p);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900589 return hent;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900590nospc:
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900591 free(p);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900592 errno = ENOSPC;
593 *he = NETDB_INTERNAL;
594 return NULL;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900595}
596
Bernie Innocentibc7d6c02021-06-22 16:59:52 +0900597/* Reserve space for mapping IPv4 address to IPv6 address in place */
598static void pad_v4v6_hostent(struct hostent* _Nonnull hp, char** _Nonnull bpp, char* _Nonnull ep) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900599 if (hp->h_addrtype != AF_INET || hp->h_length != NS_INADDRSZ) return;
nuccachen9227b7f2018-09-18 13:38:48 +0800600 for (char** ap = hp->h_addr_list; *ap; ap++) {
Bernie Innocentibc7d6c02021-06-22 16:59:52 +0900601 char* const bp = align_ptr<sizeof(int32_t)>(*bpp);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900602
Bernie Innocentibc7d6c02021-06-22 16:59:52 +0900603 if (ep - bp < NS_IN6ADDRSZ) {
604 // Out of space. Truncate address list here.
605 *ap = nullptr;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900606 return;
607 }
Bernie Innocentibc7d6c02021-06-22 16:59:52 +0900608 memcpy(bp, *ap, NS_INADDRSZ);
609 memcpy(bp + NS_INADDRSZ, NAT64_PAD, sizeof(NAT64_PAD));
610 *ap = bp;
611 *bpp = bp + NS_IN6ADDRSZ;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900612 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900613}
614
Bernie Innocenti08487112019-10-11 21:14:13 +0900615static int dns_gethtbyname(ResState* res, const char* name, int addr_type, getnamaddr* info) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900616 int n, type;
Bernie Innocentic939de02018-09-12 17:59:17 +0900617 info->hp->h_addrtype = addr_type;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900618
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900619 switch (info->hp->h_addrtype) {
620 case AF_INET:
621 info->hp->h_length = NS_INADDRSZ;
622 type = T_A;
623 break;
624 case AF_INET6:
625 info->hp->h_length = NS_IN6ADDRSZ;
626 type = T_AAAA;
627 break;
628 default:
Mike Yuc7d55102018-11-06 19:20:07 +0800629 return EAI_FAMILY;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900630 }
waynema0cac8c62019-03-08 16:58:06 +0800631 auto buf = std::make_unique<querybuf>();
632
Hungming Chena6914a62019-01-19 15:07:04 +0800633 int he;
634 n = res_nsearch(res, name, C_IN, type, buf->buf, (int)sizeof(buf->buf), &he);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900635 if (n < 0) {
Ken Chenffc224a2019-03-19 17:41:28 +0800636 LOG(DEBUG) << __func__ << ": res_nsearch failed (" << n << ")";
Hungming Chena6914a62019-01-19 15:07:04 +0800637 // Return h_errno (he) to catch more detailed errors rather than EAI_NODATA.
638 // Note that res_nsearch() doesn't set the pair NETDB_INTERNAL and errno.
639 // See also herrnoToAiErrno().
640 return herrnoToAiErrno(he);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900641 }
Bernie Innocenti3479d3b2019-10-08 22:23:26 +0900642 hostent* hp = getanswer(buf.get(), n, name, type, info->hp, info->buf, info->buflen, &he);
Hungming Chena6914a62019-01-19 15:07:04 +0800643 if (hp == NULL) return herrnoToAiErrno(he);
644
Mike Yuc7d55102018-11-06 19:20:07 +0800645 return 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900646}
647
waynema067e9842019-03-08 19:13:41 +0800648static int dns_gethtbyaddr(const unsigned char* uaddr, int len, int af,
lifr94981782019-05-17 21:15:19 +0800649 const android_net_context* netcontext, getnamaddr* info,
650 NetworkDnsEventReported* event) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900651 char qbuf[MAXDNAME + 1], *qp, *ep;
652 int n;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900653 int advance;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900654
Bernie Innocenti9f61dbd2018-09-12 20:03:11 +0900655 info->hp->h_length = len;
656 info->hp->h_addrtype = af;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900657
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900658 switch (info->hp->h_addrtype) {
659 case AF_INET:
660 (void) snprintf(qbuf, sizeof(qbuf), "%u.%u.%u.%u.in-addr.arpa", (uaddr[3] & 0xff),
661 (uaddr[2] & 0xff), (uaddr[1] & 0xff), (uaddr[0] & 0xff));
662 break;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900663
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900664 case AF_INET6:
665 qp = qbuf;
666 ep = qbuf + sizeof(qbuf) - 1;
667 for (n = NS_IN6ADDRSZ - 1; n >= 0; n--) {
668 advance = snprintf(qp, (size_t)(ep - qp), "%x.%x.", uaddr[n] & 0xf,
669 ((unsigned int) uaddr[n] >> 4) & 0xf);
670 if (advance > 0 && qp + advance < ep)
671 qp += advance;
672 else {
Hungming Chena6914a62019-01-19 15:07:04 +0800673 // TODO: Consider converting to a private extended EAI_* error code.
674 // Currently, the EAI_* value has no corresponding error code for an internal
675 // out of buffer space. In order to not rely on errno, convert the original
676 // error code EAI_SYSTEM to EAI_MEMORY.
677 return EAI_MEMORY;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900678 }
679 }
680 if (strlcat(qbuf, "ip6.arpa", sizeof(qbuf)) >= sizeof(qbuf)) {
Hungming Chena6914a62019-01-19 15:07:04 +0800681 // TODO: Consider converting to a private extended EAI_* error code.
682 // Currently, the EAI_* value has no corresponding error code for an internal
683 // out of buffer space. In order to not rely on errno, convert the original
684 // error code EAI_SYSTEM to EAI_MEMORY.
685 return EAI_MEMORY;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900686 }
687 break;
688 default:
Hungming Chen598202c2018-12-26 17:04:43 +0800689 return EAI_FAMILY;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900690 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900691
waynema067e9842019-03-08 19:13:41 +0800692 auto buf = std::make_unique<querybuf>();
693
Luke Huang1b3f4462020-12-04 17:48:39 +0800694 ResState res(netcontext, event);
Hungming Chena6914a62019-01-19 15:07:04 +0800695 int he;
Bernie Innocenti08487112019-10-11 21:14:13 +0900696 n = res_nquery(&res, qbuf, C_IN, T_PTR, buf->buf, (int)sizeof(buf->buf), &he);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900697 if (n < 0) {
Ken Chenffc224a2019-03-19 17:41:28 +0800698 LOG(DEBUG) << __func__ << ": res_nquery failed (" << n << ")";
Hungming Chena6914a62019-01-19 15:07:04 +0800699 // Note that res_nquery() doesn't set the pair NETDB_INTERNAL and errno.
700 // Return h_errno (he) to catch more detailed errors rather than EAI_NODATA.
701 // See also herrnoToAiErrno().
702 return herrnoToAiErrno(he);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900703 }
Bernie Innocenti3479d3b2019-10-08 22:23:26 +0900704 hostent* hp = getanswer(buf.get(), n, qbuf, T_PTR, info->hp, info->buf, info->buflen, &he);
Hungming Chena6914a62019-01-19 15:07:04 +0800705 if (hp == NULL) return herrnoToAiErrno(he);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900706
Bernie Innocenti9c575932018-09-07 21:10:25 +0900707 char* bf = (char*) (hp->h_addr_list + 2);
708 size_t blen = (size_t)(bf - info->buf);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900709 if (blen + info->hp->h_length > info->buflen) goto nospc;
710 hp->h_addr_list[0] = bf;
711 hp->h_addr_list[1] = NULL;
Bernie Innocenti9c575932018-09-07 21:10:25 +0900712 memcpy(bf, uaddr, (size_t) info->hp->h_length);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900713
nuccachenc28257d2018-09-11 11:20:00 +0800714 /* Reserve enough space for mapping IPv4 address to IPv6 address in place */
715 if (info->hp->h_addrtype == AF_INET) {
716 if (blen + NS_IN6ADDRSZ > info->buflen) goto nospc;
717 // Pad zero to the unused address space
718 memcpy(bf + NS_INADDRSZ, NAT64_PAD, sizeof(NAT64_PAD));
719 }
720
Hungming Chen598202c2018-12-26 17:04:43 +0800721 return 0;
Bernie Innocenti9c575932018-09-07 21:10:25 +0900722
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900723nospc:
Hungming Chen598202c2018-12-26 17:04:43 +0800724 return EAI_MEMORY;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900725}
726
Hungming Chena6914a62019-01-19 15:07:04 +0800727int herrnoToAiErrno(int he) {
728 switch (he) {
Hungming Chen947aab02018-12-27 18:33:19 +0800729 // extended h_errno
730 case NETD_RESOLV_H_ERRNO_EXT_TIMEOUT:
731 return NETD_RESOLV_TIMEOUT;
732 // legacy h_errno
733 case NETDB_SUCCESS:
734 return 0;
735 case HOST_NOT_FOUND: // TODO: Perhaps convert HOST_NOT_FOUND to EAI_NONAME instead
736 case NO_DATA: // NO_ADDRESS
Mike Yubfb1b342018-11-06 15:42:36 +0800737 return EAI_NODATA;
738 case TRY_AGAIN:
739 return EAI_AGAIN;
Hungming Chen947aab02018-12-27 18:33:19 +0800740 case NETDB_INTERNAL:
Hungming Chena6914a62019-01-19 15:07:04 +0800741 // TODO: Remove ENOSPC and call abort() immediately whenever any allocation fails.
742 if (errno == ENOSPC) return EAI_MEMORY;
743 // Theoretically, this should not happen. Leave this here just in case.
744 // Currently, getanswer() of {gethnamaddr, getaddrinfo}.cpp, res_nsearch() and
745 // res_searchN() use this function to convert error code. Only getanswer()
746 // of gethnamaddr.cpp may return the error code pair, herrno NETDB_INTERNAL and
747 // errno ENOSPC, which has already converted to EAI_MEMORY. The remaining functions
748 // don't set the pair herrno and errno.
Hungming Chen947aab02018-12-27 18:33:19 +0800749 return EAI_SYSTEM; // see errno for detail
750 case NO_RECOVERY:
Mike Yubfb1b342018-11-06 15:42:36 +0800751 default:
Hungming Chena6914a62019-01-19 15:07:04 +0800752 return EAI_FAIL; // TODO: Perhaps convert default to EAI_MAX (unknown error) instead
Mike Yubfb1b342018-11-06 15:42:36 +0800753 }
Bernie Innocentie71a28a2019-05-29 00:42:35 +0900754}
chenbruceceb3c022021-06-15 21:36:19 +0800755
756void setMdnsFlag(std::string_view hostname, uint32_t* flags) {
757 if (hostname.ends_with(".local") &&
758 android::net::Experiments::getInstance()->getFlag("mdns_resolution", 1))
759 *flags |= RES_F_MDNS;
760}
761
762bool isMdnsResolution(uint32_t flags) {
763 return flags & RES_F_MDNS;
764}