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.
170 lines
6.8 KiB
170 lines
6.8 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 |
|
|
|
#include "pkt_normalizer.h" |
|
|
|
// In struct ETCP_CONN, add: |
|
//struct pn_pair* normalizer; |
|
|
|
//!!!!!!!!!!! надо переделать ll_queue чтобы возвращала не дата а свою структуру |
|
|
|
// Forward declarations |
|
struct UTUN_INSTANCE; |
|
struct UASYNC; |
|
|
|
uint16_t get_current_timestamp(void); |
|
uint64_t get_current_time_units(void); |
|
|
|
// ETCP packet section types (from protocol spec) |
|
#define ETCP_SECTION_PAYLOAD 0x00 // Data payload |
|
#define ETCP_SECTION_ACK 0x01 // ACK section |
|
#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 |
|
#define INFLIGHT_INITIAL_HASH_SIZE 1024 |
|
|
|
// в этот список пакет добавляется когда перемещается из input_queue в input_send_q, при этом к пакету добавляется struct INFLIGHT_PACKET из inflight_pool. |
|
// пакет полностью удаляется когда приходит ACK (либо conn_reset/close) |
|
struct INFLIGHT_PACKET {// выделяется из etcp->inflight_pool |
|
struct ll_entry ll; |
|
struct ETCP_LINK* last_link; // Last sent link |
|
uint64_t last_timestamp; // Last send timestamp |
|
uint32_t seq; // packet seq (ID по документации) |
|
uint8_t send_count; // Number of sends |
|
uint8_t retrans_req_count; // Number of retrans requests |
|
uint8_t state; // WAIT_ACK or WAIT_SEND |
|
}; |
|
|
|
// Список пакетов для сборки. собирается в ll_queue (используем быстрый поиск с хешем) |
|
struct ETCP_FRAGMENT {// выделяется из пула etcp->rx_pool |
|
struct ll_entry ll; |
|
uint32_t seq; |
|
uint16_t timestamp; |
|
}; |
|
|
|
struct ACK_PACKET { |
|
struct ll_entry ll; |
|
uint32_t seq;// sequence number |
|
uint16_t pkt_timestamp;// timestamp пакета (часы уладенной стороны) |
|
uint32_t recv_timestamp;// время приема (локальное) |
|
}; |
|
|
|
// 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; |
|
struct pn_pair* normalizer; |
|
|
|
// Peer info |
|
uint64_t peer_node_id; // Peer node ID |
|
|
|
// ============ Processing incoming data to be sent by ETCP |
|
struct ll_queue* input_queue; // Incoming packets to send (storage: ETCP_FRAGMENT / rx_pool) |
|
|
|
// Inflight очереди (2 шт) - пока пакет в статусе inflight - к нему прикрепляется struct INFLIGHT_PACKET |
|
struct memory_pool* inflight_pool; // память для inflight очередей |
|
struct memory_pool* rx_pool; // память для rx очередей |
|
struct ll_queue* input_send_q; // очередь на отправку (с элементами struct INFLIGHT_PACKET) |
|
struct ll_queue* input_wait_ack; // очередь ожидающих подтверждение (с элементами struct INFLIGHT_PACKET) |
|
struct ll_queue* ack_q; // неотправленные подтверждения приема пакетов |
|
|
|
struct ll_queue* recv_q; // очередь на сборку (с элементами struct ETCP_FRAGMENT) |
|
|
|
void (*link_ready_for_send_fn)(struct ETCP_CONN*);// функцию которую должен вызвать драйвер линка при готовности линка принимать данные |
|
|
|
struct ll_queue* output_queue; // Assembled outgoing packets (storage: ETCP_FRAGMENT / rx_pool) |
|
|
|
|
|
// IDs and state |
|
uint32_t next_tx_id; // Next TX ID |
|
uint32_t last_rx_id; // Last received ID |
|
uint32_t last_delivered_id; // Last delivered to output_queue |
|
|
|
uint32_t rx_ack_till;// из ack пакета - по какой пакет получено и собрано на уданенной стороне |
|
|
|
// Metrics (RTT, jitter, etc.) |
|
uint16_t retrans_delay; |
|
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_resp_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); |
|
|
|
// Отправка: используем api ll_queue для очереди ETCP_CONN.input_queue |
|
// Прием: используем api ll_queue для очереди ETCP_CONN.output_queue |
|
// для очередей используется формат |
|
|
|
// Input from etcp_connections (decrypted packet) |
|
void etcp_conn_input(struct ETCP_DGRAM* pkt); |
|
|
|
// Send data through ETCP connection |
|
// Allocates memory from data_pool and places in input queue |
|
// Returns: 0 on success, -1 on failure |
|
int etcp_send(struct ETCP_CONN* etcp, const void* data, uint16_t len); |
|
|
|
// Request next packet for load balancer |
|
struct ETCP_DGRAM* etcp_request_pkt(struct ETCP_CONN* etcp); |
|
|
|
// Process ACK receipt - remove acknowledged packet from inflight queues |
|
void etcp_ack_recv(struct ETCP_CONN* etcp, uint32_t seq, uint16_t ts, uint16_t dts); |
|
|
|
#ifdef __cplusplus |
|
} |
|
#endif |
|
|
|
#endif // ETCP_H
|