Debian Codegraph @

extra/curl/curl-8.14.1/lib/curlx/inet_pton.c

raw
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 #include "../curl_ctype.h"
23 #include "strparse.h"
24
25 #ifndef HAVE_INET_PTON
26
27 #ifdef HAVE_SYS_PARAM_H
28 #include <sys/param.h>
29 #endif
30 #ifdef HAVE_NETINET_IN_H
31 #include <netinet/in.h>
32 #endif
33 #ifdef HAVE_ARPA_INET_H
34 #include <arpa/inet.h>
35 #endif
36
37 #include "inet_pton.h"
38
39 #define IN6ADDRSZ 16
40 #define INADDRSZ 4
41 #define INT16SZ 2
42
43 /*
44 * If USE_IPV6 is disabled, we still want to parse IPv6 addresses, so make
45 * sure we have _some_ value for AF_INET6 without polluting our fake value
46 * everywhere.
47 */
48 #if !defined(USE_IPV6) && !defined(AF_INET6)
49 #define AF_INET6 (AF_INET + 1)
50 #endif
51
52 /*
53 * WARNING: Do not even consider trying to compile this on a system where
54 * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.
55 */
56
57 static int inet_pton4(const char *src, unsigned char *dst);
58 static int inet_pton6(const char *src, unsigned char *dst);
59
60 /* int
61 * inet_pton(af, src, dst)
62 * convert from presentation format (which usually means ASCII printable)
63 * to network format (which is usually some kind of binary format).
64 * return:
65 * 1 if the address was valid for the specified address family
66 * 0 if the address was not valid (`dst' is untouched in this case)
67 * -1 if some other error occurred (`dst' is untouched in this case, too)
68 * notice:
69 * On Windows we store the error in the thread errno, not
70 * in the Winsock error code. This is to avoid losing the
71 * actual last Winsock error. When this function returns
72 * -1, check errno not SOCKERRNO.
73 * author:
74 * Paul Vixie, 1996.
75 */
76 int
77 curlx_inet_pton(int af, const char *src, void *dst)
78 {
79 switch(af) {
80 case AF_INET:
81 return inet_pton4(src, (unsigned char *)dst);
82 case AF_INET6:
83 return inet_pton6(src, (unsigned char *)dst);
84 default:
85 CURL_SETERRNO(SOCKEAFNOSUPPORT);
86 return -1;
87 }
88 /* NOTREACHED */
89 }
90
91 /* int
92 * inet_pton4(src, dst)
93 * like inet_aton() but without all the hexadecimal and shorthand.
94 * return:
95 * 1 if `src' is a valid dotted quad, else 0.
96 * notice:
97 * does not touch `dst' unless it is returning 1.
98 * author:
99 * Paul Vixie, 1996.
100 */
101 static int
102 inet_pton4(const char *src, unsigned char *dst)
103 {
104 int saw_digit, octets, ch;
105 unsigned char tmp[INADDRSZ], *tp;
106
107 saw_digit = 0;
108 octets = 0;
109 tp = tmp;
110 *tp = 0;
111 while((ch = *src++) != '\0') {
112 if(ISDIGIT(ch)) {
113 unsigned int val = (*tp * 10) + (ch - '0');
114
115 if(saw_digit && *tp == 0)
116 return 0;
117 if(val > 255)
118 return 0;
119 *tp = (unsigned char)val;
120 if(!saw_digit) {
121 if(++octets > 4)
122 return 0;
123 saw_digit = 1;
124 }
125 }
126 else if(ch == '.' && saw_digit) {
127 if(octets == 4)
128 return 0;
129 *++tp = 0;
130 saw_digit = 0;
131 }
132 else
133 return 0;
134 }
135 if(octets < 4)
136 return 0;
137 memcpy(dst, tmp, INADDRSZ);
138 return 1;
139 }
140
141 /* int
142 * inet_pton6(src, dst)
143 * convert presentation level address to network order binary form.
144 * return:
145 * 1 if `src' is a valid [RFC1884 2.2] address, else 0.
146 * notice:
147 * (1) does not touch `dst' unless it is returning 1.
148 * (2) :: in a full address is silently ignored.
149 * credit:
150 * inspired by Mark Andrews.
151 * author:
152 * Paul Vixie, 1996.
153 */
154 static int
155 inet_pton6(const char *src, unsigned char *dst)
156 {
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 if(ISXDIGIT(ch)) {
174 val <<= 4;
175 val |= Curl_hexval(ch);
176 if(++saw_xdigit > 4)
177 return 0;
178 continue;
179 }
180 if(ch == ':') {
181 curtok = src;
182 if(!saw_xdigit) {
183 if(colonp)
184 return 0;
185 colonp = tp;
186 continue;
187 }
188 if(tp + INT16SZ > endp)
189 return 0;
190 *tp++ = (unsigned char) ((val >> 8) & 0xff);
191 *tp++ = (unsigned char) (val & 0xff);
192 saw_xdigit = 0;
193 val = 0;
194 continue;
195 }
196 if(ch == '.' && ((tp + INADDRSZ) <= endp) &&
197 inet_pton4(curtok, tp) > 0) {
198 tp += INADDRSZ;
199 saw_xdigit = 0;
200 break; /* '\0' was seen by inet_pton4(). */
201 }
202 return 0;
203 }
204 if(saw_xdigit) {
205 if(tp + INT16SZ > endp)
206 return 0;
207 *tp++ = (unsigned char) ((val >> 8) & 0xff);
208 *tp++ = (unsigned char) (val & 0xff);
209 }
210 if(colonp) {
211 /*
212 * Since some memmove()'s erroneously fail to handle
213 * overlapping regions, we will do the shift by hand.
214 */
215 const ssize_t n = tp - colonp;
216 ssize_t i;
217
218 if(tp == endp)
219 return 0;
220 for(i = 1; i <= n; i++) {
221 *(endp - i) = *(colonp + n - i);
222 *(colonp + n - i) = 0;
223 }
224 tp = endp;
225 }
226 if(tp != endp)
227 return 0;
228 memcpy(dst, tmp, IN6ADDRSZ);
229 return 1;
230 }
231
232 #endif /* HAVE_INET_PTON */