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