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