hostip.c 37 KB

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