hostip.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2021, 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. ***************************************************************************/
  22. #include "curl_setup.h"
  23. #ifdef HAVE_NETINET_IN_H
  24. #include <netinet/in.h>
  25. #endif
  26. #ifdef HAVE_NETINET_IN6_H
  27. #include <netinet/in6.h>
  28. #endif
  29. #ifdef HAVE_NETDB_H
  30. #include <netdb.h>
  31. #endif
  32. #ifdef HAVE_ARPA_INET_H
  33. #include <arpa/inet.h>
  34. #endif
  35. #ifdef __VMS
  36. #include <in.h>
  37. #include <inet.h>
  38. #endif
  39. #ifdef HAVE_SETJMP_H
  40. #include <setjmp.h>
  41. #endif
  42. #ifdef HAVE_SIGNAL_H
  43. #include <signal.h>
  44. #endif
  45. #ifdef HAVE_PROCESS_H
  46. #include <process.h>
  47. #endif
  48. #include "urldata.h"
  49. #include "sendf.h"
  50. #include "hostip.h"
  51. #include "hash.h"
  52. #include "rand.h"
  53. #include "share.h"
  54. #include "strerror.h"
  55. #include "url.h"
  56. #include "inet_ntop.h"
  57. #include "inet_pton.h"
  58. #include "multiif.h"
  59. #include "doh.h"
  60. #include "warnless.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. #if defined(ENABLE_IPV6) && defined(CURL_OSX_CALL_COPYPROXIES)
  66. #include <SystemConfiguration/SCDynamicStoreCopySpecific.h>
  67. #endif
  68. #if defined(CURLRES_SYNCH) && \
  69. defined(HAVE_ALARM) && defined(SIGALRM) && defined(HAVE_SIGSETJMP)
  70. /* alarm-based timeouts can only be used with all the dependencies satisfied */
  71. #define USE_ALARM_TIMEOUT
  72. #endif
  73. #define MAX_HOSTCACHE_LEN (255 + 7) /* max FQDN + colon + port number + zero */
  74. /*
  75. * hostip.c explained
  76. * ==================
  77. *
  78. * The main COMPILE-TIME DEFINES to keep in mind when reading the host*.c
  79. * source file are these:
  80. *
  81. * CURLRES_IPV6 - this host has getaddrinfo() and family, and thus we use
  82. * that. The host may not be able to resolve IPv6, but we don't really have to
  83. * take that into account. Hosts that aren't IPv6-enabled have CURLRES_IPV4
  84. * defined.
  85. *
  86. * CURLRES_ARES - is defined if libcurl is built to use c-ares for
  87. * asynchronous name resolves. This can be Windows or *nix.
  88. *
  89. * CURLRES_THREADED - is defined if libcurl is built to run under (native)
  90. * Windows, and then the name resolve will be done in a new thread, and the
  91. * supported API will be the same as for ares-builds.
  92. *
  93. * If any of the two previous are defined, CURLRES_ASYNCH is defined too. If
  94. * libcurl is not built to use an asynchronous resolver, CURLRES_SYNCH is
  95. * defined.
  96. *
  97. * The host*.c sources files are split up like this:
  98. *
  99. * hostip.c - method-independent resolver functions and utility functions
  100. * hostasyn.c - functions for asynchronous name resolves
  101. * hostsyn.c - functions for synchronous name resolves
  102. * hostip4.c - IPv4 specific functions
  103. * hostip6.c - IPv6 specific functions
  104. *
  105. * The two asynchronous name resolver backends are implemented in:
  106. * asyn-ares.c - functions for ares-using name resolves
  107. * asyn-thread.c - functions for threaded name resolves
  108. * The hostip.h is the united header file for all this. It defines the
  109. * CURLRES_* defines based on the config*.h and curl_setup.h defines.
  110. */
  111. static void freednsentry(void *freethis);
  112. /*
  113. * Return # of addresses in a Curl_addrinfo struct
  114. */
  115. int Curl_num_addresses(const struct Curl_addrinfo *addr)
  116. {
  117. int i = 0;
  118. while(addr) {
  119. addr = addr->ai_next;
  120. i++;
  121. }
  122. return i;
  123. }
  124. /*
  125. * Curl_printable_address() stores a printable version of the 1st address
  126. * given in the 'ai' argument. The result will be stored in the buf that is
  127. * bufsize bytes big.
  128. *
  129. * If the conversion fails, the target buffer is empty.
  130. */
  131. void Curl_printable_address(const struct Curl_addrinfo *ai, char *buf,
  132. size_t bufsize)
  133. {
  134. DEBUGASSERT(bufsize);
  135. buf[0] = 0;
  136. switch(ai->ai_family) {
  137. case AF_INET: {
  138. const struct sockaddr_in *sa4 = (const void *)ai->ai_addr;
  139. const struct in_addr *ipaddr4 = &sa4->sin_addr;
  140. (void)Curl_inet_ntop(ai->ai_family, (const void *)ipaddr4, buf, bufsize);
  141. break;
  142. }
  143. #ifdef ENABLE_IPV6
  144. case AF_INET6: {
  145. const struct sockaddr_in6 *sa6 = (const void *)ai->ai_addr;
  146. const struct in6_addr *ipaddr6 = &sa6->sin6_addr;
  147. (void)Curl_inet_ntop(ai->ai_family, (const void *)ipaddr6, buf, bufsize);
  148. break;
  149. }
  150. #endif
  151. default:
  152. break;
  153. }
  154. }
  155. /*
  156. * Create a hostcache id string for the provided host + port, to be used by
  157. * the DNS caching. Without alloc.
  158. */
  159. static void
  160. create_hostcache_id(const char *name, int port, char *ptr, size_t buflen)
  161. {
  162. size_t len = strlen(name);
  163. if(len > (buflen - 7))
  164. len = buflen - 7;
  165. /* store and lower case the name */
  166. while(len--)
  167. *ptr++ = (char)TOLOWER(*name++);
  168. msnprintf(ptr, 7, ":%u", port);
  169. }
  170. struct hostcache_prune_data {
  171. long cache_timeout;
  172. time_t now;
  173. };
  174. /*
  175. * This function is set as a callback to be called for every entry in the DNS
  176. * cache when we want to prune old unused entries.
  177. *
  178. * Returning non-zero means remove the entry, return 0 to keep it in the
  179. * cache.
  180. */
  181. static int
  182. hostcache_timestamp_remove(void *datap, void *hc)
  183. {
  184. struct hostcache_prune_data *data =
  185. (struct hostcache_prune_data *) datap;
  186. struct Curl_dns_entry *c = (struct Curl_dns_entry *) hc;
  187. return (0 != c->timestamp)
  188. && (data->now - c->timestamp >= data->cache_timeout);
  189. }
  190. /*
  191. * Prune the DNS cache. This assumes that a lock has already been taken.
  192. */
  193. static void
  194. hostcache_prune(struct Curl_hash *hostcache, long cache_timeout, time_t now)
  195. {
  196. struct hostcache_prune_data user;
  197. user.cache_timeout = cache_timeout;
  198. user.now = now;
  199. Curl_hash_clean_with_criterium(hostcache,
  200. (void *) &user,
  201. hostcache_timestamp_remove);
  202. }
  203. /*
  204. * Library-wide function for pruning the DNS cache. This function takes and
  205. * returns the appropriate locks.
  206. */
  207. void Curl_hostcache_prune(struct Curl_easy *data)
  208. {
  209. time_t now;
  210. if((data->set.dns_cache_timeout == -1) || !data->dns.hostcache)
  211. /* cache forever means never prune, and NULL hostcache means
  212. we can't do it */
  213. return;
  214. if(data->share)
  215. Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
  216. time(&now);
  217. /* Remove outdated and unused entries from the hostcache */
  218. hostcache_prune(data->dns.hostcache,
  219. data->set.dns_cache_timeout,
  220. now);
  221. if(data->share)
  222. Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
  223. }
  224. #ifdef HAVE_SIGSETJMP
  225. /* Beware this is a global and unique instance. This is used to store the
  226. return address that we can jump back to from inside a signal handler. This
  227. is not thread-safe stuff. */
  228. sigjmp_buf curl_jmpenv;
  229. #endif
  230. /* lookup address, returns entry if found and not stale */
  231. static struct Curl_dns_entry *fetch_addr(struct Curl_easy *data,
  232. const char *hostname,
  233. int port)
  234. {
  235. struct Curl_dns_entry *dns = NULL;
  236. size_t entry_len;
  237. char entry_id[MAX_HOSTCACHE_LEN];
  238. /* Create an entry id, based upon the hostname and port */
  239. create_hostcache_id(hostname, port, entry_id, sizeof(entry_id));
  240. entry_len = strlen(entry_id);
  241. /* See if its already in our dns cache */
  242. dns = Curl_hash_pick(data->dns.hostcache, entry_id, entry_len + 1);
  243. /* No entry found in cache, check if we might have a wildcard entry */
  244. if(!dns && data->state.wildcard_resolve) {
  245. create_hostcache_id("*", port, entry_id, sizeof(entry_id));
  246. entry_len = strlen(entry_id);
  247. /* See if it's already in our dns cache */
  248. dns = Curl_hash_pick(data->dns.hostcache, entry_id, entry_len + 1);
  249. }
  250. if(dns && (data->set.dns_cache_timeout != -1)) {
  251. /* See whether the returned entry is stale. Done before we release lock */
  252. struct hostcache_prune_data user;
  253. time(&user.now);
  254. user.cache_timeout = data->set.dns_cache_timeout;
  255. if(hostcache_timestamp_remove(&user, dns)) {
  256. infof(data, "Hostname in DNS cache was stale, zapped\n");
  257. dns = NULL; /* the memory deallocation is being handled by the hash */
  258. Curl_hash_delete(data->dns.hostcache, entry_id, entry_len + 1);
  259. }
  260. }
  261. return dns;
  262. }
  263. /*
  264. * Curl_fetch_addr() fetches a 'Curl_dns_entry' already in the DNS cache.
  265. *
  266. * Curl_resolv() checks initially and multi_runsingle() checks each time
  267. * it discovers the handle in the state WAITRESOLVE whether the hostname
  268. * has already been resolved and the address has already been stored in
  269. * the DNS cache. This short circuits waiting for a lot of pending
  270. * lookups for the same hostname requested by different handles.
  271. *
  272. * Returns the Curl_dns_entry entry pointer or NULL if not in the cache.
  273. *
  274. * The returned data *MUST* be "unlocked" with Curl_resolv_unlock() after
  275. * use, or we'll leak memory!
  276. */
  277. struct Curl_dns_entry *
  278. Curl_fetch_addr(struct Curl_easy *data,
  279. const char *hostname,
  280. int port)
  281. {
  282. struct Curl_dns_entry *dns = NULL;
  283. if(data->share)
  284. Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
  285. dns = fetch_addr(data, hostname, port);
  286. if(dns)
  287. dns->inuse++; /* we use it! */
  288. if(data->share)
  289. Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
  290. return dns;
  291. }
  292. #ifndef CURL_DISABLE_SHUFFLE_DNS
  293. UNITTEST CURLcode Curl_shuffle_addr(struct Curl_easy *data,
  294. struct Curl_addrinfo **addr);
  295. /*
  296. * Curl_shuffle_addr() shuffles the order of addresses in a 'Curl_addrinfo'
  297. * struct by re-linking its linked list.
  298. *
  299. * The addr argument should be the address of a pointer to the head node of a
  300. * `Curl_addrinfo` list and it will be modified to point to the new head after
  301. * shuffling.
  302. *
  303. * Not declared static only to make it easy to use in a unit test!
  304. *
  305. * @unittest: 1608
  306. */
  307. UNITTEST CURLcode Curl_shuffle_addr(struct Curl_easy *data,
  308. struct Curl_addrinfo **addr)
  309. {
  310. CURLcode result = CURLE_OK;
  311. const int num_addrs = Curl_num_addresses(*addr);
  312. if(num_addrs > 1) {
  313. struct Curl_addrinfo **nodes;
  314. infof(data, "Shuffling %i addresses", num_addrs);
  315. nodes = malloc(num_addrs*sizeof(*nodes));
  316. if(nodes) {
  317. int i;
  318. unsigned int *rnd;
  319. const size_t rnd_size = num_addrs * sizeof(*rnd);
  320. /* build a plain array of Curl_addrinfo pointers */
  321. nodes[0] = *addr;
  322. for(i = 1; i < num_addrs; i++) {
  323. nodes[i] = nodes[i-1]->ai_next;
  324. }
  325. rnd = malloc(rnd_size);
  326. if(rnd) {
  327. /* Fisher-Yates shuffle */
  328. if(Curl_rand(data, (unsigned char *)rnd, rnd_size) == CURLE_OK) {
  329. struct Curl_addrinfo *swap_tmp;
  330. for(i = num_addrs - 1; i > 0; i--) {
  331. swap_tmp = nodes[rnd[i] % (i + 1)];
  332. nodes[rnd[i] % (i + 1)] = nodes[i];
  333. nodes[i] = swap_tmp;
  334. }
  335. /* relink list in the new order */
  336. for(i = 1; i < num_addrs; i++) {
  337. nodes[i-1]->ai_next = nodes[i];
  338. }
  339. nodes[num_addrs-1]->ai_next = NULL;
  340. *addr = nodes[0];
  341. }
  342. free(rnd);
  343. }
  344. else
  345. result = CURLE_OUT_OF_MEMORY;
  346. free(nodes);
  347. }
  348. else
  349. result = CURLE_OUT_OF_MEMORY;
  350. }
  351. return result;
  352. }
  353. #endif
  354. /*
  355. * Curl_cache_addr() stores a 'Curl_addrinfo' struct in the DNS cache.
  356. *
  357. * When calling Curl_resolv() has resulted in a response with a returned
  358. * address, we call this function to store the information in the dns
  359. * cache etc
  360. *
  361. * Returns the Curl_dns_entry entry pointer or NULL if the storage failed.
  362. */
  363. struct Curl_dns_entry *
  364. Curl_cache_addr(struct Curl_easy *data,
  365. struct Curl_addrinfo *addr,
  366. const char *hostname,
  367. int port)
  368. {
  369. char entry_id[MAX_HOSTCACHE_LEN];
  370. size_t entry_len;
  371. struct Curl_dns_entry *dns;
  372. struct Curl_dns_entry *dns2;
  373. #ifndef CURL_DISABLE_SHUFFLE_DNS
  374. /* shuffle addresses if requested */
  375. if(data->set.dns_shuffle_addresses) {
  376. CURLcode result = Curl_shuffle_addr(data, &addr);
  377. if(result)
  378. return NULL;
  379. }
  380. #endif
  381. /* Create a new cache entry */
  382. dns = calloc(1, sizeof(struct Curl_dns_entry));
  383. if(!dns) {
  384. return NULL;
  385. }
  386. /* Create an entry id, based upon the hostname and port */
  387. create_hostcache_id(hostname, port, entry_id, sizeof(entry_id));
  388. entry_len = strlen(entry_id);
  389. dns->inuse = 1; /* the cache has the first reference */
  390. dns->addr = addr; /* this is the address(es) */
  391. time(&dns->timestamp);
  392. if(dns->timestamp == 0)
  393. dns->timestamp = 1; /* zero indicates permanent CURLOPT_RESOLVE entry */
  394. /* Store the resolved data in our DNS cache. */
  395. dns2 = Curl_hash_add(data->dns.hostcache, entry_id, entry_len + 1,
  396. (void *)dns);
  397. if(!dns2) {
  398. free(dns);
  399. return NULL;
  400. }
  401. dns = dns2;
  402. dns->inuse++; /* mark entry as in-use */
  403. return dns;
  404. }
  405. /*
  406. * Curl_resolv() is the main name resolve function within libcurl. It resolves
  407. * a name and returns a pointer to the entry in the 'entry' argument (if one
  408. * is provided). This function might return immediately if we're using asynch
  409. * resolves. See the return codes.
  410. *
  411. * The cache entry we return will get its 'inuse' counter increased when this
  412. * function is used. You MUST call Curl_resolv_unlock() later (when you're
  413. * done using this struct) to decrease the counter again.
  414. *
  415. * Return codes:
  416. *
  417. * CURLRESOLV_ERROR (-1) = error, no pointer
  418. * CURLRESOLV_RESOLVED (0) = OK, pointer provided
  419. * CURLRESOLV_PENDING (1) = waiting for response, no pointer
  420. */
  421. enum resolve_t Curl_resolv(struct Curl_easy *data,
  422. const char *hostname,
  423. int port,
  424. bool allowDOH,
  425. struct Curl_dns_entry **entry)
  426. {
  427. struct Curl_dns_entry *dns = NULL;
  428. CURLcode result;
  429. enum resolve_t rc = CURLRESOLV_ERROR; /* default to failure */
  430. struct connectdata *conn = data->conn;
  431. *entry = NULL;
  432. conn->bits.doh = FALSE; /* default is not */
  433. if(data->share)
  434. Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
  435. dns = fetch_addr(data, hostname, port);
  436. if(dns) {
  437. infof(data, "Hostname %s was found in DNS cache\n", hostname);
  438. dns->inuse++; /* we use it! */
  439. rc = CURLRESOLV_RESOLVED;
  440. }
  441. if(data->share)
  442. Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
  443. if(!dns) {
  444. /* The entry was not in the cache. Resolve it to IP address */
  445. struct Curl_addrinfo *addr = NULL;
  446. int respwait = 0;
  447. struct in_addr in;
  448. #ifndef USE_RESOLVE_ON_IPS
  449. const
  450. #endif
  451. bool ipnum = FALSE;
  452. /* notify the resolver start callback */
  453. if(data->set.resolver_start) {
  454. int st;
  455. Curl_set_in_callback(data, true);
  456. st = data->set.resolver_start(
  457. #ifdef USE_CURL_ASYNC
  458. data->state.async.resolver,
  459. #else
  460. NULL,
  461. #endif
  462. NULL,
  463. data->set.resolver_start_client);
  464. Curl_set_in_callback(data, false);
  465. if(st)
  466. return CURLRESOLV_ERROR;
  467. }
  468. #if defined(ENABLE_IPV6) && defined(CURL_OSX_CALL_COPYPROXIES)
  469. /*
  470. * The automagic conversion from IPv4 literals to IPv6 literals only works
  471. * if the SCDynamicStoreCopyProxies system function gets called first. As
  472. * Curl currently doesn't support system-wide HTTP proxies, we therefore
  473. * don't use any value this function might return.
  474. *
  475. * This function is only available on a macOS and is not needed for
  476. * IPv4-only builds, hence the conditions above.
  477. */
  478. SCDynamicStoreCopyProxies(NULL);
  479. #endif
  480. #ifndef USE_RESOLVE_ON_IPS
  481. /* First check if this is an IPv4 address string */
  482. if(Curl_inet_pton(AF_INET, hostname, &in) > 0)
  483. /* This is a dotted IP address 123.123.123.123-style */
  484. addr = Curl_ip2addr(AF_INET, &in, hostname, port);
  485. #ifdef ENABLE_IPV6
  486. if(!addr) {
  487. struct in6_addr in6;
  488. /* check if this is an IPv6 address string */
  489. if(Curl_inet_pton(AF_INET6, hostname, &in6) > 0)
  490. /* This is an IPv6 address literal */
  491. addr = Curl_ip2addr(AF_INET6, &in6, hostname, port);
  492. }
  493. #endif /* ENABLE_IPV6 */
  494. #else /* if USE_RESOLVE_ON_IPS */
  495. /* First check if this is an IPv4 address string */
  496. if(Curl_inet_pton(AF_INET, hostname, &in) > 0)
  497. /* This is a dotted IP address 123.123.123.123-style */
  498. ipnum = TRUE;
  499. #ifdef ENABLE_IPV6
  500. else {
  501. struct in6_addr in6;
  502. /* check if this is an IPv6 address string */
  503. if(Curl_inet_pton(AF_INET6, hostname, &in6) > 0)
  504. /* This is an IPv6 address literal */
  505. ipnum = TRUE;
  506. }
  507. #endif /* ENABLE_IPV6 */
  508. #endif /* !USE_RESOLVE_ON_IPS */
  509. if(!addr) {
  510. /* Check what IP specifics the app has requested and if we can provide
  511. * it. If not, bail out. */
  512. if(!Curl_ipvalid(data, conn))
  513. return CURLRESOLV_ERROR;
  514. if(allowDOH && data->set.doh && !ipnum) {
  515. addr = Curl_doh(data, hostname, port, &respwait);
  516. }
  517. else {
  518. /* If Curl_getaddrinfo() returns NULL, 'respwait' might be set to a
  519. non-zero value indicating that we need to wait for the response to
  520. the resolve call */
  521. addr = Curl_getaddrinfo(data, hostname, port, &respwait);
  522. }
  523. }
  524. if(!addr) {
  525. if(respwait) {
  526. /* the response to our resolve call will come asynchronously at
  527. a later time, good or bad */
  528. /* First, check that we haven't received the info by now */
  529. result = Curl_resolv_check(data, &dns);
  530. if(result) /* error detected */
  531. return CURLRESOLV_ERROR;
  532. if(dns)
  533. rc = CURLRESOLV_RESOLVED; /* pointer provided */
  534. else
  535. rc = CURLRESOLV_PENDING; /* no info yet */
  536. }
  537. }
  538. else {
  539. if(data->share)
  540. Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
  541. /* we got a response, store it in the cache */
  542. dns = Curl_cache_addr(data, addr, hostname, port);
  543. if(data->share)
  544. Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
  545. if(!dns)
  546. /* returned failure, bail out nicely */
  547. Curl_freeaddrinfo(addr);
  548. else
  549. rc = CURLRESOLV_RESOLVED;
  550. }
  551. }
  552. *entry = dns;
  553. return rc;
  554. }
  555. #ifdef USE_ALARM_TIMEOUT
  556. /*
  557. * This signal handler jumps back into the main libcurl code and continues
  558. * execution. This effectively causes the remainder of the application to run
  559. * within a signal handler which is nonportable and could lead to problems.
  560. */
  561. static
  562. void alarmfunc(int sig)
  563. {
  564. /* this is for "-ansi -Wall -pedantic" to stop complaining! (rabe) */
  565. (void)sig;
  566. siglongjmp(curl_jmpenv, 1);
  567. }
  568. #endif /* USE_ALARM_TIMEOUT */
  569. /*
  570. * Curl_resolv_timeout() is the same as Curl_resolv() but specifies a
  571. * timeout. This function might return immediately if we're using asynch
  572. * resolves. See the return codes.
  573. *
  574. * The cache entry we return will get its 'inuse' counter increased when this
  575. * function is used. You MUST call Curl_resolv_unlock() later (when you're
  576. * done using this struct) to decrease the counter again.
  577. *
  578. * If built with a synchronous resolver and use of signals is not
  579. * disabled by the application, then a nonzero timeout will cause a
  580. * timeout after the specified number of milliseconds. Otherwise, timeout
  581. * is ignored.
  582. *
  583. * Return codes:
  584. *
  585. * CURLRESOLV_TIMEDOUT(-2) = warning, time too short or previous alarm expired
  586. * CURLRESOLV_ERROR (-1) = error, no pointer
  587. * CURLRESOLV_RESOLVED (0) = OK, pointer provided
  588. * CURLRESOLV_PENDING (1) = waiting for response, no pointer
  589. */
  590. enum resolve_t Curl_resolv_timeout(struct Curl_easy *data,
  591. const char *hostname,
  592. int port,
  593. struct Curl_dns_entry **entry,
  594. timediff_t timeoutms)
  595. {
  596. #ifdef USE_ALARM_TIMEOUT
  597. #ifdef HAVE_SIGACTION
  598. struct sigaction keep_sigact; /* store the old struct here */
  599. volatile bool keep_copysig = FALSE; /* whether old sigact has been saved */
  600. struct sigaction sigact;
  601. #else
  602. #ifdef HAVE_SIGNAL
  603. void (*keep_sigact)(int); /* store the old handler here */
  604. #endif /* HAVE_SIGNAL */
  605. #endif /* HAVE_SIGACTION */
  606. volatile long timeout;
  607. volatile unsigned int prev_alarm = 0;
  608. #endif /* USE_ALARM_TIMEOUT */
  609. enum resolve_t rc;
  610. *entry = NULL;
  611. if(timeoutms < 0)
  612. /* got an already expired timeout */
  613. return CURLRESOLV_TIMEDOUT;
  614. #ifdef USE_ALARM_TIMEOUT
  615. if(data->set.no_signal)
  616. /* Ignore the timeout when signals are disabled */
  617. timeout = 0;
  618. else
  619. timeout = (timeoutms > LONG_MAX) ? LONG_MAX : (long)timeoutms;
  620. if(!timeout)
  621. /* USE_ALARM_TIMEOUT defined, but no timeout actually requested */
  622. return Curl_resolv(data, hostname, port, TRUE, entry);
  623. if(timeout < 1000) {
  624. /* The alarm() function only provides integer second resolution, so if
  625. we want to wait less than one second we must bail out already now. */
  626. failf(data,
  627. "remaining timeout of %ld too small to resolve via SIGALRM method",
  628. timeout);
  629. return CURLRESOLV_TIMEDOUT;
  630. }
  631. /* This allows us to time-out from the name resolver, as the timeout
  632. will generate a signal and we will siglongjmp() from that here.
  633. This technique has problems (see alarmfunc).
  634. This should be the last thing we do before calling Curl_resolv(),
  635. as otherwise we'd have to worry about variables that get modified
  636. before we invoke Curl_resolv() (and thus use "volatile"). */
  637. if(sigsetjmp(curl_jmpenv, 1)) {
  638. /* this is coming from a siglongjmp() after an alarm signal */
  639. failf(data, "name lookup timed out");
  640. rc = CURLRESOLV_ERROR;
  641. goto clean_up;
  642. }
  643. else {
  644. /*************************************************************
  645. * Set signal handler to catch SIGALRM
  646. * Store the old value to be able to set it back later!
  647. *************************************************************/
  648. #ifdef HAVE_SIGACTION
  649. sigaction(SIGALRM, NULL, &sigact);
  650. keep_sigact = sigact;
  651. keep_copysig = TRUE; /* yes, we have a copy */
  652. sigact.sa_handler = alarmfunc;
  653. #ifdef SA_RESTART
  654. /* HPUX doesn't have SA_RESTART but defaults to that behavior! */
  655. sigact.sa_flags &= ~SA_RESTART;
  656. #endif
  657. /* now set the new struct */
  658. sigaction(SIGALRM, &sigact, NULL);
  659. #else /* HAVE_SIGACTION */
  660. /* no sigaction(), revert to the much lamer signal() */
  661. #ifdef HAVE_SIGNAL
  662. keep_sigact = signal(SIGALRM, alarmfunc);
  663. #endif
  664. #endif /* HAVE_SIGACTION */
  665. /* alarm() makes a signal get sent when the timeout fires off, and that
  666. will abort system calls */
  667. prev_alarm = alarm(curlx_sltoui(timeout/1000L));
  668. }
  669. #else
  670. #ifndef CURLRES_ASYNCH
  671. if(timeoutms)
  672. infof(data, "timeout on name lookup is not supported\n");
  673. #else
  674. (void)timeoutms; /* timeoutms not used with an async resolver */
  675. #endif
  676. #endif /* USE_ALARM_TIMEOUT */
  677. /* Perform the actual name resolution. This might be interrupted by an
  678. * alarm if it takes too long.
  679. */
  680. rc = Curl_resolv(data, hostname, port, TRUE, entry);
  681. #ifdef USE_ALARM_TIMEOUT
  682. clean_up:
  683. if(!prev_alarm)
  684. /* deactivate a possibly active alarm before uninstalling the handler */
  685. alarm(0);
  686. #ifdef HAVE_SIGACTION
  687. if(keep_copysig) {
  688. /* we got a struct as it looked before, now put that one back nice
  689. and clean */
  690. sigaction(SIGALRM, &keep_sigact, NULL); /* put it back */
  691. }
  692. #else
  693. #ifdef HAVE_SIGNAL
  694. /* restore the previous SIGALRM handler */
  695. signal(SIGALRM, keep_sigact);
  696. #endif
  697. #endif /* HAVE_SIGACTION */
  698. /* switch back the alarm() to either zero or to what it was before minus
  699. the time we spent until now! */
  700. if(prev_alarm) {
  701. /* there was an alarm() set before us, now put it back */
  702. timediff_t elapsed_secs = Curl_timediff(Curl_now(),
  703. data->conn->created) / 1000;
  704. /* the alarm period is counted in even number of seconds */
  705. unsigned long alarm_set = (unsigned long)(prev_alarm - elapsed_secs);
  706. if(!alarm_set ||
  707. ((alarm_set >= 0x80000000) && (prev_alarm < 0x80000000)) ) {
  708. /* if the alarm time-left reached zero or turned "negative" (counted
  709. with unsigned values), we should fire off a SIGALRM here, but we
  710. won't, and zero would be to switch it off so we never set it to
  711. less than 1! */
  712. alarm(1);
  713. rc = CURLRESOLV_TIMEDOUT;
  714. failf(data, "Previous alarm fired off!");
  715. }
  716. else
  717. alarm((unsigned int)alarm_set);
  718. }
  719. #endif /* USE_ALARM_TIMEOUT */
  720. return rc;
  721. }
  722. /*
  723. * Curl_resolv_unlock() unlocks the given cached DNS entry. When this has been
  724. * made, the struct may be destroyed due to pruning. It is important that only
  725. * one unlock is made for each Curl_resolv() call.
  726. *
  727. * May be called with 'data' == NULL for global cache.
  728. */
  729. void Curl_resolv_unlock(struct Curl_easy *data, struct Curl_dns_entry *dns)
  730. {
  731. if(data && data->share)
  732. Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
  733. freednsentry(dns);
  734. if(data && data->share)
  735. Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
  736. }
  737. /*
  738. * File-internal: release cache dns entry reference, free if inuse drops to 0
  739. */
  740. static void freednsentry(void *freethis)
  741. {
  742. struct Curl_dns_entry *dns = (struct Curl_dns_entry *) freethis;
  743. DEBUGASSERT(dns && (dns->inuse>0));
  744. dns->inuse--;
  745. if(dns->inuse == 0) {
  746. Curl_freeaddrinfo(dns->addr);
  747. free(dns);
  748. }
  749. }
  750. /*
  751. * Curl_mk_dnscache() inits a new DNS cache and returns success/failure.
  752. */
  753. int Curl_mk_dnscache(struct Curl_hash *hash)
  754. {
  755. return Curl_hash_init(hash, 7, Curl_hash_str, Curl_str_key_compare,
  756. freednsentry);
  757. }
  758. /*
  759. * Curl_hostcache_clean()
  760. *
  761. * This _can_ be called with 'data' == NULL but then of course no locking
  762. * can be done!
  763. */
  764. void Curl_hostcache_clean(struct Curl_easy *data,
  765. struct Curl_hash *hash)
  766. {
  767. if(data && data->share)
  768. Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
  769. Curl_hash_clean(hash);
  770. if(data && data->share)
  771. Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
  772. }
  773. CURLcode Curl_loadhostpairs(struct Curl_easy *data)
  774. {
  775. struct curl_slist *hostp;
  776. char hostname[256];
  777. int port = 0;
  778. /* Default is no wildcard found */
  779. data->state.wildcard_resolve = false;
  780. for(hostp = data->state.resolve; hostp; hostp = hostp->next) {
  781. char entry_id[MAX_HOSTCACHE_LEN];
  782. if(!hostp->data)
  783. continue;
  784. if(hostp->data[0] == '-') {
  785. size_t entry_len;
  786. if(2 != sscanf(hostp->data + 1, "%255[^:]:%d", hostname, &port)) {
  787. infof(data, "Couldn't parse CURLOPT_RESOLVE removal entry '%s'!\n",
  788. hostp->data);
  789. continue;
  790. }
  791. /* Create an entry id, based upon the hostname and port */
  792. create_hostcache_id(hostname, port, entry_id, sizeof(entry_id));
  793. entry_len = strlen(entry_id);
  794. if(data->share)
  795. Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
  796. /* delete entry, ignore if it didn't exist */
  797. Curl_hash_delete(data->dns.hostcache, entry_id, entry_len + 1);
  798. if(data->share)
  799. Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
  800. }
  801. else {
  802. struct Curl_dns_entry *dns;
  803. struct Curl_addrinfo *head = NULL, *tail = NULL;
  804. size_t entry_len;
  805. char address[64];
  806. #if !defined(CURL_DISABLE_VERBOSE_STRINGS)
  807. char *addresses = NULL;
  808. #endif
  809. char *addr_begin;
  810. char *addr_end;
  811. char *port_ptr;
  812. char *end_ptr;
  813. bool permanent = TRUE;
  814. char *host_begin;
  815. char *host_end;
  816. unsigned long tmp_port;
  817. bool error = true;
  818. host_begin = hostp->data;
  819. if(host_begin[0] == '+') {
  820. host_begin++;
  821. permanent = FALSE;
  822. }
  823. host_end = strchr(host_begin, ':');
  824. if(!host_end ||
  825. ((host_end - host_begin) >= (ptrdiff_t)sizeof(hostname)))
  826. goto err;
  827. memcpy(hostname, host_begin, host_end - host_begin);
  828. hostname[host_end - host_begin] = '\0';
  829. port_ptr = host_end + 1;
  830. tmp_port = strtoul(port_ptr, &end_ptr, 10);
  831. if(tmp_port > USHRT_MAX || end_ptr == port_ptr || *end_ptr != ':')
  832. goto err;
  833. port = (int)tmp_port;
  834. #if !defined(CURL_DISABLE_VERBOSE_STRINGS)
  835. addresses = end_ptr + 1;
  836. #endif
  837. while(*end_ptr) {
  838. size_t alen;
  839. struct Curl_addrinfo *ai;
  840. addr_begin = end_ptr + 1;
  841. addr_end = strchr(addr_begin, ',');
  842. if(!addr_end)
  843. addr_end = addr_begin + strlen(addr_begin);
  844. end_ptr = addr_end;
  845. /* allow IP(v6) address within [brackets] */
  846. if(*addr_begin == '[') {
  847. if(addr_end == addr_begin || *(addr_end - 1) != ']')
  848. goto err;
  849. ++addr_begin;
  850. --addr_end;
  851. }
  852. alen = addr_end - addr_begin;
  853. if(!alen)
  854. continue;
  855. if(alen >= sizeof(address))
  856. goto err;
  857. memcpy(address, addr_begin, alen);
  858. address[alen] = '\0';
  859. #ifndef ENABLE_IPV6
  860. if(strchr(address, ':')) {
  861. infof(data, "Ignoring resolve address '%s', missing IPv6 support.\n",
  862. address);
  863. continue;
  864. }
  865. #endif
  866. ai = Curl_str2addr(address, port);
  867. if(!ai) {
  868. infof(data, "Resolve address '%s' found illegal!\n", address);
  869. goto err;
  870. }
  871. if(tail) {
  872. tail->ai_next = ai;
  873. tail = tail->ai_next;
  874. }
  875. else {
  876. head = tail = ai;
  877. }
  878. }
  879. if(!head)
  880. goto err;
  881. error = false;
  882. err:
  883. if(error) {
  884. infof(data, "Couldn't parse CURLOPT_RESOLVE entry '%s'!\n",
  885. hostp->data);
  886. Curl_freeaddrinfo(head);
  887. continue;
  888. }
  889. /* Create an entry id, based upon the hostname and port */
  890. create_hostcache_id(hostname, port, entry_id, sizeof(entry_id));
  891. entry_len = strlen(entry_id);
  892. if(data->share)
  893. Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
  894. /* See if it's already in our dns cache */
  895. dns = Curl_hash_pick(data->dns.hostcache, entry_id, entry_len + 1);
  896. if(dns) {
  897. infof(data, "RESOLVE %s:%d is - old addresses discarded!\n",
  898. hostname, port);
  899. /* delete old entry, there are two reasons for this
  900. 1. old entry may have different addresses.
  901. 2. even if entry with correct addresses is already in the cache,
  902. but if it is close to expire, then by the time next http
  903. request is made, it can get expired and pruned because old
  904. entry is not necessarily marked as permanent.
  905. 3. when adding a non-permanent entry, we want it to remove and
  906. replace an existing permanent entry.
  907. 4. when adding a non-permanent entry, we want it to get a "fresh"
  908. timeout that starts _now_. */
  909. Curl_hash_delete(data->dns.hostcache, entry_id, entry_len + 1);
  910. }
  911. /* put this new host in the cache */
  912. dns = Curl_cache_addr(data, head, hostname, port);
  913. if(dns) {
  914. if(permanent)
  915. dns->timestamp = 0; /* mark as permanent */
  916. /* release the returned reference; the cache itself will keep the
  917. * entry alive: */
  918. dns->inuse--;
  919. }
  920. if(data->share)
  921. Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
  922. if(!dns) {
  923. Curl_freeaddrinfo(head);
  924. return CURLE_OUT_OF_MEMORY;
  925. }
  926. infof(data, "Added %s:%d:%s to DNS cache%s\n",
  927. hostname, port, addresses, permanent ? "" : " (non-permanent)");
  928. /* Wildcard hostname */
  929. if(hostname[0] == '*' && hostname[1] == '\0') {
  930. infof(data, "RESOLVE %s:%d is wildcard, enabling wildcard checks\n",
  931. hostname, port);
  932. data->state.wildcard_resolve = true;
  933. }
  934. }
  935. }
  936. data->state.resolve = NULL; /* dealt with now */
  937. return CURLE_OK;
  938. }
  939. CURLcode Curl_resolv_check(struct Curl_easy *data,
  940. struct Curl_dns_entry **dns)
  941. {
  942. #if defined(CURL_DISABLE_DOH) && !defined(CURLRES_ASYNCH)
  943. (void)dns;
  944. #endif
  945. if(data->conn->bits.doh)
  946. return Curl_doh_is_resolved(data, dns);
  947. return Curl_resolver_is_resolved(data, dns);
  948. }
  949. int Curl_resolv_getsock(struct Curl_easy *data,
  950. curl_socket_t *socks)
  951. {
  952. #ifdef CURLRES_ASYNCH
  953. if(data->conn->bits.doh)
  954. /* nothing to wait for during DOH resolve, those handles have their own
  955. sockets */
  956. return GETSOCK_BLANK;
  957. return Curl_resolver_getsock(data, socks);
  958. #else
  959. (void)data;
  960. (void)socks;
  961. return GETSOCK_BLANK;
  962. #endif
  963. }
  964. /* Call this function after Curl_connect() has returned async=TRUE and
  965. then a successful name resolve has been received.
  966. Note: this function disconnects and frees the conn data in case of
  967. resolve failure */
  968. CURLcode Curl_once_resolved(struct Curl_easy *data, bool *protocol_done)
  969. {
  970. CURLcode result;
  971. struct connectdata *conn = data->conn;
  972. #ifdef USE_CURL_ASYNC
  973. if(data->state.async.dns) {
  974. conn->dns_entry = data->state.async.dns;
  975. data->state.async.dns = NULL;
  976. }
  977. #endif
  978. result = Curl_setup_conn(data, protocol_done);
  979. if(result) {
  980. Curl_detach_connnection(data);
  981. Curl_conncache_remove_conn(data, conn, TRUE);
  982. Curl_disconnect(data, conn, TRUE);
  983. }
  984. return result;
  985. }
  986. /*
  987. * Curl_resolver_error() calls failf() with the appropriate message after a
  988. * resolve error
  989. */
  990. #ifdef USE_CURL_ASYNC
  991. CURLcode Curl_resolver_error(struct Curl_easy *data)
  992. {
  993. const char *host_or_proxy;
  994. CURLcode result;
  995. #ifndef CURL_DISABLE_PROXY
  996. struct connectdata *conn = data->conn;
  997. if(conn->bits.httpproxy) {
  998. host_or_proxy = "proxy";
  999. result = CURLE_COULDNT_RESOLVE_PROXY;
  1000. }
  1001. else
  1002. #endif
  1003. {
  1004. host_or_proxy = "host";
  1005. result = CURLE_COULDNT_RESOLVE_HOST;
  1006. }
  1007. failf(data, "Could not resolve %s: %s", host_or_proxy,
  1008. data->state.async.hostname);
  1009. return result;
  1010. }
  1011. #endif /* USE_CURL_ASYNC */