hostip.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 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 https://curl.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. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #ifdef HAVE_NETINET_IN_H
  26. #include <netinet/in.h>
  27. #endif
  28. #ifdef HAVE_NETINET_IN6_H
  29. #include <netinet/in6.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 __VMS
  38. #include <in.h>
  39. #include <inet.h>
  40. #endif
  41. #include <setjmp.h>
  42. #ifndef UNDER_CE
  43. #include <signal.h>
  44. #endif
  45. #include "urldata.h"
  46. #include "sendf.h"
  47. #include "connect.h"
  48. #include "hostip.h"
  49. #include "hash.h"
  50. #include "rand.h"
  51. #include "share.h"
  52. #include "url.h"
  53. #include "curlx/inet_ntop.h"
  54. #include "curlx/inet_pton.h"
  55. #include "multiif.h"
  56. #include "doh.h"
  57. #include "curlx/warnless.h"
  58. #include "select.h"
  59. #include "strcase.h"
  60. #include "easy_lock.h"
  61. #include "curlx/strparse.h"
  62. /* The last 3 #include files should be in this order */
  63. #include "curl_printf.h"
  64. #include "curl_memory.h"
  65. #include "memdebug.h"
  66. #if defined(CURLRES_SYNCH) && \
  67. defined(HAVE_ALARM) && \
  68. defined(SIGALRM) && \
  69. defined(HAVE_SIGSETJMP) && \
  70. defined(GLOBAL_INIT_IS_THREADSAFE)
  71. /* alarm-based timeouts can only be used with all the dependencies satisfied */
  72. #define USE_ALARM_TIMEOUT
  73. #endif
  74. #define MAX_HOSTCACHE_LEN (255 + 7) /* max FQDN + colon + port number + zero */
  75. #define MAX_DNS_CACHE_SIZE 29999
  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 do not really have to
  85. * take that into account. Hosts that are not 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. * hostip4.c - IPv4 specific functions
  103. * hostip6.c - IPv6 specific functions
  104. * asyn.h - common functions for all async resolvers
  105. * The two asynchronous name resolver backends are implemented in:
  106. * asyn-ares.c - async resolver using c-ares
  107. * asyn-thread.c - async resolver using POSIX threads
  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 curl_setup.h defines.
  111. */
  112. static void dnscache_entry_free(struct Curl_dns_entry *dns);
  113. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  114. static void show_resolve_info(struct Curl_easy *data,
  115. struct Curl_dns_entry *dns);
  116. #else
  117. #define show_resolve_info(x,y) Curl_nop_stmt
  118. #endif
  119. /*
  120. * Curl_printable_address() stores a printable version of the 1st address
  121. * given in the 'ai' argument. The result will be stored in the buf that is
  122. * bufsize bytes big.
  123. *
  124. * If the conversion fails, the target buffer is empty.
  125. */
  126. void Curl_printable_address(const struct Curl_addrinfo *ai, char *buf,
  127. size_t bufsize)
  128. {
  129. DEBUGASSERT(bufsize);
  130. buf[0] = 0;
  131. switch(ai->ai_family) {
  132. case AF_INET: {
  133. const struct sockaddr_in *sa4 = (const void *)ai->ai_addr;
  134. const struct in_addr *ipaddr4 = &sa4->sin_addr;
  135. (void)curlx_inet_ntop(ai->ai_family, (const void *)ipaddr4, buf, bufsize);
  136. break;
  137. }
  138. #ifdef USE_IPV6
  139. case AF_INET6: {
  140. const struct sockaddr_in6 *sa6 = (const void *)ai->ai_addr;
  141. const struct in6_addr *ipaddr6 = &sa6->sin6_addr;
  142. (void)curlx_inet_ntop(ai->ai_family, (const void *)ipaddr6, buf, bufsize);
  143. break;
  144. }
  145. #endif
  146. default:
  147. break;
  148. }
  149. }
  150. /*
  151. * Create a hostcache id string for the provided host + port, to be used by
  152. * the DNS caching. Without alloc. Return length of the id string.
  153. */
  154. static size_t
  155. create_dnscache_id(const char *name,
  156. size_t nlen, /* 0 or actual name length */
  157. int port, char *ptr, size_t buflen)
  158. {
  159. size_t len = nlen ? nlen : strlen(name);
  160. DEBUGASSERT(buflen >= MAX_HOSTCACHE_LEN);
  161. if(len > (buflen - 7))
  162. len = buflen - 7;
  163. /* store and lower case the name */
  164. Curl_strntolower(ptr, name, len);
  165. return msnprintf(&ptr[len], 7, ":%u", port) + len;
  166. }
  167. struct dnscache_prune_data {
  168. time_t now;
  169. time_t oldest; /* oldest time in cache not pruned. */
  170. int max_age_sec;
  171. };
  172. /*
  173. * This function is set as a callback to be called for every entry in the DNS
  174. * cache when we want to prune old unused entries.
  175. *
  176. * Returning non-zero means remove the entry, return 0 to keep it in the
  177. * cache.
  178. */
  179. static int
  180. dnscache_entry_is_stale(void *datap, void *hc)
  181. {
  182. struct dnscache_prune_data *prune =
  183. (struct dnscache_prune_data *) datap;
  184. struct Curl_dns_entry *dns = (struct Curl_dns_entry *) hc;
  185. if(dns->timestamp) {
  186. /* age in seconds */
  187. time_t age = prune->now - dns->timestamp;
  188. if(age >= (time_t)prune->max_age_sec)
  189. return TRUE;
  190. if(age > prune->oldest)
  191. prune->oldest = age;
  192. }
  193. return FALSE;
  194. }
  195. /*
  196. * Prune the DNS cache. This assumes that a lock has already been taken.
  197. * Returns the 'age' of the oldest still kept entry.
  198. */
  199. static time_t
  200. dnscache_prune(struct Curl_hash *hostcache, int cache_timeout,
  201. time_t now)
  202. {
  203. struct dnscache_prune_data user;
  204. user.max_age_sec = cache_timeout;
  205. user.now = now;
  206. user.oldest = 0;
  207. Curl_hash_clean_with_criterium(hostcache,
  208. (void *) &user,
  209. dnscache_entry_is_stale);
  210. return user.oldest;
  211. }
  212. static struct Curl_dnscache *dnscache_get(struct Curl_easy *data)
  213. {
  214. if(data->share && data->share->specifier & (1 << CURL_LOCK_DATA_DNS))
  215. return &data->share->dnscache;
  216. if(data->multi)
  217. return &data->multi->dnscache;
  218. return NULL;
  219. }
  220. static void dnscache_lock(struct Curl_easy *data,
  221. struct Curl_dnscache *dnscache)
  222. {
  223. if(data->share && dnscache == &data->share->dnscache)
  224. Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
  225. }
  226. static void dnscache_unlock(struct Curl_easy *data,
  227. struct Curl_dnscache *dnscache)
  228. {
  229. if(data->share && dnscache == &data->share->dnscache)
  230. Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
  231. }
  232. /*
  233. * Library-wide function for pruning the DNS cache. This function takes and
  234. * returns the appropriate locks.
  235. */
  236. void Curl_dnscache_prune(struct Curl_easy *data)
  237. {
  238. struct Curl_dnscache *dnscache = dnscache_get(data);
  239. time_t now;
  240. /* the timeout may be set -1 (forever) */
  241. int timeout = data->set.dns_cache_timeout;
  242. if(!dnscache)
  243. /* NULL hostcache means we cannot do it */
  244. return;
  245. dnscache_lock(data, dnscache);
  246. now = time(NULL);
  247. do {
  248. /* Remove outdated and unused entries from the hostcache */
  249. time_t oldest = dnscache_prune(&dnscache->entries, timeout, now);
  250. if(oldest < INT_MAX)
  251. timeout = (int)oldest; /* we know it fits */
  252. else
  253. timeout = INT_MAX - 1;
  254. /* if the cache size is still too big, use the oldest age as new
  255. prune limit */
  256. } while(timeout &&
  257. (Curl_hash_count(&dnscache->entries) > MAX_DNS_CACHE_SIZE));
  258. dnscache_unlock(data, dnscache);
  259. }
  260. #ifdef USE_ALARM_TIMEOUT
  261. /* Beware this is a global and unique instance. This is used to store the
  262. return address that we can jump back to from inside a signal handler. This
  263. is not thread-safe stuff. */
  264. static sigjmp_buf curl_jmpenv;
  265. static curl_simple_lock curl_jmpenv_lock;
  266. #endif
  267. /* lookup address, returns entry if found and not stale */
  268. static struct Curl_dns_entry *fetch_addr(struct Curl_easy *data,
  269. struct Curl_dnscache *dnscache,
  270. const char *hostname,
  271. int port,
  272. int ip_version)
  273. {
  274. struct Curl_dns_entry *dns = NULL;
  275. char entry_id[MAX_HOSTCACHE_LEN];
  276. size_t entry_len;
  277. if(!dnscache)
  278. return NULL;
  279. /* Create an entry id, based upon the hostname and port */
  280. entry_len = create_dnscache_id(hostname, 0, port,
  281. entry_id, sizeof(entry_id));
  282. /* See if it is already in our dns cache */
  283. dns = Curl_hash_pick(&dnscache->entries, entry_id, entry_len + 1);
  284. /* No entry found in cache, check if we might have a wildcard entry */
  285. if(!dns && data->state.wildcard_resolve) {
  286. entry_len = create_dnscache_id("*", 1, port, entry_id, sizeof(entry_id));
  287. /* See if it is already in our dns cache */
  288. dns = Curl_hash_pick(&dnscache->entries, entry_id, entry_len + 1);
  289. }
  290. if(dns && (data->set.dns_cache_timeout != -1)) {
  291. /* See whether the returned entry is stale. Done before we release lock */
  292. struct dnscache_prune_data user;
  293. user.now = time(NULL);
  294. user.max_age_sec = data->set.dns_cache_timeout;
  295. user.oldest = 0;
  296. if(dnscache_entry_is_stale(&user, dns)) {
  297. infof(data, "Hostname in DNS cache was stale, zapped");
  298. dns = NULL; /* the memory deallocation is being handled by the hash */
  299. Curl_hash_delete(&dnscache->entries, entry_id, entry_len + 1);
  300. }
  301. }
  302. /* See if the returned entry matches the required resolve mode */
  303. if(dns && ip_version != CURL_IPRESOLVE_WHATEVER) {
  304. int pf = PF_INET;
  305. bool found = FALSE;
  306. struct Curl_addrinfo *addr = dns->addr;
  307. #ifdef PF_INET6
  308. if(ip_version == CURL_IPRESOLVE_V6)
  309. pf = PF_INET6;
  310. #endif
  311. while(addr) {
  312. if(addr->ai_family == pf) {
  313. found = TRUE;
  314. break;
  315. }
  316. addr = addr->ai_next;
  317. }
  318. if(!found) {
  319. infof(data, "Hostname in DNS cache does not have needed family, zapped");
  320. dns = NULL; /* the memory deallocation is being handled by the hash */
  321. Curl_hash_delete(&dnscache->entries, entry_id, entry_len + 1);
  322. }
  323. }
  324. return dns;
  325. }
  326. /*
  327. * Curl_dnscache_get() fetches a 'Curl_dns_entry' already in the DNS cache.
  328. *
  329. * Curl_resolv() checks initially and multi_runsingle() checks each time
  330. * it discovers the handle in the state WAITRESOLVE whether the hostname
  331. * has already been resolved and the address has already been stored in
  332. * the DNS cache. This short circuits waiting for a lot of pending
  333. * lookups for the same hostname requested by different handles.
  334. *
  335. * Returns the Curl_dns_entry entry pointer or NULL if not in the cache.
  336. *
  337. * The returned data *MUST* be "released" with Curl_resolv_unlink() after
  338. * use, or we will leak memory!
  339. */
  340. struct Curl_dns_entry *
  341. Curl_dnscache_get(struct Curl_easy *data,
  342. const char *hostname,
  343. int port,
  344. int ip_version)
  345. {
  346. struct Curl_dnscache *dnscache = dnscache_get(data);
  347. struct Curl_dns_entry *dns = NULL;
  348. dnscache_lock(data, dnscache);
  349. dns = fetch_addr(data, dnscache, hostname, port, ip_version);
  350. if(dns)
  351. dns->refcount++; /* we use it! */
  352. dnscache_unlock(data, dnscache);
  353. return dns;
  354. }
  355. #ifndef CURL_DISABLE_SHUFFLE_DNS
  356. /*
  357. * Return # of addresses in a Curl_addrinfo struct
  358. */
  359. static int num_addresses(const struct Curl_addrinfo *addr)
  360. {
  361. int i = 0;
  362. while(addr) {
  363. addr = addr->ai_next;
  364. i++;
  365. }
  366. return i;
  367. }
  368. UNITTEST CURLcode Curl_shuffle_addr(struct Curl_easy *data,
  369. struct Curl_addrinfo **addr);
  370. /*
  371. * Curl_shuffle_addr() shuffles the order of addresses in a 'Curl_addrinfo'
  372. * struct by re-linking its linked list.
  373. *
  374. * The addr argument should be the address of a pointer to the head node of a
  375. * `Curl_addrinfo` list and it will be modified to point to the new head after
  376. * shuffling.
  377. *
  378. * Not declared static only to make it easy to use in a unit test!
  379. *
  380. * @unittest: 1608
  381. */
  382. UNITTEST CURLcode Curl_shuffle_addr(struct Curl_easy *data,
  383. struct Curl_addrinfo **addr)
  384. {
  385. CURLcode result = CURLE_OK;
  386. const int num_addrs = num_addresses(*addr);
  387. if(num_addrs > 1) {
  388. struct Curl_addrinfo **nodes;
  389. infof(data, "Shuffling %i addresses", num_addrs);
  390. nodes = malloc(num_addrs*sizeof(*nodes));
  391. if(nodes) {
  392. int i;
  393. unsigned int *rnd;
  394. const size_t rnd_size = num_addrs * sizeof(*rnd);
  395. /* build a plain array of Curl_addrinfo pointers */
  396. nodes[0] = *addr;
  397. for(i = 1; i < num_addrs; i++) {
  398. nodes[i] = nodes[i-1]->ai_next;
  399. }
  400. rnd = malloc(rnd_size);
  401. if(rnd) {
  402. /* Fisher-Yates shuffle */
  403. if(Curl_rand(data, (unsigned char *)rnd, rnd_size) == CURLE_OK) {
  404. struct Curl_addrinfo *swap_tmp;
  405. for(i = num_addrs - 1; i > 0; i--) {
  406. swap_tmp = nodes[rnd[i] % (unsigned int)(i + 1)];
  407. nodes[rnd[i] % (unsigned int)(i + 1)] = nodes[i];
  408. nodes[i] = swap_tmp;
  409. }
  410. /* relink list in the new order */
  411. for(i = 1; i < num_addrs; i++) {
  412. nodes[i-1]->ai_next = nodes[i];
  413. }
  414. nodes[num_addrs-1]->ai_next = NULL;
  415. *addr = nodes[0];
  416. }
  417. free(rnd);
  418. }
  419. else
  420. result = CURLE_OUT_OF_MEMORY;
  421. free(nodes);
  422. }
  423. else
  424. result = CURLE_OUT_OF_MEMORY;
  425. }
  426. return result;
  427. }
  428. #endif
  429. struct Curl_dns_entry *
  430. Curl_dnscache_mk_entry(struct Curl_easy *data,
  431. struct Curl_addrinfo *addr,
  432. const char *hostname,
  433. size_t hostlen, /* length or zero */
  434. int port,
  435. bool permanent)
  436. {
  437. struct Curl_dns_entry *dns;
  438. #ifndef CURL_DISABLE_SHUFFLE_DNS
  439. /* shuffle addresses if requested */
  440. if(data->set.dns_shuffle_addresses) {
  441. CURLcode result = Curl_shuffle_addr(data, &addr);
  442. if(result) {
  443. Curl_freeaddrinfo(addr);
  444. return NULL;
  445. }
  446. }
  447. #else
  448. (void)data;
  449. #endif
  450. if(!hostlen)
  451. hostlen = strlen(hostname);
  452. /* Create a new cache entry */
  453. dns = calloc(1, sizeof(struct Curl_dns_entry) + hostlen);
  454. if(!dns) {
  455. Curl_freeaddrinfo(addr);
  456. return NULL;
  457. }
  458. dns->refcount = 1; /* the cache has the first reference */
  459. dns->addr = addr; /* this is the address(es) */
  460. if(permanent)
  461. dns->timestamp = 0; /* an entry that never goes stale */
  462. else {
  463. dns->timestamp = time(NULL);
  464. if(dns->timestamp == 0)
  465. dns->timestamp = 1;
  466. }
  467. dns->hostport = port;
  468. if(hostlen)
  469. memcpy(dns->hostname, hostname, hostlen);
  470. return dns;
  471. }
  472. static struct Curl_dns_entry *
  473. dnscache_add_addr(struct Curl_easy *data,
  474. struct Curl_dnscache *dnscache,
  475. struct Curl_addrinfo *addr,
  476. const char *hostname,
  477. size_t hlen, /* length or zero */
  478. int port,
  479. bool permanent)
  480. {
  481. char entry_id[MAX_HOSTCACHE_LEN];
  482. size_t entry_len;
  483. struct Curl_dns_entry *dns;
  484. struct Curl_dns_entry *dns2;
  485. dns = Curl_dnscache_mk_entry(data, addr, hostname, hlen, port, permanent);
  486. if(!dns)
  487. return NULL;
  488. /* Create an entry id, based upon the hostname and port */
  489. entry_len = create_dnscache_id(hostname, hlen, port,
  490. entry_id, sizeof(entry_id));
  491. /* Store the resolved data in our DNS cache. */
  492. dns2 = Curl_hash_add(&dnscache->entries, entry_id, entry_len + 1,
  493. (void *)dns);
  494. if(!dns2) {
  495. dnscache_entry_free(dns);
  496. return NULL;
  497. }
  498. dns = dns2;
  499. dns->refcount++; /* mark entry as in-use */
  500. return dns;
  501. }
  502. CURLcode Curl_dnscache_add(struct Curl_easy *data,
  503. struct Curl_dns_entry *entry)
  504. {
  505. struct Curl_dnscache *dnscache = dnscache_get(data);
  506. char id[MAX_HOSTCACHE_LEN];
  507. size_t idlen;
  508. if(!dnscache)
  509. return CURLE_FAILED_INIT;
  510. /* Create an entry id, based upon the hostname and port */
  511. idlen = create_dnscache_id(entry->hostname, 0, entry->hostport,
  512. id, sizeof(id));
  513. /* Store the resolved data in our DNS cache and up ref count */
  514. dnscache_lock(data, dnscache);
  515. if(!Curl_hash_add(&dnscache->entries, id, idlen + 1, (void *)entry)) {
  516. dnscache_unlock(data, dnscache);
  517. return CURLE_OUT_OF_MEMORY;
  518. }
  519. entry->refcount++;
  520. dnscache_unlock(data, dnscache);
  521. return CURLE_OK;
  522. }
  523. #ifdef USE_IPV6
  524. /* return a static IPv6 ::1 for the name */
  525. static struct Curl_addrinfo *get_localhost6(int port, const char *name)
  526. {
  527. struct Curl_addrinfo *ca;
  528. const size_t ss_size = sizeof(struct sockaddr_in6);
  529. const size_t hostlen = strlen(name);
  530. struct sockaddr_in6 sa6;
  531. unsigned char ipv6[16];
  532. unsigned short port16 = (unsigned short)(port & 0xffff);
  533. ca = calloc(1, sizeof(struct Curl_addrinfo) + ss_size + hostlen + 1);
  534. if(!ca)
  535. return NULL;
  536. sa6.sin6_family = AF_INET6;
  537. sa6.sin6_port = htons(port16);
  538. sa6.sin6_flowinfo = 0;
  539. #ifdef HAVE_SOCKADDR_IN6_SIN6_SCOPE_ID
  540. sa6.sin6_scope_id = 0;
  541. #endif
  542. (void)curlx_inet_pton(AF_INET6, "::1", ipv6);
  543. memcpy(&sa6.sin6_addr, ipv6, sizeof(ipv6));
  544. ca->ai_flags = 0;
  545. ca->ai_family = AF_INET6;
  546. ca->ai_socktype = SOCK_STREAM;
  547. ca->ai_protocol = IPPROTO_TCP;
  548. ca->ai_addrlen = (curl_socklen_t)ss_size;
  549. ca->ai_next = NULL;
  550. ca->ai_addr = (void *)((char *)ca + sizeof(struct Curl_addrinfo));
  551. memcpy(ca->ai_addr, &sa6, ss_size);
  552. ca->ai_canonname = (char *)ca->ai_addr + ss_size;
  553. strcpy(ca->ai_canonname, name);
  554. return ca;
  555. }
  556. #else
  557. #define get_localhost6(x,y) NULL
  558. #endif
  559. /* return a static IPv4 127.0.0.1 for the given name */
  560. static struct Curl_addrinfo *get_localhost(int port, const char *name)
  561. {
  562. struct Curl_addrinfo *ca;
  563. struct Curl_addrinfo *ca6;
  564. const size_t ss_size = sizeof(struct sockaddr_in);
  565. const size_t hostlen = strlen(name);
  566. struct sockaddr_in sa;
  567. unsigned int ipv4;
  568. unsigned short port16 = (unsigned short)(port & 0xffff);
  569. /* memset to clear the sa.sin_zero field */
  570. memset(&sa, 0, sizeof(sa));
  571. sa.sin_family = AF_INET;
  572. sa.sin_port = htons(port16);
  573. if(curlx_inet_pton(AF_INET, "127.0.0.1", (char *)&ipv4) < 1)
  574. return NULL;
  575. memcpy(&sa.sin_addr, &ipv4, sizeof(ipv4));
  576. ca = calloc(1, sizeof(struct Curl_addrinfo) + ss_size + hostlen + 1);
  577. if(!ca)
  578. return NULL;
  579. ca->ai_flags = 0;
  580. ca->ai_family = AF_INET;
  581. ca->ai_socktype = SOCK_STREAM;
  582. ca->ai_protocol = IPPROTO_TCP;
  583. ca->ai_addrlen = (curl_socklen_t)ss_size;
  584. ca->ai_addr = (void *)((char *)ca + sizeof(struct Curl_addrinfo));
  585. memcpy(ca->ai_addr, &sa, ss_size);
  586. ca->ai_canonname = (char *)ca->ai_addr + ss_size;
  587. strcpy(ca->ai_canonname, name);
  588. ca6 = get_localhost6(port, name);
  589. if(!ca6)
  590. return ca;
  591. ca6->ai_next = ca;
  592. return ca6;
  593. }
  594. #ifdef USE_IPV6
  595. /*
  596. * Curl_ipv6works() returns TRUE if IPv6 seems to work.
  597. */
  598. bool Curl_ipv6works(struct Curl_easy *data)
  599. {
  600. if(data) {
  601. /* the nature of most system is that IPv6 status does not come and go
  602. during a program's lifetime so we only probe the first time and then we
  603. have the info kept for fast reuse */
  604. DEBUGASSERT(data);
  605. DEBUGASSERT(data->multi);
  606. if(data->multi->ipv6_up == IPV6_UNKNOWN) {
  607. bool works = Curl_ipv6works(NULL);
  608. data->multi->ipv6_up = works ? IPV6_WORKS : IPV6_DEAD;
  609. }
  610. return data->multi->ipv6_up == IPV6_WORKS;
  611. }
  612. else {
  613. int ipv6_works = -1;
  614. /* probe to see if we have a working IPv6 stack */
  615. curl_socket_t s = socket(PF_INET6, SOCK_DGRAM, 0);
  616. if(s == CURL_SOCKET_BAD)
  617. /* an IPv6 address was requested but we cannot get/use one */
  618. ipv6_works = 0;
  619. else {
  620. ipv6_works = 1;
  621. sclose(s);
  622. }
  623. return ipv6_works > 0;
  624. }
  625. }
  626. #endif /* USE_IPV6 */
  627. /*
  628. * Curl_host_is_ipnum() returns TRUE if the given string is a numerical IPv4
  629. * (or IPv6 if supported) address.
  630. */
  631. bool Curl_host_is_ipnum(const char *hostname)
  632. {
  633. struct in_addr in;
  634. #ifdef USE_IPV6
  635. struct in6_addr in6;
  636. #endif
  637. if(curlx_inet_pton(AF_INET, hostname, &in) > 0
  638. #ifdef USE_IPV6
  639. || curlx_inet_pton(AF_INET6, hostname, &in6) > 0
  640. #endif
  641. )
  642. return TRUE;
  643. return FALSE;
  644. }
  645. /* return TRUE if 'part' is a case insensitive tail of 'full' */
  646. static bool tailmatch(const char *full, size_t flen,
  647. const char *part, size_t plen)
  648. {
  649. if(plen > flen)
  650. return FALSE;
  651. return curl_strnequal(part, &full[flen - plen], plen);
  652. }
  653. static struct Curl_addrinfo *
  654. convert_ipaddr_direct(const char *hostname, int port, bool *is_ipaddr)
  655. {
  656. struct in_addr in;
  657. *is_ipaddr = FALSE;
  658. /* First check if this is an IPv4 address string */
  659. if(curlx_inet_pton(AF_INET, hostname, &in) > 0) {
  660. /* This is a dotted IP address 123.123.123.123-style */
  661. *is_ipaddr = TRUE;
  662. #ifdef USE_RESOLVE_ON_IPS
  663. (void)port;
  664. return NULL;
  665. #else
  666. return Curl_ip2addr(AF_INET, &in, hostname, port);
  667. #endif
  668. }
  669. #ifdef USE_IPV6
  670. else {
  671. struct in6_addr in6;
  672. /* check if this is an IPv6 address string */
  673. if(curlx_inet_pton(AF_INET6, hostname, &in6) > 0) {
  674. /* This is an IPv6 address literal */
  675. *is_ipaddr = TRUE;
  676. #ifdef USE_RESOLVE_ON_IPS
  677. return NULL;
  678. #else
  679. return Curl_ip2addr(AF_INET6, &in6, hostname, port);
  680. #endif
  681. }
  682. }
  683. #endif /* USE_IPV6 */
  684. return NULL;
  685. }
  686. static bool can_resolve_ip_version(struct Curl_easy *data, int ip_version)
  687. {
  688. #ifdef CURLRES_IPV6
  689. if(ip_version == CURL_IPRESOLVE_V6 && !Curl_ipv6works(data))
  690. return FALSE;
  691. #elif defined(CURLRES_IPV4)
  692. (void)data;
  693. if(ip_version == CURL_IPRESOLVE_V6)
  694. return FALSE;
  695. #else
  696. #error either CURLRES_IPV6 or CURLRES_IPV4 need to be defined
  697. #endif
  698. return TRUE;
  699. }
  700. /*
  701. * Curl_resolv() is the main name resolve function within libcurl. It resolves
  702. * a name and returns a pointer to the entry in the 'entry' argument (if one
  703. * is provided). This function might return immediately if we are using asynch
  704. * resolves. See the return codes.
  705. *
  706. * The cache entry we return will get its 'inuse' counter increased when this
  707. * function is used. You MUST call Curl_resolv_unlink() later (when you are
  708. * done using this struct) to decrease the reference counter again.
  709. *
  710. * Return codes:
  711. * CURLE_OK = success, *entry set to non-NULL
  712. * CURLE_AGAIN = resolving in progress, *entry == NULL
  713. * CURLE_COULDNT_RESOLVE_HOST = error, *entry == NULL
  714. * CURLE_OPERATION_TIMEDOUT = timeout expired, *entry == NULL
  715. */
  716. CURLcode Curl_resolv(struct Curl_easy *data,
  717. const char *hostname,
  718. int port,
  719. int ip_version,
  720. bool allowDOH,
  721. struct Curl_dns_entry **entry)
  722. {
  723. struct Curl_dnscache *dnscache = dnscache_get(data);
  724. struct Curl_dns_entry *dns = NULL;
  725. struct Curl_addrinfo *addr = NULL;
  726. int respwait = 0;
  727. bool is_ipaddr;
  728. size_t hostname_len;
  729. #ifndef CURL_DISABLE_DOH
  730. data->conn->bits.doh = FALSE; /* default is not */
  731. #else
  732. (void)allowDOH;
  733. #endif
  734. if(!dnscache)
  735. goto error;
  736. /* We should intentionally error and not resolve .onion TLDs */
  737. hostname_len = strlen(hostname);
  738. if(hostname_len >= 7 &&
  739. (curl_strequal(&hostname[hostname_len - 6], ".onion") ||
  740. curl_strequal(&hostname[hostname_len - 7], ".onion."))) {
  741. failf(data, "Not resolving .onion address (RFC 7686)");
  742. goto error;
  743. }
  744. /* Let's check our DNS cache first */
  745. dnscache_lock(data, dnscache);
  746. dns = fetch_addr(data, dnscache, hostname, port, ip_version);
  747. if(dns)
  748. dns->refcount++; /* we pass out the reference. */
  749. dnscache_unlock(data, dnscache);
  750. if(dns) {
  751. infof(data, "Hostname %s was found in DNS cache", hostname);
  752. goto out;
  753. }
  754. /* No luck, we need to resolve hostname. Notify user callback. */
  755. if(data->set.resolver_start) {
  756. void *resolver = NULL;
  757. int st;
  758. #ifdef CURLRES_ASYNCH
  759. if(Curl_async_get_impl(data, &resolver))
  760. goto error;
  761. #endif
  762. Curl_set_in_callback(data, TRUE);
  763. st = data->set.resolver_start(resolver, NULL,
  764. data->set.resolver_start_client);
  765. Curl_set_in_callback(data, FALSE);
  766. if(st)
  767. goto error;
  768. }
  769. /* shortcut literal IP addresses, if we are not told to resolve them. */
  770. addr = convert_ipaddr_direct(hostname, port, &is_ipaddr);
  771. if(addr)
  772. goto out;
  773. #ifndef USE_RESOLVE_ON_IPS
  774. /* allowed to convert, hostname is IP address, then NULL means error */
  775. if(is_ipaddr)
  776. goto error;
  777. #endif
  778. /* Really need a resolver for hostname. */
  779. if(ip_version == CURL_IPRESOLVE_V6 && !Curl_ipv6works(data))
  780. goto error;
  781. if(!is_ipaddr &&
  782. (curl_strequal(hostname, "localhost") ||
  783. curl_strequal(hostname, "localhost.") ||
  784. tailmatch(hostname, hostname_len, STRCONST(".localhost")) ||
  785. tailmatch(hostname, hostname_len, STRCONST(".localhost.")))) {
  786. addr = get_localhost(port, hostname);
  787. }
  788. #ifndef CURL_DISABLE_DOH
  789. else if(!is_ipaddr && allowDOH && data->set.doh) {
  790. addr = Curl_doh(data, hostname, port, ip_version, &respwait);
  791. }
  792. #endif
  793. else {
  794. /* Can we provide the requested IP specifics in resolving? */
  795. if(!can_resolve_ip_version(data, ip_version))
  796. goto error;
  797. #ifdef CURLRES_ASYNCH
  798. addr = Curl_async_getaddrinfo(data, hostname, port, ip_version, &respwait);
  799. #else
  800. respwait = 0; /* no async waiting here */
  801. addr = Curl_sync_getaddrinfo(data, hostname, port, ip_version);
  802. #endif
  803. }
  804. out:
  805. /* We either have found a `dns` or looked up the `addr`
  806. * or `respwait` is set for an async operation.
  807. * Everything else is a failure to resolve. */
  808. if(dns) {
  809. *entry = dns;
  810. return CURLE_OK;
  811. }
  812. else if(addr) {
  813. /* we got a response, create a dns entry, add to cache, return */
  814. dns = Curl_dnscache_mk_entry(data, addr, hostname, 0, port, FALSE);
  815. if(!dns)
  816. goto error;
  817. if(Curl_dnscache_add(data, dns))
  818. goto error;
  819. show_resolve_info(data, dns);
  820. *entry = dns;
  821. return CURLE_OK;
  822. }
  823. else if(respwait) {
  824. if(!Curl_resolv_check(data, &dns)) {
  825. *entry = dns;
  826. return dns ? CURLE_OK : CURLE_AGAIN;
  827. }
  828. }
  829. error:
  830. if(dns)
  831. Curl_resolv_unlink(data, &dns);
  832. *entry = NULL;
  833. Curl_async_shutdown(data);
  834. return CURLE_COULDNT_RESOLVE_HOST;
  835. }
  836. CURLcode Curl_resolv_blocking(struct Curl_easy *data,
  837. const char *hostname,
  838. int port,
  839. int ip_version,
  840. struct Curl_dns_entry **dnsentry)
  841. {
  842. CURLcode result;
  843. *dnsentry = NULL;
  844. result = Curl_resolv(data, hostname, port, ip_version, FALSE, dnsentry);
  845. switch(result) {
  846. case CURLE_OK:
  847. DEBUGASSERT(*dnsentry);
  848. return CURLE_OK;
  849. case CURLE_AGAIN:
  850. DEBUGASSERT(!*dnsentry);
  851. result = Curl_async_await(data, dnsentry);
  852. if(result || !*dnsentry) {
  853. /* close the connection, since we cannot return failure here without
  854. cleaning up this connection properly. */
  855. connclose(data->conn, "async resolve failed");
  856. }
  857. return result;
  858. default:
  859. return result;
  860. }
  861. }
  862. #ifdef USE_ALARM_TIMEOUT
  863. /*
  864. * This signal handler jumps back into the main libcurl code and continues
  865. * execution. This effectively causes the remainder of the application to run
  866. * within a signal handler which is nonportable and could lead to problems.
  867. */
  868. CURL_NORETURN static
  869. void alarmfunc(int sig)
  870. {
  871. (void)sig;
  872. siglongjmp(curl_jmpenv, 1);
  873. }
  874. #endif /* USE_ALARM_TIMEOUT */
  875. /*
  876. * Curl_resolv_timeout() is the same as Curl_resolv() but specifies a
  877. * timeout. This function might return immediately if we are using asynch
  878. * resolves. See the return codes.
  879. *
  880. * The cache entry we return will get its 'inuse' counter increased when this
  881. * function is used. You MUST call Curl_resolv_unlink() later (when you are
  882. * done using this struct) to decrease the reference counter again.
  883. *
  884. * If built with a synchronous resolver and use of signals is not
  885. * disabled by the application, then a nonzero timeout will cause a
  886. * timeout after the specified number of milliseconds. Otherwise, timeout
  887. * is ignored.
  888. *
  889. * Return codes:
  890. * CURLE_OK = success, *entry set to non-NULL
  891. * CURLE_AGAIN = resolving in progress, *entry == NULL
  892. * CURLE_COULDNT_RESOLVE_HOST = error, *entry == NULL
  893. * CURLE_OPERATION_TIMEDOUT = timeout expired, *entry == NULL
  894. */
  895. CURLcode Curl_resolv_timeout(struct Curl_easy *data,
  896. const char *hostname,
  897. int port,
  898. int ip_version,
  899. struct Curl_dns_entry **entry,
  900. timediff_t timeoutms)
  901. {
  902. #ifdef USE_ALARM_TIMEOUT
  903. #ifdef HAVE_SIGACTION
  904. struct sigaction keep_sigact; /* store the old struct here */
  905. volatile bool keep_copysig = FALSE; /* whether old sigact has been saved */
  906. struct sigaction sigact;
  907. #else
  908. #ifdef HAVE_SIGNAL
  909. void (*keep_sigact)(int); /* store the old handler here */
  910. #endif /* HAVE_SIGNAL */
  911. #endif /* HAVE_SIGACTION */
  912. volatile long timeout;
  913. volatile unsigned int prev_alarm = 0;
  914. #endif /* USE_ALARM_TIMEOUT */
  915. CURLcode result;
  916. *entry = NULL;
  917. if(timeoutms < 0)
  918. /* got an already expired timeout */
  919. return CURLE_OPERATION_TIMEDOUT;
  920. #ifdef USE_ALARM_TIMEOUT
  921. if(data->set.no_signal)
  922. /* Ignore the timeout when signals are disabled */
  923. timeout = 0;
  924. else
  925. timeout = (timeoutms > LONG_MAX) ? LONG_MAX : (long)timeoutms;
  926. if(!timeout
  927. #ifndef CURL_DISABLE_DOH
  928. || data->set.doh
  929. #endif
  930. )
  931. /* USE_ALARM_TIMEOUT defined, but no timeout actually requested or resolve
  932. done using DoH */
  933. return Curl_resolv(data, hostname, port, ip_version, TRUE, entry);
  934. if(timeout < 1000) {
  935. /* The alarm() function only provides integer second resolution, so if
  936. we want to wait less than one second we must bail out already now. */
  937. failf(data,
  938. "remaining timeout of %ld too small to resolve via SIGALRM method",
  939. timeout);
  940. return CURLE_OPERATION_TIMEDOUT;
  941. }
  942. /* This allows us to time-out from the name resolver, as the timeout
  943. will generate a signal and we will siglongjmp() from that here.
  944. This technique has problems (see alarmfunc).
  945. This should be the last thing we do before calling Curl_resolv(),
  946. as otherwise we would have to worry about variables that get modified
  947. before we invoke Curl_resolv() (and thus use "volatile"). */
  948. curl_simple_lock_lock(&curl_jmpenv_lock);
  949. if(sigsetjmp(curl_jmpenv, 1)) {
  950. /* this is coming from a siglongjmp() after an alarm signal */
  951. failf(data, "name lookup timed out");
  952. result = CURLE_OPERATION_TIMEDOUT;
  953. goto clean_up;
  954. }
  955. else {
  956. /*************************************************************
  957. * Set signal handler to catch SIGALRM
  958. * Store the old value to be able to set it back later!
  959. *************************************************************/
  960. #ifdef HAVE_SIGACTION
  961. sigaction(SIGALRM, NULL, &sigact);
  962. keep_sigact = sigact;
  963. keep_copysig = TRUE; /* yes, we have a copy */
  964. sigact.sa_handler = alarmfunc;
  965. #ifdef SA_RESTART
  966. /* HP-UX does not have SA_RESTART but defaults to that behavior! */
  967. sigact.sa_flags &= ~SA_RESTART;
  968. #endif
  969. /* now set the new struct */
  970. sigaction(SIGALRM, &sigact, NULL);
  971. #else /* HAVE_SIGACTION */
  972. /* no sigaction(), revert to the much lamer signal() */
  973. #ifdef HAVE_SIGNAL
  974. keep_sigact = signal(SIGALRM, alarmfunc);
  975. #endif
  976. #endif /* HAVE_SIGACTION */
  977. /* alarm() makes a signal get sent when the timeout fires off, and that
  978. will abort system calls */
  979. prev_alarm = alarm(curlx_sltoui(timeout/1000L));
  980. }
  981. #else /* USE_ALARM_TIMEOUT */
  982. #ifndef CURLRES_ASYNCH
  983. if(timeoutms)
  984. infof(data, "timeout on name lookup is not supported");
  985. #else
  986. (void)timeoutms; /* timeoutms not used with an async resolver */
  987. #endif
  988. #endif /* else USE_ALARM_TIMEOUT */
  989. /* Perform the actual name resolution. This might be interrupted by an
  990. * alarm if it takes too long.
  991. */
  992. result = Curl_resolv(data, hostname, port, ip_version, TRUE, entry);
  993. #ifdef USE_ALARM_TIMEOUT
  994. clean_up:
  995. if(!prev_alarm)
  996. /* deactivate a possibly active alarm before uninstalling the handler */
  997. alarm(0);
  998. #ifdef HAVE_SIGACTION
  999. if(keep_copysig) {
  1000. /* we got a struct as it looked before, now put that one back nice
  1001. and clean */
  1002. sigaction(SIGALRM, &keep_sigact, NULL); /* put it back */
  1003. }
  1004. #else
  1005. #ifdef HAVE_SIGNAL
  1006. /* restore the previous SIGALRM handler */
  1007. signal(SIGALRM, keep_sigact);
  1008. #endif
  1009. #endif /* HAVE_SIGACTION */
  1010. curl_simple_lock_unlock(&curl_jmpenv_lock);
  1011. /* switch back the alarm() to either zero or to what it was before minus
  1012. the time we spent until now! */
  1013. if(prev_alarm) {
  1014. /* there was an alarm() set before us, now put it back */
  1015. timediff_t elapsed_secs = curlx_timediff(curlx_now(),
  1016. data->conn->created) / 1000;
  1017. /* the alarm period is counted in even number of seconds */
  1018. unsigned long alarm_set = (unsigned long)(prev_alarm - elapsed_secs);
  1019. if(!alarm_set ||
  1020. ((alarm_set >= 0x80000000) && (prev_alarm < 0x80000000)) ) {
  1021. /* if the alarm time-left reached zero or turned "negative" (counted
  1022. with unsigned values), we should fire off a SIGALRM here, but we
  1023. will not, and zero would be to switch it off so we never set it to
  1024. less than 1! */
  1025. alarm(1);
  1026. result = CURLE_OPERATION_TIMEDOUT;
  1027. failf(data, "Previous alarm fired off");
  1028. }
  1029. else
  1030. alarm((unsigned int)alarm_set);
  1031. }
  1032. #endif /* USE_ALARM_TIMEOUT */
  1033. return result;
  1034. }
  1035. static void dnscache_entry_free(struct Curl_dns_entry *dns)
  1036. {
  1037. Curl_freeaddrinfo(dns->addr);
  1038. #ifdef USE_HTTPSRR
  1039. if(dns->hinfo) {
  1040. Curl_httpsrr_cleanup(dns->hinfo);
  1041. free(dns->hinfo);
  1042. }
  1043. #endif
  1044. free(dns);
  1045. }
  1046. /*
  1047. * Curl_resolv_unlink() releases a reference to the given cached DNS entry.
  1048. * When the reference count reaches 0, the entry is destroyed. It is important
  1049. * that only one unlink is made for each Curl_resolv() call.
  1050. *
  1051. * May be called with 'data' == NULL for global cache.
  1052. */
  1053. void Curl_resolv_unlink(struct Curl_easy *data, struct Curl_dns_entry **pdns)
  1054. {
  1055. if(*pdns) {
  1056. struct Curl_dnscache *dnscache = dnscache_get(data);
  1057. struct Curl_dns_entry *dns = *pdns;
  1058. *pdns = NULL;
  1059. dnscache_lock(data, dnscache);
  1060. dns->refcount--;
  1061. if(dns->refcount == 0)
  1062. dnscache_entry_free(dns);
  1063. dnscache_unlock(data, dnscache);
  1064. }
  1065. }
  1066. static void dnscache_entry_dtor(void *entry)
  1067. {
  1068. struct Curl_dns_entry *dns = (struct Curl_dns_entry *) entry;
  1069. DEBUGASSERT(dns && (dns->refcount > 0));
  1070. dns->refcount--;
  1071. if(dns->refcount == 0)
  1072. dnscache_entry_free(dns);
  1073. }
  1074. /*
  1075. * Curl_dnscache_init() inits a new DNS cache.
  1076. */
  1077. void Curl_dnscache_init(struct Curl_dnscache *dns, size_t size)
  1078. {
  1079. Curl_hash_init(&dns->entries, size, Curl_hash_str, curlx_str_key_compare,
  1080. dnscache_entry_dtor);
  1081. }
  1082. void Curl_dnscache_destroy(struct Curl_dnscache *dns)
  1083. {
  1084. Curl_hash_destroy(&dns->entries);
  1085. }
  1086. CURLcode Curl_loadhostpairs(struct Curl_easy *data)
  1087. {
  1088. struct Curl_dnscache *dnscache = dnscache_get(data);
  1089. struct curl_slist *hostp;
  1090. if(!dnscache)
  1091. return CURLE_FAILED_INIT;
  1092. /* Default is no wildcard found */
  1093. data->state.wildcard_resolve = FALSE;
  1094. for(hostp = data->state.resolve; hostp; hostp = hostp->next) {
  1095. char entry_id[MAX_HOSTCACHE_LEN];
  1096. const char *host = hostp->data;
  1097. struct Curl_str source;
  1098. if(!host)
  1099. continue;
  1100. if(*host == '-') {
  1101. curl_off_t num = 0;
  1102. size_t entry_len;
  1103. host++;
  1104. if(!curlx_str_single(&host, '[')) {
  1105. if(curlx_str_until(&host, &source, MAX_IPADR_LEN, ']') ||
  1106. curlx_str_single(&host, ']') ||
  1107. curlx_str_single(&host, ':'))
  1108. continue;
  1109. }
  1110. else {
  1111. if(curlx_str_until(&host, &source, 4096, ':') ||
  1112. curlx_str_single(&host, ':')) {
  1113. continue;
  1114. }
  1115. }
  1116. if(!curlx_str_number(&host, &num, 0xffff)) {
  1117. /* Create an entry id, based upon the hostname and port */
  1118. entry_len = create_dnscache_id(curlx_str(&source),
  1119. curlx_strlen(&source), (int)num,
  1120. entry_id, sizeof(entry_id));
  1121. dnscache_lock(data, dnscache);
  1122. /* delete entry, ignore if it did not exist */
  1123. Curl_hash_delete(&dnscache->entries, entry_id, entry_len + 1);
  1124. dnscache_unlock(data, dnscache);
  1125. }
  1126. }
  1127. else {
  1128. struct Curl_dns_entry *dns;
  1129. struct Curl_addrinfo *head = NULL, *tail = NULL;
  1130. size_t entry_len;
  1131. char address[64];
  1132. #if !defined(CURL_DISABLE_VERBOSE_STRINGS)
  1133. const char *addresses = NULL;
  1134. #endif
  1135. curl_off_t port = 0;
  1136. bool permanent = TRUE;
  1137. bool error = TRUE;
  1138. if(*host == '+') {
  1139. host++;
  1140. permanent = FALSE;
  1141. }
  1142. if(!curlx_str_single(&host, '[')) {
  1143. if(curlx_str_until(&host, &source, MAX_IPADR_LEN, ']') ||
  1144. curlx_str_single(&host, ']'))
  1145. continue;
  1146. }
  1147. else {
  1148. if(curlx_str_until(&host, &source, 4096, ':'))
  1149. continue;
  1150. }
  1151. if(curlx_str_single(&host, ':') ||
  1152. curlx_str_number(&host, &port, 0xffff) ||
  1153. curlx_str_single(&host, ':'))
  1154. goto err;
  1155. #if !defined(CURL_DISABLE_VERBOSE_STRINGS)
  1156. addresses = host;
  1157. #endif
  1158. /* start the address section */
  1159. while(*host) {
  1160. struct Curl_str target;
  1161. struct Curl_addrinfo *ai;
  1162. if(!curlx_str_single(&host, '[')) {
  1163. if(curlx_str_until(&host, &target, MAX_IPADR_LEN, ']') ||
  1164. curlx_str_single(&host, ']'))
  1165. goto err;
  1166. }
  1167. else {
  1168. if(curlx_str_until(&host, &target, 4096, ',')) {
  1169. if(curlx_str_single(&host, ','))
  1170. goto err;
  1171. /* survive nothing but just a comma */
  1172. continue;
  1173. }
  1174. }
  1175. #ifndef USE_IPV6
  1176. if(memchr(target.str, ':', target.len)) {
  1177. infof(data, "Ignoring resolve address '%s', missing IPv6 support.",
  1178. address);
  1179. if(curlx_str_single(&host, ','))
  1180. goto err;
  1181. continue;
  1182. }
  1183. #endif
  1184. if(curlx_strlen(&target) >= sizeof(address))
  1185. goto err;
  1186. memcpy(address, curlx_str(&target), curlx_strlen(&target));
  1187. address[curlx_strlen(&target)] = '\0';
  1188. ai = Curl_str2addr(address, (int)port);
  1189. if(!ai) {
  1190. infof(data, "Resolve address '%s' found illegal", address);
  1191. goto err;
  1192. }
  1193. if(tail) {
  1194. tail->ai_next = ai;
  1195. tail = tail->ai_next;
  1196. }
  1197. else {
  1198. head = tail = ai;
  1199. }
  1200. if(curlx_str_single(&host, ','))
  1201. break;
  1202. }
  1203. if(!head)
  1204. goto err;
  1205. error = FALSE;
  1206. err:
  1207. if(error) {
  1208. failf(data, "Couldn't parse CURLOPT_RESOLVE entry '%s'",
  1209. hostp->data);
  1210. Curl_freeaddrinfo(head);
  1211. return CURLE_SETOPT_OPTION_SYNTAX;
  1212. }
  1213. /* Create an entry id, based upon the hostname and port */
  1214. entry_len = create_dnscache_id(curlx_str(&source), curlx_strlen(&source),
  1215. (int)port,
  1216. entry_id, sizeof(entry_id));
  1217. dnscache_lock(data, dnscache);
  1218. /* See if it is already in our dns cache */
  1219. dns = Curl_hash_pick(&dnscache->entries, entry_id, entry_len + 1);
  1220. if(dns) {
  1221. infof(data, "RESOLVE %.*s:%" CURL_FORMAT_CURL_OFF_T
  1222. " - old addresses discarded", (int)curlx_strlen(&source),
  1223. curlx_str(&source), port);
  1224. /* delete old entry, there are two reasons for this
  1225. 1. old entry may have different addresses.
  1226. 2. even if entry with correct addresses is already in the cache,
  1227. but if it is close to expire, then by the time next http
  1228. request is made, it can get expired and pruned because old
  1229. entry is not necessarily marked as permanent.
  1230. 3. when adding a non-permanent entry, we want it to remove and
  1231. replace an existing permanent entry.
  1232. 4. when adding a non-permanent entry, we want it to get a "fresh"
  1233. timeout that starts _now_. */
  1234. Curl_hash_delete(&dnscache->entries, entry_id, entry_len + 1);
  1235. }
  1236. /* put this new host in the cache */
  1237. dns = dnscache_add_addr(data, dnscache, head, curlx_str(&source),
  1238. curlx_strlen(&source), (int)port, permanent);
  1239. if(dns) {
  1240. /* release the returned reference; the cache itself will keep the
  1241. * entry alive: */
  1242. dns->refcount--;
  1243. }
  1244. dnscache_unlock(data, dnscache);
  1245. if(!dns)
  1246. return CURLE_OUT_OF_MEMORY;
  1247. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  1248. infof(data, "Added %.*s:%" CURL_FORMAT_CURL_OFF_T ":%s to DNS cache%s",
  1249. (int)curlx_strlen(&source), curlx_str(&source), port, addresses,
  1250. permanent ? "" : " (non-permanent)");
  1251. #endif
  1252. /* Wildcard hostname */
  1253. if(curlx_str_casecompare(&source, "*")) {
  1254. infof(data, "RESOLVE *:%" CURL_FORMAT_CURL_OFF_T " using wildcard",
  1255. port);
  1256. data->state.wildcard_resolve = TRUE;
  1257. }
  1258. }
  1259. }
  1260. data->state.resolve = NULL; /* dealt with now */
  1261. return CURLE_OK;
  1262. }
  1263. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  1264. static void show_resolve_info(struct Curl_easy *data,
  1265. struct Curl_dns_entry *dns)
  1266. {
  1267. struct Curl_addrinfo *a;
  1268. CURLcode result = CURLE_OK;
  1269. #ifdef CURLRES_IPV6
  1270. struct dynbuf out[2];
  1271. #else
  1272. struct dynbuf out[1];
  1273. #endif
  1274. DEBUGASSERT(data);
  1275. DEBUGASSERT(dns);
  1276. if(!data->set.verbose ||
  1277. /* ignore no name or numerical IP addresses */
  1278. !dns->hostname[0] || Curl_host_is_ipnum(dns->hostname))
  1279. return;
  1280. a = dns->addr;
  1281. infof(data, "Host %s:%d was resolved.",
  1282. (dns->hostname[0] ? dns->hostname : "(none)"), dns->hostport);
  1283. curlx_dyn_init(&out[0], 1024);
  1284. #ifdef CURLRES_IPV6
  1285. curlx_dyn_init(&out[1], 1024);
  1286. #endif
  1287. while(a) {
  1288. if(
  1289. #ifdef CURLRES_IPV6
  1290. a->ai_family == PF_INET6 ||
  1291. #endif
  1292. a->ai_family == PF_INET) {
  1293. char buf[MAX_IPADR_LEN];
  1294. struct dynbuf *d = &out[(a->ai_family != PF_INET)];
  1295. Curl_printable_address(a, buf, sizeof(buf));
  1296. if(curlx_dyn_len(d))
  1297. result = curlx_dyn_addn(d, ", ", 2);
  1298. if(!result)
  1299. result = curlx_dyn_add(d, buf);
  1300. if(result) {
  1301. infof(data, "too many IP, cannot show");
  1302. goto fail;
  1303. }
  1304. }
  1305. a = a->ai_next;
  1306. }
  1307. #ifdef CURLRES_IPV6
  1308. infof(data, "IPv6: %s",
  1309. (curlx_dyn_len(&out[1]) ? curlx_dyn_ptr(&out[1]) : "(none)"));
  1310. #endif
  1311. infof(data, "IPv4: %s",
  1312. (curlx_dyn_len(&out[0]) ? curlx_dyn_ptr(&out[0]) : "(none)"));
  1313. fail:
  1314. curlx_dyn_free(&out[0]);
  1315. #ifdef CURLRES_IPV6
  1316. curlx_dyn_free(&out[1]);
  1317. #endif
  1318. }
  1319. #endif
  1320. #ifdef USE_CURL_ASYNC
  1321. CURLcode Curl_resolv_check(struct Curl_easy *data,
  1322. struct Curl_dns_entry **dns)
  1323. {
  1324. CURLcode result;
  1325. /* If async resolving is ongoing, this must be set */
  1326. if(!data->state.async.hostname)
  1327. return CURLE_FAILED_INIT;
  1328. /* check if we have the name resolved by now (from someone else) */
  1329. *dns = Curl_dnscache_get(data, data->state.async.hostname,
  1330. data->state.async.port,
  1331. data->state.async.ip_version);
  1332. if(*dns) {
  1333. /* Tell a possibly async resolver we no longer need the results. */
  1334. infof(data, "Hostname '%s' was found in DNS cache",
  1335. data->state.async.hostname);
  1336. Curl_async_shutdown(data);
  1337. data->state.async.dns = *dns;
  1338. data->state.async.done = TRUE;
  1339. return CURLE_OK;
  1340. }
  1341. #ifndef CURL_DISABLE_DOH
  1342. if(data->conn->bits.doh) {
  1343. result = Curl_doh_is_resolved(data, dns);
  1344. }
  1345. else
  1346. #endif
  1347. result = Curl_async_is_resolved(data, dns);
  1348. if(*dns)
  1349. show_resolve_info(data, *dns);
  1350. return result;
  1351. }
  1352. #endif
  1353. int Curl_resolv_getsock(struct Curl_easy *data,
  1354. curl_socket_t *socks)
  1355. {
  1356. #ifdef CURLRES_ASYNCH
  1357. #ifndef CURL_DISABLE_DOH
  1358. if(data->conn->bits.doh)
  1359. /* nothing to wait for during DoH resolve, those handles have their own
  1360. sockets */
  1361. return GETSOCK_BLANK;
  1362. #endif
  1363. return Curl_async_getsock(data, socks);
  1364. #else
  1365. (void)data;
  1366. (void)socks;
  1367. return GETSOCK_BLANK;
  1368. #endif
  1369. }
  1370. /* Call this function after Curl_connect() has returned async=TRUE and
  1371. then a successful name resolve has been received.
  1372. Note: this function disconnects and frees the conn data in case of
  1373. resolve failure */
  1374. CURLcode Curl_once_resolved(struct Curl_easy *data,
  1375. struct Curl_dns_entry *dns,
  1376. bool *protocol_done)
  1377. {
  1378. CURLcode result;
  1379. struct connectdata *conn = data->conn;
  1380. #ifdef USE_CURL_ASYNC
  1381. if(data->state.async.dns) {
  1382. DEBUGASSERT(data->state.async.dns == dns);
  1383. data->state.async.dns = NULL;
  1384. }
  1385. #endif
  1386. result = Curl_setup_conn(data, dns, protocol_done);
  1387. if(result) {
  1388. Curl_detach_connection(data);
  1389. Curl_conn_terminate(data, conn, TRUE);
  1390. }
  1391. return result;
  1392. }
  1393. /*
  1394. * Curl_resolver_error() calls failf() with the appropriate message after a
  1395. * resolve error
  1396. */
  1397. #ifdef USE_CURL_ASYNC
  1398. CURLcode Curl_resolver_error(struct Curl_easy *data)
  1399. {
  1400. struct connectdata *conn = data->conn;
  1401. const char *host_or_proxy = "host";
  1402. const char *name = conn->host.dispname;
  1403. CURLcode result = CURLE_COULDNT_RESOLVE_HOST;
  1404. #ifndef CURL_DISABLE_PROXY
  1405. if(conn->bits.proxy) {
  1406. host_or_proxy = "proxy";
  1407. result = CURLE_COULDNT_RESOLVE_PROXY;
  1408. name = conn->socks_proxy.host.name ? conn->socks_proxy.host.dispname :
  1409. conn->http_proxy.host.dispname;
  1410. }
  1411. #endif
  1412. failf(data, "Could not resolve %s: %s", host_or_proxy, name);
  1413. return result;
  1414. }
  1415. #endif /* USE_CURL_ASYNC */