From c420b8f18e8cda664ad7a4a98ce5bc99b59d4355 Mon Sep 17 00:00:00 2001 From: jeka Date: Sun, 1 Mar 2026 02:48:03 +0300 Subject: [PATCH] =?UTF-8?q?=D0=BF=D0=B5=D1=80=D0=B4=D0=B0=D0=B9=D1=82=20ic?= =?UTF-8?q?mp=20=D0=B2=20tun?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- filelist.txt | 1 + lib/debug_config.c | 1 + src/Makefile.am | 1 + src/config_parser.c | 16 ++++++++-------- src/config_updater.c | 20 ++++++++++---------- src/packet_dump.h | 5 +++-- src/tun_if.c | 5 +++++ src/tun_windows.c | 2 ++ src/utun.c | 8 ++++++-- src/utun_instance.c | 12 ++++++++++++ tests/Makefile.am | 1 + 11 files changed, 50 insertions(+), 22 deletions(-) diff --git a/filelist.txt b/filelist.txt index d4af9b1..90b499f 100644 --- a/filelist.txt +++ b/filelist.txt @@ -24,6 +24,7 @@ src/etcp_connections.h src/etcp_loadbalancer.c src/etcp_loadbalancer.h src/packet_dump.h +src/packet_dump.c src/pkt_normalizer.c src/pkt_normalizer.h src/route_lib.c diff --git a/lib/debug_config.c b/lib/debug_config.c index 3c2dc4a..cf4c758 100644 --- a/lib/debug_config.c +++ b/lib/debug_config.c @@ -109,6 +109,7 @@ void debug_config_init(void) { /* Set debug level */ void debug_set_level(debug_level_t level) { g_debug_config.level = level; + g_debug_config.console_level = level; } /* Enable/disable specific categories */ diff --git a/src/Makefile.am b/src/Makefile.am index 01abab6..8fff0fb 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -17,6 +17,7 @@ utun_CORE_SOURCES = \ secure_channel.c \ crc32.c \ pkt_normalizer.c \ + packet_dump.c \ etcp_api.c \ control_server.c diff --git a/src/config_parser.c b/src/config_parser.c index e7b90d3..53a3805 100644 --- a/src/config_parser.c +++ b/src/config_parser.c @@ -503,24 +503,24 @@ static struct utun_config* parse_config_internal(FILE *fp, const char *filename) 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); + DEBUG_WARN(DEBUG_CATEGORY_CONFIG, "%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); + DEBUG_WARN(DEBUG_CATEGORY_CONFIG, "%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); + DEBUG_WARN(DEBUG_CATEGORY_CONFIG, "%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); + DEBUG_WARN(DEBUG_CATEGORY_CONFIG, "%s:%d: Invalid client key '%s'", filename, line_num, key); } break; case SECTION_ROUTING: @@ -531,7 +531,7 @@ static struct utun_config* parse_config_internal(FILE *fp, const char *filename) } break; default: - printf( "%s:%d: Key outside section: %s", filename, line_num, key); + DEBUG_WARN(DEBUG_CATEGORY_CONFIG, "%s:%d: Key outside section: %s", filename, line_num, key); break; } } @@ -565,14 +565,14 @@ error: } struct utun_config* parse_config(const char *filename) { - printf("[CONFIG DEBUG] Opening config file: %s\n", filename); + DEBUG_DEBUG(DEBUG_CATEGORY_CONFIG, "Opening config file: %s", filename); FILE *fp = fopen(filename, "r"); if (!fp) { - printf("[CONFIG ERROR] Failed to open config file: %s (errno=%d)\n", filename, errno); + DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "Failed to open config file: %s (errno=%d)", filename, errno); return NULL; } - printf("[CONFIG DEBUG] Successfully opened config file\n"); + DEBUG_DEBUG(DEBUG_CATEGORY_CONFIG, "Successfully opened config file"); struct utun_config *config = parse_config_internal(fp, filename); fclose(fp); return config; diff --git a/src/config_updater.c b/src/config_updater.c index a717d39..6db5100 100644 --- a/src/config_updater.c +++ b/src/config_updater.c @@ -247,7 +247,7 @@ 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); + DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "Failed to parse config: %s", filename); return -1; } @@ -255,7 +255,7 @@ int config_ensure_keys_and_node_id(const char *filename) { 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", + DEBUG_DEBUG(DEBUG_CATEGORY_CONFIG, "Checking config - priv_key='%s' (len=%zu), pub_key='%s' (len=%zu), node_id=%llu", 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", @@ -271,7 +271,7 @@ int config_ensure_keys_and_node_id(const char *filename) { 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", + DEBUG_DEBUG(DEBUG_CATEGORY_CONFIG, "Validation results - need_priv_key=%d, need_pub_key=%d, need_node_id=%d", need_priv_key, need_pub_key, need_node_id); @@ -290,7 +290,7 @@ int config_ensure_keys_and_node_id(const char *filename) { // Generate new keypair if private key is invalid struct SC_MYKEYS mykeys; if (sc_generate_keypair(&mykeys) != SC_OK) { - printf( "Failed to generate keypair"); + DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "Failed to generate keypair"); free_config(config); @@ -309,7 +309,7 @@ int config_ensure_keys_and_node_id(const char *filename) { 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"); + DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "Invalid private key hex format"); free_config(config); @@ -320,7 +320,7 @@ int config_ensure_keys_and_node_id(const char *filename) { // 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"); + DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "Failed to compute public key from private key"); free_config(config); @@ -350,7 +350,7 @@ int config_ensure_keys_and_node_id(const char *filename) { 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); + DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "Failed to read config file: %s", filename); return -1; } @@ -392,14 +392,14 @@ int config_ensure_keys_and_node_id(const char *filename) { } if (ret == 0) { - printf("[CONFIG DEBUG] Writing updated config file, work_len=%zu\n", work_len); + DEBUG_DEBUG(DEBUG_CATEGORY_CONFIG, "Writing updated config file, work_len=%zu", work_len); if (write_mem_to_file(filename, work_buf, work_len) < 0) { - printf( "Failed to write updated config file: %s", filename); + DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "Failed to write updated config file: %s", filename); ret = -1; } else { - printf("[CONFIG DEBUG] Successfully updated config file: %s\n", filename); + DEBUG_DEBUG(DEBUG_CATEGORY_CONFIG, "Successfully updated config file: %s", filename); } } diff --git a/src/packet_dump.h b/src/packet_dump.h index c93fc88..8fd391b 100644 --- a/src/packet_dump.h +++ b/src/packet_dump.h @@ -2,7 +2,6 @@ #include #include #include "../lib/platform_compat.h" -#include // Packet dump function for debugging static void dump_packet(const char* direction, const uint8_t* data, size_t len, struct sockaddr_storage* addr) { @@ -39,4 +38,6 @@ static void dump_packet(const char* direction, const uint8_t* data, size_t len, } printf("\n"); -} \ No newline at end of file +} + +void dump_ip_packet(const char* direction, const uint8_t* data, size_t len); \ No newline at end of file diff --git a/src/tun_if.c b/src/tun_if.c index fa429e7..2ac6aef 100644 --- a/src/tun_if.c +++ b/src/tun_if.c @@ -2,6 +2,7 @@ #include "config_parser.h" #include "tun_if.h" #include "etcp.h" +#include "packet_dump.h" #include "../lib/debug_config.h" #include "../lib/u_async.h" #include "../lib/ll_queue.h" @@ -71,6 +72,7 @@ static void tun_read_callback(int fd, void* user_arg) tun->bytes_read += nread; tun->packets_read++; DEBUG_DEBUG(DEBUG_CATEGORY_TUN, "Read %zd bytes from TUN %s", nread, tun->ifname); + dump_ip_packet("TUN->", buffer, nread); } #endif @@ -95,6 +97,7 @@ static void tun_input_queue_callback(struct ll_queue* q, void* arg) } else { tun->bytes_written += n; tun->packets_written++; + dump_ip_packet("->TUN", pkt->ll.dgram, pkt->ll.len); } } free(pkt->ll.dgram); @@ -253,6 +256,7 @@ ssize_t tun_write(struct tun_if* tun, const uint8_t* buf, size_t len) if (n > 0) { tun->bytes_written += n; tun->packets_written++; + dump_ip_packet("->TUN", buf, len); } else { tun->write_errors++; } @@ -319,5 +323,6 @@ ssize_t tun_read_packet(struct tun_if* tun, uint8_t* buf, size_t len) tun->bytes_written += ret; tun->packets_written++; + dump_ip_packet("TUN->", buf, ret); return ret; } diff --git a/src/tun_windows.c b/src/tun_windows.c index e250dbb..227b81c 100644 --- a/src/tun_windows.c +++ b/src/tun_windows.c @@ -2,6 +2,7 @@ #include "../lib/debug_config.h" #include "../lib/u_async.h" #include "tun_if.h" +#include "packet_dump.h" #include "wintun.h" #include "ll_queue.h" // ← добавлено #include "etcp.h" // ← добавлено (struct ETCP_FRAGMENT) @@ -288,6 +289,7 @@ DWORD WINAPI tun_read_thread_proc(LPVOID arg) } else { tun->bytes_read += n; tun->packets_read++; + dump_ip_packet("TUN->", buf, n); } } diff --git a/src/utun.c b/src/utun.c index b48b733..4033771 100644 --- a/src/utun.c +++ b/src/utun.c @@ -255,7 +255,7 @@ static void signal_handler(int sig) { void test_tmr(void* arg) { struct UASYNC* ua = (struct UASYNC*)arg; uasync_set_timeout(ua, 10000, ua, test_tmr); - DEBUG_INFO(DEBUG_CATEGORY_ETCP, "tick ..."); +// DEBUG_INFO(DEBUG_CATEGORY_ETCP, "tick ..."); } int main(int argc, char *argv[]) { @@ -274,12 +274,16 @@ int main(int argc, char *argv[]) { debug_config_init(); debug_set_level(DEBUG_LEVEL_TRACE); // debug_set_categories(DEBUG_CATEGORY_ALL & ~DEBUG_CATEGORY_UASYNC & ~DEBUG_CATEGORY_TIMERS & ~DEBUG_CATEGORY_CRYPTO & ~DEBUG_CATEGORY_ETCP & ~DEBUG_CATEGORY_CONNECTION); // Enable all except uasync - debug_set_categories(DEBUG_CATEGORY_ALL); + debug_set_categories(DEBUG_CATEGORY_ALL & ~DEBUG_CATEGORY_UASYNC & ~DEBUG_CATEGORY_TIMERS); debug_enable_function_name(1); if (args.debug_config) { + DEBUG_INFO(DEBUG_CATEGORY_MEMORY, "Apply debug CLI args"); debug_parse_config(args.debug_config); } + else { + DEBUG_DEBUG(DEBUG_CATEGORY_MEMORY, "No debug CLI args - set all"); + } // Enable file logging (default: utun.log, truncate on start) const char* log_file = args.log_file ? args.log_file : "utun.log"; diff --git a/src/utun_instance.c b/src/utun_instance.c index a67d459..92b64e9 100644 --- a/src/utun_instance.c +++ b/src/utun_instance.c @@ -147,6 +147,18 @@ struct UTUN_INSTANCE* utun_instance_create(struct UASYNC* ua, const char *config debug_set_output_file(config->global.log_file); } + // Apply debug level from config + if (config->global.debug_level[0]) { + debug_level_t level = DEBUG_LEVEL_INFO; + if (strcmp(config->global.debug_level, "error") == 0) level = DEBUG_LEVEL_ERROR; + else if (strcmp(config->global.debug_level, "warn") == 0) level = DEBUG_LEVEL_WARN; + else if (strcmp(config->global.debug_level, "info") == 0) level = DEBUG_LEVEL_INFO; + else if (strcmp(config->global.debug_level, "debug") == 0) level = DEBUG_LEVEL_DEBUG; + else if (strcmp(config->global.debug_level, "trace") == 0) level = DEBUG_LEVEL_TRACE; + debug_set_level(level); + DEBUG_INFO(DEBUG_CATEGORY_CONFIG, "Applied debug_level from config: %s", config->global.debug_level); + } + // Allocate instance struct UTUN_INSTANCE *instance = calloc(1, sizeof(struct UTUN_INSTANCE)); if (!instance) { diff --git a/tests/Makefile.am b/tests/Makefile.am index ad2f5e8..b76b040 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -73,6 +73,7 @@ ETCP_FULL_OBJS = \ $(top_builddir)/src/utun-routing.o \ $(top_builddir)/src/utun-tun_if.o \ $(top_builddir)/src/utun-tun_route.o \ + $(top_builddir)/src/utun-packet_dump.o \ $(TUN_PLATFORM_OBJ) \ $(top_builddir)/src/utun-utun_instance.o \ $(ETCP_CORE_OBJS)