From 9325eb5fc443cd989f87295226d6bc0cc55afb6e Mon Sep 17 00:00:00 2001 From: Stefan Eissing Date: Fri, 13 Mar 2026 13:34:46 +0100 Subject: [PATCH] urldata: import port types and conn destination format Convert more `int port` to `uint16_t` port types. Reshuffle ports in connectdata to save some bytes. Change `conn->destination` format to - make it more readable and thus usable in tracing - add the IPv6 scope_id only when not default (global) and make it resemble more the textual format for IPv6 (e.g. suffix '%') Closes #20918 --- lib/altsvc.h | 2 +- lib/cf-h2-proxy.c | 4 ++-- lib/cf-ip-happy.c | 5 ++--- lib/cfilters.c | 10 +++++++--- lib/curl_sasl.c | 3 +-- lib/http.c | 8 ++++---- lib/http_proxy.c | 6 +++--- lib/http_proxy.h | 2 +- lib/multi.c | 31 ++++++++----------------------- lib/url.c | 30 ++++++++++++++++-------------- lib/urldata.h | 11 +++++------ lib/vtls/vtls.c | 2 +- lib/vtls/vtls.h | 6 +++--- 13 files changed, 54 insertions(+), 66 deletions(-) diff --git a/lib/altsvc.h b/lib/altsvc.h index 2931af452b..702821656c 100644 --- a/lib/altsvc.h +++ b/lib/altsvc.h @@ -30,7 +30,7 @@ struct althost { char *host; - unsigned short port; + uint16_t port; enum alpnid alpnid; }; diff --git a/lib/cf-h2-proxy.c b/lib/cf-h2-proxy.c index 1514890307..1f699f5ceb 100644 --- a/lib/cf-h2-proxy.c +++ b/lib/cf-h2-proxy.c @@ -80,7 +80,7 @@ static CURLcode tunnel_stream_init(struct Curl_cfilter *cf, struct tunnel_stream *ts) { const char *hostname; - int port; + uint16_t port; bool ipv6_ip; ts->state = H2_TUNNEL_INIT; @@ -92,7 +92,7 @@ static CURLcode tunnel_stream_init(struct Curl_cfilter *cf, Curl_http_proxy_get_destination(cf, &hostname, &port, &ipv6_ip); /* host:port with IPv6 support */ - ts->authority = curl_maprintf("%s%s%s:%d", ipv6_ip ? "[" : "", hostname, + ts->authority = curl_maprintf("%s%s%s:%u", ipv6_ip ? "[" : "", hostname, ipv6_ip ? "]" : "", port); if(!ts->authority) return CURLE_OUT_OF_MEMORY; diff --git a/lib/cf-ip-happy.c b/lib/cf-ip-happy.c index dfbf4ae183..937aa6cb56 100644 --- a/lib/cf-ip-happy.c +++ b/lib/cf-ip-happy.c @@ -662,7 +662,7 @@ static CURLcode is_connected(struct Curl_cfilter *cf, else #endif { - int port; + uint16_t port; if(cf->sockindex == SECONDARYSOCKET) port = conn->secondary_port; else if(cf->conn->bits.conn_to_port) @@ -808,8 +808,7 @@ static CURLcode cf_ip_happy_connect(struct Curl_cfilter *cf, bool is_ipv6; if(!Curl_conn_cf_get_ip_info(cf->next, data, &is_ipv6, &ipquad)) { const char *host; - int port; - Curl_conn_get_current_host(data, cf->sockindex, &host, &port); + Curl_conn_get_current_host(data, cf->sockindex, &host, NULL); CURL_TRC_CF(data, cf, "Connected to %s (%s) port %u", host, ipquad.remote_ip, ipquad.remote_port); } diff --git a/lib/cfilters.c b/lib/cfilters.c index 27b0306180..6bc351042d 100644 --- a/lib/cfilters.c +++ b/lib/cfilters.c @@ -823,11 +823,13 @@ void Curl_conn_get_current_host(struct Curl_easy *data, int sockindex, const char **phost, int *pport) { struct Curl_cfilter *cf, *cf_proxy = NULL; + int portarg = -1; if(!data->conn) { DEBUGASSERT(0); *phost = ""; - *pport = -1; + if(pport) + *pport = -1; return; } @@ -843,12 +845,14 @@ void Curl_conn_get_current_host(struct Curl_easy *data, int sockindex, * to an interim host and any authentication or other things apply * to this interim host and port. */ if(!cf_proxy || cf_proxy->cft->query(cf_proxy, data, CF_QUERY_HOST_PORT, - pport, CURL_UNCONST(phost))) { + &portarg, CURL_UNCONST(phost))) { /* Everything connected or query unsuccessful, the overall * connection's destination is the answer */ *phost = data->conn->host.name; - *pport = data->conn->remote_port; + portarg = data->conn->remote_port; } + if(pport) + *pport = portarg; } CURLcode Curl_cf_def_cntrl(struct Curl_cfilter *cf, diff --git a/lib/curl_sasl.c b/lib/curl_sasl.c index b2eb1fe884..60f085901f 100644 --- a/lib/curl_sasl.c +++ b/lib/curl_sasl.c @@ -415,9 +415,8 @@ static bool sasl_choose_ntlm(struct Curl_easy *data, struct sasl_ctx *sctx) data->set.str[STRING_SERVICE_NAME] : sctx->sasl->params->service; const char *hostname; - int port; - Curl_conn_get_current_host(data, FIRSTSOCKET, &hostname, &port); + Curl_conn_get_current_host(data, FIRSTSOCKET, &hostname, NULL); sctx->mech = SASL_MECH_STRING_NTLM; sctx->state1 = SASL_NTLM; diff --git a/lib/http.c b/lib/http.c index ca229144ed..0632ead30a 100644 --- a/lib/http.c +++ b/lib/http.c @@ -1210,12 +1210,12 @@ CURLcode Curl_http_follow(struct Curl_easy *data, const char *newurl, /* Clear auth if this redirects to a different port number or protocol, unless permitted */ if(!data->set.allow_auth_to_other_hosts && (type != FOLLOW_FAKE)) { - int port; + uint16_t port; bool clear = FALSE; if(data->set.use_port && data->state.allow_port) /* a custom port is used */ - port = (int)data->set.use_port; + port = data->set.use_port; else { curl_off_t value; char *portnum; @@ -1228,7 +1228,7 @@ CURLcode Curl_http_follow(struct Curl_easy *data, const char *newurl, } p = portnum; curlx_str_number(&p, &value, 0xffff); - port = (int)value; + port = (uint16_t)value; curlx_free(portnum); } if(port != data->info.conn_remote_port) { @@ -2969,7 +2969,7 @@ static CURLcode http_add_hd(struct Curl_easy *data, #ifndef CURL_DISABLE_ALTSVC case H1_HD_ALT_USED: if(conn->bits.altused && !Curl_checkheaders(data, STRCONST("Alt-Used"))) - result = curlx_dyn_addf(req, "Alt-Used: %s:%d\r\n", + result = curlx_dyn_addf(req, "Alt-Used: %s:%u\r\n", conn->conn_to_host.name, conn->conn_to_port); break; diff --git a/lib/http_proxy.c b/lib/http_proxy.c index 5ebac8d2df..5996c12f4e 100644 --- a/lib/http_proxy.c +++ b/lib/http_proxy.c @@ -164,7 +164,7 @@ static CURLcode dynhds_add_custom(struct Curl_easy *data, void Curl_http_proxy_get_destination(struct Curl_cfilter *cf, const char **phostname, - int *pport, bool *pipv6_ip) + uint16_t *pport, bool *pipv6_ip) { DEBUGASSERT(cf); DEBUGASSERT(cf->conn); @@ -199,14 +199,14 @@ CURLcode Curl_http_proxy_create_CONNECT(struct httpreq **preq, struct cf_proxy_ctx *ctx = cf->ctx; const char *hostname = NULL; char *authority = NULL; - int port; + uint16_t port; bool ipv6_ip; CURLcode result; struct httpreq *req = NULL; Curl_http_proxy_get_destination(cf, &hostname, &port, &ipv6_ip); - authority = curl_maprintf("%s%s%s:%d", ipv6_ip ? "[" : "", hostname, + authority = curl_maprintf("%s%s%s:%u", ipv6_ip ? "[" : "", hostname, ipv6_ip ? "]" : "", port); if(!authority) { result = CURLE_OUT_OF_MEMORY; diff --git a/lib/http_proxy.h b/lib/http_proxy.h index c27787b3a5..155b222edc 100644 --- a/lib/http_proxy.h +++ b/lib/http_proxy.h @@ -37,7 +37,7 @@ enum Curl_proxy_use { void Curl_http_proxy_get_destination(struct Curl_cfilter *cf, const char **phostname, - int *pport, bool *pipv6_ip); + uint16_t *pport, bool *pipv6_ip); CURLcode Curl_http_proxy_create_CONNECT(struct httpreq **preq, struct Curl_cfilter *cf, diff --git a/lib/multi.c b/lib/multi.c index 29dca0617b..c79516abae 100644 --- a/lib/multi.c +++ b/lib/multi.c @@ -600,22 +600,6 @@ static void multi_done_locked(struct connectdata *conn, void *userdata) { struct multi_done_ctx *mdctx = userdata; -#ifdef CURLVERBOSE - const char *host = -#ifndef CURL_DISABLE_PROXY - conn->bits.socksproxy ? - conn->socks_proxy.host.dispname : - conn->bits.httpproxy ? conn->http_proxy.host.dispname : -#endif - conn->bits.conn_to_host ? conn->conn_to_host.dispname : - conn->host.dispname; - int port = -#ifndef CURL_DISABLE_PROXY - conn->bits.httpproxy ? conn->http_proxy.port : -#endif - conn->bits.conn_to_port ? conn->conn_to_port : - conn->remote_port; -#endif /* CURLVERBOSE */ Curl_detach_connection(data); @@ -635,17 +619,18 @@ static void multi_done_locked(struct connectdata *conn, Curl_dnscache_prune(data); if(multi_conn_should_close(conn, data, (bool)mdctx->premature)) { - CURL_TRC_M(data, "multi_done, terminating conn #%" FMT_OFF_T " to %s:%d, " + CURL_TRC_M(data, "multi_done, terminating conn #%" FMT_OFF_T " to %s, " "forbid=%d, close=%d, premature=%d, conn_multiplex=%d", - conn->connection_id, host, port, data->set.reuse_forbid, - conn->bits.close, mdctx->premature, + conn->connection_id, conn->destination, + data->set.reuse_forbid, conn->bits.close, mdctx->premature, Curl_conn_is_multiplex(conn, FIRSTSOCKET)); connclose(conn, "disconnecting"); Curl_conn_terminate(data, conn, (bool)mdctx->premature); } else if(!Curl_conn_get_max_concurrent(data, conn, FIRSTSOCKET)) { - CURL_TRC_M(data, "multi_done, conn #%" FMT_OFF_T " to %s:%d was shutdown" - " by server, not reusing", conn->connection_id, host, port); + CURL_TRC_M(data, "multi_done, conn #%" FMT_OFF_T " to %s was shutdown" + " by server, not reusing", conn->connection_id, + conn->destination); connclose(conn, "server shutdown"); Curl_conn_terminate(data, conn, (bool)mdctx->premature); } @@ -654,8 +639,8 @@ static void multi_done_locked(struct connectdata *conn, if(Curl_cpool_conn_now_idle(data, conn)) { /* connection kept in the cpool */ data->state.lastconnect_id = conn->connection_id; - infof(data, "Connection #%" FMT_OFF_T " to host %s:%d left intact", - conn->connection_id, host, port); + infof(data, "Connection #%" FMT_OFF_T " to host %s left intact", + conn->connection_id, conn->destination); } else { /* connection was removed from the cpool and destroyed. */ diff --git a/lib/url.c b/lib/url.c index ab025a26af..4aca5d504f 100644 --- a/lib/url.c +++ b/lib/url.c @@ -1366,7 +1366,7 @@ static struct connectdata *allocate_conn(struct Curl_easy *data) conn->send_idx = 0; /* default for sending transfer data */ conn->connection_id = -1; /* no ID */ conn->attached_xfers = 0; - conn->remote_port = -1; /* unknown at this point */ + conn->remote_port = 0; /* unknown at this point */ /* Store creation time to help future close decision making */ conn->created = *Curl_pgrs_now(data); @@ -1863,7 +1863,7 @@ static CURLcode setup_connection_internals(struct Curl_easy *data, struct connectdata *conn) { const char *hostname; - int port; + uint16_t port; CURLcode result; DEBUGF(infof(data, "setup connection, bits.close=%d", conn->bits.close)); @@ -1883,19 +1883,21 @@ static CURLcode setup_connection_internals(struct Curl_easy *data, else #endif { - port = conn->remote_port; - if(conn->bits.conn_to_host) - hostname = conn->conn_to_host.name; - else - hostname = conn->host.name; + port = conn->bits.conn_to_port ? + conn->conn_to_port : conn->remote_port; + hostname = conn->bits.conn_to_host ? + conn->conn_to_host.name : conn->host.name; } #ifdef USE_IPV6 - conn->destination = curl_maprintf("%u/%d/%s", conn->scope_id, port, - hostname); -#else - conn->destination = curl_maprintf("%d/%s", port, hostname); + /* IPv6 addresses with a scope_id (0 is default == global) have a + * printable representation with a '%' suffix. */ + if(conn->scope_id) + conn->destination = curl_maprintf("[%s:%u]%%%d", hostname, port, + conn->scope_id); + else #endif + conn->destination = curl_maprintf("%s:%u", hostname, port); if(!conn->destination) return CURLE_OUT_OF_MEMORY; @@ -2890,7 +2892,7 @@ static CURLcode parse_connect_to_slist(struct Curl_easy *data, } if(port >= 0) { - conn->conn_to_port = port; + conn->conn_to_port = (uint16_t)port; conn->bits.conn_to_port = TRUE; infof(data, "Connecting to port: %d", port); } @@ -2994,7 +2996,7 @@ static CURLcode parse_connect_to_slist(struct Curl_easy *data, conn->conn_to_port = as->dst.port; conn->bits.conn_to_port = TRUE; conn->bits.altused = TRUE; - infof(data, "Alt-svc connecting from [%s]%s:%d to [%s]%s:%d", + infof(data, "Alt-svc connecting from [%s]%s:%u to [%s]%s:%u", Curl_alpnid2str(srcalpnid), host, conn->remote_port, Curl_alpnid2str(as->dst.alpnid), hostd, as->dst.port); if(srcalpnid != as->dst.alpnid) { @@ -3066,7 +3068,7 @@ static CURLcode resolve_server(struct Curl_easy *data, struct Curl_dns_entry **pdns) { struct hostname *ehost; - int eport; + uint16_t eport; timediff_t timeout_ms = Curl_timeleft_ms(data); const char *peertype = "host"; CURLcode result; diff --git a/lib/urldata.h b/lib/urldata.h index 0f73121282..536ad1c162 100644 --- a/lib/urldata.h +++ b/lib/urldata.h @@ -672,20 +672,19 @@ struct connectdata { that subsequent bound-requested connections are not accidentally reusing wrong connections. */ char *localdev; - uint16_t localportrange; #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI) int socks5_gssapi_enctype; #endif - /* The field below gets set in connect.c:connecthost() */ - int remote_port; /* the remote port, not the proxy port! */ - int conn_to_port; /* the remote port to connect to. valid only if - bits.conn_to_port is set */ - uint32_t attached_xfers; /* # of attached easy handles */ #ifdef USE_IPV6 uint32_t scope_id; /* Scope id for IPv6 */ #endif + /* The field below gets set in connect.c:connecthost() */ + uint16_t remote_port; /* the remote port, not the proxy port! */ + uint16_t conn_to_port; /* the remote port to connect to. valid only if + bits.conn_to_port is set */ + uint16_t localportrange; uint16_t localport; uint16_t secondary_port; /* secondary socket remote port to connect to (ftp) */ diff --git a/lib/vtls/vtls.c b/lib/vtls/vtls.c index e92e6943ed..3ec9ce27a1 100644 --- a/lib/vtls/vtls.c +++ b/lib/vtls/vtls.c @@ -1221,7 +1221,7 @@ static ssl_peer_type get_peer_type(const char *hostname) CURLcode Curl_ssl_peer_init(struct ssl_peer *peer, struct Curl_cfilter *cf, const char *tls_id, - int transport) + uint8_t transport) { const char *ehostname, *edispname; CURLcode result = CURLE_OUT_OF_MEMORY; diff --git a/lib/vtls/vtls.h b/lib/vtls/vtls.h index 30ba111fa0..6db67cf748 100644 --- a/lib/vtls/vtls.h +++ b/lib/vtls/vtls.h @@ -96,8 +96,8 @@ struct ssl_peer { char *sni; /* SNI version of hostname or NULL if not usable */ char *scache_key; /* for lookups in session cache */ ssl_peer_type type; /* type of the peer information */ - int port; /* port we are talking to */ - int transport; /* one of TRNSPRT_* defines */ + uint16_t port; /* port we are talking to */ + uint8_t transport; /* one of TRNSPRT_* defines */ }; CURLsslset Curl_init_sslset_nolock(curl_sslbackend id, const char *name, @@ -150,7 +150,7 @@ void Curl_ssl_conn_config_update(struct Curl_easy *data, bool for_proxy); CURLcode Curl_ssl_peer_init(struct ssl_peer *peer, struct Curl_cfilter *cf, const char *tls_id, - int transport); + uint8_t transport); /** * Free all allocated data and reset peer information. */