hostip.c 39 KB

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