#include | |
#include | |
#include | |
// 定义TCP连接的状态 | |
enum tcp_state { | |
TCP_CLOSED, | |
TCP_LISTEN, | |
TCP_SYN_SENT, | |
TCP_SYN_RECV, | |
TCP_ESTABLISHED, | |
TCP_CLOSE_WAIT, | |
TCP_LAST_ACK, | |
TCP_tiME_WAIT | |
}; | |
struct tcp_connection { | |
enum tcp_state state; | |
unsigned short local_port; | |
unsigned short remote_port; | |
unsigned int sequence_number; | |
unsigned int acknowledgment_number; | |
unsigned short window_size; | |
unsigned short urgent_pointer; | |
unsigned char data_offset; | |
unsigned char flags; | |
struct socket *socket; | |
}; | |
// 创建一个新的TCP连接 | |
struct tcp_connection *create_tcp_connection(unsigned short local_port, unsigned short remote_port) { | |
struct tcp_connection *conn = (struct tcp_connection *) malloc(sizeof(struct tcp_connection)); | |
conn->state = TCP_CLOSED; | |
conn->local_port = local_port; | |
conn->remote_port = remote_port; | |
conn->sequence_number = 0; | |
conn->acknowledgment_number = 0; | |
conn->window_size = 65535; // 默认窗口大小为64KB | |
conn->urgent_pointer = 0; | |
conn->data_offset = 0; // 默认数据偏移量为0 | |
conn->flags = 0; // 默认标志位为0 | |
conn->socket = NULL; // 初始时没有关联的套接字 | |
return conn; | |
} |
更多回帖