hostip.c 48 KB

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