2N/A/*
2N/A * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
2N/A * Copyright (c) 1998,1999 by Internet Software Consortium.
2N/A *
2N/A * Permission to use, copy, modify, and distribute this software for any
2N/A * purpose with or without fee is hereby granted, provided that the above
2N/A * copyright notice and this permission notice appear in all copies.
2N/A *
2N/A * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
2N/A * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
2N/A * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
2N/A * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
2N/A * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
2N/A * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
2N/A * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2N/A */
2N/A
2N/A#if defined(LIBC_SCCS) && !defined(lint)
2N/Astatic const char rcsid[] = "$Id: inet_cidr_pton.c,v 1.6 2005/04/27 04:56:19 sra Exp $";
2N/A#endif
2N/A
2N/A#include "port_before.h"
2N/A
2N/A#include <sys/types.h>
2N/A#include <sys/socket.h>
2N/A#include <netinet/in.h>
2N/A#include <arpa/nameser.h>
2N/A#include <arpa/inet.h>
2N/A
2N/A#include <isc/assertions.h>
2N/A#include <ctype.h>
2N/A#include <errno.h>
2N/A#include <stdio.h>
2N/A#include <string.h>
2N/A#include <stdlib.h>
2N/A
2N/A#include "port_after.h"
2N/A
2N/A#ifdef SPRINTF_CHAR
2N/A# define SPRINTF(x) strlen(sprintf/**/x)
2N/A#else
2N/A# define SPRINTF(x) ((size_t)sprintf x)
2N/A#endif
2N/A
2N/Astatic int inet_cidr_pton_ipv4 __P((const char *src, u_char *dst,
2N/A int *bits, int ipv6));
2N/Astatic int inet_cidr_pton_ipv6 __P((const char *src, u_char *dst,
2N/A int *bits));
2N/A
2N/Astatic int getbits(const char *, int ipv6);
2N/A
2N/A/*%
2N/A * int
2N/A * inet_cidr_pton(af, src, dst, *bits)
2N/A * convert network address from presentation to network format.
2N/A * accepts inet_pton()'s input for this "af" plus trailing "/CIDR".
2N/A * "dst" is assumed large enough for its "af". "bits" is set to the
2N/A * /CIDR prefix length, which can have defaults (like /32 for IPv4).
2N/A * return:
2N/A * -1 if an error occurred (inspect errno; ENOENT means bad format).
2N/A * 0 if successful conversion occurred.
2N/A * note:
2N/A * 192.5.5.1/28 has a nonzero host part, which means it isn't a network
2N/A * as called for by inet_net_pton() but it can be a host address with
2N/A * an included netmask.
2N/A * author:
2N/A * Paul Vixie (ISC), October 1998
2N/A */
2N/Aint
2N/Ainet_cidr_pton(int af, const char *src, void *dst, int *bits) {
2N/A switch (af) {
2N/A case AF_INET:
2N/A return (inet_cidr_pton_ipv4(src, dst, bits, 0));
2N/A case AF_INET6:
2N/A return (inet_cidr_pton_ipv6(src, dst, bits));
2N/A default:
2N/A errno = EAFNOSUPPORT;
2N/A return (-1);
2N/A }
2N/A}
2N/A
2N/Astatic const char digits[] = "0123456789";
2N/A
2N/Astatic int
2N/Ainet_cidr_pton_ipv4(const char *src, u_char *dst, int *pbits, int ipv6) {
2N/A const u_char *odst = dst;
2N/A int n, ch, tmp, bits;
2N/A size_t size = 4;
2N/A
2N/A /* Get the mantissa. */
2N/A while (ch = *src++, (isascii(ch) && isdigit(ch))) {
2N/A tmp = 0;
2N/A do {
2N/A n = strchr(digits, ch) - digits;
2N/A INSIST(n >= 0 && n <= 9);
2N/A tmp *= 10;
2N/A tmp += n;
2N/A if (tmp > 255)
2N/A goto enoent;
2N/A } while ((ch = *src++) != '\0' && isascii(ch) && isdigit(ch));
2N/A if (size-- == 0U)
2N/A goto emsgsize;
2N/A *dst++ = (u_char) tmp;
2N/A if (ch == '\0' || ch == '/')
2N/A break;
2N/A if (ch != '.')
2N/A goto enoent;
2N/A }
2N/A
2N/A /* Get the prefix length if any. */
2N/A bits = -1;
2N/A if (ch == '/' && dst > odst) {
2N/A bits = getbits(src, ipv6);
2N/A if (bits == -2)
2N/A goto enoent;
2N/A } else if (ch != '\0')
2N/A goto enoent;
2N/A
2N/A /* Prefix length can default to /32 only if all four octets spec'd. */
2N/A if (bits == -1) {
2N/A if (dst - odst == 4)
2N/A bits = ipv6 ? 128 : 32;
2N/A else
2N/A goto enoent;
2N/A }
2N/A
2N/A /* If nothing was written to the destination, we found no address. */
2N/A if (dst == odst)
2N/A goto enoent;
2N/A
2N/A /* If prefix length overspecifies mantissa, life is bad. */
2N/A if (((bits - (ipv6 ? 96 : 0)) / 8) > (dst - odst))
2N/A goto enoent;
2N/A
2N/A /* Extend address to four octets. */
2N/A while (size-- > 0U)
2N/A *dst++ = 0;
2N/A
2N/A *pbits = bits;
2N/A return (0);
2N/A
2N/A enoent:
2N/A errno = ENOENT;
2N/A return (-1);
2N/A
2N/A emsgsize:
2N/A errno = EMSGSIZE;
2N/A return (-1);
2N/A}
2N/A
2N/Astatic int
2N/Ainet_cidr_pton_ipv6(const char *src, u_char *dst, int *pbits) {
2N/A static const char xdigits_l[] = "0123456789abcdef",
2N/A xdigits_u[] = "0123456789ABCDEF";
2N/A u_char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
2N/A const char *xdigits, *curtok;
2N/A int ch, saw_xdigit;
2N/A u_int val;
2N/A int bits;
2N/A
2N/A memset((tp = tmp), '\0', NS_IN6ADDRSZ);
2N/A endp = tp + NS_IN6ADDRSZ;
2N/A colonp = NULL;
2N/A /* Leading :: requires some special handling. */
2N/A if (*src == ':')
2N/A if (*++src != ':')
2N/A return (0);
2N/A curtok = src;
2N/A saw_xdigit = 0;
2N/A val = 0;
2N/A bits = -1;
2N/A while ((ch = *src++) != '\0') {
2N/A const char *pch;
2N/A
2N/A if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
2N/A pch = strchr((xdigits = xdigits_u), ch);
2N/A if (pch != NULL) {
2N/A val <<= 4;
2N/A val |= (pch - xdigits);
2N/A if (val > 0xffff)
2N/A return (0);
2N/A saw_xdigit = 1;
2N/A continue;
2N/A }
2N/A if (ch == ':') {
2N/A curtok = src;
2N/A if (!saw_xdigit) {
2N/A if (colonp)
2N/A return (0);
2N/A colonp = tp;
2N/A continue;
2N/A } else if (*src == '\0') {
2N/A return (0);
2N/A }
2N/A if (tp + NS_INT16SZ > endp)
2N/A return (0);
2N/A *tp++ = (u_char) (val >> 8) & 0xff;
2N/A *tp++ = (u_char) val & 0xff;
2N/A saw_xdigit = 0;
2N/A val = 0;
2N/A continue;
2N/A }
2N/A if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
2N/A inet_cidr_pton_ipv4(curtok, tp, &bits, 1) == 0) {
2N/A tp += NS_INADDRSZ;
2N/A saw_xdigit = 0;
2N/A break; /*%< '\\0' was seen by inet_pton4(). */
2N/A }
2N/A if (ch == '/') {
2N/A bits = getbits(src, 1);
2N/A if (bits == -2)
2N/A goto enoent;
2N/A break;
2N/A }
2N/A goto enoent;
2N/A }
2N/A if (saw_xdigit) {
2N/A if (tp + NS_INT16SZ > endp)
2N/A goto emsgsize;
2N/A *tp++ = (u_char) (val >> 8) & 0xff;
2N/A *tp++ = (u_char) val & 0xff;
2N/A }
2N/A if (colonp != NULL) {
2N/A /*
2N/A * Since some memmove()'s erroneously fail to handle
2N/A * overlapping regions, we'll do the shift by hand.
2N/A */
2N/A const int n = tp - colonp;
2N/A int i;
2N/A
2N/A if (tp == endp)
2N/A goto enoent;
2N/A for (i = 1; i <= n; i++) {
2N/A endp[- i] = colonp[n - i];
2N/A colonp[n - i] = 0;
2N/A }
2N/A tp = endp;
2N/A }
2N/A
2N/A memcpy(dst, tmp, NS_IN6ADDRSZ);
2N/A
2N/A *pbits = bits;
2N/A return (0);
2N/A
2N/A enoent:
2N/A errno = ENOENT;
2N/A return (-1);
2N/A
2N/A emsgsize:
2N/A errno = EMSGSIZE;
2N/A return (-1);
2N/A}
2N/A
2N/Astatic int
2N/Agetbits(const char *src, int ipv6) {
2N/A int bits = 0;
2N/A char *cp, ch;
2N/A
2N/A if (*src == '\0') /*%< syntax */
2N/A return (-2);
2N/A do {
2N/A ch = *src++;
2N/A cp = strchr(digits, ch);
2N/A if (cp == NULL) /*%< syntax */
2N/A return (-2);
2N/A bits *= 10;
2N/A bits += cp - digits;
2N/A if (bits == 0 && *src != '\0') /*%< no leading zeros */
2N/A return (-2);
2N/A if (bits > (ipv6 ? 128 : 32)) /*%< range error */
2N/A return (-2);
2N/A } while (*src != '\0');
2N/A
2N/A return (bits);
2N/A}
2N/A
2N/A/*! \file */