hostip.c 21 KB

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