Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1 | /* $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 Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 54 | #include <arpa/inet.h> |
| 55 | #include <arpa/nameser.h> |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 56 | #include <assert.h> |
| 57 | #include <ctype.h> |
| 58 | #include <errno.h> |
| 59 | #include <netdb.h> |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 60 | #include <netinet/in.h> |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 61 | #include <stdarg.h> |
| 62 | #include <stdbool.h> |
| 63 | #include <stdio.h> |
Bernie Innocenti | c165ce8 | 2018-10-16 23:35:28 +0900 | [diff] [blame] | 64 | #include <stdlib.h> |
| 65 | #include <string.h> |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 66 | #include <sys/param.h> |
| 67 | #include <sys/socket.h> |
Bernie Innocenti | f89b351 | 2018-08-30 07:34:37 +0900 | [diff] [blame] | 68 | #include <sys/types.h> |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 69 | #include <sys/un.h> |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 70 | #include <syslog.h> |
| 71 | #include <unistd.h> |
nuccachen | 0a51173 | 2018-09-18 13:38:48 +0800 | [diff] [blame] | 72 | #include <functional> |
Bernie Innocenti | f89b351 | 2018-08-30 07:34:37 +0900 | [diff] [blame] | 73 | |
Bernie Innocenti | c165ce8 | 2018-10-16 23:35:28 +0900 | [diff] [blame] | 74 | #include "hostent.h" |
Bernie Innocenti | 189eb50 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 75 | #include "netd_resolv/resolv.h" |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 76 | #include "resolv_cache.h" |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 77 | #include "resolv_private.h" |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 78 | |
Bernie Innocenti | f89b351 | 2018-08-30 07:34:37 +0900 | [diff] [blame] | 79 | // NetBSD uses _DIAGASSERT to null-check arguments and the like, |
| 80 | // but it's clear from the number of mistakes in their assertions |
| 81 | // that they don't actually test or ship with this. |
| 82 | #define _DIAGASSERT(e) /* nothing */ |
| 83 | |
nuccachen | 8161c99 | 2018-09-11 11:20:00 +0800 | [diff] [blame] | 84 | // TODO: unify macro ALIGNBYTES and ALIGN for all possible data type alignment of hostent |
| 85 | // buffer. |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 86 | #define ALIGNBYTES (sizeof(uintptr_t) - 1) |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 87 | #define ALIGN(p) (((uintptr_t)(p) + ALIGNBYTES) & ~ALIGNBYTES) |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 88 | |
| 89 | #ifndef LOG_AUTH |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 90 | #define LOG_AUTH 0 |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 91 | #endif |
| 92 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 93 | #define MULTI_PTRS_ARE_ALIASES 1 /* XXX - experimental */ |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 94 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 95 | #define maybe_ok(res, nm, ok) (((res)->options & RES_NOCHECKNAME) != 0U || (ok)(nm) != 0) |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 96 | #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 Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 99 | #define addalias(d, s, arr, siz) \ |
| 100 | do { \ |
| 101 | if (d >= &arr[siz]) { \ |
| 102 | char** xptr = (char**) realloc(arr, (siz + 10) * sizeof(*arr)); \ |
| 103 | if (xptr == NULL) goto nospc; \ |
| 104 | d = xptr + (d - arr); \ |
| 105 | arr = xptr; \ |
| 106 | siz += 10; \ |
| 107 | } \ |
| 108 | *d++ = s; \ |
| 109 | } while (0) |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 110 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 111 | static const char AskedForGot[] = "gethostby*.getanswer: asked for \"%s\", got \"%s\""; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 112 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 113 | #define MAXPACKET (8 * 1024) |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 114 | |
| 115 | typedef union { |
| 116 | HEADER hdr; |
| 117 | u_char buf[MAXPACKET]; |
| 118 | } querybuf; |
| 119 | |
| 120 | typedef union { |
| 121 | int32_t al; |
| 122 | char ac; |
| 123 | } align; |
| 124 | |
| 125 | #ifdef DEBUG |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 126 | static void debugprintf(const char*, res_state, ...) __attribute__((__format__(__printf__, 1, 3))); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 127 | #endif |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 128 | static struct hostent* getanswer(const querybuf*, int, const char*, int, res_state, struct hostent*, |
| 129 | char*, size_t, int*); |
nuccachen | 0a51173 | 2018-09-18 13:38:48 +0800 | [diff] [blame] | 130 | static void convert_v4v6_hostent(struct hostent* hp, char** bpp, char* ep, |
| 131 | std::function<void(struct hostent* hp)> mapping_param, |
| 132 | std::function<void(char* src, char* dst)> mapping_addr); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 133 | static void map_v4v6_address(const char*, char*); |
| 134 | static void map_v4v6_hostent(struct hostent*, char**, char*); |
nuccachen | 0a51173 | 2018-09-18 13:38:48 +0800 | [diff] [blame] | 135 | static void pad_v4v6_hostent(struct hostent* hp, char** bpp, char* ep); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 136 | static void addrsort(char**, int, res_state); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 137 | |
Bernie Innocenti | a2cbfb1 | 2018-09-12 20:03:11 +0900 | [diff] [blame] | 138 | static bool _dns_gethtbyaddr(const unsigned char* uaddr, int len, int af, |
| 139 | const android_net_context* netcontext, getnamaddr* info); |
| 140 | static bool _dns_gethtbyname(const char* name, int af, getnamaddr* info); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 141 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 142 | static struct hostent* gethostbyname_internal(const char*, int, res_state, struct hostent*, char*, |
| 143 | size_t, int*, const struct android_net_context*); |
| 144 | static struct hostent* android_gethostbyaddrfornetcontext_proxy_internal( |
| 145 | const void*, socklen_t, int, struct hostent*, char*, size_t, int*, |
| 146 | const struct android_net_context*); |
Bernie Innocenti | 0e45e2a | 2018-09-14 16:42:36 +0900 | [diff] [blame] | 147 | static struct hostent* android_gethostbyaddrfornetcontext_proxy( |
| 148 | const void* addr, socklen_t len, int af, const struct android_net_context* netcontext); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 149 | |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 150 | #ifdef DEBUG |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 151 | static void debugprintf(const char* msg, res_state res, ...) { |
| 152 | _DIAGASSERT(msg != NULL); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 153 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 154 | if (res->options & RES_DEBUG) { |
| 155 | int save = errno; |
| 156 | va_list ap; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 157 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 158 | va_start(ap, res); |
| 159 | vprintf(msg, ap); |
| 160 | va_end(ap); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 161 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 162 | errno = save; |
| 163 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 164 | } |
| 165 | #else |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 166 | #define debugprintf(msg, res, num) /*nada*/ |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 167 | #endif |
| 168 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 169 | #define BOUNDED_INCR(x) \ |
| 170 | do { \ |
| 171 | BOUNDS_CHECK(cp, x); \ |
| 172 | cp += (x); \ |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 173 | } while (0) |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 174 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 175 | #define BOUNDS_CHECK(ptr, count) \ |
| 176 | do { \ |
| 177 | if (eom - (ptr) < (count)) goto no_recovery; \ |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 178 | } while (0) |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 179 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 180 | static struct hostent* getanswer(const querybuf* answer, int anslen, const char* qname, int qtype, |
| 181 | res_state res, struct hostent* hent, char* buf, size_t buflen, |
| 182 | int* he) { |
| 183 | const HEADER* hp; |
| 184 | const u_char* cp; |
| 185 | int n; |
| 186 | size_t qlen; |
| 187 | const u_char *eom, *erdata; |
| 188 | char *bp, **ap, **hap, *ep; |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 189 | int ancount, qdcount; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 190 | int haveanswer, had_error; |
| 191 | int toobig = 0; |
| 192 | char tbuf[MAXDNAME]; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 193 | char* addr_ptrs[MAXADDRS]; |
| 194 | const char* tname; |
| 195 | int (*name_ok)(const char*); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 196 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 197 | _DIAGASSERT(answer != NULL); |
| 198 | _DIAGASSERT(qname != NULL); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 199 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 200 | tname = qname; |
| 201 | hent->h_name = NULL; |
| 202 | eom = answer->buf + anslen; |
| 203 | switch (qtype) { |
| 204 | case T_A: |
| 205 | case T_AAAA: |
| 206 | name_ok = res_hnok; |
| 207 | break; |
| 208 | case T_PTR: |
| 209 | name_ok = res_dnok; |
| 210 | break; |
| 211 | default: |
| 212 | *he = NO_RECOVERY; |
| 213 | return NULL; /* XXX should be abort(); */ |
| 214 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 215 | |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 216 | size_t maxaliases = 10; |
| 217 | char** aliases = (char**) malloc(maxaliases * sizeof(char*)); |
| 218 | if (!aliases) goto nospc; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 219 | /* |
| 220 | * find first satisfactory answer |
| 221 | */ |
| 222 | hp = &answer->hdr; |
| 223 | ancount = ntohs(hp->ancount); |
| 224 | qdcount = ntohs(hp->qdcount); |
| 225 | bp = buf; |
| 226 | ep = buf + buflen; |
| 227 | cp = answer->buf; |
| 228 | BOUNDED_INCR(HFIXEDSZ); |
| 229 | if (qdcount != 1) goto no_recovery; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 230 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 231 | n = dn_expand(answer->buf, eom, cp, bp, (int) (ep - bp)); |
| 232 | if ((n < 0) || !maybe_ok(res, bp, name_ok)) goto no_recovery; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 233 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 234 | BOUNDED_INCR(n + QFIXEDSZ); |
| 235 | if (qtype == T_A || qtype == T_AAAA) { |
| 236 | /* res_send() has already verified that the query name is the |
| 237 | * same as the one we sent; this just gets the expanded name |
| 238 | * (i.e., with the succeeding search-domain tacked on). |
| 239 | */ |
| 240 | n = (int) strlen(bp) + 1; /* for the \0 */ |
| 241 | if (n >= MAXHOSTNAMELEN) goto no_recovery; |
| 242 | hent->h_name = bp; |
| 243 | bp += n; |
| 244 | /* The qname can be abbreviated, but h_name is now absolute. */ |
| 245 | qname = hent->h_name; |
| 246 | } |
| 247 | hent->h_aliases = ap = aliases; |
| 248 | hent->h_addr_list = hap = addr_ptrs; |
| 249 | *ap = NULL; |
| 250 | *hap = NULL; |
| 251 | haveanswer = 0; |
| 252 | had_error = 0; |
| 253 | while (ancount-- > 0 && cp < eom && !had_error) { |
| 254 | n = dn_expand(answer->buf, eom, cp, bp, (int) (ep - bp)); |
| 255 | if ((n < 0) || !maybe_ok(res, bp, name_ok)) { |
| 256 | had_error++; |
| 257 | continue; |
| 258 | } |
| 259 | cp += n; /* name */ |
| 260 | BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ); |
Bernie Innocenti | ee1b85b | 2018-09-25 14:23:19 +0900 | [diff] [blame] | 261 | int type = ns_get16(cp); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 262 | cp += INT16SZ; /* type */ |
Bernie Innocenti | ee1b85b | 2018-09-25 14:23:19 +0900 | [diff] [blame] | 263 | int cl = ns_get16(cp); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 264 | cp += INT16SZ + INT32SZ; /* class, TTL */ |
Bernie Innocenti | ee1b85b | 2018-09-25 14:23:19 +0900 | [diff] [blame] | 265 | n = ns_get16(cp); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 266 | cp += INT16SZ; /* len */ |
| 267 | BOUNDS_CHECK(cp, n); |
| 268 | erdata = cp + n; |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 269 | if (cl != C_IN) { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 270 | /* XXX - debug? syslog? */ |
| 271 | cp += n; |
| 272 | continue; /* XXX - had_error++ ? */ |
| 273 | } |
| 274 | if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) { |
| 275 | n = dn_expand(answer->buf, eom, cp, tbuf, (int) sizeof tbuf); |
| 276 | if ((n < 0) || !maybe_ok(res, tbuf, name_ok)) { |
| 277 | had_error++; |
| 278 | continue; |
| 279 | } |
| 280 | cp += n; |
| 281 | if (cp != erdata) goto no_recovery; |
| 282 | /* Store alias. */ |
| 283 | addalias(ap, bp, aliases, maxaliases); |
| 284 | n = (int) strlen(bp) + 1; /* for the \0 */ |
| 285 | if (n >= MAXHOSTNAMELEN) { |
| 286 | had_error++; |
| 287 | continue; |
| 288 | } |
| 289 | bp += n; |
| 290 | /* Get canonical name. */ |
| 291 | n = (int) strlen(tbuf) + 1; /* for the \0 */ |
| 292 | if (n > ep - bp || n >= MAXHOSTNAMELEN) { |
| 293 | had_error++; |
| 294 | continue; |
| 295 | } |
| 296 | strlcpy(bp, tbuf, (size_t)(ep - bp)); |
| 297 | hent->h_name = bp; |
| 298 | bp += n; |
| 299 | continue; |
| 300 | } |
| 301 | if (qtype == T_PTR && type == T_CNAME) { |
| 302 | n = dn_expand(answer->buf, eom, cp, tbuf, (int) sizeof tbuf); |
| 303 | if (n < 0 || !maybe_dnok(res, tbuf)) { |
| 304 | had_error++; |
| 305 | continue; |
| 306 | } |
| 307 | cp += n; |
| 308 | if (cp != erdata) goto no_recovery; |
| 309 | /* Get canonical name. */ |
| 310 | n = (int) strlen(tbuf) + 1; /* for the \0 */ |
| 311 | if (n > ep - bp || n >= MAXHOSTNAMELEN) { |
| 312 | had_error++; |
| 313 | continue; |
| 314 | } |
| 315 | strlcpy(bp, tbuf, (size_t)(ep - bp)); |
| 316 | tname = bp; |
| 317 | bp += n; |
| 318 | continue; |
| 319 | } |
| 320 | if (type != qtype) { |
| 321 | if (type != T_KEY && type != T_SIG) |
| 322 | syslog(LOG_NOTICE | LOG_AUTH, |
| 323 | "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"", qname, |
| 324 | p_class(C_IN), p_type(qtype), p_type(type)); |
| 325 | cp += n; |
| 326 | continue; /* XXX - had_error++ ? */ |
| 327 | } |
| 328 | switch (type) { |
| 329 | case T_PTR: |
| 330 | if (strcasecmp(tname, bp) != 0) { |
| 331 | syslog(LOG_NOTICE | LOG_AUTH, AskedForGot, qname, bp); |
| 332 | cp += n; |
| 333 | continue; /* XXX - had_error++ ? */ |
| 334 | } |
| 335 | n = dn_expand(answer->buf, eom, cp, bp, (int) (ep - bp)); |
| 336 | if ((n < 0) || !maybe_hnok(res, bp)) { |
| 337 | had_error++; |
| 338 | break; |
| 339 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 340 | #if MULTI_PTRS_ARE_ALIASES |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 341 | cp += n; |
| 342 | if (cp != erdata) goto no_recovery; |
| 343 | if (!haveanswer) |
| 344 | hent->h_name = bp; |
| 345 | else |
| 346 | addalias(ap, bp, aliases, maxaliases); |
| 347 | if (n != -1) { |
| 348 | n = (int) strlen(bp) + 1; /* for the \0 */ |
| 349 | if (n >= MAXHOSTNAMELEN) { |
| 350 | had_error++; |
| 351 | break; |
| 352 | } |
| 353 | bp += n; |
| 354 | } |
| 355 | break; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 356 | #else |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 357 | hent->h_name = bp; |
| 358 | if (res->options & RES_USE_INET6) { |
| 359 | n = strlen(bp) + 1; /* for the \0 */ |
| 360 | if (n >= MAXHOSTNAMELEN) { |
| 361 | had_error++; |
| 362 | break; |
| 363 | } |
| 364 | bp += n; |
| 365 | map_v4v6_hostent(hent, &bp, ep); |
| 366 | } |
| 367 | goto success; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 368 | #endif |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 369 | case T_A: |
| 370 | case T_AAAA: |
| 371 | if (strcasecmp(hent->h_name, bp) != 0) { |
| 372 | syslog(LOG_NOTICE | LOG_AUTH, AskedForGot, hent->h_name, bp); |
| 373 | cp += n; |
| 374 | continue; /* XXX - had_error++ ? */ |
| 375 | } |
| 376 | if (n != hent->h_length) { |
| 377 | cp += n; |
| 378 | continue; |
| 379 | } |
| 380 | if (type == T_AAAA) { |
| 381 | struct in6_addr in6; |
| 382 | memcpy(&in6, cp, NS_IN6ADDRSZ); |
| 383 | if (IN6_IS_ADDR_V4MAPPED(&in6)) { |
| 384 | cp += n; |
| 385 | continue; |
| 386 | } |
| 387 | } |
| 388 | if (!haveanswer) { |
| 389 | int nn; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 390 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 391 | hent->h_name = bp; |
| 392 | nn = (int) strlen(bp) + 1; /* for the \0 */ |
| 393 | bp += nn; |
| 394 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 395 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 396 | bp += sizeof(align) - (size_t)((u_long) bp % sizeof(align)); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 397 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 398 | if (bp + n >= ep) { |
| 399 | debugprintf("size (%d) too big\n", res, n); |
| 400 | had_error++; |
| 401 | continue; |
| 402 | } |
| 403 | if (hap >= &addr_ptrs[MAXADDRS - 1]) { |
| 404 | if (!toobig++) { |
| 405 | debugprintf("Too many addresses (%d)\n", res, MAXADDRS); |
| 406 | } |
| 407 | cp += n; |
| 408 | continue; |
| 409 | } |
| 410 | (void) memcpy(*hap++ = bp, cp, (size_t) n); |
| 411 | bp += n; |
| 412 | cp += n; |
| 413 | if (cp != erdata) goto no_recovery; |
| 414 | break; |
| 415 | default: |
| 416 | abort(); |
| 417 | } |
| 418 | if (!had_error) haveanswer++; |
| 419 | } |
| 420 | if (haveanswer) { |
| 421 | *ap = NULL; |
| 422 | *hap = NULL; |
| 423 | /* |
| 424 | * Note: we sort even if host can take only one address |
| 425 | * in its return structures - should give it the "best" |
| 426 | * address in that case, not some random one |
| 427 | */ |
| 428 | if (res->nsort && haveanswer > 1 && qtype == T_A) addrsort(addr_ptrs, haveanswer, res); |
| 429 | if (!hent->h_name) { |
| 430 | n = (int) strlen(qname) + 1; /* for the \0 */ |
| 431 | if (n > ep - bp || n >= MAXHOSTNAMELEN) goto no_recovery; |
| 432 | strlcpy(bp, qname, (size_t)(ep - bp)); |
| 433 | hent->h_name = bp; |
| 434 | bp += n; |
| 435 | } |
| 436 | if (res->options & RES_USE_INET6) map_v4v6_hostent(hent, &bp, ep); |
nuccachen | 0a51173 | 2018-09-18 13:38:48 +0800 | [diff] [blame] | 437 | if (hent->h_addrtype == AF_INET) pad_v4v6_hostent(hent, &bp, ep); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 438 | goto success; |
| 439 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 440 | no_recovery: |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 441 | free(aliases); |
| 442 | *he = NO_RECOVERY; |
| 443 | return NULL; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 444 | success: |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 445 | bp = (char*) ALIGN(bp); |
| 446 | n = (int) (ap - aliases); |
| 447 | qlen = (n + 1) * sizeof(*hent->h_aliases); |
| 448 | if ((size_t)(ep - bp) < qlen) goto nospc; |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 449 | hent->h_aliases = (char**) bp; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 450 | memcpy(bp, aliases, qlen); |
| 451 | free(aliases); |
| 452 | aliases = NULL; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 453 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 454 | bp += qlen; |
| 455 | n = (int) (hap - addr_ptrs); |
| 456 | qlen = (n + 1) * sizeof(*hent->h_addr_list); |
| 457 | if ((size_t)(ep - bp) < qlen) goto nospc; |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 458 | hent->h_addr_list = (char**) bp; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 459 | memcpy(bp, addr_ptrs, qlen); |
| 460 | *he = NETDB_SUCCESS; |
| 461 | return hent; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 462 | nospc: |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 463 | free(aliases); |
| 464 | errno = ENOSPC; |
| 465 | *he = NETDB_INTERNAL; |
| 466 | return NULL; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 467 | } |
| 468 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 469 | static struct hostent* gethostbyname_internal_real(const char* name, int af, res_state res, |
| 470 | struct hostent* hp, char* buf, size_t buflen, |
| 471 | int* he) { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 472 | struct getnamaddr info; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 473 | size_t size; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 474 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 475 | _DIAGASSERT(name != NULL); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 476 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 477 | switch (af) { |
| 478 | case AF_INET: |
| 479 | size = NS_INADDRSZ; |
| 480 | break; |
| 481 | case AF_INET6: |
| 482 | size = NS_IN6ADDRSZ; |
| 483 | break; |
| 484 | default: |
| 485 | *he = NETDB_INTERNAL; |
| 486 | errno = EAFNOSUPPORT; |
| 487 | return NULL; |
| 488 | } |
| 489 | if (buflen < size) goto nospc; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 490 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 491 | hp->h_addrtype = af; |
| 492 | hp->h_length = (int) size; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 493 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 494 | /* |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 495 | * disallow names consisting only of digits/dots, unless |
| 496 | * they end in a dot. |
| 497 | */ |
Bernie Innocenti | 4acba1a | 2018-09-26 11:52:04 +0900 | [diff] [blame] | 498 | if (isdigit((u_char) name[0])) { |
| 499 | for (const char* cp = name;; ++cp) { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 500 | if (!*cp) { |
| 501 | if (*--cp == '.') break; |
| 502 | /* |
| 503 | * All-numeric, no dot at the end. |
| 504 | * Fake up a hostent as if we'd actually |
| 505 | * done a lookup. |
| 506 | */ |
| 507 | goto fake; |
| 508 | } |
| 509 | if (!isdigit((u_char) *cp) && *cp != '.') break; |
| 510 | } |
Bernie Innocenti | 4acba1a | 2018-09-26 11:52:04 +0900 | [diff] [blame] | 511 | } |
| 512 | if ((isxdigit((u_char) name[0]) && strchr(name, ':') != NULL) || name[0] == ':') { |
| 513 | for (const char* cp = name;; ++cp) { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 514 | if (!*cp) { |
| 515 | if (*--cp == '.') break; |
| 516 | /* |
| 517 | * All-IPv6-legal, no dot at the end. |
| 518 | * Fake up a hostent as if we'd actually |
| 519 | * done a lookup. |
| 520 | */ |
| 521 | goto fake; |
| 522 | } |
| 523 | if (!isxdigit((u_char) *cp) && *cp != ':' && *cp != '.') break; |
| 524 | } |
Bernie Innocenti | 4acba1a | 2018-09-26 11:52:04 +0900 | [diff] [blame] | 525 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 526 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 527 | *he = NETDB_INTERNAL; |
| 528 | info.hp = hp; |
| 529 | info.buf = buf; |
| 530 | info.buflen = buflen; |
| 531 | info.he = he; |
Bernie Innocenti | 9bf0e1d | 2018-09-12 17:59:17 +0900 | [diff] [blame] | 532 | if (!_hf_gethtbyname2(name, af, &info)) { |
Bernie Innocenti | a2cbfb1 | 2018-09-12 20:03:11 +0900 | [diff] [blame] | 533 | if (!_dns_gethtbyname(name, af, &info)) { |
Bernie Innocenti | 9bf0e1d | 2018-09-12 17:59:17 +0900 | [diff] [blame] | 534 | return NULL; |
| 535 | } |
| 536 | } |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 537 | *he = NETDB_SUCCESS; |
| 538 | return hp; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 539 | nospc: |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 540 | *he = NETDB_INTERNAL; |
| 541 | errno = ENOSPC; |
| 542 | return NULL; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 543 | fake: |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 544 | HENT_ARRAY(hp->h_addr_list, 1, buf, buflen); |
| 545 | HENT_ARRAY(hp->h_aliases, 0, buf, buflen); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 546 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 547 | hp->h_aliases[0] = NULL; |
| 548 | if (size > buflen) goto nospc; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 549 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 550 | if (inet_pton(af, name, buf) <= 0) { |
| 551 | *he = HOST_NOT_FOUND; |
| 552 | return NULL; |
| 553 | } |
| 554 | hp->h_addr_list[0] = buf; |
| 555 | hp->h_addr_list[1] = NULL; |
| 556 | buf += size; |
| 557 | buflen -= size; |
| 558 | HENT_SCOPY(hp->h_name, name, buf, buflen); |
| 559 | if (res->options & RES_USE_INET6) map_v4v6_hostent(hp, &buf, buf + buflen); |
| 560 | *he = NETDB_SUCCESS; |
| 561 | return hp; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | // very similar in proxy-ness to android_getaddrinfo_proxy |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 565 | static struct hostent* gethostbyname_internal(const char* name, int af, res_state res, |
| 566 | struct hostent* hp, char* hbuf, size_t hbuflen, |
| 567 | int* errorp, |
| 568 | const struct android_net_context* netcontext) { |
Bernie Innocenti | f89b351 | 2018-08-30 07:34:37 +0900 | [diff] [blame] | 569 | res_setnetcontext(res, netcontext); |
| 570 | return gethostbyname_internal_real(name, af, res, hp, hbuf, hbuflen, errorp); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 571 | } |
| 572 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 573 | static struct hostent* android_gethostbyaddrfornetcontext_real( |
| 574 | const void* addr, socklen_t len, int af, struct hostent* hp, char* buf, size_t buflen, |
| 575 | int* he, const struct android_net_context* netcontext) { |
| 576 | const u_char* uaddr = (const u_char*) addr; |
| 577 | socklen_t size; |
| 578 | struct getnamaddr info; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 579 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 580 | _DIAGASSERT(addr != NULL); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 581 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 582 | if (af == AF_INET6 && len == NS_IN6ADDRSZ && |
| 583 | (IN6_IS_ADDR_LINKLOCAL((const struct in6_addr*) addr) || |
| 584 | IN6_IS_ADDR_SITELOCAL((const struct in6_addr*) addr))) { |
| 585 | *he = HOST_NOT_FOUND; |
| 586 | return NULL; |
| 587 | } |
| 588 | if (af == AF_INET6 && len == NS_IN6ADDRSZ && |
| 589 | (IN6_IS_ADDR_V4MAPPED((const struct in6_addr*) addr) || |
| 590 | IN6_IS_ADDR_V4COMPAT((const struct in6_addr*) addr))) { |
| 591 | /* Unmap. */ |
| 592 | uaddr += NS_IN6ADDRSZ - NS_INADDRSZ; |
| 593 | addr = uaddr; |
| 594 | af = AF_INET; |
| 595 | len = NS_INADDRSZ; |
| 596 | } |
| 597 | switch (af) { |
| 598 | case AF_INET: |
| 599 | size = NS_INADDRSZ; |
| 600 | break; |
| 601 | case AF_INET6: |
| 602 | size = NS_IN6ADDRSZ; |
| 603 | break; |
| 604 | default: |
| 605 | errno = EAFNOSUPPORT; |
| 606 | *he = NETDB_INTERNAL; |
| 607 | return NULL; |
| 608 | } |
| 609 | if (size != len) { |
| 610 | errno = EINVAL; |
| 611 | *he = NETDB_INTERNAL; |
| 612 | return NULL; |
| 613 | } |
| 614 | info.hp = hp; |
| 615 | info.buf = buf; |
| 616 | info.buflen = buflen; |
| 617 | info.he = he; |
| 618 | *he = NETDB_INTERNAL; |
Bernie Innocenti | a2cbfb1 | 2018-09-12 20:03:11 +0900 | [diff] [blame] | 619 | if (!_hf_gethtbyaddr(uaddr, len, af, &info)) { |
| 620 | if (!_dns_gethtbyaddr(uaddr, len, af, netcontext, &info)) { |
| 621 | return NULL; |
| 622 | } |
| 623 | } |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 624 | *he = NETDB_SUCCESS; |
| 625 | return hp; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 626 | } |
| 627 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 628 | static struct hostent* android_gethostbyaddrfornetcontext_proxy_internal( |
| 629 | const void* addr, socklen_t len, int af, struct hostent* hp, char* hbuf, size_t hbuflen, |
| 630 | int* he, const struct android_net_context* netcontext) { |
Bernie Innocenti | f89b351 | 2018-08-30 07:34:37 +0900 | [diff] [blame] | 631 | return android_gethostbyaddrfornetcontext_real(addr, len, af, hp, hbuf, hbuflen, he, |
| 632 | netcontext); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 633 | } |
| 634 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 635 | struct hostent* netbsd_gethostent_r(FILE* hf, struct hostent* hent, char* buf, size_t buflen, |
| 636 | int* he) { |
Bernie Innocenti | 4acba1a | 2018-09-26 11:52:04 +0900 | [diff] [blame] | 637 | const size_t line_buf_size = sizeof(res_get_static()->hostbuf); |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 638 | char *name; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 639 | char *cp, **q; |
| 640 | int af, len; |
| 641 | size_t anum; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 642 | struct in6_addr host_addr; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 643 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 644 | if (hf == NULL) { |
| 645 | *he = NETDB_INTERNAL; |
| 646 | errno = EINVAL; |
| 647 | return NULL; |
| 648 | } |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 649 | char* p = NULL; |
| 650 | size_t maxaliases = 10; |
| 651 | char** aliases = (char**) malloc(maxaliases * sizeof(char*)); |
| 652 | if (!aliases) goto nospc; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 653 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 654 | /* Allocate a new space to read file lines like upstream does. |
Bernie Innocenti | 4acba1a | 2018-09-26 11:52:04 +0900 | [diff] [blame] | 655 | * To keep reentrancy we cannot use res_get_static()->hostbuf here, |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 656 | * as the buffer may be used to store content for a previous hostent |
| 657 | * returned by non-reentrant functions like gethostbyname(). |
| 658 | */ |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 659 | if ((p = (char*) malloc(line_buf_size)) == NULL) { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 660 | goto nospc; |
| 661 | } |
| 662 | for (;;) { |
| 663 | if (!fgets(p, line_buf_size, hf)) { |
| 664 | free(p); |
| 665 | free(aliases); |
| 666 | *he = HOST_NOT_FOUND; |
| 667 | return NULL; |
| 668 | } |
| 669 | if (*p == '#') { |
| 670 | continue; |
| 671 | } |
| 672 | if (!(cp = strpbrk(p, "#\n"))) { |
| 673 | continue; |
| 674 | } |
| 675 | *cp = '\0'; |
| 676 | if (!(cp = strpbrk(p, " \t"))) continue; |
| 677 | *cp++ = '\0'; |
| 678 | if (inet_pton(AF_INET6, p, &host_addr) > 0) { |
| 679 | af = AF_INET6; |
| 680 | len = NS_IN6ADDRSZ; |
| 681 | } else { |
| 682 | if (inet_pton(AF_INET, p, &host_addr) <= 0) continue; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 683 | |
Bernie Innocenti | 4acba1a | 2018-09-26 11:52:04 +0900 | [diff] [blame] | 684 | res_state res = res_get_state(); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 685 | if (res == NULL) goto nospc; |
| 686 | if (res->options & RES_USE_INET6) { |
| 687 | map_v4v6_address(buf, buf); |
| 688 | af = AF_INET6; |
| 689 | len = NS_IN6ADDRSZ; |
| 690 | } else { |
| 691 | af = AF_INET; |
| 692 | len = NS_INADDRSZ; |
| 693 | } |
Bernie Innocenti | 4acba1a | 2018-09-26 11:52:04 +0900 | [diff] [blame] | 694 | res_put_state(res); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 695 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 696 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 697 | /* if this is not something we're looking for, skip it. */ |
| 698 | if (hent->h_addrtype != 0 && hent->h_addrtype != af) continue; |
| 699 | if (hent->h_length != 0 && hent->h_length != len) continue; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 700 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 701 | while (*cp == ' ' || *cp == '\t') cp++; |
| 702 | if ((cp = strpbrk(name = cp, " \t")) != NULL) *cp++ = '\0'; |
| 703 | q = aliases; |
| 704 | while (cp && *cp) { |
| 705 | if (*cp == ' ' || *cp == '\t') { |
| 706 | cp++; |
| 707 | continue; |
| 708 | } |
| 709 | addalias(q, cp, aliases, maxaliases); |
| 710 | if ((cp = strpbrk(cp, " \t")) != NULL) *cp++ = '\0'; |
| 711 | } |
| 712 | break; |
| 713 | } |
| 714 | hent->h_length = len; |
| 715 | hent->h_addrtype = af; |
| 716 | HENT_ARRAY(hent->h_addr_list, 1, buf, buflen); |
| 717 | anum = (size_t)(q - aliases); |
| 718 | HENT_ARRAY(hent->h_aliases, anum, buf, buflen); |
| 719 | HENT_COPY(hent->h_addr_list[0], &host_addr, hent->h_length, buf, buflen); |
| 720 | hent->h_addr_list[1] = NULL; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 721 | |
nuccachen | 8161c99 | 2018-09-11 11:20:00 +0800 | [diff] [blame] | 722 | /* Reserve space for mapping IPv4 address to IPv6 address in place */ |
| 723 | if (hent->h_addrtype == AF_INET) { |
| 724 | HENT_COPY(buf, NAT64_PAD, sizeof(NAT64_PAD), buf, buflen); |
| 725 | } |
| 726 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 727 | HENT_SCOPY(hent->h_name, name, buf, buflen); |
| 728 | for (size_t i = 0; i < anum; i++) HENT_SCOPY(hent->h_aliases[i], aliases[i], buf, buflen); |
| 729 | hent->h_aliases[anum] = NULL; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 730 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 731 | *he = NETDB_SUCCESS; |
| 732 | free(p); |
| 733 | free(aliases); |
| 734 | return hent; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 735 | nospc: |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 736 | free(p); |
| 737 | free(aliases); |
| 738 | errno = ENOSPC; |
| 739 | *he = NETDB_INTERNAL; |
| 740 | return NULL; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 741 | } |
| 742 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 743 | static void map_v4v6_address(const char* src, char* dst) { |
| 744 | u_char* p = (u_char*) dst; |
| 745 | char tmp[NS_INADDRSZ]; |
| 746 | int i; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 747 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 748 | _DIAGASSERT(src != NULL); |
| 749 | _DIAGASSERT(dst != NULL); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 750 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 751 | /* Stash a temporary copy so our caller can update in place. */ |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 752 | memcpy(tmp, src, NS_INADDRSZ); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 753 | /* Mark this ipv6 addr as a mapped ipv4. */ |
| 754 | for (i = 0; i < 10; i++) *p++ = 0x00; |
| 755 | *p++ = 0xff; |
| 756 | *p++ = 0xff; |
| 757 | /* Retrieve the saved copy and we're done. */ |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 758 | memcpy(p, tmp, NS_INADDRSZ); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 759 | } |
| 760 | |
nuccachen | 0a51173 | 2018-09-18 13:38:48 +0800 | [diff] [blame] | 761 | static void convert_v4v6_hostent(struct hostent* hp, char** bpp, char* ep, |
| 762 | std::function<void(struct hostent* hp)> map_param, |
| 763 | std::function<void(char* src, char* dst)> map_addr) { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 764 | _DIAGASSERT(hp != NULL); |
| 765 | _DIAGASSERT(bpp != NULL); |
| 766 | _DIAGASSERT(ep != NULL); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 767 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 768 | if (hp->h_addrtype != AF_INET || hp->h_length != NS_INADDRSZ) return; |
nuccachen | 0a51173 | 2018-09-18 13:38:48 +0800 | [diff] [blame] | 769 | map_param(hp); |
| 770 | for (char** ap = hp->h_addr_list; *ap; ap++) { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 771 | int i = (int) (sizeof(align) - (size_t)((u_long) *bpp % sizeof(align))); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 772 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 773 | if (ep - *bpp < (i + NS_IN6ADDRSZ)) { |
| 774 | /* Out of memory. Truncate address list here. XXX */ |
| 775 | *ap = NULL; |
| 776 | return; |
| 777 | } |
| 778 | *bpp += i; |
nuccachen | 0a51173 | 2018-09-18 13:38:48 +0800 | [diff] [blame] | 779 | map_addr(*ap, *bpp); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 780 | *ap = *bpp; |
| 781 | *bpp += NS_IN6ADDRSZ; |
| 782 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 783 | } |
| 784 | |
nuccachen | 0a51173 | 2018-09-18 13:38:48 +0800 | [diff] [blame] | 785 | static void map_v4v6_hostent(struct hostent* hp, char** bpp, char* ep) { |
| 786 | convert_v4v6_hostent(hp, bpp, ep, |
| 787 | [](struct hostent* hp) { |
| 788 | hp->h_addrtype = AF_INET6; |
| 789 | hp->h_length = NS_IN6ADDRSZ; |
| 790 | }, |
| 791 | [](char* src, char* dst) { map_v4v6_address(src, dst); }); |
| 792 | } |
| 793 | |
| 794 | /* Reserve space for mapping IPv4 address to IPv6 address in place */ |
| 795 | static void pad_v4v6_hostent(struct hostent* hp, char** bpp, char* ep) { |
| 796 | convert_v4v6_hostent(hp, bpp, ep, |
| 797 | [](struct hostent* hp) { |
| 798 | (void) hp; /* unused */ |
| 799 | }, |
| 800 | [](char* src, char* dst) { |
| 801 | memcpy(dst, src, NS_INADDRSZ); |
| 802 | memcpy(dst + NS_INADDRSZ, NAT64_PAD, sizeof(NAT64_PAD)); |
| 803 | }); |
| 804 | } |
| 805 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 806 | static void addrsort(char** ap, int num, res_state res) { |
| 807 | int i, j; |
| 808 | char** p; |
| 809 | short aval[MAXADDRS]; |
| 810 | int needsort = 0; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 811 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 812 | _DIAGASSERT(ap != NULL); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 813 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 814 | p = ap; |
| 815 | for (i = 0; i < num; i++, p++) { |
| 816 | for (j = 0; (unsigned) j < res->nsort; j++) |
| 817 | if (res->sort_list[j].addr.s_addr == |
| 818 | (((struct in_addr*) (void*) (*p))->s_addr & res->sort_list[j].mask)) |
| 819 | break; |
| 820 | aval[i] = j; |
| 821 | if (needsort == 0 && i > 0 && j < aval[i - 1]) needsort = i; |
| 822 | } |
| 823 | if (!needsort) return; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 824 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 825 | while (needsort < num) { |
| 826 | for (j = needsort - 1; j >= 0; j--) { |
| 827 | if (aval[j] > aval[j + 1]) { |
| 828 | char* hp; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 829 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 830 | i = aval[j]; |
| 831 | aval[j] = aval[j + 1]; |
| 832 | aval[j + 1] = i; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 833 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 834 | hp = ap[j]; |
| 835 | ap[j] = ap[j + 1]; |
| 836 | ap[j + 1] = hp; |
| 837 | } else |
| 838 | break; |
| 839 | } |
| 840 | needsort++; |
| 841 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 842 | } |
| 843 | |
Bernie Innocenti | a2cbfb1 | 2018-09-12 20:03:11 +0900 | [diff] [blame] | 844 | static bool _dns_gethtbyname(const char* name, int addr_type, getnamaddr* info) { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 845 | int n, type; |
| 846 | struct hostent* hp; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 847 | res_state res; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 848 | |
Bernie Innocenti | 9bf0e1d | 2018-09-12 17:59:17 +0900 | [diff] [blame] | 849 | info->hp->h_addrtype = addr_type; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 850 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 851 | switch (info->hp->h_addrtype) { |
| 852 | case AF_INET: |
| 853 | info->hp->h_length = NS_INADDRSZ; |
| 854 | type = T_A; |
| 855 | break; |
| 856 | case AF_INET6: |
| 857 | info->hp->h_length = NS_IN6ADDRSZ; |
| 858 | type = T_AAAA; |
| 859 | break; |
| 860 | default: |
Bernie Innocenti | a2cbfb1 | 2018-09-12 20:03:11 +0900 | [diff] [blame] | 861 | return false; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 862 | } |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 863 | querybuf* buf = (querybuf*) malloc(sizeof(querybuf)); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 864 | if (buf == NULL) { |
| 865 | *info->he = NETDB_INTERNAL; |
Bernie Innocenti | a2cbfb1 | 2018-09-12 20:03:11 +0900 | [diff] [blame] | 866 | return false; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 867 | } |
Bernie Innocenti | 4acba1a | 2018-09-26 11:52:04 +0900 | [diff] [blame] | 868 | res = res_get_state(); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 869 | if (res == NULL) { |
| 870 | free(buf); |
Bernie Innocenti | a2cbfb1 | 2018-09-12 20:03:11 +0900 | [diff] [blame] | 871 | return false; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 872 | } |
| 873 | n = res_nsearch(res, name, C_IN, type, buf->buf, (int) sizeof(buf->buf)); |
| 874 | if (n < 0) { |
| 875 | free(buf); |
| 876 | debugprintf("res_nsearch failed (%d)\n", res, n); |
Bernie Innocenti | 4acba1a | 2018-09-26 11:52:04 +0900 | [diff] [blame] | 877 | res_put_state(res); |
Bernie Innocenti | a2cbfb1 | 2018-09-12 20:03:11 +0900 | [diff] [blame] | 878 | return false; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 879 | } |
| 880 | hp = getanswer(buf, n, name, type, res, info->hp, info->buf, info->buflen, info->he); |
| 881 | free(buf); |
Bernie Innocenti | 4acba1a | 2018-09-26 11:52:04 +0900 | [diff] [blame] | 882 | res_put_state(res); |
Bernie Innocenti | a2cbfb1 | 2018-09-12 20:03:11 +0900 | [diff] [blame] | 883 | if (hp == NULL) { |
| 884 | return false; |
| 885 | } |
| 886 | return true; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 887 | } |
| 888 | |
Bernie Innocenti | a2cbfb1 | 2018-09-12 20:03:11 +0900 | [diff] [blame] | 889 | static bool _dns_gethtbyaddr(const unsigned char* uaddr, int len, int af, |
| 890 | const android_net_context* netcontext, getnamaddr* info) { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 891 | char qbuf[MAXDNAME + 1], *qp, *ep; |
| 892 | int n; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 893 | struct hostent* hp; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 894 | int advance; |
| 895 | res_state res; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 896 | |
Bernie Innocenti | a2cbfb1 | 2018-09-12 20:03:11 +0900 | [diff] [blame] | 897 | info->hp->h_length = len; |
| 898 | info->hp->h_addrtype = af; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 899 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 900 | switch (info->hp->h_addrtype) { |
| 901 | case AF_INET: |
| 902 | (void) snprintf(qbuf, sizeof(qbuf), "%u.%u.%u.%u.in-addr.arpa", (uaddr[3] & 0xff), |
| 903 | (uaddr[2] & 0xff), (uaddr[1] & 0xff), (uaddr[0] & 0xff)); |
| 904 | break; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 905 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 906 | case AF_INET6: |
| 907 | qp = qbuf; |
| 908 | ep = qbuf + sizeof(qbuf) - 1; |
| 909 | for (n = NS_IN6ADDRSZ - 1; n >= 0; n--) { |
| 910 | advance = snprintf(qp, (size_t)(ep - qp), "%x.%x.", uaddr[n] & 0xf, |
| 911 | ((unsigned int) uaddr[n] >> 4) & 0xf); |
| 912 | if (advance > 0 && qp + advance < ep) |
| 913 | qp += advance; |
| 914 | else { |
| 915 | *info->he = NETDB_INTERNAL; |
Bernie Innocenti | a2cbfb1 | 2018-09-12 20:03:11 +0900 | [diff] [blame] | 916 | return false; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 917 | } |
| 918 | } |
| 919 | if (strlcat(qbuf, "ip6.arpa", sizeof(qbuf)) >= sizeof(qbuf)) { |
| 920 | *info->he = NETDB_INTERNAL; |
Bernie Innocenti | a2cbfb1 | 2018-09-12 20:03:11 +0900 | [diff] [blame] | 921 | return false; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 922 | } |
| 923 | break; |
| 924 | default: |
Bernie Innocenti | a2cbfb1 | 2018-09-12 20:03:11 +0900 | [diff] [blame] | 925 | return false; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 926 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 927 | |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 928 | querybuf* buf = (querybuf*) malloc(sizeof(querybuf)); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 929 | if (buf == NULL) { |
| 930 | *info->he = NETDB_INTERNAL; |
Bernie Innocenti | a2cbfb1 | 2018-09-12 20:03:11 +0900 | [diff] [blame] | 931 | return false; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 932 | } |
Bernie Innocenti | 4acba1a | 2018-09-26 11:52:04 +0900 | [diff] [blame] | 933 | res = res_get_state(); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 934 | if (res == NULL) { |
| 935 | free(buf); |
Bernie Innocenti | a2cbfb1 | 2018-09-12 20:03:11 +0900 | [diff] [blame] | 936 | return false; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 937 | } |
| 938 | res_setnetcontext(res, netcontext); |
| 939 | n = res_nquery(res, qbuf, C_IN, T_PTR, buf->buf, (int) sizeof(buf->buf)); |
| 940 | if (n < 0) { |
| 941 | free(buf); |
| 942 | debugprintf("res_nquery failed (%d)\n", res, n); |
Bernie Innocenti | 4acba1a | 2018-09-26 11:52:04 +0900 | [diff] [blame] | 943 | res_put_state(res); |
Bernie Innocenti | a2cbfb1 | 2018-09-12 20:03:11 +0900 | [diff] [blame] | 944 | return false; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 945 | } |
| 946 | hp = getanswer(buf, n, qbuf, T_PTR, res, info->hp, info->buf, info->buflen, info->he); |
| 947 | free(buf); |
| 948 | if (hp == NULL) { |
Bernie Innocenti | 4acba1a | 2018-09-26 11:52:04 +0900 | [diff] [blame] | 949 | res_put_state(res); |
Bernie Innocenti | a2cbfb1 | 2018-09-12 20:03:11 +0900 | [diff] [blame] | 950 | return false; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 951 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 952 | |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 953 | char* bf = (char*) (hp->h_addr_list + 2); |
| 954 | size_t blen = (size_t)(bf - info->buf); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 955 | if (blen + info->hp->h_length > info->buflen) goto nospc; |
| 956 | hp->h_addr_list[0] = bf; |
| 957 | hp->h_addr_list[1] = NULL; |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 958 | memcpy(bf, uaddr, (size_t) info->hp->h_length); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 959 | if (info->hp->h_addrtype == AF_INET && (res->options & RES_USE_INET6)) { |
| 960 | if (blen + NS_IN6ADDRSZ > info->buflen) goto nospc; |
| 961 | map_v4v6_address(bf, bf); |
| 962 | hp->h_addrtype = AF_INET6; |
| 963 | hp->h_length = NS_IN6ADDRSZ; |
| 964 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 965 | |
nuccachen | 8161c99 | 2018-09-11 11:20:00 +0800 | [diff] [blame] | 966 | /* Reserve enough space for mapping IPv4 address to IPv6 address in place */ |
| 967 | if (info->hp->h_addrtype == AF_INET) { |
| 968 | if (blen + NS_IN6ADDRSZ > info->buflen) goto nospc; |
| 969 | // Pad zero to the unused address space |
| 970 | memcpy(bf + NS_INADDRSZ, NAT64_PAD, sizeof(NAT64_PAD)); |
| 971 | } |
| 972 | |
Bernie Innocenti | 4acba1a | 2018-09-26 11:52:04 +0900 | [diff] [blame] | 973 | res_put_state(res); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 974 | *info->he = NETDB_SUCCESS; |
Bernie Innocenti | a2cbfb1 | 2018-09-12 20:03:11 +0900 | [diff] [blame] | 975 | return true; |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 976 | |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 977 | nospc: |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 978 | errno = ENOSPC; |
| 979 | *info->he = NETDB_INTERNAL; |
Bernie Innocenti | a2cbfb1 | 2018-09-12 20:03:11 +0900 | [diff] [blame] | 980 | return false; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 981 | } |
| 982 | |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 983 | /* |
| 984 | * Non-reentrant versions. |
| 985 | */ |
| 986 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 987 | struct hostent* android_gethostbynamefornetcontext(const char* name, int af, |
| 988 | const struct android_net_context* netcontext) { |
| 989 | struct hostent* hp; |
Bernie Innocenti | 4acba1a | 2018-09-26 11:52:04 +0900 | [diff] [blame] | 990 | res_state res = res_get_state(); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 991 | if (res == NULL) return NULL; |
Bernie Innocenti | 4acba1a | 2018-09-26 11:52:04 +0900 | [diff] [blame] | 992 | struct res_static* rs = res_get_static(); // For thread-safety. |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 993 | hp = gethostbyname_internal(name, af, res, &rs->host, rs->hostbuf, sizeof(rs->hostbuf), |
| 994 | &h_errno, netcontext); |
Bernie Innocenti | 4acba1a | 2018-09-26 11:52:04 +0900 | [diff] [blame] | 995 | res_put_state(res); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 996 | return hp; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 997 | } |
| 998 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 999 | struct hostent* android_gethostbyaddrfornetcontext(const void* addr, socklen_t len, int af, |
| 1000 | const struct android_net_context* netcontext) { |
| 1001 | return android_gethostbyaddrfornetcontext_proxy(addr, len, af, netcontext); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1002 | } |
| 1003 | |
Bernie Innocenti | 0e45e2a | 2018-09-14 16:42:36 +0900 | [diff] [blame] | 1004 | static struct hostent* android_gethostbyaddrfornetcontext_proxy( |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1005 | const void* addr, socklen_t len, int af, const struct android_net_context* netcontext) { |
Bernie Innocenti | 4acba1a | 2018-09-26 11:52:04 +0900 | [diff] [blame] | 1006 | struct res_static* rs = res_get_static(); // For thread-safety. |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 1007 | return android_gethostbyaddrfornetcontext_proxy_internal( |
| 1008 | addr, len, af, &rs->host, rs->hostbuf, sizeof(rs->hostbuf), &h_errno, netcontext); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1009 | } |