1 | /*************************************************************************** |
2 | * _ _ ____ _ |
3 | * Project ___| | | | _ \| | |
4 | * / __| | | | |_) | | |
5 | * | (__| |_| | _ <| |___ |
6 | * \___|\___/|_| \_\_____| |
7 | * |
8 | * Copyright (C) 1998 - 2022, Daniel Stenberg, <[email protected]>, et al. |
9 | * |
10 | * This software is licensed as described in the file COPYING, which |
11 | * you should have received as part of this distribution. The terms |
12 | * are also available at https://curl.se/docs/copyright.html. |
13 | * |
14 | * You may opt to use, copy, modify, merge, publish, distribute and/or sell |
15 | * copies of the Software, and permit persons to whom the Software is |
16 | * furnished to do so, under the terms of the COPYING file. |
17 | * |
18 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
19 | * KIND, either express or implied. |
20 | * |
21 | * SPDX-License-Identifier: curl |
22 | * |
23 | ***************************************************************************/ |
24 | |
25 | #include "curl_setup.h" |
26 | |
27 | /*********************************************************************** |
28 | * Only for plain IPv4 builds |
29 | **********************************************************************/ |
30 | #ifdef CURLRES_IPV4 /* plain IPv4 code coming up */ |
31 | |
32 | #ifdef HAVE_NETINET_IN_H |
33 | #include <netinet/in.h> |
34 | #endif |
35 | #ifdef HAVE_NETDB_H |
36 | #include <netdb.h> |
37 | #endif |
38 | #ifdef HAVE_ARPA_INET_H |
39 | #include <arpa/inet.h> |
40 | #endif |
41 | #ifdef __VMS |
42 | #include <in.h> |
43 | #include <inet.h> |
44 | #endif |
45 | |
46 | #ifdef HAVE_PROCESS_H |
47 | #include <process.h> |
48 | #endif |
49 | |
50 | #include "urldata.h" |
51 | #include "sendf.h" |
52 | #include "hostip.h" |
53 | #include "hash.h" |
54 | #include "share.h" |
55 | #include "url.h" |
56 | /* The last 3 #include files should be in this order */ |
57 | #include "curl_printf.h" |
58 | #include "curl_memory.h" |
59 | #include "memdebug.h" |
60 | |
61 | /* |
62 | * Curl_ipvalid() checks what CURL_IPRESOLVE_* requirements that might've |
63 | * been set and returns TRUE if they are OK. |
64 | */ |
65 | bool Curl_ipvalid(struct Curl_easy *data, struct connectdata *conn) |
66 | { |
67 | (void)data; |
68 | if(conn->ip_version == CURL_IPRESOLVE_V6) |
69 | /* An IPv6 address was requested and we can't get/use one */ |
70 | return FALSE; |
71 | |
72 | return TRUE; /* OK, proceed */ |
73 | } |
74 | |
75 | #ifdef CURLRES_SYNCH |
76 | |
77 | /* |
78 | * Curl_getaddrinfo() - the IPv4 synchronous version. |
79 | * |
80 | * The original code to this function was from the Dancer source code, written |
81 | * by Bjorn Reese, it has since been patched and modified considerably. |
82 | * |
83 | * gethostbyname_r() is the thread-safe version of the gethostbyname() |
84 | * function. When we build for plain IPv4, we attempt to use this |
85 | * function. There are _three_ different gethostbyname_r() versions, and we |
86 | * detect which one this platform supports in the configure script and set up |
87 | * the HAVE_GETHOSTBYNAME_R_3, HAVE_GETHOSTBYNAME_R_5 or |
88 | * HAVE_GETHOSTBYNAME_R_6 defines accordingly. Note that HAVE_GETADDRBYNAME |
89 | * has the corresponding rules. This is primarily on *nix. Note that some unix |
90 | * flavours have thread-safe versions of the plain gethostbyname() etc. |
91 | * |
92 | */ |
93 | struct Curl_addrinfo *Curl_getaddrinfo(struct Curl_easy *data, |
94 | const char *hostname, |
95 | int port, |
96 | int *waitp) |
97 | { |
98 | struct Curl_addrinfo *ai = NULL; |
99 | |
100 | #ifdef CURL_DISABLE_VERBOSE_STRINGS |
101 | (void)data; |
102 | #endif |
103 | |
104 | *waitp = 0; /* synchronous response only */ |
105 | |
106 | ai = Curl_ipv4_resolve_r(hostname, port); |
107 | if(!ai) |
108 | infof(data, "Curl_ipv4_resolve_r failed for %s" , hostname); |
109 | |
110 | return ai; |
111 | } |
112 | #endif /* CURLRES_SYNCH */ |
113 | #endif /* CURLRES_IPV4 */ |
114 | |
115 | #if defined(CURLRES_IPV4) && \ |
116 | !defined(CURLRES_ARES) && !defined(CURLRES_AMIGA) |
117 | |
118 | /* |
119 | * Curl_ipv4_resolve_r() - ipv4 threadsafe resolver function. |
120 | * |
121 | * This is used for both synchronous and asynchronous resolver builds, |
122 | * implying that only threadsafe code and function calls may be used. |
123 | * |
124 | */ |
125 | struct Curl_addrinfo *Curl_ipv4_resolve_r(const char *hostname, |
126 | int port) |
127 | { |
128 | #if !defined(HAVE_GETADDRINFO_THREADSAFE) && defined(HAVE_GETHOSTBYNAME_R_3) |
129 | int res; |
130 | #endif |
131 | struct Curl_addrinfo *ai = NULL; |
132 | struct hostent *h = NULL; |
133 | struct hostent *buf = NULL; |
134 | |
135 | #if defined(HAVE_GETADDRINFO_THREADSAFE) |
136 | struct addrinfo hints; |
137 | char sbuf[12]; |
138 | char *sbufptr = NULL; |
139 | |
140 | memset(&hints, 0, sizeof(hints)); |
141 | hints.ai_family = PF_INET; |
142 | hints.ai_socktype = SOCK_STREAM; |
143 | if(port) { |
144 | msnprintf(sbuf, sizeof(sbuf), "%d" , port); |
145 | sbufptr = sbuf; |
146 | } |
147 | |
148 | (void)Curl_getaddrinfo_ex(hostname, sbufptr, &hints, &ai); |
149 | |
150 | #elif defined(HAVE_GETHOSTBYNAME_R) |
151 | /* |
152 | * gethostbyname_r() is the preferred resolve function for many platforms. |
153 | * Since there are three different versions of it, the following code is |
154 | * somewhat #ifdef-ridden. |
155 | */ |
156 | int h_errnop; |
157 | |
158 | buf = calloc(1, CURL_HOSTENT_SIZE); |
159 | if(!buf) |
160 | return NULL; /* major failure */ |
161 | /* |
162 | * The clearing of the buffer is a workaround for a gethostbyname_r bug in |
163 | * qnx nto and it is also _required_ for some of these functions on some |
164 | * platforms. |
165 | */ |
166 | |
167 | #if defined(HAVE_GETHOSTBYNAME_R_5) |
168 | /* Solaris, IRIX and more */ |
169 | h = gethostbyname_r(hostname, |
170 | (struct hostent *)buf, |
171 | (char *)buf + sizeof(struct hostent), |
172 | CURL_HOSTENT_SIZE - sizeof(struct hostent), |
173 | &h_errnop); |
174 | |
175 | /* If the buffer is too small, it returns NULL and sets errno to |
176 | * ERANGE. The errno is thread safe if this is compiled with |
177 | * -D_REENTRANT as then the 'errno' variable is a macro defined to get |
178 | * used properly for threads. |
179 | */ |
180 | |
181 | if(h) { |
182 | ; |
183 | } |
184 | else |
185 | #elif defined(HAVE_GETHOSTBYNAME_R_6) |
186 | /* Linux */ |
187 | |
188 | (void)gethostbyname_r(hostname, |
189 | (struct hostent *)buf, |
190 | (char *)buf + sizeof(struct hostent), |
191 | CURL_HOSTENT_SIZE - sizeof(struct hostent), |
192 | &h, /* DIFFERENCE */ |
193 | &h_errnop); |
194 | /* Redhat 8, using glibc 2.2.93 changed the behavior. Now all of a |
195 | * sudden this function returns EAGAIN if the given buffer size is too |
196 | * small. Previous versions are known to return ERANGE for the same |
197 | * problem. |
198 | * |
199 | * This wouldn't be such a big problem if older versions wouldn't |
200 | * sometimes return EAGAIN on a common failure case. Alas, we can't |
201 | * assume that EAGAIN *or* ERANGE means ERANGE for any given version of |
202 | * glibc. |
203 | * |
204 | * For now, we do that and thus we may call the function repeatedly and |
205 | * fail for older glibc versions that return EAGAIN, until we run out of |
206 | * buffer size (step_size grows beyond CURL_HOSTENT_SIZE). |
207 | * |
208 | * If anyone has a better fix, please tell us! |
209 | * |
210 | * ------------------------------------------------------------------- |
211 | * |
212 | * On October 23rd 2003, Dan C dug up more details on the mysteries of |
213 | * gethostbyname_r() in glibc: |
214 | * |
215 | * In glibc 2.2.5 the interface is different (this has also been |
216 | * discovered in glibc 2.1.1-6 as shipped by Redhat 6). What I can't |
217 | * explain, is that tests performed on glibc 2.2.4-34 and 2.2.4-32 |
218 | * (shipped/upgraded by Redhat 7.2) don't show this behavior! |
219 | * |
220 | * In this "buggy" version, the return code is -1 on error and 'errno' |
221 | * is set to the ERANGE or EAGAIN code. Note that 'errno' is not a |
222 | * thread-safe variable. |
223 | */ |
224 | |
225 | if(!h) /* failure */ |
226 | #elif defined(HAVE_GETHOSTBYNAME_R_3) |
227 | /* AIX, Digital Unix/Tru64, HPUX 10, more? */ |
228 | |
229 | /* For AIX 4.3 or later, we don't use gethostbyname_r() at all, because of |
230 | * the plain fact that it does not return unique full buffers on each |
231 | * call, but instead several of the pointers in the hostent structs will |
232 | * point to the same actual data! This have the unfortunate down-side that |
233 | * our caching system breaks down horribly. Luckily for us though, AIX 4.3 |
234 | * and more recent versions have a "completely thread-safe"[*] libc where |
235 | * all the data is stored in thread-specific memory areas making calls to |
236 | * the plain old gethostbyname() work fine even for multi-threaded |
237 | * programs. |
238 | * |
239 | * This AIX 4.3 or later detection is all made in the configure script. |
240 | * |
241 | * Troels Walsted Hansen helped us work this out on March 3rd, 2003. |
242 | * |
243 | * [*] = much later we've found out that it isn't at all "completely |
244 | * thread-safe", but at least the gethostbyname() function is. |
245 | */ |
246 | |
247 | if(CURL_HOSTENT_SIZE >= |
248 | (sizeof(struct hostent) + sizeof(struct hostent_data))) { |
249 | |
250 | /* August 22nd, 2000: Albert Chin-A-Young brought an updated version |
251 | * that should work! September 20: Richard Prescott worked on the buffer |
252 | * size dilemma. |
253 | */ |
254 | |
255 | res = gethostbyname_r(hostname, |
256 | (struct hostent *)buf, |
257 | (struct hostent_data *)((char *)buf + |
258 | sizeof(struct hostent))); |
259 | h_errnop = SOCKERRNO; /* we don't deal with this, but set it anyway */ |
260 | } |
261 | else |
262 | res = -1; /* failure, too smallish buffer size */ |
263 | |
264 | if(!res) { /* success */ |
265 | |
266 | h = buf; /* result expected in h */ |
267 | |
268 | /* This is the worst kind of the different gethostbyname_r() interfaces. |
269 | * Since we don't know how big buffer this particular lookup required, |
270 | * we can't realloc down the huge alloc without doing closer analysis of |
271 | * the returned data. Thus, we always use CURL_HOSTENT_SIZE for every |
272 | * name lookup. Fixing this would require an extra malloc() and then |
273 | * calling Curl_addrinfo_copy() that subsequent realloc()s down the new |
274 | * memory area to the actually used amount. |
275 | */ |
276 | } |
277 | else |
278 | #endif /* HAVE_...BYNAME_R_5 || HAVE_...BYNAME_R_6 || HAVE_...BYNAME_R_3 */ |
279 | { |
280 | h = NULL; /* set return code to NULL */ |
281 | free(buf); |
282 | } |
283 | #else /* HAVE_GETADDRINFO_THREADSAFE || HAVE_GETHOSTBYNAME_R */ |
284 | /* |
285 | * Here is code for platforms that don't have a thread safe |
286 | * getaddrinfo() nor gethostbyname_r() function or for which |
287 | * gethostbyname() is the preferred one. |
288 | */ |
289 | h = gethostbyname((void *)hostname); |
290 | #endif /* HAVE_GETADDRINFO_THREADSAFE || HAVE_GETHOSTBYNAME_R */ |
291 | |
292 | if(h) { |
293 | ai = Curl_he2ai(h, port); |
294 | |
295 | if(buf) /* used a *_r() function */ |
296 | free(buf); |
297 | } |
298 | |
299 | return ai; |
300 | } |
301 | #endif /* defined(CURLRES_IPV4) && !defined(CURLRES_ARES) && |
302 | !defined(CURLRES_AMIGA) */ |
303 | |