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