fix: Windows toolchain compatibility (curl 8.21 re-vendor, endless reconfigure loop) (#4355)

## Problem

Windows builds break with a current local toolchain (Scoop LLVM 22.1.8,
CMake 4.4.0, VS 2026), in two independent ways:

1. The build stops at curl's deliberate guard: `#error "no non-blocking
method was found/used/set"` in `third-party/curl/lib/nonblock.c`.
2. From the second configure onward, `cmake --build` re-runs CMake in an
endless loop (observed 42 consecutive reconfigure cycles in a single
build). Likely the same mechanism behind the "endlessly building" VS
2026 note in `docs/setup/dev/vs.md`.

## Root cause

1. `third-party/curl/CMake/CurlTests.c` passes `int *` to
`ioctlsocket()`, whose third parameter is `u_long *`. Clang 22 promotes
`-Wincompatible-pointer-types` to a hard error in C, so the
`HAVE_IOCTLSOCKET_FIONBIO` try_compile silently fails and
`curl_config.h` never defines it. Upstream CI does not see this because
the windows-2022 runner image ships an older LLVM. GCC 14 promotes the
same warning to a hard error, which is very likely the `CurlTests.c.obj`
failure reported from MSYS2 in open-goal/jak-project#3551. Upstream curl
hit the identical problem with GCC 14 and fixed the probe in curl 8.8.0
(curl/curl#13578).
2. The root CMakeLists copies the build tree's `compile_commands.json`
into `<src>/build/` for clangd using `configure_file()`, which registers
its input as a configure dependency. CMake rewrites
`compile_commands.json` late in every generation, after
`CTestTestfile.cmake` and `cmake_install.cmake` (outputs of the same
Ninja regen rule), so once the dependency is registered the rule is
deterministically dirty and every `ninja` invocation re-runs CMake. A
pristine first configure is safe (the file does not exist yet, so the
`if(EXISTS ...)` guard skips the copy), which is why the loop looks
machine- or IDE-specific.

## Fix

1. Per review, re-vendor `third-party/curl` at the `curl-8_21_0` tag
(previously `curl-8_3_0`), which carries the upstream probe fix plus two
years of upstream development; `vendor.yaml` updated to match.
Adjustments the version jump forced:
- curl 8.15 removed the native macOS Secure Transport backend
(`CURL_USE_SECTRANSP`), so macOS now builds curl against OpenSSL like
Linux. The two macOS workflows install Homebrew `openssl@3` and export
`OPENSSL_ROOT_DIR` (keg-only), and the macOS setup docs gained the same
two lines.
- `CURL_BROTLI` / `CURL_ZSTD` switched to AUTO-detection in curl 8.10;
pinned OFF to keep the previous no-compression behavior and avoid
silently linking whatever the CI images happen to have.
- curl's new top-level `BUILD_EXAMPLES` cache option (default ON) leaked
into discord-rpc's identically named option and broke configure at a
nonexistent `examples/send-presence` directory; pinned OFF ahead of the
third-party subdirectories.

The diff is dominated by the mechanical tag-tree swap under
`third-party/curl` (linguist-vendored, collapsed in review). The
hand-written changes are `CMakeLists.txt`, the two macOS workflows,
`docs/setup/system/macos.md`, and `vendor.yaml`.
2. Swap `configure_file()` for `file(COPY ...)`: the same clangd copy
with no configure dependency registered. (`file(COPY_FILE ...
ONLY_IF_DIFFERENT)` would be cleaner still but requires CMake 3.21,
above the declared `cmake_minimum_required(VERSION 3.10)`.)

## Test plan

- [x] Fresh `cmake --preset Release-windows-clang` (LLVM 22, no cache
seeding) completes and logs `Enabled SSL backends: Schannel`; the
FIONBIO probe passes without the previous `#error`
- [x] Full Windows Release build from scratch in the branch worktree
(all 1422 targets)
- [x] goalc-test suite: 1509 passed, 0 failed
- [x] Second consecutive configure with `compile_commands.json` present:
the regen rule in `build.ninja` has no `compile_commands.json` input;
`<src>/build/compile_commands.json` is still refreshed for clangd
- [x] Repeated `ninja` invocations after a full build no longer re-run
CMake
- [x] macOS Intel and ARM CI green (first exercise of the OpenSSL
backend switch)

---

I work off a self-hosted forge, so this GitHub account is quiet; the
configure logs and ninja dirty-node traces from the investigation are
available if anyone wants the raw data.

(AI-assisted)
This commit is contained in:
Alexander J. Semenuk
2026-07-27 19:19:18 -04:00
committed by GitHub
parent 888b300487
commit 5d5e35fb9b
5480 changed files with 345923 additions and 288294 deletions
+515 -236
View File
@@ -21,28 +21,27 @@
* SPDX-License-Identifier: curl
*
***************************************************************************/
#include "curl_setup.h"
#if !defined(CURL_DISABLE_HTTP) && !defined(USE_HYPER)
#ifndef CURL_DISABLE_HTTP
#include "urldata.h"
#include <curl/curl.h>
#include "curl_trc.h"
#include "cfilters.h"
#include "cf-dns.h"
#include "cf-setup.h"
#include "connect.h"
#include "hostip.h"
#include "httpsrr.h"
#include "multiif.h"
#include "cf-https-connect.h"
#include "http2.h"
#include "progress.h"
#include "select.h"
#include "vquic/vquic.h"
/* The last 3 #include files should be in this order */
#include "curl_printf.h"
#include "curl_memory.h"
#include "memdebug.h"
typedef enum {
CF_HC_RESOLV,
CF_HC_INIT,
CF_HC_CONNECT,
CF_HC_SUCCESS,
@@ -55,24 +54,23 @@ struct cf_hc_baller {
CURLcode result;
struct curltime started;
int reply_ms;
bool enabled;
uint8_t transport;
enum alpnid alpn_id;
BIT(shutdown);
};
static void cf_hc_baller_reset(struct cf_hc_baller *b,
struct Curl_easy *data)
static void cf_hc_baller_discard(struct cf_hc_baller *b,
struct Curl_easy *data)
{
if(b->cf) {
Curl_conn_cf_close(b->cf, data);
Curl_conn_cf_discard_chain(&b->cf, data);
b->cf = NULL;
}
b->result = CURLE_OK;
b->reply_ms = -1;
}
static bool cf_hc_baller_is_active(struct cf_hc_baller *b)
static bool cf_hc_baller_is_connecting(struct cf_hc_baller *b)
{
return b->enabled && b->cf && !b->result;
return b->cf && !b->result;
}
static bool cf_hc_baller_has_started(struct cf_hc_baller *b)
@@ -83,7 +81,7 @@ static bool cf_hc_baller_has_started(struct cf_hc_baller *b)
static int cf_hc_baller_reply_ms(struct cf_hc_baller *b,
struct Curl_easy *data)
{
if(b->reply_ms < 0)
if(b->cf && (b->reply_ms < 0))
b->cf->cft->query(b->cf, data, CF_QUERY_CONNECT_REPLY_MS,
&b->reply_ms, NULL);
return b->reply_ms;
@@ -95,31 +93,95 @@ static bool cf_hc_baller_data_pending(struct cf_hc_baller *b,
return b->cf && !b->result && b->cf->cft->has_data_pending(b->cf, data);
}
static bool cf_hc_baller_needs_flush(struct cf_hc_baller *b,
struct Curl_easy *data)
{
return b->cf && !b->result && Curl_conn_cf_needs_flush(b->cf, data);
}
static CURLcode cf_hc_baller_cntrl(struct cf_hc_baller *b,
struct Curl_easy *data,
int event, int arg1, void *arg2)
{
if(b->cf && !b->result)
return Curl_conn_cf_cntrl(b->cf, data, FALSE, event, arg1, arg2);
return CURLE_OK;
}
struct cf_hc_ctx {
cf_hc_state state;
const struct Curl_dns_entry *remotehost;
struct curltime started; /* when connect started */
CURLcode result; /* overall result */
struct cf_hc_baller h3_baller;
struct cf_hc_baller h21_baller;
int soft_eyeballs_timeout_ms;
int hard_eyeballs_timeout_ms;
CURLcode check_h3_result;
struct cf_hc_baller ballers[2];
size_t baller_count;
timediff_t soft_eyeballs_timeout_ms;
timediff_t hard_eyeballs_timeout_ms;
uint8_t def_transport;
BIT(httpsrr_resolved);
BIT(checked_h3);
BIT(ballers_complete);
};
static void cf_hc_ctx_close(struct Curl_easy *data,
struct cf_hc_ctx *ctx)
{
if(ctx) {
size_t i;
for(i = 0; i < ctx->baller_count; ++i)
cf_hc_baller_discard(&ctx->ballers[i], data);
}
}
static void cf_hc_ctx_destroy(struct Curl_easy *data,
struct cf_hc_ctx *ctx)
{
if(ctx) {
cf_hc_ctx_close(data, ctx);
curlx_free(ctx);
}
}
static void cf_hc_baller_assign(struct cf_hc_baller *b,
enum alpnid alpn_id,
uint8_t def_transport)
{
b->alpn_id = alpn_id;
b->transport = def_transport;
b->cf = NULL;
b->result = CURLE_OK;
b->reply_ms = -1;
b->shutdown = FALSE;
switch(b->alpn_id) {
case ALPN_h3:
b->name = "h3";
b->transport = TRNSPRT_QUIC;
break;
case ALPN_h2:
b->name = "h2";
break;
case ALPN_h1:
b->name = "h1";
break;
case ALPN_none:
b->name = "no-alpn";
break;
default:
b->result = CURLE_FAILED_INIT;
break;
}
}
static void cf_hc_baller_init(struct cf_hc_baller *b,
struct Curl_cfilter *cf,
struct Curl_easy *data,
const char *name,
int transport)
struct Curl_easy *data)
{
struct cf_hc_ctx *ctx = cf->ctx;
struct Curl_cfilter *save = cf->next;
b->name = name;
cf->next = NULL;
b->started = Curl_now();
b->result = Curl_cf_setup_insert_after(cf, data, ctx->remotehost,
transport, CURL_CF_SSL_ENABLE);
b->started = *Curl_pgrs_now(data);
b->result = Curl_cf_setup_insert_after(cf, data, b->transport,
CURL_CF_SSL_ENABLE);
b->cf = cf->next;
cf->next = save;
}
@@ -132,174 +194,369 @@ static CURLcode cf_hc_baller_connect(struct cf_hc_baller *b,
struct Curl_cfilter *save = cf->next;
cf->next = b->cf;
b->result = Curl_conn_cf_connect(cf->next, data, FALSE, done);
b->result = Curl_conn_cf_connect(cf->next, data, done);
b->cf = cf->next; /* it might mutate */
cf->next = save;
return b->result;
}
static void cf_hc_reset(struct Curl_cfilter *cf, struct Curl_easy *data)
{
struct cf_hc_ctx *ctx = cf->ctx;
if(ctx) {
cf_hc_baller_reset(&ctx->h3_baller, data);
cf_hc_baller_reset(&ctx->h21_baller, data);
ctx->state = CF_HC_INIT;
ctx->result = CURLE_OK;
ctx->hard_eyeballs_timeout_ms = data->set.happy_eyeballs_timeout;
ctx->soft_eyeballs_timeout_ms = data->set.happy_eyeballs_timeout / 2;
}
}
static CURLcode baller_connected(struct Curl_cfilter *cf,
struct Curl_easy *data,
struct cf_hc_baller *winner)
{
struct cf_hc_ctx *ctx = cf->ctx;
CURLcode result = CURLE_OK;
DEBUGASSERT(winner->cf);
if(winner != &ctx->h3_baller)
cf_hc_baller_reset(&ctx->h3_baller, data);
if(winner != &ctx->h21_baller)
cf_hc_baller_reset(&ctx->h21_baller, data);
/* Make the winner's connection filter out own sub-filter, check, move,
* close all remaining. */
if(cf->next) {
DEBUGASSERT(0);
return CURLE_FAILED_INIT;
}
if(!winner->cf) {
DEBUGASSERT(0);
return CURLE_FAILED_INIT;
}
CURL_TRC_CF(data, cf, "connect+handshake %s: %dms, 1st data: %dms",
winner->name, (int)Curl_timediff(Curl_now(), winner->started),
cf_hc_baller_reply_ms(winner, data));
cf->next = winner->cf;
winner->cf = NULL;
switch(cf->conn->alpn) {
case CURL_HTTP_VERSION_3:
infof(data, "using HTTP/3");
break;
case CURL_HTTP_VERSION_2:
#ifdef USE_NGHTTP2
/* Using nghttp2, we add the filter "below" us, so when the conn
* closes, we tear it down for a fresh reconnect */
result = Curl_http2_switch_at(cf, data);
if(result) {
ctx->state = CF_HC_FAILURE;
ctx->result = result;
return result;
}
#endif
infof(data, "using HTTP/2");
break;
case CURL_HTTP_VERSION_1_1:
infof(data, "using HTTP/1.1");
break;
default:
infof(data, "using HTTP/1.x");
break;
}
ctx->state = CF_HC_SUCCESS;
cf->connected = TRUE;
Curl_conn_cf_cntrl(cf->next, data, TRUE,
CF_CTRL_CONN_INFO_UPDATE, 0, NULL);
return result;
cf_hc_ctx_close(data, ctx);
/* ballers may have failf()'d, the winner resets it, so our
* errorbuf is clean again. */
Curl_reset_fail(data);
#ifdef USE_NGHTTP2
{
/* For a negotiated HTTP/2 connection insert the h2 filter. */
const char *alpn = Curl_conn_cf_get_alpn_negotiated(cf->next, data);
if(alpn && !strcmp("h2", alpn)) {
CURLcode result = Curl_http2_switch_at(cf, data);
if(result) {
ctx->state = CF_HC_FAILURE;
ctx->result = result;
return result;
}
}
}
#endif
return CURLE_OK;
}
static bool time_to_start_h21(struct Curl_cfilter *cf,
struct Curl_easy *data,
struct curltime now)
static bool time_to_start_baller2(struct Curl_cfilter *cf,
struct Curl_easy *data)
{
struct cf_hc_ctx *ctx = cf->ctx;
timediff_t elapsed_ms;
if(!ctx->h21_baller.enabled || cf_hc_baller_has_started(&ctx->h21_baller))
if(ctx->baller_count < 2)
return FALSE;
if(!ctx->h3_baller.enabled || !cf_hc_baller_is_active(&ctx->h3_baller))
return TRUE;
elapsed_ms = Curl_timediff(now, ctx->started);
if(elapsed_ms >= ctx->hard_eyeballs_timeout_ms) {
CURL_TRC_CF(data, cf, "hard timeout of %dms reached, starting h21",
ctx->hard_eyeballs_timeout_ms);
else if(cf_hc_baller_has_started(&ctx->ballers[1]))
return FALSE;
else if(ctx->ballers[0].result) {
CURL_TRC_CF(data, cf, "%s baller failed, starting %s",
ctx->ballers[0].name, ctx->ballers[1].name);
return TRUE;
}
if(elapsed_ms >= ctx->soft_eyeballs_timeout_ms) {
if(cf_hc_baller_reply_ms(&ctx->h3_baller, data) < 0) {
CURL_TRC_CF(data, cf, "soft timeout of %dms reached, h3 has not "
"seen any data, starting h21",
ctx->soft_eyeballs_timeout_ms);
elapsed_ms = curlx_ptimediff_ms(Curl_pgrs_now(data), &ctx->started);
if(elapsed_ms >= ctx->hard_eyeballs_timeout_ms) {
CURL_TRC_CF(data, cf, "%s inconclusive after %" FMT_TIMEDIFF_T ", "
"starting %s", ctx->ballers[0].name,
ctx->hard_eyeballs_timeout_ms, ctx->ballers[1].name);
return TRUE;
}
else if(elapsed_ms >= ctx->soft_eyeballs_timeout_ms) {
if(cf_hc_baller_reply_ms(&ctx->ballers[0], data) < 0) {
CURL_TRC_CF(data, cf, "%s has not seen any data after %"
FMT_TIMEDIFF_T "ms, starting %s",
ctx->ballers[0].name, ctx->soft_eyeballs_timeout_ms,
ctx->ballers[1].name);
return TRUE;
}
/* set the effective hard timeout again */
Curl_expire(data, ctx->hard_eyeballs_timeout_ms - elapsed_ms,
EXPIRE_ALPN_EYEBALLS);
}
return FALSE;
}
static CURLcode cf_hc_connect(struct Curl_cfilter *cf,
struct Curl_easy *data,
bool blocking, bool *done)
static bool cf_hc_may_h3(struct Curl_cfilter *cf,
struct Curl_easy *data)
{
struct cf_hc_ctx *ctx = cf->ctx;
if(!ctx->checked_h3) {
ctx->check_h3_result =
Curl_conn_may_http3(data, cf->conn, ctx->def_transport);
ctx->checked_h3 = TRUE;
}
return !ctx->check_h3_result;
}
static enum alpnid cf_hc_get_httpsrr_alpn(struct Curl_cfilter *cf,
struct Curl_easy *data,
enum alpnid not_this_one)
{
#ifdef USE_HTTPSRR
/* Is there an HTTPSRR use its ALPNs here.
* We are here after having selected a connection to a host+port and
* can no longer change that. Any HTTPSRR advice for other hosts and ports
* we need to ignore. */
const struct Curl_https_rrinfo *rr;
size_t i;
/* Do we have HTTPS-RR information? */
rr = Curl_conn_dns_get_https(
data, cf->sockindex, Curl_conn_get_destination(cf->conn, cf->sockindex));
/* We do not support `rr->no_def_alpn`. */
if(Curl_httpsrr_applicable(data, rr) && !rr->no_def_alpn) {
for(i = 0; i < CURL_ARRAYSIZE(rr->alpns); ++i) {
enum alpnid alpn_rr = (enum alpnid)rr->alpns[i];
if(alpn_rr == not_this_one) /* do not want this one */
continue;
switch(alpn_rr) {
case ALPN_h3:
if((data->state.http_neg.allowed & CURL_HTTP_V3x) &&
cf_hc_may_h3(cf, data)) {
return alpn_rr;
}
break;
case ALPN_h2:
if(data->state.http_neg.allowed & CURL_HTTP_V2x) {
return alpn_rr;
}
break;
case ALPN_h1:
if(data->state.http_neg.allowed & CURL_HTTP_V1x) {
return alpn_rr;
}
break;
default: /* ignore */
break;
}
}
}
#else
(void)cf;
(void)data;
(void)not_this_one;
#endif
return ALPN_none;
}
static enum alpnid cf_hc_get_pref_alpn(struct Curl_cfilter *cf,
struct Curl_easy *data,
enum alpnid not_this_one)
{
if((data->state.http_neg.preferred & data->state.http_neg.allowed)) {
switch(data->state.http_neg.preferred) {
case CURL_HTTP_V3x:
if(cf_hc_may_h3(cf, data) && (ALPN_h3 != not_this_one))
return ALPN_h3;
break;
case CURL_HTTP_V2x:
if(ALPN_h2 != not_this_one)
return ALPN_h2;
break;
case CURL_HTTP_V1x:
/* If we are trying h2 already, h1 is already used as fallback */
if((ALPN_h1 != not_this_one) && (ALPN_h2 != not_this_one))
return ALPN_h1;
break;
default:
break;
}
}
return ALPN_none;
}
static enum alpnid cf_hc_get_first_alpn(struct Curl_cfilter *cf,
struct Curl_easy *data,
http_majors choices,
enum alpnid not_this_one)
{
/* When told to not try h2, we also do not try h1 and vice versa */
bool allow_h1_or_h2 = (not_this_one != ALPN_h1) &&
(not_this_one != ALPN_h2);
if((ALPN_h3 != not_this_one) && (choices & CURL_HTTP_V3x) &&
cf_hc_may_h3(cf, data)) {
return ALPN_h3;
}
if(allow_h1_or_h2 && (choices & CURL_HTTP_V2x)) {
return ALPN_h2;
}
if(allow_h1_or_h2 && (choices & CURL_HTTP_V1x)) {
return ALPN_h1;
}
return ALPN_none;
}
static CURLcode cf_hc_set_baller1(struct Curl_cfilter *cf,
struct Curl_easy *data)
{
struct cf_hc_ctx *ctx = cf->ctx;
enum alpnid alpn1 = ALPN_none;
VERBOSE(const char *source = "HTTPS-RR");
DEBUGASSERT(cf->conn->bits.tls_enable_alpn);
alpn1 = cf_hc_get_httpsrr_alpn(cf, data, ALPN_none);
if(alpn1 == ALPN_none) {
/* preference is configured and allowed, can we use it? */
VERBOSE(source = "preferred version");
alpn1 = cf_hc_get_pref_alpn(cf, data, ALPN_none);
}
if(alpn1 == ALPN_none) {
VERBOSE(source = "wanted versions");
alpn1 = cf_hc_get_first_alpn(cf, data,
data->state.http_neg.wanted,
ALPN_none);
}
if(alpn1 == ALPN_none) {
VERBOSE(source = "allowed versions");
alpn1 = cf_hc_get_first_alpn(cf, data,
data->state.http_neg.allowed,
ALPN_none);
}
if(alpn1 == ALPN_none) {
/* None of the wanted/allowed HTTP versions could be chosen */
if(ctx->check_h3_result) {
CURL_TRC_CF(data, cf, "unable to use HTTP/3");
return ctx->check_h3_result;
}
CURL_TRC_CF(data, cf, "unable to select HTTP version");
return CURLE_FAILED_INIT;
}
cf_hc_baller_assign(&ctx->ballers[0], alpn1, ctx->def_transport);
ctx->baller_count = 1;
CURL_TRC_CF(data, cf, "1st attempt uses %s from %s",
ctx->ballers[0].name, source);
switch(alpn1) {
case ALPN_h1:
/* We really want h1, switch off h2 to make it disappear in ALPN */
data->state.http_neg.wanted &= (uint8_t)~CURL_HTTP_V2x;
break;
default:
break;
}
return CURLE_OK;
}
static void cf_hc_set_baller2(struct Curl_cfilter *cf,
struct Curl_easy *data)
{
struct cf_hc_ctx *ctx = cf->ctx;
enum alpnid alpn2 = ALPN_none, alpn1 = ctx->ballers[0].alpn_id;
VERBOSE(const char *source = "HTTPS-RR");
if(ctx->ballers_complete)
return; /* already done */
if(!ctx->httpsrr_resolved)
return; /* HTTPS-RR pending */
alpn2 = cf_hc_get_httpsrr_alpn(cf, data, alpn1);
if(alpn2 == ALPN_none) {
/* preference is configured and allowed, can we use it? */
VERBOSE(source = "preferred version");
alpn2 = cf_hc_get_pref_alpn(cf, data, alpn1);
}
if(alpn2 == ALPN_none) {
VERBOSE(source = "wanted versions");
alpn2 = cf_hc_get_first_alpn(cf, data,
data->state.http_neg.wanted,
alpn1);
}
if(alpn2 != ALPN_none) {
cf_hc_baller_assign(&ctx->ballers[1], alpn2, ctx->def_transport);
ctx->baller_count = 2;
CURL_TRC_CF(data, cf, "2nd attempt uses %s from %s",
ctx->ballers[1].name, source);
}
ctx->ballers_complete = TRUE;
}
static CURLcode cf_hc_connect(struct Curl_cfilter *cf,
struct Curl_easy *data,
bool *done)
{
struct cf_hc_ctx *ctx = cf->ctx;
struct curltime now;
CURLcode result = CURLE_OK;
(void)blocking;
if(cf->connected) {
*done = TRUE;
return CURLE_OK;
}
*done = FALSE;
now = Curl_now();
if(!ctx->httpsrr_resolved) {
ctx->httpsrr_resolved = Curl_conn_dns_resolved_https(
data, cf->sockindex, Curl_conn_get_destination(cf->conn, cf->sockindex));
#ifdef DEBUGBUILD
if(!ctx->httpsrr_resolved && getenv("CURL_DBG_AWAIT_HTTPSRR")) {
CURL_TRC_CF(data, cf, "awaiting HTTPS-RR");
return CURLE_OK;
}
#endif
}
switch(ctx->state) {
case CF_HC_RESOLV:
ctx->state = CF_HC_INIT;
FALLTHROUGH();
case CF_HC_INIT:
DEBUGASSERT(!ctx->h3_baller.cf);
DEBUGASSERT(!ctx->h21_baller.cf);
DEBUGASSERT(!cf->next);
CURL_TRC_CF(data, cf, "connect, init");
ctx->started = now;
if(ctx->h3_baller.enabled) {
cf_hc_baller_init(&ctx->h3_baller, cf, data, "h3", TRNSPRT_QUIC);
if(ctx->h21_baller.enabled)
Curl_expire(data, ctx->soft_eyeballs_timeout_ms, EXPIRE_ALPN_EYEBALLS);
result = cf_hc_set_baller1(cf, data);
if(result) {
ctx->result = result;
ctx->state = CF_HC_FAILURE;
goto out;
}
cf_hc_set_baller2(cf, data);
ctx->started = *Curl_pgrs_now(data);
cf_hc_baller_init(&ctx->ballers[0], cf, data);
if((ctx->baller_count > 1) || !ctx->ballers_complete) {
Curl_expire(data, ctx->soft_eyeballs_timeout_ms, EXPIRE_ALPN_EYEBALLS);
}
else if(ctx->h21_baller.enabled)
cf_hc_baller_init(&ctx->h21_baller, cf, data, "h21",
cf->conn->transport);
ctx->state = CF_HC_CONNECT;
/* FALLTHROUGH */
FALLTHROUGH();
case CF_HC_CONNECT:
if(cf_hc_baller_is_active(&ctx->h3_baller)) {
result = cf_hc_baller_connect(&ctx->h3_baller, cf, data, done);
if(!ctx->ballers_complete)
cf_hc_set_baller2(cf, data);
if(cf_hc_baller_is_connecting(&ctx->ballers[0])) {
result = cf_hc_baller_connect(&ctx->ballers[0], cf, data, done);
if(!result && *done) {
result = baller_connected(cf, data, &ctx->h3_baller);
result = baller_connected(cf, data, &ctx->ballers[0]);
goto out;
}
}
if(time_to_start_h21(cf, data, now)) {
cf_hc_baller_init(&ctx->h21_baller, cf, data, "h21",
cf->conn->transport);
if(time_to_start_baller2(cf, data)) {
cf_hc_baller_init(&ctx->ballers[1], cf, data);
}
if(cf_hc_baller_is_active(&ctx->h21_baller)) {
CURL_TRC_CF(data, cf, "connect, check h21");
result = cf_hc_baller_connect(&ctx->h21_baller, cf, data, done);
if(cf_hc_baller_is_connecting(&ctx->ballers[1])) {
result = cf_hc_baller_connect(&ctx->ballers[1], cf, data, done);
if(!result && *done) {
result = baller_connected(cf, data, &ctx->h21_baller);
result = baller_connected(cf, data, &ctx->ballers[1]);
goto out;
}
}
if((!ctx->h3_baller.enabled || ctx->h3_baller.result) &&
(!ctx->h21_baller.enabled || ctx->h21_baller.result)) {
/* both failed or disabled. we give up */
CURL_TRC_CF(data, cf, "connect, all failed");
result = ctx->result = ctx->h3_baller.enabled?
ctx->h3_baller.result : ctx->h21_baller.result;
if(ctx->ballers[0].result &&
(ctx->ballers[1].result ||
(ctx->ballers_complete && (ctx->baller_count < 2)))) {
/* all have failed. we give up */
CURL_TRC_CF(data, cf, "connect, all attempts failed");
ctx->result = result = ctx->ballers[0].result;
ctx->state = CF_HC_FAILURE;
goto out;
}
@@ -321,59 +578,84 @@ static CURLcode cf_hc_connect(struct Curl_cfilter *cf,
}
out:
CURL_TRC_CF(data, cf, "connect -> %d, done=%d", result, *done);
CURL_TRC_CF(data, cf, "connect -> %d, done=%d", (int)result, *done);
return result;
}
static int cf_hc_get_select_socks(struct Curl_cfilter *cf,
struct Curl_easy *data,
curl_socket_t *socks)
static CURLcode cf_hc_shutdown(struct Curl_cfilter *cf,
struct Curl_easy *data, bool *done)
{
struct cf_hc_ctx *ctx = cf->ctx;
size_t i, j, s;
int brc, rc = GETSOCK_BLANK;
curl_socket_t bsocks[MAX_SOCKSPEREASYHANDLE];
struct cf_hc_baller *ballers[2];
size_t i;
CURLcode result = CURLE_OK;
if(cf->connected)
return cf->next->cft->get_select_socks(cf->next, data, socks);
DEBUGASSERT(data);
if(cf->connected) {
*done = TRUE;
return CURLE_OK;
}
ballers[0] = &ctx->h3_baller;
ballers[1] = &ctx->h21_baller;
for(i = s = 0; i < sizeof(ballers)/sizeof(ballers[0]); i++) {
struct cf_hc_baller *b = ballers[i];
if(!cf_hc_baller_is_active(b))
/* shutdown all ballers that have not done so already. If one fails,
* continue shutting down others until all are shutdown. */
for(i = 0; i < ctx->baller_count; i++) {
struct cf_hc_baller *b = &ctx->ballers[i];
bool bdone = FALSE;
if(!cf_hc_baller_is_connecting(b) || b->shutdown)
continue;
brc = Curl_conn_cf_get_select_socks(b->cf, data, bsocks);
CURL_TRC_CF(data, cf, "get_selected_socks(%s) -> %x", b->name, brc);
if(!brc)
continue;
for(j = 0; j < MAX_SOCKSPEREASYHANDLE && s < MAX_SOCKSPEREASYHANDLE; ++j) {
if((brc & GETSOCK_WRITESOCK(j)) || (brc & GETSOCK_READSOCK(j))) {
socks[s] = bsocks[j];
if(brc & GETSOCK_WRITESOCK(j))
rc |= GETSOCK_WRITESOCK(s);
if(brc & GETSOCK_READSOCK(j))
rc |= GETSOCK_READSOCK(s);
s++;
}
b->result = b->cf->cft->do_shutdown(b->cf, data, &bdone);
if(b->result || bdone)
b->shutdown = TRUE; /* treat a failed shutdown as done */
}
*done = TRUE;
for(i = 0; i < ctx->baller_count; i++) {
if(!ctx->ballers[i].shutdown)
*done = FALSE;
}
if(*done) {
for(i = 0; i < ctx->baller_count; i++) {
if(ctx->ballers[i].result)
result = ctx->ballers[i].result;
}
}
CURL_TRC_CF(data, cf, "get_selected_socks -> %x", rc);
return rc;
CURL_TRC_CF(data, cf, "shutdown -> %d, done=%d", (int)result, *done);
return result;
}
static CURLcode cf_hc_adjust_pollset(struct Curl_cfilter *cf,
struct Curl_easy *data,
struct easy_pollset *ps)
{
CURLcode result = CURLE_OK;
if(!cf->connected) {
struct cf_hc_ctx *ctx = cf->ctx;
size_t i;
for(i = 0; (i < ctx->baller_count) && !result; i++) {
struct cf_hc_baller *b = &ctx->ballers[i];
if(!cf_hc_baller_is_connecting(b))
continue;
result = Curl_conn_cf_adjust_pollset(b->cf, data, ps);
}
CURL_TRC_CF(data, cf, "adjust_pollset -> %d, %u socks", (int)result,
ps->n);
}
return result;
}
static bool cf_hc_data_pending(struct Curl_cfilter *cf,
const struct Curl_easy *data)
{
struct cf_hc_ctx *ctx = cf->ctx;
size_t i;
if(cf->connected)
return cf->next->cft->has_data_pending(cf->next, data);
CURL_TRC_CF((struct Curl_easy *)data, cf, "data_pending");
return cf_hc_baller_data_pending(&ctx->h3_baller, data)
|| cf_hc_baller_data_pending(&ctx->h21_baller, data);
for(i = 0; i < ctx->baller_count; i++)
if(cf_hc_baller_data_pending(&ctx->ballers[i], data))
return TRUE;
return FALSE;
}
static struct curltime cf_get_max_baller_time(struct Curl_cfilter *cf,
@@ -381,21 +663,17 @@ static struct curltime cf_get_max_baller_time(struct Curl_cfilter *cf,
int query)
{
struct cf_hc_ctx *ctx = cf->ctx;
struct Curl_cfilter *cfb;
struct curltime t, tmax;
size_t i;
memset(&tmax, 0, sizeof(tmax));
memset(&t, 0, sizeof(t));
cfb = ctx->h21_baller.enabled? ctx->h21_baller.cf : NULL;
if(cfb && !cfb->cft->query(cfb, data, query, NULL, &t)) {
if((t.tv_sec || t.tv_usec) && Curl_timediff_us(t, tmax) > 0)
tmax = t;
}
memset(&t, 0, sizeof(t));
cfb = ctx->h3_baller.enabled? ctx->h3_baller.cf : NULL;
if(cfb && !cfb->cft->query(cfb, data, query, NULL, &t)) {
if((t.tv_sec || t.tv_usec) && Curl_timediff_us(t, tmax) > 0)
tmax = t;
for(i = 0; i < ctx->baller_count; i++) {
struct Curl_cfilter *cfb = ctx->ballers[i].cf;
memset(&t, 0, sizeof(t));
if(cfb && !cfb->cft->query(cfb, data, query, NULL, &t)) {
if((t.tv_sec || t.tv_usec) && curlx_ptimediff_us(&t, &tmax) > 0)
tmax = t;
}
}
return tmax;
}
@@ -404,6 +682,9 @@ static CURLcode cf_hc_query(struct Curl_cfilter *cf,
struct Curl_easy *data,
int query, int *pres1, void *pres2)
{
struct cf_hc_ctx *ctx = cf->ctx;
size_t i;
if(!cf->connected) {
switch(query) {
case CF_QUERY_TIMER_CONNECT: {
@@ -416,50 +697,63 @@ static CURLcode cf_hc_query(struct Curl_cfilter *cf,
*when = cf_get_max_baller_time(cf, data, CF_QUERY_TIMER_APPCONNECT);
return CURLE_OK;
}
case CF_QUERY_NEED_FLUSH: {
for(i = 0; i < ctx->baller_count; i++)
if(cf_hc_baller_needs_flush(&ctx->ballers[i], data)) {
*pres1 = TRUE;
return CURLE_OK;
}
break;
}
default:
break;
}
}
return cf->next?
return cf->next ?
cf->next->cft->query(cf->next, data, query, pres1, pres2) :
CURLE_UNKNOWN_OPTION;
}
static void cf_hc_close(struct Curl_cfilter *cf, struct Curl_easy *data)
static CURLcode cf_hc_cntrl(struct Curl_cfilter *cf,
struct Curl_easy *data,
int event, int arg1, void *arg2)
{
CURL_TRC_CF(data, cf, "close");
cf_hc_reset(cf, data);
cf->connected = FALSE;
struct cf_hc_ctx *ctx = cf->ctx;
CURLcode result = CURLE_OK;
size_t i;
if(cf->next) {
cf->next->cft->do_close(cf->next, data);
Curl_conn_cf_discard_chain(&cf->next, data);
if(!cf->connected) {
for(i = 0; i < ctx->baller_count; i++) {
result = cf_hc_baller_cntrl(&ctx->ballers[i], data, event, arg1, arg2);
if(result && (result != CURLE_AGAIN))
goto out;
}
result = CURLE_OK;
}
out:
return result;
}
static void cf_hc_destroy(struct Curl_cfilter *cf, struct Curl_easy *data)
{
struct cf_hc_ctx *ctx = cf->ctx;
(void)data;
CURL_TRC_CF(data, cf, "destroy");
cf_hc_reset(cf, data);
Curl_safefree(ctx);
cf_hc_ctx_destroy(data, ctx);
}
struct Curl_cftype Curl_cft_http_connect = {
"HTTPS-CONNECT",
0,
CF_TYPE_SETUP | CF_TYPE_HTTPSRR,
CURL_LOG_LVL_NONE,
cf_hc_destroy,
cf_hc_connect,
cf_hc_close,
Curl_cf_def_get_host,
cf_hc_get_select_socks,
cf_hc_shutdown,
cf_hc_adjust_pollset,
cf_hc_data_pending,
Curl_cf_def_send,
Curl_cf_def_recv,
Curl_cf_def_cntrl,
cf_hc_cntrl,
Curl_cf_def_conn_is_alive,
Curl_cf_def_conn_keep_alive,
cf_hc_query,
@@ -467,46 +761,42 @@ struct Curl_cftype Curl_cft_http_connect = {
static CURLcode cf_hc_create(struct Curl_cfilter **pcf,
struct Curl_easy *data,
const struct Curl_dns_entry *remotehost,
bool try_h3, bool try_h21)
uint8_t def_transport)
{
struct Curl_cfilter *cf = NULL;
struct cf_hc_ctx *ctx;
CURLcode result = CURLE_OK;
(void)data;
ctx = calloc(sizeof(*ctx), 1);
ctx = curlx_calloc(1, sizeof(*ctx));
if(!ctx) {
result = CURLE_OUT_OF_MEMORY;
goto out;
}
ctx->remotehost = remotehost;
ctx->h3_baller.enabled = try_h3;
ctx->h21_baller.enabled = try_h21;
ctx->def_transport = def_transport;
ctx->hard_eyeballs_timeout_ms = data->set.happy_eyeballs_timeout;
ctx->soft_eyeballs_timeout_ms = data->set.happy_eyeballs_timeout / 2;
result = Curl_cf_create(&cf, &Curl_cft_http_connect, ctx);
if(result)
goto out;
ctx = NULL;
cf_hc_reset(cf, data);
out:
*pcf = result? NULL : cf;
free(ctx);
*pcf = result ? NULL : cf;
cf_hc_ctx_destroy(data, ctx);
return result;
}
static CURLcode cf_http_connect_add(struct Curl_easy *data,
struct connectdata *conn,
int sockindex,
const struct Curl_dns_entry *remotehost,
bool try_h3, bool try_h21)
static CURLcode cf_hc_add(struct Curl_easy *data,
struct connectdata *conn,
int sockindex,
uint8_t def_transport)
{
struct Curl_cfilter *cf;
CURLcode result = CURLE_OK;
DEBUGASSERT(data);
result = cf_hc_create(&cf, data, remotehost, try_h3, try_h21);
result = cf_hc_create(&cf, data, def_transport);
if(result)
goto out;
Curl_conn_cf_add(data, conn, sockindex, cf);
@@ -516,36 +806,25 @@ out:
CURLcode Curl_cf_https_setup(struct Curl_easy *data,
struct connectdata *conn,
int sockindex,
const struct Curl_dns_entry *remotehost)
int sockindex)
{
bool try_h3 = FALSE, try_h21 = TRUE; /* defaults, for now */
CURLcode result = CURLE_OK;
(void)sockindex;
(void)remotehost;
DEBUGASSERT(conn->scheme->protocol == CURLPROTO_HTTPS);
if(!conn->bits.tls_enable_alpn)
/* This filter is intended for HTTPS using ALPN and does
* not support HTTPS Eyeballing to a proxy. */
if((conn->scheme->protocol != CURLPROTO_HTTPS) ||
#ifndef CURL_DISABLE_PROXY
conn->bits.origin_is_proxy ||
#endif
!conn->bits.tls_enable_alpn)
goto out;
if(data->state.httpwant == CURL_HTTP_VERSION_3ONLY) {
result = Curl_conn_may_http3(data, conn);
if(result) /* can't do it */
goto out;
try_h3 = TRUE;
try_h21 = FALSE;
}
else if(data->state.httpwant >= CURL_HTTP_VERSION_3) {
/* We assume that silently not even trying H3 is ok here */
/* TODO: should we fail instead? */
try_h3 = (Curl_conn_may_http3(data, conn) == CURLE_OK);
try_h21 = TRUE;
}
result = cf_hc_add(data, conn, sockindex, conn->transport_wanted);
result = cf_http_connect_add(data, conn, sockindex, remotehost,
try_h3, try_h21);
out:
return result;
}
#endif /* !defined(CURL_DISABLE_HTTP) && !defined(USE_HYPER) */
#endif /* !CURL_DISABLE_HTTP */