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

@@ -900,6 +900,9 @@ pub fn handleDhtFindNode(
return error.InvalidMessage;
};
// Auto-register the sender's DHT node ID mapping so we can respond
context.registerNodeId(msg.sender_id, source_id);
if (context.dht_coordinator) |dht_ptr| {
const dht = castContext(dht_mod.DhtCoordinator, dht_ptr) orelse {
std.log.err("DHT: Invalid dht_coordinator pointer", .{});
@@ -921,11 +924,26 @@ pub fn handleDhtFindNode(
return error.OperationFailed;
};
// Convert network.dht.NodeInfo to protocol.message.DhtFindNodeResponse.NodeInfo
const converted_nodes = context.allocator.alloc(message.DhtFindNodeResponse.NodeInfo, response.closest_nodes.len) catch {
context.metrics.recordMessageFailed();
return error.OutOfMemory;
};
defer context.allocator.free(converted_nodes);
for (response.closest_nodes, 0..) |node, i| {
converted_nodes[i] = .{
.node_id = node.node_id,
.address = node.address,
.address_len = node.address_len,
};
}
// Send response back
const response_msg = message.Message{
.dht_find_node_response = message.DhtFindNodeResponse{
.request_id = response.request_id,
.closest_nodes = response.closest_nodes,
.closest_nodes = converted_nodes,
},
};
@@ -955,10 +973,24 @@ pub fn handleDhtFindNodeResponse(
return error.InvalidState;
};
// Convert protocol message to DHT message format
// Convert protocol.message.NodeInfo to network.dht.NodeInfo
const converted_nodes = context.allocator.alloc(dht_mod.NodeInfo, msg.closest_nodes.len) catch {
context.metrics.recordMessageFailed();
return error.OutOfMemory;
};
defer context.allocator.free(converted_nodes);
for (msg.closest_nodes, 0..) |node, i| {
converted_nodes[i] = .{
.node_id = node.node_id,
.address = node.address,
.address_len = node.address_len,
};
}
const dht_response = dht_mod.DhtFindNodeResponse{
.request_id = msg.request_id,
.closest_nodes = msg.closest_nodes,
.closest_nodes = converted_nodes,
};
// Process the response
@@ -983,6 +1015,9 @@ pub fn handleDhtStore(
return error.InvalidMessage;
};
// Auto-register the sender's DHT node ID mapping so we can respond
context.registerNodeId(msg.sender_id, source_id);
if (context.dht_coordinator) |dht_ptr| {
const dht = castContext(dht_mod.DhtCoordinator, dht_ptr) orelse {
context.metrics.recordMessageFailed();
@@ -1065,6 +1100,10 @@ pub fn handleDhtFindValue(
return error.InvalidMessage;
};
// Auto-register the sender's DHT node ID mapping so we can respond
// This enables bidirectional DHT communication without manual registration
context.registerNodeId(msg.sender_id, source_id);
if (context.dht_coordinator) |dht_ptr| {
const dht = castContext(dht_mod.DhtCoordinator, dht_ptr) orelse {
context.metrics.recordMessageFailed();
@@ -1083,12 +1122,28 @@ pub fn handleDhtFindValue(
return error.OperationFailed;
};
// Convert network.dht.NodeInfo to protocol.message.DhtFindNodeResponse.NodeInfo
const converted_nodes = context.allocator.alloc(message.DhtFindNodeResponse.NodeInfo, response.closest_nodes.len) catch {
context.metrics.recordMessageFailed();
return error.OutOfMemory;
};
defer context.allocator.free(converted_nodes);
for (response.closest_nodes, 0..) |node, i| {
converted_nodes[i] = .{
.node_id = node.node_id,
.address = node.address,
.address_len = node.address_len,
};
}
// Send response (either value or closest nodes)
const response_msg = message.Message{
.dht_find_value_response = message.DhtFindValueResponse{
.request_id = response.request_id,
.found = response.found,
.value = response.value,
.closest_nodes = response.closest_nodes,
.closest_nodes = converted_nodes,
},
};
@@ -1111,10 +1166,26 @@ pub fn handleDhtFindValueResponse(
return error.InvalidState;
};
// Convert protocol.message.NodeInfo to network.dht.NodeInfo
const converted_nodes = context.allocator.alloc(dht_mod.NodeInfo, msg.closest_nodes.len) catch {
context.metrics.recordMessageFailed();
return error.OutOfMemory;
};
defer context.allocator.free(converted_nodes);
for (msg.closest_nodes, 0..) |node, i| {
converted_nodes[i] = .{
.node_id = node.node_id,
.address = node.address,
.address_len = node.address_len,
};
}
const dht_response = dht_mod.DhtFindValueResponse{
.request_id = msg.request_id,
.found = msg.found,
.value = msg.value,
.closest_nodes = msg.closest_nodes,
.closest_nodes = converted_nodes,
};
_ = dht.handleFindValueResponse(dht_response) catch |err| {