blob: f709ab9ae5c6b3e06b4b0cac5314e388a5b3c801 [file] [log] [blame]
Bernie Innocenti55864192018-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 Innocenti55864192018-08-30 04:05:20 +090054#include <arpa/inet.h>
55#include <arpa/nameser.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090056#include <assert.h>
57#include <ctype.h>
58#include <errno.h>
59#include <netdb.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090060#include <netinet/in.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090061#include <stdarg.h>
62#include <stdbool.h>
63#include <stdio.h>
Bernie Innocentic165ce82018-10-16 23:35:28 +090064#include <stdlib.h>
65#include <string.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090066#include <sys/param.h>
67#include <sys/socket.h>
Bernie Innocentif89b3512018-08-30 07:34:37 +090068#include <sys/types.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090069#include <sys/un.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090070#include <syslog.h>
71#include <unistd.h>
nuccachen0a511732018-09-18 13:38:48 +080072#include <functional>
lifr1bebdb52019-01-09 14:41:15 +080073#include <vector>
Bernie Innocentif89b3512018-08-30 07:34:37 +090074
Bernie Innocentic165ce82018-10-16 23:35:28 +090075#include "hostent.h"
Bernie Innocenti189eb502018-10-01 23:10:18 +090076#include "netd_resolv/resolv.h"
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090077#include "resolv_cache.h"
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090078#include "resolv_private.h"
Bernie Innocenti55864192018-08-30 04:05:20 +090079
Bernie Innocentif89b3512018-08-30 07:34:37 +090080// NetBSD uses _DIAGASSERT to null-check arguments and the like,
81// but it's clear from the number of mistakes in their assertions
82// that they don't actually test or ship with this.
83#define _DIAGASSERT(e) /* nothing */
84
nuccachen8161c992018-09-11 11:20:00 +080085// TODO: unify macro ALIGNBYTES and ALIGN for all possible data type alignment of hostent
86// buffer.
Bernie Innocenti55864192018-08-30 04:05:20 +090087#define ALIGNBYTES (sizeof(uintptr_t) - 1)
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090088#define ALIGN(p) (((uintptr_t)(p) + ALIGNBYTES) & ~ALIGNBYTES)
Bernie Innocenti55864192018-08-30 04:05:20 +090089
90#ifndef LOG_AUTH
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090091#define LOG_AUTH 0
Bernie Innocenti55864192018-08-30 04:05:20 +090092#endif
93
Bernie Innocenti55864192018-08-30 04:05:20 +090094
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090095#define maybe_ok(res, nm, ok) (((res)->options & RES_NOCHECKNAME) != 0U || (ok)(nm) != 0)
Bernie Innocenti55864192018-08-30 04:05:20 +090096#define maybe_hnok(res, hn) maybe_ok((res), (hn), res_hnok)
97#define maybe_dnok(res, dn) maybe_ok((res), (dn), res_dnok)
98
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090099static const char AskedForGot[] = "gethostby*.getanswer: asked for \"%s\", got \"%s\"";
Bernie Innocenti55864192018-08-30 04:05:20 +0900100
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900101#define MAXPACKET (8 * 1024)
Bernie Innocenti55864192018-08-30 04:05:20 +0900102
103typedef union {
104 HEADER hdr;
105 u_char buf[MAXPACKET];
106} querybuf;
107
108typedef union {
109 int32_t al;
110 char ac;
111} align;
112
113#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900114static void debugprintf(const char*, res_state, ...) __attribute__((__format__(__printf__, 1, 3)));
Bernie Innocenti55864192018-08-30 04:05:20 +0900115#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900116static struct hostent* getanswer(const querybuf*, int, const char*, int, res_state, struct hostent*,
117 char*, size_t, int*);
nuccachen0a511732018-09-18 13:38:48 +0800118static void convert_v4v6_hostent(struct hostent* hp, char** bpp, char* ep,
119 std::function<void(struct hostent* hp)> mapping_param,
120 std::function<void(char* src, char* dst)> mapping_addr);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900121static void map_v4v6_address(const char*, char*);
122static void map_v4v6_hostent(struct hostent*, char**, char*);
nuccachen0a511732018-09-18 13:38:48 +0800123static void pad_v4v6_hostent(struct hostent* hp, char** bpp, char* ep);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900124static void addrsort(char**, int, res_state);
Bernie Innocenti55864192018-08-30 04:05:20 +0900125
Hungming Chen806d4462018-12-26 17:04:43 +0800126static int _dns_gethtbyaddr(const unsigned char* uaddr, int len, int af,
127 const android_net_context* netcontext, getnamaddr* info);
Mike Yucac05e42018-11-06 19:20:07 +0800128static int _dns_gethtbyname(const char* name, int af, getnamaddr* info);
Bernie Innocenti55864192018-08-30 04:05:20 +0900129
Mike Yucac05e42018-11-06 19:20:07 +0800130static int gethostbyname_internal(const char* name, int af, res_state res, hostent* hp, char* hbuf,
Hungming Chen56273d72018-12-26 16:14:17 +0800131 size_t hbuflen, const android_net_context* netcontext);
Mike Yucac05e42018-11-06 19:20:07 +0800132static int gethostbyname_internal_real(const char* name, int af, res_state res, hostent* hp,
Hungming Chen56273d72018-12-26 16:14:17 +0800133 char* buf, size_t buflen);
Hungming Chen806d4462018-12-26 17:04:43 +0800134static int android_gethostbyaddrfornetcontext_proxy_internal(const void*, socklen_t, int,
Hungming Chen56273d72018-12-26 16:14:17 +0800135 struct hostent*, char*, size_t,
Hungming Chen806d4462018-12-26 17:04:43 +0800136 const struct android_net_context*);
137static int android_gethostbyaddrfornetcontext_proxy(const void* addr, socklen_t len, int af,
138 const struct android_net_context* netcontext,
139 hostent** hp);
Bernie Innocenti55864192018-08-30 04:05:20 +0900140
Bernie Innocenti55864192018-08-30 04:05:20 +0900141#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900142static void debugprintf(const char* msg, res_state res, ...) {
143 _DIAGASSERT(msg != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900144
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900145 if (res->options & RES_DEBUG) {
146 int save = errno;
147 va_list ap;
Bernie Innocenti55864192018-08-30 04:05:20 +0900148
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900149 va_start(ap, res);
150 vprintf(msg, ap);
151 va_end(ap);
Bernie Innocenti55864192018-08-30 04:05:20 +0900152
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900153 errno = save;
154 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900155}
156#else
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900157#define debugprintf(msg, res, num) /*nada*/
Bernie Innocenti55864192018-08-30 04:05:20 +0900158#endif
159
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900160#define BOUNDED_INCR(x) \
161 do { \
162 BOUNDS_CHECK(cp, x); \
163 cp += (x); \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900164 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900165
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900166#define BOUNDS_CHECK(ptr, count) \
167 do { \
168 if (eom - (ptr) < (count)) goto no_recovery; \
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900169 } while (0)
Bernie Innocenti55864192018-08-30 04:05:20 +0900170
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900171static struct hostent* getanswer(const querybuf* answer, int anslen, const char* qname, int qtype,
172 res_state res, struct hostent* hent, char* buf, size_t buflen,
173 int* he) {
174 const HEADER* hp;
175 const u_char* cp;
176 int n;
177 size_t qlen;
178 const u_char *eom, *erdata;
lifr1bebdb52019-01-09 14:41:15 +0800179 char *bp, **hap, *ep;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900180 int ancount, qdcount;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900181 int haveanswer, had_error;
182 int toobig = 0;
183 char tbuf[MAXDNAME];
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900184 char* addr_ptrs[MAXADDRS];
185 const char* tname;
186 int (*name_ok)(const char*);
lifr1bebdb52019-01-09 14:41:15 +0800187 std::vector<char*> aliases;
Bernie Innocenti55864192018-08-30 04:05:20 +0900188
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900189 _DIAGASSERT(answer != NULL);
190 _DIAGASSERT(qname != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900191
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900192 tname = qname;
193 hent->h_name = NULL;
194 eom = answer->buf + anslen;
195 switch (qtype) {
196 case T_A:
197 case T_AAAA:
198 name_ok = res_hnok;
199 break;
200 case T_PTR:
201 name_ok = res_dnok;
202 break;
203 default:
204 *he = NO_RECOVERY;
205 return NULL; /* XXX should be abort(); */
206 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900207
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900208 /*
209 * find first satisfactory answer
210 */
211 hp = &answer->hdr;
212 ancount = ntohs(hp->ancount);
213 qdcount = ntohs(hp->qdcount);
214 bp = buf;
215 ep = buf + buflen;
216 cp = answer->buf;
217 BOUNDED_INCR(HFIXEDSZ);
218 if (qdcount != 1) goto no_recovery;
Bernie Innocenti55864192018-08-30 04:05:20 +0900219
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900220 n = dn_expand(answer->buf, eom, cp, bp, (int) (ep - bp));
221 if ((n < 0) || !maybe_ok(res, bp, name_ok)) goto no_recovery;
Bernie Innocenti55864192018-08-30 04:05:20 +0900222
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900223 BOUNDED_INCR(n + QFIXEDSZ);
224 if (qtype == T_A || qtype == T_AAAA) {
225 /* res_send() has already verified that the query name is the
226 * same as the one we sent; this just gets the expanded name
227 * (i.e., with the succeeding search-domain tacked on).
228 */
229 n = (int) strlen(bp) + 1; /* for the \0 */
230 if (n >= MAXHOSTNAMELEN) goto no_recovery;
231 hent->h_name = bp;
232 bp += n;
233 /* The qname can be abbreviated, but h_name is now absolute. */
234 qname = hent->h_name;
235 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900236 hent->h_addr_list = hap = addr_ptrs;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900237 *hap = NULL;
238 haveanswer = 0;
239 had_error = 0;
240 while (ancount-- > 0 && cp < eom && !had_error) {
241 n = dn_expand(answer->buf, eom, cp, bp, (int) (ep - bp));
242 if ((n < 0) || !maybe_ok(res, bp, name_ok)) {
243 had_error++;
244 continue;
245 }
246 cp += n; /* name */
247 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
Bernie Innocentiee1b85b2018-09-25 14:23:19 +0900248 int type = ns_get16(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900249 cp += INT16SZ; /* type */
Bernie Innocentiee1b85b2018-09-25 14:23:19 +0900250 int cl = ns_get16(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900251 cp += INT16SZ + INT32SZ; /* class, TTL */
Bernie Innocentiee1b85b2018-09-25 14:23:19 +0900252 n = ns_get16(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900253 cp += INT16SZ; /* len */
254 BOUNDS_CHECK(cp, n);
255 erdata = cp + n;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900256 if (cl != C_IN) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900257 /* XXX - debug? syslog? */
258 cp += n;
259 continue; /* XXX - had_error++ ? */
260 }
261 if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) {
262 n = dn_expand(answer->buf, eom, cp, tbuf, (int) sizeof tbuf);
263 if ((n < 0) || !maybe_ok(res, tbuf, name_ok)) {
264 had_error++;
265 continue;
266 }
267 cp += n;
268 if (cp != erdata) goto no_recovery;
269 /* Store alias. */
lifr1bebdb52019-01-09 14:41:15 +0800270 aliases.push_back(bp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900271 n = (int) strlen(bp) + 1; /* for the \0 */
272 if (n >= MAXHOSTNAMELEN) {
273 had_error++;
274 continue;
275 }
276 bp += n;
277 /* Get canonical name. */
278 n = (int) strlen(tbuf) + 1; /* for the \0 */
279 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
280 had_error++;
281 continue;
282 }
283 strlcpy(bp, tbuf, (size_t)(ep - bp));
284 hent->h_name = bp;
285 bp += n;
286 continue;
287 }
288 if (qtype == T_PTR && type == T_CNAME) {
289 n = dn_expand(answer->buf, eom, cp, tbuf, (int) sizeof tbuf);
290 if (n < 0 || !maybe_dnok(res, tbuf)) {
291 had_error++;
292 continue;
293 }
294 cp += n;
295 if (cp != erdata) goto no_recovery;
296 /* Get canonical name. */
297 n = (int) strlen(tbuf) + 1; /* for the \0 */
298 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
299 had_error++;
300 continue;
301 }
302 strlcpy(bp, tbuf, (size_t)(ep - bp));
303 tname = bp;
304 bp += n;
305 continue;
306 }
307 if (type != qtype) {
308 if (type != T_KEY && type != T_SIG)
309 syslog(LOG_NOTICE | LOG_AUTH,
310 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"", qname,
311 p_class(C_IN), p_type(qtype), p_type(type));
312 cp += n;
313 continue; /* XXX - had_error++ ? */
314 }
315 switch (type) {
316 case T_PTR:
317 if (strcasecmp(tname, bp) != 0) {
318 syslog(LOG_NOTICE | LOG_AUTH, AskedForGot, qname, bp);
319 cp += n;
320 continue; /* XXX - had_error++ ? */
321 }
322 n = dn_expand(answer->buf, eom, cp, bp, (int) (ep - bp));
323 if ((n < 0) || !maybe_hnok(res, bp)) {
324 had_error++;
325 break;
326 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900327 cp += n;
328 if (cp != erdata) goto no_recovery;
329 if (!haveanswer)
330 hent->h_name = bp;
331 else
lifr1bebdb52019-01-09 14:41:15 +0800332 aliases.push_back(bp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900333 if (n != -1) {
334 n = (int) strlen(bp) + 1; /* for the \0 */
335 if (n >= MAXHOSTNAMELEN) {
336 had_error++;
337 break;
338 }
339 bp += n;
340 }
341 break;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900342 case T_A:
343 case T_AAAA:
344 if (strcasecmp(hent->h_name, bp) != 0) {
345 syslog(LOG_NOTICE | LOG_AUTH, AskedForGot, hent->h_name, bp);
346 cp += n;
347 continue; /* XXX - had_error++ ? */
348 }
349 if (n != hent->h_length) {
350 cp += n;
351 continue;
352 }
353 if (type == T_AAAA) {
354 struct in6_addr in6;
355 memcpy(&in6, cp, NS_IN6ADDRSZ);
356 if (IN6_IS_ADDR_V4MAPPED(&in6)) {
357 cp += n;
358 continue;
359 }
360 }
361 if (!haveanswer) {
362 int nn;
Bernie Innocenti55864192018-08-30 04:05:20 +0900363
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900364 hent->h_name = bp;
365 nn = (int) strlen(bp) + 1; /* for the \0 */
366 bp += nn;
367 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900368
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900369 bp += sizeof(align) - (size_t)((u_long) bp % sizeof(align));
Bernie Innocenti55864192018-08-30 04:05:20 +0900370
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900371 if (bp + n >= ep) {
372 debugprintf("size (%d) too big\n", res, n);
373 had_error++;
374 continue;
375 }
376 if (hap >= &addr_ptrs[MAXADDRS - 1]) {
377 if (!toobig++) {
378 debugprintf("Too many addresses (%d)\n", res, MAXADDRS);
379 }
380 cp += n;
381 continue;
382 }
383 (void) memcpy(*hap++ = bp, cp, (size_t) n);
384 bp += n;
385 cp += n;
386 if (cp != erdata) goto no_recovery;
387 break;
388 default:
389 abort();
390 }
391 if (!had_error) haveanswer++;
392 }
393 if (haveanswer) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900394 *hap = NULL;
395 /*
396 * Note: we sort even if host can take only one address
397 * in its return structures - should give it the "best"
398 * address in that case, not some random one
399 */
400 if (res->nsort && haveanswer > 1 && qtype == T_A) addrsort(addr_ptrs, haveanswer, res);
401 if (!hent->h_name) {
402 n = (int) strlen(qname) + 1; /* for the \0 */
403 if (n > ep - bp || n >= MAXHOSTNAMELEN) goto no_recovery;
404 strlcpy(bp, qname, (size_t)(ep - bp));
405 hent->h_name = bp;
406 bp += n;
407 }
408 if (res->options & RES_USE_INET6) map_v4v6_hostent(hent, &bp, ep);
nuccachen0a511732018-09-18 13:38:48 +0800409 if (hent->h_addrtype == AF_INET) pad_v4v6_hostent(hent, &bp, ep);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900410 goto success;
411 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900412no_recovery:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900413 *he = NO_RECOVERY;
414 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900415success:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900416 bp = (char*) ALIGN(bp);
lifr1bebdb52019-01-09 14:41:15 +0800417 aliases.push_back(nullptr);
418 qlen = aliases.size() * sizeof(*hent->h_aliases);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900419 if ((size_t)(ep - bp) < qlen) goto nospc;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900420 hent->h_aliases = (char**) bp;
lifr1bebdb52019-01-09 14:41:15 +0800421 memcpy(bp, aliases.data(), qlen);
Bernie Innocenti55864192018-08-30 04:05:20 +0900422
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900423 bp += qlen;
424 n = (int) (hap - addr_ptrs);
425 qlen = (n + 1) * sizeof(*hent->h_addr_list);
426 if ((size_t)(ep - bp) < qlen) goto nospc;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900427 hent->h_addr_list = (char**) bp;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900428 memcpy(bp, addr_ptrs, qlen);
429 *he = NETDB_SUCCESS;
430 return hent;
Bernie Innocenti55864192018-08-30 04:05:20 +0900431nospc:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900432 errno = ENOSPC;
433 *he = NETDB_INTERNAL;
434 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900435}
436
Mike Yucac05e42018-11-06 19:20:07 +0800437static int gethostbyname_internal_real(const char* name, int af, res_state res, hostent* hp,
Hungming Chen56273d72018-12-26 16:14:17 +0800438 char* buf, size_t buflen) {
Mike Yucac05e42018-11-06 19:20:07 +0800439 getnamaddr info;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900440 size_t size;
Bernie Innocenti55864192018-08-30 04:05:20 +0900441
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900442 _DIAGASSERT(name != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900443
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900444 switch (af) {
445 case AF_INET:
446 size = NS_INADDRSZ;
447 break;
448 case AF_INET6:
449 size = NS_IN6ADDRSZ;
450 break;
451 default:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900452 errno = EAFNOSUPPORT;
Mike Yucac05e42018-11-06 19:20:07 +0800453 return EAI_FAMILY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900454 }
455 if (buflen < size) goto nospc;
Bernie Innocenti55864192018-08-30 04:05:20 +0900456
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900457 hp->h_addrtype = af;
458 hp->h_length = (int) size;
Bernie Innocenti55864192018-08-30 04:05:20 +0900459
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900460 /*
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900461 * disallow names consisting only of digits/dots, unless
462 * they end in a dot.
463 */
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900464 if (isdigit((u_char) name[0])) {
465 for (const char* cp = name;; ++cp) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900466 if (!*cp) {
467 if (*--cp == '.') break;
468 /*
469 * All-numeric, no dot at the end.
470 * Fake up a hostent as if we'd actually
471 * done a lookup.
472 */
473 goto fake;
474 }
475 if (!isdigit((u_char) *cp) && *cp != '.') break;
476 }
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900477 }
478 if ((isxdigit((u_char) name[0]) && strchr(name, ':') != NULL) || name[0] == ':') {
479 for (const char* cp = name;; ++cp) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900480 if (!*cp) {
481 if (*--cp == '.') break;
482 /*
483 * All-IPv6-legal, no dot at the end.
484 * Fake up a hostent as if we'd actually
485 * done a lookup.
486 */
487 goto fake;
488 }
489 if (!isxdigit((u_char) *cp) && *cp != ':' && *cp != '.') break;
490 }
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900491 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900492
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900493 info.hp = hp;
494 info.buf = buf;
495 info.buflen = buflen;
Hungming Chen56273d72018-12-26 16:14:17 +0800496 if (_hf_gethtbyname2(name, af, &info)) {
Mike Yucac05e42018-11-06 19:20:07 +0800497 int error = _dns_gethtbyname(name, af, &info);
498 if (error != 0) {
499 return error;
Bernie Innocenti9bf0e1d2018-09-12 17:59:17 +0900500 }
501 }
Mike Yucac05e42018-11-06 19:20:07 +0800502 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900503nospc:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900504 errno = ENOSPC;
Hungming Chen56273d72018-12-26 16:14:17 +0800505 return EAI_MEMORY;
Bernie Innocenti55864192018-08-30 04:05:20 +0900506fake:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900507 HENT_ARRAY(hp->h_addr_list, 1, buf, buflen);
508 HENT_ARRAY(hp->h_aliases, 0, buf, buflen);
Bernie Innocenti55864192018-08-30 04:05:20 +0900509
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900510 hp->h_aliases[0] = NULL;
511 if (size > buflen) goto nospc;
Bernie Innocenti55864192018-08-30 04:05:20 +0900512
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900513 if (inet_pton(af, name, buf) <= 0) {
Mike Yucac05e42018-11-06 19:20:07 +0800514 return EAI_NODATA;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900515 }
516 hp->h_addr_list[0] = buf;
517 hp->h_addr_list[1] = NULL;
518 buf += size;
519 buflen -= size;
520 HENT_SCOPY(hp->h_name, name, buf, buflen);
521 if (res->options & RES_USE_INET6) map_v4v6_hostent(hp, &buf, buf + buflen);
Mike Yucac05e42018-11-06 19:20:07 +0800522 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900523}
524
525// very similar in proxy-ness to android_getaddrinfo_proxy
Mike Yucac05e42018-11-06 19:20:07 +0800526static int gethostbyname_internal(const char* name, int af, res_state res, hostent* hp, char* hbuf,
Hungming Chen56273d72018-12-26 16:14:17 +0800527 size_t hbuflen, const android_net_context* netcontext) {
Bernie Innocentif89b3512018-08-30 07:34:37 +0900528 res_setnetcontext(res, netcontext);
Hungming Chen56273d72018-12-26 16:14:17 +0800529 return gethostbyname_internal_real(name, af, res, hp, hbuf, hbuflen);
Bernie Innocenti55864192018-08-30 04:05:20 +0900530}
531
Hungming Chen806d4462018-12-26 17:04:43 +0800532static int android_gethostbyaddrfornetcontext_real(const void* addr, socklen_t len, int af,
533 struct hostent* hp, char* buf, size_t buflen,
Hungming Chen806d4462018-12-26 17:04:43 +0800534 const struct android_net_context* netcontext) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900535 const u_char* uaddr = (const u_char*) addr;
536 socklen_t size;
537 struct getnamaddr info;
Bernie Innocenti55864192018-08-30 04:05:20 +0900538
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900539 _DIAGASSERT(addr != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900540
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900541 if (af == AF_INET6 && len == NS_IN6ADDRSZ &&
542 (IN6_IS_ADDR_LINKLOCAL((const struct in6_addr*) addr) ||
543 IN6_IS_ADDR_SITELOCAL((const struct in6_addr*) addr))) {
Hungming Chen806d4462018-12-26 17:04:43 +0800544 return EAI_NODATA;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900545 }
546 if (af == AF_INET6 && len == NS_IN6ADDRSZ &&
547 (IN6_IS_ADDR_V4MAPPED((const struct in6_addr*) addr) ||
548 IN6_IS_ADDR_V4COMPAT((const struct in6_addr*) addr))) {
549 /* Unmap. */
550 uaddr += NS_IN6ADDRSZ - NS_INADDRSZ;
551 addr = uaddr;
552 af = AF_INET;
553 len = NS_INADDRSZ;
554 }
555 switch (af) {
556 case AF_INET:
557 size = NS_INADDRSZ;
558 break;
559 case AF_INET6:
560 size = NS_IN6ADDRSZ;
561 break;
562 default:
563 errno = EAFNOSUPPORT;
Hungming Chen806d4462018-12-26 17:04:43 +0800564 return EAI_FAMILY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900565 }
566 if (size != len) {
567 errno = EINVAL;
Hungming Chen806d4462018-12-26 17:04:43 +0800568 // TODO: Consider to remap error code without relying on errno.
569 return EAI_SYSTEM;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900570 }
571 info.hp = hp;
572 info.buf = buf;
573 info.buflen = buflen;
Hungming Chen56273d72018-12-26 16:14:17 +0800574 if (_hf_gethtbyaddr(uaddr, len, af, &info)) {
Hungming Chen806d4462018-12-26 17:04:43 +0800575 int error = _dns_gethtbyaddr(uaddr, len, af, netcontext, &info);
576 if (error != 0) {
577 return error;
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900578 }
579 }
Hungming Chen806d4462018-12-26 17:04:43 +0800580 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900581}
582
Hungming Chen806d4462018-12-26 17:04:43 +0800583static int android_gethostbyaddrfornetcontext_proxy_internal(
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900584 const void* addr, socklen_t len, int af, struct hostent* hp, char* hbuf, size_t hbuflen,
Hungming Chen56273d72018-12-26 16:14:17 +0800585 const struct android_net_context* netcontext) {
586 return android_gethostbyaddrfornetcontext_real(addr, len, af, hp, hbuf, hbuflen, netcontext);
Bernie Innocenti55864192018-08-30 04:05:20 +0900587}
588
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900589struct hostent* netbsd_gethostent_r(FILE* hf, struct hostent* hent, char* buf, size_t buflen,
590 int* he) {
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900591 const size_t line_buf_size = sizeof(res_get_static()->hostbuf);
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900592 char *name;
lifr1bebdb52019-01-09 14:41:15 +0800593 char* cp;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900594 int af, len;
595 size_t anum;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900596 struct in6_addr host_addr;
lifr1bebdb52019-01-09 14:41:15 +0800597 std::vector<char*> aliases;
Bernie Innocenti55864192018-08-30 04:05:20 +0900598
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900599 if (hf == NULL) {
600 *he = NETDB_INTERNAL;
601 errno = EINVAL;
602 return NULL;
603 }
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900604 char* p = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900605
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900606 /* Allocate a new space to read file lines like upstream does.
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900607 * To keep reentrancy we cannot use res_get_static()->hostbuf here,
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900608 * as the buffer may be used to store content for a previous hostent
609 * returned by non-reentrant functions like gethostbyname().
610 */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900611 if ((p = (char*) malloc(line_buf_size)) == NULL) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900612 goto nospc;
613 }
614 for (;;) {
615 if (!fgets(p, line_buf_size, hf)) {
616 free(p);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900617 *he = HOST_NOT_FOUND;
618 return NULL;
619 }
620 if (*p == '#') {
621 continue;
622 }
623 if (!(cp = strpbrk(p, "#\n"))) {
624 continue;
625 }
626 *cp = '\0';
627 if (!(cp = strpbrk(p, " \t"))) continue;
628 *cp++ = '\0';
629 if (inet_pton(AF_INET6, p, &host_addr) > 0) {
630 af = AF_INET6;
631 len = NS_IN6ADDRSZ;
632 } else {
633 if (inet_pton(AF_INET, p, &host_addr) <= 0) continue;
Bernie Innocenti55864192018-08-30 04:05:20 +0900634
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900635 res_state res = res_get_state();
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900636 if (res == NULL) goto nospc;
637 if (res->options & RES_USE_INET6) {
638 map_v4v6_address(buf, buf);
639 af = AF_INET6;
640 len = NS_IN6ADDRSZ;
641 } else {
642 af = AF_INET;
643 len = NS_INADDRSZ;
644 }
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900645 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900646
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900647 /* if this is not something we're looking for, skip it. */
648 if (hent->h_addrtype != 0 && hent->h_addrtype != af) continue;
649 if (hent->h_length != 0 && hent->h_length != len) continue;
Bernie Innocenti55864192018-08-30 04:05:20 +0900650
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900651 while (*cp == ' ' || *cp == '\t') cp++;
652 if ((cp = strpbrk(name = cp, " \t")) != NULL) *cp++ = '\0';
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900653 while (cp && *cp) {
654 if (*cp == ' ' || *cp == '\t') {
655 cp++;
656 continue;
657 }
lifr1bebdb52019-01-09 14:41:15 +0800658 aliases.push_back(cp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900659 if ((cp = strpbrk(cp, " \t")) != NULL) *cp++ = '\0';
660 }
661 break;
662 }
663 hent->h_length = len;
664 hent->h_addrtype = af;
665 HENT_ARRAY(hent->h_addr_list, 1, buf, buflen);
lifr1bebdb52019-01-09 14:41:15 +0800666 anum = aliases.size();
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900667 HENT_ARRAY(hent->h_aliases, anum, buf, buflen);
668 HENT_COPY(hent->h_addr_list[0], &host_addr, hent->h_length, buf, buflen);
669 hent->h_addr_list[1] = NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900670
nuccachen8161c992018-09-11 11:20:00 +0800671 /* Reserve space for mapping IPv4 address to IPv6 address in place */
672 if (hent->h_addrtype == AF_INET) {
673 HENT_COPY(buf, NAT64_PAD, sizeof(NAT64_PAD), buf, buflen);
674 }
675
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900676 HENT_SCOPY(hent->h_name, name, buf, buflen);
677 for (size_t i = 0; i < anum; i++) HENT_SCOPY(hent->h_aliases[i], aliases[i], buf, buflen);
678 hent->h_aliases[anum] = NULL;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900679 *he = NETDB_SUCCESS;
680 free(p);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900681 return hent;
Bernie Innocenti55864192018-08-30 04:05:20 +0900682nospc:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900683 free(p);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900684 errno = ENOSPC;
685 *he = NETDB_INTERNAL;
686 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900687}
688
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900689static void map_v4v6_address(const char* src, char* dst) {
690 u_char* p = (u_char*) dst;
691 char tmp[NS_INADDRSZ];
692 int i;
Bernie Innocenti55864192018-08-30 04:05:20 +0900693
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900694 _DIAGASSERT(src != NULL);
695 _DIAGASSERT(dst != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900696
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900697 /* Stash a temporary copy so our caller can update in place. */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900698 memcpy(tmp, src, NS_INADDRSZ);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900699 /* Mark this ipv6 addr as a mapped ipv4. */
700 for (i = 0; i < 10; i++) *p++ = 0x00;
701 *p++ = 0xff;
702 *p++ = 0xff;
703 /* Retrieve the saved copy and we're done. */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900704 memcpy(p, tmp, NS_INADDRSZ);
Bernie Innocenti55864192018-08-30 04:05:20 +0900705}
706
nuccachen0a511732018-09-18 13:38:48 +0800707static void convert_v4v6_hostent(struct hostent* hp, char** bpp, char* ep,
708 std::function<void(struct hostent* hp)> map_param,
709 std::function<void(char* src, char* dst)> map_addr) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900710 _DIAGASSERT(hp != NULL);
711 _DIAGASSERT(bpp != NULL);
712 _DIAGASSERT(ep != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900713
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900714 if (hp->h_addrtype != AF_INET || hp->h_length != NS_INADDRSZ) return;
nuccachen0a511732018-09-18 13:38:48 +0800715 map_param(hp);
716 for (char** ap = hp->h_addr_list; *ap; ap++) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900717 int i = (int) (sizeof(align) - (size_t)((u_long) *bpp % sizeof(align)));
Bernie Innocenti55864192018-08-30 04:05:20 +0900718
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900719 if (ep - *bpp < (i + NS_IN6ADDRSZ)) {
720 /* Out of memory. Truncate address list here. XXX */
721 *ap = NULL;
722 return;
723 }
724 *bpp += i;
nuccachen0a511732018-09-18 13:38:48 +0800725 map_addr(*ap, *bpp);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900726 *ap = *bpp;
727 *bpp += NS_IN6ADDRSZ;
728 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900729}
730
nuccachen0a511732018-09-18 13:38:48 +0800731static void map_v4v6_hostent(struct hostent* hp, char** bpp, char* ep) {
732 convert_v4v6_hostent(hp, bpp, ep,
733 [](struct hostent* hp) {
734 hp->h_addrtype = AF_INET6;
735 hp->h_length = NS_IN6ADDRSZ;
736 },
737 [](char* src, char* dst) { map_v4v6_address(src, dst); });
738}
739
740/* Reserve space for mapping IPv4 address to IPv6 address in place */
741static void pad_v4v6_hostent(struct hostent* hp, char** bpp, char* ep) {
742 convert_v4v6_hostent(hp, bpp, ep,
743 [](struct hostent* hp) {
744 (void) hp; /* unused */
745 },
746 [](char* src, char* dst) {
747 memcpy(dst, src, NS_INADDRSZ);
748 memcpy(dst + NS_INADDRSZ, NAT64_PAD, sizeof(NAT64_PAD));
749 });
750}
751
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900752static void addrsort(char** ap, int num, res_state res) {
753 int i, j;
754 char** p;
755 short aval[MAXADDRS];
756 int needsort = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900757
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900758 _DIAGASSERT(ap != NULL);
Bernie Innocenti55864192018-08-30 04:05:20 +0900759
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900760 p = ap;
761 for (i = 0; i < num; i++, p++) {
762 for (j = 0; (unsigned) j < res->nsort; j++)
763 if (res->sort_list[j].addr.s_addr ==
764 (((struct in_addr*) (void*) (*p))->s_addr & res->sort_list[j].mask))
765 break;
766 aval[i] = j;
767 if (needsort == 0 && i > 0 && j < aval[i - 1]) needsort = i;
768 }
769 if (!needsort) return;
Bernie Innocenti55864192018-08-30 04:05:20 +0900770
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900771 while (needsort < num) {
772 for (j = needsort - 1; j >= 0; j--) {
773 if (aval[j] > aval[j + 1]) {
774 char* hp;
Bernie Innocenti55864192018-08-30 04:05:20 +0900775
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900776 i = aval[j];
777 aval[j] = aval[j + 1];
778 aval[j + 1] = i;
Bernie Innocenti55864192018-08-30 04:05:20 +0900779
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900780 hp = ap[j];
781 ap[j] = ap[j + 1];
782 ap[j + 1] = hp;
783 } else
784 break;
785 }
786 needsort++;
787 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900788}
789
Mike Yucac05e42018-11-06 19:20:07 +0800790static int _dns_gethtbyname(const char* name, int addr_type, getnamaddr* info) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900791 int n, type;
792 struct hostent* hp;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900793 res_state res;
Bernie Innocenti55864192018-08-30 04:05:20 +0900794
Bernie Innocenti9bf0e1d2018-09-12 17:59:17 +0900795 info->hp->h_addrtype = addr_type;
Bernie Innocenti55864192018-08-30 04:05:20 +0900796
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900797 switch (info->hp->h_addrtype) {
798 case AF_INET:
799 info->hp->h_length = NS_INADDRSZ;
800 type = T_A;
801 break;
802 case AF_INET6:
803 info->hp->h_length = NS_IN6ADDRSZ;
804 type = T_AAAA;
805 break;
806 default:
Mike Yucac05e42018-11-06 19:20:07 +0800807 return EAI_FAMILY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900808 }
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900809 querybuf* buf = (querybuf*) malloc(sizeof(querybuf));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900810 if (buf == NULL) {
Mike Yucac05e42018-11-06 19:20:07 +0800811 return EAI_MEMORY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900812 }
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900813 res = res_get_state();
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900814 if (res == NULL) {
815 free(buf);
Mike Yucac05e42018-11-06 19:20:07 +0800816 return EAI_MEMORY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900817 }
Mike Yu69615f62018-11-06 15:42:36 +0800818
Hungming Chen56273d72018-12-26 16:14:17 +0800819 int herrno = NETDB_INTERNAL;
Hungming Chen7f0d3292018-12-27 18:33:19 +0800820 n = res_nsearch(res, name, C_IN, type, buf->buf, (int) sizeof(buf->buf), &herrno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900821 if (n < 0) {
822 free(buf);
823 debugprintf("res_nsearch failed (%d)\n", res, n);
Hungming Chen7f0d3292018-12-27 18:33:19 +0800824 // Pass herrno to catch more detailed errors rather than EAI_NODATA.
825 return herrnoToAiErrno(herrno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900826 }
Hungming Chen56273d72018-12-26 16:14:17 +0800827 hp = getanswer(buf, n, name, type, res, info->hp, info->buf, info->buflen, &herrno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900828 free(buf);
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900829 if (hp == NULL) {
Hungming Chen7f0d3292018-12-27 18:33:19 +0800830 return herrnoToAiErrno(herrno);
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900831 }
Mike Yucac05e42018-11-06 19:20:07 +0800832 return 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900833}
834
Hungming Chen806d4462018-12-26 17:04:43 +0800835static int _dns_gethtbyaddr(const unsigned char* uaddr, int len, int af,
836 const android_net_context* netcontext, getnamaddr* info) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900837 char qbuf[MAXDNAME + 1], *qp, *ep;
838 int n;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900839 struct hostent* hp;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900840 int advance;
841 res_state res;
Bernie Innocenti55864192018-08-30 04:05:20 +0900842
Bernie Innocentia2cbfb12018-09-12 20:03:11 +0900843 info->hp->h_length = len;
844 info->hp->h_addrtype = af;
Bernie Innocenti55864192018-08-30 04:05:20 +0900845
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900846 switch (info->hp->h_addrtype) {
847 case AF_INET:
848 (void) snprintf(qbuf, sizeof(qbuf), "%u.%u.%u.%u.in-addr.arpa", (uaddr[3] & 0xff),
849 (uaddr[2] & 0xff), (uaddr[1] & 0xff), (uaddr[0] & 0xff));
850 break;
Bernie Innocenti55864192018-08-30 04:05:20 +0900851
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900852 case AF_INET6:
853 qp = qbuf;
854 ep = qbuf + sizeof(qbuf) - 1;
855 for (n = NS_IN6ADDRSZ - 1; n >= 0; n--) {
856 advance = snprintf(qp, (size_t)(ep - qp), "%x.%x.", uaddr[n] & 0xf,
857 ((unsigned int) uaddr[n] >> 4) & 0xf);
858 if (advance > 0 && qp + advance < ep)
859 qp += advance;
860 else {
Hungming Chen806d4462018-12-26 17:04:43 +0800861 // TODO: Consider to remap error code without relying on errno.
862 return EAI_SYSTEM;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900863 }
864 }
865 if (strlcat(qbuf, "ip6.arpa", sizeof(qbuf)) >= sizeof(qbuf)) {
Hungming Chen806d4462018-12-26 17:04:43 +0800866 // TODO: Consider to remap error code without relying on errno.
867 return EAI_SYSTEM;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900868 }
869 break;
870 default:
Hungming Chen806d4462018-12-26 17:04:43 +0800871 return EAI_FAMILY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900872 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900873
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900874 querybuf* buf = (querybuf*) malloc(sizeof(querybuf));
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900875 if (buf == NULL) {
Hungming Chen806d4462018-12-26 17:04:43 +0800876 return EAI_MEMORY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900877 }
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900878 res = res_get_state();
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900879 if (res == NULL) {
880 free(buf);
Hungming Chen806d4462018-12-26 17:04:43 +0800881 return EAI_MEMORY;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900882 }
883 res_setnetcontext(res, netcontext);
Hungming Chen56273d72018-12-26 16:14:17 +0800884 int herrno = NETDB_INTERNAL;
Hungming Chen7f0d3292018-12-27 18:33:19 +0800885 n = res_nquery(res, qbuf, C_IN, T_PTR, buf->buf, (int) sizeof(buf->buf), &herrno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900886 if (n < 0) {
887 free(buf);
888 debugprintf("res_nquery failed (%d)\n", res, n);
Hungming Chen7f0d3292018-12-27 18:33:19 +0800889 return herrnoToAiErrno(herrno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900890 }
Hungming Chen56273d72018-12-26 16:14:17 +0800891 hp = getanswer(buf, n, qbuf, T_PTR, res, info->hp, info->buf, info->buflen, &herrno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900892 free(buf);
893 if (hp == NULL) {
Hungming Chen7f0d3292018-12-27 18:33:19 +0800894 return herrnoToAiErrno(herrno);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900895 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900896
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900897 char* bf = (char*) (hp->h_addr_list + 2);
898 size_t blen = (size_t)(bf - info->buf);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900899 if (blen + info->hp->h_length > info->buflen) goto nospc;
900 hp->h_addr_list[0] = bf;
901 hp->h_addr_list[1] = NULL;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900902 memcpy(bf, uaddr, (size_t) info->hp->h_length);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900903 if (info->hp->h_addrtype == AF_INET && (res->options & RES_USE_INET6)) {
904 if (blen + NS_IN6ADDRSZ > info->buflen) goto nospc;
905 map_v4v6_address(bf, bf);
906 hp->h_addrtype = AF_INET6;
907 hp->h_length = NS_IN6ADDRSZ;
908 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900909
nuccachen8161c992018-09-11 11:20:00 +0800910 /* Reserve enough space for mapping IPv4 address to IPv6 address in place */
911 if (info->hp->h_addrtype == AF_INET) {
912 if (blen + NS_IN6ADDRSZ > info->buflen) goto nospc;
913 // Pad zero to the unused address space
914 memcpy(bf + NS_INADDRSZ, NAT64_PAD, sizeof(NAT64_PAD));
915 }
916
Hungming Chen806d4462018-12-26 17:04:43 +0800917 return 0;
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900918
Bernie Innocenti55864192018-08-30 04:05:20 +0900919nospc:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900920 errno = ENOSPC;
Hungming Chen806d4462018-12-26 17:04:43 +0800921 return EAI_MEMORY;
Bernie Innocenti55864192018-08-30 04:05:20 +0900922}
923
Bernie Innocenti55864192018-08-30 04:05:20 +0900924/*
925 * Non-reentrant versions.
926 */
927
Mike Yucac05e42018-11-06 19:20:07 +0800928int android_gethostbynamefornetcontext(const char* name, int af,
929 const struct android_net_context* netcontext, hostent** hp) {
930 int error;
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900931 res_state res = res_get_state();
Mike Yucac05e42018-11-06 19:20:07 +0800932 if (res == NULL) return EAI_MEMORY;
933 res_static* rs = res_get_static(); // For thread-safety.
934 error = gethostbyname_internal(name, af, res, &rs->host, rs->hostbuf, sizeof(rs->hostbuf),
Hungming Chen56273d72018-12-26 16:14:17 +0800935 netcontext);
Mike Yucac05e42018-11-06 19:20:07 +0800936 if (error == 0) {
937 *hp = &rs->host;
938 }
939 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900940}
941
Hungming Chen806d4462018-12-26 17:04:43 +0800942int android_gethostbyaddrfornetcontext(const void* addr, socklen_t len, int af,
943 const struct android_net_context* netcontext, hostent** hp) {
944 return android_gethostbyaddrfornetcontext_proxy(addr, len, af, netcontext, hp);
Bernie Innocenti55864192018-08-30 04:05:20 +0900945}
946
Hungming Chen806d4462018-12-26 17:04:43 +0800947static int android_gethostbyaddrfornetcontext_proxy(const void* addr, socklen_t len, int af,
948 const struct android_net_context* netcontext,
949 hostent** hp) {
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900950 struct res_static* rs = res_get_static(); // For thread-safety.
Hungming Chen806d4462018-12-26 17:04:43 +0800951 int error = android_gethostbyaddrfornetcontext_proxy_internal(
Hungming Chen56273d72018-12-26 16:14:17 +0800952 addr, len, af, &rs->host, rs->hostbuf, sizeof(rs->hostbuf), netcontext);
Hungming Chen806d4462018-12-26 17:04:43 +0800953 if (error == 0) *hp = &rs->host;
954 return error;
Bernie Innocenti55864192018-08-30 04:05:20 +0900955}
Mike Yu69615f62018-11-06 15:42:36 +0800956
Hungming Chen7f0d3292018-12-27 18:33:19 +0800957int herrnoToAiErrno(int herrno) {
958 switch (herrno) {
959 // extended h_errno
960 case NETD_RESOLV_H_ERRNO_EXT_TIMEOUT:
961 return NETD_RESOLV_TIMEOUT;
962 // legacy h_errno
963 case NETDB_SUCCESS:
964 return 0;
965 case HOST_NOT_FOUND: // TODO: Perhaps convert HOST_NOT_FOUND to EAI_NONAME instead
966 case NO_DATA: // NO_ADDRESS
Mike Yu69615f62018-11-06 15:42:36 +0800967 return EAI_NODATA;
968 case TRY_AGAIN:
969 return EAI_AGAIN;
Hungming Chen7f0d3292018-12-27 18:33:19 +0800970 case NETDB_INTERNAL:
971 return EAI_SYSTEM; // see errno for detail
972 case NO_RECOVERY:
Mike Yu69615f62018-11-06 15:42:36 +0800973 default:
974 return EAI_FAIL;
975 }
Hungming Chen7f0d3292018-12-27 18:33:19 +0800976}