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.
52 lines
2.8 KiB
52 lines
2.8 KiB
#ifndef TEST_NET_H |
|
#define TEST_NET_H |
|
|
|
#include "../src/utun_instance.h" |
|
#include "../src/transport_layer/etcp_connections.h" |
|
#include "../lib/debug_config.h" |
|
#include "../lib/socket_compat.h" |
|
#include <stdlib.h> |
|
#include <string.h> |
|
|
|
// Читаем порт уже открытого сокета; владение fd и резервирование порта сохраняются. |
|
static inline uint16_t test_bound_port(socket_t fd) { |
|
uint16_t port = socket_get_bound_port(fd); |
|
if (!port) abort(); |
|
return port; |
|
} |
|
|
|
static inline struct ETCP_SOCKET* test_etcp_bound_socket(struct UTUN_INSTANCE* inst, const char* name) { |
|
for (struct ETCP_SOCKET* s = inst ? inst->etcp_sockets : NULL; s; s = s->next) { |
|
if (strcmp(s->name, name)) continue; |
|
if (ss_get_port(&s->local_addr) && ss_get_port(&s->local_addr) == ss_get_port(&s->interface_addr)) return s; |
|
DEBUG_ERROR(DEBUG_CATEGORY_SOCKET, "test fixture: socket %s has no valid bound endpoint", name); abort(); |
|
} |
|
DEBUG_ERROR(DEBUG_CATEGORY_SOCKET, "test fixture: missing listener %s", name); abort(); |
|
} |
|
|
|
// Связываем явно указанные endpoints до start; никаких замен по номеру порта или обхода эмуляторов. |
|
static inline void test_etcp_set_peer(struct UTUN_INSTANCE* client, const char* client_name, |
|
struct UTUN_INSTANCE* peer, const char* socket_name) { |
|
struct ETCP_SOCKET* target = test_etcp_bound_socket(peer, socket_name); |
|
if (!client || !client->config || client->utun_started) { |
|
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "test fixture: client %s must be configured before start", client_name); abort(); |
|
} |
|
for (struct CFG_CLIENT* c = client->config->clients; c; c = c->next) { |
|
if (strcmp(c->name, client_name)) continue; |
|
if (!c->links) { DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "test fixture: client %s has no links", client_name); abort(); } |
|
for (struct CFG_CLIENT_LINK* link = c->links; link; link = link->next) { |
|
if (link->remote_addr.ss_family != target->interface_addr.ss_family || |
|
(link->local_srv && link->local_srv->transport != target->is_tcp)) { |
|
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "test fixture: incompatible endpoints client=%s listener=%s", client_name, socket_name); |
|
abort(); |
|
} |
|
link->remote_addr = target->interface_addr; |
|
DEBUG_INFO(DEBUG_CATEGORY_DEBUG, "test fixture: client=%s listener=%s peer=%016llx port=%u", |
|
client_name, socket_name, (unsigned long long)peer->node_id, ss_get_port(&link->remote_addr)); |
|
} |
|
return; |
|
} |
|
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "test fixture: missing client %s", client_name); abort(); |
|
} |
|
|
|
#endif
|
|
|