#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***************************************************************************/25#include "curl_setup.h"2627struct Curl_easy;28struct Curl_dns_entry;2930#ifdef CURLRES_ASYNCH3132#include "curl_addrinfo.h"33#include "httpsrr.h"3435struct addrinfo;36struct hostent;37struct connectdata;38struct easy_pollset;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_pollset()73*74* This function is called from the Curl_multi_pollset() 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*/80CURLcode Curl_async_pollset(struct Curl_easy *data, struct easy_pollset *ps);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*/119CURLcode Curl_async_getaddrinfo(struct Curl_easy *data, const char *hostname,120int port, int ip_version);121122#ifdef USE_ARES123/* common functions for c-ares and threaded resolver with HTTPSRR */124#include <ares.h>125126CURLcode Curl_ares_pollset(struct Curl_easy *data,127ares_channel channel,128struct easy_pollset *ps);129130int Curl_ares_perform(ares_channel channel, timediff_t timeout_ms);131#endif132133#ifdef CURLRES_ARES134/* async resolving implementation using c-ares alone */135struct async_ares_ctx {136ares_channel channel;137int num_pending; /* number of outstanding c-ares requests */138struct Curl_addrinfo *temp_ai; /* intermediary result while fetching c-ares139parts */140int ares_status; /* ARES_SUCCESS, ARES_ENOTFOUND, etc. */141CURLcode result; /* CURLE_OK or error handling response */142#ifndef HAVE_CARES_GETADDRINFO143struct curltime happy_eyeballs_dns_time; /* when this timer started, or 0 */144#endif145#ifdef USE_HTTPSRR146struct Curl_https_rrinfo hinfo;147#endif148};149150void Curl_async_ares_shutdown(struct Curl_easy *data);151void Curl_async_ares_destroy(struct Curl_easy *data);152153/* Set the DNS server to use by ares, from `data` settings. */154CURLcode Curl_async_ares_set_dns_servers(struct Curl_easy *data);155156/* Set the DNS interfacer to use by ares, from `data` settings. */157CURLcode Curl_async_ares_set_dns_interface(struct Curl_easy *data);158159/* Set the local ipv4 address to use by ares, from `data` settings. */160CURLcode Curl_async_ares_set_dns_local_ip4(struct Curl_easy *data);161162/* Set the local ipv6 address to use by ares, from `data` settings. */163CURLcode Curl_async_ares_set_dns_local_ip6(struct Curl_easy *data);164165#endif /* CURLRES_ARES */166167#ifdef CURLRES_THREADED168/* async resolving implementation using POSIX threads */169#include "curl_threads.h"170171/* Context for threaded address resolver */172struct async_thrdd_addr_ctx {173curl_thread_t thread_hnd;174char *hostname; /* hostname to resolve, Curl_async.hostname175duplicate */176curl_mutex_t mutx;177#ifndef CURL_DISABLE_SOCKETPAIR178curl_socket_t sock_pair[2]; /* eventfd/pipes/socket pair */179#endif180struct Curl_addrinfo *res;181#ifdef HAVE_GETADDRINFO182struct addrinfo hints;183#endif184struct curltime start;185timediff_t interval_end;186unsigned int poll_interval;187int port;188int sock_error;189int ref_count;190BIT(thrd_done);191BIT(do_abort);192};193194/* Context for threaded resolver */195struct async_thrdd_ctx {196/* `addr` is a pointer since this memory is shared with a started197* thread. Since threads cannot be killed, we use reference counting198* so that we can "release" our pointer to this memory while the199* thread is still running. */200struct async_thrdd_addr_ctx *addr;201#if defined(USE_HTTPSRR) && defined(USE_ARES)202struct {203ares_channel channel;204struct Curl_https_rrinfo hinfo;205CURLcode result;206BIT(done);207} rr;208#endif209};210211void Curl_async_thrdd_shutdown(struct Curl_easy *data);212void Curl_async_thrdd_destroy(struct Curl_easy *data);213214#endif /* CURLRES_THREADED */215216#ifndef CURL_DISABLE_DOH217struct doh_probes;218#endif219220#else /* CURLRES_ASYNCH */221222/* convert these functions if an asynch resolver is not used */223#define Curl_async_get_impl(x, y) (*(y) = NULL, CURLE_OK)224#define Curl_async_is_resolved(x, y) CURLE_COULDNT_RESOLVE_HOST225#define Curl_async_await(x, y) CURLE_COULDNT_RESOLVE_HOST226#define Curl_async_global_init() CURLE_OK227#define Curl_async_global_cleanup() Curl_nop_stmt228229#endif /* !CURLRES_ASYNCH */230231#if defined(CURLRES_ASYNCH) || !defined(CURL_DISABLE_DOH)232#define USE_CURL_ASYNC233#endif234235#ifdef USE_CURL_ASYNC236struct Curl_async {237#ifdef CURLRES_ARES238struct async_ares_ctx ares;239#elif defined(CURLRES_THREADED)240struct async_thrdd_ctx thrdd;241#endif242#ifndef CURL_DISABLE_DOH243struct doh_probes *doh; /* DoH specific data for this request */244#endif245struct Curl_dns_entry *dns; /* result of resolving on success */246char *hostname; /* copy of the params resolv started with */247int port;248int ip_version;249BIT(done);250};251252/*253* Curl_async_shutdown().254*255* This shuts down all ongoing operations.256*/257void Curl_async_shutdown(struct Curl_easy *data);258259/*260* Curl_async_destroy().261*262* This frees the resources of any async resolve.263*/264void Curl_async_destroy(struct Curl_easy *data);265#else /* !USE_CURL_ASYNC */266#define Curl_async_shutdown(x) Curl_nop_stmt267#define Curl_async_destroy(x) Curl_nop_stmt268#endif /* USE_CURL_ASYNC */269270/********** end of generic resolver interface functions *****************/271#endif /* HEADER_CURL_ASYN_H */272273274