asyn.h 8.5 KB

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