11dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* $NetBSD: getnameinfo.c,v 1.43 2006/02/17 15:58:26 ginsbach Exp $ */ 21dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* $KAME: getnameinfo.c,v 1.45 2000/09/25 22:43:56 itojun Exp $ */ 31dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 41dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* 51dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Copyright (c) 2000 Ben Harris. 61dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 71dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * All rights reserved. 81dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 91dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Redistribution and use in source and binary forms, with or without 101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * modification, are permitted provided that the following conditions 111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * are met: 121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 1. Redistributions of source code must retain the above copyright 131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * notice, this list of conditions and the following disclaimer. 141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 2. Redistributions in binary form must reproduce the above copyright 151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * notice, this list of conditions and the following disclaimer in the 161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * documentation and/or other materials provided with the distribution. 171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 3. Neither the name of the project nor the names of its contributors 181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * may be used to endorse or promote products derived from this software 191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * without specific prior written permission. 201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * SUCH DAMAGE. 321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */ 331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* 351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Issues to be discussed: 361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * - Thread safe-ness must be checked 371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * - RFC2553 says that we should raise error on short buffer. X/Open says 381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * we need to truncate the result. We obey RFC2553 (and X/Open should be 391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * modified). ipngwg rough consensus seems to follow RFC2553. 401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * - What is "local" in NI_FQDN? 411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * - NI_NAMEREQD and NI_NUMERICHOST conflict with each other. 421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * - (KAME extension) always attach textual scopeid (fe80::1%lo0), if 431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * sin6_scope_id is filled - standardization status? 441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * XXX breaks backward compat for code that expects no scopeid. 451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * beware on merge. 461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */ 471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include <sys/cdefs.h> 491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if defined(LIBC_SCCS) && !defined(lint) 501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project__RCSID("$NetBSD: getnameinfo.c,v 1.43 2006/02/17 15:58:26 ginsbach Exp $"); 511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif /* LIBC_SCCS and not lint */ 521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include <sys/types.h> 541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include <sys/socket.h> 551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include <net/if.h> 561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include <net/if_dl.h> 571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include <net/if_ieee1394.h> 581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include <net/if_types.h> 591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include <netinet/in.h> 601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include <arpa/inet.h> 611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include "arpa_nameser.h" 621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include <assert.h> 631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include <limits.h> 641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include <netdb.h> 651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#ifdef ANDROID_CHANGES 661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include "resolv_private.h" 671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#else 681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include <resolv.h> 691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif 701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include <stddef.h> 711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include <string.h> 721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic const struct afd { 741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project int a_af; 751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project socklen_t a_addrlen; 761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project socklen_t a_socklen; 771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project int a_off; 781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} afdl [] = { 791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#ifdef INET6 801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project {PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6), 811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project offsetof(struct sockaddr_in6, sin6_addr)}, 821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif 831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project {PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in), 841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project offsetof(struct sockaddr_in, sin_addr)}, 851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project {0, 0, 0, 0}, 861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}; 871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstruct sockinet { 891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project u_char si_len; 901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project u_char si_family; 911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project u_short si_port; 921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}; 931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic int getnameinfo_inet __P((const struct sockaddr *, socklen_t, char *, 951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project socklen_t, char *, socklen_t, int)); 961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#ifdef INET6 971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic int ip6_parsenumeric __P((const struct sockaddr *, const char *, char *, 981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project socklen_t, int)); 991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic int ip6_sa2str __P((const struct sockaddr_in6 *, char *, size_t, 1001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project int)); 1011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif 1021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic int getnameinfo_link __P((const struct sockaddr *, socklen_t, char *, 1031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project socklen_t, char *, socklen_t, int)); 1041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic int hexname __P((const u_int8_t *, size_t, char *, socklen_t)); 1051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* 1071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Top-level getnameinfo() code. Look at the address family, and pick an 1081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * appropriate function to call. 1091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */ 1101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectint getnameinfo(const struct sockaddr* sa, socklen_t salen, char* host, size_t hostlen, char* serv, size_t servlen, int flags) 1111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{ 1121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project switch (sa->sa_family) { 1131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case AF_INET: 1141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case AF_INET6: 1151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return getnameinfo_inet(sa, salen, host, hostlen, 1161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project serv, servlen, flags); 1171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if 0 1181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case AF_LINK: 1191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return getnameinfo_link(sa, salen, host, hostlen, 1201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project serv, servlen, flags); 1211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif 1221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project default: 1231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return EAI_FAMILY; 1241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 1251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 1261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* 1291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * getnameinfo_inet(): 1301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Format an IPv4 or IPv6 sockaddr into a printable string. 1311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */ 1321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic int 1331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectgetnameinfo_inet(sa, salen, host, hostlen, serv, servlen, flags) 1341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project const struct sockaddr *sa; 1351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project socklen_t salen; 1361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project char *host; 1371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project socklen_t hostlen; 1381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project char *serv; 1391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project socklen_t servlen; 1401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project int flags; 1411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{ 1421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project const struct afd *afd; 1431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project struct servent *sp; 1441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project struct hostent *hp; 1451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project u_short port; 1461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project int family, i; 1471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project const char *addr; 1481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project u_int32_t v4a; 1491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project char numserv[512]; 1501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project char numaddr[512]; 1511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* sa is checked below */ 1531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* host may be NULL */ 1541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* serv may be NULL */ 1551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (sa == NULL) 1571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return EAI_FAIL; 1581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#ifdef BSD4_4 1601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (sa->sa_len != salen) 1611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return EAI_FAIL; 1621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif 1631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project family = sa->sa_family; 1651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project for (i = 0; afdl[i].a_af; i++) 1661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (afdl[i].a_af == family) { 1671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project afd = &afdl[i]; 1681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project goto found; 1691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 1701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return EAI_FAMILY; 1711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project found: 1731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (salen != afd->a_socklen) 1741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return EAI_FAIL; 1751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* network byte order */ 1771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project port = ((const struct sockinet *)(const void *)sa)->si_port; 1781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project addr = (const char *)(const void *)sa + afd->a_off; 1791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (serv == NULL || servlen == 0) { 1811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* 1821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * do nothing in this case. 1831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * in case you are wondering if "&&" is more correct than 1841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * "||" here: rfc2553bis-03 says that serv == NULL OR 1851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * servlen == 0 means that the caller does not want the result. 1861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */ 1871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } else { 1881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (flags & NI_NUMERICSERV) 1891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project sp = NULL; 1901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project else { 1911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project sp = getservbyport(port, 1921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project (flags & NI_DGRAM) ? "udp" : "tcp"); 1931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 1941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (sp) { 1951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (strlen(sp->s_name) + 1 > (size_t)servlen) 1961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return EAI_MEMORY; 1971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project strlcpy(serv, sp->s_name, servlen); 1981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } else { 1991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project snprintf(numserv, sizeof(numserv), "%u", ntohs(port)); 2001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (strlen(numserv) + 1 > (size_t)servlen) 2011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return EAI_MEMORY; 2021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project strlcpy(serv, numserv, servlen); 2031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 2041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 2051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 2061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project switch (sa->sa_family) { 2071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case AF_INET: 2081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project v4a = (u_int32_t) 2091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project ntohl(((const struct sockaddr_in *) 2101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project (const void *)sa)->sin_addr.s_addr); 2111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a)) 2121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project flags |= NI_NUMERICHOST; 2131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project v4a >>= IN_CLASSA_NSHIFT; 2141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (v4a == 0) 2151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project flags |= NI_NUMERICHOST; 2161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 2171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#ifdef INET6 2181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case AF_INET6: 2191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project { 2201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project const struct sockaddr_in6 *sin6; 2211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project sin6 = (const struct sockaddr_in6 *)(const void *)sa; 2221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project switch (sin6->sin6_addr.s6_addr[0]) { 2231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case 0x00: 2241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) 2251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project ; 2261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project else if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr)) 2271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project ; 2281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project else 2291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project flags |= NI_NUMERICHOST; 2301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 2311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project default: 2321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) { 2331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project flags |= NI_NUMERICHOST; 2341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 2351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) 2361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project flags |= NI_NUMERICHOST; 2371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 2381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 2391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 2401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 2411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif 2421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 2431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (host == NULL || hostlen == 0) { 2441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* 2451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * do nothing in this case. 2461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * in case you are wondering if "&&" is more correct than 2471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * "||" here: rfc2553bis-03 says that host == NULL or 2481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * hostlen == 0 means that the caller does not want the result. 2491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */ 2501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } else if (flags & NI_NUMERICHOST) { 2511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project size_t numaddrlen; 2521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 2531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* NUMERICHOST and NAMEREQD conflicts with each other */ 2541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (flags & NI_NAMEREQD) 2551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return EAI_NONAME; 2561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 2571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project switch(afd->a_af) { 2581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#ifdef INET6 2591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case AF_INET6: 2601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project { 2611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project int error; 2621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 2631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if ((error = ip6_parsenumeric(sa, addr, host, 2641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project hostlen, flags)) != 0) 2651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return(error); 2661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 2671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 2681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif 2691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project default: 2701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr)) 2711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project == NULL) 2721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return EAI_SYSTEM; 2731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project numaddrlen = strlen(numaddr); 2741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (numaddrlen + 1 > (size_t)hostlen) /* don't forget terminator */ 2751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return EAI_MEMORY; 2761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project strlcpy(host, numaddr, hostlen); 2771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 2781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 2791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } else { 2801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project hp = gethostbyaddr(addr, afd->a_addrlen, afd->a_af); 2811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 2821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (hp) { 2831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if 0 2841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* 2851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * commented out, since "for local host" is not 2861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * implemented here - see RFC2553 p30 2871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */ 2881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (flags & NI_NOFQDN) { 2891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project char *p; 2901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project p = strchr(hp->h_name, '.'); 2911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (p) 2921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *p = '\0'; 2931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 2941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif 2951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (strlen(hp->h_name) + 1 > (size_t)hostlen) { 2961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return EAI_MEMORY; 2971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 2981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project strlcpy(host, hp->h_name, hostlen); 2991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } else { 3001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (flags & NI_NAMEREQD) 3011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return EAI_NONAME; 3021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project switch(afd->a_af) { 3031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#ifdef INET6 3041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case AF_INET6: 3051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project { 3061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project int error; 3071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 3081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if ((error = ip6_parsenumeric(sa, addr, host, 3091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project hostlen, 3101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project flags)) != 0) 3111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return(error); 3121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 3131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 3141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif 3151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project default: 3161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (inet_ntop(afd->a_af, addr, host, 3171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project hostlen) == NULL) 3181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return EAI_SYSTEM; 3191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 3201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 3211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 3221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 3231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return(0); 3241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 3251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 3261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#ifdef INET6 3271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic int 3281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectip6_parsenumeric(sa, addr, host, hostlen, flags) 3291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project const struct sockaddr *sa; 3301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project const char *addr; 3311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project char *host; 3321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project socklen_t hostlen; 3331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project int flags; 3341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{ 3351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project size_t numaddrlen; 3361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project char numaddr[512]; 3371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 3381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project assert(sa != NULL); 3391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project assert(addr != NULL); 3401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project assert(host != NULL); 3411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 34250ace4fec5e8cb5afcbc656a4556fa528adfd760David 'Digit' Turner if (hostlen < 0) 34350ace4fec5e8cb5afcbc656a4556fa528adfd760David 'Digit' Turner return EAI_OVERFLOW; 34450ace4fec5e8cb5afcbc656a4556fa528adfd760David 'Digit' Turner 3451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (inet_ntop(AF_INET6, addr, numaddr, sizeof(numaddr)) == NULL) 3461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return EAI_SYSTEM; 3471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 3481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project numaddrlen = strlen(numaddr); 34950ace4fec5e8cb5afcbc656a4556fa528adfd760David 'Digit' Turner if (numaddrlen + 1 > (size_t)hostlen) /* don't forget terminator */ 3501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return EAI_OVERFLOW; 3511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project strlcpy(host, numaddr, hostlen); 3521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 3531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (((const struct sockaddr_in6 *)(const void *)sa)->sin6_scope_id) { 3541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project char zonebuf[MAXHOSTNAMELEN]; 3551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project int zonelen; 3561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 3571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project zonelen = ip6_sa2str( 3581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project (const struct sockaddr_in6 *)(const void *)sa, 3591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project zonebuf, sizeof(zonebuf), flags); 3601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (zonelen < 0) 3611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return EAI_OVERFLOW; 36250ace4fec5e8cb5afcbc656a4556fa528adfd760David 'Digit' Turner if ((size_t) zonelen + 1 + numaddrlen + 1 > (size_t)hostlen) 3631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return EAI_OVERFLOW; 3641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* construct <numeric-addr><delim><zoneid> */ 3651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project memcpy(host + numaddrlen + 1, zonebuf, 3661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project (size_t)zonelen); 3671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project host[numaddrlen] = SCOPE_DELIMITER; 3681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project host[numaddrlen + 1 + zonelen] = '\0'; 3691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 3701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 3711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return 0; 3721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 3731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 3741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* ARGSUSED */ 3751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic int 3761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectip6_sa2str(sa6, buf, bufsiz, flags) 3771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project const struct sockaddr_in6 *sa6; 3781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project char *buf; 3791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project size_t bufsiz; 3801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project int flags; 3811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{ 3821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project unsigned int ifindex; 3831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project const struct in6_addr *a6; 3841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project int n; 3851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 3861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project assert(sa6 != NULL); 3871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project assert(buf != NULL); 3881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 3891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project ifindex = (unsigned int)sa6->sin6_scope_id; 3901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project a6 = &sa6->sin6_addr; 3911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 3921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#ifdef NI_NUMERICSCOPE 3931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if ((flags & NI_NUMERICSCOPE) != 0) { 3941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id); 3951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (n < 0 || n >= bufsiz) 3961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return -1; 3971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project else 3981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return n; 3991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 4001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif 4011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 4021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* if_indextoname() does not take buffer size. not a good api... */ 4031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if ((IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) && 4041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project bufsiz >= IF_NAMESIZE) { 4051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project char *p = if_indextoname(ifindex, buf); 4061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (p) { 4071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return(strlen(p)); 4081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 4091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 4101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 4111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* last resort */ 4121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id); 4131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (n < 0 || (size_t) n >= bufsiz) 4141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return -1; 4151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project else 4161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return n; 4171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 4181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif /* INET6 */ 4191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 4201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 4211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* 4221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * getnameinfo_link(): 4231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Format a link-layer address into a printable format, paying attention to 4241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * the interface type. 4251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */ 4261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* ARGSUSED */ 4271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic int 4281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectgetnameinfo_link(const struct sockaddr *sa, socklen_t salen, 4291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project char *host, socklen_t hostlen, char *serv, socklen_t servlen, 4301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project int flags) 4311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{ 4321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project const struct sockaddr_dl *sdl = 4331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project (const struct sockaddr_dl *)(const void *)sa; 4341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project const struct ieee1394_hwaddr *iha; 4351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project int n; 4361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 4371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (serv != NULL && servlen > 0) 4381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *serv = '\0'; 4391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 4401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 && sdl->sdl_slen == 0) { 4411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project n = snprintf(host, hostlen, "link#%u", sdl->sdl_index); 4421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (n < 0 || (socklen_t) n > hostlen) { 4431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *host = '\0'; 4441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return EAI_MEMORY; 4451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 4461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return 0; 4471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 4481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 4491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project switch (sdl->sdl_type) { 4501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#ifdef IFT_ECONET 4511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case IFT_ECONET: 4521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (sdl->sdl_alen < 2) 4531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return EAI_FAMILY; 4541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (CLLADDR(sdl)[1] == 0) 4551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project n = snprintf(host, hostlen, "%u", CLLADDR(sdl)[0]); 4561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project else 4571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project n = snprintf(host, hostlen, "%u.%u", 4581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project CLLADDR(sdl)[1], CLLADDR(sdl)[0]); 4591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (n < 0 || (socklen_t) n >= hostlen) { 4601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *host = '\0'; 4611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return EAI_MEMORY; 4621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } else 4631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return 0; 4641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif 4651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case IFT_IEEE1394: 4661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (sdl->sdl_alen < sizeof(iha->iha_uid)) 4671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return EAI_FAMILY; 4681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project iha = 4691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project (const struct ieee1394_hwaddr *)(const void *)CLLADDR(sdl); 4701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return hexname(iha->iha_uid, sizeof(iha->iha_uid), 4711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project host, hostlen); 4721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* 4731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * The following have zero-length addresses. 4741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * IFT_ATM (net/if_atmsubr.c) 4751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * IFT_FAITH (net/if_faith.c) 4761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * IFT_GIF (net/if_gif.c) 4771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * IFT_LOOP (net/if_loop.c) 4781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * IFT_PPP (net/if_ppp.c, net/if_spppsubr.c) 4791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * IFT_SLIP (net/if_sl.c, net/if_strip.c) 4801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * IFT_STF (net/if_stf.c) 4811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * IFT_L2VLAN (net/if_vlan.c) 4821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * IFT_PROPVIRTUAL (net/if_bridge.h> 4831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */ 4841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* 4851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * The following use IPv4 addresses as link-layer addresses: 4861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * IFT_OTHER (net/if_gre.c) 4871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */ 4881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case IFT_ARCNET: /* default below is believed correct for all these. */ 4891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case IFT_ETHER: 4901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case IFT_FDDI: 4911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case IFT_HIPPI: 4921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case IFT_ISO88025: 4931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project default: 4941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return hexname((const u_int8_t *)CLLADDR(sdl), 4951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project (size_t)sdl->sdl_alen, host, hostlen); 4961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 4971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 4981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 4991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic int 5001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projecthexname(cp, len, host, hostlen) 5011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project const u_int8_t *cp; 5021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project char *host; 5031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project size_t len; 5041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project socklen_t hostlen; 5051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{ 5061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project int n; 5071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project size_t i; 5081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project char *outp = host; 5091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 5101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *outp = '\0'; 5111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project for (i = 0; i < len; i++) { 5121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project n = snprintf(outp, hostlen, "%s%02x", 5131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project i ? ":" : "", cp[i]); 5141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (n < 0 || (socklen_t) n >= hostlen) { 5151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *host = '\0'; 5161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return EAI_MEMORY; 5171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 5181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project outp += n; 5191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project hostlen -= n; 5201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 5211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return 0; 5221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 523