/proc/self/cwd/external/curl/lib/hostip.c
Line | Count | Source (jump to first uncovered line) |
1 | | /*************************************************************************** |
2 | | * _ _ ____ _ |
3 | | * Project ___| | | | _ \| | |
4 | | * / __| | | | |_) | | |
5 | | * | (__| |_| | _ <| |___ |
6 | | * \___|\___/|_| \_\_____| |
7 | | * |
8 | | * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, 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 | | #ifdef HAVE_NETINET_IN_H |
28 | | #include <netinet/in.h> |
29 | | #endif |
30 | | #ifdef HAVE_NETINET_IN6_H |
31 | | #include <netinet/in6.h> |
32 | | #endif |
33 | | #ifdef HAVE_NETDB_H |
34 | | #include <netdb.h> |
35 | | #endif |
36 | | #ifdef HAVE_ARPA_INET_H |
37 | | #include <arpa/inet.h> |
38 | | #endif |
39 | | #ifdef __VMS |
40 | | #include <in.h> |
41 | | #include <inet.h> |
42 | | #endif |
43 | | |
44 | | #include <setjmp.h> |
45 | | #include <signal.h> |
46 | | |
47 | | #include "urldata.h" |
48 | | #include "sendf.h" |
49 | | #include "hostip.h" |
50 | | #include "hash.h" |
51 | | #include "rand.h" |
52 | | #include "share.h" |
53 | | #include "url.h" |
54 | | #include "inet_ntop.h" |
55 | | #include "inet_pton.h" |
56 | | #include "multiif.h" |
57 | | #include "doh.h" |
58 | | #include "warnless.h" |
59 | | #include "strcase.h" |
60 | | #include "easy_lock.h" |
61 | | /* The last 3 #include files should be in this order */ |
62 | | #include "curl_printf.h" |
63 | | #include "curl_memory.h" |
64 | | #include "memdebug.h" |
65 | | |
66 | | #if defined(CURLRES_SYNCH) && \ |
67 | | defined(HAVE_ALARM) && \ |
68 | | defined(SIGALRM) && \ |
69 | | defined(HAVE_SIGSETJMP) && \ |
70 | | defined(GLOBAL_INIT_IS_THREADSAFE) |
71 | | /* alarm-based timeouts can only be used with all the dependencies satisfied */ |
72 | | #define USE_ALARM_TIMEOUT |
73 | | #endif |
74 | | |
75 | | #define MAX_HOSTCACHE_LEN (255 + 7) /* max FQDN + colon + port number + zero */ |
76 | | |
77 | 0 | #define MAX_DNS_CACHE_SIZE 29999 |
78 | | |
79 | | /* |
80 | | * hostip.c explained |
81 | | * ================== |
82 | | * |
83 | | * The main COMPILE-TIME DEFINES to keep in mind when reading the host*.c |
84 | | * source file are these: |
85 | | * |
86 | | * CURLRES_IPV6 - this host has getaddrinfo() and family, and thus we use |
87 | | * that. The host may not be able to resolve IPv6, but we don't really have to |
88 | | * take that into account. Hosts that aren't IPv6-enabled have CURLRES_IPV4 |
89 | | * defined. |
90 | | * |
91 | | * CURLRES_ARES - is defined if libcurl is built to use c-ares for |
92 | | * asynchronous name resolves. This can be Windows or *nix. |
93 | | * |
94 | | * CURLRES_THREADED - is defined if libcurl is built to run under (native) |
95 | | * Windows, and then the name resolve will be done in a new thread, and the |
96 | | * supported API will be the same as for ares-builds. |
97 | | * |
98 | | * If any of the two previous are defined, CURLRES_ASYNCH is defined too. If |
99 | | * libcurl is not built to use an asynchronous resolver, CURLRES_SYNCH is |
100 | | * defined. |
101 | | * |
102 | | * The host*.c sources files are split up like this: |
103 | | * |
104 | | * hostip.c - method-independent resolver functions and utility functions |
105 | | * hostasyn.c - functions for asynchronous name resolves |
106 | | * hostsyn.c - functions for synchronous name resolves |
107 | | * hostip4.c - IPv4 specific functions |
108 | | * hostip6.c - IPv6 specific functions |
109 | | * |
110 | | * The two asynchronous name resolver backends are implemented in: |
111 | | * asyn-ares.c - functions for ares-using name resolves |
112 | | * asyn-thread.c - functions for threaded name resolves |
113 | | |
114 | | * The hostip.h is the united header file for all this. It defines the |
115 | | * CURLRES_* defines based on the config*.h and curl_setup.h defines. |
116 | | */ |
117 | | |
118 | | static void freednsentry(void *freethis); |
119 | | |
120 | | /* |
121 | | * Curl_printable_address() stores a printable version of the 1st address |
122 | | * given in the 'ai' argument. The result will be stored in the buf that is |
123 | | * bufsize bytes big. |
124 | | * |
125 | | * If the conversion fails, the target buffer is empty. |
126 | | */ |
127 | | void Curl_printable_address(const struct Curl_addrinfo *ai, char *buf, |
128 | | size_t bufsize) |
129 | 0 | { |
130 | 0 | DEBUGASSERT(bufsize); |
131 | 0 | buf[0] = 0; |
132 | |
|
133 | 0 | switch(ai->ai_family) { |
134 | 0 | case AF_INET: { |
135 | 0 | const struct sockaddr_in *sa4 = (const void *)ai->ai_addr; |
136 | 0 | const struct in_addr *ipaddr4 = &sa4->sin_addr; |
137 | 0 | (void)Curl_inet_ntop(ai->ai_family, (const void *)ipaddr4, buf, bufsize); |
138 | 0 | break; |
139 | 0 | } |
140 | 0 | #ifdef ENABLE_IPV6 |
141 | 0 | case AF_INET6: { |
142 | 0 | const struct sockaddr_in6 *sa6 = (const void *)ai->ai_addr; |
143 | 0 | const struct in6_addr *ipaddr6 = &sa6->sin6_addr; |
144 | 0 | (void)Curl_inet_ntop(ai->ai_family, (const void *)ipaddr6, buf, bufsize); |
145 | 0 | break; |
146 | 0 | } |
147 | 0 | #endif |
148 | 0 | default: |
149 | 0 | break; |
150 | 0 | } |
151 | 0 | } |
152 | | |
153 | | /* |
154 | | * Create a hostcache id string for the provided host + port, to be used by |
155 | | * the DNS caching. Without alloc. Return length of the id string. |
156 | | */ |
157 | | static size_t |
158 | | create_hostcache_id(const char *name, |
159 | | size_t nlen, /* 0 or actual name length */ |
160 | | int port, char *ptr, size_t buflen) |
161 | 0 | { |
162 | 0 | size_t len = nlen ? nlen : strlen(name); |
163 | 0 | size_t olen = 0; |
164 | 0 | DEBUGASSERT(buflen >= MAX_HOSTCACHE_LEN); |
165 | 0 | if(len > (buflen - 7)) |
166 | 0 | len = buflen - 7; |
167 | | /* store and lower case the name */ |
168 | 0 | while(len--) { |
169 | 0 | *ptr++ = Curl_raw_tolower(*name++); |
170 | 0 | olen++; |
171 | 0 | } |
172 | 0 | olen += msnprintf(ptr, 7, ":%u", port); |
173 | 0 | return olen; |
174 | 0 | } |
175 | | |
176 | | struct hostcache_prune_data { |
177 | | time_t now; |
178 | | time_t oldest; /* oldest time in cache not pruned. */ |
179 | | int cache_timeout; |
180 | | }; |
181 | | |
182 | | /* |
183 | | * This function is set as a callback to be called for every entry in the DNS |
184 | | * cache when we want to prune old unused entries. |
185 | | * |
186 | | * Returning non-zero means remove the entry, return 0 to keep it in the |
187 | | * cache. |
188 | | */ |
189 | | static int |
190 | | hostcache_timestamp_remove(void *datap, void *hc) |
191 | 0 | { |
192 | 0 | struct hostcache_prune_data *prune = |
193 | 0 | (struct hostcache_prune_data *) datap; |
194 | 0 | struct Curl_dns_entry *c = (struct Curl_dns_entry *) hc; |
195 | |
|
196 | 0 | if(c->timestamp) { |
197 | | /* age in seconds */ |
198 | 0 | time_t age = prune->now - c->timestamp; |
199 | 0 | if(age >= prune->cache_timeout) |
200 | 0 | return TRUE; |
201 | 0 | if(age > prune->oldest) |
202 | 0 | prune->oldest = age; |
203 | 0 | } |
204 | 0 | return FALSE; |
205 | 0 | } |
206 | | |
207 | | /* |
208 | | * Prune the DNS cache. This assumes that a lock has already been taken. |
209 | | * Returns the 'age' of the oldest still kept entry. |
210 | | */ |
211 | | static time_t |
212 | | hostcache_prune(struct Curl_hash *hostcache, int cache_timeout, |
213 | | time_t now) |
214 | 0 | { |
215 | 0 | struct hostcache_prune_data user; |
216 | |
|
217 | 0 | user.cache_timeout = cache_timeout; |
218 | 0 | user.now = now; |
219 | 0 | user.oldest = 0; |
220 | |
|
221 | 0 | Curl_hash_clean_with_criterium(hostcache, |
222 | 0 | (void *) &user, |
223 | 0 | hostcache_timestamp_remove); |
224 | |
|
225 | 0 | return user.oldest; |
226 | 0 | } |
227 | | |
228 | | /* |
229 | | * Library-wide function for pruning the DNS cache. This function takes and |
230 | | * returns the appropriate locks. |
231 | | */ |
232 | | void Curl_hostcache_prune(struct Curl_easy *data) |
233 | 0 | { |
234 | 0 | time_t now; |
235 | | /* the timeout may be set -1 (forever) */ |
236 | 0 | int timeout = data->set.dns_cache_timeout; |
237 | |
|
238 | 0 | if(!data->dns.hostcache) |
239 | | /* NULL hostcache means we can't do it */ |
240 | 0 | return; |
241 | | |
242 | 0 | if(data->share) |
243 | 0 | Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE); |
244 | |
|
245 | 0 | time(&now); |
246 | |
|
247 | 0 | do { |
248 | | /* Remove outdated and unused entries from the hostcache */ |
249 | 0 | time_t oldest = hostcache_prune(data->dns.hostcache, timeout, now); |
250 | |
|
251 | 0 | if(oldest < INT_MAX) |
252 | 0 | timeout = (int)oldest; /* we know it fits */ |
253 | 0 | else |
254 | 0 | timeout = INT_MAX - 1; |
255 | | |
256 | | /* if the cache size is still too big, use the oldest age as new |
257 | | prune limit */ |
258 | 0 | } while(timeout && (data->dns.hostcache->size > MAX_DNS_CACHE_SIZE)); |
259 | |
|
260 | 0 | if(data->share) |
261 | 0 | Curl_share_unlock(data, CURL_LOCK_DATA_DNS); |
262 | 0 | } |
263 | | |
264 | | #ifdef USE_ALARM_TIMEOUT |
265 | | /* Beware this is a global and unique instance. This is used to store the |
266 | | return address that we can jump back to from inside a signal handler. This |
267 | | is not thread-safe stuff. */ |
268 | | static sigjmp_buf curl_jmpenv; |
269 | | static curl_simple_lock curl_jmpenv_lock; |
270 | | #endif |
271 | | |
272 | | /* lookup address, returns entry if found and not stale */ |
273 | | static struct Curl_dns_entry *fetch_addr(struct Curl_easy *data, |
274 | | const char *hostname, |
275 | | int port) |
276 | 0 | { |
277 | 0 | struct Curl_dns_entry *dns = NULL; |
278 | 0 | char entry_id[MAX_HOSTCACHE_LEN]; |
279 | | |
280 | | /* Create an entry id, based upon the hostname and port */ |
281 | 0 | size_t entry_len = create_hostcache_id(hostname, 0, port, |
282 | 0 | entry_id, sizeof(entry_id)); |
283 | | |
284 | | /* See if its already in our dns cache */ |
285 | 0 | dns = Curl_hash_pick(data->dns.hostcache, entry_id, entry_len + 1); |
286 | | |
287 | | /* No entry found in cache, check if we might have a wildcard entry */ |
288 | 0 | if(!dns && data->state.wildcard_resolve) { |
289 | 0 | entry_len = create_hostcache_id("*", 1, port, entry_id, sizeof(entry_id)); |
290 | | |
291 | | /* See if it's already in our dns cache */ |
292 | 0 | dns = Curl_hash_pick(data->dns.hostcache, entry_id, entry_len + 1); |
293 | 0 | } |
294 | |
|
295 | 0 | if(dns && (data->set.dns_cache_timeout != -1)) { |
296 | | /* See whether the returned entry is stale. Done before we release lock */ |
297 | 0 | struct hostcache_prune_data user; |
298 | |
|
299 | 0 | time(&user.now); |
300 | 0 | user.cache_timeout = data->set.dns_cache_timeout; |
301 | 0 | user.oldest = 0; |
302 | |
|
303 | 0 | if(hostcache_timestamp_remove(&user, dns)) { |
304 | 0 | infof(data, "Hostname in DNS cache was stale, zapped"); |
305 | 0 | dns = NULL; /* the memory deallocation is being handled by the hash */ |
306 | 0 | Curl_hash_delete(data->dns.hostcache, entry_id, entry_len + 1); |
307 | 0 | } |
308 | 0 | } |
309 | | |
310 | | /* See if the returned entry matches the required resolve mode */ |
311 | 0 | if(dns && data->conn->ip_version != CURL_IPRESOLVE_WHATEVER) { |
312 | 0 | int pf = PF_INET; |
313 | 0 | bool found = false; |
314 | 0 | struct Curl_addrinfo *addr = dns->addr; |
315 | |
|
316 | 0 | #ifdef PF_INET6 |
317 | 0 | if(data->conn->ip_version == CURL_IPRESOLVE_V6) |
318 | 0 | pf = PF_INET6; |
319 | 0 | #endif |
320 | |
|
321 | 0 | while(addr) { |
322 | 0 | if(addr->ai_family == pf) { |
323 | 0 | found = true; |
324 | 0 | break; |
325 | 0 | } |
326 | 0 | addr = addr->ai_next; |
327 | 0 | } |
328 | |
|
329 | 0 | if(!found) { |
330 | 0 | infof(data, "Hostname in DNS cache doesn't have needed family, zapped"); |
331 | 0 | dns = NULL; /* the memory deallocation is being handled by the hash */ |
332 | 0 | Curl_hash_delete(data->dns.hostcache, entry_id, entry_len + 1); |
333 | 0 | } |
334 | 0 | } |
335 | 0 | return dns; |
336 | 0 | } |
337 | | |
338 | | /* |
339 | | * Curl_fetch_addr() fetches a 'Curl_dns_entry' already in the DNS cache. |
340 | | * |
341 | | * Curl_resolv() checks initially and multi_runsingle() checks each time |
342 | | * it discovers the handle in the state WAITRESOLVE whether the hostname |
343 | | * has already been resolved and the address has already been stored in |
344 | | * the DNS cache. This short circuits waiting for a lot of pending |
345 | | * lookups for the same hostname requested by different handles. |
346 | | * |
347 | | * Returns the Curl_dns_entry entry pointer or NULL if not in the cache. |
348 | | * |
349 | | * The returned data *MUST* be "unlocked" with Curl_resolv_unlock() after |
350 | | * use, or we'll leak memory! |
351 | | */ |
352 | | struct Curl_dns_entry * |
353 | | Curl_fetch_addr(struct Curl_easy *data, |
354 | | const char *hostname, |
355 | | int port) |
356 | 0 | { |
357 | 0 | struct Curl_dns_entry *dns = NULL; |
358 | |
|
359 | 0 | if(data->share) |
360 | 0 | Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE); |
361 | |
|
362 | 0 | dns = fetch_addr(data, hostname, port); |
363 | |
|
364 | 0 | if(dns) |
365 | 0 | dns->inuse++; /* we use it! */ |
366 | |
|
367 | 0 | if(data->share) |
368 | 0 | Curl_share_unlock(data, CURL_LOCK_DATA_DNS); |
369 | |
|
370 | 0 | return dns; |
371 | 0 | } |
372 | | |
373 | | #ifndef CURL_DISABLE_SHUFFLE_DNS |
374 | | /* |
375 | | * Return # of addresses in a Curl_addrinfo struct |
376 | | */ |
377 | | static int num_addresses(const struct Curl_addrinfo *addr) |
378 | 0 | { |
379 | 0 | int i = 0; |
380 | 0 | while(addr) { |
381 | 0 | addr = addr->ai_next; |
382 | 0 | i++; |
383 | 0 | } |
384 | 0 | return i; |
385 | 0 | } |
386 | | |
387 | | UNITTEST CURLcode Curl_shuffle_addr(struct Curl_easy *data, |
388 | | struct Curl_addrinfo **addr); |
389 | | /* |
390 | | * Curl_shuffle_addr() shuffles the order of addresses in a 'Curl_addrinfo' |
391 | | * struct by re-linking its linked list. |
392 | | * |
393 | | * The addr argument should be the address of a pointer to the head node of a |
394 | | * `Curl_addrinfo` list and it will be modified to point to the new head after |
395 | | * shuffling. |
396 | | * |
397 | | * Not declared static only to make it easy to use in a unit test! |
398 | | * |
399 | | * @unittest: 1608 |
400 | | */ |
401 | | UNITTEST CURLcode Curl_shuffle_addr(struct Curl_easy *data, |
402 | | struct Curl_addrinfo **addr) |
403 | 0 | { |
404 | 0 | CURLcode result = CURLE_OK; |
405 | 0 | const int num_addrs = num_addresses(*addr); |
406 | |
|
407 | 0 | if(num_addrs > 1) { |
408 | 0 | struct Curl_addrinfo **nodes; |
409 | 0 | infof(data, "Shuffling %i addresses", num_addrs); |
410 | |
|
411 | 0 | nodes = malloc(num_addrs*sizeof(*nodes)); |
412 | 0 | if(nodes) { |
413 | 0 | int i; |
414 | 0 | unsigned int *rnd; |
415 | 0 | const size_t rnd_size = num_addrs * sizeof(*rnd); |
416 | | |
417 | | /* build a plain array of Curl_addrinfo pointers */ |
418 | 0 | nodes[0] = *addr; |
419 | 0 | for(i = 1; i < num_addrs; i++) { |
420 | 0 | nodes[i] = nodes[i-1]->ai_next; |
421 | 0 | } |
422 | |
|
423 | 0 | rnd = malloc(rnd_size); |
424 | 0 | if(rnd) { |
425 | | /* Fisher-Yates shuffle */ |
426 | 0 | if(Curl_rand(data, (unsigned char *)rnd, rnd_size) == CURLE_OK) { |
427 | 0 | struct Curl_addrinfo *swap_tmp; |
428 | 0 | for(i = num_addrs - 1; i > 0; i--) { |
429 | 0 | swap_tmp = nodes[rnd[i] % (i + 1)]; |
430 | 0 | nodes[rnd[i] % (i + 1)] = nodes[i]; |
431 | 0 | nodes[i] = swap_tmp; |
432 | 0 | } |
433 | | |
434 | | /* relink list in the new order */ |
435 | 0 | for(i = 1; i < num_addrs; i++) { |
436 | 0 | nodes[i-1]->ai_next = nodes[i]; |
437 | 0 | } |
438 | |
|
439 | 0 | nodes[num_addrs-1]->ai_next = NULL; |
440 | 0 | *addr = nodes[0]; |
441 | 0 | } |
442 | 0 | free(rnd); |
443 | 0 | } |
444 | 0 | else |
445 | 0 | result = CURLE_OUT_OF_MEMORY; |
446 | 0 | free(nodes); |
447 | 0 | } |
448 | 0 | else |
449 | 0 | result = CURLE_OUT_OF_MEMORY; |
450 | 0 | } |
451 | 0 | return result; |
452 | 0 | } |
453 | | #endif |
454 | | |
455 | | /* |
456 | | * Curl_cache_addr() stores a 'Curl_addrinfo' struct in the DNS cache. |
457 | | * |
458 | | * When calling Curl_resolv() has resulted in a response with a returned |
459 | | * address, we call this function to store the information in the dns |
460 | | * cache etc |
461 | | * |
462 | | * Returns the Curl_dns_entry entry pointer or NULL if the storage failed. |
463 | | */ |
464 | | struct Curl_dns_entry * |
465 | | Curl_cache_addr(struct Curl_easy *data, |
466 | | struct Curl_addrinfo *addr, |
467 | | const char *hostname, |
468 | | size_t hostlen, /* length or zero */ |
469 | | int port) |
470 | 0 | { |
471 | 0 | char entry_id[MAX_HOSTCACHE_LEN]; |
472 | 0 | size_t entry_len; |
473 | 0 | struct Curl_dns_entry *dns; |
474 | 0 | struct Curl_dns_entry *dns2; |
475 | |
|
476 | 0 | #ifndef CURL_DISABLE_SHUFFLE_DNS |
477 | | /* shuffle addresses if requested */ |
478 | 0 | if(data->set.dns_shuffle_addresses) { |
479 | 0 | CURLcode result = Curl_shuffle_addr(data, &addr); |
480 | 0 | if(result) |
481 | 0 | return NULL; |
482 | 0 | } |
483 | 0 | #endif |
484 | | |
485 | | /* Create a new cache entry */ |
486 | 0 | dns = calloc(1, sizeof(struct Curl_dns_entry)); |
487 | 0 | if(!dns) { |
488 | 0 | return NULL; |
489 | 0 | } |
490 | | |
491 | | /* Create an entry id, based upon the hostname and port */ |
492 | 0 | entry_len = create_hostcache_id(hostname, hostlen, port, |
493 | 0 | entry_id, sizeof(entry_id)); |
494 | |
|
495 | 0 | dns->inuse = 1; /* the cache has the first reference */ |
496 | 0 | dns->addr = addr; /* this is the address(es) */ |
497 | 0 | time(&dns->timestamp); |
498 | 0 | if(dns->timestamp == 0) |
499 | 0 | dns->timestamp = 1; /* zero indicates permanent CURLOPT_RESOLVE entry */ |
500 | | |
501 | | /* Store the resolved data in our DNS cache. */ |
502 | 0 | dns2 = Curl_hash_add(data->dns.hostcache, entry_id, entry_len + 1, |
503 | 0 | (void *)dns); |
504 | 0 | if(!dns2) { |
505 | 0 | free(dns); |
506 | 0 | return NULL; |
507 | 0 | } |
508 | | |
509 | 0 | dns = dns2; |
510 | 0 | dns->inuse++; /* mark entry as in-use */ |
511 | 0 | return dns; |
512 | 0 | } |
513 | | |
514 | | #ifdef ENABLE_IPV6 |
515 | | /* return a static IPv6 ::1 for the name */ |
516 | | static struct Curl_addrinfo *get_localhost6(int port, const char *name) |
517 | 0 | { |
518 | 0 | struct Curl_addrinfo *ca; |
519 | 0 | const size_t ss_size = sizeof(struct sockaddr_in6); |
520 | 0 | const size_t hostlen = strlen(name); |
521 | 0 | struct sockaddr_in6 sa6; |
522 | 0 | unsigned char ipv6[16]; |
523 | 0 | unsigned short port16 = (unsigned short)(port & 0xffff); |
524 | 0 | ca = calloc(sizeof(struct Curl_addrinfo) + ss_size + hostlen + 1, 1); |
525 | 0 | if(!ca) |
526 | 0 | return NULL; |
527 | | |
528 | 0 | sa6.sin6_family = AF_INET6; |
529 | 0 | sa6.sin6_port = htons(port16); |
530 | 0 | sa6.sin6_flowinfo = 0; |
531 | 0 | sa6.sin6_scope_id = 0; |
532 | 0 | if(Curl_inet_pton(AF_INET6, "::1", ipv6) < 1) |
533 | 0 | return NULL; |
534 | 0 | memcpy(&sa6.sin6_addr, ipv6, sizeof(ipv6)); |
535 | |
|
536 | 0 | ca->ai_flags = 0; |
537 | 0 | ca->ai_family = AF_INET6; |
538 | 0 | ca->ai_socktype = SOCK_STREAM; |
539 | 0 | ca->ai_protocol = IPPROTO_TCP; |
540 | 0 | ca->ai_addrlen = (curl_socklen_t)ss_size; |
541 | 0 | ca->ai_next = NULL; |
542 | 0 | ca->ai_addr = (void *)((char *)ca + sizeof(struct Curl_addrinfo)); |
543 | 0 | memcpy(ca->ai_addr, &sa6, ss_size); |
544 | 0 | ca->ai_canonname = (char *)ca->ai_addr + ss_size; |
545 | 0 | strcpy(ca->ai_canonname, name); |
546 | 0 | return ca; |
547 | 0 | } |
548 | | #else |
549 | | #define get_localhost6(x,y) NULL |
550 | | #endif |
551 | | |
552 | | /* return a static IPv4 127.0.0.1 for the given name */ |
553 | | static struct Curl_addrinfo *get_localhost(int port, const char *name) |
554 | 0 | { |
555 | 0 | struct Curl_addrinfo *ca; |
556 | 0 | struct Curl_addrinfo *ca6; |
557 | 0 | const size_t ss_size = sizeof(struct sockaddr_in); |
558 | 0 | const size_t hostlen = strlen(name); |
559 | 0 | struct sockaddr_in sa; |
560 | 0 | unsigned int ipv4; |
561 | 0 | unsigned short port16 = (unsigned short)(port & 0xffff); |
562 | | |
563 | | /* memset to clear the sa.sin_zero field */ |
564 | 0 | memset(&sa, 0, sizeof(sa)); |
565 | 0 | sa.sin_family = AF_INET; |
566 | 0 | sa.sin_port = htons(port16); |
567 | 0 | if(Curl_inet_pton(AF_INET, "127.0.0.1", (char *)&ipv4) < 1) |
568 | 0 | return NULL; |
569 | 0 | memcpy(&sa.sin_addr, &ipv4, sizeof(ipv4)); |
570 | |
|
571 | 0 | ca = calloc(sizeof(struct Curl_addrinfo) + ss_size + hostlen + 1, 1); |
572 | 0 | if(!ca) |
573 | 0 | return NULL; |
574 | 0 | ca->ai_flags = 0; |
575 | 0 | ca->ai_family = AF_INET; |
576 | 0 | ca->ai_socktype = SOCK_STREAM; |
577 | 0 | ca->ai_protocol = IPPROTO_TCP; |
578 | 0 | ca->ai_addrlen = (curl_socklen_t)ss_size; |
579 | 0 | ca->ai_addr = (void *)((char *)ca + sizeof(struct Curl_addrinfo)); |
580 | 0 | memcpy(ca->ai_addr, &sa, ss_size); |
581 | 0 | ca->ai_canonname = (char *)ca->ai_addr + ss_size; |
582 | 0 | strcpy(ca->ai_canonname, name); |
583 | |
|
584 | 0 | ca6 = get_localhost6(port, name); |
585 | 0 | if(!ca6) |
586 | 0 | return ca; |
587 | 0 | ca6->ai_next = ca; |
588 | 0 | return ca6; |
589 | 0 | } |
590 | | |
591 | | #ifdef ENABLE_IPV6 |
592 | | /* |
593 | | * Curl_ipv6works() returns TRUE if IPv6 seems to work. |
594 | | */ |
595 | | bool Curl_ipv6works(struct Curl_easy *data) |
596 | 0 | { |
597 | 0 | if(data) { |
598 | | /* the nature of most system is that IPv6 status doesn't come and go |
599 | | during a program's lifetime so we only probe the first time and then we |
600 | | have the info kept for fast reuse */ |
601 | 0 | DEBUGASSERT(data); |
602 | 0 | DEBUGASSERT(data->multi); |
603 | 0 | if(data->multi->ipv6_up == IPV6_UNKNOWN) { |
604 | 0 | bool works = Curl_ipv6works(NULL); |
605 | 0 | data->multi->ipv6_up = works ? IPV6_WORKS : IPV6_DEAD; |
606 | 0 | } |
607 | 0 | return data->multi->ipv6_up == IPV6_WORKS; |
608 | 0 | } |
609 | 0 | else { |
610 | 0 | int ipv6_works = -1; |
611 | | /* probe to see if we have a working IPv6 stack */ |
612 | 0 | curl_socket_t s = socket(PF_INET6, SOCK_DGRAM, 0); |
613 | 0 | if(s == CURL_SOCKET_BAD) |
614 | | /* an IPv6 address was requested but we can't get/use one */ |
615 | 0 | ipv6_works = 0; |
616 | 0 | else { |
617 | 0 | ipv6_works = 1; |
618 | 0 | sclose(s); |
619 | 0 | } |
620 | 0 | return (ipv6_works>0)?TRUE:FALSE; |
621 | 0 | } |
622 | 0 | } |
623 | | #endif /* ENABLE_IPV6 */ |
624 | | |
625 | | /* |
626 | | * Curl_host_is_ipnum() returns TRUE if the given string is a numerical IPv4 |
627 | | * (or IPv6 if supported) address. |
628 | | */ |
629 | | bool Curl_host_is_ipnum(const char *hostname) |
630 | 0 | { |
631 | 0 | struct in_addr in; |
632 | 0 | #ifdef ENABLE_IPV6 |
633 | 0 | struct in6_addr in6; |
634 | 0 | #endif |
635 | 0 | if(Curl_inet_pton(AF_INET, hostname, &in) > 0 |
636 | 0 | #ifdef ENABLE_IPV6 |
637 | 0 | || Curl_inet_pton(AF_INET6, hostname, &in6) > 0 |
638 | 0 | #endif |
639 | 0 | ) |
640 | 0 | return TRUE; |
641 | 0 | return FALSE; |
642 | 0 | } |
643 | | |
644 | | |
645 | | /* return TRUE if 'part' is a case insensitive tail of 'full' */ |
646 | | static bool tailmatch(const char *full, const char *part) |
647 | 0 | { |
648 | 0 | size_t plen = strlen(part); |
649 | 0 | size_t flen = strlen(full); |
650 | 0 | if(plen > flen) |
651 | 0 | return FALSE; |
652 | 0 | return strncasecompare(part, &full[flen - plen], plen); |
653 | 0 | } |
654 | | |
655 | | /* |
656 | | * Curl_resolv() is the main name resolve function within libcurl. It resolves |
657 | | * a name and returns a pointer to the entry in the 'entry' argument (if one |
658 | | * is provided). This function might return immediately if we're using asynch |
659 | | * resolves. See the return codes. |
660 | | * |
661 | | * The cache entry we return will get its 'inuse' counter increased when this |
662 | | * function is used. You MUST call Curl_resolv_unlock() later (when you're |
663 | | * done using this struct) to decrease the counter again. |
664 | | * |
665 | | * Return codes: |
666 | | * |
667 | | * CURLRESOLV_ERROR (-1) = error, no pointer |
668 | | * CURLRESOLV_RESOLVED (0) = OK, pointer provided |
669 | | * CURLRESOLV_PENDING (1) = waiting for response, no pointer |
670 | | */ |
671 | | |
672 | | enum resolve_t Curl_resolv(struct Curl_easy *data, |
673 | | const char *hostname, |
674 | | int port, |
675 | | bool allowDOH, |
676 | | struct Curl_dns_entry **entry) |
677 | 0 | { |
678 | 0 | struct Curl_dns_entry *dns = NULL; |
679 | 0 | CURLcode result; |
680 | 0 | enum resolve_t rc = CURLRESOLV_ERROR; /* default to failure */ |
681 | 0 | struct connectdata *conn = data->conn; |
682 | | /* We should intentionally error and not resolve .onion TLDs */ |
683 | 0 | size_t hostname_len = strlen(hostname); |
684 | 0 | if(hostname_len >= 7 && |
685 | 0 | (curl_strequal(&hostname[hostname_len - 6], ".onion") || |
686 | 0 | curl_strequal(&hostname[hostname_len - 7], ".onion."))) { |
687 | 0 | failf(data, "Not resolving .onion address (RFC 7686)"); |
688 | 0 | return CURLRESOLV_ERROR; |
689 | 0 | } |
690 | 0 | *entry = NULL; |
691 | 0 | #ifndef CURL_DISABLE_DOH |
692 | 0 | conn->bits.doh = FALSE; /* default is not */ |
693 | | #else |
694 | | (void)allowDOH; |
695 | | #endif |
696 | |
|
697 | 0 | if(data->share) |
698 | 0 | Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE); |
699 | |
|
700 | 0 | dns = fetch_addr(data, hostname, port); |
701 | |
|
702 | 0 | if(dns) { |
703 | 0 | infof(data, "Hostname %s was found in DNS cache", hostname); |
704 | 0 | dns->inuse++; /* we use it! */ |
705 | 0 | rc = CURLRESOLV_RESOLVED; |
706 | 0 | } |
707 | |
|
708 | 0 | if(data->share) |
709 | 0 | Curl_share_unlock(data, CURL_LOCK_DATA_DNS); |
710 | |
|
711 | 0 | if(!dns) { |
712 | | /* The entry was not in the cache. Resolve it to IP address */ |
713 | |
|
714 | 0 | struct Curl_addrinfo *addr = NULL; |
715 | 0 | int respwait = 0; |
716 | 0 | #if !defined(CURL_DISABLE_DOH) || !defined(USE_RESOLVE_ON_IPS) |
717 | 0 | struct in_addr in; |
718 | 0 | #endif |
719 | 0 | #ifndef CURL_DISABLE_DOH |
720 | 0 | #ifndef USE_RESOLVE_ON_IPS |
721 | 0 | const |
722 | 0 | #endif |
723 | 0 | bool ipnum = FALSE; |
724 | 0 | #endif |
725 | | |
726 | | /* notify the resolver start callback */ |
727 | 0 | if(data->set.resolver_start) { |
728 | 0 | int st; |
729 | 0 | Curl_set_in_callback(data, true); |
730 | 0 | st = data->set.resolver_start( |
731 | 0 | #ifdef USE_CURL_ASYNC |
732 | 0 | data->state.async.resolver, |
733 | | #else |
734 | | NULL, |
735 | | #endif |
736 | 0 | NULL, |
737 | 0 | data->set.resolver_start_client); |
738 | 0 | Curl_set_in_callback(data, false); |
739 | 0 | if(st) |
740 | 0 | return CURLRESOLV_ERROR; |
741 | 0 | } |
742 | | |
743 | 0 | #ifndef USE_RESOLVE_ON_IPS |
744 | | /* First check if this is an IPv4 address string */ |
745 | 0 | if(Curl_inet_pton(AF_INET, hostname, &in) > 0) |
746 | | /* This is a dotted IP address 123.123.123.123-style */ |
747 | 0 | addr = Curl_ip2addr(AF_INET, &in, hostname, port); |
748 | 0 | #ifdef ENABLE_IPV6 |
749 | 0 | if(!addr) { |
750 | 0 | struct in6_addr in6; |
751 | | /* check if this is an IPv6 address string */ |
752 | 0 | if(Curl_inet_pton(AF_INET6, hostname, &in6) > 0) |
753 | | /* This is an IPv6 address literal */ |
754 | 0 | addr = Curl_ip2addr(AF_INET6, &in6, hostname, port); |
755 | 0 | } |
756 | 0 | #endif /* ENABLE_IPV6 */ |
757 | |
|
758 | | #else /* if USE_RESOLVE_ON_IPS */ |
759 | | #ifndef CURL_DISABLE_DOH |
760 | | /* First check if this is an IPv4 address string */ |
761 | | if(Curl_inet_pton(AF_INET, hostname, &in) > 0) |
762 | | /* This is a dotted IP address 123.123.123.123-style */ |
763 | | ipnum = TRUE; |
764 | | #ifdef ENABLE_IPV6 |
765 | | else { |
766 | | struct in6_addr in6; |
767 | | /* check if this is an IPv6 address string */ |
768 | | if(Curl_inet_pton(AF_INET6, hostname, &in6) > 0) |
769 | | /* This is an IPv6 address literal */ |
770 | | ipnum = TRUE; |
771 | | } |
772 | | #endif /* ENABLE_IPV6 */ |
773 | | #endif /* CURL_DISABLE_DOH */ |
774 | | |
775 | | #endif /* !USE_RESOLVE_ON_IPS */ |
776 | |
|
777 | 0 | if(!addr) { |
778 | 0 | if(conn->ip_version == CURL_IPRESOLVE_V6 && !Curl_ipv6works(data)) |
779 | 0 | return CURLRESOLV_ERROR; |
780 | | |
781 | 0 | if(strcasecompare(hostname, "localhost") || |
782 | 0 | tailmatch(hostname, ".localhost")) |
783 | 0 | addr = get_localhost(port, hostname); |
784 | 0 | #ifndef CURL_DISABLE_DOH |
785 | 0 | else if(allowDOH && data->set.doh && !ipnum) |
786 | 0 | addr = Curl_doh(data, hostname, port, &respwait); |
787 | 0 | #endif |
788 | 0 | else { |
789 | | /* Check what IP specifics the app has requested and if we can provide |
790 | | * it. If not, bail out. */ |
791 | 0 | if(!Curl_ipvalid(data, conn)) |
792 | 0 | return CURLRESOLV_ERROR; |
793 | | /* If Curl_getaddrinfo() returns NULL, 'respwait' might be set to a |
794 | | non-zero value indicating that we need to wait for the response to |
795 | | the resolve call */ |
796 | 0 | addr = Curl_getaddrinfo(data, hostname, port, &respwait); |
797 | 0 | } |
798 | 0 | } |
799 | 0 | if(!addr) { |
800 | 0 | if(respwait) { |
801 | | /* the response to our resolve call will come asynchronously at |
802 | | a later time, good or bad */ |
803 | | /* First, check that we haven't received the info by now */ |
804 | 0 | result = Curl_resolv_check(data, &dns); |
805 | 0 | if(result) /* error detected */ |
806 | 0 | return CURLRESOLV_ERROR; |
807 | 0 | if(dns) |
808 | 0 | rc = CURLRESOLV_RESOLVED; /* pointer provided */ |
809 | 0 | else |
810 | 0 | rc = CURLRESOLV_PENDING; /* no info yet */ |
811 | 0 | } |
812 | 0 | } |
813 | 0 | else { |
814 | 0 | if(data->share) |
815 | 0 | Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE); |
816 | | |
817 | | /* we got a response, store it in the cache */ |
818 | 0 | dns = Curl_cache_addr(data, addr, hostname, 0, port); |
819 | |
|
820 | 0 | if(data->share) |
821 | 0 | Curl_share_unlock(data, CURL_LOCK_DATA_DNS); |
822 | |
|
823 | 0 | if(!dns) |
824 | | /* returned failure, bail out nicely */ |
825 | 0 | Curl_freeaddrinfo(addr); |
826 | 0 | else |
827 | 0 | rc = CURLRESOLV_RESOLVED; |
828 | 0 | } |
829 | 0 | } |
830 | | |
831 | 0 | *entry = dns; |
832 | |
|
833 | 0 | return rc; |
834 | 0 | } |
835 | | |
836 | | #ifdef USE_ALARM_TIMEOUT |
837 | | /* |
838 | | * This signal handler jumps back into the main libcurl code and continues |
839 | | * execution. This effectively causes the remainder of the application to run |
840 | | * within a signal handler which is nonportable and could lead to problems. |
841 | | */ |
842 | | static |
843 | | void alarmfunc(int sig) |
844 | | { |
845 | | (void)sig; |
846 | | siglongjmp(curl_jmpenv, 1); |
847 | | } |
848 | | #endif /* USE_ALARM_TIMEOUT */ |
849 | | |
850 | | /* |
851 | | * Curl_resolv_timeout() is the same as Curl_resolv() but specifies a |
852 | | * timeout. This function might return immediately if we're using asynch |
853 | | * resolves. See the return codes. |
854 | | * |
855 | | * The cache entry we return will get its 'inuse' counter increased when this |
856 | | * function is used. You MUST call Curl_resolv_unlock() later (when you're |
857 | | * done using this struct) to decrease the counter again. |
858 | | * |
859 | | * If built with a synchronous resolver and use of signals is not |
860 | | * disabled by the application, then a nonzero timeout will cause a |
861 | | * timeout after the specified number of milliseconds. Otherwise, timeout |
862 | | * is ignored. |
863 | | * |
864 | | * Return codes: |
865 | | * |
866 | | * CURLRESOLV_TIMEDOUT(-2) = warning, time too short or previous alarm expired |
867 | | * CURLRESOLV_ERROR (-1) = error, no pointer |
868 | | * CURLRESOLV_RESOLVED (0) = OK, pointer provided |
869 | | * CURLRESOLV_PENDING (1) = waiting for response, no pointer |
870 | | */ |
871 | | |
872 | | enum resolve_t Curl_resolv_timeout(struct Curl_easy *data, |
873 | | const char *hostname, |
874 | | int port, |
875 | | struct Curl_dns_entry **entry, |
876 | | timediff_t timeoutms) |
877 | 0 | { |
878 | | #ifdef USE_ALARM_TIMEOUT |
879 | | #ifdef HAVE_SIGACTION |
880 | | struct sigaction keep_sigact; /* store the old struct here */ |
881 | | volatile bool keep_copysig = FALSE; /* whether old sigact has been saved */ |
882 | | struct sigaction sigact; |
883 | | #else |
884 | | #ifdef HAVE_SIGNAL |
885 | | void (*keep_sigact)(int); /* store the old handler here */ |
886 | | #endif /* HAVE_SIGNAL */ |
887 | | #endif /* HAVE_SIGACTION */ |
888 | | volatile long timeout; |
889 | | volatile unsigned int prev_alarm = 0; |
890 | | #endif /* USE_ALARM_TIMEOUT */ |
891 | 0 | enum resolve_t rc; |
892 | |
|
893 | 0 | *entry = NULL; |
894 | |
|
895 | 0 | if(timeoutms < 0) |
896 | | /* got an already expired timeout */ |
897 | 0 | return CURLRESOLV_TIMEDOUT; |
898 | | |
899 | | #ifdef USE_ALARM_TIMEOUT |
900 | | if(data->set.no_signal) |
901 | | /* Ignore the timeout when signals are disabled */ |
902 | | timeout = 0; |
903 | | else |
904 | | timeout = (timeoutms > LONG_MAX) ? LONG_MAX : (long)timeoutms; |
905 | | |
906 | | if(!timeout) |
907 | | /* USE_ALARM_TIMEOUT defined, but no timeout actually requested */ |
908 | | return Curl_resolv(data, hostname, port, TRUE, entry); |
909 | | |
910 | | if(timeout < 1000) { |
911 | | /* The alarm() function only provides integer second resolution, so if |
912 | | we want to wait less than one second we must bail out already now. */ |
913 | | failf(data, |
914 | | "remaining timeout of %ld too small to resolve via SIGALRM method", |
915 | | timeout); |
916 | | return CURLRESOLV_TIMEDOUT; |
917 | | } |
918 | | /* This allows us to time-out from the name resolver, as the timeout |
919 | | will generate a signal and we will siglongjmp() from that here. |
920 | | This technique has problems (see alarmfunc). |
921 | | This should be the last thing we do before calling Curl_resolv(), |
922 | | as otherwise we'd have to worry about variables that get modified |
923 | | before we invoke Curl_resolv() (and thus use "volatile"). */ |
924 | | curl_simple_lock_lock(&curl_jmpenv_lock); |
925 | | |
926 | | if(sigsetjmp(curl_jmpenv, 1)) { |
927 | | /* this is coming from a siglongjmp() after an alarm signal */ |
928 | | failf(data, "name lookup timed out"); |
929 | | rc = CURLRESOLV_ERROR; |
930 | | goto clean_up; |
931 | | } |
932 | | else { |
933 | | /************************************************************* |
934 | | * Set signal handler to catch SIGALRM |
935 | | * Store the old value to be able to set it back later! |
936 | | *************************************************************/ |
937 | | #ifdef HAVE_SIGACTION |
938 | | sigaction(SIGALRM, NULL, &sigact); |
939 | | keep_sigact = sigact; |
940 | | keep_copysig = TRUE; /* yes, we have a copy */ |
941 | | sigact.sa_handler = alarmfunc; |
942 | | #ifdef SA_RESTART |
943 | | /* HPUX doesn't have SA_RESTART but defaults to that behavior! */ |
944 | | sigact.sa_flags &= ~SA_RESTART; |
945 | | #endif |
946 | | /* now set the new struct */ |
947 | | sigaction(SIGALRM, &sigact, NULL); |
948 | | #else /* HAVE_SIGACTION */ |
949 | | /* no sigaction(), revert to the much lamer signal() */ |
950 | | #ifdef HAVE_SIGNAL |
951 | | keep_sigact = signal(SIGALRM, alarmfunc); |
952 | | #endif |
953 | | #endif /* HAVE_SIGACTION */ |
954 | | |
955 | | /* alarm() makes a signal get sent when the timeout fires off, and that |
956 | | will abort system calls */ |
957 | | prev_alarm = alarm(curlx_sltoui(timeout/1000L)); |
958 | | } |
959 | | |
960 | | #else |
961 | 0 | #ifndef CURLRES_ASYNCH |
962 | 0 | if(timeoutms) |
963 | 0 | infof(data, "timeout on name lookup is not supported"); |
964 | | #else |
965 | | (void)timeoutms; /* timeoutms not used with an async resolver */ |
966 | | #endif |
967 | 0 | #endif /* USE_ALARM_TIMEOUT */ |
968 | | |
969 | | /* Perform the actual name resolution. This might be interrupted by an |
970 | | * alarm if it takes too long. |
971 | | */ |
972 | 0 | rc = Curl_resolv(data, hostname, port, TRUE, entry); |
973 | |
|
974 | | #ifdef USE_ALARM_TIMEOUT |
975 | | clean_up: |
976 | | |
977 | | if(!prev_alarm) |
978 | | /* deactivate a possibly active alarm before uninstalling the handler */ |
979 | | alarm(0); |
980 | | |
981 | | #ifdef HAVE_SIGACTION |
982 | | if(keep_copysig) { |
983 | | /* we got a struct as it looked before, now put that one back nice |
984 | | and clean */ |
985 | | sigaction(SIGALRM, &keep_sigact, NULL); /* put it back */ |
986 | | } |
987 | | #else |
988 | | #ifdef HAVE_SIGNAL |
989 | | /* restore the previous SIGALRM handler */ |
990 | | signal(SIGALRM, keep_sigact); |
991 | | #endif |
992 | | #endif /* HAVE_SIGACTION */ |
993 | | |
994 | | curl_simple_lock_unlock(&curl_jmpenv_lock); |
995 | | |
996 | | /* switch back the alarm() to either zero or to what it was before minus |
997 | | the time we spent until now! */ |
998 | | if(prev_alarm) { |
999 | | /* there was an alarm() set before us, now put it back */ |
1000 | | timediff_t elapsed_secs = Curl_timediff(Curl_now(), |
1001 | | data->conn->created) / 1000; |
1002 | | |
1003 | | /* the alarm period is counted in even number of seconds */ |
1004 | | unsigned long alarm_set = (unsigned long)(prev_alarm - elapsed_secs); |
1005 | | |
1006 | | if(!alarm_set || |
1007 | | ((alarm_set >= 0x80000000) && (prev_alarm < 0x80000000)) ) { |
1008 | | /* if the alarm time-left reached zero or turned "negative" (counted |
1009 | | with unsigned values), we should fire off a SIGALRM here, but we |
1010 | | won't, and zero would be to switch it off so we never set it to |
1011 | | less than 1! */ |
1012 | | alarm(1); |
1013 | | rc = CURLRESOLV_TIMEDOUT; |
1014 | | failf(data, "Previous alarm fired off"); |
1015 | | } |
1016 | | else |
1017 | | alarm((unsigned int)alarm_set); |
1018 | | } |
1019 | | #endif /* USE_ALARM_TIMEOUT */ |
1020 | |
|
1021 | 0 | return rc; |
1022 | 0 | } |
1023 | | |
1024 | | /* |
1025 | | * Curl_resolv_unlock() unlocks the given cached DNS entry. When this has been |
1026 | | * made, the struct may be destroyed due to pruning. It is important that only |
1027 | | * one unlock is made for each Curl_resolv() call. |
1028 | | * |
1029 | | * May be called with 'data' == NULL for global cache. |
1030 | | */ |
1031 | | void Curl_resolv_unlock(struct Curl_easy *data, struct Curl_dns_entry *dns) |
1032 | 0 | { |
1033 | 0 | if(data && data->share) |
1034 | 0 | Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE); |
1035 | |
|
1036 | 0 | freednsentry(dns); |
1037 | |
|
1038 | 0 | if(data && data->share) |
1039 | 0 | Curl_share_unlock(data, CURL_LOCK_DATA_DNS); |
1040 | 0 | } |
1041 | | |
1042 | | /* |
1043 | | * File-internal: release cache dns entry reference, free if inuse drops to 0 |
1044 | | */ |
1045 | | static void freednsentry(void *freethis) |
1046 | 0 | { |
1047 | 0 | struct Curl_dns_entry *dns = (struct Curl_dns_entry *) freethis; |
1048 | 0 | DEBUGASSERT(dns && (dns->inuse>0)); |
1049 | |
|
1050 | 0 | dns->inuse--; |
1051 | 0 | if(dns->inuse == 0) { |
1052 | 0 | Curl_freeaddrinfo(dns->addr); |
1053 | 0 | free(dns); |
1054 | 0 | } |
1055 | 0 | } |
1056 | | |
1057 | | /* |
1058 | | * Curl_init_dnscache() inits a new DNS cache. |
1059 | | */ |
1060 | | void Curl_init_dnscache(struct Curl_hash *hash, int size) |
1061 | 0 | { |
1062 | 0 | Curl_hash_init(hash, size, Curl_hash_str, Curl_str_key_compare, |
1063 | 0 | freednsentry); |
1064 | 0 | } |
1065 | | |
1066 | | /* |
1067 | | * Curl_hostcache_clean() |
1068 | | * |
1069 | | * This _can_ be called with 'data' == NULL but then of course no locking |
1070 | | * can be done! |
1071 | | */ |
1072 | | |
1073 | | void Curl_hostcache_clean(struct Curl_easy *data, |
1074 | | struct Curl_hash *hash) |
1075 | 0 | { |
1076 | 0 | if(data && data->share) |
1077 | 0 | Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE); |
1078 | |
|
1079 | 0 | Curl_hash_clean(hash); |
1080 | |
|
1081 | 0 | if(data && data->share) |
1082 | 0 | Curl_share_unlock(data, CURL_LOCK_DATA_DNS); |
1083 | 0 | } |
1084 | | |
1085 | | |
1086 | | CURLcode Curl_loadhostpairs(struct Curl_easy *data) |
1087 | 0 | { |
1088 | 0 | struct curl_slist *hostp; |
1089 | 0 | char *host_end; |
1090 | | |
1091 | | /* Default is no wildcard found */ |
1092 | 0 | data->state.wildcard_resolve = false; |
1093 | |
|
1094 | 0 | for(hostp = data->state.resolve; hostp; hostp = hostp->next) { |
1095 | 0 | char entry_id[MAX_HOSTCACHE_LEN]; |
1096 | 0 | if(!hostp->data) |
1097 | 0 | continue; |
1098 | 0 | if(hostp->data[0] == '-') { |
1099 | 0 | unsigned long num = 0; |
1100 | 0 | size_t entry_len; |
1101 | 0 | size_t hlen = 0; |
1102 | 0 | host_end = strchr(&hostp->data[1], ':'); |
1103 | |
|
1104 | 0 | if(host_end) { |
1105 | 0 | hlen = host_end - &hostp->data[1]; |
1106 | 0 | num = strtoul(++host_end, NULL, 10); |
1107 | 0 | if(!hlen || (num > 0xffff)) |
1108 | 0 | host_end = NULL; |
1109 | 0 | } |
1110 | 0 | if(!host_end) { |
1111 | 0 | infof(data, "Bad syntax CURLOPT_RESOLVE removal entry '%s'", |
1112 | 0 | hostp->data); |
1113 | 0 | continue; |
1114 | 0 | } |
1115 | | /* Create an entry id, based upon the hostname and port */ |
1116 | 0 | entry_len = create_hostcache_id(&hostp->data[1], hlen, (int)num, |
1117 | 0 | entry_id, sizeof(entry_id)); |
1118 | 0 | if(data->share) |
1119 | 0 | Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE); |
1120 | | |
1121 | | /* delete entry, ignore if it didn't exist */ |
1122 | 0 | Curl_hash_delete(data->dns.hostcache, entry_id, entry_len + 1); |
1123 | |
|
1124 | 0 | if(data->share) |
1125 | 0 | Curl_share_unlock(data, CURL_LOCK_DATA_DNS); |
1126 | 0 | } |
1127 | 0 | else { |
1128 | 0 | struct Curl_dns_entry *dns; |
1129 | 0 | struct Curl_addrinfo *head = NULL, *tail = NULL; |
1130 | 0 | size_t entry_len; |
1131 | 0 | char address[64]; |
1132 | 0 | #if !defined(CURL_DISABLE_VERBOSE_STRINGS) |
1133 | 0 | char *addresses = NULL; |
1134 | 0 | #endif |
1135 | 0 | char *addr_begin; |
1136 | 0 | char *addr_end; |
1137 | 0 | char *port_ptr; |
1138 | 0 | int port = 0; |
1139 | 0 | char *end_ptr; |
1140 | 0 | bool permanent = TRUE; |
1141 | 0 | unsigned long tmp_port; |
1142 | 0 | bool error = true; |
1143 | 0 | char *host_begin = hostp->data; |
1144 | 0 | size_t hlen = 0; |
1145 | |
|
1146 | 0 | if(host_begin[0] == '+') { |
1147 | 0 | host_begin++; |
1148 | 0 | permanent = FALSE; |
1149 | 0 | } |
1150 | 0 | host_end = strchr(host_begin, ':'); |
1151 | 0 | if(!host_end) |
1152 | 0 | goto err; |
1153 | 0 | hlen = host_end - host_begin; |
1154 | |
|
1155 | 0 | port_ptr = host_end + 1; |
1156 | 0 | tmp_port = strtoul(port_ptr, &end_ptr, 10); |
1157 | 0 | if(tmp_port > USHRT_MAX || end_ptr == port_ptr || *end_ptr != ':') |
1158 | 0 | goto err; |
1159 | | |
1160 | 0 | port = (int)tmp_port; |
1161 | 0 | #if !defined(CURL_DISABLE_VERBOSE_STRINGS) |
1162 | 0 | addresses = end_ptr + 1; |
1163 | 0 | #endif |
1164 | |
|
1165 | 0 | while(*end_ptr) { |
1166 | 0 | size_t alen; |
1167 | 0 | struct Curl_addrinfo *ai; |
1168 | |
|
1169 | 0 | addr_begin = end_ptr + 1; |
1170 | 0 | addr_end = strchr(addr_begin, ','); |
1171 | 0 | if(!addr_end) |
1172 | 0 | addr_end = addr_begin + strlen(addr_begin); |
1173 | 0 | end_ptr = addr_end; |
1174 | | |
1175 | | /* allow IP(v6) address within [brackets] */ |
1176 | 0 | if(*addr_begin == '[') { |
1177 | 0 | if(addr_end == addr_begin || *(addr_end - 1) != ']') |
1178 | 0 | goto err; |
1179 | 0 | ++addr_begin; |
1180 | 0 | --addr_end; |
1181 | 0 | } |
1182 | | |
1183 | 0 | alen = addr_end - addr_begin; |
1184 | 0 | if(!alen) |
1185 | 0 | continue; |
1186 | | |
1187 | 0 | if(alen >= sizeof(address)) |
1188 | 0 | goto err; |
1189 | | |
1190 | 0 | memcpy(address, addr_begin, alen); |
1191 | 0 | address[alen] = '\0'; |
1192 | |
|
1193 | | #ifndef ENABLE_IPV6 |
1194 | | if(strchr(address, ':')) { |
1195 | | infof(data, "Ignoring resolve address '%s', missing IPv6 support.", |
1196 | | address); |
1197 | | continue; |
1198 | | } |
1199 | | #endif |
1200 | |
|
1201 | 0 | ai = Curl_str2addr(address, port); |
1202 | 0 | if(!ai) { |
1203 | 0 | infof(data, "Resolve address '%s' found illegal", address); |
1204 | 0 | goto err; |
1205 | 0 | } |
1206 | | |
1207 | 0 | if(tail) { |
1208 | 0 | tail->ai_next = ai; |
1209 | 0 | tail = tail->ai_next; |
1210 | 0 | } |
1211 | 0 | else { |
1212 | 0 | head = tail = ai; |
1213 | 0 | } |
1214 | 0 | } |
1215 | | |
1216 | 0 | if(!head) |
1217 | 0 | goto err; |
1218 | | |
1219 | 0 | error = false; |
1220 | 0 | err: |
1221 | 0 | if(error) { |
1222 | 0 | failf(data, "Couldn't parse CURLOPT_RESOLVE entry '%s'", |
1223 | 0 | hostp->data); |
1224 | 0 | Curl_freeaddrinfo(head); |
1225 | 0 | return CURLE_SETOPT_OPTION_SYNTAX; |
1226 | 0 | } |
1227 | | |
1228 | | /* Create an entry id, based upon the hostname and port */ |
1229 | 0 | entry_len = create_hostcache_id(host_begin, hlen, port, |
1230 | 0 | entry_id, sizeof(entry_id)); |
1231 | |
|
1232 | 0 | if(data->share) |
1233 | 0 | Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE); |
1234 | | |
1235 | | /* See if it's already in our dns cache */ |
1236 | 0 | dns = Curl_hash_pick(data->dns.hostcache, entry_id, entry_len + 1); |
1237 | |
|
1238 | 0 | if(dns) { |
1239 | 0 | infof(data, "RESOLVE %.*s:%d - old addresses discarded", |
1240 | 0 | (int)hlen, host_begin, port); |
1241 | | /* delete old entry, there are two reasons for this |
1242 | | 1. old entry may have different addresses. |
1243 | | 2. even if entry with correct addresses is already in the cache, |
1244 | | but if it is close to expire, then by the time next http |
1245 | | request is made, it can get expired and pruned because old |
1246 | | entry is not necessarily marked as permanent. |
1247 | | 3. when adding a non-permanent entry, we want it to remove and |
1248 | | replace an existing permanent entry. |
1249 | | 4. when adding a non-permanent entry, we want it to get a "fresh" |
1250 | | timeout that starts _now_. */ |
1251 | |
|
1252 | 0 | Curl_hash_delete(data->dns.hostcache, entry_id, entry_len + 1); |
1253 | 0 | } |
1254 | | |
1255 | | /* put this new host in the cache */ |
1256 | 0 | dns = Curl_cache_addr(data, head, host_begin, hlen, port); |
1257 | 0 | if(dns) { |
1258 | 0 | if(permanent) |
1259 | 0 | dns->timestamp = 0; /* mark as permanent */ |
1260 | | /* release the returned reference; the cache itself will keep the |
1261 | | * entry alive: */ |
1262 | 0 | dns->inuse--; |
1263 | 0 | } |
1264 | |
|
1265 | 0 | if(data->share) |
1266 | 0 | Curl_share_unlock(data, CURL_LOCK_DATA_DNS); |
1267 | |
|
1268 | 0 | if(!dns) { |
1269 | 0 | Curl_freeaddrinfo(head); |
1270 | 0 | return CURLE_OUT_OF_MEMORY; |
1271 | 0 | } |
1272 | 0 | infof(data, "Added %.*s:%d:%s to DNS cache%s", |
1273 | 0 | (int)hlen, host_begin, port, addresses, |
1274 | 0 | permanent ? "" : " (non-permanent)"); |
1275 | | |
1276 | | /* Wildcard hostname */ |
1277 | 0 | if((hlen == 1) && (host_begin[0] == '*')) { |
1278 | 0 | infof(data, "RESOLVE *:%d using wildcard", port); |
1279 | 0 | data->state.wildcard_resolve = true; |
1280 | 0 | } |
1281 | 0 | } |
1282 | 0 | } |
1283 | 0 | data->state.resolve = NULL; /* dealt with now */ |
1284 | |
|
1285 | 0 | return CURLE_OK; |
1286 | 0 | } |
1287 | | |
1288 | | CURLcode Curl_resolv_check(struct Curl_easy *data, |
1289 | | struct Curl_dns_entry **dns) |
1290 | 0 | { |
1291 | | #if defined(CURL_DISABLE_DOH) && !defined(CURLRES_ASYNCH) |
1292 | | (void)data; |
1293 | | (void)dns; |
1294 | | #endif |
1295 | 0 | #ifndef CURL_DISABLE_DOH |
1296 | 0 | if(data->conn->bits.doh) |
1297 | 0 | return Curl_doh_is_resolved(data, dns); |
1298 | 0 | #endif |
1299 | 0 | return Curl_resolver_is_resolved(data, dns); |
1300 | 0 | } |
1301 | | |
1302 | | int Curl_resolv_getsock(struct Curl_easy *data, |
1303 | | curl_socket_t *socks) |
1304 | 0 | { |
1305 | | #ifdef CURLRES_ASYNCH |
1306 | | #ifndef CURL_DISABLE_DOH |
1307 | | if(data->conn->bits.doh) |
1308 | | /* nothing to wait for during DoH resolve, those handles have their own |
1309 | | sockets */ |
1310 | | return GETSOCK_BLANK; |
1311 | | #endif |
1312 | | return Curl_resolver_getsock(data, socks); |
1313 | | #else |
1314 | 0 | (void)data; |
1315 | 0 | (void)socks; |
1316 | 0 | return GETSOCK_BLANK; |
1317 | 0 | #endif |
1318 | 0 | } |
1319 | | |
1320 | | /* Call this function after Curl_connect() has returned async=TRUE and |
1321 | | then a successful name resolve has been received. |
1322 | | |
1323 | | Note: this function disconnects and frees the conn data in case of |
1324 | | resolve failure */ |
1325 | | CURLcode Curl_once_resolved(struct Curl_easy *data, bool *protocol_done) |
1326 | 0 | { |
1327 | 0 | CURLcode result; |
1328 | 0 | struct connectdata *conn = data->conn; |
1329 | |
|
1330 | 0 | #ifdef USE_CURL_ASYNC |
1331 | 0 | if(data->state.async.dns) { |
1332 | 0 | conn->dns_entry = data->state.async.dns; |
1333 | 0 | data->state.async.dns = NULL; |
1334 | 0 | } |
1335 | 0 | #endif |
1336 | |
|
1337 | 0 | result = Curl_setup_conn(data, protocol_done); |
1338 | |
|
1339 | 0 | if(result) { |
1340 | 0 | Curl_detach_connection(data); |
1341 | 0 | Curl_conncache_remove_conn(data, conn, TRUE); |
1342 | 0 | Curl_disconnect(data, conn, TRUE); |
1343 | 0 | } |
1344 | 0 | return result; |
1345 | 0 | } |
1346 | | |
1347 | | /* |
1348 | | * Curl_resolver_error() calls failf() with the appropriate message after a |
1349 | | * resolve error |
1350 | | */ |
1351 | | |
1352 | | #ifdef USE_CURL_ASYNC |
1353 | | CURLcode Curl_resolver_error(struct Curl_easy *data) |
1354 | 0 | { |
1355 | 0 | const char *host_or_proxy; |
1356 | 0 | CURLcode result; |
1357 | |
|
1358 | 0 | #ifndef CURL_DISABLE_PROXY |
1359 | 0 | struct connectdata *conn = data->conn; |
1360 | 0 | if(conn->bits.httpproxy) { |
1361 | 0 | host_or_proxy = "proxy"; |
1362 | 0 | result = CURLE_COULDNT_RESOLVE_PROXY; |
1363 | 0 | } |
1364 | 0 | else |
1365 | 0 | #endif |
1366 | 0 | { |
1367 | 0 | host_or_proxy = "host"; |
1368 | 0 | result = CURLE_COULDNT_RESOLVE_HOST; |
1369 | 0 | } |
1370 | |
|
1371 | 0 | failf(data, "Could not resolve %s: %s", host_or_proxy, |
1372 | 0 | data->state.async.hostname); |
1373 | |
|
1374 | 0 | return result; |
1375 | 0 | } |
1376 | | #endif /* USE_CURL_ASYNC */ |