Home | History | Annotate | Line # | Download | only in inet
inet_cidr_ntop.c revision 1.1
      1 /*	$NetBSD: inet_cidr_ntop.c,v 1.1 2004/05/20 22:29:02 christos Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
      5  * Copyright (c) 1998,1999 by Internet Software Consortium.
      6  *
      7  * Permission to use, copy, modify, and distribute this software for any
      8  * purpose with or without fee is hereby granted, provided that the above
      9  * copyright notice and this permission notice appear in all copies.
     10  *
     11  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
     12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     13  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
     14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
     17  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     18  */
     19 
     20 #if defined(LIBC_SCCS) && !defined(lint)
     21 static const char rcsid[] = "Id: inet_cidr_ntop.c,v 1.1.2.1.8.2 2004/03/17 00:29:46 marka Exp";
     22 #endif
     23 
     24 #include "port_before.h"
     25 
     26 #include <sys/types.h>
     27 #include <sys/socket.h>
     28 #include <netinet/in.h>
     29 #include <arpa/nameser.h>
     30 #include <arpa/inet.h>
     31 
     32 #include <errno.h>
     33 #include <stdio.h>
     34 #include <string.h>
     35 #include <stdlib.h>
     36 
     37 #include "port_after.h"
     38 
     39 #ifdef SPRINTF_CHAR
     40 # define SPRINTF(x) strlen(sprintf/**/x)
     41 #else
     42 # define SPRINTF(x) ((size_t)sprintf x)
     43 #endif
     44 
     45 static char *	inet_cidr_ntop_ipv4 __P((const u_char *src, int bits,
     46 					 char *dst, size_t size));
     47 static char *	inet_cidr_ntop_ipv6 __P((const u_char *src, int bits,
     48 					 char *dst, size_t size));
     49 
     50 /*
     51  * char *
     52  * inet_cidr_ntop(af, src, bits, dst, size)
     53  *	convert network address from network to presentation format.
     54  *	"src"'s size is determined from its "af".
     55  * return:
     56  *	pointer to dst, or NULL if an error occurred (check errno).
     57  * note:
     58  *	192.5.5.1/28 has a nonzero host part, which means it isn't a network
     59  *	as called for by inet_net_ntop() but it can be a host address with
     60  *	an included netmask.
     61  * author:
     62  *	Paul Vixie (ISC), October 1998
     63  */
     64 char *
     65 inet_cidr_ntop(int af, const void *src, int bits, char *dst, size_t size) {
     66 	switch (af) {
     67 	case AF_INET:
     68 		return (inet_cidr_ntop_ipv4(src, bits, dst, size));
     69 	case AF_INET6:
     70 		return (inet_cidr_ntop_ipv6(src, bits, dst, size));
     71 	default:
     72 		errno = EAFNOSUPPORT;
     73 		return (NULL);
     74 	}
     75 }
     76 
     77 static int
     78 decoct(const u_char *src, int bytes, char *dst, size_t size) {
     79 	char *odst = dst;
     80 	char *t;
     81 	int b;
     82 
     83 	for (b = 1; b <= bytes; b++) {
     84 		if (size < sizeof "255.")
     85 			return (0);
     86 		t = dst;
     87 		dst += SPRINTF((dst, "%u", *src++));
     88 		if (b != bytes) {
     89 			*dst++ = '.';
     90 			*dst = '\0';
     91 		}
     92 		size -= (size_t)(dst - t);
     93 	}
     94 	return (dst - odst);
     95 }
     96 
     97 /*
     98  * static char *
     99  * inet_cidr_ntop_ipv4(src, bits, dst, size)
    100  *	convert IPv4 network address from network to presentation format.
    101  *	"src"'s size is determined from its "af".
    102  * return:
    103  *	pointer to dst, or NULL if an error occurred (check errno).
    104  * note:
    105  *	network byte order assumed.  this means 192.5.5.240/28 has
    106  *	0b11110000 in its fourth octet.
    107  * author:
    108  *	Paul Vixie (ISC), October 1998
    109  */
    110 static char *
    111 inet_cidr_ntop_ipv4(const u_char *src, int bits, char *dst, size_t size) {
    112 	char *odst = dst;
    113 	size_t len = 4;
    114 	size_t b;
    115 	size_t bytes;
    116 
    117 	if ((bits < -1) || (bits > 32)) {
    118 		errno = EINVAL;
    119 		return (NULL);
    120 	}
    121 
    122 	/* Find number of significant bytes in address. */
    123 	if (bits == -1)
    124 		len = 4;
    125 	else
    126 		for (len = 1, b = 1 ; b < 4U; b++)
    127 			if (*(src + b))
    128 				len = b + 1;
    129 
    130 	/* Format whole octets plus nonzero trailing octets. */
    131 	bytes = (((bits <= 0) ? 1 : bits) + 7) / 8;
    132 	if (len > bytes)
    133 		bytes = len;
    134 	b = decoct(src, bytes, dst, size);
    135 	if (b == 0U)
    136 		goto emsgsize;
    137 	dst += b;
    138 	size -= b;
    139 
    140 	if (bits != -1) {
    141 		/* Format CIDR /width. */
    142 		if (size < sizeof "/32")
    143 			goto emsgsize;
    144 		dst += SPRINTF((dst, "/%u", bits));
    145 	}
    146 
    147 	return (odst);
    148 
    149  emsgsize:
    150 	errno = EMSGSIZE;
    151 	return (NULL);
    152 }
    153 
    154 static char *
    155 inet_cidr_ntop_ipv6(const u_char *src, int bits, char *dst, size_t size) {
    156 	/*
    157 	 * Note that int32_t and int16_t need only be "at least" large enough
    158 	 * to contain a value of the specified size.  On some systems, like
    159 	 * Crays, there is no such thing as an integer variable with 16 bits.
    160 	 * Keep this in mind if you think this function should have been coded
    161 	 * to use pointer overlays.  All the world's not a VAX.
    162 	 */
    163 	char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255/128"];
    164 	char *tp;
    165 	struct { int base, len; } best, cur;
    166 	u_int words[NS_IN6ADDRSZ / NS_INT16SZ];
    167 	int i;
    168 
    169 	if ((bits < -1) || (bits > 128)) {
    170 		errno = EINVAL;
    171 		return (NULL);
    172 	}
    173 
    174 	/*
    175 	 * Preprocess:
    176 	 *	Copy the input (bytewise) array into a wordwise array.
    177 	 *	Find the longest run of 0x00's in src[] for :: shorthanding.
    178 	 */
    179 	memset(words, '\0', sizeof words);
    180 	for (i = 0; i < NS_IN6ADDRSZ; i++)
    181 		words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
    182 	best.base = -1;
    183 	cur.base = -1;
    184 	for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
    185 		if (words[i] == 0) {
    186 			if (cur.base == -1)
    187 				cur.base = i, cur.len = 1;
    188 			else
    189 				cur.len++;
    190 		} else {
    191 			if (cur.base != -1) {
    192 				if (best.base == -1 || cur.len > best.len)
    193 					best = cur;
    194 				cur.base = -1;
    195 			}
    196 		}
    197 	}
    198 	if (cur.base != -1) {
    199 		if (best.base == -1 || cur.len > best.len)
    200 			best = cur;
    201 	}
    202 	if (best.base != -1 && best.len < 2)
    203 		best.base = -1;
    204 
    205 	/*
    206 	 * Format the result.
    207 	 */
    208 	tp = tmp;
    209 	for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
    210 		/* Are we inside the best run of 0x00's? */
    211 		if (best.base != -1 && i >= best.base &&
    212 		    i < (best.base + best.len)) {
    213 			if (i == best.base)
    214 				*tp++ = ':';
    215 			continue;
    216 		}
    217 		/* Are we following an initial run of 0x00s or any real hex? */
    218 		if (i != 0)
    219 			*tp++ = ':';
    220 		/* Is this address an encapsulated IPv4? */
    221 		if (i == 6 && best.base == 0 && (best.len == 6 ||
    222 		    (best.len == 7 && words[7] != 0x0001) ||
    223 		    (best.len == 5 && words[5] == 0xffff))) {
    224 			int n;
    225 
    226 			if (src[15] || bits == -1 || bits > 120)
    227 				n = 4;
    228 			else if (src[14] || bits > 112)
    229 				n = 3;
    230 			else
    231 				n = 2;
    232 			n = decoct(src+12, n, tp, sizeof tmp - (tp - tmp));
    233 			if (n == 0) {
    234 				errno = EMSGSIZE;
    235 				return (NULL);
    236 			}
    237 			tp += strlen(tp);
    238 			break;
    239 		}
    240 		tp += SPRINTF((tp, "%x", words[i]));
    241 	}
    242 
    243 	/* Was it a trailing run of 0x00's? */
    244 	if (best.base != -1 && (best.base + best.len) ==
    245 	    (NS_IN6ADDRSZ / NS_INT16SZ))
    246 		*tp++ = ':';
    247 	*tp = '\0';
    248 
    249 	if (bits != -1)
    250 		tp += SPRINTF((tp, "/%u", bits));
    251 
    252 	/*
    253 	 * Check for overflow, copy, and we're done.
    254 	 */
    255 	if ((size_t)(tp - tmp) > size) {
    256 		errno = EMSGSIZE;
    257 		return (NULL);
    258 	}
    259 	strcpy(dst, tmp);
    260 	return (dst);
    261 }
    262