]>
Commit | Line | Data |
---|---|---|
0e076fb1 | 1 | /* |
2 | * Shamelessly duplicated from the bind9 public sources | |
3 | * for use by the Squid Project under ISC written permission | |
4 | * included "as found" below. | |
5 | * | |
6 | * Update/Maintenance History: | |
7 | * | |
8 | * 24-Sep-2007 : Copied from bind 9.3.3 | |
9 | * - Added protection around libray headers | |
10 | * - Altered configure checks to import | |
11 | * | |
12 | * 06-Oct-2007 : Various fixes to allow the build on MinGW | |
13 | * | |
14 | * 28-Oct-2007: drop some dead code. now tested working without. | |
15 | * | |
0e076fb1 | 16 | * Original License and code follows. |
17 | */ | |
18 | ||
f7f3304a | 19 | #include "squid.h" |
0e076fb1 | 20 | |
32d002cb | 21 | #if !HAVE_INET_PTON |
0e076fb1 | 22 | |
23 | /* | |
24 | * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") | |
25 | * Copyright (c) 1996,1999 by Internet Software Consortium. | |
26 | * | |
27 | * Permission to use, copy, modify, and distribute this software for any | |
28 | * purpose with or without fee is hereby granted, provided that the above | |
29 | * copyright notice and this permission notice appear in all copies. | |
30 | * | |
31 | * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES | |
32 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
33 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR | |
34 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
35 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
36 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT | |
37 | * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
38 | */ | |
39 | ||
40 | #if defined(LIBC_SCCS) && !defined(lint) | |
41 | static const char rcsid[] = "inet_pton.c,v 1.2.206.2 2005/07/28 07:43:18 marka Exp"; | |
42 | #endif /* LIBC_SCCS and not lint */ | |
43 | ||
44 | #if HAVE_SYS_PARAM_H | |
45 | #include <sys/param.h> | |
46 | #endif | |
47 | #if HAVE_SYS_TYPES_H | |
48 | #include <sys/types.h> | |
49 | #endif | |
50 | #if HAVE_SYS_SOCKET_H | |
51 | #include <sys/socket.h> | |
52 | #endif | |
53 | #if HAVE_NETINET_IN_H | |
54 | #include <netinet/in.h> | |
55 | #endif | |
56 | #if ARPA_INET_H | |
57 | #include <arpa/inet.h> | |
58 | #endif | |
59 | #if HAVE_ARPA_NAMESER_H | |
60 | #include <arpa/nameser.h> | |
61 | #endif | |
62 | #if HAVE_STRING_H | |
63 | #include <string.h> | |
64 | #endif | |
65 | #if HAVE_ERRNO_H | |
66 | #include <errno.h> | |
67 | #endif | |
68 | ||
69 | #if ! defined(NS_INADDRSZ) | |
70 | #define NS_INADDRSZ 4 | |
71 | #endif | |
72 | #if ! defined(NS_IN6ADDRSZ) | |
73 | #define NS_IN6ADDRSZ 16 | |
74 | #endif | |
75 | #if ! defined(NS_INT16SZ) | |
76 | #define NS_INT16SZ 2 | |
77 | #endif | |
78 | ||
79 | /* | |
80 | * WARNING: Don't even consider trying to compile this on a system where | |
81 | * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX. | |
82 | */ | |
83 | ||
84 | static int inet_pton4 (const char *src, u_char *dst); | |
85 | static int inet_pton6 (const char *src, u_char *dst); | |
86 | ||
87 | /* int | |
88 | * inet_pton(af, src, dst) | |
89 | * convert from presentation format (which usually means ASCII printable) | |
90 | * to network format (which is usually some kind of binary format). | |
91 | * return: | |
92 | * 1 if the address was valid for the specified address family | |
93 | * 0 if the address wasn't valid (`dst' is untouched in this case) | |
94 | * -1 if some other error occurred (`dst' is untouched in this case, too) | |
95 | * author: | |
96 | * Paul Vixie, 1996. | |
97 | */ | |
98 | int | |
99 | xinet_pton(af, src, dst) | |
26ac0430 AJ |
100 | int af; |
101 | const char *src; | |
102 | void *dst; | |
0e076fb1 | 103 | { |
26ac0430 AJ |
104 | switch (af) { |
105 | case AF_INET: | |
106 | return (inet_pton4(src, dst)); | |
107 | case AF_INET6: | |
108 | return (inet_pton6(src, dst)); | |
109 | default: | |
110 | errno = EAFNOSUPPORT; | |
111 | return (-1); | |
112 | } | |
113 | /* NOTREACHED */ | |
0e076fb1 | 114 | } |
115 | ||
116 | /* int | |
117 | * inet_pton4(src, dst) | |
118 | * like inet_aton() but without all the hexadecimal and shorthand. | |
119 | * return: | |
120 | * 1 if `src' is a valid dotted quad, else 0. | |
121 | * notice: | |
122 | * does not touch `dst' unless it's returning 1. | |
123 | * author: | |
124 | * Paul Vixie, 1996. | |
125 | */ | |
126 | static int | |
127 | inet_pton4(src, dst) | |
26ac0430 AJ |
128 | const char *src; |
129 | u_char *dst; | |
0e076fb1 | 130 | { |
26ac0430 AJ |
131 | static const char digits[] = "0123456789"; |
132 | int saw_digit, octets, ch; | |
133 | u_char tmp[NS_INADDRSZ], *tp; | |
0e076fb1 | 134 | |
26ac0430 AJ |
135 | saw_digit = 0; |
136 | octets = 0; | |
137 | *(tp = tmp) = 0; | |
138 | while ((ch = *src++) != '\0') { | |
139 | const char *pch; | |
0e076fb1 | 140 | |
26ac0430 AJ |
141 | if ((pch = strchr(digits, ch)) != NULL) { |
142 | u_int new = *tp * 10 + (pch - digits); | |
0e076fb1 | 143 | |
26ac0430 AJ |
144 | if (saw_digit && *tp == 0) |
145 | return (0); | |
146 | if (new > 255) | |
147 | return (0); | |
148 | *tp = new; | |
149 | if (!saw_digit) { | |
150 | if (++octets > 4) | |
151 | return (0); | |
152 | saw_digit = 1; | |
153 | } | |
154 | } else if (ch == '.' && saw_digit) { | |
155 | if (octets == 4) | |
156 | return (0); | |
157 | *++tp = 0; | |
158 | saw_digit = 0; | |
159 | } else | |
160 | return (0); | |
161 | } | |
162 | if (octets < 4) | |
163 | return (0); | |
164 | memcpy(dst, tmp, NS_INADDRSZ); | |
165 | return (1); | |
0e076fb1 | 166 | } |
167 | ||
168 | /* int | |
169 | * inet_pton6(src, dst) | |
170 | * convert presentation level address to network order binary form. | |
171 | * return: | |
172 | * 1 if `src' is a valid [RFC1884 2.2] address, else 0. | |
173 | * notice: | |
174 | * (1) does not touch `dst' unless it's returning 1. | |
175 | * (2) :: in a full address is silently ignored. | |
176 | * credit: | |
177 | * inspired by Mark Andrews. | |
178 | * author: | |
179 | * Paul Vixie, 1996. | |
180 | */ | |
181 | static int | |
182 | inet_pton6(src, dst) | |
26ac0430 AJ |
183 | const char *src; |
184 | u_char *dst; | |
0e076fb1 | 185 | { |
26ac0430 AJ |
186 | static const char xdigits_l[] = "0123456789abcdef", |
187 | xdigits_u[] = "0123456789ABCDEF"; | |
188 | u_char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp; | |
189 | const char *xdigits, *curtok; | |
190 | int ch, seen_xdigits; | |
191 | u_int val; | |
0e076fb1 | 192 | |
26ac0430 AJ |
193 | memset((tp = tmp), '\0', NS_IN6ADDRSZ); |
194 | endp = tp + NS_IN6ADDRSZ; | |
195 | colonp = NULL; | |
196 | /* Leading :: requires some special handling. */ | |
197 | if (*src == ':') | |
198 | if (*++src != ':') | |
199 | return (0); | |
200 | curtok = src; | |
201 | seen_xdigits = 0; | |
202 | val = 0; | |
203 | while ((ch = *src++) != '\0') { | |
204 | const char *pch; | |
0e076fb1 | 205 | |
26ac0430 AJ |
206 | if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL) |
207 | pch = strchr((xdigits = xdigits_u), ch); | |
208 | if (pch != NULL) { | |
209 | val <<= 4; | |
210 | val |= (pch - xdigits); | |
211 | if (++seen_xdigits > 4) | |
212 | return (0); | |
213 | continue; | |
214 | } | |
215 | if (ch == ':') { | |
216 | curtok = src; | |
217 | if (!seen_xdigits) { | |
218 | if (colonp) | |
219 | return (0); | |
220 | colonp = tp; | |
221 | continue; | |
222 | } else if (*src == '\0') { | |
223 | return (0); | |
224 | } | |
225 | if (tp + NS_INT16SZ > endp) | |
226 | return (0); | |
227 | *tp++ = (u_char) (val >> 8) & 0xff; | |
228 | *tp++ = (u_char) val & 0xff; | |
229 | seen_xdigits = 0; | |
230 | val = 0; | |
231 | continue; | |
232 | } | |
233 | if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) && | |
234 | inet_pton4(curtok, tp) > 0) { | |
235 | tp += NS_INADDRSZ; | |
236 | seen_xdigits = 0; | |
237 | break; /* '\0' was seen by inet_pton4(). */ | |
238 | } | |
239 | return (0); | |
240 | } | |
241 | if (seen_xdigits) { | |
242 | if (tp + NS_INT16SZ > endp) | |
243 | return (0); | |
244 | *tp++ = (u_char) (val >> 8) & 0xff; | |
245 | *tp++ = (u_char) val & 0xff; | |
246 | } | |
247 | if (colonp != NULL) { | |
248 | /* | |
249 | * Since some memmove()'s erroneously fail to handle | |
250 | * overlapping regions, we'll do the shift by hand. | |
251 | */ | |
252 | const int n = tp - colonp; | |
253 | int i; | |
0e076fb1 | 254 | |
26ac0430 AJ |
255 | if (tp == endp) |
256 | return (0); | |
257 | for (i = 1; i <= n; i++) { | |
258 | endp[- i] = colonp[n - i]; | |
259 | colonp[n - i] = 0; | |
260 | } | |
261 | tp = endp; | |
262 | } | |
263 | if (tp != endp) | |
264 | return (0); | |
265 | memcpy(dst, tmp, NS_IN6ADDRSZ); | |
266 | return (1); | |
0e076fb1 | 267 | } |
268 | ||
269 | #endif /* HAVE_INET_PTON */ |