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.3 2000/01/10 17:32:51 gram Exp $";
24 #endif /* LIBC_SCCS and not lint */
26 #ifdef HAVE_SYS_PARAM_H
27 #include <sys/param.h>
30 #include <sys/types.h>
32 #ifdef HAVE_SYS_SOCKET_H
33 #include <sys/socket.h>
38 #define EAFNOSUPPORT WSAEAFNOSUPPORT
41 #ifdef HAVE_NETINET_IN_H
42 #include <netinet/in.h>
45 #ifdef HAVE_ARPA_INET_H
46 #include <arpa/inet.h>
49 #ifdef HAVE_ARPA_NAMESER_H
50 #include <arpa/nameser.h>
57 #ifdef NEED_SNPRINTF_H
63 # include "snprintf.h"
66 #include "inet_v6defs.h"
72 #define NS_IN6ADDRSZ 16
79 * WARNING: Don't even consider trying to compile this on a system where
80 * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.
83 static const char *inet_ntop4 __P((const u_char *src, char *dst, size_t size));
84 static const char *inet_ntop6 __P((const u_char *src, char *dst, size_t size));
87 * inet_ntop(af, src, dst, size)
88 * convert a network format address to presentation format.
90 * pointer to presentation format address (`dst'), or NULL (see errno).
95 inet_ntop(af, src, dst, size)
103 return (inet_ntop4(src, dst, size));
105 return (inet_ntop6(src, dst, size));
107 errno = EAFNOSUPPORT;
114 * inet_ntop4(src, dst, size)
115 * format an IPv4 address
119 * (1) uses no statics
120 * (2) takes a u_char* not an in_addr as input
125 inet_ntop4(src, dst, size)
130 static const char fmt[] = "%u.%u.%u.%u";
131 char tmp[sizeof "255.255.255.255"];
133 if (snprintf(tmp, sizeof(tmp), fmt, src[0], src[1], src[2], src[3]) > size) {
142 * inet_ntop6(src, dst, size)
143 * convert IPv6 binary address into presentation (printable) format
148 inet_ntop6(src, dst, size)
154 * Note that int32_t and int16_t need only be "at least" large enough
155 * to contain a value of the specified size. On some systems, like
156 * Crays, there is no such thing as an integer variable with 16 bits.
157 * Keep this in mind if you think this function should have been coded
158 * to use pointer overlays. All the world's not a VAX.
160 char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"], *tp;
161 struct { int base, len; } best, cur;
162 u_int words[NS_IN6ADDRSZ / NS_INT16SZ];
167 * Copy the input (bytewise) array into a wordwise array.
168 * Find the longest run of 0x00's in src[] for :: shorthanding.
170 memset(words, '\0', sizeof words);
171 for (i = 0; i < NS_IN6ADDRSZ; i++)
172 words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
175 for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
178 cur.base = i, cur.len = 1;
182 if (cur.base != -1) {
183 if (best.base == -1 || cur.len > best.len)
189 if (cur.base != -1) {
190 if (best.base == -1 || cur.len > best.len)
193 if (best.base != -1 && best.len < 2)
200 for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
201 /* Are we inside the best run of 0x00's? */
202 if (best.base != -1 && i >= best.base &&
203 i < (best.base + best.len)) {
208 /* Are we following an initial run of 0x00s or any real hex? */
211 /* Is this address an encapsulated IPv4? */
212 if (i == 6 && best.base == 0 &&
213 (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) {
214 if (!inet_ntop4(src+12, tp, sizeof tmp - (tp - tmp)))
219 tp += sprintf(tp, "%x", words[i]);
221 /* Was it a trailing run of 0x00's? */
222 if (best.base != -1 && (best.base + best.len) ==
223 (NS_IN6ADDRSZ / NS_INT16SZ))
228 * Check for overflow, copy, and we're done.
230 if ((size_t)(tp - tmp) > size) {