asyn resolver code improvements

"asyn" is the internal name under which both c-ares and threaded
resolver operate. Make the naming more consistent. Implement the c-ares
resolver in `asyn-ares.*` and the threaded resolver in `asyn-thrdd.*`.
The common functions are in `asyn-base.c`.

When `CURLRES_ASYNCH` is defined, either of the two is used and
`data->state.async` exists. Members of that struct vary for the selected
implementation, but have the fields `hostname`, `port` and `ip_version`
always present. This are populated when the async resolving starts and
eliminate the need to pass them again when checking on the status and
processing the results of the resolving.

Add a `Curl_resolv_blocking()` to `hostip.h` that relieves FTP and SOCKS
from having to repeat the same code.

`Curl_resolv_check()` remains the function to check for status of
ongoing resolving. Now it also performs internally the check if the
needed DNS entry exists in the dnscache and if so, aborts the asnyc
operation. (libcurl right now does not check for duplicate resolve
attempts. an area for future improvements).

The number of functions in `asyn.h` has been reduced. There were subtle
difference in "cancel()" and "kill()" calls, both replaced by
`Curl_async_shutdown()` now. This changes behaviour for threaded
resolver insofar as the resolving thread is now always joined unless
`data->set.quick_exit` is set. Before this was only done on some code
paths. A future improvement would be a thread pool that keeps a limit
and also could handle joins more gracefully.

DoH, not previously tagged under "asny", has its struct `doh_probes` now
also in `data->state.async`, moved there from `data->req` because it
makes more sense. Further integration of DoH underneath the "asyn"
umbrella seems like a good idea.

Closes #16963
This commit is contained in:
Stefan Eissing 2025-04-11 14:43:45 +02:00 committed by Daniel Stenberg
parent be718daf99
commit 56e40ae6a5
No known key found for this signature in database
GPG key ID: 5CC908FDB71E12C2
30 changed files with 1902 additions and 2134 deletions

View file

@ -867,7 +867,6 @@ static CURLcode ftp_state_use_port(struct Curl_easy *data,
struct sockaddr_in6 * const sa6 = (void *)sa;
#endif
static const char mode[][5] = { "EPRT", "PORT" };
enum resolve_t rc;
int error;
char *host = NULL;
char *string_ftpport = data->set.str[STRING_FTPPORT];
@ -1014,14 +1013,12 @@ static CURLcode ftp_state_use_port(struct Curl_easy *data,
}
/* resolv ip/host to ip */
rc = Curl_resolv(data, host, 0, FALSE, &dns_entry);
if(rc == CURLRESOLV_PENDING)
(void)Curl_resolver_wait_resolv(data, &dns_entry);
if(dns_entry) {
res = NULL;
result = Curl_resolv_blocking(data, host, 0, conn->ip_version, &dns_entry);
if(!result) {
DEBUGASSERT(dns_entry);
res = dns_entry->addr;
}
else
res = NULL; /* failure! */
if(!res) {
failf(data, "failed to resolve the address provided to PORT: %s", host);
@ -1786,8 +1783,7 @@ static CURLcode ftp_state_pasv_resp(struct Curl_easy *data,
struct connectdata *conn = data->conn;
struct ftp_conn *ftpc = &conn->proto.ftpc;
CURLcode result;
struct Curl_dns_entry *addr = NULL;
enum resolve_t rc;
struct Curl_dns_entry *dns = NULL;
unsigned short connectport; /* the local port connect() should use! */
struct pingpong *pp = &ftpc->pp;
char *str =
@ -1885,16 +1881,12 @@ static CURLcode ftp_state_pasv_resp(struct Curl_easy *data,
*/
const char * const host_name = conn->bits.socksproxy ?
conn->socks_proxy.host.name : conn->http_proxy.host.name;
rc = Curl_resolv(data, host_name, conn->primary.remote_port, FALSE, &addr);
if(rc == CURLRESOLV_PENDING)
/* BLOCKING, ignores the return code but 'addr' will be NULL in
case of failure */
(void)Curl_resolver_wait_resolv(data, &addr);
(void)Curl_resolv_blocking(data, host_name, conn->primary.remote_port,
conn->ip_version, &dns);
/* we connect to the proxy's port */
connectport = (unsigned short)conn->primary.remote_port;
if(!addr) {
if(!dns) {
failf(data, "cannot resolve proxy host %s:%hu", host_name, connectport);
return CURLE_COULDNT_RESOLVE_PROXY;
}
@ -1913,26 +1905,23 @@ static CURLcode ftp_state_pasv_resp(struct Curl_easy *data,
return CURLE_OUT_OF_MEMORY;
}
rc = Curl_resolv(data, ftpc->newhost, ftpc->newport, FALSE, &addr);
if(rc == CURLRESOLV_PENDING)
/* BLOCKING */
(void)Curl_resolver_wait_resolv(data, &addr);
(void)Curl_resolv_blocking(data, ftpc->newhost, ftpc->newport,
conn->ip_version, &dns);
connectport = ftpc->newport; /* we connect to the remote port */
if(!addr) {
if(!dns) {
failf(data, "cannot resolve new host %s:%hu",
ftpc->newhost, connectport);
return CURLE_FTP_CANT_GET_HOST;
}
}
result = Curl_conn_setup(data, conn, SECONDARYSOCKET, addr,
result = Curl_conn_setup(data, conn, SECONDARYSOCKET, dns,
conn->bits.ftp_use_data_ssl ?
CURL_CF_SSL_ENABLE : CURL_CF_SSL_DISABLE);
if(result) {
Curl_resolv_unlink(data, &addr); /* we are done using this address */
Curl_resolv_unlink(data, &dns); /* we are done using this dns entry */
if(ftpc->count1 == 0 && ftpcode == 229)
return ftp_epsv_disable(data, conn);
@ -1948,9 +1937,9 @@ static CURLcode ftp_state_pasv_resp(struct Curl_easy *data,
if(data->set.verbose)
/* this just dumps information about this second connection */
ftp_pasv_verbose(data, addr->addr, ftpc->newhost, connectport);
ftp_pasv_verbose(data, dns->addr, ftpc->newhost, connectport);
Curl_resolv_unlink(data, &addr); /* we are done using this address */
Curl_resolv_unlink(data, &dns); /* we are done using this address */
free(conn->secondaryhostname);
conn->secondary_port = ftpc->newport;