blob: 6a5f1161e1689c46b5d10395c77aae79fdeabdb0 [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 Innocenti10a90282020-01-23 23:28:00 +090077#include "res_comp.h"
78#include "res_debug.h" // p_class(), p_type()
Bernie Innocenti08487112019-10-11 21:14:13 +090079#include "res_init.h"
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090080#include "resolv_cache.h"
lifr94981782019-05-17 21:15:19 +080081#include "stats.pb.h"
82
83using android::net::NetworkDnsEventReported;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090084
Bernie Innocentiafaacf72018-08-30 07:34:37 +090085// NetBSD uses _DIAGASSERT to null-check arguments and the like,
86// but it's clear from the number of mistakes in their assertions
87// that they don't actually test or ship with this.
88#define _DIAGASSERT(e) /* nothing */
89
nuccachenc28257d2018-09-11 11:20:00 +080090// TODO: unify macro ALIGNBYTES and ALIGN for all possible data type alignment of hostent
91// buffer.
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090092#define ALIGNBYTES (sizeof(uintptr_t) - 1)
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090093#define ALIGN(p) (((uintptr_t)(p) + ALIGNBYTES) & ~ALIGNBYTES)
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090094
Bernie Innocentibf0b3bc2019-10-11 19:45:56 +090095constexpr int MAXADDRS = 35;
96
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090097typedef union {
98 HEADER hdr;
chenbrucec51f1212019-09-12 16:59:33 +080099 uint8_t buf[MAXPACKET];
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900100} querybuf;
101
102typedef union {
103 int32_t al;
104 char ac;
105} align;
106
nuccachen9227b7f2018-09-18 13:38:48 +0800107static void convert_v4v6_hostent(struct hostent* hp, char** bpp, char* ep,
chenbruce99cbfd82019-09-05 15:08:13 +0800108 const std::function<void(struct hostent* hp)>& mapping_param,
109 const std::function<void(char* src, char* dst)>& mapping_addr);
nuccachen9227b7f2018-09-18 13:38:48 +0800110static void pad_v4v6_hostent(struct hostent* hp, char** bpp, char* ep);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900111
waynema067e9842019-03-08 19:13:41 +0800112static int dns_gethtbyaddr(const unsigned char* uaddr, int len, int af,
lifr94981782019-05-17 21:15:19 +0800113 const android_net_context* netcontext, getnamaddr* info,
114 NetworkDnsEventReported* event);
Bernie Innocenti08487112019-10-11 21:14:13 +0900115static int dns_gethtbyname(ResState* res, const char* name, int af, getnamaddr* info);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900116
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900117#define BOUNDED_INCR(x) \
118 do { \
119 BOUNDS_CHECK(cp, x); \
120 cp += (x); \
Bernie Innocenti9c575932018-09-07 21:10:25 +0900121 } while (0)
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900122
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900123#define BOUNDS_CHECK(ptr, count) \
124 do { \
125 if (eom - (ptr) < (count)) goto no_recovery; \
Bernie Innocenti9c575932018-09-07 21:10:25 +0900126 } while (0)
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900127
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900128static struct hostent* getanswer(const querybuf* answer, int anslen, const char* qname, int qtype,
Bernie Innocenti3479d3b2019-10-08 22:23:26 +0900129 struct hostent* hent, char* buf, size_t buflen, int* he) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900130 const HEADER* hp;
chenbrucec51f1212019-09-12 16:59:33 +0800131 const uint8_t* cp;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900132 int n;
133 size_t qlen;
chenbrucec51f1212019-09-12 16:59:33 +0800134 const uint8_t *eom, *erdata;
lifrbe7dfbf2019-01-09 14:41:15 +0800135 char *bp, **hap, *ep;
Bernie Innocenti9c575932018-09-07 21:10:25 +0900136 int ancount, qdcount;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900137 int haveanswer, had_error;
138 int toobig = 0;
139 char tbuf[MAXDNAME];
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900140 char* addr_ptrs[MAXADDRS];
141 const char* tname;
lifrbe7dfbf2019-01-09 14:41:15 +0800142 std::vector<char*> aliases;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900143
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900144 _DIAGASSERT(answer != NULL);
145 _DIAGASSERT(qname != NULL);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900146
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900147 tname = qname;
148 hent->h_name = NULL;
149 eom = answer->buf + anslen;
Bernie Innocenti10a90282020-01-23 23:28:00 +0900150
151 bool (*name_ok)(const char* dn);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900152 switch (qtype) {
153 case T_A:
154 case T_AAAA:
155 name_ok = res_hnok;
156 break;
157 case T_PTR:
158 name_ok = res_dnok;
159 break;
160 default:
161 *he = NO_RECOVERY;
162 return NULL; /* XXX should be abort(); */
163 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900164
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900165 /*
166 * find first satisfactory answer
167 */
168 hp = &answer->hdr;
169 ancount = ntohs(hp->ancount);
170 qdcount = ntohs(hp->qdcount);
171 bp = buf;
172 ep = buf + buflen;
173 cp = answer->buf;
174 BOUNDED_INCR(HFIXEDSZ);
175 if (qdcount != 1) goto no_recovery;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900176
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900177 n = dn_expand(answer->buf, eom, cp, bp, (int) (ep - bp));
Bernie Innocenti10a90282020-01-23 23:28:00 +0900178 if ((n < 0) || !name_ok(bp)) goto no_recovery;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900179
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900180 BOUNDED_INCR(n + QFIXEDSZ);
181 if (qtype == T_A || qtype == T_AAAA) {
182 /* res_send() has already verified that the query name is the
183 * same as the one we sent; this just gets the expanded name
184 * (i.e., with the succeeding search-domain tacked on).
185 */
186 n = (int) strlen(bp) + 1; /* for the \0 */
187 if (n >= MAXHOSTNAMELEN) goto no_recovery;
188 hent->h_name = bp;
189 bp += n;
190 /* The qname can be abbreviated, but h_name is now absolute. */
191 qname = hent->h_name;
192 }
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900193 hent->h_addr_list = hap = addr_ptrs;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900194 *hap = NULL;
195 haveanswer = 0;
196 had_error = 0;
197 while (ancount-- > 0 && cp < eom && !had_error) {
198 n = dn_expand(answer->buf, eom, cp, bp, (int) (ep - bp));
Bernie Innocenti10a90282020-01-23 23:28:00 +0900199 if ((n < 0) || !name_ok(bp)) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900200 had_error++;
201 continue;
202 }
203 cp += n; /* name */
204 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
chenbruce0d470422019-03-28 18:44:37 +0800205 int type = ntohs(*reinterpret_cast<const uint16_t*>(cp));
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900206 cp += INT16SZ; /* type */
chenbruce0d470422019-03-28 18:44:37 +0800207 int cl = ntohs(*reinterpret_cast<const uint16_t*>(cp));
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900208 cp += INT16SZ + INT32SZ; /* class, TTL */
chenbruce0d470422019-03-28 18:44:37 +0800209 n = ntohs(*reinterpret_cast<const uint16_t*>(cp));
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900210 cp += INT16SZ; /* len */
211 BOUNDS_CHECK(cp, n);
212 erdata = cp + n;
Bernie Innocenti9c575932018-09-07 21:10:25 +0900213 if (cl != C_IN) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900214 /* XXX - debug? syslog? */
215 cp += n;
216 continue; /* XXX - had_error++ ? */
217 }
218 if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) {
219 n = dn_expand(answer->buf, eom, cp, tbuf, (int) sizeof tbuf);
Bernie Innocenti10a90282020-01-23 23:28:00 +0900220 if ((n < 0) || !name_ok(tbuf)) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900221 had_error++;
222 continue;
223 }
224 cp += n;
225 if (cp != erdata) goto no_recovery;
226 /* Store alias. */
lifrbe7dfbf2019-01-09 14:41:15 +0800227 aliases.push_back(bp);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900228 n = (int) strlen(bp) + 1; /* for the \0 */
229 if (n >= MAXHOSTNAMELEN) {
230 had_error++;
231 continue;
232 }
233 bp += n;
234 /* Get canonical name. */
235 n = (int) strlen(tbuf) + 1; /* for the \0 */
236 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
237 had_error++;
238 continue;
239 }
240 strlcpy(bp, tbuf, (size_t)(ep - bp));
241 hent->h_name = bp;
242 bp += n;
243 continue;
244 }
245 if (qtype == T_PTR && type == T_CNAME) {
246 n = dn_expand(answer->buf, eom, cp, tbuf, (int) sizeof tbuf);
Bernie Innocenti10a90282020-01-23 23:28:00 +0900247 if (n < 0 || !res_dnok(tbuf)) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900248 had_error++;
249 continue;
250 }
251 cp += n;
252 if (cp != erdata) goto no_recovery;
253 /* Get canonical name. */
254 n = (int) strlen(tbuf) + 1; /* for the \0 */
255 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
256 had_error++;
257 continue;
258 }
259 strlcpy(bp, tbuf, (size_t)(ep - bp));
260 tname = bp;
261 bp += n;
262 continue;
263 }
264 if (type != qtype) {
265 if (type != T_KEY && type != T_SIG)
Ken Chenffc224a2019-03-19 17:41:28 +0800266 LOG(DEBUG) << __func__ << ": asked for \"" << qname << " " << p_class(C_IN) << " "
267 << p_type(qtype) << "\", got type \"" << p_type(type) << "\"";
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900268 cp += n;
269 continue; /* XXX - had_error++ ? */
270 }
271 switch (type) {
272 case T_PTR:
273 if (strcasecmp(tname, bp) != 0) {
Ken Chenffc224a2019-03-19 17:41:28 +0800274 LOG(DEBUG) << __func__ << ": asked for \"" << qname << "\", got \"" << bp
275 << "\"";
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900276 cp += n;
277 continue; /* XXX - had_error++ ? */
278 }
279 n = dn_expand(answer->buf, eom, cp, bp, (int) (ep - bp));
Bernie Innocenti10a90282020-01-23 23:28:00 +0900280 if ((n < 0) || !res_hnok(bp)) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900281 had_error++;
282 break;
283 }
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900284 cp += n;
285 if (cp != erdata) goto no_recovery;
286 if (!haveanswer)
287 hent->h_name = bp;
288 else
lifrbe7dfbf2019-01-09 14:41:15 +0800289 aliases.push_back(bp);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900290 if (n != -1) {
291 n = (int) strlen(bp) + 1; /* for the \0 */
292 if (n >= MAXHOSTNAMELEN) {
293 had_error++;
294 break;
295 }
296 bp += n;
297 }
298 break;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900299 case T_A:
300 case T_AAAA:
301 if (strcasecmp(hent->h_name, bp) != 0) {
Ken Chenffc224a2019-03-19 17:41:28 +0800302 LOG(DEBUG) << __func__ << ": asked for \"" << hent->h_name << "\", got \"" << bp
303 << "\"";
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900304 cp += n;
305 continue; /* XXX - had_error++ ? */
306 }
307 if (n != hent->h_length) {
308 cp += n;
309 continue;
310 }
311 if (type == T_AAAA) {
312 struct in6_addr in6;
313 memcpy(&in6, cp, NS_IN6ADDRSZ);
314 if (IN6_IS_ADDR_V4MAPPED(&in6)) {
315 cp += n;
316 continue;
317 }
318 }
319 if (!haveanswer) {
320 int nn;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900321
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900322 hent->h_name = bp;
323 nn = (int) strlen(bp) + 1; /* for the \0 */
324 bp += nn;
325 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900326
chenbrucec51f1212019-09-12 16:59:33 +0800327 bp += sizeof(align) - (size_t)((uintptr_t)bp % sizeof(align));
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900328
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900329 if (bp + n >= ep) {
Ken Chenffc224a2019-03-19 17:41:28 +0800330 LOG(DEBUG) << __func__ << ": size (" << n << ") too big";
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900331 had_error++;
332 continue;
333 }
334 if (hap >= &addr_ptrs[MAXADDRS - 1]) {
335 if (!toobig++) {
Ken Chenffc224a2019-03-19 17:41:28 +0800336 LOG(DEBUG) << __func__ << ": Too many addresses (" << MAXADDRS << ")";
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900337 }
338 cp += n;
339 continue;
340 }
341 (void) memcpy(*hap++ = bp, cp, (size_t) n);
342 bp += n;
343 cp += n;
344 if (cp != erdata) goto no_recovery;
345 break;
346 default:
347 abort();
348 }
349 if (!had_error) haveanswer++;
350 }
351 if (haveanswer) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900352 *hap = NULL;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900353 if (!hent->h_name) {
354 n = (int) strlen(qname) + 1; /* for the \0 */
355 if (n > ep - bp || n >= MAXHOSTNAMELEN) goto no_recovery;
356 strlcpy(bp, qname, (size_t)(ep - bp));
357 hent->h_name = bp;
358 bp += n;
359 }
nuccachen9227b7f2018-09-18 13:38:48 +0800360 if (hent->h_addrtype == AF_INET) pad_v4v6_hostent(hent, &bp, ep);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900361 goto success;
362 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900363no_recovery:
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900364 *he = NO_RECOVERY;
365 return NULL;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900366success:
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900367 bp = (char*) ALIGN(bp);
lifrbe7dfbf2019-01-09 14:41:15 +0800368 aliases.push_back(nullptr);
369 qlen = aliases.size() * sizeof(*hent->h_aliases);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900370 if ((size_t)(ep - bp) < qlen) goto nospc;
Bernie Innocenti9c575932018-09-07 21:10:25 +0900371 hent->h_aliases = (char**) bp;
lifrbe7dfbf2019-01-09 14:41:15 +0800372 memcpy(bp, aliases.data(), qlen);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900373
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900374 bp += qlen;
375 n = (int) (hap - addr_ptrs);
376 qlen = (n + 1) * sizeof(*hent->h_addr_list);
377 if ((size_t)(ep - bp) < qlen) goto nospc;
Bernie Innocenti9c575932018-09-07 21:10:25 +0900378 hent->h_addr_list = (char**) bp;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900379 memcpy(bp, addr_ptrs, qlen);
380 *he = NETDB_SUCCESS;
381 return hent;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900382nospc:
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900383 errno = ENOSPC;
384 *he = NETDB_INTERNAL;
385 return NULL;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900386}
387
Bernie Innocenti36db7732019-10-11 18:03:02 +0900388int resolv_gethostbyname(const char* name, int af, hostent* hp, char* buf, size_t buflen,
389 const android_net_context* netcontext, hostent** result,
390 NetworkDnsEventReported* event) {
Bernie Innocentic0d8ada2020-11-04 12:51:22 +0900391 if (name == nullptr || hp == nullptr) {
392 return EAI_SYSTEM;
393 }
Bernie Innocenti36db7732019-10-11 18:03:02 +0900394
Bernie Innocentic0d8ada2020-11-04 12:51:22 +0900395 getnamaddr info;
Bernie Innocenti08487112019-10-11 21:14:13 +0900396 ResState res;
397 res_init(&res, netcontext, event);
Bernie Innocenti36db7732019-10-11 18:03:02 +0900398
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900399 size_t size;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900400 switch (af) {
401 case AF_INET:
402 size = NS_INADDRSZ;
403 break;
404 case AF_INET6:
405 size = NS_IN6ADDRSZ;
406 break;
407 default:
Mike Yuc7d55102018-11-06 19:20:07 +0800408 return EAI_FAMILY;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900409 }
410 if (buflen < size) goto nospc;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900411
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900412 hp->h_addrtype = af;
413 hp->h_length = (int) size;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900414
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900415 /*
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900416 * disallow names consisting only of digits/dots, unless
417 * they end in a dot.
418 */
chenbrucec51f1212019-09-12 16:59:33 +0800419 if (isdigit((uint8_t)name[0])) {
Bernie Innocenti2e8540b2018-09-26 11:52:04 +0900420 for (const char* cp = name;; ++cp) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900421 if (!*cp) {
422 if (*--cp == '.') break;
423 /*
424 * All-numeric, no dot at the end.
425 * Fake up a hostent as if we'd actually
426 * done a lookup.
427 */
428 goto fake;
429 }
chenbrucec51f1212019-09-12 16:59:33 +0800430 if (!isdigit((uint8_t)*cp) && *cp != '.') break;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900431 }
Bernie Innocenti2e8540b2018-09-26 11:52:04 +0900432 }
chenbrucec51f1212019-09-12 16:59:33 +0800433 if ((isxdigit((uint8_t)name[0]) && strchr(name, ':') != NULL) || name[0] == ':') {
Bernie Innocenti2e8540b2018-09-26 11:52:04 +0900434 for (const char* cp = name;; ++cp) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900435 if (!*cp) {
436 if (*--cp == '.') break;
437 /*
438 * All-IPv6-legal, no dot at the end.
439 * Fake up a hostent as if we'd actually
440 * done a lookup.
441 */
442 goto fake;
443 }
chenbrucec51f1212019-09-12 16:59:33 +0800444 if (!isxdigit((uint8_t)*cp) && *cp != ':' && *cp != '.') break;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900445 }
Bernie Innocenti2e8540b2018-09-26 11:52:04 +0900446 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900447
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900448 info.hp = hp;
449 info.buf = buf;
450 info.buflen = buflen;
Hungming Chen6c84a3d2018-12-26 16:14:17 +0800451 if (_hf_gethtbyname2(name, af, &info)) {
Bernie Innocenti08487112019-10-11 21:14:13 +0900452 int error = dns_gethtbyname(&res, name, af, &info);
Hungming Chena6914a62019-01-19 15:07:04 +0800453 if (error != 0) return error;
Bernie Innocentic939de02018-09-12 17:59:17 +0900454 }
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 +0900457nospc:
Hungming Chen6c84a3d2018-12-26 16:14:17 +0800458 return EAI_MEMORY;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900459fake:
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900460 HENT_ARRAY(hp->h_addr_list, 1, buf, buflen);
461 HENT_ARRAY(hp->h_aliases, 0, buf, buflen);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900462
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900463 hp->h_aliases[0] = NULL;
464 if (size > buflen) goto nospc;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900465
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900466 if (inet_pton(af, name, buf) <= 0) {
Mike Yuc7d55102018-11-06 19:20:07 +0800467 return EAI_NODATA;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900468 }
469 hp->h_addr_list[0] = buf;
470 hp->h_addr_list[1] = NULL;
471 buf += size;
472 buflen -= size;
473 HENT_SCOPY(hp->h_name, name, buf, buflen);
Bernie Innocenti36db7732019-10-11 18:03:02 +0900474 *result = hp;
Mike Yuc7d55102018-11-06 19:20:07 +0800475 return 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900476}
477
Bernie Innocenti324f9f82019-10-11 18:50:20 +0900478int resolv_gethostbyaddr(const void* addr, socklen_t len, int af, hostent* hp, char* buf,
479 size_t buflen, const struct android_net_context* netcontext,
480 hostent** result, NetworkDnsEventReported* event) {
chenbrucec51f1212019-09-12 16:59:33 +0800481 const uint8_t* uaddr = (const uint8_t*)addr;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900482 socklen_t size;
483 struct getnamaddr info;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900484
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900485 _DIAGASSERT(addr != NULL);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900486
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900487 if (af == AF_INET6 && len == NS_IN6ADDRSZ &&
488 (IN6_IS_ADDR_LINKLOCAL((const struct in6_addr*) addr) ||
489 IN6_IS_ADDR_SITELOCAL((const struct in6_addr*) addr))) {
Hungming Chen598202c2018-12-26 17:04:43 +0800490 return EAI_NODATA;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900491 }
492 if (af == AF_INET6 && len == NS_IN6ADDRSZ &&
493 (IN6_IS_ADDR_V4MAPPED((const struct in6_addr*) addr) ||
494 IN6_IS_ADDR_V4COMPAT((const struct in6_addr*) addr))) {
495 /* Unmap. */
496 uaddr += NS_IN6ADDRSZ - NS_INADDRSZ;
497 addr = uaddr;
498 af = AF_INET;
499 len = NS_INADDRSZ;
500 }
501 switch (af) {
502 case AF_INET:
503 size = NS_INADDRSZ;
504 break;
505 case AF_INET6:
506 size = NS_IN6ADDRSZ;
507 break;
508 default:
Hungming Chen598202c2018-12-26 17:04:43 +0800509 return EAI_FAMILY;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900510 }
511 if (size != len) {
Hungming Chena6914a62019-01-19 15:07:04 +0800512 // TODO: Consider converting to a private extended EAI_* error code.
513 // Currently, the EAI_* value has no corresponding error code for invalid argument socket
514 // length. In order to not rely on errno, convert the original error code pair, EAI_SYSTEM
515 // and EINVAL, to EAI_FAIL.
516 return EAI_FAIL;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900517 }
518 info.hp = hp;
519 info.buf = buf;
520 info.buflen = buflen;
Hungming Chen6c84a3d2018-12-26 16:14:17 +0800521 if (_hf_gethtbyaddr(uaddr, len, af, &info)) {
lifr94981782019-05-17 21:15:19 +0800522 int error = dns_gethtbyaddr(uaddr, len, af, netcontext, &info, event);
Hungming Chena6914a62019-01-19 15:07:04 +0800523 if (error != 0) return error;
Bernie Innocenti9f61dbd2018-09-12 20:03:11 +0900524 }
Bernie Innocenti324f9f82019-10-11 18:50:20 +0900525 *result = hp;
Hungming Chen598202c2018-12-26 17:04:43 +0800526 return 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900527}
528
Hungming Chena6914a62019-01-19 15:07:04 +0800529// TODO: Consider leaving function without returning error code as _gethtent() does because
530// the error code of the caller does not currently return to netd.
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900531struct hostent* netbsd_gethostent_r(FILE* hf, struct hostent* hent, char* buf, size_t buflen,
532 int* he) {
Bernie Innocenti9c575932018-09-07 21:10:25 +0900533 char *name;
lifrbe7dfbf2019-01-09 14:41:15 +0800534 char* cp;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900535 int af, len;
536 size_t anum;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900537 struct in6_addr host_addr;
lifrbe7dfbf2019-01-09 14:41:15 +0800538 std::vector<char*> aliases;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900539
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900540 if (hf == NULL) {
541 *he = NETDB_INTERNAL;
542 errno = EINVAL;
543 return NULL;
544 }
Bernie Innocenti9c575932018-09-07 21:10:25 +0900545 char* p = NULL;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900546
Bernie Innocenti36db7732019-10-11 18:03:02 +0900547 // Allocate a new space to read file lines like upstream does.
548 const size_t line_buf_size = MAXPACKET;
Bernie Innocenti9c575932018-09-07 21:10:25 +0900549 if ((p = (char*) malloc(line_buf_size)) == NULL) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900550 goto nospc;
551 }
552 for (;;) {
553 if (!fgets(p, line_buf_size, hf)) {
554 free(p);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900555 *he = HOST_NOT_FOUND;
556 return NULL;
557 }
558 if (*p == '#') {
559 continue;
560 }
561 if (!(cp = strpbrk(p, "#\n"))) {
562 continue;
563 }
564 *cp = '\0';
565 if (!(cp = strpbrk(p, " \t"))) continue;
566 *cp++ = '\0';
567 if (inet_pton(AF_INET6, p, &host_addr) > 0) {
568 af = AF_INET6;
569 len = NS_IN6ADDRSZ;
570 } else {
571 if (inet_pton(AF_INET, p, &host_addr) <= 0) continue;
chenbruce018fdb22019-06-12 18:08:04 +0800572 af = AF_INET;
573 len = NS_INADDRSZ;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900574 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900575
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900576 /* if this is not something we're looking for, skip it. */
577 if (hent->h_addrtype != 0 && hent->h_addrtype != af) continue;
578 if (hent->h_length != 0 && hent->h_length != len) continue;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900579
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900580 while (*cp == ' ' || *cp == '\t') cp++;
581 if ((cp = strpbrk(name = cp, " \t")) != NULL) *cp++ = '\0';
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900582 while (cp && *cp) {
583 if (*cp == ' ' || *cp == '\t') {
584 cp++;
585 continue;
586 }
lifrbe7dfbf2019-01-09 14:41:15 +0800587 aliases.push_back(cp);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900588 if ((cp = strpbrk(cp, " \t")) != NULL) *cp++ = '\0';
589 }
590 break;
591 }
592 hent->h_length = len;
593 hent->h_addrtype = af;
594 HENT_ARRAY(hent->h_addr_list, 1, buf, buflen);
lifrbe7dfbf2019-01-09 14:41:15 +0800595 anum = aliases.size();
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900596 HENT_ARRAY(hent->h_aliases, anum, buf, buflen);
597 HENT_COPY(hent->h_addr_list[0], &host_addr, hent->h_length, buf, buflen);
598 hent->h_addr_list[1] = NULL;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900599
nuccachenc28257d2018-09-11 11:20:00 +0800600 /* Reserve space for mapping IPv4 address to IPv6 address in place */
601 if (hent->h_addrtype == AF_INET) {
602 HENT_COPY(buf, NAT64_PAD, sizeof(NAT64_PAD), buf, buflen);
603 }
604
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900605 HENT_SCOPY(hent->h_name, name, buf, buflen);
606 for (size_t i = 0; i < anum; i++) HENT_SCOPY(hent->h_aliases[i], aliases[i], buf, buflen);
607 hent->h_aliases[anum] = NULL;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900608 *he = NETDB_SUCCESS;
609 free(p);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900610 return hent;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900611nospc:
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900612 free(p);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900613 errno = ENOSPC;
614 *he = NETDB_INTERNAL;
615 return NULL;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900616}
617
nuccachen9227b7f2018-09-18 13:38:48 +0800618static void convert_v4v6_hostent(struct hostent* hp, char** bpp, char* ep,
chenbruce99cbfd82019-09-05 15:08:13 +0800619 const std::function<void(struct hostent* hp)>& map_param,
620 const std::function<void(char* src, char* dst)>& map_addr) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900621 _DIAGASSERT(hp != NULL);
622 _DIAGASSERT(bpp != NULL);
623 _DIAGASSERT(ep != NULL);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900624
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900625 if (hp->h_addrtype != AF_INET || hp->h_length != NS_INADDRSZ) return;
nuccachen9227b7f2018-09-18 13:38:48 +0800626 map_param(hp);
627 for (char** ap = hp->h_addr_list; *ap; ap++) {
chenbrucec51f1212019-09-12 16:59:33 +0800628 int i = (int)(sizeof(align) - (size_t)((uintptr_t)*bpp % sizeof(align)));
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900629
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900630 if (ep - *bpp < (i + NS_IN6ADDRSZ)) {
631 /* Out of memory. Truncate address list here. XXX */
632 *ap = NULL;
633 return;
634 }
635 *bpp += i;
nuccachen9227b7f2018-09-18 13:38:48 +0800636 map_addr(*ap, *bpp);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900637 *ap = *bpp;
638 *bpp += NS_IN6ADDRSZ;
639 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900640}
641
nuccachen9227b7f2018-09-18 13:38:48 +0800642/* Reserve space for mapping IPv4 address to IPv6 address in place */
643static void pad_v4v6_hostent(struct hostent* hp, char** bpp, char* ep) {
644 convert_v4v6_hostent(hp, bpp, ep,
645 [](struct hostent* hp) {
646 (void) hp; /* unused */
647 },
648 [](char* src, char* dst) {
649 memcpy(dst, src, NS_INADDRSZ);
650 memcpy(dst + NS_INADDRSZ, NAT64_PAD, sizeof(NAT64_PAD));
651 });
652}
653
Bernie Innocenti08487112019-10-11 21:14:13 +0900654static int dns_gethtbyname(ResState* res, const char* name, int addr_type, getnamaddr* info) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900655 int n, type;
Bernie Innocentic939de02018-09-12 17:59:17 +0900656 info->hp->h_addrtype = addr_type;
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 info->hp->h_length = NS_INADDRSZ;
661 type = T_A;
662 break;
663 case AF_INET6:
664 info->hp->h_length = NS_IN6ADDRSZ;
665 type = T_AAAA;
666 break;
667 default:
Mike Yuc7d55102018-11-06 19:20:07 +0800668 return EAI_FAMILY;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900669 }
waynema0cac8c62019-03-08 16:58:06 +0800670 auto buf = std::make_unique<querybuf>();
671
Hungming Chena6914a62019-01-19 15:07:04 +0800672 int he;
673 n = res_nsearch(res, name, C_IN, type, buf->buf, (int)sizeof(buf->buf), &he);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900674 if (n < 0) {
Ken Chenffc224a2019-03-19 17:41:28 +0800675 LOG(DEBUG) << __func__ << ": res_nsearch failed (" << n << ")";
Hungming Chena6914a62019-01-19 15:07:04 +0800676 // Return h_errno (he) to catch more detailed errors rather than EAI_NODATA.
677 // Note that res_nsearch() doesn't set the pair NETDB_INTERNAL and errno.
678 // See also herrnoToAiErrno().
679 return herrnoToAiErrno(he);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900680 }
Bernie Innocenti3479d3b2019-10-08 22:23:26 +0900681 hostent* hp = getanswer(buf.get(), n, name, type, info->hp, info->buf, info->buflen, &he);
Hungming Chena6914a62019-01-19 15:07:04 +0800682 if (hp == NULL) return herrnoToAiErrno(he);
683
Mike Yuc7d55102018-11-06 19:20:07 +0800684 return 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900685}
686
waynema067e9842019-03-08 19:13:41 +0800687static int dns_gethtbyaddr(const unsigned char* uaddr, int len, int af,
lifr94981782019-05-17 21:15:19 +0800688 const android_net_context* netcontext, getnamaddr* info,
689 NetworkDnsEventReported* event) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900690 char qbuf[MAXDNAME + 1], *qp, *ep;
691 int n;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900692 int advance;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900693
Bernie Innocenti9f61dbd2018-09-12 20:03:11 +0900694 info->hp->h_length = len;
695 info->hp->h_addrtype = af;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900696
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900697 switch (info->hp->h_addrtype) {
698 case AF_INET:
699 (void) snprintf(qbuf, sizeof(qbuf), "%u.%u.%u.%u.in-addr.arpa", (uaddr[3] & 0xff),
700 (uaddr[2] & 0xff), (uaddr[1] & 0xff), (uaddr[0] & 0xff));
701 break;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900702
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900703 case AF_INET6:
704 qp = qbuf;
705 ep = qbuf + sizeof(qbuf) - 1;
706 for (n = NS_IN6ADDRSZ - 1; n >= 0; n--) {
707 advance = snprintf(qp, (size_t)(ep - qp), "%x.%x.", uaddr[n] & 0xf,
708 ((unsigned int) uaddr[n] >> 4) & 0xf);
709 if (advance > 0 && qp + advance < ep)
710 qp += advance;
711 else {
Hungming Chena6914a62019-01-19 15:07:04 +0800712 // TODO: Consider converting to a private extended EAI_* error code.
713 // Currently, the EAI_* value has no corresponding error code for an internal
714 // out of buffer space. In order to not rely on errno, convert the original
715 // error code EAI_SYSTEM to EAI_MEMORY.
716 return EAI_MEMORY;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900717 }
718 }
719 if (strlcat(qbuf, "ip6.arpa", sizeof(qbuf)) >= sizeof(qbuf)) {
Hungming Chena6914a62019-01-19 15:07:04 +0800720 // TODO: Consider converting to a private extended EAI_* error code.
721 // Currently, the EAI_* value has no corresponding error code for an internal
722 // out of buffer space. In order to not rely on errno, convert the original
723 // error code EAI_SYSTEM to EAI_MEMORY.
724 return EAI_MEMORY;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900725 }
726 break;
727 default:
Hungming Chen598202c2018-12-26 17:04:43 +0800728 return EAI_FAMILY;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900729 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900730
waynema067e9842019-03-08 19:13:41 +0800731 auto buf = std::make_unique<querybuf>();
732
Bernie Innocenti08487112019-10-11 21:14:13 +0900733 ResState res;
734 res_init(&res, netcontext, event);
Hungming Chena6914a62019-01-19 15:07:04 +0800735 int he;
Bernie Innocenti08487112019-10-11 21:14:13 +0900736 n = res_nquery(&res, qbuf, C_IN, T_PTR, buf->buf, (int)sizeof(buf->buf), &he);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900737 if (n < 0) {
Ken Chenffc224a2019-03-19 17:41:28 +0800738 LOG(DEBUG) << __func__ << ": res_nquery failed (" << n << ")";
Hungming Chena6914a62019-01-19 15:07:04 +0800739 // Note that res_nquery() doesn't set the pair NETDB_INTERNAL and errno.
740 // Return h_errno (he) to catch more detailed errors rather than EAI_NODATA.
741 // See also herrnoToAiErrno().
742 return herrnoToAiErrno(he);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900743 }
Bernie Innocenti3479d3b2019-10-08 22:23:26 +0900744 hostent* hp = getanswer(buf.get(), n, qbuf, T_PTR, info->hp, info->buf, info->buflen, &he);
Hungming Chena6914a62019-01-19 15:07:04 +0800745 if (hp == NULL) return herrnoToAiErrno(he);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900746
Bernie Innocenti9c575932018-09-07 21:10:25 +0900747 char* bf = (char*) (hp->h_addr_list + 2);
748 size_t blen = (size_t)(bf - info->buf);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900749 if (blen + info->hp->h_length > info->buflen) goto nospc;
750 hp->h_addr_list[0] = bf;
751 hp->h_addr_list[1] = NULL;
Bernie Innocenti9c575932018-09-07 21:10:25 +0900752 memcpy(bf, uaddr, (size_t) info->hp->h_length);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900753
nuccachenc28257d2018-09-11 11:20:00 +0800754 /* Reserve enough space for mapping IPv4 address to IPv6 address in place */
755 if (info->hp->h_addrtype == AF_INET) {
756 if (blen + NS_IN6ADDRSZ > info->buflen) goto nospc;
757 // Pad zero to the unused address space
758 memcpy(bf + NS_INADDRSZ, NAT64_PAD, sizeof(NAT64_PAD));
759 }
760
Hungming Chen598202c2018-12-26 17:04:43 +0800761 return 0;
Bernie Innocenti9c575932018-09-07 21:10:25 +0900762
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900763nospc:
Hungming Chen598202c2018-12-26 17:04:43 +0800764 return EAI_MEMORY;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900765}
766
Hungming Chena6914a62019-01-19 15:07:04 +0800767int herrnoToAiErrno(int he) {
768 switch (he) {
Hungming Chen947aab02018-12-27 18:33:19 +0800769 // extended h_errno
770 case NETD_RESOLV_H_ERRNO_EXT_TIMEOUT:
771 return NETD_RESOLV_TIMEOUT;
772 // legacy h_errno
773 case NETDB_SUCCESS:
774 return 0;
775 case HOST_NOT_FOUND: // TODO: Perhaps convert HOST_NOT_FOUND to EAI_NONAME instead
776 case NO_DATA: // NO_ADDRESS
Mike Yubfb1b342018-11-06 15:42:36 +0800777 return EAI_NODATA;
778 case TRY_AGAIN:
779 return EAI_AGAIN;
Hungming Chen947aab02018-12-27 18:33:19 +0800780 case NETDB_INTERNAL:
Hungming Chena6914a62019-01-19 15:07:04 +0800781 // TODO: Remove ENOSPC and call abort() immediately whenever any allocation fails.
782 if (errno == ENOSPC) return EAI_MEMORY;
783 // Theoretically, this should not happen. Leave this here just in case.
784 // Currently, getanswer() of {gethnamaddr, getaddrinfo}.cpp, res_nsearch() and
785 // res_searchN() use this function to convert error code. Only getanswer()
786 // of gethnamaddr.cpp may return the error code pair, herrno NETDB_INTERNAL and
787 // errno ENOSPC, which has already converted to EAI_MEMORY. The remaining functions
788 // don't set the pair herrno and errno.
Hungming Chen947aab02018-12-27 18:33:19 +0800789 return EAI_SYSTEM; // see errno for detail
790 case NO_RECOVERY:
Mike Yubfb1b342018-11-06 15:42:36 +0800791 default:
Hungming Chena6914a62019-01-19 15:07:04 +0800792 return EAI_FAIL; // TODO: Perhaps convert default to EAI_MAX (unknown error) instead
Mike Yubfb1b342018-11-06 15:42:36 +0800793 }
Bernie Innocentie71a28a2019-05-29 00:42:35 +0900794}