add IP Reflection to DHT for zero-infrastructure NAT traversal
bundled STUN. every DHT response now includes observed_addr field so nodes discover their public IP without external STUN servers
This commit is contained in:
@@ -769,20 +769,21 @@ pub const MessageSerializer = struct {
|
||||
// DHT messages - basic size calculations
|
||||
.dht_find_node => 8 + 32 + 32, // request_id + target_id + sender_id
|
||||
.dht_find_node_response => |resp| blk: {
|
||||
// 8 (request_id) + array of NodeInfo (node_id + address + len)
|
||||
// 8 (request_id) + array of NodeInfo (node_id + address + len) + observed_addr (18 + 1)
|
||||
const node_size = 32 + 46 + 1; // NodeInfo size
|
||||
const nodes_size = resp.closest_nodes.len * node_size;
|
||||
break :blk safeAdd(8, nodes_size) orelse std.math.maxInt(usize);
|
||||
const base = safeAdd(8, nodes_size) orelse std.math.maxInt(usize);
|
||||
break :blk safeAdd(base, 19) orelse std.math.maxInt(usize); // + observed_addr
|
||||
},
|
||||
.dht_store => |store| blk: {
|
||||
// 8 + 32 + value.len + 4 + 32
|
||||
const base = safeAdd(8 + 32, store.value.len) orelse std.math.maxInt(usize);
|
||||
break :blk safeAdd(base, 4 + 32) orelse std.math.maxInt(usize);
|
||||
},
|
||||
.dht_store_response => 8 + 1, // request_id + success
|
||||
.dht_store_response => 8 + 1 + 19, // request_id + success + observed_addr (18 + 1)
|
||||
.dht_find_value => 8 + 32 + 32, // request_id + key + sender_id
|
||||
.dht_find_value_response => |resp| blk: {
|
||||
const base = 8 + 1; // request_id + found
|
||||
const base = 8 + 1 + 19; // request_id + found + observed_addr
|
||||
const value_size = if (resp.value) |v| v.len else 0;
|
||||
const with_value = safeAdd(base, value_size) orelse std.math.maxInt(usize);
|
||||
const node_size = 32 + 46 + 1;
|
||||
@@ -1217,6 +1218,11 @@ pub const MessageSerializer = struct {
|
||||
buffer[offset] = node.address_len;
|
||||
offset += 1;
|
||||
}
|
||||
// IP Reflection: Serialize observed_addr (bundled STUN)
|
||||
try checkBounds(buffer, offset, 19);
|
||||
@memcpy(buffer[offset..offset + 18], &resp.observed_addr);
|
||||
offset += 18;
|
||||
buffer[offset] = resp.observed_addr_len;
|
||||
},
|
||||
.dht_store => |store| {
|
||||
// SECURITY: Safe bounds check with overflow protection
|
||||
@@ -1244,10 +1250,15 @@ pub const MessageSerializer = struct {
|
||||
},
|
||||
.dht_store_response => |resp| {
|
||||
// SECURITY: Safe bounds check with overflow protection
|
||||
try checkBounds(buffer, offset, 9);
|
||||
try checkBounds(buffer, offset, 28); // 8 + 1 + 19 (observed_addr)
|
||||
@memcpy(buffer[offset..offset + 8], std.mem.asBytes(&resp.request_id));
|
||||
offset += 8;
|
||||
buffer[offset] = if (resp.success) 1 else 0;
|
||||
offset += 1;
|
||||
// IP Reflection: Serialize observed_addr (bundled STUN)
|
||||
@memcpy(buffer[offset..offset + 18], &resp.observed_addr);
|
||||
offset += 18;
|
||||
buffer[offset] = resp.observed_addr_len;
|
||||
},
|
||||
.dht_find_value => |find| {
|
||||
// SECURITY: Safe bounds check with overflow protection
|
||||
@@ -1297,6 +1308,11 @@ pub const MessageSerializer = struct {
|
||||
buffer[offset] = node.address_len;
|
||||
offset += 1;
|
||||
}
|
||||
// IP Reflection: Serialize observed_addr (bundled STUN)
|
||||
try checkBounds(buffer, offset, 19);
|
||||
@memcpy(buffer[offset..offset + 18], &resp.observed_addr);
|
||||
offset += 18;
|
||||
buffer[offset] = resp.observed_addr_len;
|
||||
},
|
||||
.app_data => |app| {
|
||||
// SECURITY: Safe bounds check with overflow protection
|
||||
|
||||
Reference in New Issue
Block a user