hostip.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at 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. #error #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. #ifdef HAVE_SETJMP_H
  42. #include <setjmp.h>
  43. #endif
  44. #ifdef HAVE_SIGNAL_H
  45. #include <signal.h>
  46. #endif
  47. #include "urldata.h"
  48. #include "sendf.h"
  49. #include "hostip.h"
  50. #include "hash.h"
  51. #include "rand.h"
  52. #include "share.h"
  53. #include "url.h"
  54. #include "inet_ntop.h"
  55. #include "inet_pton.h"
  56. #include "multiif.h"
  57. #include "doh.h"
  58. #include "warnless.h"
  59. #include "strcase.h"
  60. #include "easy_lock.h"
  61. /* The last 3 #include files should be in this order */
  62. #include "curl_printf.h"
  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 don't really have to
  84. * take that into account. Hosts that aren't IPv6-enabled have CURLRES_IPV4
  85. * defined.
  86. *
  87. * CURLRES_ARES - is defined if libcurl is built to use c-ares for
  88. * asynchronous name resolves. This can be Windows or *nix.
  89. *
  90. * CURLRES_THREADED - is defined if libcurl is built to run under (native)
  91. * Windows, and then the name resolve will be done in a new thread, and the
  92. * supported API will be the same as for ares-builds.
  93. *
  94. * If any of the two previous are defined, CURLRES_ASYNCH is defined too. If
  95. * libcurl is not built to use an asynchronous resolver, CURLRES_SYNCH is
  96. * defined.
  97. *
  98. * The host*.c sources files are split up like this:
  99. *
  100. * hostip.c - method-independent resolver functions and utility functions
  101. * hostasyn.c - functions for asynchronous name resolves
  102. * hostsyn.c - functions for synchronous name resolves
  103. * hostip4.c - IPv4 specific functions
  104. * hostip6.c - IPv6 specific functions
  105. *
  106. * The two asynchronous name resolver backends are implemented in:
  107. * asyn-ares.c - functions for ares-using name resolves
  108. * asyn-thread.c - functions for threaded name resolves
  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 freednsentry(void *freethis);
  113. /*
  114. * Return # of addresses in a Curl_addrinfo struct
  115. */
  116. static int num_addresses(const struct Curl_addrinfo *addr)
  117. {
  118. int i = 0;
  119. while(addr) {
  120. addr = addr->ai_next;
  121. i++;
  122. }
  123. return i;
  124. }
  125. /*
  126. * Curl_printable_address() stores a printable version of the 1st address
  127. * given in the 'ai' argument. The result will be stored in the buf that is
  128. * bufsize bytes big.
  129. *
  130. * If the conversion fails, the target buffer is empty.
  131. */
  132. void Curl_printable_address(const struct Curl_addrinfo *ai, char *buf,
  133. size_t bufsize)
  134. {
  135. DEBUGASSERT(bufsize);
  136. buf[0] = 0;
  137. switch(ai->ai_family) {
  138. case AF_INET: {
  139. const struct sockaddr_in *sa4 = (const void *)ai->ai_addr;
  140. const struct in_addr *ipaddr4 = &sa4->sin_addr;
  141. (void)Curl_inet_ntop(ai->ai_family, (const void *)ipaddr4, buf, bufsize);
  142. break;
  143. }
  144. #ifdef ENABLE_IPV6
  145. case AF_INET6: {
  146. const struct sockaddr_in6 *sa6 = (const void *)ai->ai_addr;
  147. const struct in6_addr *ipaddr6 = &sa6->sin6_addr;
  148. (void)Curl_inet_ntop(ai->ai_family, (const void *)ipaddr6, buf, bufsize);
  149. break;
  150. }
  151. #endif
  152. default:
  153. break;
  154. }
  155. }
  156. /*
  157. * Create a hostcache id string for the provided host + port, to be used by
  158. * the DNS caching. Without alloc. Return length of the id string.
  159. */
  160. static size_t
  161. create_hostcache_id(const char *name,
  162. size_t nlen, /* 0 or actual name length */
  163. int port, char *ptr, size_t buflen)
  164. {
  165. size_t len = nlen ? nlen : strlen(name);
  166. size_t olen = 0;
  167. DEBUGASSERT(buflen >= MAX_HOSTCACHE_LEN);
  168. if(len > (buflen - 7))
  169. len = buflen - 7;
  170. /* store and lower case the name */
  171. while(len--) {
  172. *ptr++ = Curl_raw_tolower(*name++);
  173. olen++;
  174. }
  175. olen += msnprintf(ptr, 7, ":%u", port);
  176. return olen;
  177. }
  178. struct hostcache_prune_data {
  179. time_t now;
  180. time_t oldest; /* oldest time in cache not pruned. */
  181. int cache_timeout;
  182. };
  183. /*
  184. * This function is set as a callback to be called for every entry in the DNS
  185. * cache when we want to prune old unused entries.
  186. *
  187. * Returning non-zero means remove the entry, return 0 to keep it in the
  188. * cache.
  189. */
  190. static int
  191. hostcache_timestamp_remove(void *datap, void *hc)
  192. {
  193. struct hostcache_prune_data *prune =
  194. (struct hostcache_prune_data *) datap;
  195. struct Curl_dns_entry *c = (struct Curl_dns_entry *) hc;
  196. if(c->timestamp) {
  197. /* age in seconds */
  198. time_t age = prune->now - c->timestamp;
  199. if(age >= prune->cache_timeout)
  200. return TRUE;
  201. if(age > prune->oldest)
  202. prune->oldest = age;
  203. }
  204. return FALSE;
  205. }
  206. /*
  207. * Prune the DNS cache. This assumes that a lock has already been taken.
  208. * Returns the 'age' of the oldest still kept entry.
  209. */
  210. static time_t
  211. hostcache_prune(struct Curl_hash *hostcache, int cache_timeout,
  212. time_t now)
  213. {
  214. struct hostcache_prune_data user;
  215. user.cache_timeout = cache_timeout;
  216. user.now = now;
  217. user.oldest = 0;
  218. Curl_hash_clean_with_criterium(hostcache,
  219. (void *) &user,
  220. hostcache_timestamp_remove);
  221. return user.oldest;
  222. }
  223. /*
  224. * Library-wide function for pruning the DNS cache. This function takes and
  225. * returns the appropriate locks.
  226. */
  227. void Curl_hostcache_prune(struct Curl_easy *data)
  228. {
  229. time_t now;
  230. /* the timeout may be set -1 (forever) */
  231. int timeout = data->set.dns_cache_timeout;
  232. if(!data->dns.hostcache)
  233. /* NULL hostcache means we can't do it */
  234. return;
  235. if(data->share)
  236. Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
  237. time(&now);
  238. do {
  239. /* Remove outdated and unused entries from the hostcache */
  240. time_t oldest = hostcache_prune(data->dns.hostcache, timeout, now);
  241. if(oldest < INT_MAX)
  242. timeout = (int)oldest; /* we know it fits */
  243. else
  244. timeout = INT_MAX - 1;
  245. /* if the cache size is still too big, use the oldest age as new
  246. prune limit */
  247. } while(timeout && (data->dns.hostcache->size > MAX_DNS_CACHE_SIZE));
  248. if(data->share)
  249. Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
  250. }
  251. #ifdef USE_ALARM_TIMEOUT
  252. /* Beware this is a global and unique instance. This is used to store the
  253. return address that we can jump back to from inside a signal handler. This
  254. is not thread-safe stuff. */
  255. static sigjmp_buf curl_jmpenv;
  256. static curl_simple_lock curl_jmpenv_lock;
  257. #endif
  258. /* lookup address, returns entry if found and not stale */
  259. static struct Curl_dns_entry *fetch_addr(struct Curl_easy *data,
  260. const char *hostname,
  261. int port)
  262. {
  263. struct Curl_dns_entry *dns = NULL;
  264. char entry_id[MAX_HOSTCACHE_LEN];
  265. /* Create an entry id, based upon the hostname and port */
  266. size_t entry_len = create_hostcache_id(hostname, 0, port,
  267. entry_id, sizeof(entry_id));
  268. /* See if its already in our dns cache */
  269. dns = Curl_hash_pick(data->dns.hostcache, entry_id, entry_len + 1);
  270. /* No entry found in cache, check if we might have a wildcard entry */
  271. if(!dns && data->state.wildcard_resolve) {
  272. entry_len = create_hostcache_id("*", 1, port, entry_id, sizeof(entry_id));
  273. /* See if it's already in our dns cache */
  274. dns = Curl_hash_pick(data->dns.hostcache, entry_id, entry_len + 1);
  275. }
  276. if(dns && (data->set.dns_cache_timeout != -1)) {
  277. /* See whether the returned entry is stale. Done before we release lock */
  278. struct hostcache_prune_data user;
  279. time(&user.now);
  280. user.cache_timeout = data->set.dns_cache_timeout;
  281. user.oldest = 0;
  282. if(hostcache_timestamp_remove(&user, dns)) {
  283. infof(data, "Hostname in DNS cache was stale, zapped");
  284. dns = NULL; /* the memory deallocation is being handled by the hash */
  285. Curl_hash_delete(data->dns.hostcache, entry_id, entry_len + 1);
  286. }
  287. }
  288. /* See if the returned entry matches the required resolve mode */
  289. if(dns && data->conn->ip_version != CURL_IPRESOLVE_WHATEVER) {
  290. int pf = PF_INET;
  291. bool found = false;
  292. struct Curl_addrinfo *addr = dns->addr;
  293. #ifdef PF_INET6
  294. if(data->conn->ip_version == CURL_IPRESOLVE_V6)
  295. pf = PF_INET6;
  296. #endif
  297. while(addr) {
  298. if(addr->ai_family == pf) {
  299. found = true;
  300. break;
  301. }
  302. addr = addr->ai_next;
  303. }
  304. if(!found) {
  305. infof(data, "Hostname in DNS cache doesn't have needed family, zapped");
  306. dns = NULL; /* the memory deallocation is being handled by the hash */
  307. Curl_hash_delete(data->dns.hostcache, entry_id, entry_len + 1);
  308. }
  309. }
  310. return dns;
  311. }
  312. /*
  313. * Curl_fetch_addr() fetches a 'Curl_dns_entry' already in the DNS cache.
  314. *
  315. * Curl_resolv() checks initially and multi_runsingle() checks each time
  316. * it discovers the handle in the state WAITRESOLVE whether the hostname
  317. * has already been resolved and the address has already been stored in
  318. * the DNS cache. This short circuits waiting for a lot of pending
  319. * lookups for the same hostname requested by different handles.
  320. *
  321. * Returns the Curl_dns_entry entry pointer or NULL if not in the cache.
  322. *
  323. * The returned data *MUST* be "unlocked" with Curl_resolv_unlock() after
  324. * use, or we'll leak memory!
  325. */
  326. struct Curl_dns_entry *
  327. Curl_fetch_addr(struct Curl_easy *data,
  328. const char *hostname,
  329. int port)
  330. {
  331. struct Curl_dns_entry *dns = NULL;
  332. if(data->share)
  333. Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
  334. dns = fetch_addr(data, hostname, port);
  335. if(dns)
  336. dns->inuse++; /* we use it! */
  337. if(data->share)
  338. Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
  339. return dns;
  340. }
  341. #ifndef CURL_DISABLE_SHUFFLE_DNS
  342. UNITTEST CURLcode Curl_shuffle_addr(struct Curl_easy *data,
  343. struct Curl_addrinfo **addr);
  344. /*
  345. * Curl_shuffle_addr() shuffles the order of addresses in a 'Curl_addrinfo'
  346. * struct by re-linking its linked list.
  347. *
  348. * The addr argument should be the address of a pointer to the head node of a
  349. * `Curl_addrinfo` list and it will be modified to point to the new head after
  350. * shuffling.
  351. *
  352. * Not declared static only to make it easy to use in a unit test!
  353. *
  354. * @unittest: 1608
  355. */
  356. UNITTEST CURLcode Curl_shuffle_addr(struct Curl_easy *data,
  357. struct Curl_addrinfo **addr)
  358. {
  359. CURLcode result = CURLE_OK;
  360. const int num_addrs = num_addresses(*addr);
  361. if(num_addrs > 1) {
  362. struct Curl_addrinfo **nodes;
  363. infof(data, "Shuffling %i addresses", num_addrs);
  364. nodes = malloc(num_addrs*sizeof(*nodes));
  365. if(nodes) {
  366. int i;
  367. unsigned int *rnd;
  368. const size_t rnd_size = num_addrs * sizeof(*rnd);
  369. /* build a plain array of Curl_addrinfo pointers */
  370. nodes[0] = *addr;
  371. for(i = 1; i < num_addrs; i++) {
  372. nodes[i] = nodes[i-1]->ai_next;
  373. }
  374. rnd = malloc(rnd_size);
  375. if(rnd) {
  376. /* Fisher-Yates shuffle */
  377. if(Curl_rand(data, (unsigned char *)rnd, rnd_size) == CURLE_OK) {
  378. struct Curl_addrinfo *swap_tmp;
  379. for(i = num_addrs - 1; i > 0; i--) {
  380. swap_tmp = nodes[rnd[i] % (i + 1)];
  381. nodes[rnd[i] % (i + 1)] = nodes[i];
  382. nodes[i] = swap_tmp;
  383. }
  384. /* relink list in the new order */
  385. for(i = 1; i < num_addrs; i++) {
  386. nodes[i-1]->ai_next = nodes[i];
  387. }
  388. nodes[num_addrs-1]->ai_next = NULL;
  389. *addr = nodes[0];
  390. }
  391. free(rnd);
  392. }
  393. else
  394. result = CURLE_OUT_OF_MEMORY;
  395. free(nodes);
  396. }
  397. else
  398. result = CURLE_OUT_OF_MEMORY;
  399. }
  400. return result;
  401. }
  402. #endif
  403. /*
  404. * Curl_cache_addr() stores a 'Curl_addrinfo' struct in the DNS cache.
  405. *
  406. * When calling Curl_resolv() has resulted in a response with a returned
  407. * address, we call this function to store the information in the dns
  408. * cache etc
  409. *
  410. * Returns the Curl_dns_entry entry pointer or NULL if the storage failed.
  411. */
  412. struct Curl_dns_entry *
  413. Curl_cache_addr(struct Curl_easy *data,
  414. struct Curl_addrinfo *addr,
  415. const char *hostname,
  416. size_t hostlen, /* length or zero */
  417. int port)
  418. {
  419. char entry_id[MAX_HOSTCACHE_LEN];
  420. size_t entry_len;
  421. struct Curl_dns_entry *dns;
  422. struct Curl_dns_entry *dns2;
  423. #ifndef CURL_DISABLE_SHUFFLE_DNS
  424. /* shuffle addresses if requested */
  425. if(data->set.dns_shuffle_addresses) {
  426. CURLcode result = Curl_shuffle_addr(data, &addr);
  427. if(result)
  428. return NULL;
  429. }
  430. #endif
  431. /* Create a new cache entry */
  432. dns = calloc(1, sizeof(struct Curl_dns_entry));
  433. if(!dns) {
  434. return NULL;
  435. }
  436. /* Create an entry id, based upon the hostname and port */
  437. entry_len = create_hostcache_id(hostname, hostlen, port,
  438. entry_id, sizeof(entry_id));
  439. dns->inuse = 1; /* the cache has the first reference */
  440. dns->addr = addr; /* this is the address(es) */
  441. time(&dns->timestamp);
  442. if(dns->timestamp == 0)
  443. dns->timestamp = 1; /* zero indicates permanent CURLOPT_RESOLVE entry */
  444. /* Store the resolved data in our DNS cache. */
  445. dns2 = Curl_hash_add(data->dns.hostcache, entry_id, entry_len + 1,
  446. (void *)dns);
  447. if(!dns2) {
  448. free(dns);
  449. return NULL;
  450. }
  451. dns = dns2;
  452. dns->inuse++; /* mark entry as in-use */
  453. return dns;
  454. }
  455. #ifdef ENABLE_IPV6
  456. /* return a static IPv6 ::1 for the name */
  457. static struct Curl_addrinfo *get_localhost6(int port, const char *name)
  458. {
  459. struct Curl_addrinfo *ca;
  460. const size_t ss_size = sizeof(struct sockaddr_in6);
  461. const size_t hostlen = strlen(name);
  462. struct sockaddr_in6 sa6;
  463. unsigned char ipv6[16];
  464. unsigned short port16 = (unsigned short)(port & 0xffff);
  465. ca = calloc(sizeof(struct Curl_addrinfo) + ss_size + hostlen + 1, 1);
  466. if(!ca)
  467. return NULL;
  468. sa6.sin6_family = AF_INET6;
  469. sa6.sin6_port = htons(port16);
  470. sa6.sin6_flowinfo = 0;
  471. sa6.sin6_scope_id = 0;
  472. if(Curl_inet_pton(AF_INET6, "::1", ipv6) < 1)
  473. return NULL;
  474. memcpy(&sa6.sin6_addr, ipv6, sizeof(ipv6));
  475. ca->ai_flags = 0;
  476. ca->ai_family = AF_INET6;
  477. ca->ai_socktype = SOCK_STREAM;
  478. ca->ai_protocol = IPPROTO_TCP;
  479. ca->ai_addrlen = (curl_socklen_t)ss_size;
  480. ca->ai_next = NULL;
  481. ca->ai_addr = (void *)((char *)ca + sizeof(struct Curl_addrinfo));
  482. memcpy(ca->ai_addr, &sa6, ss_size);
  483. ca->ai_canonname = (char *)ca->ai_addr + ss_size;
  484. strcpy(ca->ai_canonname, name);
  485. return ca;
  486. }
  487. #else
  488. #define get_localhost6(x,y) NULL
  489. #endif
  490. /* return a static IPv4 127.0.0.1 for the given name */
  491. static struct Curl_addrinfo *get_localhost(int port, const char *name)
  492. {
  493. struct Curl_addrinfo *ca;
  494. struct Curl_addrinfo *ca6;
  495. const size_t ss_size = sizeof(struct sockaddr_in);
  496. const size_t hostlen = strlen(name);
  497. struct sockaddr_in sa;
  498. unsigned int ipv4;
  499. unsigned short port16 = (unsigned short)(port & 0xffff);
  500. /* memset to clear the sa.sin_zero field */
  501. memset(&sa, 0, sizeof(sa));
  502. sa.sin_family = AF_INET;
  503. sa.sin_port = htons(port16);
  504. if(Curl_inet_pton(AF_INET, "127.0.0.1", (char *)&ipv4) < 1)
  505. return NULL;
  506. memcpy(&sa.sin_addr, &ipv4, sizeof(ipv4));
  507. ca = calloc(sizeof(struct Curl_addrinfo) + ss_size + hostlen + 1, 1);
  508. if(!ca)
  509. return NULL;
  510. ca->ai_flags = 0;
  511. ca->ai_family = AF_INET;
  512. ca->ai_socktype = SOCK_STREAM;
  513. ca->ai_protocol = IPPROTO_TCP;
  514. ca->ai_addrlen = (curl_socklen_t)ss_size;
  515. ca->ai_addr = (void *)((char *)ca + sizeof(struct Curl_addrinfo));
  516. memcpy(ca->ai_addr, &sa, ss_size);
  517. ca->ai_canonname = (char *)ca->ai_addr + ss_size;
  518. strcpy(ca->ai_canonname, name);
  519. ca6 = get_localhost6(port, name);
  520. if(!ca6)
  521. return ca;
  522. ca6->ai_next = ca;
  523. return ca6;
  524. }
  525. #ifdef ENABLE_IPV6
  526. /*
  527. * Curl_ipv6works() returns TRUE if IPv6 seems to work.
  528. */
  529. bool Curl_ipv6works(struct Curl_easy *data)
  530. {
  531. if(data) {
  532. /* the nature of most system is that IPv6 status doesn't come and go
  533. during a program's lifetime so we only probe the first time and then we
  534. have the info kept for fast re-use */
  535. DEBUGASSERT(data);
  536. DEBUGASSERT(data->multi);
  537. if(data->multi->ipv6_up == IPV6_UNKNOWN) {
  538. bool works = Curl_ipv6works(NULL);
  539. data->multi->ipv6_up = works ? IPV6_WORKS : IPV6_DEAD;
  540. }
  541. return data->multi->ipv6_up == IPV6_WORKS;
  542. }
  543. else {
  544. int ipv6_works = -1;
  545. /* probe to see if we have a working IPv6 stack */
  546. curl_socket_t s = socket(PF_INET6, SOCK_DGRAM, 0);
  547. if(s == CURL_SOCKET_BAD)
  548. /* an IPv6 address was requested but we can't get/use one */
  549. ipv6_works = 0;
  550. else {
  551. ipv6_works = 1;
  552. sclose(s);
  553. }
  554. return (ipv6_works>0)?TRUE:FALSE;
  555. }
  556. }
  557. #endif /* ENABLE_IPV6 */
  558. /*
  559. * Curl_host_is_ipnum() returns TRUE if the given string is a numerical IPv4
  560. * (or IPv6 if supported) address.
  561. */
  562. bool Curl_host_is_ipnum(const char *hostname)
  563. {
  564. struct in_addr in;
  565. #ifdef ENABLE_IPV6
  566. struct in6_addr in6;
  567. #endif
  568. if(Curl_inet_pton(AF_INET, hostname, &in) > 0
  569. #ifdef ENABLE_IPV6
  570. || Curl_inet_pton(AF_INET6, hostname, &in6) > 0
  571. #endif
  572. )
  573. return TRUE;
  574. return FALSE;
  575. }
  576. /* return TRUE if 'part' is a case insensitive tail of 'full' */
  577. static bool tailmatch(const char *full, const char *part)
  578. {
  579. size_t plen = strlen(part);
  580. size_t flen = strlen(full);
  581. if(plen > flen)
  582. return FALSE;
  583. return strncasecompare(part, &full[flen - plen], plen);
  584. }
  585. /*
  586. * Curl_resolv() is the main name resolve function within libcurl. It resolves
  587. * a name and returns a pointer to the entry in the 'entry' argument (if one
  588. * is provided). This function might return immediately if we're using asynch
  589. * resolves. See the return codes.
  590. *
  591. * The cache entry we return will get its 'inuse' counter increased when this
  592. * function is used. You MUST call Curl_resolv_unlock() later (when you're
  593. * done using this struct) to decrease the counter again.
  594. *
  595. * Return codes:
  596. *
  597. * CURLRESOLV_ERROR (-1) = error, no pointer
  598. * CURLRESOLV_RESOLVED (0) = OK, pointer provided
  599. * CURLRESOLV_PENDING (1) = waiting for response, no pointer
  600. */
  601. enum resolve_t Curl_resolv(struct Curl_easy *data,
  602. const char *hostname,
  603. int port,
  604. bool allowDOH,
  605. struct Curl_dns_entry **entry)
  606. {
  607. struct Curl_dns_entry *dns = NULL;
  608. CURLcode result;
  609. enum resolve_t rc = CURLRESOLV_ERROR; /* default to failure */
  610. struct connectdata *conn = data->conn;
  611. /* We should intentionally error and not resolve .onion TLDs */
  612. size_t hostname_len = strlen(hostname);
  613. if(hostname_len >= 7 &&
  614. (curl_strequal(&hostname[hostname_len - 6], ".onion") ||
  615. curl_strequal(&hostname[hostname_len - 7], ".onion."))) {
  616. failf(data, "Not resolving .onion address (RFC 7686)");
  617. return CURLRESOLV_ERROR;
  618. }
  619. *entry = NULL;
  620. #ifndef CURL_DISABLE_DOH
  621. conn->bits.doh = FALSE; /* default is not */
  622. #else
  623. (void)allowDOH;
  624. #endif
  625. if(data->share)
  626. Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
  627. dns = fetch_addr(data, hostname, port);
  628. if(dns) {
  629. infof(data, "Hostname %s was found in DNS cache", hostname);
  630. dns->inuse++; /* we use it! */
  631. rc = CURLRESOLV_RESOLVED;
  632. }
  633. if(data->share)
  634. Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
  635. if(!dns) {
  636. /* The entry was not in the cache. Resolve it to IP address */
  637. struct Curl_addrinfo *addr = NULL;
  638. int respwait = 0;
  639. #if !defined(CURL_DISABLE_DOH) || !defined(USE_RESOLVE_ON_IPS)
  640. struct in_addr in;
  641. #endif
  642. #ifndef CURL_DISABLE_DOH
  643. #ifndef USE_RESOLVE_ON_IPS
  644. const
  645. #endif
  646. bool ipnum = FALSE;
  647. #endif
  648. /* notify the resolver start callback */
  649. if(data->set.resolver_start) {
  650. int st;
  651. Curl_set_in_callback(data, true);
  652. st = data->set.resolver_start(
  653. #ifdef USE_CURL_ASYNC
  654. data->state.async.resolver,
  655. #else
  656. NULL,
  657. #endif
  658. NULL,
  659. data->set.resolver_start_client);
  660. Curl_set_in_callback(data, false);
  661. if(st)
  662. return CURLRESOLV_ERROR;
  663. }
  664. #ifndef USE_RESOLVE_ON_IPS
  665. /* First check if this is an IPv4 address string */
  666. if(Curl_inet_pton(AF_INET, hostname, &in) > 0)
  667. /* This is a dotted IP address 123.123.123.123-style */
  668. addr = Curl_ip2addr(AF_INET, &in, hostname, port);
  669. #ifdef ENABLE_IPV6
  670. if(!addr) {
  671. struct in6_addr in6;
  672. /* check if this is an IPv6 address string */
  673. if(Curl_inet_pton(AF_INET6, hostname, &in6) > 0)
  674. /* This is an IPv6 address literal */
  675. addr = Curl_ip2addr(AF_INET6, &in6, hostname, port);
  676. }
  677. #endif /* ENABLE_IPV6 */
  678. #else /* if USE_RESOLVE_ON_IPS */
  679. #ifndef CURL_DISABLE_DOH
  680. /* First check if this is an IPv4 address string */
  681. if(Curl_inet_pton(AF_INET, hostname, &in) > 0)
  682. /* This is a dotted IP address 123.123.123.123-style */
  683. ipnum = TRUE;
  684. #ifdef ENABLE_IPV6
  685. else {
  686. struct in6_addr in6;
  687. /* check if this is an IPv6 address string */
  688. if(Curl_inet_pton(AF_INET6, hostname, &in6) > 0)
  689. /* This is an IPv6 address literal */
  690. ipnum = TRUE;
  691. }
  692. #endif /* ENABLE_IPV6 */
  693. #endif /* CURL_DISABLE_DOH */
  694. #endif /* !USE_RESOLVE_ON_IPS */
  695. if(!addr) {
  696. if(conn->ip_version == CURL_IPRESOLVE_V6 && !Curl_ipv6works(data))
  697. return CURLRESOLV_ERROR;
  698. if(strcasecompare(hostname, "localhost") ||
  699. tailmatch(hostname, ".localhost"))
  700. addr = get_localhost(port, hostname);
  701. #ifndef CURL_DISABLE_DOH
  702. else if(allowDOH && data->set.doh && !ipnum)
  703. addr = Curl_doh(data, hostname, port, &respwait);
  704. #endif
  705. else {
  706. /* Check what IP specifics the app has requested and if we can provide
  707. * it. If not, bail out. */
  708. if(!Curl_ipvalid(data, conn))
  709. return CURLRESOLV_ERROR;
  710. /* If Curl_getaddrinfo() returns NULL, 'respwait' might be set to a
  711. non-zero value indicating that we need to wait for the response to
  712. the resolve call */
  713. addr = Curl_getaddrinfo(data, hostname, port, &respwait);
  714. }
  715. }
  716. if(!addr) {
  717. if(respwait) {
  718. /* the response to our resolve call will come asynchronously at
  719. a later time, good or bad */
  720. /* First, check that we haven't received the info by now */
  721. result = Curl_resolv_check(data, &dns);
  722. if(result) /* error detected */
  723. return CURLRESOLV_ERROR;
  724. if(dns)
  725. rc = CURLRESOLV_RESOLVED; /* pointer provided */
  726. else
  727. rc = CURLRESOLV_PENDING; /* no info yet */
  728. }
  729. }
  730. else {
  731. if(data->share)
  732. Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
  733. /* we got a response, store it in the cache */
  734. dns = Curl_cache_addr(data, addr, hostname, 0, port);
  735. if(data->share)
  736. Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
  737. if(!dns)
  738. /* returned failure, bail out nicely */
  739. Curl_freeaddrinfo(addr);
  740. else
  741. rc = CURLRESOLV_RESOLVED;
  742. }
  743. }
  744. *entry = dns;
  745. return rc;
  746. }
  747. #ifdef USE_ALARM_TIMEOUT
  748. /*
  749. * This signal handler jumps back into the main libcurl code and continues
  750. * execution. This effectively causes the remainder of the application to run
  751. * within a signal handler which is nonportable and could lead to problems.
  752. */
  753. static
  754. void alarmfunc(int sig)
  755. {
  756. (void)sig;
  757. siglongjmp(curl_jmpenv, 1);
  758. }
  759. #endif /* USE_ALARM_TIMEOUT */
  760. /*
  761. * Curl_resolv_timeout() is the same as Curl_resolv() but specifies a
  762. * timeout. This function might return immediately if we're using asynch
  763. * resolves. See the return codes.
  764. *
  765. * The cache entry we return will get its 'inuse' counter increased when this
  766. * function is used. You MUST call Curl_resolv_unlock() later (when you're
  767. * done using this struct) to decrease the counter again.
  768. *
  769. * If built with a synchronous resolver and use of signals is not
  770. * disabled by the application, then a nonzero timeout will cause a
  771. * timeout after the specified number of milliseconds. Otherwise, timeout
  772. * is ignored.
  773. *
  774. * Return codes:
  775. *
  776. * CURLRESOLV_TIMEDOUT(-2) = warning, time too short or previous alarm expired
  777. * CURLRESOLV_ERROR (-1) = error, no pointer
  778. * CURLRESOLV_RESOLVED (0) = OK, pointer provided
  779. * CURLRESOLV_PENDING (1) = waiting for response, no pointer
  780. */
  781. enum resolve_t Curl_resolv_timeout(struct Curl_easy *data,
  782. const char *hostname,
  783. int port,
  784. struct Curl_dns_entry **entry,
  785. timediff_t timeoutms)
  786. {
  787. #ifdef USE_ALARM_TIMEOUT
  788. #ifdef HAVE_SIGACTION
  789. struct sigaction keep_sigact; /* store the old struct here */
  790. volatile bool keep_copysig = FALSE; /* whether old sigact has been saved */
  791. struct sigaction sigact;
  792. #else
  793. #ifdef HAVE_SIGNAL
  794. void (*keep_sigact)(int); /* store the old handler here */
  795. #endif /* HAVE_SIGNAL */
  796. #endif /* HAVE_SIGACTION */
  797. volatile long timeout;
  798. volatile unsigned int prev_alarm = 0;
  799. #endif /* USE_ALARM_TIMEOUT */
  800. enum resolve_t rc;
  801. *entry = NULL;
  802. if(timeoutms < 0)
  803. /* got an already expired timeout */
  804. return CURLRESOLV_TIMEDOUT;
  805. #ifdef USE_ALARM_TIMEOUT
  806. if(data->set.no_signal)
  807. /* Ignore the timeout when signals are disabled */
  808. timeout = 0;
  809. else
  810. timeout = (timeoutms > LONG_MAX) ? LONG_MAX : (long)timeoutms;
  811. if(!timeout)
  812. /* USE_ALARM_TIMEOUT defined, but no timeout actually requested */
  813. return Curl_resolv(data, hostname, port, TRUE, entry);
  814. if(timeout < 1000) {
  815. /* The alarm() function only provides integer second resolution, so if
  816. we want to wait less than one second we must bail out already now. */
  817. failf(data,
  818. "remaining timeout of %ld too small to resolve via SIGALRM method",
  819. timeout);
  820. return CURLRESOLV_TIMEDOUT;
  821. }
  822. /* This allows us to time-out from the name resolver, as the timeout
  823. will generate a signal and we will siglongjmp() from that here.
  824. This technique has problems (see alarmfunc).
  825. This should be the last thing we do before calling Curl_resolv(),
  826. as otherwise we'd have to worry about variables that get modified
  827. before we invoke Curl_resolv() (and thus use "volatile"). */
  828. curl_simple_lock_lock(&curl_jmpenv_lock);
  829. if(sigsetjmp(curl_jmpenv, 1)) {
  830. /* this is coming from a siglongjmp() after an alarm signal */
  831. failf(data, "name lookup timed out");
  832. rc = CURLRESOLV_ERROR;
  833. goto clean_up;
  834. }
  835. else {
  836. /*************************************************************
  837. * Set signal handler to catch SIGALRM
  838. * Store the old value to be able to set it back later!
  839. *************************************************************/
  840. #ifdef HAVE_SIGACTION
  841. sigaction(SIGALRM, NULL, &sigact);
  842. keep_sigact = sigact;
  843. keep_copysig = TRUE; /* yes, we have a copy */
  844. sigact.sa_handler = alarmfunc;
  845. #ifdef SA_RESTART
  846. /* HPUX doesn't have SA_RESTART but defaults to that behavior! */
  847. sigact.sa_flags &= ~SA_RESTART;
  848. #endif
  849. /* now set the new struct */
  850. sigaction(SIGALRM, &sigact, NULL);
  851. #else /* HAVE_SIGACTION */
  852. /* no sigaction(), revert to the much lamer signal() */
  853. #ifdef HAVE_SIGNAL
  854. keep_sigact = signal(SIGALRM, alarmfunc);
  855. #endif
  856. #endif /* HAVE_SIGACTION */
  857. /* alarm() makes a signal get sent when the timeout fires off, and that
  858. will abort system calls */
  859. prev_alarm = alarm(curlx_sltoui(timeout/1000L));
  860. }
  861. #else
  862. #ifndef CURLRES_ASYNCH
  863. if(timeoutms)
  864. infof(data, "timeout on name lookup is not supported");
  865. #else
  866. (void)timeoutms; /* timeoutms not used with an async resolver */
  867. #endif
  868. #endif /* USE_ALARM_TIMEOUT */
  869. /* Perform the actual name resolution. This might be interrupted by an
  870. * alarm if it takes too long.
  871. */
  872. rc = Curl_resolv(data, hostname, port, TRUE, entry);
  873. #ifdef USE_ALARM_TIMEOUT
  874. clean_up:
  875. if(!prev_alarm)
  876. /* deactivate a possibly active alarm before uninstalling the handler */
  877. alarm(0);
  878. #ifdef HAVE_SIGACTION
  879. if(keep_copysig) {
  880. /* we got a struct as it looked before, now put that one back nice
  881. and clean */
  882. sigaction(SIGALRM, &keep_sigact, NULL); /* put it back */
  883. }
  884. #else
  885. #ifdef HAVE_SIGNAL
  886. /* restore the previous SIGALRM handler */
  887. signal(SIGALRM, keep_sigact);
  888. #endif
  889. #endif /* HAVE_SIGACTION */
  890. curl_simple_lock_unlock(&curl_jmpenv_lock);
  891. /* switch back the alarm() to either zero or to what it was before minus
  892. the time we spent until now! */
  893. if(prev_alarm) {
  894. /* there was an alarm() set before us, now put it back */
  895. timediff_t elapsed_secs = Curl_timediff(Curl_now(),
  896. data->conn->created) / 1000;
  897. /* the alarm period is counted in even number of seconds */
  898. unsigned long alarm_set = (unsigned long)(prev_alarm - elapsed_secs);
  899. if(!alarm_set ||
  900. ((alarm_set >= 0x80000000) && (prev_alarm < 0x80000000)) ) {
  901. /* if the alarm time-left reached zero or turned "negative" (counted
  902. with unsigned values), we should fire off a SIGALRM here, but we
  903. won't, and zero would be to switch it off so we never set it to
  904. less than 1! */
  905. alarm(1);
  906. rc = CURLRESOLV_TIMEDOUT;
  907. failf(data, "Previous alarm fired off");
  908. }
  909. else
  910. alarm((unsigned int)alarm_set);
  911. }
  912. #endif /* USE_ALARM_TIMEOUT */
  913. return rc;
  914. }
  915. /*
  916. * Curl_resolv_unlock() unlocks the given cached DNS entry. When this has been
  917. * made, the struct may be destroyed due to pruning. It is important that only
  918. * one unlock is made for each Curl_resolv() call.
  919. *
  920. * May be called with 'data' == NULL for global cache.
  921. */
  922. void Curl_resolv_unlock(struct Curl_easy *data, struct Curl_dns_entry *dns)
  923. {
  924. if(data && data->share)
  925. Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
  926. freednsentry(dns);
  927. if(data && data->share)
  928. Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
  929. }
  930. /*
  931. * File-internal: release cache dns entry reference, free if inuse drops to 0
  932. */
  933. static void freednsentry(void *freethis)
  934. {
  935. struct Curl_dns_entry *dns = (struct Curl_dns_entry *) freethis;
  936. DEBUGASSERT(dns && (dns->inuse>0));
  937. dns->inuse--;
  938. if(dns->inuse == 0) {
  939. Curl_freeaddrinfo(dns->addr);
  940. free(dns);
  941. }
  942. }
  943. /*
  944. * Curl_init_dnscache() inits a new DNS cache.
  945. */
  946. void Curl_init_dnscache(struct Curl_hash *hash, int size)
  947. {
  948. Curl_hash_init(hash, size, Curl_hash_str, Curl_str_key_compare,
  949. freednsentry);
  950. }
  951. /*
  952. * Curl_hostcache_clean()
  953. *
  954. * This _can_ be called with 'data' == NULL but then of course no locking
  955. * can be done!
  956. */
  957. void Curl_hostcache_clean(struct Curl_easy *data,
  958. struct Curl_hash *hash)
  959. {
  960. if(data && data->share)
  961. Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
  962. Curl_hash_clean(hash);
  963. if(data && data->share)
  964. Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
  965. }
  966. CURLcode Curl_loadhostpairs(struct Curl_easy *data)
  967. {
  968. struct curl_slist *hostp;
  969. char *host_end;
  970. /* Default is no wildcard found */
  971. data->state.wildcard_resolve = false;
  972. for(hostp = data->state.resolve; hostp; hostp = hostp->next) {
  973. char entry_id[MAX_HOSTCACHE_LEN];
  974. if(!hostp->data)
  975. continue;
  976. if(hostp->data[0] == '-') {
  977. unsigned long num = 0;
  978. size_t entry_len;
  979. size_t hlen = 0;
  980. host_end = strchr(&hostp->data[1], ':');
  981. if(host_end) {
  982. hlen = host_end - &hostp->data[1];
  983. num = strtoul(++host_end, NULL, 10);
  984. if(!hlen || (num > 0xffff))
  985. host_end = NULL;
  986. }
  987. if(!host_end) {
  988. infof(data, "Bad syntax CURLOPT_RESOLVE removal entry '%s'",
  989. hostp->data);
  990. continue;
  991. }
  992. /* Create an entry id, based upon the hostname and port */
  993. entry_len = create_hostcache_id(&hostp->data[1], hlen, (int)num,
  994. entry_id, sizeof(entry_id));
  995. if(data->share)
  996. Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
  997. /* delete entry, ignore if it didn't exist */
  998. Curl_hash_delete(data->dns.hostcache, entry_id, entry_len + 1);
  999. if(data->share)
  1000. Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
  1001. }
  1002. else {
  1003. struct Curl_dns_entry *dns;
  1004. struct Curl_addrinfo *head = NULL, *tail = NULL;
  1005. size_t entry_len;
  1006. char address[64];
  1007. #if !defined(CURL_DISABLE_VERBOSE_STRINGS)
  1008. char *addresses = NULL;
  1009. #endif
  1010. char *addr_begin;
  1011. char *addr_end;
  1012. char *port_ptr;
  1013. int port = 0;
  1014. char *end_ptr;
  1015. bool permanent = TRUE;
  1016. unsigned long tmp_port;
  1017. bool error = true;
  1018. char *host_begin = hostp->data;
  1019. size_t hlen = 0;
  1020. if(host_begin[0] == '+') {
  1021. host_begin++;
  1022. permanent = FALSE;
  1023. }
  1024. host_end = strchr(host_begin, ':');
  1025. if(!host_end)
  1026. goto err;
  1027. hlen = host_end - host_begin;
  1028. port_ptr = host_end + 1;
  1029. tmp_port = strtoul(port_ptr, &end_ptr, 10);
  1030. if(tmp_port > USHRT_MAX || end_ptr == port_ptr || *end_ptr != ':')
  1031. goto err;
  1032. port = (int)tmp_port;
  1033. #if !defined(CURL_DISABLE_VERBOSE_STRINGS)
  1034. addresses = end_ptr + 1;
  1035. #endif
  1036. while(*end_ptr) {
  1037. size_t alen;
  1038. struct Curl_addrinfo *ai;
  1039. addr_begin = end_ptr + 1;
  1040. addr_end = strchr(addr_begin, ',');
  1041. if(!addr_end)
  1042. addr_end = addr_begin + strlen(addr_begin);
  1043. end_ptr = addr_end;
  1044. /* allow IP(v6) address within [brackets] */
  1045. if(*addr_begin == '[') {
  1046. if(addr_end == addr_begin || *(addr_end - 1) != ']')
  1047. goto err;
  1048. ++addr_begin;
  1049. --addr_end;
  1050. }
  1051. alen = addr_end - addr_begin;
  1052. if(!alen)
  1053. continue;
  1054. if(alen >= sizeof(address))
  1055. goto err;
  1056. memcpy(address, addr_begin, alen);
  1057. address[alen] = '\0';
  1058. #ifndef ENABLE_IPV6
  1059. if(strchr(address, ':')) {
  1060. infof(data, "Ignoring resolve address '%s', missing IPv6 support.",
  1061. address);
  1062. continue;
  1063. }
  1064. #endif
  1065. ai = Curl_str2addr(address, port);
  1066. if(!ai) {
  1067. infof(data, "Resolve address '%s' found illegal", address);
  1068. goto err;
  1069. }
  1070. if(tail) {
  1071. tail->ai_next = ai;
  1072. tail = tail->ai_next;
  1073. }
  1074. else {
  1075. head = tail = ai;
  1076. }
  1077. }
  1078. if(!head)
  1079. goto err;
  1080. error = false;
  1081. err:
  1082. if(error) {
  1083. failf(data, "Couldn't parse CURLOPT_RESOLVE entry '%s'",
  1084. hostp->data);
  1085. Curl_freeaddrinfo(head);
  1086. return CURLE_SETOPT_OPTION_SYNTAX;
  1087. }
  1088. /* Create an entry id, based upon the hostname and port */
  1089. entry_len = create_hostcache_id(host_begin, hlen, port,
  1090. entry_id, sizeof(entry_id));
  1091. if(data->share)
  1092. Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
  1093. /* See if it's already in our dns cache */
  1094. dns = Curl_hash_pick(data->dns.hostcache, entry_id, entry_len + 1);
  1095. if(dns) {
  1096. infof(data, "RESOLVE %.*s:%d is - old addresses discarded",
  1097. (int)hlen, host_begin, port);
  1098. /* delete old entry, there are two reasons for this
  1099. 1. old entry may have different addresses.
  1100. 2. even if entry with correct addresses is already in the cache,
  1101. but if it is close to expire, then by the time next http
  1102. request is made, it can get expired and pruned because old
  1103. entry is not necessarily marked as permanent.
  1104. 3. when adding a non-permanent entry, we want it to remove and
  1105. replace an existing permanent entry.
  1106. 4. when adding a non-permanent entry, we want it to get a "fresh"
  1107. timeout that starts _now_. */
  1108. Curl_hash_delete(data->dns.hostcache, entry_id, entry_len + 1);
  1109. }
  1110. /* put this new host in the cache */
  1111. dns = Curl_cache_addr(data, head, host_begin, hlen, port);
  1112. if(dns) {
  1113. if(permanent)
  1114. dns->timestamp = 0; /* mark as permanent */
  1115. /* release the returned reference; the cache itself will keep the
  1116. * entry alive: */
  1117. dns->inuse--;
  1118. }
  1119. if(data->share)
  1120. Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
  1121. if(!dns) {
  1122. Curl_freeaddrinfo(head);
  1123. return CURLE_OUT_OF_MEMORY;
  1124. }
  1125. infof(data, "Added %.*s:%d:%s to DNS cache%s",
  1126. (int)hlen, host_begin, port, addresses,
  1127. permanent ? "" : " (non-permanent)");
  1128. /* Wildcard hostname */
  1129. if((hlen == 1) && (host_begin[0] == '*')) {
  1130. infof(data, "RESOLVE *:%d using wildcard", port);
  1131. data->state.wildcard_resolve = true;
  1132. }
  1133. }
  1134. }
  1135. data->state.resolve = NULL; /* dealt with now */
  1136. return CURLE_OK;
  1137. }
  1138. CURLcode Curl_resolv_check(struct Curl_easy *data,
  1139. struct Curl_dns_entry **dns)
  1140. {
  1141. #if defined(CURL_DISABLE_DOH) && !defined(CURLRES_ASYNCH)
  1142. (void)data;
  1143. (void)dns;
  1144. #endif
  1145. #ifndef CURL_DISABLE_DOH
  1146. if(data->conn->bits.doh)
  1147. return Curl_doh_is_resolved(data, dns);
  1148. #endif
  1149. return Curl_resolver_is_resolved(data, dns);
  1150. }
  1151. int Curl_resolv_getsock(struct Curl_easy *data,
  1152. curl_socket_t *socks)
  1153. {
  1154. #ifdef CURLRES_ASYNCH
  1155. #ifndef CURL_DISABLE_DOH
  1156. if(data->conn->bits.doh)
  1157. /* nothing to wait for during DoH resolve, those handles have their own
  1158. sockets */
  1159. return GETSOCK_BLANK;
  1160. #endif
  1161. return Curl_resolver_getsock(data, socks);
  1162. #else
  1163. (void)data;
  1164. (void)socks;
  1165. return GETSOCK_BLANK;
  1166. #endif
  1167. }
  1168. /* Call this function after Curl_connect() has returned async=TRUE and
  1169. then a successful name resolve has been received.
  1170. Note: this function disconnects and frees the conn data in case of
  1171. resolve failure */
  1172. CURLcode Curl_once_resolved(struct Curl_easy *data, bool *protocol_done)
  1173. {
  1174. CURLcode result;
  1175. struct connectdata *conn = data->conn;
  1176. #ifdef USE_CURL_ASYNC
  1177. if(data->state.async.dns) {
  1178. conn->dns_entry = data->state.async.dns;
  1179. data->state.async.dns = NULL;
  1180. }
  1181. #endif
  1182. result = Curl_setup_conn(data, protocol_done);
  1183. if(result) {
  1184. Curl_detach_connection(data);
  1185. Curl_conncache_remove_conn(data, conn, TRUE);
  1186. Curl_disconnect(data, conn, TRUE);
  1187. }
  1188. return result;
  1189. }
  1190. /*
  1191. * Curl_resolver_error() calls failf() with the appropriate message after a
  1192. * resolve error
  1193. */
  1194. #ifdef USE_CURL_ASYNC
  1195. CURLcode Curl_resolver_error(struct Curl_easy *data)
  1196. {
  1197. const char *host_or_proxy;
  1198. CURLcode result;
  1199. #ifndef CURL_DISABLE_PROXY
  1200. struct connectdata *conn = data->conn;
  1201. if(conn->bits.httpproxy) {
  1202. host_or_proxy = "proxy";
  1203. result = CURLE_COULDNT_RESOLVE_PROXY;
  1204. }
  1205. else
  1206. #endif
  1207. {
  1208. host_or_proxy = "host";
  1209. result = CURLE_COULDNT_RESOLVE_HOST;
  1210. }
  1211. failf(data, "Could not resolve %s: %s", host_or_proxy,
  1212. data->state.async.hostname);
  1213. return result;
  1214. }
  1215. #endif /* USE_CURL_ASYNC */