hostip.c 39 KB

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