Files
utun2/tools/etcpmon/IMPLEMENTATION.md
T
evgeny 38c9f229bc stcp: ed25519 pubkey exchange in handshake, fix instance destroy segfault, fix chat_tcp_test
- u_async.c: process immediate_queue BEFORE epoll_wait in uasync_poll
- stcp_link.c: defensive NULL etcp_conn/etcp_link in stcp_link_close
- utun_instance.c: move stcp_server_list_destroy_all to Phase J (before ETCP),
  add deferred drain in both Phase J and L
- stcp.h/c: STCP_HS_ENC_CLIENT 6→38, STCP_HS_ENC_SERVER 7→39,
  exchange ed25519_pubkey in both handshake directions
- etcp_connections.c: copy peer_ed25519 from STCP to ETCP_CONN in
  etcp_link_enter_ready_tcp; guard link->etcp in tcp_link_close_cb
- chat_sync.c: fix lk->conn NULL dereference for TCP links
- test_stcp.c: update stcp_server_create/stcp_client_connect callsites
- tools/chat_tcp_test: change transport udp→tcp
2026-08-07 00:48:27 +03:00

4.0 KiB

ETCP Monitor - Implementation Summary

Overview

ETCP Monitor is a Windows GUI utility for real-time monitoring of ETCP connections in uTun.

Architecture

Protocol (Binary)

  • Format: [2 bytes: size] [1 byte: type] [payload...]
  • Period: 100ms updates
  • Transport: TCP socket

Message Types

Client → Server:

  • 0x01 - Request connection list
  • 0x02 - Select connection by peer_node_id
  • 0x03 - Request metrics
  • 0x04 - Disconnect

Server → Client:

  • 0x81 - Connection list response
  • 0x82 - Metrics response (ETCP + TUN + Links)
  • 0xFF - Error response

Files Created

Protocol (Shared)

  • tools/etcpmon/etcpmon_protocol.h - Protocol definitions and structures

Server (uTun)

  • src/control_server.h - Control server interface
  • src/control_server.c - TCP server implementation
  • Modified src/utun_instance.c - Integration with uTun lifecycle
  • Modified src/Makefile.am - Added control_server.c to build
  • Modified lib/debug_config.h - Added DEBUG_CATEGORY_CONTROL

Client (Windows GUI)

  • tools/etcpmon/etcpmon_client.h/c - Network layer (Winsock)
  • tools/etcpmon/etcpmon_gui.h/c - WinAPI GUI implementation
  • tools/etcpmon/etcpmon_main.c - Entry point
  • tools/etcpmon/Makefile.mingw - MinGW build
  • tools/etcpmon/build.bat - Windows build script
  • tools/etcpmon/build.sh - Unix build script
  • tools/etcpmon/clean.bat - Clean script
  • tools/etcpmon/README.md - Documentation

Test

  • tests/test_control_server.c - Integration test for control server protocol

Library Fix

  • lib/u_async.c - Modified to use select() on Windows instead of WSAPoll

Build Instructions

cd tools\etcpmon
build.bat

Unix/MinGW

cd tools/etcpmon
bash build.sh

Usage

1. Configure uTun

Add to utun.conf:

control_ip=127.0.0.1
control_port=9090

2. Start uTun

./utun

3. Start ETCP Monitor

etcpmon.exe

4. Connect

  • Enter server IP:port (default: 127.0.0.1:9090)
  • Click "Connect"
  • Select connection from list
  • View real-time metrics

Metrics Displayed

ETCP Connection

  • RTT (last, avg 10, avg 100)
  • Jitter
  • Bytes sent total
  • Retransmission count
  • ACK count
  • Unacked bytes
  • Links count

TUN Interface

  • Bytes read/written
  • Packets read/written
  • Read/write errors
  • Status (UP/DOWN)
  • Encrypt/decrypt errors
  • Send/receive errors
  • Total encrypted/decrypted bytes
  • Bandwidth (Kbps)
  • NAT changes count

Technical Details

Windows select() Implementation

On Windows, the uasync library now uses select() instead of WSAPoll() for socket monitoring. This resolves issues with accepted TCP sockets that caused WSAPoll to fail with error 10022 (WSAEINVAL).

Changes in lib/u_async.c:

  • Added Windows-specific code path using select()
  • Maintains compatibility with Linux (uses poll())
  • Properly handles both regular FDs and socket_t types

Binary Protocol

The protocol uses fixed-size binary structures for efficient communication:

  • Header: 3 bytes (2 size + 1 type)
  • Connection list item: 40 bytes (8 peer_id + 32 name)
  • ETCP metrics: 40 bytes
  • Link metrics: 32 bytes per link
  • TUN metrics: 24 bytes

Testing

Unit Test

cd tests
./test_control_server

Expected output:

================================
Control Server Protocol Test
================================

[TEST] Setting up mock data...
[TEST] Mock data ready (1 conn, 2 links)
[TEST] Starting control server on port 19090...
[TEST] Control server started ✓
[TEST] Starting client thread...
[TEST] Running event loop...

[CLIENT] Connecting to 127.0.0.1:19090...
[CLIENT] Connected
[CLIENT] Sending CMD_LIST_CONN...
[CLIENT] Waiting for response...
[CLIENT] Received connection list: 1 connection(s)
[CLIENT] Connection verified: peer_id=0x1234567890ABCDEF ✓
[CLIENT] All tests passed ✓

[TEST] Cleaning up...

================================
[PASS] All tests passed!
================================

License

See main project LICENSE file.