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