container2wasm icon indicating copy to clipboard operation
container2wasm copied to clipboard

c2w-net: add dynamic port forwarding for WebSocket mode

Open maceip opened this issue 4 months ago • 1 comments

Summary

Enables port forwarding (-p flag) in --listen-ws mode by dynamically querying DHCP leases to discover the actual container IP address, removing the need for hardcoded values or static MAC-to-IP mappings.

Changes

  • Query gvisor-tap-vsock's internal /services/dhcp/leases endpoint to discover container IP
  • Poll every 200ms for up to 10 seconds to detect when container receives DHCP lease
  • Automatically start port forwarders once container IP is discovered
  • Remove all IP address guessing and hardcoded attempts

Motivation

This change enables browser-hosted Wasm containers to accept incoming connections on forwarded ports. Use cases include:

  • Running web servers inside containers accessible from the host
  • Port-based service discovery and access
  • Multi-container networking scenarios

Technical Details

The implementation:

  1. Container connects via WebSocket
  2. c2w-net polls DHCP lease table to capture assigned IP
  3. Port forwarders start with discovered IP as target
  4. Falls back to default IP (192.168.127.3) after 10-second timeout

Testing

Existing integration tests continue to pass as changes only affect --listen-ws mode code path. The -p flag now works correctly with WebSocket mode.

tested on macos, android, ios.\

macos: works

c2w host

c2w-


android: looks like it works using screen logs, but dont have time rn to do a delegate
windows: no machine to test
ios: browser crashed

maceip avatar Oct 19 '25 21:10 maceip

Please add the signed-off-by line with your name: https://github.com/container2wasm/container2wasm/blob/255b65008ab07b7981c5462046c30d7b891f1adc/CONTRIBUTING.md#sign-your-patch

ktock avatar Oct 20 '25 15:10 ktock