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