Browse Source

backup: before adding inflight bytes limit to loadbalancer

nodeinfo-routing-update
Evgeny 2 weeks ago
parent
commit
12ba3b9719
  1. 384
      tests/test_routing_mesh.c

384
tests/test_routing_mesh.c

@ -1,5 +1,13 @@
// test_routing_mesh.c - Test routing between 3 instances in mesh topology
// Uses programmatic config creation (no temp files)
/**
* @file test_routing_mesh.c
* @brief Тест mesh-топологии из 3-х узлов (A-B-C) с использованием полного стека
*
* Использует:
* - utun_instance_create_from_config + utun_instance_init (правильный путь)
* - normalizer + ETCP + routing/BGP
* - Один shared uasync mainloop
* - Callbacks вместо прямого доступа к структурам
*/
#include <stdio.h>
#include <stdlib.h>
@ -14,23 +22,21 @@
#include "../lib/platform_compat.h"
#include "../src/etcp.h"
#include "../src/etcp_connections.h"
#include "../src/etcp_api.h"
#include "../src/config_parser.h"
#include "../src/utun_instance.h"
#include "../src/routing.h"
#include "../src/tun_if.h"
#include "../src/secure_channel.h"
#include "../lib/u_async.h"
#include "../lib/debug_config.h"
#include "../lib/mem.h"
#define TEST_TIMEOUT_MS 10000 // 10 seconds timeout
#define TEST_TIMEOUT_MS 15000
// Keys from test_etcp_two_instances.c (known working keys)
// Instance A keys
// Ключи (известно рабочие)
#define KEY_A_PRIV "67b705a92b41bcaae105af2d6a17743faa7b26ccebba8b3b9b0af05e9cd1d5fb"
#define KEY_A_PUB "1c55e4ccae7c4470707759086738b10681bf88b81f198cc2ab54a647d1556e17c65e6b1833e0c771e5a39382c03067c388915a4c732191bc130480f20f8e00b9"
// Instance B keys
#define KEY_B_PRIV "4813d31d28b7e9829247f488c6be7672f2bdf61b2508333128e386d1759afed2"
#define KEY_B_PUB "c594f33c91f3a2222795c2c110c527bf214ad1009197ce14556cb13df3c461b3c373bed8f205a8dd1fc0c364f90bf471d7c6f5db49564c33e4235d268569ac71"
@ -38,309 +44,87 @@ static struct UTUN_INSTANCE* inst_a = NULL;
static struct UTUN_INSTANCE* inst_b = NULL;
static struct UTUN_INSTANCE* inst_c = NULL;
static struct UASYNC* ua = NULL;
static int test_phase = 0;
static int test_phase = 0; // 0=running, 1=success, -1=fail
static void* timeout_id = NULL;
static int connections_established = 0;
// Helper: fill sockaddr_storage from IP:port string
static int make_sockaddr(const char* ip_port, struct sockaddr_storage* ss) {
char buf[64];
strncpy(buf, ip_port, sizeof(buf)-1);
buf[sizeof(buf)-1] = '\0';
char* colon = strrchr(buf, ':');
if (!colon) return -1;
*colon = '\0';
int port = atoi(colon + 1);
struct sockaddr_in* sin = (struct sockaddr_in*)ss;
sin->sin_family = AF_INET;
sin->sin_port = htons(port);
if (inet_pton(AF_INET, buf, &sin->sin_addr) != 1) return -1;
return 0;
}
// Helper: create CFG_SERVER
static struct CFG_SERVER* create_server(const char* name, const char* ip_port, uint8_t type) {
struct CFG_SERVER* srv = u_calloc(1, sizeof(struct CFG_SERVER));
if (!srv) return NULL;
strncpy(srv->name, name, MAX_CONN_NAME_LEN-1);
if (make_sockaddr(ip_port, &srv->ip) < 0) {
u_free(srv);
return NULL;
}
srv->type = type;
return srv;
static void on_connection_ready(struct ETCP_CONN* conn, void* arg) {
(void)arg;
connections_established++;
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "[MESH] Connection ready: %s -> %016llx",
conn->log_name, (unsigned long long)conn->peer_node_id);
}
// Helper: create CFG_CLIENT_LINK
static struct CFG_CLIENT_LINK* create_link(struct CFG_SERVER* local_srv, const char* remote_ip_port) {
struct CFG_CLIENT_LINK* link = u_calloc(1, sizeof(struct CFG_CLIENT_LINK));
if (!link) return NULL;
link->local_srv = local_srv;
strncpy(link->server_name, local_srv->name, MAX_CONN_NAME_LEN-1);
if (make_sockaddr(remote_ip_port, &link->remote_addr) < 0) {
u_free(link);
return NULL;
}
return link;
}
static void check_mesh_state(void* arg) {
(void)arg;
// Helper: create CFG_CLIENT
static struct CFG_CLIENT* create_client(const char* name, const char* peer_key, int keepalive) {
struct CFG_CLIENT* cli = u_calloc(1, sizeof(struct CFG_CLIENT));
if (!cli) return NULL;
strncpy(cli->name, name, MAX_CONN_NAME_LEN-1);
strncpy(cli->peer_public_key_hex, peer_key, MAX_KEY_LEN-1);
cli->keepalive = keepalive;
return cli;
}
int a_links = 0, b_links = 0, c_links = 0;
// Helper: add link to client
static void client_add_link(struct CFG_CLIENT* cli, struct CFG_CLIENT_LINK* link) {
link->next = cli->links;
cli->links = link;
if (inst_a && inst_a->connections) {
struct ETCP_CONN* c = inst_a->connections;
while (c) {
struct ETCP_LINK* l = c->links;
while (l) {
if (l->initialized) a_links++;
l = l->next;
}
// Helper: add server to list (at end to preserve order)
static void add_server(struct utun_config* cfg, struct CFG_SERVER* srv) {
if (!cfg->servers) {
cfg->servers = srv;
} else {
struct CFG_SERVER* tail = cfg->servers;
while (tail->next) tail = tail->next;
tail->next = srv;
c = c->next;
}
}
// Helper: find server by name
static struct CFG_SERVER* find_server(struct utun_config* cfg, const char* name) {
struct CFG_SERVER* srv = cfg->servers;
while (srv) {
if (strcmp(srv->name, name) == 0) return srv;
srv = srv->next;
if (inst_b && inst_b->connections) {
struct ETCP_CONN* c = inst_b->connections;
while (c) {
struct ETCP_LINK* l = c->links;
while (l) {
if (l->initialized) b_links++;
l = l->next;
}
return NULL;
c = c->next;
}
// Helper: add client to list
static void add_client(struct utun_config* cfg, struct CFG_CLIENT* cli) {
cli->next = cfg->clients;
cfg->clients = cli;
}
// Helper: add subnet
static void add_subnet(struct CFG_ROUTE_ENTRY** list, const char* cidr) {
struct CFG_ROUTE_ENTRY* entry = u_calloc(1, sizeof(struct CFG_ROUTE_ENTRY));
if (!entry) return;
char buf[32];
strncpy(buf, cidr, sizeof(buf)-1);
char* slash = strchr(buf, '/');
if (slash) {
*slash = '\0';
entry->netmask = atoi(slash + 1);
if (inst_c && inst_c->connections) {
struct ETCP_CONN* c = inst_c->connections;
while (c) {
struct ETCP_LINK* l = c->links;
while (l) {
if (l->initialized) c_links++;
l = l->next;
}
entry->ip.family = AF_INET;
inet_pton(AF_INET, buf, &entry->ip.addr.v4);
entry->next = *list;
*list = entry;
c = c->next;
}
// Create config for instance A
static struct utun_config* create_config_a(const char* key_c_pub) {
struct utun_config* cfg = u_calloc(1, sizeof(struct utun_config));
if (!cfg) return NULL;
// Global settings
strncpy(cfg->global.my_private_key_hex, KEY_A_PRIV, MAX_KEY_LEN-1);
strncpy(cfg->global.my_public_key_hex, KEY_A_PUB, MAX_KEY_LEN-1);
cfg->global.my_node_id = 0xAAAAAAAAAAAAAAAAULL;
strncpy(cfg->global.tun_ifname, "tun_a", 15);
inet_pton(AF_INET, "10.99.1.1", &cfg->global.tun_ip.addr.v4);
cfg->global.tun_ip.family = AF_INET;
cfg->global.tun_test_mode = 1;
cfg->global.mtu = 1500;
// Servers (for others to connect to) - unique ports
add_server(cfg, create_server("b", "127.0.0.1:9101", CFG_SERVER_TYPE_PUBLIC));
add_server(cfg, create_server("c", "127.0.0.1:9102", CFG_SERVER_TYPE_PUBLIC));
// Clients (to connect to others) - unique ports
struct CFG_CLIENT* cli_b = create_client("to_b", KEY_B_PUB, 1);
client_add_link(cli_b, create_link(find_server(cfg, "b"), "127.0.0.1:9201"));
add_client(cfg, cli_b);
struct CFG_CLIENT* cli_c = create_client("to_c", key_c_pub, 1);
client_add_link(cli_c, create_link(find_server(cfg, "c"), "127.0.0.1:9302"));
add_client(cfg, cli_c);
// Subnets
add_subnet(&cfg->my_subnets, "192.168.10.0/24");
add_subnet(&cfg->my_subnets, "192.168.11.0/24");
return cfg;
}
// Create config for instance B
static struct utun_config* create_config_b(const char* key_c_pub) {
struct utun_config* cfg = u_calloc(1, sizeof(struct utun_config));
if (!cfg) return NULL;
// Global settings
strncpy(cfg->global.my_private_key_hex, KEY_B_PRIV, MAX_KEY_LEN-1);
strncpy(cfg->global.my_public_key_hex, KEY_B_PUB, MAX_KEY_LEN-1);
cfg->global.my_node_id = 0xBBBBBBBBBBBBBBBBULL;
strncpy(cfg->global.tun_ifname, "tun_b", 15);
inet_pton(AF_INET, "10.99.2.1", &cfg->global.tun_ip.addr.v4);
cfg->global.tun_ip.family = AF_INET;
cfg->global.tun_test_mode = 1;
cfg->global.mtu = 1500;
// Servers - unique ports
add_server(cfg, create_server("a", "127.0.0.1:9201", CFG_SERVER_TYPE_PUBLIC));
add_server(cfg, create_server("c", "127.0.0.1:9202", CFG_SERVER_TYPE_PUBLIC));
// Clients - unique ports
struct CFG_CLIENT* cli_a = create_client("to_a", KEY_A_PUB, 1);
client_add_link(cli_a, create_link(find_server(cfg, "a"), "127.0.0.1:9101"));
add_client(cfg, cli_a);
struct CFG_CLIENT* cli_c = create_client("to_c", key_c_pub, 1);
client_add_link(cli_c, create_link(find_server(cfg, "c"), "127.0.0.1:9303"));
add_client(cfg, cli_c);
// Subnets
add_subnet(&cfg->my_subnets, "192.168.20.0/24");
add_subnet(&cfg->my_subnets, "192.168.21.0/24");
return cfg;
}
// Create config for instance C
static struct utun_config* create_config_c(const char* key_c_priv, const char* key_c_pub) {
struct utun_config* cfg = u_calloc(1, sizeof(struct utun_config));
if (!cfg) return NULL;
// Global settings
strncpy(cfg->global.my_private_key_hex, key_c_priv, MAX_KEY_LEN-1);
strncpy(cfg->global.my_public_key_hex, key_c_pub, MAX_KEY_LEN-1);
cfg->global.my_node_id = 0xCCCCCCCCCCCCCCCCULL;
strncpy(cfg->global.tun_ifname, "tun_c", 15);
inet_pton(AF_INET, "10.99.3.1", &cfg->global.tun_ip.addr.v4);
cfg->global.tun_ip.family = AF_INET;
cfg->global.tun_test_mode = 1;
cfg->global.mtu = 1500;
// Servers - unique ports
add_server(cfg, create_server("a", "127.0.0.1:9302", CFG_SERVER_TYPE_PUBLIC));
add_server(cfg, create_server("b", "127.0.0.1:9303", CFG_SERVER_TYPE_PUBLIC));
// Clients - unique ports
struct CFG_CLIENT* cli_a = create_client("to_a", KEY_A_PUB, 1);
client_add_link(cli_a, create_link(find_server(cfg, "a"), "127.0.0.1:9102"));
add_client(cfg, cli_a);
struct CFG_CLIENT* cli_b = create_client("to_b", KEY_B_PUB, 1);
client_add_link(cli_b, create_link(find_server(cfg, "b"), "127.0.0.1:9202"));
add_client(cfg, cli_b);
// Subnets
add_subnet(&cfg->my_subnets, "192.168.30.0/24");
add_subnet(&cfg->my_subnets, "192.168.31.0/24");
return cfg;
}
// Free config (simplified - just free top level, rest will be cleaned on exit)
static void free_config_manual(struct utun_config* cfg) {
(void)cfg; // Config structures cleaned up by utun_instance_destroy
// Note: utun_instance_create_from_config makes copies of config data,
// so we don't need to keep the config structures after instance creation
}
printf("[MESH] Links: A=%d, B=%d, C=%d | Total established: %d\n",
a_links, b_links, c_links, connections_established);
static int count_initialized_links(struct UTUN_INSTANCE* inst) {
int count = 0;
struct ETCP_CONN* conn = inst->connections;
while (conn) {
struct ETCP_LINK* link = conn->links;
while (link) {
if (link->initialized) count++;
link = link->next;
}
conn = conn->next;
}
return count;
}
static void check_connections(void* arg) {
(void)arg;
int a = count_initialized_links(inst_a);
int b = count_initialized_links(inst_b);
int c = count_initialized_links(inst_c);
printf("[CONNECT] Links ready: A=%d, B=%d, C=%d (need 2 each)\n", a, b, c);
if (a >= 2 && b >= 2 && c >= 2) {
printf("\n[SUCCESS] All connections established!\n");
if (a_links >= 2 && b_links >= 2 && c_links >= 2 && connections_established >= 6) {
printf("\n[SUCCESS] Mesh topology established successfully!\n");
test_phase = 1;
return;
}
timeout_id = uasync_set_timeout(ua, 100, NULL, check_connections);
timeout_id = uasync_set_timeout(ua, 500, NULL, check_mesh_state);
}
static void timeout_handler(void* arg) {
(void)arg;
printf("\n[TIMEOUT] Test failed\n");
printf("\n[TIMEOUT] Mesh test failed to establish all connections\n");
test_phase = -1;
}
int main(void) {
printf("========================================\n");
printf(" Routing Mesh Test (3 instances)\n");
printf(" (programmatic config, no temp files)\n");
printf(" Full stack: normalizer + ETCP + routing\n");
printf("========================================\n\n");
// Generate keys for C
struct SC_MYKEYS keys_c;
if (sc_generate_keypair(&keys_c) != SC_OK) {
fprintf(stderr, "Failed to generate keys for C\n");
return 1;
}
// Convert to hex
char key_c_priv[65], key_c_pub[129];
const char* hex_chars = "0123456789abcdef";
for (int i = 0; i < 32; i++) {
key_c_priv[i*2] = hex_chars[(keys_c.private_key[i] >> 4) & 0xF];
key_c_priv[i*2+1] = hex_chars[keys_c.private_key[i] & 0xF];
}
key_c_priv[64] = '\0';
for (int i = 0; i < 64; i++) {
key_c_pub[i*2] = hex_chars[(keys_c.public_key[i] >> 4) & 0xF];
key_c_pub[i*2+1] = hex_chars[keys_c.public_key[i] & 0xF];
}
key_c_pub[128] = '\0';
printf("Generated C keys:\n");
printf(" priv: %.16s...\n", key_c_priv);
printf(" pub: %.16s...\n\n", key_c_pub);
// Create configs programmatically
struct utun_config* cfg_a = create_config_a(key_c_pub);
struct utun_config* cfg_b = create_config_b(key_c_pub);
struct utun_config* cfg_c = create_config_c(key_c_priv, key_c_pub);
if (!cfg_a || !cfg_b || !cfg_c) {
fprintf(stderr, "Failed to create configs\n");
return 1;
}
debug_config_init();
debug_set_level(DEBUG_LEVEL_WARN);
debug_set_level(DEBUG_LEVEL_INFO);
debug_set_categories(DEBUG_CATEGORY_ETCP | DEBUG_CATEGORY_CONNECTION | DEBUG_CATEGORY_ROUTING);
utun_instance_set_tun_init_enabled(0);
ua = uasync_create();
if (!ua) {
@ -348,58 +132,10 @@ int main(void) {
return 1;
}
printf("[INIT] Creating instances from programmatic config...\n");
inst_a = utun_instance_create_from_config(ua, cfg_a);
inst_b = utun_instance_create_from_config(ua, cfg_b);
inst_c = utun_instance_create_from_config(ua, cfg_c);
if (!inst_a || !inst_b || !inst_c) {
fprintf(stderr, "Failed to create instances\n");
return 1;
}
printf(" Instance A: node_id=%016llX\n", (unsigned long long)inst_a->node_id);
printf(" Instance B: node_id=%016llX\n", (unsigned long long)inst_b->node_id);
printf(" Instance C: node_id=%016llX\n", (unsigned long long)inst_c->node_id);
printf("\n[INIT] Initializing connections...\n");
if (init_connections(inst_a) < 0 ||
init_connections(inst_b) < 0 ||
init_connections(inst_c) < 0) {
fprintf(stderr, "Failed to initialize connections\n");
return 1;
}
printf(" Connections initialized\n");
printf("\n[PHASE] Waiting for connections to establish...\n");
timeout_id = uasync_set_timeout(ua, TEST_TIMEOUT_MS, NULL, timeout_handler);
check_connections(NULL);
int polls = 0;
while (test_phase == 0 && polls < 500) {
uasync_poll(ua, 20);
polls++;
}
printf("Test architecture prepared. Running full mesh test...\n");
printf("Note: Full rewrite in progress. Current version is skeleton.\n");
// Cleanup
if (inst_a) utun_instance_destroy(inst_a);
if (inst_b) utun_instance_destroy(inst_b);
if (inst_c) utun_instance_destroy(inst_c);
if (ua) uasync_destroy(ua, 0);
// Free configs
free_config_manual(cfg_a);
free_config_manual(cfg_b);
free_config_manual(cfg_c);
if (test_phase == 1) {
printf("\n✓ TEST PASSED\n");
return 0;
} else {
printf("\n✗ TEST FAILED\n");
return 1;
}
}
Loading…
Cancel
Save