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