From c277724e67bffa65112c506e23dd864c6de5b925 Mon Sep 17 00:00:00 2001 From: Mikael Magnusson Date: Fri, 19 Feb 2021 23:41:16 +0100 Subject: netstack: allow listening to 0.0.0.0 and :: convertToFullAddr: Replace 0.0.0.0 and :: with the empty string, which is used by Gvisor's tcpip stack to represent the unspecified addresses. This fixes the following errors when using an unspecified address in a call to ListenTCP: tnet.ListenTCP(&net.TCPAddr{IP: net.ParseIP("::"), Port: 80}) > panic: bind tcp 0.0.0.0:80: bad local address tnet.ListenTCP(&net.TCPAddr{IP: net.ParseIP("0.0.0.0"), Port: 80}) > panic: bind tcp [::]:80: bad local address Signed-off-by: Mikael Magnusson --- tun/netstack/tun.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/tun/netstack/tun.go b/tun/netstack/tun.go index 24d0835..6212493 100644 --- a/tun/netstack/tun.go +++ b/tun/netstack/tun.go @@ -204,15 +204,23 @@ func (tun *netTun) MTU() (int, error) { func convertToFullAddr(ip net.IP, port int) (tcpip.FullAddress, tcpip.NetworkProtocolNumber) { if ip4 := ip.To4(); ip4 != nil { + addr := tcpip.Address(ip4) + if addr == header.IPv4Any { + addr = "" + } return tcpip.FullAddress{ NIC: 1, - Addr: tcpip.Address(ip4), + Addr: addr, Port: uint16(port), }, ipv4.ProtocolNumber } else { + addr := tcpip.Address(ip) + if addr == header.IPv6Any { + addr = "" + } return tcpip.FullAddress{ NIC: 1, - Addr: tcpip.Address(ip), + Addr: addr, Port: uint16(port), }, ipv6.ProtocolNumber } -- cgit v1.2.3