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:
@@ -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{
|
||||
.dht_find_node_response = message.DhtFindNodeResponse{
|
||||
.request_id = response.request_id,
|
||||
.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);
|
||||
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 {
|
||||
std.log.err("DHT: FIND_NODE received but no DHT coordinator configured", .{});
|
||||
context.metrics.recordMessageFailed();
|
||||
@@ -1033,33 +1041,43 @@ pub fn handleDhtStore(
|
||||
.sender_id = msg.sender_id,
|
||||
};
|
||||
|
||||
// Get observed address for IP reflection (bundled STUN)
|
||||
const observed = context.getSourceObservedAddr();
|
||||
|
||||
// Handle the store request
|
||||
const response = dht.handleStore(dht_msg) catch |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{
|
||||
.dht_store_response = message.DhtStoreResponse{
|
||||
.request_id = msg.request_id,
|
||||
.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();
|
||||
return err;
|
||||
};
|
||||
|
||||
// Send success response
|
||||
// Send success response with observed address using simple binary format
|
||||
const response_msg = message.Message{
|
||||
.dht_store_response = message.DhtStoreResponse{
|
||||
.request_id = response.request_id,
|
||||
.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);
|
||||
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 {
|
||||
context.metrics.recordMessageFailed();
|
||||
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{
|
||||
.dht_find_value_response = message.DhtFindValueResponse{
|
||||
.request_id = response.request_id,
|
||||
.found = response.found,
|
||||
.value = response.value,
|
||||
.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);
|
||||
std.log.debug("DHT: Sent FIND_VALUE response, found={}, observed_addr_len={}", .{ response.found, observed.len });
|
||||
} else {
|
||||
context.metrics.recordMessageFailed();
|
||||
return error.InvalidState;
|
||||
|
||||
Reference in New Issue
Block a user