Updated to the latest

This commit is contained in:
Greenback
2020-11-21 00:34:09 +00:00
parent 084d21cade
commit 9a9b2bfb2e
5 changed files with 149 additions and 121 deletions

View File

@@ -1,5 +1,6 @@
#nullable enable
using System;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.Sockets;
@@ -13,6 +14,11 @@ namespace MediaBrowser.Common.Net
/// </summary>
public class IPHost : IPObject
{
/// <summary>
/// Gets or sets timeout value before resolve required, in minutes.
/// </summary>
public const int Timeout = 30;
/// <summary>
/// Represents an IPHost that has no value.
/// </summary>
@@ -21,7 +27,7 @@ namespace MediaBrowser.Common.Net
/// <summary>
/// Time when last resolved in ticks.
/// </summary>
private long _lastResolved;
private DateTime? _lastResolved = null;
/// <summary>
/// Gets the IP Addresses, attempting to resolve the name, if there are none.
@@ -83,15 +89,9 @@ namespace MediaBrowser.Common.Net
{
// Not implemented, as a host object can only have a prefix length of 128 (IPv6) or 32 (IPv4) prefix length,
// which is automatically determined by it's IP type. Anything else is meaningless.
throw new NotImplementedException("The prefix length on a host cannot be set.");
}
}
/// <summary>
/// Gets or sets timeout value before resolve required, in minutes.
/// </summary>
public byte Timeout { get; set; } = 30;
/// <summary>
/// Gets a value indicating whether the address has a value.
/// </summary>
@@ -395,15 +395,15 @@ namespace MediaBrowser.Common.Net
private bool ResolveHost()
{
// When was the last time we resolved?
if (_lastResolved == 0)
if (_lastResolved == null)
{
_lastResolved = DateTime.UtcNow.Ticks;
_lastResolved = DateTime.UtcNow;
}
// If we haven't resolved before, or out timer has run out...
if ((_addresses.Length == 0 && !Resolved) || (TimeSpan.FromTicks(DateTime.UtcNow.Ticks - _lastResolved).TotalMinutes > Timeout))
// If we haven't resolved before, or our timer has run out...
if ((_addresses.Length == 0 && !Resolved) || (DateTime.UtcNow > _lastResolved?.AddMinutes(Timeout)))
{
_lastResolved = DateTime.UtcNow.Ticks;
_lastResolved = DateTime.UtcNow;
ResolveHostInternal().GetAwaiter().GetResult();
Resolved = true;
}
@@ -433,9 +433,10 @@ namespace MediaBrowser.Common.Net
IPHostEntry ip = await Dns.GetHostEntryAsync(HostName).ConfigureAwait(false);
_addresses = ip.AddressList;
}
catch (SocketException)
catch (SocketException ex)
{
// Ignore socket errors, as the result value will just be an empty array.
// Log and then ignore socket errors, as the result value will just be an empty array.
Debug.WriteLine("GetHostEntryAsync failed with {Message}.", ex.Message);
}
}
}

View File

@@ -18,17 +18,17 @@ namespace MediaBrowser.Common.Net
/// <summary>
/// IPv4 multicast address.
/// </summary>
public static readonly IPAddress MulticastIPv4 = IPAddress.Parse("239.255.255.250");
public static readonly IPAddress SSDPMulticastIPv4 = IPAddress.Parse("239.255.255.250");
/// <summary>
/// IPv6 local link multicast address.
/// </summary>
public static readonly IPAddress MulticastIPv6LinkLocal = IPAddress.Parse("ff02::C");
public static readonly IPAddress SSDPMulticastIPv6LinkLocal = IPAddress.Parse("ff02::C");
/// <summary>
/// IPv6 site local multicast address.
/// </summary>
public static readonly IPAddress MulticastIPv6SiteLocal = IPAddress.Parse("ff05::C");
public static readonly IPAddress SSDPMulticastIPv6SiteLocal = IPAddress.Parse("ff05::C");
/// <summary>
/// IP4Loopback address host.
@@ -235,7 +235,7 @@ namespace MediaBrowser.Common.Net
/// <summary>
/// Returns a textual representation of this object.
/// </summary>
/// <param name="shortVersion">Set to true, if the subnet is to be included as part of the address.</param>
/// <param name="shortVersion">Set to true, if the subnet is to be excluded as part of the address.</param>
/// <returns>String representation of this object.</returns>
public string ToString(bool shortVersion)
{

View File

@@ -86,7 +86,9 @@ namespace MediaBrowser.Common.Net
// prefix length value. eg. /16 on a 4 octet ip4 address (192.168.2.240) will result in the 2 and the 240 being zeroed out.
// Where there is not an exact boundary (eg /23), mod is used to calculate how many bits of this value are to be kept.
byte[] addressBytes = address.GetAddressBytes();
// GetAddressBytes
Span<byte> addressBytes = stackalloc byte[address.AddressFamily == AddressFamily.InterNetwork ? 4 : 16];
address.TryWriteBytes(addressBytes, out _);
int div = prefixLength / 8;
int mod = prefixLength % 8;
@@ -170,14 +172,16 @@ namespace MediaBrowser.Common.Net
if (!address.Equals(IPAddress.None))
{
if (address.IsIPv4MappedToIPv6)
{
address = address.MapToIPv4();
}
if (address.AddressFamily == AddressFamily.InterNetwork)
{
if (address.IsIPv4MappedToIPv6)
{
address = address.MapToIPv4();
}
byte[] octet = address.GetAddressBytes();
// GetAddressBytes
Span<byte> octet = stackalloc byte[4];
address.TryWriteBytes(octet, out _);
return (octet[0] == 10)
|| (octet[0] == 172 && octet[1] >= 16 && octet[1] <= 31) // RFC1918
@@ -186,7 +190,10 @@ namespace MediaBrowser.Common.Net
}
else
{
byte[] octet = address.GetAddressBytes();
// GetAddressBytes
Span<byte> octet = stackalloc byte[16];
address.TryWriteBytes(octet, out _);
uint word = (uint)(octet[0] << 8) + octet[1];
return (word >= 0xfe80 && word <= 0xfebf) // fe80::/10 :Local link.
@@ -223,7 +230,9 @@ namespace MediaBrowser.Common.Net
return false;
}
byte[] octet = address.GetAddressBytes();
// GetAddressBytes
Span<byte> octet = stackalloc byte[16];
address.TryWriteBytes(octet, out _);
uint word = (uint)(octet[0] << 8) + octet[1];
return word >= 0xfe80 && word <= 0xfebf; // fe80::/10 :Local link.
@@ -261,7 +270,9 @@ namespace MediaBrowser.Common.Net
byte cidrnet = 0;
if (!mask.Equals(IPAddress.Any))
{
byte[] bytes = mask.GetAddressBytes();
// GetAddressBytes
Span<byte> bytes = stackalloc byte[mask.AddressFamily == AddressFamily.InterNetwork ? 4 : 16];
mask.TryWriteBytes(bytes, out _);
var zeroed = false;
for (var i = 0; i < bytes.Length; i++)