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