Commit Graph
1387 Commits
Author SHA1 Message Date
Evgeny 501b636d19 topo_group: disable in chatgui; fix NAT detection for matching addresses
- Add utun_instance_set_topo_group_enabled() flag (default=1, chatgui sets 0)
- Move nat_detection create/destroy from topo_group.c to utun_instance.c (unconditional)
- NAT detection: simplify third_node search (any connected node)
- NAT detection: allow NULL topo_groups in receive_cbk and handlers
- NAT detection: handle NAT_INFO without topo_group (update socket directly)
- route_ping: make TOPO_GROUP optional in send_req_addr/handle_req
- topo_node: when NAT verified and address matches interface, set entry type to NAT instead of INTERFACE + socket_id|1
- topo_node: detect nat_type change in changed computation for rebuild
- test_nat_detection: fix socket_id check (|1 for NAT entries)
- chatgui: call utun_instance_set_topo_group_enabled(0)
2026-07-19 08:33:33 +03:00
Evgeny 208a0c8ae8 fix(chat_sync): peer online status not updated in invite flow
- cs_on_conn_up: call cs_on_peer_status_changed(peer,1) before return
  in invite path (was skipped, leaving inviter at online=0 in DB)
- cs_handle_welcome: remove unconditional set_online(0) — online
  status is managed by conn_up/conn_down, not by WELCOME
- cs_handle_welcome: add cs_post_channel_online() after WELCOME
  to send PEERS_ONLINE to GUI when channel is fully loaded
- cs_post_channel_online: add DEBUG log for PEERS_ONLINE event
- db_sync peer_check: suppress spam log when no peers connected
2026-07-19 07:54:06 +03:00
Evgeny ca6a27aadb BGP: 2 addr per socket (remove TOPO_ADDR_REAL), config IP priority, private IPs in SQLite, NAT addr != INTERFACE 2026-07-19 07:00:04 +03:00
Evgeny 4df7195a58 debug: add ADDR_SYNC logs for BGP node_put DELETE and member_sync recv ac 2026-07-19 00:09:46 +03:00
Evgeny 586d46f1e6 fix: WELCOME wire format — flags+update_sig parsing, peer_needed gap, invalid join_sig skip 2026-07-18 23:53:04 +03:00
Evgeny 39a376e255 fix: BGP и чат больше не затирают адреса друг друга в node_addresses
- BGP DELETE: +AND socket_id != 0 — не трогает чат-адреса (invite/JOIN/WELCOME)
- Чат invite DELETE: +AND socket_id=0 — не трогает BGP-адреса
2026-07-18 23:23:14 +03:00
Evgeny 20f5e6c953 sqlite: add cache_size/mmap_size/temp_store PRAGMAs to db_sync and topo_group — 159x insert speedup, fix test_chat_sync_stress timeout 2026-07-18 20:16:24 +03:00
Evgeny 29e2527b6b consolidate 3 SQLite connections into 1 shared handle
- utun_node.cpp: open topo_sqlite_db with FULLMUTEX + all PRAGMAs (WAL, FK, NORMAL sync, checkpoint)
- db_sync.c: reuse inst->topo_sqlite_db if available (shared_db flag)
- chat_core.c: reuse inst->topo_sqlite_db if available (shared_db flag)
- db_sync_destroy/chat_core_destroy: skip close on shared handle
- Only topo_groups_destroy closes the shared handle
- Fallback: each module opens own connection if topo_sqlite_db is NULL
2026-07-18 17:33:29 +03:00
Evgeny 93665c50f0 etcp: remove dead set_*_cbk functions, use add/remove only 2026-07-18 16:52:43 +03:00
Evgeny ef1922327a rename ready_cbks → init_cbks (connection init, not fully ready)
ready_cbks/etcp_conn_set_ready_cbk/add_ready/remove_ready → init_cbks/..._init_cbk
ca_ready_cb → ca_init_cb
cm_direct_ready_cb → cm_direct_init_cb
cm_reverse_ready_cb → cm_reverse_init_cb
ntp_node_on_conn_ready → ntp_node_on_conn_init
connect_ready_cb → connect_init_cb

init accurately describes when conn->initialized=1, before keepalives establish UP.
Also fire etcp_fire_conn_status INIT at this point instead of create time.
2026-07-18 16:38:24 +03:00
Evgeny 4188afb3c5 db_sync: lazy-register channel instance on INIT_SYNC when not pre-registered
When INIT_SYNC arrives for an unknown hash, scan all msg_* tables in
sqlite_master, compute SHA256 of channel_id, match the hash, and
auto-register via db_sync_instance_add. Fixes invite flow where
channel was created but db_sync wasn't notified.
2026-07-18 16:08:47 +03:00
Evgeny a510cb0018 auto_connect: add DB dump diagnostics (node_addresses state, node existence, db path+status)
- chat_core.c: dump node_addresses total, per-node counts, nodes lookup before addr query
- topo_group.c: log db path, rc, and db pointer after sqlite3_open
2026-07-18 15:47:05 +03:00
Evgeny 3aadaa3396 fix: sqlite3_open wrong path in topo_group.c (db_path vs db_path/chats.db), keep separate connections for perf
- topo_group.c: open {db_path}/chats.db instead of {db_path} — was opening a directory
- chat_core.c: remove inst->topo_sqlite_db = g_cc.db — keep topo_sqlite_db as separate
  uTun-only connection without FULLMUTEX, avoiding contention with GUI reads
2026-07-18 15:41:02 +03:00
Evgeny aa1ed006d5 etcp: instance-level conn status callbacks (NEW/UP/DOWN/DELETE), fix member online in chatgui
- Add etcp_add_conn_status_cbk/remove/set — unified callback for all connections
- 4 statuses: NEW, UP, DOWN, DELETE — one subscription covers all connections
- Fire points in etcp.c: create→NEW, on_up→UP, on_down→DOWN, close→DELETE
- chat_sync.c: replace cs_on_new_conn+per-conn subs with cs_on_conn_status
- db_sync.c: same migration to instance-level callback
- cs_is_peer_online: use conn->links_up instead of link->link_status (was 0 during INIT handshake)
2026-07-18 14:51:01 +03:00
Evgeny e71cae16c1 db_sync: unified msg table — one DB, one table per channel
- db_sync: rename column author→node_id, open chats.db instead of sync file,
  db_sync_instance_add takes table_name directly
- chat_core: remove CREATE TABLE msg_ch_*, remove on_db_sync_insert (74 lines),
  remove fallback INSERT in submit_message, add minimal on_msg_inserted
  for GUI notification only
- db_manager: update getMessages for new schema (parse JSON from data column),
  fix ORDER BY to use author_signature instead of datahash,
  update syncCursorNext to parse JSON
- si_register simplified: db_count from same table, no more msg vs sync gap
2026-07-18 13:50:35 +03:00
Evgeny 7c2798ec0e debug: add diagnostic logging for ETCP recv/INIT/dispatch paths
- etcp_connections.c: raw_recv log, INIT conn lookup details, insert_link DUP details
- etcp_api.c: dispatch id/cb logs with DROPPED warning when no handler
- chat_sync.c: etcp_send rc log, fix ac_result_cb to always clear ca_state
2026-07-18 13:04:10 +03:00
Evgeny 93871c592b db_sync: timeout for stuck sync_state=1, handle ERROR properly
- Add sync_start_tb to SI_PEER, set in db_sync_initiate_sync
- peer_check_cb: after 15s timeout reset sync_state=0 with WARN
- db_handle_error: accept si, reset sync_state=0, DEBUG_ERROR with details
- DB_SYNC_SYNC_TIMEOUT=15 added to db_sync.h
2026-07-18 10:52:54 +03:00
Evgeny de4099a26b logging: cleanup connection establishment logs
- Fix wrong categories: BGP/DEBUG -> ETCP for connection lifecycle events
- Fix level: WARN -> DEBUG for normal out-of-order pkts before init
- Fix level: INFO -> DEBUG for per-packet TX DATA spam
- Remove duplicate socket init logs (GENERAL duplicates of ETCP)
- Add missing INFO: INIT_RESPONSE received (client), INIT_RESPONSE sent (server)
- Unify all connection establishment messages under ETCP category
- Remove redundant DEBUG address prints in INIT send (info already in INFO msg)
- PING/PONG operations: BGP -> CONNECTION
- DIRECT IP detection: BGP -> NAT
2026-07-18 10:35:31 +03:00
Evgeny 4b7c7738de debug: add DEBUG_CATEGORY_DB_SYNC (26) + TRACE logs for db_sync, merkle_sync, chat_sync, member_sync 2026-07-18 03:19:46 +03:00
Evgeny 9e34e9f838 fix chatgui: self in member list, online status on reconnect, lb spam
- cs_handle_welcome(): add self to local peers_<ch_id> so
  joiner sees own account in channel member list
- cs_on_conn_up(): post CHANNEL_PEERS_ONLINE after reconnect
  so GUI shows connected status (was only posted on conn_down)
- etcp_loadbalancer: DEBUG_WARN -> DEBUG_DEBUG for no suitable
  link: tmr:wait — normal shaper behavior, not a warning
2026-07-18 02:55:10 +03:00
Evgeny 3e0b9483b7 fix chatgui: channel persistence & peer list after restart
- chat_core_sync_my_addresses(): remove private IP filter (192.168, 10.x, etc.)
  so LAN addresses are stored for direct connections
- topo_node_sqlite_channel_peers_all(): include peers without addresses
  in WELCOME message (was skipped entirely, causing empty peer list)
- chat_core_create_channel(): pass real local addresses to member_sync_put()
  instead of NULL,0
- chat_core_init(): log number of channels loaded from DB on startup
2026-07-18 02:46:53 +03:00
Evgeny c594375023 fix: derive node_id from pubkey instead of privkey
sc_derive_node_id() hashed the private key, while invite links
(joindialog.cpp) hashed the public key — giving two different
node_ids for the same keypair.

Removed sc_derive_node_id(), kept only sc_derive_node_id_from_pubkey().
All callers updated to use pubkey-based derivation.
2026-07-18 00:41:38 +03:00
Evgeny 6f4025e420 refactor: move sqlite3 db from topo_group to UTUN_INSTANCE, remove conn_mgr/topo_group deps from chatgui
- topo_sqlite_db moved from TOPO_GROUPS to UTUN_INSTANCE (direct access)
- topo_groups_set_sqlite_db() removed
- chatgui: chat_sync_connect_node → chat_core_connect_auto (direct ETCP, no conn_mgr)
- chatgui: chat_core_connect_from_invite saves to SQLite → auto connect (no BGP)
- chatgui: removed _on_node_updated BGP callback from member_sync
- chatgui: removed conn_mgr_get_status from status dump
- chatgui: removed all conn_mgr.h and topo_group.h includes
- chatgui: CONN_MGR_ERR_* constants → CC_* in chat_core.h
- removed unused cc_parallel_* code, conn_state_str/conn_type_str helpers
2026-07-18 00:10:06 +03:00
Evgeny 52587c4f2b move ETCP_ID_NAT_DETECTION to etcp_api.h alongside other ETCP_IDs 2026-07-17 21:32:48 +03:00
Evgeny b238089678 refactor(nat_detection): remove BGP dependency, auto trigger on conn_up, clean API
- find_third_node: use nd->inst->connections instead of group->senders_list
- on_conn_up callback auto-calls trigger_checks (no BGP handshake needed)
- Remove group param from link_ready, request_check_all — nat_detection self-sufficient
- Remove trigger_checks + route_ping_cancel_for_conn from public API (internal only)
- topo_group.c: removed last nat_detection_trigger_checks call — completely decoupled
- Header: 4-line summary, no duplicate function list, 7 public functions
2026-07-17 21:23:40 +03:00
Evgeny 90363650e3 refactor(nat_detection): self-sufficient module with own ETCP_ID (0x02) and docs
- Assign ETCP_ID_NAT_DETECTION = 0x02, etcp_bind in create, etcp_unbind in destroy
- nat_detection_receive_cbk: own packet dispatcher (PING_REQ/RESP, NAT_INFO/CHECK_REQ)
- 4 handle_* become static internal, send_nat_check_req stays static (dead code)
- Rename TOPO_* -> NATDET_*: all subcommands, structs, message types
- Remove all NAT/PING constants and structs from topo_group.h
- Remove dispatch lines and subcmd_name cases from topo_group_receive_cbk
- API: clear sections (lifecycle/internal service/control/test helpers)
- trigger_checks: marked with IMPORTANT note about call order (must be after add_to_senders)
2026-07-17 20:45:49 +03:00
Evgeny 910cac8dce refactor: separate NAT detection from topology into standalone nat_detection module
- Extract NAT detection logic (~250 lines) from topo_group.c into new nat_detection.c
- NAT_DETECTION struct owns ping_pending, next_ping_req_id, allow_nat_check_local (removed from TOPO_GROUP)
- route_ping.* signatures changed: TOPO_GROUP* -> NAT_DETECTION* (+ TOPO_GROUP* where needed)
- topo_group.c now only dispatches BGP subcommands to nat_detection_* handlers
- UTUN_INSTANCE gets struct NAT_DETECTION* nat_det field (init/destroy in topo_groups_init/destroy)
- External callers (etcp_connections, etcp, control_server) call nat_detection_* directly
- nat_detection_cancel_for_conn / nat_detection_send_nat_info / nat_detection_link_ready / nat_detection_request_check_all
- route_ping_destroy_pending properly implemented (was dead code)
- TOPO_SUBCMD_PING_REQ/RESP moved from route_ping.h to topo_group.h (all BGP subcommands in one place)
- Tests updated: test_nat_detection, test_route_ping - both PASSED
- Build: auto(CORE_SOURCES+libutun) and cmake(chatgui/libutun)
2026-07-17 19:49:54 +03:00
Evgeny 0381f4df89 Убраны TCP_TIMESTAMPS и TCP_KEEPALIVE из lwip_tcp_opts.h (не используются, конфликтовали с ws2ipdef.h на Windows) 2026-07-17 17:49:15 +03:00
Evgeny 706ef5b472 etcp_connections.h: data[0] -> data[] (C99 flexible array, fix -Wstringop-overflow) 2026-07-17 17:40:25 +03:00
Evgeny 3884af7eb8 ntp_time.h: replace netinet/in.h with platform_compat.h for Windows build 2026-07-17 17:36:23 +03:00
Evgeny 960cde3eda chatgui: add Status page in settings (NTP + connections status) 2026-07-17 16:25:55 +03:00
Evgeny f0bb03c2cf db_sync: datahash removed, Ed25519 signature mandatory, PK=(timestamp,author_signature), chain_hash includes signature, signed message=ts||json 2026-07-17 15:44:19 +03:00
Evgeny 05b24d36bb db_sync: fix dh_match premature sync complete — send tail when mc > tp+1
Root cause: db_handle_init_resp marked sync_state=2 when my_dh==peer_dh
without checking that all my records fit in the confirmed prefix.
If mc > tp+1 (I have records beyond the common prefix), the tail was
never sent — lost records.

Fix:
- Remove special-case 'sc==0 && my_dh==peer_dh' (redundant, same bug)
- In general dh_match: if mc > tp+1, send remaining records [tp+1,mc-1]
  using the same inline loop pattern as 'peer empty' branch

Test: 3 phases covering all dh_match paths:
- Phase 1: B empty+late instance → dh_match at tp=0 (sc=0) → tail-send 49
- Phase 2: A=80 B=50 → dh_match at tp=49 (sc>0, sparse) → tail-send 30
- Phase 3: fresh instances → peer empty → send all 30
2026-07-17 11:48:56 +03:00
Evgeny 2792586de7 db_sync: add INFO diagnostics for message sync flow tracing
- db_sync_on_conn_up: per-instance synced/skipped counters
- db_sync_peer_check_cb: summary INFO of check results
- db_sync_instance_add: peers_found + peers_synced counts
- db_handle_init_sync/initiate_sync: enhanced with tbl name
- db_handle_init_resp: tp + my_mc in sync complete/dh match
- db_handle_send_data: range + SYNC_DONE details; stop reason when mc>pk
- db_handle_sync_done: both counts + datahashes in mismatch; dh in confirm
- db_sync_recv_cb: INFO when instance NOT FOUND (timing race)
- cs_handle_channel_info_resp: sync readiness after channel ready
- cs_handle_welcome: note about db_sync peer_check timer
- on_db_sync_insert: counter (#1-3 then every 10) with ch/n/ts/dh/ct
2026-07-17 00:13:27 +03:00
Evgeny db67767405 fix: demote 6 crypto diagnostic logs from INFO to DEBUG
ECDH, ENCRYPT, DECRYPT, INIT_SEND, NORM_DECRYPT_TRY, INIT_DECRYPT_TRY
all moved to DEBUG_DEBUG — visible only with debug=crypto=debug
2026-07-16 23:35:08 +03:00
Evgeny 2aa559acaf debug: add full ECDH/session-key diagnostics (6 points)
K1: sc_set_peer_public_key — priv, peer_pub, shared_secret, session_key
K2: sc_encrypt — session_key, tx_counter, nonce, plaintext
K3: sc_decrypt — session_key, nonce from packet, ciphertext_len
K4: INIT_SEND — salt, obfuscated_pubkey, peer_pubkey, my_pubkey, session_key
K5: NORM_DECRYPT_TRY — session_key, recv_len, rx_counter
K6: INIT_DECRYPT_TRY — session_key, recv_len, recv_len-40, salt, encrypted_pubkey

All via DEBUG_INFO(DEBUG_CATEGORY_CRYPTO) — enable with debug=crypto=info
2026-07-16 22:13:35 +03:00
Evgeny cdcc32cf84 fix: move DECRYPT FAIL/collision/reinit logs from DEBUG_CATEGORY_DEBUG to proper categories
- DECRYPT FAIL (1554): DEBUG_DEBUG → ETCP (main failure signal)
- not an init packet (1623): DEBUG_DEBUG → CONNECTION (was invisible ERROR)
- pkt_pool exhaust (1537): add ERROR log (was silent)
- REINIT/collision events (1473,1751-1838): DEBUG_DEBUG → CONNECTION
2026-07-16 22:03:33 +03:00
Evgeny 95061d3723 fix: demote INIT retry spam (link/send logs) to DEBUG_DEBUG
Keep at INFO only 'Sending INIT request' line per retry.
2026-07-16 21:41:50 +03:00
Evgeny f039031883 fix: check conn_queue_entry->peer_node_id instead of conn->peer_node_id
etcp_conn_set_peer_node_id at state=0 updates conn->peer_node_id but
does not reindex the queue entry (key stays 0). Check the queue entry
directly to detect unindexed outbound connections.
2026-07-16 21:05:08 +03:00
Evgeny c3354938e1 fix: peers_* table creation + GUI_ERROR to log + cross-connect conn reuse
- chat_core.c: CREATE TABLE IF NOT EXISTS peers_* in ensure_channel_ready
- utun_node.cpp: GUI_ERROR redirected to debug log file via guiDebugFile()
- etcp_connections.c: reuse unindexed outbound conn (peer_node_id==0)
  on cross-connect INIT to avoid 'Can not insert link to socket'
2026-07-16 20:51:24 +03:00
Evgeny 3d04c37f56 stcp: add on_close callback to stcp_server_create/stcp_client_connect; member_sync: add join_ts/update_sig/update_ts fields, verify update_sig in apply; topo_node_sqlite: add join_ts/update_sig/update_ts to member_put, add member_get_join, add node_update_verified; chat_sync: include join_ts in join_msg, add update_sig/update_ts to CHANNEL_INFO_RESP; WELCOME verify join_sig; tests: use generated node_ids, reduce max ticks 2026-07-16 19:04:00 +03:00
Evgeny bb425c6722 db_sync v2: positional sync per-peer (synced_pos), datahash instead of chain_hash, no cascade on PUSH, single cascade after SEND_DATA batch, startup chain verification. PRAGMA synchronous=NORMAL + wal_autocheckpoint=10000 for performance. 2026-07-16 12:31:52 +03:00
Evgeny 0479c309bb Диагностика синхронизации: merkle_sync DEBUG_DEBUG логи обмена; убран спам auto_connect no IPv4; сохранение адреса инвайтера из ETCP; детали DECRYPT FAIL (link_state/enc_errs) 2026-07-16 00:41:51 +03:00
Evgeny 3f0fdad754 config_updater: поддержка создания конфига с нуля (без файла); новый файл — собирается с нуля, существующий — insert_or_replace 2026-07-15 23:47:00 +03:00
Evgeny e325b84a8e config: одно открытие файла — parse_config_from_buf вместо повторного fopen; убран read_file_to_mem 2026-07-15 23:39:01 +03:00
Evgeny ef24af7d24 Ключи: node_id из SHA-256(privkey) через весь конфиг; приоритет privkey — pubkey/node_id всегда выводятся из него; CHANNEL_INFO_RESP: верификация inviter_join_sig через ETCP-ключи 2026-07-15 23:13:29 +03:00
Evgeny da86841516 Диагностика: WARN при мутации конфига (ключи/node_id) + детали при invalid sig в JOIN/CHANNEL_INFO_RESP + предупреждение при расхождении node_id инвайта и ETCP 2026-07-15 22:44:33 +03:00
Evgeny 895eb0abbf Убрал спам-логи ETCP SEND/RECV/DECRYPT OK + добавил диагностику синхронизации мемберов (DEBUG_CATEGORY_DEBUG) в ключевые точки topo_group, merkle_sync, member_sync, chat_sync 2026-07-15 22:25:19 +03:00
Evgeny e708940bcd debug: upgrade router_deliver to DEBUG_INFO, show cb ptr
Router deliver now visible without trace — shows svc_id, len, remote_node_id, conn ptr, callback ptr
This will confirm whether CHANNEL_INFO_REQ reaches chat_sync on the server side
2026-07-15 22:06:54 +03:00
Evgeny d161782b46 fix: merkle_sync.c linked-list -> queue lookup, upgrade debug logs to INFO
- merkle_sync.c ms_find_conn_for_node: replace c=c->next with queue_find_data_by_index
- chat_sync.c cs_find_conn_for_node, cs_on_conn_up: DEBUG_DEBUG -> DEBUG_INFO
- etcp.c queue_set_ready: DEBUG_DEBUG -> DEBUG_INFO
2026-07-15 21:59:29 +03:00