diff --git a/netenv/addresses.go b/netenv/addresses.go index f0edb84b..a4b0af90 100644 --- a/netenv/addresses.go +++ b/netenv/addresses.go @@ -51,8 +51,9 @@ func GetAssignedGlobalAddresses() (ipv4 []net.IP, ipv6 []net.IP, err error) { } var ( - myNetworks []*net.IPNet - myNetworksLock sync.Mutex + myNetworks []*net.IPNet + myNetworksLock sync.Mutex + myNetworksNetworkChangedFlag = GetNetworkChangedFlag() ) // IsMyIP returns whether the given unicast IP is currently configured on the local host. @@ -69,8 +70,12 @@ func IsMyIP(ip net.IP) (yes bool, err error) { myNetworksLock.Lock() defer myNetworksLock.Unlock() - // Check for match. - if mine, matched := checkIfMyIP(ip); matched { + // Check if the network changed. + if myNetworksNetworkChangedFlag.IsSet() { + // Reset changed flag. + myNetworksNetworkChangedFlag.Refresh() + } else if mine, matched := checkIfMyIP(ip); matched { + // If the network did not change, check for match immediately. return mine, nil } diff --git a/netenv/location_test.go b/netenv/location_test.go index 4e0d0cb2..c243ace0 100644 --- a/netenv/location_test.go +++ b/netenv/location_test.go @@ -13,7 +13,7 @@ func init() { flag.BoolVar(&privileged, "privileged", false, "run tests that require root/admin privileges") } -func TestGetApproximateInternetLocation(t *testing.T) { +func TestGetInternetLocation(t *testing.T) { if testing.Short() { t.Skip() } @@ -21,9 +21,9 @@ func TestGetApproximateInternetLocation(t *testing.T) { t.Skip("skipping privileged test, active with -privileged argument") } - loc, err := GetInternetLocation() - if err != nil { - t.Fatalf("GetApproximateInternetLocation failed: %s", err) + loc, ok := GetInternetLocation() + if !ok { + t.Fatal("GetApproximateInternetLocation failed") } t.Logf("GetApproximateInternetLocation: %+v", loc) }