create Mutual Witness Protocol specs and implementation. #1
177
src/p2p/dht.zig
177
src/p2p/dht.zig
@@ -165,6 +165,9 @@ pub const DhtMsgType = enum(u8) {
|
|||||||
find_node = 0x01,
|
find_node = 0x01,
|
||||||
store = 0x02,
|
store = 0x02,
|
||||||
find_value = 0x03,
|
find_value = 0x03,
|
||||||
|
find_node_response = 0x11,
|
||||||
|
store_response = 0x12,
|
||||||
|
find_value_response = 0x13,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Serialize a DHT message to simple binary format
|
/// Serialize a DHT message to simple binary format
|
||||||
@@ -216,6 +219,83 @@ fn serializeDhtMessage(msg: protocol.message.Message, buffer: []u8) ?usize {
|
|||||||
offset += 32;
|
offset += 32;
|
||||||
return offset;
|
return offset;
|
||||||
},
|
},
|
||||||
|
.dht_find_node_response => |m| {
|
||||||
|
// Format: [type][request_id][node_count][nodes...][observed_addr][observed_addr_len]
|
||||||
|
const node_size = 32 + 46 + 1; // node_id + address + address_len
|
||||||
|
const required = 1 + 8 + 4 + (m.closest_nodes.len * node_size) + 18 + 1;
|
||||||
|
if (buffer.len < required) return null;
|
||||||
|
buffer[offset] = @intFromEnum(DhtMsgType.find_node_response);
|
||||||
|
offset += 1;
|
||||||
|
std.mem.writeInt(u64, buffer[offset..][0..8], m.request_id, .big);
|
||||||
|
offset += 8;
|
||||||
|
std.mem.writeInt(u32, buffer[offset..][0..4], @intCast(m.closest_nodes.len), .big);
|
||||||
|
offset += 4;
|
||||||
|
for (m.closest_nodes) |node| {
|
||||||
|
@memcpy(buffer[offset..][0..32], &node.node_id);
|
||||||
|
offset += 32;
|
||||||
|
@memcpy(buffer[offset..][0..46], &node.address);
|
||||||
|
offset += 46;
|
||||||
|
buffer[offset] = node.address_len;
|
||||||
|
offset += 1;
|
||||||
|
}
|
||||||
|
// IP Reflection: observed_addr
|
||||||
|
@memcpy(buffer[offset..][0..18], &m.observed_addr);
|
||||||
|
offset += 18;
|
||||||
|
buffer[offset] = m.observed_addr_len;
|
||||||
|
offset += 1;
|
||||||
|
return offset;
|
||||||
|
},
|
||||||
|
.dht_store_response => |m| {
|
||||||
|
// Format: [type][request_id][success][observed_addr][observed_addr_len]
|
||||||
|
if (buffer.len < 1 + 8 + 1 + 18 + 1) return null;
|
||||||
|
buffer[offset] = @intFromEnum(DhtMsgType.store_response);
|
||||||
|
offset += 1;
|
||||||
|
std.mem.writeInt(u64, buffer[offset..][0..8], m.request_id, .big);
|
||||||
|
offset += 8;
|
||||||
|
buffer[offset] = if (m.success) 1 else 0;
|
||||||
|
offset += 1;
|
||||||
|
// IP Reflection: observed_addr
|
||||||
|
@memcpy(buffer[offset..][0..18], &m.observed_addr);
|
||||||
|
offset += 18;
|
||||||
|
buffer[offset] = m.observed_addr_len;
|
||||||
|
offset += 1;
|
||||||
|
return offset;
|
||||||
|
},
|
||||||
|
.dht_find_value_response => |m| {
|
||||||
|
// Format: [type][request_id][found][value_len][value][node_count][nodes...][observed_addr][observed_addr_len]
|
||||||
|
const node_size = 32 + 46 + 1;
|
||||||
|
const value_len = if (m.value) |v| v.len else 0;
|
||||||
|
const required = 1 + 8 + 1 + 4 + value_len + 4 + (m.closest_nodes.len * node_size) + 18 + 1;
|
||||||
|
if (buffer.len < required) return null;
|
||||||
|
buffer[offset] = @intFromEnum(DhtMsgType.find_value_response);
|
||||||
|
offset += 1;
|
||||||
|
std.mem.writeInt(u64, buffer[offset..][0..8], m.request_id, .big);
|
||||||
|
offset += 8;
|
||||||
|
buffer[offset] = if (m.found) 1 else 0;
|
||||||
|
offset += 1;
|
||||||
|
std.mem.writeInt(u32, buffer[offset..][0..4], @intCast(value_len), .big);
|
||||||
|
offset += 4;
|
||||||
|
if (m.value) |v| {
|
||||||
|
@memcpy(buffer[offset..][0..v.len], v);
|
||||||
|
offset += v.len;
|
||||||
|
}
|
||||||
|
std.mem.writeInt(u32, buffer[offset..][0..4], @intCast(m.closest_nodes.len), .big);
|
||||||
|
offset += 4;
|
||||||
|
for (m.closest_nodes) |node| {
|
||||||
|
@memcpy(buffer[offset..][0..32], &node.node_id);
|
||||||
|
offset += 32;
|
||||||
|
@memcpy(buffer[offset..][0..46], &node.address);
|
||||||
|
offset += 46;
|
||||||
|
buffer[offset] = node.address_len;
|
||||||
|
offset += 1;
|
||||||
|
}
|
||||||
|
// IP Reflection: observed_addr
|
||||||
|
@memcpy(buffer[offset..][0..18], &m.observed_addr);
|
||||||
|
offset += 18;
|
||||||
|
buffer[offset] = m.observed_addr_len;
|
||||||
|
offset += 1;
|
||||||
|
return offset;
|
||||||
|
},
|
||||||
else => return null,
|
else => return null,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -284,6 +364,103 @@ pub fn deserializeDhtMessage(data: []const u8, allocator: std.mem.Allocator) ?pr
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
.find_node_response => {
|
||||||
|
// Format: [type][request_id][node_count][nodes...][observed_addr][observed_addr_len]
|
||||||
|
if (data.len < 1 + 8 + 4 + 18 + 1) return null;
|
||||||
|
const request_id = std.mem.readInt(u64, data[offset..][0..8], .big);
|
||||||
|
offset += 8;
|
||||||
|
const node_count = std.mem.readInt(u32, data[offset..][0..4], .big);
|
||||||
|
offset += 4;
|
||||||
|
const node_size = 32 + 46 + 1;
|
||||||
|
if (data.len < offset + (node_count * node_size) + 19) return null;
|
||||||
|
// Allocate nodes array
|
||||||
|
const nodes = allocator.alloc(protocol.message.DhtFindNodeResponse.NodeInfo, node_count) catch return null;
|
||||||
|
for (0..node_count) |i| {
|
||||||
|
nodes[i] = .{
|
||||||
|
.node_id = data[offset..][0..32].*,
|
||||||
|
.address = data[offset + 32 ..][0..46].*,
|
||||||
|
.address_len = data[offset + 32 + 46],
|
||||||
|
};
|
||||||
|
offset += node_size;
|
||||||
|
}
|
||||||
|
// IP Reflection: observed_addr
|
||||||
|
const observed_addr = data[offset..][0..18].*;
|
||||||
|
offset += 18;
|
||||||
|
const observed_addr_len = data[offset];
|
||||||
|
return protocol.message.Message{
|
||||||
|
.dht_find_node_response = .{
|
||||||
|
.request_id = request_id,
|
||||||
|
.closest_nodes = nodes,
|
||||||
|
.observed_addr = observed_addr,
|
||||||
|
.observed_addr_len = observed_addr_len,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
},
|
||||||
|
.store_response => {
|
||||||
|
// Format: [type][request_id][success][observed_addr][observed_addr_len]
|
||||||
|
if (data.len < 1 + 8 + 1 + 18 + 1) return null;
|
||||||
|
const request_id = std.mem.readInt(u64, data[offset..][0..8], .big);
|
||||||
|
offset += 8;
|
||||||
|
const success = data[offset] != 0;
|
||||||
|
offset += 1;
|
||||||
|
// IP Reflection: observed_addr
|
||||||
|
const observed_addr = data[offset..][0..18].*;
|
||||||
|
offset += 18;
|
||||||
|
const observed_addr_len = data[offset];
|
||||||
|
return protocol.message.Message{
|
||||||
|
.dht_store_response = .{
|
||||||
|
.request_id = request_id,
|
||||||
|
.success = success,
|
||||||
|
.observed_addr = observed_addr,
|
||||||
|
.observed_addr_len = observed_addr_len,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
},
|
||||||
|
.find_value_response => {
|
||||||
|
// Format: [type][request_id][found][value_len][value][node_count][nodes...][observed_addr][observed_addr_len]
|
||||||
|
if (data.len < 1 + 8 + 1 + 4 + 4 + 18 + 1) return null;
|
||||||
|
const request_id = std.mem.readInt(u64, data[offset..][0..8], .big);
|
||||||
|
offset += 8;
|
||||||
|
const found = data[offset] != 0;
|
||||||
|
offset += 1;
|
||||||
|
const value_len = std.mem.readInt(u32, data[offset..][0..4], .big);
|
||||||
|
offset += 4;
|
||||||
|
if (data.len < offset + value_len + 4 + 19) return null;
|
||||||
|
// Read value (may be empty)
|
||||||
|
const value: ?[]const u8 = if (value_len > 0) blk: {
|
||||||
|
const v = allocator.dupe(u8, data[offset..][0..value_len]) catch return null;
|
||||||
|
break :blk v;
|
||||||
|
} else null;
|
||||||
|
offset += value_len;
|
||||||
|
const node_count = std.mem.readInt(u32, data[offset..][0..4], .big);
|
||||||
|
offset += 4;
|
||||||
|
const node_size = 32 + 46 + 1;
|
||||||
|
if (data.len < offset + (node_count * node_size) + 19) return null;
|
||||||
|
// Allocate nodes array
|
||||||
|
const nodes = allocator.alloc(protocol.message.DhtFindNodeResponse.NodeInfo, node_count) catch return null;
|
||||||
|
for (0..node_count) |i| {
|
||||||
|
nodes[i] = .{
|
||||||
|
.node_id = data[offset..][0..32].*,
|
||||||
|
.address = data[offset + 32 ..][0..46].*,
|
||||||
|
.address_len = data[offset + 32 + 46],
|
||||||
|
};
|
||||||
|
offset += node_size;
|
||||||
|
}
|
||||||
|
// IP Reflection: observed_addr
|
||||||
|
const observed_addr = data[offset..][0..18].*;
|
||||||
|
offset += 18;
|
||||||
|
const observed_addr_len = data[offset];
|
||||||
|
return protocol.message.Message{
|
||||||
|
.dht_find_value_response = .{
|
||||||
|
.request_id = request_id,
|
||||||
|
.found = found,
|
||||||
|
.value = value,
|
||||||
|
.closest_nodes = nodes,
|
||||||
|
.observed_addr = observed_addr,
|
||||||
|
.observed_addr_len = observed_addr_len,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -44,6 +44,7 @@ const p2p_module = @import("p2p.zig");
|
|||||||
const P2P = p2p_module.P2P;
|
const P2P = p2p_module.P2P;
|
||||||
const handleDataResponseCallback = p2p_module.handleDataResponseCallback;
|
const handleDataResponseCallback = p2p_module.handleDataResponseCallback;
|
||||||
const registerNodeIdCallback = p2p_module.registerNodeIdCallback;
|
const registerNodeIdCallback = p2p_module.registerNodeIdCallback;
|
||||||
|
const sendDhtMessageCallback = p2p_module.sendDhtMessageCallback;
|
||||||
const P2PEvent = @import("events.zig").P2PEvent;
|
const P2PEvent = @import("events.zig").P2PEvent;
|
||||||
const P2PConfig = @import("config.zig").P2PConfig;
|
const P2PConfig = @import("config.zig").P2PConfig;
|
||||||
|
|
||||||
@@ -221,6 +222,11 @@ pub fn startImpl(p2p: *P2P) Error!void {
|
|||||||
.register_fn = registerNodeIdCallback,
|
.register_fn = registerNodeIdCallback,
|
||||||
.context = p2p,
|
.context = p2p,
|
||||||
});
|
});
|
||||||
|
// Wire the DHT message sender callback for simple binary format responses
|
||||||
|
ctx.setDhtMessageSender(.{
|
||||||
|
.send_fn = sendDhtMessageCallback,
|
||||||
|
.context = p2p,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -131,6 +131,19 @@ pub fn registerNodeIdCallback(node_id: [32]u8, peer_id: [32]u8, context: ?*anyop
|
|||||||
std.log.info("Registered DHT node ID mapping: {x} -> connection peer {x}", .{ node_id[0..8].*, peer_id[0..8].* });
|
std.log.info("Registered DHT node ID mapping: {x} -> connection peer {x}", .{ node_id[0..8].*, peer_id[0..8].* });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Callback to send DHT messages using simple binary format.
|
||||||
|
/// Called by DHT handlers to send responses in the same format as requests.
|
||||||
|
pub fn sendDhtMessageCallback(peer_id: [32]u8, msg: protocol.message.Message, context: ?*anyopaque) bool {
|
||||||
|
const p2p: *P2P = @ptrCast(@alignCast(context orelse {
|
||||||
|
std.log.err("sendDhtMessageCallback: null context", .{});
|
||||||
|
return false;
|
||||||
|
}));
|
||||||
|
|
||||||
|
// Use the dht module's sendDhtMessageImpl which uses simple binary format
|
||||||
|
const dht_module = @import("dht.zig");
|
||||||
|
return dht_module.sendDhtMessageImpl(p2p, peer_id, msg);
|
||||||
|
}
|
||||||
|
|
||||||
/// Callback to handle data responses during file downloads.
|
/// Callback to handle data responses during file downloads.
|
||||||
///
|
///
|
||||||
/// This function processes incoming DataResponse messages and writes
|
/// This function processes incoming DataResponse messages and writes
|
||||||
|
|||||||
@@ -43,6 +43,22 @@ pub const NodeIdRegistrationCallback = struct {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/// DHT message sender callback - sends DHT messages using simple binary format
|
||||||
|
/// Used for DHT request/response messages to maintain consistent serialization
|
||||||
|
pub const DhtMessageSender = struct {
|
||||||
|
/// Send a DHT message to a peer
|
||||||
|
/// @param peer_id: The peer's DHT node ID (= their connection peer ID for DHT)
|
||||||
|
/// @param msg: The message to send
|
||||||
|
/// @param context: User context (typically P2P instance)
|
||||||
|
/// @return: true if sent successfully, false otherwise
|
||||||
|
send_fn: *const fn (peer_id: [32]u8, msg: message.Message, context: ?*anyopaque) bool,
|
||||||
|
context: ?*anyopaque,
|
||||||
|
|
||||||
|
pub fn send(self: DhtMessageSender, peer_id: [32]u8, msg: message.Message) bool {
|
||||||
|
return self.send_fn(peer_id, msg, self.context);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
/// Handler limits for memory exhaustion protection
|
/// Handler limits for memory exhaustion protection
|
||||||
pub const HandlerLimits = struct {
|
pub const HandlerLimits = struct {
|
||||||
pub const MAX_PENDING_MESSAGES: usize = 10000; // Max messages in async queue
|
pub const MAX_PENDING_MESSAGES: usize = 10000; // Max messages in async queue
|
||||||
@@ -150,6 +166,9 @@ pub const HandlerContext = struct {
|
|||||||
// Maps remote peer's identity public key (node ID) to connection peer ID
|
// Maps remote peer's identity public key (node ID) to connection peer ID
|
||||||
node_id_registration_callback: ?NodeIdRegistrationCallback = null,
|
node_id_registration_callback: ?NodeIdRegistrationCallback = null,
|
||||||
|
|
||||||
|
// DHT message sender callback - for sending DHT messages in simple binary format
|
||||||
|
dht_message_sender: ?DhtMessageSender = null,
|
||||||
|
|
||||||
pub fn init(allocator: std.mem.Allocator, metrics: *HandlerMetrics) HandlerContext {
|
pub fn init(allocator: std.mem.Allocator, metrics: *HandlerMetrics) HandlerContext {
|
||||||
return .{
|
return .{
|
||||||
.allocator = allocator,
|
.allocator = allocator,
|
||||||
@@ -240,6 +259,28 @@ pub const HandlerContext = struct {
|
|||||||
self.node_id_registration_callback = callback;
|
self.node_id_registration_callback = callback;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Set the DHT message sender callback
|
||||||
|
/// This callback sends DHT messages using simple binary format for consistent serialization
|
||||||
|
pub fn setDhtMessageSender(self: *HandlerContext, sender: DhtMessageSender) void {
|
||||||
|
self.config_mutex.lock();
|
||||||
|
defer self.config_mutex.unlock();
|
||||||
|
self.dht_message_sender = sender;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Send a DHT message to a peer using simple binary format
|
||||||
|
/// Returns true if sent successfully, false otherwise
|
||||||
|
pub fn sendDhtMessage(self: *HandlerContext, peer_id: [32]u8, msg: message.Message) bool {
|
||||||
|
self.config_mutex.lock();
|
||||||
|
const sender = self.dht_message_sender;
|
||||||
|
self.config_mutex.unlock();
|
||||||
|
|
||||||
|
if (sender) |s| {
|
||||||
|
return s.send(peer_id, msg);
|
||||||
|
}
|
||||||
|
std.log.warn("DHT: No DHT message sender configured", .{});
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/// Register a node ID to peer ID mapping (if callback is set)
|
/// Register a node ID to peer ID mapping (if callback is set)
|
||||||
/// Called internally when a peer's identity is verified during key exchange
|
/// Called internally when a peer's identity is verified during key exchange
|
||||||
pub fn registerNodeId(self: *HandlerContext, node_id: [32]u8, peer_id: [32]u8) void {
|
pub fn registerNodeId(self: *HandlerContext, node_id: [32]u8, peer_id: [32]u8) void {
|
||||||
@@ -312,6 +353,43 @@ pub const HandlerContext = struct {
|
|||||||
self.source_peer_id = null;
|
self.source_peer_id = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Get the observed address of the source peer for IP reflection (bundled STUN)
|
||||||
|
/// Returns the peer's network address in wire format: IPv4 = 6 bytes, IPv6 = 18 bytes
|
||||||
|
pub fn getSourceObservedAddr(self: *const HandlerContext) struct { addr: [18]u8, len: u8 } {
|
||||||
|
const network = @import("network");
|
||||||
|
const source_id = self.source_peer_id orelse return .{ .addr = [_]u8{0} ** 18, .len = 0 };
|
||||||
|
|
||||||
|
const pm = self.getPeerManager() catch return .{ .addr = [_]u8{0} ** 18, .len = 0 };
|
||||||
|
const peer_manager = pm orelse return .{ .addr = [_]u8{0} ** 18, .len = 0 };
|
||||||
|
|
||||||
|
const peer_id = network.PeerId{ .bytes = source_id };
|
||||||
|
const peer_info = peer_manager.getPeerById(peer_id) orelse return .{ .addr = [_]u8{0} ** 18, .len = 0 };
|
||||||
|
|
||||||
|
// Serialize the address: type (1) + ip (4 or 16) + port (2)
|
||||||
|
var addr_buf: [18]u8 = [_]u8{0} ** 18;
|
||||||
|
var len: u8 = 0;
|
||||||
|
|
||||||
|
switch (peer_info.address.family) {
|
||||||
|
.ipv4 => {
|
||||||
|
// IPv4: 4 bytes IP + 2 bytes port = 6 bytes
|
||||||
|
const ip_bytes = peer_info.address.data.ipv4.bytes;
|
||||||
|
@memcpy(addr_buf[0..4], &ip_bytes);
|
||||||
|
std.mem.writeInt(u16, addr_buf[4..6], peer_info.address.port, .big);
|
||||||
|
len = 6;
|
||||||
|
},
|
||||||
|
.ipv6 => {
|
||||||
|
// IPv6: 16 bytes IP + 2 bytes port = 18 bytes
|
||||||
|
const ip_bytes = peer_info.address.data.ipv6.bytes;
|
||||||
|
@memcpy(addr_buf[0..16], &ip_bytes);
|
||||||
|
std.mem.writeInt(u16, addr_buf[16..18], peer_info.address.port, .big);
|
||||||
|
len = 18;
|
||||||
|
},
|
||||||
|
else => {},
|
||||||
|
}
|
||||||
|
|
||||||
|
return .{ .addr = addr_buf, .len = len };
|
||||||
|
}
|
||||||
|
|
||||||
/// Send a response message to a peer
|
/// Send a response message to a peer
|
||||||
/// Serializes the message and sends it via the network sender
|
/// Serializes the message and sends it via the network sender
|
||||||
/// SEC-M1 FIX: Uses dynamic buffer allocation instead of fixed 8KB
|
/// SEC-M1 FIX: Uses dynamic buffer allocation instead of fixed 8KB
|
||||||
|
|||||||
@@ -939,17 +939,25 @@ pub fn handleDhtFindNode(
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send response back
|
// Get observed address for IP reflection (bundled STUN)
|
||||||
|
const observed = context.getSourceObservedAddr();
|
||||||
|
|
||||||
|
// Send response back with observed address using simple binary format
|
||||||
const response_msg = message.Message{
|
const response_msg = message.Message{
|
||||||
.dht_find_node_response = message.DhtFindNodeResponse{
|
.dht_find_node_response = message.DhtFindNodeResponse{
|
||||||
.request_id = response.request_id,
|
.request_id = response.request_id,
|
||||||
.closest_nodes = converted_nodes,
|
.closest_nodes = converted_nodes,
|
||||||
|
.observed_addr = observed.addr,
|
||||||
|
.observed_addr_len = observed.len,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
try context.sendResponse(source_id, response_msg);
|
if (!context.sendDhtMessage(source_id, response_msg)) {
|
||||||
|
context.metrics.recordMessageFailed();
|
||||||
|
return error.SendFailed;
|
||||||
|
}
|
||||||
context.metrics.recordMessageProcessed(0.0);
|
context.metrics.recordMessageProcessed(0.0);
|
||||||
std.log.debug("DHT: Sent FIND_NODE response with {} nodes", .{response.closest_nodes.len});
|
std.log.debug("DHT: Sent FIND_NODE response with {} nodes, observed_addr_len={}", .{ response.closest_nodes.len, observed.len });
|
||||||
} else {
|
} else {
|
||||||
std.log.err("DHT: FIND_NODE received but no DHT coordinator configured", .{});
|
std.log.err("DHT: FIND_NODE received but no DHT coordinator configured", .{});
|
||||||
context.metrics.recordMessageFailed();
|
context.metrics.recordMessageFailed();
|
||||||
@@ -1033,33 +1041,43 @@ pub fn handleDhtStore(
|
|||||||
.sender_id = msg.sender_id,
|
.sender_id = msg.sender_id,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Get observed address for IP reflection (bundled STUN)
|
||||||
|
const observed = context.getSourceObservedAddr();
|
||||||
|
|
||||||
// Handle the store request
|
// Handle the store request
|
||||||
const response = dht.handleStore(dht_msg) catch |err| {
|
const response = dht.handleStore(dht_msg) catch |err| {
|
||||||
std.log.err("DHT: Failed to handle STORE: {}", .{err});
|
std.log.err("DHT: Failed to handle STORE: {}", .{err});
|
||||||
|
|
||||||
// Send failure response
|
// Send failure response with observed address using simple binary format
|
||||||
const fail_response = message.Message{
|
const fail_response = message.Message{
|
||||||
.dht_store_response = message.DhtStoreResponse{
|
.dht_store_response = message.DhtStoreResponse{
|
||||||
.request_id = msg.request_id,
|
.request_id = msg.request_id,
|
||||||
.success = false,
|
.success = false,
|
||||||
|
.observed_addr = observed.addr,
|
||||||
|
.observed_addr_len = observed.len,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
try context.sendResponse(source_id, fail_response);
|
_ = context.sendDhtMessage(source_id, fail_response);
|
||||||
context.metrics.recordMessageFailed();
|
context.metrics.recordMessageFailed();
|
||||||
return err;
|
return err;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Send success response
|
// Send success response with observed address using simple binary format
|
||||||
const response_msg = message.Message{
|
const response_msg = message.Message{
|
||||||
.dht_store_response = message.DhtStoreResponse{
|
.dht_store_response = message.DhtStoreResponse{
|
||||||
.request_id = response.request_id,
|
.request_id = response.request_id,
|
||||||
.success = response.success,
|
.success = response.success,
|
||||||
|
.observed_addr = observed.addr,
|
||||||
|
.observed_addr_len = observed.len,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
try context.sendResponse(source_id, response_msg);
|
if (!context.sendDhtMessage(source_id, response_msg)) {
|
||||||
|
context.metrics.recordMessageFailed();
|
||||||
|
return error.SendFailed;
|
||||||
|
}
|
||||||
context.metrics.recordMessageProcessed(0.0);
|
context.metrics.recordMessageProcessed(0.0);
|
||||||
std.log.debug("DHT: Stored value for key, success={}", .{response.success});
|
std.log.debug("DHT: Stored value for key, success={}, observed_addr_len={}", .{ response.success, observed.len });
|
||||||
} else {
|
} else {
|
||||||
context.metrics.recordMessageFailed();
|
context.metrics.recordMessageFailed();
|
||||||
return error.InvalidState;
|
return error.InvalidState;
|
||||||
@@ -1137,18 +1155,27 @@ pub fn handleDhtFindValue(
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send response (either value or closest nodes)
|
// Get observed address for IP reflection (bundled STUN)
|
||||||
|
const observed = context.getSourceObservedAddr();
|
||||||
|
|
||||||
|
// Send response (either value or closest nodes) with observed address using simple binary format
|
||||||
const response_msg = message.Message{
|
const response_msg = message.Message{
|
||||||
.dht_find_value_response = message.DhtFindValueResponse{
|
.dht_find_value_response = message.DhtFindValueResponse{
|
||||||
.request_id = response.request_id,
|
.request_id = response.request_id,
|
||||||
.found = response.found,
|
.found = response.found,
|
||||||
.value = response.value,
|
.value = response.value,
|
||||||
.closest_nodes = converted_nodes,
|
.closest_nodes = converted_nodes,
|
||||||
|
.observed_addr = observed.addr,
|
||||||
|
.observed_addr_len = observed.len,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
try context.sendResponse(source_id, response_msg);
|
if (!context.sendDhtMessage(source_id, response_msg)) {
|
||||||
|
context.metrics.recordMessageFailed();
|
||||||
|
return error.SendFailed;
|
||||||
|
}
|
||||||
context.metrics.recordMessageProcessed(0.0);
|
context.metrics.recordMessageProcessed(0.0);
|
||||||
|
std.log.debug("DHT: Sent FIND_VALUE response, found={}, observed_addr_len={}", .{ response.found, observed.len });
|
||||||
} else {
|
} else {
|
||||||
context.metrics.recordMessageFailed();
|
context.metrics.recordMessageFailed();
|
||||||
return error.InvalidState;
|
return error.InvalidState;
|
||||||
|
|||||||
@@ -559,6 +559,10 @@ pub const DhtFindNodeResponse = struct {
|
|||||||
|
|
||||||
request_id: u64,
|
request_id: u64,
|
||||||
closest_nodes: []const NodeInfo,
|
closest_nodes: []const NodeInfo,
|
||||||
|
/// IP Reflection: The observed source address of the requester (bundled STUN)
|
||||||
|
/// Format: IPv4 = 6 bytes (4 IP + 2 port), IPv6 = 18 bytes (16 IP + 2 port)
|
||||||
|
observed_addr: [18]u8 = [_]u8{0} ** 18,
|
||||||
|
observed_addr_len: u8 = 0,
|
||||||
|
|
||||||
pub const NodeInfo = struct {
|
pub const NodeInfo = struct {
|
||||||
node_id: NodeId,
|
node_id: NodeId,
|
||||||
@@ -570,6 +574,17 @@ pub const DhtFindNodeResponse = struct {
|
|||||||
return .{
|
return .{
|
||||||
.request_id = request_id,
|
.request_id = request_id,
|
||||||
.closest_nodes = closest_nodes,
|
.closest_nodes = closest_nodes,
|
||||||
|
.observed_addr = [_]u8{0} ** 18,
|
||||||
|
.observed_addr_len = 0,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn initWithObserved(request_id: u64, closest_nodes: []const NodeInfo, addr: [18]u8, addr_len: u8) DhtFindNodeResponse {
|
||||||
|
return .{
|
||||||
|
.request_id = request_id,
|
||||||
|
.closest_nodes = closest_nodes,
|
||||||
|
.observed_addr = addr,
|
||||||
|
.observed_addr_len = addr_len,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -599,11 +614,25 @@ pub const DhtStoreResponse = struct {
|
|||||||
|
|
||||||
request_id: u64,
|
request_id: u64,
|
||||||
success: bool,
|
success: bool,
|
||||||
|
/// IP Reflection: The observed source address of the requester (bundled STUN)
|
||||||
|
observed_addr: [18]u8 = [_]u8{0} ** 18,
|
||||||
|
observed_addr_len: u8 = 0,
|
||||||
|
|
||||||
pub fn init(request_id: u64, success: bool) DhtStoreResponse {
|
pub fn init(request_id: u64, success: bool) DhtStoreResponse {
|
||||||
return .{
|
return .{
|
||||||
.request_id = request_id,
|
.request_id = request_id,
|
||||||
.success = success,
|
.success = success,
|
||||||
|
.observed_addr = [_]u8{0} ** 18,
|
||||||
|
.observed_addr_len = 0,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn initWithObserved(request_id: u64, success: bool, addr: [18]u8, addr_len: u8) DhtStoreResponse {
|
||||||
|
return .{
|
||||||
|
.request_id = request_id,
|
||||||
|
.success = success,
|
||||||
|
.observed_addr = addr,
|
||||||
|
.observed_addr_len = addr_len,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -631,6 +660,9 @@ pub const DhtFindValueResponse = struct {
|
|||||||
found: bool,
|
found: bool,
|
||||||
value: ?[]const u8,
|
value: ?[]const u8,
|
||||||
closest_nodes: []const DhtFindNodeResponse.NodeInfo,
|
closest_nodes: []const DhtFindNodeResponse.NodeInfo,
|
||||||
|
/// IP Reflection: The observed source address of the requester (bundled STUN)
|
||||||
|
observed_addr: [18]u8 = [_]u8{0} ** 18,
|
||||||
|
observed_addr_len: u8 = 0,
|
||||||
|
|
||||||
pub fn init(request_id: u64, found: bool, value: ?[]const u8, closest_nodes: []const DhtFindNodeResponse.NodeInfo) DhtFindValueResponse {
|
pub fn init(request_id: u64, found: bool, value: ?[]const u8, closest_nodes: []const DhtFindNodeResponse.NodeInfo) DhtFindValueResponse {
|
||||||
return .{
|
return .{
|
||||||
@@ -638,6 +670,19 @@ pub const DhtFindValueResponse = struct {
|
|||||||
.found = found,
|
.found = found,
|
||||||
.value = value,
|
.value = value,
|
||||||
.closest_nodes = closest_nodes,
|
.closest_nodes = closest_nodes,
|
||||||
|
.observed_addr = [_]u8{0} ** 18,
|
||||||
|
.observed_addr_len = 0,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn initWithObserved(request_id: u64, found: bool, value: ?[]const u8, closest_nodes: []const DhtFindNodeResponse.NodeInfo, addr: [18]u8, addr_len: u8) DhtFindValueResponse {
|
||||||
|
return .{
|
||||||
|
.request_id = request_id,
|
||||||
|
.found = found,
|
||||||
|
.value = value,
|
||||||
|
.closest_nodes = closest_nodes,
|
||||||
|
.observed_addr = addr,
|
||||||
|
.observed_addr_len = addr_len,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -769,20 +769,21 @@ pub const MessageSerializer = struct {
|
|||||||
// DHT messages - basic size calculations
|
// DHT messages - basic size calculations
|
||||||
.dht_find_node => 8 + 32 + 32, // request_id + target_id + sender_id
|
.dht_find_node => 8 + 32 + 32, // request_id + target_id + sender_id
|
||||||
.dht_find_node_response => |resp| blk: {
|
.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 node_size = 32 + 46 + 1; // NodeInfo size
|
||||||
const nodes_size = resp.closest_nodes.len * node_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: {
|
.dht_store => |store| blk: {
|
||||||
// 8 + 32 + value.len + 4 + 32
|
// 8 + 32 + value.len + 4 + 32
|
||||||
const base = safeAdd(8 + 32, store.value.len) orelse std.math.maxInt(usize);
|
const base = safeAdd(8 + 32, store.value.len) orelse std.math.maxInt(usize);
|
||||||
break :blk safeAdd(base, 4 + 32) 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 => 8 + 32 + 32, // request_id + key + sender_id
|
||||||
.dht_find_value_response => |resp| blk: {
|
.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 value_size = if (resp.value) |v| v.len else 0;
|
||||||
const with_value = safeAdd(base, value_size) orelse std.math.maxInt(usize);
|
const with_value = safeAdd(base, value_size) orelse std.math.maxInt(usize);
|
||||||
const node_size = 32 + 46 + 1;
|
const node_size = 32 + 46 + 1;
|
||||||
@@ -1217,6 +1218,11 @@ pub const MessageSerializer = struct {
|
|||||||
buffer[offset] = node.address_len;
|
buffer[offset] = node.address_len;
|
||||||
offset += 1;
|
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| {
|
.dht_store => |store| {
|
||||||
// SECURITY: Safe bounds check with overflow protection
|
// SECURITY: Safe bounds check with overflow protection
|
||||||
@@ -1244,10 +1250,15 @@ pub const MessageSerializer = struct {
|
|||||||
},
|
},
|
||||||
.dht_store_response => |resp| {
|
.dht_store_response => |resp| {
|
||||||
// SECURITY: Safe bounds check with overflow protection
|
// 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));
|
@memcpy(buffer[offset..offset + 8], std.mem.asBytes(&resp.request_id));
|
||||||
offset += 8;
|
offset += 8;
|
||||||
buffer[offset] = if (resp.success) 1 else 0;
|
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| {
|
.dht_find_value => |find| {
|
||||||
// SECURITY: Safe bounds check with overflow protection
|
// SECURITY: Safe bounds check with overflow protection
|
||||||
@@ -1297,6 +1308,11 @@ pub const MessageSerializer = struct {
|
|||||||
buffer[offset] = node.address_len;
|
buffer[offset] = node.address_len;
|
||||||
offset += 1;
|
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| {
|
.app_data => |app| {
|
||||||
// SECURITY: Safe bounds check with overflow protection
|
// SECURITY: Safe bounds check with overflow protection
|
||||||
|
|||||||
Reference in New Issue
Block a user