Browse Source

member_sync/topo_sqlite: add proto byte to address serialization

- _compute_bucket_hash: SELECT protocol, write proto byte
- _member_get_items: SELECT protocol, write proto byte
- _member_apply_items: consume proto byte in skip calculation
- topo_node_sqlite_channel_peers_all: SELECT protocol, write proto byte
All 5 writers and 5 readers now consistently include protocol byte.
topo_upd
evgeny 2 months ago
parent
commit
5d23f461a7
  1. 27
      src/chat/member_sync.c
  2. 10
      src/routing_layer/topo_node_sqlite.c

27
src/chat/member_sync.c

@ -130,19 +130,22 @@ static int _member_update_bucket_hash(void* ctx, const char* ns, uint8_t level,
uint8_t addrs[2048]; int addr_off = 0; int addr_count = 0;
sqlite3_stmt* as = NULL;
sqlite3_prepare_v2(db,
"SELECT family, socket_id, address, port FROM node_addresses WHERE node_id=? AND addr_type=0"
"SELECT family, socket_id, protocol, address, port FROM node_addresses WHERE node_id=? AND addr_type=0"
" ORDER BY family, address, port", -1, &as, NULL);
if (as) {
sqlite3_bind_int64(as, 1, (sqlite3_int64)nid);
while (sqlite3_step(as) == SQLITE_ROW && addr_off < (int)sizeof(addrs) - 8) {
while (sqlite3_step(as) == SQLITE_ROW && addr_off < (int)sizeof(addrs) - 9) {
int fam = sqlite3_column_int(as, 0);
int sid = sqlite3_column_int(as, 1);
int proto = sqlite3_column_int(as, 2);
int ip_sz = fam == 4 ? 4 : 16;
if (addr_off + 3 + ip_sz + 2 > (int)sizeof(addrs)) break;
addrs[addr_off++] = (uint8_t)fam;
addrs[addr_off++] = (uint8_t)sid;
int ip_sz = fam == 4 ? 4 : 16;
memcpy(addrs + addr_off, sqlite3_column_blob(as, 2), (size_t)ip_sz);
addrs[addr_off++] = (uint8_t)proto;
memcpy(addrs + addr_off, sqlite3_column_blob(as, 3), (size_t)ip_sz);
addr_off += ip_sz;
uint16_t p = (uint16_t)sqlite3_column_int(as, 3);
uint16_t p = (uint16_t)sqlite3_column_int(as, 4);
addrs[addr_off++] = (uint8_t)(p >> 8);
addrs[addr_off++] = (uint8_t)(p & 0xFF);
addr_count++;
@ -202,20 +205,23 @@ static int _member_get_items(void* ctx, const char* ns, uint8_t level,
sqlite3_stmt* as = NULL;
sqlite3_prepare_v2(db,
"SELECT family, socket_id, address, port FROM node_addresses WHERE node_id=? AND addr_type=0"
"SELECT family, socket_id, protocol, address, port FROM node_addresses WHERE node_id=? AND addr_type=0"
" ORDER BY family, address, port", -1, &as, NULL);
uint8_t addrs[2048]; int addr_off = 0; int addr_count = 0;
if (as) {
sqlite3_bind_int64(as, 1, (sqlite3_int64)nid);
while (sqlite3_step(as) == SQLITE_ROW && addr_off < (int)sizeof(addrs) - 8) {
while (sqlite3_step(as) == SQLITE_ROW && addr_off < (int)sizeof(addrs) - 9) {
int fam = sqlite3_column_int(as, 0);
int sid = sqlite3_column_int(as, 1);
int proto = sqlite3_column_int(as, 2);
int ip_sz = fam == 4 ? 4 : 16;
if (addr_off + 3 + ip_sz + 2 > (int)sizeof(addrs)) break;
addrs[addr_off++] = (uint8_t)fam;
addrs[addr_off++] = (uint8_t)sid;
int ip_sz = fam == 4 ? 4 : 16;
memcpy(addrs + addr_off, sqlite3_column_blob(as, 2), (size_t)ip_sz);
addrs[addr_off++] = (uint8_t)proto;
memcpy(addrs + addr_off, sqlite3_column_blob(as, 3), (size_t)ip_sz);
addr_off += ip_sz;
uint16_t p = (uint16_t)sqlite3_column_int(as, 3);
uint16_t p = (uint16_t)sqlite3_column_int(as, 4);
addrs[addr_off++] = (uint8_t)(p >> 8);
addrs[addr_off++] = (uint8_t)(p & 0xFF);
addr_count++;
@ -325,6 +331,7 @@ static int _member_apply_items(void* ctx, const char* ns, uint64_t from_peer,
for (int a = 0; a < (int)ac && mrem >= (size_t)(2 + consumed); a++) {
uint8_t fam = mp[consumed]; consumed++;
uint8_t sid = mp[consumed]; consumed++;
uint8_t proto = mp[consumed]; consumed++;
int sz = fam == 4 ? 4 : 16;
consumed += sz + 2;
}

10
src/routing_layer/topo_node_sqlite.c

@ -382,7 +382,7 @@ int topo_node_sqlite_channel_peers_all(sqlite3* db, const char* channel_id,
sqlite3_stmt* ast = NULL;
snprintf(sql, sizeof(sql),
"SELECT family, socket_id, address, port FROM node_addresses"
"SELECT family, socket_id, protocol, address, port FROM node_addresses"
" WHERE node_id=? AND addr_type=0 LIMIT 16");
if (sqlite3_prepare_v2(db, sql, -1, &ast, NULL) != SQLITE_OK) continue;
sqlite3_bind_int64(ast, 1, (sqlite3_int64)node_id);
@ -391,14 +391,16 @@ int topo_node_sqlite_channel_peers_all(sqlite3* db, const char* channel_id,
while (sqlite3_step(ast) == SQLITE_ROW && addr_cnt < 255) {
int family = sqlite3_column_int(ast, 0);
int sock_id = sqlite3_column_int(ast, 1);
int proto = sqlite3_column_int(ast, 2);
int ip_len = (family == 4) ? 4 : 16;
const uint8_t* ip = (const uint8_t*)sqlite3_column_blob(ast, 2);
uint16_t port = (uint16_t)sqlite3_column_int(ast, 3);
const uint8_t* ip = (const uint8_t*)sqlite3_column_blob(ast, 3);
uint16_t port = (uint16_t)sqlite3_column_int(ast, 4);
if (!ip) continue;
if (off + 1 + 1 + ip_len + 2 > buf_sz) { sqlite3_finalize(ast); sqlite3_finalize(stmt); return -2; }
if (off + 1 + 1 + 1 + ip_len + 2 > buf_sz) { sqlite3_finalize(ast); sqlite3_finalize(stmt); return -2; }
if (addr_cnt == 0) addr_start = off;
buf[off++] = (uint8_t)family;
buf[off++] = (uint8_t)sock_id;
buf[off++] = (uint8_t)proto;
memcpy(buf + off, ip, (size_t)ip_len); off += (size_t)ip_len;
buf[off++] = (uint8_t)((port >> 8) & 0xFF);
buf[off++] = (uint8_t)(port & 0xFF);

Loading…
Cancel
Save