create Mutual Witness Protocol specs and implementation. mwp aims to be

a zero-infrastructure NAT traversal
This commit is contained in:
2026-02-07 18:01:12 -03:00
parent 2b7ab5f570
commit abb398d363
22 changed files with 4656 additions and 51 deletions

View File

@@ -26,7 +26,9 @@ const ConnectionRateLimiter = core.rate_limiter.ConnectionRateLimiter;
// Protocol components
const protocol = @import("protocol");
const HandlerRegistry = protocol.handler.HandlerRegistry;
const MessageHandler = protocol.handler.MessageHandler;
const MessageRouter = protocol.router.MessageRouter;
const handler_impl = protocol.handler_impl;
// Sibling impl modules
const messaging = @import("messaging.zig");
@@ -138,6 +140,9 @@ pub fn startImpl(p2p: *P2P) Error!void {
p2p.handler_registry = p2p.allocator.create(HandlerRegistry) catch return Error.SystemError;
p2p.handler_registry.?.* = HandlerRegistry.init(p2p.allocator) catch return Error.SystemError;
// Register DHT message handlers
registerDhtHandlers(p2p.handler_registry.?) catch return Error.SystemError;
p2p.message_router = p2p.allocator.create(MessageRouter) catch return Error.SystemError;
// IMP-CRIT-2 FIX: Pass local node ID for proper message routing
p2p.message_router.?.* = MessageRouter.init(p2p.allocator, p2p.node.?.id.bytes);
@@ -286,7 +291,7 @@ pub fn startImpl(p2p: *P2P) Error!void {
if (p2p.dht_coordinator) |dht| {
dht.* = network.DhtCoordinator.initInsecureForDevelopment(p2p.allocator, rt, p2p.local_node_id);
std.log.info("DHT coordinator initialized with node ID (from identity): {x}", .{p2p.local_node_id[0..8].*});
std.log.info("DHT coordinator initialized with node ID (from identity): ", .{});
// Wire DHT coordinator to handler context
if (p2p.handler_registry) |hr| {
@@ -695,3 +700,95 @@ pub fn checkErrorRateAndTransitionStateImpl(p2p: *P2P) Error!void {
p2p.error_window_start = now;
}
}
// ============================================================================
// DHT Handler Registration
// ============================================================================
/// Register DHT message handlers with the handler registry
fn registerDhtHandlers(registry: *HandlerRegistry) !void {
const context = registry.getContext();
const message = protocol.message;
// DHT FIND_NODE handler
const find_node_handler = MessageHandler.init(
struct {
fn handler(msg: message.Message, ctx: *anyopaque) !void {
const context_ptr: *protocol.handler.HandlerContext = @ptrCast(@alignCast(ctx));
try handler_impl.handleDhtFindNode(msg.dht_find_node, context_ptr);
}
}.handler,
context,
.dht_find_node,
);
// DHT FIND_NODE response handler
const find_node_response_handler = MessageHandler.init(
struct {
fn handler(msg: message.Message, ctx: *anyopaque) !void {
const context_ptr: *protocol.handler.HandlerContext = @ptrCast(@alignCast(ctx));
try handler_impl.handleDhtFindNodeResponse(msg.dht_find_node_response, context_ptr);
}
}.handler,
context,
.dht_find_node_response,
);
// DHT STORE handler
const store_handler = MessageHandler.init(
struct {
fn handler(msg: message.Message, ctx: *anyopaque) !void {
const context_ptr: *protocol.handler.HandlerContext = @ptrCast(@alignCast(ctx));
try handler_impl.handleDhtStore(msg.dht_store, context_ptr);
}
}.handler,
context,
.dht_store,
);
// DHT STORE response handler
const store_response_handler = MessageHandler.init(
struct {
fn handler(msg: message.Message, ctx: *anyopaque) !void {
const context_ptr: *protocol.handler.HandlerContext = @ptrCast(@alignCast(ctx));
try handler_impl.handleDhtStoreResponse(msg.dht_store_response, context_ptr);
}
}.handler,
context,
.dht_store_response,
);
// DHT FIND_VALUE handler
const find_value_handler = MessageHandler.init(
struct {
fn handler(msg: message.Message, ctx: *anyopaque) !void {
const context_ptr: *protocol.handler.HandlerContext = @ptrCast(@alignCast(ctx));
try handler_impl.handleDhtFindValue(msg.dht_find_value, context_ptr);
}
}.handler,
context,
.dht_find_value,
);
// DHT FIND_VALUE response handler
const find_value_response_handler = MessageHandler.init(
struct {
fn handler(msg: message.Message, ctx: *anyopaque) !void {
const context_ptr: *protocol.handler.HandlerContext = @ptrCast(@alignCast(ctx));
try handler_impl.handleDhtFindValueResponse(msg.dht_find_value_response, context_ptr);
}
}.handler,
context,
.dht_find_value_response,
);
// Register all DHT handlers
try registry.registerHandler(.dht_find_node, find_node_handler);
try registry.registerHandler(.dht_find_node_response, find_node_response_handler);
try registry.registerHandler(.dht_store, store_handler);
try registry.registerHandler(.dht_store_response, store_response_handler);
try registry.registerHandler(.dht_find_value, find_value_handler);
try registry.registerHandler(.dht_find_value_response, find_value_response_handler);
std.log.debug("DHT handlers registered successfully", .{});
}