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

@ -321,16 +321,16 @@ static CURLproxycode do_SOCKS4(struct Curl_cfilter *cf,
/* DNS resolve only for SOCKS4, not SOCKS4a */
if(!protocol4a) {
enum resolve_t rc =
Curl_resolv(data, sx->hostname, sx->remote_port, TRUE, &dns);
result = Curl_resolv(data, sx->hostname, sx->remote_port,
cf->conn->ip_version, TRUE, &dns);
if(rc == CURLRESOLV_ERROR)
return CURLPX_RESOLVE_HOST;
else if(rc == CURLRESOLV_PENDING) {
if(result == CURLE_AGAIN) {
sxstate(sx, data, CONNECT_RESOLVING);
infof(data, "SOCKS4 non-blocking resolve of %s", sx->hostname);
return CURLPX_OK;
}
else if(result)
return CURLPX_RESOLVE_HOST;
sxstate(sx, data, CONNECT_RESOLVED);
goto CONNECT_RESOLVED;
}
@ -341,21 +341,11 @@ static CURLproxycode do_SOCKS4(struct Curl_cfilter *cf,
case CONNECT_RESOLVING:
/* check if we have the name resolved by now */
dns = Curl_fetch_addr(data, sx->hostname, conn->primary.remote_port);
if(dns) {
/* Tell a possibly async resolver we no longer need the results. */
Curl_resolver_set_result(data, dns);
infof(data, "Hostname '%s' was found", sx->hostname);
sxstate(sx, data, CONNECT_RESOLVED);
}
else {
result = Curl_resolv_check(data, &dns);
if(!dns) {
if(result)
return CURLPX_RESOLVE_HOST;
return CURLPX_OK;
}
result = Curl_resolv_check(data, &dns);
if(!dns) {
if(result)
return CURLPX_RESOLVE_HOST;
return CURLPX_OK;
}
FALLTHROUGH();
case CONNECT_RESOLVED:
@ -792,16 +782,15 @@ CONNECT_AUTH_INIT:
case CONNECT_REQ_INIT:
CONNECT_REQ_INIT:
if(socks5_resolve_local) {
enum resolve_t rc = Curl_resolv(data, sx->hostname, sx->remote_port,
TRUE, &dns);
result = Curl_resolv(data, sx->hostname, sx->remote_port,
cf->conn->ip_version, TRUE, &dns);
if(rc == CURLRESOLV_ERROR)
return CURLPX_RESOLVE_HOST;
if(rc == CURLRESOLV_PENDING) {
if(result == CURLE_AGAIN) {
sxstate(sx, data, CONNECT_RESOLVING);
return CURLPX_OK;
}
else if(result)
return CURLPX_RESOLVE_HOST;
sxstate(sx, data, CONNECT_RESOLVED);
goto CONNECT_RESOLVED;
}
@ -809,21 +798,11 @@ CONNECT_REQ_INIT:
case CONNECT_RESOLVING:
/* check if we have the name resolved by now */
dns = Curl_fetch_addr(data, sx->hostname, sx->remote_port);
if(dns) {
/* Tell a possibly async resolver we no longer need the results. */
Curl_resolver_set_result(data, dns);
infof(data, "SOCKS5: hostname '%s' found", sx->hostname);
}
result = Curl_resolv_check(data, &dns);
if(!dns) {
result = Curl_resolv_check(data, &dns);
if(!dns) {
if(result)
return CURLPX_RESOLVE_HOST;
return CURLPX_OK;
}
if(result)
return CURLPX_RESOLVE_HOST;
return CURLPX_OK;
}
FALLTHROUGH();
case CONNECT_RESOLVED: