1 //==========================================================================
5 //==========================================================================
6 //####BSDCOPYRIGHTBEGIN####
8 // -------------------------------------------
10 // Portions of this software may have been derived from OpenBSD,
11 // FreeBSD or other sources, and are covered by the appropriate
12 // copyright disclaimers included herein.
14 // Portions created by Red Hat are
15 // Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
17 // -------------------------------------------
19 //####BSDCOPYRIGHTEND####
20 //==========================================================================
22 /* $OpenBSD: inet_ntop.c,v 1.1 1997/03/13 19:07:32 downsj Exp $ */
24 /* Copyright (c) 1996 by Internet Software Consortium.
26 * Permission to use, copy, modify, and distribute this software for any
27 * purpose with or without fee is hereby granted, provided that the above
28 * copyright notice and this permission notice appear in all copies.
30 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
31 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
32 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
33 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
34 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
35 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
36 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
40 #include <sys/param.h>
41 #include <sys/types.h>
42 #include <sys/socket.h>
43 #include <netinet/in.h>
44 #include <arpa/inet.h>
45 #include <arpa/nameser.h>
48 #ifndef CYGPKG_NET_FREEBSD_STACK
53 #define IN6ADDRSZ 16 /* IPv6 T_AAAA */
56 #define INT16SZ sizeof(u_int16_t)
60 * WARNING: Don't even consider trying to compile this on a system where
61 * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.
64 static const char *inet_ntop4 __P((const u_char *src, char *dst, size_t size));
65 #ifdef CYGPKG_NET_INET6
66 static const char *inet_ntop6 __P((const u_char *src, char *dst, size_t size));
70 * inet_ntop(af, src, dst, size)
71 * convert a network format address to presentation format.
73 * pointer to presentation format address (`dst'), or NULL (see errno).
78 inet_ntop(int af, const void *src, char *dst, size_t size)
82 return (inet_ntop4(src, dst, size));
83 #ifdef CYGPKG_NET_INET6
85 return (inet_ntop6(src, dst, size));
95 // A more useful conversion routine
96 // Convert a network [socket] address to presentation format
97 // This routine handles all of the mapping required and isolates
98 // the knowledge of the structure format.
101 _inet_ntop(struct sockaddr *sa, char *dst, size_t size)
103 switch (sa->sa_family) {
105 return (inet_ntop(AF_INET, (char *)&((struct sockaddr_in *)sa)->sin_addr,
107 #ifdef CYGPKG_NET_INET6
109 return (inet_ntop(AF_INET6, (char *)&((struct sockaddr_in6 *)sa)->sin6_addr,
113 errno = EAFNOSUPPORT;
119 _inet_port(struct sockaddr *sa)
121 switch (sa->sa_family) {
123 return (ntohs(((struct sockaddr_in *)sa)->sin_port));
124 #ifdef CYGPKG_NET_INET6
126 return (ntohs(((struct sockaddr_in6 *)sa)->sin6_port));
129 return 0; // Unknown!
134 * inet_ntop4(src, dst, size)
135 * format an IPv4 address, more or less like inet_ntoa()
139 * (1) uses no statics
140 * (2) takes a u_char* not an in_addr as input
145 inet_ntop4(src, dst, size)
150 static const char fmt[] = "%u.%u.%u.%u";
151 char tmp[sizeof "255.255.255.255"];
153 if (sprintf(tmp, fmt, src[0], src[1], src[2], src[3]) > size) {
161 #ifdef CYGPKG_NET_INET6
163 * inet_ntop6(src, dst, size)
164 * convert IPv6 binary address into presentation (printable) format
169 inet_ntop6(const u_char *src, char *dst, size_t size)
172 * Note that int32_t and int16_t need only be "at least" large enough
173 * to contain a value of the specified size. On some systems, like
174 * Crays, there is no such thing as an integer variable with 16 bits.
175 * Keep this in mind if you think this function should have been coded
176 * to use pointer overlays. All the world's not a VAX.
178 #if 0 // This is too optimistic
179 char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"], *tp;
181 char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"], *tp;
183 struct { int base, len; } best, cur;
184 u_int words[IN6ADDRSZ / INT16SZ];
189 * Copy the input (bytewise) array into a wordwise array.
190 * Find the longest run of 0x00's in src[] for :: shorthanding.
192 memset(words, '\0', sizeof words);
193 for (i = 0; i < IN6ADDRSZ; i++)
194 words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
197 for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++) {
200 cur.base = i, cur.len = 1;
204 if (cur.base != -1) {
205 if (best.base == -1 || cur.len > best.len)
211 if (cur.base != -1) {
212 if (best.base == -1 || cur.len > best.len)
215 if (best.base != -1 && best.len < 2)
222 for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++) {
223 /* Are we inside the best run of 0x00's? */
224 if (best.base != -1 && i >= best.base &&
225 i < (best.base + best.len)) {
230 /* Are we following an initial run of 0x00s or any real hex? */
233 /* Is this address an encapsulated IPv4? */
234 if (i == 6 && best.base == 0 &&
235 (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) {
236 if (!inet_ntop4(src+12, tp, sizeof tmp - (tp - tmp)))
241 tp += sprintf(tp, "%x", words[i]);
243 /* Was it a trailing run of 0x00's? */
244 if (best.base != -1 && (best.base + best.len) == (IN6ADDRSZ / INT16SZ))
249 * Check for overflow, copy, and we're done.
251 if ((size_t)(tp - tmp) > size) {