1 /*
2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (c) 1996,1999 by Internet Software Consortium.
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18 #if defined(LIBC_SCCS) && !defined(lint)
19 static const char rcsid[] = "$Id: inet_net_ntop.c,v 1.5 2006/06/20 02:50:14 marka Exp $";
20 #endif
21
22 #include "port_before.h"
23
24 #include <sys/types.h>
25 #include <sys/socket.h>
26 #include <netinet/in.h>
27 #include <arpa/inet.h>
28
29 #include <errno.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <stdlib.h>
33 #include <limits.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 __P((const u_char *src, int bits,
44 char *dst, size_t size));
45 static char * inet_net_ntop_ipv6 __P((const u_char *src, int bits,
46 char *dst, 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(af,src,bits,dst,size)59 inet_net_ntop(af, src, bits, dst, size)
60 int af;
61 const void *src;
62 int bits;
63 char *dst;
64 size_t size;
65 {
66 switch (af) {
67 case AF_INET:
68 return (inet_net_ntop_ipv4(src, bits, dst, size));
69 case AF_INET6:
70 return (inet_net_ntop_ipv6(src, bits, dst, size));
71 default:
72 errno = EAFNOSUPPORT;
73 return (NULL);
74 }
75 }
76
77 /*%
78 * static char *
79 * inet_net_ntop_ipv4(src, bits, dst, size)
80 * convert IPv4 network number from network to presentation format.
81 * generates CIDR style result always.
82 * return:
83 * pointer to dst, or NULL if an error occurred (check errno).
84 * note:
85 * network byte order assumed. this means 192.5.5.240/28 has
86 * 0b11110000 in its fourth octet.
87 * author:
88 * Paul Vixie (ISC), July 1996
89 */
90 static char *
inet_net_ntop_ipv4(src,bits,dst,size)91 inet_net_ntop_ipv4(src, bits, dst, size)
92 const u_char *src;
93 int bits;
94 char *dst;
95 size_t size;
96 {
97 char *odst = dst;
98 char *t;
99 u_int m;
100 int b;
101
102 if (bits < 0 || bits > 32) {
103 errno = EINVAL;
104 return (NULL);
105 }
106
107 if (bits == 0) {
108 if (size < sizeof "0")
109 goto emsgsize;
110 *dst++ = '0';
111 size--;
112 *dst = '\0';
113 }
114
115 /* Format whole octets. */
116 for (b = bits / 8; b > 0; b--) {
117 if (size <= sizeof "255.")
118 goto emsgsize;
119 t = dst;
120 dst += SPRINTF((dst, "%u", *src++));
121 if (b > 1) {
122 *dst++ = '.';
123 *dst = '\0';
124 }
125 size -= (size_t)(dst - t);
126 }
127
128 /* Format partial octet. */
129 b = bits % 8;
130 if (b > 0) {
131 if (size <= sizeof ".255")
132 goto emsgsize;
133 t = dst;
134 if (dst != odst)
135 *dst++ = '.';
136 m = ((1 << b) - 1) << (8 - b);
137 dst += SPRINTF((dst, "%u", *src & m));
138 size -= (size_t)(dst - t);
139 }
140
141 /* Format CIDR /width. */
142 if (size <= sizeof "/32")
143 goto emsgsize;
144 dst += SPRINTF((dst, "/%u", bits));
145 return (odst);
146
147 emsgsize:
148 errno = EMSGSIZE;
149 return (NULL);
150 }
151
152 /*%
153 * static char *
154 * inet_net_ntop_ipv6(src, bits, fakebits, dst, size)
155 * convert IPv6 network number from network to presentation format.
156 * generates CIDR style result always. Picks the shortest representation
157 * unless the IP is really IPv4.
158 * always prints specified number of bits (bits).
159 * return:
160 * pointer to dst, or NULL if an error occurred (check errno).
161 * note:
162 * network byte order assumed. this means 192.5.5.240/28 has
163 * 0x11110000 in its fourth octet.
164 * author:
165 * Vadim Kogan (UCB), June 2001
166 * Original version (IPv4) by Paul Vixie (ISC), July 1996
167 */
168
169 static char *
inet_net_ntop_ipv6(const u_char * src,int bits,char * dst,size_t size)170 inet_net_ntop_ipv6(const u_char *src, int bits, char *dst, size_t size) {
171 u_int m;
172 int b;
173 int p;
174 int zero_s, zero_l, tmp_zero_s, tmp_zero_l;
175 int 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 int 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 = UINT_MAX << (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 sprintf(cp, "/%u", bits);
269 if (strlen(outbuf) + 1 > size)
270 goto emsgsize;
271 strcpy(dst, outbuf);
272
273 return (dst);
274
275 emsgsize:
276 errno = EMSGSIZE;
277 return (NULL);
278 }
279
280 /*! \file */
281