| Elliott Hughes | d8213bb | 2013-02-13 09:49:33 -0800 | [diff] [blame] | 1 | /*	$NetBSD: getnameinfo.c,v 1.53 2012/09/26 23:13:00 christos Exp $	*/ | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2 | /*	$KAME: getnameinfo.c,v 1.45 2000/09/25 22:43:56 itojun Exp $	*/ | 
|  | 3 |  | 
|  | 4 | /* | 
|  | 5 | * Copyright (c) 2000 Ben Harris. | 
|  | 6 | * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. | 
|  | 7 | * 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 project 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 PROJECT 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 PROJECT 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 |  | 
|  | 34 | /* | 
|  | 35 | * Issues to be discussed: | 
|  | 36 | * - Thread safe-ness must be checked | 
|  | 37 | * - RFC2553 says that we should raise error on short buffer.  X/Open says | 
|  | 38 | *   we need to truncate the result.  We obey RFC2553 (and X/Open should be | 
|  | 39 | *   modified).  ipngwg rough consensus seems to follow RFC2553. | 
|  | 40 | * - What is "local" in NI_FQDN? | 
|  | 41 | * - NI_NAMEREQD and NI_NUMERICHOST conflict with each other. | 
|  | 42 | * - (KAME extension) always attach textual scopeid (fe80::1%lo0), if | 
|  | 43 | *   sin6_scope_id is filled - standardization status? | 
|  | 44 | *   XXX breaks backward compat for code that expects no scopeid. | 
|  | 45 | *   beware on merge. | 
|  | 46 | */ | 
|  | 47 |  | 
|  | 48 | #include <sys/cdefs.h> | 
|  | 49 | #if defined(LIBC_SCCS) && !defined(lint) | 
| Elliott Hughes | d8213bb | 2013-02-13 09:49:33 -0800 | [diff] [blame] | 50 | __RCSID("$NetBSD: getnameinfo.c,v 1.53 2012/09/26 23:13:00 christos Exp $"); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 51 | #endif /* LIBC_SCCS and not lint */ | 
|  | 52 |  | 
|  | 53 | #include <sys/types.h> | 
|  | 54 | #include <sys/socket.h> | 
| Elliott Hughes | d8213bb | 2013-02-13 09:49:33 -0800 | [diff] [blame] | 55 | #include <sys/un.h> | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 56 | #include <net/if.h> | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 57 | #include <net/if_ieee1394.h> | 
|  | 58 | #include <net/if_types.h> | 
|  | 59 | #include <netinet/in.h> | 
|  | 60 | #include <arpa/inet.h> | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 61 | #include <assert.h> | 
|  | 62 | #include <limits.h> | 
|  | 63 | #include <netdb.h> | 
| Elliott Hughes | d8213bb | 2013-02-13 09:49:33 -0800 | [diff] [blame] | 64 | #include "arpa_nameser.h" | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 65 | #include "resolv_private.h" | 
| Mattias Falk | 149f7df | 2011-02-15 08:45:26 +0100 | [diff] [blame] | 66 | #include <sys/system_properties.h> | 
|  | 67 | #include <stdlib.h> | 
|  | 68 | #include <unistd.h> | 
| Mattias Falk | 149f7df | 2011-02-15 08:45:26 +0100 | [diff] [blame] | 69 | #include <errno.h> | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 70 | #include <stddef.h> | 
|  | 71 | #include <string.h> | 
|  | 72 |  | 
|  | 73 | static const struct afd { | 
|  | 74 | int		a_af; | 
|  | 75 | socklen_t	a_addrlen; | 
|  | 76 | socklen_t	a_socklen; | 
|  | 77 | int		a_off; | 
|  | 78 | } afdl [] = { | 
|  | 79 | #ifdef INET6 | 
|  | 80 | {PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6), | 
|  | 81 | offsetof(struct sockaddr_in6, sin6_addr)}, | 
|  | 82 | #endif | 
|  | 83 | {PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in), | 
|  | 84 | offsetof(struct sockaddr_in, sin_addr)}, | 
|  | 85 | {0, 0, 0, 0}, | 
|  | 86 | }; | 
|  | 87 |  | 
|  | 88 | struct sockinet { | 
|  | 89 | u_char	si_len; | 
|  | 90 | u_char	si_family; | 
|  | 91 | u_short	si_port; | 
|  | 92 | }; | 
|  | 93 |  | 
| Elliott Hughes | d8213bb | 2013-02-13 09:49:33 -0800 | [diff] [blame] | 94 | static int getnameinfo_inet(const struct sockaddr *, socklen_t, char *, | 
| Chad Brubaker | c39214e | 2013-06-20 10:36:56 -0700 | [diff] [blame] | 95 | socklen_t, char *, socklen_t, int, const char*, int); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 96 | #ifdef INET6 | 
| Elliott Hughes | d8213bb | 2013-02-13 09:49:33 -0800 | [diff] [blame] | 97 | static int ip6_parsenumeric(const struct sockaddr *, const char *, char *, | 
|  | 98 | socklen_t, int); | 
|  | 99 | static int ip6_sa2str(const struct sockaddr_in6 *, char *, size_t, int); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 100 | #endif | 
| Elliott Hughes | d8213bb | 2013-02-13 09:49:33 -0800 | [diff] [blame] | 101 | static int getnameinfo_local(const struct sockaddr *, socklen_t, char *, | 
|  | 102 | socklen_t, char *, socklen_t, int); | 
| Selim Gurun | 06e1831 | 2012-02-27 15:58:54 -0800 | [diff] [blame] | 103 |  | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 104 | /* | 
|  | 105 | * Top-level getnameinfo() code.  Look at the address family, and pick an | 
|  | 106 | * appropriate function to call. | 
|  | 107 | */ | 
|  | 108 | int getnameinfo(const struct sockaddr* sa, socklen_t salen, char* host, size_t hostlen, char* serv, size_t servlen, int flags) | 
|  | 109 | { | 
| Chad Brubaker | c39214e | 2013-06-20 10:36:56 -0700 | [diff] [blame] | 110 | return android_getnameinfoforiface(sa, salen, host, hostlen, serv, servlen, flags, NULL, 0); | 
| Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 111 | } | 
|  | 112 |  | 
| Chad Brubaker | c39214e | 2013-06-20 10:36:56 -0700 | [diff] [blame] | 113 | int android_getnameinfoforiface(const struct sockaddr* sa, socklen_t salen, char* host, size_t hostlen, char* serv, size_t servlen, int flags, const char* iface, int mark) | 
| Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 114 | { | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 115 | switch (sa->sa_family) { | 
|  | 116 | case AF_INET: | 
|  | 117 | case AF_INET6: | 
|  | 118 | return getnameinfo_inet(sa, salen, host, hostlen, | 
| Chad Brubaker | c39214e | 2013-06-20 10:36:56 -0700 | [diff] [blame] | 119 | serv, servlen, flags, iface, mark); | 
| Elliott Hughes | d8213bb | 2013-02-13 09:49:33 -0800 | [diff] [blame] | 120 | case AF_LOCAL: | 
|  | 121 | return getnameinfo_local(sa, salen, host, hostlen, | 
| Robert Greenwalt | b002a2f | 2013-01-19 00:40:24 +0000 | [diff] [blame] | 122 | serv, servlen, flags); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 123 | default: | 
|  | 124 | return EAI_FAMILY; | 
|  | 125 | } | 
|  | 126 | } | 
| Robert Greenwalt | b002a2f | 2013-01-19 00:40:24 +0000 | [diff] [blame] | 127 |  | 
| Elliott Hughes | d8213bb | 2013-02-13 09:49:33 -0800 | [diff] [blame] | 128 | /* | 
|  | 129 | * getnameinfo_local(): | 
|  | 130 | * Format an local address into a printable format. | 
|  | 131 | */ | 
|  | 132 | /* ARGSUSED */ | 
|  | 133 | static int | 
|  | 134 | getnameinfo_local(const struct sockaddr *sa, socklen_t salen, | 
|  | 135 | char *host, socklen_t hostlen, char *serv, socklen_t servlen, | 
|  | 136 | int flags __attribute__((unused))) | 
|  | 137 | { | 
|  | 138 | const struct sockaddr_un *sun = | 
|  | 139 | (const struct sockaddr_un *)(const void *)sa; | 
|  | 140 |  | 
|  | 141 | if (salen < (socklen_t) offsetof(struct sockaddr_un, sun_path)) { | 
|  | 142 | return EAI_FAMILY; | 
|  | 143 | } | 
|  | 144 |  | 
|  | 145 | if (serv != NULL && servlen > 0) | 
|  | 146 | serv[0] = '\0'; | 
|  | 147 |  | 
|  | 148 | if (host && hostlen > 0) | 
|  | 149 | strlcpy(host, sun->sun_path, | 
|  | 150 | MIN((socklen_t) sizeof(sun->sun_path) + 1, hostlen)); | 
|  | 151 |  | 
|  | 152 | return 0; | 
|  | 153 | } | 
|  | 154 |  | 
| Mattias Falk | 149f7df | 2011-02-15 08:45:26 +0100 | [diff] [blame] | 155 | /* On success length of the host name is returned. A return | 
|  | 156 | * value of 0 means there's no host name associated with | 
|  | 157 | * the address. On failure -1 is returned in which case | 
|  | 158 | * normal execution flow shall continue. */ | 
|  | 159 | static int | 
| Chad Brubaker | c39214e | 2013-06-20 10:36:56 -0700 | [diff] [blame] | 160 | android_gethostbyaddr_proxy(char* nameBuf, size_t nameBufLen, const void *addr, socklen_t addrLen, int addrFamily, const char* iface, int mark) | 
| Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 161 | { | 
|  | 162 | struct hostent *hostResult = | 
| Chad Brubaker | c39214e | 2013-06-20 10:36:56 -0700 | [diff] [blame] | 163 | android_gethostbyaddrforiface_proxy(addr, addrLen, addrFamily, iface, mark); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 164 |  | 
| Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 165 | if (hostResult == NULL) return 0; | 
| Mattias Falk | 149f7df | 2011-02-15 08:45:26 +0100 | [diff] [blame] | 166 |  | 
| Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 167 | int lengthResult = strlen(hostResult->h_name); | 
| Mattias Falk | 149f7df | 2011-02-15 08:45:26 +0100 | [diff] [blame] | 168 |  | 
| Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 169 | if (nameBuf) strncpy(nameBuf, hostResult->h_name, nameBufLen); | 
|  | 170 | return lengthResult; | 
| Mattias Falk | 149f7df | 2011-02-15 08:45:26 +0100 | [diff] [blame] | 171 | } | 
| Elliott Hughes | d8213bb | 2013-02-13 09:49:33 -0800 | [diff] [blame] | 172 |  | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 173 | /* | 
|  | 174 | * getnameinfo_inet(): | 
|  | 175 | * Format an IPv4 or IPv6 sockaddr into a printable string. | 
|  | 176 | */ | 
| Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 177 | static int | 
| Elliott Hughes | d8213bb | 2013-02-13 09:49:33 -0800 | [diff] [blame] | 178 | getnameinfo_inet(const struct sockaddr* sa, socklen_t salen, | 
|  | 179 | char *host, socklen_t hostlen, | 
|  | 180 | char *serv, socklen_t servlen, | 
| Chad Brubaker | c39214e | 2013-06-20 10:36:56 -0700 | [diff] [blame] | 181 | int flags, const char* iface, int mark) | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 182 | { | 
|  | 183 | const struct afd *afd; | 
|  | 184 | struct servent *sp; | 
|  | 185 | struct hostent *hp; | 
|  | 186 | u_short port; | 
|  | 187 | int family, i; | 
|  | 188 | const char *addr; | 
| Elliott Hughes | d8213bb | 2013-02-13 09:49:33 -0800 | [diff] [blame] | 189 | uint32_t v4a; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 190 | char numserv[512]; | 
|  | 191 | char numaddr[512]; | 
|  | 192 |  | 
|  | 193 | /* sa is checked below */ | 
|  | 194 | /* host may be NULL */ | 
|  | 195 | /* serv may be NULL */ | 
|  | 196 |  | 
|  | 197 | if (sa == NULL) | 
|  | 198 | return EAI_FAIL; | 
|  | 199 |  | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 200 | family = sa->sa_family; | 
|  | 201 | for (i = 0; afdl[i].a_af; i++) | 
|  | 202 | if (afdl[i].a_af == family) { | 
|  | 203 | afd = &afdl[i]; | 
|  | 204 | goto found; | 
|  | 205 | } | 
|  | 206 | return EAI_FAMILY; | 
|  | 207 |  | 
|  | 208 | found: | 
| Elliott Hughes | d8213bb | 2013-02-13 09:49:33 -0800 | [diff] [blame] | 209 | // http://b/1889275: callers should be allowed to provide too much | 
|  | 210 | // space, but not too little. | 
|  | 211 | if (salen < afd->a_socklen) { | 
|  | 212 | return EAI_FAMILY; | 
|  | 213 | } | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 214 |  | 
|  | 215 | /* network byte order */ | 
|  | 216 | port = ((const struct sockinet *)(const void *)sa)->si_port; | 
|  | 217 | addr = (const char *)(const void *)sa + afd->a_off; | 
|  | 218 |  | 
|  | 219 | if (serv == NULL || servlen == 0) { | 
|  | 220 | /* | 
|  | 221 | * do nothing in this case. | 
|  | 222 | * in case you are wondering if "&&" is more correct than | 
|  | 223 | * "||" here: rfc2553bis-03 says that serv == NULL OR | 
|  | 224 | * servlen == 0 means that the caller does not want the result. | 
|  | 225 | */ | 
|  | 226 | } else { | 
|  | 227 | if (flags & NI_NUMERICSERV) | 
|  | 228 | sp = NULL; | 
|  | 229 | else { | 
|  | 230 | sp = getservbyport(port, | 
|  | 231 | (flags & NI_DGRAM) ? "udp" : "tcp"); | 
|  | 232 | } | 
|  | 233 | if (sp) { | 
|  | 234 | if (strlen(sp->s_name) + 1 > (size_t)servlen) | 
|  | 235 | return EAI_MEMORY; | 
|  | 236 | strlcpy(serv, sp->s_name, servlen); | 
|  | 237 | } else { | 
|  | 238 | snprintf(numserv, sizeof(numserv), "%u", ntohs(port)); | 
|  | 239 | if (strlen(numserv) + 1 > (size_t)servlen) | 
|  | 240 | return EAI_MEMORY; | 
|  | 241 | strlcpy(serv, numserv, servlen); | 
|  | 242 | } | 
|  | 243 | } | 
|  | 244 |  | 
|  | 245 | switch (sa->sa_family) { | 
|  | 246 | case AF_INET: | 
| Elliott Hughes | d8213bb | 2013-02-13 09:49:33 -0800 | [diff] [blame] | 247 | v4a = (uint32_t) | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 248 | ntohl(((const struct sockaddr_in *) | 
|  | 249 | (const void *)sa)->sin_addr.s_addr); | 
|  | 250 | if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a)) | 
|  | 251 | flags |= NI_NUMERICHOST; | 
|  | 252 | v4a >>= IN_CLASSA_NSHIFT; | 
|  | 253 | if (v4a == 0) | 
|  | 254 | flags |= NI_NUMERICHOST; | 
|  | 255 | break; | 
|  | 256 | #ifdef INET6 | 
|  | 257 | case AF_INET6: | 
|  | 258 | { | 
|  | 259 | const struct sockaddr_in6 *sin6; | 
|  | 260 | sin6 = (const struct sockaddr_in6 *)(const void *)sa; | 
|  | 261 | switch (sin6->sin6_addr.s6_addr[0]) { | 
|  | 262 | case 0x00: | 
|  | 263 | if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) | 
|  | 264 | ; | 
|  | 265 | else if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr)) | 
|  | 266 | ; | 
|  | 267 | else | 
|  | 268 | flags |= NI_NUMERICHOST; | 
|  | 269 | break; | 
|  | 270 | default: | 
|  | 271 | if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) { | 
|  | 272 | flags |= NI_NUMERICHOST; | 
|  | 273 | } | 
|  | 274 | else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) | 
|  | 275 | flags |= NI_NUMERICHOST; | 
|  | 276 | break; | 
|  | 277 | } | 
|  | 278 | } | 
|  | 279 | break; | 
|  | 280 | #endif | 
|  | 281 | } | 
|  | 282 | if (host == NULL || hostlen == 0) { | 
|  | 283 | /* | 
|  | 284 | * do nothing in this case. | 
|  | 285 | * in case you are wondering if "&&" is more correct than | 
|  | 286 | * "||" here: rfc2553bis-03 says that host == NULL or | 
|  | 287 | * hostlen == 0 means that the caller does not want the result. | 
|  | 288 | */ | 
|  | 289 | } else if (flags & NI_NUMERICHOST) { | 
|  | 290 | size_t numaddrlen; | 
|  | 291 |  | 
|  | 292 | /* NUMERICHOST and NAMEREQD conflicts with each other */ | 
|  | 293 | if (flags & NI_NAMEREQD) | 
|  | 294 | return EAI_NONAME; | 
|  | 295 |  | 
|  | 296 | switch(afd->a_af) { | 
|  | 297 | #ifdef INET6 | 
|  | 298 | case AF_INET6: | 
|  | 299 | { | 
|  | 300 | int error; | 
|  | 301 |  | 
|  | 302 | if ((error = ip6_parsenumeric(sa, addr, host, | 
|  | 303 | hostlen, flags)) != 0) | 
|  | 304 | return(error); | 
|  | 305 | break; | 
|  | 306 | } | 
|  | 307 | #endif | 
|  | 308 | default: | 
|  | 309 | if (inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr)) | 
|  | 310 | == NULL) | 
|  | 311 | return EAI_SYSTEM; | 
|  | 312 | numaddrlen = strlen(numaddr); | 
|  | 313 | if (numaddrlen + 1 > (size_t)hostlen) /* don't forget terminator */ | 
|  | 314 | return EAI_MEMORY; | 
|  | 315 | strlcpy(host, numaddr, hostlen); | 
|  | 316 | break; | 
|  | 317 | } | 
|  | 318 | } else { | 
| Mattias Falk | 149f7df | 2011-02-15 08:45:26 +0100 | [diff] [blame] | 319 | struct hostent android_proxy_hostent; | 
|  | 320 | char android_proxy_buf[MAXDNAME]; | 
| Mattias Falk | 149f7df | 2011-02-15 08:45:26 +0100 | [diff] [blame] | 321 |  | 
| Selim Gurun | 06e1831 | 2012-02-27 15:58:54 -0800 | [diff] [blame] | 322 | int hostnamelen = android_gethostbyaddr_proxy(android_proxy_buf, | 
| Chad Brubaker | c39214e | 2013-06-20 10:36:56 -0700 | [diff] [blame] | 323 | MAXDNAME, addr, afd->a_addrlen, afd->a_af, iface, mark); | 
| Selim Gurun | 06e1831 | 2012-02-27 15:58:54 -0800 | [diff] [blame] | 324 | if (hostnamelen > 0) { | 
|  | 325 | hp = &android_proxy_hostent; | 
|  | 326 | hp->h_name = android_proxy_buf; | 
|  | 327 | } else if (!hostnamelen) { | 
|  | 328 | hp = NULL; | 
| Mattias Falk | 149f7df | 2011-02-15 08:45:26 +0100 | [diff] [blame] | 329 | } else { | 
| Chad Brubaker | c39214e | 2013-06-20 10:36:56 -0700 | [diff] [blame] | 330 | hp = android_gethostbyaddrforiface(addr, afd->a_addrlen, afd->a_af, | 
|  | 331 | iface, mark); | 
| Mattias Falk | 149f7df | 2011-02-15 08:45:26 +0100 | [diff] [blame] | 332 | } | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 333 |  | 
|  | 334 | if (hp) { | 
| Elliott Hughes | d8213bb | 2013-02-13 09:49:33 -0800 | [diff] [blame] | 335 | #if 0 | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 336 | /* | 
|  | 337 | * commented out, since "for local host" is not | 
|  | 338 | * implemented here - see RFC2553 p30 | 
|  | 339 | */ | 
|  | 340 | if (flags & NI_NOFQDN) { | 
|  | 341 | char *p; | 
|  | 342 | p = strchr(hp->h_name, '.'); | 
|  | 343 | if (p) | 
|  | 344 | *p = '\0'; | 
|  | 345 | } | 
|  | 346 | #endif | 
|  | 347 | if (strlen(hp->h_name) + 1 > (size_t)hostlen) { | 
|  | 348 | return EAI_MEMORY; | 
|  | 349 | } | 
|  | 350 | strlcpy(host, hp->h_name, hostlen); | 
|  | 351 | } else { | 
|  | 352 | if (flags & NI_NAMEREQD) | 
|  | 353 | return EAI_NONAME; | 
|  | 354 | switch(afd->a_af) { | 
|  | 355 | #ifdef INET6 | 
|  | 356 | case AF_INET6: | 
|  | 357 | { | 
|  | 358 | int error; | 
|  | 359 |  | 
|  | 360 | if ((error = ip6_parsenumeric(sa, addr, host, | 
|  | 361 | hostlen, | 
|  | 362 | flags)) != 0) | 
|  | 363 | return(error); | 
|  | 364 | break; | 
|  | 365 | } | 
|  | 366 | #endif | 
|  | 367 | default: | 
|  | 368 | if (inet_ntop(afd->a_af, addr, host, | 
|  | 369 | hostlen) == NULL) | 
|  | 370 | return EAI_SYSTEM; | 
|  | 371 | break; | 
|  | 372 | } | 
|  | 373 | } | 
|  | 374 | } | 
|  | 375 | return(0); | 
|  | 376 | } | 
|  | 377 |  | 
|  | 378 | #ifdef INET6 | 
|  | 379 | static int | 
| Elliott Hughes | d8213bb | 2013-02-13 09:49:33 -0800 | [diff] [blame] | 380 | ip6_parsenumeric(const struct sockaddr *sa, const char *addr, char *host, | 
|  | 381 | socklen_t hostlen, int flags) | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 382 | { | 
|  | 383 | size_t numaddrlen; | 
|  | 384 | char numaddr[512]; | 
|  | 385 |  | 
|  | 386 | assert(sa != NULL); | 
|  | 387 | assert(addr != NULL); | 
|  | 388 | assert(host != NULL); | 
|  | 389 |  | 
|  | 390 | if (inet_ntop(AF_INET6, addr, numaddr, sizeof(numaddr)) == NULL) | 
|  | 391 | return EAI_SYSTEM; | 
|  | 392 |  | 
|  | 393 | numaddrlen = strlen(numaddr); | 
| David 'Digit' Turner | 50ace4f | 2010-06-16 16:36:41 -0700 | [diff] [blame] | 394 | if (numaddrlen + 1 > (size_t)hostlen) /* don't forget terminator */ | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 395 | return EAI_OVERFLOW; | 
|  | 396 | strlcpy(host, numaddr, hostlen); | 
|  | 397 |  | 
|  | 398 | if (((const struct sockaddr_in6 *)(const void *)sa)->sin6_scope_id) { | 
|  | 399 | char zonebuf[MAXHOSTNAMELEN]; | 
|  | 400 | int zonelen; | 
|  | 401 |  | 
|  | 402 | zonelen = ip6_sa2str( | 
|  | 403 | (const struct sockaddr_in6 *)(const void *)sa, | 
|  | 404 | zonebuf, sizeof(zonebuf), flags); | 
|  | 405 | if (zonelen < 0) | 
|  | 406 | return EAI_OVERFLOW; | 
| David 'Digit' Turner | 50ace4f | 2010-06-16 16:36:41 -0700 | [diff] [blame] | 407 | if ((size_t) zonelen + 1 + numaddrlen + 1 > (size_t)hostlen) | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 408 | return EAI_OVERFLOW; | 
|  | 409 | /* construct <numeric-addr><delim><zoneid> */ | 
|  | 410 | memcpy(host + numaddrlen + 1, zonebuf, | 
|  | 411 | (size_t)zonelen); | 
|  | 412 | host[numaddrlen] = SCOPE_DELIMITER; | 
|  | 413 | host[numaddrlen + 1 + zonelen] = '\0'; | 
|  | 414 | } | 
|  | 415 |  | 
|  | 416 | return 0; | 
|  | 417 | } | 
|  | 418 |  | 
|  | 419 | /* ARGSUSED */ | 
|  | 420 | static int | 
| Elliott Hughes | d8213bb | 2013-02-13 09:49:33 -0800 | [diff] [blame] | 421 | ip6_sa2str(const struct sockaddr_in6 *sa6, char *buf, size_t bufsiz, int flags) | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 422 | { | 
|  | 423 | unsigned int ifindex; | 
|  | 424 | const struct in6_addr *a6; | 
|  | 425 | int n; | 
|  | 426 |  | 
|  | 427 | assert(sa6 != NULL); | 
|  | 428 | assert(buf != NULL); | 
|  | 429 |  | 
|  | 430 | ifindex = (unsigned int)sa6->sin6_scope_id; | 
|  | 431 | a6 = &sa6->sin6_addr; | 
|  | 432 |  | 
|  | 433 | #ifdef NI_NUMERICSCOPE | 
|  | 434 | if ((flags & NI_NUMERICSCOPE) != 0) { | 
|  | 435 | n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id); | 
|  | 436 | if (n < 0 || n >= bufsiz) | 
|  | 437 | return -1; | 
|  | 438 | else | 
|  | 439 | return n; | 
|  | 440 | } | 
|  | 441 | #endif | 
|  | 442 |  | 
|  | 443 | /* if_indextoname() does not take buffer size.  not a good api... */ | 
|  | 444 | if ((IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) && | 
|  | 445 | bufsiz >= IF_NAMESIZE) { | 
|  | 446 | char *p = if_indextoname(ifindex, buf); | 
|  | 447 | if (p) { | 
|  | 448 | return(strlen(p)); | 
|  | 449 | } | 
|  | 450 | } | 
|  | 451 |  | 
|  | 452 | /* last resort */ | 
|  | 453 | n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id); | 
|  | 454 | if (n < 0 || (size_t) n >= bufsiz) | 
|  | 455 | return -1; | 
|  | 456 | else | 
|  | 457 | return n; | 
|  | 458 | } | 
|  | 459 | #endif /* INET6 */ |