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