2
0

hostip.c 20 KB

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