2014-12-17 02:03:44 +01:00
|
|
|
/* $NetBSD: gethnamaddr.c,v 1.91 2014/06/19 15:08:18 christos Exp $ */
|
2009-03-04 04:28:35 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* ++Copyright++ 1985, 1988, 1993
|
|
|
|
* -
|
|
|
|
* Copyright (c) 1985, 1988, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
* -
|
|
|
|
* Portions Copyright (c) 1993 by Digital Equipment Corporation.
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies, and that
|
|
|
|
* the name of Digital Equipment Corporation not be used in advertising or
|
|
|
|
* publicity pertaining to distribution of the document or software without
|
|
|
|
* specific, written prior permission.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
|
|
|
|
* WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
|
|
|
|
* CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
|
|
|
|
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
|
|
|
|
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
|
|
|
|
* SOFTWARE.
|
|
|
|
* -
|
|
|
|
* --Copyright--
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/socket.h>
|
2011-08-23 14:34:14 +02:00
|
|
|
#include <sys/un.h>
|
2009-03-04 04:28:35 +01:00
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
2014-03-04 16:01:29 +01:00
|
|
|
#include <arpa/nameser.h>
|
2014-05-15 20:43:07 +02:00
|
|
|
#include "NetdClientDispatch.h"
|
2014-02-14 23:07:23 +01:00
|
|
|
#include "resolv_netid.h"
|
2009-03-04 04:28:35 +01:00
|
|
|
#include "resolv_private.h"
|
|
|
|
#include "resolv_cache.h"
|
|
|
|
#include <assert.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <stdarg.h>
|
2014-12-10 23:56:46 +01:00
|
|
|
#include <stdbool.h>
|
2009-03-04 04:28:35 +01:00
|
|
|
#include <stdio.h>
|
2011-03-22 04:01:03 +01:00
|
|
|
#include <strings.h>
|
2009-03-04 04:28:35 +01:00
|
|
|
#include <syslog.h>
|
2011-08-23 14:34:14 +02:00
|
|
|
#include <unistd.h>
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2014-03-20 16:21:32 +01:00
|
|
|
#define ALIGNBYTES (sizeof(uintptr_t) - 1)
|
|
|
|
#define ALIGN(p) (((uintptr_t)(p) + ALIGNBYTES) &~ ALIGNBYTES)
|
|
|
|
|
2009-03-04 04:28:35 +01:00
|
|
|
#ifndef LOG_AUTH
|
|
|
|
# define LOG_AUTH 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define MULTI_PTRS_ARE_ALIASES 1 /* XXX - experimental */
|
|
|
|
|
|
|
|
#include "nsswitch.h"
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2014-12-17 02:03:44 +01:00
|
|
|
#include "hostent.h"
|
|
|
|
|
|
|
|
#define maybe_ok(res, nm, ok) (((res)->options & RES_NOCHECKNAME) != 0U || \
|
|
|
|
(ok)(nm) != 0)
|
|
|
|
#define maybe_hnok(res, hn) maybe_ok((res), (hn), res_hnok)
|
|
|
|
#define maybe_dnok(res, dn) maybe_ok((res), (dn), res_dnok)
|
|
|
|
|
|
|
|
#define addalias(d, s, arr, siz) do { \
|
|
|
|
if (d >= &arr[siz]) { \
|
|
|
|
char **xptr = realloc(arr, (siz + 10) * sizeof(*arr)); \
|
|
|
|
if (xptr == NULL) \
|
|
|
|
goto nospc; \
|
|
|
|
d = xptr + (d - arr); \
|
|
|
|
arr = xptr; \
|
|
|
|
siz += 10; \
|
|
|
|
} \
|
|
|
|
*d++ = s; \
|
|
|
|
} while (/*CONSTCOND*/0)
|
|
|
|
|
|
|
|
#define setup(arr, siz) do { \
|
|
|
|
arr = malloc((siz = 10) * sizeof(*arr)); \
|
|
|
|
if (arr == NULL) \
|
|
|
|
goto nospc; \
|
|
|
|
} while (/*CONSTCOND*/0)
|
|
|
|
|
2011-08-23 14:34:14 +02:00
|
|
|
// This should be synchronized to ResponseCode.h
|
|
|
|
static const int DnsProxyQueryResult = 222;
|
|
|
|
|
2014-07-07 18:44:17 +02:00
|
|
|
static const char AskedForGot[] =
|
2009-03-04 04:28:35 +01:00
|
|
|
"gethostby*.getanswer: asked for \"%s\", got \"%s\"";
|
|
|
|
|
|
|
|
#define MAXPACKET (64*1024)
|
|
|
|
|
|
|
|
typedef union {
|
|
|
|
HEADER hdr;
|
|
|
|
u_char buf[MAXPACKET];
|
|
|
|
} querybuf;
|
|
|
|
|
|
|
|
typedef union {
|
|
|
|
int32_t al;
|
|
|
|
char ac;
|
|
|
|
} align;
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
2014-12-17 02:03:44 +01:00
|
|
|
static void debugprintf(const char *, res_state, ...)
|
2009-03-04 04:28:35 +01:00
|
|
|
__attribute__((__format__(__printf__, 1, 3)));
|
|
|
|
#endif
|
|
|
|
static struct hostent *getanswer(const querybuf *, int, const char *, int,
|
2014-12-17 02:03:44 +01:00
|
|
|
res_state, struct hostent *, char *, size_t, int *);
|
2009-03-04 04:28:35 +01:00
|
|
|
static void map_v4v6_address(const char *, char *);
|
|
|
|
static void map_v4v6_hostent(struct hostent *, char **, char *);
|
|
|
|
static void addrsort(char **, int, res_state);
|
|
|
|
|
|
|
|
void ht_sethostent(int);
|
|
|
|
void ht_endhostent(void);
|
|
|
|
struct hostent *ht_gethostbyname(char *);
|
|
|
|
struct hostent *ht_gethostbyaddr(const char *, int, int);
|
|
|
|
void dns_service(void);
|
|
|
|
#undef dn_skipname
|
|
|
|
int dn_skipname(const u_char *, const u_char *);
|
2010-09-27 17:37:10 +02:00
|
|
|
static int _dns_gethtbyaddr(void *, void *, va_list);
|
|
|
|
static int _dns_gethtbyname(void *, void *, va_list);
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2014-12-17 02:03:44 +01:00
|
|
|
static struct hostent *gethostbyname_internal(const char *, int, res_state,
|
|
|
|
struct hostent *, char *, size_t, int *, unsigned, unsigned);
|
|
|
|
static struct hostent* android_gethostbyaddrfornet_proxy_internal(const void*, socklen_t,
|
|
|
|
int, struct hostent *, char *, size_t, int *, unsigned, unsigned);
|
2009-03-04 04:28:35 +01:00
|
|
|
|
|
|
|
static const ns_src default_dns_files[] = {
|
|
|
|
{ NSSRC_FILES, NS_SUCCESS },
|
|
|
|
{ NSSRC_DNS, NS_SUCCESS },
|
|
|
|
{ 0, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
static void
|
2014-12-17 02:03:44 +01:00
|
|
|
debugprintf(const char *msg, res_state res, ...)
|
2009-03-04 04:28:35 +01:00
|
|
|
{
|
2014-12-17 02:03:44 +01:00
|
|
|
_DIAGASSERT(msg != NULL);
|
2009-03-04 04:28:35 +01:00
|
|
|
|
|
|
|
if (res->options & RES_DEBUG) {
|
|
|
|
int save = errno;
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start (ap, res);
|
|
|
|
vprintf(msg, ap);
|
|
|
|
va_end (ap);
|
|
|
|
|
|
|
|
errno = save;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
2014-12-17 02:03:44 +01:00
|
|
|
# define debugprintf(msg, res, num) /*nada*/
|
2009-03-04 04:28:35 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#define BOUNDED_INCR(x) \
|
|
|
|
do { \
|
|
|
|
cp += (x); \
|
2014-12-17 02:03:44 +01:00
|
|
|
if (cp > eom) \
|
|
|
|
goto no_recovery; \
|
2009-03-04 04:28:35 +01:00
|
|
|
} while (/*CONSTCOND*/0)
|
|
|
|
|
|
|
|
#define BOUNDS_CHECK(ptr, count) \
|
|
|
|
do { \
|
2014-12-17 02:03:44 +01:00
|
|
|
if ((ptr) + (count) > eom) \
|
|
|
|
goto no_recovery; \
|
2009-03-04 04:28:35 +01:00
|
|
|
} while (/*CONSTCOND*/0)
|
|
|
|
|
|
|
|
static struct hostent *
|
|
|
|
getanswer(const querybuf *answer, int anslen, const char *qname, int qtype,
|
2014-12-17 02:03:44 +01:00
|
|
|
res_state res, struct hostent *hent, char *buf, size_t buflen, int *he)
|
2009-03-04 04:28:35 +01:00
|
|
|
{
|
|
|
|
const HEADER *hp;
|
|
|
|
const u_char *cp;
|
|
|
|
int n;
|
2014-12-17 02:03:44 +01:00
|
|
|
size_t qlen;
|
2009-03-04 04:28:35 +01:00
|
|
|
const u_char *eom, *erdata;
|
|
|
|
char *bp, **ap, **hap, *ep;
|
|
|
|
int type, class, ancount, qdcount;
|
|
|
|
int haveanswer, had_error;
|
|
|
|
int toobig = 0;
|
|
|
|
char tbuf[MAXDNAME];
|
2014-12-17 02:03:44 +01:00
|
|
|
char **aliases;
|
|
|
|
size_t maxaliases;
|
|
|
|
char *addr_ptrs[MAXADDRS];
|
2009-03-04 04:28:35 +01:00
|
|
|
const char *tname;
|
|
|
|
int (*name_ok)(const char *);
|
|
|
|
|
2014-12-17 02:03:44 +01:00
|
|
|
_DIAGASSERT(answer != NULL);
|
|
|
|
_DIAGASSERT(qname != NULL);
|
2009-03-04 04:28:35 +01:00
|
|
|
|
|
|
|
tname = qname;
|
2014-12-17 02:03:44 +01:00
|
|
|
hent->h_name = NULL;
|
2009-03-04 04:28:35 +01:00
|
|
|
eom = answer->buf + anslen;
|
|
|
|
switch (qtype) {
|
|
|
|
case T_A:
|
|
|
|
case T_AAAA:
|
|
|
|
name_ok = res_hnok;
|
|
|
|
break;
|
|
|
|
case T_PTR:
|
|
|
|
name_ok = res_dnok;
|
|
|
|
break;
|
|
|
|
default:
|
2014-12-17 02:03:44 +01:00
|
|
|
*he = NO_RECOVERY;
|
2009-03-04 04:28:35 +01:00
|
|
|
return NULL; /* XXX should be abort(); */
|
|
|
|
}
|
2014-12-17 02:03:44 +01:00
|
|
|
|
|
|
|
setup(aliases, maxaliases);
|
2009-03-04 04:28:35 +01:00
|
|
|
/*
|
|
|
|
* find first satisfactory answer
|
|
|
|
*/
|
|
|
|
hp = &answer->hdr;
|
|
|
|
ancount = ntohs(hp->ancount);
|
|
|
|
qdcount = ntohs(hp->qdcount);
|
2014-12-17 02:03:44 +01:00
|
|
|
bp = buf;
|
|
|
|
ep = buf + buflen;
|
2009-03-04 04:28:35 +01:00
|
|
|
cp = answer->buf;
|
|
|
|
BOUNDED_INCR(HFIXEDSZ);
|
2014-12-17 02:03:44 +01:00
|
|
|
if (qdcount != 1)
|
|
|
|
goto no_recovery;
|
|
|
|
|
|
|
|
n = dn_expand(answer->buf, eom, cp, bp, (int)(ep - bp));
|
|
|
|
if ((n < 0) || !maybe_ok(res, bp, name_ok))
|
|
|
|
goto no_recovery;
|
|
|
|
|
2009-03-04 04:28:35 +01:00
|
|
|
BOUNDED_INCR(n + QFIXEDSZ);
|
|
|
|
if (qtype == T_A || qtype == T_AAAA) {
|
|
|
|
/* res_send() has already verified that the query name is the
|
|
|
|
* same as the one we sent; this just gets the expanded name
|
|
|
|
* (i.e., with the succeeding search-domain tacked on).
|
|
|
|
*/
|
2014-12-17 02:03:44 +01:00
|
|
|
n = (int)strlen(bp) + 1; /* for the \0 */
|
|
|
|
if (n >= MAXHOSTNAMELEN)
|
|
|
|
goto no_recovery;
|
|
|
|
hent->h_name = bp;
|
2009-03-04 04:28:35 +01:00
|
|
|
bp += n;
|
|
|
|
/* The qname can be abbreviated, but h_name is now absolute. */
|
2014-12-17 02:03:44 +01:00
|
|
|
qname = hent->h_name;
|
2009-03-04 04:28:35 +01:00
|
|
|
}
|
2014-12-17 02:03:44 +01:00
|
|
|
hent->h_aliases = ap = aliases;
|
|
|
|
hent->h_addr_list = hap = addr_ptrs;
|
2009-03-04 04:28:35 +01:00
|
|
|
*ap = NULL;
|
|
|
|
*hap = NULL;
|
|
|
|
haveanswer = 0;
|
|
|
|
had_error = 0;
|
|
|
|
while (ancount-- > 0 && cp < eom && !had_error) {
|
2014-12-17 02:03:44 +01:00
|
|
|
n = dn_expand(answer->buf, eom, cp, bp, (int)(ep - bp));
|
|
|
|
if ((n < 0) || !maybe_ok(res, bp, name_ok)) {
|
2009-03-04 04:28:35 +01:00
|
|
|
had_error++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
cp += n; /* name */
|
|
|
|
BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
|
|
|
|
type = _getshort(cp);
|
|
|
|
cp += INT16SZ; /* type */
|
|
|
|
class = _getshort(cp);
|
|
|
|
cp += INT16SZ + INT32SZ; /* class, TTL */
|
|
|
|
n = _getshort(cp);
|
|
|
|
cp += INT16SZ; /* len */
|
|
|
|
BOUNDS_CHECK(cp, n);
|
|
|
|
erdata = cp + n;
|
|
|
|
if (class != C_IN) {
|
|
|
|
/* XXX - debug? syslog? */
|
|
|
|
cp += n;
|
|
|
|
continue; /* XXX - had_error++ ? */
|
|
|
|
}
|
|
|
|
if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) {
|
2014-12-17 02:03:44 +01:00
|
|
|
n = dn_expand(answer->buf, eom, cp, tbuf,
|
|
|
|
(int)sizeof tbuf);
|
|
|
|
if ((n < 0) || !maybe_ok(res, tbuf, name_ok)) {
|
2009-03-04 04:28:35 +01:00
|
|
|
had_error++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
cp += n;
|
2014-12-17 02:03:44 +01:00
|
|
|
if (cp != erdata)
|
|
|
|
goto no_recovery;
|
2009-03-04 04:28:35 +01:00
|
|
|
/* Store alias. */
|
2014-12-17 02:03:44 +01:00
|
|
|
addalias(ap, bp, aliases, maxaliases);
|
|
|
|
n = (int)strlen(bp) + 1; /* for the \0 */
|
2009-03-04 04:28:35 +01:00
|
|
|
if (n >= MAXHOSTNAMELEN) {
|
|
|
|
had_error++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
bp += n;
|
|
|
|
/* Get canonical name. */
|
2014-12-17 02:03:44 +01:00
|
|
|
n = (int)strlen(tbuf) + 1; /* for the \0 */
|
2009-03-04 04:28:35 +01:00
|
|
|
if (n > ep - bp || n >= MAXHOSTNAMELEN) {
|
|
|
|
had_error++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
strlcpy(bp, tbuf, (size_t)(ep - bp));
|
2014-12-17 02:03:44 +01:00
|
|
|
hent->h_name = bp;
|
2009-03-04 04:28:35 +01:00
|
|
|
bp += n;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (qtype == T_PTR && type == T_CNAME) {
|
2014-12-17 02:03:44 +01:00
|
|
|
n = dn_expand(answer->buf, eom, cp, tbuf,
|
|
|
|
(int)sizeof tbuf);
|
|
|
|
if (n < 0 || !maybe_dnok(res, tbuf)) {
|
2009-03-04 04:28:35 +01:00
|
|
|
had_error++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
cp += n;
|
2014-12-17 02:03:44 +01:00
|
|
|
if (cp != erdata)
|
|
|
|
goto no_recovery;
|
2009-03-04 04:28:35 +01:00
|
|
|
/* Get canonical name. */
|
2014-12-17 02:03:44 +01:00
|
|
|
n = (int)strlen(tbuf) + 1; /* for the \0 */
|
2009-03-04 04:28:35 +01:00
|
|
|
if (n > ep - bp || n >= MAXHOSTNAMELEN) {
|
|
|
|
had_error++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
strlcpy(bp, tbuf, (size_t)(ep - bp));
|
|
|
|
tname = bp;
|
|
|
|
bp += n;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (type != qtype) {
|
|
|
|
if (type != T_KEY && type != T_SIG)
|
|
|
|
syslog(LOG_NOTICE|LOG_AUTH,
|
|
|
|
"gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
|
|
|
|
qname, p_class(C_IN), p_type(qtype),
|
|
|
|
p_type(type));
|
|
|
|
cp += n;
|
|
|
|
continue; /* XXX - had_error++ ? */
|
|
|
|
}
|
|
|
|
switch (type) {
|
|
|
|
case T_PTR:
|
|
|
|
if (strcasecmp(tname, bp) != 0) {
|
|
|
|
syslog(LOG_NOTICE|LOG_AUTH,
|
|
|
|
AskedForGot, qname, bp);
|
|
|
|
cp += n;
|
|
|
|
continue; /* XXX - had_error++ ? */
|
|
|
|
}
|
2014-12-17 02:03:44 +01:00
|
|
|
n = dn_expand(answer->buf, eom, cp, bp, (int)(ep - bp));
|
|
|
|
if ((n < 0) || !maybe_hnok(res, bp)) {
|
2009-03-04 04:28:35 +01:00
|
|
|
had_error++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#if MULTI_PTRS_ARE_ALIASES
|
|
|
|
cp += n;
|
2014-12-17 02:03:44 +01:00
|
|
|
if (cp != erdata)
|
|
|
|
goto no_recovery;
|
2009-03-04 04:28:35 +01:00
|
|
|
if (!haveanswer)
|
2014-12-17 02:03:44 +01:00
|
|
|
hent->h_name = bp;
|
2009-03-04 04:28:35 +01:00
|
|
|
else
|
2014-12-17 02:03:44 +01:00
|
|
|
addalias(ap, bp, aliases, maxaliases);
|
2009-03-04 04:28:35 +01:00
|
|
|
if (n != -1) {
|
2014-12-17 02:03:44 +01:00
|
|
|
n = (int)strlen(bp) + 1; /* for the \0 */
|
2009-03-04 04:28:35 +01:00
|
|
|
if (n >= MAXHOSTNAMELEN) {
|
|
|
|
had_error++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
bp += n;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#else
|
2014-12-17 02:03:44 +01:00
|
|
|
hent->h_name = bp;
|
2009-03-04 04:28:35 +01:00
|
|
|
if (res->options & RES_USE_INET6) {
|
|
|
|
n = strlen(bp) + 1; /* for the \0 */
|
|
|
|
if (n >= MAXHOSTNAMELEN) {
|
|
|
|
had_error++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
bp += n;
|
2014-12-17 02:03:44 +01:00
|
|
|
map_v4v6_hostent(hent, &bp, ep);
|
2009-03-04 04:28:35 +01:00
|
|
|
}
|
2014-12-17 02:03:44 +01:00
|
|
|
goto success;
|
2009-03-04 04:28:35 +01:00
|
|
|
#endif
|
|
|
|
case T_A:
|
|
|
|
case T_AAAA:
|
2014-12-17 02:03:44 +01:00
|
|
|
if (strcasecmp(hent->h_name, bp) != 0) {
|
2009-03-04 04:28:35 +01:00
|
|
|
syslog(LOG_NOTICE|LOG_AUTH,
|
2014-12-17 02:03:44 +01:00
|
|
|
AskedForGot, hent->h_name, bp);
|
2009-03-04 04:28:35 +01:00
|
|
|
cp += n;
|
|
|
|
continue; /* XXX - had_error++ ? */
|
|
|
|
}
|
2014-12-17 02:03:44 +01:00
|
|
|
if (n != hent->h_length) {
|
2009-03-04 04:28:35 +01:00
|
|
|
cp += n;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (type == T_AAAA) {
|
|
|
|
struct in6_addr in6;
|
2014-12-17 02:03:44 +01:00
|
|
|
memcpy(&in6, cp, NS_IN6ADDRSZ);
|
2009-03-04 04:28:35 +01:00
|
|
|
if (IN6_IS_ADDR_V4MAPPED(&in6)) {
|
|
|
|
cp += n;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!haveanswer) {
|
|
|
|
int nn;
|
|
|
|
|
2014-12-17 02:03:44 +01:00
|
|
|
hent->h_name = bp;
|
|
|
|
nn = (int)strlen(bp) + 1; /* for the \0 */
|
2009-03-04 04:28:35 +01:00
|
|
|
bp += nn;
|
|
|
|
}
|
|
|
|
|
|
|
|
bp += sizeof(align) -
|
|
|
|
(size_t)((u_long)bp % sizeof(align));
|
|
|
|
|
2014-12-17 02:03:44 +01:00
|
|
|
if (bp + n >= ep) {
|
|
|
|
debugprintf("size (%d) too big\n", res, n);
|
2009-03-04 04:28:35 +01:00
|
|
|
had_error++;
|
|
|
|
continue;
|
|
|
|
}
|
2014-12-17 02:03:44 +01:00
|
|
|
if (hap >= &addr_ptrs[MAXADDRS - 1]) {
|
|
|
|
if (!toobig++) {
|
|
|
|
debugprintf("Too many addresses (%d)\n",
|
2009-03-04 04:28:35 +01:00
|
|
|
res, MAXADDRS);
|
2014-12-17 02:03:44 +01:00
|
|
|
}
|
2009-03-04 04:28:35 +01:00
|
|
|
cp += n;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
(void)memcpy(*hap++ = bp, cp, (size_t)n);
|
|
|
|
bp += n;
|
|
|
|
cp += n;
|
2014-12-17 02:03:44 +01:00
|
|
|
if (cp != erdata)
|
|
|
|
goto no_recovery;
|
2009-03-04 04:28:35 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
if (!had_error)
|
|
|
|
haveanswer++;
|
|
|
|
}
|
|
|
|
if (haveanswer) {
|
|
|
|
*ap = NULL;
|
|
|
|
*hap = NULL;
|
|
|
|
/*
|
|
|
|
* Note: we sort even if host can take only one address
|
|
|
|
* in its return structures - should give it the "best"
|
|
|
|
* address in that case, not some random one
|
|
|
|
*/
|
|
|
|
if (res->nsort && haveanswer > 1 && qtype == T_A)
|
2014-12-17 02:03:44 +01:00
|
|
|
addrsort(addr_ptrs, haveanswer, res);
|
|
|
|
if (!hent->h_name) {
|
|
|
|
n = (int)strlen(qname) + 1; /* for the \0 */
|
2009-03-04 04:28:35 +01:00
|
|
|
if (n > ep - bp || n >= MAXHOSTNAMELEN)
|
|
|
|
goto no_recovery;
|
|
|
|
strlcpy(bp, qname, (size_t)(ep - bp));
|
2014-12-17 02:03:44 +01:00
|
|
|
hent->h_name = bp;
|
2009-03-04 04:28:35 +01:00
|
|
|
bp += n;
|
|
|
|
}
|
|
|
|
if (res->options & RES_USE_INET6)
|
2014-12-17 02:03:44 +01:00
|
|
|
map_v4v6_hostent(hent, &bp, ep);
|
|
|
|
goto success;
|
2009-03-04 04:28:35 +01:00
|
|
|
}
|
2014-12-17 02:03:44 +01:00
|
|
|
no_recovery:
|
|
|
|
free(aliases);
|
|
|
|
*he = NO_RECOVERY;
|
|
|
|
return NULL;
|
|
|
|
success:
|
|
|
|
bp = (char *)ALIGN(bp);
|
|
|
|
n = (int)(ap - aliases);
|
|
|
|
qlen = (n + 1) * sizeof(*hent->h_aliases);
|
|
|
|
if ((size_t)(ep - bp) < qlen)
|
|
|
|
goto nospc;
|
|
|
|
hent->h_aliases = (void *)bp;
|
|
|
|
memcpy(bp, aliases, qlen);
|
|
|
|
free(aliases);
|
|
|
|
aliases = NULL;
|
|
|
|
|
|
|
|
bp += qlen;
|
|
|
|
n = (int)(hap - addr_ptrs);
|
|
|
|
qlen = (n + 1) * sizeof(*hent->h_addr_list);
|
|
|
|
if ((size_t)(ep - bp) < qlen)
|
|
|
|
goto nospc;
|
|
|
|
hent->h_addr_list = (void *)bp;
|
|
|
|
memcpy(bp, addr_ptrs, qlen);
|
|
|
|
*he = NETDB_SUCCESS;
|
|
|
|
return hent;
|
|
|
|
nospc:
|
|
|
|
free(aliases);
|
|
|
|
errno = ENOSPC;
|
|
|
|
*he = NETDB_INTERNAL;
|
2009-03-04 04:28:35 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-12-17 02:03:44 +01:00
|
|
|
/* The prototype of gethostbyname_r is from glibc, not that in netbsd. */
|
2009-03-04 04:28:35 +01:00
|
|
|
int
|
|
|
|
gethostbyname_r(const char *name, struct hostent *hp, char *buf, size_t buflen,
|
2014-12-17 02:03:44 +01:00
|
|
|
struct hostent **result, int *errorp)
|
2009-03-04 04:28:35 +01:00
|
|
|
{
|
|
|
|
res_state res = __res_get_state();
|
|
|
|
|
2014-12-17 02:03:44 +01:00
|
|
|
if (res == NULL) {
|
|
|
|
*result = NULL;
|
|
|
|
*errorp = NETDB_INTERNAL;
|
|
|
|
return -1;
|
|
|
|
}
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2014-12-17 02:03:44 +01:00
|
|
|
_DIAGASSERT(name != NULL);
|
2009-03-04 04:28:35 +01:00
|
|
|
|
|
|
|
if (res->options & RES_USE_INET6) {
|
2014-12-17 02:03:44 +01:00
|
|
|
*result = gethostbyname_internal(name, AF_INET6, res, hp, buf, buflen, errorp, NETID_UNSET,
|
|
|
|
MARK_UNSET);
|
|
|
|
if (*result) {
|
2009-03-04 04:28:35 +01:00
|
|
|
__res_put_state(res);
|
2014-12-17 02:03:44 +01:00
|
|
|
return 0;
|
2009-03-04 04:28:35 +01:00
|
|
|
}
|
|
|
|
}
|
2014-12-17 02:03:44 +01:00
|
|
|
*result = gethostbyname_internal(name, AF_INET, res, hp, buf, buflen, errorp, NETID_UNSET,
|
|
|
|
MARK_UNSET);
|
2009-03-04 04:28:35 +01:00
|
|
|
__res_put_state(res);
|
2014-12-17 02:03:44 +01:00
|
|
|
if (!*result && errno == ENOSPC) {
|
|
|
|
errno = ERANGE;
|
|
|
|
return ERANGE; /* Return error as in linux manual page. */
|
|
|
|
}
|
|
|
|
return (*result) ? 0 : -1;
|
2011-08-23 14:34:14 +02:00
|
|
|
}
|
|
|
|
|
2014-12-17 02:03:44 +01:00
|
|
|
/* The prototype of gethostbyname2_r is from glibc, not that in netbsd. */
|
|
|
|
int
|
|
|
|
gethostbyname2_r(const char *name, int af, struct hostent *hp, char *buf,
|
|
|
|
size_t buflen, struct hostent **result, int *errorp)
|
2009-03-04 04:28:35 +01:00
|
|
|
{
|
|
|
|
res_state res = __res_get_state();
|
|
|
|
|
2014-12-17 02:03:44 +01:00
|
|
|
if (res == NULL) {
|
|
|
|
*result = NULL;
|
|
|
|
*errorp = NETDB_INTERNAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
*result = gethostbyname_internal(name, af, res, hp, buf, buflen, errorp, NETID_UNSET,
|
|
|
|
MARK_UNSET);
|
2009-03-04 04:28:35 +01:00
|
|
|
__res_put_state(res);
|
2014-12-17 02:03:44 +01:00
|
|
|
if (!*result && errno == ENOSPC) {
|
|
|
|
errno = ERANGE;
|
|
|
|
return ERANGE;
|
|
|
|
}
|
|
|
|
return (*result) ? 0 : -1;
|
2009-03-04 04:28:35 +01:00
|
|
|
}
|
|
|
|
|
2014-12-10 23:56:46 +01:00
|
|
|
__LIBC_HIDDEN__ FILE* android_open_proxy() {
|
|
|
|
const char* cache_mode = getenv("ANDROID_DNS_MODE");
|
|
|
|
bool use_proxy = (cache_mode == NULL || strcmp(cache_mode, "local") != 0);
|
|
|
|
if (!use_proxy) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-08-23 14:34:14 +02:00
|
|
|
|
2014-12-10 23:56:46 +01:00
|
|
|
int s = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
|
|
|
|
if (s == -1) {
|
2011-08-23 14:34:14 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-12-10 23:56:46 +01:00
|
|
|
const int one = 1;
|
|
|
|
setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
|
|
|
|
|
|
|
|
struct sockaddr_un proxy_addr;
|
2011-08-23 14:34:14 +02:00
|
|
|
memset(&proxy_addr, 0, sizeof(proxy_addr));
|
|
|
|
proxy_addr.sun_family = AF_UNIX;
|
|
|
|
strlcpy(proxy_addr.sun_path, "/dev/socket/dnsproxyd", sizeof(proxy_addr.sun_path));
|
2014-12-10 23:56:46 +01:00
|
|
|
|
|
|
|
if (TEMP_FAILURE_RETRY(connect(s, (const struct sockaddr*) &proxy_addr, sizeof(proxy_addr))) != 0) {
|
|
|
|
close(s);
|
2011-08-23 14:34:14 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-12-10 23:56:46 +01:00
|
|
|
return fdopen(s, "r+");
|
2011-08-23 14:34:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct hostent *
|
2014-12-17 02:03:44 +01:00
|
|
|
android_read_hostent(FILE* proxy, struct hostent* hp, char* hbuf, size_t hbuflen, int *he)
|
2011-08-23 14:34:14 +02:00
|
|
|
{
|
|
|
|
uint32_t size;
|
|
|
|
char buf[4];
|
|
|
|
if (fread(buf, 1, sizeof(buf), proxy) != sizeof(buf)) return NULL;
|
|
|
|
|
2014-12-10 23:56:46 +01:00
|
|
|
// This is reading serialized data from system/netd/server/DnsProxyListener.cpp
|
|
|
|
// and changes here need to be matched there.
|
2011-08-23 14:34:14 +02:00
|
|
|
int result_code = strtol(buf, NULL, 10);
|
|
|
|
if (result_code != DnsProxyQueryResult) {
|
|
|
|
fread(&size, 1, sizeof(size), proxy);
|
2014-12-17 02:03:44 +01:00
|
|
|
*he = HOST_NOT_FOUND;
|
2011-08-23 14:34:14 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fread(&size, 1, sizeof(size), proxy) != sizeof(size)) return NULL;
|
|
|
|
size = ntohl(size);
|
|
|
|
|
2014-12-17 02:03:44 +01:00
|
|
|
memset(hp, 0, sizeof(*hp));
|
|
|
|
char *ptr = hbuf;
|
|
|
|
char *hbuf_end = hbuf + hbuflen;
|
|
|
|
|
|
|
|
if (ptr + size > hbuf_end) {
|
|
|
|
goto nospc;
|
|
|
|
}
|
2011-08-23 14:34:14 +02:00
|
|
|
if (fread(ptr, 1, size, proxy) != size) return NULL;
|
2014-12-17 02:03:44 +01:00
|
|
|
hp->h_name = ptr;
|
2011-08-23 14:34:14 +02:00
|
|
|
ptr += size;
|
|
|
|
|
2014-12-17 02:03:44 +01:00
|
|
|
char *aliases_ptrs[MAXALIASES];
|
|
|
|
char **aliases = &aliases_ptrs[0];
|
|
|
|
|
2011-08-23 14:34:14 +02:00
|
|
|
while (1) {
|
|
|
|
if (fread(&size, 1, sizeof(size), proxy) != sizeof(size)) return NULL;
|
|
|
|
size = ntohl(size);
|
|
|
|
|
|
|
|
if (size == 0) {
|
|
|
|
*aliases = NULL;
|
|
|
|
break;
|
|
|
|
}
|
2014-12-17 02:03:44 +01:00
|
|
|
if (ptr + size > hbuf_end) {
|
|
|
|
goto nospc;
|
|
|
|
}
|
2011-08-23 14:34:14 +02:00
|
|
|
if (fread(ptr, 1, size, proxy) != size) return NULL;
|
2014-12-17 02:03:44 +01:00
|
|
|
if (aliases < &aliases_ptrs[MAXALIASES - 1]) {
|
|
|
|
*aliases++ = ptr;
|
|
|
|
}
|
2011-08-23 14:34:14 +02:00
|
|
|
ptr += size;
|
|
|
|
}
|
|
|
|
|
2015-07-06 23:16:12 +02:00
|
|
|
// Fix alignment after variable-length data.
|
|
|
|
ptr = (char*)ALIGN(ptr);
|
|
|
|
|
2014-12-17 02:03:44 +01:00
|
|
|
int aliases_len = ((int)(aliases - aliases_ptrs) + 1) * sizeof(*hp->h_aliases);
|
|
|
|
if (ptr + aliases_len > hbuf_end) {
|
|
|
|
goto nospc;
|
|
|
|
}
|
|
|
|
hp->h_aliases = (void*)ptr;
|
|
|
|
memcpy(ptr, aliases_ptrs, aliases_len);
|
|
|
|
ptr += aliases_len;
|
|
|
|
|
2011-08-23 14:34:14 +02:00
|
|
|
if (fread(&size, 1, sizeof(size), proxy) != sizeof(size)) return NULL;
|
2014-12-17 02:03:44 +01:00
|
|
|
hp->h_addrtype = ntohl(size);
|
2011-08-23 14:34:14 +02:00
|
|
|
|
|
|
|
if (fread(&size, 1, sizeof(size), proxy) != sizeof(size)) return NULL;
|
2014-12-17 02:03:44 +01:00
|
|
|
hp->h_length = ntohl(size);
|
|
|
|
|
|
|
|
char *addr_ptrs[MAXADDRS];
|
|
|
|
char **addr_p = &addr_ptrs[0];
|
2011-08-23 14:34:14 +02:00
|
|
|
|
|
|
|
while (1) {
|
|
|
|
if (fread(&size, 1, sizeof(size), proxy) != sizeof(size)) return NULL;
|
|
|
|
size = ntohl(size);
|
|
|
|
if (size == 0) {
|
2014-12-17 02:03:44 +01:00
|
|
|
*addr_p = NULL;
|
2011-08-23 14:34:14 +02:00
|
|
|
break;
|
|
|
|
}
|
2014-12-17 02:03:44 +01:00
|
|
|
if (ptr + size > hbuf_end) {
|
|
|
|
goto nospc;
|
|
|
|
}
|
2011-08-23 14:34:14 +02:00
|
|
|
if (fread(ptr, 1, size, proxy) != size) return NULL;
|
2014-12-17 02:03:44 +01:00
|
|
|
if (addr_p < &addr_ptrs[MAXADDRS - 1]) {
|
|
|
|
*addr_p++ = ptr;
|
|
|
|
}
|
2011-08-23 14:34:14 +02:00
|
|
|
ptr += size;
|
|
|
|
}
|
|
|
|
|
2015-07-06 23:16:12 +02:00
|
|
|
// Fix alignment after variable-length data.
|
|
|
|
ptr = (char*)ALIGN(ptr);
|
|
|
|
|
2014-12-17 02:03:44 +01:00
|
|
|
int addrs_len = ((int)(addr_p - addr_ptrs) + 1) * sizeof(*hp->h_addr_list);
|
|
|
|
if (ptr + addrs_len > hbuf_end) {
|
|
|
|
goto nospc;
|
|
|
|
}
|
|
|
|
hp->h_addr_list = (void*)ptr;
|
|
|
|
memcpy(ptr, addr_ptrs, addrs_len);
|
|
|
|
*he = NETDB_SUCCESS;
|
|
|
|
return hp;
|
2011-08-23 14:34:14 +02:00
|
|
|
|
2014-12-17 02:03:44 +01:00
|
|
|
nospc:
|
|
|
|
*he = NETDB_INTERNAL;
|
|
|
|
errno = ENOSPC;
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-08-23 14:34:14 +02:00
|
|
|
|
2009-03-04 04:28:35 +01:00
|
|
|
static struct hostent *
|
2014-12-17 02:03:44 +01:00
|
|
|
gethostbyname_internal_real(const char *name, int af, res_state res, struct hostent *hp, char *buf,
|
|
|
|
size_t buflen, int *he)
|
2009-03-04 04:28:35 +01:00
|
|
|
{
|
|
|
|
const char *cp;
|
2014-12-17 02:03:44 +01:00
|
|
|
struct getnamaddr info;
|
|
|
|
char hbuf[MAXHOSTNAMELEN];
|
|
|
|
size_t size;
|
2009-03-04 04:28:35 +01:00
|
|
|
static const ns_dtab dtab[] = {
|
2014-12-17 02:03:44 +01:00
|
|
|
NS_FILES_CB(_hf_gethtbyname, NULL)
|
2009-03-04 04:28:35 +01:00
|
|
|
{ NSSRC_DNS, _dns_gethtbyname, NULL }, /* force -DHESIOD */
|
2014-12-17 02:03:44 +01:00
|
|
|
NS_NIS_CB(_yp_gethtbyname, NULL)
|
|
|
|
NS_NULL_CB
|
2009-03-04 04:28:35 +01:00
|
|
|
};
|
|
|
|
|
2014-12-17 02:03:44 +01:00
|
|
|
_DIAGASSERT(name != NULL);
|
2009-03-04 04:28:35 +01:00
|
|
|
|
|
|
|
switch (af) {
|
|
|
|
case AF_INET:
|
2014-12-17 02:03:44 +01:00
|
|
|
size = NS_INADDRSZ;
|
2009-03-04 04:28:35 +01:00
|
|
|
break;
|
|
|
|
case AF_INET6:
|
2014-12-17 02:03:44 +01:00
|
|
|
size = NS_IN6ADDRSZ;
|
2009-03-04 04:28:35 +01:00
|
|
|
break;
|
|
|
|
default:
|
2014-12-17 02:03:44 +01:00
|
|
|
*he = NETDB_INTERNAL;
|
2009-03-04 04:28:35 +01:00
|
|
|
errno = EAFNOSUPPORT;
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-12-17 02:03:44 +01:00
|
|
|
if (buflen < size)
|
|
|
|
goto nospc;
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2014-12-17 02:03:44 +01:00
|
|
|
hp->h_addrtype = af;
|
|
|
|
hp->h_length = (int)size;
|
2009-03-04 04:28:35 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* if there aren't any dots, it could be a user-level alias.
|
|
|
|
* this is also done in res_nquery() since we are not the only
|
|
|
|
* function that looks up host names.
|
|
|
|
*/
|
2014-12-17 02:03:44 +01:00
|
|
|
if (!strchr(name, '.') && (cp = res_hostalias(res, name,
|
|
|
|
hbuf, sizeof(hbuf))))
|
2009-03-04 04:28:35 +01:00
|
|
|
name = cp;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* disallow names consisting only of digits/dots, unless
|
|
|
|
* they end in a dot.
|
|
|
|
*/
|
|
|
|
if (isdigit((u_char) name[0]))
|
|
|
|
for (cp = name;; ++cp) {
|
|
|
|
if (!*cp) {
|
|
|
|
if (*--cp == '.')
|
|
|
|
break;
|
|
|
|
/*
|
|
|
|
* All-numeric, no dot at the end.
|
|
|
|
* Fake up a hostent as if we'd actually
|
|
|
|
* done a lookup.
|
|
|
|
*/
|
2014-12-17 02:03:44 +01:00
|
|
|
goto fake;
|
2009-03-04 04:28:35 +01:00
|
|
|
}
|
|
|
|
if (!isdigit((u_char) *cp) && *cp != '.')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ((isxdigit((u_char) name[0]) && strchr(name, ':') != NULL) ||
|
|
|
|
name[0] == ':')
|
|
|
|
for (cp = name;; ++cp) {
|
|
|
|
if (!*cp) {
|
|
|
|
if (*--cp == '.')
|
|
|
|
break;
|
|
|
|
/*
|
|
|
|
* All-IPv6-legal, no dot at the end.
|
|
|
|
* Fake up a hostent as if we'd actually
|
|
|
|
* done a lookup.
|
|
|
|
*/
|
2014-12-17 02:03:44 +01:00
|
|
|
goto fake;
|
2009-03-04 04:28:35 +01:00
|
|
|
}
|
|
|
|
if (!isxdigit((u_char) *cp) && *cp != ':' && *cp != '.')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-12-17 02:03:44 +01:00
|
|
|
*he = NETDB_INTERNAL;
|
|
|
|
info.hp = hp;
|
|
|
|
info.buf = buf;
|
|
|
|
info.buflen = buflen;
|
|
|
|
info.he = he;
|
|
|
|
if (nsdispatch(&info, dtab, NSDB_HOSTS, "gethostbyname",
|
|
|
|
default_dns_files, name, strlen(name), af) != NS_SUCCESS)
|
|
|
|
return NULL;
|
|
|
|
*he = NETDB_SUCCESS;
|
|
|
|
return hp;
|
|
|
|
nospc:
|
|
|
|
*he = NETDB_INTERNAL;
|
|
|
|
errno = ENOSPC;
|
|
|
|
return NULL;
|
|
|
|
fake:
|
|
|
|
HENT_ARRAY(hp->h_addr_list, 1, buf, buflen);
|
|
|
|
HENT_ARRAY(hp->h_aliases, 0, buf, buflen);
|
|
|
|
|
|
|
|
hp->h_aliases[0] = NULL;
|
|
|
|
if (size > buflen)
|
|
|
|
goto nospc;
|
|
|
|
|
|
|
|
if (inet_pton(af, name, buf) <= 0) {
|
|
|
|
*he = HOST_NOT_FOUND;
|
2009-03-04 04:28:35 +01:00
|
|
|
return NULL;
|
2011-08-23 14:34:14 +02:00
|
|
|
}
|
2014-12-17 02:03:44 +01:00
|
|
|
hp->h_addr_list[0] = buf;
|
|
|
|
hp->h_addr_list[1] = NULL;
|
|
|
|
buf += size;
|
|
|
|
buflen -= size;
|
|
|
|
HENT_SCOPY(hp->h_name, name, buf, buflen);
|
|
|
|
if (res->options & RES_USE_INET6)
|
|
|
|
map_v4v6_hostent(hp, &buf, buf + buflen);
|
|
|
|
*he = NETDB_SUCCESS;
|
2009-03-04 04:28:35 +01:00
|
|
|
return hp;
|
|
|
|
}
|
|
|
|
|
2011-08-23 14:34:14 +02:00
|
|
|
// very similar in proxy-ness to android_getaddrinfo_proxy
|
|
|
|
static struct hostent *
|
2014-12-17 02:03:44 +01:00
|
|
|
gethostbyname_internal(const char *name, int af, res_state res, struct hostent *hp, char *hbuf,
|
|
|
|
size_t hbuflen, int *errorp, unsigned netid, unsigned mark)
|
2011-08-23 14:34:14 +02:00
|
|
|
{
|
2014-12-10 23:56:46 +01:00
|
|
|
FILE* proxy = android_open_proxy();
|
|
|
|
if (proxy == NULL) {
|
|
|
|
// Either we're not supposed to be using the proxy or the proxy is unavailable.
|
2014-02-14 23:07:23 +01:00
|
|
|
res_setnetid(res, netid);
|
2013-06-20 19:36:56 +02:00
|
|
|
res_setmark(res, mark);
|
2014-12-17 02:03:44 +01:00
|
|
|
return gethostbyname_internal_real(name, af, res, hp, hbuf, hbuflen, errorp);
|
2011-08-23 14:34:14 +02:00
|
|
|
}
|
|
|
|
|
2014-05-15 20:43:07 +02:00
|
|
|
netid = __netdClientDispatch.netIdForResolv(netid);
|
|
|
|
|
2014-12-10 23:56:46 +01:00
|
|
|
// This is writing to system/netd/server/DnsProxyListener.cpp and changes
|
|
|
|
// here need to be matched there.
|
2014-02-14 23:07:23 +01:00
|
|
|
if (fprintf(proxy, "gethostbyname %u %s %d",
|
|
|
|
netid,
|
2011-08-23 14:34:14 +02:00
|
|
|
name == NULL ? "^" : name,
|
|
|
|
af) < 0) {
|
|
|
|
fclose(proxy);
|
2014-12-10 23:56:46 +01:00
|
|
|
return NULL;
|
2011-08-23 14:34:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (fputc(0, proxy) == EOF || fflush(proxy) != 0) {
|
|
|
|
fclose(proxy);
|
2014-12-10 23:56:46 +01:00
|
|
|
return NULL;
|
2011-08-23 14:34:14 +02:00
|
|
|
}
|
2014-12-10 23:56:46 +01:00
|
|
|
|
2014-12-17 02:03:44 +01:00
|
|
|
struct hostent* result = android_read_hostent(proxy, hp, hbuf, hbuflen, errorp);
|
2014-12-10 23:56:46 +01:00
|
|
|
fclose(proxy);
|
2011-08-23 14:34:14 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-12-17 02:03:44 +01:00
|
|
|
/* The prototype of gethostbyaddr_r is from glibc, not that in netbsd. */
|
|
|
|
int gethostbyaddr_r(const void *addr, socklen_t len, int af, struct hostent *hp, char *buf,
|
|
|
|
size_t buflen, struct hostent **result, int *h_errnop)
|
|
|
|
{
|
|
|
|
*result = android_gethostbyaddrfornet_proxy_internal(addr, len, af, hp, buf, buflen, h_errnop,
|
|
|
|
NETID_UNSET, MARK_UNSET);
|
|
|
|
if (!*result && errno == ENOSPC) {
|
|
|
|
errno = ERANGE;
|
|
|
|
return ERANGE;
|
|
|
|
}
|
|
|
|
return (*result) ? 0 : -1;
|
|
|
|
}
|
2014-12-10 23:56:46 +01:00
|
|
|
|
|
|
|
static struct hostent *
|
2014-12-17 02:03:44 +01:00
|
|
|
android_gethostbyaddrfornet_real(const void *addr, socklen_t len, int af, struct hostent *hp,
|
|
|
|
char *buf, size_t buflen, int *he, unsigned netid, unsigned mark)
|
|
|
|
{
|
2009-03-04 04:28:35 +01:00
|
|
|
const u_char *uaddr = (const u_char *)addr;
|
|
|
|
socklen_t size;
|
2014-12-17 02:03:44 +01:00
|
|
|
struct getnamaddr info;
|
2009-03-04 04:28:35 +01:00
|
|
|
static const ns_dtab dtab[] = {
|
2014-12-17 02:03:44 +01:00
|
|
|
NS_FILES_CB(_hf_gethtbyaddr, NULL)
|
2009-03-04 04:28:35 +01:00
|
|
|
{ NSSRC_DNS, _dns_gethtbyaddr, NULL }, /* force -DHESIOD */
|
2014-12-17 02:03:44 +01:00
|
|
|
NS_NIS_CB(_yp_gethtbyaddr, NULL)
|
|
|
|
NS_NULL_CB
|
2009-03-04 04:28:35 +01:00
|
|
|
};
|
|
|
|
|
2014-12-17 02:03:44 +01:00
|
|
|
_DIAGASSERT(addr != NULL);
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2014-05-06 20:23:40 +02:00
|
|
|
if (af == AF_INET6 && len == NS_IN6ADDRSZ &&
|
|
|
|
(IN6_IS_ADDR_LINKLOCAL((const struct in6_addr *)addr) ||
|
|
|
|
IN6_IS_ADDR_SITELOCAL((const struct in6_addr *)addr))) {
|
2014-12-17 02:03:44 +01:00
|
|
|
*he = HOST_NOT_FOUND;
|
2009-03-04 04:28:35 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2014-05-06 20:23:40 +02:00
|
|
|
if (af == AF_INET6 && len == NS_IN6ADDRSZ &&
|
|
|
|
(IN6_IS_ADDR_V4MAPPED((const struct in6_addr *)addr) ||
|
|
|
|
IN6_IS_ADDR_V4COMPAT((const struct in6_addr *)addr))) {
|
2009-03-04 04:28:35 +01:00
|
|
|
/* Unmap. */
|
2014-05-06 20:23:40 +02:00
|
|
|
uaddr += NS_IN6ADDRSZ - NS_INADDRSZ;
|
|
|
|
addr = uaddr;
|
2009-03-04 04:28:35 +01:00
|
|
|
af = AF_INET;
|
2014-05-06 20:23:40 +02:00
|
|
|
len = NS_INADDRSZ;
|
2009-03-04 04:28:35 +01:00
|
|
|
}
|
|
|
|
switch (af) {
|
|
|
|
case AF_INET:
|
2014-05-06 20:23:40 +02:00
|
|
|
size = NS_INADDRSZ;
|
2009-03-04 04:28:35 +01:00
|
|
|
break;
|
|
|
|
case AF_INET6:
|
2014-05-06 20:23:40 +02:00
|
|
|
size = NS_IN6ADDRSZ;
|
2009-03-04 04:28:35 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
errno = EAFNOSUPPORT;
|
2014-12-17 02:03:44 +01:00
|
|
|
*he = NETDB_INTERNAL;
|
2009-03-04 04:28:35 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (size != len) {
|
|
|
|
errno = EINVAL;
|
2014-12-17 02:03:44 +01:00
|
|
|
*he = NETDB_INTERNAL;
|
2009-03-04 04:28:35 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2014-12-17 02:03:44 +01:00
|
|
|
info.hp = hp;
|
|
|
|
info.buf = buf;
|
|
|
|
info.buflen = buflen;
|
|
|
|
info.he = he;
|
|
|
|
*he = NETDB_INTERNAL;
|
|
|
|
if (nsdispatch(&info, dtab, NSDB_HOSTS, "gethostbyaddr",
|
|
|
|
default_dns_files, uaddr, len, af, netid, mark) != NS_SUCCESS)
|
2009-03-04 04:28:35 +01:00
|
|
|
return NULL;
|
2014-12-17 02:03:44 +01:00
|
|
|
*he = NETDB_SUCCESS;
|
2009-03-04 04:28:35 +01:00
|
|
|
return hp;
|
|
|
|
}
|
|
|
|
|
2014-12-17 02:03:44 +01:00
|
|
|
static struct hostent*
|
|
|
|
android_gethostbyaddrfornet_proxy_internal(const void* addr, socklen_t len, int af,
|
|
|
|
struct hostent *hp, char *hbuf, size_t hbuflen, int *he,
|
|
|
|
unsigned netid, unsigned mark)
|
|
|
|
{
|
2014-12-10 23:56:46 +01:00
|
|
|
FILE* proxy = android_open_proxy();
|
|
|
|
if (proxy == NULL) {
|
|
|
|
// Either we're not supposed to be using the proxy or the proxy is unavailable.
|
2014-12-17 02:03:44 +01:00
|
|
|
return android_gethostbyaddrfornet_real(addr,len, af, hp, hbuf, hbuflen, he, netid, mark);
|
2014-12-10 23:56:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
char buf[INET6_ADDRSTRLEN]; //big enough for IPv4 and IPv6
|
|
|
|
const char * addrStr = inet_ntop(af, addr, buf, sizeof(buf));
|
|
|
|
if (addrStr == NULL) {
|
|
|
|
fclose(proxy);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
netid = __netdClientDispatch.netIdForResolv(netid);
|
|
|
|
|
|
|
|
if (fprintf(proxy, "gethostbyaddr %s %d %d %u",
|
|
|
|
addrStr, len, af, netid) < 0) {
|
|
|
|
fclose(proxy);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fputc(0, proxy) == EOF || fflush(proxy) != 0) {
|
|
|
|
fclose(proxy);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-12-17 02:03:44 +01:00
|
|
|
struct hostent *result = android_read_hostent(proxy, hp, hbuf, hbuflen, he);
|
2014-12-10 23:56:46 +01:00
|
|
|
fclose(proxy);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-12-17 02:03:44 +01:00
|
|
|
struct hostent*
|
|
|
|
netbsd_gethostent_r(FILE *hf, struct hostent *hent, char *buf, size_t buflen, int *he)
|
2011-08-23 14:34:14 +02:00
|
|
|
{
|
2014-12-17 02:03:44 +01:00
|
|
|
char *p, *name;
|
2009-03-04 04:28:35 +01:00
|
|
|
char *cp, **q;
|
|
|
|
int af, len;
|
2014-12-17 02:03:44 +01:00
|
|
|
size_t anum;
|
|
|
|
char **aliases;
|
|
|
|
size_t maxaliases;
|
|
|
|
struct in6_addr host_addr;
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2014-12-17 02:03:44 +01:00
|
|
|
if (hf == NULL) {
|
|
|
|
*he = NETDB_INTERNAL;
|
|
|
|
errno = EINVAL;
|
2009-03-04 04:28:35 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2014-12-17 02:03:44 +01:00
|
|
|
p = NULL;
|
|
|
|
setup(aliases, maxaliases);
|
|
|
|
|
|
|
|
/* Allocate a new space to read file lines like upstream does.
|
|
|
|
* To keep reentrancy we cannot use __res_get_static()->hostbuf here,
|
|
|
|
* as the buffer may be used to store content for a previous hostent
|
|
|
|
* returned by non-reentrant functions like gethostbyname().
|
|
|
|
*/
|
|
|
|
const size_t line_buf_size = sizeof(__res_get_static()->hostbuf);
|
|
|
|
if ((p = malloc(line_buf_size)) == NULL) {
|
|
|
|
goto nospc;
|
2009-03-04 04:28:35 +01:00
|
|
|
}
|
2014-12-17 02:03:44 +01:00
|
|
|
for (;;) {
|
|
|
|
if (!fgets(p, line_buf_size, hf)) {
|
|
|
|
free(p);
|
|
|
|
free(aliases);
|
|
|
|
*he = HOST_NOT_FOUND;
|
2009-03-04 04:28:35 +01:00
|
|
|
return NULL;
|
2014-12-17 02:03:44 +01:00
|
|
|
}
|
|
|
|
if (*p == '#') {
|
2009-03-04 04:28:35 +01:00
|
|
|
continue;
|
|
|
|
}
|
2014-12-17 02:03:44 +01:00
|
|
|
if (!(cp = strpbrk(p, "#\n"))) {
|
2009-03-04 04:28:35 +01:00
|
|
|
continue;
|
|
|
|
}
|
2014-12-17 02:03:44 +01:00
|
|
|
*cp = '\0';
|
|
|
|
if (!(cp = strpbrk(p, " \t")))
|
|
|
|
continue;
|
|
|
|
*cp++ = '\0';
|
|
|
|
if (inet_pton(AF_INET6, p, &host_addr) > 0) {
|
|
|
|
af = AF_INET6;
|
|
|
|
len = NS_IN6ADDRSZ;
|
|
|
|
} else {
|
|
|
|
if (inet_pton(AF_INET, p, &host_addr) <= 0)
|
|
|
|
continue;
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2014-12-17 02:03:44 +01:00
|
|
|
res_state res = __res_get_state();
|
|
|
|
if (res == NULL)
|
|
|
|
goto nospc;
|
|
|
|
if (res->options & RES_USE_INET6) {
|
|
|
|
map_v4v6_address(buf, buf);
|
|
|
|
af = AF_INET6;
|
|
|
|
len = NS_IN6ADDRSZ;
|
|
|
|
} else {
|
|
|
|
af = AF_INET;
|
|
|
|
len = NS_INADDRSZ;
|
2009-03-04 04:28:35 +01:00
|
|
|
}
|
2014-12-17 02:03:44 +01:00
|
|
|
__res_put_state(res);
|
2009-03-04 04:28:35 +01:00
|
|
|
}
|
|
|
|
|
2014-12-17 02:03:44 +01:00
|
|
|
/* if this is not something we're looking for, skip it. */
|
|
|
|
if (hent->h_addrtype != 0 && hent->h_addrtype != af)
|
|
|
|
continue;
|
|
|
|
if (hent->h_length != 0 && hent->h_length != len)
|
|
|
|
continue;
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2014-12-17 02:03:44 +01:00
|
|
|
while (*cp == ' ' || *cp == '\t')
|
|
|
|
cp++;
|
|
|
|
if ((cp = strpbrk(name = cp, " \t")) != NULL)
|
|
|
|
*cp++ = '\0';
|
|
|
|
q = aliases;
|
|
|
|
while (cp && *cp) {
|
|
|
|
if (*cp == ' ' || *cp == '\t') {
|
|
|
|
cp++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
addalias(q, cp, aliases, maxaliases);
|
|
|
|
if ((cp = strpbrk(cp, " \t")) != NULL)
|
|
|
|
*cp++ = '\0';
|
|
|
|
}
|
|
|
|
break;
|
2009-03-04 04:28:35 +01:00
|
|
|
}
|
2014-12-17 02:03:44 +01:00
|
|
|
hent->h_length = len;
|
|
|
|
hent->h_addrtype = af;
|
|
|
|
HENT_ARRAY(hent->h_addr_list, 1, buf, buflen);
|
|
|
|
anum = (size_t)(q - aliases);
|
|
|
|
HENT_ARRAY(hent->h_aliases, anum, buf, buflen);
|
|
|
|
HENT_COPY(hent->h_addr_list[0], &host_addr, hent->h_length, buf,
|
|
|
|
buflen);
|
|
|
|
hent->h_addr_list[1] = NULL;
|
|
|
|
|
|
|
|
HENT_SCOPY(hent->h_name, name, buf, buflen);
|
|
|
|
for (size_t i = 0; i < anum; i++)
|
|
|
|
HENT_SCOPY(hent->h_aliases[i], aliases[i], buf, buflen);
|
|
|
|
hent->h_aliases[anum] = NULL;
|
|
|
|
|
|
|
|
*he = NETDB_SUCCESS;
|
|
|
|
free(p);
|
|
|
|
free(aliases);
|
|
|
|
return hent;
|
|
|
|
nospc:
|
|
|
|
free(p);
|
|
|
|
free(aliases);
|
|
|
|
errno = ENOSPC;
|
|
|
|
*he = NETDB_INTERNAL;
|
|
|
|
return NULL;
|
2009-03-04 04:28:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
map_v4v6_address(const char *src, char *dst)
|
|
|
|
{
|
|
|
|
u_char *p = (u_char *)dst;
|
2014-12-17 02:03:44 +01:00
|
|
|
char tmp[NS_INADDRSZ];
|
2009-03-04 04:28:35 +01:00
|
|
|
int i;
|
|
|
|
|
2014-12-17 02:03:44 +01:00
|
|
|
_DIAGASSERT(src != NULL);
|
|
|
|
_DIAGASSERT(dst != NULL);
|
2009-03-04 04:28:35 +01:00
|
|
|
|
|
|
|
/* Stash a temporary copy so our caller can update in place. */
|
2014-12-17 02:03:44 +01:00
|
|
|
(void)memcpy(tmp, src, NS_INADDRSZ);
|
2009-03-04 04:28:35 +01:00
|
|
|
/* Mark this ipv6 addr as a mapped ipv4. */
|
|
|
|
for (i = 0; i < 10; i++)
|
|
|
|
*p++ = 0x00;
|
|
|
|
*p++ = 0xff;
|
|
|
|
*p++ = 0xff;
|
|
|
|
/* Retrieve the saved copy and we're done. */
|
2014-12-17 02:03:44 +01:00
|
|
|
(void)memcpy(p, tmp, NS_INADDRSZ);
|
2009-03-04 04:28:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
map_v4v6_hostent(struct hostent *hp, char **bpp, char *ep)
|
|
|
|
{
|
|
|
|
char **ap;
|
|
|
|
|
2014-12-17 02:03:44 +01:00
|
|
|
_DIAGASSERT(hp != NULL);
|
|
|
|
_DIAGASSERT(bpp != NULL);
|
|
|
|
_DIAGASSERT(ep != NULL);
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2014-12-17 02:03:44 +01:00
|
|
|
if (hp->h_addrtype != AF_INET || hp->h_length != NS_INADDRSZ)
|
2009-03-04 04:28:35 +01:00
|
|
|
return;
|
|
|
|
hp->h_addrtype = AF_INET6;
|
2014-12-17 02:03:44 +01:00
|
|
|
hp->h_length = NS_IN6ADDRSZ;
|
2009-03-04 04:28:35 +01:00
|
|
|
for (ap = hp->h_addr_list; *ap; ap++) {
|
2014-12-17 02:03:44 +01:00
|
|
|
int i = (int)(sizeof(align) -
|
|
|
|
(size_t)((u_long)*bpp % sizeof(align)));
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2014-12-17 02:03:44 +01:00
|
|
|
if (ep - *bpp < (i + NS_IN6ADDRSZ)) {
|
2009-03-04 04:28:35 +01:00
|
|
|
/* Out of memory. Truncate address list here. XXX */
|
|
|
|
*ap = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
*bpp += i;
|
|
|
|
map_v4v6_address(*ap, *bpp);
|
|
|
|
*ap = *bpp;
|
2014-12-17 02:03:44 +01:00
|
|
|
*bpp += NS_IN6ADDRSZ;
|
2009-03-04 04:28:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
addrsort(char **ap, int num, res_state res)
|
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
char **p;
|
|
|
|
short aval[MAXADDRS];
|
|
|
|
int needsort = 0;
|
|
|
|
|
2014-12-17 02:03:44 +01:00
|
|
|
_DIAGASSERT(ap != NULL);
|
2009-03-04 04:28:35 +01:00
|
|
|
|
|
|
|
p = ap;
|
|
|
|
for (i = 0; i < num; i++, p++) {
|
|
|
|
for (j = 0 ; (unsigned)j < res->nsort; j++)
|
|
|
|
if (res->sort_list[j].addr.s_addr ==
|
|
|
|
(((struct in_addr *)(void *)(*p))->s_addr &
|
|
|
|
res->sort_list[j].mask))
|
|
|
|
break;
|
|
|
|
aval[i] = j;
|
|
|
|
if (needsort == 0 && i > 0 && j < aval[i-1])
|
|
|
|
needsort = i;
|
|
|
|
}
|
|
|
|
if (!needsort)
|
|
|
|
return;
|
|
|
|
|
|
|
|
while (needsort < num) {
|
|
|
|
for (j = needsort - 1; j >= 0; j--) {
|
|
|
|
if (aval[j] > aval[j+1]) {
|
|
|
|
char *hp;
|
|
|
|
|
|
|
|
i = aval[j];
|
|
|
|
aval[j] = aval[j+1];
|
|
|
|
aval[j+1] = i;
|
|
|
|
|
|
|
|
hp = ap[j];
|
|
|
|
ap[j] = ap[j+1];
|
|
|
|
ap[j+1] = hp;
|
|
|
|
} else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
needsort++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*ARGSUSED*/
|
2010-09-27 17:37:10 +02:00
|
|
|
static int
|
2009-03-04 04:28:35 +01:00
|
|
|
_dns_gethtbyname(void *rv, void *cb_data, va_list ap)
|
|
|
|
{
|
|
|
|
querybuf *buf;
|
|
|
|
int n, type;
|
|
|
|
struct hostent *hp;
|
|
|
|
const char *name;
|
|
|
|
res_state res;
|
2014-12-17 02:03:44 +01:00
|
|
|
struct getnamaddr *info = rv;
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2014-12-17 02:03:44 +01:00
|
|
|
_DIAGASSERT(rv != NULL);
|
2009-03-04 04:28:35 +01:00
|
|
|
|
|
|
|
name = va_arg(ap, char *);
|
2014-12-17 02:03:44 +01:00
|
|
|
/* NOSTRICT skip string len */(void)va_arg(ap, int);
|
|
|
|
info->hp->h_addrtype = va_arg(ap, int);
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2014-12-17 02:03:44 +01:00
|
|
|
switch (info->hp->h_addrtype) {
|
2009-03-04 04:28:35 +01:00
|
|
|
case AF_INET:
|
2014-12-17 02:03:44 +01:00
|
|
|
info->hp->h_length = NS_INADDRSZ;
|
2009-03-04 04:28:35 +01:00
|
|
|
type = T_A;
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
2014-12-17 02:03:44 +01:00
|
|
|
info->hp->h_length = NS_IN6ADDRSZ;
|
2009-03-04 04:28:35 +01:00
|
|
|
type = T_AAAA;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return NS_UNAVAIL;
|
|
|
|
}
|
|
|
|
buf = malloc(sizeof(*buf));
|
|
|
|
if (buf == NULL) {
|
2014-12-17 02:03:44 +01:00
|
|
|
*info->he = NETDB_INTERNAL;
|
2009-03-04 04:28:35 +01:00
|
|
|
return NS_NOTFOUND;
|
|
|
|
}
|
|
|
|
res = __res_get_state();
|
|
|
|
if (res == NULL) {
|
|
|
|
free(buf);
|
|
|
|
return NS_NOTFOUND;
|
|
|
|
}
|
2014-12-17 02:03:44 +01:00
|
|
|
n = res_nsearch(res, name, C_IN, type, buf->buf, (int)sizeof(buf->buf));
|
2009-03-04 04:28:35 +01:00
|
|
|
if (n < 0) {
|
|
|
|
free(buf);
|
2014-12-17 02:03:44 +01:00
|
|
|
debugprintf("res_nsearch failed (%d)\n", res, n);
|
2009-03-04 04:28:35 +01:00
|
|
|
__res_put_state(res);
|
|
|
|
return NS_NOTFOUND;
|
|
|
|
}
|
2014-12-17 02:03:44 +01:00
|
|
|
hp = getanswer(buf, n, name, type, res, info->hp, info->buf,
|
|
|
|
info->buflen, info->he);
|
2009-03-04 04:28:35 +01:00
|
|
|
free(buf);
|
|
|
|
__res_put_state(res);
|
|
|
|
if (hp == NULL)
|
2014-12-19 19:10:04 +01:00
|
|
|
switch (*info->he) {
|
2009-03-04 04:28:35 +01:00
|
|
|
case HOST_NOT_FOUND:
|
|
|
|
return NS_NOTFOUND;
|
|
|
|
case TRY_AGAIN:
|
|
|
|
return NS_TRYAGAIN;
|
|
|
|
default:
|
|
|
|
return NS_UNAVAIL;
|
|
|
|
}
|
|
|
|
return NS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*ARGSUSED*/
|
2010-09-27 17:37:10 +02:00
|
|
|
static int
|
2009-03-04 04:28:35 +01:00
|
|
|
_dns_gethtbyaddr(void *rv, void *cb_data, va_list ap)
|
|
|
|
{
|
|
|
|
char qbuf[MAXDNAME + 1], *qp, *ep;
|
|
|
|
int n;
|
|
|
|
querybuf *buf;
|
|
|
|
struct hostent *hp;
|
|
|
|
const unsigned char *uaddr;
|
2014-12-17 02:03:44 +01:00
|
|
|
int advance;
|
2009-03-04 04:28:35 +01:00
|
|
|
res_state res;
|
2014-12-17 02:03:44 +01:00
|
|
|
char *bf;
|
|
|
|
size_t blen;
|
|
|
|
struct getnamaddr *info = rv;
|
2014-02-14 23:07:23 +01:00
|
|
|
unsigned netid, mark;
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2014-12-17 02:03:44 +01:00
|
|
|
_DIAGASSERT(rv != NULL);
|
2009-03-04 04:28:35 +01:00
|
|
|
|
|
|
|
uaddr = va_arg(ap, unsigned char *);
|
2014-12-17 02:03:44 +01:00
|
|
|
info->hp->h_length = va_arg(ap, int);
|
|
|
|
info->hp->h_addrtype = va_arg(ap, int);
|
2014-02-14 23:07:23 +01:00
|
|
|
netid = va_arg(ap, unsigned);
|
|
|
|
mark = va_arg(ap, unsigned);
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2014-12-17 02:03:44 +01:00
|
|
|
switch (info->hp->h_addrtype) {
|
2009-03-04 04:28:35 +01:00
|
|
|
case AF_INET:
|
|
|
|
(void)snprintf(qbuf, sizeof(qbuf), "%u.%u.%u.%u.in-addr.arpa",
|
|
|
|
(uaddr[3] & 0xff), (uaddr[2] & 0xff),
|
|
|
|
(uaddr[1] & 0xff), (uaddr[0] & 0xff));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AF_INET6:
|
|
|
|
qp = qbuf;
|
|
|
|
ep = qbuf + sizeof(qbuf) - 1;
|
2014-12-17 02:03:44 +01:00
|
|
|
for (n = NS_IN6ADDRSZ - 1; n >= 0; n--) {
|
2009-03-04 04:28:35 +01:00
|
|
|
advance = snprintf(qp, (size_t)(ep - qp), "%x.%x.",
|
|
|
|
uaddr[n] & 0xf,
|
|
|
|
((unsigned int)uaddr[n] >> 4) & 0xf);
|
|
|
|
if (advance > 0 && qp + advance < ep)
|
|
|
|
qp += advance;
|
|
|
|
else {
|
2014-12-17 02:03:44 +01:00
|
|
|
*info->he = NETDB_INTERNAL;
|
2009-03-04 04:28:35 +01:00
|
|
|
return NS_NOTFOUND;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (strlcat(qbuf, "ip6.arpa", sizeof(qbuf)) >= sizeof(qbuf)) {
|
2014-12-17 02:03:44 +01:00
|
|
|
*info->he = NETDB_INTERNAL;
|
2009-03-04 04:28:35 +01:00
|
|
|
return NS_NOTFOUND;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2014-12-17 02:03:44 +01:00
|
|
|
return NS_UNAVAIL;
|
2009-03-04 04:28:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
buf = malloc(sizeof(*buf));
|
|
|
|
if (buf == NULL) {
|
2014-12-17 02:03:44 +01:00
|
|
|
*info->he = NETDB_INTERNAL;
|
2009-03-04 04:28:35 +01:00
|
|
|
return NS_NOTFOUND;
|
|
|
|
}
|
|
|
|
res = __res_get_state();
|
|
|
|
if (res == NULL) {
|
|
|
|
free(buf);
|
|
|
|
return NS_NOTFOUND;
|
|
|
|
}
|
2014-02-14 23:07:23 +01:00
|
|
|
res_setnetid(res, netid);
|
2013-06-20 19:36:56 +02:00
|
|
|
res_setmark(res, mark);
|
2014-12-17 02:03:44 +01:00
|
|
|
n = res_nquery(res, qbuf, C_IN, T_PTR, buf->buf, (int)sizeof(buf->buf));
|
2009-03-04 04:28:35 +01:00
|
|
|
if (n < 0) {
|
|
|
|
free(buf);
|
2014-12-17 02:03:44 +01:00
|
|
|
debugprintf("res_nquery failed (%d)\n", res, n);
|
2009-03-04 04:28:35 +01:00
|
|
|
__res_put_state(res);
|
|
|
|
return NS_NOTFOUND;
|
|
|
|
}
|
2014-12-17 02:03:44 +01:00
|
|
|
hp = getanswer(buf, n, qbuf, T_PTR, res, info->hp, info->buf,
|
|
|
|
info->buflen, info->he);
|
2009-03-04 04:28:35 +01:00
|
|
|
free(buf);
|
|
|
|
if (hp == NULL) {
|
|
|
|
__res_put_state(res);
|
2014-12-17 02:03:44 +01:00
|
|
|
switch (*info->he) {
|
2009-03-04 04:28:35 +01:00
|
|
|
case HOST_NOT_FOUND:
|
|
|
|
return NS_NOTFOUND;
|
|
|
|
case TRY_AGAIN:
|
|
|
|
return NS_TRYAGAIN;
|
|
|
|
default:
|
|
|
|
return NS_UNAVAIL;
|
|
|
|
}
|
|
|
|
}
|
2014-12-17 02:03:44 +01:00
|
|
|
|
|
|
|
bf = (void *)(hp->h_addr_list + 2);
|
|
|
|
blen = (size_t)(bf - info->buf);
|
|
|
|
if (blen + info->hp->h_length > info->buflen)
|
|
|
|
goto nospc;
|
|
|
|
hp->h_addr_list[0] = bf;
|
|
|
|
hp->h_addr_list[1] = NULL;
|
|
|
|
(void)memcpy(bf, uaddr, (size_t)info->hp->h_length);
|
|
|
|
if (info->hp->h_addrtype == AF_INET && (res->options & RES_USE_INET6)) {
|
|
|
|
if (blen + NS_IN6ADDRSZ > info->buflen)
|
|
|
|
goto nospc;
|
|
|
|
map_v4v6_address(bf, bf);
|
2009-03-04 04:28:35 +01:00
|
|
|
hp->h_addrtype = AF_INET6;
|
2014-12-17 02:03:44 +01:00
|
|
|
hp->h_length = NS_IN6ADDRSZ;
|
2009-03-04 04:28:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
__res_put_state(res);
|
2014-12-17 02:03:44 +01:00
|
|
|
*info->he = NETDB_SUCCESS;
|
|
|
|
return NS_SUCCESS;
|
|
|
|
nospc:
|
2014-12-19 19:10:04 +01:00
|
|
|
errno = ENOSPC;
|
2014-12-17 02:03:44 +01:00
|
|
|
*info->he = NETDB_INTERNAL;
|
|
|
|
return NS_UNAVAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef YP
|
|
|
|
/*ARGSUSED*/
|
|
|
|
static struct hostent *
|
|
|
|
_yp_hostent(char *line, int af, struct getnamaddr *info)
|
|
|
|
{
|
|
|
|
struct in6_addr host_addrs[MAXADDRS];
|
|
|
|
char **aliases;
|
|
|
|
size_t maxaliases;
|
|
|
|
char *p = line;
|
|
|
|
char *cp, **q, *ptr;
|
|
|
|
size_t len, anum, i;
|
|
|
|
int addrok;
|
|
|
|
int more;
|
|
|
|
size_t naddrs;
|
|
|
|
struct hostent *hp = info->hp;
|
|
|
|
|
|
|
|
_DIAGASSERT(line != NULL);
|
|
|
|
|
|
|
|
hp->h_name = NULL;
|
|
|
|
hp->h_addrtype = af;
|
|
|
|
switch (af) {
|
|
|
|
case AF_INET:
|
|
|
|
hp->h_length = NS_INADDRSZ;
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
hp->h_length = NS_IN6ADDRSZ;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
setup(aliases, maxaliases);
|
|
|
|
naddrs = 0;
|
|
|
|
q = aliases;
|
|
|
|
|
|
|
|
nextline:
|
|
|
|
/* check for host_addrs overflow */
|
|
|
|
if (naddrs >= __arraycount(host_addrs))
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
more = 0;
|
|
|
|
cp = strpbrk(p, " \t");
|
|
|
|
if (cp == NULL)
|
|
|
|
goto done;
|
|
|
|
*cp++ = '\0';
|
|
|
|
|
|
|
|
/* p has should have an address */
|
|
|
|
addrok = inet_pton(af, p, &host_addrs[naddrs]);
|
|
|
|
if (addrok != 1) {
|
|
|
|
/* skip to the next line */
|
|
|
|
while (cp && *cp) {
|
|
|
|
if (*cp == '\n') {
|
|
|
|
cp++;
|
|
|
|
goto nextline;
|
|
|
|
}
|
|
|
|
cp++;
|
|
|
|
}
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
naddrs++;
|
|
|
|
|
|
|
|
while (*cp == ' ' || *cp == '\t')
|
|
|
|
cp++;
|
|
|
|
p = cp;
|
|
|
|
cp = strpbrk(p, " \t\n");
|
|
|
|
if (cp != NULL) {
|
|
|
|
if (*cp == '\n')
|
|
|
|
more = 1;
|
|
|
|
*cp++ = '\0';
|
|
|
|
}
|
|
|
|
if (!hp->h_name)
|
|
|
|
hp->h_name = p;
|
|
|
|
else if (strcmp(hp->h_name, p) == 0)
|
|
|
|
;
|
|
|
|
else
|
|
|
|
addalias(q, p, aliases, maxaliases);
|
|
|
|
p = cp;
|
|
|
|
if (more)
|
|
|
|
goto nextline;
|
|
|
|
|
|
|
|
while (cp && *cp) {
|
|
|
|
if (*cp == ' ' || *cp == '\t') {
|
|
|
|
cp++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (*cp == '\n') {
|
|
|
|
cp++;
|
|
|
|
goto nextline;
|
|
|
|
}
|
|
|
|
addalias(q, cp, aliases, maxaliases);
|
|
|
|
cp = strpbrk(cp, " \t");
|
|
|
|
if (cp != NULL)
|
|
|
|
*cp++ = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
if (hp->h_name == NULL) {
|
|
|
|
free(aliases);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ptr = info->buf;
|
|
|
|
len = info->buflen;
|
|
|
|
|
|
|
|
anum = (size_t)(q - aliases);
|
|
|
|
HENT_ARRAY(hp->h_addr_list, naddrs, ptr, len);
|
|
|
|
HENT_ARRAY(hp->h_aliases, anum, ptr, len);
|
|
|
|
|
|
|
|
for (i = 0; i < naddrs; i++)
|
|
|
|
HENT_COPY(hp->h_addr_list[i], &host_addrs[i], hp->h_length,
|
|
|
|
ptr, len);
|
|
|
|
hp->h_addr_list[naddrs] = NULL;
|
|
|
|
|
|
|
|
HENT_SCOPY(hp->h_name, hp->h_name, ptr, len);
|
|
|
|
|
|
|
|
for (i = 0; i < anum; i++)
|
|
|
|
HENT_SCOPY(hp->h_aliases[i], aliases[i], ptr, len);
|
|
|
|
hp->h_aliases[anum] = NULL;
|
|
|
|
free(aliases);
|
|
|
|
|
|
|
|
return hp;
|
|
|
|
nospc:
|
|
|
|
free(aliases);
|
|
|
|
*info->he = NETDB_INTERNAL;
|
|
|
|
errno = ENOSPC;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*ARGSUSED*/
|
|
|
|
int
|
|
|
|
_yp_gethtbyaddr(void *rv, void *cb_data, va_list ap)
|
|
|
|
{
|
|
|
|
struct hostent *hp = NULL;
|
|
|
|
char *ypcurrent;
|
|
|
|
int ypcurrentlen, r;
|
|
|
|
char name[INET6_ADDRSTRLEN]; /* XXX enough? */
|
|
|
|
const unsigned char *uaddr;
|
|
|
|
int af;
|
|
|
|
const char *map;
|
|
|
|
struct getnamaddr *info = rv;
|
|
|
|
|
|
|
|
_DIAGASSERT(rv != NULL);
|
|
|
|
|
|
|
|
uaddr = va_arg(ap, unsigned char *);
|
|
|
|
/* NOSTRICT skip len */(void)va_arg(ap, int);
|
|
|
|
af = va_arg(ap, int);
|
|
|
|
|
|
|
|
if (!__ypdomain) {
|
|
|
|
if (_yp_check(&__ypdomain) == 0)
|
|
|
|
return NS_UNAVAIL;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* XXX unfortunately, we cannot support IPv6 extended scoped address
|
|
|
|
* notation here. gethostbyaddr() is not scope-aware. too bad.
|
|
|
|
*/
|
|
|
|
if (inet_ntop(af, uaddr, name, (socklen_t)sizeof(name)) == NULL)
|
|
|
|
return NS_UNAVAIL;
|
|
|
|
switch (af) {
|
|
|
|
case AF_INET:
|
|
|
|
map = "hosts.byaddr";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
map = "ipnodes.byaddr";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ypcurrent = NULL;
|
|
|
|
r = yp_match(__ypdomain, map, name,
|
|
|
|
(int)strlen(name), &ypcurrent, &ypcurrentlen);
|
|
|
|
if (r == 0)
|
|
|
|
hp = _yp_hostent(ypcurrent, af, info);
|
|
|
|
else
|
|
|
|
hp = NULL;
|
|
|
|
free(ypcurrent);
|
|
|
|
if (hp == NULL) {
|
|
|
|
*info->he = HOST_NOT_FOUND;
|
|
|
|
return NS_NOTFOUND;
|
|
|
|
}
|
|
|
|
return NS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*ARGSUSED*/
|
|
|
|
int
|
|
|
|
_yp_gethtbyname(void *rv, void *cb_data, va_list ap)
|
|
|
|
{
|
|
|
|
struct hostent *hp;
|
|
|
|
char *ypcurrent;
|
|
|
|
int ypcurrentlen, r;
|
|
|
|
const char *name;
|
|
|
|
int af;
|
|
|
|
const char *map;
|
|
|
|
struct getnamaddr *info = rv;
|
|
|
|
|
|
|
|
_DIAGASSERT(rv != NULL);
|
|
|
|
|
|
|
|
name = va_arg(ap, char *);
|
|
|
|
/* NOSTRICT skip string len */(void)va_arg(ap, int);
|
|
|
|
af = va_arg(ap, int);
|
|
|
|
|
|
|
|
if (!__ypdomain) {
|
|
|
|
if (_yp_check(&__ypdomain) == 0)
|
|
|
|
return NS_UNAVAIL;
|
|
|
|
}
|
|
|
|
switch (af) {
|
|
|
|
case AF_INET:
|
|
|
|
map = "hosts.byname";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
map = "ipnodes.byname";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ypcurrent = NULL;
|
|
|
|
r = yp_match(__ypdomain, map, name,
|
|
|
|
(int)strlen(name), &ypcurrent, &ypcurrentlen);
|
|
|
|
if (r == 0)
|
|
|
|
hp = _yp_hostent(ypcurrent, af, info);
|
|
|
|
else
|
|
|
|
hp = NULL;
|
|
|
|
free(ypcurrent);
|
|
|
|
if (hp == NULL) {
|
|
|
|
*info->he = HOST_NOT_FOUND;
|
|
|
|
return NS_NOTFOUND;
|
|
|
|
}
|
2009-03-04 04:28:35 +01:00
|
|
|
return NS_SUCCESS;
|
|
|
|
}
|
2014-12-17 02:03:44 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Non-reentrant versions.
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct hostent *
|
|
|
|
gethostbyname(const char *name)
|
|
|
|
{
|
|
|
|
struct hostent *result = NULL;
|
|
|
|
res_static rs = __res_get_static(); /* Use res_static to provide thread-safety. */
|
|
|
|
|
|
|
|
gethostbyname_r(name, &rs->host, rs->hostbuf, sizeof(rs->hostbuf), &result, &h_errno);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct hostent *
|
|
|
|
gethostbyname2(const char *name, int af)
|
|
|
|
{
|
|
|
|
struct hostent *result = NULL;
|
|
|
|
res_static rs = __res_get_static(); /* Use res_static to provide thread-safety. */
|
|
|
|
|
|
|
|
gethostbyname2_r(name, af, &rs->host, rs->hostbuf, sizeof(rs->hostbuf), &result, &h_errno);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct hostent *
|
|
|
|
android_gethostbynamefornet(const char *name, int af, unsigned netid, unsigned mark)
|
|
|
|
{
|
|
|
|
struct hostent *hp;
|
|
|
|
res_state res = __res_get_state();
|
|
|
|
if (res == NULL)
|
|
|
|
return NULL;
|
|
|
|
res_static rs = __res_get_static(); /* Use res_static to provide thread-safety. */
|
|
|
|
hp = gethostbyname_internal(name, af, res, &rs->host, rs->hostbuf, sizeof(rs->hostbuf),
|
|
|
|
&h_errno, netid, mark);
|
|
|
|
__res_put_state(res);
|
|
|
|
return hp;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct hostent *
|
|
|
|
gethostbyaddr(const void *addr, socklen_t len, int af)
|
|
|
|
{
|
|
|
|
return android_gethostbyaddrfornet_proxy(addr, len, af, NETID_UNSET, MARK_UNSET);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct hostent *
|
|
|
|
android_gethostbyaddrfornet(const void *addr, socklen_t len, int af, unsigned netid, unsigned mark)
|
|
|
|
{
|
|
|
|
return android_gethostbyaddrfornet_proxy(addr, len, af, netid, mark);
|
|
|
|
}
|
|
|
|
|
|
|
|
__LIBC_HIDDEN__ struct hostent*
|
|
|
|
android_gethostbyaddrfornet_proxy(const void* addr, socklen_t len, int af,
|
|
|
|
unsigned netid, unsigned mark)
|
|
|
|
{
|
|
|
|
res_static rs = __res_get_static(); /* Use res_static to provide thread-safety. */
|
|
|
|
return android_gethostbyaddrfornet_proxy_internal(addr, len, af, &rs->host, rs->hostbuf,
|
|
|
|
sizeof(rs->hostbuf), &h_errno, netid, mark);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct hostent *
|
|
|
|
gethostent(void)
|
|
|
|
{
|
|
|
|
res_static rs = __res_get_static();
|
|
|
|
if (!rs->hostf) {
|
|
|
|
sethostent_r(&rs->hostf);
|
|
|
|
if (!rs->hostf) {
|
|
|
|
h_errno = NETDB_INTERNAL;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
memset(&rs->host, 0, sizeof(rs->host));
|
|
|
|
return netbsd_gethostent_r(rs->hostf, &rs->host, rs->hostbuf, sizeof(rs->hostbuf), &h_errno);
|
|
|
|
}
|