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