add NAT-PMP for automatic port mapping and external address discovery
This commit is contained in:
17
docker-tests/nat-pmp/Dockerfile.peer
Normal file
17
docker-tests/nat-pmp/Dockerfile.peer
Normal file
@@ -0,0 +1,17 @@
|
||||
# Peer container for NAT-PMP testing
|
||||
# Uses pre-built binary from host (for faster iteration)
|
||||
FROM debian:bookworm-slim
|
||||
|
||||
RUN apt-get update && apt-get install -y \
|
||||
iproute2 \
|
||||
procps \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
|
||||
# Copy pre-built binary (build with: zig build -Doptimize=ReleaseFast -Dtarget=aarch64-linux)
|
||||
COPY zig-out/bin/mwp-demo /usr/local/bin/
|
||||
|
||||
# Test script
|
||||
COPY docker-tests/nat-pmp/peer-test.sh /test.sh
|
||||
RUN chmod +x /test.sh
|
||||
|
||||
WORKDIR /app
|
||||
16
docker-tests/nat-pmp/Dockerfile.router
Normal file
16
docker-tests/nat-pmp/Dockerfile.router
Normal file
@@ -0,0 +1,16 @@
|
||||
# NAT Router with miniupnpd (NAT-PMP + UPnP IGD)
|
||||
FROM alpine:3.19
|
||||
|
||||
RUN apk add --no-cache \
|
||||
miniupnpd \
|
||||
iptables \
|
||||
iproute2 \
|
||||
bash
|
||||
|
||||
# Startup script (generates miniupnpd config dynamically)
|
||||
COPY router-entrypoint.sh /entrypoint.sh
|
||||
RUN chmod +x /entrypoint.sh
|
||||
|
||||
EXPOSE 5351/udp
|
||||
|
||||
ENTRYPOINT ["/entrypoint.sh"]
|
||||
129
docker-tests/nat-pmp/README.md
Normal file
129
docker-tests/nat-pmp/README.md
Normal 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
|
||||
95
docker-tests/nat-pmp/docker-compose.yml
Normal file
95
docker-tests/nat-pmp/docker-compose.yml
Normal file
@@ -0,0 +1,95 @@
|
||||
# NAT-PMP Test Environment
|
||||
#
|
||||
# Architecture:
|
||||
# ┌─────────────────────────────────────────────────────┐
|
||||
# │ 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 and does NAT for internal clients.
|
||||
# Peers can use NAT-PMP to discover their external address and map ports.
|
||||
|
||||
services:
|
||||
# NAT Router with miniupnpd
|
||||
router:
|
||||
build:
|
||||
context: .
|
||||
dockerfile: Dockerfile.router
|
||||
cap_add:
|
||||
- NET_ADMIN
|
||||
sysctls:
|
||||
- net.ipv4.ip_forward=1
|
||||
networks:
|
||||
internal:
|
||||
ipv4_address: 10.100.0.1
|
||||
external:
|
||||
ipv4_address: 10.200.0.1
|
||||
healthcheck:
|
||||
test: ["CMD", "pgrep", "miniupnpd"]
|
||||
interval: 2s
|
||||
timeout: 2s
|
||||
retries: 5
|
||||
|
||||
# Peer A (behind NAT)
|
||||
peer-a:
|
||||
build:
|
||||
context: ../..
|
||||
dockerfile: docker-tests/nat-pmp/Dockerfile.peer
|
||||
depends_on:
|
||||
router:
|
||||
condition: service_healthy
|
||||
cap_add:
|
||||
- NET_ADMIN
|
||||
networks:
|
||||
internal:
|
||||
ipv4_address: 10.100.0.10
|
||||
environment:
|
||||
- PEER_NAME=alice
|
||||
- PEER_PORT=18100
|
||||
- GATEWAY_IP=10.100.0.1
|
||||
command: ["sleep", "infinity"]
|
||||
|
||||
# Peer B (behind NAT)
|
||||
peer-b:
|
||||
build:
|
||||
context: ../..
|
||||
dockerfile: docker-tests/nat-pmp/Dockerfile.peer
|
||||
depends_on:
|
||||
router:
|
||||
condition: service_healthy
|
||||
cap_add:
|
||||
- NET_ADMIN
|
||||
networks:
|
||||
internal:
|
||||
ipv4_address: 10.100.0.11
|
||||
environment:
|
||||
- PEER_NAME=bob
|
||||
- PEER_PORT=18200
|
||||
- GATEWAY_IP=10.100.0.1
|
||||
command: ["sleep", "infinity"]
|
||||
|
||||
networks:
|
||||
internal:
|
||||
driver: bridge
|
||||
ipam:
|
||||
config:
|
||||
- subnet: 10.100.0.0/24
|
||||
gateway: 10.100.0.254
|
||||
external:
|
||||
driver: bridge
|
||||
ipam:
|
||||
config:
|
||||
- subnet: 10.200.0.0/24
|
||||
gateway: 10.200.0.254
|
||||
25
docker-tests/nat-pmp/peer-test.sh
Executable file
25
docker-tests/nat-pmp/peer-test.sh
Executable file
@@ -0,0 +1,25 @@
|
||||
#!/bin/bash
|
||||
set -e
|
||||
|
||||
echo "=== NAT-PMP Peer Test ==="
|
||||
echo "Peer: $PEER_NAME"
|
||||
echo "Port: $PEER_PORT"
|
||||
echo "Gateway: $GATEWAY_IP"
|
||||
|
||||
# Set default route through the NAT router
|
||||
echo "Setting default route through NAT router..."
|
||||
ip route del default 2>/dev/null || true
|
||||
ip route add default via $GATEWAY_IP
|
||||
|
||||
echo "Current routes:"
|
||||
ip route
|
||||
|
||||
echo ""
|
||||
echo "Testing NAT-PMP discovery..."
|
||||
|
||||
# Run MWP demo (it will try NAT-PMP automatically)
|
||||
exec mwp-demo \
|
||||
--name "$PEER_NAME" \
|
||||
--port "$PEER_PORT" \
|
||||
--deterministic \
|
||||
"$@"
|
||||
66
docker-tests/nat-pmp/router-entrypoint.sh
Executable file
66
docker-tests/nat-pmp/router-entrypoint.sh
Executable file
@@ -0,0 +1,66 @@
|
||||
#!/bin/bash
|
||||
set -e
|
||||
|
||||
echo "=== NAT-PMP Router Starting ==="
|
||||
|
||||
# Wait for interfaces to be ready
|
||||
sleep 1
|
||||
|
||||
# Detect interfaces by IP subnet
|
||||
# Internal network: 10.100.0.0/24, External network: 10.200.0.0/24
|
||||
ETH0_IP=$(ip -4 addr show eth0 | grep 'inet ' | awk '{print $2}' | cut -d'/' -f1)
|
||||
ETH1_IP=$(ip -4 addr show eth1 | grep 'inet ' | awk '{print $2}' | cut -d'/' -f1)
|
||||
|
||||
echo "eth0: $ETH0_IP"
|
||||
echo "eth1: $ETH1_IP"
|
||||
|
||||
# Determine which is internal (10.100.x.x) vs external (10.200.x.x)
|
||||
if echo "$ETH0_IP" | grep -q "^10\.100\."; then
|
||||
INTERNAL_IF="eth0"
|
||||
INTERNAL_IP="$ETH0_IP"
|
||||
EXTERNAL_IF="eth1"
|
||||
EXTERNAL_IP="$ETH1_IP"
|
||||
else
|
||||
INTERNAL_IF="eth1"
|
||||
INTERNAL_IP="$ETH1_IP"
|
||||
EXTERNAL_IF="eth0"
|
||||
EXTERNAL_IP="$ETH0_IP"
|
||||
fi
|
||||
|
||||
echo "Internal interface: $INTERNAL_IF ($INTERNAL_IP)"
|
||||
echo "External interface: $EXTERNAL_IF ($EXTERNAL_IP)"
|
||||
|
||||
# Set up NAT (masquerade internal traffic going out external interface)
|
||||
echo "Setting up NAT..."
|
||||
iptables -t nat -A POSTROUTING -o $EXTERNAL_IF -j MASQUERADE
|
||||
iptables -A FORWARD -i $INTERNAL_IF -o $EXTERNAL_IF -j ACCEPT
|
||||
iptables -A FORWARD -i $EXTERNAL_IF -o $INTERNAL_IF -m state --state RELATED,ESTABLISHED -j ACCEPT
|
||||
|
||||
# Create miniupnpd config with correct interfaces
|
||||
# Note: miniupnpd config format is very specific
|
||||
# ext_ip uses a public-looking IP for testing (doesn't need to route)
|
||||
FAKE_PUBLIC_IP="93.184.216.34" # example.com IP - just needs to look public
|
||||
cat > /tmp/miniupnpd.conf << EOF
|
||||
ext_ifname=$EXTERNAL_IF
|
||||
ext_ip=$FAKE_PUBLIC_IP
|
||||
listening_ip=$INTERNAL_IF
|
||||
enable_natpmp=yes
|
||||
enable_upnp=yes
|
||||
secure_mode=no
|
||||
system_uptime=yes
|
||||
lease_file=/var/lib/miniupnpd/leases
|
||||
allow 1024-65535 10.100.0.0/24 1024-65535
|
||||
EOF
|
||||
|
||||
echo "miniupnpd config:"
|
||||
cat /tmp/miniupnpd.conf
|
||||
|
||||
# Create lease directory
|
||||
mkdir -p /var/lib/miniupnpd
|
||||
|
||||
echo ""
|
||||
echo "Starting miniupnpd..."
|
||||
echo "NAT-PMP will listen on $INTERNAL_IP:5351"
|
||||
|
||||
# Start miniupnpd in foreground with debug output
|
||||
exec miniupnpd -d -f /tmp/miniupnpd.conf
|
||||
125
docker-tests/nat-pmp/run-test.sh
Executable file
125
docker-tests/nat-pmp/run-test.sh
Executable file
@@ -0,0 +1,125 @@
|
||||
#!/bin/bash
|
||||
# NAT-PMP Integration Test
|
||||
#
|
||||
# This test verifies that NAT-PMP works correctly for:
|
||||
# 1. External address discovery
|
||||
# 2. Port mapping
|
||||
# 3. Full MWP connection through NAT
|
||||
|
||||
set -e
|
||||
cd "$(dirname "$0")"
|
||||
|
||||
echo "=========================================="
|
||||
echo " NAT-PMP Integration Test"
|
||||
echo "=========================================="
|
||||
|
||||
# Cleanup function
|
||||
cleanup() {
|
||||
echo ""
|
||||
echo "Cleaning up..."
|
||||
docker compose down -v --remove-orphans 2>/dev/null || true
|
||||
}
|
||||
trap cleanup EXIT
|
||||
|
||||
# Build and start
|
||||
echo ""
|
||||
echo "[1/4] Building containers..."
|
||||
docker compose build
|
||||
|
||||
echo ""
|
||||
echo "[2/4] Starting NAT router..."
|
||||
docker compose up -d router
|
||||
sleep 3
|
||||
|
||||
# Check router is running
|
||||
echo ""
|
||||
echo "[3/4] Verifying NAT-PMP daemon..."
|
||||
docker compose exec router pgrep miniupnpd > /dev/null || {
|
||||
echo "ERROR: miniupnpd not running!"
|
||||
docker compose logs router
|
||||
exit 1
|
||||
}
|
||||
echo "miniupnpd is running"
|
||||
|
||||
# Show router state
|
||||
echo ""
|
||||
echo "Router NAT table:"
|
||||
docker compose exec router iptables -t nat -L -n | head -20
|
||||
|
||||
echo ""
|
||||
echo "[4/4] Running peer tests..."
|
||||
|
||||
# Start peer-a in background
|
||||
echo ""
|
||||
echo "--- Starting peer-a (alice) ---"
|
||||
docker compose run -d --name test-peer-a peer-a /test.sh --connect-to bob
|
||||
sleep 2
|
||||
|
||||
# Get alice's DHT ID from logs
|
||||
ALICE_DHT=$(docker logs test-peer-a 2>&1 | grep -o 'DHT Node ID: [0-9a-f]*' | head -1 | cut -d' ' -f4)
|
||||
if [ -z "$ALICE_DHT" ]; then
|
||||
echo "ERROR: Could not get Alice's DHT ID"
|
||||
docker logs test-peer-a
|
||||
exit 1
|
||||
fi
|
||||
echo "Alice DHT ID: $ALICE_DHT"
|
||||
|
||||
# Check if NAT-PMP worked for alice
|
||||
echo ""
|
||||
echo "Alice's NAT-PMP status:"
|
||||
docker logs test-peer-a 2>&1 | grep -i "nat-pmp" || echo "(no NAT-PMP messages)"
|
||||
|
||||
# Start peer-b connecting to alice
|
||||
echo ""
|
||||
echo "--- Starting peer-b (bob) ---"
|
||||
docker compose run --name test-peer-b peer-b /test.sh \
|
||||
--connect-to alice \
|
||||
--bootstrap 10.100.0.10:18100 \
|
||||
--dht-id "$ALICE_DHT" &
|
||||
|
||||
# Wait for connection
|
||||
echo ""
|
||||
echo "Waiting for MWP connection..."
|
||||
sleep 15
|
||||
|
||||
# Check results
|
||||
echo ""
|
||||
echo "=========================================="
|
||||
echo " Test Results"
|
||||
echo "=========================================="
|
||||
|
||||
echo ""
|
||||
echo "--- Alice logs ---"
|
||||
docker logs test-peer-a 2>&1 | tail -30
|
||||
|
||||
echo ""
|
||||
echo "--- Bob logs ---"
|
||||
docker logs test-peer-b 2>&1 | tail -30
|
||||
|
||||
# Check for success indicators
|
||||
ALICE_NATPMP=$(docker logs test-peer-a 2>&1 | grep -c "NAT-PMP:" || true)
|
||||
BOB_NATPMP=$(docker logs test-peer-b 2>&1 | grep -c "NAT-PMP:" || true)
|
||||
CONNECTION=$(docker logs test-peer-b 2>&1 | grep -c "Connected\|SUCCESS" || true)
|
||||
|
||||
echo ""
|
||||
echo "=========================================="
|
||||
echo " Summary"
|
||||
echo "=========================================="
|
||||
echo "Alice NAT-PMP messages: $ALICE_NATPMP"
|
||||
echo "Bob NAT-PMP messages: $BOB_NATPMP"
|
||||
echo "Connection established: $([ $CONNECTION -gt 0 ] && echo 'YES' || echo 'NO')"
|
||||
|
||||
# Check for external address discovery
|
||||
if docker logs test-peer-a 2>&1 | grep -q "Using NAT-PMP:"; then
|
||||
echo ""
|
||||
echo "SUCCESS: NAT-PMP address discovery working!"
|
||||
EXTERNAL=$(docker logs test-peer-a 2>&1 | grep "Using NAT-PMP:" | head -1)
|
||||
echo "$EXTERNAL"
|
||||
else
|
||||
echo ""
|
||||
echo "NOTE: NAT-PMP address discovery not confirmed"
|
||||
echo "(This may be expected if miniupnpd configuration needs adjustment)"
|
||||
fi
|
||||
|
||||
echo ""
|
||||
echo "Test complete."
|
||||
Reference in New Issue
Block a user