- Deleted 11 obsolete debug/isolation test files - Restored test_pkt_normalizer.c from git repository - Added test_ecc_encrypt and test_routing to Makefile (2 new functional tests) - Removed settings.o from PN_OBJS (settings.c deleted) - Added crc32.o to SC_LIB_OBJS and ETCP_OBJS to fix linker errors - Removed unused routing_get_stats() function (undefined routing_stats_t type) - Updated changelog with detailed cleanup summary Current test count: 16 tests in Makefile (14 original + 2 new)Active test files in tests/: 25 files (was 35)test_ecc_encrypt: PASStest_routing: Compilation OK
223 lines
7.5 KiB
C
223 lines
7.5 KiB
C
// etcp.c - ETCP Protocol Implementation
|
|
#include "etcp.h"
|
|
#include "../u_async/debug_config.h"
|
|
#include "crc32.h"
|
|
#include <stdlib.h>
|
|
|
|
// Creating ETCP instance
|
|
etcp_t* etcp_create(uint64_t node_id, int mtu) {
|
|
etcp_t* etcp = calloc(1, sizeof(etcp_t));
|
|
if (!etcp) return NULL;
|
|
|
|
etcp->node_id = node_id;
|
|
etcp->sockets = NULL;
|
|
etcp->socket_count = 0;
|
|
etcp->socket_capacity = 0;
|
|
etcp->channels = NULL;
|
|
etcp->channel_count = 0;
|
|
etcp->mtu = mtu; // Store MTU for future use
|
|
|
|
// 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)node_id, mtu);
|
|
|
|
return etcp;
|
|
}
|
|
|
|
// Destroying ETCP instance
|
|
void etcp_destroy(etcp_t* etcp) {
|
|
if (!etcp) return;
|
|
|
|
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "etcp_destroy: destroying instance");
|
|
|
|
// Free all sockets
|
|
for (int i = 0; i < etcp->socket_count; i++) {
|
|
if (etcp->sockets[i]) {
|
|
etcp_socket_destroy(etcp->sockets[i]);
|
|
}
|
|
}
|
|
free(etcp->sockets);
|
|
|
|
// Free all channels
|
|
etcp_channel_t* ch = etcp->channels;
|
|
while (ch) {
|
|
etcp_channel_t* next = ch->next;
|
|
etcp_channel_destroy(ch);
|
|
ch = next;
|
|
}
|
|
|
|
// Clean packet pool
|
|
packet_pool_destroy(&etcp->packet_pool);
|
|
|
|
free(etcp);
|
|
}
|
|
|
|
// Adding socket
|
|
int etcp_add_socket(etcp_t* etcp, const char* bind_addr, uint16_t port) {
|
|
if (!etcp) return -1;
|
|
|
|
// Expand array if needed
|
|
if (etcp->socket_count >= etcp->socket_capacity) {
|
|
int new_capacity = etcp->socket_capacity == 0 ? 4 : etcp->socket_capacity * 2;
|
|
etcp_socket_t** new_sockets = realloc(etcp->sockets, new_capacity * sizeof(etcp_socket_t*));
|
|
if (!new_sockets) return -1;
|
|
|
|
etcp->sockets = new_sockets;
|
|
etcp->socket_capacity = new_capacity;
|
|
}
|
|
|
|
// Create socket
|
|
etcp_socket_t* sock = etcp_socket_create(etcp, bind_addr, port);
|
|
if (!sock) return -1;
|
|
|
|
etcp->sockets[etcp->socket_count++] = sock;
|
|
|
|
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "etcp_add_socket: added socket %d, bind_addr=%s, port=%u",
|
|
etcp->socket_count - 1, bind_addr ? bind_addr : "any", port);
|
|
|
|
return 0;
|
|
}
|
|
|
|
// Main packet processing function
|
|
void etcp_packet_input(etcp_t* etcp, packet_buffer_t* pkt) {
|
|
if (!etcp || !pkt) return;
|
|
|
|
DEBUG_DEBUG(DEBUG_CATEGORY_ETCP, "etcp_packet_input: packet from socket %p, len=%u",
|
|
pkt->metadata.socket, pkt->metadata.data_len);
|
|
|
|
// 1. Search for channel by source address
|
|
etcp_channel_t* channel = NULL;
|
|
for (etcp_channel_t* ch = etcp->channels; ch; ch = ch->next) {
|
|
if (etcp_channel_addr_match(ch, (struct sockaddr*)&pkt->metadata.src_addr)) {
|
|
channel = ch;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (channel) {
|
|
// Channel found
|
|
DEBUG_DEBUG(DEBUG_CATEGORY_ETCP, "etcp_packet_input: channel found %p", channel);
|
|
pkt->metadata.channel = channel;
|
|
|
|
// 2. Decrypt in-place
|
|
if ((pkt->metadata.flags & PACKET_FLAG_ENCRYPTED) && channel->crypto_ready) {
|
|
// TODO: sc_decrypt
|
|
// Test mode: just remove flag
|
|
pkt->metadata.flags &= ~PACKET_FLAG_ENCRYPTED;
|
|
pkt->metadata.stage = PACKET_STAGE_RX_DECRYPTED;
|
|
}
|
|
|
|
// 3. Process ETCP headers
|
|
pkt->metadata.stage = PACKET_STAGE_RX_ETCP;
|
|
|
|
// 4. Pass to application
|
|
if (etcp->data_ready_cb) {
|
|
etcp->data_ready_cb(etcp, channel, pkt, etcp->data_ready_user_data);
|
|
}
|
|
} else {
|
|
// Channel not found
|
|
DEBUG_DEBUG(DEBUG_CATEGORY_ETCP, "etcp_packet_input: channel NOT found");
|
|
|
|
// Check if it's init packet
|
|
if (pkt->metadata.data_len >= 4 &&
|
|
memcmp(pkt->data, "INIT", 4) == 0) {
|
|
// Create new channel
|
|
DEBUG_DEBUG(DEBUG_CATEGORY_ETCP, "etcp_packet_input: creating new channel for init packet");
|
|
channel = etcp_channel_create(etcp);
|
|
if (channel) {
|
|
// Set up channel from packet
|
|
channel->remote_node_id = 0; // Will be extracted from INIT payload
|
|
channel->socket = pkt->metadata.socket;
|
|
memcpy(&channel->remote_addr, &pkt->metadata.src_addr,
|
|
pkt->metadata.src_addr.ss_family == AF_INET ?
|
|
sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6));
|
|
// Copy destination as local address
|
|
memcpy(&channel->local_addr, &pkt->metadata.dst_addr,
|
|
pkt->metadata.dst_addr.ss_family == AF_INET ?
|
|
sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6));
|
|
|
|
// Add to list
|
|
channel->next = etcp->channels;
|
|
etcp->channels = channel;
|
|
etcp->channel_count++;
|
|
|
|
pkt->metadata.channel = channel;
|
|
pkt->metadata.stage = PACKET_STAGE_RX_ETCP;
|
|
|
|
if (etcp->data_ready_cb) {
|
|
etcp->data_ready_cb(etcp, channel, pkt, etcp->data_ready_user_data);
|
|
}
|
|
}
|
|
} else {
|
|
// Send RST (non-init packet for unknown channel)
|
|
DEBUG_DEBUG(DEBUG_CATEGORY_ETCP, "etcp_packet_input: sending RST for unknown channel");
|
|
// TODO: etcp_send_rst(...)
|
|
|
|
// Free packet
|
|
etcp_socket_t* sock = pkt->metadata.socket;
|
|
if (sock && sock->etcp) {
|
|
packet_pool_put(&sock->etcp->packet_pool, pkt);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Sending data through channel
|
|
int etcp_send(etcp_t* etcp, etcp_channel_t* channel, const uint8_t* data, size_t len) {
|
|
if (!etcp || !channel || !data || len == 0) return -1;
|
|
if (!channel->crypto_ready) return -1;
|
|
|
|
// Get packet from pool
|
|
packet_buffer_t* pkt = packet_pool_get(&etcp->packet_pool);
|
|
if (!pkt) return -1;
|
|
|
|
// Fill metadata
|
|
pkt->metadata.channel = channel;
|
|
pkt->metadata.timestamp_us = 0; // TODO: actual time
|
|
pkt->metadata.stage = PACKET_STAGE_TX_APP;
|
|
pkt->metadata.data_len = (len > PACKET_DATA_SIZE) ? PACKET_DATA_SIZE : (uint16_t)len;
|
|
|
|
// Copy data
|
|
memcpy(pkt->data, data, pkt->metadata.data_len);
|
|
|
|
// Encrypt !!!!
|
|
|
|
pkt->metadata.stage = PACKET_STAGE_TX_ENCRYPTED;
|
|
|
|
// Send through socket
|
|
// TODO: Actually send
|
|
DEBUG_DEBUG(DEBUG_CATEGORY_ETCP, "etcp_send: sending %u bytes to channel", pkt->metadata.data_len);
|
|
|
|
// Free packet
|
|
packet_pool_put(&etcp->packet_pool, pkt);
|
|
|
|
return 0;
|
|
}
|
|
|
|
// Setting callback for ready data
|
|
void etcp_set_data_ready_callback(etcp_t* etcp,
|
|
void (*cb)(etcp_t*, etcp_channel_t*, packet_buffer_t*, void*),
|
|
void* user_data) {
|
|
if (!etcp) return;
|
|
|
|
etcp->data_ready_cb = cb;
|
|
etcp->data_ready_user_data = user_data;
|
|
|
|
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "etcp_set_data_ready_callback: callback set");
|
|
}
|
|
|
|
// Getting statistics
|
|
void etcp_get_stats(etcp_t* 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);
|
|
}
|
|
} |