fix: invite не затирает имя узла, имя читается из member_sync peers.userinfo

- conn_mgr_core.c: убрана запись node_name из cm_handle_invite_info_resp
- topo_node_sqlite.c: name в nodes не перезаписывается пустой строкой
- android_jni_bridge.c: bridge_get_node_name читает peers_<ch>.userinfo если nodes.name пуст
This commit is contained in:
evgeny committed 2026-08-03 18:50:11 +03:00
1 parent 253129cb27
commit e1e54df4f4
14 files changed
+234 -125

No files matched your search

+40 -8
View File
@@ -15,6 +15,7 @@
#include "../lib/platform_compat.h"
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <openssl/evp.h>
#include "../media_async/media_async.h"
@@ -39,6 +40,19 @@ static void md_dl_free(struct media_download* dl) {
/* ── send helpers ── */
static void md_mkdir_parent(const char* filepath) {
char path[1024]; snprintf(path, sizeof(path), "%s", filepath);
char* p = path[0] == '/' ? path + 1 : path;
while ((p = strchr(p, '/'))) {
*p = '\0';
if (utun_mkdir(path, 0755) != 0 && errno != EEXIST) {
DEBUG_ERROR(DEBUG_CATEGORY_GENERAL, "%s: mkdir(%s) failed: %s", MDL_ID, path, strerror(errno));
*p = '/'; return;
}
*p = '/'; p++;
}
}
static int md_dl_send(struct UTUN_INSTANCE* inst, uint64_t dst, const uint8_t* data, size_t len) {
struct ll_entry* e = queue_entry_new(0);
if (!e) { DEBUG_ERROR(DEBUG_CATEGORY_GENERAL, "%s: queue_entry_new failed for send to 0x%016llx", MDL_ID, (unsigned long long)dst); return -1; }
@@ -64,8 +78,9 @@ static int md_dl_send_block_req(struct UTUN_INSTANCE* inst, uint64_t dst,
req.chunk = (uint32_t)bi;
req.offset = 0;
DEBUG_INFO(DEBUG_CATEGORY_DEBUG, "%s: BLOCK_REQ to 0x%016llx group=%016llx block=%d",
MDL_ID, (unsigned long long)dst, (unsigned long long)dl->group_id, bi);
DEBUG_INFO(DEBUG_CATEGORY_DEBUG, "%s: BLOCK_REQ to 0x%016llx group=%016llx bi=%d dl->num_blocks=%d block_id=%02x%02x%02x%02x...",
MDL_ID, (unsigned long long)dst, (unsigned long long)dl->group_id, bi, dl->num_blocks,
dl->block_ids[bi * 16], dl->block_ids[bi * 16 + 1], dl->block_ids[bi * 16 + 2], dl->block_ids[bi * 16 + 3]);
return md_dl_send(inst, dst, (const uint8_t*)&req, sizeof(req));
}
@@ -104,6 +119,8 @@ static void md_dl_send_have_block(struct UTUN_INSTANCE* inst, struct media_downl
static void md_dl_conn_cb(struct CONN_MGR_HANDLE* h, uint64_t node_id, uint64_t group_id,
enum conn_mgr_event event, void* arg) {
struct media_download* dl = (struct media_download*)arg; (void)h; (void)group_id;
DEBUG_INFO(DEBUG_CATEGORY_DEBUG, "%s: conn_cb event=%d node=0x%016llx dl=%p active=%d num_peers=%d num_blocks=%d",
MDL_ID, event, (unsigned long long)node_id, (void*)dl, dl->active, dl->num_peers, dl->num_blocks);
if (event != CONN_EVENT_UP) {
DEBUG_WARN(DEBUG_CATEGORY_GENERAL, "%s: connect to 0x%016llx failed rc=%d",
MDL_ID, (unsigned long long)node_id, event);
@@ -113,9 +130,13 @@ static void md_dl_conn_cb(struct CONN_MGR_HANDLE* h, uint64_t node_id, uint64_t
for (int pi = 0; pi < dl->num_peers; pi++) {
if (dl->peers[pi].node_id == node_id) {
dl->peers[pi].connected = 1;
DEBUG_INFO(DEBUG_CATEGORY_DEBUG, "%s: matched peer[%d] num_blocks=%d",
MDL_ID, pi, dl->peers[pi].num_blocks);
for (int bi = 0; bi < dl->peers[pi].num_blocks; bi++) {
if (!dl->peers[pi].blocks[bi].started) {
dl->peers[pi].blocks[bi].started = 1;
DEBUG_INFO(DEBUG_CATEGORY_DEBUG, "%s: conn_cb BLOCK_REQ: peer-local bi=%d",
MDL_ID, bi);
md_dl_send_block_req(dl->inst, node_id, dl, bi);
}
}
@@ -234,8 +255,12 @@ void media_download_handle_query_resp(struct UTUN_INSTANCE* inst,
}
}
DEBUG_INFO(DEBUG_CATEGORY_DEBUG, "%s: QUERY_RESP entries=%d peers=%d",
MDL_ID, ne, dl->num_peers);
DEBUG_INFO(DEBUG_CATEGORY_DEBUG, "%s: QUERY_RESP entries=%d peers=%d dl=%p num_blocks=%d",
MDL_ID, ne, dl->num_peers, (void*)dl, dl->num_blocks);
for (int pi = 0; pi < dl->num_peers; pi++) {
DEBUG_INFO(DEBUG_CATEGORY_DEBUG, "%s: peer[%d] node=0x%016llx num_blocks=%d",
MDL_ID, pi, (unsigned long long)dl->peers[pi].node_id, dl->peers[pi].num_blocks);
}
/* if no peers found, try querying the author directly */
if (dl->num_peers == 0 && dl->author_node_id && dl->author_node_id != inst->node_id) {
@@ -259,11 +284,17 @@ void media_download_handle_query_resp(struct UTUN_INSTANCE* inst,
for (int pi = 0; pi < dl->num_peers; pi++) {
dl->peers[pi].connected = 1;
if (grp && grp->conn_mgr)
conn_mgr_open_invite(inst, grp->group_id, NULL, dl->peers[pi].node_id, md_dl_conn_cb, dl, NULL);
else
for (int bi = 0; bi < dl->peers[pi].num_blocks; bi++)
DEBUG_INFO(DEBUG_CATEGORY_DEBUG, "%s: peer[%d] route: grp=%p conn_mgr=%p",
MDL_ID, pi, (void*)grp, grp ? (void*)grp->conn_mgr : NULL);
if (grp && grp->conn_mgr) {
int cm_rc = conn_mgr_open_invite(inst, grp->group_id, NULL, dl->peers[pi].node_id, md_dl_conn_cb, dl, NULL);
if (cm_rc != 0) DEBUG_ERROR(DEBUG_CATEGORY_GENERAL, "%s: conn_mgr_open_invite failed rc=%d for 0x%016llx", MDL_ID, cm_rc, (unsigned long long)dl->peers[pi].node_id);
} else
for (int bi = 0; bi < dl->peers[pi].num_blocks; bi++) {
DEBUG_INFO(DEBUG_CATEGORY_DEBUG, "%s: direct BLOCK_REQ: peer-local bi=%d peer_num_blocks=%d",
MDL_ID, bi, dl->peers[pi].num_blocks);
md_dl_send_block_req(inst, dl->peers[pi].node_id, dl, bi);
}
}
}
@@ -441,6 +472,7 @@ int media_download_start(struct UTUN_INSTANCE* inst, uint64_t group_id,
memcpy(dl->media_id, result->media_id, 16);
dl->group_id = group_id;
snprintf(dl->dest_path, sizeof(dl->dest_path), "%s", dest_path);
md_mkdir_parent(dl->dest_path);
snprintf(dl->media_base, sizeof(dl->media_base), "%s", media_base);
dl->num_blocks = result->num_blocks;
dl->file_size = result->file_size;
+14 -8
View File
@@ -58,6 +58,10 @@ static void cm_deliver_up_cb(void* arg) {
* Все поля handle'а читаются до вызова коллбэка — коллбэк может закрыть
* handle (conn_mgr_close), после вызова к handle не обращаемся. */
void cm_deliver_event(struct CONN_MGR_ENTRY* entry, enum conn_mgr_event event) {
if (!entry || !entry->mgr || !entry->mgr->group) {
DEBUG_ERROR(DEBUG_CATEGORY_GENERAL, "cm_deliver_event: BAD entry=%p mgr=%p", entry, entry ? (void*)entry->mgr : NULL);
return;
}
uint64_t gid = entry->mgr->group->group_id;
struct CONN_MGR_HANDLE* h = entry->handles;
while (h) {
@@ -221,7 +225,7 @@ void cm_update_nodeinfo(struct CONN_MGR_ENTRY* entry) {
}
void cm_cleanup_db_node(struct CONN_MGR_ENTRY* entry) {
if (!entry->db_loaded) return;
if (!entry || !entry->mgr || !entry->mgr->group || !entry->db_loaded) return;
struct TOPO_GROUP* group = entry->mgr->group;
struct TOPO_GROUP_NODE* nq = topo_node_find_by_id(group, entry->node_id);
if (!nq) return;
@@ -243,7 +247,7 @@ void cm_cleanup_db_node(struct CONN_MGR_ENTRY* entry) {
* DOWN — соединение упало, доставляем DOWN. */
void cm_ncd_callback(struct NODE_CONN_DIRECT* ncd_h, enum ncd_event ncd_ev, void* arg) {
struct CONN_MGR_ENTRY* entry = (struct CONN_MGR_ENTRY*)arg; (void)ncd_h;
if (entry->state == CONN_MGR_STATE_DISCONNECTED) return;
if (!entry || !entry->mgr || entry->state == CONN_MGR_STATE_DISCONNECTED) return;
switch (ncd_ev) {
case NCD_EVENT_UP:
if (entry->state == CONN_MGR_STATE_CONNECTED) return;
@@ -255,13 +259,17 @@ void cm_ncd_callback(struct NODE_CONN_DIRECT* ncd_h, enum ncd_event ncd_ev, void
cm_update_nodeinfo(entry); cm_deliver_event(entry, CONN_EVENT_UP);
break;
case NCD_EVENT_TIMEOUT:
DEBUG_INFO(DEBUG_CATEGORY_GENERAL, "link: ETCP connect TIMEOUT to 0x%016llx — no response, trying fallback",
(unsigned long long)entry->node_id);
DEBUG_INFO(DEBUG_CATEGORY_GENERAL, "link: ETCP connect TIMEOUT to 0x%016llx — entry=%p mgr=%p db_loaded=%d",
(unsigned long long)entry->node_id, entry, (void*)entry->mgr, entry->db_loaded);
if (entry->db_loaded) {
entry->main_connect_state = CM_TRY_FAILED;
{ struct NODE_CONN_DIRECT* ncd = entry->ncd_handle; entry->ncd_handle = NULL;
if (ncd) node_conn_direct_force_close(ncd); }
cm_cleanup_db_node(entry); cm_deliver_event(entry, CONN_EVENT_TIMEOUT);
DEBUG_INFO(DEBUG_CATEGORY_GENERAL, "link: TIMEOUT after force_close, calling cleanup");
cm_cleanup_db_node(entry);
DEBUG_INFO(DEBUG_CATEGORY_GENERAL, "link: TIMEOUT after cleanup, calling deliver");
cm_deliver_event(entry, CONN_EVENT_TIMEOUT);
DEBUG_INFO(DEBUG_CATEGORY_GENERAL, "link: TIMEOUT done");
return;
}
entry->main_connect_state = CM_TRY_FAILED;
@@ -774,9 +782,7 @@ void cm_handle_invite_info_resp(struct CONN_MGR* mgr, struct ETCP_CONN* conn, co
}
if (inv->temp_nq) {
struct TOPO_NODE* ni2 = topo_node_registry_find(inv->mgr->instance->topo_groups, inv->temp_nq->node_id);
if (ni2) { memcpy(ni2->ed25519_public_key, resp->ed25519_pubkey, SC_PUBKEY_SIZE);
if (ni2->node_name) { u_free(ni2->node_name); ni2->node_name = NULL; }
if (resp->node_name_len) ni2->node_name = u_strdup((const char*)resp->node_name); }
if (ni2) memcpy(ni2->ed25519_public_key, resp->ed25519_pubkey, SC_PUBKEY_SIZE);
if (inv->mgr->instance->topo_sqlite_db) topo_node_sqlite_node_put(inv->mgr->instance->topo_sqlite_db, inv->mgr->instance->topo_groups, inv->temp_nq, (time_t)(get_time_tb()/10000));
}
if (inv->overall_timer) { uasync_cancel_timeout(inv->mgr->instance->ua, inv->overall_timer); inv->overall_timer = NULL; }
+2 -3
View File
@@ -249,10 +249,9 @@ static void topo_group_destroy(struct TOPO_GROUP* group) {
while ((e = queue_data_get(group->senders_list)) != NULL) queue_entry_free(e);
queue_free(group->senders_list);
if (group->local_node) { topo_nodeq_free_group_fields(group->instance->topo_groups, group->local_node); u_free(group->local_node); }
if (group->conn_mgr) { conn_mgr_destroy(group->conn_mgr); group->conn_mgr = NULL; }
if (group->nodes) queue_free(group->nodes);
group->conn_mgr = NULL; /* destroyed externally in utun_instance_destroy before etcp_router_destroy */
if (group->local_node) { topo_nodeq_free_group_fields(group->instance->topo_groups, group->local_node); u_free(group->local_node); }
}
struct TOPO_GROUPS* topo_groups_init(struct UTUN_INSTANCE* instance) {
+1 -1
View File
@@ -119,7 +119,7 @@ int topo_node_sqlite_node_put(sqlite3* db, struct TOPO_GROUPS* groups, struct TO
"INSERT INTO nodes(node_id, name, x25519_pubkey, ed25519_pubkey, last_seen_at)"
" VALUES(?,?,?,?,?)"
" ON CONFLICT(node_id) DO UPDATE SET"
" name=excluded.name, x25519_pubkey=excluded.x25519_pubkey,"
" name=CASE WHEN excluded.name != '' THEN excluded.name ELSE nodes.name END, x25519_pubkey=excluded.x25519_pubkey,"
" ed25519_pubkey=excluded.ed25519_pubkey, last_seen_at=excluded.last_seen_at,\n"
" created_at=COALESCE(nodes.created_at, excluded.created_at)",
-1, &stmt, NULL) == SQLITE_OK) {
+16 -4
View File
@@ -828,7 +828,7 @@ void node_conn_direct_close(struct NODE_CONN_DIRECT* h) {
entry->fin_wait_timer = uasync_set_timeout(entry->ua, NCD_FIN_WAIT_TIMEOUT_TB, entry, ncd_fin_wait_timeout_cb, "ncd_fin_wait");
DEBUG_INFO(DEBUG_CATEGORY_NCD, "[ncd] fin_wait started node=0x%016llx", (unsigned long long)node_id);
} else {
if (conn) {
if (conn && conn->state != 2) {
etcp_conn_remove_cbk(conn, ncd_init_cb, entry);
etcp_conn_remove_cbk(conn, ncd_up_cb, entry);
etcp_conn_remove_cbk(conn, ncd_down_cb, entry);
@@ -855,8 +855,10 @@ void node_conn_direct_close(struct NODE_CONN_DIRECT* h) {
*/
void node_conn_direct_force_close(struct NODE_CONN_DIRECT* h) {
if (!h) return;
if (!h->entry) { DEBUG_ERROR(DEBUG_CATEGORY_NCD, "[ncd] force_close: h=%p h->entry=NULL", h); u_free(h); return; }
struct ncd_entry* entry = h->entry;
if (!entry) { u_free(h); return; }
DEBUG_INFO(DEBUG_CATEGORY_NCD, "[ncd] force_close h=%p entry=%p node=0x%016llx handles=%d",
h, entry, (unsigned long long)entry->node_id, entry->handle_count);
uint64_t node_id = entry->node_id;
struct ETCP_CONN* conn = entry->conn;
@@ -883,19 +885,29 @@ void node_conn_direct_force_close(struct NODE_CONN_DIRECT* h) {
h->cb = NULL; h->cb_arg = NULL;
if (entry->connect_timer) { uasync_cancel_timeout(entry->ua, entry->connect_timer); entry->connect_timer = NULL; }
if (entry->fin_wait_timer) { uasync_cancel_timeout(entry->ua, entry->fin_wait_timer); entry->fin_wait_timer = NULL; }
if (conn) {
conn->fin_wait = 0; conn->fin_wait_clear_cb = NULL; conn->fin_wait_clear_arg = NULL;
if (conn && conn->state != 2 && conn->instance) {
DEBUG_INFO(DEBUG_CATEGORY_NCD, "[ncd] force_close: cleaning conn=%p", conn);
conn->fin_wait = 0;
conn->fin_wait_clear_cb = NULL; conn->fin_wait_clear_arg = NULL;
DEBUG_INFO(DEBUG_CATEGORY_NCD, "[ncd] force_close: removing ncd_init_cb");
etcp_conn_remove_cbk(conn, ncd_init_cb, entry);
DEBUG_INFO(DEBUG_CATEGORY_NCD, "[ncd] force_close: removing ncd_up_cb");
etcp_conn_remove_cbk(conn, ncd_up_cb, entry);
DEBUG_INFO(DEBUG_CATEGORY_NCD, "[ncd] force_close: removing ncd_down_cb");
etcp_conn_remove_cbk(conn, ncd_down_cb, entry);
DEBUG_INFO(DEBUG_CATEGORY_NCD, "[ncd] force_close: scheduling deferred close");
if (conn->state != 2) uasync_call_soon(entry->ua, conn, ncd_deferred_close_conn);
DEBUG_INFO(DEBUG_CATEGORY_NCD, "[ncd] force_close: removing from registry");
ncd_registry_remove(conn->instance, entry);
}
DEBUG_INFO(DEBUG_CATEGORY_NCD, "[ncd] force_close: freeing entry=%p", entry);
u_free(entry);
}
h->entry = NULL;
DEBUG_INFO(DEBUG_CATEGORY_NCD, "[ncd] force_close: freeing h=%p", h);
u_free(h);
DEBUG_INFO(DEBUG_CATEGORY_NCD, "[ncd] force_close: done");
}
/*
+5
View File
@@ -64,7 +64,9 @@ static int decrypt_and_check(uint8_t *data, size_t len, struct sc_stream_state *
static void client_do_close(struct stcp_conn *c, int err) {
if (c->state == STCP_STATE_CLOSED || c->state == STCP_STATE_ERROR) return;
int prev = c->state;
c->state = STCP_STATE_CLOSED;
DEBUG_INFO(DEBUG_CATEGORY_SOCKET, "stcp_client: closed is_server=%d err=%d prev_state=%d", c->is_server, err, prev);
if (c->socket_id) { uasync_remove_socket_t(c->ua, c->sock); c->socket_id = NULL; }
if (c->sock != SOCKET_INVALID) { socket_close_wrapper(c->sock); c->sock = SOCKET_INVALID; }
if (c->recv_buf) { u_free(c->recv_buf); c->recv_buf = NULL; c->recv_buf_len = 0; c->recv_buf_cap = 0; }
@@ -145,6 +147,7 @@ static void client_send_handshake(struct stcp_conn *c, const uint8_t *server_pub
for (int i = 0; i < padding; i++) hs[SC_PUBKEY_ENC_SIZE + STCP_HS_ENC_CLIENT + i] = (uint8_t)(salt[0] ^ i);
c->state = STCP_STATE_HS_CLIENT_SENT;
DEBUG_INFO(DEBUG_CATEGORY_SOCKET, "stcp_client: handshake sent (%zu bytes), entering HS_CLIENT_SENT", total);
client_try_send(c, hs, total);
}
@@ -163,6 +166,7 @@ static void process_server_response(struct stcp_conn *c) {
uint8_t status = enc_hs[0];
uint16_t padding_size = (uint16_t)enc_hs[1] | ((uint16_t)enc_hs[2] << 8);
c->hs_expected_len = SC_PUBKEY_ENC_SIZE + STCP_HS_ENC_SERVER + padding_size;
DEBUG_INFO(DEBUG_CATEGORY_SOCKET, "stcp_client: server response OK status=%d padding=%u", status, padding_size);
if (status != 0) { DEBUG_ERROR(DEBUG_CATEGORY_SOCKET, "server handshake status=%d", status); client_do_close(c, 4); return; }
}
@@ -235,6 +239,7 @@ static void client_connect_write_cb(socket_t sock, void *arg) {
DEBUG_ERROR(DEBUG_CATEGORY_SOCKET, "stcp_client connect failed err=%d", err);
client_do_close(c, err); return;
}
DEBUG_INFO(DEBUG_CATEGORY_SOCKET, "stcp_client: TCP connected, starting handshake");
uasync_remove_socket_t(cli->ua, sock);
c->socket_id = uasync_add_socket_t(cli->ua, sock, client_conn_read_cb, client_conn_write_cb, NULL, c);
if (!c->socket_id) { client_do_close(c, ENOMEM); return; }
+2 -1
View File
@@ -104,7 +104,8 @@ static void client_ready_cb(struct stcp_conn *conn, void *arg) {
link->etcp_conn.send_input_q = link->tx_queue;
stcp_conn_set_tx_queue(conn, link->tx_queue);
DEBUG_INFO(DEBUG_CATEGORY_SOCKET, "stcp_link: client connected");
DEBUG_INFO(DEBUG_CATEGORY_SOCKET, "stcp_link: client connected, peer_pubkey=%02x%02x%02x%02x...",
link->peer_pubkey[0], link->peer_pubkey[1], link->peer_pubkey[2], link->peer_pubkey[3]);
if (link->on_ready_cb) link->on_ready_cb(link, link->ready_arg);
}
+1
View File
@@ -200,6 +200,7 @@ static void process_client_handshake(struct stcp_conn *c) {
uint16_t padding_size = (uint16_t)enc_hs[0] | ((uint16_t)enc_hs[1] << 8);
c->hs_expected_len = SC_PUBKEY_ENC_SIZE + STCP_HS_ENC_CLIENT + padding_size;
c->hs_key_processed = 1;
DEBUG_INFO(DEBUG_CATEGORY_SOCKET, "stcp_server: client handshake key processed, expecting %zu bytes padding", c->hs_expected_len - SC_PUBKEY_ENC_SIZE - STCP_HS_ENC_CLIENT);
}
static void finish_client_handshake(struct stcp_conn *c) {
-1
View File
@@ -442,7 +442,6 @@ void utun_instance_destroy(struct UTUN_INSTANCE *instance) {
}
// Cleanup conn_mgr BEFORE sockets (unbinds from etcp_router, closes NCD handles while conns alive)
if (instance->conn_mgr) { conn_mgr_destroy(instance->conn_mgr); instance->conn_mgr = NULL; }
// Cleanup ETCP sockets and connections FIRST (before destroying uasync)
DEBUG_INFO(DEBUG_CATEGORY_MEMORY, "[INSTANCE_DESTROY] Cleaning up ETCP sockets and connections");