1 | /* This is from the BIND 4.9.4 release, modified to compile by itself */ |
2 | |
3 | /* Copyright (c) Internet Software Consortium. |
4 | * |
5 | * Permission to use, copy, modify, and distribute this software for any |
6 | * purpose with or without fee is hereby granted, provided that the above |
7 | * copyright notice and this permission notice appear in all copies. |
8 | * |
9 | * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS |
10 | * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES |
11 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE |
12 | * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL |
13 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
14 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS |
15 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS |
16 | * SOFTWARE. |
17 | * |
18 | * SPDX-License-Identifier: ISC |
19 | */ |
20 | |
21 | #include "curl_setup.h" |
22 | |
23 | #ifndef HAVE_INET_PTON |
24 | |
25 | #ifdef HAVE_SYS_PARAM_H |
26 | #include <sys/param.h> |
27 | #endif |
28 | #ifdef HAVE_NETINET_IN_H |
29 | #include <netinet/in.h> |
30 | #endif |
31 | #ifdef HAVE_ARPA_INET_H |
32 | #include <arpa/inet.h> |
33 | #endif |
34 | |
35 | #include "inet_pton.h" |
36 | |
37 | #define IN6ADDRSZ 16 |
38 | #define INADDRSZ 4 |
39 | #define INT16SZ 2 |
40 | |
41 | /* |
42 | * If ENABLE_IPV6 is disabled, we still want to parse IPv6 addresses, so make |
43 | * sure we have _some_ value for AF_INET6 without polluting our fake value |
44 | * everywhere. |
45 | */ |
46 | #if !defined(ENABLE_IPV6) && !defined(AF_INET6) |
47 | #define AF_INET6 (AF_INET + 1) |
48 | #endif |
49 | |
50 | /* |
51 | * WARNING: Don't even consider trying to compile this on a system where |
52 | * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX. |
53 | */ |
54 | |
55 | static int inet_pton4(const char *src, unsigned char *dst); |
56 | static int inet_pton6(const char *src, unsigned char *dst); |
57 | |
58 | /* int |
59 | * inet_pton(af, src, dst) |
60 | * convert from presentation format (which usually means ASCII printable) |
61 | * to network format (which is usually some kind of binary format). |
62 | * return: |
63 | * 1 if the address was valid for the specified address family |
64 | * 0 if the address wasn't valid (`dst' is untouched in this case) |
65 | * -1 if some other error occurred (`dst' is untouched in this case, too) |
66 | * notice: |
67 | * On Windows we store the error in the thread errno, not |
68 | * in the winsock error code. This is to avoid losing the |
69 | * actual last winsock error. So when this function returns |
70 | * -1, check errno not SOCKERRNO. |
71 | * author: |
72 | * Paul Vixie, 1996. |
73 | */ |
74 | int |
75 | Curl_inet_pton(int af, const char *src, void *dst) |
76 | { |
77 | switch(af) { |
78 | case AF_INET: |
79 | return (inet_pton4(src, (unsigned char *)dst)); |
80 | case AF_INET6: |
81 | return (inet_pton6(src, (unsigned char *)dst)); |
82 | default: |
83 | errno = EAFNOSUPPORT; |
84 | return (-1); |
85 | } |
86 | /* NOTREACHED */ |
87 | } |
88 | |
89 | /* int |
90 | * inet_pton4(src, dst) |
91 | * like inet_aton() but without all the hexadecimal and shorthand. |
92 | * return: |
93 | * 1 if `src' is a valid dotted quad, else 0. |
94 | * notice: |
95 | * does not touch `dst' unless it's returning 1. |
96 | * author: |
97 | * Paul Vixie, 1996. |
98 | */ |
99 | static int |
100 | inet_pton4(const char *src, unsigned char *dst) |
101 | { |
102 | static const char digits[] = "0123456789" ; |
103 | int saw_digit, octets, ch; |
104 | unsigned char tmp[INADDRSZ], *tp; |
105 | |
106 | saw_digit = 0; |
107 | octets = 0; |
108 | tp = tmp; |
109 | *tp = 0; |
110 | while((ch = *src++) != '\0') { |
111 | const char *pch; |
112 | |
113 | pch = strchr(digits, ch); |
114 | if(pch) { |
115 | unsigned int val = *tp * 10 + (unsigned int)(pch - digits); |
116 | |
117 | if(saw_digit && *tp == 0) |
118 | return (0); |
119 | if(val > 255) |
120 | return (0); |
121 | *tp = (unsigned char)val; |
122 | if(!saw_digit) { |
123 | if(++octets > 4) |
124 | return (0); |
125 | saw_digit = 1; |
126 | } |
127 | } |
128 | else if(ch == '.' && saw_digit) { |
129 | if(octets == 4) |
130 | return (0); |
131 | *++tp = 0; |
132 | saw_digit = 0; |
133 | } |
134 | else |
135 | return (0); |
136 | } |
137 | if(octets < 4) |
138 | return (0); |
139 | memcpy(dst, tmp, INADDRSZ); |
140 | return (1); |
141 | } |
142 | |
143 | /* int |
144 | * inet_pton6(src, dst) |
145 | * convert presentation level address to network order binary form. |
146 | * return: |
147 | * 1 if `src' is a valid [RFC1884 2.2] address, else 0. |
148 | * notice: |
149 | * (1) does not touch `dst' unless it's returning 1. |
150 | * (2) :: in a full address is silently ignored. |
151 | * credit: |
152 | * inspired by Mark Andrews. |
153 | * author: |
154 | * Paul Vixie, 1996. |
155 | */ |
156 | static int |
157 | inet_pton6(const char *src, unsigned char *dst) |
158 | { |
159 | static const char xdigits_l[] = "0123456789abcdef" , |
160 | xdigits_u[] = "0123456789ABCDEF" ; |
161 | unsigned char tmp[IN6ADDRSZ], *tp, *endp, *colonp; |
162 | const char *curtok; |
163 | int ch, saw_xdigit; |
164 | size_t val; |
165 | |
166 | memset((tp = tmp), 0, IN6ADDRSZ); |
167 | endp = tp + IN6ADDRSZ; |
168 | colonp = NULL; |
169 | /* Leading :: requires some special handling. */ |
170 | if(*src == ':') |
171 | if(*++src != ':') |
172 | return (0); |
173 | curtok = src; |
174 | saw_xdigit = 0; |
175 | val = 0; |
176 | while((ch = *src++) != '\0') { |
177 | const char *xdigits; |
178 | const char *pch; |
179 | |
180 | pch = strchr((xdigits = xdigits_l), ch); |
181 | if(!pch) |
182 | pch = strchr((xdigits = xdigits_u), ch); |
183 | if(pch) { |
184 | val <<= 4; |
185 | val |= (pch - xdigits); |
186 | if(++saw_xdigit > 4) |
187 | return (0); |
188 | continue; |
189 | } |
190 | if(ch == ':') { |
191 | curtok = src; |
192 | if(!saw_xdigit) { |
193 | if(colonp) |
194 | return (0); |
195 | colonp = tp; |
196 | continue; |
197 | } |
198 | if(tp + INT16SZ > endp) |
199 | return (0); |
200 | *tp++ = (unsigned char) ((val >> 8) & 0xff); |
201 | *tp++ = (unsigned char) (val & 0xff); |
202 | saw_xdigit = 0; |
203 | val = 0; |
204 | continue; |
205 | } |
206 | if(ch == '.' && ((tp + INADDRSZ) <= endp) && |
207 | inet_pton4(curtok, tp) > 0) { |
208 | tp += INADDRSZ; |
209 | saw_xdigit = 0; |
210 | break; /* '\0' was seen by inet_pton4(). */ |
211 | } |
212 | return (0); |
213 | } |
214 | if(saw_xdigit) { |
215 | if(tp + INT16SZ > endp) |
216 | return (0); |
217 | *tp++ = (unsigned char) ((val >> 8) & 0xff); |
218 | *tp++ = (unsigned char) (val & 0xff); |
219 | } |
220 | if(colonp) { |
221 | /* |
222 | * Since some memmove()'s erroneously fail to handle |
223 | * overlapping regions, we'll do the shift by hand. |
224 | */ |
225 | const ssize_t n = tp - colonp; |
226 | ssize_t i; |
227 | |
228 | if(tp == endp) |
229 | return (0); |
230 | for(i = 1; i <= n; i++) { |
231 | *(endp - i) = *(colonp + n - i); |
232 | *(colonp + n - i) = 0; |
233 | } |
234 | tp = endp; |
235 | } |
236 | if(tp != endp) |
237 | return (0); |
238 | memcpy(dst, tmp, IN6ADDRSZ); |
239 | return (1); |
240 | } |
241 | |
242 | #endif /* HAVE_INET_PTON */ |
243 | |