hostip.c 38 KB

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