Files
utun2/src/utun_instance.c
T
Evgeny 32610304d3 Добавлен тестовый режим для TUN интерфейса и тест роутинга mesh
- Добавлена опция tun_test_mode в конфиг для работы без реального TUN устройства
- Добавлены функции для работы с очередями TUN в тестовом режиме
- Добавлен API для программного создания instance из структуры конфига
- Создан тест test_routing_mesh.c с 3-instance mesh топологией
- Ключи генерируются автоматически и распределяются между instance
2026-02-15 20:37:10 +03:00

471 lines
17 KiB
C

// utun_instance.c - Root instance implementation
#include "utun_instance.h"
#include "config_parser.h"
#include "config_updater.h"
#include "tun_if.h"
#include "route_lib.h"
#include "routing.h"
#include "route_bgp.h"
#include "etcp_connections.h"
#include "etcp.h"
#include "../lib/u_async.h"
#include "../lib/debug_config.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include "../lib/platform_compat.h"
// Forward declarations
static uint32_t get_dest_ip(const uint8_t *packet, size_t len);
// Global instance for signal handlers
static struct UTUN_INSTANCE *g_instance = NULL;
// Global flag to control TUN initialization (disabled by default)
static int g_tun_init_enabled = 0;
// Function to control TUN initialization
void utun_instance_set_tun_init_enabled(int enabled) {
g_tun_init_enabled = enabled ? 1 : 0;
DEBUG_INFO(DEBUG_CATEGORY_TUN, "TUN initialization %s", enabled ? "enabled" : "disabled");
}
// Create and initialize root instance
struct UTUN_INSTANCE* utun_instance_create(struct UASYNC* ua, const char *config_file) {
struct UTUN_INSTANCE *instance = calloc(1, sizeof(struct UTUN_INSTANCE));
if (!instance) return NULL;
// Initialize basic fields
instance->running = 0;
instance->ua = ua;
// Note: Global debug system is initialized from command line arguments if provided
// If not initialized via command line, instance-specific logging can be set up here
// The first initialization wins - either global (from main) or instance-specific
// Ensure keys and node_id exist in config
if (config_ensure_keys_and_node_id(config_file) < 0) {
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "Failed to ensure keys and node_id in config: %s", config_file);
free(instance);
return NULL;
}
// Load configuration
instance->config = parse_config(config_file);
if (!instance->config) {
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "Failed to load config from %s", config_file);
free(instance);
return NULL;
}
// Open log file only if not using global debug system output
if (instance->config->global.log_file) {
debug_set_output_file(instance->config->global.log_file);
}
// Set node_id from config
instance->node_id = instance->config->global.my_node_id;
// Set my keys
if (sc_init_local_keys(&instance->my_keys, instance->config->global.my_public_key_hex, instance->config->global.my_private_key_hex)) {
DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, "Failed to initialize local keys");
}
instance->ack_pool=memory_pool_init(sizeof(struct ACK_PACKET));
instance->data_pool=memory_pool_init(PACKET_DATA_SIZE);
instance->pkt_pool=memory_pool_init(sizeof(struct ETCP_DGRAM) + PACKET_DATA_SIZE);
// Create routing module
if (routing_create(instance) != 0) {
DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, "Failed to create routing module");
free_config(instance->config);
free(instance);
return NULL;
}
DEBUG_INFO(DEBUG_CATEGORY_ROUTING, "Routing module created");
// Add local subnets from config as static routes
struct CFG_ROUTE_ENTRY* subnet = instance->config->my_subnets;
while (subnet) {
struct ROUTE_ENTRY entry = {0};
entry.network = subnet->ip.addr.v4.s_addr; // Network byte order
entry.prefix_length = subnet->netmask;
entry.next_hop = NULL; // Local route
entry.type = ROUTE_TYPE_LOCAL;
entry.flags = ROUTE_FLAG_ACTIVE | ROUTE_FLAG_VALIDATED;
entry.metrics.bandwidth_kbps = UINT32_MAX; // Unlimited
entry.metrics.latency_ms = 0;
entry.metrics.packet_loss_rate = 0;
entry.metrics.hop_count = 0;
if (route_table_insert(instance->rt, &entry)) {
char ip_str[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &entry.network, ip_str, sizeof(ip_str));
DEBUG_INFO(DEBUG_CATEGORY_ROUTING, "Added local route: %s/%d",
ip_str, entry.prefix_length);
} else {
char ip_str[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &entry.network, ip_str, sizeof(ip_str));
DEBUG_WARN(DEBUG_CATEGORY_ROUTING, "Failed to add local route: %s/%d (skipping)",
ip_str, entry.prefix_length);
}
subnet = subnet->next;
}
// Initialize TUN device only if enabled
if (g_tun_init_enabled) {
instance->tun = tun_init(ua, instance->config);
if (!instance->tun) {
DEBUG_ERROR(DEBUG_CATEGORY_TUN, "Failed to initialize TUN device");
free(instance);
return NULL;
}
DEBUG_INFO(DEBUG_CATEGORY_TUN, "TUN interface initialized: %s", instance->tun->ifname);
} else {
// TUN initialization disabled - skip TUN setup
DEBUG_INFO(DEBUG_CATEGORY_TUN, "TUN initialization disabled - skipping TUN device setup");
instance->tun = NULL;
}
// Initialize BGP module for route exchange
instance->bgp = route_bgp_init(instance);
if (!instance->bgp) {
DEBUG_ERROR(DEBUG_CATEGORY_BGP, "Failed to initialize BGP module");
// Non-fatal: BGP is optional for basic operation
} else {
DEBUG_INFO(DEBUG_CATEGORY_BGP, "BGP module initialized");
}
return instance;
}
// Create instance from existing config structure (config ownership transfers to instance)
struct UTUN_INSTANCE* utun_instance_create_from_config(struct UASYNC* ua, struct utun_config* config) {
if (!config) {
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "utun_instance_create_from_config: NULL config");
return NULL;
}
struct UTUN_INSTANCE *instance = calloc(1, sizeof(struct UTUN_INSTANCE));
if (!instance) {
DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, "Failed to allocate UTUN_INSTANCE");
return NULL;
}
// Initialize basic fields
instance->running = 0;
instance->ua = ua;
instance->config = config; // Transfer ownership
// Set node_id from config
instance->node_id = config->global.my_node_id;
// Set my keys
if (sc_init_local_keys(&instance->my_keys, config->global.my_public_key_hex, config->global.my_private_key_hex) != SC_OK) {
DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, "Failed to initialize local keys");
free(instance);
return NULL;
}
instance->ack_pool = memory_pool_init(sizeof(struct ACK_PACKET));
instance->data_pool = memory_pool_init(PACKET_DATA_SIZE);
instance->pkt_pool = memory_pool_init(sizeof(struct ETCP_DGRAM) + PACKET_DATA_SIZE);
// Create routing module
if (routing_create(instance) != 0) {
DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, "Failed to create routing module");
free(instance);
return NULL;
}
DEBUG_INFO(DEBUG_CATEGORY_ROUTING, "Routing module created");
// Add local subnets from config as static routes
struct CFG_ROUTE_ENTRY* subnet = config->my_subnets;
while (subnet) {
struct ROUTE_ENTRY entry = {0};
entry.network = subnet->ip.addr.v4.s_addr;
entry.prefix_length = subnet->netmask;
entry.next_hop = NULL;
entry.type = ROUTE_TYPE_LOCAL;
entry.flags = ROUTE_FLAG_ACTIVE | ROUTE_FLAG_VALIDATED;
entry.metrics.bandwidth_kbps = UINT32_MAX;
entry.metrics.latency_ms = 0;
entry.metrics.packet_loss_rate = 0;
entry.metrics.hop_count = 0;
if (route_table_insert(instance->rt, &entry)) {
char ip_str[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &entry.network, ip_str, sizeof(ip_str));
DEBUG_INFO(DEBUG_CATEGORY_ROUTING, "Added local route: %s/%d",
ip_str, entry.prefix_length);
}
subnet = subnet->next;
}
// Initialize TUN device if enabled
if (g_tun_init_enabled) {
instance->tun = tun_init(ua, config);
if (!instance->tun) {
DEBUG_ERROR(DEBUG_CATEGORY_TUN, "Failed to initialize TUN device");
free(instance);
return NULL;
}
DEBUG_INFO(DEBUG_CATEGORY_TUN, "TUN interface initialized: %s", instance->tun->ifname);
} else {
DEBUG_INFO(DEBUG_CATEGORY_TUN, "TUN initialization disabled - skipping TUN device setup");
instance->tun = NULL;
}
// Initialize BGP module
instance->bgp = route_bgp_init(instance);
if (!instance->bgp) {
DEBUG_ERROR(DEBUG_CATEGORY_BGP, "Failed to initialize BGP module");
} else {
DEBUG_INFO(DEBUG_CATEGORY_BGP, "BGP module initialized");
}
return instance;
}
// Destroy instance and cleanup resources
void utun_instance_destroy(struct UTUN_INSTANCE *instance) {
if (!instance) return;
DEBUG_INFO(DEBUG_CATEGORY_MEMORY, "[INSTANCE_DESTROY] Starting cleanup for instance %p", instance);
// Диагностика ресурсов ДО cleanup
utun_instance_diagnose_leaks(instance, "BEFORE_CLEANUP");
// Stop running if not already
instance->running = 0;
// Cleanup ETCP sockets and connections FIRST (before destroying uasync)
DEBUG_INFO(DEBUG_CATEGORY_MEMORY, "[INSTANCE_DESTROY] Cleaning up ETCP sockets and connections");
struct ETCP_SOCKET* sock = instance->etcp_sockets;
while (sock) {
struct ETCP_SOCKET* next = sock->next;
DEBUG_INFO(DEBUG_CATEGORY_MEMORY, "[INSTANCE_DESTROY] Removing socket %p, fd=%d", sock, sock->fd);
etcp_socket_remove(sock); // Полный cleanup сокета
sock = next;
}
instance->etcp_sockets = NULL;
DEBUG_INFO(DEBUG_CATEGORY_MEMORY, "[INSTANCE_DESTROY] ETCP sockets cleanup complete");
// Safe cleanup of ETCP connections with NULL pointer checks
struct ETCP_CONN* conn = instance->connections;
while (conn) {
struct ETCP_CONN* next = conn->next;
DEBUG_INFO(DEBUG_CATEGORY_MEMORY, "[INSTANCE_DESTROY] Closing connection %p", conn);
if (conn) { // Дополнительная проверка на случай поврежденного списка
etcp_connection_close(conn); // Закрыть соединение (с проверкой NULL внутри)
}
conn = next;
}
instance->connections = NULL;
DEBUG_INFO(DEBUG_CATEGORY_MEMORY, "[INSTANCE_DESTROY] ETCP connections cleanup complete");
// Cleanup TUN
if (instance->tun) {
DEBUG_INFO(DEBUG_CATEGORY_TUN, "Closing TUN interface: %s", instance->tun->ifname);
tun_close(instance->tun);
instance->tun = NULL;
}
// Cleanup routing module
routing_destroy(instance);
// Cleanup BGP module
if (instance->bgp) {
DEBUG_INFO(DEBUG_CATEGORY_BGP, "Destroying BGP module");
route_bgp_destroy(instance);
instance->bgp = NULL;
}
// Cleanup config
if (instance->config) {
DEBUG_INFO(DEBUG_CATEGORY_MEMORY, "[INSTANCE_DESTROY] Freeing configuration");
free_config(instance->config);
instance->config = NULL;
}
// Cleanup packet pool (ensure no leak if stop wasn't called)
if (instance->pkt_pool) {
DEBUG_INFO(DEBUG_CATEGORY_MEMORY, "[INSTANCE_DESTROY] Destroying packet pool");
memory_pool_destroy(instance->pkt_pool);
instance->pkt_pool = NULL;
}
// Cleanup packet pool (ensure no leak if stop wasn't called)
if (instance->ack_pool) {
DEBUG_INFO(DEBUG_CATEGORY_MEMORY, "[INSTANCE_DESTROY] Destroying ack pool");
memory_pool_destroy(instance->ack_pool);
instance->ack_pool = NULL;
}
// Cleanup data pool
if (instance->data_pool) {
DEBUG_INFO(DEBUG_CATEGORY_MEMORY, "[INSTANCE_DESTROY] Destroying data pool");
memory_pool_destroy(instance->data_pool);
instance->data_pool = NULL;
}
// Note: uasync is NOT destroyed here - caller must destroy it separately
// This allows sharing uasync between multiple instances
instance->ua = NULL;
// Clear global instance
if (g_instance == instance) {
g_instance = NULL;
}
// Free the instance memory
DEBUG_INFO(DEBUG_CATEGORY_MEMORY, "[INSTANCE_DESTROY] Freeing instance memory");
free(instance);
DEBUG_INFO(DEBUG_CATEGORY_MEMORY, "[INSTANCE_DESTROY] Instance destroyed completely");
}
// Stop instance
void utun_instance_stop(struct UTUN_INSTANCE *instance) {
if (!instance) return;
instance->running = 0;
// Wakeup main loop using built-in uasync wakeup
if (instance->ua) {
memory_pool_destroy(instance->pkt_pool);
uasync_wakeup(instance->ua);
}
}
int utun_instance_init(struct UTUN_INSTANCE *instance) {
if (!instance) return -1;
// Set TUN interface in routing module
if (instance->tun) {
routing_set_tun(instance);
DEBUG_INFO(DEBUG_CATEGORY_ROUTING, "TUN interface registered in routing module");
}
// Note: TUN socket is already registered in tun_init()
// Initialize connections
// Добавляем прямой вывод для отладки проблемы double free
fprintf(stderr, "DEBUG: utun_instance_init() calling init_connections() for instance %p\n", instance);
fflush(stderr);
int conn_result = init_connections(instance);
fprintf(stderr, "DEBUG: init_connections() returned: %d\n", conn_result);
fflush(stderr);
if (conn_result < 0) {
fprintf(stderr, "DEBUG: Failed to initialize connections, returning error\n");
fflush(stderr);
return -1;
}
fprintf(stderr, "DEBUG: Connections initialized successfully, count=%d\n", instance->connections_count);
fflush(stderr);
return 0;
}
// Диагностическая функция для анализа утечек
void utun_instance_diagnose_leaks(struct UTUN_INSTANCE *instance, const char *phase) {
if (!instance) {
DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, "[DIAGNOSE] NULL instance for phase: %s", phase);
return;
}
struct {
int etcp_sockets_count;
int etcp_connections_count;
int etcp_links_count;
} report = {0};
// Подсчёт ETCP сокетов
struct ETCP_SOCKET *sock = instance->etcp_sockets;
while (sock) {
report.etcp_sockets_count++;
// Подсчёт линков в каждом сокете
for (size_t i = 0; i < sock->num_channels; i++) {
if (sock->links[i]) {
report.etcp_links_count++;
}
}
sock = sock->next;
}
// Подсчёт ETCP соединений
struct ETCP_CONN *conn = instance->connections;
while (conn) {
report.etcp_connections_count++;
// Подсчёт линков в соединениях
struct ETCP_LINK *link = conn->links;
while (link) {
report.etcp_links_count++;
link = link->next;
}
conn = conn->next;
}
printf("\n🔍 [UTUN_INSTANCE LEAK DIAGNOSIS] Phase: %s\n", phase);
printf(" Instance: %p\n", instance);
printf(" Node ID: %llu\n", (unsigned long long)instance->node_id);
printf(" UA instance: %p\n", instance->ua);
printf(" Running: %d\n", instance->running);
printf("\n📊 STRUCTURE COUNTS:\n");
printf(" ETCP Sockets: %d active\n", report.etcp_sockets_count);
printf(" ETCP Connections: %d active\n", report.etcp_connections_count);
printf(" ETCP Links: %d total\n", report.etcp_links_count);
printf("\n🔧 RESOURCE STATUS:\n");
printf(" Memory Pool: %s\n", instance->pkt_pool ? "ALLOCATED" : "NULL");
printf(" TUN Interface: %s\n", instance->tun ? instance->tun->ifname : "NULL");
printf(" TUN FD: %d\n", instance->tun ? instance->tun->fd : -1);
printf(" Connections list: %p\n", instance->connections);
printf(" ETCP Sockets list: %p\n", instance->etcp_sockets);
printf("\n⚠️ POTENTIAL LEAKS:\n");
if (instance->pkt_pool) {
printf(" ❌ Memory Pool not freed\n");
}
if (instance->tun) {
printf(" ❌ TUN interface not closed\n");
}
if (report.etcp_sockets_count > 0) {
printf(" ❌ %d ETCP sockets still allocated\n", report.etcp_sockets_count);
}
if (report.etcp_connections_count > 0) {
printf(" ❌ %d ETCP connections still allocated\n", report.etcp_connections_count);
}
if (report.etcp_links_count > 0) {
printf(" ❌ %d ETCP links still allocated\n", report.etcp_links_count);
}
printf("\n📋 RECOMMENDATIONS:\n");
if (instance->pkt_pool) {
printf(" → Call memory_pool_destroy() before freeing instance\n");
}
if (instance->tun) {
printf(" → Call tun_close() to close TUN interface\n");
}
if (report.etcp_sockets_count > 0) {
printf(" → Iterate and call etcp_socket_remove() for each socket\n");
}
if (report.etcp_connections_count > 0) {
printf(" → Iterate and call etcp_connection_close() for each connection\n");
}
printf("\n");
}