xref: /onnv-gate/usr/src/lib/libnsl/nss/inet_pton.c (revision 1219:f89f56c2d9ac)
10Sstevel@tonic-gate /*
2*1219Sraf  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
3132Srobinson  * Use is subject to license terms.
40Sstevel@tonic-gate  */
50Sstevel@tonic-gate 
6132Srobinson /*
7132Srobinson  * Copyright (c) 1996 by Internet Software Consortium.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software for any
100Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
110Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
140Sstevel@tonic-gate  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
150Sstevel@tonic-gate  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
160Sstevel@tonic-gate  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
170Sstevel@tonic-gate  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
180Sstevel@tonic-gate  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
190Sstevel@tonic-gate  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
200Sstevel@tonic-gate  * SOFTWARE.
210Sstevel@tonic-gate  */
22132Srobinson 
230Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
240Sstevel@tonic-gate 
25*1219Sraf #include "mt.h"
260Sstevel@tonic-gate #include <stdlib.h>
270Sstevel@tonic-gate #include <ctype.h>
280Sstevel@tonic-gate #include <string.h>
290Sstevel@tonic-gate #include <strings.h>
300Sstevel@tonic-gate #include <netdb.h>
310Sstevel@tonic-gate #include <stdio.h>
320Sstevel@tonic-gate #include <arpa/inet.h>
330Sstevel@tonic-gate #include <netinet/in.h>
340Sstevel@tonic-gate #include <sys/socket.h>
350Sstevel@tonic-gate #include <errno.h>
360Sstevel@tonic-gate 
37132Srobinson static int	inet_pton4(const char *src, uchar_t *dst);
38132Srobinson static int	inet_pton6(const char *src, uchar_t *dst);
390Sstevel@tonic-gate 
40132Srobinson /*
41132Srobinson  * int
420Sstevel@tonic-gate  * inet_pton(af, src, dst)
430Sstevel@tonic-gate  *	convert from presentation format (which usually means ASCII printable)
440Sstevel@tonic-gate  *	to network format (which is usually some kind of binary format).
450Sstevel@tonic-gate  * return:
460Sstevel@tonic-gate  *	1 if the address was valid for the specified address family
470Sstevel@tonic-gate  *	0 if the address wasn't valid (`dst' is untouched in this case)
480Sstevel@tonic-gate  *	-1 if some other error occurred (`dst' is untouched in this case, too)
490Sstevel@tonic-gate  * author:
500Sstevel@tonic-gate  *	Paul Vixie, 1996.  Taken from on297-gate:dns stuff
510Sstevel@tonic-gate  */
520Sstevel@tonic-gate int
inet_pton(int af,const char * src,void * dst)53132Srobinson inet_pton(int af, const char *src, void *dst)
540Sstevel@tonic-gate {
550Sstevel@tonic-gate 	switch (af) {
560Sstevel@tonic-gate 	case AF_INET:
570Sstevel@tonic-gate 		return (inet_pton4(src, dst));
580Sstevel@tonic-gate 	case AF_INET6:
590Sstevel@tonic-gate 		return (inet_pton6(src, dst));
600Sstevel@tonic-gate 	default:
610Sstevel@tonic-gate 		errno = EAFNOSUPPORT;
620Sstevel@tonic-gate 		return (-1);
630Sstevel@tonic-gate 	}
640Sstevel@tonic-gate 	/* NOTREACHED */
650Sstevel@tonic-gate }
660Sstevel@tonic-gate 
67132Srobinson /*
68132Srobinson  * int
690Sstevel@tonic-gate  * inet_pton4(src, dst)
700Sstevel@tonic-gate  *	like inet_aton() but without all the hexadecimal and shorthand.
710Sstevel@tonic-gate  * return:
720Sstevel@tonic-gate  *	1 if `src' is a valid dotted quad, else 0.
730Sstevel@tonic-gate  * notice:
740Sstevel@tonic-gate  *	does not touch `dst' unless it's returning 1.
750Sstevel@tonic-gate  *
760Sstevel@tonic-gate  */
77132Srobinson #define	INADDRSZ	4
780Sstevel@tonic-gate #define	IN6ADDRSZ	16
79132Srobinson #define	INT16SZ		2
80132Srobinson 
810Sstevel@tonic-gate static int
inet_pton4(const char * src,uchar_t * dst)82132Srobinson inet_pton4(const char *src, uchar_t *dst)
830Sstevel@tonic-gate {
840Sstevel@tonic-gate 	static const char digits[] = "0123456789";
850Sstevel@tonic-gate 	int saw_digit, octets, ch;
86132Srobinson 	uchar_t tmp[INADDRSZ], *tp;
870Sstevel@tonic-gate 
880Sstevel@tonic-gate 	saw_digit = 0;
890Sstevel@tonic-gate 	octets = 0;
900Sstevel@tonic-gate 	*(tp = tmp) = 0;
910Sstevel@tonic-gate 	while ((ch = *src++) != '\0') {
920Sstevel@tonic-gate 		const char *pch;
930Sstevel@tonic-gate 
940Sstevel@tonic-gate 		if ((pch = strchr(digits, ch)) != NULL) {
95132Srobinson 			uint_t new = *tp * 10 + (pch - digits);
960Sstevel@tonic-gate 
970Sstevel@tonic-gate 			if (new > 255)
980Sstevel@tonic-gate 				return (0);
990Sstevel@tonic-gate 			*tp = new;
100132Srobinson 			if (!saw_digit) {
1010Sstevel@tonic-gate 				if (++octets > 4)
1020Sstevel@tonic-gate 					return (0);
1030Sstevel@tonic-gate 				saw_digit = 1;
1040Sstevel@tonic-gate 			}
1050Sstevel@tonic-gate 		} else if (ch == '.' && saw_digit) {
1060Sstevel@tonic-gate 			if (octets == 4)
1070Sstevel@tonic-gate 				return (0);
1080Sstevel@tonic-gate 			*++tp = 0;
1090Sstevel@tonic-gate 			saw_digit = 0;
1100Sstevel@tonic-gate 		} else
1110Sstevel@tonic-gate 			return (0);
1120Sstevel@tonic-gate 	}
1130Sstevel@tonic-gate 	if (octets < 4)
1140Sstevel@tonic-gate 		return (0);
1150Sstevel@tonic-gate 
116132Srobinson 	(void) memcpy(dst, tmp, INADDRSZ);
1170Sstevel@tonic-gate 	return (1);
1180Sstevel@tonic-gate }
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 
121132Srobinson /*
122132Srobinson  * int
1230Sstevel@tonic-gate  * inet_pton6(src, dst)
1240Sstevel@tonic-gate  *	convert presentation level address to network order binary form.
1250Sstevel@tonic-gate  * return:
1260Sstevel@tonic-gate  *	1 if `src' is a valid [RFC1884 2.2] address, else 0.
1270Sstevel@tonic-gate  * notice:
1280Sstevel@tonic-gate  *	(1) does not touch `dst' unless it's returning 1.
1290Sstevel@tonic-gate  *	(2) :: in a full address is silently ignored.
1300Sstevel@tonic-gate  * credit:
1310Sstevel@tonic-gate  *	inspired by Mark Andrews.
1320Sstevel@tonic-gate  *
1330Sstevel@tonic-gate  */
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate static int
inet_pton6(const char * src,uchar_t * dst)137132Srobinson inet_pton6(const char *src, uchar_t *dst)
1380Sstevel@tonic-gate {
1390Sstevel@tonic-gate 	static const char xdigits_l[] = "0123456789abcdef",
140132Srobinson 			xdigits_u[] = "0123456789ABCDEF";
141132Srobinson 	uchar_t tmp[IN6ADDRSZ], *tp, *endp, *colonp;
1420Sstevel@tonic-gate 	const char *xdigits, *curtok;
1430Sstevel@tonic-gate 	int ch, saw_xdigit;
144132Srobinson 	uint_t val;
1450Sstevel@tonic-gate 
146132Srobinson 	(void) memset((tp = tmp), '\0', IN6ADDRSZ);
1470Sstevel@tonic-gate 	endp = tp + IN6ADDRSZ;
1480Sstevel@tonic-gate 	colonp = NULL;
1490Sstevel@tonic-gate 	/* Leading :: requires some special handling. */
1500Sstevel@tonic-gate 	if (*src == ':')
1510Sstevel@tonic-gate 		if (*++src != ':')
1520Sstevel@tonic-gate 			return (0);
1530Sstevel@tonic-gate 	curtok = src;
1540Sstevel@tonic-gate 	saw_xdigit = 0;
1550Sstevel@tonic-gate 	val = 0;
1560Sstevel@tonic-gate 	while ((ch = *src++) != '\0') {
1570Sstevel@tonic-gate 		const char *pch;
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 		if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
1600Sstevel@tonic-gate 			pch = strchr((xdigits = xdigits_u), ch);
1610Sstevel@tonic-gate 		if (pch != NULL) {
1620Sstevel@tonic-gate 			val <<= 4;
1630Sstevel@tonic-gate 			val |= (pch - xdigits);
1640Sstevel@tonic-gate 			if (val > 0xffff)
1650Sstevel@tonic-gate 				return (0);
1660Sstevel@tonic-gate 			saw_xdigit = 1;
1670Sstevel@tonic-gate 			continue;
1680Sstevel@tonic-gate 		}
1690Sstevel@tonic-gate 		if (ch == ':') {
1700Sstevel@tonic-gate 			curtok = src;
1710Sstevel@tonic-gate 			if (!saw_xdigit) {
1720Sstevel@tonic-gate 				if (colonp)
1730Sstevel@tonic-gate 					return (0);
1740Sstevel@tonic-gate 				colonp = tp;
1750Sstevel@tonic-gate 				continue;
1760Sstevel@tonic-gate 			} else if (*src == '\0') {
1770Sstevel@tonic-gate 				return (0);
1780Sstevel@tonic-gate 			}
1790Sstevel@tonic-gate 			if (tp + INT16SZ > endp)
1800Sstevel@tonic-gate 				return (0);
181132Srobinson 			*tp++ = (uchar_t)(val >> 8) & 0xff;
182132Srobinson 			*tp++ = (uchar_t)val & 0xff;
1830Sstevel@tonic-gate 			saw_xdigit = 0;
1840Sstevel@tonic-gate 			val = 0;
1850Sstevel@tonic-gate 			continue;
1860Sstevel@tonic-gate 		}
1870Sstevel@tonic-gate 		if (ch == '.' && ((tp + INADDRSZ) <= endp) &&
1880Sstevel@tonic-gate 		    inet_pton4(curtok, tp) > 0) {
1890Sstevel@tonic-gate 			tp += INADDRSZ;
1900Sstevel@tonic-gate 			saw_xdigit = 0;
1910Sstevel@tonic-gate 			break;	/* '\0' was seen by inet_pton4(). */
1920Sstevel@tonic-gate 		}
1930Sstevel@tonic-gate 		return (0);
1940Sstevel@tonic-gate 	}
1950Sstevel@tonic-gate 	if (saw_xdigit) {
1960Sstevel@tonic-gate 		if (tp + INT16SZ > endp)
1970Sstevel@tonic-gate 			return (0);
198132Srobinson 		*tp++ = (uchar_t)(val >> 8) & 0xff;
199132Srobinson 		*tp++ = (uchar_t)val & 0xff;
2000Sstevel@tonic-gate 	}
2010Sstevel@tonic-gate 	if (colonp != NULL) {
2020Sstevel@tonic-gate 		/*
2030Sstevel@tonic-gate 		 * Since some memmove()'s erroneously fail to handle
2040Sstevel@tonic-gate 		 * overlapping regions, we'll do the shift by hand.
2050Sstevel@tonic-gate 		 */
2060Sstevel@tonic-gate 		const int n = tp - colonp;
2070Sstevel@tonic-gate 		int i;
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 		if (tp == endp)
2100Sstevel@tonic-gate 			return (0);
2110Sstevel@tonic-gate 		for (i = 1; i <= n; i++) {
2120Sstevel@tonic-gate 			endp[- i] = colonp[n - i];
2130Sstevel@tonic-gate 			colonp[n - i] = 0;
2140Sstevel@tonic-gate 		}
2150Sstevel@tonic-gate 		tp = endp;
2160Sstevel@tonic-gate 	}
2170Sstevel@tonic-gate 	if (tp != endp)
2180Sstevel@tonic-gate 		return (0);
219132Srobinson 	(void) memcpy(dst, tmp, IN6ADDRSZ);
2200Sstevel@tonic-gate 	return (1);
2210Sstevel@tonic-gate }
222