The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1 | /* $NetBSD: getnameinfo.c,v 1.43 2006/02/17 15:58:26 ginsbach Exp $ */ |
| 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) |
| 50 | __RCSID("$NetBSD: getnameinfo.c,v 1.43 2006/02/17 15:58:26 ginsbach Exp $"); |
| 51 | #endif /* LIBC_SCCS and not lint */ |
| 52 | |
| 53 | #include <sys/types.h> |
| 54 | #include <sys/socket.h> |
| 55 | #include <net/if.h> |
| 56 | #include <net/if_dl.h> |
| 57 | #include <net/if_ieee1394.h> |
| 58 | #include <net/if_types.h> |
| 59 | #include <netinet/in.h> |
| 60 | #include <arpa/inet.h> |
| 61 | #include "arpa_nameser.h" |
| 62 | #include <assert.h> |
| 63 | #include <limits.h> |
| 64 | #include <netdb.h> |
| 65 | #ifdef ANDROID_CHANGES |
| 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> |
| 70 | #include <sys/un.h> |
| 71 | #include <errno.h> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 72 | #else |
| 73 | #include <resolv.h> |
| 74 | #endif |
| 75 | #include <stddef.h> |
| 76 | #include <string.h> |
| 77 | |
| 78 | static const struct afd { |
| 79 | int a_af; |
| 80 | socklen_t a_addrlen; |
| 81 | socklen_t a_socklen; |
| 82 | int a_off; |
| 83 | } afdl [] = { |
| 84 | #ifdef INET6 |
| 85 | {PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6), |
| 86 | offsetof(struct sockaddr_in6, sin6_addr)}, |
| 87 | #endif |
| 88 | {PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in), |
| 89 | offsetof(struct sockaddr_in, sin_addr)}, |
| 90 | {0, 0, 0, 0}, |
| 91 | }; |
| 92 | |
| 93 | struct sockinet { |
| 94 | u_char si_len; |
| 95 | u_char si_family; |
| 96 | u_short si_port; |
| 97 | }; |
| 98 | |
| 99 | static int getnameinfo_inet __P((const struct sockaddr *, socklen_t, char *, |
| 100 | socklen_t, char *, socklen_t, int)); |
| 101 | #ifdef INET6 |
| 102 | static int ip6_parsenumeric __P((const struct sockaddr *, const char *, char *, |
| 103 | socklen_t, int)); |
| 104 | static int ip6_sa2str __P((const struct sockaddr_in6 *, char *, size_t, |
| 105 | int)); |
| 106 | #endif |
| 107 | static int getnameinfo_link __P((const struct sockaddr *, socklen_t, char *, |
| 108 | socklen_t, char *, socklen_t, int)); |
| 109 | static int hexname __P((const u_int8_t *, size_t, char *, socklen_t)); |
| 110 | |
| 111 | /* |
| 112 | * Top-level getnameinfo() code. Look at the address family, and pick an |
| 113 | * appropriate function to call. |
| 114 | */ |
| 115 | int getnameinfo(const struct sockaddr* sa, socklen_t salen, char* host, size_t hostlen, char* serv, size_t servlen, int flags) |
| 116 | { |
| 117 | switch (sa->sa_family) { |
| 118 | case AF_INET: |
| 119 | case AF_INET6: |
| 120 | return getnameinfo_inet(sa, salen, host, hostlen, |
| 121 | serv, servlen, flags); |
| 122 | #if 0 |
| 123 | case AF_LINK: |
| 124 | return getnameinfo_link(sa, salen, host, hostlen, |
| 125 | serv, servlen, flags); |
| 126 | #endif |
| 127 | default: |
| 128 | return EAI_FAMILY; |
| 129 | } |
| 130 | } |
| 131 | |
Mattias Falk | 149f7df | 2011-02-15 08:45:26 +0100 | [diff] [blame] | 132 | #ifdef ANDROID_CHANGES |
| 133 | /* On success length of the host name is returned. A return |
| 134 | * value of 0 means there's no host name associated with |
| 135 | * the address. On failure -1 is returned in which case |
| 136 | * normal execution flow shall continue. */ |
| 137 | static int |
Brian Carlstrom | c13fa64 | 2011-04-27 11:55:43 -0700 | [diff] [blame] | 138 | android_gethostbyaddr_proxy(struct hostent* hp, const void *addr, socklen_t addrLen, int addrFamily) { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 139 | |
Mattias Falk | 149f7df | 2011-02-15 08:45:26 +0100 | [diff] [blame] | 140 | int sock; |
| 141 | const int one = 1; |
| 142 | struct sockaddr_un proxy_addr; |
| 143 | const char* cache_mode = getenv("ANDROID_DNS_MODE"); |
| 144 | FILE* proxy = NULL; |
| 145 | int result = -1; |
| 146 | |
| 147 | if (cache_mode != NULL && strcmp(cache_mode, "local") == 0) { |
| 148 | // Don't use the proxy in local mode. This is used by the |
| 149 | // proxy itself. |
| 150 | return -1; |
| 151 | } |
| 152 | |
| 153 | // Temporary cautious hack to disable the DNS proxy for processes |
| 154 | // requesting special treatment. Ideally the DNS proxy should |
| 155 | // accomodate these apps, though. |
| 156 | char propname[PROP_NAME_MAX]; |
| 157 | char propvalue[PROP_VALUE_MAX]; |
| 158 | snprintf(propname, sizeof(propname), "net.dns1.%d", getpid()); |
| 159 | if (__system_property_get(propname, propvalue) > 0) { |
| 160 | return -1; |
| 161 | } |
| 162 | // create socket |
| 163 | sock = socket(AF_UNIX, SOCK_STREAM, 0); |
| 164 | if (sock < 0) { |
| 165 | return -1; |
| 166 | } |
| 167 | |
| 168 | setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)); |
| 169 | memset(&proxy_addr, 0, sizeof(proxy_addr)); |
| 170 | proxy_addr.sun_family = AF_UNIX; |
| 171 | strlcpy(proxy_addr.sun_path, "/dev/socket/dnsproxyd", |
| 172 | sizeof(proxy_addr.sun_path)); |
Brian Carlstrom | c13fa64 | 2011-04-27 11:55:43 -0700 | [diff] [blame] | 173 | if (TEMP_FAILURE_RETRY(connect(sock, (const struct sockaddr*) (void*) &proxy_addr, |
Mattias Falk | 149f7df | 2011-02-15 08:45:26 +0100 | [diff] [blame] | 174 | sizeof(proxy_addr))) != 0) { |
| 175 | close(sock); |
| 176 | return -1; |
| 177 | } |
| 178 | |
| 179 | // send request to DnsProxyListener |
| 180 | proxy = fdopen(sock,"r+"); |
| 181 | if (proxy == NULL) { |
| 182 | goto exit; |
| 183 | } |
| 184 | |
Brian Carlstrom | c13fa64 | 2011-04-27 11:55:43 -0700 | [diff] [blame] | 185 | char buf[INET6_ADDRSTRLEN]; // big enough for IPv4 and IPv6 |
| 186 | const char* addrStr = inet_ntop(addrFamily, addr, &buf, sizeof(buf)); |
| 187 | if (addrStr == NULL) { |
| 188 | goto exit; |
| 189 | } |
| 190 | if (fprintf(proxy, "gethostbyaddr %s %d %d", addrStr, addrLen, addrFamily) < 0) { |
Mattias Falk | 149f7df | 2011-02-15 08:45:26 +0100 | [diff] [blame] | 191 | goto exit; |
| 192 | } |
| 193 | |
| 194 | // literal NULL byte at end, required by FrameworkListener |
| 195 | if (fputc(0, proxy) == EOF || fflush(proxy) != 0) { |
| 196 | goto exit; |
| 197 | } |
| 198 | |
| 199 | result = 0; |
| 200 | uint32_t name_len; |
| 201 | if (fread(&name_len, sizeof(name_len), 1, proxy) != 1) { |
| 202 | goto exit; |
| 203 | } |
| 204 | |
| 205 | name_len = ntohl(name_len); |
| 206 | if (name_len <= 0) { |
| 207 | goto exit; |
| 208 | } |
| 209 | |
| 210 | if (fread(hp->h_name, name_len, 1, proxy) != 1) { |
| 211 | goto exit; |
| 212 | } |
| 213 | |
| 214 | result = name_len; |
| 215 | |
| 216 | exit: |
| 217 | if (proxy != NULL) { |
| 218 | fclose(proxy); |
| 219 | } |
| 220 | |
| 221 | return result; |
| 222 | } |
| 223 | #endif |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 224 | /* |
| 225 | * getnameinfo_inet(): |
| 226 | * Format an IPv4 or IPv6 sockaddr into a printable string. |
| 227 | */ |
| 228 | static int |
| 229 | getnameinfo_inet(sa, salen, host, hostlen, serv, servlen, flags) |
| 230 | const struct sockaddr *sa; |
| 231 | socklen_t salen; |
| 232 | char *host; |
| 233 | socklen_t hostlen; |
| 234 | char *serv; |
| 235 | socklen_t servlen; |
| 236 | int flags; |
| 237 | { |
| 238 | const struct afd *afd; |
| 239 | struct servent *sp; |
| 240 | struct hostent *hp; |
| 241 | u_short port; |
| 242 | int family, i; |
| 243 | const char *addr; |
| 244 | u_int32_t v4a; |
| 245 | char numserv[512]; |
| 246 | char numaddr[512]; |
| 247 | |
| 248 | /* sa is checked below */ |
| 249 | /* host may be NULL */ |
| 250 | /* serv may be NULL */ |
| 251 | |
| 252 | if (sa == NULL) |
| 253 | return EAI_FAIL; |
| 254 | |
| 255 | #ifdef BSD4_4 |
| 256 | if (sa->sa_len != salen) |
| 257 | return EAI_FAIL; |
| 258 | #endif |
| 259 | |
| 260 | family = sa->sa_family; |
| 261 | for (i = 0; afdl[i].a_af; i++) |
| 262 | if (afdl[i].a_af == family) { |
| 263 | afd = &afdl[i]; |
| 264 | goto found; |
| 265 | } |
| 266 | return EAI_FAMILY; |
| 267 | |
| 268 | found: |
| 269 | if (salen != afd->a_socklen) |
| 270 | return EAI_FAIL; |
| 271 | |
| 272 | /* network byte order */ |
| 273 | port = ((const struct sockinet *)(const void *)sa)->si_port; |
| 274 | addr = (const char *)(const void *)sa + afd->a_off; |
| 275 | |
| 276 | if (serv == NULL || servlen == 0) { |
| 277 | /* |
| 278 | * do nothing in this case. |
| 279 | * in case you are wondering if "&&" is more correct than |
| 280 | * "||" here: rfc2553bis-03 says that serv == NULL OR |
| 281 | * servlen == 0 means that the caller does not want the result. |
| 282 | */ |
| 283 | } else { |
| 284 | if (flags & NI_NUMERICSERV) |
| 285 | sp = NULL; |
| 286 | else { |
| 287 | sp = getservbyport(port, |
| 288 | (flags & NI_DGRAM) ? "udp" : "tcp"); |
| 289 | } |
| 290 | if (sp) { |
| 291 | if (strlen(sp->s_name) + 1 > (size_t)servlen) |
| 292 | return EAI_MEMORY; |
| 293 | strlcpy(serv, sp->s_name, servlen); |
| 294 | } else { |
| 295 | snprintf(numserv, sizeof(numserv), "%u", ntohs(port)); |
| 296 | if (strlen(numserv) + 1 > (size_t)servlen) |
| 297 | return EAI_MEMORY; |
| 298 | strlcpy(serv, numserv, servlen); |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | switch (sa->sa_family) { |
| 303 | case AF_INET: |
| 304 | v4a = (u_int32_t) |
| 305 | ntohl(((const struct sockaddr_in *) |
| 306 | (const void *)sa)->sin_addr.s_addr); |
| 307 | if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a)) |
| 308 | flags |= NI_NUMERICHOST; |
| 309 | v4a >>= IN_CLASSA_NSHIFT; |
| 310 | if (v4a == 0) |
| 311 | flags |= NI_NUMERICHOST; |
| 312 | break; |
| 313 | #ifdef INET6 |
| 314 | case AF_INET6: |
| 315 | { |
| 316 | const struct sockaddr_in6 *sin6; |
| 317 | sin6 = (const struct sockaddr_in6 *)(const void *)sa; |
| 318 | switch (sin6->sin6_addr.s6_addr[0]) { |
| 319 | case 0x00: |
| 320 | if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) |
| 321 | ; |
| 322 | else if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr)) |
| 323 | ; |
| 324 | else |
| 325 | flags |= NI_NUMERICHOST; |
| 326 | break; |
| 327 | default: |
| 328 | if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) { |
| 329 | flags |= NI_NUMERICHOST; |
| 330 | } |
| 331 | else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) |
| 332 | flags |= NI_NUMERICHOST; |
| 333 | break; |
| 334 | } |
| 335 | } |
| 336 | break; |
| 337 | #endif |
| 338 | } |
| 339 | if (host == NULL || hostlen == 0) { |
| 340 | /* |
| 341 | * do nothing in this case. |
| 342 | * in case you are wondering if "&&" is more correct than |
| 343 | * "||" here: rfc2553bis-03 says that host == NULL or |
| 344 | * hostlen == 0 means that the caller does not want the result. |
| 345 | */ |
| 346 | } else if (flags & NI_NUMERICHOST) { |
| 347 | size_t numaddrlen; |
| 348 | |
| 349 | /* NUMERICHOST and NAMEREQD conflicts with each other */ |
| 350 | if (flags & NI_NAMEREQD) |
| 351 | return EAI_NONAME; |
| 352 | |
| 353 | switch(afd->a_af) { |
| 354 | #ifdef INET6 |
| 355 | case AF_INET6: |
| 356 | { |
| 357 | int error; |
| 358 | |
| 359 | if ((error = ip6_parsenumeric(sa, addr, host, |
| 360 | hostlen, flags)) != 0) |
| 361 | return(error); |
| 362 | break; |
| 363 | } |
| 364 | #endif |
| 365 | default: |
| 366 | if (inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr)) |
| 367 | == NULL) |
| 368 | return EAI_SYSTEM; |
| 369 | numaddrlen = strlen(numaddr); |
| 370 | if (numaddrlen + 1 > (size_t)hostlen) /* don't forget terminator */ |
| 371 | return EAI_MEMORY; |
| 372 | strlcpy(host, numaddr, hostlen); |
| 373 | break; |
| 374 | } |
| 375 | } else { |
Mattias Falk | 149f7df | 2011-02-15 08:45:26 +0100 | [diff] [blame] | 376 | #ifdef ANDROID_CHANGES |
| 377 | struct hostent android_proxy_hostent; |
| 378 | char android_proxy_buf[MAXDNAME]; |
| 379 | android_proxy_hostent.h_name = android_proxy_buf; |
| 380 | |
| 381 | int hostnamelen = android_gethostbyaddr_proxy(&android_proxy_hostent, |
| 382 | addr, afd->a_addrlen, afd->a_af); |
| 383 | if (hostnamelen >= 0) { |
| 384 | hp = (hostnamelen > 0) ? &android_proxy_hostent : NULL; |
| 385 | } else { |
| 386 | hp = gethostbyaddr(addr, afd->a_addrlen, afd->a_af); |
| 387 | } |
| 388 | #else |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 389 | hp = gethostbyaddr(addr, afd->a_addrlen, afd->a_af); |
Mattias Falk | 149f7df | 2011-02-15 08:45:26 +0100 | [diff] [blame] | 390 | #endif |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 391 | |
| 392 | if (hp) { |
| 393 | #if 0 |
| 394 | /* |
| 395 | * commented out, since "for local host" is not |
| 396 | * implemented here - see RFC2553 p30 |
| 397 | */ |
| 398 | if (flags & NI_NOFQDN) { |
| 399 | char *p; |
| 400 | p = strchr(hp->h_name, '.'); |
| 401 | if (p) |
| 402 | *p = '\0'; |
| 403 | } |
| 404 | #endif |
| 405 | if (strlen(hp->h_name) + 1 > (size_t)hostlen) { |
| 406 | return EAI_MEMORY; |
| 407 | } |
| 408 | strlcpy(host, hp->h_name, hostlen); |
| 409 | } else { |
| 410 | if (flags & NI_NAMEREQD) |
| 411 | return EAI_NONAME; |
| 412 | switch(afd->a_af) { |
| 413 | #ifdef INET6 |
| 414 | case AF_INET6: |
| 415 | { |
| 416 | int error; |
| 417 | |
| 418 | if ((error = ip6_parsenumeric(sa, addr, host, |
| 419 | hostlen, |
| 420 | flags)) != 0) |
| 421 | return(error); |
| 422 | break; |
| 423 | } |
| 424 | #endif |
| 425 | default: |
| 426 | if (inet_ntop(afd->a_af, addr, host, |
| 427 | hostlen) == NULL) |
| 428 | return EAI_SYSTEM; |
| 429 | break; |
| 430 | } |
| 431 | } |
| 432 | } |
| 433 | return(0); |
| 434 | } |
| 435 | |
| 436 | #ifdef INET6 |
| 437 | static int |
| 438 | ip6_parsenumeric(sa, addr, host, hostlen, flags) |
| 439 | const struct sockaddr *sa; |
| 440 | const char *addr; |
| 441 | char *host; |
| 442 | socklen_t hostlen; |
| 443 | int flags; |
| 444 | { |
| 445 | size_t numaddrlen; |
| 446 | char numaddr[512]; |
| 447 | |
| 448 | assert(sa != NULL); |
| 449 | assert(addr != NULL); |
| 450 | assert(host != NULL); |
| 451 | |
David 'Digit' Turner | 50ace4f | 2010-06-16 16:36:41 -0700 | [diff] [blame] | 452 | if (hostlen < 0) |
| 453 | return EAI_OVERFLOW; |
| 454 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 455 | if (inet_ntop(AF_INET6, addr, numaddr, sizeof(numaddr)) == NULL) |
| 456 | return EAI_SYSTEM; |
| 457 | |
| 458 | numaddrlen = strlen(numaddr); |
David 'Digit' Turner | 50ace4f | 2010-06-16 16:36:41 -0700 | [diff] [blame] | 459 | 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] | 460 | return EAI_OVERFLOW; |
| 461 | strlcpy(host, numaddr, hostlen); |
| 462 | |
| 463 | if (((const struct sockaddr_in6 *)(const void *)sa)->sin6_scope_id) { |
| 464 | char zonebuf[MAXHOSTNAMELEN]; |
| 465 | int zonelen; |
| 466 | |
| 467 | zonelen = ip6_sa2str( |
| 468 | (const struct sockaddr_in6 *)(const void *)sa, |
| 469 | zonebuf, sizeof(zonebuf), flags); |
| 470 | if (zonelen < 0) |
| 471 | return EAI_OVERFLOW; |
David 'Digit' Turner | 50ace4f | 2010-06-16 16:36:41 -0700 | [diff] [blame] | 472 | 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] | 473 | return EAI_OVERFLOW; |
| 474 | /* construct <numeric-addr><delim><zoneid> */ |
| 475 | memcpy(host + numaddrlen + 1, zonebuf, |
| 476 | (size_t)zonelen); |
| 477 | host[numaddrlen] = SCOPE_DELIMITER; |
| 478 | host[numaddrlen + 1 + zonelen] = '\0'; |
| 479 | } |
| 480 | |
| 481 | return 0; |
| 482 | } |
| 483 | |
| 484 | /* ARGSUSED */ |
| 485 | static int |
| 486 | ip6_sa2str(sa6, buf, bufsiz, flags) |
| 487 | const struct sockaddr_in6 *sa6; |
| 488 | char *buf; |
| 489 | size_t bufsiz; |
| 490 | int flags; |
| 491 | { |
| 492 | unsigned int ifindex; |
| 493 | const struct in6_addr *a6; |
| 494 | int n; |
| 495 | |
| 496 | assert(sa6 != NULL); |
| 497 | assert(buf != NULL); |
| 498 | |
| 499 | ifindex = (unsigned int)sa6->sin6_scope_id; |
| 500 | a6 = &sa6->sin6_addr; |
| 501 | |
| 502 | #ifdef NI_NUMERICSCOPE |
| 503 | if ((flags & NI_NUMERICSCOPE) != 0) { |
| 504 | n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id); |
| 505 | if (n < 0 || n >= bufsiz) |
| 506 | return -1; |
| 507 | else |
| 508 | return n; |
| 509 | } |
| 510 | #endif |
| 511 | |
| 512 | /* if_indextoname() does not take buffer size. not a good api... */ |
| 513 | if ((IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) && |
| 514 | bufsiz >= IF_NAMESIZE) { |
| 515 | char *p = if_indextoname(ifindex, buf); |
| 516 | if (p) { |
| 517 | return(strlen(p)); |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | /* last resort */ |
| 522 | n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id); |
| 523 | if (n < 0 || (size_t) n >= bufsiz) |
| 524 | return -1; |
| 525 | else |
| 526 | return n; |
| 527 | } |
| 528 | #endif /* INET6 */ |
| 529 | |
| 530 | |
| 531 | /* |
| 532 | * getnameinfo_link(): |
| 533 | * Format a link-layer address into a printable format, paying attention to |
| 534 | * the interface type. |
| 535 | */ |
| 536 | /* ARGSUSED */ |
| 537 | static int |
| 538 | getnameinfo_link(const struct sockaddr *sa, socklen_t salen, |
| 539 | char *host, socklen_t hostlen, char *serv, socklen_t servlen, |
| 540 | int flags) |
| 541 | { |
| 542 | const struct sockaddr_dl *sdl = |
| 543 | (const struct sockaddr_dl *)(const void *)sa; |
| 544 | const struct ieee1394_hwaddr *iha; |
| 545 | int n; |
| 546 | |
| 547 | if (serv != NULL && servlen > 0) |
| 548 | *serv = '\0'; |
| 549 | |
| 550 | if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 && sdl->sdl_slen == 0) { |
| 551 | n = snprintf(host, hostlen, "link#%u", sdl->sdl_index); |
| 552 | if (n < 0 || (socklen_t) n > hostlen) { |
| 553 | *host = '\0'; |
| 554 | return EAI_MEMORY; |
| 555 | } |
| 556 | return 0; |
| 557 | } |
| 558 | |
| 559 | switch (sdl->sdl_type) { |
| 560 | #ifdef IFT_ECONET |
| 561 | case IFT_ECONET: |
| 562 | if (sdl->sdl_alen < 2) |
| 563 | return EAI_FAMILY; |
| 564 | if (CLLADDR(sdl)[1] == 0) |
| 565 | n = snprintf(host, hostlen, "%u", CLLADDR(sdl)[0]); |
| 566 | else |
| 567 | n = snprintf(host, hostlen, "%u.%u", |
| 568 | CLLADDR(sdl)[1], CLLADDR(sdl)[0]); |
| 569 | if (n < 0 || (socklen_t) n >= hostlen) { |
| 570 | *host = '\0'; |
| 571 | return EAI_MEMORY; |
| 572 | } else |
| 573 | return 0; |
| 574 | #endif |
| 575 | case IFT_IEEE1394: |
| 576 | if (sdl->sdl_alen < sizeof(iha->iha_uid)) |
| 577 | return EAI_FAMILY; |
| 578 | iha = |
| 579 | (const struct ieee1394_hwaddr *)(const void *)CLLADDR(sdl); |
| 580 | return hexname(iha->iha_uid, sizeof(iha->iha_uid), |
| 581 | host, hostlen); |
| 582 | /* |
| 583 | * The following have zero-length addresses. |
| 584 | * IFT_ATM (net/if_atmsubr.c) |
| 585 | * IFT_FAITH (net/if_faith.c) |
| 586 | * IFT_GIF (net/if_gif.c) |
| 587 | * IFT_LOOP (net/if_loop.c) |
| 588 | * IFT_PPP (net/if_ppp.c, net/if_spppsubr.c) |
| 589 | * IFT_SLIP (net/if_sl.c, net/if_strip.c) |
| 590 | * IFT_STF (net/if_stf.c) |
| 591 | * IFT_L2VLAN (net/if_vlan.c) |
| 592 | * IFT_PROPVIRTUAL (net/if_bridge.h> |
| 593 | */ |
| 594 | /* |
| 595 | * The following use IPv4 addresses as link-layer addresses: |
| 596 | * IFT_OTHER (net/if_gre.c) |
| 597 | */ |
| 598 | case IFT_ARCNET: /* default below is believed correct for all these. */ |
| 599 | case IFT_ETHER: |
| 600 | case IFT_FDDI: |
| 601 | case IFT_HIPPI: |
| 602 | case IFT_ISO88025: |
| 603 | default: |
| 604 | return hexname((const u_int8_t *)CLLADDR(sdl), |
| 605 | (size_t)sdl->sdl_alen, host, hostlen); |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | static int |
| 610 | hexname(cp, len, host, hostlen) |
| 611 | const u_int8_t *cp; |
| 612 | char *host; |
| 613 | size_t len; |
| 614 | socklen_t hostlen; |
| 615 | { |
| 616 | int n; |
| 617 | size_t i; |
| 618 | char *outp = host; |
| 619 | |
| 620 | *outp = '\0'; |
| 621 | for (i = 0; i < len; i++) { |
| 622 | n = snprintf(outp, hostlen, "%s%02x", |
| 623 | i ? ":" : "", cp[i]); |
| 624 | if (n < 0 || (socklen_t) n >= hostlen) { |
| 625 | *host = '\0'; |
| 626 | return EAI_MEMORY; |
| 627 | } |
| 628 | outp += n; |
| 629 | hostlen -= n; |
| 630 | } |
| 631 | return 0; |
| 632 | } |