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

@@ -0,0 +1,17 @@
# Peer container for UPnP IGD 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/upnp/peer-test.sh /test.sh
RUN chmod +x /test.sh
WORKDIR /app

View File

@@ -0,0 +1,17 @@
# NAT Router with miniupnpd (UPnP IGD only, NAT-PMP disabled)
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
# UPnP SSDP multicast + HTTP control port
EXPOSE 1900/udp
ENTRYPOINT ["/entrypoint.sh"]

113
docker-tests/upnp/README.md Normal file
View File

@@ -0,0 +1,113 @@
# UPnP IGD Protocol Testing
This directory contains a Docker-based test environment for the UPnP IGD (Internet Gateway Device) implementation.
## Overview
UPnP IGD allows clients behind a NAT to:
1. Discover the gateway via SSDP multicast (239.255.255.250:1900)
2. Fetch the gateway's XML device description
3. Use SOAP to query the external (public) IP address
4. Use SOAP to request port mappings from the NAT gateway
This test uses `miniupnpd` as the UPnP daemon with **NAT-PMP disabled**, so the mwp-demo is forced to use the UPnP IGD path.
## 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` with UPnP IGD enabled, NAT-PMP disabled
- 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/upnp
./run-test.sh
```
## What This Tests
| Component | What's Tested |
|-----------|---------------|
| SSDP discovery | M-SEARCH multicast finds the UPnP gateway |
| XML description | Fetching and parsing the gateway's device XML |
| GetExternalIPAddress | SOAP request to retrieve the public IP |
| AddPortMapping | SOAP request for port forwarding |
| MWP integration | Using UPnP-discovered address in MWP |
| NAT-PMP fallthrough | NAT-PMP fails (disabled), UPnP is tried next |
## Expected Results
### External Address Discovery: PASS
The UPnP implementation discovers the gateway via SSDP, fetches its XML description, and queries the external IP via SOAP.
```
UPnP: External address is 93.184.216.34
Using UPnP IGD: 93.184.216.34:18101
```
### Port Mapping: PARTIAL
Port mapping requests are sent correctly, but miniupnpd may return errors 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.
### NAT-PMP: SKIPPED
NAT-PMP is disabled on the router (`enable_natpmp=no`), so the mwp-demo's NAT-PMP attempt fails silently and falls through to UPnP.
## Files
| File | Purpose |
|------|---------|
| `docker-compose.yml` | Network topology and service definitions |
| `Dockerfile.router` | Router image with miniupnpd (UPnP only) |
| `Dockerfile.peer` | Peer image with mwp-demo binary |
| `router-entrypoint.sh` | Router startup (NAT setup, miniupnpd config with NAT-PMP disabled) |
| `peer-test.sh` | Peer test script |
| `run-test.sh` | Main test orchestrator |
## Troubleshooting
### "miniupnpd not running"
Check router logs:
```bash
docker compose logs router
```
### "UPnP: Gateway discovery failed"
SSDP multicast may not work on all Docker network configurations. Check that the internal bridge network allows multicast traffic.
### "No UPnP messages"
The mwp-demo tries NAT-PMP first. If NAT-PMP somehow succeeds (shouldn't with `enable_natpmp=no`), UPnP will be skipped. Check for `Using NAT-PMP:` in logs.
## See Also
- [UPnP IGD Specification](http://upnp.org/specs/gw/UPnP-gw-WANIPConnection-v1-Service.pdf)
- [miniupnpd](http://miniupnp.free.fr/)
- `src/network/upnp.zig` - Implementation
- `docker-tests/nat-pmp/` - NAT-PMP test (same topology, different protocol)

View File

@@ -0,0 +1,95 @@
# UPnP IGD 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 with UPnP enabled and NAT-PMP disabled.
# Peers discover external address via SSDP multicast + SOAP.
services:
# NAT Router with miniupnpd (UPnP only, no NAT-PMP)
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/upnp/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/upnp/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/upnp/peer-test.sh Executable file
View File

@@ -0,0 +1,25 @@
#!/bin/bash
set -e
echo "=== UPnP IGD 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 UPnP IGD discovery..."
# Run MWP demo (NAT-PMP will fail since it's disabled, then UPnP will be tried)
exec mwp-demo \
--name "$PEER_NAME" \
--port "$PEER_PORT" \
--deterministic \
"$@"

View File

@@ -0,0 +1,66 @@
#!/bin/bash
set -e
echo "=== UPnP IGD 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 UPnP ENABLED and NAT-PMP DISABLED
# This forces peers to use UPnP IGD (SSDP + SOAP) instead of NAT-PMP
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=no
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 "UPnP IGD will listen on $INTERNAL_IF (SSDP multicast + SOAP HTTP)"
echo "NAT-PMP is DISABLED"
# Start miniupnpd in foreground with debug output
exec miniupnpd -d -f /tmp/miniupnpd.conf

143
docker-tests/upnp/run-test.sh Executable file
View File

@@ -0,0 +1,143 @@
#!/bin/bash
# UPnP IGD Integration Test
#
# This test verifies that UPnP IGD works correctly for:
# 1. SSDP multicast discovery of the gateway
# 2. SOAP GetExternalIPAddress
# 3. SOAP AddPortMapping
# 4. Full MWP address discovery through UPnP
#
# NAT-PMP is DISABLED on the router so the mwp-demo falls through to UPnP.
set -e
cd "$(dirname "$0")"
echo "=========================================="
echo " UPnP IGD 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 (UPnP enabled, NAT-PMP disabled)..."
docker compose up -d router
sleep 3
# Check router is running
echo ""
echo "[3/4] Verifying UPnP daemon..."
docker compose exec router pgrep miniupnpd > /dev/null || {
echo "ERROR: miniupnpd not running!"
docker compose logs router
exit 1
}
echo "miniupnpd is running (UPnP mode)"
# 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 UPnP worked for alice
echo ""
echo "Alice's UPnP status:"
docker logs test-peer-a 2>&1 | grep -i "upnp" || echo "(no UPnP messages yet)"
# 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_UPNP=$(docker logs test-peer-a 2>&1 | grep -c "UPnP" || true)
BOB_UPNP=$(docker logs test-peer-b 2>&1 | grep -c "UPnP" || true)
ALICE_NATPMP=$(docker logs test-peer-a 2>&1 | grep -c "Using NAT-PMP:" || true)
BOB_NATPMP=$(docker logs test-peer-b 2>&1 | grep -c "Using NAT-PMP:" || true)
CONNECTION=$(docker logs test-peer-b 2>&1 | grep -c "Connected\|SUCCESS" || true)
echo ""
echo "=========================================="
echo " Summary"
echo "=========================================="
echo "Alice UPnP messages: $ALICE_UPNP"
echo "Bob UPnP messages: $BOB_UPNP"
echo "Alice NAT-PMP used: $([ $ALICE_NATPMP -gt 0 ] && echo 'YES (unexpected!)' || echo 'NO (correct)')"
echo "Bob NAT-PMP used: $([ $BOB_NATPMP -gt 0 ] && echo 'YES (unexpected!)' || echo 'NO (correct)')"
echo "Connection established: $([ $CONNECTION -gt 0 ] && echo 'YES' || echo 'NO')"
# Check for UPnP external address discovery
if docker logs test-peer-a 2>&1 | grep -q "Using UPnP IGD:"; then
echo ""
echo "SUCCESS: UPnP IGD address discovery working!"
EXTERNAL=$(docker logs test-peer-a 2>&1 | grep "Using UPnP IGD:" | head -1)
echo "$EXTERNAL"
else
echo ""
echo "NOTE: UPnP IGD address discovery not confirmed in mwp-demo output"
# Check lower-level UPnP messages
if docker logs test-peer-a 2>&1 | grep -q "UPnP: External address is"; then
echo "But UPnP SOAP GetExternalIPAddress succeeded:"
docker logs test-peer-a 2>&1 | grep "UPnP: External address is" | head -1
fi
fi
# Verify NAT-PMP was NOT used (since it's disabled)
if [ $ALICE_NATPMP -gt 0 ] || [ $BOB_NATPMP -gt 0 ]; then
echo ""
echo "WARNING: NAT-PMP was used despite being disabled on the router!"
echo "This suggests the NAT-PMP disable config is not working correctly."
fi
echo ""
echo "Test complete."