You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
113 lines
4.1 KiB
113 lines
4.1 KiB
#include <stdio.h> |
|
#include <stdlib.h> |
|
#include <string.h> |
|
#include <unistd.h> |
|
#include <sys/socket.h> |
|
#include <netinet/in.h> |
|
#include <arpa/inet.h> |
|
#include <time.h> |
|
|
|
#include "../src/etcp.h" |
|
#include "../src/etcp_connections.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" |
|
|
|
#define TEST_TIMEOUT_MS 5000 |
|
|
|
static struct UTUN_INSTANCE* server_instance = NULL; |
|
static struct UTUN_INSTANCE* client_instance = NULL; |
|
static int test_completed = 0; |
|
|
|
static void monitor_connections(void* arg) { |
|
(void)arg; |
|
|
|
if (test_completed) return; |
|
|
|
int client_initialized = 0; |
|
|
|
// Check client |
|
if (client_instance) { |
|
struct ETCP_CONN* conn = client_instance->connections; |
|
while (conn) { |
|
struct ETCP_LINK* link = conn->links; |
|
while (link) { |
|
if (link->is_server == 0) { // client link |
|
printf("[CLIENT] Link: peer=%llx init=%d retry=%d\n", |
|
(unsigned long long)conn->peer_node_id, |
|
link->initialized, |
|
link->init_retry_count); |
|
if (link->initialized) { |
|
client_initialized = 1; |
|
} |
|
} |
|
link = link->next; |
|
} |
|
conn = conn->next; |
|
} |
|
} |
|
|
|
if (client_initialized) { |
|
printf("=== SUCCESS ===\n"); |
|
test_completed = 1; |
|
return; |
|
} |
|
|
|
if (!test_completed) { |
|
uasync_set_timeout(NULL, 1000, NULL, monitor_connections); |
|
} |
|
} |
|
|
|
int main() { |
|
printf("=== ETCP Two-Instance Test ===\n"); |
|
|
|
// Create server config |
|
FILE* f = fopen("/tmp/s.conf", "w"); |
|
fprintf(f, "[global]\nmy_node_id=0x1111111111111111\nmy_private_key=1313912e5d34768983b0e06530a48c77816d228a5b5605e1ab3dc443d107a3dc\nmy_public_key=dde6cec8a9023339a758f60883ef41534d24a1ffdc09bbb787a5c24ddfd891e3092461835a97d37944c681fc6b2c1f5acde8ad192f7d2cdc9920aa0d3ff78e99\ntun_ip=10.99.0.1/24\ntun_ifname=tun99\n\n[server:test]\naddr=127.0.0.1:9001\ntype=public\n"); |
|
fclose(f); |
|
|
|
// Create client config |
|
// ВАЖНО: Использовать фиксированный порт, случайные порты (0) не поддерживаются |
|
// Клиентский сервер должен использовать другой порт, чем удаленный сервер |
|
f = fopen("/tmp/c.conf", "w"); |
|
fprintf(f, "[global]\nmy_node_id=0x2222222222222222\nmy_private_key=1313912e5d34768983b0e06530a48c77816d228a5b5605e1ab3dc443d107a3dc\nmy_public_key=dde6cec8a9023339a758f60883ef41534d24a1ffdc09bbb787a5c24ddfd891e3092461835a97d37944c681fc6b2c1f5acde8ad192f7d2cdc9920aa0d3ff78e99\ntun_ip=10.99.0.2/24\ntun_ifname=tun98\n\n[server:local]\naddr=127.0.0.1:9002\ntype=nat\n\n[client:test]\npeer_node_id=0x1111111111111111\nkeepalive=1\nlink=local:127.0.0.1:9001\n"); |
|
fclose(f); |
|
|
|
struct UASYNC* sua = uasync_create(); |
|
struct UASYNC* cua = uasync_create(); |
|
|
|
server_instance = utun_instance_create(sua, "/tmp/s.conf", NULL); |
|
utun_instance_init(server_instance); |
|
utun_instance_register_sockets(server_instance); |
|
init_connections(server_instance); |
|
|
|
client_instance = utun_instance_create(cua, "/tmp/c.conf", NULL); |
|
utun_instance_init(client_instance); |
|
utun_instance_register_sockets(client_instance); |
|
init_connections(client_instance); |
|
|
|
printf("✓ Both instances initialized\n"); |
|
printf("✓ Server listening on 127.0.0.1:9001\n"); |
|
printf("✓ Client local server on 127.0.0.1:9002\n"); |
|
printf("✓ Client connecting to server at 127.0.0.1:9001\n"); |
|
|
|
uasync_set_timeout(sua, 1000, NULL, monitor_connections); |
|
|
|
int elapsed = 0; |
|
while (!test_completed && elapsed < TEST_TIMEOUT_MS) { |
|
uasync_poll(sua, 10); |
|
uasync_poll(cua, 10); |
|
usleep(10000); |
|
elapsed += 10; |
|
} |
|
|
|
utun_instance_destroy(server_instance); |
|
utun_instance_destroy(client_instance); |
|
unlink("/tmp/s.conf"); |
|
unlink("/tmp/c.conf"); |
|
|
|
return test_completed ? 0 : 1; |
|
} |