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