1*a466cc55SCy Schubert /*
2*a466cc55SCy Schubert * Copyright (C) 2004, 2005, 2007, 2009 Internet Systems Consortium, Inc. ("ISC")
3*a466cc55SCy Schubert * Copyright (C) 1996-2001 Internet Software Consortium.
4*a466cc55SCy Schubert *
5*a466cc55SCy Schubert * Permission to use, copy, modify, and/or distribute this software for any
6*a466cc55SCy Schubert * purpose with or without fee is hereby granted, provided that the above
7*a466cc55SCy Schubert * copyright notice and this permission notice appear in all copies.
8*a466cc55SCy Schubert *
9*a466cc55SCy Schubert * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10*a466cc55SCy Schubert * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11*a466cc55SCy Schubert * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12*a466cc55SCy Schubert * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13*a466cc55SCy Schubert * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14*a466cc55SCy Schubert * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15*a466cc55SCy Schubert * PERFORMANCE OF THIS SOFTWARE.
16*a466cc55SCy Schubert */
17*a466cc55SCy Schubert
18*a466cc55SCy Schubert /*! \file */
19*a466cc55SCy Schubert
20*a466cc55SCy Schubert #if defined(LIBC_SCCS) && !defined(lint)
21*a466cc55SCy Schubert static char rcsid[] =
22*a466cc55SCy Schubert "$Id: inet_ntop.c,v 1.21 2009/07/17 23:47:41 tbox Exp $";
23*a466cc55SCy Schubert #endif /* LIBC_SCCS and not lint */
24*a466cc55SCy Schubert
25*a466cc55SCy Schubert #include <config.h>
26*a466cc55SCy Schubert
27*a466cc55SCy Schubert #include <errno.h>
28*a466cc55SCy Schubert #include <stdio.h>
29*a466cc55SCy Schubert #include <string.h>
30*a466cc55SCy Schubert
31*a466cc55SCy Schubert #include <isc/net.h>
32*a466cc55SCy Schubert #include <isc/print.h>
33*a466cc55SCy Schubert
34*a466cc55SCy Schubert #define NS_INT16SZ 2
35*a466cc55SCy Schubert #define NS_IN6ADDRSZ 16
36*a466cc55SCy Schubert
37*a466cc55SCy Schubert /*
38*a466cc55SCy Schubert * WARNING: Don't even consider trying to compile this on a system where
39*a466cc55SCy Schubert * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.
40*a466cc55SCy Schubert */
41*a466cc55SCy Schubert
42*a466cc55SCy Schubert static const char *inet_ntop4(const unsigned char *src, char *dst,
43*a466cc55SCy Schubert size_t size);
44*a466cc55SCy Schubert
45*a466cc55SCy Schubert #ifdef AF_INET6
46*a466cc55SCy Schubert static const char *inet_ntop6(const unsigned char *src, char *dst,
47*a466cc55SCy Schubert size_t size);
48*a466cc55SCy Schubert #endif
49*a466cc55SCy Schubert const char *isc_net_ntop(int af, const void *src, char *dst, size_t size);
50*a466cc55SCy Schubert
51*a466cc55SCy Schubert /*! char *
52*a466cc55SCy Schubert * isc_net_ntop(af, src, dst, size)
53*a466cc55SCy Schubert * convert a network format address to presentation format.
54*a466cc55SCy Schubert * \return
55*a466cc55SCy Schubert * pointer to presentation format address (`dst'), or NULL (see errno).
56*a466cc55SCy Schubert * \author
57*a466cc55SCy Schubert * Paul Vixie, 1996.
58*a466cc55SCy Schubert */
59*a466cc55SCy Schubert const char *
isc_net_ntop(int af,const void * src,char * dst,size_t size)60*a466cc55SCy Schubert isc_net_ntop(int af, const void *src, char *dst, size_t size)
61*a466cc55SCy Schubert {
62*a466cc55SCy Schubert switch (af) {
63*a466cc55SCy Schubert case AF_INET:
64*a466cc55SCy Schubert return (inet_ntop4(src, dst, size));
65*a466cc55SCy Schubert #ifdef AF_INET6
66*a466cc55SCy Schubert case AF_INET6:
67*a466cc55SCy Schubert return (inet_ntop6(src, dst, size));
68*a466cc55SCy Schubert #endif
69*a466cc55SCy Schubert default:
70*a466cc55SCy Schubert errno = EAFNOSUPPORT;
71*a466cc55SCy Schubert return (NULL);
72*a466cc55SCy Schubert }
73*a466cc55SCy Schubert /* NOTREACHED */
74*a466cc55SCy Schubert }
75*a466cc55SCy Schubert
76*a466cc55SCy Schubert /*! const char *
77*a466cc55SCy Schubert * inet_ntop4(src, dst, size)
78*a466cc55SCy Schubert * format an IPv4 address
79*a466cc55SCy Schubert * \return
80*a466cc55SCy Schubert * `dst' (as a const)
81*a466cc55SCy Schubert * \note
82*a466cc55SCy Schubert * (1) uses no statics
83*a466cc55SCy Schubert * \note
84*a466cc55SCy Schubert * (2) takes a unsigned char* not an in_addr as input
85*a466cc55SCy Schubert * \author
86*a466cc55SCy Schubert * Paul Vixie, 1996.
87*a466cc55SCy Schubert */
88*a466cc55SCy Schubert static const char *
inet_ntop4(const unsigned char * src,char * dst,size_t size)89*a466cc55SCy Schubert inet_ntop4(const unsigned char *src, char *dst, size_t size)
90*a466cc55SCy Schubert {
91*a466cc55SCy Schubert static const char *fmt = "%u.%u.%u.%u";
92*a466cc55SCy Schubert char tmp[sizeof("255.255.255.255")];
93*a466cc55SCy Schubert int len;
94*a466cc55SCy Schubert
95*a466cc55SCy Schubert len = snprintf(tmp, sizeof(tmp), fmt, src[0], src[1], src[2], src[3]);
96*a466cc55SCy Schubert if (len < 0 || (size_t)len >= size)
97*a466cc55SCy Schubert {
98*a466cc55SCy Schubert errno = ENOSPC;
99*a466cc55SCy Schubert return (NULL);
100*a466cc55SCy Schubert }
101*a466cc55SCy Schubert memcpy(dst, tmp, 1 + len);
102*a466cc55SCy Schubert
103*a466cc55SCy Schubert return (dst);
104*a466cc55SCy Schubert }
105*a466cc55SCy Schubert
106*a466cc55SCy Schubert /*! const char *
107*a466cc55SCy Schubert * isc_inet_ntop6(src, dst, size)
108*a466cc55SCy Schubert * convert IPv6 binary address into presentation (printable) format
109*a466cc55SCy Schubert * \author
110*a466cc55SCy Schubert * Paul Vixie, 1996.
111*a466cc55SCy Schubert */
112*a466cc55SCy Schubert #ifdef AF_INET6
113*a466cc55SCy Schubert static const char *
inet_ntop6(const unsigned char * src,char * dst,size_t size)114*a466cc55SCy Schubert inet_ntop6(const unsigned char *src, char *dst, size_t size)
115*a466cc55SCy Schubert {
116*a466cc55SCy Schubert /*
117*a466cc55SCy Schubert * Note that int32_t and int16_t need only be "at least" large enough
118*a466cc55SCy Schubert * to contain a value of the specified size. On some systems, like
119*a466cc55SCy Schubert * Crays, there is no such thing as an integer variable with 16 bits.
120*a466cc55SCy Schubert * Keep this in mind if you think this function should have been coded
121*a466cc55SCy Schubert * to use pointer overlays. All the world's not a VAX.
122*a466cc55SCy Schubert */
123*a466cc55SCy Schubert char tmp[sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")], *tp;
124*a466cc55SCy Schubert struct { int base, len; } best, cur;
125*a466cc55SCy Schubert unsigned int words[NS_IN6ADDRSZ / NS_INT16SZ];
126*a466cc55SCy Schubert int i;
127*a466cc55SCy Schubert
128*a466cc55SCy Schubert /*
129*a466cc55SCy Schubert * Preprocess:
130*a466cc55SCy Schubert * Copy the input (bytewise) array into a wordwise array.
131*a466cc55SCy Schubert * Find the longest run of 0x00's in src[] for :: shorthanding.
132*a466cc55SCy Schubert */
133*a466cc55SCy Schubert memset(words, '\0', sizeof(words));
134*a466cc55SCy Schubert for (i = 0; i < NS_IN6ADDRSZ; i++)
135*a466cc55SCy Schubert words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
136*a466cc55SCy Schubert best.base = -1;
137*a466cc55SCy Schubert cur.base = -1;
138*a466cc55SCy Schubert best.len = cur.len = 0;
139*a466cc55SCy Schubert for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
140*a466cc55SCy Schubert if (words[i] == 0) {
141*a466cc55SCy Schubert if (cur.base == -1)
142*a466cc55SCy Schubert cur.base = i, cur.len = 1;
143*a466cc55SCy Schubert else
144*a466cc55SCy Schubert cur.len++;
145*a466cc55SCy Schubert } else {
146*a466cc55SCy Schubert if (cur.base != -1) {
147*a466cc55SCy Schubert if (best.base == -1 || cur.len > best.len)
148*a466cc55SCy Schubert best = cur;
149*a466cc55SCy Schubert cur.base = -1;
150*a466cc55SCy Schubert }
151*a466cc55SCy Schubert }
152*a466cc55SCy Schubert }
153*a466cc55SCy Schubert if (cur.base != -1) {
154*a466cc55SCy Schubert if (best.base == -1 || cur.len > best.len)
155*a466cc55SCy Schubert best = cur;
156*a466cc55SCy Schubert }
157*a466cc55SCy Schubert if (best.base != -1 && best.len < 2)
158*a466cc55SCy Schubert best.base = -1;
159*a466cc55SCy Schubert
160*a466cc55SCy Schubert /*
161*a466cc55SCy Schubert * Format the result.
162*a466cc55SCy Schubert */
163*a466cc55SCy Schubert tp = tmp;
164*a466cc55SCy Schubert for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
165*a466cc55SCy Schubert /* Are we inside the best run of 0x00's? */
166*a466cc55SCy Schubert if (best.base != -1 && i >= best.base &&
167*a466cc55SCy Schubert i < (best.base + best.len)) {
168*a466cc55SCy Schubert if (i == best.base)
169*a466cc55SCy Schubert *tp++ = ':';
170*a466cc55SCy Schubert continue;
171*a466cc55SCy Schubert }
172*a466cc55SCy Schubert /* Are we following an initial run of 0x00s or any real hex? */
173*a466cc55SCy Schubert if (i != 0)
174*a466cc55SCy Schubert *tp++ = ':';
175*a466cc55SCy Schubert /* Is this address an encapsulated IPv4? */
176*a466cc55SCy Schubert if (i == 6 && best.base == 0 && (best.len == 6 ||
177*a466cc55SCy Schubert (best.len == 7 && words[7] != 0x0001) ||
178*a466cc55SCy Schubert (best.len == 5 && words[5] == 0xffff))) {
179*a466cc55SCy Schubert if (!inet_ntop4(src+12, tp,
180*a466cc55SCy Schubert sizeof(tmp) - (tp - tmp)))
181*a466cc55SCy Schubert return (NULL);
182*a466cc55SCy Schubert tp += strlen(tp);
183*a466cc55SCy Schubert break;
184*a466cc55SCy Schubert }
185*a466cc55SCy Schubert tp += snprintf(tp, sizeof(tmp) - (tp - tmp), "%x", words[i]);
186*a466cc55SCy Schubert }
187*a466cc55SCy Schubert /* Was it a trailing run of 0x00's? */
188*a466cc55SCy Schubert if (best.base != -1 && (best.base + best.len) ==
189*a466cc55SCy Schubert (NS_IN6ADDRSZ / NS_INT16SZ))
190*a466cc55SCy Schubert *tp++ = ':';
191*a466cc55SCy Schubert *tp++ = '\0';
192*a466cc55SCy Schubert
193*a466cc55SCy Schubert /*
194*a466cc55SCy Schubert * Check for overflow, copy, and we're done.
195*a466cc55SCy Schubert */
196*a466cc55SCy Schubert if ((size_t)(tp - tmp) > size) {
197*a466cc55SCy Schubert errno = ENOSPC;
198*a466cc55SCy Schubert return (NULL);
199*a466cc55SCy Schubert }
200*a466cc55SCy Schubert memcpy(dst, tmp, (size_t)(tp - tmp));
201*a466cc55SCy Schubert return (dst);
202*a466cc55SCy Schubert }
203*a466cc55SCy Schubert #endif /* AF_INET6 */
204