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.
152 lines
5.4 KiB
152 lines
5.4 KiB
// test_etcp_stcp.c — integration: etcp_send + etcp_bind over STCP link |
|
#include "stcp_link.h" |
|
#include "etcp_api.h" |
|
#include "etcp.h" |
|
#include "etcp_connections.h" |
|
#include "secure_channel.h" |
|
#include "topo_group.h" |
|
#include "topo_node.h" |
|
#include "../src/utun_instance.h" |
|
#include "../lib/u_async.h" |
|
#include "../lib/ll_queue.h" |
|
#include "../lib/debug_config.h" |
|
#include "../lib/mem.h" |
|
#include <stdio.h> |
|
#include <string.h> |
|
#include <stdlib.h> |
|
#include <time.h> |
|
|
|
#define TEST_PORT 25678 |
|
|
|
static int test_failed = 0; |
|
static struct SC_MYKEYS s_keys, c_keys; |
|
static struct UTUN_INSTANCE *srv_inst, *cli_inst; |
|
static struct ETCP_CONN *srv_conn, *cli_conn; |
|
static int g_srv_recv_count = 0; |
|
static uint8_t g_srv_recv_data[256]; |
|
|
|
#define TASSERT(cond) do { \ |
|
if (!(cond)) { DEBUG_ERROR(DEBUG_CATEGORY_GENERAL, " FAIL: %s", #cond); test_failed = 1; return test_failed; } \ |
|
} while(0) |
|
|
|
static void etcp_recv_cb(struct ETCP_CONN *conn, struct ll_entry *entry) { |
|
(void)conn; |
|
g_srv_recv_count++; |
|
if (entry->dgram && entry->len) { |
|
size_t n = entry->len < 256 ? entry->len : 255; |
|
memcpy(g_srv_recv_data, entry->dgram, n); |
|
} |
|
queue_dgram_free(entry); |
|
queue_entry_free(entry); |
|
} |
|
|
|
static void connect_cb(void *arg, struct ETCP_CONN *conn, int type) { |
|
(void)arg; |
|
if (!conn) { DEBUG_ERROR(DEBUG_CATEGORY_GENERAL, "etcp_connect failed"); test_failed = 1; return; } |
|
if (type & ETCP_CONNECT_EARLY) { |
|
DEBUG_INFO(DEBUG_CATEGORY_GENERAL, "etcp_connect: EARLY ready"); |
|
cli_conn = conn; |
|
} |
|
} |
|
|
|
static char *build_server_config(int port) { |
|
static char buf[512]; |
|
snprintf(buf, sizeof(buf), |
|
"[global]\n" |
|
"my_node_id=0xAAAAAAAAAAAAAAAA\n" |
|
"my_private_key=38240cb82199e504686507f11f6eaa4f740fde6f0c425c495e49a523019a5d68\n" |
|
"my_public_key=ce8871f07fa056c636d297115f231b08c29cdf94e0d440fce83a07c34416d36a\n" |
|
"\n" |
|
"[server: stcp]\n" |
|
"addr=127.0.0.1:%d\n" |
|
"type=public\n" |
|
"transport=tcp\n", |
|
port); |
|
return buf; |
|
} |
|
|
|
static char *build_client_config(int port) { |
|
static char buf[512]; |
|
snprintf(buf, sizeof(buf), |
|
"[global]\n" |
|
"my_node_id=0xBBBBBBBBBBBBBBBB\n" |
|
"my_private_key=704f2e012c8fa8768130cb0f988a997dccb628372bc5ceccacc78dcbfec5916f\n" |
|
"my_public_key=b3193173def895bd0fcea6f86af077c7d77216f10395275f627ac18242ec0f01\n" |
|
"\n" |
|
"[server: stcp]\n" |
|
"addr=127.0.0.1:%d\n" |
|
"type=public\n" |
|
"transport=tcp\n", |
|
port); |
|
return buf; |
|
} |
|
|
|
int main(void) { |
|
debug_config_init(); |
|
debug_set_level(DEBUG_LEVEL_INFO); |
|
debug_set_categories(DEBUG_CATEGORY_GENERAL | DEBUG_CATEGORY_SOCKET | DEBUG_CATEGORY_CRYPTO); |
|
DEBUG_INFO(DEBUG_CATEGORY_GENERAL, "=== etcp_send/bind over STCP link ==="); |
|
|
|
srand((unsigned)time(NULL)); |
|
struct UASYNC *ua = uasync_create(); TASSERT(ua); |
|
utun_instance_set_tun_init_enabled(0); |
|
|
|
int port = TEST_PORT + rand() % 1000; |
|
|
|
srv_inst = utun_instance_create_from_str(ua, build_server_config(port)); |
|
TASSERT(srv_inst); |
|
TASSERT(utun_instance_init(srv_inst) >= 0); |
|
|
|
cli_inst = utun_instance_create_from_str(ua, build_client_config(port + 1)); |
|
TASSERT(cli_inst); |
|
TASSERT(utun_instance_init(cli_inst) >= 0); |
|
|
|
TASSERT(etcp_bind(srv_inst, 0, etcp_recv_cb) >= 0); |
|
|
|
struct TOPO_GROUP_NODE *srv_node = topo_groups_get_default(srv_inst->topo_groups) ? topo_groups_get_default(srv_inst->topo_groups)->local_node : NULL; |
|
TASSERT(srv_node); |
|
{ struct TOPO_NODE* srv_ni = topo_node_registry_find(srv_inst->topo_groups, srv_node->node_id); TASSERT(srv_ni); |
|
uint8_t wire[4096]; |
|
int len = topo_node_serialize(srv_ni, srv_node, 0, 0, wire, sizeof(wire), 0); |
|
TASSERT(len > 0); |
|
struct TOPO_NODE* cli_ni = NULL; |
|
struct TOPO_GROUP* cli_group = topo_groups_get_default(cli_inst->topo_groups); |
|
TASSERT(topo_node_deserialize(cli_group, wire, (size_t)len, &cli_ni, NULL, NULL, NULL, NULL) == 0); |
|
TASSERT(topo_node_registry_store(cli_inst->topo_groups, cli_ni)); } |
|
cli_inst->etcp_connect_timeout_tb = 100000; |
|
TASSERT(etcp_connect(cli_inst, srv_node, connect_cb, NULL, ETCP_CONNECT_EARLY | ETCP_CONNECT_LATE) == 0); |
|
|
|
int ticks = 0; |
|
while (!cli_conn && ticks < 5000) { uasync_poll(ua, 10); ticks++; } |
|
TASSERT(cli_conn); |
|
|
|
{ struct ll_entry* e = srv_inst->connections->head; |
|
while (e) { struct conn_queue_entry* ce = (struct conn_queue_entry*)e->data; |
|
struct ETCP_CONN *c = ce->conn; |
|
struct ETCP_LINK *l = c->links; while (l) { if (l->is_tcp) { srv_conn = c; break; } l = l->next; } |
|
if (srv_conn) break; |
|
e = e->next; } |
|
} |
|
TASSERT(srv_conn); |
|
|
|
const char *msg = "hello via etcp_send!"; |
|
struct ll_entry *e = queue_entry_new(0); |
|
e->dgram = u_malloc(strlen(msg) + 1); |
|
strcpy((char *)e->dgram, msg); |
|
e->len = (uint16_t)strlen(msg); |
|
TASSERT(etcp_send(cli_conn, e) == 0); |
|
|
|
ticks = 0; |
|
while (g_srv_recv_count < 1 && ticks < 2000) { uasync_poll(ua, 10); ticks++; } |
|
TASSERT(g_srv_recv_count == 1); |
|
TASSERT(memcmp(g_srv_recv_data, msg, strlen(msg)) == 0); |
|
|
|
srv_inst->running = 0; cli_inst->running = 0; |
|
utun_instance_destroy(srv_inst); srv_inst = NULL; |
|
utun_instance_destroy(cli_inst); cli_inst = NULL; |
|
uasync_destroy(ua, 0); |
|
|
|
if (test_failed) { DEBUG_ERROR(DEBUG_CATEGORY_GENERAL, "=== FAILED ==="); return 1; } |
|
DEBUG_INFO(DEBUG_CATEGORY_GENERAL, "=== PASSED ==="); |
|
return 0; |
|
}
|
|
|