hostip.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2008, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at http://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * $Id$
  22. ***************************************************************************/
  23. #include "setup.h"
  24. #include <string.h>
  25. #ifdef NEED_MALLOC_H
  26. #include <malloc.h>
  27. #endif
  28. #ifdef HAVE_SYS_SOCKET_H
  29. #include <sys/socket.h>
  30. #endif
  31. #ifdef HAVE_NETINET_IN_H
  32. #include <netinet/in.h>
  33. #endif
  34. #ifdef HAVE_NETDB_H
  35. #include <netdb.h>
  36. #endif
  37. #ifdef HAVE_ARPA_INET_H
  38. #include <arpa/inet.h>
  39. #endif
  40. #ifdef HAVE_STDLIB_H
  41. #include <stdlib.h> /* required for free() prototypes */
  42. #endif
  43. #ifdef HAVE_UNISTD_H
  44. #include <unistd.h> /* for the close() proto */
  45. #endif
  46. #ifdef VMS
  47. #include <in.h>
  48. #include <inet.h>
  49. #include <stdlib.h>
  50. #endif
  51. #ifdef HAVE_SETJMP_H
  52. #include <setjmp.h>
  53. #endif
  54. #ifdef HAVE_SIGNAL_H
  55. #include <signal.h>
  56. #endif
  57. #ifdef HAVE_PROCESS_H
  58. #include <process.h>
  59. #endif
  60. #include "urldata.h"
  61. #include "sendf.h"
  62. #include "hostip.h"
  63. #include "hash.h"
  64. #include "share.h"
  65. #include "strerror.h"
  66. #include "url.h"
  67. #include "inet_ntop.h"
  68. #define _MPRINTF_REPLACE /* use our functions only */
  69. #include <curl/mprintf.h>
  70. #include "memory.h"
  71. /* The last #include file should be: */
  72. #include "memdebug.h"
  73. #if defined(HAVE_ALARM) && defined(SIGALRM) && defined(HAVE_SIGSETJMP) \
  74. && !defined(USE_ARES)
  75. /* alarm-based timeouts can only be used with all the dependencies satisfied */
  76. #define USE_ALARM_TIMEOUT
  77. #endif
  78. /*
  79. * hostip.c explained
  80. * ==================
  81. *
  82. * The main COMPILE-TIME DEFINES to keep in mind when reading the host*.c
  83. * source file are these:
  84. *
  85. * CURLRES_IPV6 - this host has getaddrinfo() and family, and thus we use
  86. * that. The host may not be able to resolve IPv6, but we don't really have to
  87. * take that into account. Hosts that aren't IPv6-enabled have CURLRES_IPV4
  88. * defined.
  89. *
  90. * CURLRES_ARES - is defined if libcurl is built to use c-ares for
  91. * asynchronous name resolves. This can be Windows or *nix.
  92. *
  93. * CURLRES_THREADED - is defined if libcurl is built to run under (native)
  94. * Windows, and then the name resolve will be done in a new thread, and the
  95. * supported API will be the same as for ares-builds.
  96. *
  97. * If any of the two previous are defined, CURLRES_ASYNCH is defined too. If
  98. * libcurl is not built to use an asynchronous resolver, CURLRES_SYNCH is
  99. * defined.
  100. *
  101. * The host*.c sources files are split up like this:
  102. *
  103. * hostip.c - method-independent resolver functions and utility functions
  104. * hostasyn.c - functions for asynchronous name resolves
  105. * hostsyn.c - functions for synchronous name resolves
  106. * hostares.c - functions for ares-using name resolves
  107. * hostthre.c - functions for threaded name resolves
  108. * hostip4.c - ipv4-specific functions
  109. * hostip6.c - ipv6-specific functions
  110. *
  111. * The hostip.h is the united header file for all this. It defines the
  112. * CURLRES_* defines based on the config*.h and setup.h defines.
  113. */
  114. /* These two symbols are for the global DNS cache */
  115. static struct curl_hash hostname_cache;
  116. static int host_cache_initialized;
  117. static void freednsentry(void *freethis);
  118. /*
  119. * Curl_global_host_cache_init() initializes and sets up a global DNS cache.
  120. * Global DNS cache is general badness. Do not use. This will be removed in
  121. * a future version. Use the share interface instead!
  122. *
  123. * Returns a struct curl_hash pointer on success, NULL on failure.
  124. */
  125. struct curl_hash *Curl_global_host_cache_init(void)
  126. {
  127. int rc = 0;
  128. if(!host_cache_initialized) {
  129. rc = Curl_hash_init(&hostname_cache, 7, Curl_hash_str,
  130. Curl_str_key_compare, freednsentry);
  131. if(!rc)
  132. host_cache_initialized = 1;
  133. }
  134. return rc?NULL:&hostname_cache;
  135. }
  136. /*
  137. * Destroy and cleanup the global DNS cache
  138. */
  139. void Curl_global_host_cache_dtor(void)
  140. {
  141. if(host_cache_initialized) {
  142. Curl_hash_clean(&hostname_cache);
  143. host_cache_initialized = 0;
  144. }
  145. }
  146. /*
  147. * Return # of adresses in a Curl_addrinfo struct
  148. */
  149. int Curl_num_addresses(const Curl_addrinfo *addr)
  150. {
  151. int i = 0;
  152. while(addr) {
  153. addr = addr->ai_next;
  154. i++;
  155. }
  156. return i;
  157. }
  158. /*
  159. * Curl_printable_address() returns a printable version of the 1st address
  160. * given in the 'ai' argument. The result will be stored in the buf that is
  161. * bufsize bytes big.
  162. *
  163. * If the conversion fails, it returns NULL.
  164. */
  165. const char *
  166. Curl_printable_address(const Curl_addrinfo *ai, char *buf, size_t bufsize)
  167. {
  168. const struct sockaddr_in *sa4;
  169. const struct in_addr *ipaddr4;
  170. #ifdef ENABLE_IPV6
  171. const struct sockaddr_in6 *sa6;
  172. const struct in6_addr *ipaddr6;
  173. #endif
  174. switch (ai->ai_family) {
  175. case AF_INET:
  176. sa4 = (const void *)ai->ai_addr;
  177. ipaddr4 = &sa4->sin_addr;
  178. return Curl_inet_ntop(ai->ai_family, (const void *)ipaddr4, buf, bufsize);
  179. #ifdef ENABLE_IPV6
  180. case AF_INET6:
  181. sa6 = (const void *)ai->ai_addr;
  182. ipaddr6 = &sa6->sin6_addr;
  183. return Curl_inet_ntop(ai->ai_family, (const void *)ipaddr6, buf, bufsize);
  184. #endif
  185. default:
  186. break;
  187. }
  188. return NULL;
  189. }
  190. /*
  191. * Return a hostcache id string for the providing host + port, to be used by
  192. * the DNS caching.
  193. */
  194. static char *
  195. create_hostcache_id(const char *server, int port)
  196. {
  197. /* create and return the new allocated entry */
  198. return aprintf("%s:%d", server, port);
  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. if((data->now - c->timestamp < data->cache_timeout) ||
  218. c->inuse) {
  219. /* please don't remove */
  220. return 0;
  221. }
  222. /* fine, remove */
  223. return 1;
  224. }
  225. /*
  226. * Prune the DNS cache. This assumes that a lock has already been taken.
  227. */
  228. static void
  229. hostcache_prune(struct curl_hash *hostcache, long cache_timeout, time_t now)
  230. {
  231. struct hostcache_prune_data user;
  232. user.cache_timeout = cache_timeout;
  233. user.now = now;
  234. Curl_hash_clean_with_criterium(hostcache,
  235. (void *) &user,
  236. hostcache_timestamp_remove);
  237. }
  238. /*
  239. * Library-wide function for pruning the DNS cache. This function takes and
  240. * returns the appropriate locks.
  241. */
  242. void Curl_hostcache_prune(struct SessionHandle *data)
  243. {
  244. time_t now;
  245. if((data->set.dns_cache_timeout == -1) || !data->dns.hostcache)
  246. /* cache forever means never prune, and NULL hostcache means
  247. we can't do it */
  248. return;
  249. if(data->share)
  250. Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
  251. time(&now);
  252. /* Remove outdated and unused entries from the hostcache */
  253. hostcache_prune(data->dns.hostcache,
  254. data->set.dns_cache_timeout,
  255. now);
  256. if(data->share)
  257. Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
  258. }
  259. /*
  260. * Check if the entry should be pruned. Assumes a locked cache.
  261. */
  262. static int
  263. remove_entry_if_stale(struct SessionHandle *data, struct Curl_dns_entry *dns)
  264. {
  265. struct hostcache_prune_data user;
  266. if( !dns || (data->set.dns_cache_timeout == -1) || !data->dns.hostcache)
  267. /* cache forever means never prune, and NULL hostcache means
  268. we can't do it */
  269. return 0;
  270. time(&user.now);
  271. user.cache_timeout = data->set.dns_cache_timeout;
  272. if( !hostcache_timestamp_remove(&user,dns) )
  273. return 0;
  274. Curl_hash_clean_with_criterium(data->dns.hostcache,
  275. (void *) &user,
  276. hostcache_timestamp_remove);
  277. return 1;
  278. }
  279. #ifdef HAVE_SIGSETJMP
  280. /* Beware this is a global and unique instance. This is used to store the
  281. return address that we can jump back to from inside a signal handler. This
  282. is not thread-safe stuff. */
  283. sigjmp_buf curl_jmpenv;
  284. #endif
  285. /*
  286. * Curl_cache_addr() stores a 'Curl_addrinfo' struct in the DNS cache.
  287. *
  288. * When calling Curl_resolv() has resulted in a response with a returned
  289. * address, we call this function to store the information in the dns
  290. * cache etc
  291. *
  292. * Returns the Curl_dns_entry entry pointer or NULL if the storage failed.
  293. */
  294. struct Curl_dns_entry *
  295. Curl_cache_addr(struct SessionHandle *data,
  296. Curl_addrinfo *addr,
  297. const char *hostname,
  298. int port)
  299. {
  300. char *entry_id;
  301. size_t entry_len;
  302. struct Curl_dns_entry *dns;
  303. struct Curl_dns_entry *dns2;
  304. time_t now;
  305. /* Create an entry id, based upon the hostname and port */
  306. entry_id = create_hostcache_id(hostname, port);
  307. /* If we can't create the entry id, fail */
  308. if(!entry_id)
  309. return NULL;
  310. entry_len = strlen(entry_id);
  311. /* Create a new cache entry */
  312. dns = calloc(sizeof(struct Curl_dns_entry), 1);
  313. if(!dns) {
  314. free(entry_id);
  315. return NULL;
  316. }
  317. dns->inuse = 0; /* init to not used */
  318. dns->addr = addr; /* this is the address(es) */
  319. /* Store the resolved data in our DNS cache. This function may return a
  320. pointer to an existing struct already present in the hash, and it may
  321. return the same argument we pass in. Make no assumptions. */
  322. dns2 = Curl_hash_add(data->dns.hostcache, entry_id, entry_len+1,
  323. (void *)dns);
  324. if(!dns2) {
  325. /* Major badness, run away. */
  326. free(dns);
  327. free(entry_id);
  328. return NULL;
  329. }
  330. time(&now);
  331. dns = dns2;
  332. dns->timestamp = now; /* used now */
  333. dns->inuse++; /* mark entry as in-use */
  334. /* free the allocated entry_id again */
  335. free(entry_id);
  336. return dns;
  337. }
  338. /*
  339. * Curl_resolv() is the main name resolve function within libcurl. It resolves
  340. * a name and returns a pointer to the entry in the 'entry' argument (if one
  341. * is provided). This function might return immediately if we're using asynch
  342. * resolves. See the return codes.
  343. *
  344. * The cache entry we return will get its 'inuse' counter increased when this
  345. * function is used. You MUST call Curl_resolv_unlock() later (when you're
  346. * done using this struct) to decrease the counter again.
  347. *
  348. * Return codes:
  349. *
  350. * CURLRESOLV_ERROR (-1) = error, no pointer
  351. * CURLRESOLV_RESOLVED (0) = OK, pointer provided
  352. * CURLRESOLV_PENDING (1) = waiting for response, no pointer
  353. */
  354. int Curl_resolv(struct connectdata *conn,
  355. const char *hostname,
  356. int port,
  357. struct Curl_dns_entry **entry)
  358. {
  359. char *entry_id = NULL;
  360. struct Curl_dns_entry *dns = NULL;
  361. size_t entry_len;
  362. struct SessionHandle *data = conn->data;
  363. CURLcode result;
  364. int rc = CURLRESOLV_ERROR; /* default to failure */
  365. *entry = NULL;
  366. /* Create an entry id, based upon the hostname and port */
  367. entry_id = create_hostcache_id(hostname, port);
  368. /* If we can't create the entry id, fail */
  369. if(!entry_id)
  370. return rc;
  371. entry_len = strlen(entry_id);
  372. if(data->share)
  373. Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
  374. /* See if its already in our dns cache */
  375. dns = Curl_hash_pick(data->dns.hostcache, entry_id, entry_len+1);
  376. /* See whether the returned entry is stale. Done before we release lock */
  377. if( remove_entry_if_stale(data, dns) )
  378. dns = NULL; /* the memory deallocation is being handled by the hash */
  379. if(dns) {
  380. dns->inuse++; /* we use it! */
  381. rc = CURLRESOLV_RESOLVED;
  382. }
  383. if(data->share)
  384. Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
  385. /* free the allocated entry_id again */
  386. free(entry_id);
  387. if(!dns) {
  388. /* The entry was not in the cache. Resolve it to IP address */
  389. Curl_addrinfo *addr;
  390. int respwait;
  391. /* Check what IP specifics the app has requested and if we can provide it.
  392. * If not, bail out. */
  393. if(!Curl_ipvalid(data))
  394. return CURLRESOLV_ERROR;
  395. /* If Curl_getaddrinfo() returns NULL, 'respwait' might be set to a
  396. non-zero value indicating that we need to wait for the response to the
  397. resolve call */
  398. addr = Curl_getaddrinfo(conn, hostname, port, &respwait);
  399. if(!addr) {
  400. if(respwait) {
  401. /* the response to our resolve call will come asynchronously at
  402. a later time, good or bad */
  403. /* First, check that we haven't received the info by now */
  404. result = Curl_is_resolved(conn, &dns);
  405. if(result) /* error detected */
  406. return CURLRESOLV_ERROR;
  407. if(dns)
  408. rc = CURLRESOLV_RESOLVED; /* pointer provided */
  409. else
  410. rc = CURLRESOLV_PENDING; /* no info yet */
  411. }
  412. }
  413. else {
  414. if(data->share)
  415. Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
  416. /* we got a response, store it in the cache */
  417. dns = Curl_cache_addr(data, addr, hostname, port);
  418. if(data->share)
  419. Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
  420. if(!dns)
  421. /* returned failure, bail out nicely */
  422. Curl_freeaddrinfo(addr);
  423. else
  424. rc = CURLRESOLV_RESOLVED;
  425. }
  426. }
  427. *entry = dns;
  428. return rc;
  429. }
  430. #ifdef USE_ALARM_TIMEOUT
  431. /*
  432. * This signal handler jumps back into the main libcurl code and continues
  433. * execution. This effectively causes the remainder of the application to run
  434. * within a signal handler which is nonportable and could lead to problems.
  435. */
  436. static
  437. RETSIGTYPE alarmfunc(int sig)
  438. {
  439. /* this is for "-ansi -Wall -pedantic" to stop complaining! (rabe) */
  440. (void)sig;
  441. siglongjmp(curl_jmpenv, 1);
  442. return;
  443. }
  444. #endif /* USE_ALARM_TIMEOUT */
  445. /*
  446. * Curl_resolv_timeout() is the same as Curl_resolv() but specifies a
  447. * timeout. This function might return immediately if we're using asynch
  448. * resolves. See the return codes.
  449. *
  450. * The cache entry we return will get its 'inuse' counter increased when this
  451. * function is used. You MUST call Curl_resolv_unlock() later (when you're
  452. * done using this struct) to decrease the counter again.
  453. *
  454. * If built with a synchronous resolver and use of signals is not
  455. * disabled by the application, then a nonzero timeout will cause a
  456. * timeout after the specified number of milliseconds. Otherwise, timeout
  457. * is ignored.
  458. *
  459. * Return codes:
  460. *
  461. * CURLRESOLV_TIMEDOUT(-2) = warning, time too short or previous alarm expired
  462. * CURLRESOLV_ERROR (-1) = error, no pointer
  463. * CURLRESOLV_RESOLVED (0) = OK, pointer provided
  464. * CURLRESOLV_PENDING (1) = waiting for response, no pointer
  465. */
  466. int Curl_resolv_timeout(struct connectdata *conn,
  467. const char *hostname,
  468. int port,
  469. struct Curl_dns_entry **entry,
  470. long timeoutms)
  471. {
  472. #ifdef USE_ALARM_TIMEOUT
  473. #ifdef HAVE_SIGACTION
  474. struct sigaction keep_sigact; /* store the old struct here */
  475. bool keep_copysig=FALSE; /* did copy it? */
  476. struct sigaction sigact;
  477. #else
  478. #ifdef HAVE_SIGNAL
  479. void (*keep_sigact)(int); /* store the old handler here */
  480. #endif /* HAVE_SIGNAL */
  481. #endif /* HAVE_SIGACTION */
  482. volatile long timeout;
  483. unsigned int prev_alarm=0;
  484. struct SessionHandle *data = conn->data;
  485. #endif /* USE_ALARM_TIMEOUT */
  486. int rc;
  487. *entry = NULL;
  488. #ifdef USE_ALARM_TIMEOUT
  489. if (data->set.no_signal)
  490. /* Ignore the timeout when signals are disabled */
  491. timeout = 0;
  492. else
  493. timeout = timeoutms;
  494. if(timeout && timeout < 1000)
  495. /* The alarm() function only provides integer second resolution, so if
  496. we want to wait less than one second we must bail out already now. */
  497. return CURLRESOLV_TIMEDOUT;
  498. if (timeout > 0) {
  499. /* This allows us to time-out from the name resolver, as the timeout
  500. will generate a signal and we will siglongjmp() from that here.
  501. This technique has problems (see alarmfunc). */
  502. if(sigsetjmp(curl_jmpenv, 1)) {
  503. /* this is coming from a siglongjmp() after an alarm signal */
  504. failf(data, "name lookup timed out");
  505. return CURLRESOLV_ERROR;
  506. }
  507. /*************************************************************
  508. * Set signal handler to catch SIGALRM
  509. * Store the old value to be able to set it back later!
  510. *************************************************************/
  511. #ifdef HAVE_SIGACTION
  512. sigaction(SIGALRM, NULL, &sigact);
  513. keep_sigact = sigact;
  514. keep_copysig = TRUE; /* yes, we have a copy */
  515. sigact.sa_handler = alarmfunc;
  516. #ifdef SA_RESTART
  517. /* HPUX doesn't have SA_RESTART but defaults to that behaviour! */
  518. sigact.sa_flags &= ~SA_RESTART;
  519. #endif
  520. /* now set the new struct */
  521. sigaction(SIGALRM, &sigact, NULL);
  522. #else /* HAVE_SIGACTION */
  523. /* no sigaction(), revert to the much lamer signal() */
  524. #ifdef HAVE_SIGNAL
  525. keep_sigact = signal(SIGALRM, alarmfunc);
  526. #endif
  527. #endif /* HAVE_SIGACTION */
  528. /* alarm() makes a signal get sent when the timeout fires off, and that
  529. will abort system calls */
  530. prev_alarm = alarm((unsigned int) (timeout/1000L));
  531. }
  532. #else
  533. #ifndef CURLRES_ASYNCH
  534. if(timeoutms)
  535. infof(conn->data, "timeout on name lookup is not supported\n");
  536. #else
  537. (void)timeoutms; /* timeoutms not used with an async resolver */
  538. #endif
  539. #endif /* USE_ALARM_TIMEOUT */
  540. /* Perform the actual name resolution. This might be interrupted by an
  541. * alarm if it takes too long.
  542. */
  543. rc = Curl_resolv(conn, hostname, port, entry);
  544. #ifdef USE_ALARM_TIMEOUT
  545. if (timeout > 0) {
  546. #ifdef HAVE_SIGACTION
  547. if(keep_copysig) {
  548. /* we got a struct as it looked before, now put that one back nice
  549. and clean */
  550. sigaction(SIGALRM, &keep_sigact, NULL); /* put it back */
  551. }
  552. #else
  553. #ifdef HAVE_SIGNAL
  554. /* restore the previous SIGALRM handler */
  555. signal(SIGALRM, keep_sigact);
  556. #endif
  557. #endif /* HAVE_SIGACTION */
  558. /* switch back the alarm() to either zero or to what it was before minus
  559. the time we spent until now! */
  560. if(prev_alarm) {
  561. /* there was an alarm() set before us, now put it back */
  562. unsigned long elapsed_ms = Curl_tvdiff(Curl_tvnow(), conn->created);
  563. /* the alarm period is counted in even number of seconds */
  564. unsigned long alarm_set = prev_alarm - elapsed_ms/1000;
  565. if(!alarm_set ||
  566. ((alarm_set >= 0x80000000) && (prev_alarm < 0x80000000)) ) {
  567. /* if the alarm time-left reached zero or turned "negative" (counted
  568. with unsigned values), we should fire off a SIGALRM here, but we
  569. won't, and zero would be to switch it off so we never set it to
  570. less than 1! */
  571. alarm(1);
  572. rc = CURLRESOLV_TIMEDOUT;
  573. failf(data, "Previous alarm fired off!");
  574. }
  575. else
  576. alarm((unsigned int)alarm_set);
  577. }
  578. else
  579. alarm(0); /* just shut it off */
  580. }
  581. #endif /* USE_ALARM_TIMEOUT */
  582. return rc;
  583. }
  584. /*
  585. * Curl_resolv_unlock() unlocks the given cached DNS entry. When this has been
  586. * made, the struct may be destroyed due to pruning. It is important that only
  587. * one unlock is made for each Curl_resolv() call.
  588. */
  589. void Curl_resolv_unlock(struct SessionHandle *data, struct Curl_dns_entry *dns)
  590. {
  591. DEBUGASSERT(dns && (dns->inuse>0));
  592. if(data->share)
  593. Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
  594. dns->inuse--;
  595. if(data->share)
  596. Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
  597. }
  598. /*
  599. * File-internal: free a cache dns entry.
  600. */
  601. static void freednsentry(void *freethis)
  602. {
  603. struct Curl_dns_entry *p = (struct Curl_dns_entry *) freethis;
  604. if(p) {
  605. Curl_freeaddrinfo(p->addr);
  606. free(p);
  607. }
  608. }
  609. /*
  610. * Curl_mk_dnscache() creates a new DNS cache and returns the handle for it.
  611. */
  612. struct curl_hash *Curl_mk_dnscache(void)
  613. {
  614. return Curl_hash_alloc(7, Curl_hash_str, Curl_str_key_compare, freednsentry);
  615. }
  616. #ifdef CURLRES_ADDRINFO_COPY
  617. /* align on even 64bit boundaries */
  618. #define MEMALIGN(x) ((x)+(8-(((unsigned long)(x))&0x7)))
  619. /*
  620. * Curl_addrinfo_copy() performs a "deep" copy of a hostent into a buffer and
  621. * returns a pointer to the malloc()ed copy. You need to call free() on the
  622. * returned buffer when you're done with it.
  623. */
  624. Curl_addrinfo *Curl_addrinfo_copy(const void *org, int port)
  625. {
  626. const struct hostent *orig = org;
  627. return Curl_he2ai(orig, port);
  628. }
  629. #endif /* CURLRES_ADDRINFO_COPY */