1*ace5b9b5Schristos /* $NetBSD: inet_cidr_pton.c,v 1.10 2024/01/20 14:52:47 christos Exp $ */
239e7bb71Schristos
339e7bb71Schristos /*
439e7bb71Schristos * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
539e7bb71Schristos * Copyright (c) 1998,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_cidr_pton.c,v 1.6 2005/04/27 04:56:19 sra Exp";
24df0952c6Schristos #else
25*ace5b9b5Schristos __RCSID("$NetBSD: inet_cidr_pton.c,v 1.10 2024/01/20 14:52:47 christos Exp $");
26df0952c6Schristos #endif
2739e7bb71Schristos #endif
2839e7bb71Schristos
2939e7bb71Schristos #include "port_before.h"
3039e7bb71Schristos
31df0952c6Schristos #include "namespace.h"
3239e7bb71Schristos #include <sys/types.h>
3339e7bb71Schristos #include <sys/socket.h>
3439e7bb71Schristos #include <netinet/in.h>
3539e7bb71Schristos #include <arpa/nameser.h>
3639e7bb71Schristos #include <arpa/inet.h>
3739e7bb71Schristos
3839e7bb71Schristos #include <isc/assertions.h>
3939e7bb71Schristos #include <ctype.h>
4039e7bb71Schristos #include <errno.h>
4139e7bb71Schristos #include <stdio.h>
4239e7bb71Schristos #include <string.h>
43c5e820caSchristos #include <stddef.h>
4439e7bb71Schristos #include <stdlib.h>
4539e7bb71Schristos
4639e7bb71Schristos #include "port_after.h"
4739e7bb71Schristos
48df0952c6Schristos #ifdef __weak_alias
49df0952c6Schristos __weak_alias(inet_cidr_pton,_inet_cidr_pton)
50df0952c6Schristos #endif
51df0952c6Schristos
521f043722Smatt static int inet_cidr_pton_ipv4(const char *src, u_char *dst,
531f043722Smatt int *bits, int ipv6);
541f043722Smatt static int inet_cidr_pton_ipv6(const char *src, u_char *dst, int *bits);
5539e7bb71Schristos
5639e7bb71Schristos static int getbits(const char *, int ipv6);
5739e7bb71Schristos
58d73eb73dSchristos /*%
5939e7bb71Schristos * int
6039e7bb71Schristos * inet_cidr_pton(af, src, dst, *bits)
6139e7bb71Schristos * convert network address from presentation to network format.
6239e7bb71Schristos * accepts inet_pton()'s input for this "af" plus trailing "/CIDR".
6339e7bb71Schristos * "dst" is assumed large enough for its "af". "bits" is set to the
6439e7bb71Schristos * /CIDR prefix length, which can have defaults (like /32 for IPv4).
6539e7bb71Schristos * return:
6639e7bb71Schristos * -1 if an error occurred (inspect errno; ENOENT means bad format).
6739e7bb71Schristos * 0 if successful conversion occurred.
6839e7bb71Schristos * note:
6939e7bb71Schristos * 192.5.5.1/28 has a nonzero host part, which means it isn't a network
7039e7bb71Schristos * as called for by inet_net_pton() but it can be a host address with
7139e7bb71Schristos * an included netmask.
7239e7bb71Schristos * author:
7339e7bb71Schristos * Paul Vixie (ISC), October 1998
7439e7bb71Schristos */
7539e7bb71Schristos int
inet_cidr_pton(int af,const char * src,void * dst,int * bits)7639e7bb71Schristos inet_cidr_pton(int af, const char *src, void *dst, int *bits) {
7739e7bb71Schristos switch (af) {
7839e7bb71Schristos case AF_INET:
79*ace5b9b5Schristos return inet_cidr_pton_ipv4(src, dst, bits, 0);
8039e7bb71Schristos case AF_INET6:
81*ace5b9b5Schristos return inet_cidr_pton_ipv6(src, dst, bits);
8239e7bb71Schristos default:
8339e7bb71Schristos errno = EAFNOSUPPORT;
84*ace5b9b5Schristos return -1;
8539e7bb71Schristos }
8639e7bb71Schristos }
8739e7bb71Schristos
8839e7bb71Schristos static const char digits[] = "0123456789";
8939e7bb71Schristos
9039e7bb71Schristos static int
inet_cidr_pton_ipv4(const char * src,u_char * dst,int * pbits,int ipv6)9139e7bb71Schristos inet_cidr_pton_ipv4(const char *src, u_char *dst, int *pbits, int ipv6) {
9239e7bb71Schristos const u_char *odst = dst;
93c5e820caSchristos int ch, bits;
94c5e820caSchristos ptrdiff_t n, tmp;
9539e7bb71Schristos size_t size = 4;
9639e7bb71Schristos
9739e7bb71Schristos /* Get the mantissa. */
9839e7bb71Schristos while (ch = *src++, (isascii(ch) && isdigit(ch))) {
9939e7bb71Schristos tmp = 0;
10039e7bb71Schristos do {
10139e7bb71Schristos n = strchr(digits, ch) - digits;
10239e7bb71Schristos INSIST(n >= 0 && n <= 9);
10339e7bb71Schristos tmp *= 10;
10439e7bb71Schristos tmp += n;
10539e7bb71Schristos if (tmp > 255)
10639e7bb71Schristos goto enoent;
10739e7bb71Schristos } while ((ch = *src++) != '\0' && isascii(ch) && isdigit(ch));
10839e7bb71Schristos if (size-- == 0U)
10939e7bb71Schristos goto emsgsize;
11039e7bb71Schristos *dst++ = (u_char) tmp;
11139e7bb71Schristos if (ch == '\0' || ch == '/')
11239e7bb71Schristos break;
11339e7bb71Schristos if (ch != '.')
11439e7bb71Schristos goto enoent;
11539e7bb71Schristos }
11639e7bb71Schristos
11739e7bb71Schristos /* Get the prefix length if any. */
11839e7bb71Schristos bits = -1;
11939e7bb71Schristos if (ch == '/' && dst > odst) {
12039e7bb71Schristos bits = getbits(src, ipv6);
12139e7bb71Schristos if (bits == -2)
12239e7bb71Schristos goto enoent;
12339e7bb71Schristos } else if (ch != '\0')
12439e7bb71Schristos goto enoent;
12539e7bb71Schristos
12639e7bb71Schristos /* Prefix length can default to /32 only if all four octets spec'd. */
12739e7bb71Schristos if (bits == -1) {
12839e7bb71Schristos if (dst - odst == 4)
12939e7bb71Schristos bits = ipv6 ? 128 : 32;
13039e7bb71Schristos else
13139e7bb71Schristos goto enoent;
13239e7bb71Schristos }
13339e7bb71Schristos
13439e7bb71Schristos /* If nothing was written to the destination, we found no address. */
13539e7bb71Schristos if (dst == odst)
13639e7bb71Schristos goto enoent;
13739e7bb71Schristos
13839e7bb71Schristos /* If prefix length overspecifies mantissa, life is bad. */
13939e7bb71Schristos if (((bits - (ipv6 ? 96 : 0)) / 8) > (dst - odst))
14039e7bb71Schristos goto enoent;
14139e7bb71Schristos
14239e7bb71Schristos /* Extend address to four octets. */
14339e7bb71Schristos while (size-- > 0U)
14439e7bb71Schristos *dst++ = 0;
14539e7bb71Schristos
14639e7bb71Schristos *pbits = bits;
147*ace5b9b5Schristos return 0;
14839e7bb71Schristos
14939e7bb71Schristos enoent:
15039e7bb71Schristos errno = ENOENT;
151*ace5b9b5Schristos return -1;
15239e7bb71Schristos
15339e7bb71Schristos emsgsize:
15439e7bb71Schristos errno = EMSGSIZE;
155*ace5b9b5Schristos return -1;
15639e7bb71Schristos }
15739e7bb71Schristos
15839e7bb71Schristos static int
inet_cidr_pton_ipv6(const char * src,u_char * dst,int * pbits)15939e7bb71Schristos inet_cidr_pton_ipv6(const char *src, u_char *dst, int *pbits) {
16039e7bb71Schristos static const char xdigits_l[] = "0123456789abcdef",
16139e7bb71Schristos xdigits_u[] = "0123456789ABCDEF";
16239e7bb71Schristos u_char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
16339e7bb71Schristos const char *xdigits, *curtok;
16439e7bb71Schristos int ch, saw_xdigit;
16539e7bb71Schristos u_int val;
16639e7bb71Schristos int bits;
16739e7bb71Schristos
16839e7bb71Schristos memset((tp = tmp), '\0', NS_IN6ADDRSZ);
16939e7bb71Schristos endp = tp + NS_IN6ADDRSZ;
17039e7bb71Schristos colonp = NULL;
17139e7bb71Schristos /* Leading :: requires some special handling. */
17239e7bb71Schristos if (*src == ':')
17339e7bb71Schristos if (*++src != ':')
174*ace5b9b5Schristos return 0;
17539e7bb71Schristos curtok = src;
17639e7bb71Schristos saw_xdigit = 0;
17739e7bb71Schristos val = 0;
17839e7bb71Schristos bits = -1;
17939e7bb71Schristos while ((ch = *src++) != '\0') {
18039e7bb71Schristos const char *pch;
18139e7bb71Schristos
18239e7bb71Schristos if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
18339e7bb71Schristos pch = strchr((xdigits = xdigits_u), ch);
18439e7bb71Schristos if (pch != NULL) {
18539e7bb71Schristos val <<= 4;
186c5e820caSchristos val |= (int)(pch - xdigits);
18739e7bb71Schristos if (val > 0xffff)
188*ace5b9b5Schristos return 0;
18939e7bb71Schristos saw_xdigit = 1;
19039e7bb71Schristos continue;
19139e7bb71Schristos }
19239e7bb71Schristos if (ch == ':') {
19339e7bb71Schristos curtok = src;
19439e7bb71Schristos if (!saw_xdigit) {
19539e7bb71Schristos if (colonp)
196*ace5b9b5Schristos return 0;
19739e7bb71Schristos colonp = tp;
19839e7bb71Schristos continue;
19939e7bb71Schristos } else if (*src == '\0') {
200*ace5b9b5Schristos return 0;
20139e7bb71Schristos }
20239e7bb71Schristos if (tp + NS_INT16SZ > endp)
203*ace5b9b5Schristos return 0;
20439e7bb71Schristos *tp++ = (u_char) (val >> 8) & 0xff;
20539e7bb71Schristos *tp++ = (u_char) val & 0xff;
20639e7bb71Schristos saw_xdigit = 0;
20739e7bb71Schristos val = 0;
20839e7bb71Schristos continue;
20939e7bb71Schristos }
21039e7bb71Schristos if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
21139e7bb71Schristos inet_cidr_pton_ipv4(curtok, tp, &bits, 1) == 0) {
21239e7bb71Schristos tp += NS_INADDRSZ;
21339e7bb71Schristos saw_xdigit = 0;
214d73eb73dSchristos break; /*%< '\\0' was seen by inet_pton4(). */
21539e7bb71Schristos }
21639e7bb71Schristos if (ch == '/') {
21739e7bb71Schristos bits = getbits(src, 1);
21839e7bb71Schristos if (bits == -2)
21939e7bb71Schristos goto enoent;
22039e7bb71Schristos break;
22139e7bb71Schristos }
22239e7bb71Schristos goto enoent;
22339e7bb71Schristos }
22439e7bb71Schristos if (saw_xdigit) {
22539e7bb71Schristos if (tp + NS_INT16SZ > endp)
22639e7bb71Schristos goto emsgsize;
22739e7bb71Schristos *tp++ = (u_char) (val >> 8) & 0xff;
22839e7bb71Schristos *tp++ = (u_char) val & 0xff;
22939e7bb71Schristos }
23039e7bb71Schristos if (colonp != NULL) {
23139e7bb71Schristos /*
23239e7bb71Schristos * Since some memmove()'s erroneously fail to handle
23339e7bb71Schristos * overlapping regions, we'll do the shift by hand.
23439e7bb71Schristos */
235c5e820caSchristos const ptrdiff_t n = tp - colonp;
23639e7bb71Schristos int i;
23739e7bb71Schristos
23839e7bb71Schristos if (tp == endp)
23939e7bb71Schristos goto enoent;
24039e7bb71Schristos for (i = 1; i <= n; i++) {
24139e7bb71Schristos endp[- i] = colonp[n - i];
24239e7bb71Schristos colonp[n - i] = 0;
24339e7bb71Schristos }
24439e7bb71Schristos tp = endp;
24539e7bb71Schristos }
24639e7bb71Schristos
24739e7bb71Schristos memcpy(dst, tmp, NS_IN6ADDRSZ);
24839e7bb71Schristos
24939e7bb71Schristos *pbits = bits;
250*ace5b9b5Schristos return 0;
25139e7bb71Schristos
25239e7bb71Schristos enoent:
25339e7bb71Schristos errno = ENOENT;
254*ace5b9b5Schristos return -1;
25539e7bb71Schristos
25639e7bb71Schristos emsgsize:
25739e7bb71Schristos errno = EMSGSIZE;
258*ace5b9b5Schristos return -1;
25939e7bb71Schristos }
26039e7bb71Schristos
26139e7bb71Schristos static int
getbits(const char * src,int ipv6)26239e7bb71Schristos getbits(const char *src, int ipv6) {
26339e7bb71Schristos int bits = 0;
26439e7bb71Schristos
265d73eb73dSchristos if (*src == '\0') /*%< syntax */
266*ace5b9b5Schristos return -2;
26739e7bb71Schristos do {
268*ace5b9b5Schristos char ch = *src++;
269*ace5b9b5Schristos const char *cp = strchr(digits, ch);
270d73eb73dSchristos if (cp == NULL) /*%< syntax */
271*ace5b9b5Schristos return -2;
27239e7bb71Schristos bits *= 10;
273c5e820caSchristos bits += (int)(cp - digits);
274d73eb73dSchristos if (bits == 0 && *src != '\0') /*%< no leading zeros */
275*ace5b9b5Schristos return -2;
276d73eb73dSchristos if (bits > (ipv6 ? 128 : 32)) /*%< range error */
277*ace5b9b5Schristos return -2;
27839e7bb71Schristos } while (*src != '\0');
27939e7bb71Schristos
280*ace5b9b5Schristos return bits;
28139e7bb71Schristos }
282d73eb73dSchristos
283d73eb73dSchristos /*! \file */
284