141 lines
4.0 KiB
C
141 lines
4.0 KiB
C
// etcp.h - Расширенный протокол управления передачей (ETCP Protocol)
|
|
#ifndef ETCP_H
|
|
#define ETCP_H
|
|
|
|
#include "etcp_connections.h"
|
|
#include "ll_queue.h"
|
|
#include "secure_channel.h"
|
|
#include <stdint.h>
|
|
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
|
|
// Forward declarations
|
|
struct UTUN_INSTANCE;
|
|
|
|
// Internal structures (integrated from etcp_master)
|
|
typedef struct rx_packet {
|
|
struct rx_packet* next;
|
|
uint16_t id;
|
|
uint16_t timestamp;
|
|
uint8_t* data;
|
|
uint16_t data_len;
|
|
uint8_t has_payload;
|
|
} rx_packet_t;
|
|
|
|
typedef struct sent_packet {
|
|
struct sent_packet* next;
|
|
uint16_t id;
|
|
uint16_t timestamp;
|
|
uint8_t* data;
|
|
uint16_t data_len; // Total packet length
|
|
uint16_t payload_len; // Payload length (for window accounting)
|
|
uint16_t send_time;
|
|
uint8_t need_ack;
|
|
uint8_t need_retransmit;
|
|
} sent_packet_t;
|
|
|
|
struct ETCP_CONN {
|
|
struct ETCP_CONN* next;
|
|
int mtu;
|
|
|
|
struct UTUN_INSTANCE* instance;
|
|
|
|
// Каналы (соединения) - linked list
|
|
struct ETCP_LINK* links;
|
|
|
|
uint8_t state; // 0 - just created, 1 - initialized, 2 - connection established
|
|
|
|
// Криптография и состояние
|
|
struct secure_channel crypto_ctx;
|
|
|
|
// Информация о пире (для клиентов)
|
|
uint64_t peer_node_id; // Node ID пира
|
|
|
|
struct ll_queue* input_queue; // отправитель -> input_queue -> etcp -> etcp_channel -> отправка (etcp_sockets)
|
|
struct ll_queue* output_queue; // etcp_sockets -> etcp_channel -> etcp -> output_queue -> получатель
|
|
|
|
// Integrated from etcp_master (retransmission logic)
|
|
rx_packet_t* rx_list;
|
|
sent_packet_t* sent_list;
|
|
|
|
// Metrics
|
|
uint16_t rtt_last;
|
|
uint16_t rtt_avg_10;
|
|
uint16_t rtt_avg_100;
|
|
uint16_t jitter;
|
|
uint16_t bandwidth; // Bytes per timebase (0.1us)
|
|
uint32_t bytes_sent_total;
|
|
uint16_t last_sent_timestamp;
|
|
uint32_t bytes_allowed;
|
|
|
|
// Statistics (extended existing get_stats)
|
|
uint32_t retransmissions_count;
|
|
uint32_t ack_packets_count;
|
|
uint32_t control_packets_count;
|
|
uint32_t total_packets_sent;
|
|
uint32_t unique_packets_sent;
|
|
uint32_t bytes_received_total;
|
|
|
|
// State
|
|
uint16_t next_tx_id;
|
|
uint16_t last_sent_id;
|
|
uint16_t last_rx_id;
|
|
uint16_t last_delivered_id;
|
|
|
|
// Timers
|
|
void* next_tx_timer;
|
|
void* retransmit_timer;
|
|
|
|
// RTT history
|
|
uint16_t rtt_history[100];
|
|
uint8_t rtt_history_idx;
|
|
uint8_t rtt_history_count;
|
|
|
|
// Pending ACKs
|
|
uint16_t pending_ack_ids[32];
|
|
uint16_t pending_ack_timestamps[32];
|
|
uint8_t pending_ack_count;
|
|
|
|
// Pending retransmits
|
|
uint16_t pending_retransmit_ids[32];
|
|
uint8_t pending_retransmit_count;
|
|
|
|
// Window management
|
|
uint32_t unacked_bytes;
|
|
uint32_t window_size;
|
|
uint16_t last_acked_id;
|
|
uint16_t last_rx_ack_id;
|
|
uint16_t retrans_timer_period; // Default 20 (2ms)
|
|
uint16_t next_retrans_time;
|
|
uint8_t window_blocked;
|
|
|
|
// Forward progress
|
|
uint16_t oldest_missing_id;
|
|
uint16_t missing_since_time;
|
|
|
|
// No reset/init here - handled in connections
|
|
};
|
|
|
|
// Создание пустого ETCP instance (после надо добавить ключи, каналы)
|
|
struct ETCP_CONN* etcp_connection_create(struct UTUN_INSTANCE* instance);
|
|
|
|
// Уничтожение ETCP instance
|
|
void etcp_destroy(struct ETCP_CONN* etcp);
|
|
|
|
// Обработать входящий пакет (вызывается из etcp_connections)
|
|
void etcp_conn_input(struct ETCP_CONN* etcp, struct ETCP_DGRAM* pkt);
|
|
|
|
// Сброс соединения (переинициализация)
|
|
void etcp_conn_reset(struct ETCP_CONN* etcp);
|
|
|
|
// Получение статистики
|
|
void etcp_get_stats(struct ETCP_CONN* etcp, size_t* packets_sent, size_t* packets_recv,
|
|
size_t* pool_allocs, size_t* pool_reuse);
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|
|
|
|
#endif // ETCP_H
|