mem
This commit is contained in:
1 parent
b2640e7201
commit
15c7968c8c
34 files changed
+332
-284
No files matched your search
+16
-15
@@ -14,6 +14,7 @@
|
||||
#include <ifaddrs.h>
|
||||
#include <net/if.h>
|
||||
#endif
|
||||
#include "../lib/mem.h"
|
||||
|
||||
#define MAX_LINE_LEN 1024
|
||||
#define INITIAL_ARRAY_CAPACITY 8
|
||||
@@ -73,7 +74,7 @@ static uint32_t parse_debug_categories(const char *value) {
|
||||
token = strtok(NULL, ",");
|
||||
}
|
||||
|
||||
free(value_copy);
|
||||
u_free(value_copy);
|
||||
return categories ? categories : DEBUG_CATEGORY_ALL; // Default to all if nothing parsed
|
||||
}
|
||||
|
||||
@@ -190,7 +191,7 @@ static uint32_t get_netif_index(const char *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));
|
||||
struct CFG_CLIENT_LINK *link = u_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;
|
||||
@@ -199,7 +200,7 @@ static struct CFG_CLIENT_LINK* create_client_link(struct CFG_SERVER* local_srv,
|
||||
link->local_srv = local_srv;
|
||||
|
||||
if (parse_address_and_port(remote_addr, &link->remote_addr) < 0) {
|
||||
free(link);
|
||||
u_free(link);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -210,20 +211,20 @@ static struct CFG_CLIENT_LINK* create_client_link(struct CFG_SERVER* local_srv,
|
||||
static void free_cfg_client_links(struct CFG_CLIENT_LINK *links) {
|
||||
while (links) {
|
||||
struct CFG_CLIENT_LINK *next = links->next;
|
||||
free(links);
|
||||
u_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));
|
||||
struct CFG_ROUTE_ENTRY *entry = u_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);
|
||||
u_free(entry);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -234,7 +235,7 @@ static struct CFG_ROUTE_ENTRY* create_route_entry(const char *subnet_str) {
|
||||
static void free_route_entries(struct CFG_ROUTE_ENTRY *entries) {
|
||||
while (entries) {
|
||||
struct CFG_ROUTE_ENTRY *next = entries->next;
|
||||
free(entries);
|
||||
u_free(entries);
|
||||
entries = next;
|
||||
}
|
||||
}
|
||||
@@ -450,7 +451,7 @@ static section_type_t parse_section_header(const char *line, char *name, size_t
|
||||
}
|
||||
|
||||
static struct utun_config* parse_config_internal(FILE *fp, const char *filename) {
|
||||
struct utun_config *cfg = calloc(1, sizeof(struct utun_config));
|
||||
struct utun_config *cfg = u_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;
|
||||
@@ -490,14 +491,14 @@ static struct utun_config* parse_config_internal(FILE *fp, const char *filename)
|
||||
cur_section = parse_section_header(trimmed, name, sizeof(name));
|
||||
|
||||
if (cur_section == SECTION_SERVER) {
|
||||
cur_server = calloc(1, sizeof(struct CFG_SERVER));
|
||||
cur_server = u_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));
|
||||
cur_client = u_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;
|
||||
@@ -573,10 +574,10 @@ static struct utun_config* parse_config_internal(FILE *fp, const char *filename)
|
||||
return cfg;
|
||||
|
||||
error:
|
||||
if (cur_server) free(cur_server);
|
||||
if (cur_server) u_free(cur_server);
|
||||
if (cur_client) {
|
||||
free_cfg_client_links(cur_client->links);
|
||||
free(cur_client);
|
||||
u_free(cur_client);
|
||||
}
|
||||
free_config(cfg);
|
||||
return NULL;
|
||||
@@ -603,7 +604,7 @@ void free_config(struct utun_config *config) {
|
||||
struct CFG_SERVER *server = config->servers;
|
||||
while (server) {
|
||||
struct CFG_SERVER *next = server->next;
|
||||
free(server);
|
||||
u_free(server);
|
||||
server = next;
|
||||
}
|
||||
|
||||
@@ -612,7 +613,7 @@ void free_config(struct utun_config *config) {
|
||||
while (client) {
|
||||
struct CFG_CLIENT *next = client->next;
|
||||
free_cfg_client_links(client->links);
|
||||
free(client);
|
||||
u_free(client);
|
||||
client = next;
|
||||
}
|
||||
|
||||
@@ -620,7 +621,7 @@ void free_config(struct utun_config *config) {
|
||||
free_route_entries(config->route_subnets);
|
||||
free_route_entries(config->my_subnets);
|
||||
|
||||
free(config);
|
||||
u_free(config);
|
||||
}
|
||||
|
||||
static const char* ip_to_string(const struct IP *ip, char *buffer, size_t buffer_size) {
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/stat.h>
|
||||
#include "../lib/mem.h"
|
||||
|
||||
#define PRIV_HEXKEY_LEN 65 // 32 bytes * 2 hex chars + null
|
||||
#define PUB_HEXKEY_LEN 129 // 64 bytes * 2 hex chars + null
|
||||
@@ -68,7 +69,7 @@ static int read_file_to_mem(const char *filename, char **buffer, size_t *size) {
|
||||
fseek(fp, 0, SEEK_SET);
|
||||
|
||||
|
||||
*buffer = malloc(file_size + 1);
|
||||
*buffer = u_malloc(file_size + 1);
|
||||
|
||||
if (!*buffer) {
|
||||
fclose(fp);
|
||||
@@ -79,7 +80,7 @@ static int read_file_to_mem(const char *filename, char **buffer, size_t *size) {
|
||||
size_t read_size = fread(*buffer, 1, file_size, fp);
|
||||
|
||||
if (read_size != (size_t)file_size) {
|
||||
free(*buffer);
|
||||
u_free(*buffer);
|
||||
|
||||
fclose(fp);
|
||||
|
||||
@@ -168,7 +169,7 @@ static int insert_or_replace_option(char **buf, size_t *buf_len, size_t *buf_cap
|
||||
// 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);
|
||||
char *new_buf = u_realloc(*buf, *buf_capacity);
|
||||
|
||||
if (!new_buf) return -1;
|
||||
*buf = new_buf;
|
||||
@@ -214,7 +215,7 @@ static int insert_or_replace_option(char **buf, size_t *buf_len, size_t *buf_cap
|
||||
// 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);
|
||||
char *new_buf = u_realloc(*buf, *buf_capacity);
|
||||
|
||||
if (!new_buf) return -1;
|
||||
*buf = new_buf;
|
||||
@@ -357,10 +358,10 @@ int config_ensure_keys_and_node_id(const char *filename) {
|
||||
|
||||
// Update config file
|
||||
size_t buf_capacity = file_size + 1024;
|
||||
char *work_buf = malloc(buf_capacity);
|
||||
char *work_buf = u_malloc(buf_capacity);
|
||||
|
||||
if (!work_buf) {
|
||||
free(file_buf);
|
||||
u_free(file_buf);
|
||||
|
||||
return -1;
|
||||
}
|
||||
@@ -404,9 +405,9 @@ int config_ensure_keys_and_node_id(const char *filename) {
|
||||
}
|
||||
}
|
||||
|
||||
free(file_buf);
|
||||
u_free(file_buf);
|
||||
|
||||
free(work_buf);
|
||||
u_free(work_buf);
|
||||
|
||||
|
||||
return ret;
|
||||
|
||||
+10
-9
@@ -11,6 +11,7 @@
|
||||
#include "tun_if.h"
|
||||
#include "../lib/u_async.h"
|
||||
#include "../lib/debug_config.h"
|
||||
#include "../lib/mem.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@@ -332,7 +333,7 @@ static void accept_callback(socket_t fd, void* arg) {
|
||||
}
|
||||
|
||||
/* Allocate client structure */
|
||||
struct control_client* client = (struct control_client*)calloc(1, sizeof(*client));
|
||||
struct control_client* client = (struct control_client*)u_calloc(1, sizeof(*client));
|
||||
if (!client) {
|
||||
if (server->log_file) {
|
||||
fprintf(server->log_file, "%llu: [ERROR] Failed to allocate client structure\n",
|
||||
@@ -367,7 +368,7 @@ static void accept_callback(socket_t fd, void* arg) {
|
||||
fflush(server->log_file);
|
||||
}
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_CONTROL, "Failed to register client socket with uasync");
|
||||
free(client);
|
||||
u_free(client);
|
||||
#ifdef _WIN32
|
||||
closesocket(client_fd);
|
||||
#else
|
||||
@@ -526,7 +527,7 @@ static void close_client(struct control_server* server, struct control_client* c
|
||||
}
|
||||
|
||||
server->client_count--;
|
||||
free(client);
|
||||
u_free(client);
|
||||
|
||||
DEBUG_INFO(DEBUG_CATEGORY_CONTROL, "Control client disconnected (total: %u)",
|
||||
server->client_count);
|
||||
@@ -642,7 +643,7 @@ static void send_conn_list(struct control_server* server, struct control_client*
|
||||
|
||||
/* Build response */
|
||||
uint16_t rsp_size = ETCPMON_CONN_LIST_SIZE(count);
|
||||
uint8_t* buffer = (uint8_t*)malloc(rsp_size);
|
||||
uint8_t* buffer = (uint8_t*)u_malloc(rsp_size);
|
||||
if (!buffer) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_CONTROL, "Failed to allocate connection list buffer");
|
||||
return;
|
||||
@@ -679,7 +680,7 @@ static void send_conn_list(struct control_server* server, struct control_client*
|
||||
send(client->fd, buffer, rsp_size, 0);
|
||||
#endif
|
||||
|
||||
free(buffer);
|
||||
u_free(buffer);
|
||||
}
|
||||
|
||||
static void send_metrics(struct control_server* server, struct control_client* client) {
|
||||
@@ -702,7 +703,7 @@ static void send_metrics(struct control_server* server, struct control_client* c
|
||||
|
||||
/* Build response */
|
||||
uint16_t rsp_size = ETCPMON_METRICS_SIZE(links_count);
|
||||
uint8_t* buffer = (uint8_t*)malloc(rsp_size);
|
||||
uint8_t* buffer = (uint8_t*)u_malloc(rsp_size);
|
||||
if (!buffer) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_CONTROL, "Failed to allocate metrics buffer");
|
||||
return;
|
||||
@@ -775,7 +776,7 @@ static void send_metrics(struct control_server* server, struct control_client* c
|
||||
send(client->fd, buffer, rsp_size, 0);
|
||||
#endif
|
||||
|
||||
free(buffer);
|
||||
u_free(buffer);
|
||||
}
|
||||
|
||||
static void send_error(struct control_client* client, uint8_t error_code, const char* msg) {
|
||||
@@ -783,7 +784,7 @@ static void send_error(struct control_client* client, uint8_t error_code, const
|
||||
uint16_t msg_len = (uint16_t)strlen(msg);
|
||||
uint16_t rsp_size = ETCPMON_ERROR_SIZE(msg_len);
|
||||
|
||||
uint8_t* buffer = (uint8_t*)malloc(rsp_size);
|
||||
uint8_t* buffer = (uint8_t*)u_malloc(rsp_size);
|
||||
if (!buffer) return;
|
||||
|
||||
struct etcpmon_msg_header* hdr = (struct etcpmon_msg_header*)buffer;
|
||||
@@ -808,7 +809,7 @@ static void send_error(struct control_client* client, uint8_t error_code, const
|
||||
send(client->fd, buffer, rsp_size, 0);
|
||||
#endif
|
||||
|
||||
free(buffer);
|
||||
u_free(buffer);
|
||||
}
|
||||
|
||||
/* ============================================================================
|
||||
|
||||
+11
-10
@@ -15,6 +15,7 @@
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include "../lib/mem.h"
|
||||
|
||||
/* Timebase: 0.1 мс */
|
||||
#define TIMEBASE_US 100
|
||||
@@ -153,7 +154,7 @@ static void dummynet_process_queue(struct dummynet* dn, int dir_idx) {
|
||||
if (delay_tb == 0) delay_tb = 1;
|
||||
}
|
||||
|
||||
struct shaper_cb_arg* sarg = (struct shaper_cb_arg*)malloc(sizeof(struct shaper_cb_arg));
|
||||
struct shaper_cb_arg* sarg = (struct shaper_cb_arg*)u_malloc(sizeof(struct shaper_cb_arg));
|
||||
if (sarg) {
|
||||
sarg->dn = dn;
|
||||
sarg->dir_idx = dir_idx;
|
||||
@@ -161,7 +162,7 @@ static void dummynet_process_queue(struct dummynet* dn, int dir_idx) {
|
||||
if (dir->shaper_timer) {
|
||||
dir->shaper_timer_set++;
|
||||
} else {
|
||||
free(sarg);
|
||||
u_free(sarg);
|
||||
dir->shaper_pending = 0;
|
||||
}
|
||||
} else {
|
||||
@@ -177,7 +178,7 @@ static void dummynet_shaper_callback(void* user_arg) {
|
||||
struct shaper_cb_arg* arg = (struct shaper_cb_arg*)user_arg;
|
||||
struct dummynet* dn = arg->dn;
|
||||
int dir_idx = arg->dir_idx;
|
||||
free(arg);
|
||||
u_free(arg);
|
||||
|
||||
dn->dirs[dir_idx].shaper_timer_fire++;
|
||||
dummynet_process_queue(dn, dir_idx);
|
||||
@@ -259,7 +260,7 @@ static void dummynet_read_callback(socket_t sock, void* user_arg) {
|
||||
dir_idx, n, delay_ms);
|
||||
|
||||
/* Создаём таймер задержки */
|
||||
struct delay_cb_arg* targ = (struct delay_cb_arg*)malloc(sizeof(struct delay_cb_arg));
|
||||
struct delay_cb_arg* targ = (struct delay_cb_arg*)u_malloc(sizeof(struct delay_cb_arg));
|
||||
if (!targ) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_DUMMYNET, "Failed to allocate timer arg");
|
||||
dir->stats.dropped++;
|
||||
@@ -273,7 +274,7 @@ static void dummynet_read_callback(socket_t sock, void* user_arg) {
|
||||
if (!timer) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_DUMMYNET, "Failed to set delay timer");
|
||||
dir->stats.dropped++;
|
||||
free(targ);
|
||||
u_free(targ);
|
||||
queue_entry_free(entry);
|
||||
return;
|
||||
}
|
||||
@@ -285,7 +286,7 @@ static void dummynet_delay_callback(void* user_arg) {
|
||||
struct delay_cb_arg* arg = (struct delay_cb_arg*)user_arg;
|
||||
struct dummynet* dn = arg->dn;
|
||||
struct ll_entry* entry = arg->entry;
|
||||
free(arg);
|
||||
u_free(arg);
|
||||
|
||||
struct dummynet_pkt* pkt = (struct dummynet_pkt*)entry->data;
|
||||
int dir_idx = pkt->direction;
|
||||
@@ -321,7 +322,7 @@ static void dummynet_delay_callback(void* user_arg) {
|
||||
/* Запускаем шейпер если не активен */
|
||||
if (!dir->shaper_pending) {
|
||||
dir->shaper_pending = 1;
|
||||
struct shaper_cb_arg* sarg = (struct shaper_cb_arg*)malloc(sizeof(struct shaper_cb_arg));
|
||||
struct shaper_cb_arg* sarg = (struct shaper_cb_arg*)u_malloc(sizeof(struct shaper_cb_arg));
|
||||
if (sarg) {
|
||||
sarg->dn = dn;
|
||||
sarg->dir_idx = dir_idx;
|
||||
@@ -339,7 +340,7 @@ struct dummynet* dummynet_create(struct UASYNC* ua, const char* bind_ip, uint16_
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct dummynet* dn = (struct dummynet*)calloc(1, sizeof(struct dummynet));
|
||||
struct dummynet* dn = (struct dummynet*)u_calloc(1, sizeof(struct dummynet));
|
||||
if (!dn) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_DUMMYNET, "Failed to allocate dummynet context");
|
||||
return NULL;
|
||||
@@ -352,7 +353,7 @@ struct dummynet* dummynet_create(struct UASYNC* ua, const char* bind_ip, uint16_
|
||||
dn->pkt_pool = memory_pool_init(sizeof(struct ll_entry) + sizeof(struct dummynet_pkt));
|
||||
if (!dn->pkt_pool) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_DUMMYNET, "Failed to create packet pool");
|
||||
free(dn);
|
||||
u_free(dn);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -447,7 +448,7 @@ void dummynet_destroy(struct dummynet* dn) {
|
||||
memory_pool_destroy(dn->pkt_pool);
|
||||
}
|
||||
|
||||
free(dn);
|
||||
u_free(dn);
|
||||
}
|
||||
|
||||
/* Настраивает параметры направления */
|
||||
|
||||
+4
-2
@@ -14,6 +14,7 @@
|
||||
#include <sys/time.h>
|
||||
#include <math.h> // For bandwidth calcs
|
||||
#include <limits.h> // For UINT16_MAX
|
||||
#include "../lib/mem.h"
|
||||
|
||||
// Enable comprehensive debug output for ETCP module
|
||||
#define DEBUG_CATEGORY_ETCP_DETAILED 1
|
||||
@@ -110,7 +111,7 @@ struct ETCP_CONN* etcp_connection_create(struct UTUN_INSTANCE* instance) {
|
||||
if (!instance) return NULL;
|
||||
|
||||
|
||||
struct ETCP_CONN* etcp = calloc(1, sizeof(struct ETCP_CONN));
|
||||
struct ETCP_CONN* etcp = u_calloc(1, sizeof(struct ETCP_CONN));
|
||||
if (!etcp) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_CONNECTION, "etcp_connection_create: creating connection failed for instance %p", instance);
|
||||
return NULL;
|
||||
@@ -241,7 +242,7 @@ void etcp_connection_close(struct ETCP_CONN* etcp) {
|
||||
etcp->next = NULL;
|
||||
|
||||
// TODO: Free rx_list, etc.
|
||||
free(etcp);
|
||||
u_free(etcp);
|
||||
}
|
||||
|
||||
// Reset connection
|
||||
@@ -682,6 +683,7 @@ struct ETCP_DGRAM* etcp_request_pkt(struct ETCP_CONN* etcp) {
|
||||
int chk=queue_check_consistency(etcp->ack_q);
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_ETCP, "[%s] only ACK (size=%d) packet with %d bytes total (chk=%d) rem=%d", etcp->log_name, ack_q_size, ptr, chk, remain_len);
|
||||
}
|
||||
if (ptr>=PACKET_DATA_SIZE-50) DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "[%s] SIZE ERROR!!! %d", ptr);
|
||||
|
||||
dgram->data_len=ptr;
|
||||
|
||||
|
||||
+18
-17
@@ -18,6 +18,7 @@
|
||||
#include "etcp_loadbalancer.h"
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include "../lib/mem.h"
|
||||
|
||||
// Forward declaration
|
||||
static void etcp_connections_read_callback_socket(socket_t sock, void* arg);
|
||||
@@ -38,7 +39,7 @@ static void etcp_link_send_init(struct ETCP_LINK* link, uint8_t reset) {
|
||||
DEBUG_INFO(DEBUG_CATEGORY_CONNECTION, "etcp_link_send_init link=%p, is_server=%d, reset=%d", link, link ? link->is_server : -1, reset);
|
||||
if (!link || !link->etcp || !link->etcp->instance) return;
|
||||
|
||||
struct ETCP_DGRAM* dgram = malloc(sizeof(struct ETCP_DGRAM) + 100);
|
||||
struct ETCP_DGRAM* dgram = u_malloc(sizeof(struct ETCP_DGRAM) + 100);
|
||||
if (!dgram) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_CONNECTION, "etcp_link_send_init: malloc failed");
|
||||
return;
|
||||
@@ -94,7 +95,7 @@ static void etcp_link_send_init(struct ETCP_LINK* link, uint8_t reset) {
|
||||
}
|
||||
|
||||
etcp_encrypt_send(dgram);
|
||||
free(dgram);
|
||||
u_free(dgram);
|
||||
|
||||
link->init_retry_count++;
|
||||
|
||||
@@ -125,7 +126,7 @@ static void etcp_link_send_keepalive(struct ETCP_LINK* link) {
|
||||
DEBUG_TRACE(DEBUG_CATEGORY_CONNECTION, "");
|
||||
if (!link || !link->etcp || !link->etcp->instance) return;
|
||||
|
||||
struct ETCP_DGRAM* dgram = malloc(sizeof(struct ETCP_DGRAM) + 2);
|
||||
struct ETCP_DGRAM* dgram = u_malloc(sizeof(struct ETCP_DGRAM) + 4);
|
||||
if (!dgram) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_CONNECTION, "etcp_link_send_keepalive: malloc failed");
|
||||
return;
|
||||
@@ -140,7 +141,7 @@ static void etcp_link_send_keepalive(struct ETCP_LINK* link) {
|
||||
link->etcp->log_name, link, link->local_link_id);
|
||||
|
||||
etcp_encrypt_send(dgram);
|
||||
free(dgram);
|
||||
u_free(dgram);
|
||||
}
|
||||
|
||||
// Check if all links for an ETCP_CONN are down
|
||||
@@ -260,7 +261,7 @@ static int etcp_link_send_reset(struct ETCP_LINK* link) {
|
||||
DEBUG_TRACE(DEBUG_CATEGORY_CONNECTION, "");
|
||||
if (!link) return -1;
|
||||
|
||||
struct ETCP_DGRAM* dgram = malloc(sizeof(struct ETCP_DGRAM) + 1);
|
||||
struct ETCP_DGRAM* dgram = u_malloc(sizeof(struct ETCP_DGRAM) + 4);
|
||||
if (!dgram) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_CONNECTION, "etcp_link_send_reset: malloc failed");
|
||||
return -1;
|
||||
@@ -273,7 +274,7 @@ static int etcp_link_send_reset(struct ETCP_LINK* link) {
|
||||
|
||||
DEBUG_INFO(DEBUG_CATEGORY_CONNECTION, "Sending RESET to link");
|
||||
int ret = etcp_encrypt_send(dgram);
|
||||
free(dgram);
|
||||
u_free(dgram);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -309,7 +310,7 @@ static int find_link_index(struct ETCP_SOCKET* e_sock, uint32_t hash) {
|
||||
static int realloc_links(struct ETCP_SOCKET* e_sock) {
|
||||
DEBUG_TRACE(DEBUG_CATEGORY_CONNECTION, "");
|
||||
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*));
|
||||
struct ETCP_LINK** new_links = u_realloc(e_sock->links, new_max * sizeof(struct ETCP_LINK*));
|
||||
if (!new_links) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_CONNECTION, "realloc_links: realloc failed");
|
||||
return -1;
|
||||
@@ -410,7 +411,7 @@ struct ETCP_SOCKET* etcp_socket_add(struct UTUN_INSTANCE* instance, struct socka
|
||||
DEBUG_TRACE(DEBUG_CATEGORY_CONNECTION, "");
|
||||
if (!instance) return NULL;
|
||||
|
||||
struct ETCP_SOCKET* e_sock = calloc(1, sizeof(struct ETCP_SOCKET));
|
||||
struct ETCP_SOCKET* e_sock = u_calloc(1, sizeof(struct ETCP_SOCKET));
|
||||
if (!e_sock) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, "Failed to allocate connection");
|
||||
return NULL;
|
||||
@@ -422,7 +423,7 @@ struct ETCP_SOCKET* etcp_socket_add(struct UTUN_INSTANCE* instance, struct socka
|
||||
family = ip->ss_family;
|
||||
if (family != AF_INET && family != AF_INET6) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_CONNECTION, "Unsupported address family: %d", family);
|
||||
free(e_sock);
|
||||
u_free(e_sock);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
@@ -431,7 +432,7 @@ struct ETCP_SOCKET* etcp_socket_add(struct UTUN_INSTANCE* instance, struct socka
|
||||
if (e_sock->fd == SOCKET_INVALID) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_CONNECTION, "Failed to create socket: %s",
|
||||
socket_strerror(socket_get_error()));
|
||||
free(e_sock);
|
||||
u_free(e_sock);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -474,7 +475,7 @@ struct ETCP_SOCKET* etcp_socket_add(struct UTUN_INSTANCE* instance, struct socka
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "[ETCP] Failed to bind to %s:%d", ip_to_str(&sin->sin_addr, AF_INET).str, ntohs(sin->sin_port));
|
||||
}
|
||||
socket_close_wrapper(e_sock->fd);
|
||||
free(e_sock);
|
||||
u_free(e_sock);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -501,7 +502,7 @@ struct ETCP_SOCKET* etcp_socket_add(struct UTUN_INSTANCE* instance, struct socka
|
||||
if (!e_sock->socket_id) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_CONNECTION, "Failed to register socket with uasync");
|
||||
socket_close_wrapper(e_sock->fd);
|
||||
free(e_sock);
|
||||
u_free(e_sock);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -534,9 +535,9 @@ void etcp_socket_remove(struct ETCP_SOCKET* conn) {
|
||||
for (size_t i = 0; i < conn->num_channels; i++) {
|
||||
etcp_link_close(conn->links[i]);
|
||||
}
|
||||
free(conn->links);
|
||||
u_free(conn->links);
|
||||
|
||||
free(conn);
|
||||
u_free(conn);
|
||||
}
|
||||
|
||||
|
||||
@@ -544,7 +545,7 @@ struct ETCP_LINK* etcp_link_new(struct ETCP_CONN* etcp, struct ETCP_SOCKET* conn
|
||||
DEBUG_TRACE(DEBUG_CATEGORY_CONNECTION, "");
|
||||
if (!remote_addr) return NULL;
|
||||
|
||||
struct ETCP_LINK* link = calloc(1, sizeof(struct ETCP_LINK));
|
||||
struct ETCP_LINK* link = u_calloc(1, sizeof(struct ETCP_LINK));
|
||||
if (!link) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_CONNECTION, "etcp_link_new: calloc failed");
|
||||
return NULL;
|
||||
@@ -576,7 +577,7 @@ struct ETCP_LINK* etcp_link_new(struct ETCP_CONN* etcp, struct ETCP_SOCKET* conn
|
||||
int free_id = etcp_find_free_local_link_id(etcp);
|
||||
if (free_id < 0) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_CONNECTION, "etcp_link_new: no free local_link_id available");
|
||||
free(link);
|
||||
u_free(link);
|
||||
return NULL;
|
||||
}
|
||||
link->local_link_id = (uint8_t)free_id;
|
||||
@@ -635,7 +636,7 @@ void etcp_link_close(struct ETCP_LINK* link) {
|
||||
|
||||
remove_link(link->conn, link->ip_port_hash);
|
||||
|
||||
free(link);
|
||||
u_free(link);
|
||||
}
|
||||
|
||||
// Reset link state (for INIT_RESPONSE with reset)
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
#include "../lib/socket_compat.h"
|
||||
#include <stdint.h>
|
||||
|
||||
#define PACKET_DATA_SIZE 1536
|
||||
#define PACKET_DATA_SIZE 1600//1536
|
||||
|
||||
// Типы кодограмм протокола
|
||||
#define ETCP_INIT_REQUEST 0x02
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
#include <stdint.h> // For UINT64_MAX
|
||||
#include <time.h> // For timespec
|
||||
#include <math.h> // For rounding
|
||||
#include "../lib/mem.h"
|
||||
|
||||
// Enable comprehensive debug output for loadbalancer module
|
||||
#define DEBUG_CATEGORY_LOADBALANCER 1
|
||||
@@ -83,7 +84,7 @@ void etcp_loadbalancer_send(struct ETCP_DGRAM* dgram) {
|
||||
if (!etcp) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_ETCP, "[%s] no ETCP_CONN associated with dgram", etcp->log_name);
|
||||
memory_pool_free(etcp->instance->pkt_pool, dgram);
|
||||
// free(dgram);
|
||||
// u_free(dgram);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -93,7 +94,7 @@ void etcp_loadbalancer_send(struct ETCP_DGRAM* dgram) {
|
||||
if (!dgram->link) {
|
||||
DEBUG_WARN(DEBUG_CATEGORY_ETCP, "[%s] no link available, dropping dgram", etcp->log_name);
|
||||
memory_pool_free(etcp->instance->pkt_pool, dgram);
|
||||
// free(dgram); // Assume free; adjust if pooled
|
||||
// u_free(dgram); // Assume free; adjust if pooled
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -143,7 +144,7 @@ void etcp_loadbalancer_send(struct ETCP_DGRAM* dgram) {
|
||||
// (unsigned long long)link->shaper_load_time_tb, (unsigned long long)link->shaper_sub_nanotime, link->shaper_timer==NULL?1:0);
|
||||
|
||||
memory_pool_free(etcp->instance->pkt_pool, dgram);
|
||||
// free(dgram); // Free the dgram in all cases - we own it
|
||||
// u_free(dgram); // Free the dgram in all cases - we own it
|
||||
}
|
||||
|
||||
// New: Notify when link is ready (called from timer or external)
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
#include <string.h>
|
||||
#include <stdio.h> // For debugging (can be removed if not needed)
|
||||
#include "../lib/debug_config.h" // For DEBUG macros
|
||||
#include "../lib/mem.h"
|
||||
|
||||
// Forward declarations
|
||||
static void packer_cb(struct ll_queue* q, void* arg);
|
||||
@@ -26,7 +27,7 @@ struct PKTNORM* pn_init(struct ETCP_CONN* etcp) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_NORMALIZER, "pn_init: ETCP MTU error=%d", etcp->mtu);
|
||||
}
|
||||
|
||||
struct PKTNORM* pn = calloc(1, sizeof(struct PKTNORM));
|
||||
struct PKTNORM* pn = u_calloc(1, sizeof(struct PKTNORM));
|
||||
if (!pn) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_NORMALIZER, "pn_init: calloc failed");
|
||||
return NULL;
|
||||
@@ -79,7 +80,7 @@ void pn_deinit(struct PKTNORM* pn) {
|
||||
struct ll_entry* entry;
|
||||
while ((entry = queue_data_get(pn->input)) != NULL) {
|
||||
if (entry->dgram) {
|
||||
free(entry->dgram);
|
||||
u_free(entry->dgram);
|
||||
}
|
||||
queue_entry_free(entry);
|
||||
}
|
||||
@@ -89,7 +90,7 @@ void pn_deinit(struct PKTNORM* pn) {
|
||||
struct ll_entry* entry;
|
||||
while ((entry = queue_data_get(pn->output)) != NULL) {
|
||||
if (entry->dgram) {
|
||||
free(entry->dgram);
|
||||
u_free(entry->dgram);
|
||||
}
|
||||
queue_entry_free(entry);
|
||||
}
|
||||
@@ -119,7 +120,7 @@ void pn_deinit(struct PKTNORM* pn) {
|
||||
}
|
||||
}
|
||||
|
||||
free(pn);
|
||||
u_free(pn);
|
||||
}
|
||||
|
||||
// Reset unpacker state
|
||||
@@ -324,6 +325,8 @@ static void pn_unpacker_cb(struct ll_queue* q, void* arg) {
|
||||
// DEBUG_INFO(DEBUG_CATEGORY_NORMALIZER, "pn_unpacker: new fragment pkt_len=%d (at %d)", part_size, ptr);
|
||||
ptr += 2;
|
||||
|
||||
if (part_size<1 || part_size>1500) DEBUG_ERROR(DEBUG_CATEGORY_NORMALIZER, "PART_SIZE ERROR!!! %d", part_size);
|
||||
|
||||
pn->recvpart = ll_alloc_lldgram(part_size);
|
||||
if (!pn->recvpart) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_NORMALIZER, "ll_alloc_lldgram failed");
|
||||
|
||||
+5
-4
@@ -20,6 +20,7 @@
|
||||
#include "utun_instance.h"
|
||||
#include "config_parser.h"
|
||||
#include "../lib/debug_config.h"
|
||||
#include "../lib/mem.h"
|
||||
|
||||
// Размер пакета (без заголовка ETCP)
|
||||
#define BGP_PACKET_SIZE (sizeof(struct ROUTE_BGP_PACKET))
|
||||
@@ -450,7 +451,7 @@ struct ROUTE_BGP* route_bgp_init(struct UTUN_INSTANCE* instance)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct ROUTE_BGP* bgp = (struct ROUTE_BGP*)calloc(1, sizeof(struct ROUTE_BGP));
|
||||
struct ROUTE_BGP* bgp = (struct ROUTE_BGP*)u_calloc(1, sizeof(struct ROUTE_BGP));
|
||||
if (!bgp) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_BGP, "Failed to allocate BGP structure");
|
||||
return NULL;
|
||||
@@ -462,7 +463,7 @@ struct ROUTE_BGP* route_bgp_init(struct UTUN_INSTANCE* instance)
|
||||
bgp->senders_list = queue_new(instance->ua, 0);
|
||||
if (!bgp->senders_list) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_BGP, "Failed to create senders queue");
|
||||
free(bgp);
|
||||
u_free(bgp);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -470,7 +471,7 @@ struct ROUTE_BGP* route_bgp_init(struct UTUN_INSTANCE* instance)
|
||||
if (etcp_bind(instance, ETCP_ID_ROUTE_ENTRY, route_bgp_receive_cbk) != 0) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_BGP, "Failed to bind ETCP callback");
|
||||
queue_free(bgp->senders_list);
|
||||
free(bgp);
|
||||
u_free(bgp);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -510,7 +511,7 @@ void route_bgp_destroy(struct UTUN_INSTANCE* instance)
|
||||
}
|
||||
queue_free(instance->bgp->senders_list);
|
||||
}
|
||||
free(instance->bgp);
|
||||
u_free(instance->bgp);
|
||||
instance->bgp = NULL;
|
||||
}
|
||||
|
||||
|
||||
+19
-18
@@ -11,6 +11,7 @@
|
||||
#include <string.h>
|
||||
#include "../lib/platform_compat.h"
|
||||
#include <time.h>
|
||||
#include "../lib/mem.h"
|
||||
|
||||
#define INITIAL_ROUTE_CAPACITY 100
|
||||
#define ROUTE_EXPANSION_FACTOR 2
|
||||
@@ -71,7 +72,7 @@ int parse_subnet(const char *subnet_str, uint32_t *network, uint8_t *prefix_leng
|
||||
* @return Указатель на созданный массив или NULL
|
||||
*/
|
||||
static struct ROUTE_ARRAY* route_array_create(uint32_t capacity) {
|
||||
struct ROUTE_ARRAY *array = calloc(1, sizeof(struct ROUTE_ARRAY) +
|
||||
struct ROUTE_ARRAY *array = u_calloc(1, sizeof(struct ROUTE_ARRAY) +
|
||||
capacity * sizeof(struct ROUTE_ENTRY*));
|
||||
if (!array) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, "route_array_create: failed to allocate memory");
|
||||
@@ -91,7 +92,7 @@ static void route_array_destroy(struct ROUTE_ARRAY *array) {
|
||||
if (!array) return;
|
||||
// entries[] содержит только указатели на ROUTE_ENTRY из таблицы,
|
||||
// их освобождать не нужно - они принадлежат таблице
|
||||
free(array);
|
||||
u_free(array);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -211,30 +212,30 @@ static const char *route_type_to_string(route_type_t type) {
|
||||
}
|
||||
|
||||
struct ROUTE_TABLE *route_table_create(void) {
|
||||
struct ROUTE_TABLE *table = calloc(1, sizeof(struct ROUTE_TABLE));
|
||||
struct ROUTE_TABLE *table = u_calloc(1, sizeof(struct ROUTE_TABLE));
|
||||
if (!table) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, "route_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));
|
||||
table->entries = u_calloc(table->capacity, sizeof(struct ROUTE_ENTRY));
|
||||
if (!table->entries) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, "route_table_create: failed to allocate memory for %zu route entries", table->capacity);
|
||||
free(table);
|
||||
u_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);
|
||||
table->dynamic_subnets = u_calloc(MAX_SUBNET_VALIDATION_RANGES, sizeof(uint32_t) * 2);
|
||||
table->local_subnets = u_calloc(MAX_SUBNET_VALIDATION_RANGES, sizeof(uint32_t) * 2);
|
||||
|
||||
if (!table->dynamic_subnets || !table->local_subnets) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, "route_table_create: failed to allocate memory for subnet validation arrays");
|
||||
free(table->entries);
|
||||
free(table->dynamic_subnets);
|
||||
free(table->local_subnets);
|
||||
free(table);
|
||||
u_free(table->entries);
|
||||
u_free(table->dynamic_subnets);
|
||||
u_free(table->local_subnets);
|
||||
u_free(table);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -245,13 +246,13 @@ void route_table_destroy(struct ROUTE_TABLE *table) {
|
||||
if (!table) return;
|
||||
|
||||
// Free route entries
|
||||
free(table->entries);
|
||||
u_free(table->entries);
|
||||
|
||||
// Free validation ranges
|
||||
free(table->dynamic_subnets);
|
||||
free(table->local_subnets);
|
||||
u_free(table->dynamic_subnets);
|
||||
u_free(table->local_subnets);
|
||||
|
||||
free(table);
|
||||
u_free(table);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -327,7 +328,7 @@ bool route_table_insert(struct ROUTE_TABLE *table, const struct ROUTE_ENTRY *ent
|
||||
// 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));
|
||||
struct ROUTE_ENTRY *new_entries = u_realloc(table->entries, new_capacity * sizeof(struct ROUTE_ENTRY));
|
||||
if (!new_entries) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, "route_table_insert: failed to expand routing table to %zu entries", new_capacity);
|
||||
return false;
|
||||
@@ -570,7 +571,7 @@ static bool enhanced_route_add_subnet_range(struct ROUTE_TABLE *table, uint32_t
|
||||
|
||||
// Expand array if needed
|
||||
if (*ranges == NULL) {
|
||||
*ranges = calloc(MAX_SUBNET_VALIDATION_RANGES, sizeof(uint32_t));
|
||||
*ranges = u_calloc(MAX_SUBNET_VALIDATION_RANGES, sizeof(uint32_t));
|
||||
if (!*ranges) return false;
|
||||
*count = 0;
|
||||
}
|
||||
@@ -614,7 +615,7 @@ bool route_get_all_routes(const struct ROUTE_TABLE *table, uint32_t network, uin
|
||||
if (*count == 0) return true; // No routes found, but not an error
|
||||
|
||||
// Allocate result array
|
||||
*routes = calloc(*count, sizeof(struct ROUTE_ENTRY));
|
||||
*routes = u_calloc(*count, sizeof(struct ROUTE_ENTRY));
|
||||
if (!*routes) {
|
||||
*count = 0;
|
||||
return false;
|
||||
|
||||
+10
-9
@@ -12,6 +12,7 @@
|
||||
#include "../lib/debug_config.h"
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include "../lib/mem.h"
|
||||
|
||||
#define IPv4_VERSION 4
|
||||
#define IPv6_VERSION 6
|
||||
@@ -116,7 +117,7 @@ static void routing_pkt_from_etcp_cb(struct ETCP_CONN* conn, struct ll_entry* en
|
||||
}
|
||||
|
||||
// Allocate packet data (TUN will free it)
|
||||
uint8_t* packet_data = malloc(ip_len);
|
||||
uint8_t* packet_data = u_malloc(ip_len);
|
||||
if (!packet_data) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_ROUTING, "Failed to allocate packet data");
|
||||
memory_pool_free(instance->tun->pool, pkt);
|
||||
@@ -135,7 +136,7 @@ static void routing_pkt_from_etcp_cb(struct ETCP_CONN* conn, struct ll_entry* en
|
||||
// Put to TUN input_queue
|
||||
if (queue_data_put(instance->tun->input_queue, (struct ll_entry*)pkt, 0) != 0) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_ROUTING, "Failed to put packet to TUN input_queue");
|
||||
free(packet_data);
|
||||
u_free(packet_data);
|
||||
memory_pool_free(instance->tun->pool, pkt);
|
||||
} else {
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_ROUTING, "Forwarded %zu bytes from ETCP to TUN", ip_len);
|
||||
@@ -169,7 +170,7 @@ static void routing_pkt_from_tun_cb(struct ll_queue* q, void* arg) {
|
||||
uint32_t dst_ip = extract_dst_ip(pkt->ll.dgram, pkt->ll.len);
|
||||
if (dst_ip == 0) {
|
||||
DEBUG_WARN(DEBUG_CATEGORY_ROUTING, "Failed to extract destination IP, dropping packet");
|
||||
free(pkt->ll.dgram);
|
||||
u_free(pkt->ll.dgram);
|
||||
memory_pool_free(instance->tun->pool, pkt);
|
||||
} else {
|
||||
struct in_addr addr;
|
||||
@@ -178,24 +179,24 @@ static void routing_pkt_from_tun_cb(struct ll_queue* q, void* arg) {
|
||||
struct ROUTE_ARRAY* routes = route_table_lookup(instance->rt, dst_ip);
|
||||
if (!routes || routes->routes == 0) {
|
||||
DEBUG_INFO(DEBUG_CATEGORY_ROUTING, "No route to %s, dropping packet", ip_to_str(&addr, AF_INET).str);
|
||||
free(pkt->ll.dgram);
|
||||
u_free(pkt->ll.dgram);
|
||||
memory_pool_free(instance->tun->pool, pkt);
|
||||
} else {
|
||||
struct ETCP_CONN* conn = routes->entries[0]->next_hop;
|
||||
if (!conn) {
|
||||
DEBUG_WARN(DEBUG_CATEGORY_ROUTING, "Route to %s has no next_hop, dropping packet", ip_to_str(&addr, AF_INET).str);
|
||||
free(pkt->ll.dgram);
|
||||
u_free(pkt->ll.dgram);
|
||||
memory_pool_free(instance->tun->pool, pkt);
|
||||
} else if (!conn->normalizer) {
|
||||
DEBUG_WARN(DEBUG_CATEGORY_ROUTING, "Connection for %s has no normalizer, dropping packet", ip_to_str(&addr, AF_INET).str);
|
||||
free(pkt->ll.dgram);
|
||||
u_free(pkt->ll.dgram);
|
||||
memory_pool_free(instance->tun->pool, pkt);
|
||||
} else {
|
||||
size_t etcp_data_len = 1 + pkt->ll.len;
|
||||
struct ll_entry* entry = ll_alloc_lldgram(etcp_data_len);
|
||||
if (!entry) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_ROUTING, "Failed to allocate entry for ETCP send");
|
||||
free(pkt->ll.dgram);
|
||||
u_free(pkt->ll.dgram);
|
||||
memory_pool_free(instance->tun->pool, pkt);
|
||||
} else {
|
||||
entry->dgram[0] = ETCP_ID_DATA;
|
||||
@@ -209,7 +210,7 @@ static void routing_pkt_from_tun_cb(struct ll_queue* q, void* arg) {
|
||||
queue_dgram_free(entry);
|
||||
}
|
||||
|
||||
free(pkt->ll.dgram);
|
||||
u_free(pkt->ll.dgram);
|
||||
memory_pool_free(instance->tun->pool, pkt);
|
||||
}
|
||||
}
|
||||
@@ -217,7 +218,7 @@ static void routing_pkt_from_tun_cb(struct ll_queue* q, void* arg) {
|
||||
}
|
||||
} else {
|
||||
if (pkt->ll.dgram) {
|
||||
free(pkt->ll.dgram);
|
||||
u_free(pkt->ll.dgram);
|
||||
}
|
||||
memory_pool_free(instance->tun->pool, pkt);
|
||||
}
|
||||
|
||||
+18
-17
@@ -10,6 +10,7 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "../lib/mem.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
@@ -36,7 +37,7 @@ static void tun_read_callback(int fd, void* user_arg)
|
||||
if (nread == 0) return;
|
||||
|
||||
// Allocate packet data
|
||||
uint8_t* packet_data = malloc(nread);
|
||||
uint8_t* packet_data = u_malloc(nread);
|
||||
if (!packet_data) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_TUN, "Failed to allocate packet data (size=%zd)", nread);
|
||||
tun->read_errors++;
|
||||
@@ -48,7 +49,7 @@ static void tun_read_callback(int fd, void* user_arg)
|
||||
struct ETCP_FRAGMENT* pkt = (struct ETCP_FRAGMENT*)queue_entry_new_from_pool(tun->pool);
|
||||
if (!pkt) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_TUN, "Failed to allocate ETCP_FRAGMENT from pool");
|
||||
free(packet_data);
|
||||
u_free(packet_data);
|
||||
tun->read_errors++;
|
||||
return;
|
||||
}
|
||||
@@ -62,7 +63,7 @@ static void tun_read_callback(int fd, void* user_arg)
|
||||
// Add to output queue (TUN → routing)
|
||||
if (queue_data_put(tun->output_queue, (struct ll_entry*)pkt, 0) != 0) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_TUN, "Failed to add packet to output queue");
|
||||
free(packet_data);
|
||||
u_free(packet_data);
|
||||
memory_pool_free(tun->pool, pkt);
|
||||
tun->read_errors++;
|
||||
return;
|
||||
@@ -100,7 +101,7 @@ static void tun_input_queue_callback(struct ll_queue* q, void* arg)
|
||||
dump_ip_packet("->TUN", pkt->ll.dgram, pkt->ll.len);
|
||||
}
|
||||
}
|
||||
free(pkt->ll.dgram);
|
||||
u_free(pkt->ll.dgram);
|
||||
}
|
||||
memory_pool_free(tun->pool, pkt);
|
||||
|
||||
@@ -116,7 +117,7 @@ struct tun_if* tun_init(struct UASYNC* ua, struct utun_config* config)
|
||||
|
||||
int test_mode = config->global.tun_test_mode;
|
||||
|
||||
struct tun_if* tun = calloc(1, sizeof(struct tun_if));
|
||||
struct tun_if* tun = u_calloc(1, sizeof(struct tun_if));
|
||||
if (!tun) return NULL;
|
||||
|
||||
tun->ua = ua;
|
||||
@@ -130,7 +131,7 @@ struct tun_if* tun_init(struct UASYNC* ua, struct utun_config* config)
|
||||
if (config->global.tun_ip.family == AF_INET) {
|
||||
snprintf(ip_str, sizeof(ip_str), "%s/32", ip_to_str(&config->global.tun_ip.addr.v4, AF_INET).str);
|
||||
} else {
|
||||
free(tun);
|
||||
u_free(tun);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -138,7 +139,7 @@ struct tun_if* tun_init(struct UASYNC* ua, struct utun_config* config)
|
||||
|
||||
if (!test_mode) {
|
||||
if (tun_platform_init(tun, tun->ifname, ip_str, mtu) != 0) {
|
||||
free(tun);
|
||||
u_free(tun);
|
||||
return NULL;
|
||||
}
|
||||
} else if (tun->ifname[0] == '\0') {
|
||||
@@ -196,7 +197,7 @@ fail:
|
||||
if (tun->input_queue) queue_free(tun->input_queue);
|
||||
if (tun->pool) memory_pool_destroy(tun->pool);
|
||||
if (!test_mode) tun_platform_cleanup(tun);
|
||||
free(tun);
|
||||
u_free(tun);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -223,20 +224,20 @@ void tun_close(struct tun_if* tun)
|
||||
|
||||
struct ETCP_FRAGMENT* pkt;
|
||||
while ((pkt = (struct ETCP_FRAGMENT*)queue_data_get(tun->input_queue)) != NULL) {
|
||||
if (pkt->ll.dgram) free(pkt->ll.dgram);
|
||||
if (pkt->ll.dgram) u_free(pkt->ll.dgram);
|
||||
memory_pool_free(tun->pool, pkt);
|
||||
}
|
||||
queue_free(tun->input_queue);
|
||||
|
||||
while ((pkt = (struct ETCP_FRAGMENT*)queue_data_get(tun->output_queue)) != NULL) {
|
||||
if (pkt->ll.dgram) free(pkt->ll.dgram);
|
||||
if (pkt->ll.dgram) u_free(pkt->ll.dgram);
|
||||
memory_pool_free(tun->pool, pkt);
|
||||
}
|
||||
queue_free(tun->output_queue);
|
||||
|
||||
memory_pool_destroy(tun->pool);
|
||||
if (!tun->test_mode) tun_platform_cleanup(tun);
|
||||
free(tun);
|
||||
u_free(tun);
|
||||
}
|
||||
|
||||
ssize_t tun_write(struct tun_if* tun, const uint8_t* buf, size_t len)
|
||||
@@ -270,7 +271,7 @@ void tun_packet_handler(void* arg) {
|
||||
struct tun_packet_data* pd = (struct tun_packet_data*)arg;
|
||||
if (!pd || !pd->tun || !pd->entry) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_TUN, "ARG ZERO");
|
||||
if (pd) free(pd);
|
||||
if (pd) u_free(pd);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -278,7 +279,7 @@ void tun_packet_handler(void* arg) {
|
||||
struct ll_entry* entry = pd->entry;
|
||||
|
||||
// Освободить временную структуру
|
||||
free(pd);
|
||||
u_free(pd);
|
||||
|
||||
dump_ip_packet("TUNh->", entry->dgram, entry->len);
|
||||
|
||||
@@ -296,12 +297,12 @@ int tun_inject_packet(struct tun_if* tun, const uint8_t* buf, size_t len)
|
||||
{
|
||||
if (!tun || !buf || len == 0 || len > TUN_MAX_PACKET_SIZE) return -1;
|
||||
|
||||
uint8_t* data = malloc(len);
|
||||
uint8_t* data = u_malloc(len);
|
||||
if (!data) { tun->read_errors++; return -1; }
|
||||
memcpy(data, buf, len);
|
||||
|
||||
struct ETCP_FRAGMENT* pkt = (struct ETCP_FRAGMENT*)queue_entry_new_from_pool(tun->pool);
|
||||
if (!pkt) { free(data); tun->read_errors++; return -1; }
|
||||
if (!pkt) { u_free(data); tun->read_errors++; return -1; }
|
||||
|
||||
pkt->seq = 0;
|
||||
pkt->timestamp = 0;
|
||||
@@ -311,7 +312,7 @@ int tun_inject_packet(struct tun_if* tun, const uint8_t* buf, size_t len)
|
||||
int ret = queue_data_put(tun->output_queue, (struct ll_entry*)pkt, 0);
|
||||
|
||||
if (ret != 0) {
|
||||
free(data);
|
||||
u_free(data);
|
||||
memory_pool_free(tun->pool, pkt);
|
||||
tun->read_errors++;
|
||||
return -1;
|
||||
@@ -337,7 +338,7 @@ ssize_t tun_read_packet(struct tun_if* tun, uint8_t* buf, size_t len)
|
||||
memcpy(buf, pkt->ll.dgram, pkt->ll.len);
|
||||
ssize_t ret = pkt->ll.len;
|
||||
|
||||
free(pkt->ll.dgram);
|
||||
u_free(pkt->ll.dgram);
|
||||
memory_pool_free(tun->pool, pkt);
|
||||
|
||||
tun->bytes_written += ret;
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
#include <linux/if.h>
|
||||
#include <linux/if_tun.h>
|
||||
#include <errno.h>
|
||||
#include "../lib/mem.h"
|
||||
|
||||
// Helper for interface ioctl operations
|
||||
static int if_ioctl(const char *ifname, unsigned long request, struct ifreq *ifr) {
|
||||
|
||||
+4
-3
@@ -8,6 +8,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include "../lib/mem.h"
|
||||
|
||||
// Platform detection
|
||||
#ifdef _WIN32
|
||||
@@ -345,12 +346,12 @@ int tun_route_flush(const char *ifname) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
table = (PMIB_IPFORWARDTABLE)malloc(size);
|
||||
table = (PMIB_IPFORWARDTABLE)u_malloc(size);
|
||||
if (!table) return -1;
|
||||
|
||||
ret = GetIpForwardTable(table, &size, 0);
|
||||
if (ret != NO_ERROR) {
|
||||
free(table);
|
||||
u_free(table);
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -361,7 +362,7 @@ int tun_route_flush(const char *ifname) {
|
||||
}
|
||||
}
|
||||
|
||||
free(table);
|
||||
u_free(table);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
+8
-5
@@ -10,6 +10,7 @@
|
||||
#include <iphlpapi.h>
|
||||
#include <ws2tcpip.h>
|
||||
#include <stdio.h>
|
||||
#include "../lib/mem.h"
|
||||
|
||||
// Wintun function pointers
|
||||
static HMODULE wintun_dll = NULL;
|
||||
@@ -262,7 +263,7 @@ DWORD WINAPI tun_read_thread_proc(LPVOID arg)
|
||||
}
|
||||
|
||||
/* === ОДНО копирование === */
|
||||
uint8_t* data = malloc(size);
|
||||
uint8_t* data = u_malloc(size);
|
||||
if (!data) {
|
||||
WintunReleaseReceivePacket(session, wintun_pkt);
|
||||
tun->read_errors++;
|
||||
@@ -273,7 +274,7 @@ DWORD WINAPI tun_read_thread_proc(LPVOID arg)
|
||||
|
||||
struct ETCP_FRAGMENT* pkt = (struct ETCP_FRAGMENT*)queue_entry_new_from_pool(tun->pool);
|
||||
if (!pkt) {
|
||||
free(data);
|
||||
u_free(data);
|
||||
tun->read_errors++;
|
||||
continue;
|
||||
}
|
||||
@@ -281,9 +282,9 @@ DWORD WINAPI tun_read_thread_proc(LPVOID arg)
|
||||
pkt->ll.dgram = data;
|
||||
pkt->ll.len = size;
|
||||
|
||||
struct tun_packet_data* pd = malloc(sizeof(*pd));
|
||||
struct tun_packet_data* pd = u_malloc(sizeof(*pd));
|
||||
if (!pd) {
|
||||
free(data);
|
||||
u_free(data);
|
||||
memory_pool_free(tun->pool, pkt);
|
||||
tun->read_errors++;
|
||||
continue;
|
||||
@@ -295,10 +296,12 @@ DWORD WINAPI tun_read_thread_proc(LPVOID arg)
|
||||
tun->bytes_read += size;
|
||||
tun->packets_read ++;
|
||||
|
||||
// dump_ip_packet("TUN->", data, size); // теперь data вместо buf
|
||||
dump_ip_packet("TUN_THREAD->", data, size); // теперь data вместо buf
|
||||
|
||||
uasync_post(tun->ua, tun_packet_handler, pd);
|
||||
// DEBUG_DEBUG(DEBUG_CATEGORY_TUN, "POST done");
|
||||
}
|
||||
}
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_TUN, "TUN exit");
|
||||
return 0;
|
||||
}
|
||||
@@ -21,6 +21,7 @@
|
||||
#include <getopt.h>
|
||||
#include "../lib/platform_compat.h"
|
||||
#include "../lib/socket_compat.h"
|
||||
#include "../lib/mem.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
|
||||
+9
-8
@@ -18,6 +18,7 @@
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
#include "../lib/platform_compat.h"
|
||||
#include "../lib/mem.h"
|
||||
|
||||
|
||||
|
||||
@@ -197,7 +198,7 @@ struct UTUN_INSTANCE* utun_instance_create(struct UASYNC* ua, const char *config
|
||||
}
|
||||
|
||||
// Allocate instance
|
||||
struct UTUN_INSTANCE *instance = calloc(1, sizeof(struct UTUN_INSTANCE));
|
||||
struct UTUN_INSTANCE *instance = u_calloc(1, sizeof(struct UTUN_INSTANCE));
|
||||
if (!instance) {
|
||||
free_config(config);
|
||||
return NULL;
|
||||
@@ -207,7 +208,7 @@ struct UTUN_INSTANCE* utun_instance_create(struct UASYNC* ua, const char *config
|
||||
if (instance_init_common(instance, ua, config) != 0) {
|
||||
// Cleanup on error
|
||||
if (instance->config) free_config(instance->config);
|
||||
free(instance);
|
||||
u_free(instance);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -222,7 +223,7 @@ struct UTUN_INSTANCE* utun_instance_create_from_config(struct UASYNC* ua, struct
|
||||
}
|
||||
|
||||
// Allocate instance
|
||||
struct UTUN_INSTANCE *instance = calloc(1, sizeof(struct UTUN_INSTANCE));
|
||||
struct UTUN_INSTANCE *instance = u_calloc(1, sizeof(struct UTUN_INSTANCE));
|
||||
if (!instance) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, "Failed to allocate UTUN_INSTANCE");
|
||||
return NULL;
|
||||
@@ -231,7 +232,7 @@ struct UTUN_INSTANCE* utun_instance_create_from_config(struct UASYNC* ua, struct
|
||||
// Initialize using common function (config ownership transferred to instance)
|
||||
if (instance_init_common(instance, ua, config) != 0) {
|
||||
// Cleanup on error - caller still owns config since we failed
|
||||
free(instance);
|
||||
u_free(instance);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -254,7 +255,7 @@ void utun_instance_destroy(struct UTUN_INSTANCE *instance) {
|
||||
if (instance->control_srv) {
|
||||
DEBUG_INFO(DEBUG_CATEGORY_CONTROL, "Shutting down control server");
|
||||
control_server_shutdown(instance->control_srv);
|
||||
free(instance->control_srv);
|
||||
u_free(instance->control_srv);
|
||||
instance->control_srv = NULL;
|
||||
}
|
||||
|
||||
@@ -346,7 +347,7 @@ void utun_instance_destroy(struct UTUN_INSTANCE *instance) {
|
||||
|
||||
// Free the instance memory
|
||||
DEBUG_INFO(DEBUG_CATEGORY_MEMORY, "[INSTANCE_DESTROY] Freeing instance memory");
|
||||
free(instance);
|
||||
u_free(instance);
|
||||
DEBUG_INFO(DEBUG_CATEGORY_MEMORY, "[INSTANCE_DESTROY] Instance destroyed completely");
|
||||
}
|
||||
|
||||
@@ -390,12 +391,12 @@ int utun_instance_init(struct UTUN_INSTANCE *instance) {
|
||||
|
||||
// Initialize control server if configured
|
||||
if (instance->config->global.control_sock.ss_family != 0) {
|
||||
instance->control_srv = (struct control_server*)calloc(1, sizeof(struct control_server));
|
||||
instance->control_srv = (struct control_server*)u_calloc(1, sizeof(struct control_server));
|
||||
if (instance->control_srv) {
|
||||
if (control_server_init(instance->control_srv, instance->ua, instance,
|
||||
&instance->config->global.control_sock, 8) != 0) {
|
||||
DEBUG_WARN(DEBUG_CATEGORY_CONTROL, "Failed to initialize control server, continuing without monitoring");
|
||||
free(instance->control_srv);
|
||||
u_free(instance->control_srv);
|
||||
instance->control_srv = NULL;
|
||||
} else {
|
||||
DEBUG_INFO(DEBUG_CATEGORY_CONTROL, "Control server initialized successfully");
|
||||
|
||||
Reference in new issue
Block a user