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.
142 lines
5.3 KiB
142 lines
5.3 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; |
|
|
|
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_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 |
|
#define INFLIGHT_INITIAL_HASH_SIZE 1024 |
|
|
|
// в этот список пакет добавляется когда перемещается из input_queue в input_send_q, при этом к пакету добавляется struct INFLIGHT_PACKET из inflight_pool. |
|
// пакет полностью удаляется когда приходит ACK (либо conn_reset/close) |
|
struct INFLIGHT_PACKET { |
|
struct ll_entry ll; |
|
struct ETCP_LINK* last_link; // Last sent link |
|
struct ETCP_DGRAM* pkt; // Packet |
|
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 |
|
}; |
|
|
|
// RX packet for assembly linked list |
|
struct RX_PACKET { |
|
uint32_t seq; |
|
uint16_t timestamp; |
|
struct ETCP_DGRAM* pkt; // Packet |
|
}; |
|
|
|
// 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 |
|
|
|
// ============ Processing incoming data to be sent by ETCP |
|
struct ll_queue* input_queue; // Incoming packets to send |
|
|
|
// Inflight очереди (2 шт) - пока пакет в статусе inflight - к нему прикрепляется struct INFLIGHT_PACKET |
|
struct memory_pool* inflight_pool; // память для inflight очередей |
|
struct ll_queue* input_send_q; // очередь на отправку (с элементами struct INFLIGHT_PACKET) |
|
struct ll_queue* input_wait_ack; // очередь ожидающих подтверждение (с элементами struct INFLIGHT_PACKET) |
|
|
|
void (*link_ready_for_send_fn)(struct ETCP_CONN*);// функцию которую должен вызвать драйвер линка при готовности линка принимать данные |
|
|
|
struct ll_queue* output_queue; // Assembled outgoing packets |
|
|
|
|
|
// 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 |
|
|
|
// 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); |
|
|
|
// Отправка: используем 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); |
|
|
|
// Request next packet for load balancer |
|
struct ETCP_DGRAM* etcp_request_pkt(struct ETCP_CONN* etcp); |
|
|
|
#ifdef __cplusplus |
|
} |
|
#endif |
|
|
|
#endif // ETCP_H
|
|
|