From 9983f9db468b86ad151fd4e56b685bf8a48a560f Mon Sep 17 00:00:00 2001 From: vivekkoya <67130044+vivekkoya@users.noreply.github.com> Date: Mon, 1 Jan 2024 20:54:06 -0800 Subject: [PATCH] Update network.go by revisiting #5134 From the go source code on potential conflicts in IP Addressing schemes: https://cs.opensource.google/go/go/+/master:src/net/rawconn_unix_test.go // There's no guarantee that IP-level socket // options work well with dual stack sockets. // A simple solution would be to take a look // at the bound address to the raw connection // and to classify the address family of the // underlying socket by the bound address: // // - When IP.To16() != nil and IP.To4() == nil, // we can assume that the raw connection // consists of an IPv6 socket using only // IPv6 addresses. // // - When IP.To16() == nil and IP.To4() != nil, // the raw connection consists of an IPv4 // socket using only IPv4 addresses. // // - Otherwise, the raw connection is a dual // stack socket, an IPv6 socket using IPv6 // addresses including IPv4-mapped or // IPv4-embedded IPv6 addresses. --- weed/util/network.go | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/weed/util/network.go b/weed/util/network.go index 69559b5f0..807a7f409 100644 --- a/weed/util/network.go +++ b/weed/util/network.go @@ -15,18 +15,14 @@ func DetectedHostAddress() string { return "" } - if v4Address := selectIpV4(netInterfaces, true); v4Address != "" { + if v4Address := selectIpV4(netInterfaces); v4Address != "" { return v4Address } - if v6Address := selectIpV4(netInterfaces, false); v6Address != "" { - return v6Address - } - return "localhost" } -func selectIpV4(netInterfaces []net.Interface, isIpV4 bool) string { +func selectIpV4(netInterfaces []net.Interface) string { for _, netInterface := range netInterfaces { if (netInterface.Flags & net.FlagUp) == 0 { continue @@ -38,15 +34,9 @@ func selectIpV4(netInterfaces []net.Interface, isIpV4 bool) string { for _, a := range addrs { if ipNet, ok := a.(*net.IPNet); ok && !ipNet.IP.IsLoopback() { - if isIpV4 { - if ipNet.IP.To4() != nil { - return ipNet.IP.String() - } - } else { - if ipNet.IP.To16() != nil { + if ipNet.IP.To4() != nil && ipNet.IP.To16() == nil || ipNet.IP.To16() != nil && ipNet.IP.To4() == nil{ return ipNet.IP.String() } - } } } }