1 /*
2 * Copyright (C) 1996-2001 Internet Software Consortium.
3 *
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.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
9 * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
10 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
11 * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
13 * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
14 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
15 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18
19 #include <platform.h>
20
21 #if defined(_WIN32) && !defined(EAFNOSUPPORT)
22 #define EAFNOSUPPORT WSAEAFNOSUPPORT
23 #endif
24
25 #define NS_INT16SZ 2
26 #define NS_IN6ADDRSZ 16
27
28 /*
29 * WARNING: Don't even consider trying to compile this on a system where
30 * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.
31 */
32
33 static const char *inet_ntop4(const unsigned char *src, char *dst,
34 size_t size);
35
36 #ifdef AF_INET6
37 static const char *inet_ntop6(const unsigned char *src, char *dst,
38 size_t size);
39 #endif
40
41 /* char *
42 * isc_net_ntop(af, src, dst, size)
43 * convert a network format address to presentation format.
44 * return:
45 * pointer to presentation format address (`dst'), or NULL (see errno).
46 * author:
47 * Paul Vixie, 1996.
48 */
49 const char *
inet_ntop(int af,const void * src,char * dst,socklen_t size)50 inet_ntop(int af, const void *src, char *dst, socklen_t size)
51 {
52 switch (af) {
53 case AF_INET:
54 return (inet_ntop4(src, dst, size));
55 #ifdef AF_INET6
56 case AF_INET6:
57 return (inet_ntop6(src, dst, size));
58 #endif
59 default:
60 errno = EAFNOSUPPORT;
61 return (NULL);
62 }
63 /* NOTREACHED */
64 }
65
66 /* const char *
67 * inet_ntop4(src, dst, size)
68 * format an IPv4 address
69 * return:
70 * `dst' (as a const)
71 * notes:
72 * (1) uses no statics
73 * (2) takes a unsigned char* not an in_addr as input
74 * author:
75 * Paul Vixie, 1996.
76 */
77 static const char *
inet_ntop4(const unsigned char * src,char * dst,size_t size)78 inet_ntop4(const unsigned char *src, char *dst, size_t size)
79 {
80 static const char *fmt = "%u.%u.%u.%u";
81 char tmp[sizeof "255.255.255.255"];
82 size_t len;
83
84 len = snprintf(tmp, sizeof tmp, fmt, src[0], src[1], src[2], src[3]);
85 if (len >= size) {
86 errno = ENOSPC;
87 return (NULL);
88 }
89 memcpy(dst, tmp, len + 1);
90
91 return (dst);
92 }
93
94 /* const char *
95 * isc_inet_ntop6(src, dst, size)
96 * convert IPv6 binary address into presentation (printable) format
97 * author:
98 * Paul Vixie, 1996.
99 */
100 #ifdef AF_INET6
101 static const char *
inet_ntop6(const unsigned char * src,char * dst,size_t size)102 inet_ntop6(const unsigned char *src, char *dst, size_t size)
103 {
104 /*
105 * Note that int32_t and int16_t need only be "at least" large enough
106 * to contain a value of the specified size. On some systems, like
107 * Crays, there is no such thing as an integer variable with 16 bits.
108 * Keep this in mind if you think this function should have been coded
109 * to use pointer overlays. All the world's not a VAX.
110 */
111 char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"], *tp;
112 struct { int base, len; } best, cur;
113 unsigned int words[NS_IN6ADDRSZ / NS_INT16SZ];
114 int i, inc;
115
116 /*
117 * Preprocess:
118 * Copy the input (bytewise) array into a wordwise array.
119 * Find the longest run of 0x00's in src[] for :: shorthanding.
120 */
121 memset(words, '\0', sizeof words);
122 for (i = 0; i < NS_IN6ADDRSZ; i++)
123 words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
124 best.base = -1;
125 cur.base = -1;
126 for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
127 if (words[i] == 0) {
128 if (cur.base == -1)
129 cur.base = i, cur.len = 1;
130 else
131 cur.len++;
132 } else {
133 if (cur.base != -1) {
134 if (best.base == -1 || cur.len > best.len)
135 best = cur;
136 cur.base = -1;
137 }
138 }
139 }
140 if (cur.base != -1) {
141 if (best.base == -1 || cur.len > best.len)
142 best = cur;
143 }
144 if (best.base != -1 && best.len < 2)
145 best.base = -1;
146
147 /*
148 * Format the result.
149 */
150 tp = tmp;
151 for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
152 /* Are we inside the best run of 0x00's? */
153 if (best.base != -1 && i >= best.base &&
154 i < (best.base + best.len)) {
155 if (i == best.base)
156 *tp++ = ':';
157 continue;
158 }
159 /* Are we following an initial run of 0x00s or any real hex? */
160 if (i != 0)
161 *tp++ = ':';
162 /* Is this address an encapsulated IPv4? */
163 if (i == 6 && best.base == 0 &&
164 (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) {
165 if (!inet_ntop4(src+12, tp, sizeof tmp - (tp - tmp)))
166 return (NULL);
167 tp += strlen(tp);
168 break;
169 }
170 inc = snprintf(tp, 5, "%x", words[i]);
171 assert(inc < 5);
172 tp += inc;
173 }
174 /* Was it a trailing run of 0x00's? */
175 if (best.base != -1 && (best.base + best.len) ==
176 (NS_IN6ADDRSZ / NS_INT16SZ))
177 *tp++ = ':';
178 *tp++ = '\0';
179
180 /*
181 * Check for overflow, copy, and we're done.
182 */
183 if ((size_t)(tp - tmp) > size) {
184 errno = ENOSPC;
185 return (NULL);
186 }
187 memcpy(dst, tmp, tp - tmp);
188 return (dst);
189 }
190 #endif /* AF_INET6 */
191