Files
zigp2p/docker-tests/nat-pmp/README.md
Muhammed Tanrıkulu 2fe03cb855 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
2026-02-10 11:26:57 -03:00

4.8 KiB

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
  • Uses a public-looking "external" IP (93.184.216.34) added as alias on the external interface (miniupnpd rejects private IPs)

Quick Start

# 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: PASS

miniupnpd creates DNAT and masquerade rules via libiptc (iptables-legacy).

info: NAT-PMP: Mapped UDP port 18101 -> 93.184.216.34:18101 (lifetime: 3600s)

Connection: PASS

Full MWP hole punch completes. Both peers establish a verified connection through the NAT router using DNAT port forwarding and hairpin masquerade.

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:

docker compose logs router

Common issues:

  • Invalid config options (miniupnpd is picky about format)
  • Interface detection problems

"NAT-PMP: Gateway returned error: Network Failure"

miniupnpd needs the MINIUPNPD-POSTROUTING chain in the nat table for masquerade rules. If missing, the DNAT rule is created but miniupnpd reports failure. Ensure router-entrypoint.sh creates all three chains: MINIUPNPD (nat), MINIUPNPD-POSTROUTING (nat), MINIUPNPD (filter).

"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):

// 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