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.
175 lines
6.9 KiB
175 lines
6.9 KiB
// test_icmp_proxy.c — 2-node ICMP ping test via etcp_router |
|
// Node1 (client): tcp_proxy_client → sends ICMP_REQUEST to exit |
|
// Node2 (exit): receives → raw socket → sends echo → receives reply → sends ICMP_REPLY |
|
#include <stdio.h> |
|
#include <stdlib.h> |
|
#include <string.h> |
|
#include "../lib/platform_compat.h" |
|
#include "test_utils.h" |
|
#ifndef _WIN32 |
|
#include <unistd.h> |
|
#include <sys/socket.h> |
|
#include <netinet/in.h> |
|
#include <netinet/ip.h> |
|
#include <netinet/ip_icmp.h> |
|
#include <arpa/inet.h> |
|
#include <sys/stat.h> |
|
#endif |
|
#include <time.h> |
|
|
|
#include "../src/etcp.h" |
|
#include "../src/etcp_connections.h" |
|
#include "../src/etcp_api.h" |
|
#include "../src/etcp_router.h" |
|
#include "../src/proxy/icmp_proxy.h" |
|
#include "../src/proxy/tcp_proxy_server.h" |
|
#include "../src/config_parser.h" |
|
#include "../src/utun_instance.h" |
|
#include "../src/routing.h" |
|
#include "../src/tun_if.h" |
|
#include "../lib/u_async.h" |
|
#include "../lib/ll_queue.h" |
|
#include "../lib/debug_config.h" |
|
#include "../lib/mem.h" |
|
|
|
#define TEST_TIMEOUT_MS 5000 |
|
#define ICMP_PAYLOAD_SIZE 56 // typical ping payload |
|
|
|
// Config strings |
|
static const char* cfg_node_client(void) { |
|
static char buf[1024]; |
|
snprintf(buf, sizeof(buf), |
|
"[global]\n" |
|
"my_node_id=0xEEEE000000000001\n" |
|
"my_private_key=67b705a92b41bcaae105af2d6a17743faa7b26ccebba8b3b9b0af05e9cd1d5fb\n" |
|
"my_public_key=1c55e4ccae7c4470707759086738b10681bf88b81f198cc2ab54a647d1556e17c65e6b1833e0c771e5a39382c03067c388915a4c732191bc130480f20f8e00b9\n" |
|
"tun_ip=10.99.0.1/24\n" |
|
"tun_ifname=tun99\n" |
|
"[server: s1]\naddr=127.0.0.1:9081\ntype=public\n" |
|
"[client: c1]\nkeepalive=1\nlink=s1:127.0.0.1:9082\n" |
|
"peer_public_key=c594f33c91f3a2222795c2c110c527bf214ad1009197ce14556cb13df3c461b3c373bed8f205a8dd1fc0c364f90bf471d7c6f5db49564c33e4235d268569ac71\n" |
|
"[tcp_proxy_client]\n" |
|
"enabled=yes\n" |
|
"tun_name=tun_tcp\n" |
|
"tun_ip=10.99.0.1\n" |
|
"via_node=0xEEEE000000000002\n"); |
|
return buf; |
|
} |
|
|
|
static const char* cfg_node_exit(void) { |
|
static char buf[1024]; |
|
snprintf(buf, sizeof(buf), |
|
"[global]\n" |
|
"my_node_id=0xEEEE000000000002\n" |
|
"my_private_key=4813d31d28b7e9829247f488c6be7672f2bdf61b2508333128e386d1759afed2\n" |
|
"my_public_key=c594f33c91f3a2222795c2c110c527bf214ad1009197ce14556cb13df3c461b3c373bed8f205a8dd1fc0c364f90bf471d7c6f5db49564c33e4235d268569ac71\n" |
|
"tun_ip=10.99.0.2/24\n" |
|
"tun_ifname=tun98\n" |
|
"[server: s1]\naddr=127.0.0.1:9082\ntype=public\n" |
|
"[allowed_keys]\nallow_all=1\n" |
|
"[tcp_proxy_server]\nenabled=yes\n"); |
|
return buf; |
|
} |
|
|
|
static struct UTUN_INSTANCE* cli = NULL; |
|
static struct UTUN_INSTANCE* exit_node = NULL; |
|
static struct UASYNC* ua = NULL; |
|
static int g_ok = 0, g_done = 0, g_test_phase = 0; |
|
static uint64_t exit_node_id = 0xEEEE000000000002ULL; |
|
static uint64_t client_node_id = 0xEEEE000000000001ULL; |
|
static uint16_t test_echo_id = 0x1234, test_echo_seq = 0x0001; |
|
static uint8_t send_buf[ICMP_PAYLOAD_SIZE]; |
|
static int reply_rcvd = 0; |
|
static void* g_to_id = NULL; |
|
|
|
static void cli_recv_cb(struct ETCP_CONN* conn, struct ll_entry* entry) { |
|
(void)conn; |
|
if (!entry || !entry->dgram || entry->len < ICMP_PROXY_HDR_SIZE + 1) { |
|
if (entry) { queue_entry_free(entry); queue_dgram_free(entry); } return; |
|
} |
|
if (entry->dgram[1] == ICMP_PROXY_SUBCMD_REPLY) { |
|
uint16_t rid, rseq; |
|
memcpy(&rid, entry->dgram + 18, 2); |
|
memcpy(&rseq, entry->dgram + 20, 2); |
|
if (rid == test_echo_id && rseq == test_echo_seq) { |
|
size_t payload_len = entry->len - ICMP_PROXY_HDR_SIZE; |
|
uint8_t* payload = entry->dgram + ICMP_PROXY_HDR_SIZE; |
|
if (payload_len == ICMP_PAYLOAD_SIZE && memcmp(payload, send_buf, ICMP_PAYLOAD_SIZE) == 0) { |
|
reply_rcvd = 1; g_done = 1; g_ok = 1; |
|
} else { |
|
printf("[FAIL] payload mismatch: got %zu expected %d\n", payload_len, ICMP_PAYLOAD_SIZE); |
|
g_done = -1; |
|
} |
|
} else { |
|
printf("[FAIL] id/seq mismatch: got id=0x%04x seq=0x%04x\n", rid, rseq); |
|
g_done = -1; |
|
} |
|
} |
|
queue_entry_free(entry); queue_dgram_free(entry); |
|
} |
|
|
|
static void monitor(void* arg) { |
|
(void)arg; |
|
if (g_done) return; |
|
|
|
if (g_test_phase == 0) { |
|
int cli_ok = 0, exit_ok = 0; |
|
for (struct ETCP_CONN* c = cli->connections; c; c = c->next) |
|
for (struct ETCP_LINK* l = c->links; l; l = l->next) |
|
if (l->initialized && c->crypto_ctx.initialized) cli_ok = 1; |
|
for (struct ETCP_CONN* c = exit_node->connections; c; c = c->next) |
|
for (struct ETCP_LINK* l = c->links; l; l = l->next) |
|
if (l->initialized && c->crypto_ctx.initialized) exit_ok = 1; |
|
if (cli_ok && exit_ok) { |
|
g_test_phase = 1; |
|
// Override client handler for test verification |
|
etcp_router_bind(cli, ETCP_ID_ICMP_PROXY, cli_recv_cb); |
|
for (int i = 0; i < ICMP_PAYLOAD_SIZE; i++) send_buf[i] = (uint8_t)(rand() & 0xFF); |
|
icmp_proxy_send_to_exit(cli, exit_node_id, |
|
inet_addr("127.0.0.1"), inet_addr("127.0.0.100"), test_echo_id, test_echo_seq, send_buf, ICMP_PAYLOAD_SIZE); |
|
} |
|
} |
|
if (!g_done) g_to_id = uasync_set_timeout(ua, 100, NULL, monitor, "mon"); |
|
} |
|
|
|
static void test_timeout(void* arg) { |
|
(void)arg; |
|
if (!g_done) { printf("[FAIL] timeout (phase=%d reply=%d)\n", g_test_phase, reply_rcvd); g_done = -1; } |
|
} |
|
|
|
int main(void) { |
|
printf("=== test_icmp_proxy ===\n"); |
|
debug_config_init(); debug_set_level(DEBUG_LEVEL_ERROR); debug_set_categories(DEBUG_CATEGORY_ALL); |
|
utun_instance_set_tun_init_enabled(0); |
|
srand((unsigned)time(NULL)); |
|
|
|
ua = uasync_create(); |
|
if (!ua) { printf("[FAIL] uasync_create\n"); return 1; } |
|
|
|
cli = utun_instance_create_from_str(ua, cfg_node_client()); |
|
if (!cli) { printf("[FAIL] client instance create\n"); goto done; } |
|
exit_node = utun_instance_create_from_str(ua, cfg_node_exit()); |
|
if (!exit_node) { printf("[FAIL] exit instance create\n"); goto done; } |
|
|
|
if (utun_instance_init(cli) < 0) { printf("[FAIL] client init\n"); goto done; } |
|
if (utun_instance_init(exit_node) < 0) { printf("[FAIL] exit init\n"); goto done; } |
|
|
|
icmp_proxy_set_test_loopback(exit_node, 1); |
|
|
|
g_to_id = uasync_set_timeout(ua, 100, NULL, monitor, "mon"); |
|
void* to_id = uasync_set_timeout(ua, TEST_TIMEOUT_MS * 10, NULL, test_timeout, "to"); |
|
|
|
while (!g_done) uasync_poll(ua, 50); |
|
|
|
if (to_id) uasync_cancel_timeout(ua, to_id); |
|
|
|
if (g_ok) printf("[PASS] test_icmp_proxy — ping echoed, id=0x%04x seq=%d\n", test_echo_id, test_echo_seq); |
|
else printf("[FAIL] test_icmp_proxy\n"); |
|
|
|
done: |
|
if (g_to_id) uasync_cancel_timeout(ua, g_to_id); |
|
if (cli) { cli->running = 0; utun_instance_destroy(cli); } |
|
if (exit_node) { exit_node->running = 0; utun_instance_destroy(exit_node); } |
|
if (ua) uasync_destroy(ua, 0); |
|
return g_ok ? 0 : 1; |
|
}
|
|
|