|
|
#include <stdio.h> |
|
|
#include <stdlib.h> |
|
|
#include <string.h> |
|
|
#include <unistd.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" |
|
|
#include "../lib/ll_queue.h" |
|
|
#include "../lib/debug_config.h" |
|
|
|
|
|
#define TEST_TIMEOUT_MS 5000 // 5 seconds for packet transmission |
|
|
#define PACKET_SIZE 100 // Test packet size |
|
|
|
|
|
static struct UTUN_INSTANCE* server_instance = NULL; |
|
|
static struct UTUN_INSTANCE* client_instance = NULL; |
|
|
static int test_completed = 0; // 0 = running, 1 = success, 2 = timeout/failure |
|
|
static void* packet_timeout_id = NULL; |
|
|
|
|
|
// Test packet data |
|
|
static uint8_t test_packet_data[PACKET_SIZE]; |
|
|
static int packet_sent = 0; |
|
|
static int packet_received = 0; |
|
|
|
|
|
// Function to check if connection is established |
|
|
static int is_connection_established(struct UTUN_INSTANCE* inst) { |
|
|
if (!inst) return 0; |
|
|
|
|
|
struct ETCP_CONN* conn = inst->connections; |
|
|
while (conn) { |
|
|
struct ETCP_LINK* link = conn->links; |
|
|
while (link) { |
|
|
if (link->initialized) { |
|
|
return 1; |
|
|
} |
|
|
link = link->next; |
|
|
} |
|
|
conn = conn->next; |
|
|
} |
|
|
return 0; |
|
|
} |
|
|
|
|
|
// Function to send test packet to client's input queue |
|
|
static void send_test_packet(void) { |
|
|
if (!client_instance || packet_sent) return; |
|
|
|
|
|
struct ETCP_CONN* conn = client_instance->connections; |
|
|
if (!conn) { |
|
|
printf("No ETCP connection found on client\n"); |
|
|
return; |
|
|
} |
|
|
|
|
|
if (!conn->input_queue) { |
|
|
printf("Client input queue is NULL\n"); |
|
|
return; |
|
|
} |
|
|
|
|
|
// Create test packet data |
|
|
for (int i = 0; i < PACKET_SIZE; i++) { |
|
|
test_packet_data[i] = (uint8_t)(i % 256); |
|
|
} |
|
|
|
|
|
// Create queue data |
|
|
void* data = queue_data_new(PACKET_SIZE); |
|
|
if (!data) { |
|
|
printf("Failed to create queue data\n"); |
|
|
return; |
|
|
} |
|
|
|
|
|
// Copy test data |
|
|
memcpy(data, test_packet_data, PACKET_SIZE); |
|
|
|
|
|
// Put data into input queue |
|
|
if (queue_data_put(conn->input_queue, data, 0) < 0) { |
|
|
printf("Failed to put packet into input queue\n"); |
|
|
queue_data_free(data); |
|
|
return; |
|
|
} |
|
|
|
|
|
packet_sent = 1; |
|
|
printf("Test packet sent to client input queue (%d bytes)\n", PACKET_SIZE); |
|
|
} |
|
|
|
|
|
// Function to check if packet received in server's output queue |
|
|
static void check_packet_received(void) { |
|
|
if (!server_instance || packet_received) return; |
|
|
|
|
|
struct ETCP_CONN* conn = server_instance->connections; |
|
|
if (!conn) return; |
|
|
|
|
|
if (!conn->output_queue) return; |
|
|
|
|
|
// Check if there's any packet in output queue |
|
|
void* data = queue_data_get(conn->output_queue); |
|
|
if (data) { |
|
|
// В новом API размер данных нужно получать из структуры |
|
|
struct ll_entry* entry = (struct ll_entry*)data - 1; |
|
|
size_t size = entry->size; |
|
|
|
|
|
if (size == PACKET_SIZE && memcmp(data, test_packet_data, PACKET_SIZE) == 0) { |
|
|
packet_received = 1; |
|
|
printf("Packet received in server output queue (%zu bytes), data matches\n", size); |
|
|
} else { |
|
|
printf("Packet received but size mismatch or data differs (expected %d, got %zu)\n", PACKET_SIZE, size); |
|
|
} |
|
|
queue_data_free(data); |
|
|
} |
|
|
} |
|
|
|
|
|
// Monitor connection and send packet when ready |
|
|
static void monitor_and_send(void* arg) { |
|
|
(void)arg; |
|
|
|
|
|
if (test_completed) { |
|
|
packet_timeout_id = NULL; |
|
|
return; |
|
|
} |
|
|
|
|
|
static int connection_checked = 0; |
|
|
static int packet_sent_flag = 0; |
|
|
|
|
|
// Check connection |
|
|
if (!connection_checked) { |
|
|
int server_ready = is_connection_established(server_instance); |
|
|
int client_ready = is_connection_established(client_instance); |
|
|
|
|
|
// We only need client link initialized to send packet |
|
|
// Server will accept packets via its socket |
|
|
if (client_ready) { |
|
|
printf("Client link initialized, ready to send packet (server=%d, client=%d)\n", server_ready, client_ready); |
|
|
connection_checked = 1; |
|
|
} else { |
|
|
printf("Waiting for connection... (server=%d, client=%d)\n", server_ready, client_ready); |
|
|
} |
|
|
} |
|
|
|
|
|
// Send packet once connection is ready |
|
|
if (connection_checked && !packet_sent_flag) { |
|
|
send_test_packet(); |
|
|
packet_sent_flag = 1; |
|
|
} |
|
|
|
|
|
// Check for received packet |
|
|
if (packet_sent_flag) { |
|
|
check_packet_received(); |
|
|
if (packet_received) { |
|
|
test_completed = 1; // Success |
|
|
printf("\n=== SUCCESS: Packet transmitted successfully! ===\n"); |
|
|
if (packet_timeout_id) { |
|
|
uasync_cancel_timeout(server_instance->ua, packet_timeout_id); |
|
|
packet_timeout_id = NULL; |
|
|
} |
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
// Schedule next check |
|
|
if (!test_completed) { |
|
|
packet_timeout_id = uasync_set_timeout(server_instance->ua, 100, NULL, monitor_and_send); |
|
|
} |
|
|
} |
|
|
|
|
|
// Timeout handler |
|
|
static void test_timeout(void* arg) { |
|
|
(void)arg; |
|
|
if (!test_completed) { |
|
|
printf("\n=== TIMEOUT: Packet not received within %d seconds ===\n", TEST_TIMEOUT_MS/1000); |
|
|
test_completed = 2; // Timeout/failure |
|
|
if (packet_timeout_id) { |
|
|
uasync_cancel_timeout(server_instance->ua, packet_timeout_id); |
|
|
packet_timeout_id = NULL; |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
int main() { |
|
|
printf("=== ETCP Simple Traffic Test (Queue-based) ===\n\n"); |
|
|
|
|
|
// Enable debug output |
|
|
debug_config_init(); |
|
|
debug_set_level(DEBUG_LEVEL_TRACE); |
|
|
debug_set_categories(DEBUG_CATEGORY_ALL); |
|
|
debug_enable_timestamp(1); |
|
|
debug_enable_function_name(1); |
|
|
|
|
|
// Explicitly disable TUN initialization for this test |
|
|
utun_instance_set_tun_init_enabled(0); |
|
|
|
|
|
// Create server instance |
|
|
printf("Creating server instance...\n"); |
|
|
struct UASYNC* server_ua = uasync_create(); |
|
|
server_instance = utun_instance_create(server_ua, "test_server.conf", NULL); |
|
|
if (!server_instance) { |
|
|
printf("Failed to create server instance\n"); |
|
|
return 1; |
|
|
} |
|
|
|
|
|
// Initialize connections and register sockets regardless of TUN state |
|
|
if (server_instance->tun.fd < 0) { |
|
|
printf("ℹ️ Server TUN disabled - initializing connections only\n"); |
|
|
} |
|
|
|
|
|
// Initialize ETCP connections (creates sockets and links) |
|
|
if (init_connections(server_instance) < 0) { |
|
|
printf("Failed to initialize server connections\n"); |
|
|
utun_instance_destroy(server_instance); |
|
|
return 1; |
|
|
} else { |
|
|
printf("Server connections initialized: sockets=%p, connections=%p\n", |
|
|
server_instance->etcp_sockets, server_instance->connections); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
printf("✅ Server instance initialized successfully (node_id=%llx)\n", (unsigned long long)server_instance->node_id); |
|
|
printf("Server instance ready (node_id=%llx)\n\n", (unsigned long long)server_instance->node_id); |
|
|
|
|
|
// Create client instance |
|
|
printf("Creating client instance...\n"); |
|
|
struct UASYNC* client_ua = uasync_create(); |
|
|
client_instance = utun_instance_create(client_ua, "test_client.conf", NULL); |
|
|
if (!client_instance) { |
|
|
printf("Failed to create client instance\n"); |
|
|
utun_instance_destroy(server_instance); |
|
|
return 1; |
|
|
} |
|
|
|
|
|
// Initialize connections and register sockets regardless of TUN state |
|
|
if (client_instance->tun.fd < 0) { |
|
|
printf("ℹ️ Client TUN disabled - initializing connections only\n"); |
|
|
} |
|
|
|
|
|
// Initialize ETCP connections (creates sockets and links) |
|
|
printf("About to call init_connections() for client instance\n"); |
|
|
fflush(stdout); |
|
|
|
|
|
int conn_result = init_connections(client_instance); |
|
|
printf("init_connections() returned: %d\n", conn_result); |
|
|
fflush(stdout); |
|
|
|
|
|
if (conn_result < 0) { |
|
|
printf("Failed to initialize client connections (result=%d)\n", conn_result); |
|
|
printf("But continuing test to analyze the issue...\n"); |
|
|
fflush(stdout); |
|
|
// Don't return error - continue to analyze |
|
|
} else { |
|
|
printf("Client connections initialized: sockets=%p, connections=%p, count=%d\n", |
|
|
client_instance->etcp_sockets, client_instance->connections, |
|
|
client_instance ? client_instance->connections_count : -1); |
|
|
fflush(stdout); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
printf("✅ Client instance initialized successfully (node_id=%llx)\n", (unsigned long long)client_instance->node_id); |
|
|
printf("Client instance ready (node_id=%llx)\n\n", (unsigned long long)client_instance->node_id); |
|
|
|
|
|
// Debug: print connection and link info |
|
|
printf("\n=== Connection Debug ===\n"); |
|
|
struct ETCP_CONN* conn = client_instance->connections; |
|
|
while (conn) { |
|
|
printf("Client connection %p: peer_node_id=%llx\n", conn, (unsigned long long)conn->peer_node_id); |
|
|
struct ETCP_LINK* link = conn->links; |
|
|
while (link) { |
|
|
printf(" Link %p: initialized=%d, is_server=%d, remote_addr family=%d, conn->fd=%d\n", |
|
|
link, link->initialized, link->is_server, link->remote_addr.ss_family, link->conn ? link->conn->fd : -1); |
|
|
link = link->next; |
|
|
} |
|
|
conn = conn->next; |
|
|
} |
|
|
conn = server_instance->connections; |
|
|
while (conn) { |
|
|
printf("Server connection %p: peer_node_id=%llx\n", conn, (unsigned long long)conn->peer_node_id); |
|
|
struct ETCP_LINK* link = conn->links; |
|
|
while (link) { |
|
|
printf(" Link %p: initialized=%d, is_server=%d\n", link, link->initialized, link->is_server); |
|
|
link = link->next; |
|
|
} |
|
|
conn = conn->next; |
|
|
} |
|
|
printf("=== End Debug ===\n\n"); |
|
|
|
|
|
// Start monitoring and packet transmission |
|
|
printf("Starting packet transmission test...\n"); |
|
|
packet_timeout_id = uasync_set_timeout(server_ua, 500, NULL, monitor_and_send); |
|
|
void* global_timeout_id = uasync_set_timeout(server_ua, TEST_TIMEOUT_MS, NULL, test_timeout); |
|
|
|
|
|
// Main event loop |
|
|
printf("Running event loop...\n\n"); |
|
|
|
|
|
// Give instances time to initialize |
|
|
printf("Waiting 0.5 seconds for instances initialization...\n"); |
|
|
for (int i = 0; i < 50; i++) { |
|
|
if (server_ua) uasync_poll(server_ua, 10); |
|
|
if (client_ua) uasync_poll(client_ua, 10); |
|
|
usleep(10000); |
|
|
} |
|
|
printf("Starting connection and packet transmission...\n"); |
|
|
|
|
|
int elapsed = 0; |
|
|
int poll_interval = 5; |
|
|
while (!test_completed && elapsed < TEST_TIMEOUT_MS + 1000) { |
|
|
if (server_ua) uasync_poll(server_ua, poll_interval); |
|
|
if (client_ua) uasync_poll(client_ua, poll_interval); |
|
|
|
|
|
usleep(poll_interval * 1000); |
|
|
elapsed += poll_interval; |
|
|
|
|
|
// Quick exit if packet received |
|
|
if (test_completed == 1) { |
|
|
printf("[TEST] Packet received, exiting early after %d ms\n", elapsed); |
|
|
break; |
|
|
} |
|
|
} |
|
|
|
|
|
// Cleanup |
|
|
printf("\nCleaning up...\n"); |
|
|
|
|
|
// Cancel timeouts |
|
|
if (packet_timeout_id) { |
|
|
uasync_cancel_timeout(server_ua, packet_timeout_id); |
|
|
packet_timeout_id = NULL; |
|
|
} |
|
|
if (global_timeout_id) { |
|
|
uasync_cancel_timeout(server_ua, global_timeout_id); |
|
|
} |
|
|
|
|
|
// Destroy instances |
|
|
if (server_instance) { |
|
|
server_instance->running = 0; |
|
|
utun_instance_destroy(server_instance); |
|
|
} |
|
|
if (client_instance) { |
|
|
client_instance->running = 0; |
|
|
utun_instance_destroy(client_instance); |
|
|
} |
|
|
|
|
|
// Evaluate test result |
|
|
if (test_completed == 1) { |
|
|
printf("\n=== TEST PASSED ===\n"); |
|
|
printf("✅ Packet successfully transmitted from client input queue to server output queue\n"); |
|
|
printf("✅ ETCP connection and queue mechanisms verified\n"); |
|
|
return 0; |
|
|
} else if (test_completed == 2) { |
|
|
printf("\n=== TEST FAILED: Packet not received within timeout ===\n"); |
|
|
printf("❌ Connection may not have been established\n"); |
|
|
printf("❌ Check if UDP sockets were created and bound correctly\n"); |
|
|
return 1; |
|
|
} else { |
|
|
printf("\n=== TEST FAILED: Unknown error ===\n"); |
|
|
return 1; |
|
|
} |
|
|
} |