hostip.c 21 KB

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