diff options
| author | gnattu <gnattuoc@me.com> | 2025-02-04 16:52:17 +0800 |
|---|---|---|
| committer | gnattu <gnattuoc@me.com> | 2025-02-04 16:52:17 +0800 |
| commit | 533ceeaaf299c9396f82e11e16314afbc03407f8 (patch) | |
| tree | c3b385e730c1cd982e7272fc5118a12965928d7a /MediaBrowser.Common | |
| parent | d376b5fbc7cf3ae7440a606a9e885d70605956bd (diff) | |
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.
Diffstat (limited to 'MediaBrowser.Common')
| -rw-r--r-- | MediaBrowser.Common/Net/NetworkUtils.cs | 19 |
1 files changed, 19 insertions, 0 deletions
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)); } + + /// <summary> + /// Check if a subnet contains an address. This method also handles IPv4 mapped to IPv6 addresses. + /// </summary> + /// <param name="network">The <see cref="IPNetwork"/>.</param> + /// <param name="address">The <see cref="IPAddress"/>.</param> + /// <returns>Whether the supplied IP is in the supplied network.</returns> + public static bool SubNetContainsAddress(IPNetwork network, IPAddress address) + { + ArgumentNullException.ThrowIfNull(address); + ArgumentNullException.ThrowIfNull(network); + + if (address.IsIPv4MappedToIPv6) + { + address = address.MapToIPv4(); + } + + return network.Contains(address); + } } |
