fix MWP hole punch for production use. bind to 0.0.0.0 instead of

published IP, resend packets every 500ms instead of single-shot, fix
Docker iptables-legacy chains so miniupnpd port mapping fully works
without warnings
This commit is contained in:
2026-02-10 11:26:57 -03:00
parent 33730fed8a
commit 2fe03cb855
12 changed files with 356 additions and 56 deletions

View File

@@ -1,4 +1,11 @@
//! MWP (Mutual Witness Protocol) Integration for zig-p2p
//! MWP (Mutual Witness Protocol) — Production P2P-integrated API
//!
//! This is the production entry point for MWP, wired directly to `*P2P` for
//! DHT storage, peer discovery, and hole punching. Use this module in real
//! applications.
//!
//! For the standalone core (with `DHTInterface` abstraction for unit testing
//! with `MockDHT`), see `src/mwp/mutual_witness.zig`.
//!
//! This module provides seamless integration between MWP and zig-p2p's DHT,
//! enabling NAT traversal and peer discovery without external infrastructure.
@@ -326,12 +333,12 @@ pub const MWP = struct {
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
// Bind to 0.0.0.0:port so we receive on all interfaces.
// The published address tells peers where to send; we bind wildcard to receive.
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);
const bind_addr = std.net.Address.initIp4(.{ 0, 0, 0, 0 }, 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
@@ -359,25 +366,33 @@ pub const MWP = struct {
pkt.signature = self.keypair.sign(hash);
const packet = pkt.serialize();
// Send to all target addresses
for (target_addresses) |addr| {
if (addr.addr_type == .ipv4) {
const sock_addr = std.net.Address.initIp4(addr.ip.v4, addr.port);
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 - must be long enough for peer to find us and respond
const deadline = std.time.milliTimestamp() + 30000; // 30 second timeout for hole punch
// Send-and-receive loop: resend periodically to handle timing jitter.
// Both peers send at the rendezvous time, but if one peer's packet arrives
// before the other's socket is bound, it's lost. Resending every 500ms
// ensures the peer will eventually receive our packet.
const deadline = std.time.milliTimestamp() + 30000; // 30 second timeout
var recv_buf: [256]u8 = undefined;
var last_send: i64 = 0;
while (std.time.milliTimestamp() < deadline) {
// Send (or resend) to all target addresses every 500ms
const loop_now = std.time.milliTimestamp();
if (loop_now - last_send >= 500) {
last_send = loop_now;
for (target_addresses) |addr| {
if (addr.addr_type == .ipv4) {
const sock_addr = std.net.Address.initIp4(addr.ip.v4, addr.port);
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,
});
}
}
}
var src_addr: std.posix.sockaddr.storage = undefined;
var addr_len: std.posix.socklen_t = @sizeOf(@TypeOf(src_addr));