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 * $Id: inet_ntop.c 663 2016-03-22 16:08:54Z ume $
18 */
19
20
21 #define NS_INT16SZ 2
22 #define NS_IN6ADDRSZ 16
23
24 /*
25 * WARNING: Don't even consider trying to compile this on a system where
26 * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.
27 */
28
29 #ifdef HAVE_SOCKLEN_T
30 #define SOCKLEN_T socklen_t
31 #else
32 #define SOCKLEN_T size_t
33 #endif
34
35 static const char *inet_ntop4(const unsigned char *src, char *dst,
36 size_t size);
37
38 #ifdef AF_INET6
39 static const char *inet_ntop6(const unsigned char *src, char *dst,
40 size_t size);
41 #endif
42
43 /* char *
44 * isc_net_ntop(af, src, dst, size)
45 * convert a network format address to presentation format.
46 * return:
47 * pointer to presentation format address (`dst'), or NULL (see errno).
48 * author:
49 * Paul Vixie, 1996.
50 */
51 const char *
inet_ntop(int af,const void * src,char * dst,SOCKLEN_T size)52 inet_ntop(int af, const void *src, char *dst, SOCKLEN_T size)
53 {
54 switch (af) {
55 case AF_INET:
56 return (inet_ntop4(src, dst, (size_t)size));
57 #ifdef AF_INET6
58 case AF_INET6:
59 return (inet_ntop6(src, dst, (size_t)size));
60 #endif
61 default:
62 errno = EAFNOSUPPORT;
63 return (NULL);
64 }
65 /* NOTREACHED */
66 }
67
68 /* const char *
69 * inet_ntop4(src, dst, size)
70 * format an IPv4 address
71 * return:
72 * `dst' (as a const)
73 * notes:
74 * (1) uses no statics
75 * (2) takes a unsigned char* not an in_addr as input
76 * author:
77 * Paul Vixie, 1996.
78 */
79 static const char *
inet_ntop4(const unsigned char * src,char * dst,size_t size)80 inet_ntop4(const unsigned char *src, char *dst, size_t size)
81 {
82 static const char *fmt = "%u.%u.%u.%u";
83 char tmp[sizeof "255.255.255.255"];
84
85 if ((size_t)sprintf(tmp, fmt, src[0], src[1], src[2], src[3]) >= size)
86 {
87 errno = ENOSPC;
88 return (NULL);
89 }
90 strcpy(dst, tmp);
91
92 return (dst);
93 }
94
95 /* const char *
96 * isc_inet_ntop6(src, dst, size)
97 * convert IPv6 binary address into presentation (printable) format
98 * author:
99 * Paul Vixie, 1996.
100 */
101 #ifdef AF_INET6
102 static const char *
inet_ntop6(const unsigned char * src,char * dst,size_t size)103 inet_ntop6(const unsigned char *src, char *dst, size_t size)
104 {
105 /*
106 * Note that int32_t and int16_t need only be "at least" large enough
107 * to contain a value of the specified size. On some systems, like
108 * Crays, there is no such thing as an integer variable with 16 bits.
109 * Keep this in mind if you think this function should have been coded
110 * to use pointer overlays. All the world's not a VAX.
111 */
112 char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"], *tp;
113 struct { int base, len; } best, cur;
114 unsigned int words[NS_IN6ADDRSZ / NS_INT16SZ];
115 int i;
116
117 /*
118 * Preprocess:
119 * Copy the input (bytewise) array into a wordwise array.
120 * Find the longest run of 0x00's in src[] for :: shorthanding.
121 */
122 memset(words, '\0', sizeof words);
123 for (i = 0; i < NS_IN6ADDRSZ; i++)
124 words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
125 best.base = -1;
126 cur.base = -1;
127 for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
128 if (words[i] == 0) {
129 if (cur.base == -1)
130 cur.base = i, cur.len = 1;
131 else
132 cur.len++;
133 } else {
134 if (cur.base != -1) {
135 if (best.base == -1 || cur.len > best.len)
136 best = cur;
137 cur.base = -1;
138 }
139 }
140 }
141 if (cur.base != -1) {
142 if (best.base == -1 || cur.len > best.len)
143 best = cur;
144 }
145 if (best.base != -1 && best.len < 2)
146 best.base = -1;
147
148 /*
149 * Format the result.
150 */
151 tp = tmp;
152 for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
153 /* Are we inside the best run of 0x00's? */
154 if (best.base != -1 && i >= best.base &&
155 i < (best.base + best.len)) {
156 if (i == best.base)
157 *tp++ = ':';
158 continue;
159 }
160 /* Are we following an initial run of 0x00s or any real hex? */
161 if (i != 0)
162 *tp++ = ':';
163 /* Is this address an encapsulated IPv4? */
164 if (i == 6 && best.base == 0 &&
165 (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) {
166 if (!inet_ntop4(src+12, tp,
167 sizeof tmp - (tp - tmp)))
168 return (NULL);
169 tp += strlen(tp);
170 break;
171 }
172 tp += sprintf(tp, "%x", words[i]);
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 strcpy(dst, tmp);
188 return (dst);
189 }
190 #endif /* AF_INET6 */
191