1*f14fb602SLionel Sambuc /* $NetBSD: inet_pton.c,v 1.8 2012/03/13 21:13:38 christos Exp $ */
22fe8fb19SBen Gras
32fe8fb19SBen Gras /*
42fe8fb19SBen Gras * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
52fe8fb19SBen Gras * Copyright (c) 1996,1999 by Internet Software Consortium.
62fe8fb19SBen Gras *
72fe8fb19SBen Gras * Permission to use, copy, modify, and distribute this software for any
82fe8fb19SBen Gras * purpose with or without fee is hereby granted, provided that the above
92fe8fb19SBen Gras * copyright notice and this permission notice appear in all copies.
102fe8fb19SBen Gras *
112fe8fb19SBen Gras * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
122fe8fb19SBen Gras * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
132fe8fb19SBen Gras * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
142fe8fb19SBen Gras * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
152fe8fb19SBen Gras * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
162fe8fb19SBen Gras * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
172fe8fb19SBen Gras * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
182fe8fb19SBen Gras */
192fe8fb19SBen Gras
202fe8fb19SBen Gras #include <sys/cdefs.h>
212fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
222fe8fb19SBen Gras #if 0
232fe8fb19SBen Gras static const char rcsid[] = "Id: inet_pton.c,v 1.5 2005/07/28 06:51:47 marka Exp";
242fe8fb19SBen Gras #else
25*f14fb602SLionel Sambuc __RCSID("$NetBSD: inet_pton.c,v 1.8 2012/03/13 21:13:38 christos Exp $");
262fe8fb19SBen Gras #endif
272fe8fb19SBen Gras #endif /* LIBC_SCCS and not lint */
282fe8fb19SBen Gras
292fe8fb19SBen Gras #include "port_before.h"
302fe8fb19SBen Gras
312fe8fb19SBen Gras #include "namespace.h"
322fe8fb19SBen Gras #include <sys/param.h>
332fe8fb19SBen Gras #include <sys/types.h>
342fe8fb19SBen Gras #include <sys/socket.h>
352fe8fb19SBen Gras #include <netinet/in.h>
362fe8fb19SBen Gras #include <arpa/inet.h>
372fe8fb19SBen Gras #include <arpa/nameser.h>
38*f14fb602SLionel Sambuc #include <stddef.h>
392fe8fb19SBen Gras #include <string.h>
402fe8fb19SBen Gras #include <assert.h>
412fe8fb19SBen Gras #include <ctype.h>
422fe8fb19SBen Gras #include <errno.h>
432fe8fb19SBen Gras
442fe8fb19SBen Gras #include "port_after.h"
452fe8fb19SBen Gras
462fe8fb19SBen Gras #ifdef __weak_alias
472fe8fb19SBen Gras __weak_alias(inet_pton,_inet_pton)
482fe8fb19SBen Gras #endif
492fe8fb19SBen Gras
502fe8fb19SBen Gras /*%
512fe8fb19SBen Gras * WARNING: Don't even consider trying to compile this on a system where
522fe8fb19SBen Gras * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.
532fe8fb19SBen Gras */
542fe8fb19SBen Gras
552fe8fb19SBen Gras static int inet_pton4(const char *src, u_char *dst, int pton);
562fe8fb19SBen Gras static int inet_pton6(const char *src, u_char *dst);
572fe8fb19SBen Gras
582fe8fb19SBen Gras /* int
592fe8fb19SBen Gras * inet_pton(af, src, dst)
602fe8fb19SBen Gras * convert from presentation format (which usually means ASCII printable)
612fe8fb19SBen Gras * to network format (which is usually some kind of binary format).
622fe8fb19SBen Gras * return:
632fe8fb19SBen Gras * 1 if the address was valid for the specified address family
642fe8fb19SBen Gras * 0 if the address wasn't valid (`dst' is untouched in this case)
652fe8fb19SBen Gras * -1 if some other error occurred (`dst' is untouched in this case, too)
662fe8fb19SBen Gras * author:
672fe8fb19SBen Gras * Paul Vixie, 1996.
682fe8fb19SBen Gras */
692fe8fb19SBen Gras int
inet_pton(int af,const char * src,void * dst)702fe8fb19SBen Gras inet_pton(int af, const char *src, void *dst)
712fe8fb19SBen Gras {
722fe8fb19SBen Gras
732fe8fb19SBen Gras _DIAGASSERT(src != NULL);
742fe8fb19SBen Gras _DIAGASSERT(dst != NULL);
752fe8fb19SBen Gras
762fe8fb19SBen Gras switch (af) {
772fe8fb19SBen Gras case AF_INET:
782fe8fb19SBen Gras return (inet_pton4(src, dst, 1));
792fe8fb19SBen Gras case AF_INET6:
802fe8fb19SBen Gras return (inet_pton6(src, dst));
812fe8fb19SBen Gras default:
822fe8fb19SBen Gras errno = EAFNOSUPPORT;
832fe8fb19SBen Gras return (-1);
842fe8fb19SBen Gras }
852fe8fb19SBen Gras /* NOTREACHED */
862fe8fb19SBen Gras }
872fe8fb19SBen Gras
882fe8fb19SBen Gras /* int
892fe8fb19SBen Gras * inet_pton4(src, dst, pton)
902fe8fb19SBen Gras * when last arg is 0: inet_aton(). with hexadecimal, octal and shorthand.
912fe8fb19SBen Gras * when last arg is 1: inet_pton(). decimal dotted-quad only.
922fe8fb19SBen Gras * return:
932fe8fb19SBen Gras * 1 if `src' is a valid input, else 0.
942fe8fb19SBen Gras * notice:
952fe8fb19SBen Gras * does not touch `dst' unless it's returning 1.
962fe8fb19SBen Gras * author:
972fe8fb19SBen Gras * Paul Vixie, 1996.
982fe8fb19SBen Gras */
992fe8fb19SBen Gras static int
inet_pton4(const char * src,u_char * dst,int pton)1002fe8fb19SBen Gras inet_pton4(const char *src, u_char *dst, int pton)
1012fe8fb19SBen Gras {
1022fe8fb19SBen Gras u_int32_t val;
1032fe8fb19SBen Gras u_int digit, base;
104*f14fb602SLionel Sambuc ptrdiff_t n;
1052fe8fb19SBen Gras unsigned char c;
1062fe8fb19SBen Gras u_int parts[4];
107*f14fb602SLionel Sambuc u_int *pp = parts;
1082fe8fb19SBen Gras
1092fe8fb19SBen Gras _DIAGASSERT(src != NULL);
1102fe8fb19SBen Gras _DIAGASSERT(dst != NULL);
1112fe8fb19SBen Gras
1122fe8fb19SBen Gras c = *src;
1132fe8fb19SBen Gras for (;;) {
1142fe8fb19SBen Gras /*
1152fe8fb19SBen Gras * Collect number up to ``.''.
1162fe8fb19SBen Gras * Values are specified as for C:
1172fe8fb19SBen Gras * 0x=hex, 0=octal, isdigit=decimal.
1182fe8fb19SBen Gras */
1192fe8fb19SBen Gras if (!isdigit(c))
1202fe8fb19SBen Gras return (0);
1212fe8fb19SBen Gras val = 0; base = 10;
1222fe8fb19SBen Gras if (c == '0') {
1232fe8fb19SBen Gras c = *++src;
1242fe8fb19SBen Gras if (c == 'x' || c == 'X')
1252fe8fb19SBen Gras base = 16, c = *++src;
1262fe8fb19SBen Gras else if (isdigit(c) && c != '9')
1272fe8fb19SBen Gras base = 8;
1282fe8fb19SBen Gras }
1292fe8fb19SBen Gras /* inet_pton() takes decimal only */
1302fe8fb19SBen Gras if (pton && base != 10)
1312fe8fb19SBen Gras return (0);
1322fe8fb19SBen Gras for (;;) {
1332fe8fb19SBen Gras if (isdigit(c)) {
1342fe8fb19SBen Gras digit = c - '0';
1352fe8fb19SBen Gras if (digit >= base)
1362fe8fb19SBen Gras break;
1372fe8fb19SBen Gras val = (val * base) + digit;
1382fe8fb19SBen Gras c = *++src;
1392fe8fb19SBen Gras } else if (base == 16 && isxdigit(c)) {
1402fe8fb19SBen Gras digit = c + 10 - (islower(c) ? 'a' : 'A');
1412fe8fb19SBen Gras if (digit >= 16)
1422fe8fb19SBen Gras break;
1432fe8fb19SBen Gras val = (val << 4) | digit;
1442fe8fb19SBen Gras c = *++src;
1452fe8fb19SBen Gras } else
1462fe8fb19SBen Gras break;
1472fe8fb19SBen Gras }
1482fe8fb19SBen Gras if (c == '.') {
1492fe8fb19SBen Gras /*
1502fe8fb19SBen Gras * Internet format:
1512fe8fb19SBen Gras * a.b.c.d
1522fe8fb19SBen Gras * a.b.c (with c treated as 16 bits)
1532fe8fb19SBen Gras * a.b (with b treated as 24 bits)
1542fe8fb19SBen Gras * a (with a treated as 32 bits)
1552fe8fb19SBen Gras */
1562fe8fb19SBen Gras if (pp >= parts + 3)
1572fe8fb19SBen Gras return (0);
1582fe8fb19SBen Gras *pp++ = val;
1592fe8fb19SBen Gras c = *++src;
1602fe8fb19SBen Gras } else
1612fe8fb19SBen Gras break;
1622fe8fb19SBen Gras }
1632fe8fb19SBen Gras /*
1642fe8fb19SBen Gras * Check for trailing characters.
1652fe8fb19SBen Gras */
1662fe8fb19SBen Gras if (c != '\0' && !isspace(c))
1672fe8fb19SBen Gras return (0);
1682fe8fb19SBen Gras /*
1692fe8fb19SBen Gras * Concoct the address according to
1702fe8fb19SBen Gras * the number of parts specified.
1712fe8fb19SBen Gras */
1722fe8fb19SBen Gras n = pp - parts + 1;
1732fe8fb19SBen Gras /* inet_pton() takes dotted-quad only. it does not take shorthand. */
1742fe8fb19SBen Gras if (pton && n != 4)
1752fe8fb19SBen Gras return (0);
1762fe8fb19SBen Gras switch (n) {
1772fe8fb19SBen Gras
1782fe8fb19SBen Gras case 0:
1792fe8fb19SBen Gras return (0); /* initial nondigit */
1802fe8fb19SBen Gras
1812fe8fb19SBen Gras case 1: /* a -- 32 bits */
1822fe8fb19SBen Gras break;
1832fe8fb19SBen Gras
1842fe8fb19SBen Gras case 2: /* a.b -- 8.24 bits */
1852fe8fb19SBen Gras if (parts[0] > 0xff || val > 0xffffff)
1862fe8fb19SBen Gras return (0);
1872fe8fb19SBen Gras val |= parts[0] << 24;
1882fe8fb19SBen Gras break;
1892fe8fb19SBen Gras
1902fe8fb19SBen Gras case 3: /* a.b.c -- 8.8.16 bits */
1912fe8fb19SBen Gras if ((parts[0] | parts[1]) > 0xff || val > 0xffff)
1922fe8fb19SBen Gras return (0);
1932fe8fb19SBen Gras val |= (parts[0] << 24) | (parts[1] << 16);
1942fe8fb19SBen Gras break;
1952fe8fb19SBen Gras
1962fe8fb19SBen Gras case 4: /* a.b.c.d -- 8.8.8.8 bits */
1972fe8fb19SBen Gras if ((parts[0] | parts[1] | parts[2] | val) > 0xff)
1982fe8fb19SBen Gras return (0);
1992fe8fb19SBen Gras val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
2002fe8fb19SBen Gras break;
2012fe8fb19SBen Gras }
2022fe8fb19SBen Gras if (dst) {
2032fe8fb19SBen Gras val = htonl(val);
2042fe8fb19SBen Gras memcpy(dst, &val, NS_INADDRSZ);
2052fe8fb19SBen Gras }
2062fe8fb19SBen Gras return (1);
2072fe8fb19SBen Gras }
2082fe8fb19SBen Gras
2092fe8fb19SBen Gras /* int
2102fe8fb19SBen Gras * inet_pton6(src, dst)
2112fe8fb19SBen Gras * convert presentation level address to network order binary form.
2122fe8fb19SBen Gras * return:
2132fe8fb19SBen Gras * 1 if `src' is a valid [RFC1884 2.2] address, else 0.
2142fe8fb19SBen Gras * notice:
2152fe8fb19SBen Gras * (1) does not touch `dst' unless it's returning 1.
2162fe8fb19SBen Gras * (2) :: in a full address is silently ignored.
2172fe8fb19SBen Gras * credit:
2182fe8fb19SBen Gras * inspired by Mark Andrews.
2192fe8fb19SBen Gras * author:
2202fe8fb19SBen Gras * Paul Vixie, 1996.
2212fe8fb19SBen Gras */
2222fe8fb19SBen Gras static int
inet_pton6(const char * src,u_char * dst)2232fe8fb19SBen Gras inet_pton6(const char *src, u_char *dst)
2242fe8fb19SBen Gras {
2252fe8fb19SBen Gras static const char xdigits_l[] = "0123456789abcdef",
2262fe8fb19SBen Gras xdigits_u[] = "0123456789ABCDEF";
2272fe8fb19SBen Gras u_char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
2282fe8fb19SBen Gras const char *xdigits, *curtok;
2292fe8fb19SBen Gras int ch, seen_xdigits;
2302fe8fb19SBen Gras u_int val;
2312fe8fb19SBen Gras
2322fe8fb19SBen Gras _DIAGASSERT(src != NULL);
2332fe8fb19SBen Gras _DIAGASSERT(dst != NULL);
2342fe8fb19SBen Gras
2352fe8fb19SBen Gras memset((tp = tmp), '\0', NS_IN6ADDRSZ);
2362fe8fb19SBen Gras endp = tp + NS_IN6ADDRSZ;
2372fe8fb19SBen Gras colonp = NULL;
2382fe8fb19SBen Gras /* Leading :: requires some special handling. */
2392fe8fb19SBen Gras if (*src == ':')
2402fe8fb19SBen Gras if (*++src != ':')
2412fe8fb19SBen Gras return (0);
2422fe8fb19SBen Gras curtok = src;
2432fe8fb19SBen Gras seen_xdigits = 0;
2442fe8fb19SBen Gras val = 0;
2452fe8fb19SBen Gras while ((ch = *src++) != '\0') {
2462fe8fb19SBen Gras const char *pch;
2472fe8fb19SBen Gras
2482fe8fb19SBen Gras if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
2492fe8fb19SBen Gras pch = strchr((xdigits = xdigits_u), ch);
2502fe8fb19SBen Gras if (pch != NULL) {
2512fe8fb19SBen Gras val <<= 4;
252*f14fb602SLionel Sambuc val |= (int)(pch - xdigits);
2532fe8fb19SBen Gras if (++seen_xdigits > 4)
2542fe8fb19SBen Gras return (0);
2552fe8fb19SBen Gras continue;
2562fe8fb19SBen Gras }
2572fe8fb19SBen Gras if (ch == ':') {
2582fe8fb19SBen Gras curtok = src;
2592fe8fb19SBen Gras if (!seen_xdigits) {
2602fe8fb19SBen Gras if (colonp)
2612fe8fb19SBen Gras return (0);
2622fe8fb19SBen Gras colonp = tp;
2632fe8fb19SBen Gras continue;
2642fe8fb19SBen Gras } else if (*src == '\0')
2652fe8fb19SBen Gras return (0);
2662fe8fb19SBen Gras if (tp + NS_INT16SZ > endp)
2672fe8fb19SBen Gras return (0);
2682fe8fb19SBen Gras *tp++ = (u_char) (val >> 8) & 0xff;
2692fe8fb19SBen Gras *tp++ = (u_char) val & 0xff;
2702fe8fb19SBen Gras seen_xdigits = 0;
2712fe8fb19SBen Gras val = 0;
2722fe8fb19SBen Gras continue;
2732fe8fb19SBen Gras }
2742fe8fb19SBen Gras if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
2752fe8fb19SBen Gras inet_pton4(curtok, tp, 1) > 0) {
2762fe8fb19SBen Gras tp += NS_INADDRSZ;
2772fe8fb19SBen Gras seen_xdigits = 0;
2782fe8fb19SBen Gras break; /*%< '\\0' was seen by inet_pton4(). */
2792fe8fb19SBen Gras }
2802fe8fb19SBen Gras return (0);
2812fe8fb19SBen Gras }
2822fe8fb19SBen Gras if (seen_xdigits) {
2832fe8fb19SBen Gras if (tp + NS_INT16SZ > endp)
2842fe8fb19SBen Gras return (0);
2852fe8fb19SBen Gras *tp++ = (u_char) (val >> 8) & 0xff;
2862fe8fb19SBen Gras *tp++ = (u_char) val & 0xff;
2872fe8fb19SBen Gras }
2882fe8fb19SBen Gras if (colonp != NULL) {
2892fe8fb19SBen Gras /*
2902fe8fb19SBen Gras * Since some memmove()'s erroneously fail to handle
2912fe8fb19SBen Gras * overlapping regions, we'll do the shift by hand.
2922fe8fb19SBen Gras */
293*f14fb602SLionel Sambuc const ptrdiff_t n = tp - colonp;
2942fe8fb19SBen Gras int i;
2952fe8fb19SBen Gras
2962fe8fb19SBen Gras if (tp == endp)
2972fe8fb19SBen Gras return (0);
2982fe8fb19SBen Gras for (i = 1; i <= n; i++) {
2992fe8fb19SBen Gras endp[- i] = colonp[n - i];
3002fe8fb19SBen Gras colonp[n - i] = 0;
3012fe8fb19SBen Gras }
3022fe8fb19SBen Gras tp = endp;
3032fe8fb19SBen Gras }
3042fe8fb19SBen Gras if (tp != endp)
3052fe8fb19SBen Gras return (0);
3062fe8fb19SBen Gras memcpy(dst, tmp, NS_IN6ADDRSZ);
3072fe8fb19SBen Gras return (1);
3082fe8fb19SBen Gras }
3092fe8fb19SBen Gras
3102fe8fb19SBen Gras /*! \file */
311