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