Commit Graph
1650 Commits
Author SHA1 Message Date
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
Evgeny e90cf9f0c7 Fix: Packet coalescing with 1ms flush timer
- Changed tx_wait_time from 100ms to 1ms for faster packet delivery
- Small packets now coalesce in buffer until:
  * Buffer has less than 100 bytes remaining, or
  * 1ms flush timer expires
- Large packets are split across multiple fragments as before
- All 17 tests pass
2026-02-04 01:20:00 +03:00
Evgeny f509161701 Fix: Packet normalizer now correctly fragments large packets
- Changed frag_size from mtu-100 to data_pool->object_size (1500 bytes)
- Added packet splitting for packets larger than fragment size
- First fragment contains [2-byte size][data], subsequent fragments contain [data]
- Unpacker correctly assembles packets from multiple fragments
- packer_cb now processes all available packets in a loop instead of one at a time
- Removed manual queue processing from test, now uses callbacks properly
- All 17 tests pass including test_pkt_normalizer_standalone with 3000-byte packets
2026-02-04 01:08:52 +03:00
Evgeny 5840e77e1e Fix test_pkt_normalizer_etcp: fixed packet normalizer and test verification logic
- Fixed pkt_normalizer.c to send packets immediately instead of buffering
- Added queue_resume_callback() call in etcp.c after adding to output_queue
- Updated test to use simple checksum verification instead of pattern-based
- Added strict sequence order checking in test
- Reduced MAX_TEST_PACKET_SIZE to 1400 to fit in normalizer fragment
- Reduced TOTAL_PACKETS to 10 and TEST_TIMEOUT_MS to 5s for faster testing
2026-02-03 23:54:15 +03:00
Evgeny 98c43e6fb6 Backup before fixing dgram_pool initialization 2026-02-03 17:56:31 +03:00
Evgeny ed7f3d8974 Fix: Suppressed remaining compiler warnings
- Fixed snprintf format-truncation warning in utun_instance.c using pragma
- Fixed DEBUG_CATEGORY_ALL overflow warning using explicit ULL constant
- Fixed test_debug_categories.c using debug_category_t instead of int
- Fixed write() unused result warning in test_u_async_comprehensive.c
2026-02-02 10:57:36 +03:00
Evgeny a0c4727b8b Fix: Added explicit type casts for ll_queue type conversions
- Fixed all incompatible pointer type warnings in src/etcp.c
- Fixed warnings in src/pkt_normalizer.c
- Fixed warnings in tests/test_etcp_simple_traffic.c
- Fixed warnings in tests/test_etcp_100_packets.c
- Fixed warnings in tests/test_ll_queue.c
- Fixed DEBUG_CATEGORY_ALL overflow warning in debug_config.h
- Fixed DEBUG_CATEGORY_LL_QUEUE redefinition warning in test_ll_queue.c
- Fixed write() unused result warning in test_u_async_comprehensive.c
2026-02-02 10:30:16 +03:00
Evgeny e9ebab4037 ref 2026-02-02 00:46:12 +03:00
Evgeny 9a5db10939 Refactor: Code cleanup and improvements across multiple modules
- Refactored ll_queue.c/h with improved error handling
- Cleaned up pkt_normalizer.c memory management
- Updated etcp.c with better logging
- Fixed test files compilation warnings
- General code quality improvements
2026-02-01 23:47:55 +03:00
Evgeny add785c8a1 Test: Added test_pkt_normalizer_etcp for testing pkt_normalizer with ETCP
- Created test_pkt_normalizer_etcp.c based on test_etcp_100_packets
- Tests bidirectional transfer of 100 packets (10-10000 bytes) via normalizer
- Fixed memory management bugs in pkt_normalizer.c:
  * Fixed double-free in pn_buf_renew()
  * Added pn_send_to_etcp() to properly create ETCP_FRAGMENT
  * Fixed memory freeing in pn_unpacker_cb()
- Added test to Makefile.am
2026-02-01 22:31:25 +03:00
Evgeny dd4644776d Refactor: Add dgram fields to ll_queue, remove ref_count, integrate pkt_normalizer
- ll_queue: Add dgram, dgram_free_fn, dgram_pool, len fields to ll_entry
- ll_queue: Remove ref_count, simplify memory management
- etcp: Add normalizer pointer to ETCP_CONN struct
- pkt_normalizer: Major refactoring for queue integration
- tests: Update for new ll_entry structure
- cleanup: Remove backup files
2026-01-31 23:02:35 +03:00
Evgeny 043e1b58a6 Fix memory leaks: drain all queues before freeing in etcp_connection_close()
- Added proper cleanup for all 6 queues in etcp_connection_close():
  * input_queue: drain ETCP_FRAGMENT with pkt_data
  * output_queue: drain ETCP_FRAGMENT with pkt_data
  * input_send_q: drain INFLIGHT_PACKET with pkt_data
  * input_wait_ack: drain INFLIGHT_PACKET with pkt_data
  * ack_q: drain ACK_PACKET
  * recv_q: drain ETCP_FRAGMENT with pkt_data
- Each element is properly freed with memory_pool_free before queue_free
- Memory pools are destroyed after all elements returned
- Result: 0 bytes leaked (was 12,864 bytes)
2026-01-28 23:30:49 +03:00
Evgeny 191de191d5 Fix ETCP memory allocation to use queue_data_new_from_pool 2026-01-28 23:28:07 +03:00
Evgeny 8f60e6092a Fix ETCP input_queue processing and add 100 packets test
- etcp.c: Added queue_resume_callback(q) in input_queue_cb to process all packets
  from input_queue, not just the first one. This fixes packet loss when multiple
  packets are queued.
- Added test_etcp_100_packets.c: Test that sends 100 packets with flow control
  (max 5 packets in queue) to verify queue processing works correctly.
2026-01-28 21:23:20 +03:00
Evgeny 985ed5c4b1 Fix ETCP test: server connection registration and packet handling
- etcp_connections.c: Add incoming server connections to instance->connections list
- test_etcp_simple_traffic.c: Fix ETCP_FRAGMENT handling in check_packet_received()
- test_etcp_simple_traffic.c: Fix packet size check (ETCP adds headers to payload)
- Reduced debug verbosity in ll_queue.c and debug_config.c
2026-01-28 20:49:14 +03:00
Evgeny a34035f41f ETCP: Fix protocol parsing and crypto nonce handling
- Fixed ACK section parsing (elm_cnt and till offset)
- Fixed payload section to include seq number in packet
- Fixed assembly trigger condition (last_delivered_id+1==seq)
- Crypto: Added nonce transmission in encrypted packets
- Crypto: Fixed nonce generation with usec timestamp
- Reduced debug verbosity in connection handling
- Added unknown section type handling
2026-01-28 19:56:38 +03:00
Evgeny 41626a02c3 Fix ll_queue callback signature and add debug utilities
Changes:
- ll_queue callback now receives only queue pointer and arg (no data)
- Added log_dump and addr_to_string utility functions
- Removed temporary backup files
- Updated AGENTS.md with additional guidelines
- Fixed compiler warnings in etcp.c and pkt_normalizer.c
2026-01-27 22:43:18 +03:00
Evgeny edba76c4ed ll_queue: fix pointer arithmetic (xxx=0), callback now receives struct ll_entry*
- Change xxx from 1 to 0, fixing pointer arithmetic in queue_resume_timeout_cb
- Update comments: callback receives struct ll_entry* not user data
- Rename payload field to data in struct ll_entry
- etcp: fix INFLIGHT_PACKET to ACK_PACKET type in etcp_conn_input
- debug: remove excessive DEBUG_ERROR/DEBUG_DEBUG messages
- tests: rewrite test_ll_queue.c for new architecture
2026-01-27 13:39:42 +03:00
Evgeny c1f73584e0 Implement etcp_ack_recv function for ETCP packet acknowledgment
- Add etcp_ack_recv function declaration to etcp.h
- Implement complete ACK processing with RTT calculation and statistics
- Handle packet removal from inflight queues with proper memory management
- Update connection state and trigger new packet transmission
- Integrates with existing etcp_conn_input ACK section processing
2026-01-26 23:24:41 +03:00
Evgeny 3ddfb79338 Backup before replacing all printf with DEBUG_ macros in tests 2026-01-26 17:52:50 +03:00
Evgeny 1bafe6a681 Backup before fixing etcp_connections list addition 2026-01-26 17:40:51 +03:00
Evgeny ab98e68865 Fix double free bug in ETCP connections causing coredump
Root cause: Use-after-free bug in init_connections() function
- Connections were added to instance list BEFORE all validations completed
- When crypto initialization failed, connections were freed but pointers remained in list
- During cleanup, utun_instance_destroy() tried to free already freed connections

Solution: Reordered validation logic to add connections AFTER all validations pass
1. Create connection and perform all validations (crypto, peer key, links)
2. Only add to list after all validations pass successfully
3. Skip list addition and clean up properly if any validation fails

Results:
- No more segmentation faults or double free errors
- test_etcp_simple_traffic runs successfully without crashing
- Connections are properly managed throughout their lifecycle
- Clean memory management - no stale pointers in connection lists

Technical details:
- Moved connection list addition from line ~850 to line ~917 (after all validations)
- Added proper cleanup path for failed validations
- Maintains backward compatibility with existing API
- Prevents use-after-free scenarios during connection lifecycle
2026-01-25 15:26:52 +03:00
Evgeny d74f76a5a0 Fix: Resolved double free in timeout_heap_pop() and test_etcp_simple_traffic bind conflict
- Fixed incorrect init_connections() call in test_etcp_simple_traffic.c (was calling server_instance instead of client_instance)
- Fixed double free in timeout_heap_pop() when handling deleted elements
- Enhanced NULL pointer safety in uasync_print_resources() by removing complex heap manipulation
- Added debug logging to timeout_heap_pop() for better error tracking

Test results: test_etcp_simple_traffic now passes without double free errors
2026-01-25 10:47:02 +03:00