4 * Copyright (c) 1996-1999 by Internet Software Consortium.
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
11 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
12 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
13 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
16 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
25 #if defined(LIBC_SCCS) && !defined(lint)
26 static char rcsid[] = "$Id$";
27 #endif /* LIBC_SCCS and not lint */
29 #ifdef HAVE_SYS_PARAM_H
30 #include <sys/param.h>
33 #ifdef HAVE_SYS_TYPES_H
34 #include <sys/types.h>
37 #ifdef HAVE_SYS_SOCKET_H
38 #include <sys/socket.h> /* needed to define AF_ values on UNIX */
41 #ifdef HAVE_WINSOCK2_H
42 #include <winsock2.h> /* needed to define AF_ values on Windows */
43 #define EAFNOSUPPORT WSAEAFNOSUPPORT
46 #ifdef HAVE_NETINET_IN_H
47 #include <netinet/in.h>
50 #ifdef HAVE_ARPA_INET_H
51 #include <arpa/inet.h>
54 #ifdef HAVE_ARPA_NAMESER_H
55 #include <arpa/nameser.h>
62 #include "inet_v6defs.h"
70 #define NS_IN6ADDRSZ 16
77 * WARNING: Don't even consider trying to compile this on a system where
78 * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.
81 static const char *inet_ntop4 __P((const u_char *src, char *dst, size_t size));
82 static const char *inet_ntop6 __P((const u_char *src, char *dst, size_t size));
85 * inet_ntop(af, src, dst, size)
86 * convert a network format address to presentation format.
88 * pointer to presentation format address (`dst'), or NULL (see errno).
93 inet_ntop(af, src, dst, size)
101 return (inet_ntop4(src, dst, size));
103 return (inet_ntop6(src, dst, size));
105 errno = EAFNOSUPPORT;
112 * inet_ntop4(src, dst, size)
113 * format an IPv4 address
117 * (1) uses no statics
118 * (2) takes a u_char* not an in_addr as input
123 inet_ntop4(src, dst, size)
128 static const char fmt[] = "%u.%u.%u.%u";
129 char tmp[sizeof "255.255.255.255"];
132 nprinted = g_snprintf(tmp, sizeof(tmp), fmt, src[0], src[1], src[2], src[3]);
133 /* Note: nprinted *excludes* the trailing '\0' character */
134 if ((size_t)nprinted >= size) {
138 g_strlcpy(dst, tmp, 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 += g_snprintf(tp, (gulong) (sizeof tmp - (tp - tmp)), "%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) {
235 g_strlcpy(dst, tmp, size);