hostip.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490
  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 >= (time_t)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. #ifdef HAVE_SOCKADDR_IN6_SIN6_SCOPE_ID
  480. sa6.sin6_scope_id = 0;
  481. #endif
  482. (void)Curl_inet_pton(AF_INET6, "::1", ipv6);
  483. memcpy(&sa6.sin6_addr, ipv6, sizeof(ipv6));
  484. ca->ai_flags = 0;
  485. ca->ai_family = AF_INET6;
  486. ca->ai_socktype = SOCK_STREAM;
  487. ca->ai_protocol = IPPROTO_TCP;
  488. ca->ai_addrlen = (curl_socklen_t)ss_size;
  489. ca->ai_next = NULL;
  490. ca->ai_addr = (void *)((char *)ca + sizeof(struct Curl_addrinfo));
  491. memcpy(ca->ai_addr, &sa6, ss_size);
  492. ca->ai_canonname = (char *)ca->ai_addr + ss_size;
  493. strcpy(ca->ai_canonname, name);
  494. return ca;
  495. }
  496. #else
  497. #define get_localhost6(x,y) NULL
  498. #endif
  499. /* return a static IPv4 127.0.0.1 for the given name */
  500. static struct Curl_addrinfo *get_localhost(int port, const char *name)
  501. {
  502. struct Curl_addrinfo *ca;
  503. struct Curl_addrinfo *ca6;
  504. const size_t ss_size = sizeof(struct sockaddr_in);
  505. const size_t hostlen = strlen(name);
  506. struct sockaddr_in sa;
  507. unsigned int ipv4;
  508. unsigned short port16 = (unsigned short)(port & 0xffff);
  509. /* memset to clear the sa.sin_zero field */
  510. memset(&sa, 0, sizeof(sa));
  511. sa.sin_family = AF_INET;
  512. sa.sin_port = htons(port16);
  513. if(Curl_inet_pton(AF_INET, "127.0.0.1", (char *)&ipv4) < 1)
  514. return NULL;
  515. memcpy(&sa.sin_addr, &ipv4, sizeof(ipv4));
  516. ca = calloc(1, sizeof(struct Curl_addrinfo) + ss_size + hostlen + 1);
  517. if(!ca)
  518. return NULL;
  519. ca->ai_flags = 0;
  520. ca->ai_family = AF_INET;
  521. ca->ai_socktype = SOCK_STREAM;
  522. ca->ai_protocol = IPPROTO_TCP;
  523. ca->ai_addrlen = (curl_socklen_t)ss_size;
  524. ca->ai_addr = (void *)((char *)ca + sizeof(struct Curl_addrinfo));
  525. memcpy(ca->ai_addr, &sa, ss_size);
  526. ca->ai_canonname = (char *)ca->ai_addr + ss_size;
  527. strcpy(ca->ai_canonname, name);
  528. ca6 = get_localhost6(port, name);
  529. if(!ca6)
  530. return ca;
  531. ca6->ai_next = ca;
  532. return ca6;
  533. }
  534. #ifdef USE_IPV6
  535. /*
  536. * Curl_ipv6works() returns TRUE if IPv6 seems to work.
  537. */
  538. bool Curl_ipv6works(struct Curl_easy *data)
  539. {
  540. if(data) {
  541. /* the nature of most system is that IPv6 status does not come and go
  542. during a program's lifetime so we only probe the first time and then we
  543. have the info kept for fast reuse */
  544. DEBUGASSERT(data);
  545. DEBUGASSERT(data->multi);
  546. if(data->multi->ipv6_up == IPV6_UNKNOWN) {
  547. bool works = Curl_ipv6works(NULL);
  548. data->multi->ipv6_up = works ? IPV6_WORKS : IPV6_DEAD;
  549. }
  550. return data->multi->ipv6_up == IPV6_WORKS;
  551. }
  552. else {
  553. int ipv6_works = -1;
  554. /* probe to see if we have a working IPv6 stack */
  555. curl_socket_t s = socket(PF_INET6, SOCK_DGRAM, 0);
  556. if(s == CURL_SOCKET_BAD)
  557. /* an IPv6 address was requested but we cannot get/use one */
  558. ipv6_works = 0;
  559. else {
  560. ipv6_works = 1;
  561. sclose(s);
  562. }
  563. return (ipv6_works > 0);
  564. }
  565. }
  566. #endif /* USE_IPV6 */
  567. /*
  568. * Curl_host_is_ipnum() returns TRUE if the given string is a numerical IPv4
  569. * (or IPv6 if supported) address.
  570. */
  571. bool Curl_host_is_ipnum(const char *hostname)
  572. {
  573. struct in_addr in;
  574. #ifdef USE_IPV6
  575. struct in6_addr in6;
  576. #endif
  577. if(Curl_inet_pton(AF_INET, hostname, &in) > 0
  578. #ifdef USE_IPV6
  579. || Curl_inet_pton(AF_INET6, hostname, &in6) > 0
  580. #endif
  581. )
  582. return TRUE;
  583. return FALSE;
  584. }
  585. /* return TRUE if 'part' is a case insensitive tail of 'full' */
  586. static bool tailmatch(const char *full, const char *part)
  587. {
  588. size_t plen = strlen(part);
  589. size_t flen = strlen(full);
  590. if(plen > flen)
  591. return FALSE;
  592. return strncasecompare(part, &full[flen - plen], plen);
  593. }
  594. /*
  595. * Curl_resolv() is the main name resolve function within libcurl. It resolves
  596. * a name and returns a pointer to the entry in the 'entry' argument (if one
  597. * is provided). This function might return immediately if we are using asynch
  598. * resolves. See the return codes.
  599. *
  600. * The cache entry we return will get its 'inuse' counter increased when this
  601. * function is used. You MUST call Curl_resolv_unlink() later (when you are
  602. * done using this struct) to decrease the reference counter again.
  603. *
  604. * Return codes:
  605. *
  606. * CURLRESOLV_ERROR (-1) = error, no pointer
  607. * CURLRESOLV_RESOLVED (0) = OK, pointer provided
  608. * CURLRESOLV_PENDING (1) = waiting for response, no pointer
  609. */
  610. enum resolve_t Curl_resolv(struct Curl_easy *data,
  611. const char *hostname,
  612. int port,
  613. bool allowDOH,
  614. struct Curl_dns_entry **entry)
  615. {
  616. struct Curl_dns_entry *dns = NULL;
  617. CURLcode result;
  618. enum resolve_t rc = CURLRESOLV_ERROR; /* default to failure */
  619. struct connectdata *conn = data->conn;
  620. /* We should intentionally error and not resolve .onion TLDs */
  621. size_t hostname_len = strlen(hostname);
  622. if(hostname_len >= 7 &&
  623. (curl_strequal(&hostname[hostname_len - 6], ".onion") ||
  624. curl_strequal(&hostname[hostname_len - 7], ".onion."))) {
  625. failf(data, "Not resolving .onion address (RFC 7686)");
  626. return CURLRESOLV_ERROR;
  627. }
  628. *entry = NULL;
  629. #ifndef CURL_DISABLE_DOH
  630. conn->bits.doh = FALSE; /* default is not */
  631. #else
  632. (void)allowDOH;
  633. #endif
  634. if(data->share)
  635. Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
  636. dns = fetch_addr(data, hostname, port);
  637. if(dns) {
  638. infof(data, "Hostname %s was found in DNS cache", hostname);
  639. dns->refcount++; /* we use it! */
  640. rc = CURLRESOLV_RESOLVED;
  641. }
  642. if(data->share)
  643. Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
  644. if(!dns) {
  645. /* The entry was not in the cache. Resolve it to IP address */
  646. struct Curl_addrinfo *addr = NULL;
  647. int respwait = 0;
  648. #if !defined(CURL_DISABLE_DOH) || !defined(USE_RESOLVE_ON_IPS)
  649. struct in_addr in;
  650. #endif
  651. #ifndef CURL_DISABLE_DOH
  652. #ifndef USE_RESOLVE_ON_IPS
  653. const
  654. #endif
  655. bool ipnum = FALSE;
  656. #endif
  657. /* notify the resolver start callback */
  658. if(data->set.resolver_start) {
  659. int st;
  660. Curl_set_in_callback(data, TRUE);
  661. st = data->set.resolver_start(
  662. #ifdef USE_CURL_ASYNC
  663. data->state.async.resolver,
  664. #else
  665. NULL,
  666. #endif
  667. NULL,
  668. data->set.resolver_start_client);
  669. Curl_set_in_callback(data, FALSE);
  670. if(st)
  671. return CURLRESOLV_ERROR;
  672. }
  673. #ifndef USE_RESOLVE_ON_IPS
  674. /* First check if this is an IPv4 address string */
  675. if(Curl_inet_pton(AF_INET, hostname, &in) > 0) {
  676. /* This is a dotted IP address 123.123.123.123-style */
  677. addr = Curl_ip2addr(AF_INET, &in, hostname, port);
  678. if(!addr)
  679. return CURLRESOLV_ERROR;
  680. }
  681. #ifdef USE_IPV6
  682. else {
  683. struct in6_addr in6;
  684. /* check if this is an IPv6 address string */
  685. if(Curl_inet_pton(AF_INET6, hostname, &in6) > 0) {
  686. /* This is an IPv6 address literal */
  687. addr = Curl_ip2addr(AF_INET6, &in6, hostname, port);
  688. if(!addr)
  689. return CURLRESOLV_ERROR;
  690. }
  691. }
  692. #endif /* USE_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 USE_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 /* USE_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. strcasecompare(hostname, "localhost.") ||
  715. tailmatch(hostname, ".localhost") ||
  716. tailmatch(hostname, ".localhost."))
  717. addr = get_localhost(port, hostname);
  718. #ifndef CURL_DISABLE_DOH
  719. else if(allowDOH && data->set.doh && !ipnum)
  720. addr = Curl_doh(data, hostname, port, &respwait);
  721. #endif
  722. else {
  723. /* Check what IP specifics the app has requested and if we can provide
  724. * it. If not, bail out. */
  725. if(!Curl_ipvalid(data, conn))
  726. return CURLRESOLV_ERROR;
  727. /* If Curl_getaddrinfo() returns NULL, 'respwait' might be set to a
  728. non-zero value indicating that we need to wait for the response to
  729. the resolve call */
  730. addr = Curl_getaddrinfo(data, hostname, port, &respwait);
  731. }
  732. }
  733. if(!addr) {
  734. if(respwait) {
  735. /* the response to our resolve call will come asynchronously at
  736. a later time, good or bad */
  737. /* First, check that we have not received the info by now */
  738. result = Curl_resolv_check(data, &dns);
  739. if(result) /* error detected */
  740. return CURLRESOLV_ERROR;
  741. if(dns)
  742. rc = CURLRESOLV_RESOLVED; /* pointer provided */
  743. else
  744. rc = CURLRESOLV_PENDING; /* no info yet */
  745. }
  746. }
  747. else {
  748. if(data->share)
  749. Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
  750. /* we got a response, store it in the cache */
  751. dns = Curl_cache_addr(data, addr, hostname, 0, port, FALSE);
  752. if(data->share)
  753. Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
  754. if(!dns)
  755. /* returned failure, bail out nicely */
  756. Curl_freeaddrinfo(addr);
  757. else {
  758. rc = CURLRESOLV_RESOLVED;
  759. show_resolve_info(data, dns);
  760. }
  761. }
  762. }
  763. *entry = dns;
  764. return rc;
  765. }
  766. #ifdef USE_ALARM_TIMEOUT
  767. /*
  768. * This signal handler jumps back into the main libcurl code and continues
  769. * execution. This effectively causes the remainder of the application to run
  770. * within a signal handler which is nonportable and could lead to problems.
  771. */
  772. CURL_NORETURN static
  773. void alarmfunc(int sig)
  774. {
  775. (void)sig;
  776. siglongjmp(curl_jmpenv, 1);
  777. }
  778. #endif /* USE_ALARM_TIMEOUT */
  779. /*
  780. * Curl_resolv_timeout() is the same as Curl_resolv() but specifies a
  781. * timeout. This function might return immediately if we are using asynch
  782. * resolves. See the return codes.
  783. *
  784. * The cache entry we return will get its 'inuse' counter increased when this
  785. * function is used. You MUST call Curl_resolv_unlink() later (when you are
  786. * done using this struct) to decrease the reference counter again.
  787. *
  788. * If built with a synchronous resolver and use of signals is not
  789. * disabled by the application, then a nonzero timeout will cause a
  790. * timeout after the specified number of milliseconds. Otherwise, timeout
  791. * is ignored.
  792. *
  793. * Return codes:
  794. *
  795. * CURLRESOLV_TIMEDOUT(-2) = warning, time too short or previous alarm expired
  796. * CURLRESOLV_ERROR (-1) = error, no pointer
  797. * CURLRESOLV_RESOLVED (0) = OK, pointer provided
  798. * CURLRESOLV_PENDING (1) = waiting for response, no pointer
  799. */
  800. enum resolve_t Curl_resolv_timeout(struct Curl_easy *data,
  801. const char *hostname,
  802. int port,
  803. struct Curl_dns_entry **entry,
  804. timediff_t timeoutms)
  805. {
  806. #ifdef USE_ALARM_TIMEOUT
  807. #ifdef HAVE_SIGACTION
  808. struct sigaction keep_sigact; /* store the old struct here */
  809. volatile bool keep_copysig = FALSE; /* whether old sigact has been saved */
  810. struct sigaction sigact;
  811. #else
  812. #ifdef HAVE_SIGNAL
  813. void (*keep_sigact)(int); /* store the old handler here */
  814. #endif /* HAVE_SIGNAL */
  815. #endif /* HAVE_SIGACTION */
  816. volatile long timeout;
  817. volatile unsigned int prev_alarm = 0;
  818. #endif /* USE_ALARM_TIMEOUT */
  819. enum resolve_t rc;
  820. *entry = NULL;
  821. if(timeoutms < 0)
  822. /* got an already expired timeout */
  823. return CURLRESOLV_TIMEDOUT;
  824. #ifdef USE_ALARM_TIMEOUT
  825. if(data->set.no_signal)
  826. /* Ignore the timeout when signals are disabled */
  827. timeout = 0;
  828. else
  829. timeout = (timeoutms > LONG_MAX) ? LONG_MAX : (long)timeoutms;
  830. if(!timeout)
  831. /* USE_ALARM_TIMEOUT defined, but no timeout actually requested */
  832. return Curl_resolv(data, hostname, port, TRUE, entry);
  833. if(timeout < 1000) {
  834. /* The alarm() function only provides integer second resolution, so if
  835. we want to wait less than one second we must bail out already now. */
  836. failf(data,
  837. "remaining timeout of %ld too small to resolve via SIGALRM method",
  838. timeout);
  839. return CURLRESOLV_TIMEDOUT;
  840. }
  841. /* This allows us to time-out from the name resolver, as the timeout
  842. will generate a signal and we will siglongjmp() from that here.
  843. This technique has problems (see alarmfunc).
  844. This should be the last thing we do before calling Curl_resolv(),
  845. as otherwise we would have to worry about variables that get modified
  846. before we invoke Curl_resolv() (and thus use "volatile"). */
  847. curl_simple_lock_lock(&curl_jmpenv_lock);
  848. if(sigsetjmp(curl_jmpenv, 1)) {
  849. /* this is coming from a siglongjmp() after an alarm signal */
  850. failf(data, "name lookup timed out");
  851. rc = CURLRESOLV_ERROR;
  852. goto clean_up;
  853. }
  854. else {
  855. /*************************************************************
  856. * Set signal handler to catch SIGALRM
  857. * Store the old value to be able to set it back later!
  858. *************************************************************/
  859. #ifdef HAVE_SIGACTION
  860. sigaction(SIGALRM, NULL, &sigact);
  861. keep_sigact = sigact;
  862. keep_copysig = TRUE; /* yes, we have a copy */
  863. sigact.sa_handler = alarmfunc;
  864. #ifdef SA_RESTART
  865. /* HP-UX does not have SA_RESTART but defaults to that behavior! */
  866. sigact.sa_flags &= ~SA_RESTART;
  867. #endif
  868. /* now set the new struct */
  869. sigaction(SIGALRM, &sigact, NULL);
  870. #else /* HAVE_SIGACTION */
  871. /* no sigaction(), revert to the much lamer signal() */
  872. #ifdef HAVE_SIGNAL
  873. keep_sigact = signal(SIGALRM, alarmfunc);
  874. #endif
  875. #endif /* HAVE_SIGACTION */
  876. /* alarm() makes a signal get sent when the timeout fires off, and that
  877. will abort system calls */
  878. prev_alarm = alarm(curlx_sltoui(timeout/1000L));
  879. }
  880. #else
  881. #ifndef CURLRES_ASYNCH
  882. if(timeoutms)
  883. infof(data, "timeout on name lookup is not supported");
  884. #else
  885. (void)timeoutms; /* timeoutms not used with an async resolver */
  886. #endif
  887. #endif /* USE_ALARM_TIMEOUT */
  888. /* Perform the actual name resolution. This might be interrupted by an
  889. * alarm if it takes too long.
  890. */
  891. rc = Curl_resolv(data, hostname, port, TRUE, entry);
  892. #ifdef USE_ALARM_TIMEOUT
  893. clean_up:
  894. if(!prev_alarm)
  895. /* deactivate a possibly active alarm before uninstalling the handler */
  896. alarm(0);
  897. #ifdef HAVE_SIGACTION
  898. if(keep_copysig) {
  899. /* we got a struct as it looked before, now put that one back nice
  900. and clean */
  901. sigaction(SIGALRM, &keep_sigact, NULL); /* put it back */
  902. }
  903. #else
  904. #ifdef HAVE_SIGNAL
  905. /* restore the previous SIGALRM handler */
  906. signal(SIGALRM, keep_sigact);
  907. #endif
  908. #endif /* HAVE_SIGACTION */
  909. curl_simple_lock_unlock(&curl_jmpenv_lock);
  910. /* switch back the alarm() to either zero or to what it was before minus
  911. the time we spent until now! */
  912. if(prev_alarm) {
  913. /* there was an alarm() set before us, now put it back */
  914. timediff_t elapsed_secs = Curl_timediff(Curl_now(),
  915. data->conn->created) / 1000;
  916. /* the alarm period is counted in even number of seconds */
  917. unsigned long alarm_set = (unsigned long)(prev_alarm - elapsed_secs);
  918. if(!alarm_set ||
  919. ((alarm_set >= 0x80000000) && (prev_alarm < 0x80000000)) ) {
  920. /* if the alarm time-left reached zero or turned "negative" (counted
  921. with unsigned values), we should fire off a SIGALRM here, but we
  922. will not, and zero would be to switch it off so we never set it to
  923. less than 1! */
  924. alarm(1);
  925. rc = CURLRESOLV_TIMEDOUT;
  926. failf(data, "Previous alarm fired off");
  927. }
  928. else
  929. alarm((unsigned int)alarm_set);
  930. }
  931. #endif /* USE_ALARM_TIMEOUT */
  932. return rc;
  933. }
  934. /*
  935. * Curl_resolv_unlink() releases a reference to the given cached DNS entry.
  936. * When the reference count reaches 0, the entry is destroyed. It is important
  937. * that only one unlink is made for each Curl_resolv() call.
  938. *
  939. * May be called with 'data' == NULL for global cache.
  940. */
  941. void Curl_resolv_unlink(struct Curl_easy *data, struct Curl_dns_entry **pdns)
  942. {
  943. struct Curl_dns_entry *dns = *pdns;
  944. *pdns = NULL;
  945. if(data && data->share)
  946. Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
  947. hostcache_unlink_entry(dns);
  948. if(data && data->share)
  949. Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
  950. }
  951. /*
  952. * File-internal: release cache dns entry reference, free if inuse drops to 0
  953. */
  954. static void hostcache_unlink_entry(void *entry)
  955. {
  956. struct Curl_dns_entry *dns = (struct Curl_dns_entry *) entry;
  957. DEBUGASSERT(dns && (dns->refcount > 0));
  958. dns->refcount--;
  959. if(dns->refcount == 0) {
  960. Curl_freeaddrinfo(dns->addr);
  961. #ifdef USE_HTTPSRR
  962. if(dns->hinfo) {
  963. if(dns->hinfo->target)
  964. free(dns->hinfo->target);
  965. if(dns->hinfo->alpns)
  966. free(dns->hinfo->alpns);
  967. if(dns->hinfo->ipv4hints)
  968. free(dns->hinfo->ipv4hints);
  969. if(dns->hinfo->echconfiglist)
  970. free(dns->hinfo->echconfiglist);
  971. if(dns->hinfo->ipv6hints)
  972. free(dns->hinfo->ipv6hints);
  973. if(dns->hinfo->val)
  974. free(dns->hinfo->val);
  975. free(dns->hinfo);
  976. }
  977. #endif
  978. free(dns);
  979. }
  980. }
  981. /*
  982. * Curl_init_dnscache() inits a new DNS cache.
  983. */
  984. void Curl_init_dnscache(struct Curl_hash *hash, size_t size)
  985. {
  986. Curl_hash_init(hash, size, Curl_hash_str, Curl_str_key_compare,
  987. hostcache_unlink_entry);
  988. }
  989. /*
  990. * Curl_hostcache_clean()
  991. *
  992. * This _can_ be called with 'data' == NULL but then of course no locking
  993. * can be done!
  994. */
  995. void Curl_hostcache_clean(struct Curl_easy *data,
  996. struct Curl_hash *hash)
  997. {
  998. if(data && data->share)
  999. Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
  1000. Curl_hash_clean(hash);
  1001. if(data && data->share)
  1002. Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
  1003. }
  1004. CURLcode Curl_loadhostpairs(struct Curl_easy *data)
  1005. {
  1006. struct curl_slist *hostp;
  1007. char *host_end;
  1008. /* Default is no wildcard found */
  1009. data->state.wildcard_resolve = FALSE;
  1010. for(hostp = data->state.resolve; hostp; hostp = hostp->next) {
  1011. char entry_id[MAX_HOSTCACHE_LEN];
  1012. if(!hostp->data)
  1013. continue;
  1014. if(hostp->data[0] == '-') {
  1015. unsigned long num = 0;
  1016. size_t entry_len;
  1017. size_t hlen = 0;
  1018. host_end = strchr(&hostp->data[1], ':');
  1019. if(host_end) {
  1020. hlen = host_end - &hostp->data[1];
  1021. num = strtoul(++host_end, NULL, 10);
  1022. if(!hlen || (num > 0xffff))
  1023. host_end = NULL;
  1024. }
  1025. if(!host_end) {
  1026. infof(data, "Bad syntax CURLOPT_RESOLVE removal entry '%s'",
  1027. hostp->data);
  1028. continue;
  1029. }
  1030. /* Create an entry id, based upon the hostname and port */
  1031. entry_len = create_hostcache_id(&hostp->data[1], hlen, (int)num,
  1032. entry_id, sizeof(entry_id));
  1033. if(data->share)
  1034. Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
  1035. /* delete entry, ignore if it did not exist */
  1036. Curl_hash_delete(data->dns.hostcache, entry_id, entry_len + 1);
  1037. if(data->share)
  1038. Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
  1039. }
  1040. else {
  1041. struct Curl_dns_entry *dns;
  1042. struct Curl_addrinfo *head = NULL, *tail = NULL;
  1043. size_t entry_len;
  1044. char address[64];
  1045. #if !defined(CURL_DISABLE_VERBOSE_STRINGS)
  1046. char *addresses = NULL;
  1047. #endif
  1048. char *addr_begin;
  1049. char *addr_end;
  1050. char *port_ptr;
  1051. int port = 0;
  1052. char *end_ptr;
  1053. bool permanent = TRUE;
  1054. unsigned long tmp_port;
  1055. bool error = TRUE;
  1056. char *host_begin = hostp->data;
  1057. size_t hlen = 0;
  1058. if(host_begin[0] == '+') {
  1059. host_begin++;
  1060. permanent = FALSE;
  1061. }
  1062. host_end = strchr(host_begin, ':');
  1063. if(!host_end)
  1064. goto err;
  1065. hlen = host_end - host_begin;
  1066. port_ptr = host_end + 1;
  1067. tmp_port = strtoul(port_ptr, &end_ptr, 10);
  1068. if(tmp_port > USHRT_MAX || end_ptr == port_ptr || *end_ptr != ':')
  1069. goto err;
  1070. port = (int)tmp_port;
  1071. #if !defined(CURL_DISABLE_VERBOSE_STRINGS)
  1072. addresses = end_ptr + 1;
  1073. #endif
  1074. while(*end_ptr) {
  1075. size_t alen;
  1076. struct Curl_addrinfo *ai;
  1077. addr_begin = end_ptr + 1;
  1078. addr_end = strchr(addr_begin, ',');
  1079. if(!addr_end)
  1080. addr_end = addr_begin + strlen(addr_begin);
  1081. end_ptr = addr_end;
  1082. /* allow IP(v6) address within [brackets] */
  1083. if(*addr_begin == '[') {
  1084. if(addr_end == addr_begin || *(addr_end - 1) != ']')
  1085. goto err;
  1086. ++addr_begin;
  1087. --addr_end;
  1088. }
  1089. alen = addr_end - addr_begin;
  1090. if(!alen)
  1091. continue;
  1092. if(alen >= sizeof(address))
  1093. goto err;
  1094. memcpy(address, addr_begin, alen);
  1095. address[alen] = '\0';
  1096. #ifndef USE_IPV6
  1097. if(strchr(address, ':')) {
  1098. infof(data, "Ignoring resolve address '%s', missing IPv6 support.",
  1099. address);
  1100. continue;
  1101. }
  1102. #endif
  1103. ai = Curl_str2addr(address, port);
  1104. if(!ai) {
  1105. infof(data, "Resolve address '%s' found illegal", address);
  1106. goto err;
  1107. }
  1108. if(tail) {
  1109. tail->ai_next = ai;
  1110. tail = tail->ai_next;
  1111. }
  1112. else {
  1113. head = tail = ai;
  1114. }
  1115. }
  1116. if(!head)
  1117. goto err;
  1118. error = FALSE;
  1119. err:
  1120. if(error) {
  1121. failf(data, "Couldn't parse CURLOPT_RESOLVE entry '%s'",
  1122. hostp->data);
  1123. Curl_freeaddrinfo(head);
  1124. return CURLE_SETOPT_OPTION_SYNTAX;
  1125. }
  1126. /* Create an entry id, based upon the hostname and port */
  1127. entry_len = create_hostcache_id(host_begin, hlen, port,
  1128. entry_id, sizeof(entry_id));
  1129. if(data->share)
  1130. Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
  1131. /* See if it is already in our dns cache */
  1132. dns = Curl_hash_pick(data->dns.hostcache, entry_id, entry_len + 1);
  1133. if(dns) {
  1134. infof(data, "RESOLVE %.*s:%d - old addresses discarded",
  1135. (int)hlen, host_begin, port);
  1136. /* delete old entry, there are two reasons for this
  1137. 1. old entry may have different addresses.
  1138. 2. even if entry with correct addresses is already in the cache,
  1139. but if it is close to expire, then by the time next http
  1140. request is made, it can get expired and pruned because old
  1141. entry is not necessarily marked as permanent.
  1142. 3. when adding a non-permanent entry, we want it to remove and
  1143. replace an existing permanent entry.
  1144. 4. when adding a non-permanent entry, we want it to get a "fresh"
  1145. timeout that starts _now_. */
  1146. Curl_hash_delete(data->dns.hostcache, entry_id, entry_len + 1);
  1147. }
  1148. /* put this new host in the cache */
  1149. dns = Curl_cache_addr(data, head, host_begin, hlen, port, permanent);
  1150. if(dns) {
  1151. /* release the returned reference; the cache itself will keep the
  1152. * entry alive: */
  1153. dns->refcount--;
  1154. }
  1155. if(data->share)
  1156. Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
  1157. if(!dns) {
  1158. Curl_freeaddrinfo(head);
  1159. return CURLE_OUT_OF_MEMORY;
  1160. }
  1161. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  1162. infof(data, "Added %.*s:%d:%s to DNS cache%s",
  1163. (int)hlen, host_begin, port, addresses,
  1164. permanent ? "" : " (non-permanent)");
  1165. #endif
  1166. /* Wildcard hostname */
  1167. if((hlen == 1) && (host_begin[0] == '*')) {
  1168. infof(data, "RESOLVE *:%d using wildcard", port);
  1169. data->state.wildcard_resolve = TRUE;
  1170. }
  1171. }
  1172. }
  1173. data->state.resolve = NULL; /* dealt with now */
  1174. return CURLE_OK;
  1175. }
  1176. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  1177. static void show_resolve_info(struct Curl_easy *data,
  1178. struct Curl_dns_entry *dns)
  1179. {
  1180. struct Curl_addrinfo *a;
  1181. CURLcode result = CURLE_OK;
  1182. #ifdef CURLRES_IPV6
  1183. struct dynbuf out[2];
  1184. #else
  1185. struct dynbuf out[1];
  1186. #endif
  1187. DEBUGASSERT(data);
  1188. DEBUGASSERT(dns);
  1189. if(!data->set.verbose ||
  1190. /* ignore no name or numerical IP addresses */
  1191. !dns->hostname[0] || Curl_host_is_ipnum(dns->hostname))
  1192. return;
  1193. a = dns->addr;
  1194. infof(data, "Host %s:%d was resolved.",
  1195. (dns->hostname[0] ? dns->hostname : "(none)"), dns->hostport);
  1196. Curl_dyn_init(&out[0], 1024);
  1197. #ifdef CURLRES_IPV6
  1198. Curl_dyn_init(&out[1], 1024);
  1199. #endif
  1200. while(a) {
  1201. if(
  1202. #ifdef CURLRES_IPV6
  1203. a->ai_family == PF_INET6 ||
  1204. #endif
  1205. a->ai_family == PF_INET) {
  1206. char buf[MAX_IPADR_LEN];
  1207. struct dynbuf *d = &out[(a->ai_family != PF_INET)];
  1208. Curl_printable_address(a, buf, sizeof(buf));
  1209. if(Curl_dyn_len(d))
  1210. result = Curl_dyn_addn(d, ", ", 2);
  1211. if(!result)
  1212. result = Curl_dyn_add(d, buf);
  1213. if(result) {
  1214. infof(data, "too many IP, cannot show");
  1215. goto fail;
  1216. }
  1217. }
  1218. a = a->ai_next;
  1219. }
  1220. #ifdef CURLRES_IPV6
  1221. infof(data, "IPv6: %s",
  1222. (Curl_dyn_len(&out[1]) ? Curl_dyn_ptr(&out[1]) : "(none)"));
  1223. #endif
  1224. infof(data, "IPv4: %s",
  1225. (Curl_dyn_len(&out[0]) ? Curl_dyn_ptr(&out[0]) : "(none)"));
  1226. fail:
  1227. Curl_dyn_free(&out[0]);
  1228. #ifdef CURLRES_IPV6
  1229. Curl_dyn_free(&out[1]);
  1230. #endif
  1231. }
  1232. #endif
  1233. CURLcode Curl_resolv_check(struct Curl_easy *data,
  1234. struct Curl_dns_entry **dns)
  1235. {
  1236. CURLcode result;
  1237. #if defined(CURL_DISABLE_DOH) && !defined(CURLRES_ASYNCH)
  1238. (void)data;
  1239. (void)dns;
  1240. #endif
  1241. #ifndef CURL_DISABLE_DOH
  1242. if(data->conn->bits.doh) {
  1243. result = Curl_doh_is_resolved(data, dns);
  1244. }
  1245. else
  1246. #endif
  1247. result = Curl_resolver_is_resolved(data, dns);
  1248. if(*dns)
  1249. show_resolve_info(data, *dns);
  1250. return result;
  1251. }
  1252. int Curl_resolv_getsock(struct Curl_easy *data,
  1253. curl_socket_t *socks)
  1254. {
  1255. #ifdef CURLRES_ASYNCH
  1256. #ifndef CURL_DISABLE_DOH
  1257. if(data->conn->bits.doh)
  1258. /* nothing to wait for during DoH resolve, those handles have their own
  1259. sockets */
  1260. return GETSOCK_BLANK;
  1261. #endif
  1262. return Curl_resolver_getsock(data, socks);
  1263. #else
  1264. (void)data;
  1265. (void)socks;
  1266. return GETSOCK_BLANK;
  1267. #endif
  1268. }
  1269. /* Call this function after Curl_connect() has returned async=TRUE and
  1270. then a successful name resolve has been received.
  1271. Note: this function disconnects and frees the conn data in case of
  1272. resolve failure */
  1273. CURLcode Curl_once_resolved(struct Curl_easy *data, bool *protocol_done)
  1274. {
  1275. CURLcode result;
  1276. struct connectdata *conn = data->conn;
  1277. #ifdef USE_CURL_ASYNC
  1278. if(data->state.async.dns) {
  1279. conn->dns_entry = data->state.async.dns;
  1280. data->state.async.dns = NULL;
  1281. }
  1282. #endif
  1283. result = Curl_setup_conn(data, protocol_done);
  1284. if(result) {
  1285. Curl_detach_connection(data);
  1286. Curl_cpool_disconnect(data, conn, TRUE);
  1287. }
  1288. return result;
  1289. }
  1290. /*
  1291. * Curl_resolver_error() calls failf() with the appropriate message after a
  1292. * resolve error
  1293. */
  1294. #ifdef USE_CURL_ASYNC
  1295. CURLcode Curl_resolver_error(struct Curl_easy *data)
  1296. {
  1297. const char *host_or_proxy;
  1298. CURLcode result;
  1299. #ifndef CURL_DISABLE_PROXY
  1300. struct connectdata *conn = data->conn;
  1301. if(conn->bits.httpproxy) {
  1302. host_or_proxy = "proxy";
  1303. result = CURLE_COULDNT_RESOLVE_PROXY;
  1304. }
  1305. else
  1306. #endif
  1307. {
  1308. host_or_proxy = "host";
  1309. result = CURLE_COULDNT_RESOLVE_HOST;
  1310. }
  1311. failf(data, "Could not resolve %s: %s", host_or_proxy,
  1312. data->state.async.hostname);
  1313. return result;
  1314. }
  1315. #endif /* USE_CURL_ASYNC */