Skip to content

Commit

Permalink
fix: refactor process_flags function for improved state handling and …
Browse files Browse the repository at this point in the history
…clarity

Signed-off-by: Dengfeng Liu <liudf0716@gmail.com>
  • Loading branch information
liudf0716 committed Nov 13, 2024
1 parent a5838d3 commit 056f179
Showing 1 changed file with 26 additions and 19 deletions.
45 changes: 26 additions & 19 deletions tcpmux.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include <stdlib.h>
#include <unistd.h>
#include <stdbool.h>

#include "client.h"
#include "common.h"
Expand Down Expand Up @@ -413,31 +414,37 @@ static void tcp_mux_send_go_away(struct bufferevent *bout, uint32_t reason) {
* @return Returns 1 on success, 0 on failure.
*/
static int process_flags(uint16_t flags, struct tmux_stream *stream) {
uint32_t close_stream = 0;
if ((flags & ACK) == ACK) {
if (stream->state == SYN_SEND)
bool should_close = false;

if (flags & ACK) {
if (stream->state == SYN_SEND) {
stream->state = ESTABLISHED;
} else if ((flags & FIN) == FIN) {
}
}

if (flags & FIN) {
switch (stream->state) {
case SYN_SEND:
case SYN_RECEIVED:
case ESTABLISHED:
stream->state = REMOTE_CLOSE;
break;
case LOCAL_CLOSE:
stream->state = CLOSED;
close_stream = 1;
break;
default:
debug(LOG_ERR, "unexpected FIN flag in state %d", stream->state);
return 0; // Return 0 to indicate an error
case SYN_SEND:
case SYN_RECEIVED:
case ESTABLISHED:
stream->state = REMOTE_CLOSE;
break;
case LOCAL_CLOSE:
stream->state = CLOSED;
should_close = true;
break;
default:
debug(LOG_ERR, "unexpected FIN flag in state %d", stream->state);
return 0;
}
} else if ((flags & RST) == RST) {
}

if (flags & RST) {
stream->state = RESET;
close_stream = 1;
should_close = true;
}

if (close_stream) {
if (should_close) {
debug(LOG_DEBUG, "free stream %d", stream->id);
del_proxy_client_by_stream_id(stream->id);
}
Expand Down

0 comments on commit 056f179

Please sign in to comment.