You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

148 lines
4.7 KiB

// etcp.h - ETCP Protocol Header (refactored based on etcp_protocol.txt)
#ifndef ETCP_H
#define ETCP_H
#include "etcp_connections.h"
#include "secure_channel.h"
#include "../lib/ll_queue.h"
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
// Forward declarations
struct UTUN_INSTANCE;
struct UASYNC;
// ETCP packet section types (from protocol spec)
#define ETCP_SECTION_PAYLOAD 0x00 // Data payload
#define ETCP_SECTION_ACK 0x01 // ACK section
#define ETCP_SECTION_RETRANS 0x10 // Retransmission request base (0x10-0x2F)
#define ETCP_SECTION_TIMESTAMP 0x06 // Channel timestamp (example, adjust if needed)
#define ETCP_SECTION_MEAS_TS 0x07 // Measurement timestamp for bandwidth
#define ETCP_SECTION_MEAS_RESP 0x08 // Measurement response
// Inflight packet states
#define INFLIGHT_STATE_WAIT_ACK 0
#define INFLIGHT_STATE_WAIT_SEND 1
// Inflight packet structure
typedef struct inflight_packet {
struct inflight_packet* next;
uint16_t id; // Packet ID
struct ETCP_LINK* last_link; // Last sent link
uint16_t last_timestamp; // Last send timestamp
uint8_t send_count; // Number of sends
uint8_t retrans_req_count; // Number of retrans requests
uint8_t state; // WAIT_ACK or WAIT_SEND
uint8_t need_retrans; // Flag for forced retrans
uint8_t* data; // Packet data
uint16_t data_len; // Data length
uint16_t payload_len; // Payload length for window
} inflight_packet_t;
// RX packet for assembly linked list
typedef struct rx_packet {
struct rx_packet* next;
uint16_t id;
uint16_t timestamp;
uint8_t* data; // Assembled data (payload only)
uint16_t data_len;
} rx_packet_t;
// ETCP connection structure (refactored)
struct ETCP_CONN {
struct ETCP_CONN* next;
int mtu;
struct UTUN_INSTANCE* instance;
// Links (channels) - linked list
struct ETCP_LINK* links;
// Crypto and state
struct secure_channel crypto_ctx;
// Peer info
uint64_t peer_node_id; // Peer node ID
// Queues
struct ll_queue* input_queue; // Incoming packets to send
struct ll_queue* output_queue; // Assembled outgoing packets
// Inflight lists (two lists as per spec)
inflight_packet_t* wait_ack_list; // Waiting for ACK
inflight_packet_t* wait_send_list; // Waiting for send (retrans)
// RX assembly list
rx_packet_t* rx_list; // Sorted by ID for gap detection
// IDs and state
uint16_t next_tx_id; // Next TX ID
uint16_t last_rx_id; // Last received ID
uint16_t last_delivered_id; // Last delivered to output_queue
// Pending ACKs and retrans
uint16_t pending_ack_ids[32];
uint16_t pending_ack_ts[32]; // Timestamps for ACKs
uint8_t pending_ack_count;
uint16_t pending_retrans_ids[32];
uint8_t pending_retrans_count;
// Metrics (RTT, jitter, etc.)
uint16_t rtt_last;
uint16_t rtt_avg_10;
uint16_t rtt_avg_100;
uint16_t jitter;
uint32_t bytes_sent_total;
uint32_t bytes_received_total;
uint32_t retransmissions_count;
// Window and inflight management
uint32_t unacked_bytes; // Current inflight bytes
uint32_t window_size; // Receive window
uint32_t optimal_inflight; // Sum over links
// Timers
void* retrans_timer; // Retrans check timer
void* ack_timer; // ACK send timer
// Bandwidth measurement state
uint8_t burst_in_progress; // Burst transmission flag
uint16_t burst_start_id; // Start ID for burst
// ... (add more for meas_ts, meas_resp)
// Statistics counters
uint32_t ack_packets_count; // Count of ACK packets received
uint16_t last_rx_ack_id; // Last ACK ID received
uint16_t rtt_history[10]; // RTT history for jitter calculation (RTT_HISTORY_SIZE=10)
uint8_t rtt_history_idx; // Current index in RTT history
uint32_t total_packets_sent; // Total packets sent counter
// Flags
uint8_t wait_timeout_active; // In wait timeout state
};
// Functions
struct ETCP_CONN* etcp_connection_create(struct UTUN_INSTANCE* instance);
void etcp_connection_close(struct ETCP_CONN* etcp);
void etcp_conn_reset(struct ETCP_CONN* etcp);
// Input from etcp_connections (decrypted packet)
void etcp_conn_input(struct ETCP_DGRAM* pkt);
// Request next packet for load balancer
struct ETCP_DGRAM* etcp_request_pkt(struct ETCP_CONN* etcp);
// Get stats
void etcp_get_stats(struct ETCP_CONN* etcp, size_t* packets_sent, size_t* packets_recv,
size_t* pool_allocs, size_t* pool_reuse);
uint16_t get_current_timestamp(void);
#ifdef __cplusplus
}
#endif
#endif // ETCP_H