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