blob: 6dcd35858697c0fd9061e453490b839fce918fdd [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
Bernie Innocentie2bc46f2018-10-16 23:35:28 +090075#include "hostent.h"
Bernie Innocentiac18b122018-10-01 23:10:18 +090076#include "netd_resolv/resolv.h"
Bernie Innocenti08487112019-10-11 21:14:13 +090077#include "res_init.h"
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090078#include "resolv_cache.h"
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090079#include "resolv_private.h"
lifr94981782019-05-17 21:15:19 +080080#include "stats.pb.h"
81
82using android::net::NetworkDnsEventReported;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090083
Bernie Innocentiafaacf72018-08-30 07:34:37 +090084// NetBSD uses _DIAGASSERT to null-check arguments and the like,
85// but it's clear from the number of mistakes in their assertions
86// that they don't actually test or ship with this.
87#define _DIAGASSERT(e) /* nothing */
88
nuccachenc28257d2018-09-11 11:20:00 +080089// TODO: unify macro ALIGNBYTES and ALIGN for all possible data type alignment of hostent
90// buffer.
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090091#define ALIGNBYTES (sizeof(uintptr_t) - 1)
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090092#define ALIGN(p) (((uintptr_t)(p) + ALIGNBYTES) & ~ALIGNBYTES)
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090093
chenbruce018fdb22019-06-12 18:08:04 +080094#define maybe_ok(res, nm, ok) ((ok)(nm) != 0)
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090095#define maybe_hnok(res, hn) maybe_ok((res), (hn), res_hnok)
96#define maybe_dnok(res, dn) maybe_ok((res), (dn), res_dnok)
97
Bernie Innocentibf0b3bc2019-10-11 19:45:56 +090098constexpr int MAXADDRS = 35;
99
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900100typedef union {
101 HEADER hdr;
chenbrucec51f1212019-09-12 16:59:33 +0800102 uint8_t buf[MAXPACKET];
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900103} querybuf;
104
105typedef union {
106 int32_t al;
107 char ac;
108} align;
109
nuccachen9227b7f2018-09-18 13:38:48 +0800110static void convert_v4v6_hostent(struct hostent* hp, char** bpp, char* ep,
chenbruce99cbfd82019-09-05 15:08:13 +0800111 const std::function<void(struct hostent* hp)>& mapping_param,
112 const std::function<void(char* src, char* dst)>& mapping_addr);
nuccachen9227b7f2018-09-18 13:38:48 +0800113static void pad_v4v6_hostent(struct hostent* hp, char** bpp, char* ep);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900114
waynema067e9842019-03-08 19:13:41 +0800115static int dns_gethtbyaddr(const unsigned char* uaddr, int len, int af,
lifr94981782019-05-17 21:15:19 +0800116 const android_net_context* netcontext, getnamaddr* info,
117 NetworkDnsEventReported* event);
Bernie Innocenti08487112019-10-11 21:14:13 +0900118static int dns_gethtbyname(ResState* res, const char* name, int af, getnamaddr* info);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900119
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900120#define BOUNDED_INCR(x) \
121 do { \
122 BOUNDS_CHECK(cp, x); \
123 cp += (x); \
Bernie Innocenti9c575932018-09-07 21:10:25 +0900124 } while (0)
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900125
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900126#define BOUNDS_CHECK(ptr, count) \
127 do { \
128 if (eom - (ptr) < (count)) goto no_recovery; \
Bernie Innocenti9c575932018-09-07 21:10:25 +0900129 } while (0)
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900130
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900131static struct hostent* getanswer(const querybuf* answer, int anslen, const char* qname, int qtype,
Bernie Innocenti3479d3b2019-10-08 22:23:26 +0900132 struct hostent* hent, char* buf, size_t buflen, int* he) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900133 const HEADER* hp;
chenbrucec51f1212019-09-12 16:59:33 +0800134 const uint8_t* cp;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900135 int n;
136 size_t qlen;
chenbrucec51f1212019-09-12 16:59:33 +0800137 const uint8_t *eom, *erdata;
lifrbe7dfbf2019-01-09 14:41:15 +0800138 char *bp, **hap, *ep;
Bernie Innocenti9c575932018-09-07 21:10:25 +0900139 int ancount, qdcount;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900140 int haveanswer, had_error;
141 int toobig = 0;
142 char tbuf[MAXDNAME];
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900143 char* addr_ptrs[MAXADDRS];
144 const char* tname;
145 int (*name_ok)(const char*);
lifrbe7dfbf2019-01-09 14:41:15 +0800146 std::vector<char*> aliases;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900147
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900148 _DIAGASSERT(answer != NULL);
149 _DIAGASSERT(qname != NULL);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900150
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900151 tname = qname;
152 hent->h_name = NULL;
153 eom = answer->buf + anslen;
154 switch (qtype) {
155 case T_A:
156 case T_AAAA:
157 name_ok = res_hnok;
158 break;
159 case T_PTR:
160 name_ok = res_dnok;
161 break;
162 default:
163 *he = NO_RECOVERY;
164 return NULL; /* XXX should be abort(); */
165 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900166
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900167 /*
168 * find first satisfactory answer
169 */
170 hp = &answer->hdr;
171 ancount = ntohs(hp->ancount);
172 qdcount = ntohs(hp->qdcount);
173 bp = buf;
174 ep = buf + buflen;
175 cp = answer->buf;
176 BOUNDED_INCR(HFIXEDSZ);
177 if (qdcount != 1) goto no_recovery;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900178
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900179 n = dn_expand(answer->buf, eom, cp, bp, (int) (ep - bp));
180 if ((n < 0) || !maybe_ok(res, bp, name_ok)) goto no_recovery;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900181
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900182 BOUNDED_INCR(n + QFIXEDSZ);
183 if (qtype == T_A || qtype == T_AAAA) {
184 /* res_send() has already verified that the query name is the
185 * same as the one we sent; this just gets the expanded name
186 * (i.e., with the succeeding search-domain tacked on).
187 */
188 n = (int) strlen(bp) + 1; /* for the \0 */
189 if (n >= MAXHOSTNAMELEN) goto no_recovery;
190 hent->h_name = bp;
191 bp += n;
192 /* The qname can be abbreviated, but h_name is now absolute. */
193 qname = hent->h_name;
194 }
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900195 hent->h_addr_list = hap = addr_ptrs;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900196 *hap = NULL;
197 haveanswer = 0;
198 had_error = 0;
199 while (ancount-- > 0 && cp < eom && !had_error) {
200 n = dn_expand(answer->buf, eom, cp, bp, (int) (ep - bp));
201 if ((n < 0) || !maybe_ok(res, bp, name_ok)) {
202 had_error++;
203 continue;
204 }
205 cp += n; /* name */
206 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
chenbruce0d470422019-03-28 18:44:37 +0800207 int type = ntohs(*reinterpret_cast<const uint16_t*>(cp));
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900208 cp += INT16SZ; /* type */
chenbruce0d470422019-03-28 18:44:37 +0800209 int cl = ntohs(*reinterpret_cast<const uint16_t*>(cp));
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900210 cp += INT16SZ + INT32SZ; /* class, TTL */
chenbruce0d470422019-03-28 18:44:37 +0800211 n = ntohs(*reinterpret_cast<const uint16_t*>(cp));
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900212 cp += INT16SZ; /* len */
213 BOUNDS_CHECK(cp, n);
214 erdata = cp + n;
Bernie Innocenti9c575932018-09-07 21:10:25 +0900215 if (cl != C_IN) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900216 /* XXX - debug? syslog? */
217 cp += n;
218 continue; /* XXX - had_error++ ? */
219 }
220 if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) {
221 n = dn_expand(answer->buf, eom, cp, tbuf, (int) sizeof tbuf);
222 if ((n < 0) || !maybe_ok(res, tbuf, name_ok)) {
223 had_error++;
224 continue;
225 }
226 cp += n;
227 if (cp != erdata) goto no_recovery;
228 /* Store alias. */
lifrbe7dfbf2019-01-09 14:41:15 +0800229 aliases.push_back(bp);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900230 n = (int) strlen(bp) + 1; /* for the \0 */
231 if (n >= MAXHOSTNAMELEN) {
232 had_error++;
233 continue;
234 }
235 bp += n;
236 /* Get canonical name. */
237 n = (int) strlen(tbuf) + 1; /* for the \0 */
238 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
239 had_error++;
240 continue;
241 }
242 strlcpy(bp, tbuf, (size_t)(ep - bp));
243 hent->h_name = bp;
244 bp += n;
245 continue;
246 }
247 if (qtype == T_PTR && type == T_CNAME) {
248 n = dn_expand(answer->buf, eom, cp, tbuf, (int) sizeof tbuf);
249 if (n < 0 || !maybe_dnok(res, tbuf)) {
250 had_error++;
251 continue;
252 }
253 cp += n;
254 if (cp != erdata) goto no_recovery;
255 /* Get canonical name. */
256 n = (int) strlen(tbuf) + 1; /* for the \0 */
257 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
258 had_error++;
259 continue;
260 }
261 strlcpy(bp, tbuf, (size_t)(ep - bp));
262 tname = bp;
263 bp += n;
264 continue;
265 }
266 if (type != qtype) {
267 if (type != T_KEY && type != T_SIG)
Ken Chenffc224a2019-03-19 17:41:28 +0800268 LOG(DEBUG) << __func__ << ": asked for \"" << qname << " " << p_class(C_IN) << " "
269 << p_type(qtype) << "\", got type \"" << p_type(type) << "\"";
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900270 cp += n;
271 continue; /* XXX - had_error++ ? */
272 }
273 switch (type) {
274 case T_PTR:
275 if (strcasecmp(tname, bp) != 0) {
Ken Chenffc224a2019-03-19 17:41:28 +0800276 LOG(DEBUG) << __func__ << ": asked for \"" << qname << "\", got \"" << bp
277 << "\"";
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900278 cp += n;
279 continue; /* XXX - had_error++ ? */
280 }
281 n = dn_expand(answer->buf, eom, cp, bp, (int) (ep - bp));
282 if ((n < 0) || !maybe_hnok(res, bp)) {
283 had_error++;
284 break;
285 }
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900286 cp += n;
287 if (cp != erdata) goto no_recovery;
288 if (!haveanswer)
289 hent->h_name = bp;
290 else
lifrbe7dfbf2019-01-09 14:41:15 +0800291 aliases.push_back(bp);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900292 if (n != -1) {
293 n = (int) strlen(bp) + 1; /* for the \0 */
294 if (n >= MAXHOSTNAMELEN) {
295 had_error++;
296 break;
297 }
298 bp += n;
299 }
300 break;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900301 case T_A:
302 case T_AAAA:
303 if (strcasecmp(hent->h_name, bp) != 0) {
Ken Chenffc224a2019-03-19 17:41:28 +0800304 LOG(DEBUG) << __func__ << ": asked for \"" << hent->h_name << "\", got \"" << bp
305 << "\"";
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900306 cp += n;
307 continue; /* XXX - had_error++ ? */
308 }
309 if (n != hent->h_length) {
310 cp += n;
311 continue;
312 }
313 if (type == T_AAAA) {
314 struct in6_addr in6;
315 memcpy(&in6, cp, NS_IN6ADDRSZ);
316 if (IN6_IS_ADDR_V4MAPPED(&in6)) {
317 cp += n;
318 continue;
319 }
320 }
321 if (!haveanswer) {
322 int nn;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900323
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900324 hent->h_name = bp;
325 nn = (int) strlen(bp) + 1; /* for the \0 */
326 bp += nn;
327 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900328
chenbrucec51f1212019-09-12 16:59:33 +0800329 bp += sizeof(align) - (size_t)((uintptr_t)bp % sizeof(align));
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900330
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900331 if (bp + n >= ep) {
Ken Chenffc224a2019-03-19 17:41:28 +0800332 LOG(DEBUG) << __func__ << ": size (" << n << ") too big";
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900333 had_error++;
334 continue;
335 }
336 if (hap >= &addr_ptrs[MAXADDRS - 1]) {
337 if (!toobig++) {
Ken Chenffc224a2019-03-19 17:41:28 +0800338 LOG(DEBUG) << __func__ << ": Too many addresses (" << MAXADDRS << ")";
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900339 }
340 cp += n;
341 continue;
342 }
343 (void) memcpy(*hap++ = bp, cp, (size_t) n);
344 bp += n;
345 cp += n;
346 if (cp != erdata) goto no_recovery;
347 break;
348 default:
349 abort();
350 }
351 if (!had_error) haveanswer++;
352 }
353 if (haveanswer) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900354 *hap = NULL;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900355 if (!hent->h_name) {
356 n = (int) strlen(qname) + 1; /* for the \0 */
357 if (n > ep - bp || n >= MAXHOSTNAMELEN) goto no_recovery;
358 strlcpy(bp, qname, (size_t)(ep - bp));
359 hent->h_name = bp;
360 bp += n;
361 }
nuccachen9227b7f2018-09-18 13:38:48 +0800362 if (hent->h_addrtype == AF_INET) pad_v4v6_hostent(hent, &bp, ep);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900363 goto success;
364 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900365no_recovery:
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900366 *he = NO_RECOVERY;
367 return NULL;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900368success:
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900369 bp = (char*) ALIGN(bp);
lifrbe7dfbf2019-01-09 14:41:15 +0800370 aliases.push_back(nullptr);
371 qlen = aliases.size() * sizeof(*hent->h_aliases);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900372 if ((size_t)(ep - bp) < qlen) goto nospc;
Bernie Innocenti9c575932018-09-07 21:10:25 +0900373 hent->h_aliases = (char**) bp;
lifrbe7dfbf2019-01-09 14:41:15 +0800374 memcpy(bp, aliases.data(), qlen);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900375
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900376 bp += qlen;
377 n = (int) (hap - addr_ptrs);
378 qlen = (n + 1) * sizeof(*hent->h_addr_list);
379 if ((size_t)(ep - bp) < qlen) goto nospc;
Bernie Innocenti9c575932018-09-07 21:10:25 +0900380 hent->h_addr_list = (char**) bp;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900381 memcpy(bp, addr_ptrs, qlen);
382 *he = NETDB_SUCCESS;
383 return hent;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900384nospc:
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900385 errno = ENOSPC;
386 *he = NETDB_INTERNAL;
387 return NULL;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900388}
389
Bernie Innocenti36db7732019-10-11 18:03:02 +0900390int resolv_gethostbyname(const char* name, int af, hostent* hp, char* buf, size_t buflen,
391 const android_net_context* netcontext, hostent** result,
392 NetworkDnsEventReported* event) {
Mike Yuc7d55102018-11-06 19:20:07 +0800393 getnamaddr info;
Bernie Innocenti36db7732019-10-11 18:03:02 +0900394
Bernie Innocenti08487112019-10-11 21:14:13 +0900395 ResState res;
396 res_init(&res, netcontext, event);
Bernie Innocenti36db7732019-10-11 18:03:02 +0900397
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900398 size_t size;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900399 switch (af) {
400 case AF_INET:
401 size = NS_INADDRSZ;
402 break;
403 case AF_INET6:
404 size = NS_IN6ADDRSZ;
405 break;
406 default:
Mike Yuc7d55102018-11-06 19:20:07 +0800407 return EAI_FAMILY;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900408 }
409 if (buflen < size) goto nospc;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900410
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900411 hp->h_addrtype = af;
412 hp->h_length = (int) size;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900413
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900414 /*
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900415 * disallow names consisting only of digits/dots, unless
416 * they end in a dot.
417 */
chenbrucec51f1212019-09-12 16:59:33 +0800418 if (isdigit((uint8_t)name[0])) {
Bernie Innocenti2e8540b2018-09-26 11:52:04 +0900419 for (const char* cp = name;; ++cp) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900420 if (!*cp) {
421 if (*--cp == '.') break;
422 /*
423 * All-numeric, no dot at the end.
424 * Fake up a hostent as if we'd actually
425 * done a lookup.
426 */
427 goto fake;
428 }
chenbrucec51f1212019-09-12 16:59:33 +0800429 if (!isdigit((uint8_t)*cp) && *cp != '.') break;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900430 }
Bernie Innocenti2e8540b2018-09-26 11:52:04 +0900431 }
chenbrucec51f1212019-09-12 16:59:33 +0800432 if ((isxdigit((uint8_t)name[0]) && strchr(name, ':') != NULL) || name[0] == ':') {
Bernie Innocenti2e8540b2018-09-26 11:52:04 +0900433 for (const char* cp = name;; ++cp) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900434 if (!*cp) {
435 if (*--cp == '.') break;
436 /*
437 * All-IPv6-legal, no dot at the end.
438 * Fake up a hostent as if we'd actually
439 * done a lookup.
440 */
441 goto fake;
442 }
chenbrucec51f1212019-09-12 16:59:33 +0800443 if (!isxdigit((uint8_t)*cp) && *cp != ':' && *cp != '.') break;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900444 }
Bernie Innocenti2e8540b2018-09-26 11:52:04 +0900445 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900446
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900447 info.hp = hp;
448 info.buf = buf;
449 info.buflen = buflen;
Hungming Chen6c84a3d2018-12-26 16:14:17 +0800450 if (_hf_gethtbyname2(name, af, &info)) {
Bernie Innocenti08487112019-10-11 21:14:13 +0900451 int error = dns_gethtbyname(&res, name, af, &info);
Hungming Chena6914a62019-01-19 15:07:04 +0800452 if (error != 0) return error;
Bernie Innocentic939de02018-09-12 17:59:17 +0900453 }
Bernie Innocenti36db7732019-10-11 18:03:02 +0900454 *result = hp;
Mike Yuc7d55102018-11-06 19:20:07 +0800455 return 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900456nospc:
Hungming Chen6c84a3d2018-12-26 16:14:17 +0800457 return EAI_MEMORY;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900458fake:
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900459 HENT_ARRAY(hp->h_addr_list, 1, buf, buflen);
460 HENT_ARRAY(hp->h_aliases, 0, buf, buflen);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900461
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900462 hp->h_aliases[0] = NULL;
463 if (size > buflen) goto nospc;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900464
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900465 if (inet_pton(af, name, buf) <= 0) {
Mike Yuc7d55102018-11-06 19:20:07 +0800466 return EAI_NODATA;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900467 }
468 hp->h_addr_list[0] = buf;
469 hp->h_addr_list[1] = NULL;
470 buf += size;
471 buflen -= size;
472 HENT_SCOPY(hp->h_name, name, buf, buflen);
Bernie Innocenti36db7732019-10-11 18:03:02 +0900473 *result = hp;
Mike Yuc7d55102018-11-06 19:20:07 +0800474 return 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900475}
476
Bernie Innocenti324f9f82019-10-11 18:50:20 +0900477int resolv_gethostbyaddr(const void* addr, socklen_t len, int af, hostent* hp, char* buf,
478 size_t buflen, const struct android_net_context* netcontext,
479 hostent** result, NetworkDnsEventReported* event) {
chenbrucec51f1212019-09-12 16:59:33 +0800480 const uint8_t* uaddr = (const uint8_t*)addr;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900481 socklen_t size;
482 struct getnamaddr info;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900483
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900484 _DIAGASSERT(addr != NULL);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900485
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900486 if (af == AF_INET6 && len == NS_IN6ADDRSZ &&
487 (IN6_IS_ADDR_LINKLOCAL((const struct in6_addr*) addr) ||
488 IN6_IS_ADDR_SITELOCAL((const struct in6_addr*) addr))) {
Hungming Chen598202c2018-12-26 17:04:43 +0800489 return EAI_NODATA;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900490 }
491 if (af == AF_INET6 && len == NS_IN6ADDRSZ &&
492 (IN6_IS_ADDR_V4MAPPED((const struct in6_addr*) addr) ||
493 IN6_IS_ADDR_V4COMPAT((const struct in6_addr*) addr))) {
494 /* Unmap. */
495 uaddr += NS_IN6ADDRSZ - NS_INADDRSZ;
496 addr = uaddr;
497 af = AF_INET;
498 len = NS_INADDRSZ;
499 }
500 switch (af) {
501 case AF_INET:
502 size = NS_INADDRSZ;
503 break;
504 case AF_INET6:
505 size = NS_IN6ADDRSZ;
506 break;
507 default:
Hungming Chen598202c2018-12-26 17:04:43 +0800508 return EAI_FAMILY;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900509 }
510 if (size != len) {
Hungming Chena6914a62019-01-19 15:07:04 +0800511 // TODO: Consider converting to a private extended EAI_* error code.
512 // Currently, the EAI_* value has no corresponding error code for invalid argument socket
513 // length. In order to not rely on errno, convert the original error code pair, EAI_SYSTEM
514 // and EINVAL, to EAI_FAIL.
515 return EAI_FAIL;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900516 }
517 info.hp = hp;
518 info.buf = buf;
519 info.buflen = buflen;
Hungming Chen6c84a3d2018-12-26 16:14:17 +0800520 if (_hf_gethtbyaddr(uaddr, len, af, &info)) {
lifr94981782019-05-17 21:15:19 +0800521 int error = dns_gethtbyaddr(uaddr, len, af, netcontext, &info, event);
Hungming Chena6914a62019-01-19 15:07:04 +0800522 if (error != 0) return error;
Bernie Innocenti9f61dbd2018-09-12 20:03:11 +0900523 }
Bernie Innocenti324f9f82019-10-11 18:50:20 +0900524 *result = hp;
Hungming Chen598202c2018-12-26 17:04:43 +0800525 return 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900526}
527
Hungming Chena6914a62019-01-19 15:07:04 +0800528// TODO: Consider leaving function without returning error code as _gethtent() does because
529// the error code of the caller does not currently return to netd.
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900530struct hostent* netbsd_gethostent_r(FILE* hf, struct hostent* hent, char* buf, size_t buflen,
531 int* he) {
Bernie Innocenti9c575932018-09-07 21:10:25 +0900532 char *name;
lifrbe7dfbf2019-01-09 14:41:15 +0800533 char* cp;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900534 int af, len;
535 size_t anum;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900536 struct in6_addr host_addr;
lifrbe7dfbf2019-01-09 14:41:15 +0800537 std::vector<char*> aliases;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900538
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900539 if (hf == NULL) {
540 *he = NETDB_INTERNAL;
541 errno = EINVAL;
542 return NULL;
543 }
Bernie Innocenti9c575932018-09-07 21:10:25 +0900544 char* p = NULL;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900545
Bernie Innocenti36db7732019-10-11 18:03:02 +0900546 // Allocate a new space to read file lines like upstream does.
547 const size_t line_buf_size = MAXPACKET;
Bernie Innocenti9c575932018-09-07 21:10:25 +0900548 if ((p = (char*) malloc(line_buf_size)) == NULL) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900549 goto nospc;
550 }
551 for (;;) {
552 if (!fgets(p, line_buf_size, hf)) {
553 free(p);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900554 *he = HOST_NOT_FOUND;
555 return NULL;
556 }
557 if (*p == '#') {
558 continue;
559 }
560 if (!(cp = strpbrk(p, "#\n"))) {
561 continue;
562 }
563 *cp = '\0';
564 if (!(cp = strpbrk(p, " \t"))) continue;
565 *cp++ = '\0';
566 if (inet_pton(AF_INET6, p, &host_addr) > 0) {
567 af = AF_INET6;
568 len = NS_IN6ADDRSZ;
569 } else {
570 if (inet_pton(AF_INET, p, &host_addr) <= 0) continue;
chenbruce018fdb22019-06-12 18:08:04 +0800571 af = AF_INET;
572 len = NS_INADDRSZ;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900573 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900574
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900575 /* if this is not something we're looking for, skip it. */
576 if (hent->h_addrtype != 0 && hent->h_addrtype != af) continue;
577 if (hent->h_length != 0 && hent->h_length != len) continue;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900578
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900579 while (*cp == ' ' || *cp == '\t') cp++;
580 if ((cp = strpbrk(name = cp, " \t")) != NULL) *cp++ = '\0';
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900581 while (cp && *cp) {
582 if (*cp == ' ' || *cp == '\t') {
583 cp++;
584 continue;
585 }
lifrbe7dfbf2019-01-09 14:41:15 +0800586 aliases.push_back(cp);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900587 if ((cp = strpbrk(cp, " \t")) != NULL) *cp++ = '\0';
588 }
589 break;
590 }
591 hent->h_length = len;
592 hent->h_addrtype = af;
593 HENT_ARRAY(hent->h_addr_list, 1, buf, buflen);
lifrbe7dfbf2019-01-09 14:41:15 +0800594 anum = aliases.size();
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900595 HENT_ARRAY(hent->h_aliases, anum, buf, buflen);
596 HENT_COPY(hent->h_addr_list[0], &host_addr, hent->h_length, buf, buflen);
597 hent->h_addr_list[1] = NULL;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900598
nuccachenc28257d2018-09-11 11:20:00 +0800599 /* Reserve space for mapping IPv4 address to IPv6 address in place */
600 if (hent->h_addrtype == AF_INET) {
601 HENT_COPY(buf, NAT64_PAD, sizeof(NAT64_PAD), buf, buflen);
602 }
603
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900604 HENT_SCOPY(hent->h_name, name, buf, buflen);
605 for (size_t i = 0; i < anum; i++) HENT_SCOPY(hent->h_aliases[i], aliases[i], buf, buflen);
606 hent->h_aliases[anum] = NULL;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900607 *he = NETDB_SUCCESS;
608 free(p);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900609 return hent;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900610nospc:
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900611 free(p);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900612 errno = ENOSPC;
613 *he = NETDB_INTERNAL;
614 return NULL;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900615}
616
nuccachen9227b7f2018-09-18 13:38:48 +0800617static void convert_v4v6_hostent(struct hostent* hp, char** bpp, char* ep,
chenbruce99cbfd82019-09-05 15:08:13 +0800618 const std::function<void(struct hostent* hp)>& map_param,
619 const std::function<void(char* src, char* dst)>& map_addr) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900620 _DIAGASSERT(hp != NULL);
621 _DIAGASSERT(bpp != NULL);
622 _DIAGASSERT(ep != NULL);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900623
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900624 if (hp->h_addrtype != AF_INET || hp->h_length != NS_INADDRSZ) return;
nuccachen9227b7f2018-09-18 13:38:48 +0800625 map_param(hp);
626 for (char** ap = hp->h_addr_list; *ap; ap++) {
chenbrucec51f1212019-09-12 16:59:33 +0800627 int i = (int)(sizeof(align) - (size_t)((uintptr_t)*bpp % sizeof(align)));
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900628
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900629 if (ep - *bpp < (i + NS_IN6ADDRSZ)) {
630 /* Out of memory. Truncate address list here. XXX */
631 *ap = NULL;
632 return;
633 }
634 *bpp += i;
nuccachen9227b7f2018-09-18 13:38:48 +0800635 map_addr(*ap, *bpp);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900636 *ap = *bpp;
637 *bpp += NS_IN6ADDRSZ;
638 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900639}
640
nuccachen9227b7f2018-09-18 13:38:48 +0800641/* Reserve space for mapping IPv4 address to IPv6 address in place */
642static void pad_v4v6_hostent(struct hostent* hp, char** bpp, char* ep) {
643 convert_v4v6_hostent(hp, bpp, ep,
644 [](struct hostent* hp) {
645 (void) hp; /* unused */
646 },
647 [](char* src, char* dst) {
648 memcpy(dst, src, NS_INADDRSZ);
649 memcpy(dst + NS_INADDRSZ, NAT64_PAD, sizeof(NAT64_PAD));
650 });
651}
652
Bernie Innocenti08487112019-10-11 21:14:13 +0900653static int dns_gethtbyname(ResState* res, const char* name, int addr_type, getnamaddr* info) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900654 int n, type;
Bernie Innocentic939de02018-09-12 17:59:17 +0900655 info->hp->h_addrtype = addr_type;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900656
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900657 switch (info->hp->h_addrtype) {
658 case AF_INET:
659 info->hp->h_length = NS_INADDRSZ;
660 type = T_A;
661 break;
662 case AF_INET6:
663 info->hp->h_length = NS_IN6ADDRSZ;
664 type = T_AAAA;
665 break;
666 default:
Mike Yuc7d55102018-11-06 19:20:07 +0800667 return EAI_FAMILY;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900668 }
waynema0cac8c62019-03-08 16:58:06 +0800669 auto buf = std::make_unique<querybuf>();
670
Hungming Chena6914a62019-01-19 15:07:04 +0800671 int he;
672 n = res_nsearch(res, name, C_IN, type, buf->buf, (int)sizeof(buf->buf), &he);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900673 if (n < 0) {
Ken Chenffc224a2019-03-19 17:41:28 +0800674 LOG(DEBUG) << __func__ << ": res_nsearch failed (" << n << ")";
Hungming Chena6914a62019-01-19 15:07:04 +0800675 // Return h_errno (he) to catch more detailed errors rather than EAI_NODATA.
676 // Note that res_nsearch() doesn't set the pair NETDB_INTERNAL and errno.
677 // See also herrnoToAiErrno().
678 return herrnoToAiErrno(he);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900679 }
Bernie Innocenti3479d3b2019-10-08 22:23:26 +0900680 hostent* hp = getanswer(buf.get(), n, name, type, info->hp, info->buf, info->buflen, &he);
Hungming Chena6914a62019-01-19 15:07:04 +0800681 if (hp == NULL) return herrnoToAiErrno(he);
682
Mike Yuc7d55102018-11-06 19:20:07 +0800683 return 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900684}
685
waynema067e9842019-03-08 19:13:41 +0800686static int dns_gethtbyaddr(const unsigned char* uaddr, int len, int af,
lifr94981782019-05-17 21:15:19 +0800687 const android_net_context* netcontext, getnamaddr* info,
688 NetworkDnsEventReported* event) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900689 char qbuf[MAXDNAME + 1], *qp, *ep;
690 int n;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900691 int advance;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900692
Bernie Innocenti9f61dbd2018-09-12 20:03:11 +0900693 info->hp->h_length = len;
694 info->hp->h_addrtype = af;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900695
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900696 switch (info->hp->h_addrtype) {
697 case AF_INET:
698 (void) snprintf(qbuf, sizeof(qbuf), "%u.%u.%u.%u.in-addr.arpa", (uaddr[3] & 0xff),
699 (uaddr[2] & 0xff), (uaddr[1] & 0xff), (uaddr[0] & 0xff));
700 break;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900701
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900702 case AF_INET6:
703 qp = qbuf;
704 ep = qbuf + sizeof(qbuf) - 1;
705 for (n = NS_IN6ADDRSZ - 1; n >= 0; n--) {
706 advance = snprintf(qp, (size_t)(ep - qp), "%x.%x.", uaddr[n] & 0xf,
707 ((unsigned int) uaddr[n] >> 4) & 0xf);
708 if (advance > 0 && qp + advance < ep)
709 qp += advance;
710 else {
Hungming Chena6914a62019-01-19 15:07:04 +0800711 // TODO: Consider converting to a private extended EAI_* error code.
712 // Currently, the EAI_* value has no corresponding error code for an internal
713 // out of buffer space. In order to not rely on errno, convert the original
714 // error code EAI_SYSTEM to EAI_MEMORY.
715 return EAI_MEMORY;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900716 }
717 }
718 if (strlcat(qbuf, "ip6.arpa", sizeof(qbuf)) >= sizeof(qbuf)) {
Hungming Chena6914a62019-01-19 15:07:04 +0800719 // TODO: Consider converting to a private extended EAI_* error code.
720 // Currently, the EAI_* value has no corresponding error code for an internal
721 // out of buffer space. In order to not rely on errno, convert the original
722 // error code EAI_SYSTEM to EAI_MEMORY.
723 return EAI_MEMORY;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900724 }
725 break;
726 default:
Hungming Chen598202c2018-12-26 17:04:43 +0800727 return EAI_FAMILY;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900728 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900729
waynema067e9842019-03-08 19:13:41 +0800730 auto buf = std::make_unique<querybuf>();
731
Bernie Innocenti08487112019-10-11 21:14:13 +0900732 ResState res;
733 res_init(&res, netcontext, event);
Hungming Chena6914a62019-01-19 15:07:04 +0800734 int he;
Bernie Innocenti08487112019-10-11 21:14:13 +0900735 n = res_nquery(&res, qbuf, C_IN, T_PTR, buf->buf, (int)sizeof(buf->buf), &he);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900736 if (n < 0) {
Ken Chenffc224a2019-03-19 17:41:28 +0800737 LOG(DEBUG) << __func__ << ": res_nquery failed (" << n << ")";
Hungming Chena6914a62019-01-19 15:07:04 +0800738 // Note that res_nquery() doesn't set the pair NETDB_INTERNAL and errno.
739 // Return h_errno (he) to catch more detailed errors rather than EAI_NODATA.
740 // See also herrnoToAiErrno().
741 return herrnoToAiErrno(he);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900742 }
Bernie Innocenti3479d3b2019-10-08 22:23:26 +0900743 hostent* hp = getanswer(buf.get(), n, qbuf, T_PTR, info->hp, info->buf, info->buflen, &he);
Hungming Chena6914a62019-01-19 15:07:04 +0800744 if (hp == NULL) return herrnoToAiErrno(he);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900745
Bernie Innocenti9c575932018-09-07 21:10:25 +0900746 char* bf = (char*) (hp->h_addr_list + 2);
747 size_t blen = (size_t)(bf - info->buf);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900748 if (blen + info->hp->h_length > info->buflen) goto nospc;
749 hp->h_addr_list[0] = bf;
750 hp->h_addr_list[1] = NULL;
Bernie Innocenti9c575932018-09-07 21:10:25 +0900751 memcpy(bf, uaddr, (size_t) info->hp->h_length);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900752
nuccachenc28257d2018-09-11 11:20:00 +0800753 /* Reserve enough space for mapping IPv4 address to IPv6 address in place */
754 if (info->hp->h_addrtype == AF_INET) {
755 if (blen + NS_IN6ADDRSZ > info->buflen) goto nospc;
756 // Pad zero to the unused address space
757 memcpy(bf + NS_INADDRSZ, NAT64_PAD, sizeof(NAT64_PAD));
758 }
759
Hungming Chen598202c2018-12-26 17:04:43 +0800760 return 0;
Bernie Innocenti9c575932018-09-07 21:10:25 +0900761
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900762nospc:
Hungming Chen598202c2018-12-26 17:04:43 +0800763 return EAI_MEMORY;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900764}
765
Hungming Chena6914a62019-01-19 15:07:04 +0800766int herrnoToAiErrno(int he) {
767 switch (he) {
Hungming Chen947aab02018-12-27 18:33:19 +0800768 // extended h_errno
769 case NETD_RESOLV_H_ERRNO_EXT_TIMEOUT:
770 return NETD_RESOLV_TIMEOUT;
771 // legacy h_errno
772 case NETDB_SUCCESS:
773 return 0;
774 case HOST_NOT_FOUND: // TODO: Perhaps convert HOST_NOT_FOUND to EAI_NONAME instead
775 case NO_DATA: // NO_ADDRESS
Mike Yubfb1b342018-11-06 15:42:36 +0800776 return EAI_NODATA;
777 case TRY_AGAIN:
778 return EAI_AGAIN;
Hungming Chen947aab02018-12-27 18:33:19 +0800779 case NETDB_INTERNAL:
Hungming Chena6914a62019-01-19 15:07:04 +0800780 // TODO: Remove ENOSPC and call abort() immediately whenever any allocation fails.
781 if (errno == ENOSPC) return EAI_MEMORY;
782 // Theoretically, this should not happen. Leave this here just in case.
783 // Currently, getanswer() of {gethnamaddr, getaddrinfo}.cpp, res_nsearch() and
784 // res_searchN() use this function to convert error code. Only getanswer()
785 // of gethnamaddr.cpp may return the error code pair, herrno NETDB_INTERNAL and
786 // errno ENOSPC, which has already converted to EAI_MEMORY. The remaining functions
787 // don't set the pair herrno and errno.
Hungming Chen947aab02018-12-27 18:33:19 +0800788 return EAI_SYSTEM; // see errno for detail
789 case NO_RECOVERY:
Mike Yubfb1b342018-11-06 15:42:36 +0800790 default:
Hungming Chena6914a62019-01-19 15:07:04 +0800791 return EAI_FAIL; // TODO: Perhaps convert default to EAI_MAX (unknown error) instead
Mike Yubfb1b342018-11-06 15:42:36 +0800792 }
Bernie Innocentie71a28a2019-05-29 00:42:35 +0900793}