diff options
Diffstat (limited to 'openbsd-compat/inet_ntop.c')
-rw-r--r-- | openbsd-compat/inet_ntop.c | 51 |
1 files changed, 34 insertions, 17 deletions
diff --git a/openbsd-compat/inet_ntop.c b/openbsd-compat/inet_ntop.c index 2b8d31f8d..3bea519af 100644 --- a/openbsd-compat/inet_ntop.c +++ b/openbsd-compat/inet_ntop.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: inet_ntop.c,v 1.1 1997/03/13 19:07:32 downsj Exp $ */ | 1 | /* $OpenBSD: inet_ntop.c,v 1.5 2002/08/23 16:27:31 itojun Exp $ */ |
2 | 2 | ||
3 | /* Copyright (c) 1996 by Internet Software Consortium. | 3 | /* Copyright (c) 1996 by Internet Software Consortium. |
4 | * | 4 | * |
@@ -16,7 +16,7 @@ | |||
16 | * SOFTWARE. | 16 | * SOFTWARE. |
17 | */ | 17 | */ |
18 | 18 | ||
19 | #include "config.h" | 19 | #include "includes.h" |
20 | 20 | ||
21 | #ifndef HAVE_INET_NTOP | 21 | #ifndef HAVE_INET_NTOP |
22 | 22 | ||
@@ -24,7 +24,7 @@ | |||
24 | #if 0 | 24 | #if 0 |
25 | static char rcsid[] = "$From: inet_ntop.c,v 8.7 1996/08/05 08:41:18 vixie Exp $"; | 25 | static char rcsid[] = "$From: inet_ntop.c,v 8.7 1996/08/05 08:41:18 vixie Exp $"; |
26 | #else | 26 | #else |
27 | static char rcsid[] = "$OpenBSD: inet_ntop.c,v 1.1 1997/03/13 19:07:32 downsj Exp $"; | 27 | static char rcsid[] = "$OpenBSD: inet_ntop.c,v 1.5 2002/08/23 16:27:31 itojun Exp $"; |
28 | #endif | 28 | #endif |
29 | #endif /* LIBC_SCCS and not lint */ | 29 | #endif /* LIBC_SCCS and not lint */ |
30 | 30 | ||
@@ -54,8 +54,8 @@ static char rcsid[] = "$OpenBSD: inet_ntop.c,v 1.1 1997/03/13 19:07:32 downsj Ex | |||
54 | * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX. | 54 | * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX. |
55 | */ | 55 | */ |
56 | 56 | ||
57 | static const char *inet_ntop4 __P((const u_char *src, char *dst, size_t size)); | 57 | static const char *inet_ntop4(const u_char *src, char *dst, size_t size); |
58 | static const char *inet_ntop6 __P((const u_char *src, char *dst, size_t size)); | 58 | static const char *inet_ntop6(const u_char *src, char *dst, size_t size); |
59 | 59 | ||
60 | /* char * | 60 | /* char * |
61 | * inet_ntop(af, src, dst, size) | 61 | * inet_ntop(af, src, dst, size) |
@@ -103,13 +103,14 @@ inet_ntop4(src, dst, size) | |||
103 | { | 103 | { |
104 | static const char fmt[] = "%u.%u.%u.%u"; | 104 | static const char fmt[] = "%u.%u.%u.%u"; |
105 | char tmp[sizeof "255.255.255.255"]; | 105 | char tmp[sizeof "255.255.255.255"]; |
106 | int l; | ||
106 | 107 | ||
107 | if (snprintf(tmp, sizeof(tmp), fmt, src[0], src[1], src[2], | 108 | l = snprintf(tmp, size, fmt, src[0], src[1], src[2], src[3]); |
108 | src[3]) > size) { | 109 | if (l <= 0 || l >= size) { |
109 | errno = ENOSPC; | 110 | errno = ENOSPC; |
110 | return (NULL); | 111 | return (NULL); |
111 | } | 112 | } |
112 | strcpy(dst, tmp); | 113 | strlcpy(dst, tmp, size); |
113 | return (dst); | 114 | return (dst); |
114 | } | 115 | } |
115 | 116 | ||
@@ -132,10 +133,12 @@ inet_ntop6(src, dst, size) | |||
132 | * Keep this in mind if you think this function should have been coded | 133 | * Keep this in mind if you think this function should have been coded |
133 | * to use pointer overlays. All the world's not a VAX. | 134 | * to use pointer overlays. All the world's not a VAX. |
134 | */ | 135 | */ |
135 | char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"], *tp; | 136 | char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"]; |
137 | char *tp, *ep; | ||
136 | struct { int base, len; } best, cur; | 138 | struct { int base, len; } best, cur; |
137 | u_int words[IN6ADDRSZ / INT16SZ]; | 139 | u_int words[IN6ADDRSZ / INT16SZ]; |
138 | int i; | 140 | int i; |
141 | int advance; | ||
139 | 142 | ||
140 | /* | 143 | /* |
141 | * Preprocess: | 144 | * Preprocess: |
@@ -172,31 +175,45 @@ inet_ntop6(src, dst, size) | |||
172 | * Format the result. | 175 | * Format the result. |
173 | */ | 176 | */ |
174 | tp = tmp; | 177 | tp = tmp; |
175 | for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++) { | 178 | ep = tmp + sizeof(tmp); |
179 | for (i = 0; i < (IN6ADDRSZ / INT16SZ) && tp < ep; i++) { | ||
176 | /* Are we inside the best run of 0x00's? */ | 180 | /* Are we inside the best run of 0x00's? */ |
177 | if (best.base != -1 && i >= best.base && | 181 | if (best.base != -1 && i >= best.base && |
178 | i < (best.base + best.len)) { | 182 | i < (best.base + best.len)) { |
179 | if (i == best.base) | 183 | if (i == best.base) { |
184 | if (tp + 1 >= ep) | ||
185 | return (NULL); | ||
180 | *tp++ = ':'; | 186 | *tp++ = ':'; |
187 | } | ||
181 | continue; | 188 | continue; |
182 | } | 189 | } |
183 | /* Are we following an initial run of 0x00s or any real hex? */ | 190 | /* Are we following an initial run of 0x00s or any real hex? */ |
184 | if (i != 0) | 191 | if (i != 0) { |
192 | if (tp + 1 >= ep) | ||
193 | return (NULL); | ||
185 | *tp++ = ':'; | 194 | *tp++ = ':'; |
195 | } | ||
186 | /* Is this address an encapsulated IPv4? */ | 196 | /* Is this address an encapsulated IPv4? */ |
187 | if (i == 6 && best.base == 0 && | 197 | if (i == 6 && best.base == 0 && |
188 | (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) { | 198 | (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) { |
189 | if (!inet_ntop4(src+12, tp, sizeof tmp - (tp - tmp))) | 199 | if (!inet_ntop4(src+12, tp, (size_t)(ep - tp))) |
190 | return (NULL); | 200 | return (NULL); |
191 | tp += strlen(tp); | 201 | tp += strlen(tp); |
192 | break; | 202 | break; |
193 | } | 203 | } |
194 | snprintf(tp, sizeof(tmp - (tp - tmp)), "%x", words[i]); | 204 | advance = snprintf(tp, ep - tp, "%x", words[i]); |
195 | tp += strlen(tp); | 205 | if (advance <= 0 || advance >= ep - tp) |
206 | return (NULL); | ||
207 | tp += advance; | ||
196 | } | 208 | } |
197 | /* Was it a trailing run of 0x00's? */ | 209 | /* Was it a trailing run of 0x00's? */ |
198 | if (best.base != -1 && (best.base + best.len) == (IN6ADDRSZ / INT16SZ)) | 210 | if (best.base != -1 && (best.base + best.len) == (IN6ADDRSZ / INT16SZ)) { |
211 | if (tp + 1 >= ep) | ||
212 | return (NULL); | ||
199 | *tp++ = ':'; | 213 | *tp++ = ':'; |
214 | } | ||
215 | if (tp + 1 >= ep) | ||
216 | return (NULL); | ||
200 | *tp++ = '\0'; | 217 | *tp++ = '\0'; |
201 | 218 | ||
202 | /* | 219 | /* |
@@ -206,7 +223,7 @@ inet_ntop6(src, dst, size) | |||
206 | errno = ENOSPC; | 223 | errno = ENOSPC; |
207 | return (NULL); | 224 | return (NULL); |
208 | } | 225 | } |
209 | strcpy(dst, tmp); | 226 | strlcpy(dst, tmp, size); |
210 | return (dst); | 227 | return (dst); |
211 | } | 228 | } |
212 | 229 | ||