From b20bff32dd44300736b04e5c5d42137595b7feaf Mon Sep 17 00:00:00 2001 From: jeka Date: Wed, 4 Mar 2026 01:53:17 +0300 Subject: [PATCH] config --- AGENTS.md | 6 +++--- doc/etcp_protocol.txt | 2 +- lib/debug_config.c | 28 ++++++++++++++++++++++++++-- lib/debug_config.h | 16 +++++++++++----- src/Makefile.am | 1 + src/config_parser.c | 4 +++- src/config_parser.h | 7 +++++++ src/etcp.c | 16 ++++++++++------ tests/Makefile.am | 3 ++- 9 files changed, 64 insertions(+), 19 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index a261992..8a2c3b9 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -12,15 +12,15 @@ This file contains essential information for AI coding agents working in the uTu ## πŸ”§ Build Commands ### Full Build -win: powershell build.bat (Π±Π΅Π· Π°Ρ€Π³ΡƒΠΌΠ΅Π½Ρ‚ΠΎΠ² ΠΈ Π΄ΠΎΠΏΠΎΠ»Π½ΠΈΡ‚Π΅Π»ΡŒΠ½Ρ‹Ρ… ΠΊΠΎΠΌΠ°Π½Π΄, Ρ‚Π΅ΠΊΡƒΡ‰ΠΈΠΉ ΠΊΠ°Ρ‚Π°Π»ΠΎΠ³ Π½Π΅ Π²Π°ΠΆΠ΅Π½) +win: powershell build_full.bat (Π² точности ΠΊΠ°ΠΊ написано Π±Π΅Π· Π΄ΠΎΠΏΠΎΠ»Π½ΠΈΡ‚Π΅Π»ΡŒΠ½Ρ‹Ρ… ΠΎΠΏΡ†ΠΈΠΉ powershell, Ρ‚Π΅ΠΊΡƒΡ‰ΠΈΠΉ ΠΊΠ°Ρ‚Π°Π»ΠΎΠ³ Π½Π΅ Π²Π°ΠΆΠ΅Π½) ```bash -./autogen.sh # Generate configure script (if needed) ./configure # Configure build make # Build everything make install # Install (requires sudo) ``` ### Partial Builds +win: powershell build.bat (Π°Π½Π°Π»ΠΎΠ³ make Ссли Π½Π΅ ΠΈΠ·ΠΌΠ΅Π½Π΅Π½ makefile.am) ```bash cd lib && make # Build only the library cd src && make # Build only the main program @@ -28,7 +28,6 @@ cd tests && make # Build only tests ``` ### Clean Build -win: powershell build_full.bat (Π±Π΅Π· Π°Ρ€Π³ΡƒΠΌΠ΅Π½Ρ‚ΠΎΠ², Ρ‚Π΅ΠΊΡƒΡ‰ΠΈΠΉ ΠΊΠ°Ρ‚Π°Π»ΠΎΠ³ Π½Π΅ Π²Π°ΠΆΠ΅Π½) ```bash make clean # Clean object files make distclean # Clean everything including configure files @@ -37,6 +36,7 @@ make distclean # Clean everything including configure files ## πŸ§ͺ Test Commands ### Run All Tests +win: powershell check.bat ```bash make check # Run all tests via automake ``` diff --git a/doc/etcp_protocol.txt b/doc/etcp_protocol.txt index c2a0f33..6464ce4 100644 --- a/doc/etcp_protocol.txt +++ b/doc/etcp_protocol.txt @@ -102,7 +102,7 @@ bandwidth ΠΏΠΎ ΠΊΠ°ΠΆΠ΄ΠΎΠΌΡƒ Π»ΠΈΠ½ΠΊΡƒ Π°Π΄Π°ΠΏΡ‚ΠΈΠ²Π½ΠΎ подстраива **** Π€ΠΎΡ€ΠΌΠ°Ρ‚ ΠΊΠΎΠ΄ΠΎΠ³Ρ€Π°ΠΌΠΌ для etcp_connections.c/h **** ΠšΠΎΠ΄ΠΎΠ³Ρ€Π°ΠΌΠΌΡ‹ с этими сСкциями ΠΎΠ±Ρ€Π°Π±Π°Ρ‚Ρ‹Π²Π°ΡŽΡ‚ΡΡ Π² etcp_connections (Π² этих ΠΊΠΎΠ΄ΠΎΠ³Ρ€Π°ΠΌΠΌΠ°Ρ… всСгда Ρ‚ΠΎΠ»ΡŒΠΊΠΎ ΠΎΠ΄Π½Π° сСкция). Π² ΠΎΠ±ΡΠ·Π°Ρ‚Π΅Π»ΡŒΠ½ΠΎΠΌ Π·Π°Π³ΠΎΠ»ΠΎΠ²ΠΊΠ΅ ID Π½Π΅ ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΡƒΠ΅Ρ‚ΡΡ, ΠΏΡ€ΠΈ ΠΏΠ΅Ρ€Π΅Π΄Π°Ρ‡Π΅ для порядка =0: [БПОРНО: Π·Π°Π³ΠΎΠ»ΠΎΠ²ΠΎΠΊ Π² etcp β€” Ρ‚ΠΎΠ»ΡŒΠΊΠΎ 2 Π±Π°ΠΉΡ‚Π° TS; здСсь подразумСваСтся ID? Π£Ρ‚ΠΎΡ‡Π½ΠΈΡ‚ΡŒ Π΅Π΄ΠΈΠ½Ρ‹ΠΉ Ρ„ΠΎΡ€ΠΌΠ°Ρ‚] 1) Init запрос - Π·Π°Π³ΠΎΠ»ΠΎΠ²ΠΎΠΊ 0x02 (со сбросом etcp сСссии) ΠΈΠ»ΠΈ 0x04 (Π±Π΅Π· сброса): - [0x02/0x04] [my_node_id 64bit] [my mtu high] [my mtu low] [keepalive high] [keepalive low] [my link_id 1 Π±Π°ΠΉΡ‚] [my public key (64 Π±Π°ΠΉΡ‚Π°, Π½Π΅ ΡˆΠΈΡ„Ρ€ΡƒΠ΅Ρ‚ΡΡ)] + [0x02/0x04] [my_node_id 64bit] [my mtu high] [my mtu low] [keepalive high] [keepalive low] [recovery high] [recovery low] [my link_id 1 Π±Π°ΠΉΡ‚] [my public key (64 Π±Π°ΠΉΡ‚Π°, Π½Π΅ ΡˆΠΈΡ„Ρ€ΡƒΠ΅Ρ‚ΡΡ)] - Π˜Π½ΠΈΡ†ΠΈΠΈΡ€ΡƒΠ΅Ρ‚ Π½ΠΎΠ²Ρ‹ΠΉ connection для tcp instance. Ссли tcp instance Π½Π΅Ρ‚ (ΠΏΠ΅Ρ€Π²ΠΎΠ΅ ΠΏΠΎΠ΄ΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠ΅) - создаёт. ΠœΠ΅ΠΆΠ΄Ρƒ Π½ΠΎΠ΄Π°ΠΌΠΈ Ρ‚ΠΎΠ»ΡŒΠΊΠΎ ΠΎΠ΄Π½ΠΎ ΠΏΠΎΠ΄ΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠ΅, Π½ΠΎ ΠΌΠΎΠΆΠ½ΠΎ Π΄ΠΎΠ±Π°Π²Π»ΡΡ‚ΡŒ ΠΊΠ°Π½Π°Π»Ρ‹. - link_id: Π»ΠΎΠΊΠ°Π»ΡŒΠ½Ρ‹ΠΉ ΠΈΠ΄Π΅Π½Ρ‚ΠΈΡ„ΠΈΠΊΠ°Ρ‚ΠΎΡ€ ΠΊΠ°Π½Π°Π»Π° (0-255), назначаСтся ΠΎΡ‚ΠΏΡ€Π°Π²ΠΈΡ‚Π΅Π»Π΅ΠΌ для ΠΈΠ΄Π΅Π½Ρ‚ΠΈΡ„ΠΈΠΊΠ°Ρ†ΠΈΠΈ ΠΊΠ°Π½Π°Π»Π° - ΠŸΡƒΠ±Π»ΠΈΡ‡Π½Ρ‹ΠΉ ΠΊΠ»ΡŽΡ‡ отправляСтся Π² ΠΊΠΎΠ½Ρ†Π΅ ΠΏΠ°ΠΊΠ΅Ρ‚Π° Π±Π΅Π· ΡˆΠΈΡ„Ρ€ΠΎΠ²Π°Π½ΠΈΡ, Ρ‡Ρ‚ΠΎΠ±Ρ‹ ΠΏΠΎΠ»ΡƒΡ‡Π°Ρ‚Π΅Π»ΡŒ ΠΌΠΎΠ³ ΡƒΡΡ‚Π°Π½ΠΎΠ²ΠΈΡ‚ΡŒ Π΅Π³ΠΎ ΠΈ Ρ€Π°ΡΡˆΠΈΡ„Ρ€ΠΎΠ²Π°Ρ‚ΡŒ ΠΎΡΡ‚Π°Π»ΡŒΠ½ΡƒΡŽ Ρ‡Π°ΡΡ‚ΡŒ ΠΏΠ°ΠΊΠ΅Ρ‚Π° (Ρ‚.ΠΊ. ΠΈΠ½ΠΈΡ†ΠΈΠ°Ρ‚ΠΎΡ€ соСдинСния всСгда ΠΈΠΌΠ΅Π΅Ρ‚ ΠΎΡ€ΠΈΠ³ΠΈΠ½Π°Π»ΡŒΠ½Ρ‹ΠΉ peer public key Π² ΠΊΠΎΠ½Ρ„ΠΈΠ³Π΅ - ΠΏΠΎ Π½Π΅ΠΌΡƒ ΠΈΡΠΊΠ»ΡŽΡ‡Π°Π΅ΠΌ MITM) diff --git a/lib/debug_config.c b/lib/debug_config.c index b49a433..d1c1bd5 100644 --- a/lib/debug_config.c +++ b/lib/debug_config.c @@ -75,6 +75,7 @@ static debug_category_t get_category_by_name(const char* name) { {"tun", DEBUG_CATEGORY_TUN}, {"routing", DEBUG_CATEGORY_ROUTING}, {"timers", DEBUG_CATEGORY_TIMERS}, + {"dump", DEBUG_CATEGORY_DUMP}, {"all", DEBUG_CATEGORY_ALL}, {NULL, 0} }; @@ -92,8 +93,11 @@ debug_config_t g_debug_config; /* Initialize debug system with default settings */ void debug_config_init(void) { - g_debug_config.level = DEBUG_LEVEL_TRACE; // Global level + g_debug_config.level = DEBUG_LEVEL_ERROR; // Global level: errors only by default g_debug_config.categories = DEBUG_CATEGORY_ALL; + for (int i = 0; i < DEBUG_CATEGORY_COUNT; i++) { + g_debug_config.category_levels[i] = DEBUG_LEVEL_NONE; // 0 = disabled per category + } g_debug_config.timestamp_enabled = 1; g_debug_config.function_name_enabled = 1; g_debug_config.file_line_enabled = 1; @@ -112,6 +116,18 @@ void debug_set_level(debug_level_t level) { g_debug_config.console_level = level; } +/* Set debug level for specific category (0 = disabled, otherwise uses that level) */ +void debug_set_category_level(debug_category_t category, debug_level_t level) { + if (category == DEBUG_CATEGORY_NONE) return; + int idx = 0; + while (!(category & (1ULL << idx)) && idx < DEBUG_CATEGORY_COUNT) { + idx++; + } + if (idx < DEBUG_CATEGORY_COUNT) { + g_debug_config.category_levels[idx] = level; + } +} + /* Enable/disable specific categories */ void debug_enable_category(debug_category_t category) { g_debug_config.categories |= category; @@ -200,8 +216,16 @@ int debug_should_output(debug_level_t level, debug_category_t category) { return 0; } + /* Determine effective level: max(global, category) */ + int idx = 0; + while (!(category & (1ULL << idx)) && idx < DEBUG_CATEGORY_COUNT) { + idx++; + } + debug_level_t cat_level = (idx < DEBUG_CATEGORY_COUNT) ? g_debug_config.category_levels[idx] : DEBUG_LEVEL_NONE; + debug_level_t effective = (cat_level > g_debug_config.level) ? cat_level : g_debug_config.level; + /* Check if level is sufficient */ - if (level > g_debug_config.level) { + if (level > effective) { return 0; } diff --git a/lib/debug_config.h b/lib/debug_config.h index 3d39561..d5af2f6 100644 --- a/lib/debug_config.h +++ b/lib/debug_config.h @@ -51,12 +51,15 @@ typedef uint64_t debug_category_t; #define DEBUG_CATEGORY_BGP ((debug_category_t)1 << 12) // BGP route exchange #define DEBUG_CATEGORY_SOCKET ((debug_category_t)1 << 13) // Socket operations #define DEBUG_CATEGORY_CONTROL ((debug_category_t)1 << 14) // Control/monitoring server +#define DEBUG_CATEGORY_DUMP ((debug_category_t)1 << 15) // Packet dump/logging +#define DEBUG_CATEGORY_COUNT 16 // Total number of categories #define DEBUG_CATEGORY_ALL ((debug_category_t)0xFFFFFFFFFFFFFFFFULL) /* Debug configuration structure */ typedef struct { - debug_level_t level; // Global debug level + debug_level_t level; // Global debug level (default: ERROR) debug_category_t categories; // Enabled categories (bitmask) + debug_level_t category_levels[DEBUG_CATEGORY_COUNT]; // Per-category levels (0 = disabled) int timestamp_enabled; // Include timestamps in output int function_name_enabled; // Include function names int file_line_enabled; // Include file:line info @@ -75,10 +78,13 @@ 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 */ +/* Set debug level */ +void debug_set_level(debug_level_t level); + +/* Set debug level for specific category (0 = disabled, otherwise uses that level) */ +void debug_set_category_level(debug_category_t category, 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(debug_category_t categories); diff --git a/src/Makefile.am b/src/Makefile.am index 8fff0fb..1568bf2 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -14,6 +14,7 @@ utun_CORE_SOURCES = \ etcp.c \ etcp_connections.c \ etcp_loadbalancer.c \ + etcp_debug.c \ secure_channel.c \ crc32.c \ pkt_normalizer.c \ diff --git a/src/config_parser.c b/src/config_parser.c index b03862c..edc17ff 100644 --- a/src/config_parser.c +++ b/src/config_parser.c @@ -27,7 +27,8 @@ typedef enum { SECTION_GLOBAL, SECTION_SERVER, SECTION_CLIENT, - SECTION_ROUTING + SECTION_ROUTING, + SECTION_DEBUG } section_type_t; /* Forward declaration for new functions */ @@ -426,6 +427,7 @@ static section_type_t parse_section_header(const char *line, char *name, size_t if (strcasecmp(section, "global") == 0) return SECTION_GLOBAL; if (strcasecmp(section, "routing") == 0) return SECTION_ROUTING; + if (strcasecmp(section, "debug") == 0) return SECTION_DEBUG; char *colon = strchr(section, ':'); if (!colon) return SECTION_UNKNOWN; diff --git a/src/config_parser.h b/src/config_parser.h index a957559..e2c3373 100644 --- a/src/config_parser.h +++ b/src/config_parser.h @@ -76,6 +76,13 @@ struct global_config { int enable_file_lines; // enable file:line in logs int enable_colors; // enable ANSI colors in logs + // Per-category debug levels (loaded from [debug] section) + struct { + char category[16][16]; // category name + char level[16][16]; // level string + int count; + } debug_levels; + int tun_test_mode; // test mode: 1 = don't open real TUN, queues only int keepalive_timeout; // keepalive timeout in ms (default: 2000) int keepalive_interval; // keepalive interval in ms (default: 200) diff --git a/src/etcp.c b/src/etcp.c index 5cf93e7..ec97e7d 100644 --- a/src/etcp.c +++ b/src/etcp.c @@ -1,6 +1,7 @@ // etcp.c - ETCP Protocol Implementation (refactored and expanded based on etcp_protocol.txt) #include "etcp.h" +#include "etcp_debug.h" #include "etcp_loadbalancer.h" #include "routing.h" #include "route_bgp.h" @@ -642,10 +643,11 @@ struct ETCP_DGRAM* etcp_request_pkt(struct ETCP_CONN* etcp) { DEBUG_DEBUG(DEBUG_CATEGORY_ETCP, "[%s] only ACK packet with %d bytes total", etcp->log_name, dgram->data_len); } - dgram->data_len=ptr; - - - return dgram; + dgram->data_len=ptr; + + etcp_dump_pkt_sections(dgram, link, 1); + + return dgram; } // Callback for when a link is ready to send data @@ -801,8 +803,10 @@ void etcp_ack_recv(struct ETCP_CONN* etcp, uint32_t seq, uint16_t ts, uint16_t d void etcp_conn_input(struct ETCP_DGRAM* pkt) { DEBUG_TRACE(DEBUG_CATEGORY_ETCP, ""); if (!pkt || !pkt->data_len) return; - - struct ETCP_CONN* etcp = pkt->link->etcp; + + etcp_dump_pkt_sections(pkt, pkt->link, 0); + + 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 diff --git a/tests/Makefile.am b/tests/Makefile.am index 232f970..ab8c3d8 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -55,7 +55,8 @@ ETCP_CORE_OBJS = \ $(top_builddir)/src/utun-etcp_connections.o \ $(top_builddir)/src/utun-etcp_loadbalancer.o \ $(top_builddir)/src/utun-pkt_normalizer.o \ - $(top_builddir)/src/utun-etcp_api.o + $(top_builddir)/src/utun-etcp_api.o \ + $(top_builddir)/src/utun-etcp_debug.o # Platform-specific TUN objects if OS_WINDOWS