2 * Copyright (c) 1996-1999 by Internet Software Consortium.
3 * Copyright (c) 2005 Free Software Foundation, Inc.
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
10 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
11 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
12 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
13 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
14 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
15 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
24 #include "inet_ntop.h"
30 #define NS_IN6ADDRSZ 16
34 * WARNING: Don't even consider trying to compile this on a system where
35 * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.
37 typedef int verify_int_size[2 * sizeof(int) - 7];
40 static const char *inet_ntop4 (const unsigned char *src, char *dst, socklen_t size);
43 static const char *inet_ntop6 (const unsigned char *src, char *dst, socklen_t size);
48 * inet_ntop(af, src, dst, size)
49 * convert a network format address to presentation format.
51 * pointer to presentation format address (`dst'), or NULL (see errno).
56 inet_ntop(af, src, dst, size)
66 return (inet_ntop4(src, dst, size));
71 return (inet_ntop6(src, dst, size));
83 * inet_ntop4(src, dst, size)
84 * format an IPv4 address
89 * (2) takes a u_char* not an in_addr as input
94 inet_ntop4(src, dst, size)
95 const unsigned char *src;
99 static const char fmt[] = "%u.%u.%u.%u";
100 char tmp[sizeof "255.255.255.255"];
102 if (sprintf(tmp, fmt, src[0], src[1], src[2], src[3]) > size) {
106 return strcpy(dst, tmp);
114 * inet_ntop6(src, dst, size)
115 * convert IPv6 binary address into presentation (printable) format
120 inet_ntop6(src, dst, size)
121 const unsigned char *src;
126 * Note that int32_t and int16_t need only be "at least" large enough
127 * to contain a value of the specified size. On some systems, like
128 * Crays, there is no such thing as an integer variable with 16 bits.
129 * Keep this in mind if you think this function should have been coded
130 * to use pointer overlays. All the world's not a VAX.
132 char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"], *tp;
133 struct { int base, len; } best, cur;
134 unsigned int words[NS_IN6ADDRSZ / NS_INT16SZ];
139 * Copy the input (bytewise) array into a wordwise array.
140 * Find the longest run of 0x00's in src[] for :: shorthanding.
142 memset(words, '\0', sizeof words);
143 for (i = 0; i < NS_IN6ADDRSZ; i += 2)
144 words[i / 2] = (src[i] << 8) | src[i + 1];
147 for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
150 cur.base = i, cur.len = 1;
154 if (cur.base != -1) {
155 if (best.base == -1 || cur.len > best.len)
161 if (cur.base != -1) {
162 if (best.base == -1 || cur.len > best.len)
165 if (best.base != -1 && best.len < 2)
172 for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
173 /* Are we inside the best run of 0x00's? */
174 if (best.base != -1 && i >= best.base &&
175 i < (best.base + best.len)) {
180 /* Are we following an initial run of 0x00s or any real hex? */
183 /* Is this address an encapsulated IPv4? */
184 if (i == 6 && best.base == 0 &&
185 (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) {
186 if (!inet_ntop4(src+12, tp, sizeof tmp - (tp - tmp)))
191 tp += sprintf(tp, "%x", words[i]);
193 /* Was it a trailing run of 0x00's? */
194 if (best.base != -1 && (best.base + best.len) ==
195 (NS_IN6ADDRSZ / NS_INT16SZ))
200 * Check for overflow, copy, and we're done.
202 if ((socklen_t)(tp - tmp) > size) {
206 return strcpy(dst, tmp);