xref: /illumos-gate/usr/src/uts/common/inet/ip/inet_ntop.c (revision 7e12ceb3)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
2259927d31SYuri Pankov 
237c478bd9Sstevel@tonic-gate /*
247c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
257c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
260b905b49SYuri Pankov  * Copyright 2017 Nexenta Systems, Inc.
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <sys/types.h>
307c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
317c478bd9Sstevel@tonic-gate #include <sys/systm.h>
327c478bd9Sstevel@tonic-gate #include <sys/socket.h>
337c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
347c478bd9Sstevel@tonic-gate #include <netinet/in.h>
357c478bd9Sstevel@tonic-gate #include <inet/led.h>
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate /*
387c478bd9Sstevel@tonic-gate  * v6 formats supported
397c478bd9Sstevel@tonic-gate  * General format xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx
407c478bd9Sstevel@tonic-gate  * The short hand notation :: is used for COMPAT addr
417c478bd9Sstevel@tonic-gate  * Other forms : fe80::xxxx:xxxx:xxxx:xxxx
427c478bd9Sstevel@tonic-gate  */
437c478bd9Sstevel@tonic-gate static void
convert2ascii(char * buf,const in6_addr_t * addr)447c478bd9Sstevel@tonic-gate convert2ascii(char *buf, const in6_addr_t *addr)
457c478bd9Sstevel@tonic-gate {
467c478bd9Sstevel@tonic-gate 	int		hexdigits;
477c478bd9Sstevel@tonic-gate 	int		head_zero = 0;
487c478bd9Sstevel@tonic-gate 	int		tail_zero = 0;
497c478bd9Sstevel@tonic-gate 	/* tempbuf must be big enough to hold ffff:\0 */
507c478bd9Sstevel@tonic-gate 	char		tempbuf[6];
517c478bd9Sstevel@tonic-gate 	char		*ptr;
527c478bd9Sstevel@tonic-gate 	uint16_t	*addr_component;
537c478bd9Sstevel@tonic-gate 	size_t		len;
547c478bd9Sstevel@tonic-gate 	boolean_t	first = B_FALSE;
557c478bd9Sstevel@tonic-gate 	boolean_t	med_zero = B_FALSE;
567c478bd9Sstevel@tonic-gate 	boolean_t	end_zero = B_FALSE;
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate 	addr_component = (uint16_t *)addr;
597c478bd9Sstevel@tonic-gate 	ptr = buf;
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate 	/* First count if trailing zeroes higher in number */
627c478bd9Sstevel@tonic-gate 	for (hexdigits = 0; hexdigits < 8; hexdigits++) {
637c478bd9Sstevel@tonic-gate 		if (*addr_component == 0) {
647c478bd9Sstevel@tonic-gate 			if (hexdigits < 4)
657c478bd9Sstevel@tonic-gate 				head_zero++;
667c478bd9Sstevel@tonic-gate 			else
677c478bd9Sstevel@tonic-gate 				tail_zero++;
687c478bd9Sstevel@tonic-gate 		}
697c478bd9Sstevel@tonic-gate 		addr_component++;
707c478bd9Sstevel@tonic-gate 	}
717c478bd9Sstevel@tonic-gate 	addr_component = (uint16_t *)addr;
727c478bd9Sstevel@tonic-gate 	if (tail_zero > head_zero && (head_zero + tail_zero) != 7)
737c478bd9Sstevel@tonic-gate 		end_zero = B_TRUE;
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate 	for (hexdigits = 0; hexdigits < 8; hexdigits++) {
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate 		/* if entry is a 0 */
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 		if (*addr_component == 0) {
807c478bd9Sstevel@tonic-gate 			if (!first && *(addr_component + 1) == 0) {
817c478bd9Sstevel@tonic-gate 				if (end_zero && (hexdigits < 4)) {
827c478bd9Sstevel@tonic-gate 					*ptr++ = '0';
837c478bd9Sstevel@tonic-gate 					*ptr++ = ':';
847c478bd9Sstevel@tonic-gate 				} else {
857c478bd9Sstevel@tonic-gate 					/*
867c478bd9Sstevel@tonic-gate 					 * address starts with 0s ..
877c478bd9Sstevel@tonic-gate 					 * stick in leading ':' of pair
887c478bd9Sstevel@tonic-gate 					 */
897c478bd9Sstevel@tonic-gate 					if (hexdigits == 0)
907c478bd9Sstevel@tonic-gate 						*ptr++ = ':';
917c478bd9Sstevel@tonic-gate 					/* add another */
927c478bd9Sstevel@tonic-gate 					*ptr++ = ':';
937c478bd9Sstevel@tonic-gate 					first = B_TRUE;
947c478bd9Sstevel@tonic-gate 					med_zero = B_TRUE;
957c478bd9Sstevel@tonic-gate 				}
967c478bd9Sstevel@tonic-gate 			} else if (first && med_zero) {
977c478bd9Sstevel@tonic-gate 				if (hexdigits == 7)
987c478bd9Sstevel@tonic-gate 					*ptr++ = ':';
997c478bd9Sstevel@tonic-gate 				addr_component++;
1007c478bd9Sstevel@tonic-gate 				continue;
1017c478bd9Sstevel@tonic-gate 			} else {
1027c478bd9Sstevel@tonic-gate 				*ptr++ = '0';
1037c478bd9Sstevel@tonic-gate 				*ptr++ = ':';
1047c478bd9Sstevel@tonic-gate 			}
1057c478bd9Sstevel@tonic-gate 			addr_component++;
1067c478bd9Sstevel@tonic-gate 			continue;
1077c478bd9Sstevel@tonic-gate 		}
1087c478bd9Sstevel@tonic-gate 		if (med_zero)
1097c478bd9Sstevel@tonic-gate 			med_zero = B_FALSE;
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 		tempbuf[0] = '\0';
1127c478bd9Sstevel@tonic-gate 		(void) sprintf(tempbuf, "%x:", ntohs(*addr_component) & 0xffff);
1137c478bd9Sstevel@tonic-gate 		len = strlen(tempbuf);
1147c478bd9Sstevel@tonic-gate 		bcopy(tempbuf, ptr, len);
1157c478bd9Sstevel@tonic-gate 		ptr = ptr + len;
1167c478bd9Sstevel@tonic-gate 		addr_component++;
1177c478bd9Sstevel@tonic-gate 	}
1187c478bd9Sstevel@tonic-gate 	*--ptr = '\0';
1197c478bd9Sstevel@tonic-gate }
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate /*
1227c478bd9Sstevel@tonic-gate  * search for char c, terminate on trailing white space
1237c478bd9Sstevel@tonic-gate  */
1247c478bd9Sstevel@tonic-gate static char *
strchr_w(const char * sp,int c)1257c478bd9Sstevel@tonic-gate strchr_w(const char *sp, int c)
1267c478bd9Sstevel@tonic-gate {
1277c478bd9Sstevel@tonic-gate 	/* skip leading white space */
1287c478bd9Sstevel@tonic-gate 	while (*sp && (*sp == ' ' || *sp == '\t')) {
1297c478bd9Sstevel@tonic-gate 		sp++;
1307c478bd9Sstevel@tonic-gate 	}
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	do {
1337c478bd9Sstevel@tonic-gate 		if (*sp == (char)c)
1347c478bd9Sstevel@tonic-gate 			return ((char *)sp);
1357c478bd9Sstevel@tonic-gate 		if (*sp == ' ' || *sp == '\t')
1367c478bd9Sstevel@tonic-gate 			return (NULL);
1377c478bd9Sstevel@tonic-gate 	} while (*sp++);
1387c478bd9Sstevel@tonic-gate 	return (NULL);
1397c478bd9Sstevel@tonic-gate }
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate static int
str2inet_addr(char * cp,ipaddr_t * addrp)1427c478bd9Sstevel@tonic-gate str2inet_addr(char *cp, ipaddr_t *addrp)
1437c478bd9Sstevel@tonic-gate {
1447c478bd9Sstevel@tonic-gate 	char *end;
1457c478bd9Sstevel@tonic-gate 	long byte;
1467c478bd9Sstevel@tonic-gate 	int i;
1477c478bd9Sstevel@tonic-gate 	ipaddr_t addr = 0;
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	for (i = 0; i < 4; i++) {
1507c478bd9Sstevel@tonic-gate 		if (ddi_strtol(cp, &end, 10, &byte) != 0 || byte < 0 ||
1517c478bd9Sstevel@tonic-gate 		    byte > 255) {
1527c478bd9Sstevel@tonic-gate 			return (0);
1537c478bd9Sstevel@tonic-gate 		}
1547c478bd9Sstevel@tonic-gate 		addr = (addr << 8) | (uint8_t)byte;
1557c478bd9Sstevel@tonic-gate 		if (i < 3) {
1567c478bd9Sstevel@tonic-gate 			if (*end != '.') {
1577c478bd9Sstevel@tonic-gate 				return (0);
1587c478bd9Sstevel@tonic-gate 			} else {
1597c478bd9Sstevel@tonic-gate 				cp = end + 1;
1607c478bd9Sstevel@tonic-gate 			}
1617c478bd9Sstevel@tonic-gate 		} else {
1627c478bd9Sstevel@tonic-gate 			cp = end;
1637c478bd9Sstevel@tonic-gate 		}
1647c478bd9Sstevel@tonic-gate 	}
1657c478bd9Sstevel@tonic-gate 	*addrp = addr;
1667c478bd9Sstevel@tonic-gate 	return (1);
1677c478bd9Sstevel@tonic-gate }
1687c478bd9Sstevel@tonic-gate 
1690b905b49SYuri Pankov /*
1700b905b49SYuri Pankov  * inet_ntop: Convert an IPv4 or IPv6 address in binary form into
1710b905b49SYuri Pankov  * printable form, and return a pointer to that string.  Caller should
1720b905b49SYuri Pankov  * provide a buffer of correct length to store string into.
1730b905b49SYuri Pankov  * Note: this routine is kernel version of inet_ntop.  It has similar
1740b905b49SYuri Pankov  * format as inet_ntop() defined in RFC 2553, but it does not do
1750b905b49SYuri Pankov  * error handling operations exactly as RFC 2553 defines.
1760b905b49SYuri Pankov  */
1770b905b49SYuri Pankov static char *
__inet_ntop(int af,const void * addr,char * buf,int addrlen,int compat)1780b905b49SYuri Pankov __inet_ntop(int af, const void *addr, char *buf, int addrlen, int compat)
1790b905b49SYuri Pankov {
1800b905b49SYuri Pankov 	static char	*badaf = "<badfamily>";
1810b905b49SYuri Pankov 	in6_addr_t	*v6addr;
1820b905b49SYuri Pankov 	uchar_t		*v4addr;
1830b905b49SYuri Pankov 	char		*caddr;
1840b905b49SYuri Pankov 
1850b905b49SYuri Pankov 	VERIFY(addr != NULL);
1860b905b49SYuri Pankov 	VERIFY(OK_32PTR(addr));
1870b905b49SYuri Pankov 	VERIFY(buf != NULL);
1880b905b49SYuri Pankov 
1890b905b49SYuri Pankov 	buf[0] = '\0';
1900b905b49SYuri Pankov 
1910b905b49SYuri Pankov #define	UC(b)	(((int)b) & 0xff)
1920b905b49SYuri Pankov 	switch (af) {
1930b905b49SYuri Pankov 	case AF_INET:
1940b905b49SYuri Pankov 		ASSERT(addrlen >= INET_ADDRSTRLEN);
1950b905b49SYuri Pankov 		v4addr = (uchar_t *)addr;
1960b905b49SYuri Pankov 		(void) sprintf(buf,
1970b905b49SYuri Pankov 		    (compat) ? "%03d.%03d.%03d.%03d" : "%d.%d.%d.%d",
1980b905b49SYuri Pankov 		    UC(v4addr[0]), UC(v4addr[1]), UC(v4addr[2]), UC(v4addr[3]));
1990b905b49SYuri Pankov 		return (buf);
2000b905b49SYuri Pankov 	case AF_INET6:
2010b905b49SYuri Pankov 		ASSERT(addrlen >= INET6_ADDRSTRLEN);
2020b905b49SYuri Pankov 		v6addr = (in6_addr_t *)addr;
2030b905b49SYuri Pankov 		if (IN6_IS_ADDR_V4MAPPED(v6addr)) {
2040b905b49SYuri Pankov 			caddr = (char *)addr;
2050b905b49SYuri Pankov 			(void) sprintf(buf, "::ffff:%d.%d.%d.%d",
2060b905b49SYuri Pankov 			    UC(caddr[12]), UC(caddr[13]),
2070b905b49SYuri Pankov 			    UC(caddr[14]), UC(caddr[15]));
2080b905b49SYuri Pankov 		} else if (IN6_IS_ADDR_V4COMPAT(v6addr)) {
2090b905b49SYuri Pankov 			caddr = (char *)addr;
2100b905b49SYuri Pankov 			(void) sprintf(buf, "::%d.%d.%d.%d",
2110b905b49SYuri Pankov 			    UC(caddr[12]), UC(caddr[13]), UC(caddr[14]),
2120b905b49SYuri Pankov 			    UC(caddr[15]));
2130b905b49SYuri Pankov 		} else if (IN6_IS_ADDR_UNSPECIFIED(v6addr)) {
2140b905b49SYuri Pankov 			(void) sprintf(buf, "::");
2150b905b49SYuri Pankov 		} else {
2160b905b49SYuri Pankov 			convert2ascii(buf, v6addr);
2170b905b49SYuri Pankov 		}
2180b905b49SYuri Pankov 		return (buf);
2190b905b49SYuri Pankov 
2200b905b49SYuri Pankov 	default:
2210b905b49SYuri Pankov 		return (badaf);
2220b905b49SYuri Pankov 	}
2230b905b49SYuri Pankov #undef UC
2240b905b49SYuri Pankov }
2250b905b49SYuri Pankov 
2260b905b49SYuri Pankov /*
2270b905b49SYuri Pankov  * Provide fixed inet_ntop() implementation.
2280b905b49SYuri Pankov  */
2290b905b49SYuri Pankov char *
_inet_ntop(int af,const void * addr,char * buf,int addrlen)2300b905b49SYuri Pankov _inet_ntop(int af, const void *addr, char *buf, int addrlen)
2310b905b49SYuri Pankov {
2320b905b49SYuri Pankov 	return (__inet_ntop(af, addr, buf, addrlen, 0));
2330b905b49SYuri Pankov }
2340b905b49SYuri Pankov 
2350b905b49SYuri Pankov /*
2360b905b49SYuri Pankov  * Provide old inet_ntop() implementation by default for binary
2370b905b49SYuri Pankov  * compatibility.
2380b905b49SYuri Pankov  */
2390b905b49SYuri Pankov char *
inet_ntop(int af,const void * addr,char * buf,int addrlen)2400b905b49SYuri Pankov inet_ntop(int af, const void *addr, char *buf, int addrlen)
2410b905b49SYuri Pankov {
2420b905b49SYuri Pankov 	static char	local_buf[INET6_ADDRSTRLEN];
2430b905b49SYuri Pankov 	static char	*badaddr = "<badaddr>";
2440b905b49SYuri Pankov 
2450b905b49SYuri Pankov 	if (addr == NULL || !(OK_32PTR(addr)))
2460b905b49SYuri Pankov 		return (badaddr);
2470b905b49SYuri Pankov 
2480b905b49SYuri Pankov 	if (buf == NULL) {
2490b905b49SYuri Pankov 		buf = local_buf;
2500b905b49SYuri Pankov 		addrlen = sizeof (local_buf);
2510b905b49SYuri Pankov 	}
2520b905b49SYuri Pankov 
2530b905b49SYuri Pankov 	return (__inet_ntop(af, addr, buf, addrlen, 1));
2540b905b49SYuri Pankov }
2550b905b49SYuri Pankov 
2567c478bd9Sstevel@tonic-gate /*
2577c478bd9Sstevel@tonic-gate  * inet_pton: This function takes string format IPv4 or IPv6 address and
2587c478bd9Sstevel@tonic-gate  * converts it to binary form. The format of this function corresponds to
2597c478bd9Sstevel@tonic-gate  * inet_pton() in the socket library.
26059927d31SYuri Pankov  *
26159927d31SYuri Pankov  * Return values:
26259927d31SYuri Pankov  *  0 invalid IPv4 or IPv6 address
26359927d31SYuri Pankov  *  1 successful conversion
26459927d31SYuri Pankov  * -1 af is not AF_INET or AF_INET6
2657c478bd9Sstevel@tonic-gate  */
2660b905b49SYuri Pankov static int
__inet_pton(int af,char * inp,void * outp,int compat)26759927d31SYuri Pankov __inet_pton(int af, char *inp, void *outp, int compat)
2687c478bd9Sstevel@tonic-gate {
2697c478bd9Sstevel@tonic-gate 	int i;
2707c478bd9Sstevel@tonic-gate 	long byte;
2717c478bd9Sstevel@tonic-gate 	char *end;
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	switch (af) {
2747c478bd9Sstevel@tonic-gate 	case AF_INET:
27559927d31SYuri Pankov 		if (str2inet_addr(inp, (ipaddr_t *)outp) != 0) {
27659927d31SYuri Pankov 			if (!compat)
27759927d31SYuri Pankov 				*(uint32_t *)outp = htonl(*(uint32_t *)outp);
27859927d31SYuri Pankov 			return (1);
27959927d31SYuri Pankov 		} else {
28059927d31SYuri Pankov 			return (0);
28159927d31SYuri Pankov 		}
2827c478bd9Sstevel@tonic-gate 	case AF_INET6: {
2837c478bd9Sstevel@tonic-gate 		union v6buf_u {
2847c478bd9Sstevel@tonic-gate 			uint16_t v6words_u[8];
2857c478bd9Sstevel@tonic-gate 			in6_addr_t v6addr_u;
2867c478bd9Sstevel@tonic-gate 		} v6buf, *v6outp;
2877c478bd9Sstevel@tonic-gate 		uint16_t	*dbl_col = NULL;
288*7e12ceb3SToomas Soome 		char lastbyte = '\0';
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 		v6outp = (union v6buf_u *)outp;
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 		if (strchr_w(inp, '.') != NULL) {
293f6714461SRobert Mustacchi 			int ret = 0;
294f6714461SRobert Mustacchi 
2957c478bd9Sstevel@tonic-gate 			/* v4 mapped or v4 compatable */
2967c478bd9Sstevel@tonic-gate 			if (strncmp(inp, "::ffff:", 7) == 0) {
2977c478bd9Sstevel@tonic-gate 				ipaddr_t ipv4_all_zeroes = 0;
2987c478bd9Sstevel@tonic-gate 				/* mapped - first init prefix and then fill */
2997c478bd9Sstevel@tonic-gate 				IN6_IPADDR_TO_V4MAPPED(ipv4_all_zeroes,
3007c478bd9Sstevel@tonic-gate 				    &v6outp->v6addr_u);
301f6714461SRobert Mustacchi 				ret = str2inet_addr(inp + 7,
302f6714461SRobert Mustacchi 				    &(v6outp->v6addr_u.s6_addr32[3]));
3037c478bd9Sstevel@tonic-gate 			} else if (strncmp(inp, "::", 2) == 0) {
3047c478bd9Sstevel@tonic-gate 				/* v4 compatable - prefix all zeroes */
3057c478bd9Sstevel@tonic-gate 				bzero(&v6outp->v6addr_u, sizeof (in6_addr_t));
306f6714461SRobert Mustacchi 				ret = str2inet_addr(inp + 2,
307f6714461SRobert Mustacchi 				    &(v6outp->v6addr_u.s6_addr32[3]));
3087c478bd9Sstevel@tonic-gate 			}
309f6714461SRobert Mustacchi 			if (ret > 0 && !compat) {
310f6714461SRobert Mustacchi 				v6outp->v6addr_u.s6_addr32[3] =
311f6714461SRobert Mustacchi 				    htonl(v6outp->v6addr_u.s6_addr32[3]);
312f6714461SRobert Mustacchi 			}
313f6714461SRobert Mustacchi 			return (ret);
3147c478bd9Sstevel@tonic-gate 		}
3157c478bd9Sstevel@tonic-gate 		for (i = 0; i < 8; i++) {
3167c478bd9Sstevel@tonic-gate 			int error;
3177c478bd9Sstevel@tonic-gate 			/*
3187c478bd9Sstevel@tonic-gate 			 * if ddi_strtol() fails it could be because
3197c478bd9Sstevel@tonic-gate 			 * the string is "::".  That is valid and
3207c478bd9Sstevel@tonic-gate 			 * checked for below so just set the value to
3217c478bd9Sstevel@tonic-gate 			 * 0 and continue.
3227c478bd9Sstevel@tonic-gate 			 */
3237c478bd9Sstevel@tonic-gate 			if ((error = ddi_strtol(inp, &end, 16, &byte)) != 0) {
3247c478bd9Sstevel@tonic-gate 				if (error == ERANGE)
3257c478bd9Sstevel@tonic-gate 					return (0);
3267c478bd9Sstevel@tonic-gate 				byte = 0;
3277c478bd9Sstevel@tonic-gate 			}
3287c478bd9Sstevel@tonic-gate 			if (byte < 0 || byte > 0x0ffff) {
3297c478bd9Sstevel@tonic-gate 				return (0);
3307c478bd9Sstevel@tonic-gate 			}
33159927d31SYuri Pankov 			if (compat) {
33259927d31SYuri Pankov 				v6buf.v6words_u[i] = (uint16_t)byte;
33359927d31SYuri Pankov 			} else {
33459927d31SYuri Pankov 				v6buf.v6words_u[i] = htons((uint16_t)byte);
33559927d31SYuri Pankov 			}
336*7e12ceb3SToomas Soome 			if (*end == '\0' || i == 7) {
3377c478bd9Sstevel@tonic-gate 				inp = end;
3387c478bd9Sstevel@tonic-gate 				break;
3397c478bd9Sstevel@tonic-gate 			}
3407c478bd9Sstevel@tonic-gate 			if (inp == end) {	/* not a number must be */
3417c478bd9Sstevel@tonic-gate 				if (*inp == ':' &&
3427c478bd9Sstevel@tonic-gate 				    ((i == 0 && *(inp + 1) == ':') ||
3437c478bd9Sstevel@tonic-gate 				    lastbyte == ':')) {
3447c478bd9Sstevel@tonic-gate 					if (dbl_col) {
3457c478bd9Sstevel@tonic-gate 						return (0);
3467c478bd9Sstevel@tonic-gate 					}
3477c478bd9Sstevel@tonic-gate 					if (byte != 0)
3487c478bd9Sstevel@tonic-gate 						i++;
3497c478bd9Sstevel@tonic-gate 					dbl_col = &v6buf.v6words_u[i];
3507c478bd9Sstevel@tonic-gate 					if (i == 0)
3517c478bd9Sstevel@tonic-gate 						inp++;
352*7e12ceb3SToomas Soome 				} else if (*inp == '\0' || *inp == ' ' ||
3537c478bd9Sstevel@tonic-gate 				    *inp == '\t') {
3547c478bd9Sstevel@tonic-gate 					break;
3557c478bd9Sstevel@tonic-gate 				} else {
3567c478bd9Sstevel@tonic-gate 					return (0);
3577c478bd9Sstevel@tonic-gate 				}
3587c478bd9Sstevel@tonic-gate 			} else {
3597c478bd9Sstevel@tonic-gate 				inp = end;
3607c478bd9Sstevel@tonic-gate 			}
3617c478bd9Sstevel@tonic-gate 			if (*inp != ':') {
3627c478bd9Sstevel@tonic-gate 				return (0);
3637c478bd9Sstevel@tonic-gate 			}
3647c478bd9Sstevel@tonic-gate 			inp++;
365*7e12ceb3SToomas Soome 			if (*inp == '\0' || *inp == ' ' || *inp == '\t') {
3667c478bd9Sstevel@tonic-gate 				break;
3677c478bd9Sstevel@tonic-gate 			}
3687c478bd9Sstevel@tonic-gate 			lastbyte = *inp;
3697c478bd9Sstevel@tonic-gate 		}
370*7e12ceb3SToomas Soome 		if (*inp != '\0' && *inp != ' ' && *inp != '\t') {
3717c478bd9Sstevel@tonic-gate 			return (0);
3727c478bd9Sstevel@tonic-gate 		}
3737c478bd9Sstevel@tonic-gate 		/*
3747c478bd9Sstevel@tonic-gate 		 * v6words now contains the bytes we could translate
3757c478bd9Sstevel@tonic-gate 		 * dbl_col points to the word (should be 0) where
3767c478bd9Sstevel@tonic-gate 		 * a double colon was found
3777c478bd9Sstevel@tonic-gate 		 */
3787c478bd9Sstevel@tonic-gate 		if (i == 7) {
3797c478bd9Sstevel@tonic-gate 			v6outp->v6addr_u = v6buf.v6addr_u;
3807c478bd9Sstevel@tonic-gate 		} else {
3817c478bd9Sstevel@tonic-gate 			int rem;
3827c478bd9Sstevel@tonic-gate 			int word;
3837c478bd9Sstevel@tonic-gate 			int next;
3847c478bd9Sstevel@tonic-gate 			if (dbl_col == NULL) {
3857c478bd9Sstevel@tonic-gate 				return (0);
3867c478bd9Sstevel@tonic-gate 			}
3877c478bd9Sstevel@tonic-gate 			bzero(&v6outp->v6addr_u, sizeof (in6_addr_t));
3887c478bd9Sstevel@tonic-gate 			rem = dbl_col - &v6buf.v6words_u[0];
3897c478bd9Sstevel@tonic-gate 			for (next = 0; next < rem; next++) {
3907c478bd9Sstevel@tonic-gate 				v6outp->v6words_u[next] = v6buf.v6words_u[next];
3917c478bd9Sstevel@tonic-gate 			}
3927c478bd9Sstevel@tonic-gate 			next++;	/* skip dbl_col 0 */
3937c478bd9Sstevel@tonic-gate 			rem = i - rem;
3947c478bd9Sstevel@tonic-gate 			word = 8 - rem;
3957c478bd9Sstevel@tonic-gate 			while (rem > 0) {
3967c478bd9Sstevel@tonic-gate 				v6outp->v6words_u[word] = v6buf.v6words_u[next];
3977c478bd9Sstevel@tonic-gate 				word++;
3987c478bd9Sstevel@tonic-gate 				rem--;
3997c478bd9Sstevel@tonic-gate 				next++;
4007c478bd9Sstevel@tonic-gate 			}
4017c478bd9Sstevel@tonic-gate 		}
4027c478bd9Sstevel@tonic-gate 		return (1);	/* Success */
4037c478bd9Sstevel@tonic-gate 	}
4047c478bd9Sstevel@tonic-gate 	}	/* switch */
4057c478bd9Sstevel@tonic-gate 	return (-1);	/* return -1 for default case */
4067c478bd9Sstevel@tonic-gate }
40759927d31SYuri Pankov 
40859927d31SYuri Pankov /*
40959927d31SYuri Pankov  * Provide fixed inet_pton() implementation.
41059927d31SYuri Pankov  */
41159927d31SYuri Pankov int
_inet_pton(int af,char * inp,void * outp)41259927d31SYuri Pankov _inet_pton(int af, char *inp, void *outp)
41359927d31SYuri Pankov {
41459927d31SYuri Pankov 	return (__inet_pton(af, inp, outp, 0));
41559927d31SYuri Pankov }
41659927d31SYuri Pankov 
41759927d31SYuri Pankov /*
41859927d31SYuri Pankov  * Provide broken inet_pton() implementation by default for binary
41959927d31SYuri Pankov  * compatibility.
42059927d31SYuri Pankov  */
42159927d31SYuri Pankov int
inet_pton(int af,char * inp,void * outp)42259927d31SYuri Pankov inet_pton(int af, char *inp, void *outp)
42359927d31SYuri Pankov {
42459927d31SYuri Pankov 	return (__inet_pton(af, inp, outp, 1));
42559927d31SYuri Pankov }
426