10Sstevel@tonic-gate /*
2*11038SRao.Shoaib@Sun.COM * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
30Sstevel@tonic-gate * Copyright (c) 1996,1999 by Internet Software Consortium.
40Sstevel@tonic-gate *
50Sstevel@tonic-gate * Permission to use, copy, modify, and distribute this software for any
60Sstevel@tonic-gate * purpose with or without fee is hereby granted, provided that the above
70Sstevel@tonic-gate * copyright notice and this permission notice appear in all copies.
80Sstevel@tonic-gate *
9*11038SRao.Shoaib@Sun.COM * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10*11038SRao.Shoaib@Sun.COM * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11*11038SRao.Shoaib@Sun.COM * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
12*11038SRao.Shoaib@Sun.COM * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13*11038SRao.Shoaib@Sun.COM * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14*11038SRao.Shoaib@Sun.COM * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15*11038SRao.Shoaib@Sun.COM * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
160Sstevel@tonic-gate */
170Sstevel@tonic-gate
180Sstevel@tonic-gate #if defined(LIBC_SCCS) && !defined(lint)
19*11038SRao.Shoaib@Sun.COM static const char rcsid[] = "$Id: inet_net_ntop.c,v 1.5 2006/06/20 02:50:14 marka Exp $";
200Sstevel@tonic-gate #endif
210Sstevel@tonic-gate
220Sstevel@tonic-gate #include "port_before.h"
230Sstevel@tonic-gate
240Sstevel@tonic-gate #include <sys/types.h>
250Sstevel@tonic-gate #include <sys/socket.h>
260Sstevel@tonic-gate #include <netinet/in.h>
270Sstevel@tonic-gate #include <arpa/inet.h>
280Sstevel@tonic-gate
290Sstevel@tonic-gate #include <errno.h>
300Sstevel@tonic-gate #include <stdio.h>
310Sstevel@tonic-gate #include <string.h>
320Sstevel@tonic-gate #include <stdlib.h>
330Sstevel@tonic-gate
340Sstevel@tonic-gate #include "port_after.h"
350Sstevel@tonic-gate
360Sstevel@tonic-gate #ifdef SPRINTF_CHAR
370Sstevel@tonic-gate # define SPRINTF(x) strlen(sprintf/**/x)
380Sstevel@tonic-gate #else
390Sstevel@tonic-gate # define SPRINTF(x) ((size_t)sprintf x)
400Sstevel@tonic-gate #endif
410Sstevel@tonic-gate
420Sstevel@tonic-gate static char * inet_net_ntop_ipv4 __P((const u_char *src, int bits,
430Sstevel@tonic-gate char *dst, size_t size));
440Sstevel@tonic-gate static char * inet_net_ntop_ipv6 __P((const u_char *src, int bits,
450Sstevel@tonic-gate char *dst, size_t size));
460Sstevel@tonic-gate
47*11038SRao.Shoaib@Sun.COM /*%
480Sstevel@tonic-gate * char *
490Sstevel@tonic-gate * inet_net_ntop(af, src, bits, dst, size)
500Sstevel@tonic-gate * convert network number from network to presentation format.
510Sstevel@tonic-gate * generates CIDR style result always.
520Sstevel@tonic-gate * return:
530Sstevel@tonic-gate * pointer to dst, or NULL if an error occurred (check errno).
540Sstevel@tonic-gate * author:
550Sstevel@tonic-gate * Paul Vixie (ISC), July 1996
560Sstevel@tonic-gate */
570Sstevel@tonic-gate char *
inet_net_ntop(af,src,bits,dst,size)580Sstevel@tonic-gate inet_net_ntop(af, src, bits, dst, size)
590Sstevel@tonic-gate int af;
600Sstevel@tonic-gate const void *src;
610Sstevel@tonic-gate int bits;
620Sstevel@tonic-gate char *dst;
630Sstevel@tonic-gate size_t size;
640Sstevel@tonic-gate {
650Sstevel@tonic-gate switch (af) {
660Sstevel@tonic-gate case AF_INET:
670Sstevel@tonic-gate return (inet_net_ntop_ipv4(src, bits, dst, size));
680Sstevel@tonic-gate case AF_INET6:
690Sstevel@tonic-gate return (inet_net_ntop_ipv6(src, bits, dst, size));
700Sstevel@tonic-gate default:
710Sstevel@tonic-gate errno = EAFNOSUPPORT;
720Sstevel@tonic-gate return (NULL);
730Sstevel@tonic-gate }
740Sstevel@tonic-gate }
750Sstevel@tonic-gate
76*11038SRao.Shoaib@Sun.COM /*%
770Sstevel@tonic-gate * static char *
780Sstevel@tonic-gate * inet_net_ntop_ipv4(src, bits, dst, size)
790Sstevel@tonic-gate * convert IPv4 network number from network to presentation format.
800Sstevel@tonic-gate * generates CIDR style result always.
810Sstevel@tonic-gate * return:
820Sstevel@tonic-gate * pointer to dst, or NULL if an error occurred (check errno).
830Sstevel@tonic-gate * note:
840Sstevel@tonic-gate * network byte order assumed. this means 192.5.5.240/28 has
850Sstevel@tonic-gate * 0b11110000 in its fourth octet.
860Sstevel@tonic-gate * author:
870Sstevel@tonic-gate * Paul Vixie (ISC), July 1996
880Sstevel@tonic-gate */
890Sstevel@tonic-gate static char *
inet_net_ntop_ipv4(src,bits,dst,size)900Sstevel@tonic-gate inet_net_ntop_ipv4(src, bits, dst, size)
910Sstevel@tonic-gate const u_char *src;
920Sstevel@tonic-gate int bits;
930Sstevel@tonic-gate char *dst;
940Sstevel@tonic-gate size_t size;
950Sstevel@tonic-gate {
960Sstevel@tonic-gate char *odst = dst;
970Sstevel@tonic-gate char *t;
980Sstevel@tonic-gate u_int m;
990Sstevel@tonic-gate int b;
1000Sstevel@tonic-gate
1010Sstevel@tonic-gate if (bits < 0 || bits > 32) {
1020Sstevel@tonic-gate errno = EINVAL;
1030Sstevel@tonic-gate return (NULL);
1040Sstevel@tonic-gate }
1050Sstevel@tonic-gate
1060Sstevel@tonic-gate if (bits == 0) {
1070Sstevel@tonic-gate if (size < sizeof "0")
1080Sstevel@tonic-gate goto emsgsize;
1090Sstevel@tonic-gate *dst++ = '0';
1100Sstevel@tonic-gate size--;
1110Sstevel@tonic-gate *dst = '\0';
1120Sstevel@tonic-gate }
1130Sstevel@tonic-gate
1140Sstevel@tonic-gate /* Format whole octets. */
1150Sstevel@tonic-gate for (b = bits / 8; b > 0; b--) {
1160Sstevel@tonic-gate if (size <= sizeof "255.")
1170Sstevel@tonic-gate goto emsgsize;
1180Sstevel@tonic-gate t = dst;
1190Sstevel@tonic-gate dst += SPRINTF((dst, "%u", *src++));
1200Sstevel@tonic-gate if (b > 1) {
1210Sstevel@tonic-gate *dst++ = '.';
1220Sstevel@tonic-gate *dst = '\0';
1230Sstevel@tonic-gate }
1240Sstevel@tonic-gate size -= (size_t)(dst - t);
1250Sstevel@tonic-gate }
1260Sstevel@tonic-gate
1270Sstevel@tonic-gate /* Format partial octet. */
1280Sstevel@tonic-gate b = bits % 8;
1290Sstevel@tonic-gate if (b > 0) {
1300Sstevel@tonic-gate if (size <= sizeof ".255")
1310Sstevel@tonic-gate goto emsgsize;
1320Sstevel@tonic-gate t = dst;
1330Sstevel@tonic-gate if (dst != odst)
1340Sstevel@tonic-gate *dst++ = '.';
1350Sstevel@tonic-gate m = ((1 << b) - 1) << (8 - b);
1360Sstevel@tonic-gate dst += SPRINTF((dst, "%u", *src & m));
1370Sstevel@tonic-gate size -= (size_t)(dst - t);
1380Sstevel@tonic-gate }
1390Sstevel@tonic-gate
1400Sstevel@tonic-gate /* Format CIDR /width. */
1410Sstevel@tonic-gate if (size <= sizeof "/32")
1420Sstevel@tonic-gate goto emsgsize;
1430Sstevel@tonic-gate dst += SPRINTF((dst, "/%u", bits));
1440Sstevel@tonic-gate return (odst);
1450Sstevel@tonic-gate
1460Sstevel@tonic-gate emsgsize:
1470Sstevel@tonic-gate errno = EMSGSIZE;
1480Sstevel@tonic-gate return (NULL);
1490Sstevel@tonic-gate }
1500Sstevel@tonic-gate
151*11038SRao.Shoaib@Sun.COM /*%
1520Sstevel@tonic-gate * static char *
1530Sstevel@tonic-gate * inet_net_ntop_ipv6(src, bits, fakebits, dst, size)
1540Sstevel@tonic-gate * convert IPv6 network number from network to presentation format.
1550Sstevel@tonic-gate * generates CIDR style result always. Picks the shortest representation
1560Sstevel@tonic-gate * unless the IP is really IPv4.
1570Sstevel@tonic-gate * always prints specified number of bits (bits).
1580Sstevel@tonic-gate * return:
1590Sstevel@tonic-gate * pointer to dst, or NULL if an error occurred (check errno).
1600Sstevel@tonic-gate * note:
1610Sstevel@tonic-gate * network byte order assumed. this means 192.5.5.240/28 has
162*11038SRao.Shoaib@Sun.COM * 0x11110000 in its fourth octet.
1630Sstevel@tonic-gate * author:
1640Sstevel@tonic-gate * Vadim Kogan (UCB), June 2001
1650Sstevel@tonic-gate * Original version (IPv4) by Paul Vixie (ISC), July 1996
1660Sstevel@tonic-gate */
1670Sstevel@tonic-gate
1680Sstevel@tonic-gate static char *
inet_net_ntop_ipv6(const u_char * src,int bits,char * dst,size_t size)1690Sstevel@tonic-gate inet_net_ntop_ipv6(const u_char *src, int bits, char *dst, size_t size) {
1700Sstevel@tonic-gate u_int m;
1710Sstevel@tonic-gate int b;
1720Sstevel@tonic-gate int p;
1730Sstevel@tonic-gate int zero_s, zero_l, tmp_zero_s, tmp_zero_l;
1740Sstevel@tonic-gate int i;
1750Sstevel@tonic-gate int is_ipv4 = 0;
1760Sstevel@tonic-gate unsigned char inbuf[16];
1770Sstevel@tonic-gate char outbuf[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255/128")];
1780Sstevel@tonic-gate char *cp;
1790Sstevel@tonic-gate int words;
1800Sstevel@tonic-gate u_char *s;
1810Sstevel@tonic-gate
1820Sstevel@tonic-gate if (bits < 0 || bits > 128) {
1830Sstevel@tonic-gate errno = EINVAL;
1840Sstevel@tonic-gate return (NULL);
1850Sstevel@tonic-gate }
1860Sstevel@tonic-gate
1870Sstevel@tonic-gate cp = outbuf;
1880Sstevel@tonic-gate
1890Sstevel@tonic-gate if (bits == 0) {
1900Sstevel@tonic-gate *cp++ = ':';
1910Sstevel@tonic-gate *cp++ = ':';
1920Sstevel@tonic-gate *cp = '\0';
1930Sstevel@tonic-gate } else {
1940Sstevel@tonic-gate /* Copy src to private buffer. Zero host part. */
1950Sstevel@tonic-gate p = (bits + 7) / 8;
1960Sstevel@tonic-gate memcpy(inbuf, src, p);
1970Sstevel@tonic-gate memset(inbuf + p, 0, 16 - p);
1980Sstevel@tonic-gate b = bits % 8;
1990Sstevel@tonic-gate if (b != 0) {
2000Sstevel@tonic-gate m = ~0 << (8 - b);
2010Sstevel@tonic-gate inbuf[p-1] &= m;
2020Sstevel@tonic-gate }
2030Sstevel@tonic-gate
2040Sstevel@tonic-gate s = inbuf;
2050Sstevel@tonic-gate
2060Sstevel@tonic-gate /* how many words need to be displayed in output */
2070Sstevel@tonic-gate words = (bits + 15) / 16;
2080Sstevel@tonic-gate if (words == 1)
2090Sstevel@tonic-gate words = 2;
2100Sstevel@tonic-gate
2110Sstevel@tonic-gate /* Find the longest substring of zero's */
2120Sstevel@tonic-gate zero_s = zero_l = tmp_zero_s = tmp_zero_l = 0;
2130Sstevel@tonic-gate for (i = 0; i < (words * 2); i += 2) {
2140Sstevel@tonic-gate if ((s[i] | s[i+1]) == 0) {
2150Sstevel@tonic-gate if (tmp_zero_l == 0)
2160Sstevel@tonic-gate tmp_zero_s = i / 2;
2170Sstevel@tonic-gate tmp_zero_l++;
2180Sstevel@tonic-gate } else {
2190Sstevel@tonic-gate if (tmp_zero_l && zero_l < tmp_zero_l) {
2200Sstevel@tonic-gate zero_s = tmp_zero_s;
2210Sstevel@tonic-gate zero_l = tmp_zero_l;
2220Sstevel@tonic-gate tmp_zero_l = 0;
2230Sstevel@tonic-gate }
2240Sstevel@tonic-gate }
2250Sstevel@tonic-gate }
2260Sstevel@tonic-gate
2270Sstevel@tonic-gate if (tmp_zero_l && zero_l < tmp_zero_l) {
2280Sstevel@tonic-gate zero_s = tmp_zero_s;
2290Sstevel@tonic-gate zero_l = tmp_zero_l;
2300Sstevel@tonic-gate }
2310Sstevel@tonic-gate
2320Sstevel@tonic-gate if (zero_l != words && zero_s == 0 && ((zero_l == 6) ||
2330Sstevel@tonic-gate ((zero_l == 5 && s[10] == 0xff && s[11] == 0xff) ||
2340Sstevel@tonic-gate ((zero_l == 7 && s[14] != 0 && s[15] != 1)))))
2350Sstevel@tonic-gate is_ipv4 = 1;
2360Sstevel@tonic-gate
2370Sstevel@tonic-gate /* Format whole words. */
2380Sstevel@tonic-gate for (p = 0; p < words; p++) {
2390Sstevel@tonic-gate if (zero_l != 0 && p >= zero_s && p < zero_s + zero_l) {
2400Sstevel@tonic-gate /* Time to skip some zeros */
2410Sstevel@tonic-gate if (p == zero_s)
2420Sstevel@tonic-gate *cp++ = ':';
2430Sstevel@tonic-gate if (p == words - 1)
2440Sstevel@tonic-gate *cp++ = ':';
2450Sstevel@tonic-gate s++;
2460Sstevel@tonic-gate s++;
2470Sstevel@tonic-gate continue;
2480Sstevel@tonic-gate }
2490Sstevel@tonic-gate
2500Sstevel@tonic-gate if (is_ipv4 && p > 5 ) {
2510Sstevel@tonic-gate *cp++ = (p == 6) ? ':' : '.';
2520Sstevel@tonic-gate cp += SPRINTF((cp, "%u", *s++));
2530Sstevel@tonic-gate /* we can potentially drop the last octet */
2540Sstevel@tonic-gate if (p != 7 || bits > 120) {
2550Sstevel@tonic-gate *cp++ = '.';
2560Sstevel@tonic-gate cp += SPRINTF((cp, "%u", *s++));
2570Sstevel@tonic-gate }
2580Sstevel@tonic-gate } else {
2590Sstevel@tonic-gate if (cp != outbuf)
2600Sstevel@tonic-gate *cp++ = ':';
2610Sstevel@tonic-gate cp += SPRINTF((cp, "%x", *s * 256 + s[1]));
2620Sstevel@tonic-gate s += 2;
2630Sstevel@tonic-gate }
2640Sstevel@tonic-gate }
2650Sstevel@tonic-gate }
2660Sstevel@tonic-gate /* Format CIDR /width. */
267*11038SRao.Shoaib@Sun.COM sprintf(cp, "/%u", bits);
2680Sstevel@tonic-gate if (strlen(outbuf) + 1 > size)
2690Sstevel@tonic-gate goto emsgsize;
2700Sstevel@tonic-gate strcpy(dst, outbuf);
2710Sstevel@tonic-gate
2720Sstevel@tonic-gate return (dst);
2730Sstevel@tonic-gate
2740Sstevel@tonic-gate emsgsize:
2750Sstevel@tonic-gate errno = EMSGSIZE;
2760Sstevel@tonic-gate return (NULL);
2770Sstevel@tonic-gate }
278*11038SRao.Shoaib@Sun.COM
279*11038SRao.Shoaib@Sun.COM /*! \file */
280