Home | History | Annotate | Line # | Download | only in inet
inet_pton.c revision 1.1
      1 /*	$NetBSD: inet_pton.c,v 1.1 2004/05/20 22:29:02 christos Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
      5  * Copyright (c) 1996,1999 by Internet Software Consortium.
      6  *
      7  * Permission to use, copy, modify, and distribute this software for any
      8  * purpose with or without fee is hereby granted, provided that the above
      9  * copyright notice and this permission notice appear in all copies.
     10  *
     11  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
     12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     13  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
     14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
     17  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     18  */
     19 
     20 #if defined(LIBC_SCCS) && !defined(lint)
     21 static const char rcsid[] = "Id: inet_pton.c,v 1.2.206.1 2004/03/09 08:33:33 marka Exp";
     22 #endif /* LIBC_SCCS and not lint */
     23 
     24 #include "port_before.h"
     25 #include <sys/param.h>
     26 #include <sys/types.h>
     27 #include <sys/socket.h>
     28 #include <netinet/in.h>
     29 #include <arpa/inet.h>
     30 #include <arpa/nameser.h>
     31 #include <string.h>
     32 #include <errno.h>
     33 #include "port_after.h"
     34 
     35 /*
     36  * WARNING: Don't even consider trying to compile this on a system where
     37  * sizeof(int) < 4.  sizeof(int) > 4 is fine; all the world's not a VAX.
     38  */
     39 
     40 static int	inet_pton4 __P((const char *src, u_char *dst));
     41 static int	inet_pton6 __P((const char *src, u_char *dst));
     42 
     43 /* int
     44  * inet_pton(af, src, dst)
     45  *	convert from presentation format (which usually means ASCII printable)
     46  *	to network format (which is usually some kind of binary format).
     47  * return:
     48  *	1 if the address was valid for the specified address family
     49  *	0 if the address wasn't valid (`dst' is untouched in this case)
     50  *	-1 if some other error occurred (`dst' is untouched in this case, too)
     51  * author:
     52  *	Paul Vixie, 1996.
     53  */
     54 int
     55 inet_pton(af, src, dst)
     56 	int af;
     57 	const char *src;
     58 	void *dst;
     59 {
     60 	switch (af) {
     61 	case AF_INET:
     62 		return (inet_pton4(src, dst));
     63 	case AF_INET6:
     64 		return (inet_pton6(src, dst));
     65 	default:
     66 		errno = EAFNOSUPPORT;
     67 		return (-1);
     68 	}
     69 	/* NOTREACHED */
     70 }
     71 
     72 /* int
     73  * inet_pton4(src, dst)
     74  *	like inet_aton() but without all the hexadecimal and shorthand.
     75  * return:
     76  *	1 if `src' is a valid dotted quad, else 0.
     77  * notice:
     78  *	does not touch `dst' unless it's returning 1.
     79  * author:
     80  *	Paul Vixie, 1996.
     81  */
     82 static int
     83 inet_pton4(src, dst)
     84 	const char *src;
     85 	u_char *dst;
     86 {
     87 	static const char digits[] = "0123456789";
     88 	int saw_digit, octets, ch;
     89 	u_char tmp[NS_INADDRSZ], *tp;
     90 
     91 	saw_digit = 0;
     92 	octets = 0;
     93 	*(tp = tmp) = 0;
     94 	while ((ch = *src++) != '\0') {
     95 		const char *pch;
     96 
     97 		if ((pch = strchr(digits, ch)) != NULL) {
     98 			u_int new = *tp * 10 + (pch - digits);
     99 
    100 			if (saw_digit && *tp == 0)
    101 				return (0);
    102 			if (new > 255)
    103 				return (0);
    104 			*tp = new;
    105 			if (!saw_digit) {
    106 				if (++octets > 4)
    107 					return (0);
    108 				saw_digit = 1;
    109 			}
    110 		} else if (ch == '.' && saw_digit) {
    111 			if (octets == 4)
    112 				return (0);
    113 			*++tp = 0;
    114 			saw_digit = 0;
    115 		} else
    116 			return (0);
    117 	}
    118 	if (octets < 4)
    119 		return (0);
    120 	memcpy(dst, tmp, NS_INADDRSZ);
    121 	return (1);
    122 }
    123 
    124 /* int
    125  * inet_pton6(src, dst)
    126  *	convert presentation level address to network order binary form.
    127  * return:
    128  *	1 if `src' is a valid [RFC1884 2.2] address, else 0.
    129  * notice:
    130  *	(1) does not touch `dst' unless it's returning 1.
    131  *	(2) :: in a full address is silently ignored.
    132  * credit:
    133  *	inspired by Mark Andrews.
    134  * author:
    135  *	Paul Vixie, 1996.
    136  */
    137 static int
    138 inet_pton6(src, dst)
    139 	const char *src;
    140 	u_char *dst;
    141 {
    142 	static const char xdigits_l[] = "0123456789abcdef",
    143 			  xdigits_u[] = "0123456789ABCDEF";
    144 	u_char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
    145 	const char *xdigits, *curtok;
    146 	int ch, saw_xdigit;
    147 	u_int val;
    148 
    149 	memset((tp = tmp), '\0', NS_IN6ADDRSZ);
    150 	endp = tp + NS_IN6ADDRSZ;
    151 	colonp = NULL;
    152 	/* Leading :: requires some special handling. */
    153 	if (*src == ':')
    154 		if (*++src != ':')
    155 			return (0);
    156 	curtok = src;
    157 	saw_xdigit = 0;
    158 	val = 0;
    159 	while ((ch = *src++) != '\0') {
    160 		const char *pch;
    161 
    162 		if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
    163 			pch = strchr((xdigits = xdigits_u), ch);
    164 		if (pch != NULL) {
    165 			val <<= 4;
    166 			val |= (pch - xdigits);
    167 			if (val > 0xffff)
    168 				return (0);
    169 			saw_xdigit = 1;
    170 			continue;
    171 		}
    172 		if (ch == ':') {
    173 			curtok = src;
    174 			if (!saw_xdigit) {
    175 				if (colonp)
    176 					return (0);
    177 				colonp = tp;
    178 				continue;
    179 			} else if (*src == '\0') {
    180 				return (0);
    181 			}
    182 			if (tp + NS_INT16SZ > endp)
    183 				return (0);
    184 			*tp++ = (u_char) (val >> 8) & 0xff;
    185 			*tp++ = (u_char) val & 0xff;
    186 			saw_xdigit = 0;
    187 			val = 0;
    188 			continue;
    189 		}
    190 		if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
    191 		    inet_pton4(curtok, tp) > 0) {
    192 			tp += NS_INADDRSZ;
    193 			saw_xdigit = 0;
    194 			break;	/* '\0' was seen by inet_pton4(). */
    195 		}
    196 		return (0);
    197 	}
    198 	if (saw_xdigit) {
    199 		if (tp + NS_INT16SZ > endp)
    200 			return (0);
    201 		*tp++ = (u_char) (val >> 8) & 0xff;
    202 		*tp++ = (u_char) val & 0xff;
    203 	}
    204 	if (colonp != NULL) {
    205 		/*
    206 		 * Since some memmove()'s erroneously fail to handle
    207 		 * overlapping regions, we'll do the shift by hand.
    208 		 */
    209 		const int n = tp - colonp;
    210 		int i;
    211 
    212 		if (tp == endp)
    213 			return (0);
    214 		for (i = 1; i <= n; i++) {
    215 			endp[- i] = colonp[n - i];
    216 			colonp[n - i] = 0;
    217 		}
    218 		tp = endp;
    219 	}
    220 	if (tp != endp)
    221 		return (0);
    222 	memcpy(dst, tmp, NS_IN6ADDRSZ);
    223 	return (1);
    224 }
    225