2
0

hostip.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #ifdef HAVE_NETINET_IN_H
  26. #include <netinet/in.h>
  27. #endif
  28. #ifdef HAVE_NETINET_IN6_H
  29. #include <netinet/in6.h>
  30. #endif
  31. #ifdef HAVE_NETDB_H
  32. #include <netdb.h>
  33. #endif
  34. #ifdef HAVE_ARPA_INET_H
  35. #include <arpa/inet.h>
  36. #endif
  37. #ifdef __VMS
  38. #include <in.h>
  39. #include <inet.h>
  40. #endif
  41. #include <setjmp.h>
  42. #include <signal.h>
  43. #include "urldata.h"
  44. #include "sendf.h"
  45. #include "hostip.h"
  46. #include "hash.h"
  47. #include "rand.h"
  48. #include "share.h"
  49. #include "url.h"
  50. #include "inet_ntop.h"
  51. #include "inet_pton.h"
  52. #include "multiif.h"
  53. #include "doh.h"
  54. #include "warnless.h"
  55. #include "strcase.h"
  56. #include "easy_lock.h"
  57. /* The last 3 #include files should be in this order */
  58. #include "curl_printf.h"
  59. #include "curl_memory.h"
  60. #include "memdebug.h"
  61. #if defined(CURLRES_SYNCH) && \
  62. defined(HAVE_ALARM) && \
  63. defined(SIGALRM) && \
  64. defined(HAVE_SIGSETJMP) && \
  65. defined(GLOBAL_INIT_IS_THREADSAFE)
  66. /* alarm-based timeouts can only be used with all the dependencies satisfied */
  67. #define USE_ALARM_TIMEOUT
  68. #endif
  69. #define MAX_HOSTCACHE_LEN (255 + 7) /* max FQDN + colon + port number + zero */
  70. #define MAX_DNS_CACHE_SIZE 29999
  71. /*
  72. * hostip.c explained
  73. * ==================
  74. *
  75. * The main COMPILE-TIME DEFINES to keep in mind when reading the host*.c
  76. * source file are these:
  77. *
  78. * CURLRES_IPV6 - this host has getaddrinfo() and family, and thus we use
  79. * that. The host may not be able to resolve IPv6, but we do not really have to
  80. * take that into account. Hosts that are not IPv6-enabled have CURLRES_IPV4
  81. * defined.
  82. *
  83. * CURLRES_ARES - is defined if libcurl is built to use c-ares for
  84. * asynchronous name resolves. This can be Windows or *nix.
  85. *
  86. * CURLRES_THREADED - is defined if libcurl is built to run under (native)
  87. * Windows, and then the name resolve will be done in a new thread, and the
  88. * supported API will be the same as for ares-builds.
  89. *
  90. * If any of the two previous are defined, CURLRES_ASYNCH is defined too. If
  91. * libcurl is not built to use an asynchronous resolver, CURLRES_SYNCH is
  92. * defined.
  93. *
  94. * The host*.c sources files are split up like this:
  95. *
  96. * hostip.c - method-independent resolver functions and utility functions
  97. * hostasyn.c - functions for asynchronous name resolves
  98. * hostsyn.c - functions for synchronous name resolves
  99. * hostip4.c - IPv4 specific functions
  100. * hostip6.c - IPv6 specific functions
  101. *
  102. * The two asynchronous name resolver backends are implemented in:
  103. * asyn-ares.c - functions for ares-using name resolves
  104. * asyn-thread.c - functions for threaded name resolves
  105. * The hostip.h is the united header file for all this. It defines the
  106. * CURLRES_* defines based on the config*.h and curl_setup.h defines.
  107. */
  108. static void hostcache_unlink_entry(void *entry);
  109. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  110. static void show_resolve_info(struct Curl_easy *data,
  111. struct Curl_dns_entry *dns);
  112. #else
  113. #define show_resolve_info(x,y) Curl_nop_stmt
  114. #endif
  115. /*
  116. * Curl_printable_address() stores a printable version of the 1st address
  117. * given in the 'ai' argument. The result will be stored in the buf that is
  118. * bufsize bytes big.
  119. *
  120. * If the conversion fails, the target buffer is empty.
  121. */
  122. void Curl_printable_address(const struct Curl_addrinfo *ai, char *buf,
  123. size_t bufsize)
  124. {
  125. DEBUGASSERT(bufsize);
  126. buf[0] = 0;
  127. switch(ai->ai_family) {
  128. case AF_INET: {
  129. const struct sockaddr_in *sa4 = (const void *)ai->ai_addr;
  130. const struct in_addr *ipaddr4 = &sa4->sin_addr;
  131. (void)Curl_inet_ntop(ai->ai_family, (const void *)ipaddr4, buf, bufsize);
  132. break;
  133. }
  134. #ifdef USE_IPV6
  135. case AF_INET6: {
  136. const struct sockaddr_in6 *sa6 = (const void *)ai->ai_addr;
  137. const struct in6_addr *ipaddr6 = &sa6->sin6_addr;
  138. (void)Curl_inet_ntop(ai->ai_family, (const void *)ipaddr6, buf, bufsize);
  139. break;
  140. }
  141. #endif
  142. default:
  143. break;
  144. }
  145. }
  146. /*
  147. * Create a hostcache id string for the provided host + port, to be used by
  148. * the DNS caching. Without alloc. Return length of the id string.
  149. */
  150. static size_t
  151. create_hostcache_id(const char *name,
  152. size_t nlen, /* 0 or actual name length */
  153. int port, char *ptr, size_t buflen)
  154. {
  155. size_t len = nlen ? nlen : strlen(name);
  156. DEBUGASSERT(buflen >= MAX_HOSTCACHE_LEN);
  157. if(len > (buflen - 7))
  158. len = buflen - 7;
  159. /* store and lower case the name */
  160. Curl_strntolower(ptr, name, len);
  161. return msnprintf(&ptr[len], 7, ":%u", port) + len;
  162. }
  163. struct hostcache_prune_data {
  164. time_t now;
  165. time_t oldest; /* oldest time in cache not pruned. */
  166. int max_age_sec;
  167. };
  168. /*
  169. * This function is set as a callback to be called for every entry in the DNS
  170. * cache when we want to prune old unused entries.
  171. *
  172. * Returning non-zero means remove the entry, return 0 to keep it in the
  173. * cache.
  174. */
  175. static int
  176. hostcache_entry_is_stale(void *datap, void *hc)
  177. {
  178. struct hostcache_prune_data *prune =
  179. (struct hostcache_prune_data *) datap;
  180. struct Curl_dns_entry *dns = (struct Curl_dns_entry *) hc;
  181. if(dns->timestamp) {
  182. /* age in seconds */
  183. time_t age = prune->now - dns->timestamp;
  184. if(age >= prune->max_age_sec)
  185. return TRUE;
  186. if(age > prune->oldest)
  187. prune->oldest = age;
  188. }
  189. return FALSE;
  190. }
  191. /*
  192. * Prune the DNS cache. This assumes that a lock has already been taken.
  193. * Returns the 'age' of the oldest still kept entry.
  194. */
  195. static time_t
  196. hostcache_prune(struct Curl_hash *hostcache, int cache_timeout,
  197. time_t now)
  198. {
  199. struct hostcache_prune_data user;
  200. user.max_age_sec = cache_timeout;
  201. user.now = now;
  202. user.oldest = 0;
  203. Curl_hash_clean_with_criterium(hostcache,
  204. (void *) &user,
  205. hostcache_entry_is_stale);
  206. return user.oldest;
  207. }
  208. /*
  209. * Library-wide function for pruning the DNS cache. This function takes and
  210. * returns the appropriate locks.
  211. */
  212. void Curl_hostcache_prune(struct Curl_easy *data)
  213. {
  214. time_t now;
  215. /* the timeout may be set -1 (forever) */
  216. int timeout = data->set.dns_cache_timeout;
  217. if(!data->dns.hostcache)
  218. /* NULL hostcache means we cannot do it */
  219. return;
  220. if(data->share)
  221. Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
  222. now = time(NULL);
  223. do {
  224. /* Remove outdated and unused entries from the hostcache */
  225. time_t oldest = hostcache_prune(data->dns.hostcache, timeout, now);
  226. if(oldest < INT_MAX)
  227. timeout = (int)oldest; /* we know it fits */
  228. else
  229. timeout = INT_MAX - 1;
  230. /* if the cache size is still too big, use the oldest age as new
  231. prune limit */
  232. } while(timeout &&
  233. (Curl_hash_count(data->dns.hostcache) > MAX_DNS_CACHE_SIZE));
  234. if(data->share)
  235. Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
  236. }
  237. #ifdef USE_ALARM_TIMEOUT
  238. /* Beware this is a global and unique instance. This is used to store the
  239. return address that we can jump back to from inside a signal handler. This
  240. is not thread-safe stuff. */
  241. static sigjmp_buf curl_jmpenv;
  242. static curl_simple_lock curl_jmpenv_lock;
  243. #endif
  244. /* lookup address, returns entry if found and not stale */
  245. static struct Curl_dns_entry *fetch_addr(struct Curl_easy *data,
  246. const char *hostname,
  247. int port)
  248. {
  249. struct Curl_dns_entry *dns = NULL;
  250. char entry_id[MAX_HOSTCACHE_LEN];
  251. /* Create an entry id, based upon the hostname and port */
  252. size_t entry_len = create_hostcache_id(hostname, 0, port,
  253. entry_id, sizeof(entry_id));
  254. /* See if it is already in our dns cache */
  255. dns = Curl_hash_pick(data->dns.hostcache, entry_id, entry_len + 1);
  256. /* No entry found in cache, check if we might have a wildcard entry */
  257. if(!dns && data->state.wildcard_resolve) {
  258. entry_len = create_hostcache_id("*", 1, port, entry_id, sizeof(entry_id));
  259. /* See if it is already in our dns cache */
  260. dns = Curl_hash_pick(data->dns.hostcache, entry_id, entry_len + 1);
  261. }
  262. if(dns && (data->set.dns_cache_timeout != -1)) {
  263. /* See whether the returned entry is stale. Done before we release lock */
  264. struct hostcache_prune_data user;
  265. user.now = time(NULL);
  266. user.max_age_sec = data->set.dns_cache_timeout;
  267. user.oldest = 0;
  268. if(hostcache_entry_is_stale(&user, dns)) {
  269. infof(data, "Hostname in DNS cache was stale, zapped");
  270. dns = NULL; /* the memory deallocation is being handled by the hash */
  271. Curl_hash_delete(data->dns.hostcache, entry_id, entry_len + 1);
  272. }
  273. }
  274. /* See if the returned entry matches the required resolve mode */
  275. if(dns && data->conn->ip_version != CURL_IPRESOLVE_WHATEVER) {
  276. int pf = PF_INET;
  277. bool found = false;
  278. struct Curl_addrinfo *addr = dns->addr;
  279. #ifdef PF_INET6
  280. if(data->conn->ip_version == CURL_IPRESOLVE_V6)
  281. pf = PF_INET6;
  282. #endif
  283. while(addr) {
  284. if(addr->ai_family == pf) {
  285. found = true;
  286. break;
  287. }
  288. addr = addr->ai_next;
  289. }
  290. if(!found) {
  291. infof(data, "Hostname in DNS cache does not have needed family, zapped");
  292. dns = NULL; /* the memory deallocation is being handled by the hash */
  293. Curl_hash_delete(data->dns.hostcache, entry_id, entry_len + 1);
  294. }
  295. }
  296. return dns;
  297. }
  298. /*
  299. * Curl_fetch_addr() fetches a 'Curl_dns_entry' already in the DNS cache.
  300. *
  301. * Curl_resolv() checks initially and multi_runsingle() checks each time
  302. * it discovers the handle in the state WAITRESOLVE whether the hostname
  303. * has already been resolved and the address has already been stored in
  304. * the DNS cache. This short circuits waiting for a lot of pending
  305. * lookups for the same hostname requested by different handles.
  306. *
  307. * Returns the Curl_dns_entry entry pointer or NULL if not in the cache.
  308. *
  309. * The returned data *MUST* be "released" with Curl_resolv_unlink() after
  310. * use, or we will leak memory!
  311. */
  312. struct Curl_dns_entry *
  313. Curl_fetch_addr(struct Curl_easy *data,
  314. const char *hostname,
  315. int port)
  316. {
  317. struct Curl_dns_entry *dns = NULL;
  318. if(data->share)
  319. Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
  320. dns = fetch_addr(data, hostname, port);
  321. if(dns)
  322. dns->refcount++; /* we use it! */
  323. if(data->share)
  324. Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
  325. return dns;
  326. }
  327. #ifndef CURL_DISABLE_SHUFFLE_DNS
  328. /*
  329. * Return # of addresses in a Curl_addrinfo struct
  330. */
  331. static int num_addresses(const struct Curl_addrinfo *addr)
  332. {
  333. int i = 0;
  334. while(addr) {
  335. addr = addr->ai_next;
  336. i++;
  337. }
  338. return i;
  339. }
  340. UNITTEST CURLcode Curl_shuffle_addr(struct Curl_easy *data,
  341. struct Curl_addrinfo **addr);
  342. /*
  343. * Curl_shuffle_addr() shuffles the order of addresses in a 'Curl_addrinfo'
  344. * struct by re-linking its linked list.
  345. *
  346. * The addr argument should be the address of a pointer to the head node of a
  347. * `Curl_addrinfo` list and it will be modified to point to the new head after
  348. * shuffling.
  349. *
  350. * Not declared static only to make it easy to use in a unit test!
  351. *
  352. * @unittest: 1608
  353. */
  354. UNITTEST CURLcode Curl_shuffle_addr(struct Curl_easy *data,
  355. struct Curl_addrinfo **addr)
  356. {
  357. CURLcode result = CURLE_OK;
  358. const int num_addrs = num_addresses(*addr);
  359. if(num_addrs > 1) {
  360. struct Curl_addrinfo **nodes;
  361. infof(data, "Shuffling %i addresses", num_addrs);
  362. nodes = malloc(num_addrs*sizeof(*nodes));
  363. if(nodes) {
  364. int i;
  365. unsigned int *rnd;
  366. const size_t rnd_size = num_addrs * sizeof(*rnd);
  367. /* build a plain array of Curl_addrinfo pointers */
  368. nodes[0] = *addr;
  369. for(i = 1; i < num_addrs; i++) {
  370. nodes[i] = nodes[i-1]->ai_next;
  371. }
  372. rnd = malloc(rnd_size);
  373. if(rnd) {
  374. /* Fisher-Yates shuffle */
  375. if(Curl_rand(data, (unsigned char *)rnd, rnd_size) == CURLE_OK) {
  376. struct Curl_addrinfo *swap_tmp;
  377. for(i = num_addrs - 1; i > 0; i--) {
  378. swap_tmp = nodes[rnd[i] % (unsigned int)(i + 1)];
  379. nodes[rnd[i] % (unsigned int)(i + 1)] = nodes[i];
  380. nodes[i] = swap_tmp;
  381. }
  382. /* relink list in the new order */
  383. for(i = 1; i < num_addrs; i++) {
  384. nodes[i-1]->ai_next = nodes[i];
  385. }
  386. nodes[num_addrs-1]->ai_next = NULL;
  387. *addr = nodes[0];
  388. }
  389. free(rnd);
  390. }
  391. else
  392. result = CURLE_OUT_OF_MEMORY;
  393. free(nodes);
  394. }
  395. else
  396. result = CURLE_OUT_OF_MEMORY;
  397. }
  398. return result;
  399. }
  400. #endif
  401. /*
  402. * Curl_cache_addr() stores a 'Curl_addrinfo' struct in the DNS cache.
  403. *
  404. * When calling Curl_resolv() has resulted in a response with a returned
  405. * address, we call this function to store the information in the dns
  406. * cache etc
  407. *
  408. * Returns the Curl_dns_entry entry pointer or NULL if the storage failed.
  409. */
  410. struct Curl_dns_entry *
  411. Curl_cache_addr(struct Curl_easy *data,
  412. struct Curl_addrinfo *addr,
  413. const char *hostname,
  414. size_t hostlen, /* length or zero */
  415. int port,
  416. bool permanent)
  417. {
  418. char entry_id[MAX_HOSTCACHE_LEN];
  419. size_t entry_len;
  420. struct Curl_dns_entry *dns;
  421. struct Curl_dns_entry *dns2;
  422. #ifndef CURL_DISABLE_SHUFFLE_DNS
  423. /* shuffle addresses if requested */
  424. if(data->set.dns_shuffle_addresses) {
  425. CURLcode result = Curl_shuffle_addr(data, &addr);
  426. if(result)
  427. return NULL;
  428. }
  429. #endif
  430. if(!hostlen)
  431. hostlen = strlen(hostname);
  432. /* Create a new cache entry */
  433. dns = calloc(1, sizeof(struct Curl_dns_entry) + hostlen);
  434. if(!dns) {
  435. return NULL;
  436. }
  437. /* Create an entry id, based upon the hostname and port */
  438. entry_len = create_hostcache_id(hostname, hostlen, port,
  439. entry_id, sizeof(entry_id));
  440. dns->refcount = 1; /* the cache has the first reference */
  441. dns->addr = addr; /* this is the address(es) */
  442. if(permanent)
  443. dns->timestamp = 0; /* an entry that never goes stale */
  444. else {
  445. dns->timestamp = time(NULL);
  446. if(dns->timestamp == 0)
  447. dns->timestamp = 1;
  448. }
  449. dns->hostport = port;
  450. if(hostlen)
  451. memcpy(dns->hostname, hostname, hostlen);
  452. /* Store the resolved data in our DNS cache. */
  453. dns2 = Curl_hash_add(data->dns.hostcache, entry_id, entry_len + 1,
  454. (void *)dns);
  455. if(!dns2) {
  456. free(dns);
  457. return NULL;
  458. }
  459. dns = dns2;
  460. dns->refcount++; /* mark entry as in-use */
  461. return dns;
  462. }
  463. #ifdef USE_IPV6
  464. /* return a static IPv6 ::1 for the name */
  465. static struct Curl_addrinfo *get_localhost6(int port, const char *name)
  466. {
  467. struct Curl_addrinfo *ca;
  468. const size_t ss_size = sizeof(struct sockaddr_in6);
  469. const size_t hostlen = strlen(name);
  470. struct sockaddr_in6 sa6;
  471. unsigned char ipv6[16];
  472. unsigned short port16 = (unsigned short)(port & 0xffff);
  473. ca = calloc(1, sizeof(struct Curl_addrinfo) + ss_size + hostlen + 1);
  474. if(!ca)
  475. return NULL;
  476. sa6.sin6_family = AF_INET6;
  477. sa6.sin6_port = htons(port16);
  478. sa6.sin6_flowinfo = 0;
  479. sa6.sin6_scope_id = 0;
  480. (void)Curl_inet_pton(AF_INET6, "::1", ipv6);
  481. memcpy(&sa6.sin6_addr, ipv6, sizeof(ipv6));
  482. ca->ai_flags = 0;
  483. ca->ai_family = AF_INET6;
  484. ca->ai_socktype = SOCK_STREAM;
  485. ca->ai_protocol = IPPROTO_TCP;
  486. ca->ai_addrlen = (curl_socklen_t)ss_size;
  487. ca->ai_next = NULL;
  488. ca->ai_addr = (void *)((char *)ca + sizeof(struct Curl_addrinfo));
  489. memcpy(ca->ai_addr, &sa6, ss_size);
  490. ca->ai_canonname = (char *)ca->ai_addr + ss_size;
  491. strcpy(ca->ai_canonname, name);
  492. return ca;
  493. }
  494. #else
  495. #define get_localhost6(x,y) NULL
  496. #endif
  497. /* return a static IPv4 127.0.0.1 for the given name */
  498. static struct Curl_addrinfo *get_localhost(int port, const char *name)
  499. {
  500. struct Curl_addrinfo *ca;
  501. struct Curl_addrinfo *ca6;
  502. const size_t ss_size = sizeof(struct sockaddr_in);
  503. const size_t hostlen = strlen(name);
  504. struct sockaddr_in sa;
  505. unsigned int ipv4;
  506. unsigned short port16 = (unsigned short)(port & 0xffff);
  507. /* memset to clear the sa.sin_zero field */
  508. memset(&sa, 0, sizeof(sa));
  509. sa.sin_family = AF_INET;
  510. sa.sin_port = htons(port16);
  511. if(Curl_inet_pton(AF_INET, "127.0.0.1", (char *)&ipv4) < 1)
  512. return NULL;
  513. memcpy(&sa.sin_addr, &ipv4, sizeof(ipv4));
  514. ca = calloc(1, sizeof(struct Curl_addrinfo) + ss_size + hostlen + 1);
  515. if(!ca)
  516. return NULL;
  517. ca->ai_flags = 0;
  518. ca->ai_family = AF_INET;
  519. ca->ai_socktype = SOCK_STREAM;
  520. ca->ai_protocol = IPPROTO_TCP;
  521. ca->ai_addrlen = (curl_socklen_t)ss_size;
  522. ca->ai_addr = (void *)((char *)ca + sizeof(struct Curl_addrinfo));
  523. memcpy(ca->ai_addr, &sa, ss_size);
  524. ca->ai_canonname = (char *)ca->ai_addr + ss_size;
  525. strcpy(ca->ai_canonname, name);
  526. ca6 = get_localhost6(port, name);
  527. if(!ca6)
  528. return ca;
  529. ca6->ai_next = ca;
  530. return ca6;
  531. }
  532. #ifdef USE_IPV6
  533. /*
  534. * Curl_ipv6works() returns TRUE if IPv6 seems to work.
  535. */
  536. bool Curl_ipv6works(struct Curl_easy *data)
  537. {
  538. if(data) {
  539. /* the nature of most system is that IPv6 status does not come and go
  540. during a program's lifetime so we only probe the first time and then we
  541. have the info kept for fast reuse */
  542. DEBUGASSERT(data);
  543. DEBUGASSERT(data->multi);
  544. if(data->multi->ipv6_up == IPV6_UNKNOWN) {
  545. bool works = Curl_ipv6works(NULL);
  546. data->multi->ipv6_up = works ? IPV6_WORKS : IPV6_DEAD;
  547. }
  548. return data->multi->ipv6_up == IPV6_WORKS;
  549. }
  550. else {
  551. int ipv6_works = -1;
  552. /* probe to see if we have a working IPv6 stack */
  553. curl_socket_t s = socket(PF_INET6, SOCK_DGRAM, 0);
  554. if(s == CURL_SOCKET_BAD)
  555. /* an IPv6 address was requested but we cannot get/use one */
  556. ipv6_works = 0;
  557. else {
  558. ipv6_works = 1;
  559. sclose(s);
  560. }
  561. return (ipv6_works > 0) ? TRUE : FALSE;
  562. }
  563. }
  564. #endif /* USE_IPV6 */
  565. /*
  566. * Curl_host_is_ipnum() returns TRUE if the given string is a numerical IPv4
  567. * (or IPv6 if supported) address.
  568. */
  569. bool Curl_host_is_ipnum(const char *hostname)
  570. {
  571. struct in_addr in;
  572. #ifdef USE_IPV6
  573. struct in6_addr in6;
  574. #endif
  575. if(Curl_inet_pton(AF_INET, hostname, &in) > 0
  576. #ifdef USE_IPV6
  577. || Curl_inet_pton(AF_INET6, hostname, &in6) > 0
  578. #endif
  579. )
  580. return TRUE;
  581. return FALSE;
  582. }
  583. /* return TRUE if 'part' is a case insensitive tail of 'full' */
  584. static bool tailmatch(const char *full, const char *part)
  585. {
  586. size_t plen = strlen(part);
  587. size_t flen = strlen(full);
  588. if(plen > flen)
  589. return FALSE;
  590. return strncasecompare(part, &full[flen - plen], plen);
  591. }
  592. /*
  593. * Curl_resolv() is the main name resolve function within libcurl. It resolves
  594. * a name and returns a pointer to the entry in the 'entry' argument (if one
  595. * is provided). This function might return immediately if we are using asynch
  596. * resolves. See the return codes.
  597. *
  598. * The cache entry we return will get its 'inuse' counter increased when this
  599. * function is used. You MUST call Curl_resolv_unlink() later (when you are
  600. * done using this struct) to decrease the reference counter again.
  601. *
  602. * Return codes:
  603. *
  604. * CURLRESOLV_ERROR (-1) = error, no pointer
  605. * CURLRESOLV_RESOLVED (0) = OK, pointer provided
  606. * CURLRESOLV_PENDING (1) = waiting for response, no pointer
  607. */
  608. enum resolve_t Curl_resolv(struct Curl_easy *data,
  609. const char *hostname,
  610. int port,
  611. bool allowDOH,
  612. struct Curl_dns_entry **entry)
  613. {
  614. struct Curl_dns_entry *dns = NULL;
  615. CURLcode result;
  616. enum resolve_t rc = CURLRESOLV_ERROR; /* default to failure */
  617. struct connectdata *conn = data->conn;
  618. /* We should intentionally error and not resolve .onion TLDs */
  619. size_t hostname_len = strlen(hostname);
  620. if(hostname_len >= 7 &&
  621. (curl_strequal(&hostname[hostname_len - 6], ".onion") ||
  622. curl_strequal(&hostname[hostname_len - 7], ".onion."))) {
  623. failf(data, "Not resolving .onion address (RFC 7686)");
  624. return CURLRESOLV_ERROR;
  625. }
  626. *entry = NULL;
  627. #ifndef CURL_DISABLE_DOH
  628. conn->bits.doh = FALSE; /* default is not */
  629. #else
  630. (void)allowDOH;
  631. #endif
  632. if(data->share)
  633. Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
  634. dns = fetch_addr(data, hostname, port);
  635. if(dns) {
  636. infof(data, "Hostname %s was found in DNS cache", hostname);
  637. dns->refcount++; /* we use it! */
  638. rc = CURLRESOLV_RESOLVED;
  639. }
  640. if(data->share)
  641. Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
  642. if(!dns) {
  643. /* The entry was not in the cache. Resolve it to IP address */
  644. struct Curl_addrinfo *addr = NULL;
  645. int respwait = 0;
  646. #if !defined(CURL_DISABLE_DOH) || !defined(USE_RESOLVE_ON_IPS)
  647. struct in_addr in;
  648. #endif
  649. #ifndef CURL_DISABLE_DOH
  650. #ifndef USE_RESOLVE_ON_IPS
  651. const
  652. #endif
  653. bool ipnum = FALSE;
  654. #endif
  655. /* notify the resolver start callback */
  656. if(data->set.resolver_start) {
  657. int st;
  658. Curl_set_in_callback(data, true);
  659. st = data->set.resolver_start(
  660. #ifdef USE_CURL_ASYNC
  661. data->state.async.resolver,
  662. #else
  663. NULL,
  664. #endif
  665. NULL,
  666. data->set.resolver_start_client);
  667. Curl_set_in_callback(data, false);
  668. if(st)
  669. return CURLRESOLV_ERROR;
  670. }
  671. #ifndef USE_RESOLVE_ON_IPS
  672. /* First check if this is an IPv4 address string */
  673. if(Curl_inet_pton(AF_INET, hostname, &in) > 0) {
  674. /* This is a dotted IP address 123.123.123.123-style */
  675. addr = Curl_ip2addr(AF_INET, &in, hostname, port);
  676. if(!addr)
  677. return CURLRESOLV_ERROR;
  678. }
  679. #ifdef USE_IPV6
  680. else {
  681. struct in6_addr in6;
  682. /* check if this is an IPv6 address string */
  683. if(Curl_inet_pton(AF_INET6, hostname, &in6) > 0) {
  684. /* This is an IPv6 address literal */
  685. addr = Curl_ip2addr(AF_INET6, &in6, hostname, port);
  686. if(!addr)
  687. return CURLRESOLV_ERROR;
  688. }
  689. }
  690. #endif /* USE_IPV6 */
  691. #else /* if USE_RESOLVE_ON_IPS */
  692. #ifndef CURL_DISABLE_DOH
  693. /* First check if this is an IPv4 address string */
  694. if(Curl_inet_pton(AF_INET, hostname, &in) > 0)
  695. /* This is a dotted IP address 123.123.123.123-style */
  696. ipnum = TRUE;
  697. #ifdef USE_IPV6
  698. else {
  699. struct in6_addr in6;
  700. /* check if this is an IPv6 address string */
  701. if(Curl_inet_pton(AF_INET6, hostname, &in6) > 0)
  702. /* This is an IPv6 address literal */
  703. ipnum = TRUE;
  704. }
  705. #endif /* USE_IPV6 */
  706. #endif /* CURL_DISABLE_DOH */
  707. #endif /* !USE_RESOLVE_ON_IPS */
  708. if(!addr) {
  709. if(conn->ip_version == CURL_IPRESOLVE_V6 && !Curl_ipv6works(data))
  710. return CURLRESOLV_ERROR;
  711. if(strcasecompare(hostname, "localhost") ||
  712. tailmatch(hostname, ".localhost"))
  713. addr = get_localhost(port, hostname);
  714. #ifndef CURL_DISABLE_DOH
  715. else if(allowDOH && data->set.doh && !ipnum)
  716. addr = Curl_doh(data, hostname, port, &respwait);
  717. #endif
  718. else {
  719. /* Check what IP specifics the app has requested and if we can provide
  720. * it. If not, bail out. */
  721. if(!Curl_ipvalid(data, conn))
  722. return CURLRESOLV_ERROR;
  723. /* If Curl_getaddrinfo() returns NULL, 'respwait' might be set to a
  724. non-zero value indicating that we need to wait for the response to
  725. the resolve call */
  726. addr = Curl_getaddrinfo(data, hostname, port, &respwait);
  727. }
  728. }
  729. if(!addr) {
  730. if(respwait) {
  731. /* the response to our resolve call will come asynchronously at
  732. a later time, good or bad */
  733. /* First, check that we have not received the info by now */
  734. result = Curl_resolv_check(data, &dns);
  735. if(result) /* error detected */
  736. return CURLRESOLV_ERROR;
  737. if(dns)
  738. rc = CURLRESOLV_RESOLVED; /* pointer provided */
  739. else
  740. rc = CURLRESOLV_PENDING; /* no info yet */
  741. }
  742. }
  743. else {
  744. if(data->share)
  745. Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
  746. /* we got a response, store it in the cache */
  747. dns = Curl_cache_addr(data, addr, hostname, 0, port, FALSE);
  748. if(data->share)
  749. Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
  750. if(!dns)
  751. /* returned failure, bail out nicely */
  752. Curl_freeaddrinfo(addr);
  753. else {
  754. rc = CURLRESOLV_RESOLVED;
  755. show_resolve_info(data, dns);
  756. }
  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. CURL_NORETURN 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 are 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_unlink() later (when you are
  782. * done using this struct) to decrease the reference 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 would 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. /* HP-UX does not 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. will not, 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_unlink() releases a reference to the given cached DNS entry.
  932. * When the reference count reaches 0, the entry is destroyed. It is important
  933. * that only one unlink is made for each Curl_resolv() call.
  934. *
  935. * May be called with 'data' == NULL for global cache.
  936. */
  937. void Curl_resolv_unlink(struct Curl_easy *data, struct Curl_dns_entry **pdns)
  938. {
  939. struct Curl_dns_entry *dns = *pdns;
  940. *pdns = NULL;
  941. if(data && data->share)
  942. Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
  943. hostcache_unlink_entry(dns);
  944. if(data && data->share)
  945. Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
  946. }
  947. /*
  948. * File-internal: release cache dns entry reference, free if inuse drops to 0
  949. */
  950. static void hostcache_unlink_entry(void *entry)
  951. {
  952. struct Curl_dns_entry *dns = (struct Curl_dns_entry *) entry;
  953. DEBUGASSERT(dns && (dns->refcount > 0));
  954. dns->refcount--;
  955. if(dns->refcount == 0) {
  956. Curl_freeaddrinfo(dns->addr);
  957. #ifdef USE_HTTPSRR
  958. if(dns->hinfo) {
  959. if(dns->hinfo->target)
  960. free(dns->hinfo->target);
  961. if(dns->hinfo->alpns)
  962. free(dns->hinfo->alpns);
  963. if(dns->hinfo->ipv4hints)
  964. free(dns->hinfo->ipv4hints);
  965. if(dns->hinfo->echconfiglist)
  966. free(dns->hinfo->echconfiglist);
  967. if(dns->hinfo->ipv6hints)
  968. free(dns->hinfo->ipv6hints);
  969. if(dns->hinfo->val)
  970. free(dns->hinfo->val);
  971. free(dns->hinfo);
  972. }
  973. #endif
  974. free(dns);
  975. }
  976. }
  977. /*
  978. * Curl_init_dnscache() inits a new DNS cache.
  979. */
  980. void Curl_init_dnscache(struct Curl_hash *hash, size_t size)
  981. {
  982. Curl_hash_init(hash, size, Curl_hash_str, Curl_str_key_compare,
  983. hostcache_unlink_entry);
  984. }
  985. /*
  986. * Curl_hostcache_clean()
  987. *
  988. * This _can_ be called with 'data' == NULL but then of course no locking
  989. * can be done!
  990. */
  991. void Curl_hostcache_clean(struct Curl_easy *data,
  992. struct Curl_hash *hash)
  993. {
  994. if(data && data->share)
  995. Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
  996. Curl_hash_clean(hash);
  997. if(data && data->share)
  998. Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
  999. }
  1000. CURLcode Curl_loadhostpairs(struct Curl_easy *data)
  1001. {
  1002. struct curl_slist *hostp;
  1003. char *host_end;
  1004. /* Default is no wildcard found */
  1005. data->state.wildcard_resolve = false;
  1006. for(hostp = data->state.resolve; hostp; hostp = hostp->next) {
  1007. char entry_id[MAX_HOSTCACHE_LEN];
  1008. if(!hostp->data)
  1009. continue;
  1010. if(hostp->data[0] == '-') {
  1011. unsigned long num = 0;
  1012. size_t entry_len;
  1013. size_t hlen = 0;
  1014. host_end = strchr(&hostp->data[1], ':');
  1015. if(host_end) {
  1016. hlen = host_end - &hostp->data[1];
  1017. num = strtoul(++host_end, NULL, 10);
  1018. if(!hlen || (num > 0xffff))
  1019. host_end = NULL;
  1020. }
  1021. if(!host_end) {
  1022. infof(data, "Bad syntax CURLOPT_RESOLVE removal entry '%s'",
  1023. hostp->data);
  1024. continue;
  1025. }
  1026. /* Create an entry id, based upon the hostname and port */
  1027. entry_len = create_hostcache_id(&hostp->data[1], hlen, (int)num,
  1028. entry_id, sizeof(entry_id));
  1029. if(data->share)
  1030. Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
  1031. /* delete entry, ignore if it did not exist */
  1032. Curl_hash_delete(data->dns.hostcache, entry_id, entry_len + 1);
  1033. if(data->share)
  1034. Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
  1035. }
  1036. else {
  1037. struct Curl_dns_entry *dns;
  1038. struct Curl_addrinfo *head = NULL, *tail = NULL;
  1039. size_t entry_len;
  1040. char address[64];
  1041. #if !defined(CURL_DISABLE_VERBOSE_STRINGS)
  1042. char *addresses = NULL;
  1043. #endif
  1044. char *addr_begin;
  1045. char *addr_end;
  1046. char *port_ptr;
  1047. int port = 0;
  1048. char *end_ptr;
  1049. bool permanent = TRUE;
  1050. unsigned long tmp_port;
  1051. bool error = true;
  1052. char *host_begin = hostp->data;
  1053. size_t hlen = 0;
  1054. if(host_begin[0] == '+') {
  1055. host_begin++;
  1056. permanent = FALSE;
  1057. }
  1058. host_end = strchr(host_begin, ':');
  1059. if(!host_end)
  1060. goto err;
  1061. hlen = host_end - host_begin;
  1062. port_ptr = host_end + 1;
  1063. tmp_port = strtoul(port_ptr, &end_ptr, 10);
  1064. if(tmp_port > USHRT_MAX || end_ptr == port_ptr || *end_ptr != ':')
  1065. goto err;
  1066. port = (int)tmp_port;
  1067. #if !defined(CURL_DISABLE_VERBOSE_STRINGS)
  1068. addresses = end_ptr + 1;
  1069. #endif
  1070. while(*end_ptr) {
  1071. size_t alen;
  1072. struct Curl_addrinfo *ai;
  1073. addr_begin = end_ptr + 1;
  1074. addr_end = strchr(addr_begin, ',');
  1075. if(!addr_end)
  1076. addr_end = addr_begin + strlen(addr_begin);
  1077. end_ptr = addr_end;
  1078. /* allow IP(v6) address within [brackets] */
  1079. if(*addr_begin == '[') {
  1080. if(addr_end == addr_begin || *(addr_end - 1) != ']')
  1081. goto err;
  1082. ++addr_begin;
  1083. --addr_end;
  1084. }
  1085. alen = addr_end - addr_begin;
  1086. if(!alen)
  1087. continue;
  1088. if(alen >= sizeof(address))
  1089. goto err;
  1090. memcpy(address, addr_begin, alen);
  1091. address[alen] = '\0';
  1092. #ifndef USE_IPV6
  1093. if(strchr(address, ':')) {
  1094. infof(data, "Ignoring resolve address '%s', missing IPv6 support.",
  1095. address);
  1096. continue;
  1097. }
  1098. #endif
  1099. ai = Curl_str2addr(address, port);
  1100. if(!ai) {
  1101. infof(data, "Resolve address '%s' found illegal", address);
  1102. goto err;
  1103. }
  1104. if(tail) {
  1105. tail->ai_next = ai;
  1106. tail = tail->ai_next;
  1107. }
  1108. else {
  1109. head = tail = ai;
  1110. }
  1111. }
  1112. if(!head)
  1113. goto err;
  1114. error = false;
  1115. err:
  1116. if(error) {
  1117. failf(data, "Couldn't parse CURLOPT_RESOLVE entry '%s'",
  1118. hostp->data);
  1119. Curl_freeaddrinfo(head);
  1120. return CURLE_SETOPT_OPTION_SYNTAX;
  1121. }
  1122. /* Create an entry id, based upon the hostname and port */
  1123. entry_len = create_hostcache_id(host_begin, hlen, port,
  1124. entry_id, sizeof(entry_id));
  1125. if(data->share)
  1126. Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
  1127. /* See if it is already in our dns cache */
  1128. dns = Curl_hash_pick(data->dns.hostcache, entry_id, entry_len + 1);
  1129. if(dns) {
  1130. infof(data, "RESOLVE %.*s:%d - old addresses discarded",
  1131. (int)hlen, host_begin, port);
  1132. /* delete old entry, there are two reasons for this
  1133. 1. old entry may have different addresses.
  1134. 2. even if entry with correct addresses is already in the cache,
  1135. but if it is close to expire, then by the time next http
  1136. request is made, it can get expired and pruned because old
  1137. entry is not necessarily marked as permanent.
  1138. 3. when adding a non-permanent entry, we want it to remove and
  1139. replace an existing permanent entry.
  1140. 4. when adding a non-permanent entry, we want it to get a "fresh"
  1141. timeout that starts _now_. */
  1142. Curl_hash_delete(data->dns.hostcache, entry_id, entry_len + 1);
  1143. }
  1144. /* put this new host in the cache */
  1145. dns = Curl_cache_addr(data, head, host_begin, hlen, port, permanent);
  1146. if(dns) {
  1147. /* release the returned reference; the cache itself will keep the
  1148. * entry alive: */
  1149. dns->refcount--;
  1150. }
  1151. if(data->share)
  1152. Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
  1153. if(!dns) {
  1154. Curl_freeaddrinfo(head);
  1155. return CURLE_OUT_OF_MEMORY;
  1156. }
  1157. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  1158. infof(data, "Added %.*s:%d:%s to DNS cache%s",
  1159. (int)hlen, host_begin, port, addresses,
  1160. permanent ? "" : " (non-permanent)");
  1161. #endif
  1162. /* Wildcard hostname */
  1163. if((hlen == 1) && (host_begin[0] == '*')) {
  1164. infof(data, "RESOLVE *:%d using wildcard", port);
  1165. data->state.wildcard_resolve = true;
  1166. }
  1167. }
  1168. }
  1169. data->state.resolve = NULL; /* dealt with now */
  1170. return CURLE_OK;
  1171. }
  1172. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  1173. static void show_resolve_info(struct Curl_easy *data,
  1174. struct Curl_dns_entry *dns)
  1175. {
  1176. struct Curl_addrinfo *a;
  1177. CURLcode result = CURLE_OK;
  1178. #ifdef CURLRES_IPV6
  1179. struct dynbuf out[2];
  1180. #else
  1181. struct dynbuf out[1];
  1182. #endif
  1183. DEBUGASSERT(data);
  1184. DEBUGASSERT(dns);
  1185. if(!data->set.verbose ||
  1186. /* ignore no name or numerical IP addresses */
  1187. !dns->hostname[0] || Curl_host_is_ipnum(dns->hostname))
  1188. return;
  1189. a = dns->addr;
  1190. infof(data, "Host %s:%d was resolved.",
  1191. (dns->hostname[0] ? dns->hostname : "(none)"), dns->hostport);
  1192. Curl_dyn_init(&out[0], 1024);
  1193. #ifdef CURLRES_IPV6
  1194. Curl_dyn_init(&out[1], 1024);
  1195. #endif
  1196. while(a) {
  1197. if(
  1198. #ifdef CURLRES_IPV6
  1199. a->ai_family == PF_INET6 ||
  1200. #endif
  1201. a->ai_family == PF_INET) {
  1202. char buf[MAX_IPADR_LEN];
  1203. struct dynbuf *d = &out[(a->ai_family != PF_INET)];
  1204. Curl_printable_address(a, buf, sizeof(buf));
  1205. if(Curl_dyn_len(d))
  1206. result = Curl_dyn_addn(d, ", ", 2);
  1207. if(!result)
  1208. result = Curl_dyn_add(d, buf);
  1209. if(result) {
  1210. infof(data, "too many IP, cannot show");
  1211. goto fail;
  1212. }
  1213. }
  1214. a = a->ai_next;
  1215. }
  1216. #ifdef CURLRES_IPV6
  1217. infof(data, "IPv6: %s",
  1218. (Curl_dyn_len(&out[1]) ? Curl_dyn_ptr(&out[1]) : "(none)"));
  1219. #endif
  1220. infof(data, "IPv4: %s",
  1221. (Curl_dyn_len(&out[0]) ? Curl_dyn_ptr(&out[0]) : "(none)"));
  1222. fail:
  1223. Curl_dyn_free(&out[0]);
  1224. #ifdef CURLRES_IPV6
  1225. Curl_dyn_free(&out[1]);
  1226. #endif
  1227. }
  1228. #endif
  1229. CURLcode Curl_resolv_check(struct Curl_easy *data,
  1230. struct Curl_dns_entry **dns)
  1231. {
  1232. CURLcode result;
  1233. #if defined(CURL_DISABLE_DOH) && !defined(CURLRES_ASYNCH)
  1234. (void)data;
  1235. (void)dns;
  1236. #endif
  1237. #ifndef CURL_DISABLE_DOH
  1238. if(data->conn->bits.doh) {
  1239. result = Curl_doh_is_resolved(data, dns);
  1240. }
  1241. else
  1242. #endif
  1243. result = Curl_resolver_is_resolved(data, dns);
  1244. if(*dns)
  1245. show_resolve_info(data, *dns);
  1246. return result;
  1247. }
  1248. int Curl_resolv_getsock(struct Curl_easy *data,
  1249. curl_socket_t *socks)
  1250. {
  1251. #ifdef CURLRES_ASYNCH
  1252. #ifndef CURL_DISABLE_DOH
  1253. if(data->conn->bits.doh)
  1254. /* nothing to wait for during DoH resolve, those handles have their own
  1255. sockets */
  1256. return GETSOCK_BLANK;
  1257. #endif
  1258. return Curl_resolver_getsock(data, socks);
  1259. #else
  1260. (void)data;
  1261. (void)socks;
  1262. return GETSOCK_BLANK;
  1263. #endif
  1264. }
  1265. /* Call this function after Curl_connect() has returned async=TRUE and
  1266. then a successful name resolve has been received.
  1267. Note: this function disconnects and frees the conn data in case of
  1268. resolve failure */
  1269. CURLcode Curl_once_resolved(struct Curl_easy *data, bool *protocol_done)
  1270. {
  1271. CURLcode result;
  1272. struct connectdata *conn = data->conn;
  1273. #ifdef USE_CURL_ASYNC
  1274. if(data->state.async.dns) {
  1275. conn->dns_entry = data->state.async.dns;
  1276. data->state.async.dns = NULL;
  1277. }
  1278. #endif
  1279. result = Curl_setup_conn(data, protocol_done);
  1280. if(result) {
  1281. Curl_detach_connection(data);
  1282. Curl_cpool_disconnect(data, conn, TRUE);
  1283. }
  1284. return result;
  1285. }
  1286. /*
  1287. * Curl_resolver_error() calls failf() with the appropriate message after a
  1288. * resolve error
  1289. */
  1290. #ifdef USE_CURL_ASYNC
  1291. CURLcode Curl_resolver_error(struct Curl_easy *data)
  1292. {
  1293. const char *host_or_proxy;
  1294. CURLcode result;
  1295. #ifndef CURL_DISABLE_PROXY
  1296. struct connectdata *conn = data->conn;
  1297. if(conn->bits.httpproxy) {
  1298. host_or_proxy = "proxy";
  1299. result = CURLE_COULDNT_RESOLVE_PROXY;
  1300. }
  1301. else
  1302. #endif
  1303. {
  1304. host_or_proxy = "host";
  1305. result = CURLE_COULDNT_RESOLVE_HOST;
  1306. }
  1307. failf(data, "Could not resolve %s: %s", host_or_proxy,
  1308. data->state.async.hostname);
  1309. return result;
  1310. }
  1311. #endif /* USE_CURL_ASYNC */