179d2dfddfr/*
279d2dfddfr * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
379d2dfddfr * Copyright (c) 1996,1999 by Internet Software Consortium.
479d2dfddfr *
579d2dfddfr * Permission to use, copy, modify, and distribute this software for any
679d2dfddfr * purpose with or without fee is hereby granted, provided that the above
779d2dfddfr * copyright notice and this permission notice appear in all copies.
879d2dfddfr *
979d2dfddfr * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
1079d2dfddfr * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1179d2dfddfr * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
1279d2dfddfr * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1379d2dfddfr * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1479d2dfddfr * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
1579d2dfddfr * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1679d2dfddfr */
1779d2dfddfr
1879d2dfddfr#if defined(LIBC_SCCS) && !defined(lint)
1979d2dfddfrstatic const char rcsid[] = "$Id: inet_pton.c,v 1.3.18.2 2005/07/28 07:38:07 marka Exp $";
2079d2dfddfr#endif /* LIBC_SCCS and not lint */
2179d2dfddfr#include <sys/cdefs.h>
2279d2dfddfr__FBSDID("$FreeBSD$");
2379d2dfddfr
2479d2dfddfr#include <sys/param.h>
2579d2dfddfr#include <sys/socket.h>
2679d2dfddfr#include <sys/systm.h>
2779d2dfddfr
28278493aattilio#include <netinet/in.h>
293d7f9d1dfr
3079d2dfddfr/*%
3179d2dfddfr * WARNING: Don't even consider trying to compile this on a system where
3279d2dfddfr * sizeof(int) < 4.  sizeof(int) > 4 is fine; all the world's not a VAX.
3379d2dfddfr */
3479d2dfddfr
3579d2dfddfrstatic int	inet_pton4(const char *src, u_char *dst);
3679d2dfddfrstatic int	inet_pton6(const char *src, u_char *dst);
3779d2dfddfr
3879d2dfddfr/* int
3979d2dfddfr * inet_pton(af, src, dst)
4079d2dfddfr *	convert from presentation format (which usually means ASCII printable)
4179d2dfddfr *	to network format (which is usually some kind of binary format).
4279d2dfddfr * return:
4379d2dfddfr *	1 if the address was valid for the specified address family
4479d2dfddfr *	0 if the address wasn't valid (`dst' is untouched in this case)
4579d2dfddfr *	-1 if some other error occurred (`dst' is untouched in this case, too)
4679d2dfddfr * author:
4779d2dfddfr *	Paul Vixie, 1996.
4879d2dfddfr */
4979d2dfddfrint
50278493aattilioinet_pton(int af, const char *src, void *dst)
5179d2dfddfr{
5279d2dfddfr	switch (af) {
5379d2dfddfr	case AF_INET:
5479d2dfddfr		return (inet_pton4(src, dst));
5579d2dfddfr	case AF_INET6:
5679d2dfddfr		return (inet_pton6(src, dst));
5779d2dfddfr	default:
5879d2dfddfr		return (-1);
5979d2dfddfr	}
6079d2dfddfr	/* NOTREACHED */
6179d2dfddfr}
6279d2dfddfr
6379d2dfddfr/* int
6479d2dfddfr * inet_pton4(src, dst)
6579d2dfddfr *	like inet_aton() but without all the hexadecimal and shorthand.
6679d2dfddfr * return:
6779d2dfddfr *	1 if `src' is a valid dotted quad, else 0.
6879d2dfddfr * notice:
6979d2dfddfr *	does not touch `dst' unless it's returning 1.
7079d2dfddfr * author:
7179d2dfddfr *	Paul Vixie, 1996.
7279d2dfddfr */
7379d2dfddfrstatic int
7479d2dfddfrinet_pton4(const char *src, u_char *dst)
7579d2dfddfr{
7679d2dfddfr	static const char digits[] = "0123456789";
7779d2dfddfr	int saw_digit, octets, ch;
7879d2dfddfr#define NS_INADDRSZ	4
7979d2dfddfr	u_char tmp[NS_INADDRSZ], *tp;
8079d2dfddfr
8179d2dfddfr	saw_digit = 0;
8279d2dfddfr	octets = 0;
8379d2dfddfr	*(tp = tmp) = 0;
8479d2dfddfr	while ((ch = *src++) != '\0') {
8579d2dfddfr		const char *pch;
8679d2dfddfr
8779d2dfddfr		if ((pch = strchr(digits, ch)) != NULL) {
8879d2dfddfr			u_int new = *tp * 10 + (pch - digits);
8979d2dfddfr
9079d2dfddfr			if (saw_digit && *tp == 0)
9179d2dfddfr				return (0);
9279d2dfddfr			if (new > 255)
9379d2dfddfr				return (0);
9479d2dfddfr			*tp = new;
9579d2dfddfr			if (!saw_digit) {
9679d2dfddfr				if (++octets > 4)
9779d2dfddfr					return (0);
9879d2dfddfr				saw_digit = 1;
9979d2dfddfr			}
10079d2dfddfr		} else if (ch == '.' && saw_digit) {
10179d2dfddfr			if (octets == 4)
10279d2dfddfr				return (0);
10379d2dfddfr			*++tp = 0;
10479d2dfddfr			saw_digit = 0;
10579d2dfddfr		} else
10679d2dfddfr			return (0);
10779d2dfddfr	}
10879d2dfddfr	if (octets < 4)
10979d2dfddfr		return (0);
11079d2dfddfr	memcpy(dst, tmp, NS_INADDRSZ);
11179d2dfddfr	return (1);
11279d2dfddfr}
11379d2dfddfr
11479d2dfddfr/* int
11579d2dfddfr * inet_pton6(src, dst)
11679d2dfddfr *	convert presentation level address to network order binary form.
11779d2dfddfr * return:
11879d2dfddfr *	1 if `src' is a valid [RFC1884 2.2] address, else 0.
11979d2dfddfr * notice:
12079d2dfddfr *	(1) does not touch `dst' unless it's returning 1.
12179d2dfddfr *	(2) :: in a full address is silently ignored.
12279d2dfddfr * credit:
12379d2dfddfr *	inspired by Mark Andrews.
12479d2dfddfr * author:
12579d2dfddfr *	Paul Vixie, 1996.
12679d2dfddfr */
12779d2dfddfrstatic int
12879d2dfddfrinet_pton6(const char *src, u_char *dst)
12979d2dfddfr{
13079d2dfddfr	static const char xdigits_l[] = "0123456789abcdef",
13179d2dfddfr			  xdigits_u[] = "0123456789ABCDEF";
13279d2dfddfr#define NS_IN6ADDRSZ	16
13379d2dfddfr#define NS_INT16SZ	2
13479d2dfddfr	u_char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
13579d2dfddfr	const char *xdigits, *curtok;
13679d2dfddfr	int ch, seen_xdigits;
13779d2dfddfr	u_int val;
13879d2dfddfr
13979d2dfddfr	memset((tp = tmp), '\0', NS_IN6ADDRSZ);
14079d2dfddfr	endp = tp + NS_IN6ADDRSZ;
14179d2dfddfr	colonp = NULL;
14279d2dfddfr	/* Leading :: requires some special handling. */
14379d2dfddfr	if (*src == ':')
14479d2dfddfr		if (*++src != ':')
14579d2dfddfr			return (0);
14679d2dfddfr	curtok = src;
14779d2dfddfr	seen_xdigits = 0;
14879d2dfddfr	val = 0;
14979d2dfddfr	while ((ch = *src++) != '\0') {
15079d2dfddfr		const char *pch;
15179d2dfddfr
15279d2dfddfr		if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
15379d2dfddfr			pch = strchr((xdigits = xdigits_u), ch);
15479d2dfddfr		if (pch != NULL) {
15579d2dfddfr			val <<= 4;
15679d2dfddfr			val |= (pch - xdigits);
15779d2dfddfr			if (++seen_xdigits > 4)
15879d2dfddfr				return (0);
15979d2dfddfr			continue;
16079d2dfddfr		}
16179d2dfddfr		if (ch == ':') {
16279d2dfddfr			curtok = src;
16379d2dfddfr			if (!seen_xdigits) {
16479d2dfddfr				if (colonp)
16579d2dfddfr					return (0);
16679d2dfddfr				colonp = tp;
16779d2dfddfr				continue;
16879d2dfddfr			} else if (*src == '\0') {
16979d2dfddfr				return (0);
17079d2dfddfr			}
17179d2dfddfr			if (tp + NS_INT16SZ > endp)
17279d2dfddfr				return (0);
17379d2dfddfr			*tp++ = (u_char) (val >> 8) & 0xff;
17479d2dfddfr			*tp++ = (u_char) val & 0xff;
17579d2dfddfr			seen_xdigits = 0;
17679d2dfddfr			val = 0;
17779d2dfddfr			continue;
17879d2dfddfr		}
17979d2dfddfr		if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
18079d2dfddfr		    inet_pton4(curtok, tp) > 0) {
18179d2dfddfr			tp += NS_INADDRSZ;
18279d2dfddfr			seen_xdigits = 0;
18379d2dfddfr			break;	/*%< '\\0' was seen by inet_pton4(). */
18479d2dfddfr		}
18579d2dfddfr		return (0);
18679d2dfddfr	}
18779d2dfddfr	if (seen_xdigits) {
18879d2dfddfr		if (tp + NS_INT16SZ > endp)
18979d2dfddfr			return (0);
19079d2dfddfr		*tp++ = (u_char) (val >> 8) & 0xff;
19179d2dfddfr		*tp++ = (u_char) val & 0xff;
19279d2dfddfr	}
19379d2dfddfr	if (colonp != NULL) {
19479d2dfddfr		/*
19579d2dfddfr		 * Since some memmove()'s erroneously fail to handle
19679d2dfddfr		 * overlapping regions, we'll do the shift by hand.
19779d2dfddfr		 */
19879d2dfddfr		const int n = tp - colonp;
19979d2dfddfr		int i;
20079d2dfddfr
20179d2dfddfr		if (tp == endp)
20279d2dfddfr			return (0);
20379d2dfddfr		for (i = 1; i <= n; i++) {
20479d2dfddfr			endp[- i] = colonp[n - i];
20579d2dfddfr			colonp[n - i] = 0;
20679d2dfddfr		}
20779d2dfddfr		tp = endp;
20879d2dfddfr	}
20979d2dfddfr	if (tp != endp)
21079d2dfddfr		return (0);
21179d2dfddfr	memcpy(dst, tmp, NS_IN6ADDRSZ);
21279d2dfddfr	return (1);
21379d2dfddfr}
21479d2dfddfr
21579d2dfddfr/*! \file */
216