mirror of
https://github.com/Radarr/Radarr.git
synced 2024-08-18 08:19:38 +02:00
New: Improve IPAddress.IsLocal method
Co-Authored-By: ta264 <ta264@users.noreply.github.com> (cherry picked from commit fd98a179ab6fed8037c99344b34593aac24a0ac0)
This commit is contained in:
parent
65c316bd6d
commit
904259df92
@ -7,34 +7,50 @@ public static class IPAddressExtensions
|
|||||||
{
|
{
|
||||||
public static bool IsLocalAddress(this IPAddress ipAddress)
|
public static bool IsLocalAddress(this IPAddress ipAddress)
|
||||||
{
|
{
|
||||||
if (ipAddress.IsIPv6LinkLocal)
|
// Map back to IPv4 if mapped to IPv6, for example "::ffff:1.2.3.4" to "1.2.3.4".
|
||||||
|
if (ipAddress.IsIPv4MappedToIPv6)
|
||||||
{
|
{
|
||||||
return true;
|
ipAddress = ipAddress.MapToIPv4();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Checks loopback ranges for both IPv4 and IPv6.
|
||||||
if (IPAddress.IsLoopback(ipAddress))
|
if (IPAddress.IsLoopback(ipAddress))
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IPv4
|
||||||
if (ipAddress.AddressFamily == AddressFamily.InterNetwork)
|
if (ipAddress.AddressFamily == AddressFamily.InterNetwork)
|
||||||
{
|
{
|
||||||
byte[] bytes = ipAddress.GetAddressBytes();
|
return IsLocalIPv4(ipAddress.GetAddressBytes());
|
||||||
switch (bytes[0])
|
}
|
||||||
{
|
|
||||||
case 10:
|
// IPv6
|
||||||
case 127:
|
if (ipAddress.AddressFamily == AddressFamily.InterNetworkV6)
|
||||||
return true;
|
{
|
||||||
case 172:
|
return ipAddress.IsIPv6LinkLocal ||
|
||||||
return bytes[1] < 32 && bytes[1] >= 16;
|
ipAddress.IsIPv6UniqueLocal ||
|
||||||
case 192:
|
ipAddress.IsIPv6SiteLocal;
|
||||||
return bytes[1] == 168;
|
|
||||||
default:
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static bool IsLocalIPv4(byte[] ipv4Bytes)
|
||||||
|
{
|
||||||
|
// Link local (no IP assigned by DHCP): 169.254.0.0 to 169.254.255.255 (169.254.0.0/16)
|
||||||
|
bool IsLinkLocal() => ipv4Bytes[0] == 169 && ipv4Bytes[1] == 254;
|
||||||
|
|
||||||
|
// Class A private range: 10.0.0.0 – 10.255.255.255 (10.0.0.0/8)
|
||||||
|
bool IsClassA() => ipv4Bytes[0] == 10;
|
||||||
|
|
||||||
|
// Class B private range: 172.16.0.0 – 172.31.255.255 (172.16.0.0/12)
|
||||||
|
bool IsClassB() => ipv4Bytes[0] == 172 && ipv4Bytes[1] >= 16 && ipv4Bytes[1] <= 31;
|
||||||
|
|
||||||
|
// Class C private range: 192.168.0.0 – 192.168.255.255 (192.168.0.0/16)
|
||||||
|
bool IsClassC() => ipv4Bytes[0] == 192 && ipv4Bytes[1] == 168;
|
||||||
|
|
||||||
|
return IsLinkLocal() || IsClassA() || IsClassC() || IsClassB();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user