17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * Issues to be discussed:
37c478bd9Sstevel@tonic-gate  * - Thread safe-ness must be checked
47c478bd9Sstevel@tonic-gate  */
57c478bd9Sstevel@tonic-gate 
69525b14bSRao Shoaib #if ( defined(__linux__) || defined(__linux) || defined(LINUX) )
79525b14bSRao Shoaib #ifndef IF_NAMESIZE
89525b14bSRao Shoaib # ifdef IFNAMSIZ
99525b14bSRao Shoaib #  define IF_NAMESIZE  IFNAMSIZ
109525b14bSRao Shoaib # else
119525b14bSRao Shoaib #  define IF_NAMESIZE 16
129525b14bSRao Shoaib # endif
139525b14bSRao Shoaib #endif
149525b14bSRao Shoaib #endif
159525b14bSRao Shoaib 
167c478bd9Sstevel@tonic-gate /*
177c478bd9Sstevel@tonic-gate  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
187c478bd9Sstevel@tonic-gate  * All rights reserved.
19*55fea89dSDan Cross  *
207c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
217c478bd9Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
227c478bd9Sstevel@tonic-gate  * are met:
237c478bd9Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
247c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
257c478bd9Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
267c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
277c478bd9Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
287c478bd9Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
297c478bd9Sstevel@tonic-gate  *    must display the following acknowledgement:
307c478bd9Sstevel@tonic-gate  *    This product includes software developed by WIDE Project and
317c478bd9Sstevel@tonic-gate  *    its contributors.
327c478bd9Sstevel@tonic-gate  * 4. Neither the name of the project nor the names of its contributors
337c478bd9Sstevel@tonic-gate  *    may be used to endorse or promote products derived from this software
347c478bd9Sstevel@tonic-gate  *    without specific prior written permission.
35*55fea89dSDan Cross  *
367c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
377c478bd9Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
387c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
397c478bd9Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
407c478bd9Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
417c478bd9Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
427c478bd9Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
437c478bd9Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
447c478bd9Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
457c478bd9Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
467c478bd9Sstevel@tonic-gate  * SUCH DAMAGE.
477c478bd9Sstevel@tonic-gate  */
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate #include <port_before.h>
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate #include <sys/types.h>
527c478bd9Sstevel@tonic-gate #include <sys/socket.h>
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate #include <netinet/in.h>
557c478bd9Sstevel@tonic-gate #include <arpa/nameser.h>
567c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
577c478bd9Sstevel@tonic-gate #include <net/if.h>
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate #include <netdb.h>
607c478bd9Sstevel@tonic-gate #include <resolv.h>
617c478bd9Sstevel@tonic-gate #include <string.h>
627c478bd9Sstevel@tonic-gate #include <stddef.h>
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate #include <port_after.h>
657c478bd9Sstevel@tonic-gate 
669525b14bSRao Shoaib /*%
677c478bd9Sstevel@tonic-gate  * Note that a_off will be dynamically adjusted so that to be consistent
687c478bd9Sstevel@tonic-gate  * with the definition of sockaddr_in{,6}.
697c478bd9Sstevel@tonic-gate  * The value presented below is just a guess.
707c478bd9Sstevel@tonic-gate  */
717c478bd9Sstevel@tonic-gate static struct afd {
727c478bd9Sstevel@tonic-gate 	int a_af;
737c478bd9Sstevel@tonic-gate 	int a_addrlen;
747c478bd9Sstevel@tonic-gate 	size_t a_socklen;
757c478bd9Sstevel@tonic-gate 	int a_off;
767c478bd9Sstevel@tonic-gate } afdl [] = {
777c478bd9Sstevel@tonic-gate 	/* first entry is linked last... */
787c478bd9Sstevel@tonic-gate 	{PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
797c478bd9Sstevel@tonic-gate 	 offsetof(struct sockaddr_in, sin_addr)},
807c478bd9Sstevel@tonic-gate 	{PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
817c478bd9Sstevel@tonic-gate 	 offsetof(struct sockaddr_in6, sin6_addr)},
827c478bd9Sstevel@tonic-gate 	{0, 0, 0, 0},
837c478bd9Sstevel@tonic-gate };
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate struct sockinet {
867c478bd9Sstevel@tonic-gate #ifdef HAVE_SA_LEN
877c478bd9Sstevel@tonic-gate 	u_char	si_len;
887c478bd9Sstevel@tonic-gate #endif
897c478bd9Sstevel@tonic-gate 	u_char	si_family;
907c478bd9Sstevel@tonic-gate 	u_short	si_port;
917c478bd9Sstevel@tonic-gate };
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate static int ip6_parsenumeric __P((const struct sockaddr *, const char *, char *,
947c478bd9Sstevel@tonic-gate 				 size_t, int));
957c478bd9Sstevel@tonic-gate #ifdef HAVE_SIN6_SCOPE_ID
967c478bd9Sstevel@tonic-gate static int ip6_sa2str __P((const struct sockaddr_in6 *, char *, size_t, int));
977c478bd9Sstevel@tonic-gate #endif
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate int
getnameinfo(sa,salen,host,hostlen,serv,servlen,flags)1007c478bd9Sstevel@tonic-gate getnameinfo(sa, salen, host, hostlen, serv, servlen, flags)
1017c478bd9Sstevel@tonic-gate 	const struct sockaddr *sa;
1027c478bd9Sstevel@tonic-gate 	size_t salen;
1037c478bd9Sstevel@tonic-gate 	char *host;
1047c478bd9Sstevel@tonic-gate 	size_t hostlen;
1057c478bd9Sstevel@tonic-gate 	char *serv;
1067c478bd9Sstevel@tonic-gate 	size_t servlen;
1077c478bd9Sstevel@tonic-gate 	int flags;
1087c478bd9Sstevel@tonic-gate {
1097c478bd9Sstevel@tonic-gate 	struct afd *afd;
1107c478bd9Sstevel@tonic-gate 	struct servent *sp;
1117c478bd9Sstevel@tonic-gate 	struct hostent *hp;
1127c478bd9Sstevel@tonic-gate 	u_short port;
1137c478bd9Sstevel@tonic-gate #ifdef HAVE_SA_LEN
1147c478bd9Sstevel@tonic-gate 	size_t len;
1157c478bd9Sstevel@tonic-gate #endif
1167c478bd9Sstevel@tonic-gate 	int family, i;
1177c478bd9Sstevel@tonic-gate 	const char *addr;
1187c478bd9Sstevel@tonic-gate 	char *p;
1197c478bd9Sstevel@tonic-gate 	char numserv[512];
1207c478bd9Sstevel@tonic-gate 	char numaddr[512];
1217c478bd9Sstevel@tonic-gate 	const struct sockaddr_in6 *sin6;
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 	if (sa == NULL)
1247c478bd9Sstevel@tonic-gate 		return EAI_FAIL;
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate #ifdef HAVE_SA_LEN
1277c478bd9Sstevel@tonic-gate 	len = sa->sa_len;
1287c478bd9Sstevel@tonic-gate 	if (len != salen) return EAI_FAIL;
1297c478bd9Sstevel@tonic-gate #endif
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	family = sa->sa_family;
1327c478bd9Sstevel@tonic-gate 	for (i = 0; afdl[i].a_af; i++)
1337c478bd9Sstevel@tonic-gate 		if (afdl[i].a_af == family) {
1347c478bd9Sstevel@tonic-gate 			afd = &afdl[i];
1357c478bd9Sstevel@tonic-gate 			goto found;
1367c478bd9Sstevel@tonic-gate 		}
1377c478bd9Sstevel@tonic-gate 	return EAI_FAMILY;
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate  found:
1407c478bd9Sstevel@tonic-gate 	if (salen != afd->a_socklen) return EAI_FAIL;
1417c478bd9Sstevel@tonic-gate 
1429525b14bSRao Shoaib 	port = ((const struct sockinet *)sa)->si_port; /*%< network byte order */
1437c478bd9Sstevel@tonic-gate 	addr = (const char *)sa + afd->a_off;
1447c478bd9Sstevel@tonic-gate 
1459525b14bSRao Shoaib 	if (serv == NULL || servlen == 0U) {
1467c478bd9Sstevel@tonic-gate 		/*
1477c478bd9Sstevel@tonic-gate 		 * rfc2553bis says that serv == NULL or servlen == 0 means that
1487c478bd9Sstevel@tonic-gate 		 * the caller does not want the result.
1497c478bd9Sstevel@tonic-gate 		 */
1507c478bd9Sstevel@tonic-gate 	} else if (flags & NI_NUMERICSERV) {
1517c478bd9Sstevel@tonic-gate 		sprintf(numserv, "%d", ntohs(port));
1527c478bd9Sstevel@tonic-gate 		if (strlen(numserv) > servlen)
1537c478bd9Sstevel@tonic-gate 			return EAI_MEMORY;
1547c478bd9Sstevel@tonic-gate 		strcpy(serv, numserv);
1557c478bd9Sstevel@tonic-gate 	} else {
1567c478bd9Sstevel@tonic-gate 		sp = getservbyport(port, (flags & NI_DGRAM) ? "udp" : "tcp");
1577c478bd9Sstevel@tonic-gate 		if (sp) {
1587c478bd9Sstevel@tonic-gate 			if (strlen(sp->s_name) + 1 > servlen)
1597c478bd9Sstevel@tonic-gate 				return EAI_MEMORY;
1607c478bd9Sstevel@tonic-gate 			strcpy(serv, sp->s_name);
1617c478bd9Sstevel@tonic-gate 		} else
1627c478bd9Sstevel@tonic-gate 			return EAI_NONAME;
1637c478bd9Sstevel@tonic-gate 	}
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	switch (sa->sa_family) {
1667c478bd9Sstevel@tonic-gate 	case AF_INET:
1679525b14bSRao Shoaib 		if (ntohl(*(const u_int32_t *)addr) >> IN_CLASSA_NSHIFT == 0)
168*55fea89dSDan Cross 			flags |= NI_NUMERICHOST;
1697c478bd9Sstevel@tonic-gate 		break;
1707c478bd9Sstevel@tonic-gate 	case AF_INET6:
1717c478bd9Sstevel@tonic-gate 		sin6 = (const struct sockaddr_in6 *)sa;
1727c478bd9Sstevel@tonic-gate 		switch (sin6->sin6_addr.s6_addr[0]) {
1737c478bd9Sstevel@tonic-gate 		case 0x00:
1747c478bd9Sstevel@tonic-gate 			if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
1757c478bd9Sstevel@tonic-gate 				;
1767c478bd9Sstevel@tonic-gate 			else if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
1777c478bd9Sstevel@tonic-gate 				;
1787c478bd9Sstevel@tonic-gate 			else
1797c478bd9Sstevel@tonic-gate 				flags |= NI_NUMERICHOST;
1807c478bd9Sstevel@tonic-gate 			break;
1817c478bd9Sstevel@tonic-gate 		default:
1827c478bd9Sstevel@tonic-gate 			if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
1837c478bd9Sstevel@tonic-gate 				flags |= NI_NUMERICHOST;
1847c478bd9Sstevel@tonic-gate 			else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
1857c478bd9Sstevel@tonic-gate 				flags |= NI_NUMERICHOST;
1867c478bd9Sstevel@tonic-gate 			break;
1877c478bd9Sstevel@tonic-gate 		}
1887c478bd9Sstevel@tonic-gate 		break;
1897c478bd9Sstevel@tonic-gate 	}
1909525b14bSRao Shoaib 	if (host == NULL || hostlen == 0U) {
1917c478bd9Sstevel@tonic-gate 		/*
1927c478bd9Sstevel@tonic-gate 		 * rfc2553bis says that host == NULL or hostlen == 0 means that
1937c478bd9Sstevel@tonic-gate 		 * the caller does not want the result.
1947c478bd9Sstevel@tonic-gate 		 */
1957c478bd9Sstevel@tonic-gate 	} else if (flags & NI_NUMERICHOST) {
1967c478bd9Sstevel@tonic-gate 		goto numeric;
1977c478bd9Sstevel@tonic-gate 	} else {
1987c478bd9Sstevel@tonic-gate 		hp = gethostbyaddr(addr, afd->a_addrlen, afd->a_af);
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 		if (hp) {
2017c478bd9Sstevel@tonic-gate 			if (flags & NI_NOFQDN) {
2027c478bd9Sstevel@tonic-gate 				p = strchr(hp->h_name, '.');
2037c478bd9Sstevel@tonic-gate 				if (p) *p = '\0';
2047c478bd9Sstevel@tonic-gate 			}
2057c478bd9Sstevel@tonic-gate 			if (strlen(hp->h_name) + 1 > hostlen)
2067c478bd9Sstevel@tonic-gate 				return EAI_MEMORY;
2077c478bd9Sstevel@tonic-gate 			strcpy(host, hp->h_name);
2087c478bd9Sstevel@tonic-gate 		} else {
2097c478bd9Sstevel@tonic-gate 			if (flags & NI_NAMEREQD)
2107c478bd9Sstevel@tonic-gate 				return EAI_NONAME;
2117c478bd9Sstevel@tonic-gate 		  numeric:
2127c478bd9Sstevel@tonic-gate 			switch(afd->a_af) {
2137c478bd9Sstevel@tonic-gate 			case AF_INET6:
2147c478bd9Sstevel@tonic-gate 			{
2157c478bd9Sstevel@tonic-gate 				int error;
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 				if ((error = ip6_parsenumeric(sa, addr, host,
2187c478bd9Sstevel@tonic-gate 							      hostlen,
2197c478bd9Sstevel@tonic-gate 							      flags)) != 0)
2207c478bd9Sstevel@tonic-gate 					return(error);
2217c478bd9Sstevel@tonic-gate 				break;
2227c478bd9Sstevel@tonic-gate 			}
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 			default:
2257c478bd9Sstevel@tonic-gate 				if (inet_ntop(afd->a_af, addr, numaddr,
2267c478bd9Sstevel@tonic-gate 					      sizeof(numaddr)) == NULL)
2277c478bd9Sstevel@tonic-gate 					return EAI_NONAME;
2287c478bd9Sstevel@tonic-gate 				if (strlen(numaddr) + 1 > hostlen)
2297c478bd9Sstevel@tonic-gate 					return EAI_MEMORY;
2307c478bd9Sstevel@tonic-gate 				strcpy(host, numaddr);
2317c478bd9Sstevel@tonic-gate 			}
2327c478bd9Sstevel@tonic-gate 		}
2337c478bd9Sstevel@tonic-gate 	}
2347c478bd9Sstevel@tonic-gate 	return(0);
2357c478bd9Sstevel@tonic-gate }
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate static int
ip6_parsenumeric(const struct sockaddr * sa,const char * addr,char * host,size_t hostlen,int flags)2387c478bd9Sstevel@tonic-gate ip6_parsenumeric(const struct sockaddr *sa, const char *addr, char *host,
2397c478bd9Sstevel@tonic-gate 		 size_t hostlen, int flags)
2407c478bd9Sstevel@tonic-gate {
2417c478bd9Sstevel@tonic-gate 	size_t numaddrlen;
2427c478bd9Sstevel@tonic-gate 	char numaddr[512];
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate #ifndef HAVE_SIN6_SCOPE_ID
2457c478bd9Sstevel@tonic-gate 	UNUSED(sa);
2467c478bd9Sstevel@tonic-gate 	UNUSED(flags);
2477c478bd9Sstevel@tonic-gate #endif
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 	if (inet_ntop(AF_INET6, addr, numaddr, sizeof(numaddr))
2507c478bd9Sstevel@tonic-gate 	    == NULL)
2517c478bd9Sstevel@tonic-gate 		return EAI_SYSTEM;
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 	numaddrlen = strlen(numaddr);
2549525b14bSRao Shoaib 	if (numaddrlen + 1 > hostlen) /*%< don't forget terminator */
2557c478bd9Sstevel@tonic-gate 		return EAI_MEMORY;
2567c478bd9Sstevel@tonic-gate 	strcpy(host, numaddr);
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate #ifdef HAVE_SIN6_SCOPE_ID
2597c478bd9Sstevel@tonic-gate 	if (((const struct sockaddr_in6 *)sa)->sin6_scope_id) {
2609525b14bSRao Shoaib 		char scopebuf[MAXHOSTNAMELEN]; /*%< XXX */
2617c478bd9Sstevel@tonic-gate 		int scopelen;
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 		/* ip6_sa2str never fails */
2647c478bd9Sstevel@tonic-gate 		scopelen = ip6_sa2str((const struct sockaddr_in6 *)sa,
2657c478bd9Sstevel@tonic-gate 				      scopebuf, sizeof(scopebuf), flags);
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 		if (scopelen + 1 + numaddrlen + 1 > hostlen)
2687c478bd9Sstevel@tonic-gate 			return EAI_MEMORY;
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 		/* construct <numeric-addr><delim><scopeid> */
2717c478bd9Sstevel@tonic-gate 		memcpy(host + numaddrlen + 1, scopebuf,
2727c478bd9Sstevel@tonic-gate 		       scopelen);
2737c478bd9Sstevel@tonic-gate 		host[numaddrlen] = SCOPE_DELIMITER;
2747c478bd9Sstevel@tonic-gate 		host[numaddrlen + 1 + scopelen] = '\0';
2757c478bd9Sstevel@tonic-gate 	}
2767c478bd9Sstevel@tonic-gate #endif
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 	return 0;
2797c478bd9Sstevel@tonic-gate }
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate #ifdef HAVE_SIN6_SCOPE_ID
2827c478bd9Sstevel@tonic-gate /* ARGSUSED */
2837c478bd9Sstevel@tonic-gate static int
ip6_sa2str(const struct sockaddr_in6 * sa6,char * buf,size_t bufsiz,int flags)2847c478bd9Sstevel@tonic-gate ip6_sa2str(const struct sockaddr_in6 *sa6, char *buf,
2857c478bd9Sstevel@tonic-gate 	   size_t bufsiz, int flags)
2867c478bd9Sstevel@tonic-gate {
2877c478bd9Sstevel@tonic-gate #ifdef USE_IFNAMELINKID
2887c478bd9Sstevel@tonic-gate 	unsigned int ifindex = (unsigned int)sa6->sin6_scope_id;
2897c478bd9Sstevel@tonic-gate 	const struct in6_addr *a6 = &sa6->sin6_addr;
2907c478bd9Sstevel@tonic-gate #endif
2917c478bd9Sstevel@tonic-gate 	char tmp[64];
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate #ifdef NI_NUMERICSCOPE
2947c478bd9Sstevel@tonic-gate 	if (flags & NI_NUMERICSCOPE) {
2957c478bd9Sstevel@tonic-gate 		sprintf(tmp, "%u", sa6->sin6_scope_id);
2969525b14bSRao Shoaib 		if (bufsiz != 0U) {
2977c478bd9Sstevel@tonic-gate 			strncpy(buf, tmp, bufsiz - 1);
2987c478bd9Sstevel@tonic-gate 			buf[bufsiz - 1] = '\0';
2997c478bd9Sstevel@tonic-gate 		}
3007c478bd9Sstevel@tonic-gate 		return(strlen(tmp));
3017c478bd9Sstevel@tonic-gate 	}
3027c478bd9Sstevel@tonic-gate #endif
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate #ifdef USE_IFNAMELINKID
3057c478bd9Sstevel@tonic-gate 	/*
3067c478bd9Sstevel@tonic-gate 	 * For a link-local address, convert the index to an interface
3077c478bd9Sstevel@tonic-gate 	 * name, assuming a one-to-one mapping between links and interfaces.
3087c478bd9Sstevel@tonic-gate 	 * Note, however, that this assumption is stronger than the
3097c478bd9Sstevel@tonic-gate 	 * specification of the scoped address architecture;  the
3107c478bd9Sstevel@tonic-gate 	 * specficication says that more than one interfaces can belong to
3117c478bd9Sstevel@tonic-gate 	 * a single link.
3127c478bd9Sstevel@tonic-gate 	 */
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 	/* if_indextoname() does not take buffer size.  not a good api... */
3157c478bd9Sstevel@tonic-gate 	if ((IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) &&
3167c478bd9Sstevel@tonic-gate 	    bufsiz >= IF_NAMESIZE) {
3177c478bd9Sstevel@tonic-gate 		char *p = if_indextoname(ifindex, buf);
3187c478bd9Sstevel@tonic-gate 		if (p) {
3197c478bd9Sstevel@tonic-gate 			return(strlen(p));
3207c478bd9Sstevel@tonic-gate 		}
3217c478bd9Sstevel@tonic-gate 	}
3227c478bd9Sstevel@tonic-gate #endif
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 	/* last resort */
3257c478bd9Sstevel@tonic-gate 	sprintf(tmp, "%u", sa6->sin6_scope_id);
3269525b14bSRao Shoaib 	if (bufsiz != 0U) {
3277c478bd9Sstevel@tonic-gate 		strncpy(buf, tmp, bufsiz - 1);
3287c478bd9Sstevel@tonic-gate 		buf[bufsiz - 1] = '\0';
3297c478bd9Sstevel@tonic-gate 	}
3307c478bd9Sstevel@tonic-gate 	return(strlen(tmp));
3317c478bd9Sstevel@tonic-gate }
3327c478bd9Sstevel@tonic-gate #endif
3339525b14bSRao Shoaib 
3349525b14bSRao Shoaib /*! \file */
335