bidirectional ip reflection for listener address discovery

This commit is contained in:
2026-02-09 16:02:23 -03:00
parent 59e9e420be
commit 7b47ef155c
31 changed files with 3192 additions and 269 deletions

View File

@@ -903,6 +903,13 @@ pub fn handleDhtFindNode(
// Auto-register the sender's DHT node ID mapping so we can respond
context.registerNodeId(msg.sender_id, source_id);
// Bidirectional IP Reflection: If the request contains our observed address, record it
// This allows the receiver (us) to learn our address from the sender's perspective
if (msg.target_observed_addr_len > 0) {
context.recordObservedAddress(msg.target_observed_addr, msg.target_observed_addr_len, source_id);
std.log.debug("DHT FIND_NODE: Received our observed addr from sender (bidirectional IP reflection), len={}", .{msg.target_observed_addr_len});
}
if (context.dht_coordinator) |dht_ptr| {
const dht = castContext(dht_mod.DhtCoordinator, dht_ptr) orelse {
std.log.err("DHT: Invalid dht_coordinator pointer", .{});
@@ -952,7 +959,7 @@ pub fn handleDhtFindNode(
},
};
if (!context.sendDhtMessage(source_id, response_msg)) {
if (!context.sendDhtMessage(msg.sender_id, response_msg)) {
context.metrics.recordMessageFailed();
return error.SendFailed;
}
@@ -1006,6 +1013,9 @@ pub fn handleDhtFindNodeResponse(
std.log.warn("DHT: Failed to process FIND_NODE response: {}", .{err});
};
// IP Reflection: Record observed address from response (Bundled STUN)
context.recordObservedAddress(msg.observed_addr, msg.observed_addr_len, source_id);
context.metrics.recordMessageProcessed(0.0);
} else {
context.metrics.recordMessageFailed();
@@ -1026,6 +1036,13 @@ pub fn handleDhtStore(
// Auto-register the sender's DHT node ID mapping so we can respond
context.registerNodeId(msg.sender_id, source_id);
// Bidirectional IP Reflection: If the request contains our observed address, record it
// This allows the receiver (us) to learn our address from the sender's perspective
if (msg.target_observed_addr_len > 0) {
context.recordObservedAddress(msg.target_observed_addr, msg.target_observed_addr_len, source_id);
std.log.debug("DHT STORE: Received our observed addr from sender (bidirectional IP reflection), len={}", .{msg.target_observed_addr_len});
}
if (context.dht_coordinator) |dht_ptr| {
const dht = castContext(dht_mod.DhtCoordinator, dht_ptr) orelse {
context.metrics.recordMessageFailed();
@@ -1057,7 +1074,7 @@ pub fn handleDhtStore(
.observed_addr_len = observed.len,
},
};
_ = context.sendDhtMessage(source_id, fail_response);
_ = context.sendDhtMessage(msg.sender_id, fail_response);
context.metrics.recordMessageFailed();
return err;
};
@@ -1072,7 +1089,7 @@ pub fn handleDhtStore(
},
};
if (!context.sendDhtMessage(source_id, response_msg)) {
if (!context.sendDhtMessage(msg.sender_id, response_msg)) {
context.metrics.recordMessageFailed();
return error.SendFailed;
}
@@ -1089,6 +1106,11 @@ pub fn handleDhtStoreResponse(
msg: message.DhtStoreResponse,
context: *handler_mod.HandlerContext,
) !void {
const source_id = context.source_peer_id orelse {
context.metrics.recordMessageFailed();
return error.InvalidMessage;
};
if (context.dht_coordinator) |dht_ptr| {
const dht = castContext(dht_mod.DhtCoordinator, dht_ptr) orelse {
context.metrics.recordMessageFailed();
@@ -1101,6 +1123,10 @@ pub fn handleDhtStoreResponse(
};
dht.handleStoreResponse(dht_response);
// IP Reflection: Record observed address from response (Bundled STUN)
context.recordObservedAddress(msg.observed_addr, msg.observed_addr_len, source_id);
context.metrics.recordMessageProcessed(0.0);
} else {
context.metrics.recordMessageFailed();
@@ -1122,6 +1148,13 @@ pub fn handleDhtFindValue(
// This enables bidirectional DHT communication without manual registration
context.registerNodeId(msg.sender_id, source_id);
// Bidirectional IP Reflection: If the request contains our observed address, record it
// This allows the receiver (us) to learn our address from the sender's perspective
if (msg.target_observed_addr_len > 0) {
context.recordObservedAddress(msg.target_observed_addr, msg.target_observed_addr_len, source_id);
std.log.debug("DHT FIND_VALUE: Received our observed addr from sender (bidirectional IP reflection), len={}", .{msg.target_observed_addr_len});
}
if (context.dht_coordinator) |dht_ptr| {
const dht = castContext(dht_mod.DhtCoordinator, dht_ptr) orelse {
context.metrics.recordMessageFailed();
@@ -1170,12 +1203,11 @@ pub fn handleDhtFindValue(
},
};
if (!context.sendDhtMessage(source_id, response_msg)) {
if (!context.sendDhtMessage(msg.sender_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;
@@ -1187,6 +1219,11 @@ pub fn handleDhtFindValueResponse(
msg: message.DhtFindValueResponse,
context: *handler_mod.HandlerContext,
) !void {
const source_id = context.source_peer_id orelse {
context.metrics.recordMessageFailed();
return error.InvalidMessage;
};
if (context.dht_coordinator) |dht_ptr| {
const dht = castContext(dht_mod.DhtCoordinator, dht_ptr) orelse {
context.metrics.recordMessageFailed();
@@ -1215,9 +1252,17 @@ pub fn handleDhtFindValueResponse(
.closest_nodes = converted_nodes,
};
_ = dht.handleFindValueResponse(dht_response) catch |err| {
// handleFindValueResponse returns an optional allocated slice that must be freed
const found_value = dht.handleFindValueResponse(dht_response) catch |err| {
std.log.warn("DHT: Failed to process FIND_VALUE response: {}", .{err});
return;
};
if (found_value) |v| {
dht.allocator.free(v);
}
// IP Reflection: Record observed address from response (Bundled STUN)
context.recordObservedAddress(msg.observed_addr, msg.observed_addr_len, source_id);
context.metrics.recordMessageProcessed(0.0);
} else {