better TCP and started using libtuntap

This commit is contained in:
Francesco Cozzuto
2023-03-25 20:18:14 +01:00
parent 57c2f61ea3
commit 092ba2d1ad
17 changed files with 807 additions and 626 deletions
+45 -14
View File
@@ -1,6 +1,5 @@
#include <stddef.h>
#include <stdint.h>
#include <endian.h>
#include "defs.h"
#define TCP_MAX_LISTENERS 32
@@ -22,11 +21,10 @@ typedef struct {
uint16_t dst_port;
uint32_t seq_no;
uint32_t ack_no;
#if __BYTE_ORDER == __LITTLE_ENDIAN
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
uint8_t unused: 4;
uint8_t offset: 4;
#endif
#if __BYTE_ORDER == __BIG_ENDIAN
#else
uint8_t offset: 4;
uint8_t unused: 4;
#endif
@@ -49,20 +47,52 @@ struct tcp_listener_t {
tcp_connection_t *connections_waiting_for_ack;
tcp_connection_t *connections_waiting_for_accept_head;
tcp_connection_t *connections_waiting_for_accept_tail;
void (*callback)(void*);
void *data;
void (*callback_ready_to_accept)(void*);
void *callback_data;
};
struct tcp_connection_t {
tcp_listener_t *listener; // Listener that accepted this connection
tcp_connection_t *next;
tcp_connection_t *prev;
ip_address_t peer_ip;
uint16_t peer_port;
uint32_t seq_no;
uint32_t ack_no;
size_t in_used;
size_t out_used;
void *callback_data;
void (*callback_ready_to_recv)(void*);
void (*callback_ready_to_send)(void*);
ip_address_t peer_ip; // Network byte order
uint16_t peer_port; // CPU byte order
uint32_t rcv_unread; // It's the sequence number of the first
// byte stored in the input buffer, such
// that [rcv_next - rcv_unread] is the
// number of bytes that the parent application
// can read from the socket.
uint32_t rcv_nxt; // RCV.NXT from RFC 793
// It's the sequence number of the next
// byte waiting to be received.
uint32_t rcv_wnd; // RCV.WND from RFC 793
// It's the size of the portion of input
// buffer that's currently free.
uint32_t snd_wnd; // SND.WND from RFC 793
// It's the number of bytes stored in
// the [out_buffer] output buffer, both
// sent but not acknowledged and not sent.
uint32_t snd_nxt; // SND.NXT from RFC 793
// It's the sequence number of the first
// not yet sent byte in the output buffer.
// By subtracting [snd_una] from this value,
// you get the amount of bytes sent out but
// not yet acknowledged.
uint32_t snd_una; // SND.UNA from RFC 793
// It's the sequence number of the last
// byte sent and acknowledged by the peer.
tcp_segment_t out_header; // There must be no padding between
char out_buffer[TCP_OUTPUT_BUFFER_SIZE]; // these two
char in_buffer[TCP_INPUT_BUFFER_SIZE];
@@ -75,6 +105,7 @@ typedef struct {
} tcp_callbacks_t;
struct tcp_state_t {
ip_address_t ip;
tcp_callbacks_t callbacks;
tcp_connection_t *used_connection_list;
tcp_connection_t *free_connection_list;
@@ -84,13 +115,13 @@ struct tcp_state_t {
tcp_listener_t listener_pool[TCP_MAX_LISTENERS];
};
void tcp_init(tcp_state_t *tcp_state, tcp_callbacks_t callbacks);
void tcp_init(tcp_state_t *tcp_state, ip_address_t ip, tcp_callbacks_t callbacks);
void tcp_free(tcp_state_t *tcp_state);
void tcp_seconds_passed(tcp_state_t *state, size_t seconds);
void tcp_process_segment(tcp_state_t *state, ip_address_t sender, tcp_segment_t *segment, size_t len);
tcp_listener_t *tcp_listener_create(tcp_state_t *state, uint16_t port, void *data, void (*callback)(void*));
void tcp_listener_destroy(tcp_listener_t *listener);
tcp_connection_t *tcp_listener_accept(tcp_listener_t *listener);
tcp_connection_t *tcp_listener_accept(tcp_listener_t *listener, void *callback_data, void (*callback_ready_to_recv)(void*), void (*callback_ready_to_send)(void*));
void tcp_connection_destroy(tcp_connection_t *connection);
size_t tcp_connection_recv(tcp_connection_t *connection, void *dst, size_t len);
size_t tcp_connection_send(tcp_connection_t *connection, const void *src, size_t len);