1*c5e820caSchristos /* $NetBSD: inet_cidr_ntop.c,v 1.8 2012/03/13 21:13:38 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_ntop.c,v 1.7 2006/10/11 02:18:18 marka Exp";
24df0952c6Schristos #else
25*c5e820caSchristos __RCSID("$NetBSD: inet_cidr_ntop.c,v 1.8 2012/03/13 21:13:38 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
38*c5e820caSchristos #include <assert.h>
3939e7bb71Schristos #include <errno.h>
4039e7bb71Schristos #include <stdio.h>
4139e7bb71Schristos #include <string.h>
4239e7bb71Schristos #include <stdlib.h>
4339e7bb71Schristos
4439e7bb71Schristos #include "port_after.h"
4539e7bb71Schristos
46df0952c6Schristos #ifdef __weak_alias
47df0952c6Schristos __weak_alias(inet_cidr_ntop,_inet_cidr_ntop)
48df0952c6Schristos #endif
49df0952c6Schristos
5039e7bb71Schristos #ifdef SPRINTF_CHAR
5139e7bb71Schristos # define SPRINTF(x) strlen(sprintf/**/x)
5239e7bb71Schristos #else
5339e7bb71Schristos # define SPRINTF(x) ((size_t)sprintf x)
5439e7bb71Schristos #endif
5539e7bb71Schristos
56d73eb73dSchristos static char *
57d73eb73dSchristos inet_cidr_ntop_ipv4(const u_char *src, int bits, char *dst, size_t size);
58d73eb73dSchristos static char *
59d73eb73dSchristos inet_cidr_ntop_ipv6(const u_char *src, int bits, char *dst, size_t size);
6039e7bb71Schristos
61d73eb73dSchristos /*%
6239e7bb71Schristos * char *
6339e7bb71Schristos * inet_cidr_ntop(af, src, bits, dst, size)
6439e7bb71Schristos * convert network address from network to presentation format.
6539e7bb71Schristos * "src"'s size is determined from its "af".
6639e7bb71Schristos * return:
6739e7bb71Schristos * pointer to dst, or NULL if an error occurred (check errno).
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_ntop() but it can be a host address with
7139e7bb71Schristos * an included netmask.
7239e7bb71Schristos * author:
7339e7bb71Schristos * Paul Vixie (ISC), October 1998
7439e7bb71Schristos */
7539e7bb71Schristos char *
inet_cidr_ntop(int af,const void * src,int bits,char * dst,size_t size)7639e7bb71Schristos inet_cidr_ntop(int af, const void *src, int bits, char *dst, size_t size) {
7739e7bb71Schristos switch (af) {
7839e7bb71Schristos case AF_INET:
7939e7bb71Schristos return (inet_cidr_ntop_ipv4(src, bits, dst, size));
8039e7bb71Schristos case AF_INET6:
8139e7bb71Schristos return (inet_cidr_ntop_ipv6(src, bits, dst, size));
8239e7bb71Schristos default:
8339e7bb71Schristos errno = EAFNOSUPPORT;
8439e7bb71Schristos return (NULL);
8539e7bb71Schristos }
8639e7bb71Schristos }
8739e7bb71Schristos
8839e7bb71Schristos static int
decoct(const u_char * src,size_t bytes,char * dst,size_t size)89df0952c6Schristos decoct(const u_char *src, size_t bytes, char *dst, size_t size) {
9039e7bb71Schristos char *odst = dst;
9139e7bb71Schristos char *t;
92df0952c6Schristos size_t b;
9339e7bb71Schristos
9439e7bb71Schristos for (b = 1; b <= bytes; b++) {
9539e7bb71Schristos if (size < sizeof "255.")
9639e7bb71Schristos return (0);
9739e7bb71Schristos t = dst;
9839e7bb71Schristos dst += SPRINTF((dst, "%u", *src++));
9939e7bb71Schristos if (b != bytes) {
10039e7bb71Schristos *dst++ = '.';
10139e7bb71Schristos *dst = '\0';
10239e7bb71Schristos }
10339e7bb71Schristos size -= (size_t)(dst - t);
10439e7bb71Schristos }
105*c5e820caSchristos _DIAGASSERT(__type_fit(int, dst - odst));
106*c5e820caSchristos return (int)(dst - odst);
10739e7bb71Schristos }
10839e7bb71Schristos
109d73eb73dSchristos /*%
11039e7bb71Schristos * static char *
11139e7bb71Schristos * inet_cidr_ntop_ipv4(src, bits, dst, size)
11239e7bb71Schristos * convert IPv4 network address from network to presentation format.
11339e7bb71Schristos * "src"'s size is determined from its "af".
11439e7bb71Schristos * return:
11539e7bb71Schristos * pointer to dst, or NULL if an error occurred (check errno).
11639e7bb71Schristos * note:
11739e7bb71Schristos * network byte order assumed. this means 192.5.5.240/28 has
11839e7bb71Schristos * 0b11110000 in its fourth octet.
11939e7bb71Schristos * author:
12039e7bb71Schristos * Paul Vixie (ISC), October 1998
12139e7bb71Schristos */
12239e7bb71Schristos static char *
inet_cidr_ntop_ipv4(const u_char * src,int bits,char * dst,size_t size)12339e7bb71Schristos inet_cidr_ntop_ipv4(const u_char *src, int bits, char *dst, size_t size) {
12439e7bb71Schristos char *odst = dst;
12539e7bb71Schristos size_t len = 4;
12639e7bb71Schristos size_t b;
12739e7bb71Schristos size_t bytes;
12839e7bb71Schristos
12939e7bb71Schristos if ((bits < -1) || (bits > 32)) {
13039e7bb71Schristos errno = EINVAL;
13139e7bb71Schristos return (NULL);
13239e7bb71Schristos }
13339e7bb71Schristos
13439e7bb71Schristos /* Find number of significant bytes in address. */
13539e7bb71Schristos if (bits == -1)
13639e7bb71Schristos len = 4;
13739e7bb71Schristos else
13839e7bb71Schristos for (len = 1, b = 1 ; b < 4U; b++)
13939e7bb71Schristos if (*(src + b))
14039e7bb71Schristos len = b + 1;
14139e7bb71Schristos
14239e7bb71Schristos /* Format whole octets plus nonzero trailing octets. */
14339e7bb71Schristos bytes = (((bits <= 0) ? 1 : bits) + 7) / 8;
14439e7bb71Schristos if (len > bytes)
14539e7bb71Schristos bytes = len;
14639e7bb71Schristos b = decoct(src, bytes, dst, size);
14739e7bb71Schristos if (b == 0U)
14839e7bb71Schristos goto emsgsize;
14939e7bb71Schristos dst += b;
15039e7bb71Schristos size -= b;
15139e7bb71Schristos
15239e7bb71Schristos if (bits != -1) {
15339e7bb71Schristos /* Format CIDR /width. */
15439e7bb71Schristos if (size < sizeof "/32")
15539e7bb71Schristos goto emsgsize;
15639e7bb71Schristos dst += SPRINTF((dst, "/%u", bits));
15739e7bb71Schristos }
15839e7bb71Schristos
15939e7bb71Schristos return (odst);
16039e7bb71Schristos
16139e7bb71Schristos emsgsize:
16239e7bb71Schristos errno = EMSGSIZE;
16339e7bb71Schristos return (NULL);
16439e7bb71Schristos }
16539e7bb71Schristos
16639e7bb71Schristos static char *
inet_cidr_ntop_ipv6(const u_char * src,int bits,char * dst,size_t size)16739e7bb71Schristos inet_cidr_ntop_ipv6(const u_char *src, int bits, char *dst, size_t size) {
16839e7bb71Schristos /*
16939e7bb71Schristos * Note that int32_t and int16_t need only be "at least" large enough
17039e7bb71Schristos * to contain a value of the specified size. On some systems, like
17139e7bb71Schristos * Crays, there is no such thing as an integer variable with 16 bits.
17239e7bb71Schristos * Keep this in mind if you think this function should have been coded
17339e7bb71Schristos * to use pointer overlays. All the world's not a VAX.
17439e7bb71Schristos */
17539e7bb71Schristos char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255/128"];
17639e7bb71Schristos char *tp;
17739e7bb71Schristos struct { int base, len; } best, cur;
17839e7bb71Schristos u_int words[NS_IN6ADDRSZ / NS_INT16SZ];
17939e7bb71Schristos int i;
18039e7bb71Schristos
18139e7bb71Schristos if ((bits < -1) || (bits > 128)) {
18239e7bb71Schristos errno = EINVAL;
18339e7bb71Schristos return (NULL);
18439e7bb71Schristos }
18539e7bb71Schristos
18639e7bb71Schristos /*
18739e7bb71Schristos * Preprocess:
18839e7bb71Schristos * Copy the input (bytewise) array into a wordwise array.
18939e7bb71Schristos * Find the longest run of 0x00's in src[] for :: shorthanding.
19039e7bb71Schristos */
19139e7bb71Schristos memset(words, '\0', sizeof words);
19239e7bb71Schristos for (i = 0; i < NS_IN6ADDRSZ; i++)
19339e7bb71Schristos words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
19439e7bb71Schristos best.base = -1;
195d73eb73dSchristos best.len = 0;
19639e7bb71Schristos cur.base = -1;
197d73eb73dSchristos cur.len = 0;
19839e7bb71Schristos for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
19939e7bb71Schristos if (words[i] == 0) {
20039e7bb71Schristos if (cur.base == -1)
20139e7bb71Schristos cur.base = i, cur.len = 1;
20239e7bb71Schristos else
20339e7bb71Schristos cur.len++;
20439e7bb71Schristos } else {
20539e7bb71Schristos if (cur.base != -1) {
20639e7bb71Schristos if (best.base == -1 || cur.len > best.len)
20739e7bb71Schristos best = cur;
20839e7bb71Schristos cur.base = -1;
20939e7bb71Schristos }
21039e7bb71Schristos }
21139e7bb71Schristos }
21239e7bb71Schristos if (cur.base != -1) {
21339e7bb71Schristos if (best.base == -1 || cur.len > best.len)
21439e7bb71Schristos best = cur;
21539e7bb71Schristos }
21639e7bb71Schristos if (best.base != -1 && best.len < 2)
21739e7bb71Schristos best.base = -1;
21839e7bb71Schristos
21939e7bb71Schristos /*
22039e7bb71Schristos * Format the result.
22139e7bb71Schristos */
22239e7bb71Schristos tp = tmp;
22339e7bb71Schristos for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
22439e7bb71Schristos /* Are we inside the best run of 0x00's? */
22539e7bb71Schristos if (best.base != -1 && i >= best.base &&
22639e7bb71Schristos i < (best.base + best.len)) {
22739e7bb71Schristos if (i == best.base)
22839e7bb71Schristos *tp++ = ':';
22939e7bb71Schristos continue;
23039e7bb71Schristos }
23139e7bb71Schristos /* Are we following an initial run of 0x00s or any real hex? */
23239e7bb71Schristos if (i != 0)
23339e7bb71Schristos *tp++ = ':';
23439e7bb71Schristos /* Is this address an encapsulated IPv4? */
23539e7bb71Schristos if (i == 6 && best.base == 0 && (best.len == 6 ||
23639e7bb71Schristos (best.len == 7 && words[7] != 0x0001) ||
23739e7bb71Schristos (best.len == 5 && words[5] == 0xffff))) {
238df0952c6Schristos size_t n;
23939e7bb71Schristos
24039e7bb71Schristos if (src[15] || bits == -1 || bits > 120)
24139e7bb71Schristos n = 4;
24239e7bb71Schristos else if (src[14] || bits > 112)
24339e7bb71Schristos n = 3;
24439e7bb71Schristos else
24539e7bb71Schristos n = 2;
24639e7bb71Schristos n = decoct(src+12, n, tp, sizeof tmp - (tp - tmp));
24739e7bb71Schristos if (n == 0) {
24839e7bb71Schristos errno = EMSGSIZE;
24939e7bb71Schristos return (NULL);
25039e7bb71Schristos }
25139e7bb71Schristos tp += strlen(tp);
25239e7bb71Schristos break;
25339e7bb71Schristos }
25439e7bb71Schristos tp += SPRINTF((tp, "%x", words[i]));
25539e7bb71Schristos }
25639e7bb71Schristos
25739e7bb71Schristos /* Was it a trailing run of 0x00's? */
25839e7bb71Schristos if (best.base != -1 && (best.base + best.len) ==
25939e7bb71Schristos (NS_IN6ADDRSZ / NS_INT16SZ))
26039e7bb71Schristos *tp++ = ':';
26139e7bb71Schristos *tp = '\0';
26239e7bb71Schristos
26339e7bb71Schristos if (bits != -1)
26439e7bb71Schristos tp += SPRINTF((tp, "/%u", bits));
26539e7bb71Schristos
26639e7bb71Schristos /*
26739e7bb71Schristos * Check for overflow, copy, and we're done.
26839e7bb71Schristos */
26939e7bb71Schristos if ((size_t)(tp - tmp) > size) {
27039e7bb71Schristos errno = EMSGSIZE;
27139e7bb71Schristos return (NULL);
27239e7bb71Schristos }
27339e7bb71Schristos strcpy(dst, tmp);
27439e7bb71Schristos return (dst);
27539e7bb71Schristos }
276d73eb73dSchristos
277d73eb73dSchristos /*! \file */
278