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