| 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_types.h> | 
|  | 58 | #include <netinet/in.h> | 
|  | 59 | #include <arpa/inet.h> | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 60 | #include <assert.h> | 
|  | 61 | #include <limits.h> | 
|  | 62 | #include <netdb.h> | 
| Calin Juravle | 569fb98 | 2014-03-04 15:01:29 +0000 | [diff] [blame] | 63 | #include <arpa/nameser.h> | 
| Szymon Jakubczak | ea9bf67 | 2014-02-14 17:07:23 -0500 | [diff] [blame] | 64 | #include "resolv_netid.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 *, | 
| Szymon Jakubczak | ea9bf67 | 2014-02-14 17:07:23 -0500 | [diff] [blame] | 95 | socklen_t, char *, socklen_t, int, unsigned, unsigned); | 
| 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 | */ | 
| Szymon Jakubczak | ea9bf67 | 2014-02-14 17:07:23 -0500 | [diff] [blame] | 108 | int getnameinfo(const struct sockaddr* sa, socklen_t salen, char* host, size_t hostlen, | 
|  | 109 | char* serv, size_t servlen, int flags) | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 110 | { | 
| Szymon Jakubczak | ea9bf67 | 2014-02-14 17:07:23 -0500 | [diff] [blame] | 111 | return android_getnameinfofornet(sa, salen, host, hostlen, serv, servlen, flags, | 
|  | 112 | NETID_UNSET, MARK_UNSET); | 
| Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 113 | } | 
|  | 114 |  | 
| Szymon Jakubczak | ea9bf67 | 2014-02-14 17:07:23 -0500 | [diff] [blame] | 115 | int android_getnameinfofornet(const struct sockaddr* sa, socklen_t salen, char* host, | 
|  | 116 | size_t hostlen, char* serv, size_t servlen, int flags, unsigned netid, | 
|  | 117 | unsigned mark) | 
| Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 118 | { | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 119 | switch (sa->sa_family) { | 
|  | 120 | case AF_INET: | 
|  | 121 | case AF_INET6: | 
|  | 122 | return getnameinfo_inet(sa, salen, host, hostlen, | 
| Szymon Jakubczak | ea9bf67 | 2014-02-14 17:07:23 -0500 | [diff] [blame] | 123 | serv, servlen, flags, netid, mark); | 
| Elliott Hughes | d8213bb | 2013-02-13 09:49:33 -0800 | [diff] [blame] | 124 | case AF_LOCAL: | 
|  | 125 | return getnameinfo_local(sa, salen, host, hostlen, | 
| Robert Greenwalt | b002a2f | 2013-01-19 00:40:24 +0000 | [diff] [blame] | 126 | serv, servlen, flags); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 127 | default: | 
|  | 128 | return EAI_FAMILY; | 
|  | 129 | } | 
|  | 130 | } | 
| Robert Greenwalt | b002a2f | 2013-01-19 00:40:24 +0000 | [diff] [blame] | 131 |  | 
| Elliott Hughes | d8213bb | 2013-02-13 09:49:33 -0800 | [diff] [blame] | 132 | /* | 
|  | 133 | * getnameinfo_local(): | 
|  | 134 | * Format an local address into a printable format. | 
|  | 135 | */ | 
|  | 136 | /* ARGSUSED */ | 
|  | 137 | static int | 
|  | 138 | getnameinfo_local(const struct sockaddr *sa, socklen_t salen, | 
|  | 139 | char *host, socklen_t hostlen, char *serv, socklen_t servlen, | 
|  | 140 | int flags __attribute__((unused))) | 
|  | 141 | { | 
|  | 142 | const struct sockaddr_un *sun = | 
|  | 143 | (const struct sockaddr_un *)(const void *)sa; | 
|  | 144 |  | 
|  | 145 | if (salen < (socklen_t) offsetof(struct sockaddr_un, sun_path)) { | 
|  | 146 | return EAI_FAMILY; | 
|  | 147 | } | 
|  | 148 |  | 
|  | 149 | if (serv != NULL && servlen > 0) | 
|  | 150 | serv[0] = '\0'; | 
|  | 151 |  | 
|  | 152 | if (host && hostlen > 0) | 
|  | 153 | strlcpy(host, sun->sun_path, | 
|  | 154 | MIN((socklen_t) sizeof(sun->sun_path) + 1, hostlen)); | 
|  | 155 |  | 
|  | 156 | return 0; | 
|  | 157 | } | 
|  | 158 |  | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 159 | /* | 
|  | 160 | * getnameinfo_inet(): | 
|  | 161 | * Format an IPv4 or IPv6 sockaddr into a printable string. | 
|  | 162 | */ | 
| Mattias Falk | c63e590 | 2011-08-23 14:34:14 +0200 | [diff] [blame] | 163 | static int | 
| Elliott Hughes | d8213bb | 2013-02-13 09:49:33 -0800 | [diff] [blame] | 164 | getnameinfo_inet(const struct sockaddr* sa, socklen_t salen, | 
|  | 165 | char *host, socklen_t hostlen, | 
|  | 166 | char *serv, socklen_t servlen, | 
| Szymon Jakubczak | ea9bf67 | 2014-02-14 17:07:23 -0500 | [diff] [blame] | 167 | int flags, unsigned netid, unsigned mark) | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 168 | { | 
|  | 169 | const struct afd *afd; | 
|  | 170 | struct servent *sp; | 
|  | 171 | struct hostent *hp; | 
|  | 172 | u_short port; | 
|  | 173 | int family, i; | 
|  | 174 | const char *addr; | 
| Elliott Hughes | d8213bb | 2013-02-13 09:49:33 -0800 | [diff] [blame] | 175 | uint32_t v4a; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 176 | char numserv[512]; | 
|  | 177 | char numaddr[512]; | 
|  | 178 |  | 
|  | 179 | /* sa is checked below */ | 
|  | 180 | /* host may be NULL */ | 
|  | 181 | /* serv may be NULL */ | 
|  | 182 |  | 
|  | 183 | if (sa == NULL) | 
|  | 184 | return EAI_FAIL; | 
|  | 185 |  | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 186 | family = sa->sa_family; | 
|  | 187 | for (i = 0; afdl[i].a_af; i++) | 
|  | 188 | if (afdl[i].a_af == family) { | 
|  | 189 | afd = &afdl[i]; | 
|  | 190 | goto found; | 
|  | 191 | } | 
|  | 192 | return EAI_FAMILY; | 
|  | 193 |  | 
|  | 194 | found: | 
| Elliott Hughes | d8213bb | 2013-02-13 09:49:33 -0800 | [diff] [blame] | 195 | // http://b/1889275: callers should be allowed to provide too much | 
|  | 196 | // space, but not too little. | 
|  | 197 | if (salen < afd->a_socklen) { | 
|  | 198 | return EAI_FAMILY; | 
|  | 199 | } | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 200 |  | 
|  | 201 | /* network byte order */ | 
|  | 202 | port = ((const struct sockinet *)(const void *)sa)->si_port; | 
|  | 203 | addr = (const char *)(const void *)sa + afd->a_off; | 
|  | 204 |  | 
|  | 205 | if (serv == NULL || servlen == 0) { | 
|  | 206 | /* | 
|  | 207 | * do nothing in this case. | 
|  | 208 | * in case you are wondering if "&&" is more correct than | 
|  | 209 | * "||" here: rfc2553bis-03 says that serv == NULL OR | 
|  | 210 | * servlen == 0 means that the caller does not want the result. | 
|  | 211 | */ | 
|  | 212 | } else { | 
|  | 213 | if (flags & NI_NUMERICSERV) | 
|  | 214 | sp = NULL; | 
|  | 215 | else { | 
|  | 216 | sp = getservbyport(port, | 
|  | 217 | (flags & NI_DGRAM) ? "udp" : "tcp"); | 
|  | 218 | } | 
|  | 219 | if (sp) { | 
|  | 220 | if (strlen(sp->s_name) + 1 > (size_t)servlen) | 
|  | 221 | return EAI_MEMORY; | 
|  | 222 | strlcpy(serv, sp->s_name, servlen); | 
|  | 223 | } else { | 
|  | 224 | snprintf(numserv, sizeof(numserv), "%u", ntohs(port)); | 
|  | 225 | if (strlen(numserv) + 1 > (size_t)servlen) | 
|  | 226 | return EAI_MEMORY; | 
|  | 227 | strlcpy(serv, numserv, servlen); | 
|  | 228 | } | 
|  | 229 | } | 
|  | 230 |  | 
|  | 231 | switch (sa->sa_family) { | 
|  | 232 | case AF_INET: | 
| Elliott Hughes | d8213bb | 2013-02-13 09:49:33 -0800 | [diff] [blame] | 233 | v4a = (uint32_t) | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 234 | ntohl(((const struct sockaddr_in *) | 
|  | 235 | (const void *)sa)->sin_addr.s_addr); | 
|  | 236 | if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a)) | 
|  | 237 | flags |= NI_NUMERICHOST; | 
|  | 238 | v4a >>= IN_CLASSA_NSHIFT; | 
|  | 239 | if (v4a == 0) | 
|  | 240 | flags |= NI_NUMERICHOST; | 
|  | 241 | break; | 
|  | 242 | #ifdef INET6 | 
|  | 243 | case AF_INET6: | 
|  | 244 | { | 
|  | 245 | const struct sockaddr_in6 *sin6; | 
|  | 246 | sin6 = (const struct sockaddr_in6 *)(const void *)sa; | 
|  | 247 | switch (sin6->sin6_addr.s6_addr[0]) { | 
|  | 248 | case 0x00: | 
|  | 249 | if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) | 
|  | 250 | ; | 
|  | 251 | else if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr)) | 
|  | 252 | ; | 
|  | 253 | else | 
|  | 254 | flags |= NI_NUMERICHOST; | 
|  | 255 | break; | 
|  | 256 | default: | 
|  | 257 | if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) { | 
|  | 258 | flags |= NI_NUMERICHOST; | 
|  | 259 | } | 
|  | 260 | else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) | 
|  | 261 | flags |= NI_NUMERICHOST; | 
|  | 262 | break; | 
|  | 263 | } | 
|  | 264 | } | 
|  | 265 | break; | 
|  | 266 | #endif | 
|  | 267 | } | 
|  | 268 | if (host == NULL || hostlen == 0) { | 
|  | 269 | /* | 
|  | 270 | * do nothing in this case. | 
|  | 271 | * in case you are wondering if "&&" is more correct than | 
|  | 272 | * "||" here: rfc2553bis-03 says that host == NULL or | 
|  | 273 | * hostlen == 0 means that the caller does not want the result. | 
|  | 274 | */ | 
|  | 275 | } else if (flags & NI_NUMERICHOST) { | 
|  | 276 | size_t numaddrlen; | 
|  | 277 |  | 
|  | 278 | /* NUMERICHOST and NAMEREQD conflicts with each other */ | 
|  | 279 | if (flags & NI_NAMEREQD) | 
|  | 280 | return EAI_NONAME; | 
|  | 281 |  | 
|  | 282 | switch(afd->a_af) { | 
|  | 283 | #ifdef INET6 | 
|  | 284 | case AF_INET6: | 
|  | 285 | { | 
|  | 286 | int error; | 
|  | 287 |  | 
|  | 288 | if ((error = ip6_parsenumeric(sa, addr, host, | 
|  | 289 | hostlen, flags)) != 0) | 
|  | 290 | return(error); | 
|  | 291 | break; | 
|  | 292 | } | 
|  | 293 | #endif | 
|  | 294 | default: | 
|  | 295 | if (inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr)) | 
|  | 296 | == NULL) | 
|  | 297 | return EAI_SYSTEM; | 
|  | 298 | numaddrlen = strlen(numaddr); | 
|  | 299 | if (numaddrlen + 1 > (size_t)hostlen) /* don't forget terminator */ | 
|  | 300 | return EAI_MEMORY; | 
|  | 301 | strlcpy(host, numaddr, hostlen); | 
|  | 302 | break; | 
|  | 303 | } | 
|  | 304 | } else { | 
| Elliott Hughes | 9773fa3 | 2014-12-10 14:56:46 -0800 | [diff] [blame] | 305 | hp = android_gethostbyaddrfornet_proxy(addr, afd->a_addrlen, afd->a_af, netid, mark); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 306 | if (hp) { | 
| Elliott Hughes | d8213bb | 2013-02-13 09:49:33 -0800 | [diff] [blame] | 307 | #if 0 | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 308 | /* | 
|  | 309 | * commented out, since "for local host" is not | 
|  | 310 | * implemented here - see RFC2553 p30 | 
|  | 311 | */ | 
|  | 312 | if (flags & NI_NOFQDN) { | 
|  | 313 | char *p; | 
|  | 314 | p = strchr(hp->h_name, '.'); | 
|  | 315 | if (p) | 
| Szymon Jakubczak | ea9bf67 | 2014-02-14 17:07:23 -0500 | [diff] [blame] | 316 | TODO: Before uncommenting rewrite to avoid modifying hp. | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 317 | *p = '\0'; | 
|  | 318 | } | 
|  | 319 | #endif | 
|  | 320 | if (strlen(hp->h_name) + 1 > (size_t)hostlen) { | 
|  | 321 | return EAI_MEMORY; | 
|  | 322 | } | 
|  | 323 | strlcpy(host, hp->h_name, hostlen); | 
|  | 324 | } else { | 
|  | 325 | if (flags & NI_NAMEREQD) | 
|  | 326 | return EAI_NONAME; | 
|  | 327 | switch(afd->a_af) { | 
|  | 328 | #ifdef INET6 | 
|  | 329 | case AF_INET6: | 
|  | 330 | { | 
|  | 331 | int error; | 
|  | 332 |  | 
|  | 333 | if ((error = ip6_parsenumeric(sa, addr, host, | 
|  | 334 | hostlen, | 
|  | 335 | flags)) != 0) | 
|  | 336 | return(error); | 
|  | 337 | break; | 
|  | 338 | } | 
|  | 339 | #endif | 
|  | 340 | default: | 
|  | 341 | if (inet_ntop(afd->a_af, addr, host, | 
|  | 342 | hostlen) == NULL) | 
|  | 343 | return EAI_SYSTEM; | 
|  | 344 | break; | 
|  | 345 | } | 
|  | 346 | } | 
|  | 347 | } | 
|  | 348 | return(0); | 
|  | 349 | } | 
|  | 350 |  | 
|  | 351 | #ifdef INET6 | 
|  | 352 | static int | 
| Elliott Hughes | d8213bb | 2013-02-13 09:49:33 -0800 | [diff] [blame] | 353 | ip6_parsenumeric(const struct sockaddr *sa, const char *addr, char *host, | 
|  | 354 | socklen_t hostlen, int flags) | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 355 | { | 
|  | 356 | size_t numaddrlen; | 
|  | 357 | char numaddr[512]; | 
|  | 358 |  | 
|  | 359 | assert(sa != NULL); | 
|  | 360 | assert(addr != NULL); | 
|  | 361 | assert(host != NULL); | 
|  | 362 |  | 
|  | 363 | if (inet_ntop(AF_INET6, addr, numaddr, sizeof(numaddr)) == NULL) | 
|  | 364 | return EAI_SYSTEM; | 
|  | 365 |  | 
|  | 366 | numaddrlen = strlen(numaddr); | 
| David 'Digit' Turner | 50ace4f | 2010-06-16 16:36:41 -0700 | [diff] [blame] | 367 | 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] | 368 | return EAI_OVERFLOW; | 
|  | 369 | strlcpy(host, numaddr, hostlen); | 
|  | 370 |  | 
|  | 371 | if (((const struct sockaddr_in6 *)(const void *)sa)->sin6_scope_id) { | 
|  | 372 | char zonebuf[MAXHOSTNAMELEN]; | 
|  | 373 | int zonelen; | 
|  | 374 |  | 
|  | 375 | zonelen = ip6_sa2str( | 
|  | 376 | (const struct sockaddr_in6 *)(const void *)sa, | 
|  | 377 | zonebuf, sizeof(zonebuf), flags); | 
|  | 378 | if (zonelen < 0) | 
|  | 379 | return EAI_OVERFLOW; | 
| David 'Digit' Turner | 50ace4f | 2010-06-16 16:36:41 -0700 | [diff] [blame] | 380 | 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] | 381 | return EAI_OVERFLOW; | 
|  | 382 | /* construct <numeric-addr><delim><zoneid> */ | 
|  | 383 | memcpy(host + numaddrlen + 1, zonebuf, | 
|  | 384 | (size_t)zonelen); | 
|  | 385 | host[numaddrlen] = SCOPE_DELIMITER; | 
|  | 386 | host[numaddrlen + 1 + zonelen] = '\0'; | 
|  | 387 | } | 
|  | 388 |  | 
|  | 389 | return 0; | 
|  | 390 | } | 
|  | 391 |  | 
|  | 392 | /* ARGSUSED */ | 
|  | 393 | static int | 
| Elliott Hughes | d8213bb | 2013-02-13 09:49:33 -0800 | [diff] [blame] | 394 | 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] | 395 | { | 
|  | 396 | unsigned int ifindex; | 
|  | 397 | const struct in6_addr *a6; | 
|  | 398 | int n; | 
|  | 399 |  | 
|  | 400 | assert(sa6 != NULL); | 
|  | 401 | assert(buf != NULL); | 
|  | 402 |  | 
|  | 403 | ifindex = (unsigned int)sa6->sin6_scope_id; | 
|  | 404 | a6 = &sa6->sin6_addr; | 
|  | 405 |  | 
|  | 406 | #ifdef NI_NUMERICSCOPE | 
|  | 407 | if ((flags & NI_NUMERICSCOPE) != 0) { | 
|  | 408 | n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id); | 
|  | 409 | if (n < 0 || n >= bufsiz) | 
|  | 410 | return -1; | 
|  | 411 | else | 
|  | 412 | return n; | 
|  | 413 | } | 
|  | 414 | #endif | 
|  | 415 |  | 
|  | 416 | /* if_indextoname() does not take buffer size.  not a good api... */ | 
|  | 417 | if ((IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) && | 
|  | 418 | bufsiz >= IF_NAMESIZE) { | 
|  | 419 | char *p = if_indextoname(ifindex, buf); | 
|  | 420 | if (p) { | 
|  | 421 | return(strlen(p)); | 
|  | 422 | } | 
|  | 423 | } | 
|  | 424 |  | 
|  | 425 | /* last resort */ | 
|  | 426 | n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id); | 
|  | 427 | if (n < 0 || (size_t) n >= bufsiz) | 
|  | 428 | return -1; | 
|  | 429 | else | 
|  | 430 | return n; | 
|  | 431 | } | 
|  | 432 | #endif /* INET6 */ |