Skip to content

Commit

Permalink
fix: add input validation and error logging to tcp_mux_encode function
Browse files Browse the repository at this point in the history
Signed-off-by: Dengfeng Liu <liudf0716@gmail.com>
  • Loading branch information
liudf0716 committed Nov 13, 2024
1 parent 5607a88 commit 29f1d79
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion tcpmux.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,40 @@ int validate_tcp_mux_protocol(struct tcp_mux_header *tmux_hdr) {
* @param length The length of the TCP MUX message payload.
* @param tmux_hdr Pointer to the tcp_mux_header structure to be filled.
*/
/**
* @brief Encodes a TCP multiplexer header with the specified parameters
*
* This function fills a TCP multiplexer header structure with the provided values,
* performing necessary network byte order conversions for cross-platform compatibility.
*
* @param type The type of TCP multiplexer message (e.g., DATA, WINDOW_UPDATE)
* @param flags Control flags for the message
* @param stream_id Identifier for the stream this message belongs to
* @param length Length of the message payload
* @param tmux_hdr Pointer to header structure to be filled
*
* @pre tmux_hdr must not be NULL
* @pre type must be a valid tcp_mux_type enum value
*/
void tcp_mux_encode(enum tcp_mux_type type, enum tcp_mux_flag flags,
uint32_t stream_id, uint32_t length,
struct tcp_mux_header *tmux_hdr) {
assert(tmux_hdr);
// Validate input parameters
if (!tmux_hdr) {
debug(LOG_ERR, "NULL header pointer provided");
return;
}

if (type > GO_AWAY) {
debug(LOG_ERR, "Invalid TCP MUX type: %d", type);
return;
}

// Fill header fields with provided values
tmux_hdr->version = proto_version;
tmux_hdr->type = type;

// Convert multi-byte fields to network byte order
tmux_hdr->flags = htons(flags);
tmux_hdr->stream_id = htonl(stream_id);
tmux_hdr->length = length ? htonl(length) : 0;
Expand Down

0 comments on commit 29f1d79

Please sign in to comment.