1*f14fb602SLionel Sambuc /* $NetBSD: inet_cidr_pton.c,v 1.8 2012/03/20 17:08:13 matt Exp $ */
22fe8fb19SBen Gras
32fe8fb19SBen Gras /*
42fe8fb19SBen Gras * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
52fe8fb19SBen Gras * Copyright (c) 1998,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_cidr_pton.c,v 1.6 2005/04/27 04:56:19 sra Exp";
242fe8fb19SBen Gras #else
25*f14fb602SLionel Sambuc __RCSID("$NetBSD: inet_cidr_pton.c,v 1.8 2012/03/20 17:08:13 matt Exp $");
262fe8fb19SBen Gras #endif
272fe8fb19SBen Gras #endif
282fe8fb19SBen Gras
292fe8fb19SBen Gras #include "port_before.h"
302fe8fb19SBen Gras
312fe8fb19SBen Gras #include "namespace.h"
322fe8fb19SBen Gras #include <sys/types.h>
332fe8fb19SBen Gras #include <sys/socket.h>
342fe8fb19SBen Gras #include <netinet/in.h>
352fe8fb19SBen Gras #include <arpa/nameser.h>
362fe8fb19SBen Gras #include <arpa/inet.h>
372fe8fb19SBen Gras
382fe8fb19SBen Gras #include <isc/assertions.h>
392fe8fb19SBen Gras #include <ctype.h>
402fe8fb19SBen Gras #include <errno.h>
412fe8fb19SBen Gras #include <stdio.h>
422fe8fb19SBen Gras #include <string.h>
43*f14fb602SLionel Sambuc #include <stddef.h>
442fe8fb19SBen Gras #include <stdlib.h>
452fe8fb19SBen Gras
462fe8fb19SBen Gras #include "port_after.h"
472fe8fb19SBen Gras
482fe8fb19SBen Gras #ifdef SPRINTF_CHAR
492fe8fb19SBen Gras # define SPRINTF(x) strlen(sprintf/**/x)
502fe8fb19SBen Gras #else
512fe8fb19SBen Gras # define SPRINTF(x) ((size_t)sprintf x)
522fe8fb19SBen Gras #endif
532fe8fb19SBen Gras
542fe8fb19SBen Gras #ifdef __weak_alias
552fe8fb19SBen Gras __weak_alias(inet_cidr_pton,_inet_cidr_pton)
562fe8fb19SBen Gras #endif
572fe8fb19SBen Gras
58*f14fb602SLionel Sambuc static int inet_cidr_pton_ipv4(const char *src, u_char *dst,
59*f14fb602SLionel Sambuc int *bits, int ipv6);
60*f14fb602SLionel Sambuc static int inet_cidr_pton_ipv6(const char *src, u_char *dst, int *bits);
612fe8fb19SBen Gras
622fe8fb19SBen Gras static int getbits(const char *, int ipv6);
632fe8fb19SBen Gras
642fe8fb19SBen Gras /*%
652fe8fb19SBen Gras * int
662fe8fb19SBen Gras * inet_cidr_pton(af, src, dst, *bits)
672fe8fb19SBen Gras * convert network address from presentation to network format.
682fe8fb19SBen Gras * accepts inet_pton()'s input for this "af" plus trailing "/CIDR".
692fe8fb19SBen Gras * "dst" is assumed large enough for its "af". "bits" is set to the
702fe8fb19SBen Gras * /CIDR prefix length, which can have defaults (like /32 for IPv4).
712fe8fb19SBen Gras * return:
722fe8fb19SBen Gras * -1 if an error occurred (inspect errno; ENOENT means bad format).
732fe8fb19SBen Gras * 0 if successful conversion occurred.
742fe8fb19SBen Gras * note:
752fe8fb19SBen Gras * 192.5.5.1/28 has a nonzero host part, which means it isn't a network
762fe8fb19SBen Gras * as called for by inet_net_pton() but it can be a host address with
772fe8fb19SBen Gras * an included netmask.
782fe8fb19SBen Gras * author:
792fe8fb19SBen Gras * Paul Vixie (ISC), October 1998
802fe8fb19SBen Gras */
812fe8fb19SBen Gras int
inet_cidr_pton(int af,const char * src,void * dst,int * bits)822fe8fb19SBen Gras inet_cidr_pton(int af, const char *src, void *dst, int *bits) {
832fe8fb19SBen Gras switch (af) {
842fe8fb19SBen Gras case AF_INET:
852fe8fb19SBen Gras return (inet_cidr_pton_ipv4(src, dst, bits, 0));
862fe8fb19SBen Gras case AF_INET6:
872fe8fb19SBen Gras return (inet_cidr_pton_ipv6(src, dst, bits));
882fe8fb19SBen Gras default:
892fe8fb19SBen Gras errno = EAFNOSUPPORT;
902fe8fb19SBen Gras return (-1);
912fe8fb19SBen Gras }
922fe8fb19SBen Gras }
932fe8fb19SBen Gras
942fe8fb19SBen Gras static const char digits[] = "0123456789";
952fe8fb19SBen Gras
962fe8fb19SBen Gras static int
inet_cidr_pton_ipv4(const char * src,u_char * dst,int * pbits,int ipv6)972fe8fb19SBen Gras inet_cidr_pton_ipv4(const char *src, u_char *dst, int *pbits, int ipv6) {
982fe8fb19SBen Gras const u_char *odst = dst;
99*f14fb602SLionel Sambuc int ch, bits;
100*f14fb602SLionel Sambuc ptrdiff_t n, tmp;
1012fe8fb19SBen Gras size_t size = 4;
1022fe8fb19SBen Gras
1032fe8fb19SBen Gras /* Get the mantissa. */
1042fe8fb19SBen Gras while (ch = *src++, (isascii(ch) && isdigit(ch))) {
1052fe8fb19SBen Gras tmp = 0;
1062fe8fb19SBen Gras do {
1072fe8fb19SBen Gras n = strchr(digits, ch) - digits;
1082fe8fb19SBen Gras INSIST(n >= 0 && n <= 9);
1092fe8fb19SBen Gras tmp *= 10;
1102fe8fb19SBen Gras tmp += n;
1112fe8fb19SBen Gras if (tmp > 255)
1122fe8fb19SBen Gras goto enoent;
1132fe8fb19SBen Gras } while ((ch = *src++) != '\0' && isascii(ch) && isdigit(ch));
1142fe8fb19SBen Gras if (size-- == 0U)
1152fe8fb19SBen Gras goto emsgsize;
1162fe8fb19SBen Gras *dst++ = (u_char) tmp;
1172fe8fb19SBen Gras if (ch == '\0' || ch == '/')
1182fe8fb19SBen Gras break;
1192fe8fb19SBen Gras if (ch != '.')
1202fe8fb19SBen Gras goto enoent;
1212fe8fb19SBen Gras }
1222fe8fb19SBen Gras
1232fe8fb19SBen Gras /* Get the prefix length if any. */
1242fe8fb19SBen Gras bits = -1;
1252fe8fb19SBen Gras if (ch == '/' && dst > odst) {
1262fe8fb19SBen Gras bits = getbits(src, ipv6);
1272fe8fb19SBen Gras if (bits == -2)
1282fe8fb19SBen Gras goto enoent;
1292fe8fb19SBen Gras } else if (ch != '\0')
1302fe8fb19SBen Gras goto enoent;
1312fe8fb19SBen Gras
1322fe8fb19SBen Gras /* Prefix length can default to /32 only if all four octets spec'd. */
1332fe8fb19SBen Gras if (bits == -1) {
1342fe8fb19SBen Gras if (dst - odst == 4)
1352fe8fb19SBen Gras bits = ipv6 ? 128 : 32;
1362fe8fb19SBen Gras else
1372fe8fb19SBen Gras goto enoent;
1382fe8fb19SBen Gras }
1392fe8fb19SBen Gras
1402fe8fb19SBen Gras /* If nothing was written to the destination, we found no address. */
1412fe8fb19SBen Gras if (dst == odst)
1422fe8fb19SBen Gras goto enoent;
1432fe8fb19SBen Gras
1442fe8fb19SBen Gras /* If prefix length overspecifies mantissa, life is bad. */
1452fe8fb19SBen Gras if (((bits - (ipv6 ? 96 : 0)) / 8) > (dst - odst))
1462fe8fb19SBen Gras goto enoent;
1472fe8fb19SBen Gras
1482fe8fb19SBen Gras /* Extend address to four octets. */
1492fe8fb19SBen Gras while (size-- > 0U)
1502fe8fb19SBen Gras *dst++ = 0;
1512fe8fb19SBen Gras
1522fe8fb19SBen Gras *pbits = bits;
1532fe8fb19SBen Gras return (0);
1542fe8fb19SBen Gras
1552fe8fb19SBen Gras enoent:
1562fe8fb19SBen Gras errno = ENOENT;
1572fe8fb19SBen Gras return (-1);
1582fe8fb19SBen Gras
1592fe8fb19SBen Gras emsgsize:
1602fe8fb19SBen Gras errno = EMSGSIZE;
1612fe8fb19SBen Gras return (-1);
1622fe8fb19SBen Gras }
1632fe8fb19SBen Gras
1642fe8fb19SBen Gras static int
inet_cidr_pton_ipv6(const char * src,u_char * dst,int * pbits)1652fe8fb19SBen Gras inet_cidr_pton_ipv6(const char *src, u_char *dst, int *pbits) {
1662fe8fb19SBen Gras static const char xdigits_l[] = "0123456789abcdef",
1672fe8fb19SBen Gras xdigits_u[] = "0123456789ABCDEF";
1682fe8fb19SBen Gras u_char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
1692fe8fb19SBen Gras const char *xdigits, *curtok;
1702fe8fb19SBen Gras int ch, saw_xdigit;
1712fe8fb19SBen Gras u_int val;
1722fe8fb19SBen Gras int bits;
1732fe8fb19SBen Gras
1742fe8fb19SBen Gras memset((tp = tmp), '\0', NS_IN6ADDRSZ);
1752fe8fb19SBen Gras endp = tp + NS_IN6ADDRSZ;
1762fe8fb19SBen Gras colonp = NULL;
1772fe8fb19SBen Gras /* Leading :: requires some special handling. */
1782fe8fb19SBen Gras if (*src == ':')
1792fe8fb19SBen Gras if (*++src != ':')
1802fe8fb19SBen Gras return (0);
1812fe8fb19SBen Gras curtok = src;
1822fe8fb19SBen Gras saw_xdigit = 0;
1832fe8fb19SBen Gras val = 0;
1842fe8fb19SBen Gras bits = -1;
1852fe8fb19SBen Gras while ((ch = *src++) != '\0') {
1862fe8fb19SBen Gras const char *pch;
1872fe8fb19SBen Gras
1882fe8fb19SBen Gras if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
1892fe8fb19SBen Gras pch = strchr((xdigits = xdigits_u), ch);
1902fe8fb19SBen Gras if (pch != NULL) {
1912fe8fb19SBen Gras val <<= 4;
192*f14fb602SLionel Sambuc val |= (int)(pch - xdigits);
1932fe8fb19SBen Gras if (val > 0xffff)
1942fe8fb19SBen Gras return (0);
1952fe8fb19SBen Gras saw_xdigit = 1;
1962fe8fb19SBen Gras continue;
1972fe8fb19SBen Gras }
1982fe8fb19SBen Gras if (ch == ':') {
1992fe8fb19SBen Gras curtok = src;
2002fe8fb19SBen Gras if (!saw_xdigit) {
2012fe8fb19SBen Gras if (colonp)
2022fe8fb19SBen Gras return (0);
2032fe8fb19SBen Gras colonp = tp;
2042fe8fb19SBen Gras continue;
2052fe8fb19SBen Gras } else if (*src == '\0') {
2062fe8fb19SBen Gras return (0);
2072fe8fb19SBen Gras }
2082fe8fb19SBen Gras if (tp + NS_INT16SZ > endp)
2092fe8fb19SBen Gras return (0);
2102fe8fb19SBen Gras *tp++ = (u_char) (val >> 8) & 0xff;
2112fe8fb19SBen Gras *tp++ = (u_char) val & 0xff;
2122fe8fb19SBen Gras saw_xdigit = 0;
2132fe8fb19SBen Gras val = 0;
2142fe8fb19SBen Gras continue;
2152fe8fb19SBen Gras }
2162fe8fb19SBen Gras if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
2172fe8fb19SBen Gras inet_cidr_pton_ipv4(curtok, tp, &bits, 1) == 0) {
2182fe8fb19SBen Gras tp += NS_INADDRSZ;
2192fe8fb19SBen Gras saw_xdigit = 0;
2202fe8fb19SBen Gras break; /*%< '\\0' was seen by inet_pton4(). */
2212fe8fb19SBen Gras }
2222fe8fb19SBen Gras if (ch == '/') {
2232fe8fb19SBen Gras bits = getbits(src, 1);
2242fe8fb19SBen Gras if (bits == -2)
2252fe8fb19SBen Gras goto enoent;
2262fe8fb19SBen Gras break;
2272fe8fb19SBen Gras }
2282fe8fb19SBen Gras goto enoent;
2292fe8fb19SBen Gras }
2302fe8fb19SBen Gras if (saw_xdigit) {
2312fe8fb19SBen Gras if (tp + NS_INT16SZ > endp)
2322fe8fb19SBen Gras goto emsgsize;
2332fe8fb19SBen Gras *tp++ = (u_char) (val >> 8) & 0xff;
2342fe8fb19SBen Gras *tp++ = (u_char) val & 0xff;
2352fe8fb19SBen Gras }
2362fe8fb19SBen Gras if (colonp != NULL) {
2372fe8fb19SBen Gras /*
2382fe8fb19SBen Gras * Since some memmove()'s erroneously fail to handle
2392fe8fb19SBen Gras * overlapping regions, we'll do the shift by hand.
2402fe8fb19SBen Gras */
241*f14fb602SLionel Sambuc const ptrdiff_t n = tp - colonp;
2422fe8fb19SBen Gras int i;
2432fe8fb19SBen Gras
2442fe8fb19SBen Gras if (tp == endp)
2452fe8fb19SBen Gras goto enoent;
2462fe8fb19SBen Gras for (i = 1; i <= n; i++) {
2472fe8fb19SBen Gras endp[- i] = colonp[n - i];
2482fe8fb19SBen Gras colonp[n - i] = 0;
2492fe8fb19SBen Gras }
2502fe8fb19SBen Gras tp = endp;
2512fe8fb19SBen Gras }
2522fe8fb19SBen Gras
2532fe8fb19SBen Gras memcpy(dst, tmp, NS_IN6ADDRSZ);
2542fe8fb19SBen Gras
2552fe8fb19SBen Gras *pbits = bits;
2562fe8fb19SBen Gras return (0);
2572fe8fb19SBen Gras
2582fe8fb19SBen Gras enoent:
2592fe8fb19SBen Gras errno = ENOENT;
2602fe8fb19SBen Gras return (-1);
2612fe8fb19SBen Gras
2622fe8fb19SBen Gras emsgsize:
2632fe8fb19SBen Gras errno = EMSGSIZE;
2642fe8fb19SBen Gras return (-1);
2652fe8fb19SBen Gras }
2662fe8fb19SBen Gras
2672fe8fb19SBen Gras static int
getbits(const char * src,int ipv6)2682fe8fb19SBen Gras getbits(const char *src, int ipv6) {
2692fe8fb19SBen Gras int bits = 0;
2702fe8fb19SBen Gras char *cp, ch;
2712fe8fb19SBen Gras
2722fe8fb19SBen Gras if (*src == '\0') /*%< syntax */
2732fe8fb19SBen Gras return (-2);
2742fe8fb19SBen Gras do {
2752fe8fb19SBen Gras ch = *src++;
2762fe8fb19SBen Gras cp = strchr(digits, ch);
2772fe8fb19SBen Gras if (cp == NULL) /*%< syntax */
2782fe8fb19SBen Gras return (-2);
2792fe8fb19SBen Gras bits *= 10;
280*f14fb602SLionel Sambuc bits += (int)(cp - digits);
2812fe8fb19SBen Gras if (bits == 0 && *src != '\0') /*%< no leading zeros */
2822fe8fb19SBen Gras return (-2);
2832fe8fb19SBen Gras if (bits > (ipv6 ? 128 : 32)) /*%< range error */
2842fe8fb19SBen Gras return (-2);
2852fe8fb19SBen Gras } while (*src != '\0');
2862fe8fb19SBen Gras
2872fe8fb19SBen Gras return (bits);
2882fe8fb19SBen Gras }
2892fe8fb19SBen Gras
2902fe8fb19SBen Gras /*! \file */
291