97 lines
2.7 KiB
C
97 lines
2.7 KiB
C
// etcp.c - ETCP Protocol Implementation
|
|
#include "etcp.h"
|
|
#include "../lib/debug_config.h"
|
|
#include "crc32.h"
|
|
#include "secure_channel.h"
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
// Creating ETCP instance
|
|
struct ETCP_CONN* etcp_create(struct UTUN_INSTANCE* instance) {
|
|
if (!instance) return NULL;
|
|
|
|
struct ETCP_CONN* etcp = calloc(1, sizeof(struct ETCP_CONN));
|
|
if (!etcp) return NULL;
|
|
|
|
// TODO: generate real random node_id
|
|
etcp->node_id = 0x123456789ABCDEF0;
|
|
etcp->mtu = 1500; // Default MTU
|
|
|
|
etcp->instance = instance;
|
|
etcp->state = 0;
|
|
|
|
// Initialize crypto context
|
|
etcp->crypto_ctx = calloc(1, sizeof(struct secure_channel));
|
|
if (!etcp->crypto_ctx) {
|
|
free(etcp);
|
|
return NULL;
|
|
}
|
|
|
|
if (sc_generate_keypair(etcp->crypto_ctx) != SC_OK) {
|
|
free(etcp->crypto_ctx);
|
|
free(etcp);
|
|
return NULL;
|
|
}
|
|
|
|
// Initialize packet pool
|
|
packet_pool_init(&etcp->packet_pool);
|
|
|
|
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "etcp_create: created instance with node_id=%llu, mtu=%d",
|
|
(unsigned long long)etcp->node_id, etcp->mtu);
|
|
|
|
return etcp;
|
|
}
|
|
|
|
// Destroying ETCP instance
|
|
void etcp_destroy(struct ETCP_CONN* etcp) {
|
|
if (!etcp) return;
|
|
|
|
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "etcp_destroy: destroying instance");
|
|
|
|
// Free crypto context
|
|
if (etcp->crypto_ctx) {
|
|
// Secure channel context doesn't need special cleanup
|
|
free(etcp->crypto_ctx);
|
|
}
|
|
|
|
// Clean packet pool
|
|
packet_pool_destroy(&etcp->packet_pool);
|
|
|
|
free(etcp);
|
|
}
|
|
|
|
// Process incoming packet
|
|
void etcp_conn_input(struct ETCP_CONN* etcp, struct packet_buffer* pkt) {
|
|
if (!etcp || !pkt) return;
|
|
|
|
DEBUG_DEBUG(DEBUG_CATEGORY_ETCP, "etcp_conn_input: packet len=%u", pkt->metadata.data_len);
|
|
|
|
// TODO: process packet through ETCP protocol
|
|
// This will be implemented later for data transfer
|
|
}
|
|
|
|
// Reset connection
|
|
void etcp_conn_reset(struct ETCP_CONN* etcp) {
|
|
if (!etcp) return;
|
|
|
|
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "etcp_conn_reset: resetting instance");
|
|
|
|
// Reset state
|
|
etcp->state = 0;
|
|
|
|
// TODO: clear queues, reset sequence numbers, etc.
|
|
}
|
|
|
|
// Getting statistics
|
|
void etcp_get_stats(struct ETCP_CONN* etcp, size_t* packets_sent, size_t* packets_recv,
|
|
size_t* pool_allocs, size_t* pool_reuse) {
|
|
if (!etcp) return;
|
|
|
|
if (packets_sent) *packets_sent = 0; // TODO: track
|
|
if (packets_recv) *packets_recv = 0; // TODO: track
|
|
|
|
if (pool_allocs || pool_reuse) {
|
|
size_t dummy_overflow;
|
|
packet_pool_get_stats(&etcp->packet_pool, pool_allocs, pool_reuse, &dummy_overflow);
|
|
}
|
|
} |