From 4dcfe098a63449f3e199373b38dd88462e92fc2c Mon Sep 17 00:00:00 2001 From: Amaury Denoyelle Date: Tue, 8 Apr 2025 11:49:37 +0200 Subject: [PATCH] MINOR: mux-h2: prepare to support PING emission Adapt the already existing function h2c_ack_ping(). The objective is to be able to emit a PING request. First, it is renamed as h2c_send_ping(). A new boolean argument is used to emit either a PING request or ack. --- src/mux_h2.c | 39 +++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/src/mux_h2.c b/src/mux_h2.c index cf780cc25..9c4178652 100644 --- a/src/mux_h2.c +++ b/src/mux_h2.c @@ -3104,10 +3104,12 @@ static int h2c_send_strm_wu(struct h2c *h2c) return ret; } -/* try to send an ACK for a ping frame on the connection. Returns > 0 on - * success, 0 on missing data or one of the h2_status values. +/* Try to send a PING frame for connection. Set to respond to an + * incoming PING, in this case payload is copied from demux buffer. + * + * Returns a positive value on success, else 0. */ -static int h2c_ack_ping(struct h2c *h2c) +static int h2c_send_ping(struct h2c *h2c, int ack) { struct buffer *res; char str[17]; @@ -3115,16 +3117,29 @@ static int h2c_ack_ping(struct h2c *h2c) TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_PING, h2c->conn); - if (b_data(&h2c->dbuf) < 8) - goto out; + if (!ack) { + memcpy(str, + "\x00\x00\x08" /* length : 8 (same payload) */ + "\x06\x00" /* type : 6, flags : ACK */ + "\x00\x00\x00\x00" /* stream ID */, 9); - memcpy(str, - "\x00\x00\x08" /* length : 8 (same payload) */ - "\x06" "\x01" /* type : 6, flags : ACK */ - "\x00\x00\x00\x00" /* stream ID */, 9); + /* opaque data */ + memcpy(str + 8, "\x00\x01\x02\x03\x04\x05\x06\x07", 8); + } + else { + if (b_data(&h2c->dbuf) < 8) { + /* incoming PING payload too short */ + goto out; + } - /* copy the original payload */ - h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0); + memcpy(str, + "\x00\x00\x08" /* length : 8 (same payload) */ + "\x06\x01" /* type : 6, flags : ACK */ + "\x00\x00\x00\x00" /* stream ID */, 9); + + /* copy the original payload */ + h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0); + } res = br_tail(h2c->mbuf); retry: @@ -4355,7 +4370,7 @@ static void h2_process_demux(struct h2c *h2c) if (h2c->st0 == H2_CS_FRAME_A) { TRACE_PROTO("sending H2 PING ACK frame", H2_EV_TX_FRAME|H2_EV_TX_SETTINGS, h2c->conn, h2s); - ret = h2c_ack_ping(h2c); + ret = h2c_send_ping(h2c, 1); } break; -- 2.39.5