hostip.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2004, 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 http://curl.haxx.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. * $Id: hostip.c,v 1.130 2004/03/17 12:46:46 bagder Exp $
  22. ***************************************************************************/
  23. #include "setup.h"
  24. #include <string.h>
  25. #include <errno.h>
  26. #define _REENTRANT
  27. #if defined(WIN32) && !defined(__GNUC__) || defined(__MINGW32__)
  28. #include <malloc.h>
  29. #else
  30. #ifdef HAVE_SYS_TYPES_H
  31. #include <sys/types.h>
  32. #endif
  33. #ifdef HAVE_SYS_SOCKET_H
  34. #include <sys/socket.h>
  35. #endif
  36. #ifdef HAVE_NETINET_IN_H
  37. #include <netinet/in.h>
  38. #endif
  39. #ifdef HAVE_NETDB_H
  40. #include <netdb.h>
  41. #endif
  42. #ifdef HAVE_ARPA_INET_H
  43. #include <arpa/inet.h>
  44. #endif
  45. #ifdef HAVE_STDLIB_H
  46. #include <stdlib.h> /* required for free() prototypes */
  47. #endif
  48. #ifdef VMS
  49. #include <in.h>
  50. #include <inet.h>
  51. #include <stdlib.h>
  52. #endif
  53. #endif
  54. #ifdef HAVE_SETJMP_H
  55. #include <setjmp.h>
  56. #endif
  57. #if (defined(NETWARE) && defined(__NOVELL_LIBC__))
  58. #undef in_addr_t
  59. #define in_addr_t unsigned long
  60. #endif
  61. #include "urldata.h"
  62. #include "sendf.h"
  63. #include "hostip.h"
  64. #include "hash.h"
  65. #include "share.h"
  66. #include "url.h"
  67. #define _MPRINTF_REPLACE /* use our functions only */
  68. #include <curl/mprintf.h>
  69. #if defined(HAVE_INET_NTOA_R) && !defined(HAVE_INET_NTOA_R_DECL)
  70. #include "inet_ntoa_r.h"
  71. #endif
  72. /* The last #include file should be: */
  73. #ifdef CURLDEBUG
  74. #include "memdebug.h"
  75. #endif
  76. #ifndef ARES_SUCCESS
  77. #define ARES_SUCCESS CURLE_OK
  78. #endif
  79. static curl_hash hostname_cache;
  80. static int host_cache_initialized;
  81. static Curl_addrinfo *my_getaddrinfo(struct connectdata *conn,
  82. char *hostname,
  83. int port,
  84. int *waitp);
  85. #ifndef ENABLE_IPV6
  86. #if !defined(HAVE_GETHOSTBYNAME_R) || defined(USE_ARES) || \
  87. defined(USE_THREADING_GETHOSTBYNAME)
  88. static struct hostent* pack_hostent(char** buf, struct hostent* orig);
  89. #endif
  90. #endif
  91. #ifdef USE_THREADING_GETHOSTBYNAME
  92. #ifdef DEBUG_THREADING_GETHOSTBYNAME
  93. /* If this is defined, provide tracing */
  94. #define TRACE(args) \
  95. do { trace_it("%u: ", __LINE__); trace_it args; } while (0)
  96. static void trace_it (const char *fmt, ...);
  97. #else
  98. #define TRACE(x)
  99. #endif
  100. static struct hostent* pack_hostent (char** buf, struct hostent* orig);
  101. static bool init_gethostbyname_thread (struct connectdata *conn,
  102. const char *hostname, int port);
  103. struct thread_data {
  104. HANDLE thread_hnd;
  105. DWORD thread_id;
  106. DWORD thread_status;
  107. };
  108. #endif
  109. void Curl_global_host_cache_init(void)
  110. {
  111. if (!host_cache_initialized) {
  112. Curl_hash_init(&hostname_cache, 7, Curl_freednsinfo);
  113. host_cache_initialized = 1;
  114. }
  115. }
  116. curl_hash *Curl_global_host_cache_get(void)
  117. {
  118. return &hostname_cache;
  119. }
  120. void Curl_global_host_cache_dtor(void)
  121. {
  122. if (host_cache_initialized) {
  123. Curl_hash_clean(&hostname_cache);
  124. host_cache_initialized = 0;
  125. }
  126. }
  127. /* count the number of characters that an integer takes up */
  128. static int _num_chars(int i)
  129. {
  130. int chars = 0;
  131. /* While the number divided by 10 is greater than one,
  132. * re-divide the number by 10, and increment the number of
  133. * characters by 1.
  134. *
  135. * this relies on the fact that for every multiple of 10,
  136. * a new digit is added onto every number
  137. */
  138. do {
  139. chars++;
  140. i = (int) i / 10;
  141. } while (i >= 1);
  142. return chars;
  143. }
  144. /* Create a hostcache id */
  145. static char *
  146. create_hostcache_id(char *server, int port, size_t *entry_len)
  147. {
  148. char *id = NULL;
  149. /* Get the length of the new entry id */
  150. *entry_len = strlen(server) + /* Hostname length */
  151. 1 + /* ':' seperator */
  152. _num_chars(port); /* number of characters the port will take up */
  153. /* Allocate the new entry id */
  154. id = malloc(*entry_len + 1); /* 1 extra for the zero terminator */
  155. if (!id)
  156. return NULL;
  157. /* Create the new entry */
  158. sprintf(id, "%s:%d", server, port);
  159. return id; /* return pointer to the string */
  160. }
  161. struct hostcache_prune_data {
  162. int cache_timeout;
  163. time_t now;
  164. };
  165. static int
  166. hostcache_timestamp_remove(void *datap, void *hc)
  167. {
  168. struct hostcache_prune_data *data =
  169. (struct hostcache_prune_data *) datap;
  170. struct Curl_dns_entry *c = (struct Curl_dns_entry *) hc;
  171. if ((data->now - c->timestamp < data->cache_timeout) ||
  172. c->inuse) {
  173. /* please don't remove */
  174. return 0;
  175. }
  176. /* fine, remove */
  177. return 1;
  178. }
  179. static void
  180. hostcache_prune(curl_hash *hostcache, int cache_timeout, time_t now)
  181. {
  182. struct hostcache_prune_data user;
  183. user.cache_timeout = cache_timeout;
  184. user.now = now;
  185. Curl_hash_clean_with_criterium(hostcache,
  186. (void *) &user,
  187. hostcache_timestamp_remove);
  188. }
  189. void Curl_hostcache_prune(struct SessionHandle *data)
  190. {
  191. time_t now;
  192. if(data->set.dns_cache_timeout == -1)
  193. /* cache forever means never prune! */
  194. return;
  195. if(data->share)
  196. Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
  197. time(&now);
  198. /* Remove outdated and unused entries from the hostcache */
  199. hostcache_prune(data->hostcache,
  200. data->set.dns_cache_timeout,
  201. now);
  202. if(data->share)
  203. Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
  204. }
  205. #ifdef HAVE_SIGSETJMP
  206. /* Beware this is a global and unique instance */
  207. sigjmp_buf curl_jmpenv;
  208. #endif
  209. /* When calling Curl_resolv() has resulted in a response with a returned
  210. address, we call this function to store the information in the dns
  211. cache etc */
  212. static struct Curl_dns_entry *
  213. cache_resolv_response(struct SessionHandle *data,
  214. Curl_addrinfo *addr,
  215. char *hostname,
  216. int port)
  217. {
  218. char *entry_id;
  219. size_t entry_len;
  220. struct Curl_dns_entry *dns;
  221. time_t now;
  222. /* Create an entry id, based upon the hostname and port */
  223. entry_id = create_hostcache_id(hostname, port, &entry_len);
  224. /* If we can't create the entry id, fail */
  225. if (!entry_id)
  226. return NULL;
  227. /* Create a new cache entry */
  228. dns = (struct Curl_dns_entry *) malloc(sizeof(struct Curl_dns_entry));
  229. if (!dns) {
  230. Curl_freeaddrinfo(addr);
  231. free(entry_id);
  232. return NULL;
  233. }
  234. dns->inuse = 0; /* init to not used */
  235. dns->addr = addr; /* this is the address(es) */
  236. /* Store the resolved data in our DNS cache. This function may return a
  237. pointer to an existing struct already present in the hash, and it may
  238. return the same argument we pass in. Make no assumptions. */
  239. dns = Curl_hash_add(data->hostcache, entry_id, entry_len+1, (void *)dns);
  240. if(!dns) {
  241. /* Major badness, run away. When this happens, the 'dns' data has
  242. already been cleared up by Curl_hash_add(). */
  243. free(entry_id);
  244. return NULL;
  245. }
  246. time(&now);
  247. dns->timestamp = now; /* used now */
  248. dns->inuse++; /* mark entry as in-use */
  249. /* free the allocated entry_id again */
  250. free(entry_id);
  251. return dns;
  252. }
  253. /* Resolve a name and return a pointer in the 'entry' argument if one
  254. is available.
  255. Return codes:
  256. -1 = error, no pointer
  257. 0 = OK, pointer provided
  258. 1 = waiting for response, no pointer
  259. */
  260. int Curl_resolv(struct connectdata *conn,
  261. char *hostname,
  262. int port,
  263. struct Curl_dns_entry **entry)
  264. {
  265. char *entry_id = NULL;
  266. struct Curl_dns_entry *dns = NULL;
  267. size_t entry_len;
  268. int wait;
  269. struct SessionHandle *data = conn->data;
  270. CURLcode result;
  271. /* default to failure */
  272. int rc = -1;
  273. *entry = NULL;
  274. #ifdef HAVE_SIGSETJMP
  275. /* this allows us to time-out from the name resolver, as the timeout
  276. will generate a signal and we will siglongjmp() from that here */
  277. if(!data->set.no_signal && sigsetjmp(curl_jmpenv, 1)) {
  278. /* this is coming from a siglongjmp() */
  279. failf(data, "name lookup timed out");
  280. return -1;
  281. }
  282. #endif
  283. /* Create an entry id, based upon the hostname and port */
  284. entry_id = create_hostcache_id(hostname, port, &entry_len);
  285. /* If we can't create the entry id, fail */
  286. if (!entry_id)
  287. return -1;
  288. if(data->share)
  289. Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
  290. /* See if its already in our dns cache */
  291. dns = Curl_hash_pick(data->hostcache, entry_id, entry_len+1);
  292. if(data->share)
  293. Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
  294. /* free the allocated entry_id again */
  295. free(entry_id);
  296. if (!dns) {
  297. /* The entry was not in the cache. Resolve it to IP address */
  298. /* If my_getaddrinfo() returns NULL, 'wait' might be set to a non-zero
  299. value indicating that we need to wait for the response to the resolve
  300. call */
  301. Curl_addrinfo *addr = my_getaddrinfo(conn, hostname, port, &wait);
  302. if (!addr) {
  303. if(wait) {
  304. /* the response to our resolve call will come asynchronously at
  305. a later time, good or bad */
  306. /* First, check that we haven't received the info by now */
  307. result = Curl_is_resolved(conn, &dns);
  308. if(result) /* error detected */
  309. return -1;
  310. if(dns)
  311. rc = 0; /* pointer provided */
  312. else
  313. rc = 1; /* no info yet */
  314. }
  315. }
  316. else {
  317. if(data->share)
  318. Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
  319. /* we got a response, store it in the cache */
  320. dns = cache_resolv_response(data, addr, hostname, port);
  321. if(data->share)
  322. Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
  323. if(!dns)
  324. /* returned failure, bail out nicely */
  325. Curl_freeaddrinfo(addr);
  326. else
  327. rc = 0;
  328. }
  329. }
  330. else {
  331. dns->inuse++; /* we use it! */
  332. rc = 0;
  333. }
  334. *entry = dns;
  335. return rc;
  336. }
  337. void Curl_resolv_unlock(struct SessionHandle *data, struct Curl_dns_entry *dns)
  338. {
  339. if(data->share)
  340. Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
  341. dns->inuse--;
  342. #ifdef CURLDEBUG
  343. if(dns->inuse < 0) {
  344. infof(data, "Interal host cache screw-up!");
  345. *(char **)0=NULL;
  346. }
  347. #endif
  348. if(data->share)
  349. Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
  350. }
  351. /*
  352. * This is a wrapper function for freeing name information in a protocol
  353. * independent way. This takes care of using the appropriate underlaying
  354. * function.
  355. */
  356. void Curl_freeaddrinfo(Curl_addrinfo *p)
  357. {
  358. #ifdef ENABLE_IPV6
  359. freeaddrinfo(p);
  360. #else
  361. free(p); /* works fine for the ARES case too */
  362. #endif
  363. }
  364. /*
  365. * Free a cache dns entry.
  366. */
  367. void Curl_freednsinfo(void *freethis)
  368. {
  369. struct Curl_dns_entry *p = (struct Curl_dns_entry *) freethis;
  370. Curl_freeaddrinfo(p->addr);
  371. free(p);
  372. }
  373. /* --- resolve name or IP-number --- */
  374. /* Allocate enough memory to hold the full name information structs and
  375. * everything. OSF1 is known to require at least 8872 bytes. The buffer
  376. * required for storing all possible aliases and IP numbers is according to
  377. * Stevens' Unix Network Programming 2nd edition, p. 304: 8192 bytes!
  378. */
  379. #define CURL_NAMELOOKUP_SIZE 9000
  380. #ifdef USE_ARES
  381. CURLcode Curl_multi_ares_fdset(struct connectdata *conn,
  382. fd_set *read_fd_set,
  383. fd_set *write_fd_set,
  384. int *max_fdp)
  385. {
  386. int max = ares_fds(conn->data->state.areschannel,
  387. read_fd_set, write_fd_set);
  388. *max_fdp = max;
  389. return CURLE_OK;
  390. }
  391. /* called to check if the name is resolved now */
  392. CURLcode Curl_is_resolved(struct connectdata *conn,
  393. struct Curl_dns_entry **dns)
  394. {
  395. fd_set read_fds, write_fds;
  396. static const struct timeval tv={0,0};
  397. int count;
  398. struct SessionHandle *data = conn->data;
  399. int nfds;
  400. FD_ZERO(&read_fds);
  401. FD_ZERO(&write_fds);
  402. nfds = ares_fds(data->state.areschannel, &read_fds, &write_fds);
  403. count = select(nfds, &read_fds, &write_fds, NULL,
  404. (struct timeval *)&tv);
  405. if(count)
  406. ares_process(data->state.areschannel, &read_fds, &write_fds);
  407. *dns = NULL;
  408. if(conn->async.done) {
  409. /* we're done, kill the ares handle */
  410. if(!conn->async.dns)
  411. return CURLE_COULDNT_RESOLVE_HOST;
  412. *dns = conn->async.dns;
  413. }
  414. return CURLE_OK;
  415. }
  416. /* This is a function that locks and waits until the name resolve operation
  417. has completed.
  418. If 'entry' is non-NULL, make it point to the resolved dns entry
  419. Return CURLE_COULDNT_RESOLVE_HOST if the host was not resolved, and
  420. CURLE_OPERATION_TIMEDOUT if a time-out occurred.
  421. */
  422. CURLcode Curl_wait_for_resolv(struct connectdata *conn,
  423. struct Curl_dns_entry **entry)
  424. {
  425. CURLcode rc=CURLE_OK;
  426. struct SessionHandle *data = conn->data;
  427. struct timeval now = Curl_tvnow();
  428. bool timedout = FALSE;
  429. long timeout = 300; /* default name resolve timeout in seconds */
  430. long elapsed = 0; /* time taken so far */
  431. /* now, see if there's a connect timeout or a regular timeout to
  432. use instead of the default one */
  433. if(conn->data->set.connecttimeout)
  434. timeout = conn->data->set.connecttimeout;
  435. else if(conn->data->set.timeout)
  436. timeout = conn->data->set.timeout;
  437. /* Wait for the name resolve query to complete. */
  438. while (1) {
  439. int nfds=0;
  440. fd_set read_fds, write_fds;
  441. struct timeval *tvp, tv, store;
  442. int count;
  443. store.tv_sec = (int)(timeout - elapsed);
  444. store.tv_usec = 0;
  445. FD_ZERO(&read_fds);
  446. FD_ZERO(&write_fds);
  447. nfds = ares_fds(data->state.areschannel, &read_fds, &write_fds);
  448. if (nfds == 0)
  449. break;
  450. tvp = ares_timeout(data->state.areschannel,
  451. &store, &tv);
  452. count = select(nfds, &read_fds, &write_fds, NULL, tvp);
  453. if (count < 0 && errno != EINVAL)
  454. break;
  455. else if(!count) {
  456. /* timeout */
  457. timedout = TRUE;
  458. break;
  459. }
  460. ares_process(data->state.areschannel, &read_fds, &write_fds);
  461. elapsed = Curl_tvdiff(Curl_tvnow(), now)/1000; /* spent time */
  462. }
  463. /* Operation complete, if the lookup was successful we now have the entry
  464. in the cache. */
  465. if(entry)
  466. *entry = conn->async.dns;
  467. if(!conn->async.dns) {
  468. /* a name was not resolved */
  469. if(timedout || (conn->async.status == ARES_ETIMEOUT)) {
  470. failf(data, "Resolving host timed out: %s", conn->name);
  471. rc = CURLE_OPERATION_TIMEDOUT;
  472. }
  473. else if(conn->async.done) {
  474. failf(data, "Could not resolve host: %s (%s)", conn->name,
  475. ares_strerror(conn->async.status));
  476. rc = CURLE_COULDNT_RESOLVE_HOST;
  477. }
  478. else
  479. rc = CURLE_OPERATION_TIMEDOUT;
  480. /* close the connection, since we can't return failure here without
  481. cleaning up this connection properly */
  482. Curl_disconnect(conn);
  483. }
  484. return rc;
  485. }
  486. #endif
  487. #if defined(USE_ARES) || defined(USE_THREADING_GETHOSTBYNAME)
  488. /* this function gets called by ares/gethostbyname_thread() when we got
  489. the name resolved or not */
  490. static void host_callback(void *arg, /* "struct connectdata *" */
  491. int status,
  492. struct hostent *hostent)
  493. {
  494. struct connectdata *conn = (struct connectdata *)arg;
  495. struct Curl_dns_entry *dns = NULL;
  496. conn->async.done = TRUE;
  497. conn->async.status = status;
  498. if(ARES_SUCCESS == status) {
  499. /* we got a resolved name in 'hostent' */
  500. char *bufp = (char *)malloc(CURL_NAMELOOKUP_SIZE);
  501. if(bufp) {
  502. /* pack_hostent() copies to and shrinks the target buffer */
  503. struct hostent *he = pack_hostent(&bufp, hostent);
  504. struct SessionHandle *data = conn->data;
  505. if(data->share)
  506. Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
  507. dns = cache_resolv_response(data, he,
  508. conn->async.hostname, conn->async.port);
  509. if(data->share)
  510. Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
  511. }
  512. }
  513. conn->async.dns = dns;
  514. /* The input hostent struct will be freed by ares when we return from this
  515. function */
  516. }
  517. #endif
  518. #ifdef USE_ARES
  519. /*
  520. * Return name information about the given hostname and port number. If
  521. * successful, the 'hostent' is returned and the forth argument will point to
  522. * memory we need to free after use. That meory *MUST* be freed with
  523. * Curl_freeaddrinfo(), nothing else.
  524. */
  525. static Curl_addrinfo *my_getaddrinfo(struct connectdata *conn,
  526. char *hostname,
  527. int port,
  528. int *waitp)
  529. {
  530. char *bufp;
  531. struct SessionHandle *data = conn->data;
  532. *waitp = FALSE;
  533. bufp = strdup(hostname);
  534. if(bufp) {
  535. Curl_safefree(conn->async.hostname);
  536. conn->async.hostname = bufp;
  537. conn->async.port = port;
  538. conn->async.done = FALSE; /* not done */
  539. conn->async.status = 0; /* clear */
  540. conn->async.dns = NULL; /* clear */
  541. /* areschannel is already setup in the Curl_open() function */
  542. ares_gethostbyname(data->state.areschannel, hostname, PF_INET,
  543. host_callback, conn);
  544. *waitp = TRUE; /* please wait for the response */
  545. }
  546. return NULL; /* no struct yet */
  547. }
  548. #endif
  549. #if !defined(USE_ARES) && !defined(USE_THREADING_GETHOSTBYNAME)
  550. /* For builds without ARES and threaded gethostbyname, Curl_resolv() can never
  551. return wait==TRUE, so this function will never be called. If it still gets
  552. called, we return failure at once. */
  553. CURLcode Curl_wait_for_resolv(struct connectdata *conn,
  554. struct Curl_dns_entry **entry)
  555. {
  556. (void)conn;
  557. *entry=NULL;
  558. return CURLE_COULDNT_RESOLVE_HOST;
  559. }
  560. CURLcode Curl_is_resolved(struct connectdata *conn,
  561. struct Curl_dns_entry **dns)
  562. {
  563. (void)conn;
  564. *dns = NULL;
  565. return CURLE_COULDNT_RESOLVE_HOST;
  566. }
  567. #endif
  568. #if !defined(USE_ARES)
  569. CURLcode Curl_multi_ares_fdset(struct connectdata *conn,
  570. fd_set *read_fd_set,
  571. fd_set *write_fd_set,
  572. int *max_fdp)
  573. {
  574. (void)conn;
  575. (void)read_fd_set;
  576. (void)write_fd_set;
  577. (void)max_fdp;
  578. return CURLE_OK;
  579. }
  580. #endif
  581. #if defined(ENABLE_IPV6) && !defined(USE_ARES)
  582. #ifdef CURLDEBUG
  583. /* These two are strictly for memory tracing and are using the same
  584. * style as the family otherwise present in memdebug.c. I put these ones
  585. * here since they require a bunch of struct types I didn't wanna include
  586. * in memdebug.c
  587. */
  588. int curl_getaddrinfo(char *hostname, char *service,
  589. struct addrinfo *hints,
  590. struct addrinfo **result,
  591. int line, const char *source)
  592. {
  593. int res=(getaddrinfo)(hostname, service, hints, result);
  594. if(0 == res) {
  595. /* success */
  596. if(logfile)
  597. fprintf(logfile, "ADDR %s:%d getaddrinfo() = %p\n",
  598. source, line, (void *)*result);
  599. }
  600. else {
  601. if(logfile)
  602. fprintf(logfile, "ADDR %s:%d getaddrinfo() failed\n",
  603. source, line);
  604. }
  605. return res;
  606. }
  607. void curl_freeaddrinfo(struct addrinfo *freethis,
  608. int line, const char *source)
  609. {
  610. (freeaddrinfo)(freethis);
  611. if(logfile)
  612. fprintf(logfile, "ADDR %s:%d freeaddrinfo(%p)\n",
  613. source, line, (void *)freethis);
  614. }
  615. #endif
  616. /*
  617. * Return name information about the given hostname and port number. If
  618. * successful, the 'addrinfo' is returned and the forth argument will point to
  619. * memory we need to free after use. That meory *MUST* be freed with
  620. * Curl_freeaddrinfo(), nothing else.
  621. */
  622. static Curl_addrinfo *my_getaddrinfo(struct connectdata *conn,
  623. char *hostname,
  624. int port,
  625. int *waitp)
  626. {
  627. struct addrinfo hints, *res;
  628. int error;
  629. char sbuf[NI_MAXSERV];
  630. int s, pf;
  631. struct SessionHandle *data = conn->data;
  632. *waitp=0; /* don't wait, we have the response now */
  633. /* see if we have an IPv6 stack */
  634. s = socket(PF_INET6, SOCK_DGRAM, 0);
  635. if (s < 0)
  636. /* Some non-IPv6 stacks have been found to make very slow name resolves
  637. * when PF_UNSPEC is used, so thus we switch to a mere PF_INET lookup if
  638. * the stack seems to be a non-ipv6 one. */
  639. pf = PF_INET;
  640. else {
  641. /* This seems to be an IPv6-capable stack, use PF_UNSPEC for the widest
  642. * possible checks. And close the socket again.
  643. */
  644. sclose(s);
  645. /*
  646. * Check if a more limited name resolve has been requested.
  647. */
  648. switch(data->set.ip_version) {
  649. case CURL_IPRESOLVE_V4:
  650. pf = PF_INET;
  651. break;
  652. case CURL_IPRESOLVE_V6:
  653. pf = PF_INET6;
  654. break;
  655. default:
  656. pf = PF_UNSPEC;
  657. break;
  658. }
  659. }
  660. memset(&hints, 0, sizeof(hints));
  661. hints.ai_family = pf;
  662. hints.ai_socktype = SOCK_STREAM;
  663. hints.ai_flags = AI_CANONNAME;
  664. snprintf(sbuf, sizeof(sbuf), "%d", port);
  665. error = getaddrinfo(hostname, sbuf, &hints, &res);
  666. if (error) {
  667. infof(data, "getaddrinfo(3) failed for %s:%d\n", hostname, port);
  668. return NULL;
  669. }
  670. return res;
  671. }
  672. #else /* following code is IPv4-only */
  673. #if !defined(HAVE_GETHOSTBYNAME_R) || defined(USE_ARES) || defined(USE_THREADING_GETHOSTBYNAME)
  674. static void hostcache_fixoffset(struct hostent *h, long offset);
  675. /*
  676. * Performs a "deep" copy of a hostent into a buffer (returns a pointer to the
  677. * copy). Make absolutely sure the destination buffer is big enough!
  678. */
  679. static struct hostent* pack_hostent(char** buf, struct hostent* orig)
  680. {
  681. char *bufptr;
  682. char *newbuf;
  683. struct hostent* copy;
  684. int i;
  685. char *str;
  686. size_t len;
  687. bufptr = *buf;
  688. copy = (struct hostent*)bufptr;
  689. bufptr += sizeof(struct hostent);
  690. copy->h_name = bufptr;
  691. len = strlen(orig->h_name) + 1;
  692. strncpy(bufptr, orig->h_name, len);
  693. bufptr += len;
  694. /* we align on even 64bit boundaries for safety */
  695. #define MEMALIGN(x) ((x)+(8-(((unsigned long)(x))&0x7)))
  696. /* This must be aligned properly to work on many CPU architectures! */
  697. bufptr = MEMALIGN(bufptr);
  698. copy->h_aliases = (char**)bufptr;
  699. /* Figure out how many aliases there are */
  700. for (i = 0; orig->h_aliases && orig->h_aliases[i]; ++i);
  701. /* Reserve room for the array */
  702. bufptr += (i + 1) * sizeof(char*);
  703. /* Clone all known aliases */
  704. if(orig->h_aliases) {
  705. for(i = 0; (str = orig->h_aliases[i]); i++) {
  706. len = strlen(str) + 1;
  707. strncpy(bufptr, str, len);
  708. copy->h_aliases[i] = bufptr;
  709. bufptr += len;
  710. }
  711. }
  712. /* if(!orig->h_aliases) i was already set to 0 */
  713. /* Terminate the alias list with a NULL */
  714. copy->h_aliases[i] = NULL;
  715. copy->h_addrtype = orig->h_addrtype;
  716. copy->h_length = orig->h_length;
  717. /* align it for (at least) 32bit accesses */
  718. bufptr = MEMALIGN(bufptr);
  719. copy->h_addr_list = (char**)bufptr;
  720. /* Figure out how many addresses there are */
  721. for (i = 0; orig->h_addr_list[i] != NULL; ++i);
  722. /* Reserve room for the array */
  723. bufptr += (i + 1) * sizeof(char*);
  724. i = 0;
  725. len = orig->h_length;
  726. str = orig->h_addr_list[i];
  727. while (str != NULL) {
  728. memcpy(bufptr, str, len);
  729. copy->h_addr_list[i] = bufptr;
  730. bufptr += len;
  731. str = orig->h_addr_list[++i];
  732. }
  733. copy->h_addr_list[i] = NULL;
  734. /* now, shrink the allocated buffer to the size we actually need, which
  735. most often is only a fraction of the original alloc */
  736. newbuf=(char *)realloc(*buf, (long)bufptr-(long)(*buf));
  737. /* if the alloc moved, we need to adjust things again */
  738. if(newbuf != *buf)
  739. hostcache_fixoffset((struct hostent*)newbuf, (long)newbuf-(long)*buf);
  740. /* setup the return */
  741. *buf = newbuf;
  742. copy = (struct hostent*)newbuf;
  743. return copy;
  744. }
  745. #endif
  746. static void hostcache_fixoffset(struct hostent *h, long offset)
  747. {
  748. int i=0;
  749. h->h_name=(char *)((long)h->h_name+offset);
  750. if(h->h_aliases) {
  751. /* only relocate aliases if there are any! */
  752. h->h_aliases=(char **)((long)h->h_aliases+offset);
  753. while(h->h_aliases[i]) {
  754. h->h_aliases[i]=(char *)((long)h->h_aliases[i]+offset);
  755. i++;
  756. }
  757. }
  758. h->h_addr_list=(char **)((long)h->h_addr_list+offset);
  759. i=0;
  760. while(h->h_addr_list[i]) {
  761. h->h_addr_list[i]=(char *)((long)h->h_addr_list[i]+offset);
  762. i++;
  763. }
  764. }
  765. #ifndef USE_ARES
  766. static char *MakeIP(unsigned long num, char *addr, int addr_len)
  767. {
  768. #if defined(HAVE_INET_NTOA) || defined(HAVE_INET_NTOA_R)
  769. struct in_addr in;
  770. in.s_addr = htonl(num);
  771. #if defined(HAVE_INET_NTOA_R)
  772. inet_ntoa_r(in,addr,addr_len);
  773. #else
  774. strncpy(addr,inet_ntoa(in),addr_len);
  775. #endif
  776. #else
  777. unsigned char *paddr;
  778. num = htonl(num); /* htonl() added to avoid endian probs */
  779. paddr = (unsigned char *)&num;
  780. sprintf(addr, "%u.%u.%u.%u", paddr[0], paddr[1], paddr[2], paddr[3]);
  781. #endif
  782. return (addr);
  783. }
  784. /* The original code to this function was once stolen from the Dancer source
  785. code, written by Bjorn Reese, it has since been patched and modified
  786. considerably. */
  787. static Curl_addrinfo *my_getaddrinfo(struct connectdata *conn,
  788. char *hostname,
  789. int port,
  790. int *waitp)
  791. {
  792. struct hostent *h = NULL;
  793. in_addr_t in;
  794. struct SessionHandle *data = conn->data;
  795. (void)port; /* unused in IPv4 code */
  796. *waitp = 0; /* don't wait, we act synchronously */
  797. in=inet_addr(hostname);
  798. if (in != CURL_INADDR_NONE) {
  799. struct in_addr *addrentry;
  800. struct namebuf {
  801. struct hostent hostentry;
  802. char *h_addr_list[2];
  803. struct in_addr addrentry;
  804. char h_name[128];
  805. } *buf = (struct namebuf *)malloc(sizeof(struct namebuf));
  806. if(!buf)
  807. return NULL; /* major failure */
  808. h = &buf->hostentry;
  809. h->h_addr_list = &buf->h_addr_list[0];
  810. addrentry = &buf->addrentry;
  811. addrentry->s_addr = in;
  812. h->h_addr_list[0] = (char*)addrentry;
  813. h->h_addr_list[1] = NULL;
  814. h->h_addrtype = AF_INET;
  815. h->h_length = sizeof(*addrentry);
  816. h->h_name = &buf->h_name[0];
  817. MakeIP(ntohl(in), (char *)h->h_name, sizeof(buf->h_name));
  818. }
  819. #if defined(HAVE_GETHOSTBYNAME_R)
  820. else {
  821. int h_errnop;
  822. int res=ERANGE;
  823. int step_size=200;
  824. int *buf = (int *)malloc(CURL_NAMELOOKUP_SIZE);
  825. if(!buf)
  826. return NULL; /* major failure */
  827. /* Workaround for gethostbyname_r bug in qnx nto. It is also _required_
  828. for some of these functions. */
  829. memset(buf, 0, CURL_NAMELOOKUP_SIZE);
  830. #ifdef HAVE_GETHOSTBYNAME_R_5
  831. /* Solaris, IRIX and more */
  832. (void)res; /* prevent compiler warning */
  833. while(!h) {
  834. h = gethostbyname_r(hostname,
  835. (struct hostent *)buf,
  836. (char *)buf + sizeof(struct hostent),
  837. step_size - sizeof(struct hostent),
  838. &h_errnop);
  839. /* If the buffer is too small, it returns NULL and sets errno to
  840. ERANGE. The errno is thread safe if this is compiled with
  841. -D_REENTRANT as then the 'errno' variable is a macro defined to
  842. get used properly for threads. */
  843. if(h || (errno != ERANGE))
  844. break;
  845. step_size+=200;
  846. }
  847. #ifdef CURLDEBUG
  848. infof(data, "gethostbyname_r() uses %d bytes\n", step_size);
  849. #endif
  850. if(h) {
  851. int offset;
  852. h=(struct hostent *)realloc(buf, step_size);
  853. offset=(long)h-(long)buf;
  854. hostcache_fixoffset(h, offset);
  855. buf=(int *)h;
  856. }
  857. else
  858. #endif /* HAVE_GETHOSTBYNAME_R_5 */
  859. #ifdef HAVE_GETHOSTBYNAME_R_6
  860. /* Linux */
  861. do {
  862. res=gethostbyname_r(hostname,
  863. (struct hostent *)buf,
  864. (char *)buf + sizeof(struct hostent),
  865. step_size - sizeof(struct hostent),
  866. &h, /* DIFFERENCE */
  867. &h_errnop);
  868. /* Redhat 8, using glibc 2.2.93 changed the behavior. Now all of a
  869. sudden this function returns EAGAIN if the given buffer size is too
  870. small. Previous versions are known to return ERANGE for the same
  871. problem.
  872. This wouldn't be such a big problem if older versions wouldn't
  873. sometimes return EAGAIN on a common failure case. Alas, we can't
  874. assume that EAGAIN *or* ERANGE means ERANGE for any given version of
  875. glibc.
  876. For now, we do that and thus we may call the function repeatedly and
  877. fail for older glibc versions that return EAGAIN, until we run out
  878. of buffer size (step_size grows beyond CURL_NAMELOOKUP_SIZE).
  879. If anyone has a better fix, please tell us!
  880. -------------------------------------------------------------------
  881. On October 23rd 2003, Dan C dug up more details on the mysteries of
  882. gethostbyname_r() in glibc:
  883. In glibc 2.2.5 the interface is different (this has also been
  884. discovered in glibc 2.1.1-6 as shipped by Redhat 6). What I can't
  885. explain, is that tests performed on glibc 2.2.4-34 and 2.2.4-32
  886. (shipped/upgraded by Redhat 7.2) don't show this behavior!
  887. In this "buggy" version, the return code is -1 on error and 'errno'
  888. is set to the ERANGE or EAGAIN code. Note that 'errno' is not a
  889. thread-safe variable.
  890. */
  891. if(((ERANGE == res) || (EAGAIN == res)) ||
  892. ((res<0) && ((ERANGE == errno) || (EAGAIN == errno))))
  893. step_size+=200;
  894. else
  895. break;
  896. } while(step_size <= CURL_NAMELOOKUP_SIZE);
  897. if(!h) /* failure */
  898. res=1;
  899. #ifdef CURLDEBUG
  900. infof(data, "gethostbyname_r() uses %d bytes\n", step_size);
  901. #endif
  902. if(!res) {
  903. int offset;
  904. h=(struct hostent *)realloc(buf, step_size);
  905. offset=(long)h-(long)buf;
  906. hostcache_fixoffset(h, offset);
  907. buf=(int *)h;
  908. }
  909. else
  910. #endif/* HAVE_GETHOSTBYNAME_R_6 */
  911. #ifdef HAVE_GETHOSTBYNAME_R_3
  912. /* AIX, Digital Unix/Tru64, HPUX 10, more? */
  913. /* For AIX 4.3 or later, we don't use gethostbyname_r() at all, because of
  914. the plain fact that it does not return unique full buffers on each
  915. call, but instead several of the pointers in the hostent structs will
  916. point to the same actual data! This have the unfortunate down-side that
  917. our caching system breaks down horribly. Luckily for us though, AIX 4.3
  918. and more recent versions have a completely thread-safe libc where all
  919. the data is stored in thread-specific memory areas making calls to the
  920. plain old gethostbyname() work fine even for multi-threaded programs.
  921. This AIX 4.3 or later detection is all made in the configure script.
  922. Troels Walsted Hansen helped us work this out on March 3rd, 2003. */
  923. if(CURL_NAMELOOKUP_SIZE >=
  924. (sizeof(struct hostent)+sizeof(struct hostent_data))) {
  925. /* August 22nd, 2000: Albert Chin-A-Young brought an updated version
  926. * that should work! September 20: Richard Prescott worked on the buffer
  927. * size dilemma. */
  928. res = gethostbyname_r(hostname,
  929. (struct hostent *)buf,
  930. (struct hostent_data *)((char *)buf +
  931. sizeof(struct hostent)));
  932. h_errnop= errno; /* we don't deal with this, but set it anyway */
  933. }
  934. else
  935. res = -1; /* failure, too smallish buffer size */
  936. if(!res) { /* success */
  937. h = (struct hostent*)buf; /* result expected in h */
  938. /* This is the worst kind of the different gethostbyname_r() interfaces.
  939. Since we don't know how big buffer this particular lookup required,
  940. we can't realloc down the huge alloc without doing closer analysis of
  941. the returned data. Thus, we always use CURL_NAMELOOKUP_SIZE for every
  942. name lookup. Fixing this would require an extra malloc() and then
  943. calling pack_hostent() that subsequent realloc()s down the new memory
  944. area to the actually used amount. */
  945. }
  946. else
  947. #endif /* HAVE_GETHOSTBYNAME_R_3 */
  948. {
  949. infof(data, "gethostbyname_r(2) failed for %s\n", hostname);
  950. h = NULL; /* set return code to NULL */
  951. free(buf);
  952. }
  953. #else /* HAVE_GETHOSTBYNAME_R */
  954. else {
  955. #ifdef USE_THREADING_GETHOSTBYNAME
  956. if (init_gethostbyname_thread(conn,hostname,port)) {
  957. *waitp = TRUE; /* please wait for the response */
  958. return NULL;
  959. }
  960. infof(data, "init_gethostbyname_thread() failed for %s; code %lu\n",
  961. hostname, GetLastError());
  962. #endif
  963. h = gethostbyname(hostname);
  964. if (!h)
  965. infof(data, "gethostbyname(2) failed for %s\n", hostname);
  966. else {
  967. char *buf=(char *)malloc(CURL_NAMELOOKUP_SIZE);
  968. /* we make a copy of the hostent right now, right here, as the static
  969. one we got a pointer to might get removed when we don't want/expect
  970. that */
  971. h = pack_hostent(&buf, h);
  972. }
  973. #endif /*HAVE_GETHOSTBYNAME_R */
  974. }
  975. return h;
  976. }
  977. #endif /* end of IPv4-specific code */
  978. #endif /* end of !USE_ARES */
  979. #if defined(USE_THREADING_GETHOSTBYNAME)
  980. #ifdef DEBUG_THREADING_GETHOSTBYNAME
  981. static void trace_it (const char *fmt, ...)
  982. {
  983. static int do_trace = -1;
  984. va_list args;
  985. if (do_trace == -1)
  986. do_trace = getenv("CURL_TRACE") ? 1 : 0;
  987. if (!do_trace)
  988. return;
  989. va_start (args, fmt);
  990. vfprintf (stderr, fmt, args);
  991. fflush (stderr);
  992. va_end (args);
  993. }
  994. #endif
  995. /* For builds without ARES/USE_IPV6, create a resolver thread and wait on it.
  996. */
  997. static DWORD WINAPI gethostbyname_thread (void *arg)
  998. {
  999. struct connectdata *conn = (struct connectdata*) arg;
  1000. struct hostent *he;
  1001. int rc;
  1002. WSASetLastError (conn->async.status = NO_DATA); /* pending status */
  1003. he = gethostbyname (conn->async.hostname);
  1004. if (he) {
  1005. host_callback(conn, ARES_SUCCESS, he);
  1006. rc = 1;
  1007. }
  1008. else {
  1009. host_callback(conn, (int)WSAGetLastError(), NULL);
  1010. rc = 0;
  1011. }
  1012. TRACE(("Winsock-error %d, addr %s\n", conn->async.status,
  1013. he ? inet_ntoa(*(struct in_addr*)he->h_addr) : "unknown"));
  1014. return (rc);
  1015. /* An implicit ExitThread() here */
  1016. }
  1017. /* complementary of ares_destroy
  1018. */
  1019. static void destroy_thread_data (struct connectdata *conn)
  1020. {
  1021. if (conn->async.hostname)
  1022. free(conn->async.hostname);
  1023. if (conn->async.os_specific)
  1024. free(conn->async.os_specific);
  1025. conn->async.hostname = NULL;
  1026. conn->async.os_specific = NULL;
  1027. }
  1028. static bool init_gethostbyname_thread (struct connectdata *conn,
  1029. const char *hostname, int port)
  1030. {
  1031. struct thread_data *td = malloc(sizeof(*td));
  1032. if (!td) {
  1033. SetLastError(ENOMEM);
  1034. return (0);
  1035. }
  1036. memset (td, 0, sizeof(*td));
  1037. Curl_safefree(conn->async.hostname);
  1038. conn->async.hostname = strdup(hostname);
  1039. if (!conn->async.hostname) {
  1040. free(td);
  1041. SetLastError(ENOMEM);
  1042. return (0);
  1043. }
  1044. conn->async.port = port;
  1045. conn->async.done = FALSE;
  1046. conn->async.status = 0;
  1047. conn->async.dns = NULL;
  1048. conn->async.os_specific = (void*) td;
  1049. td->thread_hnd = CreateThread(NULL, 0, gethostbyname_thread,
  1050. conn, 0, &td->thread_id);
  1051. if (!td->thread_hnd) {
  1052. TRACE(("CreateThread() failed; %lu\n", GetLastError()));
  1053. destroy_thread_data(conn);
  1054. return (0);
  1055. }
  1056. return (1);
  1057. }
  1058. /* called to check if the name is resolved now */
  1059. CURLcode Curl_wait_for_resolv(struct connectdata *conn,
  1060. struct Curl_dns_entry **entry)
  1061. {
  1062. struct thread_data *td = (struct thread_data*) conn->async.os_specific;
  1063. struct SessionHandle *data = conn->data;
  1064. long timeout;
  1065. DWORD status, ticks;
  1066. CURLcode rc;
  1067. curlassert (conn && td);
  1068. /* now, see if there's a connect timeout or a regular timeout to
  1069. use instead of the default one */
  1070. timeout = conn->data->set.connecttimeout ? conn->data->set.connecttimeout :
  1071. conn->data->set.timeout ? conn->data->set.timeout :
  1072. 300; /* default name resolve timeout in seconds */
  1073. ticks = GetTickCount();
  1074. status = WaitForSingleObject(td->thread_hnd, 1000UL*timeout);
  1075. if (status == WAIT_OBJECT_0 || status == WAIT_ABANDONED) {
  1076. /* Thread finished before timeout; propagate Winsock error to this thread */
  1077. WSASetLastError(conn->async.status);
  1078. GetExitCodeThread(td->thread_hnd, &td->thread_status);
  1079. TRACE(("status %lu, thread-status %08lX\n", status, td->thread_status));
  1080. }
  1081. else {
  1082. conn->async.done = TRUE;
  1083. TerminateThread(td->thread_hnd, (DWORD)-1);
  1084. td->thread_status = (DWORD)-1;
  1085. }
  1086. TRACE(("gethostbyname_thread() retval %08lX, elapsed %lu ms\n",
  1087. td->thread_status, GetTickCount()-ticks));
  1088. if(entry)
  1089. *entry = conn->async.dns;
  1090. rc = CURLE_OK;
  1091. if (!conn->async.dns) {
  1092. /* a name was not resolved */
  1093. if (td->thread_status == (DWORD)-1 || conn->async.status == NO_DATA) {
  1094. failf(data, "Resolving host timed out: %s", conn->name);
  1095. rc = CURLE_OPERATION_TIMEDOUT;
  1096. }
  1097. else if(conn->async.done) {
  1098. failf(data, "Could not resolve host: %s (code %lu)", conn->name, conn->async.status);
  1099. rc = CURLE_COULDNT_RESOLVE_HOST;
  1100. }
  1101. else
  1102. rc = CURLE_OPERATION_TIMEDOUT;
  1103. destroy_thread_data(conn);
  1104. /* close the connection, since we can't return failure here without
  1105. cleaning up this connection properly */
  1106. Curl_disconnect(conn);
  1107. }
  1108. return (rc);
  1109. }
  1110. CURLcode Curl_is_resolved(struct connectdata *conn,
  1111. struct Curl_dns_entry **entry)
  1112. {
  1113. *entry = NULL;
  1114. if (conn->async.done) {
  1115. /* we're done */
  1116. destroy_thread_data(conn);
  1117. if (!conn->async.dns) {
  1118. TRACE(("Curl_is_resolved(): CURLE_COULDNT_RESOLVE_HOST\n"));
  1119. return CURLE_COULDNT_RESOLVE_HOST;
  1120. }
  1121. *entry = conn->async.dns;
  1122. TRACE(("resolved okay, dns %p\n", *entry));
  1123. }
  1124. else
  1125. TRACE(("not yet\n"));
  1126. return CURLE_OK;
  1127. }
  1128. #endif