hostip.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  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 provided host + port, to be used by
  187. * the DNS caching.
  188. */
  189. static char *
  190. create_hostcache_id(const char *name, int port)
  191. {
  192. /* create and return the new allocated entry */
  193. char *id = aprintf("%s:%d", name, port);
  194. char *ptr = id;
  195. if(ptr) {
  196. /* lower case the name part */
  197. while(*ptr && (*ptr != ':')) {
  198. *ptr = (char)TOLOWER(*ptr);
  199. ptr++;
  200. }
  201. }
  202. return id;
  203. }
  204. struct hostcache_prune_data {
  205. long cache_timeout;
  206. time_t now;
  207. };
  208. /*
  209. * This function is set as a callback to be called for every entry in the DNS
  210. * cache when we want to prune old unused entries.
  211. *
  212. * Returning non-zero means remove the entry, return 0 to keep it in the
  213. * cache.
  214. */
  215. static int
  216. hostcache_timestamp_remove(void *datap, void *hc)
  217. {
  218. struct hostcache_prune_data *data =
  219. (struct hostcache_prune_data *) datap;
  220. struct Curl_dns_entry *c = (struct Curl_dns_entry *) hc;
  221. return (data->now - c->timestamp >= data->cache_timeout);
  222. }
  223. /*
  224. * Prune the DNS cache. This assumes that a lock has already been taken.
  225. */
  226. static void
  227. hostcache_prune(struct curl_hash *hostcache, long cache_timeout, time_t now)
  228. {
  229. struct hostcache_prune_data user;
  230. user.cache_timeout = cache_timeout;
  231. user.now = now;
  232. Curl_hash_clean_with_criterium(hostcache,
  233. (void *) &user,
  234. hostcache_timestamp_remove);
  235. }
  236. /*
  237. * Library-wide function for pruning the DNS cache. This function takes and
  238. * returns the appropriate locks.
  239. */
  240. void Curl_hostcache_prune(struct SessionHandle *data)
  241. {
  242. time_t now;
  243. if((data->set.dns_cache_timeout == -1) || !data->dns.hostcache)
  244. /* cache forever means never prune, and NULL hostcache means
  245. we can't do it */
  246. return;
  247. if(data->share)
  248. Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
  249. time(&now);
  250. /* Remove outdated and unused entries from the hostcache */
  251. hostcache_prune(data->dns.hostcache,
  252. data->set.dns_cache_timeout,
  253. now);
  254. if(data->share)
  255. Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
  256. }
  257. /*
  258. * Check if the entry should be pruned. Assumes a locked cache.
  259. */
  260. static int
  261. remove_entry_if_stale(struct SessionHandle *data, struct Curl_dns_entry *dns)
  262. {
  263. struct hostcache_prune_data user;
  264. if(!dns || (data->set.dns_cache_timeout == -1) || !data->dns.hostcache)
  265. /* cache forever means never prune, and NULL hostcache means
  266. we can't do it */
  267. return 0;
  268. time(&user.now);
  269. user.cache_timeout = data->set.dns_cache_timeout;
  270. if(!hostcache_timestamp_remove(&user,dns) )
  271. return 0;
  272. Curl_hash_clean_with_criterium(data->dns.hostcache,
  273. (void *) &user,
  274. hostcache_timestamp_remove);
  275. return 1;
  276. }
  277. #ifdef HAVE_SIGSETJMP
  278. /* Beware this is a global and unique instance. This is used to store the
  279. return address that we can jump back to from inside a signal handler. This
  280. is not thread-safe stuff. */
  281. sigjmp_buf curl_jmpenv;
  282. #endif
  283. /*
  284. * Curl_cache_addr() stores a 'Curl_addrinfo' struct in the DNS cache.
  285. *
  286. * When calling Curl_resolv() has resulted in a response with a returned
  287. * address, we call this function to store the information in the dns
  288. * cache etc
  289. *
  290. * Returns the Curl_dns_entry entry pointer or NULL if the storage failed.
  291. */
  292. struct Curl_dns_entry *
  293. Curl_cache_addr(struct SessionHandle *data,
  294. Curl_addrinfo *addr,
  295. const char *hostname,
  296. int port)
  297. {
  298. char *entry_id;
  299. size_t entry_len;
  300. struct Curl_dns_entry *dns;
  301. struct Curl_dns_entry *dns2;
  302. /* Create an entry id, based upon the hostname and port */
  303. entry_id = create_hostcache_id(hostname, port);
  304. /* If we can't create the entry id, fail */
  305. if(!entry_id)
  306. return NULL;
  307. entry_len = strlen(entry_id);
  308. /* Create a new cache entry */
  309. dns = calloc(1, sizeof(struct Curl_dns_entry));
  310. if(!dns) {
  311. free(entry_id);
  312. return NULL;
  313. }
  314. dns->inuse = 0; /* init to not used */
  315. dns->addr = addr; /* this is the address(es) */
  316. time(&dns->timestamp);
  317. if(dns->timestamp == 0)
  318. dns->timestamp = 1; /* zero indicates that entry isn't in hash table */
  319. /* Store the resolved data in our DNS cache. */
  320. dns2 = Curl_hash_add(data->dns.hostcache, entry_id, entry_len+1,
  321. (void *)dns);
  322. if(!dns2) {
  323. free(dns);
  324. free(entry_id);
  325. return NULL;
  326. }
  327. dns = dns2;
  328. dns->inuse++; /* mark entry as in-use */
  329. /* free the allocated entry_id */
  330. free(entry_id);
  331. return dns;
  332. }
  333. /*
  334. * Curl_resolv() is the main name resolve function within libcurl. It resolves
  335. * a name and returns a pointer to the entry in the 'entry' argument (if one
  336. * is provided). This function might return immediately if we're using asynch
  337. * resolves. See the return codes.
  338. *
  339. * The cache entry we return will get its 'inuse' counter increased when this
  340. * function is used. You MUST call Curl_resolv_unlock() later (when you're
  341. * done using this struct) to decrease the counter again.
  342. *
  343. * In debug mode, we specifically test for an interface name "LocalHost"
  344. * and resolve "localhost" instead as a means to permit test cases
  345. * to connect to a local test server with any host name.
  346. *
  347. * Return codes:
  348. *
  349. * CURLRESOLV_ERROR (-1) = error, no pointer
  350. * CURLRESOLV_RESOLVED (0) = OK, pointer provided
  351. * CURLRESOLV_PENDING (1) = waiting for response, no pointer
  352. */
  353. int Curl_resolv(struct connectdata *conn,
  354. const char *hostname,
  355. int port,
  356. struct Curl_dns_entry **entry)
  357. {
  358. char *entry_id = NULL;
  359. struct Curl_dns_entry *dns = NULL;
  360. size_t entry_len;
  361. struct SessionHandle *data = conn->data;
  362. CURLcode result;
  363. int rc = CURLRESOLV_ERROR; /* default to failure */
  364. *entry = NULL;
  365. /* Create an entry id, based upon the hostname and port */
  366. entry_id = create_hostcache_id(hostname, port);
  367. /* If we can't create the entry id, fail */
  368. if(!entry_id)
  369. return rc;
  370. entry_len = strlen(entry_id);
  371. if(data->share)
  372. Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
  373. /* See if its already in our dns cache */
  374. dns = Curl_hash_pick(data->dns.hostcache, entry_id, entry_len+1);
  375. /* free the allocated entry_id again */
  376. free(entry_id);
  377. /* See whether the returned entry is stale. Done before we release lock */
  378. if(remove_entry_if_stale(data, dns))
  379. dns = NULL; /* the memory deallocation is being handled by the hash */
  380. if(dns) {
  381. dns->inuse++; /* we use it! */
  382. rc = CURLRESOLV_RESOLVED;
  383. }
  384. if(data->share)
  385. Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
  386. if(!dns) {
  387. /* The entry was not in the cache. Resolve it to IP address */
  388. Curl_addrinfo *addr;
  389. int respwait;
  390. /* Check what IP specifics the app has requested and if we can provide it.
  391. * If not, bail out. */
  392. if(!Curl_ipvalid(conn))
  393. return CURLRESOLV_ERROR;
  394. /* If Curl_getaddrinfo() returns NULL, 'respwait' might be set to a
  395. non-zero value indicating that we need to wait for the response to the
  396. resolve call */
  397. addr = Curl_getaddrinfo(conn,
  398. #ifdef DEBUGBUILD
  399. (data->set.str[STRING_DEVICE]
  400. && !strcmp(data->set.str[STRING_DEVICE],
  401. "LocalHost"))?"localhost":
  402. #endif
  403. hostname, port, &respwait);
  404. if(!addr) {
  405. if(respwait) {
  406. /* the response to our resolve call will come asynchronously at
  407. a later time, good or bad */
  408. /* First, check that we haven't received the info by now */
  409. result = Curl_resolver_is_resolved(conn, &dns);
  410. if(result) /* error detected */
  411. return CURLRESOLV_ERROR;
  412. if(dns)
  413. rc = CURLRESOLV_RESOLVED; /* pointer provided */
  414. else
  415. rc = CURLRESOLV_PENDING; /* no info yet */
  416. }
  417. }
  418. else {
  419. if(data->share)
  420. Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
  421. /* we got a response, store it in the cache */
  422. dns = Curl_cache_addr(data, addr, hostname, port);
  423. if(data->share)
  424. Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
  425. if(!dns)
  426. /* returned failure, bail out nicely */
  427. Curl_freeaddrinfo(addr);
  428. else
  429. rc = CURLRESOLV_RESOLVED;
  430. }
  431. }
  432. *entry = dns;
  433. return rc;
  434. }
  435. #ifdef USE_ALARM_TIMEOUT
  436. /*
  437. * This signal handler jumps back into the main libcurl code and continues
  438. * execution. This effectively causes the remainder of the application to run
  439. * within a signal handler which is nonportable and could lead to problems.
  440. */
  441. static
  442. RETSIGTYPE alarmfunc(int sig)
  443. {
  444. /* this is for "-ansi -Wall -pedantic" to stop complaining! (rabe) */
  445. (void)sig;
  446. siglongjmp(curl_jmpenv, 1);
  447. return;
  448. }
  449. #endif /* USE_ALARM_TIMEOUT */
  450. /*
  451. * Curl_resolv_timeout() is the same as Curl_resolv() but specifies a
  452. * timeout. This function might return immediately if we're using asynch
  453. * resolves. See the return codes.
  454. *
  455. * The cache entry we return will get its 'inuse' counter increased when this
  456. * function is used. You MUST call Curl_resolv_unlock() later (when you're
  457. * done using this struct) to decrease the counter again.
  458. *
  459. * If built with a synchronous resolver and use of signals is not
  460. * disabled by the application, then a nonzero timeout will cause a
  461. * timeout after the specified number of milliseconds. Otherwise, timeout
  462. * is ignored.
  463. *
  464. * Return codes:
  465. *
  466. * CURLRESOLV_TIMEDOUT(-2) = warning, time too short or previous alarm expired
  467. * CURLRESOLV_ERROR (-1) = error, no pointer
  468. * CURLRESOLV_RESOLVED (0) = OK, pointer provided
  469. * CURLRESOLV_PENDING (1) = waiting for response, no pointer
  470. */
  471. int Curl_resolv_timeout(struct connectdata *conn,
  472. const char *hostname,
  473. int port,
  474. struct Curl_dns_entry **entry,
  475. long timeoutms)
  476. {
  477. #ifdef USE_ALARM_TIMEOUT
  478. #ifdef HAVE_SIGACTION
  479. struct sigaction keep_sigact; /* store the old struct here */
  480. volatile bool keep_copysig = FALSE; /* wether old sigact has been saved */
  481. struct sigaction sigact;
  482. #else
  483. #ifdef HAVE_SIGNAL
  484. void (*keep_sigact)(int); /* store the old handler here */
  485. #endif /* HAVE_SIGNAL */
  486. #endif /* HAVE_SIGACTION */
  487. volatile long timeout;
  488. volatile unsigned int prev_alarm = 0;
  489. struct SessionHandle *data = conn->data;
  490. #endif /* USE_ALARM_TIMEOUT */
  491. int rc;
  492. *entry = NULL;
  493. if(timeoutms < 0)
  494. /* got an already expired timeout */
  495. return CURLRESOLV_TIMEDOUT;
  496. #ifdef USE_ALARM_TIMEOUT
  497. if(data->set.no_signal)
  498. /* Ignore the timeout when signals are disabled */
  499. timeout = 0;
  500. else
  501. timeout = timeoutms;
  502. if(!timeout)
  503. /* USE_ALARM_TIMEOUT defined, but no timeout actually requested */
  504. return Curl_resolv(conn, hostname, port, entry);
  505. if(timeout < 1000)
  506. /* The alarm() function only provides integer second resolution, so if
  507. we want to wait less than one second we must bail out already now. */
  508. return CURLRESOLV_TIMEDOUT;
  509. /*************************************************************
  510. * Set signal handler to catch SIGALRM
  511. * Store the old value to be able to set it back later!
  512. *************************************************************/
  513. #ifdef HAVE_SIGACTION
  514. sigaction(SIGALRM, NULL, &sigact);
  515. keep_sigact = sigact;
  516. keep_copysig = TRUE; /* yes, we have a copy */
  517. sigact.sa_handler = alarmfunc;
  518. #ifdef SA_RESTART
  519. /* HPUX doesn't have SA_RESTART but defaults to that behaviour! */
  520. sigact.sa_flags &= ~SA_RESTART;
  521. #endif
  522. /* now set the new struct */
  523. sigaction(SIGALRM, &sigact, NULL);
  524. #else /* HAVE_SIGACTION */
  525. /* no sigaction(), revert to the much lamer signal() */
  526. #ifdef HAVE_SIGNAL
  527. keep_sigact = signal(SIGALRM, alarmfunc);
  528. #endif
  529. #endif /* HAVE_SIGACTION */
  530. /* alarm() makes a signal get sent when the timeout fires off, and that
  531. will abort system calls */
  532. prev_alarm = alarm(curlx_sltoui(timeout/1000L));
  533. /* This allows us to time-out from the name resolver, as the timeout
  534. will generate a signal and we will siglongjmp() from that here.
  535. This technique has problems (see alarmfunc).
  536. This should be the last thing we do before calling Curl_resolv(),
  537. as otherwise we'd have to worry about variables that get modified
  538. before we invoke Curl_resolv() (and thus use "volatile"). */
  539. if(sigsetjmp(curl_jmpenv, 1)) {
  540. /* this is coming from a siglongjmp() after an alarm signal */
  541. failf(data, "name lookup timed out");
  542. rc = CURLRESOLV_ERROR;
  543. goto clean_up;
  544. }
  545. #else
  546. #ifndef CURLRES_ASYNCH
  547. if(timeoutms)
  548. infof(conn->data, "timeout on name lookup is not supported\n");
  549. #else
  550. (void)timeoutms; /* timeoutms not used with an async resolver */
  551. #endif
  552. #endif /* USE_ALARM_TIMEOUT */
  553. /* Perform the actual name resolution. This might be interrupted by an
  554. * alarm if it takes too long.
  555. */
  556. rc = Curl_resolv(conn, hostname, port, entry);
  557. #ifdef USE_ALARM_TIMEOUT
  558. clean_up:
  559. if(!prev_alarm)
  560. /* deactivate a possibly active alarm before uninstalling the handler */
  561. alarm(0);
  562. #ifdef HAVE_SIGACTION
  563. if(keep_copysig) {
  564. /* we got a struct as it looked before, now put that one back nice
  565. and clean */
  566. sigaction(SIGALRM, &keep_sigact, NULL); /* put it back */
  567. }
  568. #else
  569. #ifdef HAVE_SIGNAL
  570. /* restore the previous SIGALRM handler */
  571. signal(SIGALRM, keep_sigact);
  572. #endif
  573. #endif /* HAVE_SIGACTION */
  574. /* switch back the alarm() to either zero or to what it was before minus
  575. the time we spent until now! */
  576. if(prev_alarm) {
  577. /* there was an alarm() set before us, now put it back */
  578. unsigned long elapsed_ms = Curl_tvdiff(Curl_tvnow(), conn->created);
  579. /* the alarm period is counted in even number of seconds */
  580. unsigned long alarm_set = prev_alarm - elapsed_ms/1000;
  581. if(!alarm_set ||
  582. ((alarm_set >= 0x80000000) && (prev_alarm < 0x80000000)) ) {
  583. /* if the alarm time-left reached zero or turned "negative" (counted
  584. with unsigned values), we should fire off a SIGALRM here, but we
  585. won't, and zero would be to switch it off so we never set it to
  586. less than 1! */
  587. alarm(1);
  588. rc = CURLRESOLV_TIMEDOUT;
  589. failf(data, "Previous alarm fired off!");
  590. }
  591. else
  592. alarm((unsigned int)alarm_set);
  593. }
  594. #endif /* USE_ALARM_TIMEOUT */
  595. return rc;
  596. }
  597. /*
  598. * Curl_resolv_unlock() unlocks the given cached DNS entry. When this has been
  599. * made, the struct may be destroyed due to pruning. It is important that only
  600. * one unlock is made for each Curl_resolv() call.
  601. */
  602. void Curl_resolv_unlock(struct SessionHandle *data, struct Curl_dns_entry *dns)
  603. {
  604. DEBUGASSERT(dns && (dns->inuse>0));
  605. if(data->share)
  606. Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
  607. dns->inuse--;
  608. /* only free if nobody is using AND it is not in hostcache (timestamp ==
  609. 0) */
  610. if(dns->inuse == 0 && dns->timestamp == 0) {
  611. Curl_freeaddrinfo(dns->addr);
  612. free(dns);
  613. }
  614. if(data->share)
  615. Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
  616. }
  617. /*
  618. * File-internal: free a cache dns entry.
  619. */
  620. static void freednsentry(void *freethis)
  621. {
  622. struct Curl_dns_entry *p = (struct Curl_dns_entry *) freethis;
  623. /* mark the entry as not in hostcache */
  624. p->timestamp = 0;
  625. if(p->inuse == 0) {
  626. Curl_freeaddrinfo(p->addr);
  627. free(p);
  628. }
  629. }
  630. /*
  631. * Curl_mk_dnscache() creates a new DNS cache and returns the handle for it.
  632. */
  633. struct curl_hash *Curl_mk_dnscache(void)
  634. {
  635. return Curl_hash_alloc(7, Curl_hash_str, Curl_str_key_compare, freednsentry);
  636. }
  637. static int hostcache_inuse(void *data, void *hc)
  638. {
  639. struct Curl_dns_entry *c = (struct Curl_dns_entry *) hc;
  640. if(c->inuse == 1)
  641. Curl_resolv_unlock(data, c);
  642. return 1; /* free all entries */
  643. }
  644. void Curl_hostcache_destroy(struct SessionHandle *data)
  645. {
  646. /* Entries added to the hostcache with the CURLOPT_RESOLVE function are
  647. * still present in the cache with the inuse counter set to 1. Detect them
  648. * and cleanup!
  649. */
  650. Curl_hash_clean_with_criterium(data->dns.hostcache, data, hostcache_inuse);
  651. Curl_hash_destroy(data->dns.hostcache);
  652. data->dns.hostcachetype = HCACHE_NONE;
  653. data->dns.hostcache = NULL;
  654. }
  655. CURLcode Curl_loadhostpairs(struct SessionHandle *data)
  656. {
  657. struct curl_slist *hostp;
  658. char hostname[256];
  659. char address[256];
  660. int port;
  661. for(hostp = data->change.resolve; hostp; hostp = hostp->next ) {
  662. if(!hostp->data)
  663. continue;
  664. if(hostp->data[0] == '-') {
  665. /* TODO: mark an entry for removal */
  666. }
  667. else if(3 == sscanf(hostp->data, "%255[^:]:%d:%255s", hostname, &port,
  668. address)) {
  669. struct Curl_dns_entry *dns;
  670. Curl_addrinfo *addr;
  671. char *entry_id;
  672. size_t entry_len;
  673. addr = Curl_str2addr(address, port);
  674. if(!addr) {
  675. infof(data, "Resolve %s found illegal!\n", hostp->data);
  676. continue;
  677. }
  678. /* Create an entry id, based upon the hostname and port */
  679. entry_id = create_hostcache_id(hostname, port);
  680. /* If we can't create the entry id, fail */
  681. if(!entry_id) {
  682. Curl_freeaddrinfo(addr);
  683. return CURLE_OUT_OF_MEMORY;
  684. }
  685. entry_len = strlen(entry_id);
  686. if(data->share)
  687. Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
  688. /* See if its already in our dns cache */
  689. dns = Curl_hash_pick(data->dns.hostcache, entry_id, entry_len+1);
  690. /* free the allocated entry_id again */
  691. free(entry_id);
  692. if(!dns)
  693. /* if not in the cache already, put this host in the cache */
  694. dns = Curl_cache_addr(data, addr, hostname, port);
  695. else
  696. /* this is a duplicate, free it again */
  697. Curl_freeaddrinfo(addr);
  698. if(data->share)
  699. Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
  700. if(!dns) {
  701. Curl_freeaddrinfo(addr);
  702. return CURLE_OUT_OF_MEMORY;
  703. }
  704. infof(data, "Added %s:%d:%s to DNS cache\n",
  705. hostname, port, address);
  706. }
  707. }
  708. data->change.resolve = NULL; /* dealt with now */
  709. return CURLE_OK;
  710. }