hostip.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2015, 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 http://curl.haxx.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_NETDB_H
  27. #include <netdb.h>
  28. #endif
  29. #ifdef HAVE_ARPA_INET_H
  30. #include <arpa/inet.h>
  31. #endif
  32. #ifdef __VMS
  33. #include <in.h>
  34. #include <inet.h>
  35. #endif
  36. #ifdef HAVE_SETJMP_H
  37. #include <setjmp.h>
  38. #endif
  39. #ifdef HAVE_SIGNAL_H
  40. #include <signal.h>
  41. #endif
  42. #ifdef HAVE_PROCESS_H
  43. #include <process.h>
  44. #endif
  45. #include "urldata.h"
  46. #include "sendf.h"
  47. #include "hostip.h"
  48. #include "hash.h"
  49. #include "share.h"
  50. #include "strerror.h"
  51. #include "url.h"
  52. #include "inet_ntop.h"
  53. #include "warnless.h"
  54. #define _MPRINTF_REPLACE /* use our functions only */
  55. #include <curl/mprintf.h>
  56. #include "curl_memory.h"
  57. /* The last #include file should be: */
  58. #include "memdebug.h"
  59. #if defined(CURLRES_SYNCH) && \
  60. defined(HAVE_ALARM) && defined(SIGALRM) && defined(HAVE_SIGSETJMP)
  61. /* alarm-based timeouts can only be used with all the dependencies satisfied */
  62. #define USE_ALARM_TIMEOUT
  63. #endif
  64. /*
  65. * hostip.c explained
  66. * ==================
  67. *
  68. * The main COMPILE-TIME DEFINES to keep in mind when reading the host*.c
  69. * source file are these:
  70. *
  71. * CURLRES_IPV6 - this host has getaddrinfo() and family, and thus we use
  72. * that. The host may not be able to resolve IPv6, but we don't really have to
  73. * take that into account. Hosts that aren't IPv6-enabled have CURLRES_IPV4
  74. * defined.
  75. *
  76. * CURLRES_ARES - is defined if libcurl is built to use c-ares for
  77. * asynchronous name resolves. This can be Windows or *nix.
  78. *
  79. * CURLRES_THREADED - is defined if libcurl is built to run under (native)
  80. * Windows, and then the name resolve will be done in a new thread, and the
  81. * supported API will be the same as for ares-builds.
  82. *
  83. * If any of the two previous are defined, CURLRES_ASYNCH is defined too. If
  84. * libcurl is not built to use an asynchronous resolver, CURLRES_SYNCH is
  85. * defined.
  86. *
  87. * The host*.c sources files are split up like this:
  88. *
  89. * hostip.c - method-independent resolver functions and utility functions
  90. * hostasyn.c - functions for asynchronous name resolves
  91. * hostsyn.c - functions for synchronous name resolves
  92. * hostip4.c - IPv4 specific functions
  93. * hostip6.c - IPv6 specific functions
  94. *
  95. * The two asynchronous name resolver backends are implemented in:
  96. * asyn-ares.c - functions for ares-using name resolves
  97. * asyn-thread.c - functions for threaded name resolves
  98. * The hostip.h is the united header file for all this. It defines the
  99. * CURLRES_* defines based on the config*.h and curl_setup.h defines.
  100. */
  101. /* These two symbols are for the global DNS cache */
  102. static struct curl_hash hostname_cache;
  103. static int host_cache_initialized;
  104. static void freednsentry(void *freethis);
  105. /*
  106. * Curl_global_host_cache_init() initializes and sets up a global DNS cache.
  107. * Global DNS cache is general badness. Do not use. This will be removed in
  108. * a future version. Use the share interface instead!
  109. *
  110. * Returns a struct curl_hash pointer on success, NULL on failure.
  111. */
  112. struct curl_hash *Curl_global_host_cache_init(void)
  113. {
  114. int rc = 0;
  115. if(!host_cache_initialized) {
  116. rc = Curl_hash_init(&hostname_cache, 7, Curl_hash_str,
  117. Curl_str_key_compare, freednsentry);
  118. if(!rc)
  119. host_cache_initialized = 1;
  120. }
  121. return rc?NULL:&hostname_cache;
  122. }
  123. /*
  124. * Destroy and cleanup the global DNS cache
  125. */
  126. void Curl_global_host_cache_dtor(void)
  127. {
  128. if(host_cache_initialized) {
  129. /* first make sure that any custom "CURLOPT_RESOLVE" names are
  130. cleared off */
  131. Curl_hostcache_clean(NULL, &hostname_cache);
  132. /* then free the remaining hash completely */
  133. Curl_hash_clean(&hostname_cache);
  134. host_cache_initialized = 0;
  135. }
  136. }
  137. /*
  138. * Return # of adresses in a Curl_addrinfo struct
  139. */
  140. int Curl_num_addresses(const Curl_addrinfo *addr)
  141. {
  142. int i = 0;
  143. while(addr) {
  144. addr = addr->ai_next;
  145. i++;
  146. }
  147. return i;
  148. }
  149. /*
  150. * Curl_printable_address() returns a printable version of the 1st address
  151. * given in the 'ai' argument. The result will be stored in the buf that is
  152. * bufsize bytes big.
  153. *
  154. * If the conversion fails, it returns NULL.
  155. */
  156. const char *
  157. Curl_printable_address(const Curl_addrinfo *ai, char *buf, size_t bufsize)
  158. {
  159. const struct sockaddr_in *sa4;
  160. const struct in_addr *ipaddr4;
  161. #ifdef ENABLE_IPV6
  162. const struct sockaddr_in6 *sa6;
  163. const struct in6_addr *ipaddr6;
  164. #endif
  165. switch (ai->ai_family) {
  166. case AF_INET:
  167. sa4 = (const void *)ai->ai_addr;
  168. ipaddr4 = &sa4->sin_addr;
  169. return Curl_inet_ntop(ai->ai_family, (const void *)ipaddr4, buf,
  170. bufsize);
  171. #ifdef ENABLE_IPV6
  172. case AF_INET6:
  173. sa6 = (const void *)ai->ai_addr;
  174. ipaddr6 = &sa6->sin6_addr;
  175. return Curl_inet_ntop(ai->ai_family, (const void *)ipaddr6, buf,
  176. bufsize);
  177. #endif
  178. default:
  179. break;
  180. }
  181. return NULL;
  182. }
  183. /*
  184. * Return a hostcache id string for the provided host + port, to be used by
  185. * the DNS caching.
  186. */
  187. static char *
  188. create_hostcache_id(const char *name, int port)
  189. {
  190. /* create and return the new allocated entry */
  191. char *id = aprintf("%s:%d", name, port);
  192. char *ptr = id;
  193. if(ptr) {
  194. /* lower case the name part */
  195. while(*ptr && (*ptr != ':')) {
  196. *ptr = (char)TOLOWER(*ptr);
  197. ptr++;
  198. }
  199. }
  200. return id;
  201. }
  202. struct hostcache_prune_data {
  203. long cache_timeout;
  204. time_t now;
  205. };
  206. /*
  207. * This function is set as a callback to be called for every entry in the DNS
  208. * cache when we want to prune old unused entries.
  209. *
  210. * Returning non-zero means remove the entry, return 0 to keep it in the
  211. * cache.
  212. */
  213. static int
  214. hostcache_timestamp_remove(void *datap, void *hc)
  215. {
  216. struct hostcache_prune_data *data =
  217. (struct hostcache_prune_data *) datap;
  218. struct Curl_dns_entry *c = (struct Curl_dns_entry *) hc;
  219. return !c->inuse && (data->now - c->timestamp >= data->cache_timeout);
  220. }
  221. /*
  222. * Prune the DNS cache. This assumes that a lock has already been taken.
  223. */
  224. static void
  225. hostcache_prune(struct curl_hash *hostcache, long cache_timeout, time_t now)
  226. {
  227. struct hostcache_prune_data user;
  228. user.cache_timeout = cache_timeout;
  229. user.now = now;
  230. Curl_hash_clean_with_criterium(hostcache,
  231. (void *) &user,
  232. hostcache_timestamp_remove);
  233. }
  234. /*
  235. * Library-wide function for pruning the DNS cache. This function takes and
  236. * returns the appropriate locks.
  237. */
  238. void Curl_hostcache_prune(struct SessionHandle *data)
  239. {
  240. time_t now;
  241. if((data->set.dns_cache_timeout == -1) || !data->dns.hostcache)
  242. /* cache forever means never prune, and NULL hostcache means
  243. we can't do it */
  244. return;
  245. if(data->share)
  246. Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
  247. time(&now);
  248. /* Remove outdated and unused entries from the hostcache */
  249. hostcache_prune(data->dns.hostcache,
  250. data->set.dns_cache_timeout,
  251. now);
  252. if(data->share)
  253. Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
  254. }
  255. /*
  256. * Check if the entry should be pruned. Assumes a locked cache.
  257. */
  258. static int
  259. remove_entry_if_stale(struct SessionHandle *data, struct Curl_dns_entry *dns)
  260. {
  261. struct hostcache_prune_data user;
  262. if(!dns || (data->set.dns_cache_timeout == -1) || !data->dns.hostcache ||
  263. dns->inuse)
  264. /* cache forever means never prune, and NULL hostcache means we can't do
  265. it, if it still is in use then we leave it */
  266. return 0;
  267. time(&user.now);
  268. user.cache_timeout = data->set.dns_cache_timeout;
  269. if(!hostcache_timestamp_remove(&user,dns) )
  270. return 0;
  271. Curl_hash_clean_with_criterium(data->dns.hostcache,
  272. (void *) &user,
  273. hostcache_timestamp_remove);
  274. return 1;
  275. }
  276. #ifdef HAVE_SIGSETJMP
  277. /* Beware this is a global and unique instance. This is used to store the
  278. return address that we can jump back to from inside a signal handler. This
  279. is not thread-safe stuff. */
  280. sigjmp_buf curl_jmpenv;
  281. #endif
  282. /*
  283. * Curl_fetch_addr() fetches a 'Curl_dns_entry' already in the DNS cache.
  284. *
  285. * Curl_resolv() checks initially and multi_runsingle() checks each time
  286. * it discovers the handle in the state WAITRESOLVE whether the hostname
  287. * has already been resolved and the address has already been stored in
  288. * the DNS cache. This short circuits waiting for a lot of pending
  289. * lookups for the same hostname requested by different handles.
  290. *
  291. * Returns the Curl_dns_entry entry pointer or NULL if not in the cache.
  292. */
  293. struct Curl_dns_entry *
  294. Curl_fetch_addr(struct connectdata *conn,
  295. const char *hostname,
  296. int port)
  297. {
  298. char *entry_id = NULL;
  299. struct Curl_dns_entry *dns = NULL;
  300. size_t entry_len;
  301. struct SessionHandle *data = conn->data;
  302. int stale;
  303. /* Create an entry id, based upon the hostname and port */
  304. entry_id = create_hostcache_id(hostname, port);
  305. /* If we can't create the entry id, fail */
  306. if(!entry_id)
  307. return dns;
  308. entry_len = strlen(entry_id);
  309. /* See if its already in our dns cache */
  310. dns = Curl_hash_pick(data->dns.hostcache, entry_id, entry_len+1);
  311. /* free the allocated entry_id again */
  312. free(entry_id);
  313. /* See whether the returned entry is stale. Done before we release lock */
  314. stale = remove_entry_if_stale(data, dns);
  315. if(stale) {
  316. infof(data, "Hostname in DNS cache was stale, zapped\n");
  317. dns = NULL; /* the memory deallocation is being handled by the hash */
  318. }
  319. return dns;
  320. }
  321. /*
  322. * Curl_cache_addr() stores a 'Curl_addrinfo' struct in the DNS cache.
  323. *
  324. * When calling Curl_resolv() has resulted in a response with a returned
  325. * address, we call this function to store the information in the dns
  326. * cache etc
  327. *
  328. * Returns the Curl_dns_entry entry pointer or NULL if the storage failed.
  329. */
  330. struct Curl_dns_entry *
  331. Curl_cache_addr(struct SessionHandle *data,
  332. Curl_addrinfo *addr,
  333. const char *hostname,
  334. int port)
  335. {
  336. char *entry_id;
  337. size_t entry_len;
  338. struct Curl_dns_entry *dns;
  339. struct Curl_dns_entry *dns2;
  340. /* Create an entry id, based upon the hostname and port */
  341. entry_id = create_hostcache_id(hostname, port);
  342. /* If we can't create the entry id, fail */
  343. if(!entry_id)
  344. return NULL;
  345. entry_len = strlen(entry_id);
  346. /* Create a new cache entry */
  347. dns = calloc(1, sizeof(struct Curl_dns_entry));
  348. if(!dns) {
  349. free(entry_id);
  350. return NULL;
  351. }
  352. dns->inuse = 0; /* init to not used */
  353. dns->addr = addr; /* this is the address(es) */
  354. time(&dns->timestamp);
  355. if(dns->timestamp == 0)
  356. dns->timestamp = 1; /* zero indicates that entry isn't in hash table */
  357. /* Store the resolved data in our DNS cache. */
  358. dns2 = Curl_hash_add(data->dns.hostcache, entry_id, entry_len+1,
  359. (void *)dns);
  360. if(!dns2) {
  361. free(dns);
  362. free(entry_id);
  363. return NULL;
  364. }
  365. dns = dns2;
  366. dns->inuse++; /* mark entry as in-use */
  367. /* free the allocated entry_id */
  368. free(entry_id);
  369. return dns;
  370. }
  371. /*
  372. * Curl_resolv() is the main name resolve function within libcurl. It resolves
  373. * a name and returns a pointer to the entry in the 'entry' argument (if one
  374. * is provided). This function might return immediately if we're using asynch
  375. * resolves. See the return codes.
  376. *
  377. * The cache entry we return will get its 'inuse' counter increased when this
  378. * function is used. You MUST call Curl_resolv_unlock() later (when you're
  379. * done using this struct) to decrease the counter again.
  380. *
  381. * In debug mode, we specifically test for an interface name "LocalHost"
  382. * and resolve "localhost" instead as a means to permit test cases
  383. * to connect to a local test server with any host name.
  384. *
  385. * Return codes:
  386. *
  387. * CURLRESOLV_ERROR (-1) = error, no pointer
  388. * CURLRESOLV_RESOLVED (0) = OK, pointer provided
  389. * CURLRESOLV_PENDING (1) = waiting for response, no pointer
  390. */
  391. int Curl_resolv(struct connectdata *conn,
  392. const char *hostname,
  393. int port,
  394. struct Curl_dns_entry **entry)
  395. {
  396. struct Curl_dns_entry *dns = NULL;
  397. struct SessionHandle *data = conn->data;
  398. CURLcode result;
  399. int rc = CURLRESOLV_ERROR; /* default to failure */
  400. *entry = NULL;
  401. if(data->share)
  402. Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
  403. dns = Curl_fetch_addr(conn, hostname, port);
  404. if(dns) {
  405. infof(data, "Hostname %s was found in DNS cache\n", hostname);
  406. dns->inuse++; /* we use it! */
  407. rc = CURLRESOLV_RESOLVED;
  408. }
  409. if(data->share)
  410. Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
  411. if(!dns) {
  412. /* The entry was not in the cache. Resolve it to IP address */
  413. Curl_addrinfo *addr;
  414. int respwait;
  415. /* Check what IP specifics the app has requested and if we can provide it.
  416. * If not, bail out. */
  417. if(!Curl_ipvalid(conn))
  418. return CURLRESOLV_ERROR;
  419. /* If Curl_getaddrinfo() returns NULL, 'respwait' might be set to a
  420. non-zero value indicating that we need to wait for the response to the
  421. resolve call */
  422. addr = Curl_getaddrinfo(conn,
  423. #ifdef DEBUGBUILD
  424. (data->set.str[STRING_DEVICE]
  425. && !strcmp(data->set.str[STRING_DEVICE],
  426. "LocalHost"))?"localhost":
  427. #endif
  428. hostname, port, &respwait);
  429. if(!addr) {
  430. if(respwait) {
  431. /* the response to our resolve call will come asynchronously at
  432. a later time, good or bad */
  433. /* First, check that we haven't received the info by now */
  434. result = Curl_resolver_is_resolved(conn, &dns);
  435. if(result) /* error detected */
  436. return CURLRESOLV_ERROR;
  437. if(dns)
  438. rc = CURLRESOLV_RESOLVED; /* pointer provided */
  439. else
  440. rc = CURLRESOLV_PENDING; /* no info yet */
  441. }
  442. }
  443. else {
  444. if(data->share)
  445. Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
  446. /* we got a response, store it in the cache */
  447. dns = Curl_cache_addr(data, addr, hostname, port);
  448. if(data->share)
  449. Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
  450. if(!dns)
  451. /* returned failure, bail out nicely */
  452. Curl_freeaddrinfo(addr);
  453. else
  454. rc = CURLRESOLV_RESOLVED;
  455. }
  456. }
  457. *entry = dns;
  458. return rc;
  459. }
  460. #ifdef USE_ALARM_TIMEOUT
  461. /*
  462. * This signal handler jumps back into the main libcurl code and continues
  463. * execution. This effectively causes the remainder of the application to run
  464. * within a signal handler which is nonportable and could lead to problems.
  465. */
  466. static
  467. RETSIGTYPE alarmfunc(int sig)
  468. {
  469. /* this is for "-ansi -Wall -pedantic" to stop complaining! (rabe) */
  470. (void)sig;
  471. siglongjmp(curl_jmpenv, 1);
  472. return;
  473. }
  474. #endif /* USE_ALARM_TIMEOUT */
  475. /*
  476. * Curl_resolv_timeout() is the same as Curl_resolv() but specifies a
  477. * timeout. This function might return immediately if we're using asynch
  478. * resolves. See the return codes.
  479. *
  480. * The cache entry we return will get its 'inuse' counter increased when this
  481. * function is used. You MUST call Curl_resolv_unlock() later (when you're
  482. * done using this struct) to decrease the counter again.
  483. *
  484. * If built with a synchronous resolver and use of signals is not
  485. * disabled by the application, then a nonzero timeout will cause a
  486. * timeout after the specified number of milliseconds. Otherwise, timeout
  487. * is ignored.
  488. *
  489. * Return codes:
  490. *
  491. * CURLRESOLV_TIMEDOUT(-2) = warning, time too short or previous alarm expired
  492. * CURLRESOLV_ERROR (-1) = error, no pointer
  493. * CURLRESOLV_RESOLVED (0) = OK, pointer provided
  494. * CURLRESOLV_PENDING (1) = waiting for response, no pointer
  495. */
  496. int Curl_resolv_timeout(struct connectdata *conn,
  497. const char *hostname,
  498. int port,
  499. struct Curl_dns_entry **entry,
  500. long timeoutms)
  501. {
  502. #ifdef USE_ALARM_TIMEOUT
  503. #ifdef HAVE_SIGACTION
  504. struct sigaction keep_sigact; /* store the old struct here */
  505. volatile bool keep_copysig = FALSE; /* wether old sigact has been saved */
  506. struct sigaction sigact;
  507. #else
  508. #ifdef HAVE_SIGNAL
  509. void (*keep_sigact)(int); /* store the old handler here */
  510. #endif /* HAVE_SIGNAL */
  511. #endif /* HAVE_SIGACTION */
  512. volatile long timeout;
  513. volatile unsigned int prev_alarm = 0;
  514. struct SessionHandle *data = conn->data;
  515. #endif /* USE_ALARM_TIMEOUT */
  516. int rc;
  517. *entry = NULL;
  518. if(timeoutms < 0)
  519. /* got an already expired timeout */
  520. return CURLRESOLV_TIMEDOUT;
  521. #ifdef USE_ALARM_TIMEOUT
  522. if(data->set.no_signal)
  523. /* Ignore the timeout when signals are disabled */
  524. timeout = 0;
  525. else
  526. timeout = timeoutms;
  527. if(!timeout)
  528. /* USE_ALARM_TIMEOUT defined, but no timeout actually requested */
  529. return Curl_resolv(conn, hostname, port, entry);
  530. if(timeout < 1000)
  531. /* The alarm() function only provides integer second resolution, so if
  532. we want to wait less than one second we must bail out already now. */
  533. return CURLRESOLV_TIMEDOUT;
  534. /*************************************************************
  535. * Set signal handler to catch SIGALRM
  536. * Store the old value to be able to set it back later!
  537. *************************************************************/
  538. #ifdef HAVE_SIGACTION
  539. sigaction(SIGALRM, NULL, &sigact);
  540. keep_sigact = sigact;
  541. keep_copysig = TRUE; /* yes, we have a copy */
  542. sigact.sa_handler = alarmfunc;
  543. #ifdef SA_RESTART
  544. /* HPUX doesn't have SA_RESTART but defaults to that behaviour! */
  545. sigact.sa_flags &= ~SA_RESTART;
  546. #endif
  547. /* now set the new struct */
  548. sigaction(SIGALRM, &sigact, NULL);
  549. #else /* HAVE_SIGACTION */
  550. /* no sigaction(), revert to the much lamer signal() */
  551. #ifdef HAVE_SIGNAL
  552. keep_sigact = signal(SIGALRM, alarmfunc);
  553. #endif
  554. #endif /* HAVE_SIGACTION */
  555. /* alarm() makes a signal get sent when the timeout fires off, and that
  556. will abort system calls */
  557. prev_alarm = alarm(curlx_sltoui(timeout/1000L));
  558. /* This allows us to time-out from the name resolver, as the timeout
  559. will generate a signal and we will siglongjmp() from that here.
  560. This technique has problems (see alarmfunc).
  561. This should be the last thing we do before calling Curl_resolv(),
  562. as otherwise we'd have to worry about variables that get modified
  563. before we invoke Curl_resolv() (and thus use "volatile"). */
  564. if(sigsetjmp(curl_jmpenv, 1)) {
  565. /* this is coming from a siglongjmp() after an alarm signal */
  566. failf(data, "name lookup timed out");
  567. rc = CURLRESOLV_ERROR;
  568. goto clean_up;
  569. }
  570. #else
  571. #ifndef CURLRES_ASYNCH
  572. if(timeoutms)
  573. infof(conn->data, "timeout on name lookup is not supported\n");
  574. #else
  575. (void)timeoutms; /* timeoutms not used with an async resolver */
  576. #endif
  577. #endif /* USE_ALARM_TIMEOUT */
  578. /* Perform the actual name resolution. This might be interrupted by an
  579. * alarm if it takes too long.
  580. */
  581. rc = Curl_resolv(conn, hostname, port, entry);
  582. #ifdef USE_ALARM_TIMEOUT
  583. clean_up:
  584. if(!prev_alarm)
  585. /* deactivate a possibly active alarm before uninstalling the handler */
  586. alarm(0);
  587. #ifdef HAVE_SIGACTION
  588. if(keep_copysig) {
  589. /* we got a struct as it looked before, now put that one back nice
  590. and clean */
  591. sigaction(SIGALRM, &keep_sigact, NULL); /* put it back */
  592. }
  593. #else
  594. #ifdef HAVE_SIGNAL
  595. /* restore the previous SIGALRM handler */
  596. signal(SIGALRM, keep_sigact);
  597. #endif
  598. #endif /* HAVE_SIGACTION */
  599. /* switch back the alarm() to either zero or to what it was before minus
  600. the time we spent until now! */
  601. if(prev_alarm) {
  602. /* there was an alarm() set before us, now put it back */
  603. unsigned long elapsed_ms = Curl_tvdiff(Curl_tvnow(), conn->created);
  604. /* the alarm period is counted in even number of seconds */
  605. unsigned long alarm_set = prev_alarm - elapsed_ms/1000;
  606. if(!alarm_set ||
  607. ((alarm_set >= 0x80000000) && (prev_alarm < 0x80000000)) ) {
  608. /* if the alarm time-left reached zero or turned "negative" (counted
  609. with unsigned values), we should fire off a SIGALRM here, but we
  610. won't, and zero would be to switch it off so we never set it to
  611. less than 1! */
  612. alarm(1);
  613. rc = CURLRESOLV_TIMEDOUT;
  614. failf(data, "Previous alarm fired off!");
  615. }
  616. else
  617. alarm((unsigned int)alarm_set);
  618. }
  619. #endif /* USE_ALARM_TIMEOUT */
  620. return rc;
  621. }
  622. /*
  623. * Curl_resolv_unlock() unlocks the given cached DNS entry. When this has been
  624. * made, the struct may be destroyed due to pruning. It is important that only
  625. * one unlock is made for each Curl_resolv() call.
  626. *
  627. * May be called with 'data' == NULL for global cache.
  628. */
  629. void Curl_resolv_unlock(struct SessionHandle *data, struct Curl_dns_entry *dns)
  630. {
  631. DEBUGASSERT(dns && (dns->inuse>0));
  632. if(data && data->share)
  633. Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
  634. dns->inuse--;
  635. /* only free if nobody is using AND it is not in hostcache (timestamp ==
  636. 0) */
  637. if(dns->inuse == 0 && dns->timestamp == 0) {
  638. Curl_freeaddrinfo(dns->addr);
  639. free(dns);
  640. }
  641. if(data && data->share)
  642. Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
  643. }
  644. /*
  645. * File-internal: free a cache dns entry.
  646. */
  647. static void freednsentry(void *freethis)
  648. {
  649. struct Curl_dns_entry *p = (struct Curl_dns_entry *) freethis;
  650. /* mark the entry as not in hostcache */
  651. p->timestamp = 0;
  652. if(p->inuse == 0) {
  653. Curl_freeaddrinfo(p->addr);
  654. free(p);
  655. }
  656. }
  657. /*
  658. * Curl_mk_dnscache() creates a new DNS cache and returns the handle for it.
  659. */
  660. struct curl_hash *Curl_mk_dnscache(void)
  661. {
  662. return Curl_hash_alloc(7, Curl_hash_str, Curl_str_key_compare, freednsentry);
  663. }
  664. static int hostcache_inuse(void *data, void *hc)
  665. {
  666. struct Curl_dns_entry *c = (struct Curl_dns_entry *) hc;
  667. if(c->inuse == 1)
  668. Curl_resolv_unlock(data, c);
  669. return 1; /* free all entries */
  670. }
  671. /*
  672. * Curl_hostcache_clean()
  673. *
  674. * This _can_ be called with 'data' == NULL but then of course no locking
  675. * can be done!
  676. */
  677. void Curl_hostcache_clean(struct SessionHandle *data,
  678. struct curl_hash *hash)
  679. {
  680. /* Entries added to the hostcache with the CURLOPT_RESOLVE function are
  681. * still present in the cache with the inuse counter set to 1. Detect them
  682. * and cleanup!
  683. */
  684. Curl_hash_clean_with_criterium(hash, data, hostcache_inuse);
  685. }
  686. CURLcode Curl_loadhostpairs(struct SessionHandle *data)
  687. {
  688. struct curl_slist *hostp;
  689. char hostname[256];
  690. char address[256];
  691. int port;
  692. for(hostp = data->change.resolve; hostp; hostp = hostp->next ) {
  693. if(!hostp->data)
  694. continue;
  695. if(hostp->data[0] == '-') {
  696. /* TODO: mark an entry for removal */
  697. }
  698. else if(3 == sscanf(hostp->data, "%255[^:]:%d:%255s", hostname, &port,
  699. address)) {
  700. struct Curl_dns_entry *dns;
  701. Curl_addrinfo *addr;
  702. char *entry_id;
  703. size_t entry_len;
  704. addr = Curl_str2addr(address, port);
  705. if(!addr) {
  706. infof(data, "Resolve %s found illegal!\n", hostp->data);
  707. continue;
  708. }
  709. /* Create an entry id, based upon the hostname and port */
  710. entry_id = create_hostcache_id(hostname, port);
  711. /* If we can't create the entry id, fail */
  712. if(!entry_id) {
  713. Curl_freeaddrinfo(addr);
  714. return CURLE_OUT_OF_MEMORY;
  715. }
  716. entry_len = strlen(entry_id);
  717. if(data->share)
  718. Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
  719. /* See if its already in our dns cache */
  720. dns = Curl_hash_pick(data->dns.hostcache, entry_id, entry_len+1);
  721. /* free the allocated entry_id again */
  722. free(entry_id);
  723. if(!dns)
  724. /* if not in the cache already, put this host in the cache */
  725. dns = Curl_cache_addr(data, addr, hostname, port);
  726. else
  727. /* this is a duplicate, free it again */
  728. Curl_freeaddrinfo(addr);
  729. if(data->share)
  730. Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
  731. if(!dns) {
  732. Curl_freeaddrinfo(addr);
  733. return CURLE_OUT_OF_MEMORY;
  734. }
  735. infof(data, "Added %s:%d:%s to DNS cache\n",
  736. hostname, port, address);
  737. }
  738. }
  739. data->change.resolve = NULL; /* dealt with now */
  740. return CURLE_OK;
  741. }