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 '%<scope_id>')

Closes #20918
This commit is contained in:
Stefan Eissing 2026-03-13 13:34:46 +01:00 committed by Daniel Stenberg
parent dfadec7ec3
commit 9325eb5fc4
No known key found for this signature in database
GPG key ID: 5CC908FDB71E12C2
13 changed files with 54 additions and 66 deletions

View file

@ -30,7 +30,7 @@
struct althost { struct althost {
char *host; char *host;
unsigned short port; uint16_t port;
enum alpnid alpnid; enum alpnid alpnid;
}; };

View file

@ -80,7 +80,7 @@ static CURLcode tunnel_stream_init(struct Curl_cfilter *cf,
struct tunnel_stream *ts) struct tunnel_stream *ts)
{ {
const char *hostname; const char *hostname;
int port; uint16_t port;
bool ipv6_ip; bool ipv6_ip;
ts->state = H2_TUNNEL_INIT; 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); Curl_http_proxy_get_destination(cf, &hostname, &port, &ipv6_ip);
/* host:port with IPv6 support */ /* 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); ipv6_ip ? "]" : "", port);
if(!ts->authority) if(!ts->authority)
return CURLE_OUT_OF_MEMORY; return CURLE_OUT_OF_MEMORY;

View file

@ -662,7 +662,7 @@ static CURLcode is_connected(struct Curl_cfilter *cf,
else else
#endif #endif
{ {
int port; uint16_t port;
if(cf->sockindex == SECONDARYSOCKET) if(cf->sockindex == SECONDARYSOCKET)
port = conn->secondary_port; port = conn->secondary_port;
else if(cf->conn->bits.conn_to_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; bool is_ipv6;
if(!Curl_conn_cf_get_ip_info(cf->next, data, &is_ipv6, &ipquad)) { if(!Curl_conn_cf_get_ip_info(cf->next, data, &is_ipv6, &ipquad)) {
const char *host; const char *host;
int port; Curl_conn_get_current_host(data, cf->sockindex, &host, NULL);
Curl_conn_get_current_host(data, cf->sockindex, &host, &port);
CURL_TRC_CF(data, cf, "Connected to %s (%s) port %u", CURL_TRC_CF(data, cf, "Connected to %s (%s) port %u",
host, ipquad.remote_ip, ipquad.remote_port); host, ipquad.remote_ip, ipquad.remote_port);
} }

View file

@ -823,11 +823,13 @@ void Curl_conn_get_current_host(struct Curl_easy *data, int sockindex,
const char **phost, int *pport) const char **phost, int *pport)
{ {
struct Curl_cfilter *cf, *cf_proxy = NULL; struct Curl_cfilter *cf, *cf_proxy = NULL;
int portarg = -1;
if(!data->conn) { if(!data->conn) {
DEBUGASSERT(0); DEBUGASSERT(0);
*phost = ""; *phost = "";
*pport = -1; if(pport)
*pport = -1;
return; 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 an interim host and any authentication or other things apply
* to this interim host and port. */ * to this interim host and port. */
if(!cf_proxy || cf_proxy->cft->query(cf_proxy, data, CF_QUERY_HOST_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 /* Everything connected or query unsuccessful, the overall
* connection's destination is the answer */ * connection's destination is the answer */
*phost = data->conn->host.name; *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, CURLcode Curl_cf_def_cntrl(struct Curl_cfilter *cf,

View file

@ -415,9 +415,8 @@ static bool sasl_choose_ntlm(struct Curl_easy *data, struct sasl_ctx *sctx)
data->set.str[STRING_SERVICE_NAME] : data->set.str[STRING_SERVICE_NAME] :
sctx->sasl->params->service; sctx->sasl->params->service;
const char *hostname; 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->mech = SASL_MECH_STRING_NTLM;
sctx->state1 = SASL_NTLM; sctx->state1 = SASL_NTLM;

View file

@ -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, /* Clear auth if this redirects to a different port number or protocol,
unless permitted */ unless permitted */
if(!data->set.allow_auth_to_other_hosts && (type != FOLLOW_FAKE)) { if(!data->set.allow_auth_to_other_hosts && (type != FOLLOW_FAKE)) {
int port; uint16_t port;
bool clear = FALSE; bool clear = FALSE;
if(data->set.use_port && data->state.allow_port) if(data->set.use_port && data->state.allow_port)
/* a custom port is used */ /* a custom port is used */
port = (int)data->set.use_port; port = data->set.use_port;
else { else {
curl_off_t value; curl_off_t value;
char *portnum; char *portnum;
@ -1228,7 +1228,7 @@ CURLcode Curl_http_follow(struct Curl_easy *data, const char *newurl,
} }
p = portnum; p = portnum;
curlx_str_number(&p, &value, 0xffff); curlx_str_number(&p, &value, 0xffff);
port = (int)value; port = (uint16_t)value;
curlx_free(portnum); curlx_free(portnum);
} }
if(port != data->info.conn_remote_port) { if(port != data->info.conn_remote_port) {
@ -2969,7 +2969,7 @@ static CURLcode http_add_hd(struct Curl_easy *data,
#ifndef CURL_DISABLE_ALTSVC #ifndef CURL_DISABLE_ALTSVC
case H1_HD_ALT_USED: case H1_HD_ALT_USED:
if(conn->bits.altused && !Curl_checkheaders(data, STRCONST("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_host.name,
conn->conn_to_port); conn->conn_to_port);
break; break;

View file

@ -164,7 +164,7 @@ static CURLcode dynhds_add_custom(struct Curl_easy *data,
void Curl_http_proxy_get_destination(struct Curl_cfilter *cf, void Curl_http_proxy_get_destination(struct Curl_cfilter *cf,
const char **phostname, const char **phostname,
int *pport, bool *pipv6_ip) uint16_t *pport, bool *pipv6_ip)
{ {
DEBUGASSERT(cf); DEBUGASSERT(cf);
DEBUGASSERT(cf->conn); DEBUGASSERT(cf->conn);
@ -199,14 +199,14 @@ CURLcode Curl_http_proxy_create_CONNECT(struct httpreq **preq,
struct cf_proxy_ctx *ctx = cf->ctx; struct cf_proxy_ctx *ctx = cf->ctx;
const char *hostname = NULL; const char *hostname = NULL;
char *authority = NULL; char *authority = NULL;
int port; uint16_t port;
bool ipv6_ip; bool ipv6_ip;
CURLcode result; CURLcode result;
struct httpreq *req = NULL; struct httpreq *req = NULL;
Curl_http_proxy_get_destination(cf, &hostname, &port, &ipv6_ip); 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); ipv6_ip ? "]" : "", port);
if(!authority) { if(!authority) {
result = CURLE_OUT_OF_MEMORY; result = CURLE_OUT_OF_MEMORY;

View file

@ -37,7 +37,7 @@ enum Curl_proxy_use {
void Curl_http_proxy_get_destination(struct Curl_cfilter *cf, void Curl_http_proxy_get_destination(struct Curl_cfilter *cf,
const char **phostname, const char **phostname,
int *pport, bool *pipv6_ip); uint16_t *pport, bool *pipv6_ip);
CURLcode Curl_http_proxy_create_CONNECT(struct httpreq **preq, CURLcode Curl_http_proxy_create_CONNECT(struct httpreq **preq,
struct Curl_cfilter *cf, struct Curl_cfilter *cf,

View file

@ -600,22 +600,6 @@ static void multi_done_locked(struct connectdata *conn,
void *userdata) void *userdata)
{ {
struct multi_done_ctx *mdctx = 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); Curl_detach_connection(data);
@ -635,17 +619,18 @@ static void multi_done_locked(struct connectdata *conn,
Curl_dnscache_prune(data); Curl_dnscache_prune(data);
if(multi_conn_should_close(conn, data, (bool)mdctx->premature)) { 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", "forbid=%d, close=%d, premature=%d, conn_multiplex=%d",
conn->connection_id, host, port, data->set.reuse_forbid, conn->connection_id, conn->destination,
conn->bits.close, mdctx->premature, data->set.reuse_forbid, conn->bits.close, mdctx->premature,
Curl_conn_is_multiplex(conn, FIRSTSOCKET)); Curl_conn_is_multiplex(conn, FIRSTSOCKET));
connclose(conn, "disconnecting"); connclose(conn, "disconnecting");
Curl_conn_terminate(data, conn, (bool)mdctx->premature); Curl_conn_terminate(data, conn, (bool)mdctx->premature);
} }
else if(!Curl_conn_get_max_concurrent(data, conn, FIRSTSOCKET)) { 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" CURL_TRC_M(data, "multi_done, conn #%" FMT_OFF_T " to %s was shutdown"
" by server, not reusing", conn->connection_id, host, port); " by server, not reusing", conn->connection_id,
conn->destination);
connclose(conn, "server shutdown"); connclose(conn, "server shutdown");
Curl_conn_terminate(data, conn, (bool)mdctx->premature); 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)) { if(Curl_cpool_conn_now_idle(data, conn)) {
/* connection kept in the cpool */ /* connection kept in the cpool */
data->state.lastconnect_id = conn->connection_id; data->state.lastconnect_id = conn->connection_id;
infof(data, "Connection #%" FMT_OFF_T " to host %s:%d left intact", infof(data, "Connection #%" FMT_OFF_T " to host %s left intact",
conn->connection_id, host, port); conn->connection_id, conn->destination);
} }
else { else {
/* connection was removed from the cpool and destroyed. */ /* connection was removed from the cpool and destroyed. */

View file

@ -1366,7 +1366,7 @@ static struct connectdata *allocate_conn(struct Curl_easy *data)
conn->send_idx = 0; /* default for sending transfer data */ conn->send_idx = 0; /* default for sending transfer data */
conn->connection_id = -1; /* no ID */ conn->connection_id = -1; /* no ID */
conn->attached_xfers = 0; 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 */ /* Store creation time to help future close decision making */
conn->created = *Curl_pgrs_now(data); conn->created = *Curl_pgrs_now(data);
@ -1863,7 +1863,7 @@ static CURLcode setup_connection_internals(struct Curl_easy *data,
struct connectdata *conn) struct connectdata *conn)
{ {
const char *hostname; const char *hostname;
int port; uint16_t port;
CURLcode result; CURLcode result;
DEBUGF(infof(data, "setup connection, bits.close=%d", conn->bits.close)); 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 else
#endif #endif
{ {
port = conn->remote_port; port = conn->bits.conn_to_port ?
if(conn->bits.conn_to_host) conn->conn_to_port : conn->remote_port;
hostname = conn->conn_to_host.name; hostname = conn->bits.conn_to_host ?
else conn->conn_to_host.name : conn->host.name;
hostname = conn->host.name;
} }
#ifdef USE_IPV6 #ifdef USE_IPV6
conn->destination = curl_maprintf("%u/%d/%s", conn->scope_id, port, /* IPv6 addresses with a scope_id (0 is default == global) have a
hostname); * printable representation with a '%<scope_id>' suffix. */
#else if(conn->scope_id)
conn->destination = curl_maprintf("%d/%s", port, hostname); conn->destination = curl_maprintf("[%s:%u]%%%d", hostname, port,
conn->scope_id);
else
#endif #endif
conn->destination = curl_maprintf("%s:%u", hostname, port);
if(!conn->destination) if(!conn->destination)
return CURLE_OUT_OF_MEMORY; return CURLE_OUT_OF_MEMORY;
@ -2890,7 +2892,7 @@ static CURLcode parse_connect_to_slist(struct Curl_easy *data,
} }
if(port >= 0) { if(port >= 0) {
conn->conn_to_port = port; conn->conn_to_port = (uint16_t)port;
conn->bits.conn_to_port = TRUE; conn->bits.conn_to_port = TRUE;
infof(data, "Connecting to port: %d", port); 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->conn_to_port = as->dst.port;
conn->bits.conn_to_port = TRUE; conn->bits.conn_to_port = TRUE;
conn->bits.altused = 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(srcalpnid), host, conn->remote_port,
Curl_alpnid2str(as->dst.alpnid), hostd, as->dst.port); Curl_alpnid2str(as->dst.alpnid), hostd, as->dst.port);
if(srcalpnid != as->dst.alpnid) { if(srcalpnid != as->dst.alpnid) {
@ -3066,7 +3068,7 @@ static CURLcode resolve_server(struct Curl_easy *data,
struct Curl_dns_entry **pdns) struct Curl_dns_entry **pdns)
{ {
struct hostname *ehost; struct hostname *ehost;
int eport; uint16_t eport;
timediff_t timeout_ms = Curl_timeleft_ms(data); timediff_t timeout_ms = Curl_timeleft_ms(data);
const char *peertype = "host"; const char *peertype = "host";
CURLcode result; CURLcode result;

View file

@ -672,20 +672,19 @@ struct connectdata {
that subsequent bound-requested connections are not accidentally reusing that subsequent bound-requested connections are not accidentally reusing
wrong connections. */ wrong connections. */
char *localdev; char *localdev;
uint16_t localportrange;
#if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI) #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
int socks5_gssapi_enctype; int socks5_gssapi_enctype;
#endif #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 */ uint32_t attached_xfers; /* # of attached easy handles */
#ifdef USE_IPV6 #ifdef USE_IPV6
uint32_t scope_id; /* Scope id for IPv6 */ uint32_t scope_id; /* Scope id for IPv6 */
#endif #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 localport;
uint16_t secondary_port; /* secondary socket remote port to connect to uint16_t secondary_port; /* secondary socket remote port to connect to
(ftp) */ (ftp) */

View file

@ -1221,7 +1221,7 @@ static ssl_peer_type get_peer_type(const char *hostname)
CURLcode Curl_ssl_peer_init(struct ssl_peer *peer, CURLcode Curl_ssl_peer_init(struct ssl_peer *peer,
struct Curl_cfilter *cf, struct Curl_cfilter *cf,
const char *tls_id, const char *tls_id,
int transport) uint8_t transport)
{ {
const char *ehostname, *edispname; const char *ehostname, *edispname;
CURLcode result = CURLE_OUT_OF_MEMORY; CURLcode result = CURLE_OUT_OF_MEMORY;

View file

@ -96,8 +96,8 @@ struct ssl_peer {
char *sni; /* SNI version of hostname or NULL if not usable */ char *sni; /* SNI version of hostname or NULL if not usable */
char *scache_key; /* for lookups in session cache */ char *scache_key; /* for lookups in session cache */
ssl_peer_type type; /* type of the peer information */ ssl_peer_type type; /* type of the peer information */
int port; /* port we are talking to */ uint16_t port; /* port we are talking to */
int transport; /* one of TRNSPRT_* defines */ uint8_t transport; /* one of TRNSPRT_* defines */
}; };
CURLsslset Curl_init_sslset_nolock(curl_sslbackend id, const char *name, 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, CURLcode Curl_ssl_peer_init(struct ssl_peer *peer,
struct Curl_cfilter *cf, struct Curl_cfilter *cf,
const char *tls_id, const char *tls_id,
int transport); uint8_t transport);
/** /**
* Free all allocated data and reset peer information. * Free all allocated data and reset peer information.
*/ */