hostip.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2017, 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 https://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. /* The last 3 #include files should be in this order */
  55. #include "curl_printf.h"
  56. #include "curl_memory.h"
  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 addresses 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 Curl_easy *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 Curl_easy *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 Curl_easy *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)
  315. dns->inuse++; /* we use it! */
  316. if(data->share)
  317. Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
  318. return dns;
  319. }
  320. /*
  321. * Curl_cache_addr() stores a 'Curl_addrinfo' struct in the DNS cache.
  322. *
  323. * When calling Curl_resolv() has resulted in a response with a returned
  324. * address, we call this function to store the information in the dns
  325. * cache etc
  326. *
  327. * Returns the Curl_dns_entry entry pointer or NULL if the storage failed.
  328. */
  329. struct Curl_dns_entry *
  330. Curl_cache_addr(struct Curl_easy *data,
  331. Curl_addrinfo *addr,
  332. const char *hostname,
  333. int port)
  334. {
  335. char *entry_id;
  336. size_t entry_len;
  337. struct Curl_dns_entry *dns;
  338. struct Curl_dns_entry *dns2;
  339. /* Create an entry id, based upon the hostname and port */
  340. entry_id = create_hostcache_id(hostname, port);
  341. /* If we can't create the entry id, fail */
  342. if(!entry_id)
  343. return NULL;
  344. entry_len = strlen(entry_id);
  345. /* Create a new cache entry */
  346. dns = calloc(1, sizeof(struct Curl_dns_entry));
  347. if(!dns) {
  348. free(entry_id);
  349. return NULL;
  350. }
  351. dns->inuse = 1; /* the cache has the first reference */
  352. dns->addr = addr; /* this is the address(es) */
  353. time(&dns->timestamp);
  354. if(dns->timestamp == 0)
  355. dns->timestamp = 1; /* zero indicates CURLOPT_RESOLVE entry */
  356. /* Store the resolved data in our DNS cache. */
  357. dns2 = Curl_hash_add(data->dns.hostcache, entry_id, entry_len+1,
  358. (void *)dns);
  359. if(!dns2) {
  360. free(dns);
  361. free(entry_id);
  362. return NULL;
  363. }
  364. dns = dns2;
  365. dns->inuse++; /* mark entry as in-use */
  366. /* free the allocated entry_id */
  367. free(entry_id);
  368. return dns;
  369. }
  370. /*
  371. * Curl_resolv() is the main name resolve function within libcurl. It resolves
  372. * a name and returns a pointer to the entry in the 'entry' argument (if one
  373. * is provided). This function might return immediately if we're using asynch
  374. * resolves. See the return codes.
  375. *
  376. * The cache entry we return will get its 'inuse' counter increased when this
  377. * function is used. You MUST call Curl_resolv_unlock() later (when you're
  378. * done using this struct) to decrease the counter again.
  379. *
  380. * In debug mode, we specifically test for an interface name "LocalHost"
  381. * and resolve "localhost" instead as a means to permit test cases
  382. * to connect to a local test server with any host name.
  383. *
  384. * Return codes:
  385. *
  386. * CURLRESOLV_ERROR (-1) = error, no pointer
  387. * CURLRESOLV_RESOLVED (0) = OK, pointer provided
  388. * CURLRESOLV_PENDING (1) = waiting for response, no pointer
  389. */
  390. int Curl_resolv(struct connectdata *conn,
  391. const char *hostname,
  392. int port,
  393. struct Curl_dns_entry **entry)
  394. {
  395. struct Curl_dns_entry *dns = NULL;
  396. struct Curl_easy *data = conn->data;
  397. CURLcode result;
  398. int rc = CURLRESOLV_ERROR; /* default to failure */
  399. *entry = NULL;
  400. if(data->share)
  401. Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
  402. dns = fetch_addr(conn, hostname, port);
  403. if(dns) {
  404. infof(data, "Hostname %s was found in DNS cache\n", hostname);
  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. }
  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. time_t 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; /* whether 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 Curl_easy *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 > LONG_MAX) ? LONG_MAX : (long)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. failf(data,
  532. "remaining timeout of %ld too small to resolve via SIGALRM method",
  533. timeout);
  534. return CURLRESOLV_TIMEDOUT;
  535. }
  536. /* This allows us to time-out from the name resolver, as the timeout
  537. will generate a signal and we will siglongjmp() from that here.
  538. This technique has problems (see alarmfunc).
  539. This should be the last thing we do before calling Curl_resolv(),
  540. as otherwise we'd have to worry about variables that get modified
  541. before we invoke Curl_resolv() (and thus use "volatile"). */
  542. if(sigsetjmp(curl_jmpenv, 1)) {
  543. /* this is coming from a siglongjmp() after an alarm signal */
  544. failf(data, "name lookup timed out");
  545. rc = CURLRESOLV_ERROR;
  546. goto clean_up;
  547. }
  548. else {
  549. /*************************************************************
  550. * Set signal handler to catch SIGALRM
  551. * Store the old value to be able to set it back later!
  552. *************************************************************/
  553. #ifdef HAVE_SIGACTION
  554. sigaction(SIGALRM, NULL, &sigact);
  555. keep_sigact = sigact;
  556. keep_copysig = TRUE; /* yes, we have a copy */
  557. sigact.sa_handler = alarmfunc;
  558. #ifdef SA_RESTART
  559. /* HPUX doesn't have SA_RESTART but defaults to that behaviour! */
  560. sigact.sa_flags &= ~SA_RESTART;
  561. #endif
  562. /* now set the new struct */
  563. sigaction(SIGALRM, &sigact, NULL);
  564. #else /* HAVE_SIGACTION */
  565. /* no sigaction(), revert to the much lamer signal() */
  566. #ifdef HAVE_SIGNAL
  567. keep_sigact = signal(SIGALRM, alarmfunc);
  568. #endif
  569. #endif /* HAVE_SIGACTION */
  570. /* alarm() makes a signal get sent when the timeout fires off, and that
  571. will abort system calls */
  572. prev_alarm = alarm(curlx_sltoui(timeout/1000L));
  573. }
  574. #else
  575. #ifndef CURLRES_ASYNCH
  576. if(timeoutms)
  577. infof(conn->data, "timeout on name lookup is not supported\n");
  578. #else
  579. (void)timeoutms; /* timeoutms not used with an async resolver */
  580. #endif
  581. #endif /* USE_ALARM_TIMEOUT */
  582. /* Perform the actual name resolution. This might be interrupted by an
  583. * alarm if it takes too long.
  584. */
  585. rc = Curl_resolv(conn, hostname, port, entry);
  586. #ifdef USE_ALARM_TIMEOUT
  587. clean_up:
  588. if(!prev_alarm)
  589. /* deactivate a possibly active alarm before uninstalling the handler */
  590. alarm(0);
  591. #ifdef HAVE_SIGACTION
  592. if(keep_copysig) {
  593. /* we got a struct as it looked before, now put that one back nice
  594. and clean */
  595. sigaction(SIGALRM, &keep_sigact, NULL); /* put it back */
  596. }
  597. #else
  598. #ifdef HAVE_SIGNAL
  599. /* restore the previous SIGALRM handler */
  600. signal(SIGALRM, keep_sigact);
  601. #endif
  602. #endif /* HAVE_SIGACTION */
  603. /* switch back the alarm() to either zero or to what it was before minus
  604. the time we spent until now! */
  605. if(prev_alarm) {
  606. /* there was an alarm() set before us, now put it back */
  607. unsigned long elapsed_secs = (unsigned long) (Curl_tvdiff(Curl_tvnow(),
  608. conn->created) / 1000);
  609. /* the alarm period is counted in even number of seconds */
  610. unsigned long alarm_set = prev_alarm - elapsed_secs;
  611. if(!alarm_set ||
  612. ((alarm_set >= 0x80000000) && (prev_alarm < 0x80000000)) ) {
  613. /* if the alarm time-left reached zero or turned "negative" (counted
  614. with unsigned values), we should fire off a SIGALRM here, but we
  615. won't, and zero would be to switch it off so we never set it to
  616. less than 1! */
  617. alarm(1);
  618. rc = CURLRESOLV_TIMEDOUT;
  619. failf(data, "Previous alarm fired off!");
  620. }
  621. else
  622. alarm((unsigned int)alarm_set);
  623. }
  624. #endif /* USE_ALARM_TIMEOUT */
  625. return rc;
  626. }
  627. /*
  628. * Curl_resolv_unlock() unlocks the given cached DNS entry. When this has been
  629. * made, the struct may be destroyed due to pruning. It is important that only
  630. * one unlock is made for each Curl_resolv() call.
  631. *
  632. * May be called with 'data' == NULL for global cache.
  633. */
  634. void Curl_resolv_unlock(struct Curl_easy *data, struct Curl_dns_entry *dns)
  635. {
  636. if(data && data->share)
  637. Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
  638. freednsentry(dns);
  639. if(data && data->share)
  640. Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
  641. }
  642. /*
  643. * File-internal: release cache dns entry reference, free if inuse drops to 0
  644. */
  645. static void freednsentry(void *freethis)
  646. {
  647. struct Curl_dns_entry *dns = (struct Curl_dns_entry *) freethis;
  648. DEBUGASSERT(dns && (dns->inuse>0));
  649. dns->inuse--;
  650. if(dns->inuse == 0) {
  651. Curl_freeaddrinfo(dns->addr);
  652. free(dns);
  653. }
  654. }
  655. /*
  656. * Curl_mk_dnscache() inits a new DNS cache and returns success/failure.
  657. */
  658. int Curl_mk_dnscache(struct curl_hash *hash)
  659. {
  660. return Curl_hash_init(hash, 7, Curl_hash_str, Curl_str_key_compare,
  661. freednsentry);
  662. }
  663. /*
  664. * Curl_hostcache_clean()
  665. *
  666. * This _can_ be called with 'data' == NULL but then of course no locking
  667. * can be done!
  668. */
  669. void Curl_hostcache_clean(struct Curl_easy *data,
  670. struct curl_hash *hash)
  671. {
  672. if(data && data->share)
  673. Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
  674. Curl_hash_clean(hash);
  675. if(data && data->share)
  676. Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
  677. }
  678. CURLcode Curl_loadhostpairs(struct Curl_easy *data)
  679. {
  680. struct curl_slist *hostp;
  681. char hostname[256];
  682. char address[256];
  683. int port;
  684. for(hostp = data->change.resolve; hostp; hostp = hostp->next) {
  685. if(!hostp->data)
  686. continue;
  687. if(hostp->data[0] == '-') {
  688. char *entry_id;
  689. size_t entry_len;
  690. if(2 != sscanf(hostp->data + 1, "%255[^:]:%d", hostname, &port)) {
  691. infof(data, "Couldn't parse CURLOPT_RESOLVE removal entry '%s'!\n",
  692. hostp->data);
  693. continue;
  694. }
  695. /* Create an entry id, based upon the hostname and port */
  696. entry_id = create_hostcache_id(hostname, port);
  697. /* If we can't create the entry id, fail */
  698. if(!entry_id) {
  699. return CURLE_OUT_OF_MEMORY;
  700. }
  701. entry_len = strlen(entry_id);
  702. if(data->share)
  703. Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
  704. /* delete entry, ignore if it didn't exist */
  705. Curl_hash_delete(data->dns.hostcache, entry_id, entry_len+1);
  706. if(data->share)
  707. Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
  708. /* free the allocated entry_id again */
  709. free(entry_id);
  710. }
  711. else {
  712. struct Curl_dns_entry *dns;
  713. Curl_addrinfo *addr;
  714. char *entry_id;
  715. size_t entry_len;
  716. if(3 != sscanf(hostp->data, "%255[^:]:%d:%255s", hostname, &port,
  717. address)) {
  718. infof(data, "Couldn't parse CURLOPT_RESOLVE entry '%s'!\n",
  719. hostp->data);
  720. continue;
  721. }
  722. addr = Curl_str2addr(address, port);
  723. if(!addr) {
  724. infof(data, "Address in '%s' found illegal!\n", hostp->data);
  725. continue;
  726. }
  727. /* Create an entry id, based upon the hostname and port */
  728. entry_id = create_hostcache_id(hostname, port);
  729. /* If we can't create the entry id, fail */
  730. if(!entry_id) {
  731. Curl_freeaddrinfo(addr);
  732. return CURLE_OUT_OF_MEMORY;
  733. }
  734. entry_len = strlen(entry_id);
  735. if(data->share)
  736. Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
  737. /* See if its already in our dns cache */
  738. dns = Curl_hash_pick(data->dns.hostcache, entry_id, entry_len+1);
  739. /* free the allocated entry_id again */
  740. free(entry_id);
  741. if(!dns) {
  742. /* if not in the cache already, put this host in the cache */
  743. dns = Curl_cache_addr(data, addr, hostname, port);
  744. if(dns) {
  745. dns->timestamp = 0; /* mark as added by CURLOPT_RESOLVE */
  746. /* release the returned reference; the cache itself will keep the
  747. * entry alive: */
  748. dns->inuse--;
  749. }
  750. }
  751. else
  752. /* this is a duplicate, free it again */
  753. Curl_freeaddrinfo(addr);
  754. if(data->share)
  755. Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
  756. if(!dns) {
  757. Curl_freeaddrinfo(addr);
  758. return CURLE_OUT_OF_MEMORY;
  759. }
  760. infof(data, "Added %s:%d:%s to DNS cache\n",
  761. hostname, port, address);
  762. }
  763. }
  764. data->change.resolve = NULL; /* dealt with now */
  765. return CURLE_OK;
  766. }