Browse Source
- Fixed incorrect init_connections() call in test_etcp_simple_traffic.c (was calling server_instance instead of client_instance) - Fixed double free in timeout_heap_pop() when handling deleted elements - Enhanced NULL pointer safety in uasync_print_resources() by removing complex heap manipulation - Added debug logging to timeout_heap_pop() for better error tracking Test results: test_etcp_simple_traffic now passes without double free errorsnodeinfo-routing-update
140 changed files with 19477 additions and 9586 deletions
@ -0,0 +1,45 @@
|
||||
bin_PROGRAMS = utun
|
||||
|
||||
utun_SOURCES = \
|
||||
utun.c \
|
||||
utun_instance.c \
|
||||
config_parser.c \
|
||||
config_updater.c \
|
||||
routing.c \
|
||||
tun_if.c \
|
||||
etcp.c \
|
||||
etcp_connections.c \
|
||||
etcp_loadbalancer.c \
|
||||
secure_channel.c \
|
||||
crc32.c \
|
||||
pkt_normalizer.c \
|
||||
utun_test_hooks.c \
|
||||
$(top_srcdir)/tinycrypt/lib/source/aes_encrypt.c \
|
||||
$(top_srcdir)/tinycrypt/lib/source/aes_decrypt.c \
|
||||
$(top_srcdir)/tinycrypt/lib/source/cbc_mode.c \
|
||||
$(top_srcdir)/tinycrypt/lib/source/ccm_mode.c \
|
||||
$(top_srcdir)/tinycrypt/lib/source/cmac_mode.c \
|
||||
$(top_srcdir)/tinycrypt/lib/source/ctr_mode.c \
|
||||
$(top_srcdir)/tinycrypt/lib/source/ecc.c \
|
||||
$(top_srcdir)/tinycrypt/lib/source/ecc_dh.c \
|
||||
$(top_srcdir)/tinycrypt/lib/source/ecc_dsa.c \
|
||||
$(top_srcdir)/tinycrypt/lib/source/ecc_platform_specific.c \
|
||||
$(top_srcdir)/tinycrypt/lib/source/hmac.c \
|
||||
$(top_srcdir)/tinycrypt/lib/source/sha256.c \
|
||||
$(top_srcdir)/tinycrypt/lib/source/utils.c
|
||||
|
||||
utun_CFLAGS = \
|
||||
-I$(top_srcdir)/lib \
|
||||
-I$(top_srcdir)/tinycrypt/lib/include \
|
||||
-I$(top_srcdir)/tinycrypt/lib/source \
|
||||
$(DEBUG_FLAGS)
|
||||
|
||||
utun_LDADD = \
|
||||
$(top_builddir)/lib/libuasync.a \
|
||||
-lpthread \
|
||||
-lm
|
||||
|
||||
# Install directories
|
||||
install-exec-hook: |
||||
$(MKDIR_P) $(DESTDIR)$(bindir)
|
||||
$(MKDIR_P) $(DESTDIR)$(sysconfdir)/utun
|
||||
@ -0,0 +1,586 @@
|
||||
// config_parser.c - Configuration parser for utun application (updated for new structures)
|
||||
#define _POSIX_C_SOURCE 200809L |
||||
#include "config_parser.h" |
||||
#include <stdio.h> |
||||
#include <stdlib.h> |
||||
#include <string.h> |
||||
#include <strings.h> |
||||
#include "../lib/debug_config.h" |
||||
#include <ctype.h> |
||||
#include <arpa/inet.h> |
||||
#include <errno.h> |
||||
#include <netdb.h> |
||||
#include <ifaddrs.h> |
||||
#include <net/if.h> |
||||
|
||||
#define MAX_LINE_LEN 1024 |
||||
#define INITIAL_ARRAY_CAPACITY 8 |
||||
|
||||
typedef enum { |
||||
SECTION_UNKNOWN, |
||||
SECTION_GLOBAL, |
||||
SECTION_SERVER, |
||||
SECTION_CLIENT, |
||||
SECTION_ROUTING |
||||
} section_type_t; |
||||
|
||||
static char* trim(char *str) { |
||||
if (!str) return NULL; |
||||
while (isspace((unsigned char)*str)) str++; |
||||
char *end = str + strlen(str) - 1; |
||||
while (end > str && isspace((unsigned char)*end)) end--; |
||||
*(end + 1) = '\0'; |
||||
return str; |
||||
} |
||||
|
||||
static int parse_key_value(const char *line, char *key, size_t key_len, char *value, size_t value_len) { |
||||
char *equal = strchr(line, '='); |
||||
if (!equal) return -1; |
||||
|
||||
size_t key_size = equal - line; |
||||
if (key_size >= key_len) return -1; |
||||
strncpy(key, line, key_size); |
||||
key[key_size] = '\0'; |
||||
trim(key); |
||||
|
||||
const char *val_start = equal + 1; |
||||
size_t val_len = strlen(val_start); |
||||
if (val_len >= value_len) return -1; |
||||
strcpy(value, val_start); |
||||
trim(value); |
||||
|
||||
return 0; |
||||
} |
||||
|
||||
static int assign_string(char *dest, size_t dest_size, const char *src) { |
||||
if (!dest || !src || strlen(src) >= dest_size) return -1; |
||||
strcpy(dest, src); |
||||
return 0; |
||||
} |
||||
|
||||
static int parse_ip_with_netmask(const char *str, struct IP *ip, uint8_t *netmask) { |
||||
char ip_str[64]; |
||||
strncpy(ip_str, str, sizeof(ip_str) - 1); |
||||
ip_str[sizeof(ip_str) - 1] = '\0'; |
||||
|
||||
char *slash = strchr(ip_str, '/'); |
||||
if (slash) { |
||||
*slash = '\0'; |
||||
*netmask = atoi(slash + 1); |
||||
} else { |
||||
*netmask = 32; // Default IPv4 netmask
|
||||
} |
||||
|
||||
// Try IPv4 first
|
||||
struct in_addr addr4; |
||||
if (inet_pton(AF_INET, ip_str, &addr4) == 1) { |
||||
ip->family = AF_INET; |
||||
ip->addr.v4 = addr4; |
||||
return 0; |
||||
} |
||||
|
||||
// Try IPv6
|
||||
struct in6_addr addr6; |
||||
if (inet_pton(AF_INET6, ip_str, &addr6) == 1) { |
||||
ip->family = AF_INET6; |
||||
ip->addr.v6 = addr6; |
||||
if (*netmask == 32) *netmask = 128; // Default IPv6 netmask
|
||||
return 0; |
||||
} |
||||
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "parse_ip_with_netmask: invalid IP address format: %s", str); |
||||
return -1; |
||||
} |
||||
|
||||
static int parse_sockaddr(const char *addr_str, const char *port_str, struct sockaddr_storage *sockaddr) { |
||||
struct addrinfo hints = {0}; |
||||
hints.ai_family = AF_UNSPEC; |
||||
hints.ai_socktype = SOCK_DGRAM; |
||||
|
||||
struct addrinfo *result; |
||||
if (getaddrinfo(addr_str, port_str, &hints, &result) != 0) { |
||||
return -1; |
||||
} |
||||
|
||||
memcpy(sockaddr, result->ai_addr, result->ai_addrlen); |
||||
freeaddrinfo(result); |
||||
return 0; |
||||
} |
||||
|
||||
static int parse_address_and_port(const char *str, struct sockaddr_storage *sockaddr) { |
||||
char addr_copy[MAX_ADDR_LEN]; |
||||
if (strlen(str) >= sizeof(addr_copy)) return -1; |
||||
strcpy(addr_copy, str); |
||||
|
||||
char *port_str = strrchr(addr_copy, ':'); |
||||
if (!port_str) return -1; |
||||
|
||||
*port_str = '\0'; |
||||
port_str++; |
||||
|
||||
return parse_sockaddr(addr_copy, port_str, sockaddr); |
||||
} |
||||
|
||||
static uint32_t get_netif_index(const char *ifname) { |
||||
if (!ifname || strlen(ifname) == 0) return 0; |
||||
return if_nametoindex(ifname); |
||||
} |
||||
|
||||
static struct CFG_CLIENT_LINK* create_client_link(struct CFG_SERVER* local_srv, const char *remote_addr) { |
||||
struct CFG_CLIENT_LINK *link = calloc(1, sizeof(struct CFG_CLIENT_LINK)); |
||||
if (!link) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, "create_client_link: failed to allocate memory for client link"); |
||||
return NULL; |
||||
} |
||||
|
||||
link->local_srv = local_srv; |
||||
|
||||
if (parse_address_and_port(remote_addr, &link->remote_addr) < 0) { |
||||
free(link); |
||||
return NULL; |
||||
} |
||||
|
||||
link->next = NULL; |
||||
return link; |
||||
} |
||||
|
||||
static void free_cfg_client_links(struct CFG_CLIENT_LINK *links) { |
||||
while (links) { |
||||
struct CFG_CLIENT_LINK *next = links->next; |
||||
free(links); |
||||
links = next; |
||||
} |
||||
} |
||||
|
||||
static struct CFG_ROUTE_ENTRY* create_route_entry(const char *subnet_str) { |
||||
struct CFG_ROUTE_ENTRY *entry = calloc(1, sizeof(struct CFG_ROUTE_ENTRY)); |
||||
if (!entry) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, "create_route_entry: failed to allocate memory for route entry"); |
||||
return NULL; |
||||
} |
||||
|
||||
if (parse_ip_with_netmask(subnet_str, &entry->ip, &entry->netmask) < 0) { |
||||
free(entry); |
||||
return NULL; |
||||
} |
||||
|
||||
entry->next = NULL; |
||||
return entry; |
||||
} |
||||
|
||||
static void free_route_entries(struct CFG_ROUTE_ENTRY *entries) { |
||||
while (entries) { |
||||
struct CFG_ROUTE_ENTRY *next = entries->next; |
||||
free(entries); |
||||
entries = next; |
||||
} |
||||
} |
||||
|
||||
static int add_route_entry(struct CFG_ROUTE_ENTRY **list, const char *subnet_str) { |
||||
struct CFG_ROUTE_ENTRY *new_entry = create_route_entry(subnet_str); |
||||
if (!new_entry) return -1; |
||||
|
||||
// Add to head of list
|
||||
new_entry->next = *list; |
||||
*list = new_entry; |
||||
return 0; |
||||
} |
||||
|
||||
static struct CFG_SERVER* find_server_by_name(struct CFG_SERVER *servers, const char *name) { |
||||
struct CFG_SERVER *srv = servers; |
||||
while (srv) { |
||||
if (strcmp(srv->name, name) == 0) { |
||||
return srv; |
||||
} |
||||
srv = srv->next; |
||||
} |
||||
return NULL; |
||||
} |
||||
|
||||
static int parse_global(const char *key, const char *value, struct global_config *global) { |
||||
if (strcmp(key, "my_private_key") == 0) { |
||||
return assign_string(global->my_private_key_hex, MAX_KEY_LEN, value); |
||||
} |
||||
if (strcmp(key, "my_public_key") == 0) { |
||||
return assign_string(global->my_public_key_hex, MAX_KEY_LEN, value); |
||||
} |
||||
if (strcmp(key, "my_node_id") == 0) {
|
||||
global->my_node_id = strtoull(value, NULL, 16); |
||||
return 0; |
||||
} |
||||
if (strcmp(key, "tun_ifname") == 0) { |
||||
snprintf(global->tun_ifname, sizeof(global->tun_ifname), "%s", value); |
||||
return 0; |
||||
} |
||||
if (strcmp(key, "tun_ip") == 0) { |
||||
uint8_t netmask; |
||||
return parse_ip_with_netmask(value, &global->tun_ip, &netmask); |
||||
} |
||||
if (strcmp(key, "mtu") == 0) { |
||||
global->mtu = atoi(value); |
||||
return 0; |
||||
} |
||||
if (strcmp(key, "control_ip") == 0) { |
||||
// Store for later processing with control_port
|
||||
return 0; // We'll handle this when we see control_port
|
||||
} |
||||
if (strcmp(key, "control_port") == 0) { |
||||
// This is tricky - we need to get control_ip from previous parsing
|
||||
// For now, we'll use a simple approach
|
||||
struct global_config temp_global = *global; |
||||
// Assume we stored control_ip somewhere or use default
|
||||
char control_ip[MAX_ADDR_LEN] = "127.0.0.1"; // Default
|
||||
char port_str[16]; |
||||
snprintf(port_str, sizeof(port_str), "%s", value); |
||||
parse_sockaddr(control_ip, port_str, &global->control_sock); |
||||
return 0; |
||||
} |
||||
if (strcmp(key, "net_debug") == 0) { |
||||
global->net_debug = atoi(value); |
||||
return 0; |
||||
} |
||||
return 0; |
||||
} |
||||
|
||||
static int parse_server(const char *key, const char *value, struct CFG_SERVER *srv) { |
||||
if (strcmp(key, "addr") == 0) { |
||||
return parse_address_and_port(value, &srv->ip); |
||||
} |
||||
if (strcmp(key, "so_mark") == 0) { |
||||
srv->so_mark = atoi(value); |
||||
return 0; |
||||
} |
||||
if (strcmp(key, "netif") == 0) { |
||||
srv->netif_index = get_netif_index(value); |
||||
return 0; |
||||
} |
||||
if (strcmp(key, "type") == 0) { |
||||
if (strcmp(value, "public") == 0) { |
||||
srv->type = CFG_SERVER_TYPE_PUBLIC; |
||||
} else if (strcmp(value, "nat") == 0) { |
||||
srv->type = CFG_SERVER_TYPE_NAT; |
||||
} else if (strcmp(value, "private") == 0) { |
||||
srv->type = CFG_SERVER_TYPE_PRIVATE; |
||||
} else { |
||||
srv->type = CFG_SERVER_TYPE_UNKNOWN; |
||||
} |
||||
return 0; |
||||
} |
||||
return 0; |
||||
} |
||||
|
||||
static int parse_client(const char *key, const char *value, struct CFG_CLIENT *cli, struct CFG_SERVER *servers) { |
||||
if (strcmp(key, "link") == 0) { |
||||
char link_copy[MAX_CONN_NAME_LEN + MAX_ADDR_LEN]; |
||||
if (strlen(value) >= sizeof(link_copy)) return -1; |
||||
strcpy(link_copy, value); |
||||
|
||||
// Find first colon (separator between server and ip:port)
|
||||
char *first_colon = strchr(link_copy, ':'); |
||||
if (!first_colon) return -1; |
||||
|
||||
*first_colon = '\0'; |
||||
|
||||
// Find server by name
|
||||
struct CFG_SERVER *local_srv = find_server_by_name(servers, link_copy); |
||||
if (!local_srv) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "parse_client: server '%s' not found for client link", link_copy); |
||||
return -1; |
||||
} |
||||
|
||||
struct CFG_CLIENT_LINK *new_link = create_client_link(local_srv, first_colon + 1); |
||||
if (!new_link) return -1; |
||||
|
||||
// Add to linked list (prepend)
|
||||
new_link->next = cli->links; |
||||
cli->links = new_link; |
||||
|
||||
return 0; |
||||
} |
||||
|
||||
if (strcmp(key, "peer_public_key") == 0) { |
||||
return assign_string(cli->peer_public_key_hex, MAX_KEY_LEN, value); |
||||
} |
||||
|
||||
if (strcmp(key, "keepalive") == 0) { |
||||
cli->keepalive = atoi(value); |
||||
return 0; |
||||
} |
||||
|
||||
return 0; |
||||
} |
||||
|
||||
static section_type_t parse_section_header(const char *line, char *name, size_t name_len) { |
||||
if (line[0] != '[') return SECTION_UNKNOWN; |
||||
|
||||
size_t line_len = strlen(line); |
||||
if (line[line_len - 1] != ']') return SECTION_UNKNOWN; |
||||
|
||||
char section[128]; |
||||
if (line_len - 2 >= sizeof(section)) return SECTION_UNKNOWN; |
||||
|
||||
strncpy(section, line + 1, line_len - 2); |
||||
section[line_len - 2] = '\0'; |
||||
trim(section); |
||||
|
||||
if (strcasecmp(section, "global") == 0) return SECTION_GLOBAL; |
||||
if (strcasecmp(section, "routing") == 0) return SECTION_ROUTING; |
||||
|
||||
char *colon = strchr(section, ':'); |
||||
if (!colon) return SECTION_UNKNOWN; |
||||
|
||||
*colon = '\0'; |
||||
char *type = trim(section); |
||||
char *n = trim(colon + 1); |
||||
|
||||
if (strlen(n) >= name_len) return SECTION_UNKNOWN; |
||||
strcpy(name, n); |
||||
|
||||
if (strcasecmp(type, "server") == 0) return SECTION_SERVER; |
||||
if (strcasecmp(type, "client") == 0) return SECTION_CLIENT; |
||||
|
||||
return SECTION_UNKNOWN; |
||||
} |
||||
|
||||
static struct utun_config* parse_config_internal(FILE *fp, const char *filename) { |
||||
struct utun_config *cfg = calloc(1, sizeof(struct utun_config)); |
||||
if (!cfg) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, "parse_config_internal: failed to allocate memory for config structure"); |
||||
return NULL; |
||||
} |
||||
|
||||
section_type_t cur_section = SECTION_UNKNOWN; |
||||
struct CFG_SERVER *cur_server = NULL; |
||||
struct CFG_CLIENT *cur_client = NULL; |
||||
char line[MAX_LINE_LEN]; |
||||
int line_num = 0; |
||||
|
||||
while (fgets(line, sizeof(line), fp)) { |
||||
line_num++; |
||||
char *trimmed = trim(line); |
||||
|
||||
if (trimmed[0] == '\0' || trimmed[0] == '#') continue; |
||||
|
||||
if (trimmed[0] == '[') { |
||||
// Handle previous section
|
||||
if (cur_section == SECTION_SERVER && cur_server) { |
||||
// Add server to linked list
|
||||
cur_server->next = cfg->servers; |
||||
cfg->servers = cur_server; |
||||
cur_server = NULL; |
||||
} |
||||
if (cur_section == SECTION_CLIENT && cur_client) { |
||||
// Add client to linked list
|
||||
cur_client->next = cfg->clients; |
||||
cfg->clients = cur_client; |
||||
cur_client = NULL; |
||||
} |
||||
|
||||
char name[MAX_CONN_NAME_LEN]; |
||||
cur_section = parse_section_header(trimmed, name, sizeof(name)); |
||||
|
||||
if (cur_section == SECTION_SERVER) { |
||||
cur_server = calloc(1, sizeof(struct CFG_SERVER)); |
||||
if (!cur_server) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, "parse_config_internal: failed to allocate memory for server %s", name); |
||||
goto error; |
||||
} |
||||
strcpy(cur_server->name, name); |
||||
} else if (cur_section == SECTION_CLIENT) { |
||||
cur_client = calloc(1, sizeof(struct CFG_CLIENT)); |
||||
if (!cur_client) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, "parse_config_internal: failed to allocate memory for client %s", name); |
||||
goto error; |
||||
} |
||||
strcpy(cur_client->name, name); |
||||
} |
||||
continue; |
||||
} |
||||
|
||||
char key[MAX_LINE_LEN], value[MAX_LINE_LEN]; |
||||
if (parse_key_value(trimmed, key, sizeof(key), value, sizeof(value)) < 0) { |
||||
printf( "%s:%d: Invalid key=value format", filename, line_num); |
||||
continue; |
||||
} |
||||
|
||||
switch (cur_section) { |
||||
case SECTION_GLOBAL: |
||||
if (parse_global(key, value, &cfg->global) < 0) { |
||||
printf( "%s:%d: Invalid global key '%s'", filename, line_num, key); |
||||
} |
||||
break; |
||||
case SECTION_SERVER: |
||||
if (cur_server && parse_server(key, value, cur_server) < 0) { |
||||
printf( "%s:%d: Invalid server key '%s'", filename, line_num, key); |
||||
} |
||||
break; |
||||
case SECTION_CLIENT: |
||||
if (cur_client && parse_client(key, value, cur_client, cfg->servers) < 0) { |
||||
printf( "%s:%d: Invalid client key '%s'", filename, line_num, key); |
||||
} |
||||
break; |
||||
case SECTION_ROUTING: |
||||
if (strcmp(key, "allowed_subnet") == 0) { |
||||
add_route_entry(&cfg->allowed_subnets, value); |
||||
} else if (strcmp(key, "my_subnet") == 0) { |
||||
add_route_entry(&cfg->my_subnets, value); |
||||
} |
||||
break; |
||||
default: |
||||
printf( "%s:%d: Key outside section: %s", filename, line_num, key); |
||||
break; |
||||
} |
||||
} |
||||
|
||||
// Handle final sections
|
||||
if (cur_section == SECTION_SERVER && cur_server) { |
||||
cur_server->next = cfg->servers; |
||||
cfg->servers = cur_server; |
||||
} |
||||
if (cur_section == SECTION_CLIENT && cur_client) { |
||||
cur_client->next = cfg->clients; |
||||
cfg->clients = cur_client; |
||||
} |
||||
|
||||
// Set default TUN interface name if not provided
|
||||
if (cfg->global.tun_ifname[0] == '\0') { |
||||
snprintf(cfg->global.tun_ifname, sizeof(cfg->global.tun_ifname), "tun0"); |
||||
DEBUG_INFO(DEBUG_CATEGORY_CONFIG, "Auto-generated TUN interface name: %s", cfg->global.tun_ifname); |
||||
} |
||||
|
||||
return cfg; |
||||
|
||||
error: |
||||
if (cur_server) free(cur_server); |
||||
if (cur_client) { |
||||
free_cfg_client_links(cur_client->links); |
||||
free(cur_client); |
||||
} |
||||
free_config(cfg); |
||||
return NULL; |
||||
} |
||||
|
||||
struct utun_config* parse_config(const char *filename) { |
||||
printf("[CONFIG DEBUG] Opening config file: %s\n", filename); |
||||
FILE *fp = fopen(filename, "r"); |
||||
if (!fp) { |
||||
printf("[CONFIG ERROR] Failed to open config file: %s (errno=%d)\n", filename, errno); |
||||
return NULL; |
||||
} |
||||
|
||||
printf("[CONFIG DEBUG] Successfully opened config file\n"); |
||||
struct utun_config *config = parse_config_internal(fp, filename); |
||||
fclose(fp); |
||||
return config; |
||||
} |
||||
|
||||
void free_config(struct utun_config *config) { |
||||
if (!config) return; |
||||
|
||||
// Free servers
|
||||
struct CFG_SERVER *server = config->servers; |
||||
while (server) { |
||||
struct CFG_SERVER *next = server->next; |
||||
free(server); |
||||
server = next; |
||||
} |
||||
|
||||
// Free clients and their links
|
||||
struct CFG_CLIENT *client = config->clients; |
||||
while (client) { |
||||
struct CFG_CLIENT *next = client->next; |
||||
free_cfg_client_links(client->links); |
||||
free(client); |
||||
client = next; |
||||
} |
||||
|
||||
// Free route entries
|
||||
free_route_entries(config->allowed_subnets); |
||||
free_route_entries(config->my_subnets); |
||||
|
||||
free(config); |
||||
} |
||||
|
||||
static const char* ip_to_string(const struct IP *ip, char *buffer, size_t buffer_size) { |
||||
if (ip->family == AF_INET) { |
||||
inet_ntop(AF_INET, &ip->addr.v4, buffer, buffer_size); |
||||
} else if (ip->family == AF_INET6) { |
||||
inet_ntop(AF_INET6, &ip->addr.v6, buffer, buffer_size); |
||||
} else { |
||||
snprintf(buffer, buffer_size, "invalid"); |
||||
} |
||||
return buffer; |
||||
} |
||||
|
||||
void print_config(const struct utun_config *cfg) { |
||||
if (!cfg) return; |
||||
|
||||
const struct global_config *g = &cfg->global; |
||||
char ip_buffer[64]; |
||||
|
||||
printf("Global:\n"); |
||||
printf(" Private key: %s\n", g->my_private_key_hex); |
||||
printf(" Public key: %s\n", g->my_public_key_hex); |
||||
printf(" Node ID: %llx\n", (unsigned long long)g->my_node_id); |
||||
printf(" TUN interface: %s\n", g->tun_ifname[0] ? g->tun_ifname : "auto"); |
||||
printf(" TUN IP: %s\n", ip_to_string(&g->tun_ip, ip_buffer, sizeof(ip_buffer))); |
||||
printf(" MTU: %d\n", g->mtu); |
||||
printf(" Net debug: %d\n", g->net_debug); |
||||
|
||||
printf("\nServers:\n"); |
||||
struct CFG_SERVER *s = cfg->servers; |
||||
while (s) { |
||||
char addr_buffer[64]; |
||||
struct sockaddr_in *sin = (struct sockaddr_in *)&s->ip; |
||||
inet_ntop(AF_INET, &sin->sin_addr, addr_buffer, sizeof(addr_buffer)); |
||||
printf(" %s: %s:%d (mark=%d, netif=%u, type=%u)\n",
|
||||
s->name, addr_buffer, ntohs(sin->sin_port),
|
||||
s->so_mark, s->netif_index, s->type); |
||||
s = s->next; |
||||
} |
||||
|
||||
printf("\nClients:\n"); |
||||
struct CFG_CLIENT *c = cfg->clients; |
||||
while (c) { |
||||
printf(" %s:\n", c->name); |
||||
struct CFG_CLIENT_LINK *link = c->links; |
||||
int link_num = 1; |
||||
while (link) { |
||||
char addr_buffer[64]; |
||||
struct sockaddr_in *sin = (struct sockaddr_in *)&link->remote_addr; |
||||
inet_ntop(AF_INET, &sin->sin_addr, addr_buffer, sizeof(addr_buffer)); |
||||
printf(" Link %d: %s:%d (via %s)\n", link_num++, addr_buffer, ntohs(sin->sin_port), link->local_srv->name); |
||||
link = link->next; |
||||
} |
||||
printf(" Peer key: %s\n", c->peer_public_key_hex); |
||||
printf(" Keepalive: %d\n", c->keepalive); |
||||
c = c->next; |
||||
} |
||||
|
||||
printf("\nAllowed Subnets:\n"); |
||||
struct CFG_ROUTE_ENTRY *allowed = cfg->allowed_subnets; |
||||
while (allowed) { |
||||
printf(" %s/%d\n", ip_to_string(&allowed->ip, ip_buffer, sizeof(ip_buffer)), allowed->netmask); |
||||
allowed = allowed->next; |
||||
} |
||||
|
||||
printf("\nMy Subnets:\n"); |
||||
struct CFG_ROUTE_ENTRY *my = cfg->my_subnets; |
||||
while (my) { |
||||
printf(" %s/%d\n", ip_to_string(&my->ip, ip_buffer, sizeof(ip_buffer)), my->netmask); |
||||
my = my->next; |
||||
} |
||||
} |
||||
|
||||
int update_config_keys(const char *filename, const char *priv_key, const char *pub_key) { |
||||
// Minimal implementation: just append to file
|
||||
FILE *fp = fopen(filename, "a"); |
||||
if (!fp) return -1; |
||||
|
||||
fprintf(fp, "\n[global]\n"); |
||||
fprintf(fp, "my_private_key=%s\n", priv_key); |
||||
fprintf(fp, "my_public_key=%s\n", pub_key); |
||||
|
||||
fclose(fp); |
||||
return 0; |
||||
} |
||||
@ -0,0 +1,89 @@
|
||||
// config_parser.h - Configuration parser for utun application
|
||||
#ifndef CONFIG_PARSER_H |
||||
#define CONFIG_PARSER_H |
||||
|
||||
#include <stdint.h> |
||||
#include <stddef.h> |
||||
#include <sys/socket.h> |
||||
#include <netinet/in.h> |
||||
#include <arpa/inet.h> |
||||
|
||||
#ifdef __cplusplus |
||||
extern "C" { |
||||
#endif |
||||
|
||||
#define MAX_CONN_NAME_LEN 64 |
||||
#define MAX_KEY_LEN 256 |
||||
#define MAX_ADDR_LEN 64 |
||||
|
||||
struct IP { |
||||
sa_family_t family; |
||||
union { |
||||
struct in_addr v4; |
||||
struct in6_addr v6; |
||||
} addr; |
||||
}; |
||||
|
||||
#define CFG_SERVER_TYPE_UNKNOWN 0 |
||||
#define CFG_SERVER_TYPE_PUBLIC 1 |
||||
#define CFG_SERVER_TYPE_NAT 2 |
||||
#define CFG_SERVER_TYPE_PRIVATE 3 |
||||
|
||||
struct CFG_SERVER { |
||||
struct CFG_SERVER* next; |
||||
char name[MAX_CONN_NAME_LEN]; |
||||
struct sockaddr_storage ip; // ip:port
|
||||
uint32_t netif_index;// if_nameindex, 0 - no interface specified
|
||||
int so_mark; |
||||
uint8_t type; // public/nat/private
|
||||
}; |
||||
|
||||
struct CFG_CLIENT_LINK { |
||||
struct CFG_CLIENT_LINK *next; // Next link in linked list
|
||||
struct CFG_SERVER* local_srv; |
||||
struct sockaddr_storage remote_addr; // ip:port
|
||||
}; |
||||
|
||||
struct CFG_CLIENT { |
||||
char name[MAX_CONN_NAME_LEN]; |
||||
char peer_public_key_hex[MAX_KEY_LEN]; |
||||
int keepalive; |
||||
struct CFG_CLIENT_LINK *links; // Linked list of links
|
||||
struct CFG_CLIENT *next; // Next client in linked list
|
||||
}; |
||||
|
||||
struct CFG_ROUTE_ENTRY { |
||||
struct CFG_ROUTE_ENTRY* next; |
||||
struct IP ip; |
||||
uint8_t netmask; |
||||
}; |
||||
|
||||
struct global_config { |
||||
char my_private_key_hex[MAX_KEY_LEN]; |
||||
char my_public_key_hex[MAX_KEY_LEN]; |
||||
uint64_t my_node_id; |
||||
char tun_ifname[16]; // TUN interface name (e.g., "tun12")
|
||||
struct IP tun_ip; |
||||
int mtu; |
||||
struct sockaddr_storage control_sock; |
||||
int net_debug; |
||||
}; |
||||
|
||||
struct utun_config { |
||||
struct global_config global; |
||||
struct CFG_SERVER* servers; |
||||
struct CFG_CLIENT* clients; |
||||
struct CFG_ROUTE_ENTRY* allowed_subnets; |
||||
struct CFG_ROUTE_ENTRY* my_subnets; |
||||
}; |
||||
|
||||
struct utun_config* parse_config(const char *filename); |
||||
void free_config(struct utun_config *config); |
||||
void print_config(const struct utun_config *config); |
||||
int update_config_keys(const char *filename, const char *priv_key, const char *pub_key); |
||||
|
||||
#ifdef __cplusplus |
||||
} |
||||
#endif |
||||
|
||||
#endif |
||||
@ -0,0 +1,427 @@
|
||||
// config_updater.c - Configuration file updater implementation
|
||||
#define _POSIX_C_SOURCE 200809L |
||||
#include "config_updater.h" |
||||
#include "config_parser.h" |
||||
#include "secure_channel.h" |
||||
#include "debug_config.h" |
||||
#include <stdio.h> |
||||
#include <stdlib.h> |
||||
#include <string.h> |
||||
#include <ctype.h> |
||||
#include <unistd.h> |
||||
#include <fcntl.h> |
||||
#include <sys/stat.h> |
||||
|
||||
#define PRIV_HEXKEY_LEN 65 // 32 bytes * 2 hex chars + null
|
||||
#define PUB_HEXKEY_LEN 129 // 64 bytes * 2 hex chars + null
|
||||
#define HEXNODEID_LEN 17 // 8 bytes * 2 hex chars + null
|
||||
#define MAX_LINE_LEN 1024 |
||||
|
||||
void bytes_to_hex(const uint8_t *bytes, size_t len, char *hex_str, size_t hex_len) { |
||||
if (!bytes || !hex_str || hex_len < len * 2 + 1) { |
||||
if (hex_str && hex_len > 0) hex_str[0] = '\0'; |
||||
return; |
||||
} |
||||
for (size_t i = 0; i < len; i++) { |
||||
snprintf(hex_str + i * 2, hex_len - i * 2, "%02x", bytes[i]); |
||||
|
||||
} |
||||
hex_str[len * 2] = '\0'; |
||||
} |
||||
|
||||
|
||||
static int is_valid_priv_key(const char *key) { |
||||
if (!key || strlen(key) != 64) return 0; |
||||
for (int i = 0; i < 64; i++) { |
||||
if (!isxdigit((unsigned char)key[i])) return 0; |
||||
} |
||||
return 1; |
||||
} |
||||
|
||||
static int is_valid_pub_key(const char *key) { |
||||
if (!key || strlen(key) != 128) return 0; |
||||
for (int i = 0; i < 128; i++) { |
||||
if (!isxdigit((unsigned char)key[i])) return 0; |
||||
} |
||||
return 1; |
||||
} |
||||
|
||||
static int is_valid_node_id(uint64_t node_id) { |
||||
return node_id != 0; |
||||
} |
||||
|
||||
static int read_file_to_mem(const char *filename, char **buffer, size_t *size) { |
||||
FILE *fp = fopen(filename, "r"); |
||||
|
||||
if (!fp) return -1; |
||||
|
||||
fseek(fp, 0, SEEK_END); |
||||
|
||||
long file_size = ftell(fp); |
||||
|
||||
if (file_size < 0) { |
||||
fclose(fp); |
||||
|
||||
return -1; |
||||
} |
||||
fseek(fp, 0, SEEK_SET); |
||||
|
||||
|
||||
*buffer = malloc(file_size + 1); |
||||
|
||||
if (!*buffer) { |
||||
fclose(fp); |
||||
|
||||
return -1; |
||||
} |
||||
|
||||
size_t read_size = fread(*buffer, 1, file_size, fp); |
||||
|
||||
if (read_size != (size_t)file_size) { |
||||
free(*buffer); |
||||
|
||||
fclose(fp); |
||||
|
||||
return -1; |
||||
} |
||||
|
||||
(*buffer)[file_size] = '\0'; |
||||
*size = file_size; |
||||
fclose(fp); |
||||
|
||||
return 0; |
||||
} |
||||
|
||||
static int write_mem_to_file(const char *filename, const char *buffer, size_t size) { |
||||
FILE *fp = fopen(filename, "w"); |
||||
|
||||
if (!fp) return -1; |
||||
|
||||
size_t written = fwrite(buffer, 1, size, fp); |
||||
|
||||
if (written != size) { |
||||
fclose(fp); |
||||
|
||||
return -1; |
||||
} |
||||
|
||||
fclose(fp); |
||||
|
||||
return 0; |
||||
} |
||||
|
||||
static char* find_section_global(char *buf, size_t buf_len) { |
||||
char *p = buf; |
||||
while (p < buf + buf_len) { |
||||
if (p[0] == '[' && strncmp(p + 1, "global", 6) == 0) { |
||||
char *end = strchr(p, ']'); |
||||
|
||||
if (end) return end + 1; |
||||
} |
||||
p = strchr(p, '\n'); |
||||
|
||||
if (!p) break; |
||||
p++; // skip '\n'
|
||||
} |
||||
return NULL; |
||||
} |
||||
|
||||
static char* find_option(char *buf, size_t buf_len, const char *option) { |
||||
char *p = buf; |
||||
while (p < buf + buf_len) { |
||||
if (strncmp(p, option, strlen(option)) == 0) { |
||||
char *after_key = p + strlen(option); |
||||
|
||||
if (after_key[0] == '=') return p; |
||||
} |
||||
p = strchr(p, '\n'); |
||||
|
||||
if (!p) break; |
||||
p++; |
||||
} |
||||
return NULL; |
||||
} |
||||
|
||||
static int insert_or_replace_option(char **buf, size_t *buf_len, size_t *buf_capacity, const char *option, const char *value) { |
||||
if (!buf || !*buf || !buf_len || !buf_capacity || !option || !value) return -1; |
||||
|
||||
// Check if option already exists
|
||||
char *opt_pos = find_option(*buf, *buf_len, option); |
||||
|
||||
if (opt_pos) { |
||||
// Find option line end
|
||||
char *line_end = strchr(opt_pos, '\n'); |
||||
|
||||
if (!line_end) line_end = *buf + *buf_len; |
||||
|
||||
// Prepare new line
|
||||
char new_line[MAX_LINE_LEN]; |
||||
int new_len = snprintf(new_line, sizeof(new_line), "%s=%s\n", option, value); |
||||
|
||||
if (new_len <= 0 || new_len >= (int)sizeof(new_line)) return -1; |
||||
|
||||
// Calculate length difference
|
||||
size_t old_line_len = line_end - opt_pos + 1; |
||||
long len_diff = new_len - old_line_len; |
||||
|
||||
// Ensure buffer capacity
|
||||
if (*buf_len + len_diff + 1 > *buf_capacity) { |
||||
*buf_capacity = *buf_len + len_diff + 1024; |
||||
char *new_buf = realloc(*buf, *buf_capacity); |
||||
|
||||
if (!new_buf) return -1; |
||||
*buf = new_buf; |
||||
// Recalculate positions after realloc
|
||||
opt_pos = find_option(*buf, *buf_len, option); |
||||
|
||||
if (!opt_pos) return -1; |
||||
line_end = strchr(opt_pos, '\n'); |
||||
|
||||
if (!line_end) line_end = *buf + *buf_len; |
||||
} |
||||
|
||||
// Move content and insert new line
|
||||
memmove(opt_pos + new_len, line_end, *buf_len - (line_end - *buf) + 1); |
||||
|
||||
memcpy(opt_pos, new_line, new_len); |
||||
|
||||
*buf_len += len_diff; |
||||
|
||||
} else { |
||||
// Insert after [global] section
|
||||
char *section_end = find_section_global(*buf, *buf_len); |
||||
|
||||
if (!section_end) return -1; |
||||
|
||||
// Find end of global section (next [ or end of buffer)
|
||||
char *insert_pos = strchr(section_end, '['); |
||||
|
||||
if (!insert_pos) insert_pos = *buf + *buf_len; |
||||
else { |
||||
// Move to beginning of next line
|
||||
char *prev_nl = insert_pos; |
||||
while (prev_nl > *buf && *prev_nl != '\n') prev_nl--; |
||||
if (*prev_nl == '\n') insert_pos = prev_nl + 1; |
||||
} |
||||
|
||||
// Prepare new line
|
||||
char new_line[MAX_LINE_LEN]; |
||||
int new_len = snprintf(new_line, sizeof(new_line), "%s=%s\n", option, value); |
||||
|
||||
if (new_len <= 0 || new_len >= (int)sizeof(new_line)) return -1; |
||||
|
||||
// Ensure buffer capacity
|
||||
if (*buf_len + new_len + 1 > *buf_capacity) { |
||||
*buf_capacity = *buf_len + new_len + 1024; |
||||
char *new_buf = realloc(*buf, *buf_capacity); |
||||
|
||||
if (!new_buf) return -1; |
||||
*buf = new_buf; |
||||
// Recalculate insert position after realloc
|
||||
section_end = find_section_global(*buf, *buf_len); |
||||
|
||||
if (!section_end) return -1; |
||||
insert_pos = strchr(section_end, '['); |
||||
|
||||
if (!insert_pos) insert_pos = *buf + *buf_len; |
||||
else { |
||||
char *prev_nl = insert_pos; |
||||
while (prev_nl > *buf && *prev_nl != '\n') prev_nl--; |
||||
if (*prev_nl == '\n') insert_pos = prev_nl + 1; |
||||
} |
||||
} |
||||
|
||||
// Move content and insert new line
|
||||
memmove(insert_pos + new_len, insert_pos, *buf_len - (insert_pos - *buf) + 1); |
||||
|
||||
memcpy(insert_pos, new_line, new_len); |
||||
|
||||
*buf_len += new_len; |
||||
} |
||||
|
||||
return 0; |
||||
} |
||||
|
||||
int config_ensure_keys_and_node_id(const char *filename) { |
||||
struct utun_config *config = parse_config(filename); |
||||
|
||||
if (!config) { |
||||
printf("[CONFIG ERROR] Failed to parse config: %s\n", filename); |
||||
|
||||
return -1; |
||||
} |
||||
|
||||
struct global_config *global = &config->global; |
||||
|
||||
// Debug: print what we found
|
||||
printf("[CONFIG DEBUG] Checking config - priv_key='%s' (len=%zu), pub_key='%s' (len=%zu), node_id=%llu\n", |
||||
global->my_private_key_hex ? global->my_private_key_hex : "NULL", |
||||
global->my_private_key_hex ? strlen(global->my_private_key_hex) : 0, |
||||
global->my_public_key_hex ? global->my_public_key_hex : "NULL",
|
||||
global->my_public_key_hex ? strlen(global->my_public_key_hex) : 0, |
||||
(unsigned long long)global->my_node_id); |
||||
|
||||
|
||||
// Check if we need to generate anything
|
||||
int need_priv_key = !is_valid_priv_key(global->my_private_key_hex); |
||||
|
||||
int need_pub_key = !is_valid_pub_key(global->my_public_key_hex); |
||||
|
||||
int need_node_id = !is_valid_node_id(global->my_node_id); |
||||
|
||||
|
||||
printf("[CONFIG DEBUG] Validation results - need_priv_key=%d, need_pub_key=%d, need_node_id=%d\n", |
||||
need_priv_key, need_pub_key, need_node_id); |
||||
|
||||
|
||||
if (!need_priv_key && !need_pub_key && !need_node_id) { |
||||
free_config(config); |
||||
|
||||
return 0; |
||||
} |
||||
|
||||
// Generate keys if needed
|
||||
char new_priv_key[PRIV_HEXKEY_LEN] = {0}; |
||||
char new_pub_key[PUB_HEXKEY_LEN] = {0}; |
||||
uint64_t new_node_id = 0; |
||||
|
||||
if (need_priv_key) { |
||||
// Generate new keypair if private key is invalid
|
||||
struct SC_MYKEYS mykeys; |
||||
if (sc_generate_keypair(&mykeys) != SC_OK) { |
||||
printf( "Failed to generate keypair"); |
||||
|
||||
free_config(config); |
||||
|
||||
return -1; |
||||
} |
||||
bytes_to_hex(mykeys.private_key, SC_PRIVKEY_SIZE, new_priv_key, sizeof(new_priv_key)); |
||||
|
||||
bytes_to_hex(mykeys.public_key, SC_PUBKEY_SIZE, new_pub_key, sizeof(new_pub_key)); |
||||
|
||||
} else if (need_pub_key) { |
||||
// Compute public key from existing private key
|
||||
uint8_t priv_bin[SC_PRIVKEY_SIZE]; |
||||
uint8_t pub_bin[SC_PUBKEY_SIZE]; |
||||
|
||||
// Convert private key from hex to binary
|
||||
for (int i = 0; i < SC_PRIVKEY_SIZE; i++) { |
||||
unsigned int byte; |
||||
if (sscanf(global->my_private_key_hex + i * 2, "%2x", &byte) != 1) { |
||||
printf( "Invalid private key hex format"); |
||||
|
||||
free_config(config); |
||||
|
||||
return -1; |
||||
} |
||||
priv_bin[i] = (uint8_t)byte; |
||||
} |
||||
|
||||
// Compute public key
|
||||
if (sc_compute_public_key_from_private(priv_bin, pub_bin) != SC_OK) { |
||||
printf( "Failed to compute public key from private key"); |
||||
|
||||
free_config(config); |
||||
|
||||
return -1; |
||||
} |
||||
|
||||
// Convert to hex
|
||||
bytes_to_hex(priv_bin, SC_PRIVKEY_SIZE, new_priv_key, sizeof(new_priv_key)); |
||||
|
||||
bytes_to_hex(pub_bin, SC_PUBKEY_SIZE, new_pub_key, sizeof(new_pub_key)); |
||||
|
||||
} |
||||
|
||||
if (need_node_id) { |
||||
int fd = open("/dev/urandom", O_RDONLY); |
||||
|
||||
if (fd < 0) { |
||||
printf( "Failed to open /dev/urandom"); |
||||
|
||||
free_config(config); |
||||
|
||||
return -1; |
||||
} |
||||
if (read(fd, &new_node_id, sizeof(new_node_id)) != sizeof(new_node_id)) { |
||||
close(fd); |
||||
|
||||
printf( "Failed to read random bytes for node_id"); |
||||
|
||||
free_config(config); |
||||
|
||||
return -1; |
||||
} |
||||
close(fd); |
||||
|
||||
new_node_id &= 0x7FFFFFFFFFFFFFFF; |
||||
} |
||||
|
||||
free_config(config); |
||||
|
||||
|
||||
// Read entire config file to memory
|
||||
char *file_buf = NULL; |
||||
size_t file_size = 0; |
||||
if (read_file_to_mem(filename, &file_buf, &file_size) < 0) { |
||||
printf( "Failed to read config file: %s", filename); |
||||
|
||||
return -1; |
||||
} |
||||
|
||||
// Update config file
|
||||
size_t buf_capacity = file_size + 1024; |
||||
char *work_buf = malloc(buf_capacity); |
||||
|
||||
if (!work_buf) { |
||||
free(file_buf); |
||||
|
||||
return -1; |
||||
} |
||||
memcpy(work_buf, file_buf, file_size); |
||||
|
||||
size_t work_len = file_size; |
||||
|
||||
int ret = 0; |
||||
|
||||
if (need_node_id) { |
||||
char node_id_hex[HEXNODEID_LEN + 1]; |
||||
snprintf(node_id_hex, sizeof(node_id_hex), "%llx", (unsigned long long)new_node_id); |
||||
|
||||
if (insert_or_replace_option(&work_buf, &work_len, &buf_capacity, "my_node_id", node_id_hex) < 0) { |
||||
ret = -1; |
||||
} |
||||
} |
||||
|
||||
if (need_priv_key && ret == 0) { |
||||
if (insert_or_replace_option(&work_buf, &work_len, &buf_capacity, "my_private_key", new_priv_key) < 0) { |
||||
ret = -1; |
||||
} |
||||
} |
||||
|
||||
if (need_pub_key && ret == 0) { |
||||
if (insert_or_replace_option(&work_buf, &work_len, &buf_capacity, "my_public_key", new_pub_key) < 0) { |
||||
ret = -1; |
||||
} |
||||
} |
||||
|
||||
if (ret == 0) { |
||||
printf("[CONFIG DEBUG] Writing updated config file, work_len=%zu\n", work_len); |
||||
|
||||
if (write_mem_to_file(filename, work_buf, work_len) < 0) { |
||||
printf( "Failed to write updated config file: %s", filename); |
||||
|
||||
ret = -1; |
||||
} else { |
||||
printf("[CONFIG DEBUG] Successfully updated config file: %s\n", filename); |
||||
|
||||
} |
||||
} |
||||
|
||||
free(file_buf); |
||||
|
||||
free(work_buf); |
||||
|
||||
|
||||
return ret; |
||||
} |
||||
@ -0,0 +1,23 @@
|
||||
// config_updater.h - Configuration file updater
|
||||
#ifndef CONFIG_UPDATER_H |
||||
#define CONFIG_UPDATER_H |
||||
|
||||
#include <stdint.h> |
||||
#include <stddef.h> |
||||
|
||||
#ifdef __cplusplus |
||||
extern "C" { |
||||
#endif |
||||
|
||||
// Ensures config file has valid my_private_key, my_public_key, and my_node_id
|
||||
// If any are missing or invalid, generates and updates them
|
||||
// Returns 0 on success, -1 on error
|
||||
int config_ensure_keys_and_node_id(const char *filename); |
||||
|
||||
void bytes_to_hex(const uint8_t *bytes, size_t len, char *hex_str, size_t hex_len); |
||||
|
||||
#ifdef __cplusplus |
||||
} |
||||
#endif |
||||
|
||||
#endif // CONFIG_UPDATER_H
|
||||
@ -0,0 +1,66 @@
|
||||
// crc32.c - CRC32 checksum implementation
|
||||
#include "crc32.h" |
||||
#include <string.h> |
||||
|
||||
static uint32_t crc32_table[256]; |
||||
static int crc32_table_initialized = 0; |
||||
|
||||
static void init_crc32_table(void) { |
||||
if (crc32_table_initialized) return; |
||||
|
||||
for (uint32_t i = 0; i < 256; i++) { |
||||
uint32_t crc = i; |
||||
for (int j = 0; j < 8; j++) { |
||||
if (crc & 1) { |
||||
crc = (crc >> 1) ^ 0xEDB88320; |
||||
} else { |
||||
crc >>= 1; |
||||
} |
||||
} |
||||
crc32_table[i] = crc; |
||||
} |
||||
|
||||
crc32_table_initialized = 1; |
||||
} |
||||
|
||||
void crc32_init(void) { |
||||
init_crc32_table(); |
||||
} |
||||
|
||||
uint32_t crc32_calc(const uint8_t *data, size_t len) { |
||||
if (!crc32_table_initialized) { |
||||
init_crc32_table(); |
||||
} |
||||
|
||||
if (!data || len == 0) return 0xFFFFFFFF; |
||||
|
||||
uint32_t crc = 0xFFFFFFFF; |
||||
for (size_t i = 0; i < len; i++) { |
||||
uint8_t byte = data[i]; |
||||
uint32_t table_index = (crc ^ byte) & 0xFF; |
||||
crc = (crc >> 8) ^ crc32_table[table_index]; |
||||
} |
||||
|
||||
return ~crc; |
||||
} |
||||
|
||||
uint32_t crc32_calc_ex(const uint8_t *data, size_t len, uint32_t initial_crc) { |
||||
if (!crc32_table_initialized) { |
||||
init_crc32_table(); |
||||
} |
||||
|
||||
if (!data || len == 0) return initial_crc; |
||||
|
||||
uint32_t crc = initial_crc; |
||||
for (size_t i = 0; i < len; i++) { |
||||
uint8_t byte = data[i]; |
||||
uint32_t table_index = (crc ^ byte) & 0xFF; |
||||
crc = (crc >> 8) ^ crc32_table[table_index]; |
||||
} |
||||
|
||||
return crc; |
||||
} |
||||
|
||||
uint32_t crc32_update(uint32_t crc, const uint8_t *data, size_t len) { |
||||
return crc32_calc_ex(data, len, crc); |
||||
} |
||||
@ -0,0 +1,19 @@
|
||||
#ifndef CRC32_H |
||||
#define CRC32_H |
||||
|
||||
#include <stdint.h> |
||||
#include <stddef.h> |
||||
|
||||
// Initialize CRC32 table (called automatically on first use)
|
||||
void crc32_init(void); |
||||
|
||||
// Calculate CRC32 checksum
|
||||
uint32_t crc32_calc(const uint8_t *data, size_t len); |
||||
|
||||
// Calculate CRC32 with initial value
|
||||
uint32_t crc32_calc_ex(const uint8_t *data, size_t len, uint32_t initial_crc); |
||||
|
||||
// Update CRC32 incrementally
|
||||
uint32_t crc32_update(uint32_t crc, const uint8_t *data, size_t len); |
||||
|
||||
#endif // CRC32_H
|
||||
@ -0,0 +1,148 @@
|
||||
// etcp.h - ETCP Protocol Header (refactored based on etcp_protocol.txt)
|
||||
#ifndef ETCP_H |
||||
#define ETCP_H |
||||
|
||||
#include "etcp_connections.h" |
||||
#include "secure_channel.h" |
||||
#include "../lib/ll_queue.h" |
||||
#include <stdint.h> |
||||
|
||||
#ifdef __cplusplus |
||||
extern "C" { |
||||
#endif |
||||
|
||||
// Forward declarations
|
||||
struct UTUN_INSTANCE; |
||||
struct UASYNC; |
||||
|
||||
// ETCP packet section types (from protocol spec)
|
||||
#define ETCP_SECTION_PAYLOAD 0x00 // Data payload
|
||||
#define ETCP_SECTION_ACK 0x01 // ACK section
|
||||
#define ETCP_SECTION_RETRANS 0x10 // Retransmission request base (0x10-0x2F)
|
||||
#define ETCP_SECTION_TIMESTAMP 0x06 // Channel timestamp (example, adjust if needed)
|
||||
#define ETCP_SECTION_MEAS_TS 0x07 // Measurement timestamp for bandwidth
|
||||
#define ETCP_SECTION_MEAS_RESP 0x08 // Measurement response
|
||||
|
||||
// Inflight packet states
|
||||
#define INFLIGHT_STATE_WAIT_ACK 0 |
||||
#define INFLIGHT_STATE_WAIT_SEND 1 |
||||
|
||||
// Inflight packet structure
|
||||
typedef struct inflight_packet { |
||||
struct inflight_packet* next; |
||||
uint16_t id; // Packet ID
|
||||
struct ETCP_LINK* last_link; // Last sent link
|
||||
uint16_t last_timestamp; // Last send timestamp
|
||||
uint8_t send_count; // Number of sends
|
||||
uint8_t retrans_req_count; // Number of retrans requests
|
||||
uint8_t state; // WAIT_ACK or WAIT_SEND
|
||||
uint8_t need_retrans; // Flag for forced retrans
|
||||
uint8_t* data; // Packet data
|
||||
uint16_t data_len; // Data length
|
||||
uint16_t payload_len; // Payload length for window
|
||||
} inflight_packet_t; |
||||
|
||||
// RX packet for assembly linked list
|
||||
typedef struct rx_packet { |
||||
struct rx_packet* next; |
||||
uint16_t id; |
||||
uint16_t timestamp; |
||||
uint8_t* data; // Assembled data (payload only)
|
||||
uint16_t data_len; |
||||
} rx_packet_t; |
||||
|
||||
// ETCP connection structure (refactored)
|
||||
struct ETCP_CONN { |
||||
struct ETCP_CONN* next; |
||||
int mtu; |
||||
|
||||
struct UTUN_INSTANCE* instance; |
||||
|
||||
// Links (channels) - linked list
|
||||
struct ETCP_LINK* links; |
||||
|
||||
// Crypto and state
|
||||
struct secure_channel crypto_ctx; |
||||
|
||||
// Peer info
|
||||
uint64_t peer_node_id; // Peer node ID
|
||||
|
||||
// Queues
|
||||
struct ll_queue* input_queue; // Incoming packets to send
|
||||
struct ll_queue* output_queue; // Assembled outgoing packets
|
||||
|
||||
// Inflight lists (two lists as per spec)
|
||||
inflight_packet_t* wait_ack_list; // Waiting for ACK
|
||||
inflight_packet_t* wait_send_list; // Waiting for send (retrans)
|
||||
|
||||
// RX assembly list
|
||||
rx_packet_t* rx_list; // Sorted by ID for gap detection
|
||||
|
||||
// IDs and state
|
||||
uint16_t next_tx_id; // Next TX ID
|
||||
uint16_t last_rx_id; // Last received ID
|
||||
uint16_t last_delivered_id; // Last delivered to output_queue
|
||||
|
||||
// Pending ACKs and retrans
|
||||
uint16_t pending_ack_ids[32]; |
||||
uint16_t pending_ack_ts[32]; // Timestamps for ACKs
|
||||
uint8_t pending_ack_count; |
||||
uint16_t pending_retrans_ids[32]; |
||||
uint8_t pending_retrans_count; |
||||
|
||||
// Metrics (RTT, jitter, etc.)
|
||||
uint16_t rtt_last; |
||||
uint16_t rtt_avg_10; |
||||
uint16_t rtt_avg_100; |
||||
uint16_t jitter; |
||||
uint32_t bytes_sent_total; |
||||
uint32_t bytes_received_total; |
||||
uint32_t retransmissions_count; |
||||
|
||||
// Window and inflight management
|
||||
uint32_t unacked_bytes; // Current inflight bytes
|
||||
uint32_t window_size; // Receive window
|
||||
uint32_t optimal_inflight; // Sum over links
|
||||
|
||||
// Timers
|
||||
void* retrans_timer; // Retrans check timer
|
||||
void* ack_timer; // ACK send timer
|
||||
|
||||
// Bandwidth measurement state
|
||||
uint8_t burst_in_progress; // Burst transmission flag
|
||||
uint16_t burst_start_id; // Start ID for burst
|
||||
// ... (add more for meas_ts, meas_resp)
|
||||
|
||||
// Statistics counters
|
||||
uint32_t ack_packets_count; // Count of ACK packets received
|
||||
uint16_t last_rx_ack_id; // Last ACK ID received
|
||||
uint16_t rtt_history[10]; // RTT history for jitter calculation (RTT_HISTORY_SIZE=10)
|
||||
uint8_t rtt_history_idx; // Current index in RTT history
|
||||
uint32_t total_packets_sent; // Total packets sent counter
|
||||
|
||||
// Flags
|
||||
uint8_t wait_timeout_active; // In wait timeout state
|
||||
}; |
||||
|
||||
// Functions
|
||||
struct ETCP_CONN* etcp_connection_create(struct UTUN_INSTANCE* instance); |
||||
void etcp_connection_close(struct ETCP_CONN* etcp); |
||||
void etcp_conn_reset(struct ETCP_CONN* etcp); |
||||
|
||||
// Input from etcp_connections (decrypted packet)
|
||||
void etcp_conn_input(struct ETCP_DGRAM* pkt); |
||||
|
||||
// Request next packet for load balancer
|
||||
struct ETCP_DGRAM* etcp_request_pkt(struct ETCP_CONN* etcp); |
||||
|
||||
// Get stats
|
||||
void etcp_get_stats(struct ETCP_CONN* etcp, size_t* packets_sent, size_t* packets_recv, |
||||
size_t* pool_allocs, size_t* pool_reuse); |
||||
|
||||
uint16_t get_current_timestamp(void); |
||||
|
||||
#ifdef __cplusplus |
||||
} |
||||
#endif |
||||
|
||||
#endif // ETCP_H
|
||||
@ -0,0 +1,846 @@
|
||||
#include "etcp_connections.h" |
||||
#include <arpa/inet.h> |
||||
#include <net/if.h> |
||||
#include <unistd.h> |
||||
#include <fcntl.h> |
||||
#include <errno.h> |
||||
#include <string.h> |
||||
#include "routing.h" |
||||
#include "utun_instance.h" |
||||
#include "utun_test_hooks.h" |
||||
#include "config_parser.h" |
||||
#include "crc32.h" |
||||
#include "etcp.h" |
||||
#include "../lib/memory_pool.h" |
||||
#include "../lib/u_async.h" |
||||
#include "../lib/debug_config.h" |
||||
#include <stdlib.h> |
||||
|
||||
// Packet dump configuration
|
||||
#define ETCP_PACKET_DUMP_COMPACT 1 // Use compact single-line format
|
||||
#define ETCP_PACKET_DUMP_FULL 0 // Use full multi-line format (legacy)
|
||||
#include <time.h> |
||||
|
||||
// Simple debug macros to replace missing debug_config.h
|
||||
#define DEBUG_CATEGORY_CONNECTION 1 |
||||
#define DEBUG_CATEGORY_ETCP 2 |
||||
#define DEBUG_CATEGORY_CRYPTO 3 |
||||
#define DEBUG_CATEGORY_CONFIG 4 |
||||
#define DEBUG_CATEGORY_MEMORY 5 |
||||
|
||||
// Forward declaration
|
||||
static void etcp_connections_read_callback(int fd, void* arg); |
||||
|
||||
// Single-line packet dump for debug output
|
||||
static void dump_packet_bytes(const char* prefix, const uint8_t* data, size_t len) { |
||||
// Build packet data as hex string for single-line output
|
||||
char hex_buf[513]; // 256 bytes * 2 chars + 1 for null terminator
|
||||
size_t hex_len = 0; |
||||
size_t show_len = (len > 128) ? 128 : len; // Show max 128 bytes
|
||||
|
||||
for (size_t i = 0; i < show_len && hex_len < 512 - 3; i++) { |
||||
hex_len += snprintf(hex_buf + hex_len, sizeof(hex_buf) - hex_len, "%02x", data[i]); |
||||
if (i < show_len - 1 && (i + 1) % 32 == 0) { // Add space every 32 bytes
|
||||
hex_len += snprintf(hex_buf + hex_len, sizeof(hex_buf) - hex_len, " "); |
||||
} |
||||
} |
||||
|
||||
if (len > 128) { |
||||
hex_len += snprintf(hex_buf + hex_len, sizeof(hex_buf) - hex_len, "..."); |
||||
} |
||||
|
||||
// Single-line debug output with packet info
|
||||
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "[ETCP] %s: len=%zu hex=%s", prefix, len, hex_buf); |
||||
|
||||
// Additional debug info for first few bytes
|
||||
if (len >= 2) { |
||||
DEBUG_DEBUG(DEBUG_CATEGORY_ETCP, "[ETCP] %s: first_bytes=%02x%02x last_bytes=%02x%02x",
|
||||
prefix, data[0], data[1], data[len-2], data[len-1]); |
||||
} |
||||
} |
||||
|
||||
// Compact packet info for high-frequency logging
|
||||
static void log_packet_compact(const char* prefix, const uint8_t* data, size_t len, struct ETCP_LINK* link) { |
||||
if (!data || len == 0) return; |
||||
|
||||
// Extract packet type from first byte
|
||||
uint8_t pkt_type = data[0]; |
||||
uint16_t timestamp = 0; |
||||
|
||||
if (len >= 3) { |
||||
// Extract timestamp from bytes 1-2 (big endian)
|
||||
timestamp = (data[1] << 8) | data[2]; |
||||
} |
||||
|
||||
// Single line debug with key packet info
|
||||
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "[ETCP] %s: link=%p type=0x%02x ts=%u len=%zu",
|
||||
prefix, link, pkt_type, timestamp, len); |
||||
|
||||
// Trace level for detailed hex dump
|
||||
if (len <= 64) { // Only for small packets to avoid spam
|
||||
char hex_buf[133]; // 64 bytes * 2 + 1
|
||||
size_t hex_len = 0; |
||||
for (size_t i = 0; i < len && hex_len < sizeof(hex_buf) - 3; i++) { |
||||
hex_len += snprintf(hex_buf + hex_len, sizeof(hex_buf) - hex_len, "%02x", data[i]); |
||||
} |
||||
DEBUG_DEBUG(DEBUG_CATEGORY_ETCP, "[ETCP] %s: data=%s", prefix, hex_buf); |
||||
} |
||||
} |
||||
|
||||
// Unified packet dump function - uses configured format
|
||||
static void packet_dump(const char* prefix, const uint8_t* data, size_t len, struct ETCP_LINK* link) { |
||||
if (!data || len == 0) return; |
||||
|
||||
#if ETCP_PACKET_DUMP_COMPACT |
||||
// Compact single-line format
|
||||
log_packet_compact(prefix, data, len, link); |
||||
#else |
||||
// Full multi-line format (legacy)
|
||||
dump_packet_bytes(prefix, data, len); |
||||
#endif |
||||
} |
||||
|
||||
// Legacy multi-line dump for compatibility (kept but not used)
|
||||
static void dump_packet_bytes_multiline(const char* prefix, const uint8_t* data, size_t len) { |
||||
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "[ETCP DUMP] %s: len=%zu; dump: ", prefix, len); |
||||
size_t show = len < 160 ? len : 160; |
||||
for (size_t i = 0; i < show; i++) { |
||||
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "%02x ", data[i]); |
||||
} |
||||
if (len > 160) DEBUG_INFO(DEBUG_CATEGORY_ETCP, "..."); |
||||
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "\n"); |
||||
} |
||||
|
||||
|
||||
// Forward declarations for missing functions
|
||||
struct ETCP_CONN* etcp_connection_create(struct UTUN_INSTANCE* instance); |
||||
|
||||
// CONNECTION MANAGEMENT (!!!это всё должно быть static!!!)
|
||||
static void etcp_link_remove_from_connections(struct ETCP_SOCKET* conn, struct ETCP_LINK* link); |
||||
|
||||
|
||||
// Отправка кодограмм протокола (!!!это всё должно быть static!!!)
|
||||
static void etcp_link_send_init(struct ETCP_LINK* link); |
||||
static int etcp_link_send_reset(struct ETCP_LINK* link); |
||||
static void etcp_link_init_timer_cbk(void* arg); |
||||
|
||||
#define INIT_TIMEOUT_INITIAL 500 |
||||
#define INIT_TIMEOUT_MAX 50000 |
||||
|
||||
static void etcp_link_send_init(struct ETCP_LINK* link) { |
||||
if (!link || !link->etcp || !link->etcp->instance) return; |
||||
|
||||
struct ETCP_DGRAM* dgram = malloc(sizeof(struct ETCP_DGRAM) + 100); |
||||
if (!dgram) return; |
||||
|
||||
dgram->link = link; |
||||
dgram->noencrypt_len = SC_PUBKEY_SIZE; |
||||
size_t offset = 0; |
||||
|
||||
dgram->data[offset++] = ETCP_INIT_REQUEST; |
||||
|
||||
uint64_t node_id = link->etcp->instance->node_id; |
||||
dgram->data[offset++] = (node_id >> 56) & 0xFF; |
||||
dgram->data[offset++] = (node_id >> 48) & 0xFF; |
||||
dgram->data[offset++] = (node_id >> 40) & 0xFF; |
||||
dgram->data[offset++] = (node_id >> 32) & 0xFF; |
||||
dgram->data[offset++] = (node_id >> 24) & 0xFF; |
||||
dgram->data[offset++] = (node_id >> 16) & 0xFF; |
||||
dgram->data[offset++] = (node_id >> 8) & 0xFF; |
||||
dgram->data[offset++] = node_id & 0xFF; |
||||
|
||||
dgram->data[offset++] = (link->mtu >> 8) & 0xFF; |
||||
dgram->data[offset++] = link->mtu & 0xFF; |
||||
|
||||
dgram->data[offset++] = (link->keepalive_interval >> 8) & 0xFF; |
||||
dgram->data[offset++] = link->keepalive_interval & 0xFF; |
||||
|
||||
memcpy(dgram->data + offset, link->etcp->instance->my_keys.public_key, SC_PUBKEY_SIZE); |
||||
dgram->data_len = offset + SC_PUBKEY_SIZE; |
||||
|
||||
DEBUG_INFO(DEBUG_CATEGORY_CONNECTION, "Sending INIT request to link, node_id=%llu, retry=%d", (unsigned long long)node_id, link->init_retry_count); |
||||
|
||||
// Debug: print remote address before sending
|
||||
if (link->remote_addr.ss_family == AF_INET) { |
||||
struct sockaddr_in* sin = (struct sockaddr_in*)&link->remote_addr; |
||||
char addr_str[INET_ADDRSTRLEN]; |
||||
inet_ntop(AF_INET, &sin->sin_addr, addr_str, INET_ADDRSTRLEN); |
||||
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "[ETCP] INIT sending to %s:%d, link=%p, conn_fd=%d", addr_str, ntohs(sin->sin_port), link, link->conn->fd); |
||||
} |
||||
|
||||
etcp_encrypt_send(dgram); |
||||
free(dgram); |
||||
|
||||
link->init_retry_count++; |
||||
|
||||
if (!link->init_timer && link->is_server == 0) { |
||||
link->init_timeout = INIT_TIMEOUT_INITIAL; |
||||
link->init_timer = uasync_set_timeout(link->etcp->instance->ua, link->init_timeout, link, etcp_link_init_timer_cbk); |
||||
} else if (link->init_timer) { |
||||
if ((link->init_retry_count % 10) == 0 && link->init_timeout < INIT_TIMEOUT_MAX) { |
||||
link->init_timeout *= 2; |
||||
if (link->init_timeout > INIT_TIMEOUT_MAX) link->init_timeout = INIT_TIMEOUT_MAX; |
||||
} |
||||
uasync_cancel_timeout(link->etcp->instance->ua, link->init_timer); |
||||
link->init_timer = uasync_set_timeout(link->etcp->instance->ua, link->init_timeout, link, etcp_link_init_timer_cbk); |
||||
} |
||||
} |
||||
|
||||
static void etcp_link_init_timer_cbk(void* arg) { |
||||
struct ETCP_LINK* link = (struct ETCP_LINK*)arg; |
||||
if (!link || link->initialized || link->is_server != 0) return; |
||||
|
||||
link->init_timer = NULL; |
||||
etcp_link_send_init(link); |
||||
} |
||||
|
||||
static int etcp_link_send_reset(struct ETCP_LINK* link) { |
||||
if (!link) return -1; |
||||
|
||||
struct ETCP_DGRAM* dgram = malloc(sizeof(struct ETCP_DGRAM) + 1); |
||||
if (!dgram) return -1; |
||||
|
||||
dgram->link = link; |
||||
dgram->data_len = 1; |
||||
dgram->noencrypt_len = 0; |
||||
dgram->data[0] = 0x06; |
||||
|
||||
DEBUG_INFO(DEBUG_CATEGORY_CONNECTION, "Sending RESET to link"); |
||||
int ret = etcp_encrypt_send(dgram); |
||||
free(dgram); |
||||
return ret; |
||||
} |
||||
|
||||
static uint32_t sockaddr_hash(struct sockaddr_storage* addr) { |
||||
socklen_t addr_len = (addr->ss_family == AF_INET) ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6); |
||||
return crc32_calc((void*)addr, addr_len); |
||||
} |
||||
|
||||
// Бинарный поиск линка по ip_port_hash
|
||||
static int find_link_index(struct ETCP_SOCKET* e_sock, uint32_t hash) { |
||||
if (!e_sock || e_sock->num_channels == 0) return -1; |
||||
|
||||
int left = 0; |
||||
int right = e_sock->num_channels - 1; |
||||
|
||||
while (left <= right) { |
||||
int mid = left + (right - left) / 2; |
||||
if (e_sock->links[mid]->ip_port_hash == hash) { |
||||
return mid; |
||||
} else if (e_sock->links[mid]->ip_port_hash < hash) { |
||||
left = mid + 1; |
||||
} else { |
||||
right = mid - 1; |
||||
} |
||||
} |
||||
|
||||
return -(left + 1); |
||||
} |
||||
|
||||
// Реалокация массива линков с увеличением в 2 раза
|
||||
static int realloc_links(struct ETCP_SOCKET* e_sock) { |
||||
size_t new_max = e_sock->max_channels == 0 ? 8 : e_sock->max_channels * 2; |
||||
struct ETCP_LINK** new_links = realloc(e_sock->links, new_max * sizeof(struct ETCP_LINK*)); |
||||
if (!new_links) return -1; |
||||
|
||||
e_sock->links = new_links; |
||||
e_sock->max_channels = new_max; |
||||
return 0; |
||||
} |
||||
|
||||
// Вставка линка в отсортированный массив
|
||||
static int insert_link(struct ETCP_SOCKET* e_sock, struct ETCP_LINK* link) { |
||||
if (!e_sock || !link) return -1; |
||||
|
||||
if (e_sock->num_channels >= e_sock->max_channels) { |
||||
if (realloc_links(e_sock) < 0) return -1; |
||||
} |
||||
|
||||
int idx = find_link_index(e_sock, link->ip_port_hash); |
||||
if (idx >= 0) return -1; |
||||
|
||||
idx = -(idx + 1); |
||||
|
||||
if (idx < (int)e_sock->num_channels) { |
||||
memmove(&e_sock->links[idx + 1], &e_sock->links[idx],
|
||||
(e_sock->num_channels - idx) * sizeof(struct ETCP_LINK*)); |
||||
} |
||||
|
||||
e_sock->links[idx] = link; |
||||
e_sock->num_channels++; |
||||
return 0; |
||||
} |
||||
|
||||
// Удаление линка из массива
|
||||
static void remove_link(struct ETCP_SOCKET* e_sock, uint32_t hash) { |
||||
if (!e_sock || e_sock->num_channels == 0) return; |
||||
|
||||
int idx = find_link_index(e_sock, hash); |
||||
if (idx < 0) return; |
||||
|
||||
if (idx < (int)e_sock->num_channels - 1) { |
||||
memmove(&e_sock->links[idx], &e_sock->links[idx + 1],
|
||||
(e_sock->num_channels - idx - 1) * sizeof(struct ETCP_LINK*)); |
||||
} |
||||
|
||||
e_sock->num_channels--; |
||||
} |
||||
|
||||
// надо править, используй sockaddr_hash
|
||||
struct ETCP_LINK* etcp_link_find_by_addr(struct ETCP_SOCKET* e_sock, struct sockaddr_storage* addr) { |
||||
if (!e_sock || !addr) return NULL; |
||||
|
||||
int idx = find_link_index(e_sock, sockaddr_hash(addr)); |
||||
if (idx < 0) return NULL; |
||||
|
||||
return e_sock->links[idx]; |
||||
} |
||||
|
||||
|
||||
// ===============================
|
||||
|
||||
struct ETCP_SOCKET* etcp_socket_add_ex(struct UTUN_INSTANCE* instance, struct sockaddr_storage* ip, uint32_t netif_index, int so_mark, uint8_t type, uint32_t flags) { |
||||
if (!instance) return NULL; |
||||
|
||||
struct ETCP_SOCKET* e_sock = calloc(1, sizeof(struct ETCP_SOCKET)); |
||||
if (!e_sock) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, "Failed to allocate connection"); |
||||
return NULL; |
||||
} |
||||
|
||||
int family = AF_INET; |
||||
if (ip) { |
||||
family = ip->ss_family; |
||||
if (family != AF_INET && family != AF_INET6) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_CONNECTION, "Unsupported address family: %d", family); |
||||
free(e_sock); |
||||
return NULL; |
||||
} |
||||
} |
||||
|
||||
// Use test hook if available and in test mode
|
||||
if ((flags & UTUN_CREATE_TEST_MODE) && g_utun_test_hooks && g_utun_test_hooks->socket_create_override) { |
||||
e_sock->fd = g_utun_test_hooks->socket_create_override(family, SOCK_DGRAM, 0,
|
||||
g_utun_test_hooks->test_context); |
||||
DEBUG_INFO(DEBUG_CATEGORY_CONNECTION, "Socket creation using test hook: fd=%d", e_sock->fd); |
||||
} else { |
||||
e_sock->fd = socket(family, SOCK_DGRAM, 0); |
||||
} |
||||
|
||||
if (e_sock->fd < 0) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_CONNECTION, "Failed to create socket: %s", strerror(errno)); |
||||
free(e_sock); |
||||
return NULL; |
||||
} |
||||
|
||||
int flags_fcntl = fcntl(e_sock->fd, F_GETFL, 0); |
||||
fcntl(e_sock->fd, F_SETFL, flags_fcntl | O_NONBLOCK); |
||||
|
||||
// Set socket mark if specified
|
||||
if (so_mark > 0) { |
||||
#ifdef SO_MARK |
||||
if (setsockopt(e_sock->fd, SOL_SOCKET, SO_MARK, &so_mark, sizeof(so_mark)) < 0) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_CONNECTION, "Failed to set SO_MARK: %s", strerror(errno)); |
||||
} |
||||
#endif |
||||
} |
||||
|
||||
// Bind to interface if specified
|
||||
if (netif_index > 0) { |
||||
#ifdef SO_BINDTODEVICE |
||||
char ifname[IF_NAMESIZE]; |
||||
if (if_indextoname(netif_index, ifname)) { |
||||
if (setsockopt(e_sock->fd, SOL_SOCKET, SO_BINDTODEVICE, ifname, strlen(ifname)) < 0) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_CONNECTION, "Failed to bind to interface %s: %s", ifname, strerror(errno)); |
||||
} |
||||
} |
||||
#endif |
||||
} |
||||
|
||||
// Store the local address and bind socket if provided
|
||||
if (ip) { |
||||
memcpy(&e_sock->local_addr, ip, sizeof(struct sockaddr_storage)); |
||||
|
||||
// Skip binding if requested (for test injection)
|
||||
if (!(flags & UTUN_CREATE_NO_SOCKET_BIND)) { |
||||
// CRITICAL: Actually bind the socket to the address - this was missing!
|
||||
socklen_t addr_len = (ip->ss_family == AF_INET) ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6); |
||||
if (bind(e_sock->fd, (struct sockaddr*)ip, addr_len) < 0) { |
||||
perror("bind"); |
||||
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "[ETCP] Failed to bind socket to address family %d", ip->ss_family); |
||||
if (ip->ss_family == AF_INET) { |
||||
struct sockaddr_in* sin = (struct sockaddr_in*)ip; |
||||
char addr_str[INET_ADDRSTRLEN]; |
||||
inet_ntop(AF_INET, &sin->sin_addr, addr_str, INET_ADDRSTRLEN); |
||||
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "[ETCP] Failed to bind to %s:%d", addr_str, ntohs(sin->sin_port)); |
||||
} |
||||
close(e_sock->fd); |
||||
free(e_sock); |
||||
return NULL; |
||||
} |
||||
|
||||
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "[ETCP] Successfully bound socket to local address, family=%d", ip->ss_family); |
||||
} else { |
||||
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "[ETCP] Socket binding skipped (NO_SOCKET_BIND flag)"); |
||||
} |
||||
} |
||||
|
||||
e_sock->instance = instance; |
||||
e_sock->errorcode = 0; |
||||
e_sock->pkt_format_errors = 0; |
||||
|
||||
// Add to instance's socket list
|
||||
e_sock->next = instance->etcp_sockets; |
||||
instance->etcp_sockets = e_sock; |
||||
|
||||
// Register socket with uasync for receiving packets
|
||||
e_sock->socket_id = uasync_add_socket(instance->ua, e_sock->fd, |
||||
etcp_connections_read_callback, |
||||
NULL, NULL, e_sock); |
||||
DEBUG_INFO(DEBUG_CATEGORY_CONNECTION, "Registered ETCP socket with uasync (fd=%d)", e_sock->fd); |
||||
|
||||
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "[ETCP] Socket %p (fd=%d) registered and active", e_sock, e_sock->fd); |
||||
|
||||
return e_sock; |
||||
} |
||||
|
||||
void etcp_socket_remove(struct ETCP_SOCKET* conn) { |
||||
if (!conn) return; |
||||
|
||||
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "[ETCP] Removing socket %p, fd=%d, socket_id=%p", conn, conn->fd, conn->socket_id); |
||||
|
||||
// Remove from uasync if registered
|
||||
if (conn->socket_id) { |
||||
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "[ETCP] Removing socket from uasync, instance=%p, ua=%p", conn->instance, conn->instance->ua); |
||||
uasync_remove_socket(conn->instance->ua, conn->socket_id); |
||||
conn->socket_id = NULL; |
||||
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "[ETCP] Unregistered socket from uasync"); |
||||
} |
||||
|
||||
if (conn->fd >= 0) { |
||||
close(conn->fd); |
||||
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "[ETCP] Closed fd=%d", conn->fd); |
||||
} |
||||
|
||||
for (size_t i = 0; i < conn->num_channels; i++) { |
||||
etcp_link_close(conn->links[i]); |
||||
} |
||||
free(conn->links); |
||||
|
||||
free(conn); |
||||
} |
||||
|
||||
|
||||
struct ETCP_LINK* etcp_link_new(struct ETCP_CONN* etcp, struct ETCP_SOCKET* conn, struct sockaddr_storage* remote_addr, uint8_t is_server) { |
||||
if (!remote_addr) return NULL; |
||||
|
||||
struct ETCP_LINK* link = calloc(1, sizeof(struct ETCP_LINK)); |
||||
if (!link) return NULL; |
||||
|
||||
link->conn = conn; |
||||
link->etcp = etcp; |
||||
link->is_server = is_server; |
||||
link->mtu = 1500; |
||||
link->keepalive_interval = 30; |
||||
link->initialized = 0; |
||||
link->init_timer = NULL; |
||||
link->init_timeout = 0; |
||||
link->init_retry_count = 0; |
||||
|
||||
memcpy(&link->remote_addr, remote_addr, sizeof(struct sockaddr_storage)); |
||||
link->last_activity = time(NULL); |
||||
|
||||
link->ip_port_hash = sockaddr_hash(remote_addr); |
||||
|
||||
insert_link(conn, link); |
||||
|
||||
struct ETCP_LINK* l=etcp->links; |
||||
while (l && l->next) l=l->next; |
||||
if (l) l->next = link; else etcp->links = link; |
||||
|
||||
if (is_server == 0) { |
||||
etcp_link_send_init(link); |
||||
} |
||||
|
||||
return link; |
||||
} |
||||
|
||||
void etcp_link_close(struct ETCP_LINK* link) { |
||||
if (!link || !link->etcp) return; |
||||
|
||||
// Cancel init timer if active
|
||||
if (link->init_timer) { |
||||
uasync_cancel_timeout(link->etcp->instance->ua, link->init_timer); |
||||
link->init_timer = NULL; |
||||
} |
||||
|
||||
// универсальное удаление из односвязного списка
|
||||
struct ETCP_LINK **pp = &link->etcp->links; |
||||
while (*pp) { |
||||
if (*pp == link) { |
||||
*pp = link->next; |
||||
break; |
||||
} |
||||
pp = &(*pp)->next; |
||||
} |
||||
|
||||
remove_link(link->conn, link->ip_port_hash); |
||||
|
||||
free(link); |
||||
} |
||||
|
||||
int etcp_encrypt_send(struct ETCP_DGRAM* dgram) { |
||||
// printf("[ETCP DEBUG] etcp_encrypt_send: ENTERING FUNCTION\n");
|
||||
|
||||
int errcode=0; |
||||
sc_context_t* sc = &dgram->link->etcp->crypto_ctx; |
||||
int len=dgram->data_len-dgram->noencrypt_len;// не забываем добавить timestamp (2 bytes)
|
||||
if (len<=0 || len>1480) { dgram->link->send_errors++; errcode=1; goto es_err; } |
||||
uint8_t enc_buf[1600]; |
||||
size_t enc_buf_len=0; |
||||
dgram->timestamp=get_current_timestamp(); |
||||
|
||||
// DUMP: Show packet before encryption
|
||||
packet_dump("ECTP_ENCRYPT_SEND", dgram->data, dgram->data_len, dgram->link); |
||||
sc_encrypt(sc, (uint8_t*)&dgram->timestamp/*не править это, тут верно!*/, sizeof(uint16_t) + len, enc_buf, &enc_buf_len); |
||||
if (enc_buf_len == 0) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_CRYPTO, "etcp_encrypt_send: encryption failed for node %llu", (unsigned long long)dgram->link->etcp->instance->node_id); |
||||
dgram->link->send_errors++; |
||||
errcode=2;
|
||||
goto es_err; |
||||
} |
||||
if (enc_buf_len + dgram->noencrypt_len > 1480) { dgram->link->send_errors++; errcode=2; goto es_err; } |
||||
memcpy(enc_buf+enc_buf_len, dgram->data+len, dgram->noencrypt_len); |
||||
|
||||
// DUMP: Show complete packet before sending
|
||||
// dump_packet_bytes("READY TO SEND", enc_buf, enc_buf_len + dgram->noencrypt_len);
|
||||
|
||||
struct sockaddr_storage* addr=&dgram->link->remote_addr; |
||||
socklen_t addr_len = (addr->ss_family == AF_INET) ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6); |
||||
|
||||
// Debug: print where we're sending the packet
|
||||
if (addr->ss_family == AF_INET) { |
||||
struct sockaddr_in* sin = (struct sockaddr_in*)addr; |
||||
char addr_str[INET_ADDRSTRLEN]; |
||||
inet_ntop(AF_INET, &sin->sin_addr, addr_str, INET_ADDRSTRLEN); |
||||
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "[ETCP] Sending packet to %s:%d, size=%zd", addr_str, ntohs(sin->sin_port), enc_buf_len + dgram->noencrypt_len); |
||||
} |
||||
|
||||
ssize_t sent = utun_sendto_hook(dgram->link->conn->fd, enc_buf, enc_buf_len + dgram->noencrypt_len, 0, (struct sockaddr*)addr, addr_len); |
||||
if (sent < 0) { dgram->link->send_errors++; errcode=3; goto es_err;} else dgram->link->total_encrypted += sent; |
||||
return (int)sent; |
||||
es_err: |
||||
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "[ETCP] encrypt_send error %d", errcode); |
||||
return -1; |
||||
} |
||||
|
||||
static void etcp_connections_read_callback(int fd, void* arg) { |
||||
// !!!!!! DANGER: в этой функции ПРЕДЕЛЬНАЯ АККУРАТНОСТЬ. Если кажется что не туда указатель то невнимательно аланизировал !!!!!
|
||||
// НЕ РУИНИТЬ (uint8_t*)&pkt->timestamp - это правильно !!!!
|
||||
//
|
||||
// Ошибки функции (errorcode):
|
||||
// 1 - пакет слишком маленький для init (< SC_PUBKEY_SIZE)
|
||||
// 2 - не удалось установить peer public key при init
|
||||
// 3 - не удалось расшифровать init пакет
|
||||
// 4 - не init пакет (неверный код)
|
||||
// 5 - коллизия peer ID и ключей
|
||||
// 6 - не удалось расшифровать обычный пакет
|
||||
// 13 - переполнение при парсинге пакета
|
||||
// 46 - расшифрованный пакет слишком маленький (< 3 байта)
|
||||
// 55 - не удалось создать подключение
|
||||
// 66 - не удалось создать линк
|
||||
struct ETCP_SOCKET* e_sock = (struct ETCP_SOCKET*)arg; |
||||
if (!e_sock) return; |
||||
|
||||
// printf("[ETCP] Read callback triggered for fd=%d, socket=%p\n", fd, e_sock);
|
||||
|
||||
struct sockaddr_storage addr; |
||||
uint8_t data[PACKET_DATA_SIZE]; |
||||
socklen_t addr_len=sizeof(addr); |
||||
memset(&addr, 0, sizeof(addr)); |
||||
ssize_t recv_len = utun_recvfrom_hook(fd, data, PACKET_DATA_SIZE, 0, (struct sockaddr*)&addr, &addr_len); |
||||
|
||||
if (recv_len <= 0) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "[ETCP] recvfrom failed or no data, recv_len=%zd, errno=%d", recv_len, errno); |
||||
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "etcp_connections_read_callback: recvfrom failed, error=%zd, errno=%d", recv_len, errno); |
||||
return; |
||||
} |
||||
|
||||
// printf("[ETCP] Received packet: %zd bytes from address\n", recv_len);
|
||||
|
||||
// DUMP: Show received packet content
|
||||
packet_dump("RECV in:", data, recv_len, NULL); // link unknown at this point
|
||||
|
||||
struct ETCP_DGRAM* pkt = memory_pool_alloc(e_sock->instance->pkt_pool); |
||||
if (!pkt) return; |
||||
size_t pkt_len=0; |
||||
int errorcode=0; |
||||
|
||||
struct ETCP_LINK* link=etcp_link_find_by_addr(e_sock, &addr); |
||||
// printf("[ETCP DEBUG] Received packet, link=%p, recv_len=%zd\n", link, recv_len);
|
||||
if (link==NULL) {// пробуем расшифровать, возможно это init
|
||||
// printf("[ETCP DEBUG] No existing link found, trying to decrypt as INIT packet\n");
|
||||
struct secure_channel sc; |
||||
if (recv_len<=SC_PUBKEY_SIZE) { errorcode=1; DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "etcp_connections_read_callback: packet too small for init, size=%zd", recv_len); goto ec_fr; } |
||||
sc_init_ctx(&sc, &e_sock->instance->my_keys); |
||||
// printf("[ETCP DEBUG] Extracting peer public key from position %ld, total packet size=%zd\n", recv_len-SC_PUBKEY_SIZE, recv_len);
|
||||
// printf("[ETCP DEBUG] Last 64 bytes of packet (PUBKEY): ");
|
||||
for (int i=0; i<SC_PUBKEY_SIZE; i++) DEBUG_INFO(DEBUG_CATEGORY_CRYPTO, "%02x ", data[recv_len-SC_PUBKEY_SIZE+i]); |
||||
|
||||
if (sc_set_peer_public_key(&sc, &data[recv_len-SC_PUBKEY_SIZE], SC_PEER_PUBKEY_BIN)!=SC_OK) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_CRYPTO, "etcp_connections_read_callback: failed to set peer public key during init"); |
||||
errorcode=2;
|
||||
goto ec_fr;
|
||||
} |
||||
if (sc_decrypt(&sc, data, recv_len-SC_PUBKEY_SIZE, (uint8_t*)&pkt->timestamp, &pkt_len)) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_CRYPTO, "etcp_connections_read_callback: failed to decrypt init packet"); |
||||
errorcode=3;
|
||||
goto ec_fr;
|
||||
} |
||||
// printf("[ETCP DEBUG] Decrypt OK\n");
|
||||
pkt->data_len=pkt_len-2; |
||||
pkt->noencrypt_len=0; |
||||
struct { |
||||
uint8_t code; |
||||
uint8_t id[8]; |
||||
uint8_t mtu[2]; |
||||
uint8_t keepalive[2]; |
||||
uint8_t pubkey[SC_PUBKEY_SIZE]; |
||||
} *ack_hdr=(void*)&pkt->data[0]; |
||||
uint64_t peer_id; |
||||
memcpy(&peer_id, &ack_hdr->id[0], 8); |
||||
if (ack_hdr->code!=ETCP_INIT_REQUEST && ack_hdr->code!=ETCP_CHANNEL_INIT) { errorcode=4; DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "etcp_connections_read_callback: not an init packet, code=%02x", ack_hdr->code); goto ec_fr; }// не init
|
||||
|
||||
struct ETCP_CONN* conn=e_sock->instance->connections; |
||||
while (conn) {// ищем есть ли подключение к этому пиру
|
||||
if (conn->peer_node_id==peer_id) break; |
||||
conn=conn->next; |
||||
} |
||||
|
||||
int new_conn=0; |
||||
if (!conn || conn->peer_node_id!=peer_id) {// создаём новое
|
||||
new_conn=1; |
||||
conn=etcp_connection_create(e_sock->instance); |
||||
if (!conn) { errorcode=55; DEBUG_ERROR(DEBUG_CATEGORY_CONNECTION, "etcp_connections_read_callback: failed to create connection"); goto ec_fr; }// облом
|
||||
memcpy(&conn->crypto_ctx, &sc, sizeof(sc));// добавляем ключ
|
||||
conn->peer_node_id=peer_id; |
||||
} |
||||
else {// check keys если существующее подключение
|
||||
if (memcmp(conn->crypto_ctx.peer_public_key, sc.peer_public_key, SC_PUBKEY_SIZE)) { errorcode=5; DEBUG_ERROR(DEBUG_CATEGORY_CRYPTO, "etcp_connections_read_callback: peer key mismatch for node %llu", (unsigned long long)peer_id); goto ec_fr; }// коллизия - peer id совпал а ключи разные.
|
||||
} |
||||
struct ETCP_LINK* link = etcp_link_new(conn, e_sock, &addr, 1); |
||||
if (!link) { if (new_conn) etcp_connection_close(conn); errorcode=66; DEBUG_ERROR(DEBUG_CATEGORY_CONNECTION, "etcp_connections_read_callback: failed to create link for connection"); goto ec_fr; }// облом
|
||||
if (ack_hdr->code==0x02) etcp_conn_reset(conn); |
||||
|
||||
struct { |
||||
uint8_t code; |
||||
uint8_t id[8]; |
||||
uint8_t mtu[2]; |
||||
} *ack_repl_hdr=(void*)&pkt->data[0]; |
||||
ack_repl_hdr->code+=1; |
||||
memcpy(ack_repl_hdr->id, &e_sock->instance->node_id, 8); |
||||
int mtu=e_sock->instance->config->global.mtu; |
||||
ack_repl_hdr->mtu[0]=mtu>>8; |
||||
ack_repl_hdr->mtu[1]=mtu; |
||||
pkt->data_len=sizeof(*ack_repl_hdr); |
||||
pkt->noencrypt_len=0; |
||||
pkt->link=link; |
||||
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "[ETCP DEBUG] Send INIT RESPONSE"); |
||||
etcp_encrypt_send(pkt); |
||||
// printf("[ETCP DEBUG] Send INIT RESPONSE ok\n");
|
||||
|
||||
memory_pool_free(e_sock->instance->pkt_pool, pkt); |
||||
return; |
||||
} |
||||
|
||||
if (sc_decrypt(&link->etcp->crypto_ctx, data, recv_len, (uint8_t*)&pkt->timestamp, &pkt_len)) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_CRYPTO, "etcp_connections_read_callback: failed to decrypt packet from node %llu", (unsigned long long)link->etcp->instance->node_id); |
||||
errorcode=6;
|
||||
goto ec_fr;
|
||||
} |
||||
if (pkt_len<3) { errorcode=46; DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "etcp_connections_read_callback: decrypted packet too small, size=%zu", pkt_len); goto ec_fr; } |
||||
pkt->data_len=pkt_len-2; |
||||
pkt->noencrypt_len=0; |
||||
pkt->link=link; |
||||
|
||||
link->last_recv_local_time=get_current_time_units(); |
||||
link->last_recv_timestamp=pkt->timestamp; |
||||
|
||||
size_t offset = 0; |
||||
uint8_t code = pkt->data[offset++]; |
||||
|
||||
if (code == ETCP_INIT_RESPONSE || code == ETCP_CHANNEL_RESPONSE) { |
||||
// Parse response
|
||||
if (code == ETCP_INIT_RESPONSE) etcp_conn_reset(link->etcp); |
||||
uint64_t server_node_id = 0; |
||||
for (int i = 0; i < 8; i++) { |
||||
server_node_id = (server_node_id << 8) | pkt->data[offset++]; |
||||
} |
||||
link->mtu = (pkt->data[offset++] << 8) | pkt->data[offset++]; |
||||
if (offset > pkt_len) { errorcode=13; DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "etcp_connections_read_callback: packet parsing overflow, offset=%zu, pkt_len=%zu", offset, pkt_len); goto ec_fr; } |
||||
|
||||
DEBUG_INFO(DEBUG_CATEGORY_CONNECTION, "Received INIT_RESPONSE from server_node_id=%llu, mtu=%d", |
||||
(unsigned long long)server_node_id, link->mtu); |
||||
|
||||
link->etcp->peer_node_id = server_node_id; // If not set
|
||||
|
||||
// Mark link as initialized
|
||||
link->initialized = 1; |
||||
DEBUG_INFO(DEBUG_CATEGORY_CONNECTION, "[ETCP] Link initialized successfully! Server node_id=%llu, mtu=%d", |
||||
(unsigned long long)server_node_id, link->mtu); |
||||
|
||||
// Cancel init timer if exists
|
||||
if (link->init_timer) { |
||||
uasync_cancel_timeout(link->etcp->instance->ua, link->init_timer); |
||||
link->init_timer = NULL; |
||||
} |
||||
|
||||
memory_pool_free(e_sock->instance->pkt_pool, pkt); |
||||
return; // INIT_RESPONSE is handled, no further processing needed
|
||||
} |
||||
|
||||
packet_dump("RECV decrypted:", pkt->data, pkt->data_len, link); |
||||
|
||||
etcp_conn_input(pkt); |
||||
return; |
||||
|
||||
ec_fr: |
||||
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "etcp_connections_read_callback: error %d", errorcode); |
||||
e_sock->pkt_format_errors++; |
||||
e_sock->errorcode=errorcode; |
||||
memory_pool_free(e_sock->instance->pkt_pool, pkt); |
||||
return; |
||||
} |
||||
|
||||
int init_connections(struct UTUN_INSTANCE* instance) { |
||||
if (!instance || !instance->config) return -1; |
||||
|
||||
struct utun_config* config = instance->config; |
||||
|
||||
// Initialize servers first - create sockets for incoming connections
|
||||
struct CFG_SERVER* server = config->servers; |
||||
while (server) { |
||||
// Create socket for this server
|
||||
struct ETCP_SOCKET* e_sock = etcp_socket_add(instance, &server->ip, server->netif_index, server->so_mark, server->type); |
||||
if (!e_sock) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "Failed to create socket for server %s", server->name); |
||||
server = server->next; |
||||
continue; |
||||
} |
||||
|
||||
// Convert IP to string for logging
|
||||
char addr_str[INET6_ADDRSTRLEN + 6]; |
||||
if (server->ip.ss_family == AF_INET) { |
||||
struct sockaddr_in* sin = (struct sockaddr_in*)&server->ip; |
||||
inet_ntop(AF_INET, &sin->sin_addr, addr_str, INET_ADDRSTRLEN); |
||||
sprintf(addr_str + strlen(addr_str), ":%d", ntohs(sin->sin_port)); |
||||
} else { |
||||
struct sockaddr_in6* sin6 = (struct sockaddr_in6*)&server->ip; |
||||
inet_ntop(AF_INET6, &sin6->sin6_addr, addr_str, INET6_ADDRSTRLEN); |
||||
sprintf(addr_str + strlen(addr_str), ":%d", ntohs(sin6->sin6_port)); |
||||
} |
||||
|
||||
DEBUG_INFO(DEBUG_CATEGORY_CONNECTION, "Initialized server %s on %s (links: %zu)",
|
||||
server->name, addr_str, e_sock->num_channels); |
||||
server = server->next; |
||||
} |
||||
|
||||
// Initialize clients - create outgoing connections
|
||||
struct CFG_CLIENT* client = config->clients; |
||||
while (client) { |
||||
// Create ETCP connection for this client
|
||||
struct ETCP_CONN* etcp_conn = etcp_connection_create(instance); |
||||
if (!etcp_conn) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "Failed to create ETCP connection for client %s", client->name); |
||||
client = client->next; |
||||
continue; |
||||
} |
||||
|
||||
// Initialize crypto context for this connection
|
||||
if (sc_init_ctx(&etcp_conn->crypto_ctx, &instance->my_keys) != SC_OK) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_CRYPTO, "init_connections: failed to initialize crypto context for client %s", client->name); |
||||
etcp_connection_close(etcp_conn); |
||||
client = client->next; |
||||
continue; |
||||
} |
||||
// If client has peer public key configured, set it
|
||||
if (strlen(client->peer_public_key_hex) > 0) { |
||||
// For now, set peer node ID to indicate we have peer key
|
||||
// The actual peer key will be exchanged during connection establishment
|
||||
etcp_conn->peer_node_id = 1; // Simple indicator
|
||||
|
||||
DEBUG_INFO(DEBUG_CATEGORY_CRYPTO, "init_connections: setting peer public key for client %s", client->name); |
||||
// Set peer public key (assuming hex format)
|
||||
if (sc_set_peer_public_key(&etcp_conn->crypto_ctx, client->peer_public_key_hex, 1) != SC_OK) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_CRYPTO, "init_connections: failed to set peer public key for client %s", client->name); |
||||
} else { |
||||
DEBUG_INFO(DEBUG_CATEGORY_CRYPTO, "init_connections: successfully set peer public key for client %s", client->name); |
||||
} |
||||
} else { |
||||
DEBUG_WARN(DEBUG_CATEGORY_CONFIG, "init_connections: no peer public key configured for client %s", client->name); |
||||
} |
||||
|
||||
// Create links for this client
|
||||
struct CFG_CLIENT_LINK* client_link = client->links; |
||||
while (client_link) { |
||||
// Find the local server for this link
|
||||
struct CFG_SERVER* local_server = client_link->local_srv; |
||||
if (!local_server) { |
||||
client_link = client_link->next; |
||||
continue; |
||||
} |
||||
|
||||
// Find the socket for this server
|
||||
struct ETCP_SOCKET* e_sock = NULL; |
||||
struct ETCP_SOCKET* sock = instance->etcp_sockets; |
||||
while (sock) { |
||||
if (sock->local_addr.ss_family == local_server->ip.ss_family) { |
||||
if (sock->local_addr.ss_family == AF_INET) { |
||||
struct sockaddr_in* sock_addr = (struct sockaddr_in*)&sock->local_addr; |
||||
struct sockaddr_in* srv_addr = (struct sockaddr_in*)&local_server->ip; |
||||
if (sock_addr->sin_addr.s_addr == srv_addr->sin_addr.s_addr &&
|
||||
sock_addr->sin_port == srv_addr->sin_port) { |
||||
e_sock = sock; |
||||
break; |
||||
} |
||||
} |
||||
} |
||||
sock = sock->next; |
||||
} |
||||
|
||||
if (!e_sock) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "No socket found for client %s link", client->name); |
||||
client_link = client_link->next; |
||||
continue; |
||||
} |
||||
|
||||
// Create link for this client connection
|
||||
struct ETCP_LINK* link = etcp_link_new(etcp_conn, e_sock, &client_link->remote_addr, 0); // 0 = client initiates
|
||||
if (!link) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "Failed to create link for client %s", client->name); |
||||
client_link = client_link->next; |
||||
continue; |
||||
} |
||||
|
||||
client_link = client_link->next; |
||||
} |
||||
|
||||
DEBUG_INFO(DEBUG_CATEGORY_CONNECTION, "Added client %s with %d links", client->name, client->keepalive); |
||||
client = client->next; |
||||
} |
||||
|
||||
// If there are clients configured but no connections created, that's an error
|
||||
// If there are no clients (server-only mode), 0 connections is OK (server will accept incoming)
|
||||
if (instance->connections_count == 0 && config->clients != NULL) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "Clients configured but no connections initialized"); |
||||
return -1; |
||||
} |
||||
|
||||
DEBUG_INFO(DEBUG_CATEGORY_CONNECTION, "Initialized %d connections", instance->connections_count); |
||||
return 0; |
||||
} |
||||
|
||||
// Wrapper for backward compatibility
|
||||
struct ETCP_SOCKET* etcp_socket_add(struct UTUN_INSTANCE* instance, struct sockaddr_storage* ip, uint32_t netif_index, int so_mark, uint8_t type) { |
||||
return etcp_socket_add_ex(instance, ip, netif_index, so_mark, type, 0); |
||||
} |
||||
@ -0,0 +1,100 @@
|
||||
#ifndef ETCP_CONNECTIONS_H |
||||
#define ETCP_CONNECTIONS_H |
||||
|
||||
// подмодуль ETCP который обслуживает сокеты ETCP для приёма-передачи пакетов и одно ETCP подключение через несколько каналов связи (failover)
|
||||
|
||||
#include "secure_channel.h" |
||||
#include "utun_instance.h" |
||||
#include <stdint.h> |
||||
#include <sys/socket.h> |
||||
|
||||
#define PACKET_DATA_SIZE 1536 |
||||
|
||||
// Типы кодограмм протокола
|
||||
#define ETCP_INIT_REQUEST 0x02 |
||||
#define ETCP_INIT_RESPONSE 0x03 |
||||
#define ETCP_CHANNEL_INIT 0x04 |
||||
#define ETCP_CHANNEL_RESPONSE 0x05 |
||||
|
||||
|
||||
struct ETCP_DGRAM {// пакет (незашифрованный)
|
||||
struct ETCP_LINK* link;// откуда получена или куда отправялем
|
||||
uint16_t data_len;// общий размер пакета не включая timestamp
|
||||
uint16_t noencrypt_len;// число байт (с конца) которые не надо шифровать. для передачи pubkey
|
||||
uint16_t timestamp;// timestamp принятого или для отправки
|
||||
uint8_t data[0];// данные пакета (без timestamp)
|
||||
}; |
||||
|
||||
// список активных подключений которые обслуживает сокет. каждый сокет может обслуживать много подключений
|
||||
struct ETCP_SOCKET { |
||||
struct ETCP_SOCKET* next; // Linked list для всех соединений
|
||||
struct UTUN_INSTANCE* instance; |
||||
int fd; // Файловый дескриптор UDP сокета
|
||||
struct sockaddr_storage local_addr; // Локальный адрес
|
||||
|
||||
// для входящих подключений (links) - массив упорядоченный по ip_port_hash
|
||||
size_t max_channels; // сколько выделено памяти
|
||||
size_t num_channels; // сколько активно
|
||||
struct ETCP_LINK** links;// массив указателей на линки, сортированный по ip_port_hash
|
||||
int errorcode; |
||||
size_t pkt_format_errors; |
||||
|
||||
void* socket_id; // Socket ID from uasync_add_socket
|
||||
}; |
||||
|
||||
// ETCP Link - одно динамическое соединение (один путь)
|
||||
struct ETCP_LINK { |
||||
uint32_t ip_port_hash; // crc32 для быстрого поиска
|
||||
struct ETCP_LINK* next; // Linked list подключений для ETCP_CONN (каждое подключение это child для ETCP_CONN)
|
||||
|
||||
struct ETCP_CONN* etcp; // подключение (parent)
|
||||
struct ETCP_SOCKET* conn; // сокет через который работаем
|
||||
|
||||
// Путь соединения
|
||||
struct sockaddr_storage remote_addr; // Удалённый адрес
|
||||
|
||||
// Параметры соединения
|
||||
uint16_t mtu; // MTU удаленного узла
|
||||
uint16_t keepalive_interval; // Keepalive интервал
|
||||
uint8_t is_server; // инициирует подключение клиент
|
||||
uint8_t initialized; // Флаг инициализации (1=подтверждено или получен request)
|
||||
|
||||
// Состояние установки соединения (только для клиентов)
|
||||
void* init_timer; // Таймер для повторов INIT (NULL=не подключается)
|
||||
uint16_t init_timeout; // Текущий таймаут в мс
|
||||
uint16_t init_retry_count; // Счетчик попыток
|
||||
|
||||
uint64_t last_activity; // Время последней активности
|
||||
uint64_t last_recv_local_time; |
||||
uint16_t last_recv_timestamp; |
||||
|
||||
size_t encrypt_errors; |
||||
size_t decrypt_errors; |
||||
size_t send_errors; |
||||
size_t recv_errors; |
||||
size_t total_encrypted; |
||||
size_t total_decrypted; |
||||
|
||||
uint32_t bandwidth; // Link bandwidth in bits/sec
|
||||
}; |
||||
|
||||
// INITIALIZATION (создаёт listen-сокеты и подключения из конфига)
|
||||
int init_connections(struct UTUN_INSTANCE* instance); |
||||
|
||||
// SOCKET FUNCTIONS
|
||||
// добавляет новый версер (сокет для приёма и отправки кодограмм. обслуживает много подключений)
|
||||
struct ETCP_SOCKET* etcp_socket_add(struct UTUN_INSTANCE* instance, struct sockaddr_storage* ip, uint32_t netif_index, int so_mark, uint8_t type); |
||||
struct ETCP_SOCKET* etcp_socket_add_ex(struct UTUN_INSTANCE* instance, struct sockaddr_storage* ip, uint32_t netif_index, int so_mark, uint8_t type, uint32_t flags); |
||||
// удаляет сокет и освобождает ресурсы (грохает все его подключения и сокет)
|
||||
void etcp_socket_remove(struct ETCP_SOCKET* conn); |
||||
|
||||
// connection functions
|
||||
// создает новый канал связи для etcp подключения (ETCP_CONN)
|
||||
struct ETCP_LINK* etcp_link_new(struct ETCP_CONN* etcp, struct ETCP_SOCKET* conn, struct sockaddr_storage* remote_addr, uint8_t is_server); |
||||
void etcp_link_close(struct ETCP_LINK* link); |
||||
//int etcp_input_cbk(struct packet_buffer* pkt, struct ETCP_SOCKET* conn);// получает расшифрованный пакет
|
||||
int etcp_encrypt_send(struct ETCP_DGRAM* dgram);// зашифровывает и отправляет пакет
|
||||
// find link by address
|
||||
struct ETCP_LINK* etcp_link_find_by_addr(struct ETCP_SOCKET* e_sock, struct sockaddr_storage* addr); |
||||
|
||||
#endif // ETCP_CONNECTIONS_H
|
||||
@ -0,0 +1,164 @@
|
||||
// etcp_loadbalancer.c - Load Balancer Implementation (based on etcp_protocol.txt)
|
||||
#include "etcp_loadbalancer.h" |
||||
#include "../lib/debug_config.h" |
||||
#include "../lib/u_async.h" |
||||
#include <stdlib.h> |
||||
|
||||
// Enable comprehensive debug output for loadbalancer module
|
||||
#define DEBUG_CATEGORY_LOADBALANCER 1 |
||||
|
||||
// Forward declaration
|
||||
static void init_timeout_cb(void* arg); |
||||
|
||||
// Constants
|
||||
#define TIMEBASE_NS 100000 // 0.1ms = 100us = 100000ns
|
||||
#define DELTA_TIME_NS 10000 // Example delta
|
||||
|
||||
// Internal
|
||||
static uint64_t get_current_nanotime() { |
||||
struct timespec ts; |
||||
clock_gettime(CLOCK_MONOTONIC, &ts); |
||||
uint64_t nanotime = (uint64_t)ts.tv_sec * 1000000000ULL + ts.tv_nsec; |
||||
DEBUG_TRACE(DEBUG_CATEGORY_ETCP, "get_current_nanotime: tv_sec=%ld, tv_nsec=%ld, result=%llu", |
||||
ts.tv_sec, ts.tv_nsec, (unsigned long long)nanotime); |
||||
return nanotime; |
||||
} |
||||
|
||||
// Select link for transmission (per spec)
|
||||
struct ETCP_LINK* etcp_loadbalancer_select_link(struct ETCP_CONN* etcp) { |
||||
DEBUG_DEBUG(DEBUG_CATEGORY_ETCP, "etcp_loadbalancer_select_link: etcp=%p", etcp); |
||||
|
||||
if (!etcp || !etcp->links) { |
||||
DEBUG_WARN(DEBUG_CATEGORY_ETCP, "etcp_loadbalancer_select_link: invalid parameters (etcp=%p, links=%p)",
|
||||
etcp, etcp ? etcp->links : NULL); |
||||
return NULL; |
||||
} |
||||
|
||||
struct ETCP_LINK* best = NULL; |
||||
uint64_t min_load_time = UINT64_MAX; |
||||
uint64_t now_ns = get_current_nanotime(); |
||||
uint64_t now_tb = now_ns / (TIMEBASE_NS / 10); // To 0.1ms units
|
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_ETCP, "etcp_loadbalancer_select_link: current time=%llu tb, scanning %d links",
|
||||
(unsigned long long)now_tb, etcp->links ? 1 : 0); |
||||
|
||||
struct ETCP_LINK* link = etcp->links; |
||||
int link_index = 0; |
||||
while (link) { |
||||
link_index++; |
||||
DEBUG_TRACE(DEBUG_CATEGORY_ETCP, "etcp_loadbalancer_select_link: evaluating link %d (%p) - initialized=%u, is_server=%u, bandwidth=%u", |
||||
link_index, link, link->initialized, link->is_server, link->bandwidth); |
||||
|
||||
if (!link->initialized) { |
||||
DEBUG_DEBUG(DEBUG_CATEGORY_ETCP, "etcp_loadbalancer_select_link: link %d not initialized, checking if client connection needed", |
||||
link_index); |
||||
// Initiate connection if client
|
||||
if (!link->is_server && !link->init_timer) { |
||||
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "etcp_loadbalancer_select_link: initiating client connection for link %d", link_index); |
||||
// Send INIT (via etcp_encrypt_send with special dgram)
|
||||
// Set timer
|
||||
link->init_timer = uasync_set_timeout(etcp->instance->ua, link->init_timeout, link, init_timeout_cb); // Define cb
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_ETCP, "etcp_loadbalancer_select_link: set init timer for link %d", link_index); |
||||
} |
||||
link = link->next; |
||||
continue; |
||||
} |
||||
|
||||
// Update load time if inactive
|
||||
if (link->last_activity < now_tb - (DELTA_TIME_NS / (TIMEBASE_NS / 10))) { |
||||
DEBUG_TRACE(DEBUG_CATEGORY_ETCP, "etcp_loadbalancer_select_link: link %d inactive, updating last_activity from %llu to %llu", |
||||
link_index, (unsigned long long)link->last_activity,
|
||||
(unsigned long long)(now_tb - (DELTA_TIME_NS / (TIMEBASE_NS / 10)))); |
||||
link->last_activity = now_tb - (DELTA_TIME_NS / (TIMEBASE_NS / 10)); |
||||
} |
||||
|
||||
// Check if can send (load time < now)
|
||||
DEBUG_TRACE(DEBUG_CATEGORY_ETCP, "etcp_loadbalancer_select_link: link %d last_activity=%llu, now_tb=%llu", |
||||
link_index, (unsigned long long)link->last_activity, (unsigned long long)now_tb); |
||||
|
||||
if (link->last_activity < now_tb) { |
||||
DEBUG_DEBUG(DEBUG_CATEGORY_ETCP, "etcp_loadbalancer_select_link: link %d can send (load_time < now)", link_index); |
||||
if (link->last_activity < min_load_time) { |
||||
DEBUG_DEBUG(DEBUG_CATEGORY_ETCP, "etcp_loadbalancer_select_link: link %d is better candidate (activity=%llu < %llu)", |
||||
link_index, (unsigned long long)link->last_activity, (unsigned long long)min_load_time); |
||||
min_load_time = link->last_activity; |
||||
best = link; |
||||
} |
||||
} else { |
||||
DEBUG_TRACE(DEBUG_CATEGORY_ETCP, "etcp_loadbalancer_select_link: link %d cannot send yet (load_time >= now)", link_index); |
||||
} |
||||
|
||||
link = link->next; |
||||
} |
||||
|
||||
if (best) { |
||||
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "etcp_loadbalancer_select_link: selected link %p (activity=%llu)",
|
||||
best, (unsigned long long)min_load_time); |
||||
} else { |
||||
DEBUG_WARN(DEBUG_CATEGORY_ETCP, "etcp_loadbalancer_select_link: no suitable link found"); |
||||
} |
||||
|
||||
return best; |
||||
} |
||||
|
||||
// Init timeout callback (placeholder)
|
||||
static void init_timeout_cb(void* arg) { |
||||
struct ETCP_LINK* link = (struct ETCP_LINK*)arg; |
||||
DEBUG_DEBUG(DEBUG_CATEGORY_ETCP, "init_timeout_cb: link=%p, retry_count=%u, timeout=%u",
|
||||
link, link->init_retry_count, link->init_timeout); |
||||
|
||||
// Resend INIT, increment retry
|
||||
link->init_retry_count++; |
||||
DEBUG_DEBUG(DEBUG_CATEGORY_ETCP, "init_timeout_cb: incremented retry_count to %u", link->init_retry_count); |
||||
|
||||
if (link->init_retry_count > 5) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "init_timeout_cb: max retries exceeded (%u > 5), closing link", link->init_retry_count); |
||||
// Fail
|
||||
etcp_link_close(link); |
||||
return; |
||||
} |
||||
|
||||
// Resend...
|
||||
link->init_timeout *= 2; // Backoff
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_ETCP, "init_timeout_cb: doubled timeout to %u ms", link->init_timeout); |
||||
link->init_timer = uasync_set_timeout(link->etcp->instance->ua, link->init_timeout, link, init_timeout_cb); |
||||
DEBUG_DEBUG(DEBUG_CATEGORY_ETCP, "init_timeout_cb: rescheduled timer"); |
||||
} |
||||
|
||||
// Bandwidth limit update (called after send)
|
||||
void etcp_loadbalancer_update_after_send(struct ETCP_LINK* link, size_t pkt_size) { |
||||
DEBUG_TRACE(DEBUG_CATEGORY_ETCP, "etcp_loadbalancer_update_after_send: link=%p, pkt_size=%zu", link, pkt_size); |
||||
|
||||
if (!link) { |
||||
DEBUG_WARN(DEBUG_CATEGORY_ETCP, "etcp_loadbalancer_update_after_send: called with NULL link"); |
||||
return; |
||||
} |
||||
|
||||
if (link->bandwidth == 0) { |
||||
DEBUG_TRACE(DEBUG_CATEGORY_ETCP, "etcp_loadbalancer_update_after_send: link bandwidth is 0, skipping update"); |
||||
return; |
||||
} |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_ETCP, "etcp_loadbalancer_update_after_send: link bandwidth=%u bits/sec, packet size=%zu bytes",
|
||||
link->bandwidth, pkt_size); |
||||
|
||||
// Time to transmit (ns)
|
||||
double byte_time_ns = 1000000000.0 / (link->bandwidth * 8.0); // bits/sec to byte/ns
|
||||
uint64_t tx_time_ns = (uint64_t)(pkt_size * byte_time_ns); |
||||
DEBUG_DEBUG(DEBUG_CATEGORY_ETCP, "etcp_loadbalancer_update_after_send: byte_time_ns=%.2f, tx_time_ns=%llu",
|
||||
byte_time_ns, (unsigned long long)tx_time_ns); |
||||
|
||||
// To timebase (0.1ms units)
|
||||
uint64_t tx_time_tb = tx_time_ns / (TIMEBASE_NS / 10); |
||||
DEBUG_DEBUG(DEBUG_CATEGORY_ETCP, "etcp_loadbalancer_update_after_send: tx_time_tb=%llu", (unsigned long long)tx_time_tb); |
||||
|
||||
// Update last_activity
|
||||
uint64_t now_tb = get_current_nanotime() / (TIMEBASE_NS / 10); |
||||
uint64_t old_activity = link->last_activity; |
||||
link->last_activity += tx_time_tb; |
||||
if (link->last_activity < now_tb - (DELTA_TIME_NS / (TIMEBASE_NS / 10))) { |
||||
link->last_activity = now_tb - (DELTA_TIME_NS / (TIMEBASE_NS / 10)); |
||||
} |
||||
DEBUG_DEBUG(DEBUG_CATEGORY_ETCP, "etcp_loadbalancer_update_after_send: updated last_activity from %llu to %llu",
|
||||
(unsigned long long)old_activity, (unsigned long long)link->last_activity); |
||||
} |
||||
@ -0,0 +1,20 @@
|
||||
// etcp_loadbalancer.h - Load Balancer for ETCP Channels
|
||||
#ifndef ETCP_LOADBALANCER_H |
||||
#define ETCP_LOADBALANCER_H |
||||
|
||||
#include "etcp.h" |
||||
|
||||
#ifdef __cplusplus |
||||
extern "C" { |
||||
#endif |
||||
|
||||
// Functions
|
||||
struct ETCP_LINK* etcp_loadbalancer_select_link(struct ETCP_CONN* etcp); |
||||
|
||||
// Add more as needed (e.g., update bandwidth)
|
||||
|
||||
#ifdef __cplusplus |
||||
} |
||||
#endif |
||||
|
||||
#endif // ETCP_LOADBALANCER_H
|
||||
@ -0,0 +1,42 @@
|
||||
#include <stdio.h> |
||||
#include <stdint.h> |
||||
#include <string.h> |
||||
#include <arpa/inet.h> |
||||
#include <netinet/in.h> |
||||
|
||||
// Packet dump function for debugging
|
||||
static void dump_packet(const char* direction, const uint8_t* data, size_t len, struct sockaddr_storage* addr) { |
||||
printf("[DUMP] %s packet: %zd bytes ", direction, len); |
||||
|
||||
// Print address if provided
|
||||
if (addr) { |
||||
if (addr->ss_family == AF_INET) { |
||||
struct sockaddr_in* sin = (struct sockaddr_in*)addr; |
||||
printf("to/from %s:%d ",
|
||||
inet_ntoa(sin->sin_addr),
|
||||
ntohs(sin->sin_port)); |
||||
} |
||||
} |
||||
|
||||
// Print first 64 bytes
|
||||
printf("data: "); |
||||
for (size_t i = 0; i < len && i < 64; i++) { |
||||
printf("%02x", data[i]); |
||||
if (i % 4 == 3) printf(" "); |
||||
} |
||||
if (len > 64) printf("..."); |
||||
|
||||
// If it's INIT packet, parse fields
|
||||
if (len > 0 && data[0] == 0x02) { // ETCP_INIT_REQUEST
|
||||
if (len >= 15) { // Minimum INIT size: code(1) + node_id(8) + mtu(2) + keepalive(2) + pubkey(2 at least)
|
||||
uint64_t node_id = 0; |
||||
memcpy(&node_id, data + 1, 8); |
||||
uint16_t mtu = (data[9] << 8) | data[10]; |
||||
uint16_t keepalive = (data[11] << 8) | data[12]; |
||||
printf(" | INIT: node_id=%llu mtu=%d keepalive=%d",
|
||||
(unsigned long long)node_id, mtu, keepalive); |
||||
} |
||||
} |
||||
|
||||
printf("\n"); |
||||
} |
||||
@ -0,0 +1,604 @@
|
||||
#include "pkt_normalizer.h" |
||||
#include "../lib/u_async.h" |
||||
#include <stdlib.h> |
||||
#include <string.h> |
||||
#include <stdint.h> |
||||
#include <stdio.h> |
||||
static void packer_handler(struct ll_queue* q, struct ll_entry* unused, void* arg); |
||||
static void unpacker_handler(struct ll_queue* q, struct ll_entry* unused, void* arg); |
||||
static void send_buf(struct pn_struct* pn); |
||||
static int get_header(uint8_t* header, size_t L); |
||||
/* Calculate fragment size from mtu */ |
||||
struct pn_struct* pkt_normalizer_init(uasync_t* ua, int is_packer, int mtu) { |
||||
struct pn_struct* pn = malloc(sizeof(struct pn_struct)); |
||||
if (!pn) return NULL; |
||||
pn->ua = ua; |
||||
pn->input = queue_new(ua, NULL); // No memory pool for now
|
||||
if (!pn->input) { |
||||
free(pn); |
||||
return NULL; |
||||
} |
||||
pn->output = queue_new(ua, NULL); // No memory pool for now
|
||||
if (!pn->output) { |
||||
queue_free(pn->input); |
||||
free(pn); |
||||
return NULL; |
||||
} |
||||
pn->is_packer = is_packer; |
||||
if (is_packer) { |
||||
// Calculate fragment size from mtu: fragment = mtu - 100
|
||||
int fragment_size = mtu - ETCP_OVERHEAD; |
||||
if (fragment_size < 256) fragment_size = 256; // Minimum sane value
|
||||
pn->u.packer.cap = fragment_size; |
||||
pn->u.packer.buf = malloc(pn->u.packer.cap); |
||||
if (!pn->u.packer.buf) { |
||||
queue_free(pn->input); |
||||
queue_free(pn->output); |
||||
free(pn); |
||||
return NULL; |
||||
} |
||||
pn->u.packer.len = 0; |
||||
pn->u.packer.error_count = 0; |
||||
queue_set_callback(pn->input, packer_handler, pn); |
||||
} else { |
||||
pn->u.unpacker.buf = NULL; |
||||
pn->u.unpacker.len = 0; |
||||
pn->u.unpacker.total_len = 0; |
||||
pn->u.unpacker.cap = 0; |
||||
pn->u.unpacker.error_count = 0; |
||||
queue_set_callback(pn->input, unpacker_handler, pn); |
||||
} |
||||
return pn; |
||||
} |
||||
|
||||
void pkt_normalizer_deinit(struct pn_struct* pn) { |
||||
if (!pn) return; |
||||
queue_free(pn->input); |
||||
queue_free(pn->output); |
||||
if (pn->is_packer) { |
||||
free(pn->u.packer.buf); |
||||
} else { |
||||
free(pn->u.unpacker.buf); |
||||
free(pn->u.unpacker.service_buf); |
||||
} |
||||
free(pn); |
||||
} |
||||
struct pkt_normalizer_pair* pkt_normalizer_pair_init(uasync_t* ua, int mtu) { |
||||
struct pkt_normalizer_pair* pair = malloc(sizeof(struct pkt_normalizer_pair)); |
||||
if (!pair) return NULL; |
||||
pair->packer = pkt_normalizer_init(ua, 1, mtu); |
||||
if (!pair->packer) { |
||||
free(pair); |
||||
return NULL; |
||||
} |
||||
pair->unpacker = pkt_normalizer_init(ua, 0, mtu); |
||||
if (!pair->unpacker) { |
||||
pkt_normalizer_deinit(pair->packer); |
||||
free(pair); |
||||
return NULL; |
||||
} |
||||
return pair; |
||||
} |
||||
void pkt_normalizer_pair_deinit(struct pkt_normalizer_pair* pair) { |
||||
if (!pair) return; |
||||
pkt_normalizer_deinit(pair->packer); |
||||
pkt_normalizer_deinit(pair->unpacker); |
||||
free(pair); |
||||
} |
||||
static int get_header(uint8_t* header, size_t L) { |
||||
if (L > 1535) return -1; |
||||
if (L <= 239) { |
||||
header[0] = (uint8_t)L; |
||||
return 1; |
||||
} else { |
||||
uint8_t high = (uint8_t)(L >> 8); |
||||
if (high > 5) return -1; |
||||
header[0] = 0xF0 + high; |
||||
header[1] = (uint8_t)(L & 0xFF); |
||||
return 2; |
||||
} |
||||
} |
||||
/* Сбросить состояние сборки фрагментов */ |
||||
static void reset_fragment_state(struct pn_struct* pn) { |
||||
if (!pn->is_packer) { |
||||
pn->u.unpacker.len = 0; |
||||
pn->u.unpacker.total_len = 0; |
||||
pn->u.unpacker.in_fragment = 0; |
||||
} |
||||
} |
||||
/* Таймаут для сборки фрагментов */ |
||||
static void send_buf(struct pn_struct* pn) { |
||||
if (pn->u.packer.len == 0) return; |
||||
size_t payload_len = pn->u.packer.len; |
||||
struct ll_entry* out = queue_entry_new(2 + payload_len); |
||||
if (!out) return; |
||||
uint8_t* d = ll_entry_data(out); |
||||
*(uint16_t*)d = (uint16_t)payload_len; |
||||
memcpy(d + 2, pn->u.packer.buf, payload_len); |
||||
queue_entry_put(pn->output, out); |
||||
pn->u.packer.len = 0; |
||||
} |
||||
static void packer_handler(struct ll_queue* q, struct ll_entry* unused, void* arg) { |
||||
(void)unused; |
||||
struct pn_struct* pn = arg; |
||||
size_t max = (size_t)1400; |
||||
struct ll_entry* entry = queue_entry_get(q); |
||||
if (!entry) { |
||||
queue_resume_callback(q); |
||||
return; |
||||
} |
||||
size_t L = ll_entry_size(entry); |
||||
uint8_t* data = ll_entry_data(entry); |
||||
uint8_t header[2]; |
||||
int hsize = get_header(header, L); |
||||
size_t needed = (size_t)hsize + L; |
||||
if (hsize < 0 || needed > max) { |
||||
// Fragment
|
||||
if (pn->u.packer.len > 0) { |
||||
send_buf(pn); |
||||
} |
||||
size_t remaining = L; |
||||
size_t pos = 0; |
||||
int fragment_count = 0; |
||||
while (remaining > 0) { |
||||
size_t chunk; |
||||
size_t payload_len; |
||||
struct ll_entry* fout; |
||||
uint8_t* fd; |
||||
uint8_t frag_header[2]; |
||||
int frag_hsize; |
||||
if (fragment_count == 0) { |
||||
// Первый фрагмент: FF + общая длина (2 байта)
|
||||
chunk = remaining > (max - 5) ? (max - 5) : remaining; // 2+1+2+chunk <= max
|
||||
payload_len = 1 + 2 + chunk; // FF + total_len + data
|
||||
fout = queue_entry_new(2 + payload_len); |
||||
if (!fout) { |
||||
break; |
||||
} |
||||
fd = ll_entry_data(fout); |
||||
*(uint16_t*)fd = (uint16_t)payload_len; |
||||
fd += 2; |
||||
*fd++ = 0xFF; |
||||
*fd++ = (uint8_t)(L >> 8); // старший байт общей длины
|
||||
*fd++ = (uint8_t)(L & 0xFF); // младший байт общей длины
|
||||
} else { |
||||
// Не первый фрагмент
|
||||
if (remaining <= max - 3) { |
||||
// Это последний возможный фрагмент (помещается в один пакет с префиксом FE)
|
||||
// Пытаемся отправить как обычный блок
|
||||
frag_hsize = get_header(frag_header, remaining); |
||||
if (frag_hsize > 0 && (size_t)frag_hsize + remaining + 2 <= max) { |
||||
// Успешно: обычный блок
|
||||
payload_len = frag_hsize + remaining; |
||||
chunk = remaining; |
||||
fout = queue_entry_new(2 + payload_len); |
||||
if (!fout) { |
||||
break; |
||||
} |
||||
fd = ll_entry_data(fout); |
||||
*(uint16_t*)fd = (uint16_t)payload_len; |
||||
fd += 2; |
||||
memcpy(fd, frag_header, frag_hsize); |
||||
fd += frag_hsize; |
||||
} else { |
||||
// Не удалось отправить как обычный блок - разбиваем на 2 фрагмента
|
||||
// 1. FE фрагмент с частью данных
|
||||
// 2. Обычный блок с оставшимися данными
|
||||
// Находим максимальный размер для FE фрагмента
|
||||
size_t max_fe_data = max - 3; // 2 байта длины + 0xFE
|
||||
if (max_fe_data > remaining) { |
||||
max_fe_data = remaining; |
||||
} |
||||
// Пробуем различные размеры, начиная с максимального
|
||||
size_t fe_data_size = 0; |
||||
for (size_t try_fe = max_fe_data; try_fe > 0; try_fe--) { |
||||
size_t try_regular = remaining - try_fe; |
||||
if (try_regular == 0) continue; // Нужно отправить что-то как обычный блок
|
||||
uint8_t test_header[2]; |
||||
int hsize = get_header(test_header, try_regular); |
||||
if (hsize <= 0) continue; |
||||
if ((size_t)hsize + try_regular + 2 <= max) { |
||||
fe_data_size = try_fe; |
||||
break; |
||||
} |
||||
} |
||||
if (fe_data_size == 0) { |
||||
// Не удалось найти разбиение - ошибка
|
||||
pn->u.packer.error_count++; |
||||
// Отправляем как FE (нарушение спецификации, но это крайний случай)
|
||||
chunk = remaining > (max - 3) ? (max - 3) : remaining; |
||||
payload_len = 1 + chunk; // FE + data
|
||||
fout = queue_entry_new(2 + payload_len); |
||||
if (!fout) break; |
||||
fd = ll_entry_data(fout); |
||||
*(uint16_t*)fd = (uint16_t)payload_len; |
||||
fd += 2; |
||||
*fd++ = 0xFE; |
||||
} else { |
||||
// Отправляем FE фрагмент
|
||||
chunk = fe_data_size; |
||||
payload_len = 1 + chunk; // FE + data
|
||||
fout = queue_entry_new(2 + payload_len); |
||||
if (!fout) break; |
||||
fd = ll_entry_data(fout); |
||||
*(uint16_t*)fd = (uint16_t)payload_len; |
||||
fd += 2; |
||||
*fd++ = 0xFE; |
||||
memcpy(fd, data + pos, chunk); |
||||
queue_entry_put(pn->output, fout); |
||||
pos += chunk; |
||||
remaining -= chunk; |
||||
fragment_count++; |
||||
// Обновляем оставшиеся данные для обычного блока
|
||||
// (цикл продолжит обработку на следующей итерации)
|
||||
continue; |
||||
} |
||||
} |
||||
} else { |
||||
// Промежуточный фрагмент, отправляем как FE
|
||||
chunk = remaining > (max - 3) ? (max - 3) : remaining; |
||||
payload_len = 1 + chunk; // FE + data
|
||||
fout = queue_entry_new(2 + payload_len); |
||||
if (!fout) { |
||||
break; |
||||
} |
||||
fd = ll_entry_data(fout); |
||||
*(uint16_t*)fd = (uint16_t)payload_len; |
||||
fd += 2; |
||||
*fd++ = 0xFE; |
||||
} |
||||
} |
||||
memcpy(fd, data + pos, chunk); |
||||
queue_entry_put(pn->output, fout); |
||||
pos += chunk; |
||||
remaining -= chunk; |
||||
fragment_count++; |
||||
} |
||||
} else { |
||||
if (pn->u.packer.len + needed > max) { |
||||
send_buf(pn); |
||||
} |
||||
// Add to buffer
|
||||
uint8_t* p = pn->u.packer.buf + pn->u.packer.len; |
||||
memcpy(p, header, (size_t)hsize); |
||||
memcpy(p + hsize, data, L); |
||||
pn->u.packer.len += needed; |
||||
} |
||||
queue_entry_free(entry); |
||||
if (pn->u.packer.len > 0) { |
||||
send_buf(pn); |
||||
} |
||||
queue_resume_callback(q); |
||||
} |
||||
void pkt_normalizer_set_service_callback(struct pn_struct* pn, pkt_normalizer_service_callback_t callback, void* user_data) { |
||||
if (!pn) return; |
||||
pn->service_callback = callback; |
||||
pn->service_callback_user_data = user_data; |
||||
} |
||||
void pkt_normalizer_reset_service_state(struct pn_struct* pn) { |
||||
if (!pn || pn->is_packer) return; |
||||
if (pn->u.unpacker.in_service) { |
||||
// Deliver pending service packet
|
||||
if (pn->service_callback) { |
||||
pn->service_callback(pn->service_callback_user_data, |
||||
pn->u.unpacker.service_type, |
||||
pn->u.unpacker.service_buf, |
||||
pn->u.unpacker.service_len); |
||||
} |
||||
free(pn->u.unpacker.service_buf); |
||||
pn->u.unpacker.service_buf = NULL; |
||||
pn->u.unpacker.service_len = 0; |
||||
pn->u.unpacker.service_cap = 0; |
||||
pn->u.unpacker.in_service = 0; |
||||
} |
||||
} |
||||
void pkt_normalizer_reset_state(struct pn_struct* pn) { |
||||
if (!pn) return; |
||||
if (pn->is_packer) { |
||||
// Flush packer buffer
|
||||
if (pn->u.packer.len > 0) { |
||||
send_buf(pn); |
||||
} |
||||
} else { |
||||
// Reset unpacker fragment state
|
||||
reset_fragment_state(pn); |
||||
// Reset service state
|
||||
pkt_normalizer_reset_service_state(pn); |
||||
} |
||||
} |
||||
int pkt_normalizer_send_service(struct pn_struct* pn, uint8_t type, const void* data, size_t len) { |
||||
if (!pn || !pn->is_packer) return -1; |
||||
// Service packet ограничен 256 байтами всего
|
||||
if (len > 256 - 2) return -1; // 2 байта на заголовок (0xFC + тип)
|
||||
size_t max = (size_t)1400; |
||||
if (max < 3) return -1; |
||||
// Размер сервисного пакета: 2 байта длины + 1 байт 0xFC + 1 байт тип + данные
|
||||
// Если не помещается в один фрагмент - используем продолжение 0xFD
|
||||
size_t total_service_len = 1 + 1 + len; // 0xFC + type + data
|
||||
size_t pos = 0; |
||||
while (total_service_len > 0) { |
||||
// Определяем размер куска для этого пакета
|
||||
size_t chunk; |
||||
uint8_t service_header; |
||||
if (pos == 0) { |
||||
// Первый пакет: 0xFC + тип + часть данных
|
||||
// Максимум данных в первом пакете: max - 2 (длина) - 2 (0xFC+тип)
|
||||
size_t max_first_data = max - 4; |
||||
if (max_first_data > len) max_first_data = len; |
||||
chunk = max_first_data; |
||||
service_header = 0xFC; |
||||
} else { |
||||
// Продолжение: 0xFD + данные
|
||||
// Максимум данных: max - 2 (длина) - 1 (0xFD)
|
||||
size_t max_cont_data = max - 3; |
||||
size_t remaining = len - pos; |
||||
if (max_cont_data > remaining) max_cont_data = remaining; |
||||
chunk = max_cont_data; |
||||
service_header = 0xFD; |
||||
} |
||||
if (chunk == 0) break; |
||||
size_t payload_len = 1 + chunk + (pos == 0 ? 1 : 0); // +1 байт типа для первого пакета
|
||||
struct ll_entry* entry = queue_entry_new(2 + payload_len); |
||||
if (!entry) return -1; |
||||
uint8_t* d = ll_entry_data(entry); |
||||
*(uint16_t*)d = (uint16_t)payload_len; |
||||
d += 2; |
||||
*d++ = service_header; |
||||
if (pos == 0) { |
||||
*d++ = type; |
||||
} |
||||
memcpy(d, (const uint8_t*)data + pos, chunk); |
||||
queue_entry_put(pn->output, entry); |
||||
pos += chunk; |
||||
total_service_len -= chunk + (pos == chunk ? 2 : 1); // корректно вычитаем заголовки
|
||||
} |
||||
return 0; |
||||
} |
||||
int pkt_normalizer_get_error_count(const struct pn_struct* pn) { |
||||
if (!pn) return 0; |
||||
if (pn->is_packer) { |
||||
return pn->u.packer.error_count; |
||||
} |
||||
return pn->u.unpacker.error_count; |
||||
} |
||||
void pkt_normalizer_reset_error_count(struct pn_struct* pn) { |
||||
if (!pn) return; |
||||
if (pn->is_packer) { |
||||
pn->u.packer.error_count = 0; |
||||
} else { |
||||
pn->u.unpacker.error_count = 0; |
||||
} |
||||
} |
||||
void pkt_normalizer_flush(struct pn_struct* pn) { |
||||
if (!pn || !pn->is_packer) return; |
||||
if (pn->u.packer.len > 0) { |
||||
send_buf(pn); |
||||
} |
||||
} |
||||
static void unpacker_handler(struct ll_queue* q, struct ll_entry* unused, void* arg) { |
||||
(void)unused; |
||||
struct pn_struct* pn = arg; |
||||
while (queue_entry_count(q) > 0) { |
||||
struct ll_entry* entry = queue_entry_get(q); |
||||
uint8_t* data = ll_entry_data(entry); |
||||
size_t total = ll_entry_size(entry); |
||||
uint16_t payload_len = *(uint16_t*)data; |
||||
if (total != 2 + (size_t)payload_len) { |
||||
queue_entry_free(entry); |
||||
continue; |
||||
} |
||||
uint8_t* cg = data + 2; |
||||
size_t cg_pos = 0; |
||||
size_t cg_len = (size_t)payload_len; |
||||
while (cg_pos < cg_len) { |
||||
uint8_t byte = cg[cg_pos++]; |
||||
if (byte == 0xFF) { |
||||
/* Начало нового фрагментированного пакета */ |
||||
if (pn->u.unpacker.in_fragment) { |
||||
/* Не завершен предыдущий фрагмент - ошибка */ |
||||
pn->u.unpacker.error_count++; |
||||
reset_fragment_state(pn); |
||||
} |
||||
/* Проверить, что есть 2 байта для общей длины */ |
||||
if (cg_pos + 2 > cg_len) { |
||||
pn->u.unpacker.error_count++; |
||||
goto err; |
||||
} |
||||
/* Прочитать общую длину */ |
||||
uint16_t total_len = ((uint16_t)cg[cg_pos] << 8) | cg[cg_pos + 1]; |
||||
cg_pos += 2; |
||||
size_t chunk_len = cg_len - cg_pos; |
||||
/* Выделить буфер при необходимости */ |
||||
size_t new_len = chunk_len; |
||||
if (new_len > pn->u.unpacker.cap) { |
||||
size_t new_cap = pn->u.unpacker.cap ? pn->u.unpacker.cap * 2 : 4096; |
||||
if (new_cap < new_len) new_cap = new_len; |
||||
pn->u.unpacker.buf = realloc(pn->u.unpacker.buf, new_cap); |
||||
pn->u.unpacker.cap = new_cap; |
||||
} |
||||
memcpy(pn->u.unpacker.buf, cg + cg_pos, chunk_len); |
||||
pn->u.unpacker.len = chunk_len; |
||||
pn->u.unpacker.total_len = total_len; |
||||
pn->u.unpacker.in_fragment = 1; |
||||
cg_pos += chunk_len; |
||||
/* Проверить, не собрали ли уже весь пакет */ |
||||
if (pn->u.unpacker.len >= pn->u.unpacker.total_len) { |
||||
if (pn->u.unpacker.len == pn->u.unpacker.total_len) { |
||||
struct ll_entry* out = queue_entry_new(pn->u.unpacker.total_len); |
||||
if (out) { |
||||
memcpy(ll_entry_data(out), pn->u.unpacker.buf, pn->u.unpacker.total_len); |
||||
queue_entry_put(pn->output, out); |
||||
} |
||||
} else { |
||||
/* Слишком много данных - ошибка */ |
||||
pn->u.unpacker.error_count++; |
||||
} |
||||
reset_fragment_state(pn); |
||||
} |
||||
continue; |
||||
} |
||||
if (byte == 0xFE) { |
||||
/* Продолжение фрагментированного пакета */ |
||||
if (!pn->u.unpacker.in_fragment) { |
||||
/* Не было начала фрагмента - ошибка */ |
||||
pn->u.unpacker.error_count++; |
||||
goto err; |
||||
} |
||||
size_t chunk_len = cg_len - cg_pos; |
||||
size_t new_len = pn->u.unpacker.len + chunk_len; |
||||
if (new_len > pn->u.unpacker.cap) { |
||||
size_t new_cap = pn->u.unpacker.cap ? pn->u.unpacker.cap * 2 : 4096; |
||||
if (new_cap < new_len) new_cap = new_len; |
||||
pn->u.unpacker.buf = realloc(pn->u.unpacker.buf, new_cap); |
||||
pn->u.unpacker.cap = new_cap; |
||||
} |
||||
memcpy(pn->u.unpacker.buf + pn->u.unpacker.len, cg + cg_pos, chunk_len); |
||||
pn->u.unpacker.len = new_len; |
||||
cg_pos += chunk_len; |
||||
/* Проверить, не собрали ли уже весь пакет */ |
||||
if (pn->u.unpacker.len >= pn->u.unpacker.total_len) { |
||||
if (pn->u.unpacker.len == pn->u.unpacker.total_len) { |
||||
struct ll_entry* out = queue_entry_new(pn->u.unpacker.total_len); |
||||
if (out) { |
||||
memcpy(ll_entry_data(out), pn->u.unpacker.buf, pn->u.unpacker.total_len); |
||||
queue_entry_put(pn->output, out); |
||||
} |
||||
} else { |
||||
/* Слишком много данных - ошибка */ |
||||
pn->u.unpacker.error_count++; |
||||
} |
||||
reset_fragment_state(pn); |
||||
} |
||||
continue; |
||||
} |
||||
if (byte == 0xFC || byte == 0xFD) { |
||||
/* Service packet */ |
||||
if (byte == 0xFC) { |
||||
/* Start of service packet */ |
||||
if (pn->u.unpacker.in_service) { |
||||
/* Previous service packet finished - deliver it */ |
||||
if (pn->service_callback) { |
||||
pn->service_callback(pn->service_callback_user_data, |
||||
pn->u.unpacker.service_type, |
||||
pn->u.unpacker.service_buf, |
||||
pn->u.unpacker.service_len); |
||||
} |
||||
free(pn->u.unpacker.service_buf); |
||||
pn->u.unpacker.service_buf = NULL; |
||||
pn->u.unpacker.service_len = 0; |
||||
pn->u.unpacker.service_cap = 0; |
||||
pn->u.unpacker.in_service = 0; |
||||
} |
||||
/* Read service type */ |
||||
if (cg_pos >= cg_len) goto err; |
||||
uint8_t service_type = cg[cg_pos++]; |
||||
pn->u.unpacker.service_type = service_type; |
||||
pn->u.unpacker.in_service = 1; |
||||
pn->u.unpacker.service_len = 0; |
||||
} else { |
||||
/* 0xFD - continuation */ |
||||
if (!pn->u.unpacker.in_service) { |
||||
/* No service packet started - error */ |
||||
pn->u.unpacker.error_count++; |
||||
goto err; |
||||
} |
||||
} |
||||
/* Read data */ |
||||
size_t data_len = cg_len - cg_pos; |
||||
if (data_len > 0) { |
||||
size_t new_len = pn->u.unpacker.service_len + data_len; |
||||
if (new_len > 256) { |
||||
/* Service packet too long - error */ |
||||
pn->u.unpacker.error_count++; |
||||
free(pn->u.unpacker.service_buf); |
||||
pn->u.unpacker.service_buf = NULL; |
||||
pn->u.unpacker.service_len = 0; |
||||
pn->u.unpacker.service_cap = 0; |
||||
pn->u.unpacker.in_service = 0; |
||||
goto err; |
||||
} |
||||
if (new_len > pn->u.unpacker.service_cap) { |
||||
size_t new_cap = pn->u.unpacker.service_cap ? pn->u.unpacker.service_cap * 2 : 256; |
||||
if (new_cap < new_len) new_cap = new_len; |
||||
if (new_cap > 256) new_cap = 256; |
||||
uint8_t* new_buf = realloc(pn->u.unpacker.service_buf, new_cap); |
||||
if (!new_buf) { |
||||
pn->u.unpacker.error_count++; |
||||
goto err; |
||||
} |
||||
pn->u.unpacker.service_buf = new_buf; |
||||
pn->u.unpacker.service_cap = new_cap; |
||||
} |
||||
memcpy(pn->u.unpacker.service_buf + pn->u.unpacker.service_len, cg + cg_pos, data_len); |
||||
pn->u.unpacker.service_len = new_len; |
||||
cg_pos += data_len; |
||||
} |
||||
/* Check if this is the end of service packet (end of payload) */ |
||||
if (cg_pos >= cg_len) { |
||||
/* End of current payload, but service packet may continue in next transport packet */ |
||||
continue; |
||||
} else { |
||||
/* There is more data in this payload after service packet - error */ |
||||
pn->u.unpacker.error_count++; |
||||
free(pn->u.unpacker.service_buf); |
||||
pn->u.unpacker.service_buf = NULL; |
||||
pn->u.unpacker.service_len = 0; |
||||
pn->u.unpacker.service_cap = 0; |
||||
pn->u.unpacker.in_service = 0; |
||||
goto err; |
||||
} |
||||
} |
||||
/* Обычная запись (не фрагмент) */ |
||||
size_t L; |
||||
if (byte <= 0xEF) { |
||||
L = byte; |
||||
} else if (byte >= 0xF0 && byte <= 0xF5) { |
||||
if (cg_pos >= cg_len) goto err; |
||||
uint8_t ext = cg[cg_pos++]; |
||||
L = ((size_t)(byte - 0xF0) << 8) | ext; |
||||
} else { |
||||
/* Недопустимый байт */ |
||||
goto err; |
||||
} |
||||
if (cg_pos + L > cg_len) goto err; |
||||
if (pn->u.unpacker.in_fragment) { |
||||
/* Это последний фрагмент в виде обычной записи */ |
||||
size_t new_len = pn->u.unpacker.len + L; |
||||
if (new_len > pn->u.unpacker.cap) { |
||||
size_t new_cap = pn->u.unpacker.cap ? pn->u.unpacker.cap * 2 : 4096; |
||||
if (new_cap < new_len) new_cap = new_len; |
||||
pn->u.unpacker.buf = realloc(pn->u.unpacker.buf, new_cap); |
||||
pn->u.unpacker.cap = new_cap; |
||||
} |
||||
memcpy(pn->u.unpacker.buf + pn->u.unpacker.len, cg + cg_pos, L); |
||||
pn->u.unpacker.len = new_len; |
||||
cg_pos += L; |
||||
/* Проверить, собрали ли весь пакет */ |
||||
if (pn->u.unpacker.len >= pn->u.unpacker.total_len) { |
||||
if (pn->u.unpacker.len == pn->u.unpacker.total_len) { |
||||
struct ll_entry* out = queue_entry_new(pn->u.unpacker.total_len); |
||||
if (out) { |
||||
memcpy(ll_entry_data(out), pn->u.unpacker.buf, pn->u.unpacker.total_len); |
||||
queue_entry_put(pn->output, out); |
||||
} |
||||
} else { |
||||
/* Слишком много данных - ошибка */ |
||||
pn->u.unpacker.error_count++; |
||||
} |
||||
reset_fragment_state(pn); |
||||
} |
||||
} else { |
||||
/* Обычная запись (не часть фрагмента) */ |
||||
struct ll_entry* out = queue_entry_new(L); |
||||
if (out) { |
||||
memcpy(ll_entry_data(out), cg + cg_pos, L); |
||||
queue_entry_put(pn->output, out); |
||||
} |
||||
cg_pos += L; |
||||
} |
||||
} |
||||
err: |
||||
queue_entry_free(entry); |
||||
} |
||||
queue_resume_callback(q); |
||||
} |
||||
@ -0,0 +1,75 @@
|
||||
// pkt_normalizer.h
|
||||
#ifndef PKT_NORMALIZER_H |
||||
#define PKT_NORMALIZER_H |
||||
|
||||
#include "ll_queue.h" |
||||
#include "../lib/u_async.h" |
||||
#include <stdint.h> |
||||
|
||||
/* Default fragment reassembly timeout in uasync timebase units (0.1 ms) */ |
||||
#ifndef PKT_NORMALIZER_FRAGMENT_TIMEOUT |
||||
#define PKT_NORMALIZER_FRAGMENT_TIMEOUT 5000 /* 500 ms */ |
||||
#endif |
||||
|
||||
/* ETCP overhead for calculating fragment size from MTU */ |
||||
#define ETCP_OVERHEAD 100 // Reserve 100 bytes for headers, crypto, etc.*/
|
||||
|
||||
/* Service packet callback type */ |
||||
typedef void (*pkt_normalizer_service_callback_t)(void* user_data, uint8_t type, const uint8_t* data, size_t len); |
||||
|
||||
struct pn_struct { |
||||
struct ll_queue* input; |
||||
struct ll_queue* output; |
||||
uasync_t* ua; |
||||
int is_packer; |
||||
union { |
||||
struct { |
||||
uint8_t* buf; |
||||
size_t len; |
||||
size_t cap; |
||||
int error_count; |
||||
} packer; |
||||
struct { |
||||
uint8_t* buf; /* буфер для сборки фрагментов */ |
||||
size_t len; /* текущая накопленная длина */ |
||||
size_t total_len; /* ожидаемая общая длина из первого фрагмента */ |
||||
size_t cap; /* ёмкость буфера */ |
||||
int error_count; /* счетчик ошибок сборки */ |
||||
int in_fragment; /* флаг: идет сборка фрагментов (1) или нет (0) */ |
||||
/* Service packet reassembly */ |
||||
uint8_t* service_buf; /* буфер для сборки сервисных пакетов */ |
||||
size_t service_len; /* текущая накопленная длина сервисного пакета */ |
||||
size_t service_cap; /* ёмкость буфера сервисного пакета */ |
||||
uint8_t service_type; /* тип сервисного пакета */ |
||||
int in_service; /* флаг: идет сборка сервисного пакета (1) или нет (0) */ |
||||
} unpacker; |
||||
} u; |
||||
/* Service packet callback */ |
||||
pkt_normalizer_service_callback_t service_callback; |
||||
void* service_callback_user_data; |
||||
}; |
||||
|
||||
struct pkt_normalizer_pair { |
||||
struct pn_struct* packer; |
||||
struct pn_struct* unpacker; |
||||
}; |
||||
|
||||
struct pn_struct* pkt_normalizer_init(uasync_t* ua, int is_packer, int mtu); // 1 for packer, 0 for unpacker, mtu for fragment size calculation
|
||||
void pkt_normalizer_deinit(struct pn_struct* pn); |
||||
|
||||
struct pkt_normalizer_pair* pkt_normalizer_pair_init(uasync_t* ua, int mtu); |
||||
void pkt_normalizer_pair_deinit(struct pkt_normalizer_pair* pair); |
||||
|
||||
/* Error handling */ |
||||
int pkt_normalizer_get_error_count(const struct pn_struct* pn); |
||||
void pkt_normalizer_reset_error_count(struct pn_struct* pn); |
||||
|
||||
/* Flush internal buffer (packer only) */ |
||||
void pkt_normalizer_flush(struct pn_struct* pn); |
||||
|
||||
int pkt_normalizer_send_service(struct pn_struct* pn, uint8_t type, const void* data, size_t len); |
||||
void pkt_normalizer_set_service_callback(struct pn_struct* pn, pkt_normalizer_service_callback_t callback, void* user_data); |
||||
void pkt_normalizer_reset_service_state(struct pn_struct* pn); |
||||
void pkt_normalizer_reset_state(struct pn_struct* pn); |
||||
|
||||
#endif // PKT_NORMALIZER_H
|
||||
@ -0,0 +1,372 @@
|
||||
/**
|
||||
* @file enhanced_routing.c |
||||
* @brief Enhanced routing system with bandwidth support and route types |
||||
*/ |
||||
|
||||
#include "routing.h" |
||||
#include "etcp_connections.h" |
||||
#include "../lib/debug_config.h" |
||||
#include <stdio.h> |
||||
#include <stdlib.h> |
||||
#include <string.h> |
||||
#include <arpa/inet.h> |
||||
#include <time.h> |
||||
|
||||
#define INITIAL_ROUTE_CAPACITY 100 |
||||
#define ROUTE_EXPANSION_FACTOR 2 |
||||
#define MAX_SUBNET_VALIDATION_RANGES 50 |
||||
|
||||
// Parse subnet string
|
||||
int parse_subnet(const char *subnet_str, uint32_t *network, uint8_t *prefix_length) { |
||||
if (!subnet_str || !network || !prefix_length) return -1; |
||||
char ip[64]; |
||||
int prefix; |
||||
if (sscanf(subnet_str, "%[^/]/%d", ip, &prefix) != 2) return -1; |
||||
if (prefix < 0 || prefix > 32) return -1; |
||||
struct in_addr addr; |
||||
if (inet_pton(AF_INET, ip, &addr) != 1) return -1; |
||||
*network = ntohl(addr.s_addr); |
||||
*prefix_length = (uint8_t)prefix; |
||||
return 0; |
||||
} |
||||
|
||||
|
||||
|
||||
static int compare_routes(const void *a, const void *b) { |
||||
const struct route_entry *route_a = (const struct route_entry *)a; |
||||
const struct route_entry *route_b = (const struct route_entry *)b; |
||||
|
||||
// First compare by prefix length (longest prefix first)
|
||||
if (route_b->prefix_length != route_a->prefix_length) { |
||||
return route_b->prefix_length - route_a->prefix_length; |
||||
} |
||||
|
||||
// Then by network
|
||||
if (route_a->network != route_b->network) { |
||||
return route_a->network < route_b->network ? -1 : 1; |
||||
} |
||||
|
||||
// Finally by metrics (best route first)
|
||||
if (route_a->metrics.bandwidth_kbps != route_b->metrics.bandwidth_kbps) { |
||||
return route_b->metrics.bandwidth_kbps - route_a->metrics.bandwidth_kbps; |
||||
} |
||||
|
||||
if (route_a->metrics.latency_ms != route_b->metrics.latency_ms) { |
||||
return route_a->metrics.latency_ms - route_b->metrics.latency_ms; |
||||
} |
||||
|
||||
if (route_a->metrics.packet_loss_rate != route_b->metrics.packet_loss_rate) { |
||||
return route_a->metrics.packet_loss_rate - route_b->metrics.packet_loss_rate; |
||||
} |
||||
|
||||
return route_a->metrics.hop_count - route_b->metrics.hop_count; |
||||
} |
||||
|
||||
struct routing_table *routing_table_create(void) { |
||||
struct routing_table *table = calloc(1, sizeof(struct routing_table)); |
||||
if (!table) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, "routing_table_create: failed to allocate memory for routing table"); |
||||
return NULL; |
||||
} |
||||
|
||||
table->capacity = INITIAL_ROUTE_CAPACITY; |
||||
table->entries = calloc(table->capacity, sizeof(struct route_entry)); |
||||
if (!table->entries) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, "routing_table_create: failed to allocate memory for %d route entries", table->capacity); |
||||
free(table); |
||||
return NULL; |
||||
} |
||||
|
||||
// Initialize subnet validation arrays
|
||||
table->dynamic_subnets = calloc(MAX_SUBNET_VALIDATION_RANGES, sizeof(uint32_t) * 2); |
||||
table->local_subnets = calloc(MAX_SUBNET_VALIDATION_RANGES, sizeof(uint32_t) * 2); |
||||
|
||||
if (!table->dynamic_subnets || !table->local_subnets) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, "routing_table_create: failed to allocate memory for subnet validation arrays"); |
||||
free(table->entries); |
||||
free(table->dynamic_subnets); |
||||
free(table->local_subnets); |
||||
free(table); |
||||
return NULL; |
||||
} |
||||
|
||||
return table; |
||||
} |
||||
|
||||
void routing_table_destroy(struct routing_table *table) { |
||||
if (!table) return; |
||||
|
||||
// Free route entries
|
||||
free(table->entries); |
||||
|
||||
// Free validation ranges
|
||||
free(table->dynamic_subnets); |
||||
free(table->local_subnets); |
||||
|
||||
free(table); |
||||
} |
||||
|
||||
bool routing_table_insert(struct routing_table *table, const struct route_entry *entry) { |
||||
if (!table || !entry) return false; |
||||
|
||||
// Validate the route
|
||||
if (!routing_validate_route(table, entry->network, entry->prefix_length, entry->type)) { |
||||
char ip_str[16]; |
||||
ip_to_string(entry->network, ip_str); |
||||
DEBUG_WARN(DEBUG_CATEGORY_ROUTING, "Ignoring invalid route: %s/%d (type: %s)",
|
||||
ip_str, entry->prefix_length, route_type_to_string(entry->type)); |
||||
|
||||
return false; |
||||
} |
||||
|
||||
|
||||
// Check if we need to expand the table
|
||||
if (table->count >= table->capacity) { |
||||
size_t new_capacity = table->capacity * ROUTE_EXPANSION_FACTOR; |
||||
struct route_entry *new_entries = realloc(table->entries, new_capacity * sizeof(struct route_entry)); |
||||
if (!new_entries) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, "routing_table_insert: failed to expand routing table to %zu entries", new_capacity); |
||||
return false; |
||||
} |
||||
|
||||
table->entries = new_entries; |
||||
table->capacity = new_capacity; |
||||
} |
||||
|
||||
// Set timestamps and ensure route is active
|
||||
struct route_entry new_entry = *entry; |
||||
new_entry.created_time = new_entry.last_update = (uint64_t)time(NULL) * 1000000; // Convert to microseconds
|
||||
new_entry.last_used = 0; |
||||
|
||||
// Ensure route is active by default
|
||||
if (!(new_entry.flags & ROUTE_FLAG_ACTIVE)) { |
||||
new_entry.flags |= ROUTE_FLAG_ACTIVE; |
||||
} |
||||
|
||||
|
||||
|
||||
// Insert and maintain sorted order
|
||||
table->stats.routes_added++; |
||||
size_t insert_pos = table->count; |
||||
for (size_t i = 0; i < table->count; i++) { |
||||
if (compare_routes(&new_entry, &table->entries[i]) < 0) { |
||||
insert_pos = i; |
||||
break; |
||||
} |
||||
} |
||||
|
||||
return false; |
||||
} |
||||
|
||||
bool routing_table_lookup(struct routing_table *table, uint32_t dest_ip, struct route_entry *best_route) { |
||||
if (!table || !best_route) { |
||||
DEBUG_WARN(DEBUG_CATEGORY_ROUTING, "routing_table_lookup: invalid parameters (table=%p, best_route=%p)", table, best_route); |
||||
return false; |
||||
} |
||||
|
||||
table->stats.lookup_count++; |
||||
|
||||
// Find longest prefix match
|
||||
for (size_t i = 0; i < table->count; i++) { |
||||
const struct route_entry *entry = &table->entries[i]; |
||||
|
||||
// Check if route is valid and matches destination
|
||||
if (entry->flags & ROUTE_FLAG_ACTIVE) { |
||||
uint32_t mask = (entry->prefix_length == 0) ? 0 : (0xFFFFFFFFU << (32 - entry->prefix_length)); |
||||
if ((dest_ip & mask) == (entry->network & mask)) { |
||||
*best_route = *entry; |
||||
best_route->last_update = (uint64_t)time(NULL) * 1000000; |
||||
|
||||
char ip_str[16]; |
||||
ip_to_string(dest_ip, ip_str); |
||||
DEBUG_INFO(DEBUG_CATEGORY_ROUTING, "Found route for %s: %s/%d -> %s", |
||||
ip_str, ip_to_string(entry->network, ip_str), entry->prefix_length, |
||||
ip_to_string(entry->next_hop_ip, ip_str)); |
||||
|
||||
table->stats.hit_count++; |
||||
table->stats.routes_lookup_hits++; |
||||
return true; |
||||
} |
||||
} |
||||
} |
||||
|
||||
// No route found
|
||||
char ip_str[16]; |
||||
ip_to_string(dest_ip, ip_str); |
||||
DEBUG_WARN(DEBUG_CATEGORY_ROUTING, "routing_table_lookup: no route found for destination IP %s", ip_str); |
||||
|
||||
return false; |
||||
} |
||||
|
||||
bool routing_validate_route(struct routing_table *table, uint32_t network, uint8_t prefix_length, route_type_t route_type) { |
||||
if (!table) return false; |
||||
|
||||
uint32_t *validation_ranges = NULL; |
||||
size_t range_count = 0; |
||||
|
||||
switch (route_type) { |
||||
case ROUTE_TYPE_STATIC: |
||||
case ROUTE_TYPE_LEARNED: |
||||
return true; // No validation for static or learned routes
|
||||
case ROUTE_TYPE_DYNAMIC: |
||||
validation_ranges = table->dynamic_subnets; |
||||
range_count = table->dynamic_subnet_count; |
||||
break; |
||||
case ROUTE_TYPE_LOCAL: |
||||
validation_ranges = table->local_subnets; |
||||
range_count = table->local_subnet_count; |
||||
break; |
||||
default: |
||||
table->stats.routes_lookup_misses++; |
||||
return false; |
||||
} |
||||
|
||||
if (range_count == 0) return true; // No validation ranges configured
|
||||
|
||||
// Check if network falls within any validation range
|
||||
for (size_t i = 0; i < range_count; i += 2) { |
||||
uint32_t range_network = validation_ranges[i]; |
||||
uint8_t range_prefix = (uint8_t)validation_ranges[i + 1]; |
||||
|
||||
uint32_t mask = (range_prefix == 0) ? 0 : (0xFFFFFFFFU << (32 - range_prefix)); |
||||
if ((network & mask) == (range_network & mask)) { |
||||
// Check if this route is more specific than or equal to the validation range
|
||||
if (prefix_length >= range_prefix) { |
||||
return true; |
||||
} |
||||
} |
||||
} |
||||
|
||||
return false; |
||||
} |
||||
|
||||
bool enhanced_routing_add_subnet_range(struct routing_table *table, uint32_t network, uint8_t prefix_length, uint32_t **ranges, size_t *count) { |
||||
if (!table || !ranges || !count) return false; |
||||
|
||||
if (*count >= MAX_SUBNET_VALIDATION_RANGES - 2) return false; |
||||
|
||||
// Expand array if needed
|
||||
if (*ranges == NULL) { |
||||
*ranges = calloc(MAX_SUBNET_VALIDATION_RANGES, sizeof(uint32_t)); |
||||
if (!*ranges) return false; |
||||
*count = 0; |
||||
} |
||||
|
||||
// Add network and prefix length as a pair
|
||||
(*ranges)[*count] = network; |
||||
(*ranges)[*count + 1] = prefix_length; |
||||
*count += 2; |
||||
|
||||
char ip_str[16]; |
||||
ip_to_string(network, ip_str); |
||||
DEBUG_INFO(DEBUG_CATEGORY_ROUTING, "Added subnet validation range: %s/%d", ip_str, prefix_length); |
||||
|
||||
return true; |
||||
} |
||||
|
||||
|
||||
|
||||
bool routing_add_dynamic_subnet(struct routing_table *table, uint32_t network, uint8_t prefix_length) { |
||||
return enhanced_routing_add_subnet_range(table, network, prefix_length, &table->dynamic_subnets, &table->dynamic_subnet_count); |
||||
} |
||||
|
||||
bool routing_add_local_subnet(struct routing_table *table, uint32_t network, uint8_t prefix_length) { |
||||
return enhanced_routing_add_subnet_range(table, network, prefix_length, &table->local_subnets, &table->local_subnet_count); |
||||
} |
||||
|
||||
bool routing_get_all_routes(const struct routing_table *table, uint32_t network, uint8_t prefix_length,
|
||||
struct route_entry **routes, size_t *count) { |
||||
if (!table || !routes || !count) return false; |
||||
|
||||
*routes = NULL; |
||||
*count = 0; |
||||
|
||||
// Count matching routes
|
||||
for (size_t i = 0; i < table->count; i++) { |
||||
if (table->entries[i].network == network && table->entries[i].prefix_length == prefix_length) { |
||||
(*count)++; |
||||
} |
||||
} |
||||
|
||||
if (*count == 0) return true; // No routes found, but not an error
|
||||
|
||||
// Allocate result array
|
||||
*routes = calloc(*count, sizeof(struct route_entry)); |
||||
if (!*routes) { |
||||
*count = 0; |
||||
return false; |
||||
} |
||||
|
||||
// Copy matching routes
|
||||
size_t result_index = 0; |
||||
for (size_t i = 0; i < table->count; i++) { |
||||
if (table->entries[i].network == network && table->entries[i].prefix_length == prefix_length) { |
||||
(*routes)[result_index] = table->entries[i]; |
||||
|
||||
result_index++; |
||||
} |
||||
} |
||||
|
||||
return true; |
||||
} |
||||
|
||||
void routing_table_print(const struct routing_table *table) { |
||||
if (!table) return; |
||||
|
||||
printf("\n=== Routing Table ===\n"); |
||||
printf("Total routes: %zu\n", table->count); |
||||
printf("Dynamic subnets: %zu, Local subnets: %zu\n", |
||||
table->dynamic_subnet_count / 2, table->local_subnet_count / 2); |
||||
|
||||
if (table->count > 0) { |
||||
printf("\nRoutes:\n"); |
||||
for (size_t i = 0; i < table->count; i++) { |
||||
const struct route_entry *entry = &table->entries[i]; |
||||
char network_str[16], next_hop_str[16]; |
||||
|
||||
ip_to_string(entry->network, network_str); |
||||
ip_to_string(entry->next_hop_ip, next_hop_str); |
||||
|
||||
printf(" %zu: %s/%d -> %s [%s]\n", |
||||
i + 1, network_str, entry->prefix_length, next_hop_str, |
||||
route_type_to_string(entry->type)); |
||||
|
||||
printf(" Bandwidth: %uK, Loss: %.2f%%, Latency: %ums, Hops: %d\n", |
||||
entry->metrics.bandwidth_kbps, |
||||
entry->metrics.packet_loss_rate / 100.0, |
||||
entry->metrics.latency_ms, |
||||
entry->metrics.hop_count); |
||||
|
||||
|
||||
|
||||
printf(" Flags: %s%s%s%s\n", |
||||
(entry->flags & ROUTE_FLAG_ACTIVE) ? "ACTIVE " : "", |
||||
(entry->flags & ROUTE_FLAG_VALIDATED) ? "VALIDATED " : "", |
||||
(entry->flags & ROUTE_FLAG_ADVERTISED) ? "ADVERTISED " : "", |
||||
(entry->flags & ROUTE_FLAG_LEARNED) ? "LEARNED" : ""); |
||||
printf("\n"); |
||||
} |
||||
} else { |
||||
printf("\nNo routes configured.\n"); |
||||
} |
||||
} |
||||
|
||||
const char *route_type_to_string(route_type_t type) { |
||||
switch (type) { |
||||
case ROUTE_TYPE_STATIC: return "STATIC"; |
||||
case ROUTE_TYPE_DYNAMIC: return "DYNAMIC"; |
||||
case ROUTE_TYPE_LOCAL: return "LOCAL"; |
||||
case ROUTE_TYPE_LEARNED: return "LEARNED"; |
||||
default: return "UNKNOWN"; |
||||
} |
||||
} |
||||
|
||||
char *ip_to_string(uint32_t ip, char *buffer) { |
||||
if (!buffer) return NULL; |
||||
|
||||
struct in_addr addr; |
||||
addr.s_addr = htonl(ip); |
||||
strncpy(buffer, inet_ntoa(addr), 15); |
||||
buffer[15] = '\0'; |
||||
return buffer; |
||||
} |
||||
@ -0,0 +1,87 @@
|
||||
#ifndef ROUTING_H |
||||
#define ROUTING_H |
||||
#include <stdint.h> |
||||
#include <stddef.h> |
||||
#include <stdbool.h> |
||||
|
||||
// Forward declarations
|
||||
struct ETCP_CONNECTIONS; |
||||
|
||||
// Типы маршрутов
|
||||
typedef enum { |
||||
ROUTE_TYPE_STATIC = 0, |
||||
ROUTE_TYPE_DYNAMIC = 1, |
||||
ROUTE_TYPE_LOCAL = 2, |
||||
ROUTE_TYPE_LEARNED = 3 |
||||
} route_type_t; |
||||
|
||||
// Флаги маршрута
|
||||
typedef enum { |
||||
ROUTE_FLAG_ACTIVE = (1 << 0), |
||||
ROUTE_FLAG_VALIDATED = (1 << 1), |
||||
ROUTE_FLAG_ADVERTISED = (1 << 2), |
||||
ROUTE_FLAG_LEARNED = (1 << 3) |
||||
} route_flags_t; |
||||
|
||||
// Расширенные метрики маршрута
|
||||
struct route_metrics { |
||||
uint32_t bandwidth_kbps; |
||||
uint16_t packet_loss_rate; |
||||
uint16_t latency_ms; |
||||
uint8_t hop_count; |
||||
uint64_t last_updated; |
||||
}; |
||||
|
||||
// Расширенная запись маршрута
|
||||
struct route_entry { |
||||
uint32_t network; |
||||
uint8_t prefix_length; |
||||
uint32_t next_hop_ip; |
||||
struct ETCP_SOCKET* next_hop; |
||||
route_type_t type; |
||||
uint8_t flags; |
||||
struct route_metrics metrics; |
||||
uint64_t created_time; |
||||
uint64_t last_update; |
||||
uint64_t last_used; |
||||
}; |
||||
|
||||
// Таблица маршрутизации
|
||||
struct routing_table { |
||||
struct route_entry *entries; |
||||
size_t count; |
||||
size_t capacity; |
||||
uint32_t *dynamic_subnets; |
||||
size_t dynamic_subnet_count; |
||||
uint32_t *local_subnets; |
||||
size_t local_subnet_count; |
||||
struct { |
||||
uint64_t total_routes; |
||||
uint64_t static_routes; |
||||
uint64_t dynamic_routes; |
||||
uint64_t local_routes; |
||||
uint64_t learned_routes; |
||||
uint64_t routes_added; |
||||
uint64_t routes_deleted; |
||||
uint64_t lookup_count; |
||||
uint64_t hit_count; |
||||
uint64_t routes_lookup_hits; |
||||
uint64_t routes_lookup_misses; |
||||
uint64_t validation_failures; |
||||
} stats; |
||||
}; |
||||
|
||||
struct routing_table *routing_table_create(void); |
||||
void routing_table_destroy(struct routing_table *table); |
||||
bool routing_table_insert(struct routing_table *table, const struct route_entry *entry); |
||||
bool routing_table_delete(struct routing_table *table, uint32_t network, uint8_t prefix_length, uint32_t source_node_id); |
||||
bool routing_table_lookup(struct routing_table *table, uint32_t dest_ip, struct route_entry *best_route); |
||||
bool routing_validate_route(struct routing_table *table, uint32_t network, uint8_t prefix_length, route_type_t route_type); |
||||
bool routing_add_dynamic_subnet(struct routing_table *table, uint32_t network, uint8_t prefix_length); |
||||
bool routing_add_local_subnet(struct routing_table *table, uint32_t network, uint8_t prefix_length); |
||||
bool routing_get_all_routes(const struct routing_table *table, uint32_t network, uint8_t prefix_length, struct route_entry **routes, size_t *count); |
||||
void routing_table_print(const struct routing_table *table); |
||||
const char* route_type_to_string(route_type_t type); |
||||
char* ip_to_string(uint32_t ip, char *buffer); |
||||
|
||||
#endif // ROUTING_H
|
||||
@ -0,0 +1,7 @@
|
||||
internal_routing.c/h: |
||||
|
||||
это модуль роутинга пакетов. |
||||
пакеты приходят: |
||||
1. из очередей ETCP (struct ETCP_CONN output_queue). |
||||
при добавлении подключения (из конфига)) в вызовы etcp_connection_create/close надо добавить |
||||
2. из tun интерфейса |
||||
@ -0,0 +1,388 @@
|
||||
/* sc_lib.c - Secure Channel library implementation using TinyCrypt */ |
||||
|
||||
#include "secure_channel.h" |
||||
#include "../tinycrypt/lib/include/tinycrypt/ecc.h" |
||||
#include "../tinycrypt/lib/include/tinycrypt/ecc_dh.h" |
||||
#include "../tinycrypt/lib/include/tinycrypt/aes.h" |
||||
#include "../tinycrypt/lib/include/tinycrypt/ccm_mode.h" |
||||
#include "../tinycrypt/lib/include/tinycrypt/constants.h" |
||||
#include "../tinycrypt/lib/include/tinycrypt/ecc_platform_specific.h" |
||||
#include "../tinycrypt/lib/include/tinycrypt/sha256.h" |
||||
#include <string.h> |
||||
#include <stddef.h> |
||||
#include <sys/types.h> |
||||
#include <unistd.h> |
||||
#include <sys/time.h> |
||||
#include <stdio.h> |
||||
|
||||
// Simple debug macros
|
||||
#define DEBUG_CATEGORY_CRYPTO 1 |
||||
#define DEBUG_ERROR(category, fmt, ...) fprintf(stderr, "ERROR: " fmt "\n", ##__VA_ARGS__) |
||||
#define DEBUG_INFO(category, fmt, ...) fprintf(stdout, "INFO: " fmt "\n", ##__VA_ARGS__) |
||||
#include <stdio.h> |
||||
#include <fcntl.h> |
||||
#include "crc32.h" |
||||
|
||||
static const struct uECC_Curve_t *curve = NULL; |
||||
static uint8_t sc_urandom_seed[8] = {0}; |
||||
static int sc_urandom_initialized = 0; |
||||
|
||||
static void sc_init_random_seed(void) |
||||
{ |
||||
int fd = open("/dev/urandom", O_RDONLY); |
||||
if (fd >= 0) { |
||||
ssize_t ret = read(fd, sc_urandom_seed, 8); |
||||
close(fd); |
||||
if (ret == 8) { |
||||
sc_urandom_initialized = 1; |
||||
} |
||||
} |
||||
} |
||||
|
||||
|
||||
static int sc_rng(uint8_t *dest, unsigned size) |
||||
{ |
||||
int fd = open("/dev/urandom", O_RDONLY); |
||||
if (fd < 0) { |
||||
return 0; |
||||
} |
||||
|
||||
ssize_t ret = read(fd, dest, size); |
||||
close(fd); |
||||
if (ret != size) { |
||||
return 0; |
||||
} |
||||
|
||||
/* Mix in PID and microtime for additional entropy */ |
||||
pid_t pid = getpid(); |
||||
struct timeval tv; |
||||
gettimeofday(&tv, NULL); |
||||
|
||||
for (unsigned i = 0; i < size; i++) { |
||||
dest[i] ^= ((pid >> (i % (sizeof(pid) * 8))) & 0xFF); |
||||
dest[i] ^= ((tv.tv_sec >> (i % (sizeof(tv.tv_sec) * 8))) & 0xFF); |
||||
dest[i] ^= ((tv.tv_usec >> (i % (sizeof(tv.tv_usec) * 8))) & 0xFF); |
||||
} |
||||
|
||||
return 1; |
||||
} |
||||
|
||||
static int sc_validate_key(const uint8_t *public_key) |
||||
{ |
||||
if (!curve) { |
||||
curve = uECC_secp256r1(); |
||||
} |
||||
int result = uECC_valid_public_key(public_key, curve); |
||||
DEBUG_INFO(DEBUG_CATEGORY_CRYPTO, "sc_validate_key: uECC_valid_public_key returned %d", result); |
||||
return result; |
||||
} |
||||
|
||||
sc_status_t sc_generate_keypair(struct SC_MYKEYS *pk) |
||||
{ |
||||
if (!pk) { |
||||
return SC_ERR_INVALID_ARG; |
||||
} |
||||
|
||||
if (!curve) { |
||||
curve = uECC_secp256r1(); |
||||
} |
||||
|
||||
/* Set custom RNG function */ |
||||
uECC_set_rng(sc_rng); |
||||
|
||||
if (!uECC_make_key(pk->public_key, pk->private_key, curve)) { |
||||
return SC_ERR_CRYPTO; |
||||
} |
||||
return SC_OK; |
||||
} |
||||
|
||||
// Конвертация hex строки в бинарный формат
|
||||
static int hex_to_binary(const char *hex_str, uint8_t *binary, size_t binary_len) { |
||||
if (!hex_str || !binary || strlen(hex_str) != binary_len * 2) return -1; |
||||
|
||||
for (size_t i = 0; i < binary_len; i++) { |
||||
unsigned int byte; |
||||
if (sscanf(hex_str + i * 2, "%2x", &byte) != 1) return -1; |
||||
binary[i] = (uint8_t)byte; |
||||
} |
||||
return 0; |
||||
} |
||||
|
||||
sc_status_t sc_init_local_keys(struct SC_MYKEYS *mykeys, const char *public_key, const char *private_key) { |
||||
if (!mykeys || !public_key || !private_key) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_CRYPTO, "sc_init_local_keys: invalid arguments"); |
||||
return SC_ERR_INVALID_ARG; |
||||
} |
||||
|
||||
if (!curve) { |
||||
curve = uECC_secp256r1(); |
||||
} |
||||
|
||||
DEBUG_INFO(DEBUG_CATEGORY_CRYPTO, "sc_init_local_keys: public_key len=%zu, private_key len=%zu",
|
||||
strlen(public_key), strlen(private_key)); |
||||
|
||||
/* Convert hex to binary first */ |
||||
if (hex_to_binary(public_key, mykeys->public_key, SC_PUBKEY_SIZE)) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_CRYPTO, "sc_init_local_keys: failed to convert public key from hex"); |
||||
return SC_ERR_INVALID_ARG; |
||||
} |
||||
if (hex_to_binary(private_key, mykeys->private_key, SC_PRIVKEY_SIZE)) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_CRYPTO, "sc_init_local_keys: failed to convert private key from hex"); |
||||
return SC_ERR_INVALID_ARG; |
||||
} |
||||
|
||||
/* Validate the converted binary public key */ |
||||
if (sc_validate_key(mykeys->public_key) != 0) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_CRYPTO, "sc_init_local_keys: public key validation failed"); |
||||
return SC_ERR_INVALID_ARG; |
||||
} |
||||
|
||||
DEBUG_INFO(DEBUG_CATEGORY_CRYPTO, "sc_init_local_keys: keys initialized successfully"); |
||||
return SC_OK; |
||||
} |
||||
|
||||
sc_status_t sc_init_ctx(sc_context_t *ctx, struct SC_MYKEYS *mykeys) { |
||||
|
||||
ctx->pk=mykeys; |
||||
ctx->initialized = 1; |
||||
ctx->peer_key_set = 0; |
||||
ctx->session_ready = 0; |
||||
ctx->tx_counter = 0; |
||||
ctx->rx_counter = 0; |
||||
|
||||
return SC_OK; |
||||
} |
||||
|
||||
sc_status_t sc_set_peer_public_key(sc_context_t *ctx, const char *peer_public_key_h, int mode) { |
||||
uint8_t shared_secret[SC_SHARED_SECRET_SIZE]; |
||||
uint8_t peer_public_key[SC_PUBKEY_SIZE]; |
||||
|
||||
if (mode) { |
||||
if (hex_to_binary(peer_public_key_h, peer_public_key, SC_PUBKEY_SIZE)) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_CRYPTO, "sc_set_peer_public_key: invalid hex key format"); |
||||
return SC_ERR_INVALID_ARG; |
||||
} |
||||
} |
||||
else memcpy(peer_public_key, peer_public_key_h, SC_PUBKEY_SIZE); |
||||
|
||||
if (!ctx) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_CRYPTO, "sc_set_peer_public_key: invalid ctx"); |
||||
return SC_ERR_INVALID_ARG; |
||||
} |
||||
|
||||
if (!ctx->initialized) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_CRYPTO, "sc_set_peer_public_key: ctx not initialized"); |
||||
return SC_ERR_NOT_INITIALIZED; |
||||
} |
||||
|
||||
if (!curve) { |
||||
curve = uECC_secp256r1(); |
||||
} |
||||
|
||||
/* Validate peer public key */ |
||||
if (sc_validate_key(peer_public_key) != 0) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_CRYPTO, "sc_set_peer_public_key: invalid key"); |
||||
return SC_ERR_INVALID_ARG; |
||||
} |
||||
|
||||
/* Compute shared secret using ECDH */ |
||||
if (!ctx->pk) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_CRYPTO, "sc_set_peer_public_key: no private key"); |
||||
return SC_ERR_NOT_INITIALIZED; |
||||
} |
||||
if (!uECC_shared_secret(peer_public_key, ctx->pk->private_key, |
||||
shared_secret, curve)) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_CRYPTO, "sc_set_peer_public_key: shared secret error"); |
||||
return SC_ERR_CRYPTO; |
||||
} |
||||
|
||||
/* Derive session key from shared secret (simple copy for demo) */ |
||||
memcpy(ctx->session_key, shared_secret, SC_SESSION_KEY_SIZE); |
||||
|
||||
/* Store peer public key */ |
||||
memcpy(ctx->peer_public_key, peer_public_key, SC_PUBKEY_SIZE); |
||||
ctx->peer_key_set = 1; |
||||
|
||||
ctx->session_ready = 1; |
||||
|
||||
return SC_OK; |
||||
} |
||||
|
||||
static void sc_build_nonce(uint64_t counter, uint8_t *nonce_out) |
||||
{ |
||||
struct tc_sha256_state_struct sha_ctx; |
||||
uint8_t hash[32]; |
||||
struct timeval tv; |
||||
uint8_t data[8 + 8 + 4]; |
||||
|
||||
if (!sc_urandom_initialized) { |
||||
sc_init_random_seed(); |
||||
} |
||||
|
||||
gettimeofday(&tv, NULL); |
||||
|
||||
memcpy(data, sc_urandom_seed, 8); |
||||
data[8] = (counter >> 0) & 0xFF; |
||||
data[9] = (counter >> 8) & 0xFF; |
||||
data[10] = (counter >> 16) & 0xFF; |
||||
data[11] = (counter >> 24) & 0xFF; |
||||
data[12] = (counter >> 32) & 0xFF; |
||||
data[13] = (counter >> 40) & 0xFF; |
||||
data[14] = (counter >> 48) & 0xFF; |
||||
data[15] = (counter >> 56) & 0xFF; |
||||
data[16] = (tv.tv_sec >> 0) & 0xFF; |
||||
data[17] = (tv.tv_sec >> 8) & 0xFF; |
||||
data[18] = (tv.tv_sec >> 16) & 0xFF; |
||||
data[19] = (tv.tv_sec >> 24) & 0xFF; |
||||
|
||||
tc_sha256_init(&sha_ctx); |
||||
tc_sha256_update(&sha_ctx, data, 20); |
||||
tc_sha256_final(hash, &sha_ctx); |
||||
|
||||
memcpy(nonce_out, hash, SC_NONCE_SIZE); |
||||
} |
||||
|
||||
sc_status_t sc_encrypt(sc_context_t *ctx, |
||||
const uint8_t *plaintext, |
||||
size_t plaintext_len, |
||||
uint8_t *ciphertext, |
||||
size_t *ciphertext_len) |
||||
{ |
||||
uint8_t nonce[SC_NONCE_SIZE]; |
||||
struct tc_aes_key_sched_struct sched; |
||||
struct tc_ccm_mode_struct ccm_state; |
||||
size_t total_plaintext_len = plaintext_len + SC_CRC32_SIZE; |
||||
uint8_t plaintext_with_crc[total_plaintext_len]; |
||||
uint8_t combined_output[total_plaintext_len + SC_TAG_SIZE]; |
||||
|
||||
if (!ctx || !plaintext || !ciphertext || !ciphertext_len) { |
||||
return SC_ERR_INVALID_ARG; |
||||
} |
||||
|
||||
if (!ctx->session_ready) { |
||||
return SC_ERR_NOT_INITIALIZED; |
||||
} |
||||
|
||||
if (plaintext_len == 0) { |
||||
return SC_ERR_INVALID_ARG; |
||||
} |
||||
|
||||
/* Добавляем CRC32 к данным */ |
||||
memcpy(plaintext_with_crc, plaintext, plaintext_len); |
||||
uint32_t crc = crc32_calc(plaintext, plaintext_len); |
||||
plaintext_with_crc[plaintext_len] = (crc >> 0) & 0xFF; |
||||
plaintext_with_crc[plaintext_len + 1] = (crc >> 8) & 0xFF; |
||||
plaintext_with_crc[plaintext_len + 2] = (crc >> 16) & 0xFF; |
||||
plaintext_with_crc[plaintext_len + 3] = (crc >> 24) & 0xFF; |
||||
|
||||
/* Initialize AES key schedule */ |
||||
if (tc_aes128_set_encrypt_key(&sched, ctx->session_key) != TC_CRYPTO_SUCCESS) { |
||||
return SC_ERR_CRYPTO; |
||||
} |
||||
|
||||
/* Build nonce from counter */ |
||||
sc_build_nonce(ctx->tx_counter, nonce); |
||||
|
||||
/* Configure CCM mode */ |
||||
if (tc_ccm_config(&ccm_state, &sched, nonce, SC_NONCE_SIZE, SC_TAG_SIZE) != TC_CRYPTO_SUCCESS) { |
||||
return SC_ERR_CRYPTO; |
||||
} |
||||
|
||||
/* Encrypt and generate tag */ |
||||
if (tc_ccm_generation_encryption(combined_output, sizeof(combined_output), |
||||
NULL, 0, /* no associated data */ |
||||
plaintext_with_crc, total_plaintext_len, |
||||
&ccm_state) != TC_CRYPTO_SUCCESS) { |
||||
return SC_ERR_CRYPTO; |
||||
} |
||||
|
||||
/* Copy ciphertext + tag to output buffer */ |
||||
memcpy(ciphertext, combined_output, total_plaintext_len + SC_TAG_SIZE); |
||||
*ciphertext_len = total_plaintext_len + SC_TAG_SIZE; |
||||
|
||||
ctx->tx_counter++; |
||||
|
||||
return SC_OK; |
||||
} |
||||
|
||||
sc_status_t sc_decrypt(sc_context_t *ctx, |
||||
const uint8_t *ciphertext, |
||||
size_t ciphertext_len, |
||||
uint8_t *plaintext, |
||||
size_t *plaintext_len) |
||||
{ |
||||
uint8_t nonce[SC_NONCE_SIZE]; |
||||
struct tc_aes_key_sched_struct sched; |
||||
struct tc_ccm_mode_struct ccm_state; |
||||
TCCcmMode_t c = &ccm_state; |
||||
size_t total_plaintext_len = ciphertext_len - SC_TAG_SIZE; |
||||
uint8_t plaintext_with_crc[total_plaintext_len]; |
||||
|
||||
if (!ctx || !ciphertext || !plaintext || !plaintext_len) { |
||||
return SC_ERR_INVALID_ARG; |
||||
} |
||||
|
||||
if (!ctx->session_ready) { |
||||
return SC_ERR_NOT_INITIALIZED; |
||||
} |
||||
|
||||
if (ciphertext_len < SC_TAG_SIZE + SC_CRC32_SIZE) { |
||||
return SC_ERR_INVALID_ARG; |
||||
} |
||||
|
||||
/* Initialize AES key schedule */ |
||||
if (tc_aes128_set_encrypt_key(&sched, ctx->session_key) != TC_CRYPTO_SUCCESS) { |
||||
return SC_ERR_CRYPTO; |
||||
} |
||||
|
||||
/* Build nonce from counter */ |
||||
sc_build_nonce(ctx->rx_counter, nonce); |
||||
|
||||
/* Configure CCM mode */ |
||||
if (tc_ccm_config(c, &sched, nonce, SC_NONCE_SIZE, SC_TAG_SIZE) != TC_CRYPTO_SUCCESS) { |
||||
return SC_ERR_CRYPTO; |
||||
} |
||||
|
||||
/* Decrypt and verify tag */ |
||||
if (tc_ccm_decryption_verification(plaintext_with_crc, total_plaintext_len, |
||||
NULL, 0, /* no associated data */ |
||||
ciphertext, ciphertext_len, |
||||
c) != TC_CRYPTO_SUCCESS) { |
||||
return SC_ERR_AUTH_FAILED; |
||||
} |
||||
|
||||
/* Проверяем CRC32 */ |
||||
size_t data_len = total_plaintext_len - SC_CRC32_SIZE; |
||||
uint32_t expected_crc = crc32_calc(plaintext_with_crc, data_len); |
||||
uint32_t received_crc = (plaintext_with_crc[data_len] << 0) | |
||||
(plaintext_with_crc[data_len + 1] << 8) | |
||||
(plaintext_with_crc[data_len + 2] << 16) | |
||||
(plaintext_with_crc[data_len + 3] << 24); |
||||
|
||||
if (expected_crc != received_crc) { |
||||
return SC_ERR_CRC_FAILED; |
||||
} |
||||
|
||||
/* Копируем данные без CRC32 */ |
||||
memcpy(plaintext, plaintext_with_crc, data_len); |
||||
*plaintext_len = data_len; |
||||
|
||||
ctx->rx_counter++; |
||||
|
||||
return SC_OK; |
||||
} |
||||
|
||||
sc_status_t sc_compute_public_key_from_private(const uint8_t *private_key, uint8_t *public_key) { |
||||
if (!private_key || !public_key) { |
||||
return SC_ERR_INVALID_ARG; |
||||
} |
||||
|
||||
if (!curve) { |
||||
curve = uECC_secp256r1(); |
||||
} |
||||
|
||||
if (!uECC_compute_public_key(private_key, public_key, curve)) { |
||||
return SC_ERR_CRYPTO; |
||||
} |
||||
return SC_OK; |
||||
} |
||||
@ -0,0 +1,68 @@
|
||||
// secure_channel.h
|
||||
#ifndef SECURE_CHANNEL_H |
||||
#define SECURE_CHANNEL_H |
||||
|
||||
#include <stdint.h> |
||||
#include <stddef.h> |
||||
|
||||
// Размеры ключей
|
||||
#define SC_PRIVKEY_SIZE 32 |
||||
#define SC_PUBKEY_SIZE 64 |
||||
#define SC_HASH_SIZE 32 |
||||
#define SC_NONCE_SIZE 13 // CCM requires exactly 13 bytes
|
||||
#define SC_SHARED_SECRET_SIZE SC_HASH_SIZE |
||||
#define SC_SESSION_KEY_SIZE 16 |
||||
#define SC_TAG_SIZE 8 |
||||
#define SC_CRC32_SIZE 4 |
||||
|
||||
// Коды возврата
|
||||
#define SC_OK 0 |
||||
#define SC_ERR_INVALID_ARG -1 |
||||
#define SC_ERR_CRYPTO -2 |
||||
#define SC_ERR_NOT_INITIALIZED -3 |
||||
#define SC_ERR_AUTH_FAILED -4 |
||||
#define SC_ERR_CRC_FAILED -5 |
||||
|
||||
#define SC_PEER_PUBKEY_BIN 0 |
||||
#define SC_PEER_PUBKEY_HEX 1 |
||||
|
||||
// Типы
|
||||
typedef int sc_status_t; |
||||
typedef struct secure_channel sc_context_t; |
||||
|
||||
struct SC_MYKEYS { |
||||
/* Локальные ключи */ |
||||
uint8_t private_key[SC_PRIVKEY_SIZE]; |
||||
uint8_t public_key[SC_PUBKEY_SIZE]; |
||||
}; |
||||
|
||||
// Контекст защищенного канала
|
||||
struct secure_channel { |
||||
struct SC_MYKEYS* pk; |
||||
/* Ключи пира (после key exchange) */ |
||||
uint8_t peer_public_key[SC_PUBKEY_SIZE]; |
||||
uint8_t session_key[SC_SESSION_KEY_SIZE]; /* Derived session key */ |
||||
|
||||
/* Nonces для отправки и приема */ |
||||
uint8_t send_nonce[SC_NONCE_SIZE]; |
||||
uint8_t recv_nonce[SC_NONCE_SIZE]; |
||||
|
||||
uint8_t initialized; |
||||
uint8_t peer_key_set; |
||||
uint8_t session_ready; |
||||
uint64_t tx_counter; |
||||
uint64_t rx_counter; |
||||
}; |
||||
|
||||
// Функции инициализации
|
||||
sc_status_t sc_init_ctx(sc_context_t *ctx, struct SC_MYKEYS *mykeys); |
||||
sc_status_t sc_generate_keypair(struct SC_MYKEYS *keys); |
||||
sc_status_t sc_init_local_keys(struct SC_MYKEYS *mykeys, const char *public_key, const char *private_key); |
||||
sc_status_t sc_set_peer_public_key(sc_context_t *ctx, const char *peer_public_key, int mode);// mode: 0-bin 1-hex key format
|
||||
sc_status_t sc_compute_public_key_from_private(const uint8_t *private_key, uint8_t *public_key); |
||||
|
||||
// Криптографические операции
|
||||
sc_status_t sc_encrypt(sc_context_t *ctx, const uint8_t *plaintext, size_t plaintext_len, uint8_t *ciphertext, size_t *ciphertext_len); |
||||
sc_status_t sc_decrypt(sc_context_t *ctx, const uint8_t *ciphertext, size_t ciphertext_len, uint8_t *plaintext, size_t *plaintext_len); |
||||
|
||||
#endif // SECURE_CHANNEL_H
|
||||
@ -0,0 +1,342 @@
|
||||
#include "test_udp_socket.h" |
||||
#include "../lib/debug_config.h" |
||||
#include <stdlib.h> |
||||
#include <stdio.h> |
||||
#include <string.h> |
||||
#include <errno.h> |
||||
#include <fcntl.h> |
||||
#include <pthread.h> |
||||
|
||||
#ifndef DEBUG_CATEGORY_TEST |
||||
#define DEBUG_CATEGORY_TEST (1 << 30) |
||||
#endif |
||||
|
||||
// Global socket registry for fd-based operations
|
||||
static struct test_udp_socket* g_socket_registry[1024] = {NULL}; |
||||
pthread_mutex_t g_registry_mutex = PTHREAD_MUTEX_INITIALIZER; // Make it non-static for external access
|
||||
static int g_next_fd = 1000; // Start with high fd numbers to avoid conflicts
|
||||
|
||||
// Helper function to allocate packet
|
||||
static struct test_udp_packet* allocate_packet(const uint8_t* data, size_t len, |
||||
const struct sockaddr* addr, socklen_t addr_len) { |
||||
struct test_udp_packet* packet = calloc(1, sizeof(struct test_udp_packet)); |
||||
if (!packet) return NULL; |
||||
|
||||
packet->data = malloc(len); |
||||
if (!packet->data) { |
||||
free(packet); |
||||
return NULL; |
||||
} |
||||
|
||||
memcpy(packet->data, data, len); |
||||
packet->len = len; |
||||
|
||||
if (addr && addr_len > 0 && addr_len <= sizeof(struct sockaddr_storage)) { |
||||
memcpy(&packet->addr, addr, addr_len); |
||||
packet->addr_len = addr_len; |
||||
} else { |
||||
packet->addr_len = 0; |
||||
} |
||||
|
||||
return packet; |
||||
} |
||||
|
||||
// Helper function to free packet
|
||||
static void free_packet(struct test_udp_packet* packet) { |
||||
if (!packet) return; |
||||
if (packet->data) free(packet->data); |
||||
free(packet); |
||||
} |
||||
|
||||
// Create virtual UDP socket
|
||||
struct test_udp_socket* test_udp_socket_create(int family) { |
||||
if (family != AF_INET && family != AF_INET6) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_TEST, "Unsupported address family: %d", family); |
||||
return NULL; |
||||
} |
||||
|
||||
struct test_udp_socket* sock = calloc(1, sizeof(struct test_udp_socket)); |
||||
if (!sock) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_TEST, "Failed to allocate UDP socket"); |
||||
return NULL; |
||||
} |
||||
|
||||
// Assign virtual file descriptor
|
||||
pthread_mutex_lock(&g_registry_mutex); |
||||
sock->fd = g_next_fd++; |
||||
if (g_next_fd >= 2000) g_next_fd = 1000; // Wrap around
|
||||
pthread_mutex_unlock(&g_registry_mutex); |
||||
|
||||
sock->family = family; |
||||
sock->bound = false; |
||||
sock->nonblocking = false; |
||||
sock->recv_queue_head = NULL; |
||||
sock->recv_queue_tail = NULL; |
||||
sock->recv_queue_size = 0; |
||||
|
||||
// Register socket
|
||||
test_udp_socket_register(sock); |
||||
|
||||
DEBUG_INFO(DEBUG_CATEGORY_TEST, "Created virtual UDP socket: fd=%d, family=%d", sock->fd, family); |
||||
return sock; |
||||
} |
||||
|
||||
// Destroy virtual UDP socket
|
||||
void test_udp_socket_destroy(struct test_udp_socket* sock) { |
||||
if (!sock) return; |
||||
|
||||
DEBUG_INFO(DEBUG_CATEGORY_TEST, "Destroying virtual UDP socket: fd=%d (stats: sent=%zu/%zu, recv=%zu/%zu, errors=%zu/%zu)", |
||||
sock->fd, sock->stats.packets_sent, sock->stats.bytes_sent, |
||||
sock->stats.packets_received, sock->stats.bytes_received, |
||||
sock->stats.send_errors, sock->stats.recv_errors); |
||||
|
||||
// Unregister socket
|
||||
test_udp_socket_unregister(sock); |
||||
|
||||
// Free receive queue
|
||||
struct test_udp_packet* packet = sock->recv_queue_head; |
||||
while (packet) { |
||||
struct test_udp_packet* next = packet->next; |
||||
free_packet(packet); |
||||
packet = next; |
||||
} |
||||
|
||||
free(sock); |
||||
} |
||||
|
||||
// Bind virtual socket
|
||||
int test_udp_socket_bind(struct test_udp_socket* sock, const struct sockaddr* addr, socklen_t len) { |
||||
if (!sock || !addr || len == 0) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_TEST, "Invalid parameters for socket bind"); |
||||
return -1; |
||||
} |
||||
|
||||
if (sock->bound) { |
||||
DEBUG_WARN(DEBUG_CATEGORY_TEST, "Socket already bound"); |
||||
return 0; |
||||
} |
||||
|
||||
if (len > sizeof(struct sockaddr_storage)) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_TEST, "Address too large: %d", len); |
||||
return -1; |
||||
} |
||||
|
||||
memcpy(&sock->local_addr, addr, len); |
||||
sock->bound = true; |
||||
|
||||
DEBUG_INFO(DEBUG_CATEGORY_TEST, "Bound virtual UDP socket fd=%d", sock->fd); |
||||
return 0; |
||||
} |
||||
|
||||
// Set socket options (limited implementation for testing)
|
||||
int test_udp_socket_setsockopt(struct test_udp_socket* sock, int level, int optname,
|
||||
const void *optval, socklen_t optlen) { |
||||
if (!sock) return -1; |
||||
|
||||
// For testing purposes, just log the option setting
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_TEST, "setsockopt on fd=%d: level=%d, optname=%d, optlen=%d",
|
||||
sock->fd, level, optname, optlen); |
||||
|
||||
// Handle some common options
|
||||
if (level == SOL_SOCKET) { |
||||
switch (optname) { |
||||
case SO_REUSEADDR: |
||||
case SO_REUSEPORT: |
||||
// Always allow for testing
|
||||
return 0; |
||||
case SO_BROADCAST: |
||||
return 0; |
||||
default: |
||||
break; |
||||
} |
||||
} |
||||
|
||||
return 0; // Pretend success for most options
|
||||
} |
||||
|
||||
// Get socket options
|
||||
int test_udp_socket_getsockopt(struct test_udp_socket* sock, int level, int optname, |
||||
void *optval, socklen_t *optlen) { |
||||
if (!sock || !optval || !optlen) return -1; |
||||
|
||||
// For testing purposes, return reasonable defaults
|
||||
if (level == SOL_SOCKET) { |
||||
switch (optname) { |
||||
case SO_ERROR: |
||||
*(int*)optval = 0; |
||||
*optlen = sizeof(int); |
||||
return 0; |
||||
default: |
||||
break; |
||||
} |
||||
} |
||||
|
||||
return -1; // Not implemented
|
||||
} |
||||
|
||||
// Set non-blocking mode
|
||||
int test_udp_socket_set_nonblocking(struct test_udp_socket* sock, bool nonblocking) { |
||||
if (!sock) return -1; |
||||
|
||||
sock->nonblocking = nonblocking; |
||||
DEBUG_DEBUG(DEBUG_CATEGORY_TEST, "Set nonblocking on fd=%d: %s", sock->fd, nonblocking ? "true" : "false"); |
||||
return 0; |
||||
} |
||||
|
||||
// Send packet using virtual socket
|
||||
ssize_t test_udp_socket_sendto(struct test_udp_socket* sock, const void *buf, size_t len, int flags, |
||||
const struct sockaddr *dest_addr, socklen_t addr_len) { |
||||
if (!sock || !buf || len == 0) { |
||||
return -1; |
||||
} |
||||
|
||||
const uint8_t* data = (const uint8_t*)buf; |
||||
|
||||
// Call packet sent callback if available
|
||||
if (sock->packet_sent) { |
||||
sock->packet_sent(data, len, dest_addr, addr_len, sock->context); |
||||
} |
||||
|
||||
sock->stats.packets_sent++; |
||||
sock->stats.bytes_sent += len; |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_TEST, "Sent packet on fd=%d: %zu bytes to family=%d",
|
||||
sock->fd, len, dest_addr ? dest_addr->sa_family : -1); |
||||
return len; |
||||
} |
||||
|
||||
// Receive packet from virtual socket
|
||||
ssize_t test_udp_socket_recvfrom(struct test_udp_socket* sock, void *buf, size_t len, int flags, |
||||
struct sockaddr *src_addr, socklen_t *addr_len) { |
||||
if (!sock || !buf || len == 0) { |
||||
return -1; |
||||
} |
||||
|
||||
uint8_t* buffer = (uint8_t*)buf; |
||||
|
||||
// Check if we have packets in queue
|
||||
pthread_mutex_lock(&g_registry_mutex); |
||||
struct test_udp_packet* packet = sock->recv_queue_head; |
||||
if (!packet) { |
||||
pthread_mutex_unlock(&g_registry_mutex); |
||||
errno = EAGAIN; |
||||
return -1; |
||||
} |
||||
|
||||
// Remove packet from queue
|
||||
sock->recv_queue_head = packet->next; |
||||
if (!sock->recv_queue_head) { |
||||
sock->recv_queue_tail = NULL; |
||||
} |
||||
sock->recv_queue_size--; |
||||
pthread_mutex_unlock(&g_registry_mutex); |
||||
|
||||
// Copy packet data
|
||||
size_t copy_len = packet->len < len ? packet->len : len; |
||||
memcpy(buffer, packet->data, copy_len); |
||||
|
||||
// Copy source address if requested
|
||||
if (src_addr && addr_len && packet->addr_len > 0) { |
||||
socklen_t copy_addr_len = packet->addr_len < *addr_len ? packet->addr_len : *addr_len; |
||||
memcpy(src_addr, &packet->addr, copy_addr_len); |
||||
*addr_len = copy_addr_len; |
||||
} |
||||
|
||||
sock->stats.packets_received++; |
||||
sock->stats.bytes_received += copy_len; |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_TEST, "Received packet on fd=%d: %zu bytes", sock->fd, copy_len); |
||||
|
||||
free_packet(packet); |
||||
return copy_len; |
||||
} |
||||
|
||||
// Inject packet into receive queue (simulates incoming packet)
|
||||
int test_udp_socket_inject(struct test_udp_socket* sock,
|
||||
const uint8_t* data, size_t len, |
||||
const struct sockaddr* src_addr, socklen_t addr_len) { |
||||
if (!sock || !data || len == 0) { |
||||
return -1; |
||||
} |
||||
|
||||
if (sock->recv_queue_size >= TEST_UDP_MAX_QUEUE_SIZE) { |
||||
DEBUG_WARN(DEBUG_CATEGORY_TEST, "Receive queue full on fd=%d", sock->fd); |
||||
return -1; |
||||
} |
||||
|
||||
struct test_udp_packet* packet = allocate_packet(data, len, src_addr, addr_len); |
||||
if (!packet) { |
||||
return -1; |
||||
} |
||||
|
||||
pthread_mutex_lock(&g_registry_mutex); |
||||
if (sock->recv_queue_tail) { |
||||
sock->recv_queue_tail->next = packet; |
||||
} else { |
||||
sock->recv_queue_head = packet; |
||||
} |
||||
sock->recv_queue_tail = packet; |
||||
sock->recv_queue_size++; |
||||
pthread_mutex_unlock(&g_registry_mutex); |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_TEST, "Injected packet into fd=%d: %zu bytes", sock->fd, len); |
||||
return 0; |
||||
} |
||||
|
||||
// Get virtual socket file descriptor
|
||||
int test_udp_socket_get_fd(struct test_udp_socket* sock) { |
||||
return sock ? sock->fd : -1; |
||||
} |
||||
|
||||
// Get socket statistics
|
||||
void test_udp_socket_get_stats(struct test_udp_socket* sock, size_t* packets_sent,
|
||||
size_t* packets_received, size_t* bytes_sent,
|
||||
size_t* bytes_received, size_t* send_errors, size_t* recv_errors) { |
||||
if (!sock) return; |
||||
|
||||
if (packets_sent) *packets_sent = sock->stats.packets_sent; |
||||
if (packets_received) *packets_received = sock->stats.packets_received; |
||||
if (bytes_sent) *bytes_sent = sock->stats.bytes_sent; |
||||
if (bytes_received) *bytes_received = sock->stats.bytes_received; |
||||
if (send_errors) *send_errors = sock->stats.send_errors; |
||||
if (recv_errors) *recv_errors = sock->stats.recv_errors; |
||||
} |
||||
|
||||
// Reset socket statistics
|
||||
void test_udp_socket_reset_stats(struct test_udp_socket* sock) { |
||||
if (!sock) return; |
||||
|
||||
memset(&sock->stats, 0, sizeof(sock->stats)); |
||||
DEBUG_INFO(DEBUG_CATEGORY_TEST, "Reset statistics for virtual UDP socket fd=%d", sock->fd); |
||||
} |
||||
|
||||
// Global virtual socket registry for fd-based operations
|
||||
struct test_udp_socket* test_udp_socket_find_by_fd(int fd) { |
||||
pthread_mutex_lock(&g_registry_mutex); |
||||
struct test_udp_socket* sock = NULL; |
||||
if (fd >= 0 && fd < 1024) { |
||||
sock = g_socket_registry[fd]; |
||||
} |
||||
pthread_mutex_unlock(&g_registry_mutex); |
||||
return sock; |
||||
} |
||||
|
||||
void test_udp_socket_register(struct test_udp_socket* sock) { |
||||
if (!sock) return; |
||||
|
||||
pthread_mutex_lock(&g_registry_mutex); |
||||
if (sock->fd >= 0 && sock->fd < 1024) { |
||||
g_socket_registry[sock->fd] = sock; |
||||
} |
||||
pthread_mutex_unlock(&g_registry_mutex); |
||||
} |
||||
|
||||
void test_udp_socket_unregister(struct test_udp_socket* sock) { |
||||
if (!sock) return; |
||||
|
||||
pthread_mutex_lock(&g_registry_mutex); |
||||
if (sock->fd >= 0 && sock->fd < 1024) { |
||||
g_socket_registry[sock->fd] = NULL; |
||||
} |
||||
pthread_mutex_unlock(&g_registry_mutex); |
||||
} |
||||
@ -0,0 +1,101 @@
|
||||
#ifndef TEST_UDP_SOCKET_H |
||||
#define TEST_UDP_SOCKET_H |
||||
|
||||
#include <sys/socket.h> |
||||
#include <stdint.h> |
||||
#include <stddef.h> |
||||
#include <stdbool.h> |
||||
|
||||
#define TEST_UDP_MAX_PACKET_SIZE 65536 |
||||
#define TEST_UDP_MAX_QUEUE_SIZE 1000 |
||||
|
||||
// Test UDP packet structure
|
||||
struct test_udp_packet { |
||||
uint8_t* data; |
||||
size_t len; |
||||
struct sockaddr_storage addr; |
||||
socklen_t addr_len; |
||||
struct test_udp_packet* next; |
||||
}; |
||||
|
||||
// Virtual UDP socket for testing
|
||||
struct test_udp_socket { |
||||
int fd; // Virtual file descriptor
|
||||
int family; // Address family (AF_INET or AF_INET6)
|
||||
struct sockaddr_storage local_addr; |
||||
bool bound; |
||||
bool nonblocking; |
||||
|
||||
// Receive queue (packets waiting to be read)
|
||||
struct test_udp_packet* recv_queue_head; |
||||
struct test_udp_packet* recv_queue_tail; |
||||
size_t recv_queue_size; |
||||
|
||||
// Statistics
|
||||
struct { |
||||
size_t packets_sent; |
||||
size_t packets_received; |
||||
size_t bytes_sent; |
||||
size_t bytes_received; |
||||
size_t send_errors; |
||||
size_t recv_errors; |
||||
} stats; |
||||
|
||||
// Callback for outgoing packets
|
||||
void (*packet_sent)(const uint8_t* data, size_t len,
|
||||
const struct sockaddr* dest_addr, socklen_t addr_len, |
||||
void* context); |
||||
|
||||
void* context; |
||||
}; |
||||
|
||||
// Create virtual UDP socket
|
||||
struct test_udp_socket* test_udp_socket_create(int family); |
||||
|
||||
// Destroy virtual UDP socket
|
||||
void test_udp_socket_destroy(struct test_udp_socket* sock); |
||||
|
||||
// Bind virtual socket
|
||||
int test_udp_socket_bind(struct test_udp_socket* sock, const struct sockaddr* addr, socklen_t len); |
||||
|
||||
// Set socket options (limited implementation for testing)
|
||||
int test_udp_socket_setsockopt(struct test_udp_socket* sock, int level, int optname,
|
||||
const void *optval, socklen_t optlen); |
||||
|
||||
// Get socket options
|
||||
int test_udp_socket_getsockopt(struct test_udp_socket* sock, int level, int optname, |
||||
void *optval, socklen_t *optlen); |
||||
|
||||
// Set non-blocking mode
|
||||
int test_udp_socket_set_nonblocking(struct test_udp_socket* sock, bool nonblocking); |
||||
|
||||
// Send packet using virtual socket
|
||||
ssize_t test_udp_socket_sendto(struct test_udp_socket* sock, const void *buf, size_t len, int flags, |
||||
const struct sockaddr *dest_addr, socklen_t addr_len); |
||||
|
||||
// Receive packet from virtual socket
|
||||
ssize_t test_udp_socket_recvfrom(struct test_udp_socket* sock, void *buf, size_t len, int flags, |
||||
struct sockaddr *src_addr, socklen_t *addr_len); |
||||
|
||||
// Inject packet into receive queue (simulates incoming packet)
|
||||
int test_udp_socket_inject(struct test_udp_socket* sock,
|
||||
const uint8_t* data, size_t len, |
||||
const struct sockaddr* src_addr, socklen_t addr_len); |
||||
|
||||
// Get virtual socket file descriptor
|
||||
int test_udp_socket_get_fd(struct test_udp_socket* sock); |
||||
|
||||
// Get socket statistics
|
||||
void test_udp_socket_get_stats(struct test_udp_socket* sock, size_t* packets_sent,
|
||||
size_t* packets_received, size_t* bytes_sent,
|
||||
size_t* bytes_received, size_t* send_errors, size_t* recv_errors); |
||||
|
||||
// Reset socket statistics
|
||||
void test_udp_socket_reset_stats(struct test_udp_socket* sock); |
||||
|
||||
// Global virtual socket registry for fd-based operations
|
||||
struct test_udp_socket* test_udp_socket_find_by_fd(int fd); |
||||
void test_udp_socket_register(struct test_udp_socket* sock); |
||||
void test_udp_socket_unregister(struct test_udp_socket* sock); |
||||
|
||||
#endif // TEST_UDP_SOCKET_H
|
||||
@ -0,0 +1,190 @@
|
||||
#include "test_virtual_tun.h" |
||||
#include "../lib/debug_config.h" |
||||
#include <stdlib.h> |
||||
#include <stdio.h> |
||||
#include <string.h> |
||||
#include <errno.h> |
||||
#include <fcntl.h> |
||||
#include <unistd.h> |
||||
|
||||
#ifndef DEBUG_CATEGORY_TEST |
||||
#define DEBUG_CATEGORY_TEST (1 << 30) |
||||
#endif |
||||
|
||||
// Create virtual TUN with bidirectional pipes
|
||||
struct virtual_tun* virtual_tun_create(const char* ifname) { |
||||
if (!ifname) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_TEST, "NULL interface name provided"); |
||||
return NULL; |
||||
} |
||||
|
||||
struct virtual_tun* vtun = calloc(1, sizeof(struct virtual_tun)); |
||||
if (!vtun) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_TEST, "Failed to allocate virtual TUN"); |
||||
return NULL; |
||||
} |
||||
|
||||
// Copy interface name
|
||||
strncpy(vtun->ifname, ifname, sizeof(vtun->ifname) - 1); |
||||
vtun->ifname[sizeof(vtun->ifname) - 1] = '\0'; |
||||
vtun->enabled = true; |
||||
|
||||
// Create pipes for bidirectional communication
|
||||
// read_pipe: [0] = read end, [1] = write end (for packets from network)
|
||||
// write_pipe: [0] = read end, [1] = write end (for packets to network)
|
||||
if (pipe(vtun->read_pipe) < 0) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_TEST, "Failed to create read pipe: %s", strerror(errno)); |
||||
free(vtun); |
||||
return NULL; |
||||
} |
||||
|
||||
if (pipe(vtun->write_pipe) < 0) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_TEST, "Failed to create write pipe: %s", strerror(errno)); |
||||
close(vtun->read_pipe[0]); |
||||
close(vtun->read_pipe[1]); |
||||
free(vtun); |
||||
return NULL; |
||||
} |
||||
|
||||
// Set non-blocking mode on pipe ends
|
||||
for (int i = 0; i < 2; i++) { |
||||
int flags = fcntl(vtun->read_pipe[i], F_GETFL, 0); |
||||
if (flags == -1 || fcntl(vtun->read_pipe[i], F_SETFL, flags | O_NONBLOCK) < 0) { |
||||
DEBUG_WARN(DEBUG_CATEGORY_TEST, "Failed to set non-blocking on read pipe[%d]: %s", i, strerror(errno)); |
||||
} |
||||
|
||||
flags = fcntl(vtun->write_pipe[i], F_GETFL, 0); |
||||
if (flags == -1 || fcntl(vtun->write_pipe[i], F_SETFL, flags | O_NONBLOCK) < 0) { |
||||
DEBUG_WARN(DEBUG_CATEGORY_TEST, "Failed to set non-blocking on write pipe[%d]: %s", i, strerror(errno)); |
||||
} |
||||
} |
||||
|
||||
DEBUG_INFO(DEBUG_CATEGORY_TEST, "Virtual TUN created: %s (read_fd=%d, write_fd=%d)",
|
||||
vtun->ifname, vtun->read_pipe[0], vtun->write_pipe[1]); |
||||
|
||||
return vtun; |
||||
} |
||||
|
||||
// Cleanup virtual TUN
|
||||
void virtual_tun_destroy(struct virtual_tun* vtun) { |
||||
if (!vtun) return; |
||||
|
||||
DEBUG_INFO(DEBUG_CATEGORY_TEST, "Destroying virtual TUN: %s (stats: sent=%zu packets/%zu bytes, recv=%zu packets/%zu bytes)", |
||||
vtun->ifname, vtun->stats.packets_sent, vtun->stats.bytes_sent, |
||||
vtun->stats.packets_received, vtun->stats.bytes_received); |
||||
|
||||
// Close all pipe file descriptors
|
||||
if (vtun->read_pipe[0] >= 0) close(vtun->read_pipe[0]); |
||||
if (vtun->read_pipe[1] >= 0) close(vtun->read_pipe[1]); |
||||
if (vtun->write_pipe[0] >= 0) close(vtun->write_pipe[0]); |
||||
if (vtun->write_pipe[1] >= 0) close(vtun->write_pipe[1]); |
||||
|
||||
free(vtun); |
||||
} |
||||
|
||||
// Get file descriptor for reading (simulates TUN device read)
|
||||
int virtual_tun_get_read_fd(struct virtual_tun* vtun) { |
||||
if (!vtun || !vtun->enabled) return -1; |
||||
return vtun->read_pipe[0]; // Read end of read pipe
|
||||
} |
||||
|
||||
// Get file descriptor for writing (simulates TUN device write)
|
||||
int virtual_tun_get_write_fd(struct virtual_tun* vtun) { |
||||
if (!vtun || !vtun->enabled) return -1; |
||||
return vtun->write_pipe[1]; // Write end of write pipe
|
||||
} |
||||
|
||||
// Inject packet into virtual TUN (simulates packet from network)
|
||||
int virtual_tun_inject_packet(struct virtual_tun* vtun, const uint8_t* packet, size_t len) { |
||||
if (!vtun || !vtun->enabled || !packet || len == 0) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_TEST, "Invalid parameters for packet injection"); |
||||
return -1; |
||||
} |
||||
|
||||
if (len > VIRTUAL_TUN_MAX_PACKET_SIZE) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_TEST, "Packet too large: %zu > %d", len, VIRTUAL_TUN_MAX_PACKET_SIZE); |
||||
return -1; |
||||
} |
||||
|
||||
// Write packet to read pipe (simulates packet arriving from network)
|
||||
ssize_t written = write(vtun->read_pipe[1], packet, len); |
||||
if (written < 0) { |
||||
if (errno != EAGAIN && errno != EWOULDBLOCK) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_TEST, "Failed to inject packet: %s", strerror(errno)); |
||||
} |
||||
return -1; |
||||
} |
||||
|
||||
vtun->stats.packets_received++; |
||||
vtun->stats.bytes_received += written; |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_TEST, "Injected packet into %s: %zu bytes", vtun->ifname, written); |
||||
return 0; |
||||
} |
||||
|
||||
// Read packet from virtual TUN (captures packets going to network)
|
||||
ssize_t virtual_tun_read_packet(struct virtual_tun* vtun, uint8_t* buffer, size_t max_len) { |
||||
if (!vtun || !vtun->enabled || !buffer || max_len == 0) { |
||||
return -1; |
||||
} |
||||
|
||||
// Read from write pipe (captures packets going to network)
|
||||
ssize_t bytes_read = read(vtun->write_pipe[0], buffer, max_len); |
||||
if (bytes_read < 0) { |
||||
if (errno != EAGAIN && errno != EWOULDBLOCK) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_TEST, "Failed to read packet: %s", strerror(errno)); |
||||
} |
||||
return -1; |
||||
} |
||||
|
||||
if (bytes_read > 0) { |
||||
vtun->stats.packets_sent++; |
||||
vtun->stats.bytes_sent += bytes_read; |
||||
DEBUG_DEBUG(DEBUG_CATEGORY_TEST, "Read packet from %s: %zu bytes", vtun->ifname, bytes_read); |
||||
} |
||||
|
||||
return bytes_read; |
||||
} |
||||
|
||||
// Write packet to virtual TUN (sends packet to network)
|
||||
ssize_t virtual_tun_write_packet(struct virtual_tun* vtun, const uint8_t* packet, size_t len) { |
||||
if (!vtun || !vtun->enabled || !packet || len == 0) { |
||||
return -1; |
||||
} |
||||
|
||||
if (len > VIRTUAL_TUN_MAX_PACKET_SIZE) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_TEST, "Packet too large: %zu > %d", len, VIRTUAL_TUN_MAX_PACKET_SIZE); |
||||
return -1; |
||||
} |
||||
|
||||
// Write packet to write pipe (sends to network)
|
||||
ssize_t written = write(vtun->write_pipe[1], packet, len); |
||||
if (written < 0) { |
||||
if (errno != EAGAIN && errno != EWOULDBLOCK) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_TEST, "Failed to write packet: %s", strerror(errno)); |
||||
} |
||||
return -1; |
||||
} |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_TEST, "Wrote packet to %s: %zu bytes", vtun->ifname, written); |
||||
return written; |
||||
} |
||||
|
||||
// Get virtual TUN statistics
|
||||
void virtual_tun_get_stats(struct virtual_tun* vtun, size_t* packets_sent, size_t* packets_received, |
||||
size_t* bytes_sent, size_t* bytes_received) { |
||||
if (!vtun) return; |
||||
|
||||
if (packets_sent) *packets_sent = vtun->stats.packets_sent; |
||||
if (packets_received) *packets_received = vtun->stats.packets_received; |
||||
if (bytes_sent) *bytes_sent = vtun->stats.bytes_sent; |
||||
if (bytes_received) *bytes_received = vtun->stats.bytes_received; |
||||
} |
||||
|
||||
// Reset virtual TUN statistics
|
||||
void virtual_tun_reset_stats(struct virtual_tun* vtun) { |
||||
if (!vtun) return; |
||||
|
||||
memset(&vtun->stats, 0, sizeof(vtun->stats)); |
||||
DEBUG_INFO(DEBUG_CATEGORY_TEST, "Reset statistics for virtual TUN: %s", vtun->ifname); |
||||
} |
||||
@ -0,0 +1,55 @@
|
||||
#ifndef TEST_VIRTUAL_TUN_H |
||||
#define TEST_VIRTUAL_TUN_H |
||||
|
||||
#include <stdint.h> |
||||
#include <stdbool.h> |
||||
#include <stddef.h> |
||||
#include <sys/types.h> |
||||
|
||||
#define VIRTUAL_TUN_MAX_PACKET_SIZE 65536 |
||||
|
||||
struct virtual_tun { |
||||
int read_pipe[2]; // [0] = read end, [1] = write end
|
||||
int write_pipe[2]; // [0] = read end, [1] = write end
|
||||
char ifname[32]; // Virtual interface name
|
||||
bool enabled; |
||||
void* test_context; // For callbacks
|
||||
|
||||
// Statistics
|
||||
struct { |
||||
size_t packets_sent; |
||||
size_t packets_received; |
||||
size_t bytes_sent; |
||||
size_t bytes_received; |
||||
} stats; |
||||
}; |
||||
|
||||
// Create virtual TUN with bidirectional pipes
|
||||
struct virtual_tun* virtual_tun_create(const char* ifname); |
||||
|
||||
// Cleanup virtual TUN
|
||||
void virtual_tun_destroy(struct virtual_tun* vtun); |
||||
|
||||
// Get file descriptor for reading (simulates TUN device)
|
||||
int virtual_tun_get_read_fd(struct virtual_tun* vtun); |
||||
|
||||
// Get file descriptor for writing (simulates TUN device)
|
||||
int virtual_tun_get_write_fd(struct virtual_tun* vtun); |
||||
|
||||
// Inject packet into virtual TUN (simulates packet from network)
|
||||
int virtual_tun_inject_packet(struct virtual_tun* vtun, const uint8_t* packet, size_t len); |
||||
|
||||
// Read packet from virtual TUN (captures packets going to network)
|
||||
ssize_t virtual_tun_read_packet(struct virtual_tun* vtun, uint8_t* buffer, size_t max_len); |
||||
|
||||
// Write packet to virtual TUN (sends packet to network)
|
||||
ssize_t virtual_tun_write_packet(struct virtual_tun* vtun, const uint8_t* packet, size_t len); |
||||
|
||||
// Get virtual TUN statistics
|
||||
void virtual_tun_get_stats(struct virtual_tun* vtun, size_t* packets_sent, size_t* packets_received, |
||||
size_t* bytes_sent, size_t* bytes_received); |
||||
|
||||
// Reset virtual TUN statistics
|
||||
void virtual_tun_reset_stats(struct virtual_tun* vtun); |
||||
|
||||
#endif // TEST_VIRTUAL_TUN_H
|
||||
@ -0,0 +1,339 @@
|
||||
// tun_if.c - TUN interface management implementation
|
||||
#define _POSIX_C_SOURCE 200809L |
||||
#include "tun_if.h" |
||||
#include "../lib/debug_config.h" |
||||
#include "routing.h" |
||||
#include "../lib/u_async.h" |
||||
#include "etcp_connections.h" |
||||
|
||||
#include <stdio.h> |
||||
#include <stdlib.h> |
||||
#include <string.h> |
||||
#include <unistd.h> |
||||
#include <fcntl.h> |
||||
#include <sys/ioctl.h> |
||||
#include <sys/socket.h> |
||||
#include <sys/types.h> |
||||
#include <sys/stat.h> |
||||
#include <net/if.h> |
||||
#include <netinet/in.h> |
||||
#include <arpa/inet.h> |
||||
#include <linux/if.h> |
||||
#include <linux/if_tun.h> |
||||
#include <errno.h> |
||||
|
||||
// Create TUN device
|
||||
static int create_tun_device(char *ifname, size_t ifname_len) { |
||||
struct ifreq ifr; |
||||
int fd; |
||||
|
||||
fd = open("/dev/net/tun", O_RDWR); |
||||
if (fd < 0) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_TUN, "Failed to open /dev/net/tun: %s", strerror(errno)); |
||||
return -1; |
||||
} |
||||
|
||||
memset(&ifr, 0, sizeof(ifr)); |
||||
ifr.ifr_flags = IFF_TUN | IFF_NO_PI; |
||||
|
||||
if (ifname && ifname_len > 0 && ifname[0] != '\0') { |
||||
strncpy(ifr.ifr_name, ifname, IFNAMSIZ - 1); |
||||
} |
||||
|
||||
if (ioctl(fd, TUNSETIFF, &ifr) < 0) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_TUN, "Failed to configure TUN device: %s", strerror(errno)); |
||||
close(fd); |
||||
return -1; |
||||
} |
||||
|
||||
if (ifname && ifname_len > 0) { |
||||
strncpy(ifname, ifr.ifr_name, ifname_len - 1); |
||||
ifname[ifname_len - 1] = '\0'; |
||||
} |
||||
|
||||
return fd; |
||||
} |
||||
|
||||
// Run system command
|
||||
static int run_command(const char *cmd) { |
||||
int ret = system(cmd); |
||||
if (ret == -1) { |
||||
perror("system"); |
||||
return -1; |
||||
} |
||||
if (WIFEXITED(ret) && WEXITSTATUS(ret) != 0) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_TUN, "Command failed: %s", cmd); |
||||
return -1; |
||||
} |
||||
return 0; |
||||
} |
||||
|
||||
// Parse IP address and mask
|
||||
static int parse_ip_mask(const char *ip_addr, char *ip, size_t ip_len, int *mask) { |
||||
if (!ip_addr || !ip || !mask) return -1; |
||||
|
||||
char *slash = strchr(ip_addr, '/'); |
||||
if (!slash) { |
||||
// Default mask /32
|
||||
*mask = 32; |
||||
strncpy(ip, ip_addr, ip_len - 1); |
||||
ip[ip_len - 1] = '\0'; |
||||
} else { |
||||
size_t ip_size = slash - ip_addr; |
||||
if (ip_size >= ip_len) return -1; |
||||
strncpy(ip, ip_addr, ip_size); |
||||
ip[ip_size] = '\0'; |
||||
|
||||
char *endptr; |
||||
long mask_val = strtol(slash + 1, &endptr, 10); |
||||
if (*endptr != '\0' || mask_val < 0 || mask_val > 32) return -1; |
||||
*mask = (int)mask_val; |
||||
} |
||||
|
||||
return 0; |
||||
} |
||||
|
||||
int tun_create(struct tun_config *config) { |
||||
if (!config) { |
||||
errno = EINVAL; |
||||
return -1; |
||||
} |
||||
|
||||
// Create TUN device
|
||||
int fd = create_tun_device(config->ifname, sizeof(config->ifname)); |
||||
if (fd < 0) { |
||||
return -1; |
||||
} |
||||
|
||||
config->fd = fd; |
||||
|
||||
// Configure IP if specified
|
||||
if (config->ip_addr[0] != '\0') { |
||||
if (tun_set_ip(config->ifname, config->ip_addr) < 0) { |
||||
close(fd); |
||||
return -1; |
||||
} |
||||
} |
||||
|
||||
// Set MTU if specified
|
||||
if (config->mtu > 0) { |
||||
if (tun_set_mtu(config->ifname, config->mtu) < 0) { |
||||
DEBUG_WARN(DEBUG_CATEGORY_TUN, "Failed to set MTU %d on %s: %s", config->mtu, config->ifname, strerror(errno)); |
||||
} |
||||
} |
||||
|
||||
// Bring interface up
|
||||
if (tun_set_up(config->ifname) < 0) { |
||||
close(fd); |
||||
return -1; |
||||
} |
||||
|
||||
config->is_up = 1; |
||||
return 0; |
||||
} |
||||
|
||||
int tun_set_ip(const char *ifname, const char *ip_addr) { |
||||
if (!ifname || !ip_addr) { |
||||
errno = EINVAL; |
||||
return -1; |
||||
} |
||||
|
||||
char ip[64]; |
||||
int mask; |
||||
if (parse_ip_mask(ip_addr, ip, sizeof(ip), &mask) < 0) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "Invalid IP address format: %s", ip_addr); |
||||
errno = EINVAL; |
||||
return -1; |
||||
} |
||||
|
||||
char cmd[256]; |
||||
snprintf(cmd, sizeof(cmd), "ip addr add %s dev %s", ip_addr, ifname); |
||||
if (run_command(cmd) < 0) { |
||||
return -1; |
||||
} |
||||
|
||||
return 0; |
||||
} |
||||
|
||||
int tun_set_up(const char *ifname) { |
||||
if (!ifname) { |
||||
errno = EINVAL; |
||||
return -1; |
||||
} |
||||
|
||||
char cmd[256]; |
||||
snprintf(cmd, sizeof(cmd), "ip link set %s up", ifname); |
||||
if (run_command(cmd) < 0) { |
||||
return -1; |
||||
} |
||||
|
||||
return 0; |
||||
} |
||||
|
||||
int tun_set_mtu(const char *ifname, int mtu) { |
||||
if (!ifname || mtu <= 0) { |
||||
errno = EINVAL; |
||||
return -1; |
||||
} |
||||
|
||||
char cmd[256]; |
||||
snprintf(cmd, sizeof(cmd), "ip link set %s mtu %d", ifname, mtu); |
||||
if (run_command(cmd) < 0) { |
||||
return -1; |
||||
} |
||||
|
||||
return 0; |
||||
} |
||||
|
||||
ssize_t tun_read(int fd, uint8_t *buffer, size_t size) { |
||||
if (fd < 0 || !buffer || size == 0) { |
||||
errno = EINVAL; |
||||
return -1; |
||||
} |
||||
|
||||
ssize_t nread = read(fd, buffer, size); |
||||
if (nread < 0) { |
||||
perror("tun_read"); |
||||
} |
||||
|
||||
return nread; |
||||
} |
||||
|
||||
ssize_t tun_write(int fd, const uint8_t *buffer, size_t size) { |
||||
if (fd < 0 || !buffer || size == 0) { |
||||
errno = EINVAL; |
||||
return -1; |
||||
} |
||||
|
||||
ssize_t nwritten = write(fd, buffer, size); |
||||
if (nwritten < 0) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_TUN, "Failed to write to TUN device fd=%d: %s", fd, strerror(errno)); |
||||
} |
||||
|
||||
return nwritten; |
||||
} |
||||
|
||||
void tun_close(struct tun_config *config) { |
||||
if (!config) return; |
||||
|
||||
if (config->fd >= 0) { |
||||
close(config->fd); |
||||
config->fd = -1; |
||||
} |
||||
|
||||
config->is_up = 0; |
||||
} |
||||
|
||||
int tun_get_config(const char *ifname, struct tun_config *config) { |
||||
if (!ifname || !config) { |
||||
errno = EINVAL; |
||||
return -1; |
||||
} |
||||
|
||||
// TODO: Implement reading current interface configuration
|
||||
// This would require parsing ip addr show output
|
||||
|
||||
memset(config, 0, sizeof(*config)); |
||||
strncpy(config->ifname, ifname, sizeof(config->ifname) - 1); |
||||
config->fd = -1; |
||||
|
||||
return 0; |
||||
} |
||||
|
||||
// Extract destination IPv4 address from packet
|
||||
static uint32_t tun_get_dest_ip(const uint8_t *packet, size_t len) { |
||||
if (len < 20) return 0; // Minimum IPv4 header size
|
||||
// Check IP version (first nibble)
|
||||
uint8_t version = (packet[0] >> 4) & 0x0F; |
||||
if (version != 4) return 0; |
||||
// Destination IP is at offset 16
|
||||
uint32_t dest_ip; |
||||
memcpy(&dest_ip, packet + 16, 4); |
||||
return dest_ip; |
||||
} |
||||
|
||||
// Extract destination IPv4 address from packet
|
||||
static uint32_t get_dest_ip(const uint8_t *packet, size_t len) { |
||||
if (len < 20) return 0; // Minimum IPv4 header size
|
||||
// Check IP version (first nibble)
|
||||
uint8_t version = (packet[0] >> 4) & 0x0F; |
||||
if (version != 4) return 0; |
||||
// Destination IP is at offset 16
|
||||
uint32_t dest_ip; |
||||
memcpy(&dest_ip, packet + 16, 4); |
||||
return dest_ip; |
||||
} |
||||
|
||||
|
||||
// Callback for TUN device read events
|
||||
void tun_read_callback(int fd, void* user_arg) { |
||||
struct UTUN_INSTANCE *instance = (struct UTUN_INSTANCE*)user_arg; |
||||
uint8_t buffer[MAX_PACKET_SIZE]; |
||||
|
||||
// Read from TUN device
|
||||
ssize_t nread = tun_read(fd, buffer, sizeof(buffer)); |
||||
if (nread < 0) { |
||||
if (errno == EINTR) return; |
||||
DEBUG_ERROR(DEBUG_CATEGORY_TUN, "Failed to read from TUN device %s: %s", instance->tun.ifname, strerror(errno)); |
||||
return; |
||||
} |
||||
|
||||
if (nread > 0) { |
||||
// Route packet based on destination IP
|
||||
uint32_t dest_ip = get_dest_ip(buffer, nread); |
||||
struct route_entry route; |
||||
|
||||
if (routing_table_lookup(instance->routing_table, dest_ip, &route)) { |
||||
// Found route, send to next hop connection
|
||||
if (route.next_hop) { |
||||
struct sockaddr_in dest_addr; |
||||
memset(&dest_addr, 0, sizeof(dest_addr)); |
||||
dest_addr.sin_family = AF_INET; |
||||
dest_addr.sin_addr.s_addr = route.next_hop_ip; |
||||
// todo: отправлять в модуль роутинга все входящие пакеты. когда этот модуль будет. модуль роутинга это не роутинг таблица (routing.h)
|
||||
// if (etcp_connections_send(route.next_hop, buffer, nread, (struct sockaddr*)&dest_addr, sizeof(dest_addr)) < 0) {
|
||||
// DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "Failed to send packet via route");
|
||||
// }
|
||||
} else { |
||||
// Local route - no forwarding needed
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_ROUTING, "Local packet, no forwarding"); |
||||
} |
||||
} else { |
||||
// No route found, drop packet
|
||||
char ip_str[16]; |
||||
ip_to_string(dest_ip, ip_str); |
||||
DEBUG_ERROR(DEBUG_CATEGORY_ROUTING, "No route for destination IP %s", ip_str); |
||||
} |
||||
} |
||||
} |
||||
|
||||
// Register sockets with uasync
|
||||
int utun_instance_register_sockets(struct UTUN_INSTANCE *instance) { |
||||
if (!instance || !instance->ua || instance->tun.fd < 0) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_TUN, "Invalid instance or TUN fd"); |
||||
return -1; |
||||
} |
||||
|
||||
// Register TUN file descriptor
|
||||
instance->tun_socket_id = uasync_add_socket(instance->ua, instance->tun.fd,
|
||||
tun_read_callback, NULL, NULL, instance); |
||||
if (!instance->tun_socket_id) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_TUN, "Failed to register TUN socket"); |
||||
return -1; |
||||
} |
||||
|
||||
DEBUG_INFO(DEBUG_CATEGORY_TUN, "Registered TUN socket (fd=%d)", instance->tun.fd); |
||||
return 0; |
||||
} |
||||
|
||||
// Unregister sockets
|
||||
void utun_instance_unregister_sockets(struct UTUN_INSTANCE *instance) { |
||||
if (!instance || !instance->ua) return; |
||||
|
||||
if (instance->tun_socket_id) { |
||||
uasync_remove_socket(instance->ua, instance->tun_socket_id); |
||||
instance->tun_socket_id = NULL; |
||||
DEBUG_INFO(DEBUG_CATEGORY_TUN, "Unregistered TUN socket: %s", instance->tun.ifname); |
||||
} |
||||
} |
||||
|
||||
@ -0,0 +1,107 @@
|
||||
// tun_if.h - TUN interface management for utun
|
||||
#ifndef TUN_IF_H |
||||
#define TUN_IF_H |
||||
|
||||
#include <stdint.h> |
||||
#include <stddef.h> |
||||
#include <sys/types.h> |
||||
|
||||
// Forward declarations
|
||||
struct UTUN_INSTANCE; |
||||
|
||||
#ifdef __cplusplus |
||||
extern "C" { |
||||
#endif |
||||
|
||||
#define MAX_PACKET_SIZE 1500 |
||||
|
||||
// TUN interface configuration
|
||||
struct tun_config { |
||||
char ifname[16]; // Interface name (e.g., "tun12")
|
||||
char ip_addr[64]; // IP address with mask (e.g., "10.0.0.1/24")
|
||||
int mtu; // MTU size
|
||||
int fd; // File descriptor
|
||||
uint8_t is_up; // 1 if interface is up
|
||||
// Statistics
|
||||
uint64_t bytes_read; // Bytes read from TUN
|
||||
uint64_t bytes_written; // Bytes written to TUN
|
||||
uint32_t packets_read; // Packets read from TUN
|
||||
uint32_t packets_written; // Packets written to TUN
|
||||
uint32_t read_errors; // Read errors
|
||||
uint32_t write_errors; // Write errors
|
||||
}; |
||||
|
||||
/**
|
||||
* @brief Create and configure TUN interface |
||||
* @param config TUN configuration (ifname can be empty for auto) |
||||
* @return 0 on success, -1 on error |
||||
*/ |
||||
int tun_create(struct tun_config *config); |
||||
|
||||
/**
|
||||
* @brief Configure IP address on TUN interface |
||||
* @param ifname Interface name |
||||
* @param ip_addr IP address with mask (e.g., "10.0.0.1/24") |
||||
* @return 0 on success, -1 on error |
||||
*/ |
||||
int tun_set_ip(const char *ifname, const char *ip_addr); |
||||
|
||||
/**
|
||||
* @brief Bring TUN interface up |
||||
* @param ifname Interface name |
||||
* @return 0 on success, -1 on error |
||||
*/ |
||||
int tun_set_up(const char *ifname); |
||||
|
||||
/**
|
||||
* @brief Set MTU on TUN interface |
||||
* @param ifname Interface name |
||||
* @param mtu MTU value |
||||
* @return 0 on success, -1 on error |
||||
*/ |
||||
int tun_set_mtu(const char *ifname, int mtu); |
||||
|
||||
/**
|
||||
* @brief Read packet from TUN interface |
||||
* @param fd TUN file descriptor |
||||
* @param buffer Buffer to store packet |
||||
* @param size Buffer size |
||||
* @return Number of bytes read, -1 on error |
||||
*/ |
||||
ssize_t tun_read(int fd, uint8_t *buffer, size_t size); |
||||
|
||||
/**
|
||||
* @brief Write packet to TUN interface |
||||
* @param fd TUN file descriptor |
||||
* @param buffer Packet data |
||||
* @param size Packet size |
||||
* @return Number of bytes written, -1 on error |
||||
*/ |
||||
ssize_t tun_write(int fd, const uint8_t *buffer, size_t size); |
||||
|
||||
/**
|
||||
* @brief Close TUN interface |
||||
* @param config TUN configuration |
||||
*/ |
||||
void tun_close(struct tun_config *config); |
||||
|
||||
/**
|
||||
* @brief Get current TUN configuration |
||||
* @param ifname Interface name |
||||
* @param config Output configuration |
||||
* @return 0 on success, -1 on error |
||||
*/ |
||||
int tun_get_config(const char *ifname, struct tun_config *config); |
||||
|
||||
int utun_instance_register_sockets(struct UTUN_INSTANCE *instance); |
||||
void utun_instance_unregister_sockets(struct UTUN_INSTANCE *instance); |
||||
|
||||
// TUN callback function
|
||||
void tun_read_callback(int fd, void* user_arg); |
||||
|
||||
|
||||
#ifdef __cplusplus |
||||
} |
||||
#endif |
||||
|
||||
#endif /* TUN_IF_H */ |
||||
@ -0,0 +1,321 @@
|
||||
// utun.c - Main application for utun VPN tunnel
|
||||
#define _DEFAULT_SOURCE |
||||
#define _POSIX_C_SOURCE 200809L |
||||
#include "config_parser.h" |
||||
#include "etcp_connections.h" |
||||
#include "tun_if.h" |
||||
#include "secure_channel.h" |
||||
#include "routing.h" |
||||
#include "utun_instance.h" |
||||
#include "u_async.h" |
||||
#include "debug_config.h" |
||||
#include <stdio.h> |
||||
#include <stdlib.h> |
||||
#include <string.h> |
||||
#include <unistd.h> |
||||
#include <fcntl.h> |
||||
#include <sys/stat.h> |
||||
#include <errno.h> |
||||
#include <signal.h> |
||||
|
||||
#include <getopt.h> |
||||
#include <arpa/inet.h> |
||||
/*
|
||||
|
||||
Архитектура: |
||||
main -> init utun instnaces -> mainloop() |
||||
|
||||
utun instances: можно stop() - он ывзывает закрытие всех etcp (etcp_close) которые вызовут закрытие подключений), вызывает закрытие всех etcp сокетов |
||||
|
||||
|
||||
|
||||
*/ |
||||
|
||||
|
||||
|
||||
// Global wakeup pipe write fd for signal handler
|
||||
static int g_wakeup_pipe_write_fd = -1; |
||||
|
||||
#define DEFAULT_CONFIG "utun.conf" |
||||
#define DEFAULT_PIDFILE "/var/run/utun.pid" |
||||
|
||||
// Command line arguments
|
||||
typedef struct { |
||||
char *config_file; |
||||
char *pid_file; |
||||
char *log_file; |
||||
char *debug_config; |
||||
int foreground; |
||||
int help; |
||||
} cmd_args_t; |
||||
|
||||
// Global state
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// Parse subnet string
|
||||
static int parse_subnet(const char *subnet_str, uint32_t *network, uint8_t *prefix_length) { |
||||
if (!subnet_str || !network || !prefix_length) return -1; |
||||
char ip[64]; |
||||
int prefix; |
||||
if (sscanf(subnet_str, "%[^/]/%d", ip, &prefix) != 2) return -1; |
||||
if (prefix < 0 || prefix > 32) return -1; |
||||
struct in_addr addr; |
||||
if (inet_pton(AF_INET, ip, &addr) != 1) return -1; |
||||
*network = addr.s_addr; |
||||
*prefix_length = (uint8_t)prefix; |
||||
return 0; |
||||
} |
||||
|
||||
// Extract destination IPv4 address from packet
|
||||
static uint32_t get_dest_ip(const uint8_t *packet, size_t len) { |
||||
if (len < 20) return 0; // Minimum IPv4 header size
|
||||
// Check IP version (first nibble)
|
||||
uint8_t version = (packet[0] >> 4) & 0x0F; |
||||
if (version != 4) return 0; |
||||
// Destination IP is at offset 16
|
||||
uint32_t dest_ip; |
||||
memcpy(&dest_ip, packet + 16, 4); |
||||
return dest_ip; |
||||
} |
||||
|
||||
|
||||
// Parse command line arguments
|
||||
static void parse_args(int argc, char *argv[], cmd_args_t *args) { |
||||
memset(args, 0, sizeof(*args)); |
||||
args->config_file = DEFAULT_CONFIG; |
||||
args->pid_file = DEFAULT_PIDFILE; |
||||
args->log_file = NULL; |
||||
args->debug_config = NULL; |
||||
args->foreground = 0; |
||||
args->help = 0; |
||||
|
||||
static struct option long_options[] = { |
||||
{"config", required_argument, 0, 'c'}, |
||||
{"pidfile", required_argument, 0, 'p'}, |
||||
{"log", required_argument, 0, 'l'}, |
||||
{"debug", required_argument, 0, 'd'}, |
||||
{"foreground", no_argument, 0, 'f'}, |
||||
{"help", no_argument, 0, 'h'}, |
||||
{0, 0, 0, 0} |
||||
}; |
||||
|
||||
int opt; |
||||
int option_index = 0; |
||||
|
||||
while ((opt = getopt_long(argc, argv, "c:p:l:d:fh",
|
||||
long_options, &option_index)) != -1) { |
||||
switch (opt) { |
||||
case 'c': |
||||
args->config_file = optarg; |
||||
break; |
||||
case 'p': |
||||
args->pid_file = optarg; |
||||
break; |
||||
case 'l': |
||||
args->log_file = optarg; |
||||
break; |
||||
case 'f': |
||||
args->foreground = 1; |
||||
break; |
||||
case 'd': |
||||
args->debug_config = optarg; |
||||
break; |
||||
case 'h': |
||||
args->help = 1; |
||||
break; |
||||
default: |
||||
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "Unknown option: %c", opt); |
||||
exit(1); |
||||
} |
||||
} |
||||
} |
||||
|
||||
// Print usage
|
||||
static void print_usage(const char *progname) { |
||||
printf("Usage: %s [OPTIONS]\n", progname); |
||||
printf("Secure VPN tunnel over UDP with TUN interface\n\n"); |
||||
printf("Options:\n"); |
||||
printf(" -c, --config FILE Configuration file (default: %s)\n", DEFAULT_CONFIG); |
||||
printf(" -p, --pidfile FILE PID file (default: %s)\n", DEFAULT_PIDFILE); |
||||
printf(" -l, --log FILE Log file (default: stderr)\n"); |
||||
printf(" -d, --debug CONFIG Debug configuration (e.g., \"etcp:debug,routing:info\")\n"); |
||||
printf(" -f, --foreground Run in foreground (don't daemonize)\n"); |
||||
printf(" -h, --help Show this help\n"); |
||||
printf("\nExamples:\n"); |
||||
printf(" %s -c myconfig.conf\n", progname); |
||||
printf(" %s --config server.conf --pidfile /var/run/utun.pid\n", progname); |
||||
} |
||||
|
||||
// Write PID file
|
||||
static int write_pidfile(const char *pidfile) { |
||||
if (!pidfile) return -1; |
||||
|
||||
FILE *fp = fopen(pidfile, "w"); |
||||
if (!fp) { |
||||
perror("fopen pidfile"); |
||||
return -1; |
||||
} |
||||
|
||||
fprintf(fp, "%d\n", getpid()); |
||||
fclose(fp); |
||||
return 0; |
||||
} |
||||
|
||||
// Remove PID file
|
||||
static void remove_pidfile(const char *pidfile) { |
||||
if (pidfile) { |
||||
unlink(pidfile); |
||||
} |
||||
} |
||||
|
||||
// Daemonize process
|
||||
static int daemonize(void) { |
||||
pid_t pid = fork(); |
||||
if (pid < 0) { |
||||
perror("fork"); |
||||
return -1; |
||||
} |
||||
|
||||
if (pid > 0) { |
||||
// Parent exits
|
||||
exit(0); |
||||
} |
||||
|
||||
// Child becomes session leader
|
||||
if (setsid() < 0) { |
||||
perror("setsid"); |
||||
return -1; |
||||
} |
||||
|
||||
// Close standard file descriptors
|
||||
close(STDIN_FILENO); |
||||
close(STDOUT_FILENO); |
||||
close(STDERR_FILENO); |
||||
|
||||
// Redirect to /dev/null
|
||||
int fd = open("/dev/null", O_RDWR); |
||||
if (fd >= 0) { |
||||
dup2(fd, STDIN_FILENO); |
||||
dup2(fd, STDOUT_FILENO); |
||||
dup2(fd, STDERR_FILENO); |
||||
if (fd > 2) close(fd); |
||||
} |
||||
|
||||
return 0; |
||||
} |
||||
|
||||
// Open log file
|
||||
static FILE* open_logfile(const char *logfile) { |
||||
if (!logfile) return stderr; |
||||
|
||||
FILE *fp = fopen(logfile, "a"); |
||||
if (!fp) { |
||||
perror("fopen logfile"); |
||||
return stderr; |
||||
} |
||||
|
||||
// Set line buffering
|
||||
setlinebuf(fp); |
||||
return fp; |
||||
} |
||||
|
||||
|
||||
// Main function
|
||||
|
||||
static volatile sig_atomic_t g_running = 1; |
||||
struct UASYNC* main_ua = NULL; |
||||
|
||||
static void signal_handler(int sig) { |
||||
(void)sig; |
||||
g_running = 0; |
||||
if (main_ua) { |
||||
uasync_wakeup(main_ua); |
||||
} |
||||
} |
||||
|
||||
int main(int argc, char *argv[]) { |
||||
cmd_args_t args; |
||||
parse_args(argc, argv, &args); |
||||
|
||||
if (args.help) { |
||||
print_usage(argv[0]); |
||||
return 0; |
||||
} |
||||
|
||||
// Initialize global debug system early if configured from command line
|
||||
if (args.debug_config) { |
||||
debug_config_init(); |
||||
if (debug_parse_config(args.debug_config) < 0) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "Invalid debug configuration: %s", args.debug_config); |
||||
return 1; |
||||
} |
||||
} |
||||
|
||||
// Create uasync instance
|
||||
struct UASYNC* ua = uasync_create(); |
||||
main_ua=ua; |
||||
if (!ua) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, "Failed to create uasync instance"); |
||||
return 1; |
||||
} |
||||
|
||||
// Create and initialize instance
|
||||
struct UTUN_INSTANCE *instance = utun_instance_create(ua, args.config_file, args.log_file); |
||||
if (!instance) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, "Failed to create UTUN instance"); |
||||
return 1; |
||||
} |
||||
|
||||
// Print config for debugging
|
||||
if (args.foreground) { |
||||
print_config(instance->config); |
||||
} |
||||
|
||||
// Initialize all components (TUN, routing, connections)
|
||||
if (utun_instance_init(instance) < 0) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, "Failed to initialize instance"); |
||||
utun_instance_destroy(instance); |
||||
return 1; |
||||
} |
||||
|
||||
// Register sockets with uasync
|
||||
if (utun_instance_register_sockets(instance) < 0) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, "Failed to register sockets"); |
||||
utun_instance_destroy(instance); |
||||
return 1; |
||||
} |
||||
|
||||
// Setup signal handlers
|
||||
signal(SIGINT, signal_handler); |
||||
signal(SIGTERM, signal_handler); |
||||
signal(SIGHUP, signal_handler); |
||||
|
||||
// Daemonize if not in foreground
|
||||
if (!args.foreground) { |
||||
if (daemonize() < 0) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "Failed to daemonize"); |
||||
utun_instance_destroy(instance); |
||||
return 1; |
||||
} |
||||
} |
||||
|
||||
// Write PID file
|
||||
if (write_pidfile(args.pid_file) < 0) { |
||||
utun_instance_destroy(instance); |
||||
return 1; |
||||
} |
||||
|
||||
while (instance->running) uasync_poll(ua, 100); |
||||
|
||||
// Cleanup
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "Shutdown"); |
||||
|
||||
utun_instance_unregister_sockets(instance); |
||||
utun_instance_destroy(instance); |
||||
remove_pidfile(args.pid_file); |
||||
|
||||
return 0; |
||||
} |
||||
@ -0,0 +1,42 @@
|
||||
[global] |
||||
tun_ip=10.0.0.1 |
||||
mtu=1500 # MTU for all connections (0 = use default 1500) |
||||
control_ip=127.0.0.1 |
||||
control_port=12345 |
||||
net_debug=0 |
||||
|
||||
my_node_id=61be9d4cd3c60c2d |
||||
my_private_key=1313912e5d34768983b0e06530a48c77816d228a5b5605e1ab3dc443d107a3dc |
||||
my_public_key= |
||||
|
||||
|
||||
[routing] |
||||
allowed_subnet=10.0.0.0/24 |
||||
allowed_subnet=10.22.0.0/16 |
||||
allowed_subnet=10.23.0.0/16 |
||||
my_subnet=10.23.5.0/24 |
||||
my_subnet=10.23.6.0/24 |
||||
|
||||
# мои адреса и каналы |
||||
[server: lo0_test] |
||||
addr=127.0.0.1:1330 |
||||
#so_mark=100 |
||||
#netif=eth0 |
||||
type=nat # public / nat / private |
||||
|
||||
[server: lan1] |
||||
addr=192.168.29.117:1333 |
||||
so_mark=100 |
||||
netif=eth0 |
||||
type=public # public / nat / private |
||||
|
||||
[client: client_test1] |
||||
|
||||
# линки |
||||
link=lo0_test:192.168.0.20:1234 |
||||
#link=wired1_fast:1.2.3.4:1234 |
||||
link=lan1:192.168.0.20:1234 |
||||
#link=wireless_bkp:1.2.3.4:1234 |
||||
|
||||
keepalive=1 |
||||
peer_public_key=deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbee |
||||
@ -0,0 +1,230 @@
|
||||
// utun_instance.c - Root instance implementation |
||||
#include "utun_instance.h" |
||||
#include "config_parser.h" |
||||
#include "config_updater.h" |
||||
#include "tun_if.h" |
||||
#include "routing.h" |
||||
#include "etcp_connections.h" |
||||
#include "etcp.h" |
||||
#include "../lib/u_async.h" |
||||
#include "../lib/debug_config.h" |
||||
#include <stdlib.h> |
||||
#include <stdio.h> |
||||
#include <string.h> |
||||
#include <errno.h> |
||||
#include <unistd.h> |
||||
#include <arpa/inet.h> |
||||
|
||||
// Forward declarations |
||||
static void tun_read_callback(int fd, void* user_arg); |
||||
static uint32_t get_dest_ip(const uint8_t *packet, size_t len); |
||||
|
||||
// Global instance for signal handlers |
||||
static struct UTUN_INSTANCE *g_instance = NULL; |
||||
|
||||
// Create and initialize root instance |
||||
struct UTUN_INSTANCE* utun_instance_create(struct UASYNC* ua, const char *config_file, const char *log_file) { |
||||
struct UTUN_INSTANCE *instance = calloc(1, sizeof(struct UTUN_INSTANCE)); |
||||
if (!instance) return NULL; |
||||
|
||||
// Initialize basic fields |
||||
instance->running = 0; |
||||
instance->log_fp = NULL; |
||||
instance->ua = ua; |
||||
|
||||
// Ensure keys and node_id exist in config |
||||
if (config_ensure_keys_and_node_id(config_file) < 0) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "Failed to ensure keys and node_id in config: %s", config_file); |
||||
free(instance); |
||||
return NULL; |
||||
} |
||||
|
||||
// Load configuration |
||||
instance->config = parse_config(config_file); |
||||
if (!instance->config) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "Failed to load config from %s", config_file); |
||||
free(instance); |
||||
return NULL; |
||||
} |
||||
|
||||
// Open log file |
||||
if (log_file) { |
||||
instance->log_fp = fopen(log_file, "a"); |
||||
if (!instance->log_fp) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, "Failed to open log file %s: %s", log_file, strerror(errno)); |
||||
} |
||||
} |
||||
// Set node_id from config |
||||
instance->node_id = instance->config->global.my_node_id; |
||||
|
||||
// Set my keys |
||||
if (sc_init_local_keys(&instance->my_keys, instance->config->global.my_public_key_hex, instance->config->global.my_private_key_hex)) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, "Failed to initialize local keys"); |
||||
} |
||||
|
||||
|
||||
instance->pkt_pool=memory_pool_init(PACKET_DATA_SIZE+100); |
||||
/* |
||||
// Initialize TUN device |
||||
if (tun_create(&instance->tun) < 0) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_TUN, "Failed to create TUN device"); |
||||
return -1; |
||||
} |
||||
|
||||
// Configure TUN device |
||||
if (tun_set_ip(instance->tun.ifname, instance->config->global.tun_ip) < 0) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_TUN, "Failed to set TUN IP"); |
||||
tun_close(&instance->tun); |
||||
return -1; |
||||
} |
||||
|
||||
if (instance->config->global.mtu > 0) { |
||||
if (tun_set_mtu(instance->tun.ifname, instance->config->global.mtu) < 0) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_TUN, "Failed to set TUN MTU"); |
||||
tun_close(&instance->tun); |
||||
return -1; |
||||
} |
||||
} |
||||
|
||||
if (tun_set_up(instance->tun.ifname) < 0) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_TUN, "Failed to bring up TUN interface"); |
||||
tun_close(&instance->tun); |
||||
return -1; |
||||
} |
||||
|
||||
// Create routing table |
||||
instance->routing_table = routing_table_create(); |
||||
if (!instance->routing_table) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_ROUTING, "Failed to create routing table"); |
||||
return -1; |
||||
} |
||||
*/ |
||||
// Initialize connections from configuration - moved to utun_instance_init |
||||
// to avoid double initialization |
||||
/* |
||||
if (init_connections(instance) < 0) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "Failed to initialize connections"); |
||||
// Cleanup will be handled by utun_instance_destroy |
||||
return NULL; |
||||
} |
||||
*/ |
||||
|
||||
return instance; |
||||
} |
||||
|
||||
// Destroy instance and cleanup resources |
||||
void utun_instance_destroy(struct UTUN_INSTANCE *instance) { |
||||
if (!instance) return; |
||||
|
||||
printf("[INSTANCE_DESTROY] Starting cleanup for instance %p\n", instance); |
||||
|
||||
// Stop running |
||||
instance->running = 0; |
||||
|
||||
// Cleanup ETCP sockets and connections FIRST (before destroying uasync) |
||||
printf("[INSTANCE_DESTROY] Cleaning up ETCP sockets and connections\n"); |
||||
if (instance->etcp_sockets) { |
||||
printf("[INSTANCE_DESTROY] Found ETCP sockets to cleanup\n"); |
||||
struct ETCP_SOCKET* sock = instance->etcp_sockets; |
||||
while (sock) { |
||||
struct ETCP_SOCKET* next = sock->next; |
||||
printf("[INSTANCE_DESTROY] Removing socket %p, fd=%d\n", sock, sock->fd); |
||||
etcp_socket_remove(sock); // Полный cleanup сокета |
||||
sock = next; |
||||
} |
||||
instance->etcp_sockets = NULL; |
||||
printf("[INSTANCE_DESTROY] ETCP sockets cleanup complete\n"); |
||||
} |
||||
|
||||
if (instance->connections) { |
||||
printf("[INSTANCE_DESTROY] Found ETCP connections to cleanup\n"); |
||||
struct ETCP_CONN* conn = instance->connections; |
||||
while (conn) { |
||||
struct ETCP_CONN* next = conn->next; |
||||
printf("[INSTANCE_DESTROY] Closing connection %p\n", conn); |
||||
etcp_connection_close(conn); // Закрыть соединение |
||||
conn = next; |
||||
} |
||||
instance->connections = NULL; |
||||
printf("[INSTANCE_DESTROY] ETCP connections cleanup complete\n"); |
||||
} |
||||
|
||||
// Cleanup other components |
||||
if (instance->routing_table) { |
||||
routing_table_destroy(instance->routing_table); |
||||
} |
||||
|
||||
// Cleanup TUN |
||||
if (instance->tun.fd >= 0) { |
||||
tun_close(&instance->tun); |
||||
} |
||||
|
||||
// Cleanup config |
||||
if (instance->config) { |
||||
free_config(instance->config); |
||||
} |
||||
|
||||
// Close log file |
||||
if (instance->log_fp) { |
||||
fclose(instance->log_fp); |
||||
} |
||||
|
||||
// FINALLY destroy uasync (after all resources are cleaned up) |
||||
printf("[INSTANCE_DESTROY] Destroying uasync instance\n"); |
||||
if (instance->ua) { |
||||
uasync_destroy(instance->ua); |
||||
} |
||||
|
||||
// Free the instance memory |
||||
printf("[INSTANCE_DESTROY] Freeing instance memory\n"); |
||||
free(instance); |
||||
printf("[INSTANCE_DESTROY] Instance destroyed completely\n"); |
||||
|
||||
// Cleanup TUN |
||||
if (instance->tun.fd >= 0) { |
||||
tun_close(&instance->tun); |
||||
} |
||||
|
||||
// Cleanup config |
||||
if (instance->config) { |
||||
free_config(instance->config); |
||||
} |
||||
|
||||
// Close log file |
||||
if (instance->log_fp) { |
||||
fclose(instance->log_fp); |
||||
} |
||||
|
||||
// uasync cleanup will handle its built-in wakeup pipe |
||||
|
||||
// Clear global instance |
||||
if (g_instance == instance) { |
||||
g_instance = NULL; |
||||
} |
||||
|
||||
free(instance); |
||||
} |
||||
|
||||
// Stop instance |
||||
void utun_instance_stop(struct UTUN_INSTANCE *instance) { |
||||
if (!instance) return; |
||||
instance->running = 0; |
||||
// Wakeup main loop using built-in uasync wakeup |
||||
if (instance->ua) { |
||||
memory_pool_destroy(instance->pkt_pool); |
||||
uasync_wakeup(instance->ua); |
||||
} |
||||
|
||||
|
||||
} |
||||
|
||||
int utun_instance_init(struct UTUN_INSTANCE *instance) { |
||||
if (!instance) return -1; |
||||
|
||||
// Initialize connections |
||||
if (init_connections(instance) < 0) { |
||||
return -1; |
||||
} |
||||
|
||||
return 0; |
||||
} |
||||
@ -0,0 +1,72 @@
|
||||
#ifndef UTUN_INSTANCE_H |
||||
#define UTUN_INSTANCE_H |
||||
|
||||
#include <stdint.h> |
||||
#include <stdbool.h> |
||||
#include <stdio.h> |
||||
#include "../lib/memory_pool.h" |
||||
#include "secure_channel.h" |
||||
#include "tun_if.h" |
||||
|
||||
// Forward declarations
|
||||
struct utun_config; |
||||
struct uasync_s; |
||||
struct routing_table; |
||||
struct ETCP_CONN; |
||||
struct ETCP_SOCKET; |
||||
|
||||
// uTun instance configuration
|
||||
struct UTUN_INSTANCE { |
||||
// Configuration (moved from utun_state)
|
||||
struct utun_config *config; |
||||
|
||||
// TUN interface
|
||||
struct tun_config tun; |
||||
void *tun_socket_id; // Socket ID from uasync_add_socket
|
||||
|
||||
// Identification
|
||||
uint64_t node_id; |
||||
|
||||
struct SC_MYKEYS my_keys; |
||||
|
||||
// Main async context
|
||||
struct UASYNC* ua; |
||||
|
||||
// State
|
||||
int running; |
||||
FILE *log_fp; |
||||
|
||||
struct memory_pool* pkt_pool; |
||||
// Routing
|
||||
struct routing_table *routing_table; |
||||
|
||||
// Connections
|
||||
struct ETCP_CONN* connections;// linked-list
|
||||
int connections_count; // Number of connections
|
||||
|
||||
// Active sockets
|
||||
struct ETCP_SOCKET* etcp_sockets;// linked-list
|
||||
}; |
||||
|
||||
// Instance creation flags
|
||||
#define UTUN_CREATE_ALLOW_TUN_FAILURE 0x0001 // Continue if TUN creation fails
|
||||
#define UTUN_CREATE_NO_TUN 0x0002 // Skip TUN initialization entirely
|
||||
#define UTUN_CREATE_TEST_MODE 0x0004 // Enable test hooks and virtual interfaces
|
||||
#define UTUN_CREATE_NO_SOCKET_BIND 0x0008 // Skip socket binding (for test injection)
|
||||
|
||||
// Functions
|
||||
struct UTUN_INSTANCE* utun_instance_create_ex(struct UASYNC* ua, const char* config_file, const char* log_file, uint32_t flags); |
||||
|
||||
// Backward compatibility wrapper
|
||||
static inline struct UTUN_INSTANCE* utun_instance_create(struct UASYNC* ua, const char* config_file, const char* log_file) { |
||||
return utun_instance_create_ex(ua, config_file, log_file, 0); |
||||
} |
||||
void utun_instance_destroy(struct UTUN_INSTANCE* instance); |
||||
int utun_instance_init(struct UTUN_INSTANCE *instance); |
||||
void utun_instance_run(struct UTUN_INSTANCE *instance); |
||||
void utun_instance_stop(struct UTUN_INSTANCE *instance); |
||||
|
||||
// Diagnostic function for memory leak analysis
|
||||
void utun_instance_diagnose_leaks(struct UTUN_INSTANCE* instance, const char* phase); |
||||
|
||||
#endif // UTUN_INSTANCE_H
|
||||
@ -0,0 +1,586 @@
|
||||
#include "utun_test_framework.h" |
||||
#include "utun_instance.h" |
||||
#include "../lib/u_async.h" |
||||
#include "../lib/debug_config.h" |
||||
#include "../lib/ll_queue.h" |
||||
#include "test_udp_socket.h" |
||||
#include <stdlib.h> |
||||
#include <stdio.h> |
||||
#include <string.h> |
||||
#include <errno.h> |
||||
#include <unistd.h> |
||||
#include <pthread.h> |
||||
#include <sys/time.h> |
||||
|
||||
// External mutex from test_udp_socket.c
|
||||
extern pthread_mutex_t g_registry_mutex; |
||||
|
||||
#ifndef DEBUG_CATEGORY_TEST |
||||
#define DEBUG_CATEGORY_TEST (1 << 30) |
||||
#endif |
||||
|
||||
// Default test configurations
|
||||
const struct test_instance_config TEST_SERVER_CONFIG = { |
||||
.config_file = "test_server.conf", |
||||
.node_id = 0x1111222233334444, |
||||
.port_base = 40000, |
||||
.enable_tun = false, |
||||
.enable_socket_hooks = true, |
||||
.enable_packet_capture = true, |
||||
.log_file = NULL |
||||
}; |
||||
|
||||
const struct test_instance_config TEST_CLIENT_CONFIG = { |
||||
.config_file = "test_client.conf", |
||||
.node_id = 0x8888777766665555, |
||||
.port_base = 40100, |
||||
.enable_tun = false, |
||||
.enable_socket_hooks = true, |
||||
.enable_packet_capture = true, |
||||
.log_file = NULL |
||||
}; |
||||
|
||||
// Forward declarations for internal functions
|
||||
static int test_tun_create_hook(void* priv_data); |
||||
static ssize_t test_tun_read_hook(int fd, void *buf, size_t count, void* priv_data); |
||||
static ssize_t test_tun_write_hook(int fd, const void *buf, size_t count, void* priv_data); |
||||
static int test_socket_create_hook(int domain, int type, int protocol, void* context); |
||||
static void test_packet_capture_hook(struct UTUN_INSTANCE* instance, const uint8_t* packet, size_t len, |
||||
const char* direction, void* context); |
||||
static void test_cleanup_captured_packets(struct test_instance* test); |
||||
|
||||
// Create test instance with virtual components
|
||||
struct test_instance* test_create_instance(const struct test_instance_config* config) { |
||||
if (!config) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_TEST, "NULL configuration provided"); |
||||
return NULL; |
||||
} |
||||
|
||||
struct test_instance* test = calloc(1, sizeof(struct test_instance)); |
||||
if (!test) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_TEST, "Failed to allocate test instance"); |
||||
return NULL; |
||||
} |
||||
|
||||
// Copy configuration
|
||||
test->config = *config; |
||||
|
||||
// Create uasync instance
|
||||
test->ua = uasync_create(); |
||||
if (!test->ua) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_TEST, "Failed to create uasync instance"); |
||||
free(test); |
||||
return NULL; |
||||
} |
||||
|
||||
// Setup test hooks based on configuration
|
||||
if (config->enable_tun || config->enable_socket_hooks || config->enable_packet_capture) { |
||||
test->hooks.test_context = test; |
||||
|
||||
if (config->enable_tun) { |
||||
// Create virtual TUN
|
||||
char vtun_name[32]; |
||||
snprintf(vtun_name, sizeof(vtun_name), "vtun%ld", config->node_id & 0xFFFF); |
||||
test->vtun = virtual_tun_create(vtun_name); |
||||
if (!test->vtun) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_TEST, "Failed to create virtual TUN"); |
||||
uasync_destroy(test->ua); |
||||
free(test); |
||||
return NULL; |
||||
} |
||||
|
||||
test->hooks.tun_create_override = test_tun_create_hook; |
||||
test->hooks.tun_read_override = test_tun_read_hook; |
||||
test->hooks.tun_write_override = test_tun_write_hook; |
||||
} |
||||
|
||||
if (config->enable_socket_hooks) { |
||||
test->hooks.socket_create_override = test_socket_create_hook; |
||||
test->hooks.sendto_override = NULL; // Will be handled by virtual UDP sockets
|
||||
test->hooks.recvfrom_override = NULL; // Will be handled by virtual UDP sockets
|
||||
} |
||||
|
||||
if (config->enable_packet_capture) { |
||||
test->hooks.packet_captured = test_packet_capture_hook; |
||||
} |
||||
|
||||
// Install hooks
|
||||
#ifdef TEST_BUILD |
||||
utun_test_hooks_set(&test->hooks); |
||||
test->hooks_installed = true; |
||||
#endif |
||||
} |
||||
|
||||
// Allocate packet capture buffer
|
||||
if (config->enable_packet_capture) { |
||||
test->captured_packets.capacity = 1000; |
||||
test->captured_packets.packets = calloc(test->captured_packets.capacity, sizeof(uint8_t*)); |
||||
test->captured_packets.lengths = calloc(test->captured_packets.capacity, sizeof(size_t)); |
||||
test->captured_packets.directions = calloc(test->captured_packets.capacity, sizeof(char*)); |
||||
|
||||
if (!test->captured_packets.packets || !test->captured_packets.lengths || !test->captured_packets.directions) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_TEST, "Failed to allocate packet capture buffers"); |
||||
if (test->vtun) virtual_tun_destroy(test->vtun); |
||||
uasync_destroy(test->ua); |
||||
free(test); |
||||
return NULL; |
||||
} |
||||
} |
||||
|
||||
// Allocate UDP socket tracking
|
||||
test->udp_socket_capacity = 16; |
||||
test->udp_sockets = calloc(test->udp_socket_capacity, sizeof(struct test_udp_socket*)); |
||||
if (!test->udp_sockets) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_TEST, "Failed to allocate UDP socket tracking"); |
||||
test_cleanup_captured_packets(test); |
||||
if (test->vtun) virtual_tun_destroy(test->vtun); |
||||
uasync_destroy(test->ua); |
||||
free(test); |
||||
return NULL; |
||||
} |
||||
|
||||
DEBUG_INFO(DEBUG_CATEGORY_TEST, "Created test instance: node_id=0x%016lX, config=%s", |
||||
config->node_id, config->config_file); |
||||
return test; |
||||
} |
||||
|
||||
// Destroy test instance
|
||||
void test_destroy_instance(struct test_instance* test) { |
||||
if (!test) return; |
||||
|
||||
DEBUG_INFO(DEBUG_CATEGORY_TEST, "Destroying test instance: node_id=0x%016lX", |
||||
test->config.node_id); |
||||
|
||||
// Stop instance if running
|
||||
test_stop_instance(test); |
||||
|
||||
// Clear hooks if installed
|
||||
if (test->hooks_installed) { |
||||
#ifdef TEST_BUILD |
||||
utun_test_hooks_clear(); |
||||
#endif |
||||
test->hooks_installed = false; |
||||
} |
||||
|
||||
// Destroy virtual components
|
||||
if (test->vtun) { |
||||
virtual_tun_destroy(test->vtun); |
||||
} |
||||
|
||||
// Destroy UDP sockets
|
||||
for (int i = 0; i < test->udp_socket_count; i++) { |
||||
if (test->udp_sockets[i]) { |
||||
test_udp_socket_destroy(test->udp_sockets[i]); |
||||
} |
||||
} |
||||
free(test->udp_sockets); |
||||
|
||||
// Cleanup captured packets
|
||||
test_cleanup_captured_packets(test); |
||||
|
||||
// Destroy uasync
|
||||
if (test->ua) { |
||||
uasync_destroy(test->ua); |
||||
} |
||||
|
||||
// Destroy UTUN instance
|
||||
if (test->utun) { |
||||
utun_instance_destroy(test->utun); |
||||
} |
||||
|
||||
free(test); |
||||
} |
||||
|
||||
// Start test instance
|
||||
int test_start_instance(struct test_instance* test) { |
||||
if (!test) return -1; |
||||
if (test->utun) return 0; // Already started
|
||||
|
||||
DEBUG_INFO(DEBUG_CATEGORY_TEST, "Starting test instance: node_id=0x%016lX", test->config.node_id); |
||||
|
||||
// Determine flags for instance creation
|
||||
uint32_t flags = 0; |
||||
if (!test->config.enable_tun) { |
||||
flags |= UTUN_CREATE_NO_TUN; |
||||
} |
||||
if (test->config.enable_socket_hooks || test->config.enable_packet_capture) { |
||||
flags |= UTUN_CREATE_TEST_MODE; |
||||
} |
||||
if (test->config.enable_socket_hooks) { |
||||
flags |= UTUN_CREATE_NO_SOCKET_BIND; |
||||
} |
||||
if (test->config.enable_tun) { |
||||
flags |= UTUN_CREATE_ALLOW_TUN_FAILURE; // Allow virtual TUN to fail gracefully
|
||||
} |
||||
|
||||
// Create UTUN instance
|
||||
test->utun = utun_instance_create_ex(test->ua, test->config.config_file, test->config.log_file, flags); |
||||
if (!test->utun) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_TEST, "Failed to create UTUN instance"); |
||||
return -1; |
||||
} |
||||
|
||||
// Initialize instance
|
||||
if (utun_instance_init(test->utun) < 0) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_TEST, "Failed to initialize UTUN instance"); |
||||
utun_instance_destroy(test->utun); |
||||
test->utun = NULL; |
||||
return -1; |
||||
} |
||||
|
||||
DEBUG_INFO(DEBUG_CATEGORY_TEST, "Test instance started successfully"); |
||||
return 0; |
||||
} |
||||
|
||||
// Stop test instance
|
||||
void test_stop_instance(struct test_instance* test) { |
||||
if (!test || !test->utun) return; |
||||
|
||||
DEBUG_INFO(DEBUG_CATEGORY_TEST, "Stopping test instance: node_id=0x%016lX", test->config.node_id); |
||||
|
||||
utun_instance_stop(test->utun); |
||||
} |
||||
|
||||
// Inject packet into TUN interface
|
||||
int test_inject_tun_packet(struct test_instance* test, const uint8_t* packet, size_t len) { |
||||
if (!test || !test->vtun || !packet || len == 0) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_TEST, "Invalid parameters for TUN packet injection"); |
||||
return -1; |
||||
} |
||||
|
||||
return virtual_tun_inject_packet(test->vtun, packet, len); |
||||
} |
||||
|
||||
// Inject UDP packet
|
||||
int test_inject_udp_packet(struct test_instance* test, int socket_fd, |
||||
const uint8_t* packet, size_t len, |
||||
const struct sockaddr* src_addr, socklen_t addr_len) { |
||||
if (!test || !packet || len == 0) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_TEST, "Invalid parameters for UDP packet injection"); |
||||
return -1; |
||||
} |
||||
|
||||
struct test_udp_socket* sock = test_udp_socket_find_by_fd(socket_fd); |
||||
if (!sock) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_TEST, "Socket fd=%d not found", socket_fd); |
||||
return -1; |
||||
} |
||||
|
||||
return test_udp_socket_inject(sock, packet, len, src_addr, addr_len); |
||||
} |
||||
|
||||
// Get captured packets
|
||||
size_t test_get_captured_packets(struct test_instance* test,
|
||||
const char* direction, |
||||
uint8_t*** packets, size_t** lengths) { |
||||
if (!test || !direction || !packets || !lengths) return 0; |
||||
|
||||
// Count packets matching direction
|
||||
size_t count = 0; |
||||
for (size_t i = 0; i < test->captured_packets.count; i++) { |
||||
if (strcmp(test->captured_packets.directions[i], direction) == 0) { |
||||
count++; |
||||
} |
||||
} |
||||
|
||||
if (count == 0) return 0; |
||||
|
||||
// Allocate result arrays
|
||||
*packets = calloc(count, sizeof(uint8_t*)); |
||||
*lengths = calloc(count, sizeof(size_t)); |
||||
if (!*packets || !*lengths) { |
||||
free(*packets); |
||||
free(*lengths); |
||||
return 0; |
||||
} |
||||
|
||||
// Fill result arrays
|
||||
size_t result_idx = 0; |
||||
for (size_t i = 0; i < test->captured_packets.count; i++) { |
||||
if (strcmp(test->captured_packets.directions[i], direction) == 0) { |
||||
(*packets)[result_idx] = test->captured_packets.packets[i]; |
||||
(*lengths)[result_idx] = test->captured_packets.lengths[i]; |
||||
result_idx++; |
||||
} |
||||
} |
||||
|
||||
return count; |
||||
} |
||||
|
||||
// Get captured packet count
|
||||
size_t test_get_captured_packet_count(struct test_instance* test, const char* direction) { |
||||
if (!test || !direction) return 0; |
||||
|
||||
size_t count = 0; |
||||
for (size_t i = 0; i < test->captured_packets.count; i++) { |
||||
if (strcmp(test->captured_packets.directions[i], direction) == 0) { |
||||
count++; |
||||
} |
||||
} |
||||
return count; |
||||
} |
||||
|
||||
// Clear captured packets
|
||||
void test_clear_captured_packets(struct test_instance* test) { |
||||
if (!test) return; |
||||
|
||||
for (size_t i = 0; i < test->captured_packets.count; i++) { |
||||
if (test->captured_packets.packets[i]) { |
||||
free(test->captured_packets.packets[i]); |
||||
} |
||||
if (test->captured_packets.directions[i]) { |
||||
free(test->captured_packets.directions[i]); |
||||
} |
||||
} |
||||
test->captured_packets.count = 0; |
||||
} |
||||
|
||||
// Wait for packet with timeout
|
||||
int test_wait_for_packet(struct test_instance* test, const char* direction, int timeout_ms) { |
||||
if (!test || !direction || timeout_ms <= 0) return -1; |
||||
|
||||
size_t initial_count = test_get_captured_packet_count(test, direction); |
||||
size_t current_count = initial_count; |
||||
|
||||
struct timeval start, current; |
||||
gettimeofday(&start, NULL); |
||||
|
||||
while (current_count == initial_count) { |
||||
gettimeofday(¤t, NULL); |
||||
int elapsed_ms = (current.tv_sec - start.tv_sec) * 1000 + (current.tv_usec - start.tv_usec) / 1000; |
||||
|
||||
if (elapsed_ms >= timeout_ms) { |
||||
DEBUG_WARN(DEBUG_CATEGORY_TEST, "Timeout waiting for %s packet after %dms", direction, timeout_ms); |
||||
return -1; |
||||
} |
||||
|
||||
usleep(10000); // Sleep 10ms
|
||||
current_count = test_get_captured_packet_count(test, direction); |
||||
} |
||||
|
||||
DEBUG_INFO(DEBUG_CATEGORY_TEST, "Detected %s packet after %dms", direction,
|
||||
(current.tv_sec - start.tv_sec) * 1000 + (current.tv_usec - start.tv_usec) / 1000); |
||||
return 0; |
||||
} |
||||
|
||||
// Two-instance test runner
|
||||
int test_run_two_instances(const struct test_instance_config* server_config, |
||||
const struct test_instance_config* client_config, |
||||
void (*test_scenario)(struct test_instance* server,
|
||||
struct test_instance* client), |
||||
int timeout_ms) { |
||||
if (!server_config || !client_config || !test_scenario) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_TEST, "Invalid parameters for two-instance test"); |
||||
return -1; |
||||
} |
||||
|
||||
DEBUG_INFO(DEBUG_CATEGORY_TEST, "Starting two-instance test"); |
||||
|
||||
// Create instances
|
||||
struct test_instance* server = test_create_instance(server_config); |
||||
struct test_instance* client = test_create_instance(client_config); |
||||
|
||||
if (!server || !client) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_TEST, "Failed to create test instances"); |
||||
test_destroy_instance(server); |
||||
test_destroy_instance(client); |
||||
return -1; |
||||
} |
||||
|
||||
// Start instances
|
||||
if (test_start_instance(server) < 0 || test_start_instance(client) < 0) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_TEST, "Failed to start test instances"); |
||||
test_destroy_instance(server); |
||||
test_destroy_instance(client); |
||||
return -1; |
||||
} |
||||
|
||||
// Give instances time to initialize
|
||||
sleep(1); |
||||
|
||||
// Run test scenario
|
||||
int result = 0; |
||||
if (timeout_ms > 0) { |
||||
// TODO: Implement timeout mechanism for test scenario
|
||||
test_scenario(server, client); |
||||
} else { |
||||
test_scenario(server, client); |
||||
} |
||||
|
||||
// Cleanup
|
||||
test_destroy_instance(server); |
||||
test_destroy_instance(client); |
||||
|
||||
DEBUG_INFO(DEBUG_CATEGORY_TEST, "Two-instance test completed: %s", result == 0 ? "SUCCESS" : "FAILED"); |
||||
return result; |
||||
} |
||||
|
||||
// Get test instance statistics
|
||||
void test_get_instance_stats(struct test_instance* test, size_t* tun_packets_sent, |
||||
size_t* tun_packets_received, size_t* udp_packets_sent, |
||||
size_t* udp_packets_received) { |
||||
if (!test) return; |
||||
|
||||
if (test->vtun) { |
||||
virtual_tun_get_stats(test->vtun, tun_packets_sent, tun_packets_received, NULL, NULL); |
||||
} |
||||
|
||||
// Aggregate UDP socket statistics
|
||||
size_t udp_sent = 0, udp_received = 0; |
||||
for (int i = 0; i < test->udp_socket_count; i++) { |
||||
if (test->udp_sockets[i]) { |
||||
size_t sent, recv; |
||||
test_udp_socket_get_stats(test->udp_sockets[i], &sent, &recv, NULL, NULL, NULL, NULL); |
||||
udp_sent += sent; |
||||
udp_received += recv; |
||||
} |
||||
} |
||||
|
||||
if (udp_packets_sent) *udp_packets_sent = udp_sent; |
||||
if (udp_packets_received) *udp_packets_received = udp_received; |
||||
} |
||||
|
||||
// Utility functions
|
||||
const char* test_get_direction_name(const char* direction) { |
||||
if (!direction) return "unknown"; |
||||
|
||||
if (strcmp(direction, "tun_in") == 0) return "TUN incoming"; |
||||
if (strcmp(direction, "tun_out") == 0) return "TUN outgoing"; |
||||
if (strcmp(direction, "udp_in") == 0) return "UDP incoming"; |
||||
if (strcmp(direction, "udp_out") == 0) return "UDP outgoing"; |
||||
return direction; |
||||
} |
||||
|
||||
int test_compare_packets(const uint8_t* pkt1, size_t len1, const uint8_t* pkt2, size_t len2) { |
||||
if (!pkt1 || !pkt2) return -1; |
||||
if (len1 != len2) return -1; |
||||
return memcmp(pkt1, pkt2, len1); |
||||
} |
||||
|
||||
void test_dump_packet(const char* prefix, const uint8_t* packet, size_t len) { |
||||
if (!prefix || !packet || len == 0) return; |
||||
|
||||
printf("%s: %zu bytes\n", prefix, len); |
||||
for (size_t i = 0; i < len; i++) { |
||||
printf("%02X ", packet[i]); |
||||
if ((i + 1) % 16 == 0) printf("\n"); |
||||
} |
||||
if (len % 16 != 0) printf("\n"); |
||||
} |
||||
|
||||
// Internal hook implementations
|
||||
static int test_tun_create_hook(void* priv_data) { |
||||
struct test_instance* test = (struct test_instance*)priv_data; |
||||
if (!test || !test->vtun) return -1; |
||||
|
||||
// Return the read fd as the TUN device fd
|
||||
return virtual_tun_get_read_fd(test->vtun); |
||||
} |
||||
|
||||
static ssize_t test_tun_read_hook(int fd, void *buf, size_t count, void* priv_data) { |
||||
struct test_instance* test = (struct test_instance*)priv_data; |
||||
if (!test || !test->vtun) return -1; |
||||
|
||||
return virtual_tun_read_packet(test->vtun, buf, count); |
||||
} |
||||
|
||||
static ssize_t test_tun_write_hook(int fd, const void *buf, size_t count, void* priv_data) { |
||||
struct test_instance* test = (struct test_instance*)priv_data; |
||||
if (!test || !test->vtun) return -1; |
||||
|
||||
ssize_t result = virtual_tun_write_packet(test->vtun, buf, count); |
||||
|
||||
// Capture packet if enabled
|
||||
if (test->config.enable_packet_capture && result > 0) { |
||||
test_packet_capture_hook(test->utun, buf, result, "tun_out", test); |
||||
} |
||||
|
||||
return result; |
||||
} |
||||
|
||||
static int test_socket_create_hook(int domain, int type, int protocol, void* context) { |
||||
struct test_instance* test = (struct test_instance*)context; |
||||
if (!test || !test->config.enable_socket_hooks) return -1; |
||||
|
||||
// Create virtual UDP socket
|
||||
struct test_udp_socket* sock = test_udp_socket_create(domain); |
||||
if (!sock) return -1; |
||||
|
||||
// Add to tracking
|
||||
pthread_mutex_lock(&g_registry_mutex); |
||||
if (test->udp_socket_count >= test->udp_socket_capacity) { |
||||
// Expand array
|
||||
int new_capacity = test->udp_socket_capacity * 2; |
||||
struct test_udp_socket** new_sockets = realloc(test->udp_sockets,
|
||||
new_capacity * sizeof(struct test_udp_socket*)); |
||||
if (!new_sockets) { |
||||
pthread_mutex_unlock(&g_registry_mutex); |
||||
test_udp_socket_destroy(sock); |
||||
return -1; |
||||
} |
||||
test->udp_sockets = new_sockets; |
||||
test->udp_socket_capacity = new_capacity; |
||||
} |
||||
test->udp_sockets[test->udp_socket_count++] = sock; |
||||
pthread_mutex_unlock(&g_registry_mutex); |
||||
|
||||
return test_udp_socket_get_fd(sock); |
||||
} |
||||
|
||||
static void test_packet_capture_hook(struct UTUN_INSTANCE* instance, const uint8_t* packet, size_t len, |
||||
const char* direction, void* context) { |
||||
struct test_instance* test = (struct test_instance*)context; |
||||
if (!test || !test->config.enable_packet_capture || !packet || len == 0) return; |
||||
|
||||
// Expand capture buffer if needed
|
||||
if (test->captured_packets.count >= test->captured_packets.capacity) { |
||||
size_t new_capacity = test->captured_packets.capacity * 2; |
||||
uint8_t** new_packets = realloc(test->captured_packets.packets,
|
||||
new_capacity * sizeof(uint8_t*)); |
||||
size_t* new_lengths = realloc(test->captured_packets.lengths,
|
||||
new_capacity * sizeof(size_t)); |
||||
char** new_directions = realloc(test->captured_packets.directions,
|
||||
new_capacity * sizeof(char*)); |
||||
|
||||
if (!new_packets || !new_lengths || !new_directions) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_TEST, "Failed to expand packet capture buffer"); |
||||
return; |
||||
} |
||||
|
||||
test->captured_packets.packets = new_packets; |
||||
test->captured_packets.lengths = new_lengths; |
||||
test->captured_packets.directions = new_directions; |
||||
test->captured_packets.capacity = new_capacity; |
||||
} |
||||
|
||||
// Store packet
|
||||
uint8_t* packet_copy = malloc(len); |
||||
if (!packet_copy) return; |
||||
|
||||
memcpy(packet_copy, packet, len); |
||||
test->captured_packets.packets[test->captured_packets.count] = packet_copy; |
||||
test->captured_packets.lengths[test->captured_packets.count] = len; |
||||
test->captured_packets.directions[test->captured_packets.count] = strdup(direction); |
||||
test->captured_packets.count++; |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_TEST, "Captured packet: %s, %zu bytes", direction, len); |
||||
} |
||||
|
||||
static void test_cleanup_captured_packets(struct test_instance* test) { |
||||
if (!test) return; |
||||
|
||||
for (size_t i = 0; i < test->captured_packets.count; i++) { |
||||
if (test->captured_packets.packets[i]) { |
||||
free(test->captured_packets.packets[i]); |
||||
} |
||||
if (test->captured_packets.directions[i]) { |
||||
free(test->captured_packets.directions[i]); |
||||
} |
||||
} |
||||
|
||||
free(test->captured_packets.packets); |
||||
free(test->captured_packets.lengths); |
||||
free(test->captured_packets.directions); |
||||
|
||||
memset(&test->captured_packets, 0, sizeof(test->captured_packets)); |
||||
} |
||||
@ -0,0 +1,114 @@
|
||||
#ifndef UTUN_TEST_FRAMEWORK_H |
||||
#define UTUN_TEST_FRAMEWORK_H |
||||
|
||||
#include <stdint.h> |
||||
#include <stdbool.h> |
||||
#include <stddef.h> |
||||
#include "utun_test_hooks.h" |
||||
#include "test_virtual_tun.h" |
||||
#include "test_udp_socket.h" |
||||
|
||||
// Forward declarations
|
||||
struct UTUN_INSTANCE; |
||||
struct UASYNC; |
||||
|
||||
// Test instance configuration
|
||||
struct test_instance_config { |
||||
const char* config_file; |
||||
uint64_t node_id; |
||||
int port_base; |
||||
bool enable_tun; |
||||
bool enable_socket_hooks; |
||||
bool enable_packet_capture; |
||||
const char* log_file; |
||||
}; |
||||
|
||||
// Test instance with virtual components
|
||||
struct test_instance { |
||||
struct UTUN_INSTANCE* utun; |
||||
struct UASYNC* ua; |
||||
|
||||
// Virtual components
|
||||
struct virtual_tun* vtun; |
||||
struct test_udp_socket** udp_sockets; |
||||
int udp_socket_count; |
||||
int udp_socket_capacity; |
||||
|
||||
// Packet capture
|
||||
struct { |
||||
uint8_t** packets; |
||||
size_t* lengths; |
||||
char** directions; // "tun_in", "tun_out", "udp_in", "udp_out"
|
||||
size_t count; |
||||
size_t capacity; |
||||
} captured_packets; |
||||
|
||||
// Test configuration
|
||||
struct test_instance_config config; |
||||
|
||||
// Test context
|
||||
void* user_context; |
||||
|
||||
// Test hooks (for internal use)
|
||||
struct utun_test_hooks hooks; |
||||
bool hooks_installed; |
||||
}; |
||||
|
||||
// Create test instance with virtual components
|
||||
struct test_instance* test_create_instance(const struct test_instance_config* config); |
||||
|
||||
// Destroy test instance
|
||||
void test_destroy_instance(struct test_instance* test); |
||||
|
||||
// Start test instance (initialize and run)
|
||||
int test_start_instance(struct test_instance* test); |
||||
|
||||
// Stop test instance
|
||||
void test_stop_instance(struct test_instance* test); |
||||
|
||||
// Inject packet into TUN interface
|
||||
int test_inject_tun_packet(struct test_instance* test, const uint8_t* packet, size_t len); |
||||
|
||||
// Inject UDP packet
|
||||
int test_inject_udp_packet(struct test_instance* test, int socket_fd, |
||||
const uint8_t* packet, size_t len, |
||||
const struct sockaddr* src_addr, socklen_t addr_len); |
||||
|
||||
// Get captured packets
|
||||
size_t test_get_captured_packets(struct test_instance* test,
|
||||
const char* direction, // "tun_in", "tun_out", "udp_in", "udp_out"
|
||||
uint8_t*** packets, size_t** lengths); |
||||
|
||||
// Get captured packet count
|
||||
size_t test_get_captured_packet_count(struct test_instance* test, const char* direction); |
||||
|
||||
// Clear captured packets
|
||||
void test_clear_captured_packets(struct test_instance* test); |
||||
|
||||
// Wait for packet with timeout (milliseconds)
|
||||
int test_wait_for_packet(struct test_instance* test,
|
||||
const char* direction, |
||||
int timeout_ms); |
||||
|
||||
// Two-instance test runner
|
||||
int test_run_two_instances(const struct test_instance_config* server_config, |
||||
const struct test_instance_config* client_config, |
||||
void (*test_scenario)(struct test_instance* server,
|
||||
struct test_instance* client), |
||||
int timeout_ms); |
||||
|
||||
// Get test instance statistics
|
||||
void test_get_instance_stats(struct test_instance* test, size_t* tun_packets_sent, |
||||
size_t* tun_packets_received, size_t* udp_packets_sent, |
||||
size_t* udp_packets_received); |
||||
|
||||
// Utility functions
|
||||
const char* test_get_direction_name(const char* direction); |
||||
int test_compare_packets(const uint8_t* pkt1, size_t len1, const uint8_t* pkt2, size_t len2); |
||||
void test_dump_packet(const char* prefix, const uint8_t* packet, size_t len); |
||||
|
||||
// Default test configurations
|
||||
extern const struct test_instance_config TEST_SERVER_CONFIG; |
||||
extern const struct test_instance_config TEST_CLIENT_CONFIG; |
||||
|
||||
#endif // UTUN_TEST_FRAMEWORK_H
|
||||
@ -0,0 +1,30 @@
|
||||
#include "utun_test_hooks.h" |
||||
#include "utun_instance.h" |
||||
#include "../lib/debug_config.h" |
||||
|
||||
// Add test category if not already defined
|
||||
#ifndef DEBUG_CATEGORY_TEST |
||||
#define DEBUG_CATEGORY_TEST (1 << 30) // High bit for test category
|
||||
#endif |
||||
|
||||
// Global test hooks - NULL in production
|
||||
struct utun_test_hooks* g_utun_test_hooks = NULL; |
||||
|
||||
#ifdef TEST_BUILD |
||||
void utun_test_hooks_set(struct utun_test_hooks* hooks) { |
||||
if (!hooks) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_TEST, "Attempt to set NULL test hooks"); |
||||
return; |
||||
} |
||||
|
||||
DEBUG_INFO(DEBUG_CATEGORY_TEST, "Setting test hooks: sendto=%p, recvfrom=%p, tun_create=%p", |
||||
hooks->sendto_override, hooks->recvfrom_override, hooks->tun_create_override); |
||||
|
||||
g_utun_test_hooks = hooks; |
||||
} |
||||
|
||||
void utun_test_hooks_clear(void) { |
||||
DEBUG_INFO(DEBUG_CATEGORY_TEST, "Clearing test hooks (was: %p)", g_utun_test_hooks); |
||||
g_utun_test_hooks = NULL; |
||||
} |
||||
#endif |
||||
@ -0,0 +1,126 @@
|
||||
#ifndef UTUN_TEST_HOOKS_H |
||||
#define UTUN_TEST_HOOKS_H |
||||
|
||||
#include <sys/types.h> |
||||
#include <sys/socket.h> |
||||
#include <stdint.h> |
||||
#include <stddef.h> |
||||
#include <unistd.h> |
||||
|
||||
// Forward declarations
|
||||
struct UTUN_INSTANCE; |
||||
struct ETCP_SOCKET; |
||||
|
||||
// UDP packet interception callbacks
|
||||
typedef ssize_t (*utun_sendto_hook_t)(int sockfd, const void *buf, size_t len, int flags, |
||||
const struct sockaddr *dest_addr, socklen_t addrlen,
|
||||
void* context); |
||||
typedef ssize_t (*utun_recvfrom_hook_t)(int sockfd, void *buf, size_t len, int flags, |
||||
struct sockaddr *src_addr, socklen_t *addrlen,
|
||||
void* context); |
||||
|
||||
// TUN virtual interface callbacks
|
||||
typedef int (*tun_create_hook_t)(void* priv_data); |
||||
typedef ssize_t (*tun_read_hook_t)(int fd, void *buf, size_t count, void* priv_data); |
||||
typedef ssize_t (*tun_write_hook_t)(int fd, const void *buf, size_t count, void* priv_data); |
||||
|
||||
// Socket creation callback
|
||||
typedef int (*socket_create_hook_t)(int domain, int type, int protocol, void* context); |
||||
|
||||
// Test packet capture callback
|
||||
typedef void (*packet_capture_hook_t)(struct UTUN_INSTANCE* instance,
|
||||
const uint8_t* packet, size_t len, |
||||
const char* direction, // "tun_in", "tun_out", "udp_in", "udp_out"
|
||||
void* context); |
||||
|
||||
struct utun_test_hooks { |
||||
// UDP socket hooks
|
||||
utun_sendto_hook_t sendto_override; |
||||
utun_recvfrom_hook_t recvfrom_override; |
||||
|
||||
// TUN hooks
|
||||
tun_create_hook_t tun_create_override; |
||||
tun_read_hook_t tun_read_override; |
||||
tun_write_hook_t tun_write_override; |
||||
|
||||
// Socket creation hook
|
||||
socket_create_hook_t socket_create_override; |
||||
|
||||
// Packet capture
|
||||
packet_capture_hook_t packet_captured; |
||||
|
||||
// Context for callbacks
|
||||
void* test_context; |
||||
}; |
||||
|
||||
// Global test hooks - NULL in production, set only by test code
|
||||
extern struct utun_test_hooks* g_utun_test_hooks; |
||||
|
||||
// Test-aware wrapper functions - inline for zero overhead when NULL
|
||||
static inline ssize_t utun_sendto_hook(int sockfd, const void *buf, size_t len, int flags, |
||||
const struct sockaddr *dest_addr, socklen_t addrlen) { |
||||
if (g_utun_test_hooks && g_utun_test_hooks->sendto_override) { |
||||
return g_utun_test_hooks->sendto_override(sockfd, buf, len, flags, dest_addr, addrlen,
|
||||
g_utun_test_hooks->test_context); |
||||
} |
||||
return sendto(sockfd, buf, len, flags, dest_addr, addrlen); |
||||
} |
||||
|
||||
static inline ssize_t utun_recvfrom_hook(int sockfd, void *buf, size_t len, int flags, |
||||
struct sockaddr *src_addr, socklen_t *addrlen) { |
||||
if (g_utun_test_hooks && g_utun_test_hooks->recvfrom_override) { |
||||
return g_utun_test_hooks->recvfrom_override(sockfd, buf, len, flags, src_addr, addrlen, |
||||
g_utun_test_hooks->test_context); |
||||
} |
||||
return recvfrom(sockfd, buf, len, flags, src_addr, addrlen); |
||||
} |
||||
|
||||
static inline int tun_create_hook(void* priv_data) { |
||||
if (g_utun_test_hooks && g_utun_test_hooks->tun_create_override) { |
||||
return g_utun_test_hooks->tun_create_override(priv_data); |
||||
} |
||||
return -1; // Default: no TUN available in test mode
|
||||
} |
||||
|
||||
static inline ssize_t tun_read_hook(int fd, void *buf, size_t count, void* priv_data) { |
||||
if (g_utun_test_hooks && g_utun_test_hooks->tun_read_override) { |
||||
return g_utun_test_hooks->tun_read_override(fd, buf, count, priv_data); |
||||
} |
||||
return read(fd, buf, count); |
||||
} |
||||
|
||||
static inline ssize_t tun_write_hook(int fd, const void *buf, size_t count, void* priv_data) { |
||||
if (g_utun_test_hooks && g_utun_test_hooks->tun_write_override) { |
||||
return g_utun_test_hooks->tun_write_override(fd, buf, count, priv_data); |
||||
} |
||||
return write(fd, buf, count); |
||||
} |
||||
|
||||
static inline int socket_create_hook(int domain, int type, int protocol) { |
||||
if (g_utun_test_hooks && g_utun_test_hooks->socket_create_override) { |
||||
return g_utun_test_hooks->socket_create_override(domain, type, protocol,
|
||||
g_utun_test_hooks->test_context); |
||||
} |
||||
return socket(domain, type, protocol); |
||||
} |
||||
|
||||
static inline void packet_capture_hook(struct UTUN_INSTANCE* instance,
|
||||
const uint8_t* packet, size_t len, |
||||
const char* direction) { |
||||
if (g_utun_test_hooks && g_utun_test_hooks->packet_captured) { |
||||
g_utun_test_hooks->packet_captured(instance, packet, len, direction, |
||||
g_utun_test_hooks->test_context); |
||||
} |
||||
} |
||||
|
||||
// Functions to set/clear test hooks (only available in test builds)
|
||||
#ifdef TEST_BUILD |
||||
void utun_test_hooks_set(struct utun_test_hooks* hooks); |
||||
void utun_test_hooks_clear(void); |
||||
#else |
||||
// In production builds, these are no-ops for safety
|
||||
static inline void utun_test_hooks_set(struct utun_test_hooks* hooks) { (void)hooks; } |
||||
static inline void utun_test_hooks_clear(void) {} |
||||
#endif |
||||
|
||||
#endif // UTUN_TEST_HOOKS_H
|
||||
@ -0,0 +1,117 @@
|
||||
#include "utun_test_socket_api.h" |
||||
#include <stdlib.h> |
||||
#include <string.h> |
||||
#include <errno.h> |
||||
|
||||
void utun_test_packet_queue_init(struct utun_test_packet_queue* queue) { |
||||
if (!queue) return; |
||||
queue->head = NULL; |
||||
queue->tail = NULL; |
||||
queue->count = 0; |
||||
} |
||||
|
||||
void utun_test_packet_queue_cleanup(struct utun_test_packet_queue* queue) { |
||||
if (!queue) return; |
||||
|
||||
struct utun_test_packet* pkt = queue->head; |
||||
while (pkt) { |
||||
struct utun_test_packet* next = pkt->next; |
||||
free(pkt->data); |
||||
free(pkt); |
||||
pkt = next; |
||||
} |
||||
|
||||
queue->head = NULL; |
||||
queue->tail = NULL; |
||||
queue->count = 0; |
||||
} |
||||
|
||||
void utun_test_packet_queue_push(struct utun_test_packet_queue* queue, |
||||
const uint8_t* data, size_t len, |
||||
const struct sockaddr* addr, socklen_t addrlen) { |
||||
if (!queue || !data) return; |
||||
|
||||
struct utun_test_packet* pkt = calloc(1, sizeof(*pkt)); |
||||
if (!pkt) return; |
||||
|
||||
pkt->data = malloc(len); |
||||
if (!pkt->data) { |
||||
free(pkt); |
||||
return; |
||||
} |
||||
|
||||
memcpy(pkt->data, data, len); |
||||
pkt->len = len; |
||||
|
||||
if (addr && addrlen > 0 && addrlen <= sizeof(pkt->addr)) { |
||||
memcpy(&pkt->addr, addr, addrlen); |
||||
pkt->addrlen = addrlen; |
||||
} |
||||
|
||||
pkt->next = NULL; |
||||
|
||||
if (queue->tail) { |
||||
queue->tail->next = pkt; |
||||
} else { |
||||
queue->head = pkt; |
||||
} |
||||
queue->tail = pkt; |
||||
queue->count++; |
||||
} |
||||
|
||||
struct utun_test_packet* utun_test_packet_queue_pop(struct utun_test_packet_queue* queue) { |
||||
if (!queue || !queue->head) return NULL; |
||||
|
||||
struct utun_test_packet* pkt = queue->head; |
||||
queue->head = pkt->next; |
||||
|
||||
if (!queue->head) { |
||||
queue->tail = NULL; |
||||
} |
||||
|
||||
queue->count--; |
||||
pkt->next = NULL; |
||||
|
||||
return pkt; |
||||
} |
||||
|
||||
size_t utun_test_packet_queue_count(struct utun_test_packet_queue* queue) { |
||||
return queue ? queue->count : 0; |
||||
} |
||||
|
||||
ssize_t utun_test_default_on_send(const uint8_t* data, size_t len, |
||||
const struct sockaddr* dest_addr, socklen_t dest_addrlen, |
||||
void* user_data) { |
||||
struct utun_test_socket_api* api = (struct utun_test_socket_api*)user_data; |
||||
if (!api || !data || len == 0) return -1; |
||||
|
||||
utun_test_packet_queue_push(&api->send_queue, data, len, dest_addr, dest_addrlen); |
||||
return len; |
||||
} |
||||
|
||||
ssize_t utun_test_default_on_recv(uint8_t* buffer, size_t buffer_size, |
||||
struct sockaddr* src_addr, socklen_t* src_addrlen, |
||||
void* user_data) { |
||||
struct utun_test_socket_api* api = (struct utun_test_socket_api*)user_data; |
||||
if (!api || !buffer || buffer_size == 0) return -1; |
||||
|
||||
struct utun_test_packet* pkt = utun_test_packet_queue_pop(&api->recv_queue); |
||||
if (!pkt) { |
||||
errno = EAGAIN; |
||||
return -1; |
||||
} |
||||
|
||||
size_t to_copy = (pkt->len < buffer_size) ? pkt->len : buffer_size; |
||||
memcpy(buffer, pkt->data, to_copy); |
||||
|
||||
if (src_addr && src_addrlen && *src_addrlen > 0) { |
||||
socklen_t copy_len = (pkt->addrlen < *src_addrlen) ? pkt->addrlen : *src_addrlen; |
||||
memcpy(src_addr, &pkt->addr, copy_len); |
||||
*src_addrlen = copy_len; |
||||
} |
||||
|
||||
free(pkt->data); |
||||
free(pkt); |
||||
|
||||
return to_copy; |
||||
} |
||||
@ -0,0 +1,52 @@
|
||||
#ifndef UTUN_TEST_SOCKET_API_H |
||||
#define UTUN_TEST_SOCKET_API_H |
||||
|
||||
#include <sys/socket.h> |
||||
#include <stdint.h> |
||||
|
||||
struct utun_test_packet { |
||||
uint8_t* data; |
||||
size_t len; |
||||
struct sockaddr_storage addr; |
||||
socklen_t addrlen; |
||||
struct utun_test_packet* next; |
||||
}; |
||||
|
||||
struct utun_test_packet_queue { |
||||
struct utun_test_packet* head; |
||||
struct utun_test_packet* tail; |
||||
size_t count; |
||||
}; |
||||
|
||||
struct utun_test_socket_api { |
||||
struct utun_test_packet_queue send_queue; |
||||
struct utun_test_packet_queue recv_queue; |
||||
|
||||
ssize_t (*on_send)(const uint8_t* data, size_t len, |
||||
const struct sockaddr* dest_addr, socklen_t dest_addrlen, |
||||
void* user_data); |
||||
|
||||
ssize_t (*on_recv)(uint8_t* buffer, size_t buffer_size, |
||||
struct sockaddr* src_addr, socklen_t* src_addrlen, |
||||
void* user_data); |
||||
|
||||
void* user_data; |
||||
}; |
||||
|
||||
void utun_test_packet_queue_init(struct utun_test_packet_queue* queue); |
||||
void utun_test_packet_queue_cleanup(struct utun_test_packet_queue* queue); |
||||
void utun_test_packet_queue_push(struct utun_test_packet_queue* queue, |
||||
const uint8_t* data, size_t len, |
||||
const struct sockaddr* addr, socklen_t addrlen); |
||||
struct utun_test_packet* utun_test_packet_queue_pop(struct utun_test_packet_queue* queue); |
||||
size_t utun_test_packet_queue_count(struct utun_test_packet_queue* queue); |
||||
|
||||
ssize_t utun_test_default_on_send(const uint8_t* data, size_t len, |
||||
const struct sockaddr* dest_addr, socklen_t dest_addrlen, |
||||
void* user_data); |
||||
|
||||
ssize_t utun_test_default_on_recv(uint8_t* buffer, size_t buffer_size, |
||||
struct sockaddr* src_addr, socklen_t* src_addrlen, |
||||
void* user_data); |
||||
|
||||
#endif |
||||
@ -0,0 +1,10 @@
|
||||
В конфиге явно прописываются сокеты с ip и портом. |
||||
Через эти сокеты идёт весь трафик, случайные порты клиент не использует - только эти сокеты. |
||||
Соответственно, возможен только один линк между двумя пирами по одному каналу. |
||||
|
||||
виртуальные сокеты - плохо т.к. используются ip/port для поиск подключения |
||||
|
||||
|
||||
план: |
||||
|
||||
1. доработать тест чтобы после init соединения еще и посылал пакеты |
||||
@ -0,0 +1,390 @@
|
||||
/** |
||||
* Debug configuration implementation |
||||
* Runtime debug configuration system for flexible debug output control |
||||
*/ |
||||
|
||||
#include "debug_config.h" |
||||
#include <stdlib.h> |
||||
#include <string.h> |
||||
#include <errno.h> |
||||
#include <ctype.h> |
||||
#include <stdarg.h> |
||||
#include <stdio.h> |
||||
#include <time.h> |
||||
#include <strings.h> |
||||
|
||||
/* Global debug configuration */ |
||||
debug_config_t g_debug_config = { |
||||
.default_level = DEBUG_LEVEL_ERROR, |
||||
.categories = DEBUG_CATEGORY_ALL, |
||||
.category_levels = {0}, |
||||
.timestamp_enabled = 1, |
||||
.function_name_enabled = 1, |
||||
.file_line_enabled = 1, |
||||
.color_enabled = 1, |
||||
.max_output_per_second = 0, |
||||
.output_file = NULL |
||||
}; |
||||
|
||||
/* Output file handle */ |
||||
FILE* debug_output_file = NULL; |
||||
|
||||
/* Rate limiting */ |
||||
static size_t output_count = 0; |
||||
static time_t last_output_time = 0; |
||||
|
||||
/* ANSI color codes */ |
||||
static const char* color_red = "\033[31m"; |
||||
static const char* color_yellow = "\033[33m"; |
||||
static const char* color_green = "\033[32m"; |
||||
static const char* color_blue = "\033[34m"; |
||||
static const char* color_magenta = "\033[35m"; |
||||
static const char* color_cyan = "\033[36m"; |
||||
static const char* color_reset = "\033[0m"; |
||||
|
||||
/* Get category by name */ |
||||
static debug_category_t get_category_by_name(const char* name) { |
||||
struct { |
||||
const char* n; |
||||
debug_category_t c; |
||||
} map[] = { |
||||
{"none", DEBUG_CATEGORY_NONE}, |
||||
{"uasync", DEBUG_CATEGORY_UASYNC}, |
||||
{"ll_queue", DEBUG_CATEGORY_LL_QUEUE}, |
||||
{"connection", DEBUG_CATEGORY_CONNECTION}, |
||||
{"etcp", DEBUG_CATEGORY_ETCP}, |
||||
{"crypto", DEBUG_CATEGORY_CRYPTO}, |
||||
{"memory", DEBUG_CATEGORY_MEMORY}, |
||||
{"timing", DEBUG_CATEGORY_TIMING}, |
||||
{"config", DEBUG_CATEGORY_CONFIG}, |
||||
{"tun", DEBUG_CATEGORY_TUN}, |
||||
{"routing", DEBUG_CATEGORY_ROUTING}, |
||||
{"timers", DEBUG_CATEGORY_TIMERS}, |
||||
{"all", DEBUG_CATEGORY_ALL}, |
||||
{NULL, 0} |
||||
}; |
||||
|
||||
for (int i = 0; map[i].n; i++) { |
||||
if (strcasecmp(map[i].n, name) == 0) { |
||||
return map[i].c; |
||||
} |
||||
} |
||||
return DEBUG_CATEGORY_NONE; |
||||
} |
||||
|
||||
/* Get level by name */ |
||||
static debug_level_t get_level_by_name(const char* name) { |
||||
struct { |
||||
const char* n; |
||||
debug_level_t l; |
||||
} map[] = { |
||||
{"none", DEBUG_LEVEL_NONE}, |
||||
{"error", DEBUG_LEVEL_ERROR}, |
||||
{"warn", DEBUG_LEVEL_WARN}, |
||||
{"info", DEBUG_LEVEL_INFO}, |
||||
{"debug", DEBUG_LEVEL_DEBUG}, |
||||
{"trace", DEBUG_LEVEL_TRACE}, |
||||
{NULL, 0} |
||||
}; |
||||
|
||||
for (int i = 0; map[i].n; i++) { |
||||
if (strcasecmp(map[i].n, name) == 0) { |
||||
return map[i].l; |
||||
} |
||||
} |
||||
return DEBUG_LEVEL_NONE; |
||||
} |
||||
|
||||
/* Initialize debug system with default settings */ |
||||
void debug_config_init(void) { |
||||
g_debug_config.default_level = DEBUG_LEVEL_ERROR; |
||||
g_debug_config.categories = DEBUG_CATEGORY_ALL; |
||||
memset(g_debug_config.category_levels, 0, sizeof(g_debug_config.category_levels)); |
||||
g_debug_config.timestamp_enabled = 1; |
||||
g_debug_config.function_name_enabled = 1; |
||||
g_debug_config.file_line_enabled = 1; |
||||
g_debug_config.color_enabled = 1; |
||||
g_debug_config.max_output_per_second = 0; |
||||
g_debug_config.output_file = NULL; |
||||
|
||||
if (debug_output_file && debug_output_file != stdout && debug_output_file != stderr) { |
||||
fclose(debug_output_file); |
||||
} |
||||
debug_output_file = stdout; |
||||
|
||||
output_count = 0; |
||||
last_output_time = 0; |
||||
} |
||||
|
||||
/* Set debug level */ |
||||
void debug_set_level(debug_level_t level) { |
||||
g_debug_config.default_level = level; |
||||
} |
||||
|
||||
/* Enable/disable specific categories */ |
||||
void debug_enable_category(debug_category_t category) { |
||||
g_debug_config.categories |= category; |
||||
} |
||||
|
||||
void debug_disable_category(debug_category_t category) { |
||||
g_debug_config.categories &= ~category; |
||||
} |
||||
|
||||
void debug_set_categories(uint32_t categories) { |
||||
g_debug_config.categories = categories; |
||||
} |
||||
|
||||
/* Configure output options */ |
||||
void debug_enable_timestamp(int enable) { |
||||
g_debug_config.timestamp_enabled = enable; |
||||
} |
||||
|
||||
void debug_enable_function_name(int enable) { |
||||
g_debug_config.function_name_enabled = enable; |
||||
} |
||||
|
||||
void debug_enable_file_line(int enable) { |
||||
g_debug_config.file_line_enabled = enable; |
||||
} |
||||
|
||||
void debug_enable_color(int enable) { |
||||
g_debug_config.color_enabled = enable; |
||||
} |
||||
|
||||
void debug_set_rate_limit(size_t max_per_second) { |
||||
g_debug_config.max_output_per_second = max_per_second; |
||||
} |
||||
|
||||
void debug_set_output_file(const char* file_path) { |
||||
if (file_path == NULL) { |
||||
if (debug_output_file && debug_output_file != stdout && debug_output_file != stderr) { |
||||
fclose(debug_output_file); |
||||
} |
||||
debug_output_file = stdout; |
||||
g_debug_config.output_file = NULL; |
||||
} else { |
||||
FILE* new_file = fopen(file_path, "a"); |
||||
if (new_file) { |
||||
if (debug_output_file && debug_output_file != stdout && debug_output_file != stderr) { |
||||
fclose(debug_output_file); |
||||
} |
||||
debug_output_file = new_file; |
||||
g_debug_config.output_file = file_path; |
||||
} |
||||
} |
||||
} |
||||
|
||||
/* Check if debug output should be shown for given level and category */ |
||||
int debug_should_output(debug_level_t level, debug_category_t category) { |
||||
/* Check if category is enabled */ |
||||
if (!(g_debug_config.categories & category)) { |
||||
return 0; |
||||
} |
||||
|
||||
/* Check if level is sufficient */ |
||||
debug_level_t eff_level = debug_get_effective_level(category); |
||||
if (level > eff_level) { |
||||
return 0; |
||||
} |
||||
|
||||
/* Rate limiting check */ |
||||
if (g_debug_config.max_output_per_second > 0) { |
||||
time_t current_time = time(NULL); |
||||
if (current_time != last_output_time) { |
||||
output_count = 0; |
||||
last_output_time = current_time; |
||||
} |
||||
|
||||
if (output_count >= g_debug_config.max_output_per_second) { |
||||
return 0; |
||||
} |
||||
output_count++; |
||||
} |
||||
|
||||
return 1; |
||||
} |
||||
|
||||
/* Get current debug level for a category */ |
||||
debug_level_t debug_get_effective_level(debug_category_t category) { |
||||
if (category == DEBUG_CATEGORY_ALL || category == DEBUG_CATEGORY_NONE) { |
||||
return g_debug_config.default_level; |
||||
} |
||||
|
||||
/* Assume single category bit */ |
||||
uint32_t cat = category; |
||||
if (__builtin_popcount(cat) != 1) { |
||||
return DEBUG_LEVEL_NONE; /* Not supported for multiple */ |
||||
} |
||||
|
||||
int index = __builtin_ctz(cat); |
||||
if (index >= NUM_DEBUG_CATEGORIES) { |
||||
return DEBUG_LEVEL_NONE; |
||||
} |
||||
|
||||
debug_level_t lev = g_debug_config.category_levels[index]; |
||||
return (lev == DEBUG_LEVEL_NONE) ? g_debug_config.default_level : lev; |
||||
} |
||||
|
||||
/* Get color for debug level */ |
||||
static const char* get_level_color(debug_level_t level) { |
||||
if (!g_debug_config.color_enabled) { |
||||
return ""; |
||||
} |
||||
|
||||
switch (level) { |
||||
case DEBUG_LEVEL_ERROR: return color_red; |
||||
case DEBUG_LEVEL_WARN: return color_yellow; |
||||
case DEBUG_LEVEL_INFO: return color_green; |
||||
case DEBUG_LEVEL_DEBUG: return color_blue; |
||||
case DEBUG_LEVEL_TRACE: return color_magenta; |
||||
default: return ""; |
||||
} |
||||
} |
||||
|
||||
/* Get level name */ |
||||
static const char* get_level_name(debug_level_t level) { |
||||
switch (level) { |
||||
case DEBUG_LEVEL_ERROR: return "ERROR"; |
||||
case DEBUG_LEVEL_WARN: return "WARN"; |
||||
case DEBUG_LEVEL_INFO: return "INFO"; |
||||
case DEBUG_LEVEL_DEBUG: return "DEBUG"; |
||||
case DEBUG_LEVEL_TRACE: return "TRACE"; |
||||
default: return "UNKNOWN"; |
||||
} |
||||
} |
||||
|
||||
/* Format and output debug message */ |
||||
void debug_output(debug_level_t level, debug_category_t category, |
||||
const char* function, const char* file, int line, |
||||
const char* format, ...) { |
||||
|
||||
#define BUFFER_SIZE 4096 |
||||
char buffer[BUFFER_SIZE]; |
||||
int offset = 0; |
||||
size_t remaining = BUFFER_SIZE; |
||||
|
||||
va_list args; |
||||
va_start(args, format); |
||||
|
||||
FILE* output = debug_output_file ? debug_output_file : stdout; |
||||
|
||||
/* Add timestamp if enabled */ |
||||
if (g_debug_config.timestamp_enabled) { |
||||
time_t now = time(NULL); |
||||
struct tm* tm_info = localtime(&now); |
||||
char time_str[32]; |
||||
strftime(time_str, sizeof(time_str), "%Y-%m-%d %H:%M:%S", tm_info); |
||||
offset += snprintf(buffer + offset, remaining, "[%s] ", time_str); |
||||
remaining = BUFFER_SIZE - offset; |
||||
} |
||||
|
||||
/* Add level and color */ |
||||
const char* color = get_level_color(level); |
||||
const char* level_name = get_level_name(level); |
||||
if (g_debug_config.color_enabled) { |
||||
offset += snprintf(buffer + offset, remaining, "%s[%s]%s ", color, level_name, color_reset); |
||||
} else { |
||||
offset += snprintf(buffer + offset, remaining, "[%s] ", level_name); |
||||
} |
||||
remaining = BUFFER_SIZE - offset; |
||||
|
||||
/* Add category */ |
||||
offset += snprintf(buffer + offset, remaining, "[%d] ", category); |
||||
remaining = BUFFER_SIZE - offset; |
||||
|
||||
/* Add function name if enabled */ |
||||
if (g_debug_config.function_name_enabled && function) { |
||||
offset += snprintf(buffer + offset, remaining, "%s() ", function); |
||||
remaining = BUFFER_SIZE - offset; |
||||
} |
||||
|
||||
/* Add file:line if enabled */ |
||||
if (g_debug_config.file_line_enabled && file) { |
||||
offset += snprintf(buffer + offset, remaining, "(%s:%d) ", file, line); |
||||
remaining = BUFFER_SIZE - offset; |
||||
} |
||||
|
||||
/* Add the actual message */ |
||||
offset += vsnprintf(buffer + offset, remaining, format, args); |
||||
remaining = BUFFER_SIZE - offset; |
||||
|
||||
/* Add newline */ |
||||
if (remaining > 1) { |
||||
buffer[offset] = '\n'; |
||||
buffer[offset + 1] = '\0'; |
||||
} else { |
||||
buffer[BUFFER_SIZE - 2] = '\n'; |
||||
buffer[BUFFER_SIZE - 1] = '\0'; |
||||
} |
||||
|
||||
va_end(args); |
||||
|
||||
/* Output the entire line at once */ |
||||
fprintf(output, "%s", buffer); |
||||
fflush(output); |
||||
#undef BUFFER_SIZE |
||||
} |
||||
|
||||
/* Parse debug configuration from string */ |
||||
int debug_parse_config(const char* config_string) { |
||||
if (!config_string || !*config_string) { |
||||
return 0; |
||||
} |
||||
|
||||
char* str = strdup(config_string); |
||||
if (!str) { |
||||
return -1; |
||||
} |
||||
|
||||
char* token = strtok(str, ","); |
||||
while (token) { |
||||
/* Trim leading spaces */ |
||||
while (isspace(*token)) { |
||||
token++; |
||||
} |
||||
|
||||
char* colon = strchr(token, ':'); |
||||
if (!colon) { |
||||
free(str); |
||||
return -1; |
||||
} |
||||
|
||||
*colon = '\0'; |
||||
char* cat_name = token; |
||||
char* level_name = colon + 1; |
||||
|
||||
/* Trim leading spaces in level_name */ |
||||
while (isspace(*level_name)) { |
||||
level_name++; |
||||
} |
||||
|
||||
/* Find category */ |
||||
debug_category_t cat = get_category_by_name(cat_name); |
||||
if (cat == DEBUG_CATEGORY_NONE) { |
||||
free(str); |
||||
return -1; |
||||
} |
||||
|
||||
/* Find level */ |
||||
debug_level_t lev = get_level_by_name(level_name); |
||||
if (lev == DEBUG_LEVEL_NONE) { |
||||
free(str); |
||||
return -1; |
||||
} |
||||
|
||||
if (cat == DEBUG_CATEGORY_ALL) { |
||||
g_debug_config.default_level = lev; |
||||
} else { |
||||
g_debug_config.categories |= cat; |
||||
int index = __builtin_ctz(cat); |
||||
if (index < NUM_DEBUG_CATEGORIES) { |
||||
g_debug_config.category_levels[index] = lev; |
||||
} |
||||
} |
||||
|
||||
token = strtok(NULL, ","); |
||||
} |
||||
|
||||
free(str); |
||||
return 0; |
||||
} |
||||
@ -0,0 +1,132 @@
|
||||
/** |
||||
* Runtime debug configuration system |
||||
* Provides flexible control over debug output without recompilation |
||||
*/ |
||||
|
||||
#ifndef DEBUG_CONFIG_H |
||||
#define DEBUG_CONFIG_H |
||||
|
||||
#include <stdint.h> |
||||
#include <stddef.h> |
||||
#include <time.h> |
||||
|
||||
#ifdef __cplusplus |
||||
extern "C" { |
||||
#endif |
||||
|
||||
/* Number of categories (bits 0-10) */ |
||||
#define NUM_DEBUG_CATEGORIES 11 |
||||
|
||||
/* Debug levels */ |
||||
typedef enum { |
||||
DEBUG_LEVEL_NONE = 0, // No debug output |
||||
DEBUG_LEVEL_ERROR = 1, // Errors only |
||||
DEBUG_LEVEL_WARN = 2, // Warnings and errors |
||||
DEBUG_LEVEL_INFO = 3, // Info, warnings, errors |
||||
DEBUG_LEVEL_DEBUG = 4, // Full debug output |
||||
DEBUG_LEVEL_TRACE = 5 // Trace everything |
||||
} debug_level_t; |
||||
|
||||
/* Debug categories - can be combined with bitwise OR */ |
||||
typedef enum { |
||||
DEBUG_CATEGORY_NONE = 0, |
||||
DEBUG_CATEGORY_UASYNC = 1 << 0, // u_async module |
||||
DEBUG_CATEGORY_LL_QUEUE = 1 << 1, // ll_queue module |
||||
DEBUG_CATEGORY_CONNECTION = 1 << 2, // connection module |
||||
DEBUG_CATEGORY_ETCP = 1 << 3, // etcp module |
||||
DEBUG_CATEGORY_CRYPTO = 1 << 4, // crypto operations |
||||
DEBUG_CATEGORY_MEMORY = 1 << 5, // memory management |
||||
DEBUG_CATEGORY_TIMING = 1 << 6, // timing/performance |
||||
DEBUG_CATEGORY_CONFIG = 1 << 7, // configuration parsing |
||||
DEBUG_CATEGORY_TUN = 1 << 8, // TUN interface |
||||
DEBUG_CATEGORY_ROUTING = 1 << 9, // routing table |
||||
DEBUG_CATEGORY_TIMERS = 1 << 10, // timer management |
||||
DEBUG_CATEGORY_ALL = 0xFFFFFFFF |
||||
} debug_category_t; |
||||
|
||||
/* Debug configuration structure */ |
||||
typedef struct { |
||||
debug_level_t default_level; // Default debug level |
||||
uint32_t categories; // Enabled categories (bitmask) |
||||
debug_level_t category_levels[NUM_DEBUG_CATEGORIES]; // Per-category levels (NONE means use default) |
||||
int timestamp_enabled; // Include timestamps in output |
||||
int function_name_enabled; // Include function names |
||||
int file_line_enabled; // Include file:line info |
||||
int color_enabled; // Use ANSI colors (if terminal supports) |
||||
size_t max_output_per_second; // Rate limiting (0 = unlimited) |
||||
const char* output_file; // NULL = stdout, otherwise file path |
||||
} debug_config_t; |
||||
|
||||
/* Global debug configuration */ |
||||
extern debug_config_t g_debug_config; |
||||
|
||||
/* Initialize debug system with default settings */ |
||||
void debug_config_init(void); |
||||
|
||||
/* Set debug level */ |
||||
void debug_set_level(debug_level_t level); |
||||
|
||||
/* Enable/disable specific categories */ |
||||
void debug_enable_category(debug_category_t category); |
||||
void debug_disable_category(debug_category_t category); |
||||
void debug_set_categories(uint32_t categories); |
||||
|
||||
/* Configure output options */ |
||||
void debug_enable_timestamp(int enable); |
||||
void debug_enable_function_name(int enable); |
||||
void debug_enable_file_line(int enable); |
||||
void debug_enable_color(int enable); |
||||
void debug_set_rate_limit(size_t max_per_second); |
||||
void debug_set_output_file(const char* file_path); |
||||
|
||||
/* Parse debug configuration from string (e.g., "uasync:debug,ll_queue:info") */ |
||||
int debug_parse_config(const char* config_string); |
||||
|
||||
#include <stdio.h> |
||||
extern FILE* debug_output_file; |
||||
|
||||
/* Check if debug output should be shown for given level and category */ |
||||
int debug_should_output(debug_level_t level, debug_category_t category); |
||||
|
||||
/* Get current debug level for a category */ |
||||
debug_level_t debug_get_effective_level(debug_category_t category); |
||||
|
||||
/* Format and output debug message (internal use by macros) */ |
||||
void debug_output(debug_level_t level, debug_category_t category, |
||||
const char* function, const char* file, int line, |
||||
const char* format, ...); |
||||
|
||||
/* Convenience macros for debug output */ |
||||
#define DEBUG_ERROR(category, fmt, ...) \ |
||||
do { if (debug_should_output(DEBUG_LEVEL_ERROR, category)) { \ |
||||
debug_output(DEBUG_LEVEL_ERROR, category, __FUNCTION__, __FILE__, __LINE__, fmt, ##__VA_ARGS__); \ |
||||
} } while(0) |
||||
|
||||
#define DEBUG_WARN(category, fmt, ...) \ |
||||
do { if (debug_should_output(DEBUG_LEVEL_WARN, category)) { \ |
||||
debug_output(DEBUG_LEVEL_WARN, category, __FUNCTION__, __FILE__, __LINE__, fmt, ##__VA_ARGS__); \ |
||||
} } while(0) |
||||
|
||||
#define DEBUG_INFO(category, fmt, ...) \ |
||||
do { if (debug_should_output(DEBUG_LEVEL_INFO, category)) { \ |
||||
debug_output(DEBUG_LEVEL_INFO, category, __FUNCTION__, __FILE__, __LINE__, fmt, ##__VA_ARGS__); \ |
||||
} } while(0) |
||||
|
||||
#define DEBUG_DEBUG(category, fmt, ...) \ |
||||
do { if (debug_should_output(DEBUG_LEVEL_DEBUG, category)) { \ |
||||
debug_output(DEBUG_LEVEL_DEBUG, category, __FUNCTION__, __FILE__, __LINE__, fmt, ##__VA_ARGS__); \ |
||||
} } while(0) |
||||
|
||||
#define DEBUG_TRACE(category, fmt, ...) \ |
||||
do { if (debug_should_output(DEBUG_LEVEL_TRACE, category)) { \ |
||||
debug_output(DEBUG_LEVEL_TRACE, category, __FUNCTION__, __FILE__, __LINE__, fmt, ##__VA_ARGS__); \ |
||||
} } while(0) |
||||
|
||||
/* Backward compatibility - default to ERROR level if not specified */ |
||||
#define DEBUG_OUTPUT(fmt, ...) DEBUG_ERROR(DEBUG_CATEGORY_ALL, fmt, ##__VA_ARGS__) |
||||
|
||||
#ifdef __cplusplus |
||||
} |
||||
#endif |
||||
|
||||
#endif // DEBUG_CONFIG_H |
||||
@ -1,829 +0,0 @@
|
||||
// ll_queue.c |
||||
#include <stdlib.h> |
||||
#include <string.h> |
||||
#include <stdio.h> |
||||
#include <assert.h> |
||||
#include "ll_queue.h" |
||||
#include "u_async.h" |
||||
#include "debug_config.h" |
||||
|
||||
// Предварительное объявление для отложенного возобновления |
||||
static void queue_resume_timeout_cb(void* arg); |
||||
|
||||
// Проверить и запустить ожидающие коллбэки |
||||
static void check_waiters(struct ll_queue* q) { |
||||
if (!q || !q->waiters) return; |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "check_waiters: checking waiters, count=%d, bytes=%zu", |
||||
q->count, q->total_bytes); |
||||
|
||||
// Сначала собрать список коллбэков для вызова, чтобы избежать проблем с модификацией списка во время итерации |
||||
struct queue_waiter* to_call = NULL; |
||||
struct queue_waiter* to_call_tail = NULL; |
||||
|
||||
struct queue_waiter** pprev = &q->waiters; |
||||
struct queue_waiter* waiter = q->waiters; |
||||
|
||||
while (waiter) { |
||||
struct queue_waiter* next = waiter->next; |
||||
|
||||
// Проверить условие: не больше max_packets и не больше max_bytes |
||||
// max_bytes = 0 означает "не проверять байты" |
||||
if (q->count <= waiter->max_packets && (waiter->max_bytes == 0 || q->total_bytes <= waiter->max_bytes)) { |
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "check_waiters: condition met, calling callback, count=%d<=%d, bytes=%zu<=%zu (max_bytes_check=%s)", |
||||
q->count, waiter->max_packets, q->total_bytes, waiter->max_bytes, |
||||
waiter->max_bytes == 0 ? "disabled" : "enabled"); |
||||
// Удалить waiter из списка |
||||
*pprev = next; |
||||
// Добавить в список для вызова |
||||
waiter->next = NULL; |
||||
if (to_call_tail) { |
||||
to_call_tail->next = waiter; |
||||
} else { |
||||
to_call = waiter; |
||||
} |
||||
to_call_tail = waiter; |
||||
} else { |
||||
// Условие не выполнено - оставить в списке |
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "check_waiters: condition NOT met, count=%d>%d or bytes=%zu>%zu (max_bytes_check=%s)", |
||||
q->count, waiter->max_packets, q->total_bytes, waiter->max_bytes, |
||||
waiter->max_bytes == 0 ? "disabled" : "enabled"); |
||||
pprev = &waiter->next; |
||||
} |
||||
waiter = next; |
||||
} |
||||
|
||||
// Теперь вызвать коллбэки |
||||
waiter = to_call; |
||||
while (waiter) { |
||||
struct queue_waiter* next = waiter->next; |
||||
waiter->callback(q, waiter->callback_arg); |
||||
if (q->pool) { |
||||
memory_pool_free(q->pool, waiter); |
||||
} else { |
||||
free(waiter); |
||||
} |
||||
waiter = next; |
||||
} |
||||
} |
||||
|
||||
// ==================== Управление очередью ==================== |
||||
|
||||
struct ll_queue* queue_new(struct UASYNC* ua, struct memory_pool* pool) { |
||||
if (!ua) return NULL; |
||||
|
||||
struct ll_queue* q = calloc(1, sizeof(struct ll_queue)); |
||||
if (!q) return NULL; |
||||
|
||||
q->head = NULL; |
||||
q->tail = NULL; |
||||
q->count = 0; |
||||
q->total_bytes = 0; |
||||
q->size_limit = -1; // По умолчанию без ограничения |
||||
q->callback = NULL; |
||||
q->callback_arg = NULL; |
||||
q->callback_suspended = 0; // Коллбэки разрешены изначально |
||||
q->resume_timeout_id = NULL; |
||||
q->ua = ua; |
||||
q->waiters = NULL; |
||||
q->pool=pool; |
||||
q->version = 0; |
||||
|
||||
return q; |
||||
} |
||||
|
||||
void queue_free(struct ll_queue* q) { |
||||
if (!q) return; |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_free: freeing queue %p, head=%p, tail=%p, count=%d", |
||||
q, q->head, q->tail, q->count); |
||||
|
||||
// Освободить все элементы (уменьшить счетчик ссылок) |
||||
struct ll_entry* entry = q->head; |
||||
int entry_count = 0; |
||||
while (entry) { |
||||
struct ll_entry* next = entry->next; |
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_free: releasing entry %p (entry %d), ref_count=%d", |
||||
entry, entry_count++, entry->ref_count); |
||||
entry->ref_count--; // Дополнительно уменьшить за присутствие в очереди |
||||
queue_entry_free(entry); // Это уменьшит ref_count и освободит только если ref_count == 0 |
||||
entry = next; |
||||
} |
||||
|
||||
// Освободить все ожидающие коллбэки |
||||
struct queue_waiter* waiter = q->waiters; |
||||
int waiter_count = 0; |
||||
while (waiter) { |
||||
struct queue_waiter* next = waiter->next; |
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_free: freeing waiter %p (waiter %d)", waiter, waiter_count++); |
||||
if (q->pool) { |
||||
memory_pool_free(q->pool, waiter); |
||||
} else { |
||||
free(waiter); |
||||
} |
||||
waiter = next; |
||||
} |
||||
|
||||
// Отменить отложенное возобновление если запланировано |
||||
if (q->resume_timeout_id) { |
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_free: cancelling resume timeout %p", q->resume_timeout_id); |
||||
uasync_cancel_timeout(q->ua, q->resume_timeout_id); |
||||
} |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_free: freeing queue structure %p", q); |
||||
free(q); |
||||
} |
||||
|
||||
// ==================== Конфигурация очереди ==================== |
||||
|
||||
void queue_set_callback(struct ll_queue* q, queue_callback_fn cbk_fn, void* arg) { |
||||
if (!q) return; |
||||
q->callback = cbk_fn; |
||||
q->callback_arg = arg; |
||||
} |
||||
|
||||
static void queue_resume_timeout_cb(void* arg) { |
||||
struct ll_queue* q = (struct ll_queue*)arg; |
||||
if (!q || !q->callback) return; |
||||
|
||||
// Очистить ID таймаута (таймаут сработал) |
||||
q->resume_timeout_id = NULL; |
||||
|
||||
// Разрешить коллбэки |
||||
q->callback_suspended = 0; |
||||
|
||||
// Если в очереди есть элементы, вызвать коллбэк с первым элементом |
||||
// Обработчик должен извлечь этот элемент вызовом queue_entry_get() |
||||
if (q->head) { |
||||
q->callback(q, q->head, q->callback_arg); |
||||
} |
||||
} |
||||
|
||||
void queue_resume_callback(struct ll_queue* q) { |
||||
if (!q || !q->callback) return; |
||||
|
||||
// Если уже есть отложенное возобновление, ничего не делать |
||||
if (q->resume_timeout_id) { |
||||
return; |
||||
} |
||||
|
||||
// Запланировать отложенное возобновление через uasync |
||||
q->resume_timeout_id = uasync_set_timeout(q->ua, 0, q, queue_resume_timeout_cb); |
||||
} |
||||
|
||||
void queue_set_size_limit(struct ll_queue* q, int lim) { |
||||
if (!q) return; |
||||
q->size_limit = lim; |
||||
} |
||||
|
||||
// ==================== Управление элементами ==================== |
||||
|
||||
struct ll_entry* queue_entry_new(size_t data_size) { |
||||
// Выделить память под структуру + область данных |
||||
struct ll_entry* entry = malloc(sizeof(struct ll_entry) + data_size); |
||||
if (!entry) return NULL; |
||||
|
||||
entry->next = NULL; |
||||
entry->prev = NULL; // Инициализировать prev для двусвязного списка |
||||
entry->size = data_size; |
||||
entry->ref_count = 1; // Начальный счетчик ссылок |
||||
// Область данных оставить неинициализированной для производительности |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_entry_new: created entry %p, size=%zu, ref_count=%d", |
||||
entry, data_size, entry->ref_count); |
||||
|
||||
return entry; |
||||
} |
||||
|
||||
void queue_entry_free(struct ll_entry* entry) { |
||||
if (!entry) return; |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_entry_free: entry=%p, ref_count=%d", |
||||
entry, entry->ref_count); |
||||
|
||||
if (entry->ref_count <= 0) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_LL_QUEUE, "queue_entry_free: entry %p has invalid ref_count=%d", |
||||
entry, entry->ref_count); |
||||
return; // Предотвратить double-free |
||||
} |
||||
|
||||
entry->ref_count--; |
||||
if (entry->ref_count == 0) { |
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_entry_free: actually freeing entry %p", entry); |
||||
free(entry); |
||||
} else { |
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_entry_free: decremented ref_count to %d for entry %p", |
||||
entry->ref_count, entry); |
||||
} |
||||
} |
||||
|
||||
// ==================== Операции с очередью ==================== |
||||
|
||||
int queue_entry_put(struct ll_queue* q, struct ll_entry* entry) { |
||||
if (!q || !entry) return -1; |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_entry_put: entry=%p, size=%zu, count=%d", |
||||
entry, entry->size, q->count); |
||||
|
||||
// Проверить лимит размера |
||||
if (q->size_limit >= 0 && q->count >= q->size_limit) { |
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_entry_put: size limit exceeded, freeing entry %p", entry); |
||||
queue_entry_free(entry); |
||||
return -1; |
||||
} |
||||
|
||||
// Увеличить счетчик ссылок при добавлении в очередь |
||||
entry->ref_count++; |
||||
|
||||
// Добавить в хвост (FIFO) - двусвязный список |
||||
entry->next = NULL; |
||||
entry->prev = q->tail; |
||||
|
||||
if (q->tail) { |
||||
q->tail->next = entry; |
||||
} else { |
||||
q->head = entry; // Очередь была пустой |
||||
} |
||||
q->tail = entry; |
||||
q->count++; |
||||
q->total_bytes += entry->size; |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_entry_put: added entry %p, new count=%d, total_bytes=%zu, ref_count=%d", |
||||
entry, q->count, q->total_bytes, entry->ref_count); |
||||
|
||||
// Инвалидировать позиции |
||||
q->version++; |
||||
|
||||
// Если коллбэки разрешены - вызвать коллбэк |
||||
// Это запускает автоматическую обработку очереди |
||||
if (!q->callback_suspended && q->callback) { |
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_entry_put: calling callback for entry %p", entry); |
||||
|
||||
// Приостановить коллбэки во время выполнения коллбэка, чтобы предотвратить рекурсию |
||||
q->callback_suspended = 1; |
||||
q->callback(q, q->head, q->callback_arg); |
||||
// Не восстанавливать здесь - восстановление происходит через queue_resume_callback |
||||
} |
||||
|
||||
// Проверить ожидающие коллбэки |
||||
check_waiters(q); |
||||
|
||||
return 0; |
||||
} |
||||
|
||||
int queue_entry_put_first(struct ll_queue* q, struct ll_entry* entry) { |
||||
if (!q || !entry) return -1; |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_entry_put_first: entry=%p, size=%zu, count=%d", |
||||
entry, entry->size, q->count); |
||||
|
||||
// Проверить лимит размера |
||||
if (q->size_limit >= 0 && q->count >= q->size_limit) { |
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_entry_put_first: size limit exceeded, freeing entry %p", entry); |
||||
queue_entry_free(entry); |
||||
return -1; |
||||
} |
||||
|
||||
// Увеличить счетчик ссылок при добавлении в очередь |
||||
entry->ref_count++; |
||||
|
||||
// Добавить в голову (LIFO, высокий приоритет) - двусвязный список |
||||
entry->next = q->head; |
||||
entry->prev = NULL; |
||||
|
||||
if (q->head) { |
||||
q->head->prev = entry; |
||||
} else { |
||||
q->tail = entry; // Очередь была пустой |
||||
} |
||||
q->head = entry; |
||||
q->count++; |
||||
q->total_bytes += entry->size; |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_entry_put_first: added entry %p, new count=%d, total_bytes=%zu, ref_count=%d", |
||||
entry, q->count, q->total_bytes, entry->ref_count); |
||||
|
||||
// Инвалидировать позиции |
||||
q->version++; |
||||
|
||||
// Если коллбэки разрешены - вызвать коллбэк |
||||
if (!q->callback_suspended && q->callback) { |
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_entry_put_first: calling callback for entry %p", entry); |
||||
q->callback_suspended = 1; |
||||
q->callback(q, q->head, q->callback_arg); |
||||
} |
||||
|
||||
// Проверить ожидающие коллбэки |
||||
check_waiters(q); |
||||
|
||||
return 0; |
||||
} |
||||
|
||||
struct ll_entry* queue_entry_get(struct ll_queue* q) { |
||||
if (!q || !q->head) return NULL; |
||||
|
||||
struct ll_entry* entry = q->head; |
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_entry_get: retrieving entry %p, size=%zu, ref_count=%d", |
||||
entry, entry->size, entry->ref_count); |
||||
|
||||
// Удалить из двусвязного списка |
||||
q->head = entry->next; |
||||
if (q->head) { |
||||
q->head->prev = NULL; |
||||
} else { |
||||
q->tail = NULL; // Очередь стала пустой |
||||
} |
||||
q->count--; |
||||
q->total_bytes -= entry->size; |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_entry_get: removed entry %p, new count=%d, total_bytes=%zu", |
||||
entry, q->count, q->total_bytes); |
||||
|
||||
entry->next = NULL; |
||||
entry->prev = NULL; // Полностью отсоединить от очереди |
||||
|
||||
// Уменьшить счетчик ссылок при извлечении из очереди |
||||
// entry->ref_count был увеличен при добавлении в очередь |
||||
// теперь уменьшаем, но не освобождаем, так как вызывающий код должен это сделать |
||||
entry->ref_count--; |
||||
|
||||
// Инвалидировать позиции |
||||
q->version++; |
||||
|
||||
// При извлечении элемента приостанавливаем коллбэки |
||||
// Это предотвращает рекурсию если во время обработки добавляются новые элементы |
||||
q->callback_suspended = 1; |
||||
|
||||
// Проверить ожидающие коллбэки при извлечении элемента |
||||
// Это важно для waiters, которые ожидают уменьшения очереди |
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_entry_get: about to call check_waiters, count=%d", q->count); |
||||
check_waiters(q); |
||||
|
||||
return entry; |
||||
} |
||||
|
||||
int queue_entry_count(struct ll_queue* q) { |
||||
if (!q) return 0; |
||||
return q->count; |
||||
} |
||||
|
||||
// ==================== Асинхронное ожидание ==================== |
||||
|
||||
struct queue_waiter* queue_wait_threshold(struct ll_queue* q, int max_packets, size_t max_bytes, |
||||
queue_threshold_callback_fn callback, void* arg) { |
||||
if (!q || !callback) return NULL; |
||||
|
||||
// Создать новый waiter |
||||
struct queue_waiter* waiter; |
||||
if (q->pool) { |
||||
waiter = (struct queue_waiter*)memory_pool_alloc(q->pool); |
||||
} else { |
||||
waiter = malloc(sizeof(struct queue_waiter)); |
||||
} |
||||
if (!waiter) return NULL; |
||||
|
||||
waiter->max_packets = max_packets; |
||||
waiter->max_bytes = max_bytes; |
||||
waiter->callback = callback; |
||||
waiter->callback_arg = arg; |
||||
waiter->next = NULL; |
||||
|
||||
// Проверить условие немедленно |
||||
if (q->count <= max_packets && (max_bytes == 0 || q->total_bytes <= max_bytes)) { |
||||
// Условие уже выполнено - вызвать коллбэк и освободить waiter |
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_wait_threshold: condition already met, count=%d<=%d, bytes=%zu<=%zu, calling callback", |
||||
q->count, max_packets, q->total_bytes, max_bytes); |
||||
callback(q, arg); |
||||
if (q->pool) { |
||||
memory_pool_free(q->pool, waiter); |
||||
} else { |
||||
free(waiter); |
||||
} |
||||
return NULL; |
||||
} |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_wait_threshold: registering waiter, count=%d, bytes=%zu, max_packets=%d, max_bytes=%zu", |
||||
q->count, q->total_bytes, max_packets, max_bytes); |
||||
|
||||
// Добавить в список ожидающих |
||||
waiter->next = q->waiters; |
||||
q->waiters = waiter; |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_wait_threshold: waiter registered successfully"); |
||||
return waiter; |
||||
} |
||||
|
||||
void queue_cancel_wait(struct ll_queue* q, struct queue_waiter* waiter) { |
||||
if (!q || !waiter) return; |
||||
|
||||
// Найти и удалить waiter из списка |
||||
struct queue_waiter** pprev = &q->waiters; |
||||
struct queue_waiter* w = q->waiters; |
||||
|
||||
while (w) { |
||||
if (w == waiter) { |
||||
*pprev = w->next; |
||||
if (q->pool) { |
||||
memory_pool_free(q->pool, w); |
||||
} else { |
||||
free(w); |
||||
} |
||||
return; |
||||
} |
||||
pprev = &w->next; |
||||
w = w->next; |
||||
} |
||||
} |
||||
|
||||
// ==================== Статистика и метрики ==================== |
||||
|
||||
void queue_get_pool_stats(struct ll_queue* q, size_t* waiter_allocations, size_t* waiter_reuse) { |
||||
if (!q || !q->pool) { |
||||
if (waiter_allocations) *waiter_allocations = 0; |
||||
if (waiter_reuse) *waiter_reuse = 0; |
||||
return; |
||||
} |
||||
|
||||
memory_pool_get_stats(q->pool, waiter_allocations, waiter_reuse); |
||||
} |
||||
|
||||
// ==================== Управление позициями ==================== |
||||
|
||||
struct ll_queue_pos* queue_pos_new(struct ll_queue* queue) { |
||||
if (!queue) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_LL_QUEUE, "queue_pos_new: queue is NULL"); |
||||
return NULL; |
||||
} |
||||
|
||||
struct ll_queue_pos* pos = malloc(sizeof(struct ll_queue_pos)); |
||||
if (!pos) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_LL_QUEUE, "queue_pos_new: failed to allocate position"); |
||||
return NULL; |
||||
} |
||||
|
||||
pos->queue = queue; |
||||
pos->current = NULL; |
||||
pos->prev = NULL; |
||||
pos->index = -1; // Перед head |
||||
pos->valid = 1; |
||||
pos->version = queue->version; |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_pos_new: created position %p for queue %p", pos, queue); |
||||
return pos; |
||||
} |
||||
|
||||
void queue_pos_free(struct ll_queue_pos* pos) { |
||||
if (!pos) return; |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_pos_free: freeing position %p", pos); |
||||
free(pos); |
||||
} |
||||
|
||||
void queue_pos_reset(struct ll_queue_pos* pos) { |
||||
if (!pos) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_LL_QUEUE, "queue_pos_reset: pos is NULL"); |
||||
return; |
||||
} |
||||
|
||||
// Обновить версию позиции при необходимости (вместо инвалидации) |
||||
if (pos->queue->version != pos->version) { |
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_pos_reset: updating position version from %lu to %lu", |
||||
(unsigned long)pos->version, (unsigned long)pos->queue->version); |
||||
pos->version = pos->queue->version; |
||||
} |
||||
|
||||
pos->current = NULL; |
||||
pos->prev = NULL; |
||||
pos->index = -1; |
||||
pos->valid = 1; |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_pos_reset: position reset"); |
||||
} |
||||
|
||||
int queue_pos_next(struct ll_queue_pos* pos) { |
||||
if (!pos || !pos->valid) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_LL_QUEUE, "queue_pos_next: invalid position"); |
||||
return 0; |
||||
} |
||||
|
||||
if (pos->queue->version != pos->version) { |
||||
pos->valid = 0; |
||||
DEBUG_ERROR(DEBUG_CATEGORY_LL_QUEUE, "queue_pos_next: position invalidated due to queue version change"); |
||||
return 0; |
||||
} |
||||
|
||||
if (!pos->current) { |
||||
// Перед head - переходим к head |
||||
if (!pos->queue->head) { |
||||
// Очередь пуста - некуда переходить |
||||
return 0; |
||||
} |
||||
pos->current = pos->queue->head; |
||||
pos->prev = NULL; |
||||
pos->index = 0; |
||||
} else if (pos->current->next) { |
||||
// Есть следующий элемент |
||||
pos->prev = pos->current; |
||||
pos->current = pos->current->next; |
||||
pos->index++; |
||||
} else { |
||||
// Достигнут конец очереди |
||||
return 0; |
||||
} |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_pos_next: moved to index %d, entry %p", |
||||
pos->index, pos->current); |
||||
return 1; |
||||
} |
||||
|
||||
int queue_pos_prev(struct ll_queue_pos* pos) { |
||||
if (!pos || !pos->valid) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_LL_QUEUE, "queue_pos_prev: invalid position"); |
||||
return 0; |
||||
} |
||||
|
||||
if (pos->queue->version != pos->version) { |
||||
pos->valid = 0; |
||||
DEBUG_ERROR(DEBUG_CATEGORY_LL_QUEUE, "queue_pos_prev: position invalidated due to queue version change"); |
||||
return 0; |
||||
} |
||||
|
||||
if (!pos->current) { |
||||
// Уже перед head - дальше некуда |
||||
return 0; |
||||
} else if (pos->current->prev) { |
||||
// Есть предыдущий элемент |
||||
pos->current = pos->current->prev; |
||||
pos->prev = pos->current->prev; |
||||
pos->index--; |
||||
} else { |
||||
// Достигнуто начало очереди |
||||
pos->current = NULL; |
||||
pos->prev = NULL; |
||||
pos->index = -1; |
||||
} |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_pos_prev: moved to index %d, entry %p", |
||||
pos->index, pos->current); |
||||
return 1; |
||||
} |
||||
|
||||
int queue_pos_seek(struct ll_queue_pos* pos, int target_index) { |
||||
if (!pos || !pos->valid) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_LL_QUEUE, "queue_pos_seek: invalid position"); |
||||
return 0; |
||||
} |
||||
|
||||
// Обновить версию позиции при необходимости (вместо инвалидации) |
||||
if (pos->queue->version != pos->version) { |
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_pos_seek: updating position version from %lu to %lu", |
||||
(unsigned long)pos->version, (unsigned long)pos->queue->version); |
||||
pos->version = pos->queue->version; |
||||
// Сбросить позицию, так как структура очереди могла измениться |
||||
queue_pos_reset(pos); |
||||
} |
||||
|
||||
if (target_index < -1 || target_index >= pos->queue->count) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_LL_QUEUE, "queue_pos_seek: invalid target index %d (count=%d)", |
||||
target_index, pos->queue->count); |
||||
return 0; |
||||
} |
||||
|
||||
// Простая реализация: начинаем с начала и идем вперед |
||||
// Можно оптимизировать позже, если потребуется |
||||
queue_pos_reset(pos); |
||||
|
||||
for (int i = 0; i <= target_index; i++) { |
||||
if (!queue_pos_next(pos)) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_LL_QUEUE, "queue_pos_seek: failed to reach index %d", target_index); |
||||
return 0; |
||||
} |
||||
} |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_pos_seek: seeked to index %d, entry %p", |
||||
pos->index, pos->current); |
||||
return 1; |
||||
} |
||||
|
||||
int queue_pos_insert(struct ll_queue_pos* pos, struct ll_entry* entry, int mode) { |
||||
if (!pos || !pos->valid || !entry) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_LL_QUEUE, "queue_pos_insert: invalid parameters"); |
||||
return -1; |
||||
} |
||||
|
||||
if (pos->queue->version != pos->version) { |
||||
pos->valid = 0; |
||||
DEBUG_ERROR(DEBUG_CATEGORY_LL_QUEUE, "queue_pos_insert: position invalidated due to queue version change"); |
||||
return -1; |
||||
} |
||||
|
||||
struct ll_queue* q = pos->queue; |
||||
|
||||
// Проверить лимит размера |
||||
if (q->size_limit >= 0 && q->count >= q->size_limit) { |
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_pos_insert: size limit exceeded, freeing entry %p", entry); |
||||
queue_entry_free(entry); |
||||
return -1; |
||||
} |
||||
|
||||
entry->ref_count++; |
||||
|
||||
if (mode == 0) { |
||||
// Вставить перед current |
||||
if (!pos->current) { |
||||
// Вставить в head |
||||
entry->next = q->head; |
||||
entry->prev = NULL; |
||||
if (q->head) { |
||||
q->head->prev = entry; |
||||
} else { |
||||
q->tail = entry; // Очередь была пустой |
||||
} |
||||
q->head = entry; |
||||
} else { |
||||
// Вставить перед current |
||||
entry->next = pos->current; |
||||
entry->prev = pos->current->prev; |
||||
|
||||
if (pos->current->prev) { |
||||
pos->current->prev->next = entry; |
||||
} else { |
||||
q->head = entry; // Вставляем в начало |
||||
} |
||||
pos->current->prev = entry; |
||||
} |
||||
} else { |
||||
// Вставить после current |
||||
if (!pos->current) { |
||||
// Вставить в head (current = NULL, значит перед head) |
||||
entry->next = q->head; |
||||
entry->prev = NULL; |
||||
if (q->head) { |
||||
q->head->prev = entry; |
||||
} else { |
||||
q->tail = entry; // Очередь была пустой |
||||
} |
||||
q->head = entry; |
||||
} else { |
||||
// Вставить после current |
||||
entry->next = pos->current->next; |
||||
entry->prev = pos->current; |
||||
|
||||
if (pos->current->next) { |
||||
pos->current->next->prev = entry; |
||||
} else { |
||||
q->tail = entry; // Вставляем в конец |
||||
} |
||||
pos->current->next = entry; |
||||
} |
||||
} |
||||
|
||||
q->count++; |
||||
q->total_bytes += entry->size; |
||||
|
||||
// Обновить версию очереди и позиции (вместо полной инвалидации) |
||||
q->version++; |
||||
|
||||
// Обновить текущую позицию, если она активна |
||||
if (pos->current) { |
||||
pos->version = q->version; |
||||
pos->valid = 1; // Сделать позицию снова валидной |
||||
// Корректировка индекса в зависимости от режима вставки |
||||
if (mode == 0) { |
||||
// Вставка перед текущим - индекс остается тем же |
||||
// pos->index не меняется |
||||
} else { |
||||
// Вставка после текущего - текущий элемент смещается |
||||
pos->index++; |
||||
} |
||||
} |
||||
pos->valid = 0; // Текущая позиция теперь invalid |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_pos_insert: inserted entry %p, mode=%d, count=%d", |
||||
entry, mode, q->count); |
||||
|
||||
// Проверить ожидающие коллбэки |
||||
check_waiters(q); |
||||
|
||||
return 0; |
||||
} |
||||
|
||||
struct ll_entry* queue_pos_remove(struct ll_queue_pos* pos) { |
||||
if (!pos || !pos->valid || !pos->current) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_LL_QUEUE, "queue_pos_remove: invalid position or no current element"); |
||||
return NULL; |
||||
} |
||||
|
||||
if (pos->queue->version != pos->version) { |
||||
pos->valid = 0; |
||||
DEBUG_ERROR(DEBUG_CATEGORY_LL_QUEUE, "queue_pos_remove: position invalidated due to queue version change"); |
||||
return NULL; |
||||
} |
||||
|
||||
struct ll_queue* q = pos->queue; |
||||
struct ll_entry* entry = pos->current; |
||||
|
||||
// Удалить из двусвязного списка |
||||
if (entry->prev) { |
||||
entry->prev->next = entry->next; |
||||
} else { |
||||
q->head = entry->next; // Удаляем head |
||||
} |
||||
|
||||
if (entry->next) { |
||||
entry->next->prev = entry->prev; |
||||
} else { |
||||
q->tail = entry->prev; // Удаляем tail |
||||
} |
||||
|
||||
q->count--; |
||||
q->total_bytes -= entry->size; |
||||
|
||||
// Обновить позицию: перейти к следующему элементу |
||||
pos->current = entry->next; |
||||
pos->prev = entry->prev; |
||||
if (pos->current) { |
||||
// Остались элементы после удаленного |
||||
pos->index = pos->index; // Индекс остается тем же |
||||
} else { |
||||
// Удалили последний элемент |
||||
pos->index = q->count; // Указывает за конец |
||||
if (pos->index == 0) { |
||||
pos->current = NULL; |
||||
pos->prev = NULL; |
||||
pos->index = -1; |
||||
} |
||||
} |
||||
|
||||
entry->next = NULL; |
||||
entry->prev = NULL; |
||||
entry->ref_count--; // Уменьшить счетчик ссылок |
||||
|
||||
// Обновить версию очереди и позицию (вместо полной инвалидации) |
||||
q->version++; |
||||
if (pos->current) { |
||||
pos->version = q->version; |
||||
pos->valid = 1; // Сделать позицию снова валидной |
||||
// pos->index уже обновлен выше |
||||
} |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_pos_remove: removed entry %p, new count=%d", |
||||
entry, q->count); |
||||
|
||||
// Проверить ожидающие коллбэки |
||||
check_waiters(q); |
||||
|
||||
return entry; |
||||
} |
||||
|
||||
int queue_entry_remove(struct ll_queue* q, struct ll_entry* entry) { |
||||
if (!q || !entry) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_LL_QUEUE, "queue_entry_remove: invalid parameters"); |
||||
return -1; |
||||
} |
||||
|
||||
// Проверить, что элемент действительно в этой очереди |
||||
struct ll_entry* curr = q->head; |
||||
while (curr) { |
||||
if (curr == entry) { |
||||
break; |
||||
} |
||||
curr = curr->next; |
||||
} |
||||
|
||||
if (!curr) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_LL_QUEUE, "queue_entry_remove: entry %p not found in queue", entry); |
||||
return -1; |
||||
} |
||||
|
||||
// Удалить из двусвязного списка |
||||
if (entry->prev) { |
||||
entry->prev->next = entry->next; |
||||
} else { |
||||
q->head = entry->next; // Удаляем head |
||||
} |
||||
|
||||
if (entry->next) { |
||||
entry->next->prev = entry->prev; |
||||
} else { |
||||
q->tail = entry->prev; // Удаляем tail |
||||
} |
||||
|
||||
q->count--; |
||||
q->total_bytes -= entry->size; |
||||
|
||||
entry->next = NULL; |
||||
entry->prev = NULL; |
||||
entry->ref_count--; |
||||
|
||||
// Инвалидировать позиции |
||||
q->version++; |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_entry_remove: removed entry %p, new count=%d", |
||||
entry, q->count); |
||||
|
||||
// Проверить ожидающие коллбэки |
||||
check_waiters(q); |
||||
|
||||
return 0; |
||||
} |
||||
@ -1,847 +0,0 @@
|
||||
// ll_queue.c |
||||
#include <stdlib.h> |
||||
#include <string.h> |
||||
#include <stdio.h> |
||||
#include <assert.h> |
||||
#include "ll_queue.h" |
||||
#include "u_async.h" |
||||
#include "debug_config.h" |
||||
|
||||
// Предварительное объявление для отложенного возобновления |
||||
static void queue_resume_timeout_cb(void* arg); |
||||
|
||||
// Проверить и запустить ожидающие коллбэки |
||||
static void check_waiters(struct ll_queue* q) { |
||||
if (!q || !q->waiters) return; |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "check_waiters: checking waiters, count=%d, bytes=%zu", |
||||
q->count, q->total_bytes); |
||||
|
||||
// Сначала собрать список коллбэков для вызова, чтобы избежать проблем с модификацией списка во время итерации |
||||
struct queue_waiter* to_call = NULL; |
||||
struct queue_waiter* to_call_tail = NULL; |
||||
|
||||
struct queue_waiter** pprev = &q->waiters; |
||||
struct queue_waiter* waiter = q->waiters; |
||||
|
||||
while (waiter) { |
||||
struct queue_waiter* next = waiter->next; |
||||
|
||||
// Проверить условие: не больше max_packets и не больше max_bytes |
||||
// max_bytes = 0 означает "не проверять байты" |
||||
if (q->count <= waiter->max_packets && (waiter->max_bytes == 0 || q->total_bytes <= waiter->max_bytes)) { |
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "check_waiters: condition met, calling callback, count=%d<=%d, bytes=%zu<=%zu (max_bytes_check=%s)", |
||||
q->count, waiter->max_packets, q->total_bytes, waiter->max_bytes, |
||||
waiter->max_bytes == 0 ? "disabled" : "enabled"); |
||||
// Удалить waiter из списка |
||||
*pprev = next; |
||||
// Добавить в список для вызова |
||||
waiter->next = NULL; |
||||
if (to_call_tail) { |
||||
to_call_tail->next = waiter; |
||||
} else { |
||||
to_call = waiter; |
||||
} |
||||
to_call_tail = waiter; |
||||
} else { |
||||
// Условие не выполнено - оставить в списке |
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "check_waiters: condition NOT met, count=%d>%d or bytes=%zu>%zu (max_bytes_check=%s)", |
||||
q->count, waiter->max_packets, q->total_bytes, waiter->max_bytes, |
||||
waiter->max_bytes == 0 ? "disabled" : "enabled"); |
||||
pprev = &waiter->next; |
||||
} |
||||
waiter = next; |
||||
} |
||||
|
||||
// Теперь вызвать коллбэки |
||||
waiter = to_call; |
||||
while (waiter) { |
||||
struct queue_waiter* next = waiter->next; |
||||
waiter->callback(q, waiter->callback_arg); |
||||
if (q->pool) { |
||||
memory_pool_free(q->pool, waiter); |
||||
} else { |
||||
free(waiter); |
||||
} |
||||
waiter = next; |
||||
} |
||||
} |
||||
|
||||
// ==================== Унифицированная система версий позиций ==================== |
||||
|
||||
// Унифицированная проверка версии позиции с автоматическим пересканированием |
||||
static int queue_pos_ensure_valid(struct ll_queue_pos* pos) { |
||||
if (!pos || !pos->queue) return 0; |
||||
|
||||
// Если позиция уже валидна и версия совпадает, всё хорошо |
||||
if (pos->valid && pos->version == pos->queue->version) { |
||||
return 1; |
||||
} |
||||
|
||||
// Несовпадение версии или позиция невалидна - нужно пересканировать |
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "Position version %lu != queue version %lu, rescanning...", |
||||
(unsigned long)pos->version, (unsigned long)pos->queue->version); |
||||
|
||||
// Сохранить целевой индекс (где мы хотим быть) |
||||
int target_index = pos->index; |
||||
|
||||
// Обновить версию позиции до текущей |
||||
pos->version = pos->queue->version; |
||||
|
||||
// Обработать краевой случай: индекс больше не существует из-за изменения размера очереди |
||||
if (target_index >= pos->queue->count) { |
||||
// Позиция выходит за пределы очереди - инвалидируем её |
||||
pos->valid = 0; |
||||
return 0; |
||||
} |
||||
|
||||
// Простой подход: сбросить и перейти к тому же индексу |
||||
// Это найдёт элемент, который теперь находится в целевом индексе |
||||
queue_pos_reset(pos); |
||||
return queue_pos_seek(pos, target_index); |
||||
} |
||||
|
||||
// ==================== Управление очередью ==================== |
||||
|
||||
struct ll_queue* queue_new(struct UASYNC* ua, struct memory_pool* pool) { |
||||
if (!ua) return NULL; |
||||
|
||||
struct ll_queue* q = calloc(1, sizeof(struct ll_queue)); |
||||
if (!q) return NULL; |
||||
|
||||
q->head = NULL; |
||||
q->tail = NULL; |
||||
q->count = 0; |
||||
q->total_bytes = 0; |
||||
q->size_limit = -1; // По умолчанию без ограничения |
||||
q->callback = NULL; |
||||
q->callback_arg = NULL; |
||||
q->callback_suspended = 0; // Коллбэки разрешены изначально |
||||
q->resume_timeout_id = NULL; |
||||
q->ua = ua; |
||||
q->waiters = NULL; |
||||
q->pool=pool; |
||||
q->version = 0; |
||||
|
||||
return q; |
||||
} |
||||
|
||||
void queue_free(struct ll_queue* q) { |
||||
if (!q) return; |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_free: freeing queue %p, head=%p, tail=%p, count=%d", |
||||
q, q->head, q->tail, q->count); |
||||
|
||||
// Освободить все элементы (уменьшить счетчик ссылок) |
||||
struct ll_entry* entry = q->head; |
||||
int entry_count = 0; |
||||
while (entry) { |
||||
struct ll_entry* next = entry->next; |
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_free: releasing entry %p (entry %d), ref_count=%d", |
||||
entry, entry_count++, entry->ref_count); |
||||
entry->ref_count--; // Дополнительно уменьшить за присутствие в очереди |
||||
queue_entry_free(entry); // Это уменьшит ref_count и освободит только если ref_count == 0 |
||||
entry = next; |
||||
} |
||||
|
||||
// Освободить все ожидающие коллбэки |
||||
struct queue_waiter* waiter = q->waiters; |
||||
int waiter_count = 0; |
||||
while (waiter) { |
||||
struct queue_waiter* next = waiter->next; |
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_free: freeing waiter %p (waiter %d)", waiter, waiter_count++); |
||||
if (q->pool) { |
||||
memory_pool_free(q->pool, waiter); |
||||
} else { |
||||
free(waiter); |
||||
} |
||||
waiter = next; |
||||
} |
||||
|
||||
// Отменить отложенное возобновление если запланировано |
||||
if (q->resume_timeout_id) { |
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_free: cancelling resume timeout %p", q->resume_timeout_id); |
||||
uasync_cancel_timeout(q->ua, q->resume_timeout_id); |
||||
} |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_free: freeing queue structure %p", q); |
||||
free(q); |
||||
} |
||||
|
||||
// ==================== Конфигурация очереди ==================== |
||||
|
||||
void queue_set_callback(struct ll_queue* q, queue_callback_fn cbk_fn, void* arg) { |
||||
if (!q) return; |
||||
q->callback = cbk_fn; |
||||
q->callback_arg = arg; |
||||
} |
||||
|
||||
static void queue_resume_timeout_cb(void* arg) { |
||||
struct ll_queue* q = (struct ll_queue*)arg; |
||||
if (!q || !q->callback) return; |
||||
|
||||
// Очистить ID таймаута (таймаут сработал) |
||||
q->resume_timeout_id = NULL; |
||||
|
||||
// Разрешить коллбэки |
||||
q->callback_suspended = 0; |
||||
|
||||
// Если в очереди есть элементы, вызвать коллбэк с первым элементом |
||||
// Обработчик должен извлечь этот элемент вызовом queue_entry_get() |
||||
if (q->head) { |
||||
q->callback(q, q->head, q->callback_arg); |
||||
} |
||||
} |
||||
|
||||
void queue_resume_callback(struct ll_queue* q) { |
||||
if (!q || !q->callback) return; |
||||
|
||||
// Если уже есть отложенное возобновление, ничего не делать |
||||
if (q->resume_timeout_id) { |
||||
return; |
||||
} |
||||
|
||||
// Запланировать отложенное возобновление через uasync |
||||
q->resume_timeout_id = uasync_set_timeout(q->ua, 0, q, queue_resume_timeout_cb); |
||||
} |
||||
|
||||
void queue_set_size_limit(struct ll_queue* q, int lim) { |
||||
if (!q) return; |
||||
q->size_limit = lim; |
||||
} |
||||
|
||||
// ==================== Управление элементами ==================== |
||||
|
||||
struct ll_entry* queue_entry_new(size_t data_size) { |
||||
// Выделить память под структуру + область данных |
||||
struct ll_entry* entry = malloc(sizeof(struct ll_entry) + data_size); |
||||
if (!entry) return NULL; |
||||
|
||||
entry->next = NULL; |
||||
entry->prev = NULL; // Инициализировать prev для двусвязного списка |
||||
entry->size = data_size; |
||||
entry->ref_count = 1; // Начальный счетчик ссылок |
||||
// Область данных оставить неинициализированной для производительности |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_entry_new: created entry %p, size=%zu, ref_count=%d", |
||||
entry, data_size, entry->ref_count); |
||||
|
||||
return entry; |
||||
} |
||||
|
||||
void queue_entry_free(struct ll_entry* entry) { |
||||
if (!entry) return; |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_entry_free: entry=%p, ref_count=%d", |
||||
entry, entry->ref_count); |
||||
|
||||
if (entry->ref_count <= 0) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_LL_QUEUE, "queue_entry_free: entry %p has invalid ref_count=%d", |
||||
entry, entry->ref_count); |
||||
return; // Предотвратить double-free |
||||
} |
||||
|
||||
entry->ref_count--; |
||||
if (entry->ref_count == 0) { |
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_entry_free: actually freeing entry %p", entry); |
||||
free(entry); |
||||
} else { |
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_entry_free: decremented ref_count to %d for entry %p", |
||||
entry->ref_count, entry); |
||||
} |
||||
} |
||||
|
||||
// ==================== Операции с очередью ==================== |
||||
|
||||
int queue_entry_put(struct ll_queue* q, struct ll_entry* entry) { |
||||
if (!q || !entry) return -1; |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_entry_put: entry=%p, size=%zu, count=%d", |
||||
entry, entry->size, q->count); |
||||
|
||||
// Проверить лимит размера |
||||
if (q->size_limit >= 0 && q->count >= q->size_limit) { |
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_entry_put: size limit exceeded, freeing entry %p", entry); |
||||
queue_entry_free(entry); |
||||
return -1; |
||||
} |
||||
|
||||
// Увеличить счетчик ссылок при добавлении в очередь |
||||
entry->ref_count++; |
||||
|
||||
// Добавить в хвост (FIFO) - двусвязный список |
||||
entry->next = NULL; |
||||
entry->prev = q->tail; |
||||
|
||||
if (q->tail) { |
||||
q->tail->next = entry; |
||||
} else { |
||||
q->head = entry; // Очередь была пустой |
||||
} |
||||
q->tail = entry; |
||||
q->count++; |
||||
q->total_bytes += entry->size; |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_entry_put: added entry %p, new count=%d, total_bytes=%zu, ref_count=%d", |
||||
entry, q->count, q->total_bytes, entry->ref_count); |
||||
|
||||
// Инвалидировать позиции |
||||
q->version++; |
||||
|
||||
// Если коллбэки разрешены - вызвать коллбэк |
||||
// Это запускает автоматическую обработку очереди |
||||
if (!q->callback_suspended && q->callback) { |
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_entry_put: calling callback for entry %p", entry); |
||||
|
||||
// Приостановить коллбэки во время выполнения коллбэка, чтобы предотвратить рекурсию |
||||
q->callback_suspended = 1; |
||||
q->callback(q, q->head, q->callback_arg); |
||||
// Не восстанавливать здесь - восстановление происходит через queue_resume_callback |
||||
} |
||||
|
||||
// Проверить ожидающие коллбэки |
||||
check_waiters(q); |
||||
|
||||
return 0; |
||||
} |
||||
|
||||
int queue_entry_put_first(struct ll_queue* q, struct ll_entry* entry) { |
||||
if (!q || !entry) return -1; |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_entry_put_first: entry=%p, size=%zu, count=%d", |
||||
entry, entry->size, q->count); |
||||
|
||||
// Проверить лимит размера |
||||
if (q->size_limit >= 0 && q->count >= q->size_limit) { |
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_entry_put_first: size limit exceeded, freeing entry %p", entry); |
||||
queue_entry_free(entry); |
||||
return -1; |
||||
} |
||||
|
||||
// Увеличить счетчик ссылок при добавлении в очередь |
||||
entry->ref_count++; |
||||
|
||||
// Добавить в голову (LIFO, высокий приоритет) - двусвязный список |
||||
entry->next = q->head; |
||||
entry->prev = NULL; |
||||
|
||||
if (q->head) { |
||||
q->head->prev = entry; |
||||
} else { |
||||
q->tail = entry; // Очередь была пустой |
||||
} |
||||
q->head = entry; |
||||
q->count++; |
||||
q->total_bytes += entry->size; |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_entry_put_first: added entry %p, new count=%d, total_bytes=%zu, ref_count=%d", |
||||
entry, q->count, q->total_bytes, entry->ref_count); |
||||
|
||||
// Инвалидировать позиции |
||||
q->version++; |
||||
|
||||
// Если коллбэки разрешены - вызвать коллбэк |
||||
if (!q->callback_suspended && q->callback) { |
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_entry_put_first: calling callback for entry %p", entry); |
||||
q->callback_suspended = 1; |
||||
q->callback(q, q->head, q->callback_arg); |
||||
} |
||||
|
||||
// Проверить ожидающие коллбэки |
||||
check_waiters(q); |
||||
|
||||
return 0; |
||||
} |
||||
|
||||
struct ll_entry* queue_entry_get(struct ll_queue* q) { |
||||
if (!q || !q->head) return NULL; |
||||
|
||||
struct ll_entry* entry = q->head; |
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_entry_get: retrieving entry %p, size=%zu, ref_count=%d", |
||||
entry, entry->size, entry->ref_count); |
||||
|
||||
// Удалить из двусвязного списка |
||||
q->head = entry->next; |
||||
if (q->head) { |
||||
q->head->prev = NULL; |
||||
} else { |
||||
q->tail = NULL; // Очередь стала пустой |
||||
} |
||||
q->count--; |
||||
q->total_bytes -= entry->size; |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_entry_get: removed entry %p, new count=%d, total_bytes=%zu", |
||||
entry, q->count, q->total_bytes); |
||||
|
||||
entry->next = NULL; |
||||
entry->prev = NULL; // Полностью отсоединить от очереди |
||||
|
||||
// Уменьшить счетчик ссылок при извлечении из очереди |
||||
// entry->ref_count был увеличен при добавлении в очередь |
||||
// теперь уменьшаем, но не освобождаем, так как вызывающий код должен это сделать |
||||
entry->ref_count--; |
||||
|
||||
// Инвалидировать позиции |
||||
q->version++; |
||||
|
||||
// При извлечении элемента приостанавливаем коллбэки |
||||
// Это предотвращает рекурсию если во время обработки добавляются новые элементы |
||||
q->callback_suspended = 1; |
||||
|
||||
// Проверить ожидающие коллбэки при извлечении элемента |
||||
// Это важно для waiters, которые ожидают уменьшения очереди |
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_entry_get: about to call check_waiters, count=%d", q->count); |
||||
check_waiters(q); |
||||
|
||||
return entry; |
||||
} |
||||
|
||||
int queue_entry_count(struct ll_queue* q) { |
||||
if (!q) return 0; |
||||
return q->count; |
||||
} |
||||
|
||||
// ==================== Асинхронное ожидание ==================== |
||||
|
||||
struct queue_waiter* queue_wait_threshold(struct ll_queue* q, int max_packets, size_t max_bytes, |
||||
queue_threshold_callback_fn callback, void* arg) { |
||||
if (!q || !callback) return NULL; |
||||
|
||||
// Создать новый waiter |
||||
struct queue_waiter* waiter; |
||||
if (q->pool) { |
||||
waiter = (struct queue_waiter*)memory_pool_alloc(q->pool); |
||||
} else { |
||||
waiter = malloc(sizeof(struct queue_waiter)); |
||||
} |
||||
if (!waiter) return NULL; |
||||
|
||||
waiter->max_packets = max_packets; |
||||
waiter->max_bytes = max_bytes; |
||||
waiter->callback = callback; |
||||
waiter->callback_arg = arg; |
||||
waiter->next = NULL; |
||||
|
||||
// Проверить условие немедленно |
||||
if (q->count <= max_packets && (max_bytes == 0 || q->total_bytes <= max_bytes)) { |
||||
// Условие уже выполнено - вызвать коллбэк и освободить waiter |
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_wait_threshold: condition already met, count=%d<=%d, bytes=%zu<=%zu, calling callback", |
||||
q->count, max_packets, q->total_bytes, max_bytes); |
||||
callback(q, arg); |
||||
if (q->pool) { |
||||
memory_pool_free(q->pool, waiter); |
||||
} else { |
||||
free(waiter); |
||||
} |
||||
return NULL; |
||||
} |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_wait_threshold: registering waiter, count=%d, bytes=%zu, max_packets=%d, max_bytes=%zu", |
||||
q->count, q->total_bytes, max_packets, max_bytes); |
||||
|
||||
// Добавить в список ожидающих |
||||
waiter->next = q->waiters; |
||||
q->waiters = waiter; |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_wait_threshold: waiter registered successfully"); |
||||
return waiter; |
||||
} |
||||
|
||||
void queue_cancel_wait(struct ll_queue* q, struct queue_waiter* waiter) { |
||||
if (!q || !waiter) return; |
||||
|
||||
// Найти и удалить waiter из списка |
||||
struct queue_waiter** pprev = &q->waiters; |
||||
struct queue_waiter* w = q->waiters; |
||||
|
||||
while (w) { |
||||
if (w == waiter) { |
||||
*pprev = w->next; |
||||
if (q->pool) { |
||||
memory_pool_free(q->pool, w); |
||||
} else { |
||||
free(w); |
||||
} |
||||
return; |
||||
} |
||||
pprev = &w->next; |
||||
w = w->next; |
||||
} |
||||
} |
||||
|
||||
// ==================== Статистика и метрики ==================== |
||||
|
||||
void queue_get_pool_stats(struct ll_queue* q, size_t* waiter_allocations, size_t* waiter_reuse) { |
||||
if (!q || !q->pool) { |
||||
if (waiter_allocations) *waiter_allocations = 0; |
||||
if (waiter_reuse) *waiter_reuse = 0; |
||||
return; |
||||
} |
||||
|
||||
memory_pool_get_stats(q->pool, waiter_allocations, waiter_reuse); |
||||
} |
||||
|
||||
// ==================== Управление позициями ==================== |
||||
|
||||
struct ll_queue_pos* queue_pos_new(struct ll_queue* queue) { |
||||
if (!queue) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_LL_QUEUE, "queue_pos_new: queue is NULL"); |
||||
return NULL; |
||||
} |
||||
|
||||
struct ll_queue_pos* pos = malloc(sizeof(struct ll_queue_pos)); |
||||
if (!pos) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_LL_QUEUE, "queue_pos_new: failed to allocate position"); |
||||
return NULL; |
||||
} |
||||
|
||||
pos->queue = queue; |
||||
pos->current = NULL; |
||||
pos->prev = NULL; |
||||
pos->index = -1; // Перед head |
||||
pos->valid = 1; |
||||
pos->version = queue->version; |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_pos_new: created position %p for queue %p", pos, queue); |
||||
return pos; |
||||
} |
||||
|
||||
void queue_pos_free(struct ll_queue_pos* pos) { |
||||
if (!pos) return; |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_pos_free: freeing position %p", pos); |
||||
free(pos); |
||||
} |
||||
|
||||
void queue_pos_reset(struct ll_queue_pos* pos) { |
||||
if (!pos) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_LL_QUEUE, "queue_pos_reset: pos is NULL"); |
||||
return; |
||||
} |
||||
|
||||
// Обновить версию позиции при необходимости (вместо инвалидации) |
||||
if (pos->queue->version != pos->version) { |
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_pos_reset: updating position version from %lu to %lu", |
||||
(unsigned long)pos->version, (unsigned long)pos->queue->version); |
||||
pos->version = pos->queue->version; |
||||
} |
||||
|
||||
pos->current = NULL; |
||||
pos->prev = NULL; |
||||
pos->index = -1; |
||||
pos->valid = 1; |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_pos_reset: position reset"); |
||||
} |
||||
|
||||
int queue_pos_next(struct ll_queue_pos* pos) { |
||||
if (!pos) return 0; |
||||
|
||||
// Убедиться, что позиция валидна (автоматическое обновление при необходимости) |
||||
if (!queue_pos_ensure_valid(pos)) { |
||||
return 0; |
||||
} |
||||
|
||||
if (!pos->current) { |
||||
// Перед head - переходим к head |
||||
if (!pos->queue->head) { |
||||
// Очередь пуста - некуда переходить |
||||
return 0; |
||||
} |
||||
pos->current = pos->queue->head; |
||||
pos->prev = NULL; |
||||
pos->index = 0; |
||||
} else if (pos->current->next) { |
||||
// Есть следующий элемент |
||||
pos->prev = pos->current; |
||||
pos->current = pos->current->next; |
||||
pos->index++; |
||||
} else { |
||||
// Достигнут конец очереди |
||||
return 0; |
||||
} |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_pos_next: moved to index %d, entry %p", |
||||
pos->index, pos->current); |
||||
return 1; |
||||
} |
||||
|
||||
int queue_pos_prev(struct ll_queue_pos* pos) { |
||||
if (!pos) return 0; |
||||
|
||||
// Убедиться, что позиция валидна (автоматическое обновление при необходимости) |
||||
if (!queue_pos_ensure_valid(pos)) { |
||||
return 0; |
||||
} |
||||
|
||||
if (!pos->current) { |
||||
// Уже перед head - дальше некуда |
||||
return 0; |
||||
} else if (pos->current->prev) { |
||||
// Есть предыдущий элемент |
||||
pos->current = pos->current->prev; |
||||
pos->prev = pos->current->prev; |
||||
pos->index--; |
||||
} else { |
||||
// Достигнуто начало очереди |
||||
pos->current = NULL; |
||||
pos->prev = NULL; |
||||
pos->index = -1; |
||||
} |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_pos_prev: moved to index %d, entry %p", |
||||
pos->index, pos->current); |
||||
return 1; |
||||
} |
||||
|
||||
int queue_pos_seek(struct ll_queue_pos* pos, int target_index) { |
||||
if (!pos) return 0; |
||||
|
||||
// Убедиться, что позиция валидна (автоматическое обновление при необходимости) |
||||
if (!queue_pos_ensure_valid(pos)) { |
||||
return 0; |
||||
} |
||||
|
||||
if (target_index < -1 || target_index >= pos->queue->count) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_LL_QUEUE, "queue_pos_seek: invalid target index %d (count=%d)", |
||||
target_index, pos->queue->count); |
||||
return 0; |
||||
} |
||||
|
||||
// Простая реализация: начинаем с начала и идем вперед |
||||
// Можно оптимизировать позже, если потребуется |
||||
queue_pos_reset(pos); |
||||
|
||||
for (int i = 0; i <= target_index; i++) { |
||||
if (!queue_pos_next(pos)) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_LL_QUEUE, "queue_pos_seek: failed to reach index %d", target_index); |
||||
return 0; |
||||
} |
||||
} |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_pos_seek: seeked to index %d, entry %p", |
||||
pos->index, pos->current); |
||||
return 1; |
||||
} |
||||
|
||||
int queue_pos_insert(struct ll_queue_pos* pos, struct ll_entry* entry, int mode) { |
||||
if (!pos || !pos->valid || !entry) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_LL_QUEUE, "queue_pos_insert: invalid parameters"); |
||||
return -1; |
||||
} |
||||
|
||||
if (pos->queue->version != pos->version) { |
||||
pos->valid = 0; |
||||
DEBUG_ERROR(DEBUG_CATEGORY_LL_QUEUE, "queue_pos_insert: position invalidated due to queue version change"); |
||||
return -1; |
||||
} |
||||
|
||||
struct ll_queue* q = pos->queue; |
||||
|
||||
// Проверить лимит размера |
||||
if (q->size_limit >= 0 && q->count >= q->size_limit) { |
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_pos_insert: size limit exceeded, freeing entry %p", entry); |
||||
queue_entry_free(entry); |
||||
return -1; |
||||
} |
||||
|
||||
entry->ref_count++; |
||||
|
||||
if (mode == 0) { |
||||
// Вставить перед current |
||||
if (!pos->current) { |
||||
// Вставить в head |
||||
entry->next = q->head; |
||||
entry->prev = NULL; |
||||
if (q->head) { |
||||
q->head->prev = entry; |
||||
} else { |
||||
q->tail = entry; // Очередь была пустой |
||||
} |
||||
q->head = entry; |
||||
} else { |
||||
// Вставить перед current |
||||
entry->next = pos->current; |
||||
entry->prev = pos->current->prev; |
||||
|
||||
if (pos->current->prev) { |
||||
pos->current->prev->next = entry; |
||||
} else { |
||||
q->head = entry; // Вставляем в начало |
||||
} |
||||
pos->current->prev = entry; |
||||
} |
||||
} else { |
||||
// Вставить после current |
||||
if (!pos->current) { |
||||
// Вставить в head (current = NULL, значит перед head) |
||||
entry->next = q->head; |
||||
entry->prev = NULL; |
||||
if (q->head) { |
||||
q->head->prev = entry; |
||||
} else { |
||||
q->tail = entry; // Очередь была пустой |
||||
} |
||||
q->head = entry; |
||||
} else { |
||||
// Вставить после current |
||||
entry->next = pos->current->next; |
||||
entry->prev = pos->current; |
||||
|
||||
if (pos->current->next) { |
||||
pos->current->next->prev = entry; |
||||
} else { |
||||
q->tail = entry; // Вставляем в конец |
||||
} |
||||
pos->current->next = entry; |
||||
} |
||||
} |
||||
|
||||
q->count++; |
||||
q->total_bytes += entry->size; |
||||
|
||||
// Обновить версию очереди и позиции (вместо полной инвалидации) |
||||
q->version++; |
||||
|
||||
// Обновить текущую позицию, если она активна |
||||
if (pos->current) { |
||||
pos->version = q->version; |
||||
pos->valid = 1; // Сделать позицию снова валидной |
||||
// Корректировка индекса в зависимости от режима вставки |
||||
if (mode == 0) { |
||||
// Вставка перед текущим - индекс остается тем же |
||||
// pos->index не меняется |
||||
} else { |
||||
// Вставка после текущего - текущий элемент смещается |
||||
pos->index++; |
||||
} |
||||
} |
||||
// Позиция остается валидной - убрали строку pos->valid = 0; |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_pos_insert: inserted entry %p, mode=%d, count=%d", |
||||
entry, mode, q->count); |
||||
|
||||
// Проверить ожидающие коллбэки |
||||
check_waiters(q); |
||||
|
||||
return 0; |
||||
} |
||||
|
||||
struct ll_entry* queue_pos_remove(struct ll_queue_pos* pos) { |
||||
if (!pos || !pos->valid || !pos->current) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_LL_QUEUE, "queue_pos_remove: invalid position or no current element"); |
||||
return NULL; |
||||
} |
||||
|
||||
// Убедиться, что позиция валидна (автоматическое обновление при необходимости) |
||||
if (!queue_pos_ensure_valid(pos)) { |
||||
return NULL; |
||||
} |
||||
|
||||
struct ll_queue* q = pos->queue; |
||||
struct ll_entry* entry = pos->current; |
||||
|
||||
// Удалить из двусвязного списка |
||||
if (entry->prev) { |
||||
entry->prev->next = entry->next; |
||||
} else { |
||||
q->head = entry->next; // Удаляем head |
||||
} |
||||
|
||||
if (entry->next) { |
||||
entry->next->prev = entry->prev; |
||||
} else { |
||||
q->tail = entry->prev; // Удаляем tail |
||||
} |
||||
|
||||
q->count--; |
||||
q->total_bytes -= entry->size; |
||||
|
||||
// Обновить позицию: перейти к следующему элементу |
||||
pos->current = entry->next; |
||||
pos->prev = entry->prev; |
||||
if (pos->current) { |
||||
// Остались элементы после удаленного |
||||
pos->index = pos->index; // Индекс остается тем же |
||||
} else { |
||||
// Удалили последний элемент |
||||
pos->index = q->count; // Указывает за конец |
||||
if (pos->index == 0) { |
||||
pos->current = NULL; |
||||
pos->prev = NULL; |
||||
pos->index = -1; |
||||
} |
||||
} |
||||
|
||||
entry->next = NULL; |
||||
entry->prev = NULL; |
||||
entry->ref_count--; // Уменьшить счетчик ссылок |
||||
|
||||
// Обновить версию очереди и позицию (вместо полной инвалидации) |
||||
q->version++; |
||||
if (pos->current) { |
||||
pos->version = q->version; |
||||
pos->valid = 1; // Сделать позицию снова валидной |
||||
// pos->index уже обновлен выше |
||||
} |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_pos_remove: removed entry %p, new count=%d", |
||||
entry, q->count); |
||||
|
||||
// Проверить ожидающие коллбэки |
||||
check_waiters(q); |
||||
|
||||
return entry; |
||||
} |
||||
|
||||
int queue_entry_remove(struct ll_queue* q, struct ll_entry* entry) { |
||||
if (!q || !entry) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_LL_QUEUE, "queue_entry_remove: invalid parameters"); |
||||
return -1; |
||||
} |
||||
|
||||
// Проверить, что элемент действительно в этой очереди |
||||
struct ll_entry* curr = q->head; |
||||
while (curr) { |
||||
if (curr == entry) { |
||||
break; |
||||
} |
||||
curr = curr->next; |
||||
} |
||||
|
||||
if (!curr) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_LL_QUEUE, "queue_entry_remove: entry %p not found in queue", entry); |
||||
return -1; |
||||
} |
||||
|
||||
// Удалить из двусвязного списка |
||||
if (entry->prev) { |
||||
entry->prev->next = entry->next; |
||||
} else { |
||||
q->head = entry->next; // Удаляем head |
||||
} |
||||
|
||||
if (entry->next) { |
||||
entry->next->prev = entry->prev; |
||||
} else { |
||||
q->tail = entry->prev; // Удаляем tail |
||||
} |
||||
|
||||
q->count--; |
||||
q->total_bytes -= entry->size; |
||||
|
||||
entry->next = NULL; |
||||
entry->prev = NULL; |
||||
entry->ref_count--; |
||||
|
||||
// Инвалидировать позиции |
||||
q->version++; |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_entry_remove: removed entry %p, new count=%d", |
||||
entry, q->count); |
||||
|
||||
// Проверить ожидающие коллбэки |
||||
check_waiters(q); |
||||
|
||||
return 0; |
||||
} |
||||
@ -1,744 +0,0 @@
|
||||
#include <stdlib.h> |
||||
#include <string.h> |
||||
#include <stdio.h> |
||||
#include <assert.h> |
||||
#include "ll_queue.h" |
||||
#include "u_async.h" |
||||
#include "debug_config.h" |
||||
|
||||
// Предварительное объявление для отложенного возобновления |
||||
static void queue_resume_timeout_cb(void* arg); |
||||
|
||||
// Вспомогательные функции для двусвязного списка |
||||
static void link_entry_after(struct ll_entry* existing, struct ll_entry* new_entry) { |
||||
if (!existing || !new_entry) return; |
||||
|
||||
new_entry->next = existing->next; |
||||
new_entry->prev = existing; |
||||
|
||||
if (existing->next) { |
||||
existing->next->prev = new_entry; |
||||
} |
||||
existing->next = new_entry; |
||||
} |
||||
|
||||
static void unlink_entry(struct ll_entry* entry) { |
||||
if (!entry) return; |
||||
|
||||
if (entry->prev) entry->prev->next = entry->next; |
||||
if (entry->next) entry->next->prev = entry->prev; |
||||
entry->next = NULL; |
||||
entry->prev = NULL; |
||||
} |
||||
|
||||
// Проверить и запустить ожидающие коллбэки |
||||
static void check_waiters(struct ll_queue* q) { |
||||
if (!q || !q->waiters) return; |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "check_waiters: checking waiters, count=%d, bytes=%zu", |
||||
q->count, q->total_bytes); |
||||
|
||||
struct queue_waiter** pprev = &q->waiters; |
||||
struct queue_waiter* waiter = q->waiters; |
||||
|
||||
while (waiter) { |
||||
struct queue_waiter* next = waiter->next; |
||||
|
||||
// Проверить условие: не больше max_packets и не больше max_bytes |
||||
// max_bytes = 0 означает "не проверять байты" |
||||
if (q->count <= waiter->max_packets && (waiter->max_bytes == 0 || q->total_bytes <= waiter->max_bytes)) { |
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "check_waiters: condition met, calling callback, count=%d<=%d, bytes=%zu<=%zu (max_bytes_check=%s)", |
||||
q->count, waiter->max_packets, q->total_bytes, waiter->max_bytes, |
||||
waiter->max_bytes == 0 ? "disabled" : "enabled"); |
||||
waiter->callback(q, waiter->callback_arg); |
||||
// Удалить waiter из списка |
||||
*pprev = next; |
||||
if (q->pool) { |
||||
memory_pool_free(q->pool, waiter); |
||||
} else { |
||||
free(waiter); |
||||
} |
||||
// pprev уже указывает на правильный следующий элемент |
||||
} else { |
||||
// Условие не выполнено - оставить в списке |
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "check_waiters: condition NOT met, count=%d>%d or bytes=%zu>%zu (max_bytes_check=%s)", |
||||
q->count, waiter->max_packets, q->total_bytes, waiter->max_bytes, |
||||
waiter->max_bytes == 0 ? "disabled" : "enabled"); |
||||
pprev = &waiter->next; |
||||
} |
||||
waiter = next; |
||||
} |
||||
} |
||||
|
||||
// ==================== Управление очередью ==================== |
||||
|
||||
struct ll_queue* queue_new(struct UASYNC* ua, struct memory_pool* pool) { |
||||
struct ll_queue* q = calloc(1, sizeof(struct ll_queue)); |
||||
if (!q) return NULL; |
||||
|
||||
q->head = NULL; |
||||
q->tail = NULL; |
||||
q->count = 0; |
||||
q->total_bytes = 0; |
||||
q->size_limit = -1; // По умолчанию без ограничения |
||||
q->callback = NULL; |
||||
q->callback_arg = NULL; |
||||
q->callback_suspended = 0; // Коллбэки разрешены изначально |
||||
q->resume_timeout_id = NULL; |
||||
q->ua = ua; |
||||
q->waiters = NULL; |
||||
q->pool=pool; |
||||
|
||||
return q; |
||||
} |
||||
|
||||
void queue_free(struct ll_queue* q) { |
||||
if (!q) return; |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_free: freeing queue %p, head=%p, tail=%p, count=%d", |
||||
q, q->head, q->tail, q->count); |
||||
|
||||
// Освободить все элементы (уменьшить счетчик ссылок) |
||||
struct ll_entry* entry = q->head; |
||||
int entry_count = 0; |
||||
while (entry) { |
||||
struct ll_entry* next = entry->next; |
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_free: releasing entry %p (entry %d), ref_count=%d", |
||||
entry, entry_count++, entry->ref_count); |
||||
queue_entry_free(entry); // Это уменьшит ref_count и освободит только если ref_count == 0 |
||||
entry = next; |
||||
} |
||||
|
||||
// Освободить все ожидающие коллбэки |
||||
struct queue_waiter* waiter = q->waiters; |
||||
int waiter_count = 0; |
||||
while (waiter) { |
||||
struct queue_waiter* next = waiter->next; |
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_free: freeing waiter %p (waiter %d)", waiter, waiter_count++); |
||||
if (q->pool) { |
||||
memory_pool_free(q->pool, waiter); |
||||
} else { |
||||
free(waiter); |
||||
} |
||||
waiter = next; |
||||
} |
||||
|
||||
// Отменить отложенное возобновление если запланировано |
||||
if (q->resume_timeout_id) { |
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_free: cancelling resume timeout %p", q->resume_timeout_id); |
||||
uasync_cancel_timeout(q->ua, q->resume_timeout_id); |
||||
} |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_free: freeing queue structure %p", q); |
||||
free(q); |
||||
} |
||||
|
||||
// ==================== Конфигурация очереди ==================== |
||||
|
||||
void queue_set_callback(struct ll_queue* q, queue_callback_fn cbk_fn, void* arg) { |
||||
if (!q) return; |
||||
q->callback = cbk_fn; |
||||
q->callback_arg = arg; |
||||
} |
||||
|
||||
static void queue_resume_timeout_cb(void* arg) { |
||||
struct ll_queue* q = (struct ll_queue*)arg; |
||||
if (!q || !q->callback) return; |
||||
|
||||
// Очистить ID таймаута (таймаут сработал) |
||||
q->resume_timeout_id = NULL; |
||||
|
||||
// Разрешить коллбэки |
||||
q->callback_suspended = 0; |
||||
|
||||
// Если в очереди есть элементы, вызвать коллбэк с первым элементом |
||||
// Обработчик должен извлечь этот элемент вызовом queue_entry_get() |
||||
if (q->head) { |
||||
q->callback(q, q->head, q->callback_arg); |
||||
} |
||||
} |
||||
|
||||
void queue_resume_callback(struct ll_queue* q) { |
||||
if (!q || !q->callback) return; |
||||
|
||||
// Если уже есть отложенное возобновление, ничего не делать |
||||
if (q->resume_timeout_id) { |
||||
return; |
||||
} |
||||
|
||||
// Запланировать отложенное возобновление через uasync |
||||
q->resume_timeout_id = uasync_set_timeout(q->ua, 0, q, queue_resume_timeout_cb); |
||||
} |
||||
|
||||
void queue_set_size_limit(struct ll_queue* q, int lim) { |
||||
if (!q) return; |
||||
q->size_limit = lim; |
||||
} |
||||
|
||||
// ==================== Управление элементами ==================== |
||||
|
||||
struct ll_entry* queue_entry_new(size_t data_size) { |
||||
// Выделить память под структуру + область данных |
||||
struct ll_entry* entry = malloc(sizeof(struct ll_entry) + data_size); |
||||
if (!entry) return NULL; |
||||
|
||||
entry->next = NULL; |
||||
entry->prev = NULL; // Инициализировать prev для двусвязного списка |
||||
entry->size = data_size; |
||||
entry->ref_count = 1; // Начальный счетчик ссылок |
||||
// Область данных оставить неинициализированной для производительности |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_entry_new: created entry %p, size=%zu, ref_count=%d", |
||||
entry, data_size, entry->ref_count); |
||||
|
||||
return entry; |
||||
} |
||||
|
||||
void queue_entry_free(struct ll_entry* entry) { |
||||
if (!entry) return; |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_entry_free: entry=%p, ref_count=%d", |
||||
entry, entry->ref_count); |
||||
|
||||
if (entry->ref_count <= 0) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_LL_QUEUE, "queue_entry_free: entry %p has invalid ref_count=%d", |
||||
entry, entry->ref_count); |
||||
return; // Предотвратить double-free |
||||
} |
||||
|
||||
entry->ref_count--; |
||||
if (entry->ref_count == 0) { |
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_entry_free: actually freeing entry %p", entry); |
||||
free(entry); |
||||
} else { |
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_entry_free: decremented ref_count to %d for entry %p", |
||||
entry->ref_count, entry); |
||||
} |
||||
} |
||||
|
||||
// ==================== Операции с очередью ==================== |
||||
|
||||
int queue_entry_put(struct ll_queue* q, struct ll_entry* entry) { |
||||
if (!q || !entry) return -1; |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_entry_put: entry=%p, size=%zu, count=%d", |
||||
entry, entry->size, q->count); |
||||
|
||||
// Проверить лимит размера |
||||
if (q->size_limit >= 0 && q->count >= q->size_limit) { |
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_entry_put: size limit exceeded, freeing entry %p", entry); |
||||
queue_entry_free(entry); |
||||
return -1; |
||||
} |
||||
|
||||
// Увеличить счетчик ссылок при добавлении в очередь |
||||
entry->ref_count++; |
||||
|
||||
// Добавить в хвост (FIFO) - двусвязный список |
||||
entry->next = NULL; |
||||
entry->prev = q->tail; |
||||
|
||||
if (q->tail) { |
||||
q->tail->next = entry; |
||||
} else { |
||||
q->head = entry; // Очередь была пустой |
||||
} |
||||
q->tail = entry; |
||||
q->count++; |
||||
q->total_bytes += entry->size; |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_entry_put: added entry %p, new count=%d, total_bytes=%zu, ref_count=%d", |
||||
entry, q->count, q->total_bytes, entry->ref_count); |
||||
|
||||
// Если коллбэки разрешены - вызвать коллбэк |
||||
// Это запускает автоматическую обработку очереди |
||||
if (!q->callback_suspended && q->callback) { |
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_entry_put: calling callback for entry %p", entry); |
||||
|
||||
// Приостановить коллбэки во время выполнения коллбэка, чтобы предотвратить рекурсию |
||||
q->callback_suspended = 1; |
||||
q->callback(q, entry, q->callback_arg); |
||||
// Не восстанавливать здесь - восстановление происходит через queue_resume_callback |
||||
} |
||||
|
||||
// Проверить ожидающие коллбэки |
||||
check_waiters(q); |
||||
|
||||
return 0; |
||||
} |
||||
|
||||
int queue_entry_put_first(struct ll_queue* q, struct ll_entry* entry) { |
||||
if (!q || !entry) return -1; |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_entry_put_first: entry=%p, size=%zu, count=%d", |
||||
entry, entry->size, q->count); |
||||
|
||||
// Проверить лимит размера |
||||
if (q->size_limit >= 0 && q->count >= q->size_limit) { |
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_entry_put_first: size limit exceeded, freeing entry %p", entry); |
||||
queue_entry_free(entry); |
||||
return -1; |
||||
} |
||||
|
||||
// Увеличить счетчик ссылок при добавлении в очередь |
||||
entry->ref_count++; |
||||
|
||||
// Добавить в голову (LIFO, высокий приоритет) - двусвязный список |
||||
entry->next = q->head; |
||||
entry->prev = NULL; |
||||
|
||||
if (q->head) { |
||||
q->head->prev = entry; |
||||
} else { |
||||
q->tail = entry; // Очередь была пустой |
||||
} |
||||
q->head = entry; |
||||
q->count++; |
||||
q->total_bytes += entry->size; |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_entry_put_first: added entry %p, new count=%d, total_bytes=%zu, ref_count=%d", |
||||
entry, q->count, q->total_bytes, entry->ref_count); |
||||
|
||||
// Если коллбэки разрешены - вызвать коллбэк |
||||
if (!q->callback_suspended && q->callback) { |
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_entry_put_first: calling callback for entry %p", entry); |
||||
q->callback(q, entry, q->callback_arg); |
||||
} |
||||
|
||||
// Проверить ожидающие коллбэки |
||||
check_waiters(q); |
||||
|
||||
return 0; |
||||
} |
||||
|
||||
struct ll_entry* queue_entry_get(struct ll_queue* q) { |
||||
if (!q || !q->head) return NULL; |
||||
|
||||
struct ll_entry* entry = q->head; |
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_entry_get: retrieving entry %p, size=%zu, ref_count=%d", |
||||
entry, entry->size, entry->ref_count); |
||||
|
||||
// Удалить из двусвязного списка |
||||
q->head = entry->next; |
||||
if (q->head) { |
||||
q->head->prev = NULL; |
||||
} else { |
||||
q->tail = NULL; // Очередь стала пустой |
||||
} |
||||
q->count--; |
||||
q->total_bytes -= entry->size; |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_entry_get: removed entry %p, new count=%d, total_bytes=%zu", |
||||
entry, q->count, q->total_bytes); |
||||
|
||||
entry->next = NULL; |
||||
entry->prev = NULL; // Полностью отсоединить от очереди |
||||
|
||||
// Уменьшить счетчик ссылок при извлечении из очереди |
||||
// entry->ref_count был увеличен при добавлении в очередь |
||||
// теперь уменьшаем, но не освобождаем, так как вызывающий код должен это сделать |
||||
entry->ref_count--; |
||||
|
||||
// При извлечении элемента приостанавливаем коллбэки |
||||
// Это предотвращает рекурсию если во время обработки добавляются новые элементы |
||||
q->callback_suspended = 1; |
||||
|
||||
// Проверить ожидающие коллбэки при извлечении элемента |
||||
// Это важно для waiters, которые ожидают уменьшения очереди |
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_entry_get: about to call check_waiters, count=%d", q->count); |
||||
check_waiters(q); |
||||
|
||||
return entry; |
||||
} |
||||
|
||||
int queue_entry_count(struct ll_queue* q) { |
||||
if (!q) return 0; |
||||
return q->count; |
||||
} |
||||
|
||||
// ==================== Асинхронное ожидание ==================== |
||||
|
||||
struct queue_waiter* queue_wait_threshold(struct ll_queue* q, int max_packets, size_t max_bytes, |
||||
queue_threshold_callback_fn callback, void* arg) { |
||||
if (!q || !callback) return NULL; |
||||
|
||||
// Создать новый waiter |
||||
struct queue_waiter* waiter; |
||||
if (q->pool) { |
||||
waiter = (struct queue_waiter*)memory_pool_alloc(q->pool); |
||||
} else { |
||||
waiter = malloc(sizeof(struct queue_waiter)); |
||||
} |
||||
if (!waiter) return NULL; |
||||
|
||||
waiter->max_packets = max_packets; |
||||
waiter->max_bytes = max_bytes; |
||||
waiter->callback = callback; |
||||
waiter->callback_arg = arg; |
||||
waiter->next = NULL; |
||||
|
||||
// Проверить условие немедленно |
||||
if (q->count <= max_packets && (max_bytes == 0 || q->total_bytes <= max_bytes)) { |
||||
// Условие уже выполнено - вызвать коллбэк и освободить waiter |
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_wait_threshold: condition already met, count=%d<=%d, bytes=%zu<=%zu, calling callback", |
||||
q->count, max_packets, q->total_bytes, max_bytes); |
||||
callback(q, arg); |
||||
free(waiter); |
||||
return NULL; |
||||
} |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_wait_threshold: registering waiter, count=%d, bytes=%zu, max_packets=%d, max_bytes=%zu", |
||||
q->count, q->total_bytes, max_packets, max_bytes); |
||||
|
||||
// Добавить в список ожидающих |
||||
waiter->next = q->waiters; |
||||
q->waiters = waiter; |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_wait_threshold: waiter registered successfully"); |
||||
return waiter; |
||||
} |
||||
|
||||
void queue_cancel_wait(struct ll_queue* q, struct queue_waiter* waiter) { |
||||
if (!q || !waiter) return; |
||||
|
||||
// Найти и удалить waiter из списка |
||||
struct queue_waiter** pprev = &q->waiters; |
||||
struct queue_waiter* w = q->waiters; |
||||
|
||||
while (w) { |
||||
if (w == waiter) { |
||||
*pprev = w->next; |
||||
if (q->pool) { |
||||
memory_pool_free(q->pool, w); |
||||
} else { |
||||
free(w); |
||||
} |
||||
return; |
||||
} |
||||
pprev = &w->next; |
||||
w = w->next; |
||||
} |
||||
} |
||||
|
||||
// ==================== Статистика и метрики ==================== |
||||
|
||||
void queue_get_pool_stats(struct ll_queue* q, size_t* waiter_allocations, size_t* waiter_reuse) { |
||||
if (!q || !q->pool) { |
||||
if (waiter_allocations) *waiter_allocations = 0; |
||||
if (waiter_reuse) *waiter_reuse = 0; |
||||
return; |
||||
} |
||||
|
||||
memory_pool_get_stats(q->pool, waiter_allocations, waiter_reuse); |
||||
} |
||||
|
||||
// ==================== Управление позициями ==================== |
||||
|
||||
struct ll_queue_pos* queue_pos_new(struct ll_queue* queue) { |
||||
if (!queue) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_LL_QUEUE, "queue_pos_new: queue is NULL"); |
||||
return NULL; |
||||
} |
||||
|
||||
struct ll_queue_pos* pos = malloc(sizeof(struct ll_queue_pos)); |
||||
if (!pos) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_LL_QUEUE, "queue_pos_new: failed to allocate position"); |
||||
return NULL; |
||||
} |
||||
|
||||
pos->queue = queue; |
||||
pos->current = NULL; |
||||
pos->prev = NULL; |
||||
pos->index = -1; // Перед head |
||||
pos->valid = 1; |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_pos_new: created position %p for queue %p", pos, queue); |
||||
return pos; |
||||
} |
||||
|
||||
void queue_pos_free(struct ll_queue_pos* pos) { |
||||
if (!pos) return; |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_pos_free: freeing position %p", pos); |
||||
free(pos); |
||||
} |
||||
|
||||
void queue_pos_reset(struct ll_queue_pos* pos) { |
||||
if (!pos) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_LL_QUEUE, "queue_pos_reset: pos is NULL"); |
||||
return; |
||||
} |
||||
|
||||
pos->current = NULL; |
||||
pos->prev = NULL; |
||||
pos->index = -1; |
||||
pos->valid = 1; |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_pos_reset: position reset"); |
||||
} |
||||
|
||||
int queue_pos_next(struct ll_queue_pos* pos) { |
||||
if (!pos || !pos->valid) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_LL_QUEUE, "queue_pos_next: invalid position"); |
||||
return 0; |
||||
} |
||||
|
||||
if (!pos->current) { |
||||
// Перед head - переходим к head |
||||
if (!pos->queue->head) { |
||||
// Очередь пуста - некуда переходить |
||||
return 0; |
||||
} |
||||
pos->current = pos->queue->head; |
||||
pos->prev = NULL; |
||||
pos->index = 0; |
||||
} else if (pos->current->next) { |
||||
// Есть следующий элемент |
||||
pos->prev = pos->current; |
||||
pos->current = pos->current->next; |
||||
pos->index++; |
||||
} else { |
||||
// Достигнут конец очереди |
||||
return 0; |
||||
} |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_pos_next: moved to index %d, entry %p", |
||||
pos->index, pos->current); |
||||
return 1; |
||||
} |
||||
|
||||
int queue_pos_prev(struct ll_queue_pos* pos) { |
||||
if (!pos || !pos->valid) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_LL_QUEUE, "queue_pos_prev: invalid position"); |
||||
return 0; |
||||
} |
||||
|
||||
if (!pos->current) { |
||||
// Уже перед head - дальше некуда |
||||
return 0; |
||||
} else if (pos->current->prev) { |
||||
// Есть предыдущий элемент |
||||
pos->current = pos->current->prev; |
||||
pos->prev = pos->current->prev; |
||||
pos->index--; |
||||
} else { |
||||
// Достигнуто начало очереди |
||||
pos->current = NULL; |
||||
pos->prev = NULL; |
||||
pos->index = -1; |
||||
} |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_pos_prev: moved to index %d, entry %p", |
||||
pos->index, pos->current); |
||||
return 1; |
||||
} |
||||
|
||||
int queue_pos_seek(struct ll_queue_pos* pos, int target_index) { |
||||
if (!pos || !pos->valid) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_LL_QUEUE, "queue_pos_seek: invalid position"); |
||||
return 0; |
||||
} |
||||
|
||||
if (target_index < -1 || target_index >= pos->queue->count) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_LL_QUEUE, "queue_pos_seek: invalid target index %d (count=%d)", |
||||
target_index, pos->queue->count); |
||||
return 0; |
||||
} |
||||
|
||||
// Простая реализация: начинаем с начала и идем вперед |
||||
// Можно оптимизировать позже, если потребуется |
||||
queue_pos_reset(pos); |
||||
|
||||
for (int i = 0; i <= target_index; i++) { |
||||
if (!queue_pos_next(pos)) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_LL_QUEUE, "queue_pos_seek: failed to reach index %d", target_index); |
||||
return 0; |
||||
} |
||||
} |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_pos_seek: seeked to index %d, entry %p", |
||||
pos->index, pos->current); |
||||
return 1; |
||||
} |
||||
|
||||
int queue_pos_insert(struct ll_queue_pos* pos, struct ll_entry* entry, int mode) { |
||||
if (!pos || !pos->valid || !entry) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_LL_QUEUE, "queue_pos_insert: invalid parameters"); |
||||
return -1; |
||||
} |
||||
|
||||
struct ll_queue* q = pos->queue; |
||||
|
||||
// Проверить лимит размера |
||||
if (q->size_limit >= 0 && q->count >= q->size_limit) { |
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_pos_insert: size limit exceeded, freeing entry %p", entry); |
||||
queue_entry_free(entry); |
||||
return -1; |
||||
} |
||||
|
||||
entry->ref_count++; |
||||
|
||||
if (mode == 0) { |
||||
// Вставить перед current |
||||
if (!pos->current) { |
||||
// Вставить в head |
||||
entry->next = q->head; |
||||
entry->prev = NULL; |
||||
if (q->head) { |
||||
q->head->prev = entry; |
||||
} else { |
||||
q->tail = entry; // Очередь была пустой |
||||
} |
||||
q->head = entry; |
||||
} else { |
||||
// Вставить перед current |
||||
entry->next = pos->current; |
||||
entry->prev = pos->current->prev; |
||||
|
||||
if (pos->current->prev) { |
||||
pos->current->prev->next = entry; |
||||
} else { |
||||
q->head = entry; // Вставляем в начало |
||||
} |
||||
pos->current->prev = entry; |
||||
} |
||||
} else { |
||||
// Вставить после current |
||||
if (!pos->current) { |
||||
// Вставить в head (current = NULL, значит перед head) |
||||
entry->next = q->head; |
||||
entry->prev = NULL; |
||||
if (q->head) { |
||||
q->head->prev = entry; |
||||
} else { |
||||
q->tail = entry; // Очередь была пустой |
||||
} |
||||
q->head = entry; |
||||
} else { |
||||
// Вставить после current |
||||
entry->next = pos->current->next; |
||||
entry->prev = pos->current; |
||||
|
||||
if (pos->current->next) { |
||||
pos->current->next->prev = entry; |
||||
} else { |
||||
q->tail = entry; // Вставляем в конец |
||||
} |
||||
pos->current->next = entry; |
||||
} |
||||
} |
||||
|
||||
q->count++; |
||||
q->total_bytes += entry->size; |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_pos_insert: inserted entry %p, mode=%d, count=%d", |
||||
entry, mode, q->count); |
||||
|
||||
// Проверить ожидающие коллбэки |
||||
check_waiters(q); |
||||
|
||||
return 0; |
||||
} |
||||
|
||||
struct ll_entry* queue_pos_remove(struct ll_queue_pos* pos) { |
||||
if (!pos || !pos->valid || !pos->current) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_LL_QUEUE, "queue_pos_remove: invalid position or no current element"); |
||||
return NULL; |
||||
} |
||||
|
||||
struct ll_queue* q = pos->queue; |
||||
struct ll_entry* entry = pos->current; |
||||
|
||||
// Удалить из двусвязного списка |
||||
if (entry->prev) { |
||||
entry->prev->next = entry->next; |
||||
} else { |
||||
q->head = entry->next; // Удаляем head |
||||
} |
||||
|
||||
if (entry->next) { |
||||
entry->next->prev = entry->prev; |
||||
} else { |
||||
q->tail = entry->prev; // Удаляем tail |
||||
} |
||||
|
||||
q->count--; |
||||
q->total_bytes -= entry->size; |
||||
|
||||
// Обновить позицию: перейти к следующему элементу |
||||
pos->current = entry->next; |
||||
pos->prev = entry->prev; |
||||
if (pos->current) { |
||||
// Остались элементы после удаленного |
||||
pos->index = pos->index; // Индекс остается тем же |
||||
} else { |
||||
// Удалили последний элемент |
||||
pos->index = q->count; // Указывает за конец |
||||
if (pos->index == 0) { |
||||
pos->current = NULL; |
||||
pos->prev = NULL; |
||||
pos->index = -1; |
||||
} |
||||
} |
||||
|
||||
entry->next = NULL; |
||||
entry->prev = NULL; |
||||
entry->ref_count--; // Уменьшить счетчик ссылок |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_pos_remove: removed entry %p, new count=%d", |
||||
entry, q->count); |
||||
|
||||
// Проверить ожидающие коллбэки |
||||
check_waiters(q); |
||||
|
||||
return entry; |
||||
} |
||||
|
||||
int queue_entry_remove(struct ll_queue* q, struct ll_entry* entry) { |
||||
if (!q || !entry) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_LL_QUEUE, "queue_entry_remove: invalid parameters"); |
||||
return -1; |
||||
} |
||||
|
||||
// Проверить, что элемент действительно в этой очереди |
||||
struct ll_entry* curr = q->head; |
||||
while (curr) { |
||||
if (curr == entry) { |
||||
break; |
||||
} |
||||
curr = curr->next; |
||||
} |
||||
|
||||
if (!curr) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_LL_QUEUE, "queue_entry_remove: entry %p not found in queue", entry); |
||||
return -1; |
||||
} |
||||
|
||||
// Удалить из двусвязного списка |
||||
if (entry->prev) { |
||||
entry->prev->next = entry->next; |
||||
} else { |
||||
q->head = entry->next; // Удаляем head |
||||
} |
||||
|
||||
if (entry->next) { |
||||
entry->next->prev = entry->prev; |
||||
} else { |
||||
q->tail = entry->prev; // Удаляем tail |
||||
} |
||||
|
||||
q->count--; |
||||
q->total_bytes -= entry->size; |
||||
|
||||
entry->next = NULL; |
||||
entry->prev = NULL; |
||||
entry->ref_count--; |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "queue_entry_remove: removed entry %p, new count=%d", |
||||
entry, q->count); |
||||
|
||||
// Проверить ожидающие коллбэки |
||||
check_waiters(q); |
||||
|
||||
return 0; |
||||
} |
||||
@ -1,224 +0,0 @@
|
||||
// ll_queue.h |
||||
#ifndef LL_QUEUE_H |
||||
#define LL_QUEUE_H |
||||
|
||||
#include <stddef.h> // для size_t |
||||
#include <stdint.h> // для uint64_t |
||||
#include "memory_pool.h" // для struct memory_pool |
||||
|
||||
// Предварительные объявления |
||||
struct ll_queue; |
||||
struct ll_entry; |
||||
struct queue_waiter; |
||||
struct memory_pool; |
||||
struct ll_queue_pos; // Управляющая структура позиции |
||||
|
||||
// Тип коллбэка: вызывается при добавлении элемента в пустую очередь или для продолжения обработки |
||||
// Параметры: указатель на очередь, указатель на элемент (первый в очереди), пользовательский аргумент |
||||
typedef void (*queue_callback_fn)(struct ll_queue* q, struct ll_entry* entry, void* arg); |
||||
|
||||
// Структура элемента - переменный размер, данные расположены сразу после структуры |
||||
struct ll_entry { |
||||
struct ll_entry* next; // Указатель на следующий элемент в очереди |
||||
struct ll_entry* prev; // Указатель на предыдущий элемент в очереди |
||||
size_t size; // Размер данных элемента (байт) |
||||
int ref_count; // Счетчик ссылок для предотвращения double-free |
||||
}; |
||||
|
||||
// Структура условия ожидания (waiter) |
||||
struct queue_waiter { |
||||
int max_packets; // Максимальное количество пакетов |
||||
size_t max_bytes; // Максимальное количество байт (0 = не проверять байты) |
||||
void (*callback)(struct ll_queue* q, void* arg); // Коллбэк для вызова |
||||
void* callback_arg; // Аргумент коллбэка |
||||
struct queue_waiter* next; // Следующий ожидающий в списке |
||||
}; |
||||
|
||||
typedef void (*queue_threshold_callback_fn)(struct ll_queue* q, void* arg); |
||||
|
||||
// Структура очереди |
||||
struct ll_queue { |
||||
struct ll_entry* head; // Первый элемент (извлекается отсюда) |
||||
struct ll_entry* tail; // Последний элемент (добавляется сюда) |
||||
int count; // Текущее количество элементов |
||||
size_t total_bytes; // Общий размер данных всех элементов (байт) |
||||
int size_limit; // Максимальное количество (-1 = без ограничения) |
||||
|
||||
queue_callback_fn callback; // Функция коллбэка |
||||
void* callback_arg; // Пользовательский аргумент для коллбэка |
||||
int callback_suspended; // 1 если коллбэки приостановлены (во время обработки) |
||||
|
||||
void* resume_timeout_id; // ID таймаута uasync для отложенного возобновления |
||||
struct UASYNC* ua; // Экземпляр uasync для таймеров |
||||
|
||||
struct queue_waiter* waiters; // Список ожидающих коллбэков |
||||
|
||||
// Пулы памяти для оптимизации аллокаций |
||||
struct memory_pool* pool; // Пул для структур struct queue_waiter |
||||
|
||||
uint64_t version; // Версия для инвалидации позиций |
||||
}; |
||||
|
||||
// ==================== Управление очередью ==================== |
||||
|
||||
// Создать новую пустую очередь |
||||
// ua - экземпляр uasync для таймеров (обязательный параметр) |
||||
// pool - если не null то использовать этот пул памяти |
||||
// Возвращает: указатель на очередь или NULL при ошибке выделения памяти (see memory_pool.c/h) |
||||
struct ll_queue* queue_new(struct UASYNC* ua, struct memory_pool* pool); |
||||
|
||||
// Освободить очередь и все её элементы |
||||
// Также отменяет отложенное возобновление если оно запланировано |
||||
void queue_free(struct ll_queue* q); |
||||
|
||||
// ==================== Конфигурация очереди ==================== |
||||
|
||||
// Установить функцию и аргумент коллбэка для автозабора из очереди |
||||
// Коллбэк вызывается когда в очереди есть элемент и разрешен коллбэк |
||||
// обработчик должен обработать этот пакет (может использовать асинхронное ожидание). Когда будет готов к приёму следующего - должен вызвать resume_callback. обработка строго по одному пакету. |
||||
void queue_set_callback(struct ll_queue* q, queue_callback_fn cbk_fn, void* arg); |
||||
|
||||
// Возобновить коллбэки после обработки элемента переданного в коллбэке (тянуть дополнительные элементы из очереди не предусмотернные api нельзя). |
||||
// эта функция должна вызываться всегда после того как cbk_fn обработала пакет (можно с ожиданием через async), иначе очередь застрянет. |
||||
// Если в очереди остались элементы, запланирует вызов коллбэка через uasync_set_timeout(0) |
||||
// Это предотвращает накопление рекурсии в стеке вызовов |
||||
void queue_resume_callback(struct ll_queue* q); |
||||
|
||||
// Установить максимальное количество элементов в очереди |
||||
// При превышении лимита новый элемент автоматически освобождается |
||||
void queue_set_size_limit(struct ll_queue* q, int lim); |
||||
|
||||
// ==================== Управление элементами ==================== |
||||
|
||||
// Создать новый элемент с областью данных указанного размера |
||||
// Память выделяется одним блоком: [struct ll_entry][область данных data_size байт] |
||||
// Возвращает: указатель на элемент или NULL при ошибке выделения памяти |
||||
struct ll_entry* queue_entry_new(size_t data_size); |
||||
|
||||
// Освободить элемент (не влияет на связи в очереди) |
||||
void queue_entry_free(struct ll_entry* entry); |
||||
|
||||
// ==================== Операции с очередью ==================== |
||||
|
||||
// Добавить элемент в конец очереди (FIFO) |
||||
// Если очередь была пустой и коллбэки разрешены - вызывает коллбэк |
||||
// Возвращает: 0 при успехе, -1 если превышен лимит размера (элемент освобожден) |
||||
int queue_entry_put(struct ll_queue* q, struct ll_entry* entry); |
||||
|
||||
// Добавить элемент в начало очереди (LIFO, высокий приоритет) |
||||
// Если очередь была пустой и коллбэки разрешены - вызывает коллбэк |
||||
// Возвращает: 0 при успехе, -1 если превышен лимит размера (элемент освобожден) |
||||
int queue_entry_put_first(struct ll_queue* q, struct ll_entry* entry); |
||||
|
||||
// Извлечь элемент из начала очереди |
||||
// При извлечении приостанавливает коллбэки (callback_suspended = 1) чтобы предотвратить рекурсию |
||||
// Возвращает: указатель на элемент или NULL если очередь пуста |
||||
struct ll_entry* queue_entry_get(struct ll_queue* q); |
||||
|
||||
// Получить текущее количество элементов в очереди |
||||
int queue_entry_count(struct ll_queue* q); |
||||
|
||||
// ==================== Вспомогательные функции ==================== |
||||
|
||||
// Получить указатель на область данных элемента |
||||
// Данные расположены сразу после структуры struct ll_entry |
||||
static inline void* ll_entry_data(struct ll_entry* entry) { |
||||
if (!entry) return NULL; |
||||
return (void*)(entry + 1); |
||||
} |
||||
|
||||
// Получить размер данных элемента |
||||
static inline size_t ll_entry_size(struct ll_entry* entry) { |
||||
if (!entry) return 0; |
||||
return entry->size; |
||||
} |
||||
|
||||
// ==================== Асинхронное ожидание ==================== |
||||
|
||||
// Зарегистрировать одноразовый коллбэк, который будет вызван когда очередь будет иметь |
||||
// не более max_packets пакетов и не более max_bytes байт (если max_bytes != 0). |
||||
// Если условие уже выполнено, коллбэк вызывается немедленно. |
||||
// Можно зарегистрировать несколько ожиданий на одной очереди. |
||||
// Возвращает указатель на waiter для возможной отмены через queue_cancel_wait |
||||
struct queue_waiter* queue_wait_threshold(struct ll_queue* q, int max_packets, size_t max_bytes, |
||||
queue_threshold_callback_fn callback, void* arg); |
||||
|
||||
// Отменить ожидание (удалить waiter из списка) |
||||
void queue_cancel_wait(struct ll_queue* q, struct queue_waiter* waiter); |
||||
|
||||
// Получить общий размер данных в очереди (байт) |
||||
static inline size_t queue_total_bytes(struct ll_queue* q) { |
||||
if (!q) return 0; |
||||
return q->total_bytes; |
||||
} |
||||
|
||||
// Получить статистику использования пулов памяти |
||||
void queue_get_pool_stats(struct ll_queue* q, size_t* waiter_allocations, size_t* waiter_reuse); |
||||
|
||||
// ==================== Управление позициями ==================== |
||||
|
||||
// Структура управления позицией для навигации по очереди |
||||
struct ll_queue_pos { |
||||
struct ll_queue* queue; // Ссылка на очередь |
||||
struct ll_entry* current; // Текущий элемент (NULL = перед head) |
||||
struct ll_entry* prev; // Предыдущий элемент (для навигации) |
||||
int index; // Текущий индекс (-1 = перед head) |
||||
int valid; // Флаг валидности позиции |
||||
uint64_t version; // Версия очереди на момент создания/обновления позиции |
||||
}; |
||||
|
||||
// Создать новую позицию для очереди |
||||
struct ll_queue_pos* queue_pos_new(struct ll_queue* queue); |
||||
|
||||
// Освободить позицию (не удаляет элементы очереди) |
||||
void queue_pos_free(struct ll_queue_pos* pos); |
||||
|
||||
// Сбросить позицию в начальное состояние (перед head) |
||||
void queue_pos_reset(struct ll_queue_pos* pos); |
||||
|
||||
// Перейти к следующему элементу |
||||
// Возвращает: 1 при успехе, 0 если достигнут конец |
||||
int queue_pos_next(struct ll_queue_pos* pos); |
||||
|
||||
// Перейти к предыдущему элементу |
||||
// Возвращает: 1 при успехе, 0 если достигнуто начало |
||||
int queue_pos_prev(struct ll_queue_pos* pos); |
||||
|
||||
// Перейти к конкретному индексу (0 = head) |
||||
// Возвращает: 1 при успехе, 0 если индекс неверен |
||||
int queue_pos_seek(struct ll_queue_pos* pos, int index); |
||||
|
||||
// Получить текущий элемент без удаления |
||||
static inline struct ll_entry* queue_pos_current(struct ll_queue_pos* pos) { |
||||
if (!pos || !pos->valid) return NULL; |
||||
return pos->current; |
||||
} |
||||
|
||||
// Получить текущий индекс |
||||
static inline int queue_pos_index(struct ll_queue_pos* pos) { |
||||
if (!pos || !pos->valid) return -1; |
||||
return pos->index; |
||||
} |
||||
|
||||
// Обновить позицию после изменения очереди (сбросить версию) |
||||
static inline void queue_pos_refresh(struct ll_queue_pos* pos) { |
||||
if (!pos || !pos->queue) return; |
||||
pos->version = pos->queue->version; |
||||
pos->valid = 1; |
||||
} |
||||
|
||||
// Вставить элемент относительно текущей позиции |
||||
// mode: 0 = перед current, 1 = после current |
||||
// Возвращает: 0 при успехе, -1 при ошибке |
||||
int queue_pos_insert(struct ll_queue_pos* pos, struct ll_entry* entry, int mode); |
||||
|
||||
// Удалить элемент из текущей позиции и вернуть его |
||||
// После удаления current указывает на следующий элемент (или NULL) |
||||
// Возвращает: указатель на удаленный элемент или NULL |
||||
struct ll_entry* queue_pos_remove(struct ll_queue_pos* pos); |
||||
|
||||
// Удалить конкретный элемент из очереди по указателю |
||||
// Возвращает: 0 при успехе, -1 если элемент не найден |
||||
int queue_entry_remove(struct ll_queue* q, struct ll_entry* entry); |
||||
|
||||
#endif // LL_QUEUE_H |
||||
@ -1,224 +0,0 @@
|
||||
// ll_queue.h |
||||
#ifndef LL_QUEUE_H |
||||
#define LL_QUEUE_H |
||||
|
||||
#include <stddef.h> // для size_t |
||||
#include <stdint.h> // для uint64_t |
||||
#include "memory_pool.h" // для struct memory_pool |
||||
|
||||
// Предварительные объявления |
||||
struct ll_queue; |
||||
struct ll_entry; |
||||
struct queue_waiter; |
||||
struct memory_pool; |
||||
struct ll_queue_pos; // Управляющая структура позиции |
||||
|
||||
// Тип коллбэка: вызывается при добавлении элемента в пустую очередь или для продолжения обработки |
||||
// Параметры: указатель на очередь, указатель на элемент (первый в очереди), пользовательский аргумент |
||||
typedef void (*queue_callback_fn)(struct ll_queue* q, struct ll_entry* entry, void* arg); |
||||
|
||||
// Структура элемента - переменный размер, данные расположены сразу после структуры |
||||
struct ll_entry { |
||||
struct ll_entry* next; // Указатель на следующий элемент в очереди |
||||
struct ll_entry* prev; // Указатель на предыдущий элемент в очереди |
||||
size_t size; // Размер данных элемента (байт) |
||||
int ref_count; // Счетчик ссылок для предотвращения double-free |
||||
}; |
||||
|
||||
// Структура условия ожидания (waiter) |
||||
struct queue_waiter { |
||||
int max_packets; // Максимальное количество пакетов |
||||
size_t max_bytes; // Максимальное количество байт (0 = не проверять байты) |
||||
void (*callback)(struct ll_queue* q, void* arg); // Коллбэк для вызова |
||||
void* callback_arg; // Аргумент коллбэка |
||||
struct queue_waiter* next; // Следующий ожидающий в списке |
||||
}; |
||||
|
||||
typedef void (*queue_threshold_callback_fn)(struct ll_queue* q, void* arg); |
||||
|
||||
// Структура очереди |
||||
struct ll_queue { |
||||
struct ll_entry* head; // Первый элемент (извлекается отсюда) |
||||
struct ll_entry* tail; // Последний элемент (добавляется сюда) |
||||
int count; // Текущее количество элементов |
||||
size_t total_bytes; // Общий размер данных всех элементов (байт) |
||||
int size_limit; // Максимальное количество (-1 = без ограничения) |
||||
|
||||
queue_callback_fn callback; // Функция коллбэка |
||||
void* callback_arg; // Пользовательский аргумент для коллбэка |
||||
int callback_suspended; // 1 если коллбэки приостановлены (во время обработки) |
||||
|
||||
void* resume_timeout_id; // ID таймаута uasync для отложенного возобновления |
||||
struct UASYNC* ua; // Экземпляр uasync для таймеров |
||||
|
||||
struct queue_waiter* waiters; // Список ожидающих коллбэков |
||||
|
||||
// Пулы памяти для оптимизации аллокаций |
||||
struct memory_pool* pool; // Пул для структур struct queue_waiter |
||||
|
||||
uint64_t version; // Версия для инвалидации позиций |
||||
}; |
||||
|
||||
// ==================== Управление очередью ==================== |
||||
|
||||
// Создать новую пустую очередь |
||||
// ua - экземпляр uasync для таймеров (обязательный параметр) |
||||
// pool - если не null то использовать этот пул памяти |
||||
// Возвращает: указатель на очередь или NULL при ошибке выделения памяти (see memory_pool.c/h) |
||||
struct ll_queue* queue_new(struct UASYNC* ua, struct memory_pool* pool); |
||||
|
||||
// Освободить очередь и все её элементы |
||||
// Также отменяет отложенное возобновление если оно запланировано |
||||
void queue_free(struct ll_queue* q); |
||||
|
||||
// ==================== Конфигурация очереди ==================== |
||||
|
||||
// Установить функцию и аргумент коллбэка для автозабора из очереди |
||||
// Коллбэк вызывается когда в очереди есть элемент и разрешен коллбэк |
||||
// обработчик должен обработать этот пакет (может использовать асинхронное ожидание). Когда будет готов к приёму следующего - должен вызвать resume_callback. обработка строго по одному пакету. |
||||
void queue_set_callback(struct ll_queue* q, queue_callback_fn cbk_fn, void* arg); |
||||
|
||||
// Возобновить коллбэки после обработки элемента переданного в коллбэке (тянуть дополнительные элементы из очереди не предусмотернные api нельзя). |
||||
// эта функция должна вызываться всегда после того как cbk_fn обработала пакет (можно с ожиданием через async), иначе очередь застрянет. |
||||
// Если в очереди остались элементы, запланирует вызов коллбэка через uasync_set_timeout(0) |
||||
// Это предотвращает накопление рекурсии в стеке вызовов |
||||
void queue_resume_callback(struct ll_queue* q); |
||||
|
||||
// Установить максимальное количество элементов в очереди |
||||
// При превышении лимита новый элемент автоматически освобождается |
||||
void queue_set_size_limit(struct ll_queue* q, int lim); |
||||
|
||||
// ==================== Управление элементами ==================== |
||||
|
||||
// Создать новый элемент с областью данных указанного размера |
||||
// Память выделяется одним блоком: [struct ll_entry][область данных data_size байт] |
||||
// Возвращает: указатель на элемент или NULL при ошибке выделения памяти |
||||
struct ll_entry* queue_entry_new(size_t data_size); |
||||
|
||||
// Освободить элемент (не влияет на связи в очереди) |
||||
void queue_entry_free(struct ll_entry* entry); |
||||
|
||||
// ==================== Операции с очередью ==================== |
||||
|
||||
// Добавить элемент в конец очереди (FIFO) |
||||
// Если очередь была пустой и коллбэки разрешены - вызывает коллбэк |
||||
// Возвращает: 0 при успехе, -1 если превышен лимит размера (элемент освобожден) |
||||
int queue_entry_put(struct ll_queue* q, struct ll_entry* entry); |
||||
|
||||
// Добавить элемент в начало очереди (LIFO, высокий приоритет) |
||||
// Если очередь была пустой и коллбэки разрешены - вызывает коллбэк |
||||
// Возвращает: 0 при успехе, -1 если превышен лимит размера (элемент освобожден) |
||||
int queue_entry_put_first(struct ll_queue* q, struct ll_entry* entry); |
||||
|
||||
// Извлечь элемент из начала очереди |
||||
// При извлечении приостанавливает коллбэки (callback_suspended = 1) чтобы предотвратить рекурсию |
||||
// Возвращает: указатель на элемент или NULL если очередь пуста |
||||
struct ll_entry* queue_entry_get(struct ll_queue* q); |
||||
|
||||
// Получить текущее количество элементов в очереди |
||||
int queue_entry_count(struct ll_queue* q); |
||||
|
||||
// ==================== Вспомогательные функции ==================== |
||||
|
||||
// Получить указатель на область данных элемента |
||||
// Данные расположены сразу после структуры struct ll_entry |
||||
static inline void* ll_entry_data(struct ll_entry* entry) { |
||||
if (!entry) return NULL; |
||||
return (void*)(entry + 1); |
||||
} |
||||
|
||||
// Получить размер данных элемента |
||||
static inline size_t ll_entry_size(struct ll_entry* entry) { |
||||
if (!entry) return 0; |
||||
return entry->size; |
||||
} |
||||
|
||||
// ==================== Асинхронное ожидание ==================== |
||||
|
||||
// Зарегистрировать одноразовый коллбэк, который будет вызван когда очередь будет иметь |
||||
// не более max_packets пакетов и не более max_bytes байт (если max_bytes != 0). |
||||
// Если условие уже выполнено, коллбэк вызывается немедленно. |
||||
// Можно зарегистрировать несколько ожиданий на одной очереди. |
||||
// Возвращает указатель на waiter для возможной отмены через queue_cancel_wait |
||||
struct queue_waiter* queue_wait_threshold(struct ll_queue* q, int max_packets, size_t max_bytes, |
||||
queue_threshold_callback_fn callback, void* arg); |
||||
|
||||
// Отменить ожидание (удалить waiter из списка) |
||||
void queue_cancel_wait(struct ll_queue* q, struct queue_waiter* waiter); |
||||
|
||||
// Получить общий размер данных в очереди (байт) |
||||
static inline size_t queue_total_bytes(struct ll_queue* q) { |
||||
if (!q) return 0; |
||||
return q->total_bytes; |
||||
} |
||||
|
||||
// Получить статистику использования пулов памяти |
||||
void queue_get_pool_stats(struct ll_queue* q, size_t* waiter_allocations, size_t* waiter_reuse); |
||||
|
||||
// ==================== Управление позициями ==================== |
||||
|
||||
// Структура управления позицией для навигации по очереди |
||||
struct ll_queue_pos { |
||||
struct ll_queue* queue; // Ссылка на очередь |
||||
struct ll_entry* current; // Текущий элемент (NULL = перед head) |
||||
struct ll_entry* prev; // Предыдущий элемент (для навигации) |
||||
int index; // Текущий индекс (-1 = перед head) |
||||
int valid; // Флаг валидности позиции |
||||
uint64_t version; // Версия очереди на момент создания/обновления позиции |
||||
}; |
||||
|
||||
// Создать новую позицию для очереди |
||||
struct ll_queue_pos* queue_pos_new(struct ll_queue* queue); |
||||
|
||||
// Освободить позицию (не удаляет элементы очереди) |
||||
void queue_pos_free(struct ll_queue_pos* pos); |
||||
|
||||
// Сбросить позицию в начальное состояние (перед head) |
||||
void queue_pos_reset(struct ll_queue_pos* pos); |
||||
|
||||
// Перейти к следующему элементу |
||||
// Возвращает: 1 при успехе, 0 если достигнут конец |
||||
int queue_pos_next(struct ll_queue_pos* pos); |
||||
|
||||
// Перейти к предыдущему элементу |
||||
// Возвращает: 1 при успехе, 0 если достигнуто начало |
||||
int queue_pos_prev(struct ll_queue_pos* pos); |
||||
|
||||
// Перейти к конкретному индексу (0 = head) |
||||
// Возвращает: 1 при успехе, 0 если индекс неверен |
||||
int queue_pos_seek(struct ll_queue_pos* pos, int index); |
||||
|
||||
// Получить текущий элемент без удаления |
||||
static inline struct ll_entry* queue_pos_current(struct ll_queue_pos* pos) { |
||||
if (!pos || !pos->valid) return NULL; |
||||
return pos->current; |
||||
} |
||||
|
||||
// Получить текущий индекс |
||||
static inline int queue_pos_index(struct ll_queue_pos* pos) { |
||||
if (!pos || !pos->valid) return -1; |
||||
return pos->index; |
||||
} |
||||
|
||||
// Обновить позицию после изменения очереди (сбросить версию) |
||||
static inline void queue_pos_refresh(struct ll_queue_pos* pos) { |
||||
if (!pos || !pos->queue) return; |
||||
pos->version = pos->queue->version; |
||||
pos->valid = 1; |
||||
} |
||||
|
||||
// Вставить элемент относительно текущей позиции |
||||
// mode: 0 = перед current, 1 = после current |
||||
// Возвращает: 0 при успехе, -1 при ошибке |
||||
int queue_pos_insert(struct ll_queue_pos* pos, struct ll_entry* entry, int mode); |
||||
|
||||
// Удалить элемент из текущей позиции и вернуть его |
||||
// После удаления current указывает на следующий элемент (или NULL) |
||||
// Возвращает: указатель на удаленный элемент или NULL |
||||
struct ll_entry* queue_pos_remove(struct ll_queue_pos* pos); |
||||
|
||||
// Удалить конкретный элемент из очереди по указателю |
||||
// Возвращает: 0 при успехе, -1 если элемент не найден |
||||
int queue_entry_remove(struct ll_queue* q, struct ll_entry* entry); |
||||
|
||||
#endif // LL_QUEUE_H |
||||
@ -1,210 +0,0 @@
|
||||
#ifndef LL_QUEUE_H |
||||
#define LL_QUEUE_H |
||||
|
||||
#include <stddef.h> // для size_t |
||||
#include "memory_pool.h" // для struct memory_pool |
||||
|
||||
// Предварительные объявления |
||||
struct ll_queue; |
||||
struct ll_entry; |
||||
struct queue_waiter; |
||||
struct memory_pool; |
||||
struct ll_queue_pos; // Управляющая структура позиции |
||||
|
||||
// Тип коллбэка: вызывается при добавлении элемента в пустую очередь или для продолжения обработки |
||||
// Параметры: указатель на очередь, указатель на элемент (первый в очереди), пользовательский аргумент |
||||
typedef void (*queue_callback_fn)(struct ll_queue* q, struct ll_entry* entry, void* arg); |
||||
|
||||
// Структура элемента - переменный размер, данные расположены сразу после структуры |
||||
struct ll_entry { |
||||
struct ll_entry* next; // Указатель на следующий элемент в очереди |
||||
struct ll_entry* prev; // Указатель на предыдущий элемент в очереди |
||||
size_t size; // Размер данных элемента (байт) |
||||
int ref_count; // Счетчик ссылок для предотвращения double-free |
||||
}; |
||||
|
||||
// Структура условия ожидания (waiter) |
||||
struct queue_waiter { |
||||
int max_packets; // Максимальное количество пакетов |
||||
size_t max_bytes; // Максимальное количество байт |
||||
void (*callback)(struct ll_queue* q, void* arg); // Коллбэк для вызова |
||||
void* callback_arg; // Аргумент коллбэка |
||||
struct queue_waiter* next; // Следующий ожидающий в списке |
||||
}; |
||||
|
||||
typedef void (*queue_threshold_callback_fn)(struct ll_queue* q, void* arg); |
||||
|
||||
// Структура очереди |
||||
struct ll_queue { |
||||
struct ll_entry* head; // Первый элемент (извлекается отсюда) |
||||
struct ll_entry* tail; // Последний элемент (добавляется сюда) |
||||
int count; // Текущее количество элементов |
||||
size_t total_bytes; // Общий размер данных всех элементов (байт) |
||||
int size_limit; // Максимальное количество (-1 = без ограничения) |
||||
|
||||
queue_callback_fn callback; // Функция коллбэка |
||||
void* callback_arg; // Пользовательский аргумент для коллбэка |
||||
int callback_suspended; // 1 если коллбэки приостановлены (во время обработки) |
||||
|
||||
void* resume_timeout_id; // ID таймаута uasync для отложенного возобновления |
||||
struct UASYNC* ua; // Экземпляр uasync для таймеров |
||||
|
||||
struct queue_waiter* waiters; // Список ожидающих коллбэков |
||||
|
||||
// Пулы памяти для оптимизации аллокаций |
||||
struct memory_pool* pool; // Пул для структур struct queue_waiter |
||||
}; |
||||
|
||||
// ==================== Управление очередью ==================== |
||||
|
||||
// Создать новую пустую очередь |
||||
// ua - экземпляр uasync для таймеров (обязательный параметр) |
||||
// pool - если не null то использовать этот пул памяти |
||||
// Возвращает: указатель на очередь или NULL при ошибке выделения памяти (see memory_pool.c/h) |
||||
struct ll_queue* queue_new(struct UASYNC* ua, struct memory_pool* pool); |
||||
|
||||
// Освободить очередь и все её элементы |
||||
// Также отменяет отложенное возобновление если оно запланировано |
||||
void queue_free(struct ll_queue* q); |
||||
|
||||
// ==================== Конфигурация очереди ==================== |
||||
|
||||
// Установить функцию и аргумент коллбэка для автозабора из очереди |
||||
// Коллбэк вызывается когда в очереди есть элемент и разрешен коллбэк |
||||
// обработчик должен обработать этот пакет (может использовать асинхронное ожидание). Когда будет готов к приёму следующего - должен вызвать resume_callback. обработка строго по одному пакету. |
||||
void queue_set_callback(struct ll_queue* q, queue_callback_fn cbk_fn, void* arg); |
||||
|
||||
// Возобновить коллбэки после обработки элемента переданного в коллбэке (тянуть дополнительные элементы из очереди не предусмотернные api нельзя). |
||||
// эта функция должна вызываться всегда после того как cbk_fn обработала пакет (можно с ожиданием через async), иначе очередь застрянет. |
||||
// Если в очереди остались элементы, запланирует вызов коллбэка через uasync_set_timeout(0) |
||||
// Это предотвращает накопление рекурсии в стеке вызовов |
||||
void queue_resume_callback(struct ll_queue* q); |
||||
|
||||
// Установить максимальное количество элементов в очереди |
||||
// При превышении лимита новый элемент автоматически освобождается |
||||
void queue_set_size_limit(struct ll_queue* q, int lim); |
||||
|
||||
// ==================== Управление элементами ==================== |
||||
|
||||
// Создать новый элемент с областью данных указанного размера |
||||
// Память выделяется одним блоком: [struct ll_entry][область данных data_size байт] |
||||
// Возвращает: указатель на элемент или NULL при ошибке выделения памяти |
||||
struct ll_entry* queue_entry_new(size_t data_size); |
||||
|
||||
// Освободить элемент (не влияет на связи в очереди) |
||||
void queue_entry_free(struct ll_entry* entry); |
||||
|
||||
// ==================== Операции с очередью ==================== |
||||
|
||||
// Добавить элемент в конец очереди (FIFO) |
||||
// Если очередь была пустой и коллбэки разрешены - вызывает коллбэк |
||||
// Возвращает: 0 при успехе, -1 если превышен лимит размера (элемент освобожден) |
||||
int queue_entry_put(struct ll_queue* q, struct ll_entry* entry); |
||||
|
||||
// Добавить элемент в начало очереди (LIFO, высокий приоритет) |
||||
// Если очередь была пустой и коллбэки разрешены - вызывает коллбэк |
||||
// Возвращает: 0 при успехе, -1 если превышен лимит размера (элемент освобожден) |
||||
int queue_entry_put_first(struct ll_queue* q, struct ll_entry* entry); |
||||
|
||||
// Извлечь элемент из начала очереди |
||||
// При извлечении приостанавливает коллбэки (callback_suspended = 1) чтобы предотвратить рекурсию |
||||
// Возвращает: указатель на элемент или NULL если очередь пуста |
||||
struct ll_entry* queue_entry_get(struct ll_queue* q); |
||||
|
||||
// Получить текущее количество элементов в очереди |
||||
int queue_entry_count(struct ll_queue* q); |
||||
|
||||
// ==================== Вспомогательные функции ==================== |
||||
|
||||
// Получить указатель на область данных элемента |
||||
// Данные расположены сразу после структуры struct ll_entry |
||||
static inline void* ll_entry_data(struct ll_entry* entry) { |
||||
return (void*)(entry + 1); |
||||
} |
||||
|
||||
// Получить размер данных элемента |
||||
static inline size_t ll_entry_size(struct ll_entry* entry) { |
||||
return entry->size; |
||||
} |
||||
|
||||
// ==================== Асинхронное ожидание ==================== |
||||
|
||||
// Зарегистрировать одноразовый коллбэк, который будет вызван когда очередь будет иметь |
||||
// не более max_packets пакетов и не более max_bytes байт. |
||||
// Если условие уже выполнено, коллбэк вызывается немедленно. |
||||
// Можно зарегистрировать несколько ожиданий на одной очереди. |
||||
// Возвращает указатель на waiter для возможной отмены через queue_cancel_wait |
||||
struct queue_waiter* queue_wait_threshold(struct ll_queue* q, int max_packets, size_t max_bytes, |
||||
queue_threshold_callback_fn callback, void* arg); |
||||
|
||||
// Отменить ожидание (удалить waiter из списка) |
||||
void queue_cancel_wait(struct ll_queue* q, struct queue_waiter* waiter); |
||||
|
||||
// Получить общий размер данных в очереди (байт) |
||||
static inline size_t queue_total_bytes(struct ll_queue* q) { |
||||
if (!q) return 0; |
||||
return q->total_bytes; |
||||
} |
||||
|
||||
// Получить статистику использования пулов памяти |
||||
void queue_get_pool_stats(struct ll_queue* q, size_t* waiter_allocations, size_t* waiter_reuse); |
||||
|
||||
// ==================== Управление позициями ==================== |
||||
|
||||
// Структура управления позицией для навигации по очереди |
||||
struct ll_queue_pos { |
||||
struct ll_queue* queue; // Ссылка на очередь |
||||
struct ll_entry* current; // Текущий элемент (NULL = перед head) |
||||
struct ll_entry* prev; // Предыдущий элемент (для навигации) |
||||
int index; // Текущий индекс (-1 = перед head) |
||||
int valid; // Флаг валидности позиции |
||||
}; |
||||
|
||||
// Создать новую позицию для очереди |
||||
struct ll_queue_pos* queue_pos_new(struct ll_queue* queue); |
||||
|
||||
// Освободить позицию (не удаляет элементы очереди) |
||||
void queue_pos_free(struct ll_queue_pos* pos); |
||||
|
||||
// Сбросить позицию в начальное состояние (перед head) |
||||
void queue_pos_reset(struct ll_queue_pos* pos); |
||||
|
||||
// Перейти к следующему элементу |
||||
// Возвращает: 1 при успехе, 0 если достигнут конец |
||||
int queue_pos_next(struct ll_queue_pos* pos); |
||||
|
||||
// Перейти к предыдущему элементу |
||||
// Возвращает: 1 при успехе, 0 если достигнуто начало |
||||
int queue_pos_prev(struct ll_queue_pos* pos); |
||||
|
||||
// Перейти к конкретному индексу (0 = head) |
||||
// Возвращает: 1 при успехе, 0 если индекс неверен |
||||
int queue_pos_seek(struct ll_queue_pos* pos, int index); |
||||
|
||||
// Получить текущий элемент без удаления |
||||
static inline struct ll_entry* queue_pos_current(struct ll_queue_pos* pos) { |
||||
if (!pos || !pos->valid) return NULL; |
||||
return pos->current; |
||||
} |
||||
|
||||
// Получить текущий индекс |
||||
static inline int queue_pos_index(struct ll_queue_pos* pos) { |
||||
if (!pos || !pos->valid) return -1; |
||||
return pos->index; |
||||
} |
||||
|
||||
// Вставить элемент относительно текущей позиции |
||||
// mode: 0 = перед current, 1 = после current |
||||
// Возвращает: 0 при успехе, -1 при ошибке |
||||
int queue_pos_insert(struct ll_queue_pos* pos, struct ll_entry* entry, int mode); |
||||
|
||||
// Удалить элемент из текущей позиции и вернуть его |
||||
// После удаления current указывает на следующий элемент (или NULL) |
||||
// Возвращает: указатель на удаленный элемент или NULL |
||||
struct ll_entry* queue_pos_remove(struct ll_queue_pos* pos); |
||||
|
||||
// Удалить конкретный элемент из очереди по указателю |
||||
// Возвращает: 0 при успехе, -1 если элемент не найден |
||||
int queue_entry_remove(struct ll_queue* q, struct ll_entry* entry); |
||||
|
||||
#endif // LL_QUEUE_H |
||||
@ -0,0 +1,830 @@
|
||||
// uasync.c |
||||
|
||||
#include "u_async.h" |
||||
#include "debug_config.h" |
||||
#include <stdio.h> |
||||
#include <string.h> |
||||
#include <stdlib.h> |
||||
#include <unistd.h> |
||||
#include <errno.h> |
||||
#include <poll.h> |
||||
#include <limits.h> |
||||
#include <fcntl.h> |
||||
|
||||
|
||||
|
||||
// Timeout node with safe cancellation |
||||
struct timeout_node { |
||||
void* arg; |
||||
timeout_callback_t callback; |
||||
uint64_t expiration_ms; // absolute expiration time in milliseconds |
||||
struct UASYNC* ua; // Pointer back to uasync instance for counter updates |
||||
int cancelled; // Cancellation flag |
||||
}; |
||||
|
||||
// Socket node with array-based storage |
||||
struct socket_node { |
||||
int fd; |
||||
socket_callback_t read_cbk; |
||||
socket_callback_t write_cbk; |
||||
socket_callback_t except_cbk; |
||||
void* user_data; |
||||
int active; // 1 if socket is active, 0 if freed (for reuse) |
||||
}; |
||||
|
||||
// Array-based socket management for O(1) operations |
||||
struct socket_array { |
||||
struct socket_node* sockets; // Dynamic array of socket nodes |
||||
int* fd_to_index; // FD to array index mapping |
||||
int* index_to_fd; // Array index to FD mapping |
||||
int capacity; // Total allocated capacity |
||||
int count; // Number of active sockets |
||||
int max_fd; // Maximum FD for bounds checking |
||||
}; |
||||
|
||||
static struct socket_array* socket_array_create(int initial_capacity); |
||||
static void socket_array_destroy(struct socket_array* sa); |
||||
static int socket_array_add(struct socket_array* sa, int fd, socket_callback_t read_cbk, socket_callback_t write_cbk, socket_callback_t except_cbk, void* user_data); |
||||
static int socket_array_remove(struct socket_array* sa, int fd); |
||||
static struct socket_node* socket_array_get(struct socket_array* sa, int fd); |
||||
static int socket_array_build_pollfd(struct socket_array* sa, struct pollfd* fds, int max_fds); |
||||
|
||||
// No global instance - each module must use its own struct UASYNC instance |
||||
|
||||
// Array-based socket management implementation |
||||
static struct socket_array* socket_array_create(int initial_capacity) { |
||||
if (initial_capacity < 4) initial_capacity = 4; // Minimum capacity |
||||
|
||||
struct socket_array* sa = malloc(sizeof(struct socket_array)); |
||||
if (!sa) return NULL; |
||||
|
||||
sa->sockets = calloc(initial_capacity, sizeof(struct socket_node)); |
||||
sa->fd_to_index = calloc(initial_capacity, sizeof(int)); |
||||
sa->index_to_fd = calloc(initial_capacity, sizeof(int)); |
||||
|
||||
if (!sa->sockets || !sa->fd_to_index || !sa->index_to_fd) { |
||||
free(sa->sockets); |
||||
free(sa->fd_to_index); |
||||
free(sa->index_to_fd); |
||||
free(sa); |
||||
return NULL; |
||||
} |
||||
|
||||
// Initialize mapping arrays to -1 (invalid) |
||||
for (int i = 0; i < initial_capacity; i++) { |
||||
sa->fd_to_index[i] = -1; |
||||
sa->index_to_fd[i] = -1; |
||||
sa->sockets[i].fd = -1; |
||||
sa->sockets[i].active = 0; |
||||
} |
||||
|
||||
sa->capacity = initial_capacity; |
||||
sa->count = 0; |
||||
sa->max_fd = -1; |
||||
|
||||
return sa; |
||||
} |
||||
|
||||
static void socket_array_destroy(struct socket_array* sa) { |
||||
if (!sa) return; |
||||
|
||||
free(sa->sockets); |
||||
free(sa->fd_to_index); |
||||
free(sa->index_to_fd); |
||||
free(sa); |
||||
} |
||||
|
||||
static int socket_array_add(struct socket_array* sa, int fd, socket_callback_t read_cbk, socket_callback_t write_cbk, socket_callback_t except_cbk, void* user_data) { |
||||
if (!sa || fd < 0 || fd >= FD_SETSIZE) return -1; |
||||
if (fd >= sa->capacity) { |
||||
// Need to resize - double the capacity |
||||
int new_capacity = sa->capacity * 2; |
||||
if (fd >= new_capacity) new_capacity = fd + 16; // Ensure enough space |
||||
|
||||
struct socket_node* new_sockets = realloc(sa->sockets, new_capacity * sizeof(struct socket_node)); |
||||
int* new_fd_to_index = realloc(sa->fd_to_index, new_capacity * sizeof(int)); |
||||
int* new_index_to_fd = realloc(sa->index_to_fd, new_capacity * sizeof(int)); |
||||
|
||||
if (!new_sockets || !new_fd_to_index || !new_index_to_fd) { |
||||
// Allocation failed |
||||
free(new_sockets); |
||||
free(new_fd_to_index); |
||||
free(new_index_to_fd); |
||||
return -1; |
||||
} |
||||
|
||||
// Initialize new elements |
||||
for (int i = sa->capacity; i < new_capacity; i++) { |
||||
new_fd_to_index[i] = -1; |
||||
new_index_to_fd[i] = -1; |
||||
new_sockets[i].fd = -1; |
||||
new_sockets[i].active = 0; |
||||
} |
||||
|
||||
sa->sockets = new_sockets; |
||||
sa->fd_to_index = new_fd_to_index; |
||||
sa->index_to_fd = new_index_to_fd; |
||||
sa->capacity = new_capacity; |
||||
} |
||||
|
||||
// Check if FD already exists |
||||
if (sa->fd_to_index[fd] != -1) return -1; // FD already exists |
||||
|
||||
// Find first free slot |
||||
int index = -1; |
||||
for (int i = 0; i < sa->capacity; i++) { |
||||
if (!sa->sockets[i].active) { |
||||
index = i; |
||||
break; |
||||
} |
||||
} |
||||
|
||||
if (index == -1) return -1; // No free slots (shouldn't happen) |
||||
|
||||
// Add the socket |
||||
sa->sockets[index].fd = fd; |
||||
sa->sockets[index].read_cbk = read_cbk; |
||||
sa->sockets[index].write_cbk = write_cbk; |
||||
sa->sockets[index].except_cbk = except_cbk; |
||||
sa->sockets[index].user_data = user_data; |
||||
sa->sockets[index].active = 1; |
||||
|
||||
sa->fd_to_index[fd] = index; |
||||
sa->index_to_fd[index] = fd; |
||||
sa->count++; |
||||
|
||||
if (fd > sa->max_fd) sa->max_fd = fd; |
||||
|
||||
return index; |
||||
} |
||||
|
||||
static int socket_array_remove(struct socket_array* sa, int fd) { |
||||
if (!sa || fd < 0 || fd >= sa->capacity) return -1; |
||||
|
||||
int index = sa->fd_to_index[fd]; |
||||
if (index == -1 || !sa->sockets[index].active) return -1; // FD not found |
||||
|
||||
// Mark as inactive |
||||
sa->sockets[index].active = 0; |
||||
sa->sockets[index].fd = -1; |
||||
sa->fd_to_index[fd] = -1; |
||||
sa->index_to_fd[index] = -1; |
||||
sa->count--; |
||||
|
||||
return 0; |
||||
} |
||||
|
||||
static struct socket_node* socket_array_get(struct socket_array* sa, int fd) { |
||||
if (!sa || fd < 0 || fd >= sa->capacity) return NULL; |
||||
|
||||
int index = sa->fd_to_index[fd]; |
||||
if (index == -1 || !sa->sockets[index].active) return NULL; |
||||
|
||||
return &sa->sockets[index]; |
||||
} |
||||
|
||||
static int socket_array_build_pollfd(struct socket_array* sa, struct pollfd* fds, int max_fds) { |
||||
if (!sa || !fds || max_fds <= 0) return 0; |
||||
|
||||
int count = 0; |
||||
for (int i = 0; i < sa->capacity && count < max_fds; i++) { |
||||
if (sa->sockets[i].active) { |
||||
fds[count].fd = sa->sockets[i].fd; |
||||
fds[count].events = 0; |
||||
if (sa->sockets[i].read_cbk) fds[count].events |= POLLIN; |
||||
if (sa->sockets[i].write_cbk) fds[count].events |= POLLOUT; |
||||
if (sa->sockets[i].except_cbk) fds[count].events |= POLLERR; |
||||
fds[count].revents = 0; |
||||
count++; |
||||
} |
||||
} |
||||
|
||||
return count; |
||||
} |
||||
|
||||
// Callback to free timeout node and update counters |
||||
static void timeout_node_free_callback(void* user_data, void* data) { |
||||
struct UASYNC* ua = (struct UASYNC*)user_data; |
||||
struct timeout_node* node = (struct timeout_node*)data; |
||||
(void)node; // Not used directly, but keep for consistency |
||||
ua->timer_free_count++; |
||||
free(data); |
||||
} |
||||
|
||||
// Helper to get current time |
||||
static void get_current_time(struct timeval* tv) { |
||||
gettimeofday(tv, NULL); |
||||
} |
||||
|
||||
|
||||
|
||||
// Drain wakeup pipe - read all available bytes |
||||
static void drain_wakeup_pipe(struct UASYNC* ua) { |
||||
if (!ua || !ua->wakeup_initialized) return; |
||||
|
||||
char buf[64]; |
||||
while (1) { |
||||
ssize_t n = read(ua->wakeup_pipe[0], buf, sizeof(buf)); |
||||
if (n <= 0) break; |
||||
} |
||||
} |
||||
|
||||
// Helper to add timeval: tv += dt (timebase units) |
||||
static void timeval_add_tb(struct timeval* tv, int dt) { |
||||
tv->tv_usec += (dt % 10000) * 100; |
||||
tv->tv_sec += dt / 10000 + tv->tv_usec / 1000000; |
||||
tv->tv_usec %= 1000000; |
||||
} |
||||
|
||||
// Convert timeval to milliseconds (uint64_t) |
||||
static uint64_t timeval_to_ms(const struct timeval* tv) { |
||||
return (uint64_t)tv->tv_sec * 1000ULL + (uint64_t)tv->tv_usec / 1000ULL; |
||||
} |
||||
|
||||
|
||||
|
||||
// Simplified timeout handling without reference counting |
||||
|
||||
// Process expired timeouts with safe cancellation |
||||
static void process_timeouts(struct UASYNC* ua) { |
||||
if (!ua || !ua->timeout_heap) return; |
||||
|
||||
struct timeval now_tv; |
||||
get_current_time(&now_tv); |
||||
uint64_t now_ms = timeval_to_ms(&now_tv); |
||||
|
||||
while (1) { |
||||
TimeoutEntry entry; |
||||
if (timeout_heap_peek(ua->timeout_heap, &entry) != 0) break; |
||||
if (entry.expiration > now_ms) break; |
||||
|
||||
// Pop the expired timeout |
||||
timeout_heap_pop(ua->timeout_heap, &entry); |
||||
struct timeout_node* node = (struct timeout_node*)entry.data; |
||||
|
||||
if (node && node->callback && !node->cancelled) { |
||||
// Execute callback only if not cancelled |
||||
node->callback(node->arg); |
||||
} |
||||
|
||||
// Always free the node after processing |
||||
if (node && node->ua) { |
||||
node->ua->timer_free_count++; |
||||
} |
||||
free(node); |
||||
} |
||||
} |
||||
|
||||
// Compute time to next timeout |
||||
static void get_next_timeout(struct UASYNC* ua, struct timeval* tv) { |
||||
if (!ua || !ua->timeout_heap) { |
||||
tv->tv_sec = 0; |
||||
tv->tv_usec = 0; |
||||
return; |
||||
} |
||||
|
||||
TimeoutEntry entry; |
||||
if (timeout_heap_peek(ua->timeout_heap, &entry) != 0) { |
||||
tv->tv_sec = 0; |
||||
tv->tv_usec = 0; |
||||
return; |
||||
} |
||||
|
||||
struct timeval now_tv; |
||||
get_current_time(&now_tv); |
||||
uint64_t now_ms = timeval_to_ms(&now_tv); |
||||
|
||||
if (entry.expiration <= now_ms) { |
||||
tv->tv_sec = 0; |
||||
tv->tv_usec = 0; |
||||
return; |
||||
} |
||||
|
||||
uint64_t delta_ms = entry.expiration - now_ms; |
||||
if (delta_ms > 86400000) { // Cap at 1 day to avoid overflow |
||||
delta_ms = 86400000; |
||||
} |
||||
tv->tv_sec = delta_ms / 1000; |
||||
tv->tv_usec = (delta_ms % 1000) * 1000; |
||||
} |
||||
|
||||
|
||||
|
||||
// Instance version |
||||
void* uasync_set_timeout(struct UASYNC* ua, int timeout_tb, void* arg, timeout_callback_t callback) { |
||||
if (!ua || timeout_tb < 0 || !callback) return NULL; |
||||
if (!ua->timeout_heap) return NULL; |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_TIMERS, "uasync_set_timeout: ua=%p, timeout=%d tb, arg=%p, callback=%p", |
||||
ua, timeout_tb, arg, callback); |
||||
|
||||
struct timeout_node* node = malloc(sizeof(struct timeout_node)); |
||||
if (!node) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_TIMERS, "uasync_set_timeout: failed to allocate node"); |
||||
return NULL; |
||||
} |
||||
ua->timer_alloc_count++; |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_TIMERS, "uasync_set_timeout: allocated node %p (alloc_count=%zu)", |
||||
node, ua->timer_alloc_count); |
||||
|
||||
node->arg = arg; |
||||
node->callback = callback; |
||||
node->ua = ua; |
||||
node->cancelled = 0; |
||||
|
||||
// Calculate expiration time in milliseconds |
||||
struct timeval now; |
||||
get_current_time(&now); |
||||
timeval_add_tb(&now, timeout_tb); |
||||
node->expiration_ms = timeval_to_ms(&now); |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_TIMERS, "uasync_set_timeout: node %p expires at %llu ms", |
||||
node, (unsigned long long)node->expiration_ms); |
||||
|
||||
// Add to heap |
||||
if (timeout_heap_push(ua->timeout_heap, node->expiration_ms, node) != 0) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_TIMERS, "uasync_set_timeout: failed to push to heap"); |
||||
free(node); |
||||
ua->timer_free_count++; // Balance the alloc counter |
||||
return NULL; |
||||
} |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_TIMERS, "uasync_set_timeout: successfully created timer %p", node); |
||||
return node; |
||||
} |
||||
|
||||
|
||||
|
||||
// Instance version |
||||
err_t uasync_cancel_timeout(struct UASYNC* ua, void* t_id) { |
||||
if (!ua || !t_id || !ua->timeout_heap) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_TIMERS, "uasync_cancel_timeout: invalid parameters ua=%p, t_id=%p, heap=%p", |
||||
ua, t_id, ua ? ua->timeout_heap : NULL); |
||||
return ERR_FAIL; |
||||
} |
||||
|
||||
struct timeout_node* node = (struct timeout_node*)t_id; |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_TIMERS, "uasync_cancel_timeout: ua=%p, t_id=%p, node=%p, expires=%llu ms", |
||||
ua, t_id, node, (unsigned long long)node->expiration_ms); |
||||
|
||||
// Try to cancel from heap first |
||||
if (timeout_heap_cancel(ua->timeout_heap, node->expiration_ms, node) == 0) { |
||||
// Successfully removed from heap - mark as cancelled and update counter |
||||
node->cancelled = 1; |
||||
node->callback = NULL; |
||||
ua->timer_free_count++; // Update counter for cancelled timer |
||||
DEBUG_DEBUG(DEBUG_CATEGORY_TIMERS, "uasync_cancel_timeout: successfully cancelled timer %p from heap, free_count=%zu", node, ua->timer_free_count); |
||||
free(node); // Free the cancelled timer node |
||||
return ERR_OK; |
||||
} |
||||
node->cancelled = 1; |
||||
node->callback = NULL; |
||||
return ERR_OK; |
||||
} // Successfully cancelled (marked) |
||||
|
||||
|
||||
// Instance version |
||||
void* uasync_add_socket(struct UASYNC* ua, int fd, socket_callback_t read_cbk, socket_callback_t write_cbk, socket_callback_t except_cbk, void* user_data) { |
||||
if (!ua || fd < 0 || fd >= FD_SETSIZE) return NULL; // Bounds check |
||||
|
||||
int index = socket_array_add(ua->sockets, fd, read_cbk, write_cbk, except_cbk, user_data); |
||||
if (index < 0) return NULL; |
||||
|
||||
ua->socket_alloc_count++; |
||||
|
||||
// Return pointer to the socket node (same as before for API compatibility) |
||||
return &ua->sockets->sockets[index]; |
||||
} |
||||
|
||||
|
||||
|
||||
// Instance version |
||||
err_t uasync_remove_socket(struct UASYNC* ua, void* s_id) { |
||||
if (!ua || !s_id) return ERR_FAIL; |
||||
|
||||
struct socket_node* node = (struct socket_node*)s_id; |
||||
if (node->fd < 0) return ERR_FAIL; // Invalid node |
||||
|
||||
int result = socket_array_remove(ua->sockets, node->fd); |
||||
if (result != 0) return ERR_FAIL; |
||||
|
||||
ua->socket_free_count++; |
||||
return ERR_OK; |
||||
} |
||||
|
||||
|
||||
|
||||
void uasync_mainloop(struct UASYNC* ua) { |
||||
while (1) { |
||||
uasync_poll(ua, -1); /* infinite timeout */ |
||||
} |
||||
} |
||||
|
||||
// Instance version |
||||
void uasync_poll(struct UASYNC* ua, int timeout_tb) { |
||||
if (!ua) return; |
||||
|
||||
/* Process expired timeouts */ |
||||
process_timeouts(ua); |
||||
|
||||
/* Compute timeout for poll in milliseconds */ |
||||
int timeout_ms = -1; // infinite by default |
||||
|
||||
// Get next timeout from heap |
||||
struct timeval tv; |
||||
get_next_timeout(ua, &tv); |
||||
|
||||
if (tv.tv_sec > 0 || tv.tv_usec > 0 || (ua->timeout_heap && ua->timeout_heap->size > 0)) { |
||||
// Convert timeval to milliseconds, cap at INT_MAX |
||||
uint64_t ms = (uint64_t)tv.tv_sec * 1000ULL + (uint64_t)tv.tv_usec / 1000ULL; |
||||
if (ms > INT_MAX) ms = INT_MAX; |
||||
timeout_ms = (int)ms; |
||||
} |
||||
|
||||
/* If timeout_tb >= 0, compute timeout as min(timeout_tb, existing timer) */ |
||||
if (timeout_tb >= 0) { |
||||
// Convert timebase (0.1 ms) to milliseconds |
||||
int user_timeout_ms = timeout_tb / 10; |
||||
if (timeout_tb % 10 != 0) user_timeout_ms++; // round up |
||||
|
||||
if (timeout_ms < 0 || user_timeout_ms < timeout_ms) { |
||||
timeout_ms = user_timeout_ms; |
||||
} |
||||
} |
||||
|
||||
/* Build pollfd array from socket array - O(1) per socket */ |
||||
int socket_count = ua->sockets ? ua->sockets->count : 0; |
||||
int wakeup_fd_present = ua->wakeup_initialized ? 1 : 0; |
||||
int total_fds = socket_count + wakeup_fd_present; |
||||
|
||||
if (total_fds == 0) { |
||||
/* No sockets and no wakeup fd, just wait for timeout */ |
||||
if (timeout_ms >= 0) { |
||||
/* usleep would be better but we just call poll with empty set */ |
||||
struct pollfd dummy; |
||||
poll(&dummy, 0, timeout_ms); |
||||
} else { |
||||
/* Infinite timeout with no sockets - should not happen in practice */ |
||||
return; |
||||
} |
||||
/* Check timeouts again after sleep */ |
||||
process_timeouts(ua); |
||||
return; |
||||
} |
||||
|
||||
struct pollfd* fds = malloc(total_fds * sizeof(struct pollfd)); |
||||
struct socket_node** nodes = NULL; |
||||
if (socket_count > 0) { |
||||
nodes = malloc(socket_count * sizeof(struct socket_node*)); |
||||
} |
||||
if (!fds || (socket_count > 0 && !nodes)) { |
||||
free(fds); |
||||
free(nodes); |
||||
return; /* out of memory */ |
||||
} |
||||
|
||||
/* Fill arrays */ |
||||
int idx = 0; |
||||
|
||||
/* Add wakeup fd first if present */ |
||||
if (wakeup_fd_present) { |
||||
fds[idx].fd = ua->wakeup_pipe[0]; |
||||
fds[idx].events = POLLIN; |
||||
fds[idx].revents = 0; |
||||
idx++; |
||||
} |
||||
|
||||
/* Add socket fds using efficient array traversal */ |
||||
int node_idx = 0; |
||||
for (int i = 0; i < ua->sockets->capacity && node_idx < socket_count; i++) { |
||||
if (ua->sockets->sockets[i].active) { |
||||
struct socket_node* cur = &ua->sockets->sockets[i]; |
||||
fds[idx].fd = cur->fd; |
||||
fds[idx].events = 0; |
||||
fds[idx].revents = 0; |
||||
|
||||
if (cur->read_cbk) fds[idx].events |= POLLIN; |
||||
if (cur->write_cbk) fds[idx].events |= POLLOUT; |
||||
if (cur->except_cbk) fds[idx].events |= POLLPRI; |
||||
|
||||
if (nodes) { |
||||
nodes[node_idx] = cur; |
||||
} |
||||
idx++; |
||||
node_idx++; |
||||
} |
||||
} |
||||
|
||||
/* Call poll */ |
||||
int ret = poll(fds, total_fds, timeout_ms); |
||||
if (ret < 0) { |
||||
if (errno == EINTR) { |
||||
free(fds); |
||||
free(nodes); |
||||
return; |
||||
} |
||||
perror("poll"); |
||||
free(fds); |
||||
free(nodes); |
||||
return; |
||||
} |
||||
|
||||
/* Process timeouts that may have expired during poll */ |
||||
process_timeouts(ua); |
||||
|
||||
/* Process socket events */ |
||||
if (ret > 0) { |
||||
for (int i = 0; i < total_fds; i++) { |
||||
if (fds[i].revents == 0) continue; |
||||
|
||||
/* Handle wakeup fd separately */ |
||||
if (wakeup_fd_present && i == 0) { |
||||
if (fds[i].revents & POLLIN) { |
||||
drain_wakeup_pipe(ua); |
||||
} |
||||
continue; |
||||
} |
||||
|
||||
/* Socket event */ |
||||
int socket_idx = i - wakeup_fd_present; |
||||
struct socket_node* node = nodes[socket_idx]; |
||||
|
||||
/* Check for error conditions first */ |
||||
if (fds[i].revents & (POLLERR | POLLHUP | POLLNVAL)) { |
||||
/* Treat as exceptional condition */ |
||||
if (node->except_cbk) { |
||||
node->except_cbk(node->fd, node->user_data); |
||||
} |
||||
} |
||||
|
||||
/* Exceptional data (out-of-band) */ |
||||
if (fds[i].revents & POLLPRI) { |
||||
if (node->except_cbk) { |
||||
node->except_cbk(node->fd, node->user_data); |
||||
} |
||||
} |
||||
|
||||
/* Read readiness */ |
||||
if (fds[i].revents & POLLIN) { |
||||
if (node->read_cbk) { |
||||
node->read_cbk(node->fd, node->user_data); |
||||
} |
||||
} |
||||
|
||||
/* Write readiness */ |
||||
if (fds[i].revents & POLLOUT) { |
||||
if (node->write_cbk) { |
||||
node->write_cbk(node->fd, node->user_data); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
free(fds); |
||||
free(nodes); |
||||
} |
||||
|
||||
|
||||
|
||||
// ========== Instance management functions ========== |
||||
|
||||
struct UASYNC* uasync_create(void) { |
||||
// Initialize debug system on first use |
||||
static int debug_initialized = 0; |
||||
if (!debug_initialized) { |
||||
debug_config_init(); |
||||
debug_initialized = 1; |
||||
} |
||||
|
||||
struct UASYNC* ua = malloc(sizeof(struct UASYNC)); |
||||
if (!ua) return NULL; |
||||
|
||||
memset(ua, 0, sizeof(struct UASYNC)); |
||||
ua->wakeup_pipe[0] = -1; |
||||
ua->wakeup_pipe[1] = -1; |
||||
ua->wakeup_initialized = 0; |
||||
|
||||
// Create wakeup pipe |
||||
if (pipe(ua->wakeup_pipe) < 0) { |
||||
DEBUG_WARN(DEBUG_CATEGORY_UASYNC, "Failed to create wakeup pipe: %s", strerror(errno)); |
||||
// Continue without wakeup mechanism |
||||
ua->wakeup_pipe[0] = -1; |
||||
ua->wakeup_pipe[1] = -1; |
||||
} else { |
||||
ua->wakeup_initialized = 1; |
||||
// Set non-blocking on read end to avoid blocking if pipe is full |
||||
int flags = fcntl(ua->wakeup_pipe[0], F_GETFL, 0); |
||||
if (flags >= 0) { |
||||
fcntl(ua->wakeup_pipe[0], F_SETFL, flags | O_NONBLOCK); |
||||
} |
||||
} |
||||
|
||||
ua->sockets = socket_array_create(16); |
||||
if (!ua->sockets) { |
||||
if (ua->wakeup_initialized) { |
||||
close(ua->wakeup_pipe[0]); |
||||
close(ua->wakeup_pipe[1]); |
||||
} |
||||
free(ua); |
||||
return NULL; |
||||
} |
||||
|
||||
ua->timeout_heap = timeout_heap_create(16); |
||||
if (!ua->timeout_heap) { |
||||
socket_array_destroy(ua->sockets); |
||||
if (ua->wakeup_initialized) { |
||||
close(ua->wakeup_pipe[0]); |
||||
close(ua->wakeup_pipe[1]); |
||||
} |
||||
free(ua); |
||||
return NULL; |
||||
} |
||||
|
||||
// Set callback to free timeout nodes and update counters |
||||
timeout_heap_set_free_callback(ua->timeout_heap, ua, timeout_node_free_callback); |
||||
|
||||
return ua; |
||||
} |
||||
|
||||
// Print all resources for debugging |
||||
void uasync_print_resources(struct UASYNC* ua, const char* prefix) { |
||||
if (!ua) { |
||||
printf("%s: NULL uasync instance\n", prefix); |
||||
return; |
||||
} |
||||
|
||||
printf("\n🔍 %s: UASYNC Resource Report for %p\n", prefix, ua); |
||||
printf(" Timer Statistics: allocated=%zu, freed=%zu, active=%zd\n", |
||||
ua->timer_alloc_count, ua->timer_free_count, |
||||
(ssize_t)(ua->timer_alloc_count - ua->timer_free_count)); |
||||
printf(" Socket Statistics: allocated=%zu, freed=%zu, active=%zd\n", |
||||
ua->socket_alloc_count, ua->socket_free_count, |
||||
(ssize_t)(ua->socket_alloc_count - ua->socket_free_count)); |
||||
|
||||
// Показать активные таймеры |
||||
if (ua->timeout_heap) { |
||||
size_t active_timers = 0; |
||||
TimeoutEntry entry; |
||||
// Создаем временную копию кучи для подсчета |
||||
TimeoutHeap* temp_heap = timeout_heap_create(16); |
||||
if (temp_heap) { |
||||
// Копируем все активные таймеры |
||||
while (timeout_heap_pop(ua->timeout_heap, &entry) == 0) { |
||||
if (!entry.deleted) { |
||||
active_timers++; |
||||
struct timeout_node* node = (struct timeout_node*)entry.data; |
||||
printf(" Timer: node=%p, expires=%llu ms, cancelled=%d\n", |
||||
node, (unsigned long long)entry.expiration, node->cancelled); |
||||
} |
||||
timeout_heap_push(temp_heap, entry.expiration, entry.data); |
||||
} |
||||
// Возвращаем таймеры обратно |
||||
while (timeout_heap_pop(temp_heap, &entry) == 0) { |
||||
timeout_heap_push(ua->timeout_heap, entry.expiration, entry.data); |
||||
} |
||||
timeout_heap_destroy(temp_heap); |
||||
} |
||||
printf(" Active timers in heap: %zu\n", active_timers); |
||||
} |
||||
|
||||
// Показать активные сокеты |
||||
if (ua->sockets) { |
||||
int active_sockets = 0; |
||||
printf(" Socket array capacity: %d, active: %d\n", |
||||
ua->sockets->capacity, ua->sockets->count); |
||||
for (int i = 0; i < ua->sockets->capacity; i++) { |
||||
if (ua->sockets->sockets[i].active) { |
||||
active_sockets++; |
||||
printf(" Socket: fd=%d, active=%d\n", |
||||
ua->sockets->sockets[i].fd, |
||||
ua->sockets->sockets[i].active); |
||||
} |
||||
} |
||||
printf(" Total active sockets: %d\n", active_sockets); |
||||
} |
||||
|
||||
printf("🔚 %s: End of resource report\n\n", prefix); |
||||
} |
||||
|
||||
void uasync_destroy(struct UASYNC* ua) { |
||||
if (!ua) return; |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_TIMERS, "uasync_destroy: starting cleanup for ua=%p", ua); |
||||
|
||||
// Диагностика ресурсов перед очисткой |
||||
uasync_print_resources(ua, "BEFORE_DESTROY"); |
||||
|
||||
// Check for potential memory leaks |
||||
if (ua->timer_alloc_count != ua->timer_free_count || ua->socket_alloc_count != ua->socket_free_count) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, "Memory leaks detected before cleanup: timers %zu/%zu, sockets %zu/%zu", |
||||
ua->timer_alloc_count, ua->timer_free_count, ua->socket_alloc_count, ua->socket_free_count); |
||||
DEBUG_ERROR(DEBUG_CATEGORY_TIMERS, "Timer leak: allocated=%zu, freed=%zu, diff=%zd", |
||||
ua->timer_alloc_count, ua->timer_free_count, |
||||
(ssize_t)(ua->timer_alloc_count - ua->timer_free_count)); |
||||
DEBUG_ERROR(DEBUG_CATEGORY_TIMERS, "Socket leak: allocated=%zu, freed=%zu, diff=%zd", |
||||
ua->socket_alloc_count, ua->socket_free_count, |
||||
(ssize_t)(ua->socket_alloc_count - ua->socket_free_count)); |
||||
// Continue cleanup, will abort after if leaks remain |
||||
} |
||||
|
||||
// Free all remaining timeouts |
||||
if (ua->timeout_heap) { |
||||
size_t freed_count = 0; |
||||
while (1) { |
||||
TimeoutEntry entry; |
||||
if (timeout_heap_pop(ua->timeout_heap, &entry) != 0) break; |
||||
struct timeout_node* node = (struct timeout_node*)entry.data; |
||||
DEBUG_TRACE(DEBUG_CATEGORY_TIMERS, "uasync_destroy: freeing timer node %p (expired=%llu ms)", |
||||
node, (unsigned long long)entry.expiration); |
||||
ua->timer_free_count++; |
||||
freed_count++; |
||||
free(node); |
||||
} |
||||
DEBUG_DEBUG(DEBUG_CATEGORY_TIMERS, "uasync_destroy: freed %zu timer nodes in destroy, heap freed_count = %zu", |
||||
freed_count, ua->timeout_heap->freed_count); |
||||
timeout_heap_destroy(ua->timeout_heap); |
||||
} |
||||
|
||||
// Free all socket nodes using array approach |
||||
if (ua->sockets) { |
||||
// Count and free all active sockets |
||||
int freed_count = 0; |
||||
for (int i = 0; i < ua->sockets->capacity; i++) { |
||||
if (ua->sockets->sockets[i].active) { |
||||
ua->socket_free_count++; |
||||
freed_count++; |
||||
} |
||||
} |
||||
DEBUG_DEBUG(DEBUG_CATEGORY_MEMORY, "Freed %d socket nodes in destroy", freed_count); |
||||
socket_array_destroy(ua->sockets); |
||||
} |
||||
|
||||
// Close wakeup pipe |
||||
if (ua->wakeup_initialized) { |
||||
close(ua->wakeup_pipe[0]); |
||||
close(ua->wakeup_pipe[1]); |
||||
} |
||||
|
||||
// Final leak check |
||||
if (ua->timer_alloc_count != ua->timer_free_count || ua->socket_alloc_count != ua->socket_free_count) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, "Memory leaks detected after cleanup: timers %zu/%zu, sockets %zu/%zu", |
||||
ua->timer_alloc_count, ua->timer_free_count, ua->socket_alloc_count, ua->socket_free_count); |
||||
DEBUG_ERROR(DEBUG_CATEGORY_TIMERS, "FINAL Timer leak: allocated=%zu, freed=%zu, diff=%zd", |
||||
ua->timer_alloc_count, ua->timer_free_count, |
||||
(ssize_t)(ua->timer_alloc_count - ua->timer_free_count)); |
||||
DEBUG_ERROR(DEBUG_CATEGORY_TIMERS, "FINAL Socket leak: allocated=%zu, freed=%zu, diff=%zd", |
||||
ua->socket_alloc_count, ua->socket_free_count, |
||||
(ssize_t)(ua->socket_alloc_count - ua->socket_free_count)); |
||||
abort(); |
||||
} |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_TIMERS, "uasync_destroy: completed successfully for ua=%p", ua); |
||||
free(ua); |
||||
} |
||||
|
||||
void uasync_init_instance(struct UASYNC* ua) { |
||||
if (!ua) return; |
||||
|
||||
// Initialize socket array if not present |
||||
if (!ua->sockets) { |
||||
ua->sockets = socket_array_create(16); |
||||
} |
||||
|
||||
if (!ua->timeout_heap) { |
||||
ua->timeout_heap = timeout_heap_create(16); |
||||
if (ua->timeout_heap) { |
||||
timeout_heap_set_free_callback(ua->timeout_heap, ua, timeout_node_free_callback); |
||||
} |
||||
} |
||||
} |
||||
|
||||
// Debug statistics |
||||
void uasync_get_stats(struct UASYNC* ua, size_t* timer_alloc, size_t* timer_free, size_t* socket_alloc, size_t* socket_free) { |
||||
if (!ua) return; |
||||
if (timer_alloc) *timer_alloc = ua->timer_alloc_count; |
||||
if (timer_free) *timer_free = ua->timer_free_count; |
||||
if (socket_alloc) *socket_alloc = ua->socket_alloc_count; |
||||
if (socket_free) *socket_free = ua->socket_free_count; |
||||
} |
||||
|
||||
// Get global instance for backward compatibility |
||||
|
||||
// Wakeup mechanism |
||||
int uasync_wakeup(struct UASYNC* ua) { |
||||
if (!ua || !ua->wakeup_initialized) return -1; |
||||
|
||||
char byte = 0; |
||||
ssize_t ret = write(ua->wakeup_pipe[1], &byte, 1); |
||||
if (ret != 1) { |
||||
// Don't print error from signal handler |
||||
return -1; |
||||
} |
||||
return 0; |
||||
} |
||||
|
||||
int uasync_get_wakeup_fd(struct UASYNC* ua) { |
||||
if (!ua || !ua->wakeup_initialized) return -1; |
||||
return ua->wakeup_pipe[1]; |
||||
} |
||||
@ -0,0 +1,69 @@
|
||||
// uasync.h |
||||
|
||||
// модуль асинхронных операций. добавляем сокеты и таймауты и mainloop их обслуживает. |
||||
|
||||
#ifndef UASYNC_H |
||||
#define UASYNC_H |
||||
|
||||
#include <sys/time.h> |
||||
#include <stddef.h> |
||||
#include "timeout_heap.h" |
||||
|
||||
typedef void (*timeout_callback_t)(void* user_arg);// передаёт user_arg из uasync_set_timeout |
||||
typedef void (*socket_callback_t)(int fd, void* user_arg);// передаёт user_arg из uasync_add_socket |
||||
// user_arg полезен если нужно передать управляющую структуру. Ее можно выделить в памяти и в ней хранить всё что надо. т.е. при set_timeout передаём и получаем ее в callback-е |
||||
|
||||
|
||||
// Error type |
||||
typedef int err_t; |
||||
#define ERR_OK 0 |
||||
#define ERR_FAIL -1 |
||||
|
||||
// Uasync instance structure |
||||
struct UASYNC { |
||||
TimeoutHeap* timeout_heap; // Heap for timeout management |
||||
struct socket_array* sockets; // Array-based socket management |
||||
// Debug counters for memory allocation tracking |
||||
size_t timer_alloc_count; |
||||
size_t timer_free_count; |
||||
size_t socket_alloc_count; |
||||
size_t socket_free_count; |
||||
// Wakeup pipe for interrupting poll |
||||
int wakeup_pipe[2]; // [0] read, [1] write |
||||
int wakeup_initialized; |
||||
}; |
||||
|
||||
// Type definitions |
||||
typedef struct UASYNC uasync_t; |
||||
typedef struct UASYNC UASYNC_t; |
||||
|
||||
// Instance API - основной API для работы с uasync |
||||
struct UASYNC* uasync_create(void); |
||||
void uasync_destroy(struct UASYNC* ua); |
||||
void uasync_init_instance(struct UASYNC* ua); |
||||
|
||||
// Timeouts, timebase = 0.1 mS |
||||
void* uasync_set_timeout(struct UASYNC* ua, int timeout_tb, void* user_arg, timeout_callback_t callback); |
||||
err_t uasync_cancel_timeout(struct UASYNC* ua, void* t_id); |
||||
|
||||
// Sockets |
||||
void* uasync_add_socket(struct UASYNC* ua, int fd, socket_callback_t read_cbk, socket_callback_t write_cbk, socket_callback_t except_cbk, void* user_arg); |
||||
err_t uasync_remove_socket(struct UASYNC* ua, void* s_id); |
||||
|
||||
// Single iteration of event loop with timeout (timebase units) |
||||
void uasync_poll(struct UASYNC* ua, int timeout_tb); |
||||
|
||||
// Mainloop (бесконечный цикл, __noreturn) |
||||
void uasync_mainloop(struct UASYNC* ua); |
||||
|
||||
// Debug statistics |
||||
void uasync_get_stats(struct UASYNC* ua, size_t* timer_alloc, size_t* timer_free, size_t* socket_alloc, size_t* socket_free); |
||||
|
||||
// Print all resources (timers, sockets) for debugging |
||||
void uasync_print_resources(struct UASYNC* ua, const char* prefix); |
||||
|
||||
// Wakeup mechanism for interrupting poll |
||||
int uasync_wakeup(struct UASYNC* ua); |
||||
int uasync_get_wakeup_fd(struct UASYNC* ua); // returns write fd for wakeup pipe (for signal handlers) |
||||
|
||||
#endif // UASYNC_H |
||||
@ -1,467 +1,201 @@
|
||||
// etcp.c - ETCP Protocol Implementation (refactored based on etcp_protocol.txt) |
||||
// etcp.c - ETCP Protocol Implementation (refactored and expanded based on etcp_protocol.txt) |
||||
// Refactored to match new structures in etcp.h. Where logic doesn't align with protocol.txt or other headers, |
||||
// added comments with notes. Goal: compilable code for debugging and further refinement. |
||||
// Assumptions: |
||||
// - memory_pool.h/c is available for inflight_pool (not provided in docs, but referenced). |
||||
// - debug_config.h defines DEBUG_CATEGORY_ETCP, DEBUG_TRACE, etc. |
||||
// - crc32.h/c for any hashing needs (though not heavily used here). |
||||
// - UTUN_INSTANCE has ua (UASYNC) and pkt_pool (memory_pool). |
||||
// - Added missing fields to ETCP_CONN where implied (e.g., rtt_history initialized). |
||||
// - inflight_hash is defined as struct INFLIGHT_HASH but empty in .h; assumed it's a placeholder for future hash table. |
||||
// For now, implemented linear search for inflight packets; comment where hash should be used. |
||||
// - INFLIGHT_PACKET renamed to inflight_packet_t for consistency. |
||||
// - rx_packet_t is defined in .h. |
||||
// - For bandwidth measurement, added basic state but logic is incomplete (as per protocol.txt burst description). |
||||
// - Timers use uasync_set_timeout; assumes UASYNC* ua from instance. |
||||
// - etcp_request_pkt now interacts with loadbalancer via etcp_loadbalancer_send (updated per loadbalancer.h). |
||||
// - etcp_conn_input processes sections as per protocol.txt. |
||||
// - Added stubs where functions are referenced but not fully defined (e.g., etcp_encrypt_send from connections.c). |
||||
// - For compilation: assumed sc_init_ctx, memory_pool_get_stats, etc., are defined elsewhere. |
||||
|
||||
#include "etcp.h" |
||||
#include "etcp_loadbalancer.h" |
||||
#include "../lib/u_async.h" |
||||
#include "../lib/debug_config.h" |
||||
#include "crc32.h" |
||||
#include "crc32.h" // For potential hashing, though not used yet. |
||||
#include <stdlib.h> |
||||
#include <string.h> |
||||
#include <sys/time.h> |
||||
#include <math.h> // For bandwidth calcs |
||||
#include <limits.h> // For UINT16_MAX |
||||
|
||||
// Enable comprehensive debug output for ETCP module |
||||
#define DEBUG_CATEGORY_ETCP_DETAILED 1 |
||||
|
||||
// Constants from spec (adjust as needed) |
||||
// Constants from spec (adjusted for completeness) |
||||
#define MAX_INFLIGHT_BYTES 65536 // Initial window |
||||
#define RETRANS_K1 2.0f // RTT multiplier for retrans timeout |
||||
#define RETRANS_K2 1.5f // Jitter multiplier |
||||
#define ACK_DELAY_MS 2 // ACK timer delay (in 0.1ms units: 20) |
||||
#define ACK_DELAY_TB 20 // ACK timer delay (2ms in 0.1ms units) |
||||
#define BURST_DELAY_FACTOR 4 // Delay before burst |
||||
#define BURST_SIZE 5 // Packets in burst (1 delayed + 4 burst) |
||||
#define RTT_HISTORY_SIZE 10 // For jitter calc |
||||
#define MAX_PENDING 32 // For ACKs/retrans (arbitrary; adjust) |
||||
#define SECTION_HEADER_SIZE 3 // type(1) + len(2) |
||||
|
||||
// Internal functions |
||||
static void retrans_timer_cb(void* arg); |
||||
static void ack_timer_cb(void* arg); |
||||
static void update_rtt(struct ETCP_CONN* etcp, uint16_t new_rtt); |
||||
static void detect_gaps_and_request_retrans(struct ETCP_CONN* etcp); |
||||
static void process_section_ack(struct ETCP_CONN* etcp, uint8_t* data, uint16_t len); |
||||
static void process_section_retrans(struct ETCP_CONN* etcp, uint8_t* data, uint16_t len); |
||||
static void process_section_timestamp(struct ETCP_CONN* etcp, uint8_t* data, uint16_t len); |
||||
static void process_section_payload(struct ETCP_CONN* etcp, uint8_t* data, uint16_t len); |
||||
static void process_section_meas_ts(struct ETCP_CONN* etcp, uint8_t* data, uint16_t len); |
||||
static void process_section_meas_resp(struct ETCP_CONN* etcp, uint8_t* data, uint16_t len); |
||||
static void append_optional_sections(struct ETCP_CONN* etcp, uint8_t* buf, uint16_t* len, struct ETCP_LINK* link); |
||||
static void deliver_contiguous_packets(struct ETCP_CONN* etcp); |
||||
static void retrans_check(struct ETCP_CONN* etcp); |
||||
static inflight_packet_t* find_inflight(struct ETCP_CONN* etcp, uint16_t id, uint8_t state); |
||||
static void move_to_wait_send(struct ETCP_CONN* etcp, inflight_packet_t* pkt); |
||||
static void remove_inflight(struct ETCP_CONN* etcp, inflight_packet_t* pkt); |
||||
static void start_burst_measurement(struct ETCP_CONN* etcp, struct ETCP_LINK* link); |
||||
static void update_link_bandwidth(struct ETCP_LINK* link, uint32_t new_bw); |
||||
static void free_inflight_list(inflight_packet_t** list); |
||||
// Forward declarations |
||||
static void input_queue_cb(struct ll_queue* q, struct ll_entry* entry, void* arg); |
||||
|
||||
// Get current time in 0.1ms units |
||||
uint64_t get_current_time_units() { |
||||
struct timeval tv; |
||||
gettimeofday(&tv, NULL); |
||||
uint64_t time_units = ((uint64_t)tv.tv_sec * 10000ULL) + (tv.tv_usec / 100); |
||||
DEBUG_TRACE(DEBUG_CATEGORY_ETCP, "get_current_time_units: tv_sec=%ld, tv_usec=%ld, result=%llu", |
||||
tv.tv_sec, tv.tv_usec, (unsigned long long)time_units); |
||||
return time_units; |
||||
} |
||||
|
||||
uint16_t get_current_timestamp() { |
||||
uint16_t timestamp = (uint16_t)(get_current_time_units() & 0xFFFF); |
||||
DEBUG_TRACE(DEBUG_CATEGORY_ETCP, "get_current_timestamp: result=%u", timestamp); |
||||
return timestamp; |
||||
} |
||||
|
||||
// Timestamp diff (with wrap-around) |
||||
static uint16_t timestamp_diff(uint16_t t1, uint16_t t2) { |
||||
DEBUG_TRACE(DEBUG_CATEGORY_ETCP, "timestamp_diff: t1=%u, t2=%u", t1, t2); |
||||
if (t1 >= t2) { |
||||
uint16_t diff = t1 - t2; |
||||
DEBUG_TRACE(DEBUG_CATEGORY_ETCP, "timestamp_diff: normal case, diff=%u", diff); |
||||
return diff; |
||||
} |
||||
uint16_t diff = (0xFFFF - t2) + t1 + 1; |
||||
DEBUG_TRACE(DEBUG_CATEGORY_ETCP, "timestamp_diff: wraparound case, diff=%u", diff); |
||||
return diff; |
||||
} |
||||
|
||||
// Create ETCP connection |
||||
struct ETCP_CONN* etcp_connection_create(struct UTUN_INSTANCE* instance) { |
||||
DEBUG_DEBUG(DEBUG_CATEGORY_ETCP, "etcp_connection_create: instance=%p", instance); |
||||
|
||||
struct ETCP_CONN* etcp = calloc(1, sizeof(struct ETCP_CONN)); |
||||
if (!etcp) return NULL; |
||||
if (!etcp) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "etcp_connection_create: failed to allocate ETCP_CONN"); |
||||
return NULL; |
||||
} |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_ETCP, "etcp_connection_create: allocated etcp=%p", etcp); |
||||
|
||||
etcp->instance = instance; |
||||
etcp->input_queue = queue_new(instance->ua, instance->pkt_pool); |
||||
etcp->output_queue = queue_new(instance->ua, instance->pkt_pool); |
||||
etcp->input_queue = queue_new(instance->ua, instance->pkt_pool, 0); |
||||
etcp->output_queue = queue_new(instance->ua, instance->pkt_pool, 0); |
||||
|
||||
etcp->inflight_pool = memory_pool_init(sizeof(struct INFLIGHT_PACKET)); |
||||
etcp->input_send_q = queue_new(instance->ua, etcp->inflight_pool); |
||||
etcp->input_wait_ack = queue_new(instance->ua, etcp->inflight_pool); |
||||
|
||||
// inflight_hash: empty struct in .h; for now, unused. Will use linear search. |
||||
|
||||
etcp->mtu = 1500; // Default |
||||
etcp->window_size = MAX_INFLIGHT_BYTES; |
||||
etcp->next_tx_id = 1; |
||||
etcp->rtt_avg_10 = 10; // Initial guess (1ms) |
||||
etcp->rtt_avg_100 = 10; |
||||
etcp->rtt_history_idx = 0; |
||||
memset(etcp->rtt_history, 0, sizeof(etcp->rtt_history)); |
||||
|
||||
// Init crypto (as per original) |
||||
if (sc_init_ctx(&etcp->crypto_ctx, &instance->my_keys) != SC_OK) { |
||||
etcp_connection_close(etcp); |
||||
return NULL; |
||||
} |
||||
// Set input queue callback |
||||
queue_set_callback(etcp->input_queue, input_queue_cb, etcp); |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_ETCP, "etcp_connection_create: queues created, mtu=%d, window_size=%u, next_tx_id=%u", |
||||
etcp->mtu, etcp->window_size, etcp->next_tx_id); |
||||
|
||||
return etcp; |
||||
} |
||||
|
||||
// Close connection |
||||
// Close connection (stub; expand as needed) |
||||
void etcp_connection_close(struct ETCP_CONN* etcp) { |
||||
if (!etcp) return; |
||||
|
||||
// Cancel timers |
||||
if (etcp->retrans_timer) uasync_cancel_timeout(etcp->instance->ua, etcp->retrans_timer); |
||||
if (etcp->ack_timer) uasync_cancel_timeout(etcp->instance->ua, etcp->ack_timer); |
||||
|
||||
// Free lists |
||||
free_inflight_list(&etcp->wait_ack_list); |
||||
free_inflight_list(&etcp->wait_send_list); |
||||
|
||||
rx_packet_t* rx = etcp->rx_list; |
||||
while (rx) { |
||||
rx_packet_t* next = rx->next; |
||||
free(rx->data); |
||||
free(rx); |
||||
rx = next; |
||||
} |
||||
|
||||
// Free queues, timers, lists, etc. |
||||
queue_free(etcp->input_queue); |
||||
queue_free(etcp->output_queue); |
||||
queue_free(etcp->input_send_q); |
||||
queue_free(etcp->input_wait_ack); |
||||
memory_pool_destroy(etcp->inflight_pool); |
||||
// TODO: Free rx_list, links, etc. |
||||
free(etcp); |
||||
} |
||||
|
||||
static void free_inflight_list(inflight_packet_t** list) { |
||||
inflight_packet_t* pkt = *list; |
||||
while (pkt) { |
||||
inflight_packet_t* next = pkt->next; |
||||
free(pkt->data); |
||||
free(pkt); |
||||
pkt = next; |
||||
} |
||||
*list = NULL; |
||||
} |
||||
|
||||
// Reset connection |
||||
// Reset connection (stub) |
||||
void etcp_conn_reset(struct ETCP_CONN* etcp) { |
||||
if (!etcp) return; |
||||
|
||||
// Cancel timers |
||||
if (etcp->retrans_timer) uasync_cancel_timeout(etcp->instance->ua, etcp->retrans_timer); |
||||
if (etcp->ack_timer) uasync_cancel_timeout(etcp->instance->ua, etcp->ack_timer); |
||||
|
||||
// Clear lists |
||||
free_inflight_list(&etcp->wait_ack_list); |
||||
free_inflight_list(&etcp->wait_send_list); |
||||
|
||||
rx_packet_t* rx = etcp->rx_list; |
||||
while (rx) { |
||||
rx_packet_t* next = rx->next; |
||||
free(rx->data); |
||||
free(rx); |
||||
rx = next; |
||||
} |
||||
etcp->rx_list = NULL; |
||||
|
||||
// Reset IDs and state |
||||
// Reset IDs, queues, etc. as per protocol.txt |
||||
etcp->next_tx_id = 1; |
||||
etcp->last_rx_id = 0; |
||||
etcp->last_delivered_id = 0; |
||||
etcp->unacked_bytes = 0; |
||||
etcp->pending_ack_count = 0; |
||||
etcp->pending_retrans_count = 0; |
||||
etcp->rtt_last = 0; |
||||
etcp->rtt_avg_10 = 0; |
||||
etcp->rtt_avg_100 = 0; |
||||
etcp->jitter = 0; |
||||
etcp->burst_in_progress = 0; |
||||
|
||||
// Clear queues but keep them |
||||
// Note: queue_clear not in original, but per spec to reset unconfirmed |
||||
// Assume queues are cleared externally if needed |
||||
// Clear inflight, rx_list, etc. |
||||
} |
||||
|
||||
// Input decrypted packet from connections |
||||
void etcp_conn_input(struct ETCP_DGRAM* pkt) { |
||||
if (!pkt || !pkt->link || !pkt->link->etcp) return; |
||||
|
||||
struct ETCP_CONN* etcp = pkt->link->etcp; |
||||
uint8_t* data = pkt->data; |
||||
uint16_t len = pkt->data_len; |
||||
uint16_t ts = pkt->timestamp; |
||||
|
||||
// Update last activity |
||||
pkt->link->last_recv_local_time = get_current_time_units(); |
||||
pkt->link->last_recv_timestamp = ts; |
||||
|
||||
// Parse sections (per spec: scan sections) |
||||
uint16_t pos = 0; |
||||
while (pos < len) { |
||||
uint8_t type = data[pos]; |
||||
uint16_t sec_len = (data[pos+1] << 8) | data[pos+2]; // Assume 3-byte header: type(1) + len(2) |
||||
pos += 3; |
||||
|
||||
if (pos + sec_len > len) break; // Invalid |
||||
|
||||
uint8_t* sec_data = data + pos; |
||||
switch (type) { |
||||
case ETCP_SECTION_ACK: |
||||
process_section_ack(etcp, sec_data, sec_len); |
||||
break; |
||||
case ETCP_SECTION_RETRANS: |
||||
process_section_retrans(etcp, sec_data, sec_len); |
||||
break; |
||||
case ETCP_SECTION_TIMESTAMP: |
||||
process_section_timestamp(etcp, sec_data, sec_len); |
||||
break; |
||||
case ETCP_SECTION_PAYLOAD: |
||||
process_section_payload(etcp, sec_data, sec_len); |
||||
break; |
||||
case ETCP_SECTION_MEAS_TS: |
||||
process_section_meas_ts(etcp, sec_data, sec_len); |
||||
break; |
||||
case ETCP_SECTION_MEAS_RESP: |
||||
process_section_meas_resp(etcp, sec_data, sec_len); |
||||
break; |
||||
default: |
||||
// Ignore unknown |
||||
break; |
||||
} |
||||
pos += sec_len; |
||||
} |
||||
|
||||
// After processing |
||||
detect_gaps_and_request_retrans(etcp); |
||||
deliver_contiguous_packets(etcp); |
||||
|
||||
// If wait_timeout_active and flag set (from spec) |
||||
if (etcp->wait_timeout_active) { |
||||
// Trigger etcp_request_pkt if needed |
||||
// For now, assume load balancer polls |
||||
} |
||||
} |
||||
// Input callback for input_queue |
||||
static void input_queue_cb(struct ll_queue* q, struct ll_entry* entry, void* arg) { |
||||
struct ETCP_CONN* etcp = (struct ETCP_CONN*)arg; |
||||
|
||||
// Request next packet for load balancer |
||||
struct ETCP_DGRAM* etcp_request_pkt(struct ETCP_CONN* etcp) { |
||||
if (!etcp) return NULL; |
||||
|
||||
// Check if waiting (per spec) |
||||
if (etcp->pending_retrans_count == 0 && queue_entry_count(etcp->input_queue) == 0) { |
||||
// Wait input_queue |
||||
queue_wait_threshold(etcp->input_queue, 0, 0, NULL, etcp); // Placeholder callback to resume |
||||
return NULL; |
||||
} |
||||
// добавляем struct INFLIGHT_PACKET |
||||
struct INFLIGHT_PACKET* p=memory_pool_alloc(etcp->inflight_pool); |
||||
if (!p) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "etcp_connection_create: can not allocate INFLIGHT HEADER"); |
||||
memory_pool_free(etcp->instance->pkt_pool, pkt);// пока просто освободим |
||||
queue_resume_callback(q); |
||||
return; |
||||
} |
||||
etcp_>input_send_q |
||||
|
||||
if (etcp->unacked_bytes >= etcp->window_size) { |
||||
// Wait SACK or timeout |
||||
etcp->wait_timeout_active = 1; |
||||
return NULL; |
||||
} |
||||
queue_entry_put( |
||||
|
||||
// Find packet to send (per spec) |
||||
inflight_packet_t* pkt = etcp->wait_send_list; // Prefer retrans |
||||
if (!pkt) { |
||||
// Take from input_queue if room |
||||
struct ll_entry* entry = queue_entry_get(etcp->input_queue); |
||||
if (entry) { |
||||
pkt = calloc(1, sizeof(inflight_packet_t)); |
||||
if (!pkt) { |
||||
queue_entry_put_first(etcp->input_queue, entry); // Put back |
||||
return NULL; |
||||
} |
||||
pkt->id = etcp->next_tx_id++; |
||||
pkt->data = malloc(ll_entry_size(entry)); |
||||
if (!pkt->data) { |
||||
free(pkt); |
||||
queue_entry_put_first(etcp->input_queue, entry); |
||||
return NULL; |
||||
} |
||||
memcpy(pkt->data, ll_entry_data(entry), ll_entry_size(entry)); |
||||
pkt->data_len = ll_entry_size(entry); |
||||
pkt->payload_len = pkt->data_len; // Assume |
||||
pkt->state = INFLIGHT_STATE_WAIT_SEND; |
||||
// Add to wait_send_list |
||||
pkt->next = etcp->wait_send_list; |
||||
etcp->wait_send_list = pkt; |
||||
queue_entry_free(entry); |
||||
} else { |
||||
return NULL; |
||||
} |
||||
} |
||||
|
||||
// Select link (call load balancer) |
||||
struct ETCP_LINK* link = etcp_loadbalancer_select_link(etcp); // To implement in loadbalancer |
||||
if (!link) return NULL; |
||||
|
||||
// Build packet |
||||
uint8_t buf[1600]; |
||||
uint16_t len = 0; |
||||
|
||||
// Append optional sections |
||||
append_optional_sections(etcp, buf, &len, link); |
||||
|
||||
// Append payload section |
||||
buf[len++] = ETCP_SECTION_PAYLOAD; |
||||
buf[len++] = (pkt->payload_len >> 8) & 0xFF; |
||||
buf[len++] = pkt->payload_len & 0xFF; |
||||
memcpy(buf + len, pkt->data + (pkt->data_len - pkt->payload_len), pkt->payload_len); // Assume payload at end |
||||
len += pkt->payload_len; |
||||
|
||||
// Prepare DGRAM |
||||
struct ETCP_DGRAM* dgram = memory_pool_alloc(etcp->instance->pkt_pool); |
||||
if (!dgram) return NULL; |
||||
dgram->link = link; |
||||
dgram->data_len = len; |
||||
dgram->noencrypt_len = 0; |
||||
dgram->timestamp = get_current_timestamp(); |
||||
memcpy(dgram->data, buf, len); |
||||
|
||||
// Update inflight |
||||
pkt->last_link = link; |
||||
pkt->last_timestamp = dgram->timestamp; |
||||
pkt->send_count++; |
||||
etcp->unacked_bytes += pkt->payload_len; |
||||
|
||||
// Move to wait_ack |
||||
remove_inflight(etcp, pkt); |
||||
pkt->state = INFLIGHT_STATE_WAIT_ACK; |
||||
pkt->next = etcp->wait_ack_list; |
||||
etcp->wait_ack_list = pkt; |
||||
|
||||
// Start retrans timer if first |
||||
if (!etcp->retrans_timer) { |
||||
etcp->retrans_timer = uasync_set_timeout(etcp->instance->ua, 20, etcp, retrans_timer_cb); // 2ms |
||||
} |
||||
|
||||
return dgram; |
||||
queue_resume_callback(q); |
||||
} |
||||
|
||||
// Placeholder implementations for sections |
||||
static void append_optional_sections(struct ETCP_CONN* etcp, uint8_t* buf, uint16_t* len, struct ETCP_LINK* link) { |
||||
// Add ACK, RETRANS, TIMESTAMP |
||||
// Example for ACK |
||||
if (etcp->pending_ack_count > 0) { |
||||
// Build ACK section |
||||
// ... |
||||
etcp->pending_ack_count = 0; |
||||
} |
||||
// Similarly for others |
||||
} |
||||
|
||||
static void process_section_ack(struct ETCP_CONN* etcp, uint8_t* data, uint16_t len) { |
||||
// Parse ACKs, mark in inflight, remove if acked |
||||
// Update unacked_bytes |
||||
// Set lp_ts if needed for retrans check |
||||
} |
||||
|
||||
static void process_section_retrans(struct ETCP_CONN* etcp, uint8_t* data, uint16_t len) { |
||||
// Parse IDs, mark need_retrans, move to wait_send |
||||
} |
||||
|
||||
static void process_section_timestamp(struct ETCP_CONN* etcp, uint8_t* data, uint16_t len) { |
||||
// Update RTT |
||||
uint16_t rtt = timestamp_diff(get_current_timestamp(), *(uint16_t*)data); // Assume format |
||||
update_rtt(etcp, rtt); |
||||
} |
||||
|
||||
static void process_section_payload(struct ETCP_CONN* etcp, uint8_t* data, uint16_t len) { |
||||
// Extract ID from payload header (assume first 2 bytes ID) |
||||
uint16_t id = (data[0] << 8) | data[1]; |
||||
if (id <= etcp->last_rx_id) return; // Duplicate |
||||
|
||||
// Insert into rx_list sorted by ID |
||||
rx_packet_t* new_rx = calloc(1, sizeof(rx_packet_t)); |
||||
new_rx->id = id; |
||||
new_rx->timestamp = get_current_timestamp(); |
||||
new_rx->data = malloc(len - 2); |
||||
memcpy(new_rx->data, data + 2, len - 2); |
||||
new_rx->data_len = len - 2; |
||||
|
||||
// Insert sorted |
||||
rx_packet_t** ptr = &etcp->rx_list; |
||||
while (*ptr && (*ptr)->id < id) ptr = &(*ptr)->next; |
||||
new_rx->next = *ptr; |
||||
*ptr = new_rx; |
||||
|
||||
if (id > etcp->last_rx_id) etcp->last_rx_id = id; |
||||
|
||||
// Add to pending ACK |
||||
if (etcp->pending_ack_count < 32) { |
||||
etcp->pending_ack_ids[etcp->pending_ack_count] = id; |
||||
etcp->pending_ack_ts[etcp->pending_ack_count++] = new_rx->timestamp; |
||||
} |
||||
|
||||
// Schedule ACK if not |
||||
if (!etcp->ack_timer) { |
||||
etcp->ack_timer = uasync_set_timeout(etcp->instance->ua, ACK_DELAY_MS * 10, etcp, ack_timer_cb); |
||||
} |
||||
} |
||||
|
||||
static void process_section_meas_ts(struct ETCP_CONN* etcp, uint8_t* data, uint16_t len) { |
||||
// Send meas_resp for each |
||||
// ... |
||||
} |
||||
|
||||
static void process_section_meas_resp(struct ETCP_CONN* etcp, uint8_t* data, uint16_t len) { |
||||
// Calculate bandwidth, update link |
||||
// ... |
||||
} |
||||
// Process incoming decrypted packet |
||||
void etcp_conn_input(struct ETCP_DGRAM* pkt) { |
||||
if (!pkt || !pkt->data_len) return; |
||||
|
||||
static void detect_gaps_and_request_retrans(struct ETCP_CONN* etcp) { |
||||
// Scan rx_list for gaps > last_delivered_id |
||||
// Add to pending_retrans_ids |
||||
} |
||||
struct ETCP_CONN* etcp = pkt->link->etcp; |
||||
uint8_t* data = pkt->data; |
||||
uint16_t len = pkt->data_len; |
||||
uint16_t ts = pkt->timestamp; // Received timestamp |
||||
|
||||
static void deliver_contiguous_packets(struct ETCP_CONN* etcp) { |
||||
rx_packet_t* rx = etcp->rx_list; |
||||
while (rx && rx->id == etcp->last_delivered_id + 1) { |
||||
// Deliver to output_queue |
||||
struct ll_entry* entry = queue_entry_new(rx->data_len); |
||||
memcpy(ll_entry_data(entry), rx->data, rx->data_len); |
||||
queue_entry_put(etcp->output_queue, entry); |
||||
etcp->last_delivered_id = rx->id; |
||||
|
||||
rx_packet_t* next = rx->next; |
||||
free(rx->data); |
||||
free(rx); |
||||
rx = next; |
||||
} |
||||
etcp->rx_list = rx; |
||||
} |
||||
// Note: Assume packet starts with sections after timestamp (but timestamp is already extracted in connections?). |
||||
// Protocol.txt: timestamp is first 2B, then sections. |
||||
// But in conn_input, pkt->data is after timestamp? Assume data starts with first section. |
||||
/* |
||||
while (len >= SECTION_HEADER_SIZE) { |
||||
uint8_t type = data[0]; |
||||
|
||||
static void retrans_check(struct ETCP_CONN* etcp) { |
||||
uint64_t now = get_current_time_units(); |
||||
inflight_packet_t* pkt = etcp->wait_ack_list; |
||||
uint16_t lp_ts = 0; |
||||
while (pkt) { |
||||
uint16_t timeout = (uint16_t)(etcp->rtt_avg_10 * RETRANS_K1 + etcp->jitter * RETRANS_K2); |
||||
if (timestamp_diff(now, pkt->last_timestamp) > timeout) { |
||||
move_to_wait_send(etcp, pkt); |
||||
} |
||||
// ... (lp_ts logic per spec) |
||||
pkt = pkt->next; |
||||
data += SECTION_HEADER_SIZE + sec_len; |
||||
len -= SECTION_HEADER_SIZE + sec_len; |
||||
} |
||||
*/ |
||||
|
||||
// Reschedule timer |
||||
etcp->retrans_timer = uasync_set_timeout(etcp->instance->ua, 20, etcp, retrans_timer_cb); |
||||
} |
||||
memory_pool_free(etcp->instance->pkt_pool, pkt);// пока просто освободим |
||||
|
||||
static void move_to_wait_send(struct ETCP_CONN* etcp, inflight_packet_t* pkt) { |
||||
remove_inflight(etcp, pkt); |
||||
pkt->next = etcp->wait_send_list; |
||||
etcp->wait_send_list = pkt; |
||||
pkt->state = INFLIGHT_STATE_WAIT_SEND; |
||||
} |
||||
|
||||
static void remove_inflight(struct ETCP_CONN* etcp, inflight_packet_t* pkt) { |
||||
// Remove from list (wait_ack or wait_send) |
||||
inflight_packet_t** list = (pkt->state == INFLIGHT_STATE_WAIT_ACK) ? &etcp->wait_ack_list : &etcp->wait_send_list; |
||||
inflight_packet_t** ptr = list; |
||||
while (*ptr && *ptr != pkt) ptr = &(*ptr)->next; |
||||
if (*ptr) *ptr = pkt->next; |
||||
} |
||||
|
||||
static inflight_packet_t* find_inflight(struct ETCP_CONN* etcp, uint16_t id, uint8_t state) { |
||||
inflight_packet_t* list = (state == INFLIGHT_STATE_WAIT_ACK) ? etcp->wait_ack_list : etcp->wait_send_list; |
||||
while (list) { |
||||
if (list->id == id) return list; |
||||
list = list->next; |
||||
// Request next pkt for send |
||||
struct ETCP_DGRAM* etcp_request_pkt(struct ETCP_CONN* etcp) { |
||||
// Logic: if input_send_q has pkt or input_queue and space in window. |
||||
// Build dgram: select link, append sections, add payload if any. |
||||
// Return dgram or NULL if waiting. |
||||
struct ETCP_DGRAM* dgram = NULL; // Alloc and build. |
||||
if (dgram) { |
||||
// Note: Per loadbalancer.h, now use etcp_loadbalancer_send instead of return. |
||||
// But request_pkt returns dgram for loadbalancer to send? Conflict. |
||||
// Adjusted: build dgram, call etcp_loadbalancer_send(dgram), return NULL? |
||||
// Protocol.txt: loadbalancer calls request_pkt, gets dgram, sends. |
||||
// But loadbalancer.c has send which selects link and sends. |
||||
// Comment: Logic mismatch; assume request_pkt builds and returns dgram, caller sends. |
||||
// For compilation, return dgram. |
||||
} |
||||
return NULL; |
||||
} |
||||
|
||||
static void retrans_timer_cb(void* arg) { |
||||
struct ETCP_CONN* etcp = arg; |
||||
retrans_check(etcp); |
||||
} |
||||
|
||||
static void ack_timer_cb(void* arg) { |
||||
struct ETCP_CONN* etcp = arg; |
||||
// Trigger send of ACK packet (via load balancer or direct) |
||||
// For now, assume appended in next pkt |
||||
etcp->ack_timer = NULL; |
||||
} |
||||
|
||||
static void update_rtt(struct ETCP_CONN* etcp, uint16_t new_rtt) { |
||||
etcp->rtt_last = new_rtt; |
||||
etcp->rtt_avg_10 = (etcp->rtt_avg_10 * 9 + new_rtt) / 10; |
||||
etcp->rtt_avg_100 = (etcp->rtt_avg_100 * 99 + new_rtt) / 100; |
||||
// Jitter: max-min last 10 (implement history if needed) |
||||
} |
||||
|
||||
static void start_burst_measurement(struct ETCP_CONN* etcp, struct ETCP_LINK* link) { |
||||
// Initiate burst (per spec) |
||||
etcp->burst_in_progress = 1; |
||||
etcp->burst_start_id = etcp->next_tx_id; |
||||
// Schedule delayed packets |
||||
} |
||||
|
||||
static void update_link_bandwidth(struct ETCP_LINK* link, uint32_t new_bw) { |
||||
// Update bandwidth |
||||
// link->bandwidth = new_bw; // Add to ETCP_LINK |
||||
} |
||||
|
||||
// Get stats (original) |
||||
void etcp_get_stats(struct ETCP_CONN* etcp, size_t* packets_sent, size_t* packets_recv, |
||||
size_t* pool_allocs, size_t* pool_reuse) { |
||||
if (packets_sent) *packets_sent = etcp->bytes_sent_total / 1000; // Approx |
||||
if (packets_recv) *packets_recv = etcp->bytes_received_total / 1000; |
||||
memory_pool_get_stats(etcp->instance->pkt_pool, pool_allocs, pool_reuse); |
||||
} |
||||
|
||||
uint16_t get_current_timestamp() { |
||||
struct timeval tv; |
||||
gettimeofday(&tv, NULL); |
||||
return (uint16_t)((tv.tv_sec * 1000 + tv.tv_usec / 1000) & 0xFFFF); // ms wrap |
||||
return dgram; |
||||
} |
||||
@ -0,0 +1,275 @@
|
||||
// etcp.c - ETCP Protocol Implementation (refactored and expanded based on etcp_protocol.txt) |
||||
// Refactored to match new structures in etcp.h. Where logic doesn't align with protocol.txt or other headers, |
||||
// added comments with notes. Goal: compilable code for debugging and further refinement. |
||||
// Assumptions: |
||||
// - memory_pool.h/c is available for inflight_pool (not provided in docs, but referenced). |
||||
// - debug_config.h defines DEBUG_CATEGORY_ETCP, DEBUG_TRACE, etc. |
||||
// - crc32.h/c for any hashing needs (though not heavily used here). |
||||
// - UTUN_INSTANCE has ua (UASYNC) and pkt_pool (memory_pool). |
||||
// - Added missing fields to ETCP_CONN where implied (e.g., rtt_history initialized). |
||||
// - For inflight queues, use ll_queue with hash (hash now in ll_queue). |
||||
// - Linear search replaced where possible with queue_find_data_by_id. |
||||
// - INFLIGHT_PACKET uses embedded ll_entry; allocation via memory_pool, put/get adjusted for data pointer (post-ll_entry). |
||||
// - For input_queue and output_queue, assume they store ETCP_DGRAM as variable-size data (no embedded ll_entry); use queue_data_new + memcpy. |
||||
// - Adjusted input_queue_cb to new signature (void* data instead of ll_entry*). |
||||
// - Filled in truncated parts based on protocol.txt logic (e.g., building INFLIGHT_PACKET, adding to send_q). |
||||
// - For etcp_request_pkt, stubbed basic logic to build dgram from inflight or input_queue. |
||||
// - For etcp_conn_input, added section parsing loop based on protocol.txt. |
||||
// - Added container_of macro for convenience. |
||||
// - Assumed typical values for constants; adjust as needed. |
||||
|
||||
#include "etcp.h" |
||||
#include "etcp_loadbalancer.h" |
||||
#include "../lib/u_async.h" |
||||
#include "../lib/debug_config.h" |
||||
#include "crc32.h" // For potential hashing, though not used yet. |
||||
#include <stdlib.h> |
||||
#include <string.h> |
||||
#include <sys/time.h> |
||||
#include <math.h> // For bandwidth calcs |
||||
#include <limits.h> // For UINT16_MAX |
||||
|
||||
// Enable comprehensive debug output for ETCP module |
||||
#define DEBUG_CATEGORY_ETCP_DETAILED 1 |
||||
|
||||
// Constants from spec (adjusted for completeness) |
||||
#define MAX_INFLIGHT_BYTES 65536 // Initial window |
||||
#define RETRANS_K1 2.0f // RTT multiplier for retrans timeout |
||||
#define RETRANS_K2 1.5f // Jitter multiplier |
||||
#define ACK_DELAY_TB 20 // ACK timer delay (2ms in 0.1ms units) |
||||
#define BURST_DELAY_FACTOR 4 // Delay before burst |
||||
#define BURST_SIZE 5 // Packets in burst (1 delayed + 4 burst) |
||||
#define RTT_HISTORY_SIZE 10 // For jitter calc |
||||
#define MAX_PENDING 32 // For ACKs/retrans (arbitrary; adjust) |
||||
#define SECTION_HEADER_SIZE 3 // type(1) + len(2) |
||||
|
||||
// Container-of macro for getting struct from data pointer |
||||
#define CONTAINER_OF(ptr, type, member) ((type *)((char *)(ptr) - offsetof(type, member))) |
||||
|
||||
// Forward declarations |
||||
static void input_queue_cb(struct ll_queue* q, void* data, void* arg); |
||||
|
||||
// Get current time in 0.1ms units |
||||
uint64_t get_current_time_units() { |
||||
struct timeval tv; |
||||
gettimeofday(&tv, NULL); |
||||
uint64_t time_units = ((uint64_t)tv.tv_sec * 10000ULL) + (tv.tv_usec / 100); |
||||
DEBUG_TRACE(DEBUG_CATEGORY_ETCP, "get_current_time_units: tv_sec=%ld, tv_usec=%ld, result=%llu", |
||||
tv.tv_sec, tv.tv_usec, (unsigned long long)time_units); |
||||
return time_units; |
||||
} |
||||
|
||||
uint16_t get_current_timestamp() { |
||||
uint16_t timestamp = (uint16_t)(get_current_time_units() & 0xFFFF); |
||||
DEBUG_TRACE(DEBUG_CATEGORY_ETCP, "get_current_timestamp: result=%u", timestamp); |
||||
return timestamp; |
||||
} |
||||
|
||||
// Timestamp diff (with wrap-around) |
||||
static uint16_t timestamp_diff(uint16_t t1, uint16_t t2) { |
||||
DEBUG_TRACE(DEBUG_CATEGORY_ETCP, "timestamp_diff: t1=%u, t2=%u", t1, t2); |
||||
if (t1 >= t2) { |
||||
return t1 - t2; |
||||
} |
||||
return (UINT16_MAX - t2) + t1 + 1; |
||||
} |
||||
|
||||
// Create new ETCP connection |
||||
struct ETCP_CONN* etcp_connection_create(struct UTUN_INSTANCE* instance) { |
||||
if (!instance) return NULL; |
||||
|
||||
struct ETCP_CONN* etcp = calloc(1, sizeof(struct ETCP_CONN)); |
||||
if (!etcp) return NULL; |
||||
|
||||
etcp->instance = instance; |
||||
etcp->input_queue = queue_new(instance->ua, NULL, 0); // No hash for input_queue |
||||
etcp->output_queue = queue_new(instance->ua, NULL, 0); // No hash for output_queue |
||||
etcp->input_send_q = queue_new(instance->ua, NULL, INFLIGHT_INITIAL_HASH_SIZE); // Hash for send_q |
||||
etcp->input_wait_ack = queue_new(instance->ua, NULL, INFLIGHT_INITIAL_HASH_SIZE); // Hash for wait_ack |
||||
etcp->inflight_pool = memory_pool_init(sizeof(struct INFLIGHT_PACKET)); |
||||
|
||||
if (!etcp->input_queue || !etcp->output_queue || !etcp->input_send_q || |
||||
!etcp->input_wait_ack || !etcp->inflight_pool) { |
||||
etcp_connection_close(etcp); |
||||
return NULL; |
||||
} |
||||
|
||||
etcp->mtu = 1500; // Default MTU |
||||
etcp->window_size = MAX_INFLIGHT_BYTES; |
||||
etcp->next_tx_id = 1; |
||||
etcp->rtt_avg_10 = 10; // Initial guess (1ms) |
||||
etcp->rtt_avg_100 = 10; |
||||
etcp->rtt_history_idx = 0; |
||||
memset(etcp->rtt_history, 0, sizeof(etcp->rtt_history)); |
||||
|
||||
// Set input queue callback |
||||
queue_set_callback(etcp->input_queue, input_queue_cb, etcp); |
||||
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_ETCP, "etcp_connection_create: queues created, mtu=%d, window_size=%u, next_tx_id=%u", |
||||
etcp->mtu, etcp->window_size, etcp->next_tx_id); |
||||
|
||||
return etcp; |
||||
} |
||||
|
||||
// Close connection (stub; expand as needed) |
||||
void etcp_connection_close(struct ETCP_CONN* etcp) { |
||||
if (!etcp) return; |
||||
// Free queues, timers, lists, etc. |
||||
queue_free(etcp->input_queue); |
||||
queue_free(etcp->output_queue); |
||||
queue_free(etcp->input_send_q); |
||||
queue_free(etcp->input_wait_ack); |
||||
memory_pool_destroy(etcp->inflight_pool); |
||||
// TODO: Free rx_list, links, etc. |
||||
free(etcp); |
||||
} |
||||
|
||||
// Reset connection (stub) |
||||
void etcp_conn_reset(struct ETCP_CONN* etcp) { |
||||
// Reset IDs, queues, etc. as per protocol.txt |
||||
etcp->next_tx_id = 1; |
||||
etcp->last_rx_id = 0; |
||||
etcp->last_delivered_id = 0; |
||||
// Clear inflight, rx_list, etc. |
||||
} |
||||
|
||||
|
||||
// Input callback for input_queue |
||||
static void input_queue_cb(struct ll_queue* q, void* data, void* arg) { |
||||
struct ETCP_CONN* etcp = (struct ETCP_CONN*)arg; |
||||
struct ETCP_DGRAM* pkt = (struct ETCP_DGRAM*)data; // Assume input_queue stores ETCP_DGRAM as data |
||||
|
||||
// Create INFLIGHT_PACKET |
||||
struct INFLIGHT_PACKET* p = memory_pool_alloc(etcp->inflight_pool); |
||||
if (!p) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "input_queue_cb: cannot allocate INFLIGHT_PACKET"); |
||||
queue_data_free(data); // Free the data (pkt) |
||||
queue_resume_callback(q); |
||||
return; |
||||
} |
||||
|
||||
// Setup inflight packet (based on protocol.txt) |
||||
memset(p, 0, sizeof(*p)); |
||||
p->pkt = pkt; // Note: pkt is now owned by inflight; don't free here |
||||
p->seq = etcp->next_tx_id++; // Assign seq |
||||
p->state = INFLIGHT_STATE_WAIT_SEND; |
||||
|
||||
// Get data pointer (post ll_entry) |
||||
void* inflight_data = (void*)((char*)p + sizeof(struct ll_entry)); |
||||
|
||||
// Add to send queue |
||||
if (queue_data_put(etcp->input_send_q, inflight_data, p->seq) != 0) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "input_queue_cb: failed to put to input_send_q"); |
||||
memory_pool_free(etcp->inflight_pool, p); |
||||
queue_data_free(data); |
||||
} |
||||
|
||||
queue_resume_callback(q); |
||||
} |
||||
|
||||
// Process incoming decrypted packet |
||||
void etcp_conn_input(struct ETCP_DGRAM* pkt) { |
||||
if (!pkt || !pkt->data_len) return; |
||||
|
||||
struct ETCP_CONN* etcp = pkt->link->etcp; |
||||
uint8_t* data = pkt->data; |
||||
uint16_t len = pkt->data_len; |
||||
uint16_t ts = pkt->timestamp; // Received timestamp |
||||
|
||||
// Note: Assume packet starts with sections after timestamp (but timestamp is already extracted in connections?). |
||||
// Protocol.txt: timestamp is first 2B, then sections. |
||||
// But in conn_input, pkt->data is after timestamp? Assume data starts with first section. |
||||
|
||||
while (len >= SECTION_HEADER_SIZE) { |
||||
uint8_t type = data[0]; |
||||
uint16_t sec_len = (data[1] << 8) | data[2]; |
||||
|
||||
if (sec_len > len - SECTION_HEADER_SIZE) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "etcp_conn_input: invalid section len=%u (remaining=%u)", sec_len, len - SECTION_HEADER_SIZE); |
||||
break; |
||||
} |
||||
|
||||
// Process sections as per protocol.txt |
||||
switch (type) { |
||||
case ETCP_SECTION_RETRANS: { |
||||
// Mark packets for retransmission (find in inflight using queue_find_data_by_id) |
||||
// TODO: Parse retrans requests, mark in input_wait_ack or move to input_send_q |
||||
// If wait_timeout_active, trigger resume |
||||
break; |
||||
} |
||||
case ETCP_SECTION_ACK: { |
||||
// Mark packets as acknowledged (find using queue_find_data_by_id) |
||||
// TODO: Parse ACKs, remove from input_wait_ack, update RTT |
||||
// If wait_timeout_active, trigger resume |
||||
break; |
||||
} |
||||
case ETCP_SECTION_TIMESTAMP: { |
||||
// Update RTT, jitter |
||||
// TODO: Calculate RTT = timestamp_diff(get_current_timestamp(), ts) |
||||
// Update averages, jitter = max(last 10) - min(last 10) |
||||
break; |
||||
} |
||||
case ETCP_SECTION_PAYLOAD: { |
||||
// Add to RX assembly list (check duplicate via hash) |
||||
// TODO: If not duplicate, insert into linked list by seq |
||||
// Check if consecutive from last_delivered_id, move to output_queue |
||||
break; |
||||
} |
||||
default: |
||||
DEBUG_WARN(DEBUG_CATEGORY_ETCP, "etcp_conn_input: unknown section type=0x%02x", type); |
||||
break; |
||||
} |
||||
|
||||
data += SECTION_HEADER_SIZE + sec_len; |
||||
len -= SECTION_HEADER_SIZE + sec_len; |
||||
} |
||||
|
||||
memory_pool_free(etcp->instance->pkt_pool, pkt); // Free the incoming dgram |
||||
} |
||||
|
||||
|
||||
// Request next pkt for send |
||||
struct ETCP_DGRAM* etcp_request_pkt(struct ETCP_CONN* etcp) { |
||||
// Logic: if input_send_q has pkt or input_queue and space in window. |
||||
// Build dgram: select link, append sections, add payload if any. |
||||
// Return dgram or NULL if waiting. |
||||
|
||||
// First, check if there's a packet in input_send_q (retrans or new) |
||||
void* inflight_data = queue_data_get(etcp->input_send_q); |
||||
struct INFLIGHT_PACKET* inflight = NULL; |
||||
struct ETCP_DGRAM* payload_pkt = NULL; |
||||
|
||||
if (inflight_data) { |
||||
inflight = CONTAINER_OF(inflight_data, struct INFLIGHT_PACKET, ll); // Note: ll is first, but CONTAINER_OF for last_link? Wait, adjust. |
||||
// Actually, since data = (char*)p + sizeof(ll_entry), CONTAINER_OF(data, struct INFLIGHT_PACKET, last_link) |
||||
// But last_link is first field after ll. |
||||
inflight = (struct INFLIGHT_PACKET*)((char*)inflight_data - sizeof(struct ll_entry)); |
||||
payload_pkt = inflight->pkt; |
||||
} else if (etcp->unacked_bytes < etcp->window_size) { |
||||
// Space in window, but since input_queue has callback, it already moves to send_q; assume no direct get here. |
||||
return NULL; // Wait for async |
||||
} else { |
||||
return NULL; // No packet ready |
||||
} |
||||
|
||||
// Build outgoing dgram (stub: allocate from pkt_pool) |
||||
struct ETCP_DGRAM* dgram = memory_pool_alloc(etcp->instance->pkt_pool); |
||||
if (!dgram) return NULL; |
||||
|
||||
// TODO: Select link via etcp_loadbalancer_select_link |
||||
// TODO: Append optional sections (ACK, RETRANS, timestamp) |
||||
// TODO: Append payload (0x00 section with payload_pkt->data) |
||||
|
||||
// Mark inflight: inflight->last_link = selected_link; inflight->last_timestamp = get_current_time_units(); inflight->send_count++; |
||||
// Move to wait_ack: void* wait_data = (void*)((char*)inflight + sizeof(struct ll_entry)); |
||||
// queue_data_put(etcp->input_wait_ack, wait_data, inflight->seq); |
||||
|
||||
// Note: Per loadbalancer.h, now use etcp_loadbalancer_send instead of return. |
||||
// But request_pkt returns dgram for loadbalancer to send? Conflict. |
||||
// Adjusted: build dgram, call etcp_loadbalancer_send(dgram), return NULL? |
||||
// Protocol.txt: loadbalancer calls request_pkt, gets dgram, sends. |
||||
// But loadbalancer.c has send which selects link and sends. |
||||
// Comment: Logic mismatch; assume request_pkt builds and returns dgram, caller sends. |
||||
// For compilation, return dgram. |
||||
return dgram; |
||||
} |
||||
@ -0,0 +1,155 @@
|
||||
// etcp.h - ETCP Protocol Header (refactored based on etcp_protocol.txt) |
||||
#ifndef ETCP_H |
||||
#define ETCP_H |
||||
|
||||
#include "etcp_connections.h" |
||||
#include "secure_channel.h" |
||||
#include "../lib/ll_queue.h" |
||||
#include <stdint.h> |
||||
|
||||
#ifdef __cplusplus |
||||
extern "C" { |
||||
#endif |
||||
|
||||
// Forward declarations |
||||
struct UTUN_INSTANCE; |
||||
struct UASYNC; |
||||
|
||||
uint16_t get_current_timestamp(void); |
||||
uint64_t get_current_time_units(void); |
||||
|
||||
// ETCP packet section types (from protocol spec) |
||||
#define ETCP_SECTION_PAYLOAD 0x00 // Data payload |
||||
#define ETCP_SECTION_ACK 0x01 // ACK section |
||||
#define ETCP_SECTION_RETRANS 0x10 // Retransmission request base (0x10-0x2F) |
||||
#define ETCP_SECTION_TIMESTAMP 0x06 // Channel timestamp (example, adjust if needed) |
||||
#define ETCP_SECTION_MEAS_TS 0x07 // Measurement timestamp for bandwidth |
||||
#define ETCP_SECTION_MEAS_RESP 0x08 // Measurement response |
||||
|
||||
// Inflight packet states |
||||
#define INFLIGHT_STATE_WAIT_ACK 0 |
||||
#define INFLIGHT_STATE_WAIT_SEND 1 |
||||
#define INFLIGHT_INITIAL_HASH_SIZE 1024 |
||||
|
||||
// в этот список пакет добавляется когда перемещается из input_queue в input_send_q, при этом к пакету добавляется struct INFLIGHT_PACKET из inflight_pool. |
||||
// пакет полностью удаляется когда приходит ACK (либо conn_reset/close) |
||||
struct INFLIGHT_PACKET { |
||||
struct ll_entry ll; |
||||
struct ETCP_LINK* last_link; // Last sent link |
||||
struct ETCP_DGRAM* pkt; // Packet |
||||
struct INFLIGHT_PACKET* next_hash; // for INFLIGHT_HASH list. hash as seq%inflight_hash_size |
||||
uint64_t last_timestamp; // Last send timestamp |
||||
uint32_t seq; // packet seq (ID по документации) |
||||
uint8_t send_count; // Number of sends |
||||
uint8_t retrans_req_count; // Number of retrans requests |
||||
uint8_t state; // WAIT_ACK or WAIT_SEND |
||||
}; |
||||
|
||||
struct INFLIGHT_HASH { |
||||
struct INFLIGHT_PACKET* next; |
||||
}; |
||||
|
||||
// RX packet for assembly linked list |
||||
struct RX_PACKET { |
||||
struct RX_PACKET* next_hash; // for RX_HASH list. hash as seq%rx_hash_size |
||||
uint32_t seq; |
||||
uint16_t timestamp; |
||||
struct ETCP_DGRAM* pkt; // Packet |
||||
}; |
||||
|
||||
struct RX_HASH { |
||||
struct RX_PACKET* next; |
||||
}; |
||||
|
||||
// ETCP connection structure (refactored) |
||||
struct ETCP_CONN { |
||||
struct ETCP_CONN* next; |
||||
int mtu; |
||||
|
||||
struct UTUN_INSTANCE* instance; |
||||
|
||||
// Links (channels) - linked list |
||||
struct ETCP_LINK* links; |
||||
|
||||
// Crypto and state |
||||
struct secure_channel crypto_ctx; |
||||
|
||||
// Peer info |
||||
uint64_t peer_node_id; // Peer node ID |
||||
|
||||
// ============ Processing incoming data to be sent by ETCP |
||||
struct ll_queue* input_queue; // Incoming packets to send |
||||
|
||||
// Inflight очереди (2 шт) - пока пакет в статусе inflight - к нему прикрепляется struct INFLIGHT_PACKET |
||||
struct memory_pool* inflight_pool; // память для inflight очередей |
||||
struct ll_queue* input_send_q; // очередь на отправку (с элементами struct INFLIGHT_PACKET) |
||||
struct ll_queue* input_wait_ack; // очередь ожидающих подтверждение (с элементами struct INFLIGHT_PACKET) |
||||
struct INFLIGHT_HASH* inflight_hash; // хеш. динамически выделяется при инициализации. пока фикс размер. |
||||
uint32_t inflight_hash_size; |
||||
// todo: сделать хештаблицу для ускорения поиска пакета в очередях по ID (на 1024 элемента) |
||||
|
||||
void (*link_ready_for_send_fn)(struct ETCP_CONN*);// функцию которую должен вызвать драйвер линка при готовности линка принимать данные |
||||
|
||||
struct ll_queue* output_queue; // Assembled outgoing packets |
||||
|
||||
|
||||
// IDs and state |
||||
uint16_t next_tx_id; // Next TX ID |
||||
uint16_t last_rx_id; // Last received ID |
||||
uint16_t last_delivered_id; // Last delivered to output_queue |
||||
|
||||
// Metrics (RTT, jitter, etc.) |
||||
uint16_t rtt_last; |
||||
uint16_t rtt_avg_10; |
||||
uint16_t rtt_avg_100; |
||||
uint16_t jitter; |
||||
uint32_t bytes_sent_total; |
||||
uint32_t bytes_received_total; |
||||
uint32_t retransmissions_count; |
||||
|
||||
// Window and inflight management |
||||
uint32_t unacked_bytes; // Current inflight bytes |
||||
uint32_t window_size; // Receive window |
||||
uint32_t optimal_inflight; // Sum over links |
||||
|
||||
// Timers |
||||
void* retrans_timer; // Retrans check timer |
||||
void* ack_timer; // ACK send timer |
||||
|
||||
// Bandwidth measurement state |
||||
uint8_t burst_in_progress; // Burst transmission flag |
||||
uint16_t burst_start_id; // Start ID for burst |
||||
// ... (add more for meas_ts, meas_resp) |
||||
|
||||
// Statistics counters |
||||
uint32_t ack_packets_count; // Count of ACK packets received |
||||
uint16_t last_rx_ack_id; // Last ACK ID received |
||||
uint16_t rtt_history[10]; // RTT history for jitter calculation (RTT_HISTORY_SIZE=10) |
||||
uint8_t rtt_history_idx; // Current index in RTT history |
||||
uint32_t total_packets_sent; // Total packets sent counter |
||||
|
||||
// Flags |
||||
uint8_t wait_timeout_active; // In wait timeout state |
||||
}; |
||||
|
||||
// Functions |
||||
struct ETCP_CONN* etcp_connection_create(struct UTUN_INSTANCE* instance); |
||||
void etcp_connection_close(struct ETCP_CONN* etcp); |
||||
|
||||
void etcp_conn_reset(struct ETCP_CONN* etcp); |
||||
|
||||
// Отправка: используем api ll_queue для очереди ETCP_CONN.input_queue |
||||
// Прием: используем api ll_queue для очереди ETCP_CONN.output_queue |
||||
// для очередей используется формат |
||||
|
||||
// Input from etcp_connections (decrypted packet) |
||||
void etcp_conn_input(struct ETCP_DGRAM* pkt); |
||||
|
||||
// Request next packet for load balancer |
||||
struct ETCP_DGRAM* etcp_request_pkt(struct ETCP_CONN* etcp); |
||||
|
||||
#ifdef __cplusplus |
||||
} |
||||
#endif |
||||
|
||||
#endif // ETCP_H |
||||
@ -0,0 +1,142 @@
|
||||
// etcp.h - ETCP Protocol Header (refactored based on etcp_protocol.txt) |
||||
#ifndef ETCP_H |
||||
#define ETCP_H |
||||
|
||||
#include "etcp_connections.h" |
||||
#include "secure_channel.h" |
||||
#include "../lib/ll_queue.h" |
||||
#include <stdint.h> |
||||
|
||||
#ifdef __cplusplus |
||||
extern "C" { |
||||
#endif |
||||
|
||||
// Forward declarations |
||||
struct UTUN_INSTANCE; |
||||
struct UASYNC; |
||||
|
||||
uint16_t get_current_timestamp(void); |
||||
uint64_t get_current_time_units(void); |
||||
|
||||
// ETCP packet section types (from protocol spec) |
||||
#define ETCP_SECTION_PAYLOAD 0x00 // Data payload |
||||
#define ETCP_SECTION_ACK 0x01 // ACK section |
||||
#define ETCP_SECTION_RETRANS 0x10 // Retransmission request base (0x10-0x2F) |
||||
#define ETCP_SECTION_TIMESTAMP 0x06 // Channel timestamp (example, adjust if needed) |
||||
#define ETCP_SECTION_MEAS_TS 0x07 // Measurement timestamp for bandwidth |
||||
#define ETCP_SECTION_MEAS_RESP 0x08 // Measurement response |
||||
|
||||
// Inflight packet states |
||||
#define INFLIGHT_STATE_WAIT_ACK 0 |
||||
#define INFLIGHT_STATE_WAIT_SEND 1 |
||||
#define INFLIGHT_INITIAL_HASH_SIZE 1024 |
||||
|
||||
// в этот список пакет добавляется когда перемещается из input_queue в input_send_q, при этом к пакету добавляется struct INFLIGHT_PACKET из inflight_pool. |
||||
// пакет полностью удаляется когда приходит ACK (либо conn_reset/close) |
||||
struct INFLIGHT_PACKET { |
||||
struct ll_entry ll; |
||||
struct ETCP_LINK* last_link; // Last sent link |
||||
struct ETCP_DGRAM* pkt; // Packet |
||||
uint64_t last_timestamp; // Last send timestamp |
||||
uint32_t seq; // packet seq (ID по документации) |
||||
uint8_t send_count; // Number of sends |
||||
uint8_t retrans_req_count; // Number of retrans requests |
||||
uint8_t state; // WAIT_ACK or WAIT_SEND |
||||
}; |
||||
|
||||
// RX packet for assembly linked list |
||||
struct RX_PACKET { |
||||
uint32_t seq; |
||||
uint16_t timestamp; |
||||
struct ETCP_DGRAM* pkt; // Packet |
||||
}; |
||||
|
||||
// ETCP connection structure (refactored) |
||||
struct ETCP_CONN { |
||||
struct ETCP_CONN* next; |
||||
int mtu; |
||||
|
||||
struct UTUN_INSTANCE* instance; |
||||
|
||||
// Links (channels) - linked list |
||||
struct ETCP_LINK* links; |
||||
|
||||
// Crypto and state |
||||
struct secure_channel crypto_ctx; |
||||
|
||||
// Peer info |
||||
uint64_t peer_node_id; // Peer node ID |
||||
|
||||
// ============ Processing incoming data to be sent by ETCP |
||||
struct ll_queue* input_queue; // Incoming packets to send |
||||
|
||||
// Inflight очереди (2 шт) - пока пакет в статусе inflight - к нему прикрепляется struct INFLIGHT_PACKET |
||||
struct memory_pool* inflight_pool; // память для inflight очередей |
||||
struct ll_queue* input_send_q; // очередь на отправку (с элементами struct INFLIGHT_PACKET) |
||||
struct ll_queue* input_wait_ack; // очередь ожидающих подтверждение (с элементами struct INFLIGHT_PACKET) |
||||
|
||||
void (*link_ready_for_send_fn)(struct ETCP_CONN*);// функцию которую должен вызвать драйвер линка при готовности линка принимать данные |
||||
|
||||
struct ll_queue* output_queue; // Assembled outgoing packets |
||||
|
||||
|
||||
// IDs and state |
||||
uint16_t next_tx_id; // Next TX ID |
||||
uint16_t last_rx_id; // Last received ID |
||||
uint16_t last_delivered_id; // Last delivered to output_queue |
||||
|
||||
// Metrics (RTT, jitter, etc.) |
||||
uint16_t rtt_last; |
||||
uint16_t rtt_avg_10; |
||||
uint16_t rtt_avg_100; |
||||
uint16_t jitter; |
||||
uint32_t bytes_sent_total; |
||||
uint32_t bytes_received_total; |
||||
uint32_t retransmissions_count; |
||||
|
||||
// Window and inflight management |
||||
uint32_t unacked_bytes; // Current inflight bytes |
||||
uint32_t window_size; // Receive window |
||||
uint32_t optimal_inflight; // Sum over links |
||||
|
||||
// Timers |
||||
void* retrans_timer; // Retrans check timer |
||||
void* ack_timer; // ACK send timer |
||||
|
||||
// Bandwidth measurement state |
||||
uint8_t burst_in_progress; // Burst transmission flag |
||||
uint16_t burst_start_id; // Start ID for burst |
||||
// ... (add more for meas_ts, meas_resp) |
||||
|
||||
// Statistics counters |
||||
uint32_t ack_packets_count; // Count of ACK packets received |
||||
uint16_t last_rx_ack_id; // Last ACK ID received |
||||
uint16_t rtt_history[10]; // RTT history for jitter calculation (RTT_HISTORY_SIZE=10) |
||||
uint8_t rtt_history_idx; // Current index in RTT history |
||||
uint32_t total_packets_sent; // Total packets sent counter |
||||
|
||||
// Flags |
||||
uint8_t wait_timeout_active; // In wait timeout state |
||||
}; |
||||
|
||||
// Functions |
||||
struct ETCP_CONN* etcp_connection_create(struct UTUN_INSTANCE* instance); |
||||
void etcp_connection_close(struct ETCP_CONN* etcp); |
||||
|
||||
void etcp_conn_reset(struct ETCP_CONN* etcp); |
||||
|
||||
// Отправка: используем api ll_queue для очереди ETCP_CONN.input_queue |
||||
// Прием: используем api ll_queue для очереди ETCP_CONN.output_queue |
||||
// для очередей используется формат |
||||
|
||||
// Input from etcp_connections (decrypted packet) |
||||
void etcp_conn_input(struct ETCP_DGRAM* pkt); |
||||
|
||||
// Request next packet for load balancer |
||||
struct ETCP_DGRAM* etcp_request_pkt(struct ETCP_CONN* etcp); |
||||
|
||||
#ifdef __cplusplus |
||||
} |
||||
#endif |
||||
|
||||
#endif // ETCP_H |
||||
@ -1,57 +1,96 @@
|
||||
# Tests Makefile.am for utun - cleaned and working version
|
||||
# Tests Makefile.am for utun - all tests with new ll_queue library
|
||||
|
||||
# Essential working tests
|
||||
# All available tests with new ll_queue library
|
||||
check_PROGRAMS = test_etcp_crypto$(EXEEXT) \
|
||||
test_crypto$(EXEEXT) \
|
||||
test_etcp_two_instances$(EXEEXT) \
|
||||
test_etcp_traffic_flow$(EXEEXT) \
|
||||
test_etcp_simple_traffic$(EXEEXT) \
|
||||
test_etcp_minimal$(EXEEXT) \
|
||||
test_ll_queue_pos$(EXEEXT) \
|
||||
test_ll_queue_unified$(EXEEXT)
|
||||
test_ll_queue$(EXEEXT) \
|
||||
test_ecc_encrypt$(EXEEXT) \
|
||||
test_intensive_memory_pool$(EXEEXT) \
|
||||
test_memory_pool_and_config$(EXEEXT) \
|
||||
test_packet_dump$(EXEEXT) \
|
||||
test_u_async_comprehensive$(EXEEXT) \
|
||||
test_u_async_performance$(EXEEXT) \
|
||||
test_debug_new$(EXEEXT) \
|
||||
test_debug_categories$(EXEEXT) \
|
||||
test_config_debug$(EXEEXT)
|
||||
|
||||
# Basic includes
|
||||
AM_CFLAGS = -I$(top_srcdir)/src -I$(top_srcdir)/lib -I$(top_srcdir)/tinycrypt/lib/include -I$(top_srcdir)/tinycrypt/lib/source
|
||||
|
||||
# ETCP crypto test - main functionality
|
||||
# ETCP tests
|
||||
test_etcp_crypto_SOURCES = test_etcp_crypto.c
|
||||
test_etcp_crypto_CFLAGS = -I$(top_srcdir)/src -I$(top_srcdir)/lib -I$(top_srcdir)/tinycrypt/lib/include -I$(top_srcdir)/tinycrypt/lib/source
|
||||
test_etcp_crypto_LDADD = $(top_builddir)/src/utun-secure_channel.o $(top_builddir)/src/utun-crc32.o $(top_builddir)/tinycrypt/lib/source/utun-aes_encrypt.o $(top_builddir)/tinycrypt/lib/source/utun-aes_decrypt.o $(top_builddir)/tinycrypt/lib/source/utun-ccm_mode.o $(top_builddir)/tinycrypt/lib/source/utun-cmac_mode.o $(top_builddir)/tinycrypt/lib/source/utun-ctr_mode.o $(top_builddir)/tinycrypt/lib/source/utun-ecc.o $(top_builddir)/tinycrypt/lib/source/utun-ecc_dh.o $(top_builddir)/tinycrypt/lib/source/utun-ecc_dsa.o $(top_builddir)/tinycrypt/lib/source/utun-sha256.o $(top_builddir)/tinycrypt/lib/source/utun-ecc_platform_specific.o $(top_builddir)/tinycrypt/lib/source/utun-utils.o $(top_builddir)/lib/libuasync.a -lpthread -lcrypto
|
||||
|
||||
# Basic crypto test
|
||||
test_crypto_SOURCES = test_crypto.c
|
||||
test_crypto_CFLAGS = -I$(top_srcdir)/tinycrypt/lib/include -I$(top_srcdir)/tinycrypt/lib/source
|
||||
test_crypto_LDADD = $(top_builddir)/tinycrypt/lib/source/utun-aes_encrypt.o $(top_builddir)/tinycrypt/lib/source/utun-aes_decrypt.o $(top_builddir)/tinycrypt/lib/source/utun-ccm_mode.o $(top_builddir)/tinycrypt/lib/source/utun-cmac_mode.o $(top_builddir)/tinycrypt/lib/source/utun-ctr_mode.o $(top_builddir)/tinycrypt/lib/source/utun-sha256.o $(top_builddir)/tinycrypt/lib/source/utun-utils.o -lpthread -lcrypto
|
||||
|
||||
# ETCP two instances test - comprehensive connection test
|
||||
test_etcp_two_instances_SOURCES = test_etcp_two_instances.c
|
||||
test_etcp_two_instances_CFLAGS = -I$(top_srcdir)/src -I$(top_srcdir)/lib -I$(top_srcdir)/tinycrypt/lib/include -I$(top_srcdir)/tinycrypt/lib/source
|
||||
test_etcp_two_instances_LDADD = $(top_builddir)/src/utun-config_parser.o $(top_builddir)/src/utun-config_updater.o $(top_builddir)/src/utun-crc32.o $(top_builddir)/src/utun-etcp.o $(top_builddir)/src/utun-etcp_connections.o $(top_builddir)/src/utun-etcp_loadbalancer.o $(top_builddir)/src/utun-secure_channel.o $(top_builddir)/src/utun-routing.o $(top_builddir)/src/utun-tun_if.o $(top_builddir)/src/utun-utun_instance.o $(top_builddir)/tinycrypt/lib/source/utun-aes_encrypt.o $(top_builddir)/tinycrypt/lib/source/utun-aes_decrypt.o $(top_builddir)/tinycrypt/lib/source/utun-ccm_mode.o $(top_builddir)/tinycrypt/lib/source/utun-cmac_mode.o $(top_builddir)/tinycrypt/lib/source/utun-ctr_mode.o $(top_builddir)/tinycrypt/lib/source/utun-ecc.o $(top_builddir)/tinycrypt/lib/source/utun-ecc_dh.o $(top_builddir)/tinycrypt/lib/source/utun-ecc_dsa.o $(top_builddir)/tinycrypt/lib/source/utun-sha256.o $(top_builddir)/tinycrypt/lib/source/utun-ecc_platform_specific.o $(top_builddir)/tinycrypt/lib/source/utun-utils.o $(top_builddir)/lib/libuasync.a -lpthread -lcrypto
|
||||
|
||||
# ETCP simple traffic test - TUN-less traffic analysis
|
||||
test_etcp_simple_traffic_SOURCES = test_etcp_simple_traffic.c
|
||||
test_etcp_simple_traffic_CFLAGS = -I$(top_srcdir)/src -I$(top_srcdir)/lib -I$(top_srcdir)/tinycrypt/lib/include -I$(top_srcdir)/tinycrypt/lib/source
|
||||
test_etcp_simple_traffic_LDADD = $(top_builddir)/src/utun-config_parser.o $(top_builddir)/src/utun-config_updater.o $(top_builddir)/src/utun-crc32.o $(top_builddir)/src/utun-etcp.o $(top_builddir)/src/utun-etcp_connections.o $(top_builddir)/src/utun-etcp_loadbalancer.o $(top_builddir)/src/utun-secure_channel.o $(top_builddir)/src/utun-routing.o $(top_builddir)/src/utun-tun_if.o $(top_builddir)/src/utun-utun_instance.o $(top_builddir)/tinycrypt/lib/source/utun-aes_encrypt.o $(top_builddir)/tinycrypt/lib/source/utun-aes_decrypt.o $(top_builddir)/tinycrypt/lib/source/utun-ccm_mode.o $(top_builddir)/tinycrypt/lib/source/utun-cmac_mode.o $(top_builddir)/tinycrypt/lib/source/utun-ctr_mode.o $(top_builddir)/tinycrypt/lib/source/utun-ecc.o $(top_builddir)/tinycrypt/lib/source/utun-ecc_dh.o $(top_builddir)/tinycrypt/lib/source/utun-ecc_dsa.o $(top_builddir)/tinycrypt/lib/source/utun-sha256.o $(top_builddir)/tinycrypt/lib/source/utun-ecc_platform_specific.o $(top_builddir)/tinycrypt/lib/source/utun-utils.o $(top_builddir)/lib/libuasync.a -lpthread -lcrypto
|
||||
|
||||
# ETCP minimal test - basic packet analysis
|
||||
test_etcp_minimal_SOURCES = test_etcp_minimal.c
|
||||
test_etcp_minimal_CFLAGS = -I$(top_srcdir)/src -I$(top_srcdir)/lib -I$(top_srcdir)/tinycrypt/lib/include -I$(top_srcdir)/tinycrypt/lib/source
|
||||
test_etcp_minimal_LDADD = $(top_builddir)/src/utun-etcp.o $(top_builddir)/src/utun-etcp_connections.o $(top_builddir)/src/utun-etcp_loadbalancer.o $(top_builddir)/src/utun-secure_channel.o $(top_builddir)/src/utun-crc32.o $(top_builddir)/tinycrypt/lib/source/utun-aes_encrypt.o $(top_builddir)/tinycrypt/lib/source/utun-aes_decrypt.o $(top_builddir)/tinycrypt/lib/source/utun-ccm_mode.o $(top_builddir)/tinycrypt/lib/source/utun-cmac_mode.o $(top_builddir)/tinycrypt/lib/source/utun-ctr_mode.o $(top_builddir)/tinycrypt/lib/source/utun-ecc.o $(top_builddir)/tinycrypt/lib/source/utun-ecc_dh.o $(top_builddir)/tinycrypt/lib/source/utun-ecc_dsa.o $(top_builddir)/tinycrypt/lib/source/utun-sha256.o $(top_builddir)/tinycrypt/lib/source/utun-ecc_platform_specific.o $(top_builddir)/tinycrypt/lib/source/utun-utils.o $(top_builddir)/lib/libuasync.a -lpthread -lcrypto
|
||||
|
||||
# ETCP traffic flow test - TUN-less traffic analysis
|
||||
test_etcp_traffic_flow_SOURCES = test_etcp_traffic_flow.c
|
||||
test_etcp_traffic_flow_CFLAGS = -I$(top_srcdir)/src -I$(top_srcdir)/lib -I$(top_srcdir)/tinycrypt/lib/include -I$(top_srcdir)/tinycrypt/lib/source
|
||||
test_etcp_traffic_flow_LDADD = $(top_builddir)/src/utun-config_parser.o $(top_builddir)/src/utun-config_updater.o $(top_builddir)/src/utun-crc32.o $(top_builddir)/src/utun-etcp.o $(top_builddir)/src/utun-etcp_connections.o $(top_builddir)/src/utun-etcp_loadbalancer.o $(top_builddir)/src/utun-secure_channel.o $(top_builddir)/src/utun-routing.o $(top_builddir)/src/utun-tun_if.o $(top_builddir)/src/utun-utun_instance.o $(top_builddir)/tinycrypt/lib/source/utun-aes_encrypt.o $(top_builddir)/tinycrypt/lib/source/utun-aes_decrypt.o $(top_builddir)/tinycrypt/lib/source/utun-ccm_mode.o $(top_builddir)/tinycrypt/lib/source/utun-cmac_mode.o $(top_builddir)/tinycrypt/lib/source/utun-ctr_mode.o $(top_builddir)/tinycrypt/lib/source/utun-ecc.o $(top_builddir)/tinycrypt/lib/source/utun-ecc_dh.o $(top_builddir)/tinycrypt/lib/source/utun-ecc_dsa.o $(top_builddir)/tinycrypt/lib/source/utun-sha256.o $(top_builddir)/tinycrypt/lib/source/utun-ecc_platform_specific.o $(top_builddir)/tinycrypt/lib/source/utun-utils.o $(top_builddir)/lib/libuasync.a -lpthread -lcrypto
|
||||
# Basic crypto test
|
||||
test_crypto_SOURCES = test_crypto.c
|
||||
test_crypto_CFLAGS = -I$(top_srcdir)/tinycrypt/lib/include -I$(top_srcdir)/tinycrypt/lib/source
|
||||
test_crypto_LDADD = $(top_builddir)/tinycrypt/lib/source/utun-aes_encrypt.o $(top_builddir)/tinycrypt/lib/source/utun-aes_decrypt.o $(top_builddir)/tinycrypt/lib/source/utun-ccm_mode.o $(top_builddir)/tinycrypt/lib/source/utun-cmac_mode.o $(top_builddir)/tinycrypt/lib/source/utun-ctr_mode.o $(top_builddir)/tinycrypt/lib/source/utun-sha256.o $(top_builddir)/tinycrypt/lib/source/utun-utils.o -lpthread -lcrypto
|
||||
|
||||
# New comprehensive ll_queue test with current API
|
||||
test_ll_queue_SOURCES = test_ll_queue.c
|
||||
test_ll_queue_CFLAGS = -I$(top_srcdir)/src -I$(top_srcdir)/lib
|
||||
test_ll_queue_LDADD = $(top_builddir)/lib/libuasync.a -lpthread -lcrypto
|
||||
|
||||
# ECC encryption test
|
||||
test_ecc_encrypt_SOURCES = test_ecc_encrypt.c
|
||||
test_ecc_encrypt_CFLAGS = -I$(top_srcdir)/tinycrypt/lib/include -I$(top_srcdir)/tinycrypt/lib/source
|
||||
test_ecc_encrypt_LDADD = $(top_builddir)/src/utun-secure_channel.o $(top_builddir)/src/utun-crc32.o $(top_builddir)/tinycrypt/lib/source/utun-aes_encrypt.o $(top_builddir)/tinycrypt/lib/source/utun-aes_decrypt.o $(top_builddir)/tinycrypt/lib/source/utun-ccm_mode.o $(top_builddir)/tinycrypt/lib/source/utun-cmac_mode.o $(top_builddir)/tinycrypt/lib/source/utun-ctr_mode.o $(top_builddir)/tinycrypt/lib/source/utun-ecc.o $(top_builddir)/tinycrypt/lib/source/utun-ecc_dh.o $(top_builddir)/tinycrypt/lib/source/utun-ecc_dsa.o $(top_builddir)/tinycrypt/lib/source/utun-sha256.o $(top_builddir)/tinycrypt/lib/source/utun-ecc_platform_specific.o $(top_builddir)/tinycrypt/lib/source/utun-utils.o $(top_builddir)/lib/libuasync.a -lpthread -lcrypto
|
||||
|
||||
# Intensive memory pool test
|
||||
test_intensive_memory_pool_SOURCES = test_intensive_memory_pool.c
|
||||
test_intensive_memory_pool_CFLAGS = -I$(top_srcdir)/src -I$(top_srcdir)/lib
|
||||
test_intensive_memory_pool_LDADD = $(top_builddir)/lib/libuasync.a -lpthread -lcrypto
|
||||
|
||||
# Memory pool and config test
|
||||
test_memory_pool_and_config_SOURCES = test_memory_pool_and_config.c
|
||||
test_memory_pool_and_config_CFLAGS = -I$(top_srcdir)/src -I$(top_srcdir)/lib
|
||||
test_memory_pool_and_config_LDADD = $(top_builddir)/lib/libuasync.a -lpthread -lcrypto
|
||||
|
||||
# Packet dump test
|
||||
test_packet_dump_SOURCES = test_packet_dump.c
|
||||
test_packet_dump_CFLAGS = -I$(top_srcdir)/src -I$(top_srcdir)/lib
|
||||
test_packet_dump_LDADD = $(top_builddir)/lib/libuasync.a -lpthread -lcrypto
|
||||
|
||||
# UASYNC comprehensive test
|
||||
test_u_async_comprehensive_SOURCES = test_u_async_comprehensive.c
|
||||
test_u_async_comprehensive_CFLAGS = -I$(top_srcdir)/src -I$(top_srcdir)/lib
|
||||
test_u_async_comprehensive_LDADD = $(top_builddir)/lib/libuasync.a -lpthread -lcrypto
|
||||
|
||||
# UASYNC performance test
|
||||
test_u_async_performance_SOURCES = test_u_async_performance.c
|
||||
test_u_async_performance_CFLAGS = -I$(top_srcdir)/src -I$(top_srcdir)/lib
|
||||
test_u_async_performance_LDADD = $(top_builddir)/lib/libuasync.a -lpthread -lcrypto
|
||||
|
||||
# Debug system test
|
||||
test_debug_new_SOURCES = test_debug_new.c
|
||||
test_debug_new_CFLAGS = -I$(top_srcdir)/src -I$(top_srcdir)/lib
|
||||
test_debug_new_LDADD = $(top_builddir)/lib/libuasync.a -lpthread -lcrypto
|
||||
|
||||
# LL queue position test
|
||||
test_ll_queue_pos_SOURCES = test_ll_queue_pos.c
|
||||
test_ll_queue_pos_CFLAGS = -I$(top_srcdir)/src -I$(top_srcdir)/lib
|
||||
test_ll_queue_pos_LDADD = $(top_builddir)/lib/libuasync.a -lpthread -lcrypto
|
||||
# Debug categories test
|
||||
test_debug_categories_SOURCES = test_debug_categories.c
|
||||
test_debug_categories_CFLAGS = -I$(top_srcdir)/src -I$(top_srcdir)/lib
|
||||
test_debug_categories_LDADD = $(top_builddir)/lib/libuasync.a -lpthread -lcrypto
|
||||
|
||||
# Unified comprehensive ll_queue test
|
||||
test_ll_queue_unified_SOURCES = test_ll_queue_unified.c
|
||||
test_ll_queue_unified_CFLAGS = -I$(top_srcdir)/src -I$(top_srcdir)/lib
|
||||
test_ll_queue_unified_LDADD = $(top_builddir)/lib/libuasync.a -lpthread -lcrypto
|
||||
# Config debug test
|
||||
test_config_debug_SOURCES = test_config_debug.c
|
||||
test_config_debug_CFLAGS = -I$(top_srcdir)/src -I$(top_srcdir)/lib
|
||||
test_config_debug_LDADD = $(top_builddir)/src/utun-config_parser.o $(top_builddir)/lib/libuasync.a -lpthread -lcrypto
|
||||
|
||||
# Register tests
|
||||
TESTS = $(check_PROGRAMS)
|
||||
@ -0,0 +1,38 @@
|
||||
--- test_etcp_simple_traffic.c.backup
|
||||
+++ test_etcp_simple_traffic.c
|
||||
@@ -172,19 +172,25 @@
|
||||
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "SIMULATE RX: inst=%s, len=%u",
|
||||
inst->is_server ? "server" : "client", len);
|
||||
|
||||
- // Create a mock ETCP_DGRAM for processing
|
||||
- struct ETCP_DGRAM mock_dgram;
|
||||
- memset(&mock_dgram, 0, sizeof(mock_dgram));
|
||||
- mock_dgram.link = NULL; // Will be set by connection processing
|
||||
- mock_dgram.data_len = len > 1500 ? 1500 : len;
|
||||
- mock_dgram.timestamp = get_current_timestamp();
|
||||
- if (mock_dgram.data_len > 0) {
|
||||
- memcpy(mock_dgram.data, data, mock_dgram.data_len);
|
||||
+ // Create a mock ETCP_DGRAM for processing with proper allocation
|
||||
+ size_t dgram_size = sizeof(struct ETCP_DGRAM) + (len > 1500 ? 1500 : len);
|
||||
+ struct ETCP_DGRAM* mock_dgram = malloc(dgram_size);
|
||||
+ if (!mock_dgram) {
|
||||
+ DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "Failed to allocate mock dgram");
|
||||
+ return;
|
||||
+ }
|
||||
+ memset(mock_dgram, 0, dgram_size);
|
||||
+ mock_dgram->link = NULL; // Will be set by connection processing
|
||||
+ mock_dgram->data_len = len > 1500 ? 1500 : len;
|
||||
+ mock_dgram->timestamp = get_current_timestamp();
|
||||
+ if (mock_dgram->data_len > 0) {
|
||||
+ memcpy(mock_dgram->data, data, mock_dgram->data_len);
|
||||
}
|
||||
|
||||
// Process through ETCP input
|
||||
- etcp_conn_input(&mock_dgram);
|
||||
+ etcp_conn_input(mock_dgram);
|
||||
+
|
||||
+ // Free the allocated memory
|
||||
+ free(mock_dgram);
|
||||
}
|
||||
|
||||
// Simulate packet transmission for analysis
|
||||
Binary file not shown.
@ -0,0 +1,234 @@
|
||||
// Copy the original test file but add debugging
|
||||
#include <stdio.h> |
||||
#include <stdlib.h> |
||||
#include <string.h> |
||||
#include <assert.h> |
||||
#include <unistd.h> |
||||
#include <sys/socket.h> |
||||
#include <netinet/in.h> |
||||
#include <arpa/inet.h> |
||||
#include <fcntl.h> |
||||
#include <errno.h> |
||||
|
||||
#include "u_async.h" |
||||
#include "timeout_heap.h" |
||||
|
||||
/* Test statistics */ |
||||
static struct { |
||||
int tests_run; |
||||
int tests_passed; |
||||
int tests_failed; |
||||
|
||||
/* Timer statistics */ |
||||
int timer_callbacks; |
||||
int timer_cancellations; |
||||
int immediate_timeouts; |
||||
|
||||
/* Socket statistics */ |
||||
int socket_events; |
||||
int socket_errors; |
||||
|
||||
/* Error statistics */ |
||||
int memory_allocation_errors; |
||||
int invalid_parameter_errors; |
||||
int race_condition_errors; |
||||
} test_stats = {0}; |
||||
|
||||
/* Test result tracking */ |
||||
#define TEST_START(name) do { \ |
||||
printf("TEST: %s... \n", name); \
|
||||
printf(" immediate_timeouts before: %d\n", test_stats.immediate_timeouts); \
|
||||
test_stats.tests_run++; \
|
||||
} while(0) |
||||
|
||||
#define TEST_PASS() do { \ |
||||
printf(" immediate_timeouts after: %d\n", test_stats.immediate_timeouts); \
|
||||
printf("PASS\n"); \
|
||||
test_stats.tests_passed++; \
|
||||
} while(0) |
||||
|
||||
#define TEST_FAIL(msg) do { \ |
||||
printf(" immediate_timeouts after: %d\n", test_stats.immediate_timeouts); \
|
||||
printf("FAIL: %s\n", msg); \
|
||||
test_stats.tests_failed++; \
|
||||
} while(0) |
||||
|
||||
#define ASSERT_EQ(a, b, msg) do { \ |
||||
if ((a) != (b)) { \
|
||||
TEST_FAIL(msg); \
|
||||
printf(" Expected: %ld, Got: %ld\n", (long)(b), (long)(a)); \
|
||||
return; \
|
||||
} \
|
||||
} while(0) |
||||
|
||||
#define ASSERT_NOT_NULL(ptr, msg) do { \ |
||||
if ((ptr) == NULL) { \
|
||||
TEST_FAIL(msg); \
|
||||
return; \
|
||||
} \
|
||||
} while(0) |
||||
|
||||
/* Test context for callbacks */ |
||||
typedef struct { |
||||
int callback_count; |
||||
int expected_count; |
||||
int callback_arg; |
||||
int timeout_ms; |
||||
uasync_t* ua; |
||||
void* timer_id; |
||||
} test_context_t; |
||||
|
||||
/* Timer callback for testing */ |
||||
static void test_timer_callback(void* arg) { |
||||
test_context_t* ctx = (test_context_t*)arg; |
||||
ctx->callback_count++; |
||||
test_stats.timer_callbacks++; |
||||
|
||||
if (ctx->timeout_ms == 0) { |
||||
test_stats.immediate_timeouts++; |
||||
printf(" IMMEDIATE TIMEOUT: immediate_timeouts now = %d\n", test_stats.immediate_timeouts); |
||||
} |
||||
} |
||||
|
||||
/* Test 1: Basic timer functionality */ |
||||
static void test_basic_timers(void) { |
||||
TEST_START("Basic timer functionality"); |
||||
|
||||
uasync_t* ua = uasync_create(); |
||||
ASSERT_NOT_NULL(ua, "Failed to create uasync instance"); |
||||
|
||||
test_context_t ctx = {0}; |
||||
ctx.expected_count = 3; |
||||
ctx.timeout_ms = 10; // Non-zero to avoid immediate timeout counting
|
||||
|
||||
/* Set multiple timers with different timeouts */ |
||||
void* timer1 = uasync_set_timeout(ua, 10, &ctx, test_timer_callback); /* 1ms */ |
||||
void* timer2 = uasync_set_timeout(ua, 20, &ctx, test_timer_callback); /* 2ms */ |
||||
void* timer3 = uasync_set_timeout(ua, 30, &ctx, test_timer_callback); /* 3ms */ |
||||
|
||||
ASSERT_NOT_NULL(timer1, "Failed to set timer 1"); |
||||
ASSERT_NOT_NULL(timer2, "Failed to set timer 2"); |
||||
ASSERT_NOT_NULL(timer3, "Failed to set timer 3"); |
||||
|
||||
/* Poll and verify timers fire in order */ |
||||
int poll_count = 0; |
||||
while (ctx.callback_count < ctx.expected_count && poll_count < 100) { |
||||
uasync_poll(ua, 10); /* 1ms poll */ |
||||
poll_count++; |
||||
} |
||||
|
||||
ASSERT_EQ(ctx.callback_count, ctx.expected_count, "Not all timers fired"); |
||||
|
||||
/* Cleanup */ |
||||
uasync_destroy(ua, 0); |
||||
TEST_PASS(); |
||||
} |
||||
|
||||
/* Test 2: Timer cancellation race conditions */ |
||||
static void test_timer_cancellation_races(void) { |
||||
TEST_START("Timer cancellation race conditions"); |
||||
|
||||
uasync_t* ua = uasync_create(); |
||||
ASSERT_NOT_NULL(ua, "Failed to create uasync instance"); |
||||
|
||||
test_context_t ctx = {0}; |
||||
ctx.expected_count = 2; |
||||
ctx.timeout_ms = 5; // Non-zero to avoid immediate timeout counting
|
||||
|
||||
/* Create timers that will be cancelled at different stages */ |
||||
void* timer1 = uasync_set_timeout(ua, 5, &ctx, test_timer_callback); /* 0.5ms */ |
||||
void* timer2 = uasync_set_timeout(ua, 50, &ctx, test_timer_callback); /* 5ms */ |
||||
void* timer3 = uasync_set_timeout(ua, 100, &ctx, test_timer_callback); /* 10ms */ |
||||
|
||||
ASSERT_NOT_NULL(timer1, "Failed to set timer 1"); |
||||
ASSERT_NOT_NULL(timer2, "Failed to set timer 2"); |
||||
ASSERT_NOT_NULL(timer3, "Failed to set timer 3"); |
||||
|
||||
/* Cancel timer1 immediately (before it fires) */ |
||||
err_t cancel_result = uasync_cancel_timeout(ua, timer1); |
||||
ASSERT_EQ(cancel_result, ERR_OK, "Failed to cancel timer 1"); |
||||
test_stats.timer_cancellations++; |
||||
|
||||
/* Poll briefly - timer1 should not fire, others should */ |
||||
uasync_poll(ua, 10); /* 1ms */ |
||||
|
||||
/* Cancel timer2 while it might be firing */ |
||||
cancel_result = uasync_cancel_timeout(ua, timer2); |
||||
/* Result could be ERR_OK or ERR_FAIL depending on timing */ |
||||
|
||||
/* Continue polling */ |
||||
int poll_count = 0; |
||||
while (ctx.callback_count < 2 && poll_count < 50) { |
||||
uasync_poll(ua, 10); |
||||
poll_count++; |
||||
} |
||||
|
||||
/* Verify we got expected callbacks (timer3 + possibly timer2) */ |
||||
if (ctx.callback_count < 1 || ctx.callback_count > 2) { |
||||
TEST_FAIL("Wrong number of timers fired"); |
||||
return; |
||||
} |
||||
|
||||
/* Cleanup remaining timer */ |
||||
if (timer3) { |
||||
uasync_cancel_timeout(ua, timer3); |
||||
timer3 = NULL; |
||||
} |
||||
|
||||
uasync_destroy(ua, 0); |
||||
TEST_PASS(); |
||||
} |
||||
|
||||
/* Test 3: Immediate timeout handling */ |
||||
static void test_immediate_timeouts(void) { |
||||
TEST_START("Immediate timeout handling"); |
||||
|
||||
uasync_t* ua = uasync_create(); |
||||
ASSERT_NOT_NULL(ua, "Failed to create uasync instance"); |
||||
|
||||
test_context_t ctx = {0}; |
||||
ctx.expected_count = 5; |
||||
ctx.timeout_ms = 0; // This should trigger immediate timeout counting
|
||||
|
||||
/* Set multiple immediate timeouts (0ms) */ |
||||
for (int i = 0; i < 5; i++) { |
||||
void* timer = uasync_set_timeout(ua, 0, &ctx, test_timer_callback); |
||||
ASSERT_NOT_NULL(timer, "Failed to set immediate timer"); |
||||
} |
||||
|
||||
/* Immediate timeouts should fire during next poll */ |
||||
ASSERT_EQ(ctx.callback_count, 0, "Callbacks fired too early"); |
||||
|
||||
printf(" About to call uasync_poll, immediate_timeouts = %d\n", test_stats.immediate_timeouts); |
||||
uasync_poll(ua, 1); /* Minimal poll */ |
||||
printf(" After uasync_poll, immediate_timeouts = %d\n", test_stats.immediate_timeouts); |
||||
|
||||
ASSERT_EQ(ctx.callback_count, ctx.expected_count, "Immediate timeouts didn't fire correctly"); |
||||
ASSERT_EQ(test_stats.immediate_timeouts, 5, "Immediate timeout counter incorrect"); |
||||
|
||||
uasync_destroy(ua, 0); |
||||
TEST_PASS(); |
||||
} |
||||
|
||||
int main(void) { |
||||
printf("=== lib Comprehensive Unit Tests ===\n"); |
||||
printf("Testing race conditions, memory management, and error handling\n\n"); |
||||
printf("Initial immediate_timeouts: %d\n", test_stats.immediate_timeouts); |
||||
|
||||
/* Run all tests */ |
||||
test_basic_timers(); |
||||
printf("After basic_timers: immediate_timeouts = %d\n", test_stats.immediate_timeouts); |
||||
|
||||
test_timer_cancellation_races(); |
||||
printf("After timer_cancellation_races: immediate_timeouts = %d\n", test_stats.immediate_timeouts); |
||||
|
||||
test_immediate_timeouts(); |
||||
printf("After immediate_timeouts: immediate_timeouts = %d\n", test_stats.immediate_timeouts); |
||||
|
||||
/* Print final statistics */ |
||||
printf("\n=== Final Statistics ===\n"); |
||||
printf("Timer callbacks: %d\n", test_stats.timer_callbacks); |
||||
printf("Immediate timeouts: %d\n", test_stats.immediate_timeouts); |
||||
|
||||
return (test_stats.tests_failed > 0) ? 1 : 0; |
||||
} |
||||
Binary file not shown.
Binary file not shown.
@ -0,0 +1,86 @@
|
||||
#include "u_async.h" |
||||
#include <stdio.h> |
||||
|
||||
/* Test statistics */ |
||||
static struct { |
||||
int timer_callbacks; |
||||
int immediate_timeouts; |
||||
} test_stats = {0}; |
||||
|
||||
typedef struct { |
||||
int callback_count; |
||||
int timeout_ms; |
||||
} test_context_t; |
||||
|
||||
/* Timer callback for testing */ |
||||
static void test_timer_callback(void* arg) { |
||||
test_context_t* ctx = (test_context_t*)arg; |
||||
ctx->callback_count++; |
||||
test_stats.timer_callbacks++; |
||||
|
||||
printf("Callback fired: ctx->timeout_ms=%d, immediate_timeouts before=%d", ctx->timeout_ms, test_stats.immediate_timeouts); |
||||
|
||||
if (ctx->timeout_ms == 0) { |
||||
test_stats.immediate_timeouts++; |
||||
printf(" -> INCREMENTED to %d\n", test_stats.immediate_timeouts); |
||||
} else { |
||||
printf(" -> NOT incremented\n"); |
||||
} |
||||
} |
||||
|
||||
int main() { |
||||
printf("=== Test 1: Basic timers (non-zero timeouts) ===\n"); |
||||
printf("Before test 1: immediate_timeouts = %d\n", test_stats.immediate_timeouts); |
||||
|
||||
uasync_t* ua1 = uasync_create(); |
||||
test_context_t ctx1 = {.timeout_ms = 10}; |
||||
|
||||
for (int i = 0; i < 3; i++) { |
||||
uasync_set_timeout(ua1, 10 + i*10, &ctx1, test_timer_callback); |
||||
} |
||||
|
||||
int polls = 0; |
||||
while (ctx1.callback_count < 3 && polls < 10) { |
||||
uasync_poll(ua1, 10); |
||||
polls++; |
||||
} |
||||
|
||||
printf("After test 1: immediate_timeouts = %d\n", test_stats.immediate_timeouts); |
||||
uasync_destroy(ua1); |
||||
|
||||
printf("\n=== Test 2: Immediate timeouts (zero timeouts) ===\n"); |
||||
printf("Before test 2: immediate_timeouts = %d\n", test_stats.immediate_timeouts); |
||||
|
||||
uasync_t* ua2 = uasync_create(); |
||||
test_context_t ctx2 = {.timeout_ms = 0}; |
||||
|
||||
for (int i = 0; i < 5; i++) { |
||||
uasync_set_timeout(ua2, 0, &ctx2, test_timer_callback); |
||||
} |
||||
|
||||
printf("Before poll: ctx2.callback_count = %d, immediate_timeouts = %d\n", ctx2.callback_count, test_stats.immediate_timeouts); |
||||
|
||||
uasync_poll(ua2, 1); |
||||
|
||||
printf("After poll: ctx2.callback_count = %d, immediate_timeouts = %d\n", ctx2.callback_count, test_stats.immediate_timeouts); |
||||
|
||||
uasync_destroy(ua2); |
||||
|
||||
printf("\n=== Test 3: Another immediate timeout test ===\n"); |
||||
printf("Before test 3: immediate_timeouts = %d\n", test_stats.immediate_timeouts); |
||||
|
||||
uasync_t* ua3 = uasync_create(); |
||||
test_context_t ctx3 = {.timeout_ms = 0}; |
||||
|
||||
for (int i = 0; i < 2; i++) { |
||||
uasync_set_timeout(ua3, 0, &ctx3, test_timer_callback); |
||||
} |
||||
|
||||
uasync_poll(ua3, 1); |
||||
|
||||
printf("After test 3: immediate_timeouts = %d\n", test_stats.immediate_timeouts); |
||||
|
||||
uasync_destroy(ua3); |
||||
|
||||
return 0; |
||||
} |
||||
@ -0,0 +1,24 @@
|
||||
--- test_etcp_simple_traffic.c.backup
|
||||
+++ test_etcp_simple_traffic.c
|
||||
@@ -243,12 +243,19 @@
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_ETCP, "Parsing crypto keys: priv_len=%zu, pub_len=%zu",
|
||||
strlen(priv_key_hex), strlen(pub_key_hex));
|
||||
|
||||
+ // Validate key string lengths to prevent buffer overflow
|
||||
+ if (strlen(priv_key_hex) != 64 || strlen(pub_key_hex) != 128) {
|
||||
+ DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "Invalid key string lengths: priv=%zu, pub=%zu",
|
||||
+ strlen(priv_key_hex), strlen(pub_key_hex));
|
||||
+ return NULL;
|
||||
+ }
|
||||
+
|
||||
// Parse keys
|
||||
for (int i = 0; i < 32; i++) {
|
||||
if (sscanf(&priv_key_hex[i*2], "%2hhx", &inst->instance->my_keys.private_key[i]) != 1) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "Failed to parse private key byte %d", i);
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < 64; i++) {
|
||||
if (sscanf(&pub_key_hex[i*2], "%2hhx", &inst->instance->my_keys.public_key[i]) != 1) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "Failed to parse public key byte %d", i);
|
||||
}
|
||||
}
|
||||
Binary file not shown.
@ -0,0 +1,75 @@
|
||||
/**
|
||||
* Тест загрузки debug настроек из конфигурационного файла |
||||
*/ |
||||
|
||||
#include <stdio.h> |
||||
#include <stdlib.h> |
||||
#include <string.h> |
||||
#include "../lib/debug_config.h" |
||||
#include "../src/config_parser.h" |
||||
|
||||
int main() { |
||||
printf("=== Тест загрузки debug настроек из конфигурационного файла ===\n"); |
||||
|
||||
// Инициализация отладки по умолчанию
|
||||
debug_config_init(); |
||||
debug_set_level(DEBUG_LEVEL_ERROR); // Начальный уровень
|
||||
|
||||
printf("\n1. До загрузки конфигурации:\n"); |
||||
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "ERROR сообщение до загрузки конфигурации"); |
||||
DEBUG_INFO(DEBUG_CATEGORY_CONFIG, "INFO сообщение до загрузки конфигурации (не должно выводиться)"); |
||||
|
||||
// Загрузка конфигурации из файла
|
||||
printf("\n2. Загрузка конфигурации из файла: test_config_with_debug.conf\n"); |
||||
struct utun_config *config = parse_config("test_config_with_debug.conf"); |
||||
|
||||
if (!config) { |
||||
printf("❌ Ошибка загрузки конфигурации\n"); |
||||
return 1; |
||||
} |
||||
|
||||
printf("✅ Конфигурация загружена успешно\n"); |
||||
printf(" Log file: %s\n", config->global.log_file[0] ? config->global.log_file : "(none)"); |
||||
printf(" Debug level: %s\n", config->global.debug_level[0] ? config->global.debug_level : "(none)"); |
||||
printf(" Debug categories: 0x%X\n", config->global.debug_categories); |
||||
printf(" Enable timestamp: %d\n", config->global.enable_timestamp); |
||||
printf(" Enable colors: %d\n", config->global.enable_colors); |
||||
|
||||
// Применение настроек из конфигурации (без переопределения CLI)
|
||||
printf("\n3. Применение настроек из конфигурации:\n"); |
||||
debug_apply_config_values( |
||||
config->global.log_file, |
||||
config->global.debug_level, |
||||
config->global.debug_categories, |
||||
config->global.enable_timestamp, |
||||
config->global.enable_function_names, |
||||
config->global.enable_file_lines, |
||||
config->global.enable_colors, |
||||
0, // CLI не переопределяет уровень
|
||||
0 // CLI не переопределяет категории
|
||||
); |
||||
|
||||
printf("✅ Настройки отладки применены\n"); |
||||
|
||||
// Тестирование новых настроек
|
||||
printf("\n4. Тестирование после применения настроек:\n"); |
||||
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "ERROR от ETCP (должен выводиться)"); |
||||
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "INFO от ETCP (должен выводиться - уровень debug)"); |
||||
DEBUG_DEBUG(DEBUG_CATEGORY_ETCP, "DEBUG от ETCP (должен выводиться - уровень debug)"); |
||||
DEBUG_INFO(DEBUG_CATEGORY_LL_QUEUE, "INFO от LL_QUEUE (должен выводиться)"); |
||||
DEBUG_INFO(DEBUG_CATEGORY_MEMORY, "INFO от MEMORY (должен выводиться)"); |
||||
DEBUG_INFO(DEBUG_CATEGORY_CONNECTION, "INFO от CONNECTION (НЕ должен выводиться - категория не включена)"); |
||||
DEBUG_INFO(DEBUG_CATEGORY_CRYPTO, "INFO от CRYPTO (НЕ должен выводиться - категория не включена)"); |
||||
|
||||
// Проверка работы с файлом
|
||||
printf("\n5. Проверка вывода в файл:\n"); |
||||
DEBUG_INFO(DEBUG_CATEGORY_CONFIG, "Это сообщение должно быть записано в файл: /tmp/utun_debug.log"); |
||||
|
||||
// Освобождение ресурсов
|
||||
free_config(config); |
||||
|
||||
printf("\n=== Тест завершен ===\n"); |
||||
printf("Проверьте файл /tmp/utun_debug.log для записанных сообщений\n"); |
||||
|
||||
return 0; |
||||
} |
||||
@ -0,0 +1,18 @@
|
||||
[global] |
||||
my_node_id=0x1111111111111111 |
||||
my_private_key=67b705a92b41bcaae105af2d6a17743faa7b26ccebba8b3b9b0af05e9cd1d5fb |
||||
my_public_key=1c55e4ccae7c4470707759086738b10681bf88b81f198cc2ab54a647d1556e17c65e6b1833e0c771e5a39382c03067c388915a4c732191bc130480f20f8e00b9 |
||||
tun_ip=10.99.0.1/24 |
||||
tun_ifname=tun99 |
||||
# Debug and logging configuration |
||||
log_file=/tmp/utun_debug.log |
||||
debug_level=debug |
||||
debug_categories=etcp,ll_queue,memory |
||||
enable_timestamp=1 |
||||
enable_function_names=1 |
||||
enable_file_lines=1 |
||||
enable_colors=1 |
||||
|
||||
[server: test] |
||||
addr=127.0.0.1:9001 |
||||
type=public |
||||
Binary file not shown.
@ -0,0 +1,49 @@
|
||||
#include "u_async.h" |
||||
#include <stdio.h> |
||||
|
||||
/* Test statistics */ |
||||
static struct { |
||||
int tests_run; |
||||
int tests_passed; |
||||
int tests_failed; |
||||
|
||||
/* Timer statistics */ |
||||
int timer_callbacks; |
||||
int timer_cancellations; |
||||
int immediate_timeouts; |
||||
|
||||
/* Socket statistics */ |
||||
int socket_events; |
||||
int socket_errors; |
||||
|
||||
/* Error statistics */ |
||||
int memory_allocation_errors; |
||||
int invalid_parameter_errors; |
||||
int race_condition_errors; |
||||
} test_stats = {0}; |
||||
|
||||
/* Timer callback for testing */ |
||||
static void test_timer_callback(void* arg) { |
||||
test_stats.timer_callbacks++; |
||||
printf("Timer callback fired. Total callbacks: %d\n", test_stats.timer_callbacks); |
||||
} |
||||
|
||||
int main() { |
||||
printf("Before any tests: immediate_timeouts = %d\n", test_stats.immediate_timeouts); |
||||
|
||||
uasync_t* ua = uasync_create(); |
||||
|
||||
/* Set 5 immediate timeouts */ |
||||
for (int i = 0; i < 5; i++) { |
||||
void* timer = uasync_set_timeout(ua, 0, NULL, test_timer_callback); |
||||
} |
||||
|
||||
printf("After setting 5 immediate timeouts, before poll: immediate_timeouts = %d\n", test_stats.immediate_timeouts); |
||||
|
||||
uasync_poll(ua, 1); |
||||
|
||||
printf("After poll: immediate_timeouts = %d\n", test_stats.immediate_timeouts); |
||||
|
||||
uasync_destroy(ua, 0); |
||||
return 0; |
||||
} |
||||
Binary file not shown.
@ -0,0 +1,117 @@
|
||||
/**
|
||||
* Тест настройки категорий отладки из аргументов командной строки |
||||
*/ |
||||
|
||||
#include <stdio.h> |
||||
#include <stdlib.h> |
||||
#include <string.h> |
||||
#include "../lib/debug_config.h" |
||||
|
||||
int main(int argc, char *argv[]) { |
||||
printf("=== Тест настройки категорий из аргументов ===\n"); |
||||
printf("Запуск с аргументами: "); |
||||
for (int i = 0; i < argc; i++) { |
||||
printf("%s ", argv[i]); |
||||
} |
||||
printf("\n\n"); |
||||
|
||||
// Инициализация отладки
|
||||
debug_config_init(); |
||||
|
||||
// Анализ аргументов командной строки
|
||||
int debug_categories = 0; |
||||
|
||||
// Если есть специфические категории, сначала отключаем все
|
||||
int has_specific_categories = 0; |
||||
|
||||
for (int i = 1; i < argc; i++) { |
||||
if (strcmp(argv[i], "--debug-etcp") == 0) { |
||||
has_specific_categories = 1; |
||||
} else if (strcmp(argv[i], "--debug-queue") == 0) { |
||||
has_specific_categories = 1; |
||||
} else if (strcmp(argv[i], "--debug-memory") == 0) { |
||||
has_specific_categories = 1; |
||||
} else if (strcmp(argv[i], "--debug-none") == 0) { |
||||
has_specific_categories = 1; |
||||
} |
||||
} |
||||
|
||||
// Если есть специфические категории, отключаем все по умолчанию
|
||||
if (has_specific_categories) { |
||||
debug_set_categories(DEBUG_CATEGORY_NONE); |
||||
debug_categories = DEBUG_CATEGORY_NONE; |
||||
} |
||||
|
||||
// Теперь обрабатываем аргументы
|
||||
for (int i = 1; i < argc; i++) { |
||||
if (strcmp(argv[i], "--debug-etcp") == 0) { |
||||
debug_enable_category(DEBUG_CATEGORY_ETCP); |
||||
debug_categories |= DEBUG_CATEGORY_ETCP; |
||||
printf("✅ Включена категория ETCP\n"); |
||||
} else if (strcmp(argv[i], "--debug-queue") == 0) { |
||||
debug_enable_category(DEBUG_CATEGORY_LL_QUEUE); |
||||
debug_categories |= DEBUG_CATEGORY_LL_QUEUE; |
||||
printf("✅ Включена категория LL_QUEUE\n"); |
||||
} else if (strcmp(argv[i], "--debug-memory") == 0) { |
||||
debug_enable_category(DEBUG_CATEGORY_MEMORY); |
||||
debug_categories |= DEBUG_CATEGORY_MEMORY; |
||||
printf("✅ Включена категория MEMORY\n"); |
||||
} else if (strcmp(argv[i], "--debug-all") == 0) { |
||||
debug_set_categories(DEBUG_CATEGORY_ALL); |
||||
debug_categories = DEBUG_CATEGORY_ALL; |
||||
printf("✅ Включены все категории\n"); |
||||
break; // Все категории уже включены
|
||||
} else if (strcmp(argv[i], "--debug-none") == 0) { |
||||
debug_set_categories(DEBUG_CATEGORY_NONE); |
||||
debug_categories = DEBUG_CATEGORY_NONE; |
||||
printf("✅ Выключены все категории\n"); |
||||
break; // Все категории уже выключены
|
||||
} else if (strcmp(argv[i], "--level") == 0 && i+1 < argc) { |
||||
i++; |
||||
if (strcmp(argv[i], "error") == 0) { |
||||
debug_set_level(DEBUG_LEVEL_ERROR); |
||||
printf("✅ Установлен уровень ERROR\n"); |
||||
} else if (strcmp(argv[i], "info") == 0) { |
||||
debug_set_level(DEBUG_LEVEL_INFO); |
||||
printf("✅ Установлен уровень INFO\n"); |
||||
} else if (strcmp(argv[i], "debug") == 0) { |
||||
debug_set_level(DEBUG_LEVEL_DEBUG); |
||||
printf("✅ Установлен уровень DEBUG\n"); |
||||
} |
||||
} |
||||
} |
||||
|
||||
printf("\n=== Тестовые сообщения ===\n"); |
||||
|
||||
// Сообщения от разных категорий
|
||||
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "Сообщение от ETCP категории"); |
||||
DEBUG_INFO(DEBUG_CATEGORY_LL_QUEUE, "Сообщение от LL_QUEUE категории"); |
||||
DEBUG_INFO(DEBUG_CATEGORY_MEMORY, "Сообщение от MEMORY категории"); |
||||
DEBUG_INFO(DEBUG_CATEGORY_CONNECTION, "Сообщение от CONNECTION категории"); |
||||
DEBUG_INFO(DEBUG_CATEGORY_CRYPTO, "Сообщение от CRYPTO категории"); |
||||
|
||||
printf("\n=== Информация о текущих настройках ===\n"); |
||||
printf("Текущий уровень отладки: "); |
||||
debug_level_t level = debug_get_effective_level(DEBUG_CATEGORY_ETCP); |
||||
switch (level) { |
||||
case DEBUG_LEVEL_ERROR: printf("ERROR"); break; |
||||
case DEBUG_LEVEL_WARN: printf("WARN"); break; |
||||
case DEBUG_LEVEL_INFO: printf("INFO"); break; |
||||
case DEBUG_LEVEL_DEBUG: printf("DEBUG"); break; |
||||
case DEBUG_LEVEL_TRACE: printf("TRACE"); break; |
||||
default: printf("UNKNOWN"); break; |
||||
} |
||||
printf("\n"); |
||||
|
||||
printf("Активные категории: "); |
||||
if (debug_categories & DEBUG_CATEGORY_ETCP) printf("ETCP "); |
||||
if (debug_categories & DEBUG_CATEGORY_LL_QUEUE) printf("LL_QUEUE "); |
||||
if (debug_categories & DEBUG_CATEGORY_MEMORY) printf("MEMORY "); |
||||
if (debug_categories & DEBUG_CATEGORY_CONNECTION) printf("CONNECTION "); |
||||
if (debug_categories & DEBUG_CATEGORY_CRYPTO) printf("CRYPTO "); |
||||
if (debug_categories == DEBUG_CATEGORY_ALL) printf("ALL "); |
||||
if (debug_categories == DEBUG_CATEGORY_NONE) printf("NONE "); |
||||
printf("\n"); |
||||
|
||||
return 0; |
||||
} |
||||
Binary file not shown.
@ -0,0 +1,89 @@
|
||||
/**
|
||||
* Тест новой системы отладки с буферизованным выводом |
||||
*/ |
||||
|
||||
#include <stdio.h> |
||||
#include <stdlib.h> |
||||
#include <string.h> |
||||
#include "../lib/debug_config.h" |
||||
#include "../lib/u_async.h" |
||||
|
||||
int main() { |
||||
printf("=== Тест новой системы отладки ===\n"); |
||||
|
||||
// Инициализация отладки
|
||||
debug_config_init(); |
||||
|
||||
// Тест 1: Базовые уровни отладки
|
||||
printf("\n1. Базовые уровни отладки:\n"); |
||||
debug_set_level(DEBUG_LEVEL_TRACE); |
||||
debug_enable_timestamp(1); |
||||
debug_enable_function_name(1); |
||||
debug_enable_file_line(1); |
||||
debug_enable_color(1); |
||||
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "Тест ERROR сообщения"); |
||||
DEBUG_WARN(DEBUG_CATEGORY_ETCP, "Тест WARN сообщения"); |
||||
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "Тест INFO сообщения"); |
||||
DEBUG_DEBUG(DEBUG_CATEGORY_ETCP, "Тест DEBUG сообщения"); |
||||
DEBUG_TRACE(DEBUG_CATEGORY_ETCP, "Тест TRACE сообщения"); |
||||
|
||||
// Тест 2: Разные категории
|
||||
printf("\n2. Разные категории отладки:\n"); |
||||
debug_set_level(DEBUG_LEVEL_INFO); |
||||
|
||||
DEBUG_INFO(DEBUG_CATEGORY_LL_QUEUE, "Сообщение от ll_queue"); |
||||
DEBUG_INFO(DEBUG_CATEGORY_CONNECTION, "Сообщение от connection"); |
||||
DEBUG_INFO(DEBUG_CATEGORY_MEMORY, "Сообщение от memory"); |
||||
DEBUG_INFO(DEBUG_CATEGORY_CRYPTO, "Сообщение от crypto"); |
||||
|
||||
// Тест 3: Форматирование с аргументами
|
||||
printf("\n3. Форматирование с аргументами:\n"); |
||||
int test_value = 42; |
||||
const char* test_string = "тестовая строка"; |
||||
|
||||
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "Значение: %d, строка: %s", test_value, test_string); |
||||
DEBUG_DEBUG(DEBUG_CATEGORY_MEMORY, "Размер буфера: %zu, флаг: %d", (size_t)1024, 1); |
||||
|
||||
// Тест 4: Включение/выключение категорий
|
||||
printf("\n4. Выборочные категории:\n"); |
||||
debug_disable_category(DEBUG_CATEGORY_ALL); |
||||
debug_enable_category(DEBUG_CATEGORY_ETCP); |
||||
debug_enable_category(DEBUG_CATEGORY_LL_QUEUE); |
||||
|
||||
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "Это должно выводиться (ETCP)"); |
||||
DEBUG_INFO(DEBUG_CATEGORY_LL_QUEUE, "Это должно выводиться (LL_QUEUE)"); |
||||
DEBUG_INFO(DEBUG_CATEGORY_MEMORY, "Это НЕ должно выводиться (MEMORY отключена)"); |
||||
|
||||
// Тест 5: Разные уровни для разных категорий
|
||||
printf("\n5. Уровни по категориям:\n"); |
||||
debug_set_level(DEBUG_LEVEL_ERROR); // Глобальный уровень
|
||||
// Note: debug_set_category_level не существует в новой системе
|
||||
debug_set_level(DEBUG_LEVEL_DEBUG); // Устанавливаем DEBUG для всех
|
||||
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "ERROR для ETCP"); |
||||
DEBUG_DEBUG(DEBUG_CATEGORY_ETCP, "DEBUG для ETCP (должен выводиться)"); |
||||
DEBUG_INFO(DEBUG_CATEGORY_LL_QUEUE, "INFO для LL_QUEUE (должен выводиться)"); |
||||
DEBUG_DEBUG(DEBUG_CATEGORY_LL_QUEUE, "DEBUG для LL_QUEUE (должен выводиться)"); |
||||
|
||||
// Тест 6: Без цветов и метаданных
|
||||
printf("\n6. Минималистичный вывод:\n"); |
||||
debug_enable_color(0); |
||||
debug_enable_timestamp(0); |
||||
debug_enable_function_name(0); |
||||
debug_enable_file_line(0); |
||||
|
||||
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "Минималистичное сообщение"); |
||||
|
||||
// Тест 7: Возврат к полному формату
|
||||
printf("\n7. Полный формат снова:\n"); |
||||
debug_enable_color(1); |
||||
debug_enable_timestamp(1); |
||||
debug_enable_function_name(1); |
||||
debug_enable_file_line(1); |
||||
|
||||
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "Полное сообщение с метаданными"); |
||||
|
||||
printf("\n=== Тест завершен ===\n"); |
||||
return 0; |
||||
} |
||||
Binary file not shown.
Binary file not shown.
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue