asyn.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. #ifndef HEADER_CURL_ASYN_H
  2. #define HEADER_CURL_ASYN_H
  3. /***************************************************************************
  4. * _ _ ____ _
  5. * Project ___| | | | _ \| |
  6. * / __| | | | |_) | |
  7. * | (__| |_| | _ <| |___
  8. * \___|\___/|_| \_\_____|
  9. *
  10. * Copyright (C) Daniel Stenberg, <[email protected]>, et al.
  11. *
  12. * This software is licensed as described in the file COPYING, which
  13. * you should have received as part of this distribution. The terms
  14. * are also available at https://curl.se/docs/copyright.html.
  15. *
  16. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  17. * copies of the Software, and permit persons to whom the Software is
  18. * furnished to do so, under the terms of the COPYING file.
  19. *
  20. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  21. * KIND, either express or implied.
  22. *
  23. * SPDX-License-Identifier: curl
  24. *
  25. ***************************************************************************/
  26. #include "curl_setup.h"
  27. struct Curl_easy;
  28. struct Curl_dns_entry;
  29. #ifdef CURLRES_ASYNCH
  30. #include "curl_addrinfo.h"
  31. #include "httpsrr.h"
  32. struct addrinfo;
  33. struct hostent;
  34. struct connectdata;
  35. struct easy_pollset;
  36. #if defined(CURLRES_ARES) && defined(CURLRES_THREADED)
  37. #error cannot have both CURLRES_ARES and CURLRES_THREADED defined
  38. #endif
  39. /*
  40. * This header defines all functions in the internal asynch resolver interface.
  41. * All asynch resolvers need to provide these functions.
  42. * asyn-ares.c and asyn-thread.c are the current implementations of asynch
  43. * resolver backends.
  44. */
  45. /*
  46. * Curl_async_global_init()
  47. *
  48. * Called from curl_global_init() to initialize global resolver environment.
  49. * Returning anything else than CURLE_OK fails curl_global_init().
  50. */
  51. int Curl_async_global_init(void);
  52. /*
  53. * Curl_async_global_cleanup()
  54. * Called from curl_global_cleanup() to destroy global resolver environment.
  55. */
  56. void Curl_async_global_cleanup(void);
  57. /*
  58. * Curl_async_get_impl()
  59. * Get the resolver implementation instance (c-ares channel) or NULL
  60. * for passing to application callback.
  61. */
  62. CURLcode Curl_async_get_impl(struct Curl_easy *easy, void **impl);
  63. /* Curl_async_pollset()
  64. *
  65. * This function is called from the Curl_multi_pollset() function. 'sock' is a
  66. * pointer to an array to hold the file descriptors, with 'numsock' being the
  67. * size of that array (in number of entries). This function is supposed to
  68. * return bitmask indicating what file descriptors (referring to array indexes
  69. * in the 'sock' array) to wait for, read/write.
  70. */
  71. CURLcode Curl_async_pollset(struct Curl_easy *data, struct easy_pollset *ps);
  72. /*
  73. * Curl_async_is_resolved()
  74. *
  75. * Called repeatedly to check if a previous name resolve request has
  76. * completed. It should also make sure to time-out if the operation seems to
  77. * take too long.
  78. *
  79. * Returns normal CURLcode errors.
  80. */
  81. CURLcode Curl_async_is_resolved(struct Curl_easy *data,
  82. struct Curl_dns_entry **dns);
  83. /*
  84. * Curl_async_await()
  85. *
  86. * Waits for a resolve to finish. This function should be avoided since using
  87. * this risk getting the multi interface to "hang".
  88. *
  89. * On return 'entry' is assigned the resolved dns (CURLE_OK or NULL otherwise.
  90. *
  91. * Returns CURLE_COULDNT_RESOLVE_HOST if the host was not resolved,
  92. * CURLE_OPERATION_TIMEDOUT if a time-out occurred, or other errors.
  93. */
  94. CURLcode Curl_async_await(struct Curl_easy *data,
  95. struct Curl_dns_entry **dnsentry);
  96. /*
  97. * Curl_async_getaddrinfo() - when using this resolver
  98. *
  99. * Returns name information about the given hostname and port number. If
  100. * successful, the 'hostent' is returned and the fourth argument will point to
  101. * memory we need to free after use. That memory *MUST* be freed with
  102. * Curl_freeaddrinfo(), nothing else.
  103. *
  104. * Each resolver backend must of course make sure to return data in the
  105. * correct format to comply with this.
  106. */
  107. struct Curl_addrinfo *Curl_async_getaddrinfo(struct Curl_easy *data,
  108. const char *hostname,
  109. int port,
  110. int ip_version,
  111. int *waitp);
  112. #ifdef USE_ARES
  113. /* common functions for c-ares and threaded resolver with HTTPSRR */
  114. #include <ares.h>
  115. CURLcode Curl_ares_pollset(struct Curl_easy *data,
  116. ares_channel channel,
  117. struct easy_pollset *ps);
  118. int Curl_ares_perform(ares_channel channel,
  119. timediff_t timeout_ms);
  120. #endif
  121. #ifdef CURLRES_ARES
  122. /* async resolving implementation using c-ares alone */
  123. struct async_ares_ctx {
  124. ares_channel channel;
  125. int num_pending; /* number of outstanding c-ares requests */
  126. struct Curl_addrinfo *temp_ai; /* intermediary result while fetching c-ares
  127. parts */
  128. int ares_status; /* ARES_SUCCESS, ARES_ENOTFOUND, etc. */
  129. CURLcode result; /* CURLE_OK or error handling response */
  130. #ifndef HAVE_CARES_GETADDRINFO
  131. struct curltime happy_eyeballs_dns_time; /* when this timer started, or 0 */
  132. #endif
  133. #ifdef USE_HTTPSRR
  134. struct Curl_https_rrinfo hinfo;
  135. #endif
  136. };
  137. void Curl_async_ares_shutdown(struct Curl_easy *data);
  138. void Curl_async_ares_destroy(struct Curl_easy *data);
  139. /* Set the DNS server to use by ares, from `data` settings. */
  140. CURLcode Curl_async_ares_set_dns_servers(struct Curl_easy *data);
  141. /* Set the DNS interfacer to use by ares, from `data` settings. */
  142. CURLcode Curl_async_ares_set_dns_interface(struct Curl_easy *data);
  143. /* Set the local ipv4 address to use by ares, from `data` settings. */
  144. CURLcode Curl_async_ares_set_dns_local_ip4(struct Curl_easy *data);
  145. /* Set the local ipv6 address to use by ares, from `data` settings. */
  146. CURLcode Curl_async_ares_set_dns_local_ip6(struct Curl_easy *data);
  147. #endif /* CURLRES_ARES */
  148. #ifdef CURLRES_THREADED
  149. /* async resolving implementation using POSIX threads */
  150. #include "curl_threads.h"
  151. /* Context for threaded address resolver */
  152. struct async_thrdd_addr_ctx {
  153. curl_thread_t thread_hnd;
  154. char *hostname; /* hostname to resolve, Curl_async.hostname
  155. duplicate */
  156. curl_mutex_t mutx;
  157. #ifndef CURL_DISABLE_SOCKETPAIR
  158. curl_socket_t sock_pair[2]; /* eventfd/pipes/socket pair */
  159. #endif
  160. struct Curl_addrinfo *res;
  161. #ifdef HAVE_GETADDRINFO
  162. struct addrinfo hints;
  163. #endif
  164. struct curltime start;
  165. timediff_t interval_end;
  166. unsigned int poll_interval;
  167. int port;
  168. int sock_error;
  169. int ref_count;
  170. BIT(thrd_done);
  171. BIT(do_abort);
  172. };
  173. /* Context for threaded resolver */
  174. struct async_thrdd_ctx {
  175. /* `addr` is a pointer since this memory is shared with a started
  176. * thread. Since threads cannot be killed, we use reference counting
  177. * so that we can "release" our pointer to this memory while the
  178. * thread is still running. */
  179. struct async_thrdd_addr_ctx *addr;
  180. #if defined(USE_HTTPSRR) && defined(USE_ARES)
  181. struct {
  182. ares_channel channel;
  183. struct Curl_https_rrinfo hinfo;
  184. CURLcode result;
  185. BIT(done);
  186. } rr;
  187. #endif
  188. };
  189. void Curl_async_thrdd_shutdown(struct Curl_easy *data);
  190. void Curl_async_thrdd_destroy(struct Curl_easy *data);
  191. #endif /* CURLRES_THREADED */
  192. #ifndef CURL_DISABLE_DOH
  193. struct doh_probes;
  194. #endif
  195. #else /* CURLRES_ASYNCH */
  196. /* convert these functions if an asynch resolver is not used */
  197. #define Curl_async_get_impl(x,y) (*(y) = NULL, CURLE_OK)
  198. #define Curl_async_is_resolved(x,y) CURLE_COULDNT_RESOLVE_HOST
  199. #define Curl_async_await(x,y) CURLE_COULDNT_RESOLVE_HOST
  200. #define Curl_async_global_init() CURLE_OK
  201. #define Curl_async_global_cleanup() Curl_nop_stmt
  202. #endif /* !CURLRES_ASYNCH */
  203. #if defined(CURLRES_ASYNCH) || !defined(CURL_DISABLE_DOH)
  204. #define USE_CURL_ASYNC
  205. #endif
  206. #ifdef USE_CURL_ASYNC
  207. struct Curl_async {
  208. #ifdef CURLRES_ARES
  209. struct async_ares_ctx ares;
  210. #elif defined(CURLRES_THREADED)
  211. struct async_thrdd_ctx thrdd;
  212. #endif
  213. #ifndef CURL_DISABLE_DOH
  214. struct doh_probes *doh; /* DoH specific data for this request */
  215. #endif
  216. struct Curl_dns_entry *dns; /* result of resolving on success */
  217. char *hostname; /* copy of the params resolv started with */
  218. int port;
  219. int ip_version;
  220. BIT(done);
  221. };
  222. /*
  223. * Curl_async_shutdown().
  224. *
  225. * This shuts down all ongoing operations.
  226. */
  227. void Curl_async_shutdown(struct Curl_easy *data);
  228. /*
  229. * Curl_async_destroy().
  230. *
  231. * This frees the resources of any async resolve.
  232. */
  233. void Curl_async_destroy(struct Curl_easy *data);
  234. #else /* !USE_CURL_ASYNC */
  235. #define Curl_async_shutdown(x) Curl_nop_stmt
  236. #define Curl_async_destroy(x) Curl_nop_stmt
  237. #endif /* USE_CURL_ASYNC */
  238. /********** end of generic resolver interface functions *****************/
  239. #endif /* HEADER_CURL_ASYN_H */