Use new IPNetwork.TryParse function
This commit is contained in:
parent
464de13acf
commit
3c3f0a765e
|
@ -197,46 +197,29 @@ public static partial class NetworkUtils
|
||||||
/// <returns><c>True</c> if parsing was successful.</returns>
|
/// <returns><c>True</c> if parsing was successful.</returns>
|
||||||
public static bool TryParseToSubnet(ReadOnlySpan<char> value, [NotNullWhen(true)] out IPNetwork? result, bool negated = false)
|
public static bool TryParseToSubnet(ReadOnlySpan<char> value, [NotNullWhen(true)] out IPNetwork? result, bool negated = false)
|
||||||
{
|
{
|
||||||
var splitString = value.Trim().Split('/');
|
value = value.Trim();
|
||||||
if (splitString.MoveNext())
|
if (value.Contains('/'))
|
||||||
{
|
{
|
||||||
var ipBlock = splitString.Current;
|
if (negated && value.StartsWith("!") && IPNetwork.TryParse(value[1..], out result))
|
||||||
var address = IPAddress.None;
|
|
||||||
if (negated && ipBlock.StartsWith("!") && IPAddress.TryParse(ipBlock[1..], out var tmpAddress))
|
|
||||||
{
|
{
|
||||||
address = tmpAddress;
|
return true;
|
||||||
}
|
}
|
||||||
else if (!negated && IPAddress.TryParse(ipBlock, out tmpAddress))
|
else if (!negated && IPNetwork.TryParse(value, out result))
|
||||||
{
|
{
|
||||||
address = tmpAddress;
|
return true;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if (address != IPAddress.None)
|
else if (IPAddress.TryParse(value, out var address))
|
||||||
|
{
|
||||||
|
if (address.AddressFamily == AddressFamily.InterNetwork)
|
||||||
{
|
{
|
||||||
if (splitString.MoveNext())
|
result = address.Equals(IPAddress.Any) ? NetworkConstants.IPv4Any : new IPNetwork(address, NetworkConstants.MinimumIPv4PrefixSize);
|
||||||
{
|
return true;
|
||||||
var subnetBlock = splitString.Current;
|
}
|
||||||
if (int.TryParse(subnetBlock, out var netmask))
|
else if (address.AddressFamily == AddressFamily.InterNetworkV6)
|
||||||
{
|
{
|
||||||
result = new IPNetwork(address, netmask);
|
result = address.Equals(IPAddress.IPv6Any) ? NetworkConstants.IPv6Any : new IPNetwork(address, NetworkConstants.MinimumIPv6PrefixSize);
|
||||||
return true;
|
return true;
|
||||||
}
|
|
||||||
else if (IPAddress.TryParse(subnetBlock, out var netmaskAddress))
|
|
||||||
{
|
|
||||||
result = new IPNetwork(address, NetworkUtils.MaskToCidr(netmaskAddress));
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (address.AddressFamily == AddressFamily.InterNetwork)
|
|
||||||
{
|
|
||||||
result = address.Equals(IPAddress.Any) ? NetworkConstants.IPv4Any : new IPNetwork(address, NetworkConstants.MinimumIPv4PrefixSize);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
else if (address.AddressFamily == AddressFamily.InterNetworkV6)
|
|
||||||
{
|
|
||||||
result = address.Equals(IPAddress.IPv6Any) ? NetworkConstants.IPv6Any : new IPNetwork(address, NetworkConstants.MinimumIPv6PrefixSize);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -71,7 +71,6 @@ namespace Jellyfin.Networking.Tests
|
||||||
[InlineData("127.0.0.1/8")]
|
[InlineData("127.0.0.1/8")]
|
||||||
[InlineData("192.168.1.2")]
|
[InlineData("192.168.1.2")]
|
||||||
[InlineData("192.168.1.2/24")]
|
[InlineData("192.168.1.2/24")]
|
||||||
[InlineData("192.168.1.2/255.255.255.0")]
|
|
||||||
[InlineData("fd23:184f:2029:0:3139:7386:67d7:d517")]
|
[InlineData("fd23:184f:2029:0:3139:7386:67d7:d517")]
|
||||||
[InlineData("[fd23:184f:2029:0:3139:7386:67d7:d517]")]
|
[InlineData("[fd23:184f:2029:0:3139:7386:67d7:d517]")]
|
||||||
[InlineData("fe80::7add:12ff:febb:c67b%16")]
|
[InlineData("fe80::7add:12ff:febb:c67b%16")]
|
||||||
|
@ -103,12 +102,10 @@ namespace Jellyfin.Networking.Tests
|
||||||
[Theory]
|
[Theory]
|
||||||
[InlineData("192.168.5.85/24", "192.168.5.1")]
|
[InlineData("192.168.5.85/24", "192.168.5.1")]
|
||||||
[InlineData("192.168.5.85/24", "192.168.5.254")]
|
[InlineData("192.168.5.85/24", "192.168.5.254")]
|
||||||
[InlineData("192.168.5.85/255.255.255.0", "192.168.5.254")]
|
|
||||||
[InlineData("10.128.240.50/30", "10.128.240.48")]
|
[InlineData("10.128.240.50/30", "10.128.240.48")]
|
||||||
[InlineData("10.128.240.50/30", "10.128.240.49")]
|
[InlineData("10.128.240.50/30", "10.128.240.49")]
|
||||||
[InlineData("10.128.240.50/30", "10.128.240.50")]
|
[InlineData("10.128.240.50/30", "10.128.240.50")]
|
||||||
[InlineData("10.128.240.50/30", "10.128.240.51")]
|
[InlineData("10.128.240.50/30", "10.128.240.51")]
|
||||||
[InlineData("10.128.240.50/255.255.255.252", "10.128.240.51")]
|
|
||||||
[InlineData("127.0.0.1/8", "127.0.0.1")]
|
[InlineData("127.0.0.1/8", "127.0.0.1")]
|
||||||
public void IPv4SubnetMaskMatchesValidIPAddress(string netMask, string ipAddress)
|
public void IPv4SubnetMaskMatchesValidIPAddress(string netMask, string ipAddress)
|
||||||
{
|
{
|
||||||
|
@ -124,12 +121,10 @@ namespace Jellyfin.Networking.Tests
|
||||||
[Theory]
|
[Theory]
|
||||||
[InlineData("192.168.5.85/24", "192.168.4.254")]
|
[InlineData("192.168.5.85/24", "192.168.4.254")]
|
||||||
[InlineData("192.168.5.85/24", "191.168.5.254")]
|
[InlineData("192.168.5.85/24", "191.168.5.254")]
|
||||||
[InlineData("192.168.5.85/255.255.255.252", "192.168.4.254")]
|
|
||||||
[InlineData("10.128.240.50/30", "10.128.240.47")]
|
[InlineData("10.128.240.50/30", "10.128.240.47")]
|
||||||
[InlineData("10.128.240.50/30", "10.128.240.52")]
|
[InlineData("10.128.240.50/30", "10.128.240.52")]
|
||||||
[InlineData("10.128.240.50/30", "10.128.239.50")]
|
[InlineData("10.128.240.50/30", "10.128.239.50")]
|
||||||
[InlineData("10.128.240.50/30", "10.127.240.51")]
|
[InlineData("10.128.240.50/30", "10.127.240.51")]
|
||||||
[InlineData("10.128.240.50/255.255.255.252", "10.127.240.51")]
|
|
||||||
public void IPv4SubnetMaskDoesNotMatchInvalidIPAddress(string netMask, string ipAddress)
|
public void IPv4SubnetMaskDoesNotMatchInvalidIPAddress(string netMask, string ipAddress)
|
||||||
{
|
{
|
||||||
var ipa = IPAddress.Parse(ipAddress);
|
var ipa = IPAddress.Parse(ipAddress);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user