From 533ceeaaf299c9396f82e11e16314afbc03407f8 Mon Sep 17 00:00:00 2001 From: gnattu Date: Tue, 4 Feb 2025 16:52:17 +0800 Subject: Fix subnet contains check We are still using `Subnet.Contains` a lot but that does not handle IPv4 mapped to IPv6 addresses at all. It was partially fixed by #12094 in local network checking, but it may not always happen on LAN. Also make all local network checking to use IsInLocalNetwork method instead of just performing `Subnet.Contains` which is not accurate. Filter out all link-local addresses for external interface matching. --- MediaBrowser.Common/Net/NetworkUtils.cs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'MediaBrowser.Common') diff --git a/MediaBrowser.Common/Net/NetworkUtils.cs b/MediaBrowser.Common/Net/NetworkUtils.cs index 738096352..e21fdeb3d 100644 --- a/MediaBrowser.Common/Net/NetworkUtils.cs +++ b/MediaBrowser.Common/Net/NetworkUtils.cs @@ -326,4 +326,23 @@ public static partial class NetworkUtils return new IPAddress(BitConverter.GetBytes(broadCastIPAddress)); } + + /// + /// Check if a subnet contains an address. This method also handles IPv4 mapped to IPv6 addresses. + /// + /// The . + /// The . + /// Whether the supplied IP is in the supplied network. + public static bool SubNetContainsAddress(IPNetwork network, IPAddress address) + { + ArgumentNullException.ThrowIfNull(address); + ArgumentNullException.ThrowIfNull(network); + + if (address.IsIPv4MappedToIPv6) + { + address = address.MapToIPv4(); + } + + return network.Contains(address); + } } -- cgit v1.2.3 From 9aec576c763eeb6a2d1538175d397ab933227e5a Mon Sep 17 00:00:00 2001 From: gnattu Date: Wed, 5 Feb 2025 08:04:29 +0800 Subject: Typo Co-authored-by: Cody Robibero --- MediaBrowser.Common/Net/NetworkUtils.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'MediaBrowser.Common') diff --git a/MediaBrowser.Common/Net/NetworkUtils.cs b/MediaBrowser.Common/Net/NetworkUtils.cs index e21fdeb3d..a498d6271 100644 --- a/MediaBrowser.Common/Net/NetworkUtils.cs +++ b/MediaBrowser.Common/Net/NetworkUtils.cs @@ -333,7 +333,7 @@ public static partial class NetworkUtils /// The . /// The . /// Whether the supplied IP is in the supplied network. - public static bool SubNetContainsAddress(IPNetwork network, IPAddress address) + public static bool SubnetContainsAddress(IPNetwork network, IPAddress address) { ArgumentNullException.ThrowIfNull(address); ArgumentNullException.ThrowIfNull(network); -- cgit v1.2.3