1*e6e30c83Schristos /*	$NetBSD: inet_net_ntop.c,v 1.1.1.2 2012/09/09 16:07:50 christos Exp $	*/
2b5677b36Schristos 
3b5677b36Schristos /*
4b5677b36Schristos  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5b5677b36Schristos  * Copyright (c) 1996,1999 by Internet Software Consortium.
6b5677b36Schristos  *
7b5677b36Schristos  * Permission to use, copy, modify, and distribute this software for any
8b5677b36Schristos  * purpose with or without fee is hereby granted, provided that the above
9b5677b36Schristos  * copyright notice and this permission notice appear in all copies.
10b5677b36Schristos  *
11b5677b36Schristos  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
12b5677b36Schristos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13b5677b36Schristos  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
14b5677b36Schristos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15b5677b36Schristos  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16b5677b36Schristos  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17b5677b36Schristos  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18b5677b36Schristos  */
19b5677b36Schristos 
20b5677b36Schristos #if defined(LIBC_SCCS) && !defined(lint)
21b5677b36Schristos static const char rcsid[] = "Id: inet_net_ntop.c,v 1.5 2006/06/20 02:50:14 marka Exp ";
22b5677b36Schristos #endif
23b5677b36Schristos 
24b5677b36Schristos #include "port_before.h"
25b5677b36Schristos 
26b5677b36Schristos #include <sys/types.h>
27b5677b36Schristos #include <sys/socket.h>
28b5677b36Schristos #include <netinet/in.h>
29b5677b36Schristos #include <arpa/inet.h>
30b5677b36Schristos 
31b5677b36Schristos #include <errno.h>
32b5677b36Schristos #include <stdio.h>
33b5677b36Schristos #include <string.h>
34b5677b36Schristos #include <stdlib.h>
35b5677b36Schristos 
36b5677b36Schristos #include "port_after.h"
37b5677b36Schristos 
38b5677b36Schristos #ifdef SPRINTF_CHAR
39b5677b36Schristos # define SPRINTF(x) strlen(sprintf/**/x)
40b5677b36Schristos #else
41b5677b36Schristos # define SPRINTF(x) ((size_t)sprintf x)
42b5677b36Schristos #endif
43b5677b36Schristos 
44b5677b36Schristos static char *	inet_net_ntop_ipv4 __P((const u_char *src, int bits,
45b5677b36Schristos 					char *dst, size_t size));
46b5677b36Schristos static char *	inet_net_ntop_ipv6 __P((const u_char *src, int bits,
47b5677b36Schristos 					char *dst, size_t size));
48b5677b36Schristos 
49b5677b36Schristos /*%
50b5677b36Schristos  * char *
51b5677b36Schristos  * inet_net_ntop(af, src, bits, dst, size)
52b5677b36Schristos  *	convert network number from network to presentation format.
53b5677b36Schristos  *	generates CIDR style result always.
54b5677b36Schristos  * return:
55b5677b36Schristos  *	pointer to dst, or NULL if an error occurred (check errno).
56b5677b36Schristos  * author:
57b5677b36Schristos  *	Paul Vixie (ISC), July 1996
58b5677b36Schristos  */
59b5677b36Schristos char *
inet_net_ntop(af,src,bits,dst,size)60b5677b36Schristos inet_net_ntop(af, src, bits, dst, size)
61b5677b36Schristos 	int af;
62b5677b36Schristos 	const void *src;
63b5677b36Schristos 	int bits;
64b5677b36Schristos 	char *dst;
65b5677b36Schristos 	size_t size;
66b5677b36Schristos {
67b5677b36Schristos 	switch (af) {
68b5677b36Schristos 	case AF_INET:
69b5677b36Schristos 		return (inet_net_ntop_ipv4(src, bits, dst, size));
70b5677b36Schristos 	case AF_INET6:
71b5677b36Schristos 		return (inet_net_ntop_ipv6(src, bits, dst, size));
72b5677b36Schristos 	default:
73b5677b36Schristos 		errno = EAFNOSUPPORT;
74b5677b36Schristos 		return (NULL);
75b5677b36Schristos 	}
76b5677b36Schristos }
77b5677b36Schristos 
78b5677b36Schristos /*%
79b5677b36Schristos  * static char *
80b5677b36Schristos  * inet_net_ntop_ipv4(src, bits, dst, size)
81b5677b36Schristos  *	convert IPv4 network number from network to presentation format.
82b5677b36Schristos  *	generates CIDR style result always.
83b5677b36Schristos  * return:
84b5677b36Schristos  *	pointer to dst, or NULL if an error occurred (check errno).
85b5677b36Schristos  * note:
86b5677b36Schristos  *	network byte order assumed.  this means 192.5.5.240/28 has
87b5677b36Schristos  *	0b11110000 in its fourth octet.
88b5677b36Schristos  * author:
89b5677b36Schristos  *	Paul Vixie (ISC), July 1996
90b5677b36Schristos  */
91b5677b36Schristos static char *
inet_net_ntop_ipv4(src,bits,dst,size)92b5677b36Schristos inet_net_ntop_ipv4(src, bits, dst, size)
93b5677b36Schristos 	const u_char *src;
94b5677b36Schristos 	int bits;
95b5677b36Schristos 	char *dst;
96b5677b36Schristos 	size_t size;
97b5677b36Schristos {
98b5677b36Schristos 	char *odst = dst;
99b5677b36Schristos 	char *t;
100b5677b36Schristos 	u_int m;
101b5677b36Schristos 	int b;
102b5677b36Schristos 
103b5677b36Schristos 	if (bits < 0 || bits > 32) {
104b5677b36Schristos 		errno = EINVAL;
105b5677b36Schristos 		return (NULL);
106b5677b36Schristos 	}
107b5677b36Schristos 
108b5677b36Schristos 	if (bits == 0) {
109b5677b36Schristos 		if (size < sizeof "0")
110b5677b36Schristos 			goto emsgsize;
111b5677b36Schristos 		*dst++ = '0';
112b5677b36Schristos 		size--;
113b5677b36Schristos 		*dst = '\0';
114b5677b36Schristos 	}
115b5677b36Schristos 
116b5677b36Schristos 	/* Format whole octets. */
117b5677b36Schristos 	for (b = bits / 8; b > 0; b--) {
118b5677b36Schristos 		if (size <= sizeof "255.")
119b5677b36Schristos 			goto emsgsize;
120b5677b36Schristos 		t = dst;
121b5677b36Schristos 		dst += SPRINTF((dst, "%u", *src++));
122b5677b36Schristos 		if (b > 1) {
123b5677b36Schristos 			*dst++ = '.';
124b5677b36Schristos 			*dst = '\0';
125b5677b36Schristos 		}
126b5677b36Schristos 		size -= (size_t)(dst - t);
127b5677b36Schristos 	}
128b5677b36Schristos 
129b5677b36Schristos 	/* Format partial octet. */
130b5677b36Schristos 	b = bits % 8;
131b5677b36Schristos 	if (b > 0) {
132b5677b36Schristos 		if (size <= sizeof ".255")
133b5677b36Schristos 			goto emsgsize;
134b5677b36Schristos 		t = dst;
135b5677b36Schristos 		if (dst != odst)
136b5677b36Schristos 			*dst++ = '.';
137b5677b36Schristos 		m = ((1 << b) - 1) << (8 - b);
138b5677b36Schristos 		dst += SPRINTF((dst, "%u", *src & m));
139b5677b36Schristos 		size -= (size_t)(dst - t);
140b5677b36Schristos 	}
141b5677b36Schristos 
142b5677b36Schristos 	/* Format CIDR /width. */
143b5677b36Schristos 	if (size <= sizeof "/32")
144b5677b36Schristos 		goto emsgsize;
145b5677b36Schristos 	dst += SPRINTF((dst, "/%u", bits));
146b5677b36Schristos 	return (odst);
147b5677b36Schristos 
148b5677b36Schristos  emsgsize:
149b5677b36Schristos 	errno = EMSGSIZE;
150b5677b36Schristos 	return (NULL);
151b5677b36Schristos }
152b5677b36Schristos 
153b5677b36Schristos /*%
154b5677b36Schristos  * static char *
155b5677b36Schristos  * inet_net_ntop_ipv6(src, bits, fakebits, dst, size)
156b5677b36Schristos  *	convert IPv6 network number from network to presentation format.
157b5677b36Schristos  *	generates CIDR style result always. Picks the shortest representation
158b5677b36Schristos  *	unless the IP is really IPv4.
159b5677b36Schristos  *	always prints specified number of bits (bits).
160b5677b36Schristos  * return:
161b5677b36Schristos  *	pointer to dst, or NULL if an error occurred (check errno).
162b5677b36Schristos  * note:
163b5677b36Schristos  *	network byte order assumed.  this means 192.5.5.240/28 has
164b5677b36Schristos  *	0x11110000 in its fourth octet.
165b5677b36Schristos  * author:
166b5677b36Schristos  *	Vadim Kogan (UCB), June 2001
167b5677b36Schristos  *  Original version (IPv4) by Paul Vixie (ISC), July 1996
168b5677b36Schristos  */
169b5677b36Schristos 
170b5677b36Schristos static char *
inet_net_ntop_ipv6(const u_char * src,int bits,char * dst,size_t size)171b5677b36Schristos inet_net_ntop_ipv6(const u_char *src, int bits, char *dst, size_t size) {
172b5677b36Schristos 	u_int	m;
173b5677b36Schristos 	int	b;
174b5677b36Schristos 	int	p;
175b5677b36Schristos 	int	zero_s, zero_l, tmp_zero_s, tmp_zero_l;
176b5677b36Schristos 	int	i;
177b5677b36Schristos 	int	is_ipv4 = 0;
178b5677b36Schristos 	unsigned char inbuf[16];
179b5677b36Schristos 	char outbuf[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255/128")];
180b5677b36Schristos 	char	*cp;
181b5677b36Schristos 	int	words;
182b5677b36Schristos 	u_char	*s;
183b5677b36Schristos 
184b5677b36Schristos 	if (bits < 0 || bits > 128) {
185b5677b36Schristos 		errno = EINVAL;
186b5677b36Schristos 		return (NULL);
187b5677b36Schristos 	}
188b5677b36Schristos 
189b5677b36Schristos 	cp = outbuf;
190b5677b36Schristos 
191b5677b36Schristos 	if (bits == 0) {
192b5677b36Schristos 		*cp++ = ':';
193b5677b36Schristos 		*cp++ = ':';
194b5677b36Schristos 		*cp = '\0';
195b5677b36Schristos 	} else {
196b5677b36Schristos 		/* Copy src to private buffer.  Zero host part. */
197b5677b36Schristos 		p = (bits + 7) / 8;
198b5677b36Schristos 		memcpy(inbuf, src, p);
199b5677b36Schristos 		memset(inbuf + p, 0, 16 - p);
200b5677b36Schristos 		b = bits % 8;
201b5677b36Schristos 		if (b != 0) {
202b5677b36Schristos 			m = ~0 << (8 - b);
203b5677b36Schristos 			inbuf[p-1] &= m;
204b5677b36Schristos 		}
205b5677b36Schristos 
206b5677b36Schristos 		s = inbuf;
207b5677b36Schristos 
208b5677b36Schristos 		/* how many words need to be displayed in output */
209b5677b36Schristos 		words = (bits + 15) / 16;
210b5677b36Schristos 		if (words == 1)
211b5677b36Schristos 			words = 2;
212b5677b36Schristos 
213b5677b36Schristos 		/* Find the longest substring of zero's */
214b5677b36Schristos 		zero_s = zero_l = tmp_zero_s = tmp_zero_l = 0;
215b5677b36Schristos 		for (i = 0; i < (words * 2); i += 2) {
216b5677b36Schristos 			if ((s[i] | s[i+1]) == 0) {
217b5677b36Schristos 				if (tmp_zero_l == 0)
218b5677b36Schristos 					tmp_zero_s = i / 2;
219b5677b36Schristos 				tmp_zero_l++;
220b5677b36Schristos 			} else {
221b5677b36Schristos 				if (tmp_zero_l && zero_l < tmp_zero_l) {
222b5677b36Schristos 					zero_s = tmp_zero_s;
223b5677b36Schristos 					zero_l = tmp_zero_l;
224b5677b36Schristos 					tmp_zero_l = 0;
225b5677b36Schristos 				}
226b5677b36Schristos 			}
227b5677b36Schristos 		}
228b5677b36Schristos 
229b5677b36Schristos 		if (tmp_zero_l && zero_l < tmp_zero_l) {
230b5677b36Schristos 			zero_s = tmp_zero_s;
231b5677b36Schristos 			zero_l = tmp_zero_l;
232b5677b36Schristos 		}
233b5677b36Schristos 
234b5677b36Schristos 		if (zero_l != words && zero_s == 0 && ((zero_l == 6) ||
235b5677b36Schristos 		    ((zero_l == 5 && s[10] == 0xff && s[11] == 0xff) ||
236b5677b36Schristos 		    ((zero_l == 7 && s[14] != 0 && s[15] != 1)))))
237b5677b36Schristos 			is_ipv4 = 1;
238b5677b36Schristos 
239b5677b36Schristos 		/* Format whole words. */
240b5677b36Schristos 		for (p = 0; p < words; p++) {
241b5677b36Schristos 			if (zero_l != 0 && p >= zero_s && p < zero_s + zero_l) {
242b5677b36Schristos 				/* Time to skip some zeros */
243b5677b36Schristos 				if (p == zero_s)
244b5677b36Schristos 					*cp++ = ':';
245b5677b36Schristos 				if (p == words - 1)
246b5677b36Schristos 					*cp++ = ':';
247b5677b36Schristos 				s++;
248b5677b36Schristos 				s++;
249b5677b36Schristos 				continue;
250b5677b36Schristos 			}
251b5677b36Schristos 
252b5677b36Schristos 			if (is_ipv4 && p > 5 ) {
253b5677b36Schristos 				*cp++ = (p == 6) ? ':' : '.';
254b5677b36Schristos 				cp += SPRINTF((cp, "%u", *s++));
255b5677b36Schristos 				/* we can potentially drop the last octet */
256b5677b36Schristos 				if (p != 7 || bits > 120) {
257b5677b36Schristos 					*cp++ = '.';
258b5677b36Schristos 					cp += SPRINTF((cp, "%u", *s++));
259b5677b36Schristos 				}
260b5677b36Schristos 			} else {
261b5677b36Schristos 				if (cp != outbuf)
262b5677b36Schristos 					*cp++ = ':';
263b5677b36Schristos 				cp += SPRINTF((cp, "%x", *s * 256 + s[1]));
264b5677b36Schristos 				s += 2;
265b5677b36Schristos 			}
266b5677b36Schristos 		}
267b5677b36Schristos 	}
268b5677b36Schristos 	/* Format CIDR /width. */
269b5677b36Schristos 	sprintf(cp, "/%u", bits);
270b5677b36Schristos 	if (strlen(outbuf) + 1 > size)
271b5677b36Schristos 		goto emsgsize;
272b5677b36Schristos 	strcpy(dst, outbuf);
273b5677b36Schristos 
274b5677b36Schristos 	return (dst);
275b5677b36Schristos 
276b5677b36Schristos emsgsize:
277b5677b36Schristos 	errno = EMSGSIZE;
278b5677b36Schristos 	return (NULL);
279b5677b36Schristos }
280b5677b36Schristos 
281b5677b36Schristos /*! \file */
282