#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;39struct easy_pollset;4041#if defined(CURLRES_ARES) && defined(CURLRES_THREADED)42#error cannot have both CURLRES_ARES and CURLRES_THREADED defined43#endif4445/*46* This header defines all functions in the internal asynch resolver interface.47* All asynch resolvers need to provide these functions.48* asyn-ares.c and asyn-thread.c are the current implementations of asynch49* resolver backends.50*/5152/*53* Curl_async_global_init()54*55* Called from curl_global_init() to initialize global resolver environment.56* Returning anything else than CURLE_OK fails curl_global_init().57*/58int Curl_async_global_init(void);5960/*61* Curl_async_global_cleanup()62* Called from curl_global_cleanup() to destroy global resolver environment.63*/64void Curl_async_global_cleanup(void);6566/*67* Curl_async_get_impl()68* Get the resolver implementation instance (c-ares channel) or NULL69* for passing to application callback.70*/71CURLcode Curl_async_get_impl(struct Curl_easy *easy, void **impl);7273/* Curl_async_pollset()74*75* This function is called from the Curl_multi_pollset() function. 'sock' is a76* pointer to an array to hold the file descriptors, with 'numsock' being the77* size of that array (in number of entries). This function is supposed to78* return bitmask indicating what file descriptors (referring to array indexes79* in the 'sock' array) to wait for, read/write.80*/81CURLcode Curl_async_pollset(struct Curl_easy *data, struct easy_pollset *ps);8283/*84* Curl_async_is_resolved()85*86* Called repeatedly to check if a previous name resolve request has87* completed. It should also make sure to time-out if the operation seems to88* take too long.89*90* Returns normal CURLcode errors.91*/92CURLcode Curl_async_is_resolved(struct Curl_easy *data,93struct Curl_dns_entry **dns);9495/*96* Curl_async_await()97*98* Waits for a resolve to finish. This function should be avoided since using99* this risk getting the multi interface to "hang".100*101* On return 'entry' is assigned the resolved dns (CURLE_OK or NULL otherwise.102*103* Returns CURLE_COULDNT_RESOLVE_HOST if the host was not resolved,104* CURLE_OPERATION_TIMEDOUT if a time-out occurred, or other errors.105*/106CURLcode Curl_async_await(struct Curl_easy *data,107struct Curl_dns_entry **dnsentry);108109/*110* Curl_async_getaddrinfo() - when using this resolver111*112* Returns name information about the given hostname and port number. If113* successful, the 'hostent' is returned and the fourth argument will point to114* memory we need to free after use. That memory *MUST* be freed with115* Curl_freeaddrinfo(), nothing else.116*117* Each resolver backend must of course make sure to return data in the118* correct format to comply with this.119*/120struct Curl_addrinfo *Curl_async_getaddrinfo(struct Curl_easy *data,121const char *hostname,122int port,123int ip_version,124int *waitp);125126#ifdef USE_ARES127/* common functions for c-ares and threaded resolver with HTTPSRR */128#include <ares.h>129130CURLcode Curl_ares_pollset(struct Curl_easy *data,131ares_channel channel,132struct easy_pollset *ps);133134int Curl_ares_perform(ares_channel channel,135timediff_t timeout_ms);136#endif137138#ifdef CURLRES_ARES139/* async resolving implementation using c-ares alone */140struct async_ares_ctx {141ares_channel channel;142int num_pending; /* number of outstanding c-ares requests */143struct Curl_addrinfo *temp_ai; /* intermediary result while fetching c-ares144parts */145int ares_status; /* ARES_SUCCESS, ARES_ENOTFOUND, etc. */146CURLcode result; /* CURLE_OK or error handling response */147#ifndef HAVE_CARES_GETADDRINFO148struct curltime happy_eyeballs_dns_time; /* when this timer started, or 0 */149#endif150#ifdef USE_HTTPSRR151struct Curl_https_rrinfo hinfo;152#endif153};154155void Curl_async_ares_shutdown(struct Curl_easy *data);156void Curl_async_ares_destroy(struct Curl_easy *data);157158/* Set the DNS server to use by ares, from `data` settings. */159CURLcode Curl_async_ares_set_dns_servers(struct Curl_easy *data);160161/* Set the DNS interfacer to use by ares, from `data` settings. */162CURLcode Curl_async_ares_set_dns_interface(struct Curl_easy *data);163164/* Set the local ipv4 address to use by ares, from `data` settings. */165CURLcode Curl_async_ares_set_dns_local_ip4(struct Curl_easy *data);166167/* Set the local ipv6 address to use by ares, from `data` settings. */168CURLcode Curl_async_ares_set_dns_local_ip6(struct Curl_easy *data);169170#endif /* CURLRES_ARES */171172#ifdef CURLRES_THREADED173/* async resolving implementation using POSIX threads */174#include "curl_threads.h"175176/* Context for threaded address resolver */177struct async_thrdd_addr_ctx {178curl_thread_t thread_hnd;179char *hostname; /* hostname to resolve, Curl_async.hostname180duplicate */181curl_mutex_t mutx;182#ifndef CURL_DISABLE_SOCKETPAIR183curl_socket_t sock_pair[2]; /* eventfd/pipes/socket pair */184#endif185struct Curl_addrinfo *res;186#ifdef HAVE_GETADDRINFO187struct addrinfo hints;188#endif189struct curltime start;190timediff_t interval_end;191unsigned int poll_interval;192int port;193int sock_error;194int ref_count;195BIT(thrd_done);196BIT(do_abort);197};198199/* Context for threaded resolver */200struct async_thrdd_ctx {201/* `addr` is a pointer since this memory is shared with a started202* thread. Since threads cannot be killed, we use reference counting203* so that we can "release" our pointer to this memory while the204* thread is still running. */205struct async_thrdd_addr_ctx *addr;206#if defined(USE_HTTPSRR) && defined(USE_ARES)207struct {208ares_channel channel;209struct Curl_https_rrinfo hinfo;210CURLcode result;211BIT(done);212} rr;213#endif214};215216void Curl_async_thrdd_shutdown(struct Curl_easy *data);217void Curl_async_thrdd_destroy(struct Curl_easy *data);218219#endif /* CURLRES_THREADED */220221#ifndef CURL_DISABLE_DOH222struct doh_probes;223#endif224225#else /* CURLRES_ASYNCH */226227/* convert these functions if an asynch resolver is not used */228#define Curl_async_get_impl(x,y) (*(y) = NULL, CURLE_OK)229#define Curl_async_is_resolved(x,y) CURLE_COULDNT_RESOLVE_HOST230#define Curl_async_await(x,y) CURLE_COULDNT_RESOLVE_HOST231#define Curl_async_global_init() CURLE_OK232#define Curl_async_global_cleanup() Curl_nop_stmt233234#endif /* !CURLRES_ASYNCH */235236#if defined(CURLRES_ASYNCH) || !defined(CURL_DISABLE_DOH)237#define USE_CURL_ASYNC238#endif239240#ifdef USE_CURL_ASYNC241struct Curl_async {242#ifdef CURLRES_ARES243struct async_ares_ctx ares;244#elif defined(CURLRES_THREADED)245struct async_thrdd_ctx thrdd;246#endif247#ifndef CURL_DISABLE_DOH248struct doh_probes *doh; /* DoH specific data for this request */249#endif250struct Curl_dns_entry *dns; /* result of resolving on success */251char *hostname; /* copy of the params resolv started with */252int port;253int ip_version;254BIT(done);255};256257/*258* Curl_async_shutdown().259*260* This shuts down all ongoing operations.261*/262void Curl_async_shutdown(struct Curl_easy *data);263264/*265* Curl_async_destroy().266*267* This frees the resources of any async resolve.268*/269void Curl_async_destroy(struct Curl_easy *data);270#else /* !USE_CURL_ASYNC */271#define Curl_async_shutdown(x) Curl_nop_stmt272#define Curl_async_destroy(x) Curl_nop_stmt273#endif /* USE_CURL_ASYNC */274275276/********** end of generic resolver interface functions *****************/277#endif /* HEADER_CURL_ASYN_H */278279280