diff --git a/src/etcp_connections.c b/src/etcp_connections.c index 1d6d1666..758083c5 100644 --- a/src/etcp_connections.c +++ b/src/etcp_connections.c @@ -12,7 +12,12 @@ #include "etcp.h" #include "../lib/memory_pool.h" #include "../lib/u_async.h" +#include "../lib/debug_config.h" #include + +// 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 // Simple debug macros to replace missing debug_config.h @@ -25,12 +30,77 @@ // Forward declaration static void etcp_connections_read_callback(int fd, void* arg); -#define DEBUG_ERROR(category, fmt, ...) fprintf(stderr, "ERROR: " fmt "\n", ##__VA_ARGS__) -#define DEBUG_WARN(category, fmt, ...) fprintf(stderr, "WARN: " fmt "\n", ##__VA_ARGS__) -#define DEBUG_INFO(category, fmt, ...) fprintf(stdout, "INFO: " fmt "\n", ##__VA_ARGS__) - -// Minimal packet dump - only shows first 16 bytes to avoid slowing down +// 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++) { @@ -417,7 +487,7 @@ int etcp_encrypt_send(struct ETCP_DGRAM* dgram) { dgram->timestamp=get_current_timestamp(); // DUMP: Show packet before encryption - dump_packet_bytes("ECTP_ENCRYPT_SEND", dgram->data, dgram->data_len); + 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); @@ -485,7 +555,7 @@ static void etcp_connections_read_callback(int fd, void* arg) { // printf("[ETCP] Received packet: %zd bytes from address\n", recv_len); // DUMP: Show received packet content - dump_packet_bytes("RECV in:", data, recv_len); + 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; @@ -615,7 +685,7 @@ static void etcp_connections_read_callback(int fd, void* arg) { return; // INIT_RESPONSE is handled, no further processing needed } - dump_packet_bytes("RECV decrypted:", pkt->data, pkt->data_len); + packet_dump("RECV decrypted:", pkt->data, pkt->data_len, link); etcp_conn_input(pkt); return; diff --git a/test_packet_dump.c b/test_packet_dump.c new file mode 100644 index 00000000..e69de29b diff --git a/tests/debug_test b/tests/debug_test new file mode 100755 index 00000000..788f6c79 Binary files /dev/null and b/tests/debug_test differ diff --git a/tests/debug_test.c b/tests/debug_test.c new file mode 100644 index 00000000..66385cf6 --- /dev/null +++ b/tests/debug_test.c @@ -0,0 +1,11 @@ +#include "../lib/debug_config.h" +#include "../src/etcp_connections.h" + +int main() { + debug_config_init(); + debug_set_level(DEBUG_LEVEL_DEBUG); + debug_enable_category(DEBUG_CATEGORY_ETCP); + printf("Debug config: level=%d, categories=0x%x\n", + g_debug_config.level, g_debug_config.categories); + return 0; +} diff --git a/tests/test_etcp_two_instances b/tests/test_etcp_two_instances index 3f243a52..a47efb15 100755 Binary files a/tests/test_etcp_two_instances and b/tests/test_etcp_two_instances differ diff --git a/tests/test_packet_dump b/tests/test_packet_dump new file mode 100755 index 00000000..2b73c2d6 Binary files /dev/null and b/tests/test_packet_dump differ diff --git a/tests/test_packet_dump.c b/tests/test_packet_dump.c new file mode 100644 index 00000000..01aed248 --- /dev/null +++ b/tests/test_packet_dump.c @@ -0,0 +1,22 @@ +#include +#include +#include "../lib/debug_config.h" + +// Simple test packet dump +void test_packet_dump() { + uint8_t test_data[] = {0x02, 0x11, 0x22, 0x33, 0x44, 0x55}; + + // Simulate single-line packet dump + char hex_buf[32]; + snprintf(hex_buf, sizeof(hex_buf), "%02x%02x%02x%02x%02x%02x", + test_data[0], test_data[1], test_data[2], test_data[3], test_data[4], test_data[5]); + + printf("[ETCP] TEST_SEND: len=%zu hex=%s\n", sizeof(test_data), hex_buf); + printf("[ETCP] TEST_COMPACT: link=NULL type=0x%02x ts=0 len=%zu\n", test_data[0], sizeof(test_data)); +} + +int main() { + printf("=== Testing single-line packet dump ===\n"); + test_packet_dump(); + return 0; +}