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