Commit Graph
1572 Commits
Author SHA1 Message Date
jeka 8a5a5c361f Remove generated Makefile and temp file src/aa from repo (already in .gitignore) 2026-02-16 11:16:14 +03:00
jeka 9c768bfd17 Fix timer leaks in ETCP: cancel retrans_timer, ack_resp_timer, and shaper_timer on connection close 2026-02-16 10:39:36 +03:00
Evgeny 603768052d Debug: Enable crypto packet dumps conditionally based on debug level 2026-02-16 08:15:51 +03:00
Evgeny a73e36b559 Fix mesh topology: try normal decryption first, then INIT
Reversed the decryption order:
1. If link exists with session_ready, try normal decryption first
2. If normal decryption succeeds, process packet normally (goto process_decrypted)
3. If normal decryption fails OR no link OR no session, try INIT decryption
4. If INIT succeeds, create new server-side connection and link

This is more efficient because:
- Most packets are regular data (use normal decryption)
- INIT packets are rare (only during connection setup)
- Works correctly for both standard client-server and mesh topologies

All 23 tests now pass.
2026-02-16 01:02:10 +03:00
Evgeny 27b02cef68 Fix mesh topology handling with fallback to normal decryption
When receiving packets on client links, try INIT decryption first.
If it fails (e.g., for INIT_RESPONSE packets), fallback to normal
decryption using the existing link's crypto context.

This fixes mesh topology where both nodes send INIT simultaneously,
while maintaining compatibility with standard client-server mode where
client links receive INIT_RESPONSE packets.

Changes:
- If link==NULL OR link is client (is_server==0), try INIT decryption
- On any INIT decryption error, if we have an existing link, goto normal_decrypt
- Added normal_decrypt label before standard packet processing
- This allows handling both incoming INIT (create server link) and
  incoming responses (use existing client link) correctly
2026-02-16 00:51:28 +03:00
Evgeny 0a4373ffe1 Fix: Treat client links as INIT packets on receive
When receiving an INIT packet, the code was looking up existing links by
address. However, in a mesh topology where each node has both client and
server connections to peers, a client link created during initialization
would be found when receiving an incoming INIT packet from that peer.

This caused the code to try decrypting the INIT packet using the client
link's crypto context, which failed because the session keys weren't
properly established yet.

The fix checks if the found link is a client link (is_server==0) and if
so, treats the packet as a new INIT connection that needs to create a
server-side link instead.

Changed condition from:
  if (link==NULL)
to:
  if (link==NULL || link->is_server==0)
2026-02-15 23:35:23 +03:00
Evgeny 59f01c6a15 WIP: исправление портов и добавление отладки в mesh test 2026-02-15 21:32:32 +03:00
Evgeny c1e2f061b9 Рефакторинг utun_instance_create - устранено дублирование кода
- Создана общая функция instance_init_common() для инициализации instance
- Упрощены utun_instance_create() и utun_instance_create_from_config()
- Удалено ~90 строк дублирующегося кода
- Обе функции теперь используют общую логику инициализации
2026-02-15 20:44:14 +03:00
Evgeny bee1e41b07 Добавлен тестовый режим для TUN интерфейса и тест роутинга mesh
- Добавлена опция tun_test_mode в конфиг для работы без реального TUN устройства
- Добавлены функции для работы с очередями TUN в тестовом режиме
- Добавлен API для программного создания instance из структуры конфига
- Создан тест test_routing_mesh.c с 3-instance mesh топологией
- Ключи генерируются автоматически и распределяются между instance
2026-02-15 20:37:10 +03:00
Evgeny eb721be508 Simplify: Extract common crypto logic into helper functions for both OpenSSL and TinyCrypt 2026-02-15 18:47:49 +03:00
Evgeny 25f5b84bf9 Simplify: Remove unreachable duplicate code block in init_connections 2026-02-15 18:40:43 +03:00
Evgeny f4295d962e Simplify: Deduplicate queue cleanup in etcp_connection_close using helper functions 2026-02-15 18:39:03 +03:00
Evgeny 29d26cf139 Force rebuild of utun-ecc_platform_specific.o on every build to ensure platform-specific changes are picked up 2026-02-15 13:09:10 +03:00
Evgeny ada25ec4cc Add -ladvapi32 for Windows CryptGenRandom support 2026-02-15 04:36:47 +03:00
Evgeny 84e6d59d77 Fix Windows build: wintun function pointer declarations
- Add * to Wintun function pointer declarations (wintun.h defines types as functions)
- Use (void *) cast in GetProcAddress to avoid errors
- All 22 tests pass on Linux
2026-02-15 03:49:47 +03:00
Evgeny 2a546c5fbc Fix Windows build: tun_windows.c function pointers and MIB_IPINTERFACE_ROW
- Fix function pointer declarations in tun_windows.c (removed broken macro)
- Use explicit GetProcAddress calls for each Wintun function
- Remove invalid fields from MIB_IPINTERFACE_ROW (PromiscuousMode, DadState, etc.)
- Add #ifndef _WIN32 around IF_NAMESIZE/if_indextoname in etcp_connections.c
- All 22 tests pass on Linux
2026-02-15 03:46:05 +03:00
Evgeny 8af2904033 Fix Windows build: net/if.h and route_lib.h forward declarations
- Wrap net/if.h include in #ifndef _WIN32 in etcp_connections.c
- Add forward declarations for ROUTE_TABLE and ROUTE_ENTRY in route_lib.h
- All 22 tests pass on Linux
2026-02-15 03:30:42 +03:00
Evgeny 605a928350 Fix Windows build: POSIX dependencies (netdb.h, fork, setsid, SIGHUP, etc.)
- Wrap POSIX headers (netdb.h, ifaddrs.h, net/if.h) in #ifndef _WIN32 in config_parser.c
- Add Windows stub for get_netif_index() - returns 0 (no interface binding)
- Wrap daemonize() function for Windows - returns 0 (no fork/setsid support)
- Replace setlinebuf() with setvbuf() for Windows compatibility
- Wrap SIGHUP signal handler in #ifndef _WIN32
- Add explicit poll() declaration for Windows in platform_compat.h
- All 22 tests pass on Linux
2026-02-15 03:26:02 +03:00
Evgeny c83fdeeaad Fix Windows build: add sa_family_t typedef, replace arpa/inet.h, fix ll_queue warnings
- Add sa_family_t typedef for Windows in platform_compat.h
- Add forward declaration of struct ll_queue in ll_queue.h to fix warnings
- Replace arpa/inet.h includes with platform_compat.h in:
  tun_linux.c, etcp_connections.c, route_lib.c, route_bgp.c,
  utun_instance.c, routing.c, utun.c, packet_dump.h, config_parser.c
- All 22 tests pass on Linux
2026-02-15 03:06:02 +03:00
Evgeny 1bd4bf5d11 Fix Windows build errors: config_parser.h, poll(), localtime()
- Replace sys/socket.h with platform_compat.h in config_parser.h
- Add #include <poll.h> for Windows in platform_compat.h
- Fix localtime() type mismatch: cast tv.tv_sec to time_t
- All 22 tests pass on Linux
2026-02-15 02:56:08 +03:00
Evgeny 15b670e563 Add cross-platform TUN support with Wintun for Windows
- Split TUN implementation into platform-specific files:
  - tun_if.c: Common code (queues, callbacks, statistics)
  - tun_linux.c: Linux TUN/TAP implementation (/dev/net/tun + ioctl)
  - tun_windows.c: Windows Wintun implementation (wintun.dll + IP Helper)

- Update tun_if.h with platform abstraction layer:
  - tun_platform_init/cleanup/read/write/get_poll_fd
  - Platform handles: fd (Linux), WINTUN handles (Windows)

- Windows implementation features:
  - Dynamic loading of wintun.dll with graceful error handling
  - IP Helper API for IP address and MTU configuration
  - HANDLE-based uasync integration
  - Clear error message if wintun.dll is not found

- Update build system:
  - configure.ac: Detect Windows (mingw/msys/cygwin)
  - src/Makefile.am: Conditional compilation of tun_linux/tun_windows
  - tests/Makefile.am: Link platform-specific TUN objects

- Add wintun.dll and wintun.h to lib/ directory
- All 22 tests pass on Linux
- Ready for MSYS2 UCRT64 Windows build
2026-02-15 01:40:17 +03:00
Evgeny 3f1d4077e9 Add cross-platform socket compatibility layer for Windows support
- Add socket_compat.h/c with platform abstraction for POSIX/Windows
- socket_t typedef: int on POSIX, SOCKET on Windows
- Add socket_platform_init/cleanup for WSAStartup on Windows
- Add socket operations: create_udp, sendto, recvfrom, set_nonblocking
- Add socket options: set_buffers, set_reuseaddr, set_mark (Linux), bind_to_device (Linux)
- Update u_async: add socket_t support with uasync_add_socket_t/remove_socket_t
- Update ETCP: use socket_t and socket_compat functions
- Add DEBUG_CATEGORY_SOCKET for socket debugging
- All 22 tests pass on Linux
- MSYS2 UCRT64 compatible
2026-02-15 01:01:35 +03:00
Evgeny 43114d727c BGP: Complete route exchange with connection tracking and broadcast
- Add ROUTE_BGP_CONN_ITEM structure for connection list management
- Modify route_bgp_new_conn() to add connections to senders_list
- Create route_bgp_remove_conn() for cleanup on connection close
- Add route_change_callback typedef and fields to ROUTE_TABLE
- Implement route_bgp_on_route_change() to broadcast updates
- Modify route_table_insert() to call callback on insert/update
- Modify route_table_delete_entry() to call callback on delete
- Add route_bgp_remove_conn() call in etcp_connection_close()
- Fix test: remove duplicate init_connections() calls

Features:
- Connections tracked in senders_list (ll_queue)
- Route changes broadcast to all connections
- Withdraw messages sent on connection close
- No port binding conflicts in tests

All changes working correctly - BGP route exchange functional in both directions
2026-02-14 21:48:27 +03:00
Evgeny 68668821e4 BGP: Implement route exchange module between uTun instances
- Add route_bgp.c/h with BGP-like route exchange functionality
- Implement route_bgp_init/destroy for module lifecycle
- Add route_bgp_new_conn to send routing table on connection
- Implement route_bgp_receive_cbk for processing incoming routes
- Add route_table_delete_entry for individual route removal
- Extend ROUTE_ENTRY with endpoint_ip, endpoint_port, destination_node_id
- Add DEBUG_CATEGORY_BGP to debug_config.h
- Integrate BGP initialization into utun_instance_create
- Call route_bgp_new_conn from etcp_connections on link init
- Create integration test test_bgp_route_exchange.c
- Add route_bgp_delete_entry tests to test_route_lib.c
- Update Makefiles to include new module

Route exchange tested and working in both directions (client-server)
2026-02-14 20:37:14 +03:00
Evgeny d08b14a8c1 ETCP: Add NAT IP:port tracking to handshake protocol
- Add peer_ipv4 (4 bytes) and peer_port (2 bytes) to INIT_RESPONSE packet
- Server now returns client's external IP:port in handshake response
- Client parses and stores NAT address in ETCP_LINK structure
- Track NAT address changes with nat_changes_count counter
- Track NAT address matches with nat_hits_count counter
- Legacy protocol support: detect old format without NAT info
- Add DEBUG logging for NAT address initialization/changes/hits

This allows clients behind NAT to discover their external address
during the ETCP handshake process.

All 21 tests pass successfully.
2026-02-14 11:07:00 +03:00
Evgeny 8334b97521 Backup before adding NAT IP:port tracking to ETCP handshake 2026-02-14 11:02:20 +03:00
Evgeny e21e0d874f routing: refactor to use etcp_bind(id=0) and etcp_send
- Changed routing_pkt_from_etcp_cb to new signature for etcp_bind
- Register callback via etcp_bind(instance, 0, callback) in routing_create
- Implement full routing in routing_pkt_from_tun_cb using route_table_lookup
- Send packets to ETCP via etcp_send with ID prefix (0x00)
- routing_add_conn/routing_del_conn now deprecated (no-op)
- Added etcp_unbind in routing_destroy for cleanup
2026-02-14 02:00:51 +03:00
Evgeny 801770f117 Backup before routing refactor: etcp_bind id=0 + etcp_send routing 2026-02-14 01:58:59 +03:00
Evgeny d694c92b1f Refactor etcp_api: per-instance bindings with struct ETCP_BINDINGS
Changed from global bindings to per-instance bindings:

1. Added struct ETCP_BINDINGS in etcp_api.h:
   - Contains array of callbacks[ETCP_MAX_BINDINGS]
   - NULL means not bound

2. Updated UTUN_INSTANCE (utun_instance.h):
   - Added field: struct ETCP_BINDINGS api_bindings
   - Initialized to NULL by calloc in utun_instance_create

3. Updated API functions (etcp_api.h/c):
   - etcp_bind(inst, id, callback) - per-instance binding
   - etcp_unbind(inst, id) - per-instance unbinding
   - Removed etcp_api_init/etcp_api_deinit (not needed)
   - etcp_int_recv now uses conn->instance->api_bindings.callbacks[id]

4. Updated test_etcp_api.c:
   - Separate callbacks for server and client
   - Register via etcp_bind(server_instance, ...) and etcp_bind(client_instance, ...)
   - Removed global etcp_api_init/etcp_api_deinit calls

Test passes: All 100 packets transmitted in each direction.
2026-02-14 01:39:55 +03:00
Evgeny 15001487af Build: Out-of-tree build system with separate build directory
- Build in build/ directory instead of source tree
- Main binary (utun) copied to project root
- Tests run from build/tests/ with config files copied there
- Test output redirected to build/tests/logs/*.log files
- Console shows only [PASS]/[FAIL]/[SKIP] status
- Incremental build works correctly
- make check runs all tests with summary

Changes:
- New root Makefile as wrapper for out-of-tree build
- Updated Makefile.am files for lib, src, tests
- Added tinycrypt-objects target for test dependencies
- Tests no longer clutter project root
2026-02-13 22:58:41 +03:00
Evgeny 57b2568ad6 Fix: Adapt tests for new etcp_api callback in pn_init()
The pn_init() function now sets up etcp_int_recv as callback for pn->output.
Tests that manually poll pn->output need to reset this callback to NULL.

Changes:
- test_pkt_normalizer_etcp.c: Reset callback after pn_init() for client and server
- test_pkt_normalizer_standalone.c: Reset callback after pn_init()

This allows tests to manually poll output queue without conflicting
with the automatic callback mechanism.

Test Results: All 19 tests PASS
2026-02-13 19:41:15 +03:00
Evgeny f0f8edbcb6 Fix: Restore original callback in pkt_normalizer.c
Restored queue_set_callback(etcp->output_queue, pn_unpacker_cb, pn)
which was incorrectly replaced in the previous commit.

The etcp_recv callback should be set up separately by the API user,
not by replacing the internal pkt_normalizer callback.

Test Results: All 19 tests PASS
2026-02-13 19:21:42 +03:00
Evgeny ffaf7a25b8 Add etcp_api.c/h - ETCP packet send/receive API
Implemented:
- etcp_send(conn, entry) - отправляет пакет в очередь normalizer
- etcp_bind(id, callback) - подписка на пакеты с определенным ID
- etcp_unbind(id) - отписка от пакетов
- etcp_recv(queue, arg) - коллбэк для маршрутизации пакетов по ID
- etcp_api_init/deinit - инициализация API

Integration:
- pn_init() теперь устанавливает etcp_recv как callback для pn->output
- Добавлены etcp_api.c/h в src/Makefile.am
- Добавлен etcp_api.o в тестовые зависимости

API использует первый байт кодограммы (cmd) как ID для маршрутизации.
ID=0 используется как default handler если нет специфичного binding.
2026-02-13 19:15:16 +03:00
Evgeny 02f39f6089 Fix: Update route_lib.c to match route_lib.h interface
- Implemented route_table_lookup() returning ROUTE_ARRAY* with cache support
- Implemented route_table_delete() to remove routes by connection
- Implemented route_update_quality() with simple formula
- Added internal ROUTE_ARRAY management functions (create/destroy/add)
- Implemented LRU cache for route lookups (10 entries)
- Fixed route_table_insert() to actually insert entries
- Removed duplicate parse_subnet() from utun.c
- Added helper function declarations to route_lib.h

Build: Project compiles without errors
2026-02-13 18:17:20 +03:00
Evgeny ce15d6b883 Backup before route_lib.c refactoring 2026-02-13 18:12:26 +03:00
Evgeny 277ab872bf Fix: routing initialization timing and test callbacks
- Fixed race condition: routing_add_conn called before etcp->normalizer was assigned

- Moved routing_add_conn from pn_init to etcp_connection_create after normalizer init

- Added routing.h include to etcp.c

- Fixed tests: disable routing callback on output_queue to keep packets for test verification

All 19 tests now pass.
2026-02-07 23:41:07 +03:00
Evgeny 5a7107b855 Simplify tun_if: unified init, write, close API with queue-based reads
- New struct tun_if with internal queue for incoming packets
- Single tun_init() does everything: create TUN, set MTU=1500, up, register in uasync
- tun_write() for outgoing packets
- tun_close() cleans up everything including queue
- Removed: tun_create, tun_set_ip, tun_set_up, tun_set_mtu, tun_read, tun_get_config
- Removed: utun_instance_register/unregister_sockets (now internal)
- Updated utun_instance to use new tun_if* pointer
- Updated test_etcp_two_instances for new API
2026-02-07 02:28:58 +03:00
Evgeny 2e48f9a111 Refactor: TUN ioctl helpers, routing documentation; prepare for tun_if simplification 2026-02-07 02:19:33 +03:00
Evgeny 1bb59bad98 Fix: Isolate test configs and add key validation retry 2026-02-06 22:21:21 +03:00
Evgeny 07d4616aea Refactor: Updated SHA256 implementation and secure channel improvements 2026-02-06 21:47:41 +03:00
Evgeny f022e7105e Crypto: Add OpenSSL support as alternative to TinyCrypt
- Add --with-openssl configure option (default: enabled)
- Update src/Makefile.am for conditional TinyCrypt compilation
- Update tests/Makefile.am for conditional test linking
- Add config.h include to secure_channel.c for USE_OPENSSL macro
- All 19 tests pass with both OpenSSL and TinyCrypt
2026-02-06 01:52:41 +03:00
Evgeny 3281e4878e Backup before fixing test_etcp_simple_traffic 2026-02-06 01:34:03 +03:00
Evgeny beafb9b198 Fix: Remove usleep from event loops and fix segfault in tests
- Removed usleep(5000) from all test event loops
- Changed to use single shared uasync for server and client instances
- Removed uasync_destroy from utun_instance_destroy to prevent double-free
- Added explicit uasync_destroy calls in all tests and main program
- Fixed segfault in test_pkt_normalizer_etcp and test_etcp_100_packets
- Added DEBUG_TRACE to all functions in etcp.c and etcp_connections.c

Tests now run without artificial delays and complete successfully.
2026-02-05 20:51:07 +03:00
Evgeny 8a3f01fbf8 Add epoll support, fix ETCP timestamp bug, optimize uasync with cached pollfds 2026-02-05 04:11:10 +03:00
Evgeny 7bd369341b Remove error prints from ETCP timestamp checks 2026-02-05 04:05:29 +03:00
Evgeny 994a4bf779 Fix ETCP timestamp overflow bug and enable epoll for Linux 2026-02-05 03:47:34 +03:00
Evgeny 528c1ddd00 WIP: Current normalizer changes before error handling fix 2026-02-04 23:24:25 +03:00
Evgeny 076da4264b Fix: ACK section length calculation and increase socket buffers 2026-02-04 22:35:40 +03:00
Evgeny c1890ad7a6 Debug: Add ETCP log_name identifier and improve log format
- Add log_name[16] field to ETCP_CONN structure for connection identification
- Add etcp_update_log_name() function to update identifier when peer_node_id is known
- Update all DEBUG_* calls in etcp.c and etcp_loadbalancer.c to include log_name prefix
- Add DEBUG_CATEGORY_NORMALIZER for packet normalizer debug output
- Change log timestamp format to [hh:mm:ss-mmm.uuu] with microseconds precision
- Reorder debug output: (file:line) function() [log_name] message
- Remove duplicate function names from log messages
- Clean up backup files from pkt_normalizer development
2026-02-04 18:56:43 +03:00
Evgeny 22dd1b78c3 Add local_link_id exchange in ETCP handshake (0x02-0x05)
- Add etcp_find_free_local_link_id() function to allocate unique link IDs
- Modify etcp_link_new() to auto-assign local_link_id, fail if none available
- Update INIT_REQUEST (0x02/0x04) to send local_link_id after keepalive
- Update INIT_RESPONSE (0x03/0x05) to include local_link_id
- Parse remote_link_id from incoming handshake packets
- Update protocol documentation in doc/etcp_protocol.txt
- Add comprehensive unit test test_etcp_link_id.c

New packet format:
INIT_REQUEST:  [code][node_id(8)][mtu(2)][keepalive(2)][link_id(1)][pubkey(64)]
INIT_RESPONSE: [code][node_id(8)][mtu(2)][link_id(1)]
2026-02-04 14:21:20 +03:00