#ifndef HEADER_CURL_ASYN_H1#define HEADER_CURL_ASYN_H2/***************************************************************************3* _ _ ____ _4* Project ___| | | | _ \| |5* / __| | | | |_) | |6* | (__| |_| | _ <| |___7* \___|\___/|_| \_\_____|8*9* Copyright (C) Daniel Stenberg, <[email protected]>, et al.10*11* This software is licensed as described in the file COPYING, which12* you should have received as part of this distribution. The terms13* are also available at https://curl.se/docs/copyright.html.14*15* You may opt to use, copy, modify, merge, publish, distribute and/or sell16* copies of the Software, and permit persons to whom the Software is17* furnished to do so, under the terms of the COPYING file.18*19* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY20* KIND, either express or implied.21*22* SPDX-License-Identifier: curl23*24***************************************************************************/2526#include "curl_setup.h"2728struct Curl_easy;29struct Curl_dns_entry;3031#ifdef CURLRES_ASYNCH3233#include "curl_addrinfo.h"34#include "httpsrr.h"3536struct addrinfo;37struct hostent;38struct connectdata;3940#if defined(CURLRES_ARES) && defined(CURLRES_THREADED)41#error cannot have both CURLRES_ARES and CURLRES_THREADED defined42#endif4344/*45* This header defines all functions in the internal asynch resolver interface.46* All asynch resolvers need to provide these functions.47* asyn-ares.c and asyn-thread.c are the current implementations of asynch48* resolver backends.49*/5051/*52* Curl_async_global_init()53*54* Called from curl_global_init() to initialize global resolver environment.55* Returning anything else than CURLE_OK fails curl_global_init().56*/57int Curl_async_global_init(void);5859/*60* Curl_async_global_cleanup()61* Called from curl_global_cleanup() to destroy global resolver environment.62*/63void Curl_async_global_cleanup(void);6465/*66* Curl_async_get_impl()67* Get the resolver implementation instance (c-ares channel) or NULL68* for passing to application callback.69*/70CURLcode Curl_async_get_impl(struct Curl_easy *easy, void **impl);7172/* Curl_async_getsock()73*74* This function is called from the Curl_multi_getsock() function. 'sock' is a75* pointer to an array to hold the file descriptors, with 'numsock' being the76* size of that array (in number of entries). This function is supposed to77* return bitmask indicating what file descriptors (referring to array indexes78* in the 'sock' array) to wait for, read/write.79*/80int Curl_async_getsock(struct Curl_easy *data, curl_socket_t *sock);8182/*83* Curl_async_is_resolved()84*85* Called repeatedly to check if a previous name resolve request has86* completed. It should also make sure to time-out if the operation seems to87* take too long.88*89* Returns normal CURLcode errors.90*/91CURLcode Curl_async_is_resolved(struct Curl_easy *data,92struct Curl_dns_entry **dns);9394/*95* Curl_async_await()96*97* Waits for a resolve to finish. This function should be avoided since using98* this risk getting the multi interface to "hang".99*100* On return 'entry' is assigned the resolved dns (CURLE_OK or NULL otherwise.101*102* Returns CURLE_COULDNT_RESOLVE_HOST if the host was not resolved,103* CURLE_OPERATION_TIMEDOUT if a time-out occurred, or other errors.104*/105CURLcode Curl_async_await(struct Curl_easy *data,106struct Curl_dns_entry **dnsentry);107108/*109* Curl_async_getaddrinfo() - when using this resolver110*111* Returns name information about the given hostname and port number. If112* successful, the 'hostent' is returned and the fourth argument will point to113* memory we need to free after use. That memory *MUST* be freed with114* Curl_freeaddrinfo(), nothing else.115*116* Each resolver backend must of course make sure to return data in the117* correct format to comply with this.118*/119struct Curl_addrinfo *Curl_async_getaddrinfo(struct Curl_easy *data,120const char *hostname,121int port,122int ip_version,123int *waitp);124125#ifdef USE_ARES126/* common functions for c-ares and threaded resolver with HTTPSRR */127#include <ares.h>128129int Curl_ares_getsock(struct Curl_easy *data,130ares_channel channel,131curl_socket_t *socks);132int Curl_ares_perform(ares_channel channel,133timediff_t timeout_ms);134#endif135136#ifdef CURLRES_ARES137/* async resolving implementation using c-ares alone */138struct async_ares_ctx {139ares_channel channel;140int num_pending; /* number of outstanding c-ares requests */141struct Curl_addrinfo *temp_ai; /* intermediary result while fetching c-ares142parts */143int last_status;144CURLcode result; /* CURLE_OK or error handling response */145#ifndef HAVE_CARES_GETADDRINFO146struct curltime happy_eyeballs_dns_time; /* when this timer started, or 0 */147#endif148#ifdef USE_HTTPSRR149struct Curl_https_rrinfo hinfo;150#endif151};152153void Curl_async_ares_shutdown(struct Curl_easy *data);154void Curl_async_ares_destroy(struct Curl_easy *data);155156/* Set the DNS server to use by ares, from `data` settings. */157CURLcode Curl_async_ares_set_dns_servers(struct Curl_easy *data);158159/* Set the DNS interfacer to use by ares, from `data` settings. */160CURLcode Curl_async_ares_set_dns_interface(struct Curl_easy *data);161162/* Set the local ipv4 address to use by ares, from `data` settings. */163CURLcode Curl_async_ares_set_dns_local_ip4(struct Curl_easy *data);164165/* Set the local ipv6 address to use by ares, from `data` settings. */166CURLcode Curl_async_ares_set_dns_local_ip6(struct Curl_easy *data);167168#endif /* CURLRES_ARES */169170#ifdef CURLRES_THREADED171/* async resolving implementation using POSIX threads */172#include "curl_threads.h"173174/* Context for threaded address resolver */175struct async_thrdd_addr_ctx {176curl_thread_t thread_hnd;177char *hostname; /* hostname to resolve, Curl_async.hostname178duplicate */179curl_mutex_t mutx;180#ifndef CURL_DISABLE_SOCKETPAIR181curl_socket_t sock_pair[2]; /* eventfd/pipes/socket pair */182#endif183struct Curl_addrinfo *res;184#ifdef HAVE_GETADDRINFO185struct addrinfo hints;186#endif187struct curltime start;188timediff_t interval_end;189unsigned int poll_interval;190int port;191int sock_error;192int ref_count;193};194195/* Context for threaded resolver */196struct async_thrdd_ctx {197/* `addr` is a pointer since this memory is shared with a started198* thread. Since threads cannot be killed, we use reference counting199* so that we can "release" our pointer to this memory while the200* thread is still running. */201struct async_thrdd_addr_ctx *addr;202#if defined(USE_HTTPSRR) && defined(USE_ARES)203struct {204ares_channel channel;205struct Curl_https_rrinfo hinfo;206CURLcode result;207BIT(done);208} rr;209#endif210};211212void Curl_async_thrdd_shutdown(struct Curl_easy *data);213void Curl_async_thrdd_destroy(struct Curl_easy *data);214215#endif /* CURLRES_THREADED */216217#ifndef CURL_DISABLE_DOH218struct doh_probes;219#endif220221#else /* CURLRES_ASYNCH */222223/* convert these functions if an asynch resolver is not used */224#define Curl_async_get_impl(x,y) (*(y) = NULL, CURLE_OK)225#define Curl_async_is_resolved(x,y) CURLE_COULDNT_RESOLVE_HOST226#define Curl_async_await(x,y) CURLE_COULDNT_RESOLVE_HOST227#define Curl_async_global_init() CURLE_OK228#define Curl_async_global_cleanup() Curl_nop_stmt229230#endif /* !CURLRES_ASYNCH */231232#if defined(CURLRES_ASYNCH) || !defined(CURL_DISABLE_DOH)233#define USE_CURL_ASYNC234#endif235236#ifdef USE_CURL_ASYNC237struct Curl_async {238#ifdef CURLRES_ARES /* */239struct async_ares_ctx ares;240#elif defined(CURLRES_THREADED)241struct async_thrdd_ctx thrdd;242#endif243#ifndef CURL_DISABLE_DOH244struct doh_probes *doh; /* DoH specific data for this request */245#endif246struct Curl_dns_entry *dns; /* result of resolving on success */247char *hostname; /* copy of the params resolv started with */248int port;249int ip_version;250BIT(done);251};252253/*254* Curl_async_shutdown().255*256* This shuts down all ongoing operations.257*/258void Curl_async_shutdown(struct Curl_easy *data);259260/*261* Curl_async_destroy().262*263* This frees the resources of any async resolve.264*/265void Curl_async_destroy(struct Curl_easy *data);266#else /* !USE_CURL_ASYNC */267#define Curl_async_shutdown(x) Curl_nop_stmt268#define Curl_async_destroy(x) Curl_nop_stmt269#endif /* USE_CURL_ASYNC */270271272/********** end of generic resolver interface functions *****************/273#endif /* HEADER_CURL_ASYN_H */274275276