Skip to content

Commit 3b6a911

Browse files
authored
Local addresses cache validity check (#1458)
* Add network change detection to clear cached addresses. * Return cached ip only when it is valid
1 parent 55bd0c6 commit 3b6a911

File tree

1 file changed

+35
-34
lines changed

1 file changed

+35
-34
lines changed

src/sys/Net/NetServices.cs

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -615,56 +615,57 @@ public static IPAddress GetLocalAddressForRemote(IPAddress destination)
615615

616616
if (m_localAddressTable.TryGetValue(destination, out var cachedAddress))
617617
{
618-
if (DateTime.Now.Subtract(cachedAddress.Item2).TotalSeconds >= LOCAL_ADDRESS_CACHE_LIFETIME_SECONDS)
618+
if (DateTime.Now.Subtract(cachedAddress.Item2).TotalSeconds < LOCAL_ADDRESS_CACHE_LIFETIME_SECONDS)
619+
{
620+
// Cached item is valid, return the value
621+
return cachedAddress.Item1;
622+
}
623+
else
619624
{
620625
m_localAddressTable.TryRemove(destination, out _);
621626
}
622-
623-
return cachedAddress.Item1;
624627
}
625-
else
626-
{
627-
IPAddress localAddress = null;
628628

629-
if (destination.AddressFamily == AddressFamily.InterNetwork || destination.IsIPv4MappedToIPv6)
629+
IPAddress localAddress = null;
630+
631+
if (destination.AddressFamily == AddressFamily.InterNetwork || destination.IsIPv4MappedToIPv6)
632+
{
633+
using (UdpClient udpClient = new UdpClient())
630634
{
631-
using (UdpClient udpClient = new UdpClient())
635+
try
632636
{
633-
try
634-
{
635-
udpClient.Connect(destination.MapToIPv4(), NETWORK_TEST_PORT);
636-
localAddress = (udpClient.Client.LocalEndPoint as IPEndPoint)?.Address;
637-
}
638-
catch (SocketException)
639-
{
640-
// Socket exception is thrown if the OS cannot find a suitable entry in the routing table.
641-
}
637+
udpClient.Connect(destination.MapToIPv4(), NETWORK_TEST_PORT);
638+
localAddress = (udpClient.Client.LocalEndPoint as IPEndPoint)?.Address;
639+
}
640+
catch (SocketException)
641+
{
642+
// Socket exception is thrown if the OS cannot find a suitable entry in the routing table.
642643
}
643644
}
644-
else
645+
}
646+
else
647+
{
648+
using (UdpClient udpClient = new UdpClient(AddressFamily.InterNetworkV6))
645649
{
646-
using (UdpClient udpClient = new UdpClient(AddressFamily.InterNetworkV6))
650+
try
647651
{
648-
try
649-
{
650-
udpClient.Connect(destination, NETWORK_TEST_PORT);
651-
localAddress = (udpClient.Client.LocalEndPoint as IPEndPoint)?.Address;
652-
}
653-
catch (SocketException)
654-
{
655-
// Socket exception is thrown if the OS cannot find a suitable entry in the routing table.
656-
}
652+
udpClient.Connect(destination, NETWORK_TEST_PORT);
653+
localAddress = (udpClient.Client.LocalEndPoint as IPEndPoint)?.Address;
654+
}
655+
catch (SocketException)
656+
{
657+
// Socket exception is thrown if the OS cannot find a suitable entry in the routing table.
657658
}
658-
659659
}
660660

661-
if (localAddress != null)
662-
{
663-
m_localAddressTable.TryAdd(destination, new Tuple<IPAddress, DateTime>(localAddress, DateTime.Now));
664-
}
661+
}
665662

666-
return localAddress;
663+
if (localAddress != null)
664+
{
665+
m_localAddressTable.TryAdd(destination, new Tuple<IPAddress, DateTime>(localAddress, DateTime.Now));
667666
}
667+
668+
return localAddress;
668669
}
669670

670671
/// <summary>

0 commit comments

Comments
 (0)