fix(chat): node online status race, WELCOME peer_needed off-by-one, JOIN missing proto byte

- node_put/node_update_verified: set online=1 on INSERT (nodes created when conn is UP)
- channel_peers_all: fix peer_needed calculation (atl byte was missing)
- cs_send_channel_join: add proto byte to address serialization matching all parsers
This commit is contained in:
evgeny committed 2026-08-06 01:18:27 +03:00
1 parent c1bcf6590a
commit 4e56590958
18 files changed
+431 -24

No files matched your search

+12 -8
View File
@@ -1128,9 +1128,10 @@ static void cs_handle_channel_info_resp(struct chat_sync* cs, uint64_t peer,
else { sock = sock->next; continue; }
if (sa->ss_family == AF_INET) {
struct sockaddr_in* sin = (struct sockaddr_in*)sa;
if (joff + 8 > sizeof(jbuf)) break;
if (joff + 9 > sizeof(jbuf)) break;
jbuf[joff++] = 4;
jbuf[joff++] = sock->sock_id;
jbuf[joff++] = 1;
memcpy(jbuf + joff, &sin->sin_addr, 4); joff += 4;
uint16_t p = ntohs(sin->sin_port);
jbuf[joff++] = (uint8_t)((p >> 8) & 0xFF);
@@ -1138,9 +1139,10 @@ static void cs_handle_channel_info_resp(struct chat_sync* cs, uint64_t peer,
addr_cnt++;
} else if (sa->ss_family == AF_INET6) {
struct sockaddr_in6* sin6 = (struct sockaddr_in6*)sa;
if (joff + 20 > sizeof(jbuf)) break;
if (joff + 21 > sizeof(jbuf)) break;
jbuf[joff++] = 6;
jbuf[joff++] = sock->sock_id;
jbuf[joff++] = 1;
memcpy(jbuf + joff, &sin6->sin6_addr, 16); joff += 16;
uint16_t p = ntohs(sin6->sin6_port);
jbuf[joff++] = (uint8_t)((p >> 8) & 0xFF);
@@ -1217,9 +1219,10 @@ static void cs_handle_channel_join(struct chat_sync* cs, uint64_t peer,
if (sqlite3_prepare_v2(db, sql, -1, &stmt, NULL) == SQLITE_OK) {
sqlite3_bind_int64(stmt, 1, (sqlite3_int64)node_id);
sqlite3_bind_int(stmt, 2, fm);
sqlite3_bind_blob(stmt, 3, ip, ip_len, SQLITE_STATIC);
sqlite3_bind_int(stmt, 4, (int)port);
sqlite3_bind_int(stmt, 5, (int)sid);
sqlite3_bind_int(stmt, 3, (int)proto);
sqlite3_bind_blob(stmt, 4, ip, ip_len, SQLITE_STATIC);
sqlite3_bind_int(stmt, 5, (int)port);
sqlite3_bind_int(stmt, 6, (int)sid);
sqlite3_step(stmt); sqlite3_finalize(stmt);
}
if (fm == 4)
@@ -1391,9 +1394,10 @@ static void cs_handle_welcome(struct chat_sync* cs, uint64_t peer,
if (sqlite3_prepare_v2(db, sql, -1, &stmt2, NULL) == SQLITE_OK) {
sqlite3_bind_int64(stmt2, 1, (sqlite3_int64)node_id);
sqlite3_bind_int(stmt2, 2, fm);
sqlite3_bind_blob(stmt2, 3, ip, ip_len, SQLITE_STATIC);
sqlite3_bind_int(stmt2, 4, (int)port);
sqlite3_bind_int(stmt2, 5, (int)sid);
sqlite3_bind_int(stmt2, 3, (int)proto);
sqlite3_bind_blob(stmt2, 4, ip, ip_len, SQLITE_STATIC);
sqlite3_bind_int(stmt2, 5, (int)port);
sqlite3_bind_int(stmt2, 6, (int)sid);
sqlite3_step(stmt2); sqlite3_finalize(stmt2);
}
if (fm == 4)
+16 -1
View File
@@ -791,7 +791,22 @@ void cm_handle_invite_info_resp(struct CONN_MGR* mgr, struct ETCP_CONN* conn, co
DEBUG_INFO(DEBUG_CATEGORY_GENERAL, "invite: membership CONFIRMED by 0x%016llx name=\"%.*s\" — joined to channel, handle registered",
(unsigned long long)inv->node_id, resp->node_name_len, resp->node_name_len ? (const char*)resp->node_name : "");
if (inv->cb) inv->cb(inv->handle, inv->node_id, inv->mgr->group->group_id, CONN_EVENT_JOIN, inv->cb_arg);
if (inv->ncd_handle) { node_conn_direct_close(inv->ncd_handle); inv->ncd_handle = NULL; }
if (inv->ncd_handle) {
struct CONN_MGR_ENTRY* old_e = cm_find_entry(inv->mgr, inv->node_id);
if (old_e) {
if (old_e->ncd_handle) { node_conn_direct_force_close(old_e->ncd_handle); old_e->ncd_handle = NULL; }
cm_entry_cleanup(old_e);
}
struct CONN_MGR_ENTRY* cm_entry = cm_ensure_entry(inv->mgr, inv->node_id);
if (cm_entry) {
node_conn_direct_transfer(inv->ncd_handle, cm_ncd_callback, cm_entry);
cm_entry->ncd_handle = inv->ncd_handle;
cm_entry->state = CONN_MGR_STATE_CONNECTED;
cm_entry->conn_type = CONN_TYPE_DIRECT;
cm_update_nodeinfo(cm_entry);
}
inv->ncd_handle = NULL;
}
cm_invite_cleanup(inv);
}
+8 -8
View File
@@ -292,16 +292,16 @@ static void tgc_phase2_try_next(struct TOPO_GROUP_CONNECT* gc) {
if (gc->tried_super == 0) {
topo_node_sqlite_get_supernode_peers(db, gc->group->channel_id, &gc->candidate_ids, &gc->candidate_count);
gc->tried_super = 1; gc->cursor = 0;
DEBUG_DEBUG(DEBUG_CATEGORY_BGP, "%s: Phase2 supernode round — loaded %d for ch=%s",
DEBUG_INFO(DEBUG_CATEGORY_BGP, "%s: Phase2 supernode round — loaded %d for ch=%s",
TGC_ID, gc->candidate_count, gc->group->channel_id);
} else if (gc->tried_super == 1) {
topo_node_sqlite_get_public_peers(db, gc->group->channel_id, &gc->candidate_ids, &gc->candidate_count,
gc->group->instance->node_id);
gc->tried_super = 2; gc->cursor = 0;
DEBUG_DEBUG(DEBUG_CATEGORY_BGP, "%s: Phase2 public round — loaded %d for ch=%s",
DEBUG_INFO(DEBUG_CATEGORY_BGP, "%s: Phase2 public round — loaded %d for ch=%s",
TGC_ID, gc->candidate_count, gc->group->channel_id);
} else {
DEBUG_DEBUG(DEBUG_CATEGORY_BGP, "%s: Phase2 exhausted — no connections, ch=%s", TGC_ID, gc->group->channel_id);
DEBUG_INFO(DEBUG_CATEGORY_BGP, "%s: Phase2 exhausted — no connections, ch=%s", TGC_ID, gc->group->channel_id);
gc->phase = TGC_PHASE_THREE; gc->candidate_count = 0;
tgc_phase3_try_next(gc);
return;
@@ -309,9 +309,9 @@ static void tgc_phase2_try_next(struct TOPO_GROUP_CONNECT* gc) {
}
if (gc->cursor < gc->candidate_count) {
uint64_t nid = gc->candidate_ids[gc->cursor++];
DEBUG_DEBUG(DEBUG_CATEGORY_BGP, "%s: Phase2 trying 0x%016llx (%d/%d) super_round=%d", TGC_ID,
DEBUG_INFO(DEBUG_CATEGORY_BGP, "%s: Phase2 trying 0x%016llx (%d/%d) super_round=%d", TGC_ID,
(unsigned long long)nid, gc->cursor, gc->candidate_count, gc->tried_super);
conn_mgr_open_invite(gc->group->instance, gc->group->group_id, NULL, nid, tgc_callback, gc, NULL);
if (conn_mgr_open_invite(gc->group->instance, gc->group->group_id, NULL, nid, tgc_callback, gc, NULL) < 0) continue;
return;
}
if (gc->candidate_ids) { u_free(gc->candidate_ids); gc->candidate_ids = NULL; }
@@ -330,14 +330,14 @@ static void tgc_phase3_try_next(struct TOPO_GROUP_CONNECT* gc) {
topo_node_sqlite_get_local_peers(db, gc->group->channel_id, &gc->candidate_ids, &gc->candidate_count,
gc->group->instance->node_id);
gc->cursor = 0;
DEBUG_DEBUG(DEBUG_CATEGORY_BGP, "%s: Phase3 loaded %d local peers for ch=%s",
DEBUG_INFO(DEBUG_CATEGORY_BGP, "%s: Phase3 loaded %d local peers for ch=%s",
TGC_ID, gc->candidate_count, gc->group->channel_id);
}
while (gc->cursor < gc->candidate_count) {
uint64_t nid = gc->candidate_ids[gc->cursor++];
DEBUG_DEBUG(DEBUG_CATEGORY_BGP, "%s: Phase3 trying 0x%016llx (%d/%d)", TGC_ID,
DEBUG_INFO(DEBUG_CATEGORY_BGP, "%s: Phase3 trying 0x%016llx (%d/%d)", TGC_ID,
(unsigned long long)nid, gc->cursor, gc->candidate_count);
conn_mgr_open_invite(gc->group->instance, gc->group->group_id, NULL, nid, tgc_callback, gc, NULL);
if (conn_mgr_open_invite(gc->group->instance, gc->group->group_id, NULL, nid, tgc_callback, gc, NULL) < 0) continue;
return;
}
DEBUG_INFO(DEBUG_CATEGORY_BGP, "%s: Phase3 exhausted — no connections, ch=%s", TGC_ID, gc->group->channel_id);
+5 -5
View File
@@ -116,8 +116,8 @@ int topo_node_sqlite_node_put(sqlite3* db, struct TOPO_GROUPS* groups, struct TO
sqlite3_stmt* stmt = NULL;
if (sqlite3_prepare_v2(db,
"INSERT INTO nodes(node_id, name, x25519_pubkey, ed25519_pubkey, last_seen_at)"
" VALUES(?,?,?,?,?)"
"INSERT INTO nodes(node_id, name, x25519_pubkey, ed25519_pubkey, last_seen_at, online)"
" VALUES(?,?,?,?,?,1)"
" ON CONFLICT(node_id) DO UPDATE SET"
" 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"
@@ -273,8 +273,8 @@ int topo_node_sqlite_node_update_verified(sqlite3* db, uint64_t node_id,
if (!db || !x25519 || !ed25519) return -1;
sqlite3_stmt* stmt = NULL;
if (sqlite3_prepare_v2(db,
"INSERT INTO nodes(node_id, name, x25519_pubkey, ed25519_pubkey, created_at, update_ts)"
" VALUES(?,?,?,?,?,?)"
"INSERT INTO nodes(node_id, name, x25519_pubkey, ed25519_pubkey, created_at, update_ts, online)"
" VALUES(?,?,?,?,?,?,1)"
" ON CONFLICT(node_id) DO UPDATE SET"
" name=CASE WHEN excluded.name IS NOT NULL AND excluded.name != ''"
" THEN excluded.name ELSE nodes.name END,"
@@ -409,7 +409,7 @@ int topo_node_sqlite_channel_peers_all(sqlite3* db, const char* channel_id,
sqlite3_finalize(ast);
uint8_t flags = (join_sig && join_ts) ? PEERS_FLAG_HAS_JOIN : 0;
size_t peer_needed = 8 + 32 + 32 + 1 + (flags & PEERS_FLAG_HAS_JOIN ? 64+8 : 0) + 64 + 8 + 1 + (size_t)nl + 1 + (size_t)atl + 64 + 1;
size_t peer_needed = 8 + 32 + 32 + 1 + (flags & PEERS_FLAG_HAS_JOIN ? 64+8 : 0) + 64 + 8 + 1 + (size_t)nl + 1 + 1 + (size_t)atl + 64;
size_t addr_sz = off - addr_start;
if (addr_cnt > 0) {
if (off + peer_needed > buf_sz) { sqlite3_finalize(stmt); return -2; }