hostip.c 25 KB

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