2 * Copyright (c) 1996-1999 by Internet Software Consortium.
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
9 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
10 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
11 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
12 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
13 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
14 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
22 #if defined(LIBC_SCCS) && !defined(lint)
23 static char rcsid[] = "$Id: inet_ntop.c,v 1.7 2002/10/29 00:28:10 guy Exp $";
24 #endif /* LIBC_SCCS and not lint */
26 #ifdef HAVE_SYS_PARAM_H
27 #include <sys/param.h>
30 #ifdef HAVE_SYS_TYPES_H
31 #include <sys/types.h>
34 #ifdef HAVE_SYS_SOCKET_H
35 #include <sys/socket.h> /* needed to define AF_ values on UNIX */
38 #ifdef HAVE_WINSOCK2_H
39 #include <winsock2.h> /* needed to define AF_ values on Windows */
40 #define EAFNOSUPPORT WSAEAFNOSUPPORT
43 #ifdef HAVE_NETINET_IN_H
44 #include <netinet/in.h>
47 #ifdef HAVE_ARPA_INET_H
48 #include <arpa/inet.h>
51 #ifdef HAVE_ARPA_NAMESER_H
52 #include <arpa/nameser.h>
59 #ifdef NEED_SNPRINTF_H
60 # include "snprintf.h"
63 #include "inet_v6defs.h"
69 #define NS_IN6ADDRSZ 16
76 * WARNING: Don't even consider trying to compile this on a system where
77 * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.
80 static const char *inet_ntop4 __P((const u_char *src, char *dst, size_t size));
81 static const char *inet_ntop6 __P((const u_char *src, char *dst, size_t size));
84 * inet_ntop(af, src, dst, size)
85 * convert a network format address to presentation format.
87 * pointer to presentation format address (`dst'), or NULL (see errno).
92 inet_ntop(af, src, dst, size)
100 return (inet_ntop4(src, dst, size));
102 return (inet_ntop6(src, dst, size));
104 errno = EAFNOSUPPORT;
111 * inet_ntop4(src, dst, size)
112 * format an IPv4 address
116 * (1) uses no statics
117 * (2) takes a u_char* not an in_addr as input
122 inet_ntop4(src, dst, size)
127 static const char fmt[] = "%u.%u.%u.%u";
128 char tmp[sizeof "255.255.255.255"];
131 nprinted = snprintf(tmp, sizeof(tmp), fmt, src[0], src[1], src[2], src[3]);
133 return (NULL); /* we assume "errno" was set by "snprintf()" */
134 if ((size_t)nprinted > size) {
143 * inet_ntop6(src, dst, size)
144 * convert IPv6 binary address into presentation (printable) format
149 inet_ntop6(src, dst, size)
155 * Note that int32_t and int16_t need only be "at least" large enough
156 * to contain a value of the specified size. On some systems, like
157 * Crays, there is no such thing as an integer variable with 16 bits.
158 * Keep this in mind if you think this function should have been coded
159 * to use pointer overlays. All the world's not a VAX.
161 char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"], *tp;
162 struct { int base, len; } best, cur;
163 u_int words[NS_IN6ADDRSZ / NS_INT16SZ];
168 * Copy the input (bytewise) array into a wordwise array.
169 * Find the longest run of 0x00's in src[] for :: shorthanding.
171 memset(words, '\0', sizeof words);
172 for (i = 0; i < NS_IN6ADDRSZ; i++)
173 words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
176 for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
179 cur.base = i, cur.len = 1;
183 if (cur.base != -1) {
184 if (best.base == -1 || cur.len > best.len)
190 if (cur.base != -1) {
191 if (best.base == -1 || cur.len > best.len)
194 if (best.base != -1 && best.len < 2)
201 for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
202 /* Are we inside the best run of 0x00's? */
203 if (best.base != -1 && i >= best.base &&
204 i < (best.base + best.len)) {
209 /* Are we following an initial run of 0x00s or any real hex? */
212 /* Is this address an encapsulated IPv4? */
213 if (i == 6 && best.base == 0 &&
214 (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) {
215 if (!inet_ntop4(src+12, tp, sizeof tmp - (tp - tmp)))
220 tp += sprintf(tp, "%x", words[i]);
222 /* Was it a trailing run of 0x00's? */
223 if (best.base != -1 && (best.base + best.len) ==
224 (NS_IN6ADDRSZ / NS_INT16SZ))
229 * Check for overflow, copy, and we're done.
231 if ((size_t)(tp - tmp) > size) {