- Remove ETCP_CONN.next linked-list, UTUN_INSTANCE.connections list/count
- Unified connections in single ll_queue indexed by peer_node_id (0=pending)
- struct conn_queue_entry { peer_node_id, conn* } for uniform queue data
- Connection lifecycle via etcp.c: create→pending, conn_ready→indexed, close→detach+deferred free
- Close split: phase1 detach from external world, phase2 deferred via uasync_call_soon
- etcp_conn_set_peer_node_id: reindex on ID change (WARN)
- etcp_conn_ref_take/free: blocking close until refcount returns to 0
- Remove chat_connections — use unified connections queue directly
- Update all iteration/search patterns across 40 files (src + tests + chatgui)
180 lines
5.5 KiB
C
180 lines
5.5 KiB
C
#ifndef UTUN_INSTANCE_H
|
|
#define UTUN_INSTANCE_H
|
|
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
|
|
|
|
#include <stdint.h>
|
|
#include <stdbool.h>
|
|
#include <stdio.h>
|
|
#include "../lib/memory_pool.h"
|
|
#include "secure_channel.h"
|
|
#include "etcp_api.h"
|
|
#include "config_parser.h"
|
|
#include "stcp_link.h"
|
|
#include "firewall.h"
|
|
#include "eim_nat.h"
|
|
#include "nat_transport.h"
|
|
#include "proxy/tcp_proxy_client.h"
|
|
#include "etcp_router.h"
|
|
#include "proxy/tcp_proxy_server.h"
|
|
#include "ntp_time.h"
|
|
#include "ntp_node_time.h"
|
|
|
|
#include "etcp_api.h"
|
|
#include "config_parser.h"
|
|
|
|
// Forward declarations
|
|
struct utun_config;
|
|
struct uasync_s;
|
|
struct ROUTE_TABLE;
|
|
struct ETCP_CONN;
|
|
typedef void (*etcp_new_conn_fn)(struct ETCP_CONN* conn, void* arg);
|
|
struct ETCP_SOCKET;
|
|
struct tun_if;
|
|
struct ETCP_BINDINGS;
|
|
struct ETCP_ROUTER_BINDINGS;
|
|
struct TOPO_GROUP;
|
|
struct TOPO_GROUPS;
|
|
struct control_server;
|
|
struct msg_transport;
|
|
struct PING_CONTEXT;
|
|
struct CONN_MGR;
|
|
struct ETCP_CONNECT;
|
|
struct DB_SYNC;
|
|
|
|
struct NETWORK_ENTRY {
|
|
uint64_t id; // 56-bit (offset 0 = index key)
|
|
uint8_t pubkey[SC_PUBKEY_SIZE]; // 32 bytes binary
|
|
uint8_t signing_key[SC_PRIVKEY_SIZE]; // 32 bytes binary
|
|
char name[64];
|
|
};
|
|
|
|
// Queue entry for instance->connections (indexed by peer_node_id)
|
|
struct conn_queue_entry {
|
|
uint64_t peer_node_id;
|
|
struct ETCP_CONN* conn;
|
|
};
|
|
|
|
// uTun instance configuration
|
|
struct UTUN_INSTANCE {
|
|
// Identification
|
|
char name[16]; // Instance name from config
|
|
|
|
// Configuration (moved from utun_state)
|
|
struct utun_config *config;
|
|
|
|
// TUN interface
|
|
struct tun_if* tun;
|
|
|
|
// Route subnets (for cleanup on shutdown)
|
|
struct CFG_ROUTE_ENTRY* route_subnets;
|
|
|
|
struct ROUTE_TABLE* rt;
|
|
struct TOPO_GROUPS* topo_groups; // Groups module for topology exchange
|
|
|
|
// Identification
|
|
uint64_t node_id;
|
|
|
|
char stats_dir[512]; // <config_dir>/stats/ для файлов метрик
|
|
|
|
struct SC_MYKEYS my_keys;
|
|
|
|
uint8_t my_ed25519_pubkey[SC_PUBKEY_SIZE]; // свой Ed25519 pubkey (для подписи метрик)
|
|
uint8_t my_ed25519_privkey[SC_PRIVKEY_SIZE]; // свой Ed25519 privkey (derived from X25519)
|
|
|
|
uint8_t next_socket_id; // Counter for unique socket IDs (0-255)
|
|
|
|
// Main async context
|
|
struct UASYNC* ua;
|
|
|
|
// State
|
|
int running;
|
|
|
|
// Connections (очередь всех подключений для instance)
|
|
struct ll_queue* connections; // indexed by peer_node_id (0=pending), data=conn_queue_entry
|
|
|
|
// Callback chain for new ETCP connections
|
|
struct etcp_cbk_entry* new_conn_cbks;
|
|
void* test_user_ptr; // Generic user pointer (used by tests)
|
|
|
|
struct memory_pool* data_pool;// для входных-выходных данных пакета
|
|
struct memory_pool* pkt_pool;
|
|
struct memory_pool* ack_pool;
|
|
|
|
// Active sockets (UDP)
|
|
struct ETCP_SOCKET* etcp_sockets;// linked-list
|
|
struct stcp_server *stcp_server; // TCP server (single for now)
|
|
|
|
// Pending one-shot pings (for callback on PONG or timeout)
|
|
struct PING_CONTEXT* pending_pings;
|
|
|
|
// Routing statistics
|
|
uint64_t routed_packets;
|
|
uint64_t dropped_packets;
|
|
|
|
// ETCP API bindings (per-instance)
|
|
struct ETCP_BINDINGS api_bindings;
|
|
|
|
// Control server for monitoring
|
|
struct control_server* control_srv;
|
|
|
|
// Message transport server (local IPC → ETCP)
|
|
struct msg_transport* msg_t;
|
|
|
|
// Firewall
|
|
struct firewall_ctx fw;
|
|
|
|
// EIM NAT
|
|
struct eim_nat_ctx nat;
|
|
struct nat_transport_ctx nat_tr;
|
|
|
|
// Socket initialization status: 0=OK, 1=partial (some sockets failed), -1=error (none created)
|
|
int socket_init_status;
|
|
|
|
// TCP proxy client (optional, NULL if not enabled)
|
|
struct tcp_proxy_client* tcp_proxy_client;
|
|
|
|
// etcp_router bindings и seq-connections (per-instance service routing)
|
|
struct ETCP_ROUTER_BINDINGS router_bindings;
|
|
struct ll_queue* router_conns;
|
|
struct CONN_MGR* conn_mgr; // Connection Manager (может быть NULL)
|
|
struct DB_SYNC* db_sync; // Distributed DB sync (может быть NULL)
|
|
|
|
// TCP proxy server (exit node)
|
|
struct tcp_proxy_server tcp_proxy_server;
|
|
|
|
// Networks (queue of NETWORK_ENTRY, indexed by 56-bit id)
|
|
struct ll_queue* networks;
|
|
|
|
// Pending background connections (etcp_connect API)
|
|
struct ETCP_CONNECT* pending_connects;
|
|
uint32_t etcp_connect_timeout_tb; // Initial timeout in 0.1ms units, default 20000 (2s)
|
|
|
|
// NTP time synchronization
|
|
struct NTP_TIME ntp;
|
|
struct NTP_NODE_TIME ntp_node;
|
|
};
|
|
|
|
// Functions
|
|
struct UTUN_INSTANCE* utun_instance_create(struct UASYNC* ua, const char* config_file);
|
|
struct UTUN_INSTANCE* utun_instance_create_from_config(struct UASYNC* ua, struct utun_config* config);
|
|
struct UTUN_INSTANCE* utun_instance_create_from_str(struct UASYNC* ua, const char* config_text);
|
|
void utun_instance_destroy(struct UTUN_INSTANCE* instance);
|
|
int utun_instance_init(struct UTUN_INSTANCE *instance);
|
|
struct UTUN_INSTANCE *utun_instance_reload(struct UTUN_INSTANCE *instance, struct UASYNC *ua, const char *config_file);
|
|
void utun_instance_run(struct UTUN_INSTANCE *instance);
|
|
void utun_instance_stop(struct UTUN_INSTANCE *instance);
|
|
void utun_instance_set_tun_init_enabled(int enabled);
|
|
|
|
// Diagnostic function for memory leak analysis
|
|
void utun_instance_diagnose_leaks(struct UTUN_INSTANCE* instance, const char* phase);
|
|
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|
|
#endif // UTUN_INSTANCE_H
|