17c478bd9Sstevel@tonic-gate /*
29525b14bSRao Shoaib * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
37c478bd9Sstevel@tonic-gate * Copyright (c) 1998,1999 by Internet Software Consortium.
47c478bd9Sstevel@tonic-gate *
57c478bd9Sstevel@tonic-gate * Permission to use, copy, modify, and distribute this software for any
67c478bd9Sstevel@tonic-gate * purpose with or without fee is hereby granted, provided that the above
77c478bd9Sstevel@tonic-gate * copyright notice and this permission notice appear in all copies.
87c478bd9Sstevel@tonic-gate *
99525b14bSRao Shoaib * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
109525b14bSRao Shoaib * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
119525b14bSRao Shoaib * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
129525b14bSRao Shoaib * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
139525b14bSRao Shoaib * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
149525b14bSRao Shoaib * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
159525b14bSRao Shoaib * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
167c478bd9Sstevel@tonic-gate */
177c478bd9Sstevel@tonic-gate
187c478bd9Sstevel@tonic-gate #if defined(LIBC_SCCS) && !defined(lint)
199525b14bSRao Shoaib static const char rcsid[] = "$Id: inet_cidr_ntop.c,v 1.7 2006/10/11 02:18:18 marka Exp $";
207c478bd9Sstevel@tonic-gate #endif
217c478bd9Sstevel@tonic-gate
227c478bd9Sstevel@tonic-gate #include "port_before.h"
237c478bd9Sstevel@tonic-gate
247c478bd9Sstevel@tonic-gate #include <sys/types.h>
257c478bd9Sstevel@tonic-gate #include <sys/socket.h>
267c478bd9Sstevel@tonic-gate #include <netinet/in.h>
277c478bd9Sstevel@tonic-gate #include <arpa/nameser.h>
287c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
297c478bd9Sstevel@tonic-gate
307c478bd9Sstevel@tonic-gate #include <errno.h>
317c478bd9Sstevel@tonic-gate #include <stdio.h>
327c478bd9Sstevel@tonic-gate #include <string.h>
337c478bd9Sstevel@tonic-gate #include <stdlib.h>
347c478bd9Sstevel@tonic-gate
357c478bd9Sstevel@tonic-gate #include "port_after.h"
367c478bd9Sstevel@tonic-gate
377c478bd9Sstevel@tonic-gate #ifdef SPRINTF_CHAR
387c478bd9Sstevel@tonic-gate # define SPRINTF(x) strlen(sprintf/**/x)
397c478bd9Sstevel@tonic-gate #else
407c478bd9Sstevel@tonic-gate # define SPRINTF(x) ((size_t)sprintf x)
417c478bd9Sstevel@tonic-gate #endif
427c478bd9Sstevel@tonic-gate
439525b14bSRao Shoaib static char *
449525b14bSRao Shoaib inet_cidr_ntop_ipv4(const u_char *src, int bits, char *dst, size_t size);
459525b14bSRao Shoaib static char *
469525b14bSRao Shoaib inet_cidr_ntop_ipv6(const u_char *src, int bits, char *dst, size_t size);
477c478bd9Sstevel@tonic-gate
489525b14bSRao Shoaib /*%
497c478bd9Sstevel@tonic-gate * char *
507c478bd9Sstevel@tonic-gate * inet_cidr_ntop(af, src, bits, dst, size)
517c478bd9Sstevel@tonic-gate * convert network address from network to presentation format.
527c478bd9Sstevel@tonic-gate * "src"'s size is determined from its "af".
537c478bd9Sstevel@tonic-gate * return:
547c478bd9Sstevel@tonic-gate * pointer to dst, or NULL if an error occurred (check errno).
557c478bd9Sstevel@tonic-gate * note:
567c478bd9Sstevel@tonic-gate * 192.5.5.1/28 has a nonzero host part, which means it isn't a network
577c478bd9Sstevel@tonic-gate * as called for by inet_net_ntop() but it can be a host address with
587c478bd9Sstevel@tonic-gate * an included netmask.
597c478bd9Sstevel@tonic-gate * author:
607c478bd9Sstevel@tonic-gate * Paul Vixie (ISC), October 1998
617c478bd9Sstevel@tonic-gate */
627c478bd9Sstevel@tonic-gate char *
inet_cidr_ntop(int af,const void * src,int bits,char * dst,size_t size)637c478bd9Sstevel@tonic-gate inet_cidr_ntop(int af, const void *src, int bits, char *dst, size_t size) {
647c478bd9Sstevel@tonic-gate switch (af) {
657c478bd9Sstevel@tonic-gate case AF_INET:
667c478bd9Sstevel@tonic-gate return (inet_cidr_ntop_ipv4(src, bits, dst, size));
677c478bd9Sstevel@tonic-gate case AF_INET6:
687c478bd9Sstevel@tonic-gate return (inet_cidr_ntop_ipv6(src, bits, dst, size));
697c478bd9Sstevel@tonic-gate default:
707c478bd9Sstevel@tonic-gate errno = EAFNOSUPPORT;
717c478bd9Sstevel@tonic-gate return (NULL);
727c478bd9Sstevel@tonic-gate }
737c478bd9Sstevel@tonic-gate }
747c478bd9Sstevel@tonic-gate
757c478bd9Sstevel@tonic-gate static int
decoct(const u_char * src,int bytes,char * dst,size_t size)767c478bd9Sstevel@tonic-gate decoct(const u_char *src, int bytes, char *dst, size_t size) {
777c478bd9Sstevel@tonic-gate char *odst = dst;
787c478bd9Sstevel@tonic-gate char *t;
797c478bd9Sstevel@tonic-gate int b;
807c478bd9Sstevel@tonic-gate
817c478bd9Sstevel@tonic-gate for (b = 1; b <= bytes; b++) {
827c478bd9Sstevel@tonic-gate if (size < sizeof "255.")
837c478bd9Sstevel@tonic-gate return (0);
847c478bd9Sstevel@tonic-gate t = dst;
857c478bd9Sstevel@tonic-gate dst += SPRINTF((dst, "%u", *src++));
867c478bd9Sstevel@tonic-gate if (b != bytes) {
877c478bd9Sstevel@tonic-gate *dst++ = '.';
887c478bd9Sstevel@tonic-gate *dst = '\0';
897c478bd9Sstevel@tonic-gate }
907c478bd9Sstevel@tonic-gate size -= (size_t)(dst - t);
917c478bd9Sstevel@tonic-gate }
927c478bd9Sstevel@tonic-gate return (dst - odst);
937c478bd9Sstevel@tonic-gate }
947c478bd9Sstevel@tonic-gate
959525b14bSRao Shoaib /*%
967c478bd9Sstevel@tonic-gate * static char *
977c478bd9Sstevel@tonic-gate * inet_cidr_ntop_ipv4(src, bits, dst, size)
987c478bd9Sstevel@tonic-gate * convert IPv4 network address from network to presentation format.
997c478bd9Sstevel@tonic-gate * "src"'s size is determined from its "af".
1007c478bd9Sstevel@tonic-gate * return:
1017c478bd9Sstevel@tonic-gate * pointer to dst, or NULL if an error occurred (check errno).
1027c478bd9Sstevel@tonic-gate * note:
1037c478bd9Sstevel@tonic-gate * network byte order assumed. this means 192.5.5.240/28 has
1047c478bd9Sstevel@tonic-gate * 0b11110000 in its fourth octet.
1057c478bd9Sstevel@tonic-gate * author:
1067c478bd9Sstevel@tonic-gate * Paul Vixie (ISC), October 1998
1077c478bd9Sstevel@tonic-gate */
1087c478bd9Sstevel@tonic-gate static char *
inet_cidr_ntop_ipv4(const u_char * src,int bits,char * dst,size_t size)1097c478bd9Sstevel@tonic-gate inet_cidr_ntop_ipv4(const u_char *src, int bits, char *dst, size_t size) {
1107c478bd9Sstevel@tonic-gate char *odst = dst;
1117c478bd9Sstevel@tonic-gate size_t len = 4;
1127c478bd9Sstevel@tonic-gate size_t b;
1137c478bd9Sstevel@tonic-gate size_t bytes;
1147c478bd9Sstevel@tonic-gate
1157c478bd9Sstevel@tonic-gate if ((bits < -1) || (bits > 32)) {
1167c478bd9Sstevel@tonic-gate errno = EINVAL;
1177c478bd9Sstevel@tonic-gate return (NULL);
1187c478bd9Sstevel@tonic-gate }
1197c478bd9Sstevel@tonic-gate
1207c478bd9Sstevel@tonic-gate /* Find number of significant bytes in address. */
1217c478bd9Sstevel@tonic-gate if (bits == -1)
1227c478bd9Sstevel@tonic-gate len = 4;
1237c478bd9Sstevel@tonic-gate else
1249525b14bSRao Shoaib for (len = 1, b = 1 ; b < 4U; b++)
1257c478bd9Sstevel@tonic-gate if (*(src + b))
1267c478bd9Sstevel@tonic-gate len = b + 1;
1277c478bd9Sstevel@tonic-gate
1287c478bd9Sstevel@tonic-gate /* Format whole octets plus nonzero trailing octets. */
1297c478bd9Sstevel@tonic-gate bytes = (((bits <= 0) ? 1 : bits) + 7) / 8;
1307c478bd9Sstevel@tonic-gate if (len > bytes)
1317c478bd9Sstevel@tonic-gate bytes = len;
1327c478bd9Sstevel@tonic-gate b = decoct(src, bytes, dst, size);
1339525b14bSRao Shoaib if (b == 0U)
1347c478bd9Sstevel@tonic-gate goto emsgsize;
1357c478bd9Sstevel@tonic-gate dst += b;
1367c478bd9Sstevel@tonic-gate size -= b;
1377c478bd9Sstevel@tonic-gate
1387c478bd9Sstevel@tonic-gate if (bits != -1) {
1397c478bd9Sstevel@tonic-gate /* Format CIDR /width. */
1407c478bd9Sstevel@tonic-gate if (size < sizeof "/32")
1417c478bd9Sstevel@tonic-gate goto emsgsize;
1427c478bd9Sstevel@tonic-gate dst += SPRINTF((dst, "/%u", bits));
1437c478bd9Sstevel@tonic-gate }
1447c478bd9Sstevel@tonic-gate
1457c478bd9Sstevel@tonic-gate return (odst);
1467c478bd9Sstevel@tonic-gate
1477c478bd9Sstevel@tonic-gate emsgsize:
1487c478bd9Sstevel@tonic-gate errno = EMSGSIZE;
1497c478bd9Sstevel@tonic-gate return (NULL);
1507c478bd9Sstevel@tonic-gate }
151*55fea89dSDan Cross
1527c478bd9Sstevel@tonic-gate static char *
inet_cidr_ntop_ipv6(const u_char * src,int bits,char * dst,size_t size)1537c478bd9Sstevel@tonic-gate inet_cidr_ntop_ipv6(const u_char *src, int bits, char *dst, size_t size) {
1547c478bd9Sstevel@tonic-gate /*
1557c478bd9Sstevel@tonic-gate * Note that int32_t and int16_t need only be "at least" large enough
1567c478bd9Sstevel@tonic-gate * to contain a value of the specified size. On some systems, like
1577c478bd9Sstevel@tonic-gate * Crays, there is no such thing as an integer variable with 16 bits.
1587c478bd9Sstevel@tonic-gate * Keep this in mind if you think this function should have been coded
1597c478bd9Sstevel@tonic-gate * to use pointer overlays. All the world's not a VAX.
1607c478bd9Sstevel@tonic-gate */
1617c478bd9Sstevel@tonic-gate char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255/128"];
1627c478bd9Sstevel@tonic-gate char *tp;
1637c478bd9Sstevel@tonic-gate struct { int base, len; } best, cur;
1647c478bd9Sstevel@tonic-gate u_int words[NS_IN6ADDRSZ / NS_INT16SZ];
1657c478bd9Sstevel@tonic-gate int i;
1667c478bd9Sstevel@tonic-gate
1677c478bd9Sstevel@tonic-gate if ((bits < -1) || (bits > 128)) {
1687c478bd9Sstevel@tonic-gate errno = EINVAL;
1697c478bd9Sstevel@tonic-gate return (NULL);
1707c478bd9Sstevel@tonic-gate }
1717c478bd9Sstevel@tonic-gate
1727c478bd9Sstevel@tonic-gate /*
1737c478bd9Sstevel@tonic-gate * Preprocess:
1747c478bd9Sstevel@tonic-gate * Copy the input (bytewise) array into a wordwise array.
1757c478bd9Sstevel@tonic-gate * Find the longest run of 0x00's in src[] for :: shorthanding.
1767c478bd9Sstevel@tonic-gate */
1777c478bd9Sstevel@tonic-gate memset(words, '\0', sizeof words);
1787c478bd9Sstevel@tonic-gate for (i = 0; i < NS_IN6ADDRSZ; i++)
1797c478bd9Sstevel@tonic-gate words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
1807c478bd9Sstevel@tonic-gate best.base = -1;
1819525b14bSRao Shoaib best.len = 0;
1827c478bd9Sstevel@tonic-gate cur.base = -1;
1839525b14bSRao Shoaib cur.len = 0;
1847c478bd9Sstevel@tonic-gate for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
1857c478bd9Sstevel@tonic-gate if (words[i] == 0) {
1867c478bd9Sstevel@tonic-gate if (cur.base == -1)
1877c478bd9Sstevel@tonic-gate cur.base = i, cur.len = 1;
1887c478bd9Sstevel@tonic-gate else
1897c478bd9Sstevel@tonic-gate cur.len++;
1907c478bd9Sstevel@tonic-gate } else {
1917c478bd9Sstevel@tonic-gate if (cur.base != -1) {
1927c478bd9Sstevel@tonic-gate if (best.base == -1 || cur.len > best.len)
1937c478bd9Sstevel@tonic-gate best = cur;
1947c478bd9Sstevel@tonic-gate cur.base = -1;
1957c478bd9Sstevel@tonic-gate }
1967c478bd9Sstevel@tonic-gate }
1977c478bd9Sstevel@tonic-gate }
1987c478bd9Sstevel@tonic-gate if (cur.base != -1) {
1997c478bd9Sstevel@tonic-gate if (best.base == -1 || cur.len > best.len)
2007c478bd9Sstevel@tonic-gate best = cur;
2017c478bd9Sstevel@tonic-gate }
2027c478bd9Sstevel@tonic-gate if (best.base != -1 && best.len < 2)
2037c478bd9Sstevel@tonic-gate best.base = -1;
2047c478bd9Sstevel@tonic-gate
2057c478bd9Sstevel@tonic-gate /*
2067c478bd9Sstevel@tonic-gate * Format the result.
2077c478bd9Sstevel@tonic-gate */
2087c478bd9Sstevel@tonic-gate tp = tmp;
2097c478bd9Sstevel@tonic-gate for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
2107c478bd9Sstevel@tonic-gate /* Are we inside the best run of 0x00's? */
2117c478bd9Sstevel@tonic-gate if (best.base != -1 && i >= best.base &&
2127c478bd9Sstevel@tonic-gate i < (best.base + best.len)) {
2137c478bd9Sstevel@tonic-gate if (i == best.base)
2147c478bd9Sstevel@tonic-gate *tp++ = ':';
2157c478bd9Sstevel@tonic-gate continue;
2167c478bd9Sstevel@tonic-gate }
2177c478bd9Sstevel@tonic-gate /* Are we following an initial run of 0x00s or any real hex? */
2187c478bd9Sstevel@tonic-gate if (i != 0)
2197c478bd9Sstevel@tonic-gate *tp++ = ':';
2207c478bd9Sstevel@tonic-gate /* Is this address an encapsulated IPv4? */
2217c478bd9Sstevel@tonic-gate if (i == 6 && best.base == 0 && (best.len == 6 ||
2227c478bd9Sstevel@tonic-gate (best.len == 7 && words[7] != 0x0001) ||
2237c478bd9Sstevel@tonic-gate (best.len == 5 && words[5] == 0xffff))) {
2247c478bd9Sstevel@tonic-gate int n;
2257c478bd9Sstevel@tonic-gate
2267c478bd9Sstevel@tonic-gate if (src[15] || bits == -1 || bits > 120)
2277c478bd9Sstevel@tonic-gate n = 4;
2287c478bd9Sstevel@tonic-gate else if (src[14] || bits > 112)
2297c478bd9Sstevel@tonic-gate n = 3;
2307c478bd9Sstevel@tonic-gate else
2317c478bd9Sstevel@tonic-gate n = 2;
2327c478bd9Sstevel@tonic-gate n = decoct(src+12, n, tp, sizeof tmp - (tp - tmp));
2337c478bd9Sstevel@tonic-gate if (n == 0) {
2347c478bd9Sstevel@tonic-gate errno = EMSGSIZE;
2357c478bd9Sstevel@tonic-gate return (NULL);
2367c478bd9Sstevel@tonic-gate }
2377c478bd9Sstevel@tonic-gate tp += strlen(tp);
2387c478bd9Sstevel@tonic-gate break;
2397c478bd9Sstevel@tonic-gate }
2407c478bd9Sstevel@tonic-gate tp += SPRINTF((tp, "%x", words[i]));
2417c478bd9Sstevel@tonic-gate }
2427c478bd9Sstevel@tonic-gate
2437c478bd9Sstevel@tonic-gate /* Was it a trailing run of 0x00's? */
244*55fea89dSDan Cross if (best.base != -1 && (best.base + best.len) ==
2457c478bd9Sstevel@tonic-gate (NS_IN6ADDRSZ / NS_INT16SZ))
2467c478bd9Sstevel@tonic-gate *tp++ = ':';
2477c478bd9Sstevel@tonic-gate *tp = '\0';
2487c478bd9Sstevel@tonic-gate
2497c478bd9Sstevel@tonic-gate if (bits != -1)
2507c478bd9Sstevel@tonic-gate tp += SPRINTF((tp, "/%u", bits));
2517c478bd9Sstevel@tonic-gate
2527c478bd9Sstevel@tonic-gate /*
2537c478bd9Sstevel@tonic-gate * Check for overflow, copy, and we're done.
2547c478bd9Sstevel@tonic-gate */
2557c478bd9Sstevel@tonic-gate if ((size_t)(tp - tmp) > size) {
2567c478bd9Sstevel@tonic-gate errno = EMSGSIZE;
2577c478bd9Sstevel@tonic-gate return (NULL);
2587c478bd9Sstevel@tonic-gate }
2597c478bd9Sstevel@tonic-gate strcpy(dst, tmp);
2607c478bd9Sstevel@tonic-gate return (dst);
2617c478bd9Sstevel@tonic-gate }
2629525b14bSRao Shoaib
2639525b14bSRao Shoaib /*! \file */
264