hostip.c 40 KB

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