hostip.c 25 KB

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