create Mutual Witness Protocol specs and implementation. mwp aims to be
a zero-infrastructure NAT traversal
This commit is contained in:
@@ -236,6 +236,12 @@ pub fn handleNetworkMessageImpl(peer_id: PeerId, data: []const u8, context: ?*an
|
||||
return;
|
||||
}
|
||||
|
||||
// DHT messages - deserialize and route through handler_registry
|
||||
if (data.len > 1 and app_id == 0x02) {
|
||||
handleDhtMessageImpl(p2p, peer_id, data[1..]);
|
||||
return;
|
||||
}
|
||||
|
||||
const payload_src: []const u8 = if (data.len > 1) data[1..] else &[_]u8{};
|
||||
|
||||
const payload = p2p.allocator.dupe(u8, payload_src) catch return;
|
||||
@@ -256,6 +262,35 @@ pub fn handleNetworkMessageImpl(peer_id: PeerId, data: []const u8, context: ?*an
|
||||
};
|
||||
}
|
||||
|
||||
/// Handle incoming DHT protocol messages (app_id = 0x02)
|
||||
/// This deserializes the message and routes it through the handler registry,
|
||||
/// setting the connection peer ID as source_peer_id for auto-registration.
|
||||
fn handleDhtMessageImpl(p2p: *P2P, connection_peer_id: PeerId, payload: []const u8) void {
|
||||
const registry = p2p.handler_registry orelse {
|
||||
std.log.warn("DHT: Received message but no handler registry initialized", .{});
|
||||
return;
|
||||
};
|
||||
|
||||
// Use simple binary deserialization (no HMAC - Noise transport provides security)
|
||||
const dht = @import("dht.zig");
|
||||
const msg = dht.deserializeDhtMessage(payload, p2p.allocator) orelse {
|
||||
std.log.warn("DHT: Failed to deserialize message (len={})", .{payload.len});
|
||||
return;
|
||||
};
|
||||
|
||||
// CRITICAL: Set the connection peer ID as source before processing
|
||||
// This allows DHT handlers to auto-register the node_id -> peer_id mapping
|
||||
registry.context.setSource(connection_peer_id.bytes);
|
||||
defer registry.context.clearSource();
|
||||
|
||||
// Process the message through the handler registry
|
||||
registry.processMessage(msg) catch |err| {
|
||||
std.log.warn("DHT: Failed to process message: {}", .{err});
|
||||
};
|
||||
|
||||
std.log.debug("DHT: Processed message from peer", .{});
|
||||
}
|
||||
|
||||
/// Callback for peer disconnection from NetworkManager
|
||||
pub fn handleNetworkDisconnectImpl(peer_id: PeerId, context: ?*anyopaque) void {
|
||||
const p2p: *P2P = @ptrCast(@alignCast(context orelse return));
|
||||
|
||||
Reference in New Issue
Block a user