hostip.c 26 KB

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