add UPnP IGD for automatic port mapping and external address discovery

This commit is contained in:
2026-02-09 17:49:44 -03:00
parent cf5eb0d5fd
commit 33730fed8a
13 changed files with 1364 additions and 7 deletions

View File

@@ -12,6 +12,7 @@ Comprehensive guide for using the P2P file sharing library. This guide covers tu
- [Best Practices](#best-practices)
- [Performance Optimization](#performance-optimization)
- [Security Considerations](#security-considerations)
- [NAT Traversal](#nat-traversal)
- [Troubleshooting](#troubleshooting)
- [Migration Guide](#migration-guide)
@@ -914,6 +915,49 @@ var p2p_instance = try p2p.P2P.init(allocator, config);
defer p2p_instance.deinit();
```
## NAT Traversal
The library automatically discovers your public address and maps ports through NAT gateways. No manual configuration is needed for most networks — the system tries multiple strategies in order and uses the first one that succeeds.
### Address Discovery Priority
| Priority | Method | Protocol | How It Works |
|----------|--------|----------|-------------|
| 1 | Explicit IP | Manual | `--public-ip` flag (for known static IPs or Docker) |
| 2 | NAT-PMP | RFC 6886 | 2-packet UDP exchange with the gateway on port 5351 |
| 3 | UPnP IGD | SSDP + SOAP | Multicast discovery (239.255.255.250:1900), then HTTP/SOAP for port mapping |
| 4 | IP Reflection | DHT-bundled | Every DHT response includes the requester's observed address (zero-infrastructure STUN) |
Each method both discovers the external IP and requests a port mapping (where applicable). If a method fails (e.g., router doesn't support NAT-PMP), the next one is tried automatically.
### What Happens Automatically
When a P2P node starts and needs to advertise a reachable address (e.g., for MWP hole punching):
1. **NAT-PMP** — Sends a UDP request to the default gateway. If the router supports NAT-PMP (Apple AirPort, many consumer routers), the external IP and port mapping are returned in ~10ms.
2. **UPnP IGD** — If NAT-PMP fails, sends an SSDP M-SEARCH multicast to find UPnP-enabled routers, fetches the XML device description, then uses SOAP to query the external IP and request a port mapping. Works on most consumer routers.
3. **IP Reflection** — If both NAT-PMP and UPnP fail (e.g., on a VPS or network without gateway support), the node learns its public IP from DHT peers. Every DHT response includes an `observed_addr` field containing the sender's view of the requester's IP. Multiple observers build consensus for reliability.
### When You Might Need Manual Configuration
- **Carrier-grade NAT (CGNAT)**: Double-NAT scenarios where your router's "external" IP is still private. Port mapping succeeds but the mapped address isn't publicly reachable.
- **Strict corporate firewalls**: UDP may be blocked entirely. The library is UDP-only (no TCP fallback).
- **Symmetric NAT**: Port mapping works, but hole punching may fail because the NAT assigns different external ports per destination.
In these cases, use `--public-ip` with a known reachable address, or set up a relay node on a public server.
### Implementation Details
| Component | Source File |
|-----------|------------|
| NAT-PMP client | `src/network/nat_pmp.zig` |
| UPnP IGD client | `src/network/upnp.zig` |
| IP Reflection | Built into DHT responses (`src/p2p/dht.zig`) |
| Address discovery orchestration | `src/p2p/p2p.zig` (`tryNatPmpDiscovery`, `tryUpnpDiscovery`) |
| Docker E2E tests | `docker-tests/nat-pmp/`, `docker-tests/upnp/` |
## Troubleshooting
### Common Issues