From 0def4f8e38c6cf8f2f2004607de4fef6b7dcb79c Mon Sep 17 00:00:00 2001 From: Aaron Klotz Date: Wed, 31 Jul 2024 10:37:08 -0600 Subject: [PATCH] net/netns: on Windows, fall back to default interface index when unspecified address is passed to ControlC and bindToInterfaceByRoute is enabled We were returning an error instead of binding to the default interface. Updates #12979 Signed-off-by: Aaron Klotz --- net/netns/netns_windows.go | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/net/netns/netns_windows.go b/net/netns/netns_windows.go index 3c4bafbb5..c78001560 100644 --- a/net/netns/netns_windows.go +++ b/net/netns/netns_windows.go @@ -86,23 +86,24 @@ func controlC(logf logger.Logf, network, address string, c syscall.RawConn) (err var ifaceIdxV4, ifaceIdxV6 uint32 if useRoute := bindToInterfaceByRoute.Load() || bindToInterfaceByRouteEnv(); useRoute { addr, err := parseAddress(address) - if err != nil { - return fmt.Errorf("parseAddress: %w", err) - } - - if canV4 && (addr.Is4() || addr.Is4In6()) { - addrV4 := addr.Unmap() - ifaceIdxV4, err = getInterfaceIndex(logf, addrV4, defIfaceIdxV4) - if err != nil { - return fmt.Errorf("getInterfaceIndex(%v): %w", addrV4, err) + if err == nil { + if canV4 && (addr.Is4() || addr.Is4In6()) { + addrV4 := addr.Unmap() + ifaceIdxV4, err = getInterfaceIndex(logf, addrV4, defIfaceIdxV4) + if err != nil { + return fmt.Errorf("getInterfaceIndex(%v): %w", addrV4, err) + } } - } - if canV6 && addr.Is6() { - ifaceIdxV6, err = getInterfaceIndex(logf, addr, defIfaceIdxV6) - if err != nil { - return fmt.Errorf("getInterfaceIndex(%v): %w", addr, err) + if canV6 && addr.Is6() { + ifaceIdxV6, err = getInterfaceIndex(logf, addr, defIfaceIdxV6) + if err != nil { + return fmt.Errorf("getInterfaceIndex(%v): %w", addr, err) + } } + } else { + logf("[unexpected] netns: error parsing address %q: %v", address, err) + ifaceIdxV4, ifaceIdxV6 = defIfaceIdxV4, defIfaceIdxV6 } } else { ifaceIdxV4, ifaceIdxV6 = defIfaceIdxV4, defIfaceIdxV6