2
0

hostip.c 42 KB

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