xref: /netbsd-src/lib/libc/inet/inet_pton.c (revision c5e820cae412164fcbee52f470436200af5358ea)
1*c5e820caSchristos /*	$NetBSD: inet_pton.c,v 1.8 2012/03/13 21:13:38 christos Exp $	*/
239e7bb71Schristos 
339e7bb71Schristos /*
439e7bb71Schristos  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
539e7bb71Schristos  * Copyright (c) 1996,1999 by Internet Software Consortium.
639e7bb71Schristos  *
739e7bb71Schristos  * Permission to use, copy, modify, and distribute this software for any
839e7bb71Schristos  * purpose with or without fee is hereby granted, provided that the above
939e7bb71Schristos  * copyright notice and this permission notice appear in all copies.
1039e7bb71Schristos  *
1139e7bb71Schristos  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
1239e7bb71Schristos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1339e7bb71Schristos  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
1439e7bb71Schristos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1539e7bb71Schristos  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1639e7bb71Schristos  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
1739e7bb71Schristos  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1839e7bb71Schristos  */
1939e7bb71Schristos 
20df0952c6Schristos #include <sys/cdefs.h>
2139e7bb71Schristos #if defined(LIBC_SCCS) && !defined(lint)
22df0952c6Schristos #if 0
233873655bSchristos static const char rcsid[] = "Id: inet_pton.c,v 1.5 2005/07/28 06:51:47 marka Exp";
24df0952c6Schristos #else
25*c5e820caSchristos __RCSID("$NetBSD: inet_pton.c,v 1.8 2012/03/13 21:13:38 christos Exp $");
26df0952c6Schristos #endif
2739e7bb71Schristos #endif /* LIBC_SCCS and not lint */
2839e7bb71Schristos 
2939e7bb71Schristos #include "port_before.h"
30df0952c6Schristos 
31df0952c6Schristos #include "namespace.h"
3239e7bb71Schristos #include <sys/param.h>
3339e7bb71Schristos #include <sys/types.h>
3439e7bb71Schristos #include <sys/socket.h>
3539e7bb71Schristos #include <netinet/in.h>
3639e7bb71Schristos #include <arpa/inet.h>
3739e7bb71Schristos #include <arpa/nameser.h>
38*c5e820caSchristos #include <stddef.h>
3939e7bb71Schristos #include <string.h>
40df0952c6Schristos #include <assert.h>
41df0952c6Schristos #include <ctype.h>
4239e7bb71Schristos #include <errno.h>
43df0952c6Schristos 
4439e7bb71Schristos #include "port_after.h"
4539e7bb71Schristos 
46df0952c6Schristos #ifdef __weak_alias
47df0952c6Schristos __weak_alias(inet_pton,_inet_pton)
48df0952c6Schristos #endif
49df0952c6Schristos 
50d73eb73dSchristos /*%
5139e7bb71Schristos  * WARNING: Don't even consider trying to compile this on a system where
5239e7bb71Schristos  * sizeof(int) < 4.  sizeof(int) > 4 is fine; all the world's not a VAX.
5339e7bb71Schristos  */
5439e7bb71Schristos 
55df0952c6Schristos static int	inet_pton4(const char *src, u_char *dst, int pton);
56df0952c6Schristos static int	inet_pton6(const char *src, u_char *dst);
5739e7bb71Schristos 
5839e7bb71Schristos /* int
5939e7bb71Schristos  * inet_pton(af, src, dst)
6039e7bb71Schristos  *	convert from presentation format (which usually means ASCII printable)
6139e7bb71Schristos  *	to network format (which is usually some kind of binary format).
6239e7bb71Schristos  * return:
6339e7bb71Schristos  *	1 if the address was valid for the specified address family
6439e7bb71Schristos  *	0 if the address wasn't valid (`dst' is untouched in this case)
6539e7bb71Schristos  *	-1 if some other error occurred (`dst' is untouched in this case, too)
6639e7bb71Schristos  * author:
6739e7bb71Schristos  *	Paul Vixie, 1996.
6839e7bb71Schristos  */
6939e7bb71Schristos int
inet_pton(int af,const char * src,void * dst)70df0952c6Schristos inet_pton(int af, const char *src, void *dst)
7139e7bb71Schristos {
72df0952c6Schristos 
73df0952c6Schristos 	_DIAGASSERT(src != NULL);
74df0952c6Schristos 	_DIAGASSERT(dst != NULL);
75df0952c6Schristos 
7639e7bb71Schristos 	switch (af) {
7739e7bb71Schristos 	case AF_INET:
78df0952c6Schristos 		return (inet_pton4(src, dst, 1));
7939e7bb71Schristos 	case AF_INET6:
8039e7bb71Schristos 		return (inet_pton6(src, dst));
8139e7bb71Schristos 	default:
8239e7bb71Schristos 		errno = EAFNOSUPPORT;
8339e7bb71Schristos 		return (-1);
8439e7bb71Schristos 	}
8539e7bb71Schristos 	/* NOTREACHED */
8639e7bb71Schristos }
8739e7bb71Schristos 
8839e7bb71Schristos /* int
89df0952c6Schristos  * inet_pton4(src, dst, pton)
90df0952c6Schristos  *	when last arg is 0: inet_aton(). with hexadecimal, octal and shorthand.
91df0952c6Schristos  *	when last arg is 1: inet_pton(). decimal dotted-quad only.
9239e7bb71Schristos  * return:
93df0952c6Schristos  *	1 if `src' is a valid input, else 0.
9439e7bb71Schristos  * notice:
9539e7bb71Schristos  *	does not touch `dst' unless it's returning 1.
9639e7bb71Schristos  * author:
9739e7bb71Schristos  *	Paul Vixie, 1996.
9839e7bb71Schristos  */
9939e7bb71Schristos static int
inet_pton4(const char * src,u_char * dst,int pton)100df0952c6Schristos inet_pton4(const char *src, u_char *dst, int pton)
10139e7bb71Schristos {
102df0952c6Schristos 	u_int32_t val;
103df0952c6Schristos 	u_int digit, base;
104*c5e820caSchristos 	ptrdiff_t n;
105df0952c6Schristos 	unsigned char c;
106df0952c6Schristos 	u_int parts[4];
107*c5e820caSchristos 	u_int *pp = parts;
10839e7bb71Schristos 
109df0952c6Schristos 	_DIAGASSERT(src != NULL);
110df0952c6Schristos 	_DIAGASSERT(dst != NULL);
11139e7bb71Schristos 
112df0952c6Schristos 	c = *src;
113df0952c6Schristos 	for (;;) {
114df0952c6Schristos 		/*
115df0952c6Schristos 		 * Collect number up to ``.''.
116df0952c6Schristos 		 * Values are specified as for C:
117df0952c6Schristos 		 * 0x=hex, 0=octal, isdigit=decimal.
118df0952c6Schristos 		 */
119df0952c6Schristos 		if (!isdigit(c))
12039e7bb71Schristos 			return (0);
121df0952c6Schristos 		val = 0; base = 10;
122df0952c6Schristos 		if (c == '0') {
123df0952c6Schristos 			c = *++src;
124df0952c6Schristos 			if (c == 'x' || c == 'X')
125df0952c6Schristos 				base = 16, c = *++src;
126df0952c6Schristos 			else if (isdigit(c) && c != '9')
127df0952c6Schristos 				base = 8;
12839e7bb71Schristos 		}
129df0952c6Schristos 		/* inet_pton() takes decimal only */
130df0952c6Schristos 		if (pton && base != 10)
13139e7bb71Schristos 			return (0);
132df0952c6Schristos 		for (;;) {
133df0952c6Schristos 			if (isdigit(c)) {
134df0952c6Schristos 				digit = c - '0';
135df0952c6Schristos 				if (digit >= base)
136df0952c6Schristos 					break;
137df0952c6Schristos 				val = (val * base) + digit;
138df0952c6Schristos 				c = *++src;
139df0952c6Schristos 			} else if (base == 16 && isxdigit(c)) {
140df0952c6Schristos 				digit = c + 10 - (islower(c) ? 'a' : 'A');
141df0952c6Schristos 				if (digit >= 16)
142df0952c6Schristos 					break;
143df0952c6Schristos 				val = (val << 4) | digit;
144df0952c6Schristos 				c = *++src;
14539e7bb71Schristos 			} else
146df0952c6Schristos 				break;
14739e7bb71Schristos 		}
148df0952c6Schristos 		if (c == '.') {
149df0952c6Schristos 			/*
150df0952c6Schristos 			 * Internet format:
151df0952c6Schristos 			 *	a.b.c.d
152df0952c6Schristos 			 *	a.b.c	(with c treated as 16 bits)
153df0952c6Schristos 			 *	a.b	(with b treated as 24 bits)
154df0952c6Schristos 			 *	a	(with a treated as 32 bits)
155df0952c6Schristos 			 */
156df0952c6Schristos 			if (pp >= parts + 3)
15739e7bb71Schristos 				return (0);
158df0952c6Schristos 			*pp++ = val;
159df0952c6Schristos 			c = *++src;
160df0952c6Schristos 		} else
161df0952c6Schristos 			break;
162df0952c6Schristos 	}
163df0952c6Schristos 	/*
164df0952c6Schristos 	 * Check for trailing characters.
165df0952c6Schristos 	 */
166df0952c6Schristos 	if (c != '\0' && !isspace(c))
167df0952c6Schristos 		return (0);
168df0952c6Schristos 	/*
169df0952c6Schristos 	 * Concoct the address according to
170df0952c6Schristos 	 * the number of parts specified.
171df0952c6Schristos 	 */
172df0952c6Schristos 	n = pp - parts + 1;
173df0952c6Schristos 	/* inet_pton() takes dotted-quad only.  it does not take shorthand. */
174df0952c6Schristos 	if (pton && n != 4)
175df0952c6Schristos 		return (0);
176df0952c6Schristos 	switch (n) {
177df0952c6Schristos 
178df0952c6Schristos 	case 0:
179df0952c6Schristos 		return (0);		/* initial nondigit */
180df0952c6Schristos 
181df0952c6Schristos 	case 1:				/* a -- 32 bits */
182df0952c6Schristos 		break;
183df0952c6Schristos 
184df0952c6Schristos 	case 2:				/* a.b -- 8.24 bits */
185df0952c6Schristos 		if (parts[0] > 0xff || val > 0xffffff)
186df0952c6Schristos 			return (0);
187df0952c6Schristos 		val |= parts[0] << 24;
188df0952c6Schristos 		break;
189df0952c6Schristos 
190df0952c6Schristos 	case 3:				/* a.b.c -- 8.8.16 bits */
191df0952c6Schristos 		if ((parts[0] | parts[1]) > 0xff || val > 0xffff)
192df0952c6Schristos 			return (0);
193df0952c6Schristos 		val |= (parts[0] << 24) | (parts[1] << 16);
194df0952c6Schristos 		break;
195df0952c6Schristos 
196df0952c6Schristos 	case 4:				/* a.b.c.d -- 8.8.8.8 bits */
197df0952c6Schristos 		if ((parts[0] | parts[1] | parts[2] | val) > 0xff)
198df0952c6Schristos 			return (0);
199df0952c6Schristos 		val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
200df0952c6Schristos 		break;
201df0952c6Schristos 	}
202df0952c6Schristos 	if (dst) {
203df0952c6Schristos 		val = htonl(val);
204df0952c6Schristos 		memcpy(dst, &val, NS_INADDRSZ);
205df0952c6Schristos 	}
20639e7bb71Schristos 	return (1);
20739e7bb71Schristos }
20839e7bb71Schristos 
20939e7bb71Schristos /* int
21039e7bb71Schristos  * inet_pton6(src, dst)
21139e7bb71Schristos  *	convert presentation level address to network order binary form.
21239e7bb71Schristos  * return:
21339e7bb71Schristos  *	1 if `src' is a valid [RFC1884 2.2] address, else 0.
21439e7bb71Schristos  * notice:
21539e7bb71Schristos  *	(1) does not touch `dst' unless it's returning 1.
21639e7bb71Schristos  *	(2) :: in a full address is silently ignored.
21739e7bb71Schristos  * credit:
21839e7bb71Schristos  *	inspired by Mark Andrews.
21939e7bb71Schristos  * author:
22039e7bb71Schristos  *	Paul Vixie, 1996.
22139e7bb71Schristos  */
22239e7bb71Schristos static int
inet_pton6(const char * src,u_char * dst)223df0952c6Schristos inet_pton6(const char *src, u_char *dst)
22439e7bb71Schristos {
22539e7bb71Schristos 	static const char xdigits_l[] = "0123456789abcdef",
22639e7bb71Schristos 			  xdigits_u[] = "0123456789ABCDEF";
22739e7bb71Schristos 	u_char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
22839e7bb71Schristos 	const char *xdigits, *curtok;
229d73eb73dSchristos 	int ch, seen_xdigits;
23039e7bb71Schristos 	u_int val;
23139e7bb71Schristos 
232df0952c6Schristos 	_DIAGASSERT(src != NULL);
233df0952c6Schristos 	_DIAGASSERT(dst != NULL);
234df0952c6Schristos 
23539e7bb71Schristos 	memset((tp = tmp), '\0', NS_IN6ADDRSZ);
23639e7bb71Schristos 	endp = tp + NS_IN6ADDRSZ;
23739e7bb71Schristos 	colonp = NULL;
23839e7bb71Schristos 	/* Leading :: requires some special handling. */
23939e7bb71Schristos 	if (*src == ':')
24039e7bb71Schristos 		if (*++src != ':')
24139e7bb71Schristos 			return (0);
24239e7bb71Schristos 	curtok = src;
243d73eb73dSchristos 	seen_xdigits = 0;
24439e7bb71Schristos 	val = 0;
24539e7bb71Schristos 	while ((ch = *src++) != '\0') {
24639e7bb71Schristos 		const char *pch;
24739e7bb71Schristos 
24839e7bb71Schristos 		if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
24939e7bb71Schristos 			pch = strchr((xdigits = xdigits_u), ch);
25039e7bb71Schristos 		if (pch != NULL) {
25139e7bb71Schristos 			val <<= 4;
252*c5e820caSchristos 			val |= (int)(pch - xdigits);
253d73eb73dSchristos 			if (++seen_xdigits > 4)
25439e7bb71Schristos 				return (0);
25539e7bb71Schristos 			continue;
25639e7bb71Schristos 		}
25739e7bb71Schristos 		if (ch == ':') {
25839e7bb71Schristos 			curtok = src;
259d73eb73dSchristos 			if (!seen_xdigits) {
26039e7bb71Schristos 				if (colonp)
26139e7bb71Schristos 					return (0);
26239e7bb71Schristos 				colonp = tp;
26339e7bb71Schristos 				continue;
264df0952c6Schristos 			} else if (*src == '\0')
26539e7bb71Schristos 				return (0);
266fa373d4dSlukem 			if (tp + NS_INT16SZ > endp)
26739e7bb71Schristos 				return (0);
26839e7bb71Schristos 			*tp++ = (u_char) (val >> 8) & 0xff;
26939e7bb71Schristos 			*tp++ = (u_char) val & 0xff;
270d73eb73dSchristos 			seen_xdigits = 0;
27139e7bb71Schristos 			val = 0;
27239e7bb71Schristos 			continue;
27339e7bb71Schristos 		}
27439e7bb71Schristos 		if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
275df0952c6Schristos 		    inet_pton4(curtok, tp, 1) > 0) {
27639e7bb71Schristos 			tp += NS_INADDRSZ;
277d73eb73dSchristos 			seen_xdigits = 0;
278d73eb73dSchristos 			break;	/*%< '\\0' was seen by inet_pton4(). */
27939e7bb71Schristos 		}
28039e7bb71Schristos 		return (0);
28139e7bb71Schristos 	}
282d73eb73dSchristos 	if (seen_xdigits) {
28339e7bb71Schristos 		if (tp + NS_INT16SZ > endp)
28439e7bb71Schristos 			return (0);
28539e7bb71Schristos 		*tp++ = (u_char) (val >> 8) & 0xff;
28639e7bb71Schristos 		*tp++ = (u_char) val & 0xff;
28739e7bb71Schristos 	}
28839e7bb71Schristos 	if (colonp != NULL) {
28939e7bb71Schristos 		/*
29039e7bb71Schristos 		 * Since some memmove()'s erroneously fail to handle
29139e7bb71Schristos 		 * overlapping regions, we'll do the shift by hand.
29239e7bb71Schristos 		 */
293*c5e820caSchristos 		const ptrdiff_t n = tp - colonp;
29439e7bb71Schristos 		int i;
29539e7bb71Schristos 
29639e7bb71Schristos 		if (tp == endp)
29739e7bb71Schristos 			return (0);
29839e7bb71Schristos 		for (i = 1; i <= n; i++) {
29939e7bb71Schristos 			endp[- i] = colonp[n - i];
30039e7bb71Schristos 			colonp[n - i] = 0;
30139e7bb71Schristos 		}
30239e7bb71Schristos 		tp = endp;
30339e7bb71Schristos 	}
30439e7bb71Schristos 	if (tp != endp)
30539e7bb71Schristos 		return (0);
30639e7bb71Schristos 	memcpy(dst, tmp, NS_IN6ADDRSZ);
30739e7bb71Schristos 	return (1);
30839e7bb71Schristos }
309d73eb73dSchristos 
310d73eb73dSchristos /*! \file */
311