hostip.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2006, Daniel Stenberg, <[email protected]>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at http://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * $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_TYPES_H
  29. #include <sys/types.h>
  30. #endif
  31. #ifdef HAVE_SYS_SOCKET_H
  32. #include <sys/socket.h>
  33. #endif
  34. #ifdef HAVE_NETINET_IN_H
  35. #include <netinet/in.h>
  36. #endif
  37. #ifdef HAVE_NETDB_H
  38. #include <netdb.h>
  39. #endif
  40. #ifdef HAVE_ARPA_INET_H
  41. #include <arpa/inet.h>
  42. #endif
  43. #ifdef HAVE_STDLIB_H
  44. #include <stdlib.h> /* required for free() prototypes */
  45. #endif
  46. #ifdef HAVE_UNISTD_H
  47. #include <unistd.h> /* for the close() proto */
  48. #endif
  49. #ifdef VMS
  50. #include <in.h>
  51. #include <inet.h>
  52. #include <stdlib.h>
  53. #endif
  54. #ifdef HAVE_SETJMP_H
  55. #include <setjmp.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. #if defined(HAVE_INET_NTOA_R) && !defined(HAVE_INET_NTOA_R_DECL)
  71. #include "inet_ntoa_r.h"
  72. #endif
  73. #include "memory.h"
  74. /* The last #include file should be: */
  75. #include "memdebug.h"
  76. /*
  77. * hostip.c explained
  78. * ==================
  79. *
  80. * The main COMPILE-TIME DEFINES to keep in mind when reading the host*.c
  81. * source file are these:
  82. *
  83. * CURLRES_IPV6 - this host has getaddrinfo() and family, and thus we use
  84. * that. The host may not be able to resolve IPv6, but we don't really have to
  85. * take that into account. Hosts that aren't IPv6-enabled have CURLRES_IPV4
  86. * defined.
  87. *
  88. * CURLRES_ARES - is defined if libcurl is built to use c-ares for
  89. * asynchronous name resolves. This can be Windows or *nix.
  90. *
  91. * CURLRES_THREADED - is defined if libcurl is built to run under (native)
  92. * Windows, and then the name resolve will be done in a new thread, and the
  93. * supported API will be the same as for ares-builds.
  94. *
  95. * If any of the two previous are defined, CURLRES_ASYNCH is defined too. If
  96. * libcurl is not built to use an asynchronous resolver, CURLRES_SYNCH is
  97. * defined.
  98. *
  99. * The host*.c sources files are split up like this:
  100. *
  101. * hostip.c - method-independent resolver functions and utility functions
  102. * hostasyn.c - functions for asynchronous name resolves
  103. * hostsyn.c - functions for synchronous name resolves
  104. * hostares.c - functions for ares-using name resolves
  105. * hostthre.c - functions for threaded name resolves
  106. * hostip4.c - ipv4-specific functions
  107. * hostip6.c - ipv6-specific functions
  108. *
  109. * The hostip.h is the united header file for all this. It defines the
  110. * CURLRES_* defines based on the config*.h and setup.h defines.
  111. */
  112. /* These two symbols are for the global DNS cache */
  113. static struct curl_hash hostname_cache;
  114. static int host_cache_initialized;
  115. static void freednsentry(void *freethis);
  116. /*
  117. * Curl_global_host_cache_init() initializes and sets up a global DNS cache.
  118. * Global DNS cache is general badness. Do not use. This will be removed in
  119. * a future version. Use the share interface instead!
  120. */
  121. void Curl_global_host_cache_init(void)
  122. {
  123. if (!host_cache_initialized) {
  124. Curl_hash_init(&hostname_cache, 7, freednsentry);
  125. host_cache_initialized = 1;
  126. }
  127. }
  128. /*
  129. * Return a pointer to the global cache
  130. */
  131. struct curl_hash *Curl_global_host_cache_get(void)
  132. {
  133. return &hostname_cache;
  134. }
  135. /*
  136. * Destroy and cleanup the global DNS cache
  137. */
  138. void Curl_global_host_cache_dtor(void)
  139. {
  140. if (host_cache_initialized) {
  141. Curl_hash_clean(&hostname_cache);
  142. host_cache_initialized = 0;
  143. }
  144. }
  145. /*
  146. * Return # of adresses in a Curl_addrinfo struct
  147. */
  148. int Curl_num_addresses(const Curl_addrinfo *addr)
  149. {
  150. int i;
  151. for (i = 0; addr; addr = addr->ai_next, i++)
  152. ; /* empty loop */
  153. return i;
  154. }
  155. /*
  156. * Curl_printable_address() returns a printable version of the 1st address
  157. * given in the 'ip' 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 *Curl_printable_address(const Curl_addrinfo *ip,
  163. char *buf, size_t bufsize)
  164. {
  165. const void *ip4 = &((const struct sockaddr_in*)ip->ai_addr)->sin_addr;
  166. int af = ip->ai_family;
  167. #ifdef CURLRES_IPV6
  168. const void *ip6 = &((const struct sockaddr_in6*)ip->ai_addr)->sin6_addr;
  169. #else
  170. const void *ip6 = NULL;
  171. #endif
  172. return Curl_inet_ntop(af, af == AF_INET ? ip4 : ip6, buf, bufsize);
  173. }
  174. /*
  175. * Return a hostcache id string for the providing host + port, to be used by
  176. * the DNS caching.
  177. */
  178. static char *
  179. create_hostcache_id(const char *server, int port)
  180. {
  181. /* create and return the new allocated entry */
  182. return aprintf("%s:%d", server, port);
  183. }
  184. struct hostcache_prune_data {
  185. int cache_timeout;
  186. time_t now;
  187. };
  188. /*
  189. * This function is set as a callback to be called for every entry in the DNS
  190. * cache when we want to prune old unused entries.
  191. *
  192. * Returning non-zero means remove the entry, return 0 to keep it in the
  193. * cache.
  194. */
  195. static int
  196. hostcache_timestamp_remove(void *datap, void *hc)
  197. {
  198. struct hostcache_prune_data *data =
  199. (struct hostcache_prune_data *) datap;
  200. struct Curl_dns_entry *c = (struct Curl_dns_entry *) hc;
  201. if ((data->now - c->timestamp < data->cache_timeout) ||
  202. c->inuse) {
  203. /* please don't remove */
  204. return 0;
  205. }
  206. /* fine, remove */
  207. return 1;
  208. }
  209. /*
  210. * Prune the DNS cache. This assumes that a lock has already been taken.
  211. */
  212. static void
  213. hostcache_prune(struct curl_hash *hostcache, int cache_timeout, time_t now)
  214. {
  215. struct hostcache_prune_data user;
  216. user.cache_timeout = cache_timeout;
  217. user.now = now;
  218. Curl_hash_clean_with_criterium(hostcache,
  219. (void *) &user,
  220. hostcache_timestamp_remove);
  221. }
  222. /*
  223. * Library-wide function for pruning the DNS cache. This function takes and
  224. * returns the appropriate locks.
  225. */
  226. void Curl_hostcache_prune(struct SessionHandle *data)
  227. {
  228. time_t now;
  229. if((data->set.dns_cache_timeout == -1) || !data->dns.hostcache)
  230. /* cache forever means never prune, and NULL hostcache means
  231. we can't do it */
  232. return;
  233. if(data->share)
  234. Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
  235. time(&now);
  236. /* Remove outdated and unused entries from the hostcache */
  237. hostcache_prune(data->dns.hostcache,
  238. data->set.dns_cache_timeout,
  239. now);
  240. if(data->share)
  241. Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
  242. }
  243. static int
  244. remove_entry_if_stale(struct SessionHandle *data, struct Curl_dns_entry *dns)
  245. {
  246. struct hostcache_prune_data user;
  247. if( !dns || (data->set.dns_cache_timeout == -1) || !data->dns.hostcache)
  248. /* cache forever means never prune, and NULL hostcache means
  249. we can't do it */
  250. return 0;
  251. time(&user.now);
  252. user.cache_timeout = data->set.dns_cache_timeout;
  253. if ( !hostcache_timestamp_remove(&user,dns) )
  254. return 0;
  255. /* ok, we do need to clear the cache. although we need to remove just a
  256. single entry we clean the entire hash, as no explicit delete function
  257. is provided */
  258. if(data->share)
  259. Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
  260. Curl_hash_clean_with_criterium(data->dns.hostcache,
  261. (void *) &user,
  262. hostcache_timestamp_remove);
  263. if(data->share)
  264. Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
  265. return 1;
  266. }
  267. #ifdef HAVE_SIGSETJMP
  268. /* Beware this is a global and unique instance. This is used to store the
  269. return address that we can jump back to from inside a signal handler. This
  270. is not thread-safe stuff. */
  271. sigjmp_buf curl_jmpenv;
  272. #endif
  273. /*
  274. * Curl_cache_addr() stores a 'Curl_addrinfo' struct in the DNS cache.
  275. *
  276. * When calling Curl_resolv() has resulted in a response with a returned
  277. * address, we call this function to store the information in the dns
  278. * cache etc
  279. *
  280. * Returns the Curl_dns_entry entry pointer or NULL if the storage failed.
  281. */
  282. struct Curl_dns_entry *
  283. Curl_cache_addr(struct SessionHandle *data,
  284. Curl_addrinfo *addr,
  285. const char *hostname,
  286. int port)
  287. {
  288. char *entry_id;
  289. size_t entry_len;
  290. struct Curl_dns_entry *dns;
  291. struct Curl_dns_entry *dns2;
  292. time_t now;
  293. /* Create an entry id, based upon the hostname and port */
  294. entry_id = create_hostcache_id(hostname, port);
  295. /* If we can't create the entry id, fail */
  296. if (!entry_id)
  297. return NULL;
  298. entry_len = strlen(entry_id);
  299. /* Create a new cache entry */
  300. dns = (struct Curl_dns_entry *) calloc(sizeof(struct Curl_dns_entry), 1);
  301. if (!dns) {
  302. free(entry_id);
  303. return NULL;
  304. }
  305. dns->inuse = 0; /* init to not used */
  306. dns->addr = addr; /* this is the address(es) */
  307. /* Store the resolved data in our DNS cache. This function may return a
  308. pointer to an existing struct already present in the hash, and it may
  309. return the same argument we pass in. Make no assumptions. */
  310. dns2 = Curl_hash_add(data->dns.hostcache, entry_id, entry_len+1,
  311. (void *)dns);
  312. if(!dns2) {
  313. /* Major badness, run away. */
  314. free(dns);
  315. free(entry_id);
  316. return NULL;
  317. }
  318. time(&now);
  319. dns = dns2;
  320. dns->timestamp = now; /* used now */
  321. dns->inuse++; /* mark entry as in-use */
  322. /* free the allocated entry_id again */
  323. free(entry_id);
  324. return dns;
  325. }
  326. /*
  327. * Curl_resolv() is the main name resolve function within libcurl. It resolves
  328. * a name and returns a pointer to the entry in the 'entry' argument (if one
  329. * is provided). This function might return immediately if we're using asynch
  330. * resolves. See the return codes.
  331. *
  332. * The cache entry we return will get its 'inuse' counter increased when this
  333. * function is used. You MUST call Curl_resolv_unlock() later (when you're
  334. * done using this struct) to decrease the counter again.
  335. *
  336. * Return codes:
  337. *
  338. * CURLRESOLV_ERROR (-1) = error, no pointer
  339. * CURLRESOLV_RESOLVED (0) = OK, pointer provided
  340. * CURLRESOLV_PENDING (1) = waiting for response, no pointer
  341. */
  342. int Curl_resolv(struct connectdata *conn,
  343. const char *hostname,
  344. int port,
  345. struct Curl_dns_entry **entry)
  346. {
  347. char *entry_id = NULL;
  348. struct Curl_dns_entry *dns = NULL;
  349. size_t entry_len;
  350. int wait;
  351. struct SessionHandle *data = conn->data;
  352. CURLcode result;
  353. int rc;
  354. *entry = NULL;
  355. #ifdef HAVE_SIGSETJMP
  356. /* this allows us to time-out from the name resolver, as the timeout
  357. will generate a signal and we will siglongjmp() from that here */
  358. if(!data->set.no_signal) {
  359. if (sigsetjmp(curl_jmpenv, 1)) {
  360. /* this is coming from a siglongjmp() */
  361. failf(data, "name lookup timed out");
  362. return CURLRESOLV_ERROR;
  363. }
  364. }
  365. #endif
  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 CURLRESOLV_ERROR;
  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. if(data->share)
  377. Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
  378. /* free the allocated entry_id again */
  379. free(entry_id);
  380. /* See whether the returned entry is stale. Deliberately done after the
  381. locked block */
  382. if ( remove_entry_if_stale(data,dns) )
  383. dns = NULL; /* the memory deallocation is being handled by the hash */
  384. rc = CURLRESOLV_ERROR; /* default to failure */
  385. if (!dns) {
  386. /* The entry was not in the cache. Resolve it to IP address */
  387. Curl_addrinfo *addr;
  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, 'wait' might be set to a non-zero
  393. value indicating that we need to wait for the response to the resolve
  394. call */
  395. addr = Curl_getaddrinfo(conn, hostname, port, &wait);
  396. if (!addr) {
  397. if(wait) {
  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. else {
  425. if(data->share)
  426. Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
  427. dns->inuse++; /* we use it! */
  428. if(data->share)
  429. Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
  430. rc = CURLRESOLV_RESOLVED;
  431. }
  432. *entry = dns;
  433. return rc;
  434. }
  435. /*
  436. * Curl_resolv_unlock() unlocks the given cached DNS entry. When this has been
  437. * made, the struct may be destroyed due to pruning. It is important that only
  438. * one unlock is made for each Curl_resolv() call.
  439. */
  440. void Curl_resolv_unlock(struct SessionHandle *data, struct Curl_dns_entry *dns)
  441. {
  442. curlassert(dns && (dns->inuse>0));
  443. if(data->share)
  444. Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
  445. dns->inuse--;
  446. if(data->share)
  447. Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
  448. }
  449. /*
  450. * File-internal: free a cache dns entry.
  451. */
  452. static void freednsentry(void *freethis)
  453. {
  454. struct Curl_dns_entry *p = (struct Curl_dns_entry *) freethis;
  455. Curl_freeaddrinfo(p->addr);
  456. free(p);
  457. }
  458. /*
  459. * Curl_mk_dnscache() creates a new DNS cache and returns the handle for it.
  460. */
  461. struct curl_hash *Curl_mk_dnscache(void)
  462. {
  463. return Curl_hash_alloc(7, freednsentry);
  464. }
  465. #ifdef CURLRES_ADDRINFO_COPY
  466. /* align on even 64bit boundaries */
  467. #define MEMALIGN(x) ((x)+(8-(((unsigned long)(x))&0x7)))
  468. /*
  469. * Curl_addrinfo_copy() performs a "deep" copy of a hostent into a buffer and
  470. * returns a pointer to the malloc()ed copy. You need to call free() on the
  471. * returned buffer when you're done with it.
  472. */
  473. Curl_addrinfo *Curl_addrinfo_copy(const void *org, int port)
  474. {
  475. const struct hostent *orig = org;
  476. return Curl_he2ai(orig, port);
  477. }
  478. #endif /* CURLRES_ADDRINFO_COPY */
  479. /***********************************************************************
  480. * Only for plain-ipv4 and c-ares builds
  481. **********************************************************************/
  482. #if defined(CURLRES_IPV4) || defined(CURLRES_ARES)
  483. /*
  484. * This is a function for freeing name information in a protocol independent
  485. * way.
  486. */
  487. void Curl_freeaddrinfo(Curl_addrinfo *ai)
  488. {
  489. Curl_addrinfo *next;
  490. /* walk over the list and free all entries */
  491. while(ai) {
  492. next = ai->ai_next;
  493. free(ai);
  494. ai = next;
  495. }
  496. }
  497. struct namebuf {
  498. struct hostent hostentry;
  499. char *h_addr_list[2];
  500. struct in_addr addrentry;
  501. char h_name[16]; /* 123.123.123.123 = 15 letters is maximum */
  502. };
  503. /*
  504. * Curl_ip2addr() takes a 32bit ipv4 internet address as input parameter
  505. * together with a pointer to the string version of the address, and it
  506. * returns a Curl_addrinfo chain filled in correctly with information for this
  507. * address/host.
  508. *
  509. * The input parameters ARE NOT checked for validity but they are expected
  510. * to have been checked already when this is called.
  511. */
  512. Curl_addrinfo *Curl_ip2addr(in_addr_t num, const char *hostname, int port)
  513. {
  514. Curl_addrinfo *ai;
  515. struct hostent *h;
  516. struct in_addr *addrentry;
  517. struct namebuf buffer;
  518. struct namebuf *buf = &buffer;
  519. h = &buf->hostentry;
  520. h->h_addr_list = &buf->h_addr_list[0];
  521. addrentry = &buf->addrentry;
  522. #ifdef _CRAYC
  523. /* On UNICOS, s_addr is a bit field and for some reason assigning to it
  524. * doesn't work. There must be a better fix than this ugly hack.
  525. */
  526. memcpy(addrentry, &num, SIZEOF_in_addr);
  527. #else
  528. addrentry->s_addr = num;
  529. #endif
  530. h->h_addr_list[0] = (char*)addrentry;
  531. h->h_addr_list[1] = NULL;
  532. h->h_addrtype = AF_INET;
  533. h->h_length = sizeof(*addrentry);
  534. h->h_name = &buf->h_name[0];
  535. h->h_aliases = NULL;
  536. /* Now store the dotted version of the address */
  537. snprintf((char *)h->h_name, 16, "%s", hostname);
  538. ai = Curl_he2ai(h, port);
  539. return ai;
  540. }
  541. #endif