add NAT-PMP for automatic port mapping and external address discovery

This commit is contained in:
2026-02-09 17:05:24 -03:00
parent 7b47ef155c
commit cf5eb0d5fd
11 changed files with 970 additions and 1 deletions

View File

@@ -0,0 +1,129 @@
# NAT-PMP Protocol Testing
This directory contains a Docker-based test environment for the NAT-PMP (RFC 6886) implementation.
## Overview
NAT-PMP allows clients behind a NAT to:
1. Discover their external (public) IP address
2. Request port mappings from the NAT gateway
This test uses `miniupnpd` as the NAT-PMP daemon running on a simulated router.
## Network Topology
```
┌─────────────────────────────────────────────────────┐
│ Internal (10.100.0.0/24) │
│ │
│ ┌─────────┐ ┌─────────┐ ┌───────┐ │
│ │ peer-a │ │ peer-b │ │router │ │
│ │ .10 │ │ .11 │ │ .1 │ │
│ └─────────┘ └─────────┘ └───┬───┘ │
└─────────────────────────────────────────────┼──────┘
│
┌─────────────────────────────────────────────┼──────┐
│ External (10.200.0.0/24) │ │
│ ┌─────┴────┐ │
│ │ router │ │
│ │ .1 │ │
│ └──────────┘ │
└────────────────────────────────────────────────────┘
```
The router:
- Runs `miniupnpd` (NAT-PMP + UPnP IGD daemon)
- Does NAT (iptables MASQUERADE) for internal clients
- Reports a fake "external" IP (93.184.216.34) for testing
## Quick Start
```bash
# Build Linux binary first (from project root)
zig build -Doptimize=ReleaseFast -Dtarget=aarch64-linux
# Run the test
cd docker-tests/nat-pmp
./run-test.sh
```
## What This Tests
| Component | What's Tested |
|-----------|---------------|
| Gateway discovery | Finding the NAT-PMP daemon on the network |
| External address query | Retrieving the public IP from the gateway |
| Port mapping request | Requesting a port forwarding rule |
| MWP integration | Using NAT-PMP discovered address in MWP |
## Expected Results
### External Address Discovery: PASS
The NAT-PMP implementation correctly queries miniupnpd and receives the external IP address.
```
info: NAT-PMP: External address is 93.184.216.34
Using NAT-PMP: 93.184.216.34:18201
```
### Port Mapping: PARTIAL
Port mapping requests are sent correctly, but miniupnpd returns "Network Failure" because:
- The fake external IP doesn't match the actual interface
- iptables rules can't be created for a non-local address
This is expected in the Docker test environment. On a real router with NAT-PMP support, port mapping would succeed.
### Connection: LIMITED
Full MWP connection can't complete because:
- The fake external IP can't be bound locally
- No actual internet routing exists
The test validates the protocol implementation, not end-to-end connectivity.
## Files
| File | Purpose |
|------|---------|
| `docker-compose.yml` | Network topology and service definitions |
| `Dockerfile.router` | Router image with miniupnpd |
| `Dockerfile.peer` | Peer image with mwp-demo binary |
| `router-entrypoint.sh` | Router startup (NAT setup, miniupnpd config) |
| `peer-test.sh` | Peer test script |
| `run-test.sh` | Main test orchestrator |
## Troubleshooting
### "miniupnpd not running"
Check router logs:
```bash
docker compose logs router
```
Common issues:
- Invalid config options (miniupnpd is picky about format)
- Interface detection problems
### "NAT-PMP: Gateway returned error: Network Failure"
This is expected when port mapping fails. External address discovery should still work.
### "Cannot determine gateway"
The NAT-PMP client probes common gateway IPs. Ensure the router is at 10.100.0.1.
## Real-World Usage
On a real network with NAT-PMP support (e.g., Apple AirPort, many consumer routers):
```zig
// NAT-PMP is automatically tried during address discovery
const external_ip = p2p_node.tryNatPmpDiscovery(18000, 3600);
if (external_ip) |ip| {
// Port 18000 is now mapped externally
std.log.info("External address: {}.{}.{}.{}:18000", .{ip[0], ip[1], ip[2], ip[3]});
}
```
## See Also
- [RFC 6886 - NAT-PMP](https://tools.ietf.org/html/rfc6886)
- [miniupnpd](http://miniupnp.free.fr/)
- `src/network/nat_pmp.zig` - Implementation