Merge pull request #5763 from Bond-009/ipobjects

Minor improvements to IPObjects
This commit is contained in:
Bond-009 2021-04-11 23:31:03 +02:00 committed by GitHub
commit 1361ccd9ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 25 deletions

View File

@ -135,7 +135,7 @@ namespace MediaBrowser.Common.Net
}
// See if it's an IPv6 with port address e.g. [::1] or [::1]:120.
int i = host.IndexOf("]", StringComparison.OrdinalIgnoreCase);
int i = host.IndexOf(']', StringComparison.Ordinal);
if (i != -1)
{
return TryParse(host.Remove(i - 1).TrimStart(' ', '['), out hostObj);
@ -389,8 +389,8 @@ namespace MediaBrowser.Common.Net
/// <inheritdoc/>
protected override IPObject CalculateNetworkAddress()
{
var netAddr = NetworkAddressOf(this[0], PrefixLength);
return new IPNetAddress(netAddr.Address, netAddr.PrefixLength);
var (address, prefixLength) = NetworkAddressOf(this[0], PrefixLength);
return new IPNetAddress(address, prefixLength);
}
/// <summary>
@ -427,7 +427,7 @@ namespace MediaBrowser.Common.Net
// Resolves the host name - so save a DNS lookup.
if (string.Equals(HostName, "localhost", StringComparison.OrdinalIgnoreCase))
{
_addresses = new IPAddress[] { new IPAddress(Ipv4Loopback), new IPAddress(Ipv6Loopback) };
_addresses = new IPAddress[] { IPAddress.Loopback, IPAddress.IPv6Loopback };
return;
}

View File

@ -38,7 +38,7 @@ namespace MediaBrowser.Common.Net
/// <summary>
/// IP6Loopback address host.
/// </summary>
public static readonly IPNetAddress IP6Loopback = IPNetAddress.Parse("::1");
public static readonly IPNetAddress IP6Loopback = new IPNetAddress(IPAddress.IPv6Loopback);
/// <summary>
/// Object's IP address.
@ -113,7 +113,7 @@ namespace MediaBrowser.Common.Net
}
// Is it a network?
string[] tokens = addr.Split("/");
string[] tokens = addr.Split('/');
if (tokens.Length == 2)
{
@ -171,8 +171,8 @@ namespace MediaBrowser.Common.Net
address = address.MapToIPv4();
}
var altAddress = NetworkAddressOf(address, PrefixLength);
return NetworkAddress.Address.Equals(altAddress.Address) && NetworkAddress.PrefixLength >= altAddress.PrefixLength;
var (altAddress, altPrefix) = NetworkAddressOf(address, PrefixLength);
return NetworkAddress.Address.Equals(altAddress) && NetworkAddress.PrefixLength >= altPrefix;
}
/// <inheritdoc/>
@ -196,8 +196,8 @@ namespace MediaBrowser.Common.Net
return NetworkAddress.PrefixLength <= netaddrObj.PrefixLength;
}
var altAddress = NetworkAddressOf(netaddrObj.Address, PrefixLength);
return NetworkAddress.Address.Equals(altAddress.Address);
var altAddress = NetworkAddressOf(netaddrObj.Address, PrefixLength).address;
return NetworkAddress.Address.Equals(altAddress);
}
return false;
@ -270,8 +270,8 @@ namespace MediaBrowser.Common.Net
/// <inheritdoc/>
protected override IPObject CalculateNetworkAddress()
{
var value = NetworkAddressOf(_address, PrefixLength);
return new IPNetAddress(value.Address, value.PrefixLength);
var (address, prefixLength) = NetworkAddressOf(_address, PrefixLength);
return new IPNetAddress(address, prefixLength);
}
}
}

View File

@ -10,16 +10,6 @@ namespace MediaBrowser.Common.Net
/// </summary>
public abstract class IPObject : IEquatable<IPObject>
{
/// <summary>
/// IPv6 Loopback address.
/// </summary>
protected static readonly byte[] Ipv6Loopback = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
/// <summary>
/// IPv4 Loopback address.
/// </summary>
protected static readonly byte[] Ipv4Loopback = { 127, 0, 0, 1 };
/// <summary>
/// The network address of this object.
/// </summary>
@ -64,7 +54,7 @@ namespace MediaBrowser.Common.Net
/// <param name="address">IP Address to convert.</param>
/// <param name="prefixLength">Subnet prefix.</param>
/// <returns>IPAddress.</returns>
public static (IPAddress Address, byte PrefixLength) NetworkAddressOf(IPAddress address, byte prefixLength)
public static (IPAddress address, byte prefixLength) NetworkAddressOf(IPAddress address, byte prefixLength)
{
if (address == null)
{
@ -78,7 +68,7 @@ namespace MediaBrowser.Common.Net
if (IsLoopback(address))
{
return (Address: address, PrefixLength: prefixLength);
return (address, prefixLength);
}
// An ip address is just a list of bytes, each one representing a segment on the network.
@ -110,7 +100,7 @@ namespace MediaBrowser.Common.Net
}
// Return the network address for the prefix.
return (Address: new IPAddress(addressBytes), PrefixLength: prefixLength);
return (new IPAddress(addressBytes), prefixLength);
}
/// <summary>