From 7dafe10db2f0b104317e6640670b4cd973a4e3da Mon Sep 17 00:00:00 2001 From: David Zhuang Date: Tue, 5 Aug 2025 17:45:06 -0700 Subject: [PATCH] ngtcp2: use custom mem funcs Pass curl's memory functions to the nghttp3 and ngtcp2 functions that allow them. This allows custom memory functions passed by the curl user to be used in nghttp3 and ngtcp2. Closes #18196 --- lib/vquic/curl_ngtcp2.c | 4 +-- lib/vquic/curl_osslq.c | 2 +- lib/vquic/vquic.c | 56 +++++++++++++++++++++++++++++++++++++++++ lib/vquic/vquic_int.h | 9 +++++++ 4 files changed, 68 insertions(+), 3 deletions(-) diff --git a/lib/vquic/curl_ngtcp2.c b/lib/vquic/curl_ngtcp2.c index 6467025137..575ebb9022 100644 --- a/lib/vquic/curl_ngtcp2.c +++ b/lib/vquic/curl_ngtcp2.c @@ -1212,7 +1212,7 @@ static CURLcode init_ngh3_conn(struct Curl_cfilter *cf, rc = nghttp3_conn_client_new(&ctx->h3conn, &ngh3_callbacks, &ctx->h3settings, - nghttp3_mem_default(), + Curl_nghttp3_mem(), cf); if(rc) { failf(data, "error creating nghttp3 connection instance"); @@ -2475,7 +2475,7 @@ static const struct alpn_spec ALPN_SPEC_H3 = { &ctx->connected_path, NGTCP2_PROTO_VER_V1, &ng_callbacks, &ctx->settings, &ctx->transport_params, - NULL, cf); + Curl_ngtcp2_mem(), cf); if(rc) return CURLE_QUIC_CONNECT_ERROR; diff --git a/lib/vquic/curl_osslq.c b/lib/vquic/curl_osslq.c index fa2c0a2899..3dd9753a72 100644 --- a/lib/vquic/curl_osslq.c +++ b/lib/vquic/curl_osslq.c @@ -1106,7 +1106,7 @@ static CURLcode cf_osslq_h3conn_init(struct cf_osslq_ctx *ctx, SSL *conn, rc = nghttp3_conn_client_new(&h3->conn, &ngh3_callbacks, &h3->settings, - nghttp3_mem_default(), + Curl_nghttp3_mem(), user_data); if(rc) { result = CURLE_OUT_OF_MEMORY; diff --git a/lib/vquic/vquic.c b/lib/vquic/vquic.c index f8ce34129b..43a2c95021 100644 --- a/lib/vquic/vquic.c +++ b/lib/vquic/vquic.c @@ -724,6 +724,62 @@ CURLcode Curl_conn_may_http3(struct Curl_easy *data, return CURLE_OK; } +#if defined(USE_NGTCP2) || defined(USE_NGHTTP3) + +static void *Curl_ngtcp2_malloc(size_t size, void *user_data) +{ + (void)user_data; + return Curl_cmalloc(size); +} + +static void Curl_ngtcp2_free(void *ptr, void *user_data) +{ + (void)user_data; + Curl_cfree(ptr); +} + +static void *Curl_ngtcp2_calloc(size_t nmemb, size_t size, void *user_data) +{ + (void)user_data; + return Curl_ccalloc(nmemb, size); +} + +static void *Curl_ngtcp2_realloc(void *ptr, size_t size, void *user_data) +{ + (void)user_data; + return Curl_crealloc(ptr, size); +} + +#ifdef USE_NGTCP2 +static struct ngtcp2_mem curl_ngtcp2_mem = { + NULL, + Curl_ngtcp2_malloc, + Curl_ngtcp2_free, + Curl_ngtcp2_calloc, + Curl_ngtcp2_realloc +}; +struct ngtcp2_mem *Curl_ngtcp2_mem(void) +{ + return &curl_ngtcp2_mem; +} +#endif + +#ifdef USE_NGHTTP3 +static struct nghttp3_mem curl_nghttp3_mem = { + NULL, + Curl_ngtcp2_malloc, + Curl_ngtcp2_free, + Curl_ngtcp2_calloc, + Curl_ngtcp2_realloc +}; +struct nghttp3_mem *Curl_nghttp3_mem(void) +{ + return &curl_nghttp3_mem; +} +#endif + +#endif /* USE_NGTCP2 || USE_NGHTTP3 */ + #else /* CURL_DISABLE_HTTP || !USE_HTTP3 */ CURLcode Curl_conn_may_http3(struct Curl_easy *data, diff --git a/lib/vquic/vquic_int.h b/lib/vquic/vquic_int.h index d271368d29..38189beb70 100644 --- a/lib/vquic/vquic_int.h +++ b/lib/vquic/vquic_int.h @@ -91,4 +91,13 @@ CURLcode vquic_recv_packets(struct Curl_cfilter *cf, #endif /* !USE_HTTP3 */ +#ifdef USE_NGTCP2 +struct ngtcp2_mem; +struct ngtcp2_mem *Curl_ngtcp2_mem(void); +#endif +#ifdef USE_NGHTTP3 +struct nghttp3_mem; +struct nghttp3_mem *Curl_nghttp3_mem(void); +#endif + #endif /* HEADER_CURL_VQUIC_QUIC_INT_H */ -- 2.47.3