1 | /* This is from the BIND 4.9.4 release, modified to compile by itself */ |
2 | |
3 | /* Copyright (c) 2003 - 2022 by 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 | * WARNING: Don't even consider trying to compile this on a system where |
43 | * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX. |
44 | */ |
45 | |
46 | static int inet_pton4(const char *src, unsigned char *dst); |
47 | #ifdef ENABLE_IPV6 |
48 | static int inet_pton6(const char *src, unsigned char *dst); |
49 | #endif |
50 | |
51 | /* int |
52 | * inet_pton(af, src, dst) |
53 | * convert from presentation format (which usually means ASCII printable) |
54 | * to network format (which is usually some kind of binary format). |
55 | * return: |
56 | * 1 if the address was valid for the specified address family |
57 | * 0 if the address wasn't valid (`dst' is untouched in this case) |
58 | * -1 if some other error occurred (`dst' is untouched in this case, too) |
59 | * notice: |
60 | * On Windows we store the error in the thread errno, not |
61 | * in the winsock error code. This is to avoid losing the |
62 | * actual last winsock error. So when this function returns |
63 | * -1, check errno not SOCKERRNO. |
64 | * author: |
65 | * Paul Vixie, 1996. |
66 | */ |
67 | int |
68 | Curl_inet_pton(int af, const char *src, void *dst) |
69 | { |
70 | switch(af) { |
71 | case AF_INET: |
72 | return (inet_pton4(src, (unsigned char *)dst)); |
73 | #ifdef ENABLE_IPV6 |
74 | case AF_INET6: |
75 | return (inet_pton6(src, (unsigned char *)dst)); |
76 | #endif |
77 | default: |
78 | errno = EAFNOSUPPORT; |
79 | return (-1); |
80 | } |
81 | /* NOTREACHED */ |
82 | } |
83 | |
84 | /* int |
85 | * inet_pton4(src, dst) |
86 | * like inet_aton() but without all the hexadecimal and shorthand. |
87 | * return: |
88 | * 1 if `src' is a valid dotted quad, else 0. |
89 | * notice: |
90 | * does not touch `dst' unless it's returning 1. |
91 | * author: |
92 | * Paul Vixie, 1996. |
93 | */ |
94 | static int |
95 | inet_pton4(const char *src, unsigned char *dst) |
96 | { |
97 | static const char digits[] = "0123456789" ; |
98 | int saw_digit, octets, ch; |
99 | unsigned char tmp[INADDRSZ], *tp; |
100 | |
101 | saw_digit = 0; |
102 | octets = 0; |
103 | tp = tmp; |
104 | *tp = 0; |
105 | while((ch = *src++) != '\0') { |
106 | const char *pch; |
107 | |
108 | pch = strchr(digits, ch); |
109 | if(pch) { |
110 | unsigned int val = *tp * 10 + (unsigned int)(pch - digits); |
111 | |
112 | if(saw_digit && *tp == 0) |
113 | return (0); |
114 | if(val > 255) |
115 | return (0); |
116 | *tp = (unsigned char)val; |
117 | if(!saw_digit) { |
118 | if(++octets > 4) |
119 | return (0); |
120 | saw_digit = 1; |
121 | } |
122 | } |
123 | else if(ch == '.' && saw_digit) { |
124 | if(octets == 4) |
125 | return (0); |
126 | *++tp = 0; |
127 | saw_digit = 0; |
128 | } |
129 | else |
130 | return (0); |
131 | } |
132 | if(octets < 4) |
133 | return (0); |
134 | memcpy(dst, tmp, INADDRSZ); |
135 | return (1); |
136 | } |
137 | |
138 | #ifdef ENABLE_IPV6 |
139 | /* int |
140 | * inet_pton6(src, dst) |
141 | * convert presentation level address to network order binary form. |
142 | * return: |
143 | * 1 if `src' is a valid [RFC1884 2.2] address, else 0. |
144 | * notice: |
145 | * (1) does not touch `dst' unless it's returning 1. |
146 | * (2) :: in a full address is silently ignored. |
147 | * credit: |
148 | * inspired by Mark Andrews. |
149 | * author: |
150 | * Paul Vixie, 1996. |
151 | */ |
152 | static int |
153 | inet_pton6(const char *src, unsigned char *dst) |
154 | { |
155 | static const char xdigits_l[] = "0123456789abcdef" , |
156 | xdigits_u[] = "0123456789ABCDEF" ; |
157 | unsigned char tmp[IN6ADDRSZ], *tp, *endp, *colonp; |
158 | const char *curtok; |
159 | int ch, saw_xdigit; |
160 | size_t val; |
161 | |
162 | memset((tp = tmp), 0, IN6ADDRSZ); |
163 | endp = tp + IN6ADDRSZ; |
164 | colonp = NULL; |
165 | /* Leading :: requires some special handling. */ |
166 | if(*src == ':') |
167 | if(*++src != ':') |
168 | return (0); |
169 | curtok = src; |
170 | saw_xdigit = 0; |
171 | val = 0; |
172 | while((ch = *src++) != '\0') { |
173 | const char *xdigits; |
174 | const char *pch; |
175 | |
176 | pch = strchr((xdigits = xdigits_l), ch); |
177 | if(!pch) |
178 | pch = strchr((xdigits = xdigits_u), ch); |
179 | if(pch) { |
180 | val <<= 4; |
181 | val |= (pch - xdigits); |
182 | if(++saw_xdigit > 4) |
183 | return (0); |
184 | continue; |
185 | } |
186 | if(ch == ':') { |
187 | curtok = src; |
188 | if(!saw_xdigit) { |
189 | if(colonp) |
190 | return (0); |
191 | colonp = tp; |
192 | continue; |
193 | } |
194 | if(tp + INT16SZ > endp) |
195 | return (0); |
196 | *tp++ = (unsigned char) ((val >> 8) & 0xff); |
197 | *tp++ = (unsigned char) (val & 0xff); |
198 | saw_xdigit = 0; |
199 | val = 0; |
200 | continue; |
201 | } |
202 | if(ch == '.' && ((tp + INADDRSZ) <= endp) && |
203 | inet_pton4(curtok, tp) > 0) { |
204 | tp += INADDRSZ; |
205 | saw_xdigit = 0; |
206 | break; /* '\0' was seen by inet_pton4(). */ |
207 | } |
208 | return (0); |
209 | } |
210 | if(saw_xdigit) { |
211 | if(tp + INT16SZ > endp) |
212 | return (0); |
213 | *tp++ = (unsigned char) ((val >> 8) & 0xff); |
214 | *tp++ = (unsigned char) (val & 0xff); |
215 | } |
216 | if(colonp) { |
217 | /* |
218 | * Since some memmove()'s erroneously fail to handle |
219 | * overlapping regions, we'll do the shift by hand. |
220 | */ |
221 | const ssize_t n = tp - colonp; |
222 | ssize_t i; |
223 | |
224 | if(tp == endp) |
225 | return (0); |
226 | for(i = 1; i <= n; i++) { |
227 | *(endp - i) = *(colonp + n - i); |
228 | *(colonp + n - i) = 0; |
229 | } |
230 | tp = endp; |
231 | } |
232 | if(tp != endp) |
233 | return (0); |
234 | memcpy(dst, tmp, IN6ADDRSZ); |
235 | return (1); |
236 | } |
237 | #endif /* ENABLE_IPV6 */ |
238 | |
239 | #endif /* HAVE_INET_PTON */ |
240 | |