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.
 
 
 
 
 
 

206 lines
9.2 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;
// Forward declarations
struct UTUN_INSTANCE;
struct ETCP_CONN;
typedef void (*etcp_on_conn_ready)(struct ETCP_CONN* conn, void* arg);
struct UASYNC;
uint16_t get_current_timestamp(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
#define MAX_INFLIGHT_SIZE 16384 // максимальное число элементов в inflight приёмной очереди (для предотвращения атак)
// в этот список пакет добавляется когда перемещается из 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;// время приема (локальное)
};
#define ETCP_TX_STATE_WAIT_LINKS 1
#define ETCP_TX_STATE_NO_DATA 2
#define ETCP_TX_STATE_ERR_MEM 100
// 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 PKTNORM* 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 (rx_pool -> ETCP_FRAGMENT)
// Inflight очереди (2 шт) - пока пакет в статусе inflight - к нему прикрепляется struct INFLIGHT_PACKET
struct memory_pool* inflight_pool; // память для inflight очередей
struct memory_pool* io_pool; // память для rx очередей
struct ll_queue* input_send_q; // очередь на отправку (inflight_pool -> INFLIGHT_PACKET)
struct ll_queue* input_wait_ack; // очередь ожидающих подтверждение (inflight_pool -> struct INFLIGHT_PACKET)
struct ll_queue* ack_q; // неотправленные подтверждения приема пакетов (instance.ack_pool -> struct ACK_PACKET)
struct ll_queue* recv_q; // очередь на сборку фрагментированных пакетов(rx_pool -> 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; // Not used
uint16_t rtt_last;
uint16_t tt_last;
uint16_t rtt_avg_10;
uint16_t rtt_avg_100;
uint16_t jitter;
uint32_t bytes_sent_total;
// uint32_t bytes_received_total; // Not used
uint32_t retransmissions_count;
uint32_t reinit_count;
uint32_t reset_count;
// uint32_t bytes_sent_norx; // сколько отправили байт без единого ответного пакета (для детекции запроса реконнекта)
// Window and inflight management
uint32_t unacked_bytes; // Current inflight bytes
// uint32_t window_size; // Receive window - Not used
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 - Not used
// uint16_t burst_start_id; // Start ID for burst - Not used
// Statistics counters
uint32_t ack_packets_count; // Count of ACK packets received
// uint16_t last_rx_ack_id; // Last ACK ID received - Not used
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 - Not used
// Flags
uint8_t routing_exchange_active; // 0 - не активен, 1 - надо инициировать обмен маршрутами (клиент), 2 - обмен маршрутами активен
uint8_t got_initial_pkt; //
uint8_t initialized; // 0 - только созданный ETCP, 1 - хотя бы один линк проинициалзирован (обмен ключами произведен)
// Callback for ready notification
etcp_on_conn_ready ready_cbk; // callback при готовности соединения
void* ready_arg; // аргумент для ready_cbk
uint32_t cnt_ack_hit_inf; // счетчик удлений из inflight
uint32_t cnt_ack_hit_sndq; // счетчик удалений inflight пакетов из sndq
uint32_t cnt_ack_miss; // счетчик не найденных ack
uint32_t cnt_link_wait; // счетчик переходов в ожидание когда link busy
uint32_t tx_state; // 1 - wait link ready, 2
uint32_t debug[8]; // 8 значений для дебага (live watch)
// Logging identifier (format: "XXXX→XXXX [name]" - last 4 digits of local and peer node_id + optional name)
char log_name[256];
const char* name; // Connection name from config (e.g., "client_test1"), or NULL/empty
};
// Functions
struct ETCP_CONN* etcp_connection_create(struct UTUN_INSTANCE* instance, char* name);
void etcp_connection_close(struct ETCP_CONN* etcp);
void etcp_conn_reset(struct ETCP_CONN* etcp);
void etcp_conn_reinit(struct ETCP_CONN* etcp);
void etcp_connection_ready(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_int_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);
// Update log_name when peer_node_id becomes known
void etcp_update_log_name(struct ETCP_CONN* etcp);
// Вызывается стеком etcp когда соединение установлено (можно передавать данные)
void etcp_conn_ready(struct ETCP_CONN* conn);
#ifdef __cplusplus
}
#endif
#endif // ETCP_H