Skip to content

Commit

Permalink
cfilter: remove 'blocking' connect handling
Browse files Browse the repository at this point in the history
Remove `blocking` argument from cfilter's connect method.

Implement blocking behaviour in Curl_conn_connect() instead for all
filter chains.

Update filters implementations. Several of which did never use the
paramter (QUIC for example). Simplifies connect handling in TLS filters
that no longer need to loop

Fixed a blocking connect call in FTP when waiting on a socket accept()
which only worked because the filter did not implement it.

Closes curl#16397
  • Loading branch information
icing authored and bagder committed Feb 20, 2025
1 parent 654f8cb commit a1850ad
Show file tree
Hide file tree
Showing 29 changed files with 241 additions and 841 deletions.
6 changes: 3 additions & 3 deletions lib/cf-h1-proxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ static CURLcode H1_CONNECT(struct Curl_cfilter *cf,
infof(data, "Connect me again please");
Curl_conn_cf_close(cf, data);
connkeep(conn, "HTTP proxy CONNECT");
result = Curl_conn_cf_connect(cf->next, data, FALSE, &done);
result = Curl_conn_cf_connect(cf->next, data, &done);
goto out;
}
else {
Expand Down Expand Up @@ -638,7 +638,7 @@ static CURLcode H1_CONNECT(struct Curl_cfilter *cf,

static CURLcode cf_h1_proxy_connect(struct Curl_cfilter *cf,
struct Curl_easy *data,
bool blocking, bool *done)
bool *done)
{
CURLcode result;
struct h1_tunnel_state *ts = cf->ctx;
Expand All @@ -649,7 +649,7 @@ static CURLcode cf_h1_proxy_connect(struct Curl_cfilter *cf,
}

CURL_TRC_CF(data, cf, "connect");
result = cf->next->cft->do_connect(cf->next, data, blocking, done);
result = cf->next->cft->do_connect(cf->next, data, done);
if(result || !*done)
return result;

Expand Down
4 changes: 2 additions & 2 deletions lib/cf-h2-proxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -1090,7 +1090,7 @@ static CURLcode H2_CONNECT(struct Curl_cfilter *cf,

static CURLcode cf_h2_proxy_connect(struct Curl_cfilter *cf,
struct Curl_easy *data,
bool blocking, bool *done)
bool *done)
{
struct cf_h2_proxy_ctx *ctx = cf->ctx;
CURLcode result = CURLE_OK;
Expand All @@ -1105,7 +1105,7 @@ static CURLcode cf_h2_proxy_connect(struct Curl_cfilter *cf,

/* Connect the lower filters first */
if(!cf->next->connected) {
result = Curl_conn_cf_connect(cf->next, data, blocking, done);
result = Curl_conn_cf_connect(cf->next, data, done);
if(result || !*done)
return result;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/cf-haproxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ static CURLcode cf_haproxy_date_out_set(struct Curl_cfilter*cf,

static CURLcode cf_haproxy_connect(struct Curl_cfilter *cf,
struct Curl_easy *data,
bool blocking, bool *done)
bool *done)
{
struct cf_haproxy_ctx *ctx = cf->ctx;
CURLcode result;
Expand All @@ -117,7 +117,7 @@ static CURLcode cf_haproxy_connect(struct Curl_cfilter *cf,
return CURLE_OK;
}

result = cf->next->cft->do_connect(cf->next, data, blocking, done);
result = cf->next->cft->do_connect(cf->next, data, done);
if(result || !*done)
return result;

Expand Down
5 changes: 2 additions & 3 deletions lib/cf-https-connect.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ 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;
Expand Down Expand Up @@ -291,14 +291,13 @@ static bool time_to_start_next(struct Curl_cfilter *cf,

static CURLcode cf_hc_connect(struct Curl_cfilter *cf,
struct Curl_easy *data,
bool blocking, bool *done)
bool *done)
{
struct cf_hc_ctx *ctx = cf->ctx;
struct curltime now;
CURLcode result = CURLE_OK;
size_t i, failed_ballers;

(void)blocking;
if(cf->connected) {
*done = TRUE;
return CURLE_OK;
Expand Down
11 changes: 3 additions & 8 deletions lib/cf-socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -1304,7 +1304,7 @@ static int do_connect(struct Curl_cfilter *cf, struct Curl_easy *data,

static CURLcode cf_tcp_connect(struct Curl_cfilter *cf,
struct Curl_easy *data,
bool blocking, bool *done)
bool *done)
{
struct cf_socket_ctx *ctx = cf->ctx;
CURLcode result = CURLE_COULDNT_CONNECT;
Expand All @@ -1316,9 +1316,6 @@ static CURLcode cf_tcp_connect(struct Curl_cfilter *cf,
return CURLE_OK;
}

if(blocking)
return CURLE_UNSUPPORTED_PROTOCOL;

*done = FALSE; /* a negative world view is best */
if(ctx->sock == CURL_SOCKET_BAD) {
int error;
Expand Down Expand Up @@ -1889,12 +1886,11 @@ static CURLcode cf_udp_setup_quic(struct Curl_cfilter *cf,

static CURLcode cf_udp_connect(struct Curl_cfilter *cf,
struct Curl_easy *data,
bool blocking, bool *done)
bool *done)
{
struct cf_socket_ctx *ctx = cf->ctx;
CURLcode result = CURLE_COULDNT_CONNECT;

(void)blocking;
if(cf->connected) {
*done = TRUE;
return CURLE_OK;
Expand Down Expand Up @@ -2099,7 +2095,7 @@ static void cf_tcp_set_accepted_remote_ip(struct Curl_cfilter *cf,

static CURLcode cf_tcp_accept_connect(struct Curl_cfilter *cf,
struct Curl_easy *data,
bool blocking, bool *done)
bool *done)
{
struct cf_socket_ctx *ctx = cf->ctx;
#ifdef USE_IPV6
Expand All @@ -2115,7 +2111,6 @@ static CURLcode cf_tcp_accept_connect(struct Curl_cfilter *cf,

/* we start accepted, if we ever close, we cannot go on */
(void)data;
(void)blocking;
if(cf->connected) {
*done = TRUE;
return CURLE_OK;
Expand Down
61 changes: 57 additions & 4 deletions lib/cfilters.c
Original file line number Diff line number Diff line change
Expand Up @@ -368,10 +368,10 @@ bool Curl_conn_cf_discard_sub(struct Curl_cfilter *cf,

CURLcode Curl_conn_cf_connect(struct Curl_cfilter *cf,
struct Curl_easy *data,
bool blocking, bool *done)
bool *done)
{
if(cf)
return cf->cft->do_connect(cf, data, blocking, done);
return cf->cft->do_connect(cf, data, done);
return CURLE_FAILED_INIT;
}

Expand Down Expand Up @@ -405,6 +405,9 @@ CURLcode Curl_conn_connect(struct Curl_easy *data,
bool blocking,
bool *done)
{
#define CF_CONN_NUM_POLLS_ON_STACK 5
struct pollfd a_few_on_stack[CF_CONN_NUM_POLLS_ON_STACK];
struct curl_pollfds cpfds;
struct Curl_cfilter *cf;
CURLcode result = CURLE_OK;

Expand All @@ -419,15 +422,21 @@ CURLcode Curl_conn_connect(struct Curl_easy *data,
}

*done = cf->connected;
if(!*done) {
if(*done)
return CURLE_OK;

Curl_pollfds_init(&cpfds, a_few_on_stack, CF_CONN_NUM_POLLS_ON_STACK);
while(!*done) {
if(Curl_conn_needs_flush(data, sockindex)) {
DEBUGF(infof(data, "Curl_conn_connect(index=%d), flush", sockindex));
result = Curl_conn_flush(data, sockindex);
if(result && (result != CURLE_AGAIN))
return result;
}

result = cf->cft->do_connect(cf, data, blocking, done);
result = cf->cft->do_connect(cf, data, done);
CURL_TRC_CF(data, cf, "Curl_conn_connect(block=%d) -> %d, done=%d",
blocking, result, *done);
if(!result && *done) {
/* Now that the complete filter chain is connected, let all filters
* persist information at the connection. E.g. cf-socket sets the
Expand All @@ -436,12 +445,56 @@ CURLcode Curl_conn_connect(struct Curl_easy *data,
conn_report_connect_stats(data, data->conn);
data->conn->keepalive = Curl_now();
Curl_verboseconnect(data, data->conn, sockindex);
goto out;
}
else if(result) {
CURL_TRC_CF(data, cf, "Curl_conn_connect(), filter returned %d",
result);
conn_report_connect_stats(data, data->conn);
goto out;
}

if(!blocking)
goto out;
else {
/* check allowed time left */
const timediff_t timeout_ms = Curl_timeleft(data, NULL, TRUE);
curl_socket_t sockfd = Curl_conn_cf_get_socket(cf, data);
struct easy_pollset ps;
int rc;

if(timeout_ms < 0) {
/* no need to continue if time already is up */
failf(data, "connect timeout");
result = CURLE_OPERATION_TIMEDOUT;
goto out;
}

CURL_TRC_CF(data, cf, "Curl_conn_connect(block=1), do poll");
Curl_pollfds_reset(&cpfds);
memset(&ps, 0, sizeof(ps));
/* In general, we want to send after connect, wait on that. */
if(sockfd != CURL_SOCKET_BAD)
Curl_pollset_set_out_only(data, &ps, sockfd);
Curl_conn_adjust_pollset(data, &ps);
result = Curl_pollfds_add_ps(&cpfds, &ps);
if(result)
goto out;

rc = Curl_poll(cpfds.pfds, cpfds.n,
CURLMIN(timeout_ms, (cpfds.n ? 1000 : 10)));
CURL_TRC_CF(data, cf, "Curl_conn_connect(block=1), Curl_poll() -> %d",
rc);
if(rc < 0) {
result = CURLE_COULDNT_CONNECT;
goto out;
}
/* continue iterating */
}
}

out:
Curl_pollfds_cleanup(&cpfds);
return result;
}

Expand Down
4 changes: 2 additions & 2 deletions lib/cfilters.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ typedef CURLcode Curl_cft_shutdown(struct Curl_cfilter *cf,

typedef CURLcode Curl_cft_connect(struct Curl_cfilter *cf,
struct Curl_easy *data,
bool blocking, bool *done);
bool *done);

/* Return the hostname and port the connection goes to.
* This may change with the connection state of filters when tunneling
Expand Down Expand Up @@ -324,7 +324,7 @@ void Curl_conn_cf_discard_all(struct Curl_easy *data,

CURLcode Curl_conn_cf_connect(struct Curl_cfilter *cf,
struct Curl_easy *data,
bool blocking, bool *done);
bool *done);
void Curl_conn_cf_close(struct Curl_cfilter *cf, struct Curl_easy *data);
ssize_t Curl_conn_cf_send(struct Curl_cfilter *cf, struct Curl_easy *data,
const void *buf, size_t len, bool eos,
Expand Down
9 changes: 4 additions & 5 deletions lib/connect.c
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ static CURLcode baller_connect(struct Curl_cfilter *cf,
*connected = baller->connected;
if(!baller->result && !*connected) {
/* evaluate again */
baller->result = Curl_conn_cf_connect(baller->cf, data, 0, connected);
baller->result = Curl_conn_cf_connect(baller->cf, data, connected);

if(!baller->result) {
if(*connected) {
Expand Down Expand Up @@ -948,7 +948,7 @@ static void cf_he_adjust_pollset(struct Curl_cfilter *cf,

static CURLcode cf_he_connect(struct Curl_cfilter *cf,
struct Curl_easy *data,
bool blocking, bool *done)
bool *done)
{
struct cf_he_ctx *ctx = cf->ctx;
CURLcode result = CURLE_OK;
Expand All @@ -958,7 +958,6 @@ static CURLcode cf_he_connect(struct Curl_cfilter *cf,
return CURLE_OK;
}

(void)blocking;
DEBUGASSERT(ctx);
*done = FALSE;

Expand Down Expand Up @@ -1263,7 +1262,7 @@ struct cf_setup_ctx {

static CURLcode cf_setup_connect(struct Curl_cfilter *cf,
struct Curl_easy *data,
bool blocking, bool *done)
bool *done)
{
struct cf_setup_ctx *ctx = cf->ctx;
CURLcode result = CURLE_OK;
Expand All @@ -1276,7 +1275,7 @@ static CURLcode cf_setup_connect(struct Curl_cfilter *cf,
/* connect current sub-chain */
connect_sub_chain:
if(cf->next && !cf->next->connected) {
result = Curl_conn_cf_connect(cf->next, data, blocking, done);
result = Curl_conn_cf_connect(cf->next, data, done);
if(result || !*done)
return result;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/ftp.c
Original file line number Diff line number Diff line change
Expand Up @@ -3603,7 +3603,7 @@ static CURLcode ftp_do_more(struct Curl_easy *data, int *completep)
if(ftpc->wait_data_conn) {
bool serv_conned;

result = Curl_conn_connect(data, SECONDARYSOCKET, TRUE, &serv_conned);
result = Curl_conn_connect(data, SECONDARYSOCKET, FALSE, &serv_conned);
if(result)
return result; /* Failed to accept data connection */

Expand Down
10 changes: 5 additions & 5 deletions lib/http2.c
Original file line number Diff line number Diff line change
Expand Up @@ -2441,7 +2441,7 @@ static void cf_h2_adjust_pollset(struct Curl_cfilter *cf,

static CURLcode cf_h2_connect(struct Curl_cfilter *cf,
struct Curl_easy *data,
bool blocking, bool *done)
bool *done)
{
struct cf_h2_ctx *ctx = cf->ctx;
CURLcode result = CURLE_OK;
Expand All @@ -2455,7 +2455,7 @@ static CURLcode cf_h2_connect(struct Curl_cfilter *cf,

/* Connect the lower filters first */
if(!cf->next->connected) {
result = Curl_conn_cf_connect(cf->next, data, blocking, done);
result = Curl_conn_cf_connect(cf->next, data, done);
if(result || !*done)
return result;
}
Expand Down Expand Up @@ -2827,7 +2827,7 @@ CURLcode Curl_http2_switch(struct Curl_easy *data)

if(cf->next) {
bool done;
return Curl_conn_cf_connect(cf, data, FALSE, &done);
return Curl_conn_cf_connect(cf, data, &done);
}
return CURLE_OK;
}
Expand All @@ -2849,7 +2849,7 @@ CURLcode Curl_http2_switch_at(struct Curl_cfilter *cf, struct Curl_easy *data)

if(cf_h2->next) {
bool done;
return Curl_conn_cf_connect(cf_h2, data, FALSE, &done);
return Curl_conn_cf_connect(cf_h2, data, &done);
}
return CURLE_OK;
}
Expand Down Expand Up @@ -2900,7 +2900,7 @@ CURLcode Curl_http2_upgrade(struct Curl_easy *data,

if(cf->next) {
bool done;
return Curl_conn_cf_connect(cf, data, FALSE, &done);
return Curl_conn_cf_connect(cf, data, &done);
}
return CURLE_OK;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/http_proxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ CURLcode Curl_http_proxy_create_CONNECT(struct httpreq **preq,

static CURLcode http_proxy_cf_connect(struct Curl_cfilter *cf,
struct Curl_easy *data,
bool blocking, bool *done)
bool *done)
{
struct cf_proxy_ctx *ctx = cf->ctx;
CURLcode result;
Expand All @@ -319,7 +319,7 @@ static CURLcode http_proxy_cf_connect(struct Curl_cfilter *cf,

CURL_TRC_CF(data, cf, "connect");
connect_sub:
result = cf->next->cft->do_connect(cf->next, data, blocking, done);
result = cf->next->cft->do_connect(cf->next, data, done);
if(result || !*done)
return result;

Expand Down
5 changes: 5 additions & 0 deletions lib/select.c
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,11 @@ void Curl_pollfds_init(struct curl_pollfds *cpfds,
}
}

void Curl_pollfds_reset(struct curl_pollfds *cpfds)
{
cpfds->n = 0;
}

void Curl_pollfds_cleanup(struct curl_pollfds *cpfds)
{
DEBUGASSERT(cpfds);
Expand Down
2 changes: 2 additions & 0 deletions lib/select.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ void Curl_pollfds_init(struct curl_pollfds *cpfds,
struct pollfd *static_pfds,
unsigned int static_count);

void Curl_pollfds_reset(struct curl_pollfds *cpfds);

void Curl_pollfds_cleanup(struct curl_pollfds *cpfds);

CURLcode Curl_pollfds_add_ps(struct curl_pollfds *cpfds,
Expand Down
Loading

0 comments on commit a1850ad

Please sign in to comment.