Skip to content

Commit 8c9cfee

Browse files
committed
fix: enhance tcp_mux_send_ping function with input validation and error handling
Signed-off-by: Dengfeng Liu <liudf0716@gmail.com>
1 parent 76f71be commit 8c9cfee

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

tcpmux.c

+28-2
Original file line numberDiff line numberDiff line change
@@ -353,14 +353,40 @@ void tcp_mux_send_data(struct bufferevent *bout, uint16_t flags,
353353
* @param bout A pointer to the bufferevent to write the ping message to.
354354
* @param ping_id The identifier for the ping message.
355355
*/
356+
/**
357+
* @brief Sends a ping message over a TCP multiplexed connection
358+
*
359+
* This function constructs and sends a ping message with the SYN flag set
360+
* if TCP multiplexing is enabled. The ping message includes a unique ping ID
361+
* for tracking responses.
362+
*
363+
* @param bout The bufferevent to send the ping through
364+
* @param ping_id Unique identifier for this ping message
365+
*
366+
* @note Function silently returns if TCP multiplexing is disabled
367+
* or if bufferevent is invalid
368+
*/
356369
void tcp_mux_send_ping(struct bufferevent *bout, uint32_t ping_id) {
357-
if (!tcp_mux_flag())
370+
// Early return if TCP multiplexing is disabled
371+
if (!tcp_mux_flag()) {
372+
debug(LOG_DEBUG, "TCP multiplexing is disabled");
358373
return;
374+
}
359375

376+
// Validate bufferevent
377+
if (!bout) {
378+
debug(LOG_ERR, "Invalid bufferevent for ping");
379+
return;
380+
}
381+
382+
// Prepare and send ping message
360383
struct tcp_mux_header tmux_hdr;
361384
memset(&tmux_hdr, 0, sizeof(tmux_hdr));
362385
tcp_mux_encode(PING, SYN, 0, ping_id, &tmux_hdr);
363-
bufferevent_write(bout, (uint8_t *)&tmux_hdr, sizeof(tmux_hdr));
386+
387+
if (bufferevent_write(bout, &tmux_hdr, sizeof(tmux_hdr)) < 0) {
388+
debug(LOG_ERR, "Failed to send ping message");
389+
}
364390
}
365391

366392
/**

0 commit comments

Comments
 (0)