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

@@ -208,6 +208,12 @@ pub const MWP = struct {
const deadline = std.time.milliTimestamp() + @as(i64, @intCast(timeout_ms));
while (std.time.milliTimestamp() < deadline) {
// CRITICAL: Must call p2p.update() to process incoming network messages
// Without this, DHT responses are never received
self.p2p.update() catch |err| {
std.log.warn("MWP: p2p.update() failed: {}", .{err});
};
if (try self.findPeer(their_pubkey)) |publication| {
return publication;
}
@@ -249,6 +255,21 @@ pub const MWP = struct {
return self.attemptHolePunch(peer_publication.addresses, params, options);
}
/// Connect using an existing publication (for listeners who already published)
/// Use this when you've already published and found the peer's publication.
pub fn connectWithPublication(
self: *MWP,
our_publication: MeetingPublication,
peer_publication: MeetingPublication,
options: ConnectOptions,
) ConnectionResult {
// Compute rendezvous params from both publications
const params = computeRendezvousParamsFromPubs(our_publication, peer_publication, options.rendezvous_buffer_ms);
// Attempt hole punch
return self.attemptHolePunch(peer_publication.addresses, params, options);
}
fn computeRendezvousParamsFromPubs(our_pub: MeetingPublication, their_pub: MeetingPublication, buffer_ms: i64) RendezvousParams {
var hasher = std.crypto.hash.blake2.Blake2b256.init(.{});
if (std.mem.lessThan(u8, &our_pub.nonce, &their_pub.nonce)) {
@@ -261,7 +282,11 @@ pub const MWP = struct {
var shared_randomness: [32]u8 = undefined;
hasher.final(&shared_randomness);
const rendezvous_time = std.time.milliTimestamp() + buffer_ms;
// CRITICAL: Rendezvous time must be the same for both peers
// Use the later publication timestamp plus buffer, so both peers agree
const later_timestamp = @max(our_pub.timestamp, their_pub.timestamp);
const rendezvous_time = later_timestamp + buffer_ms;
const port_offset = std.mem.readInt(u16, shared_randomness[0..2], .little);
const port_hint = 49152 + (port_offset % 16384);
@@ -297,6 +322,23 @@ pub const MWP = struct {
};
defer std.posix.close(fd);
// Enable address reuse so we can bind even if the port was recently used
const enable: c_int = 1;
_ = std.posix.setsockopt(fd, std.posix.SOL.SOCKET, std.posix.SO.REUSEADDR, std.mem.asBytes(&enable)) catch {};
// Bind to our published address so we can receive on the right port
// This is critical for hole punching - we must send FROM the port we published
if (self.our_addresses.len > 0) {
const our_addr = self.our_addresses[0];
if (our_addr.addr_type == .ipv4) {
const bind_addr = std.net.Address.initIp4(our_addr.ip.v4, our_addr.port);
std.posix.bind(fd, &bind_addr.any, bind_addr.getOsSockLen()) catch |err| {
std.log.warn("MWP: Failed to bind to published address: {}", .{err});
// Continue anyway - might work with ephemeral port in some cases
};
}
}
// Set non-blocking
const flags = std.posix.fcntl(fd, std.posix.F.GETFL, 0) catch {
return .{ .hole_punch_failed = .all_addresses_failed };
@@ -321,12 +363,18 @@ pub const MWP = struct {
for (target_addresses) |addr| {
if (addr.addr_type == .ipv4) {
const sock_addr = std.net.Address.initIp4(addr.ip.v4, addr.port);
_ = std.posix.sendto(fd, &packet, 0, &sock_addr.any, sock_addr.getOsSockLen()) catch continue;
const sent = std.posix.sendto(fd, &packet, 0, &sock_addr.any, sock_addr.getOsSockLen()) catch |err| {
std.log.warn("MWP: sendto failed: {}", .{err});
continue;
};
std.log.info("MWP: Sent {d} bytes to {d}.{d}.{d}.{d}:{d}", .{
sent, addr.ip.v4[0], addr.ip.v4[1], addr.ip.v4[2], addr.ip.v4[3], addr.port,
});
}
}
// Wait for response
const deadline = std.time.milliTimestamp() + 2000; // 2 second timeout
// Wait for response - must be long enough for peer to find us and respond
const deadline = std.time.milliTimestamp() + 30000; // 30 second timeout for hole punch
var recv_buf: [256]u8 = undefined;
while (std.time.milliTimestamp() < deadline) {
@@ -338,19 +386,36 @@ pub const MWP = struct {
std.Thread.sleep(1 * std.time.ns_per_ms);
continue;
}
std.log.warn("MWP: recvfrom error: {}", .{err});
continue;
};
if (len < 124) continue;
std.log.info("MWP: Received {d} bytes", .{len});
if (len < 124) {
std.log.warn("MWP: Packet too small: {d} bytes (need 124)", .{len});
continue;
}
// Deserialize and verify packet
const recv_pkt = HolePunchPacket.deserialize(&recv_buf) catch continue;
const recv_pkt = HolePunchPacket.deserialize(&recv_buf) catch |err| {
std.log.warn("MWP: Failed to deserialize packet: {}", .{err});
continue;
};
// Verify connection ID
if (!std.mem.eql(u8, &recv_pkt.connection_id, &params.connection_id)) continue;
if (!std.mem.eql(u8, &recv_pkt.connection_id, &params.connection_id)) {
std.log.warn("MWP: Connection ID mismatch", .{});
continue;
}
// Verify signature
if (!recv_pkt.verify()) continue;
if (!recv_pkt.verify()) {
std.log.warn("MWP: Signature verification failed", .{});
continue;
}
std.log.info("MWP: Packet verified successfully!", .{});
// Extract remote address
const family = src_addr.family;