jellyfin-server/Emby.Server.Implementations/Networking/NetworkManager.cs

728 lines
25 KiB
C#
Raw Normal View History

using System;
2016-10-29 05:40:15 +00:00
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
2016-10-29 18:46:14 +00:00
using System.Threading.Tasks;
using MediaBrowser.Common.Net;
2016-10-29 05:40:15 +00:00
using MediaBrowser.Model.Extensions;
2016-11-11 07:24:36 +00:00
using MediaBrowser.Model.IO;
using MediaBrowser.Model.Net;
2018-09-12 17:26:21 +00:00
using MediaBrowser.Model.System;
using Microsoft.Extensions.Logging;
2016-10-29 05:40:15 +00:00
namespace Emby.Server.Implementations.Networking
2016-10-29 05:40:15 +00:00
{
2016-11-11 07:24:36 +00:00
public class NetworkManager : INetworkManager
2016-10-29 05:40:15 +00:00
{
protected ILogger Logger { get; private set; }
2017-11-29 20:50:18 +00:00
public event EventHandler NetworkChanged;
2018-09-12 17:26:21 +00:00
public Func<string[]> LocalSubnetsFn { get; set; }
2016-10-29 05:40:15 +00:00
public NetworkManager(
ILoggerFactory loggerFactory,
IEnvironmentInfo environment)
2016-10-29 05:40:15 +00:00
{
Logger = loggerFactory.CreateLogger(nameof(NetworkManager));
2017-11-27 20:16:41 +00:00
2018-09-12 17:26:21 +00:00
// In FreeBSD these events cause a crash
if (environment.OperatingSystem != MediaBrowser.Model.System.OperatingSystem.BSD)
2017-11-27 20:16:41 +00:00
{
2018-09-12 17:26:21 +00:00
try
{
NetworkChange.NetworkAddressChanged += NetworkChange_NetworkAddressChanged;
}
catch (Exception ex)
{
2018-12-20 12:11:26 +00:00
Logger.LogError(ex, "Error binding to NetworkAddressChanged event");
2018-09-12 17:26:21 +00:00
}
2017-11-27 20:16:41 +00:00
2018-09-12 17:26:21 +00:00
try
{
NetworkChange.NetworkAvailabilityChanged += NetworkChange_NetworkAvailabilityChanged;
}
catch (Exception ex)
{
2018-12-20 12:11:26 +00:00
Logger.LogError(ex, "Error binding to NetworkChange_NetworkAvailabilityChanged event");
2018-09-12 17:26:21 +00:00
}
2017-11-27 20:16:41 +00:00
}
}
private void NetworkChange_NetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs e)
{
Logger.LogDebug("NetworkAvailabilityChanged");
2017-11-29 20:50:18 +00:00
OnNetworkChanged();
2017-11-27 20:16:41 +00:00
}
private void NetworkChange_NetworkAddressChanged(object sender, EventArgs e)
{
Logger.LogDebug("NetworkAddressChanged");
2017-11-29 20:50:18 +00:00
OnNetworkChanged();
}
private void OnNetworkChanged()
{
lock (_localIpAddressSyncLock)
{
_localIpAddresses = null;
2018-09-12 17:26:21 +00:00
_macAddresses = null;
}
2017-11-29 20:50:18 +00:00
if (NetworkChanged != null)
{
NetworkChanged(this, EventArgs.Empty);
}
2016-10-29 05:40:15 +00:00
}
2018-09-12 17:26:21 +00:00
private IpAddressInfo[] _localIpAddresses;
2016-10-29 05:40:15 +00:00
private readonly object _localIpAddressSyncLock = new object();
public IpAddressInfo[] GetLocalIpAddresses(bool ignoreVirtualInterface = true)
2016-10-29 05:40:15 +00:00
{
lock (_localIpAddressSyncLock)
{
if (_localIpAddresses == null)
2016-10-29 05:40:15 +00:00
{
var addresses = GetLocalIpAddressesInternal(ignoreVirtualInterface).Result.Select(ToIpAddressInfo).ToArray();
2016-10-29 05:40:15 +00:00
_localIpAddresses = addresses;
return addresses;
}
return _localIpAddresses;
2016-10-29 05:40:15 +00:00
}
}
private async Task<List<IPAddress>> GetLocalIpAddressesInternal(bool ignoreVirtualInterface)
2016-10-29 05:40:15 +00:00
{
var list = GetIPsDefault(ignoreVirtualInterface)
2016-10-29 05:40:15 +00:00
.ToList();
if (list.Count == 0)
{
2017-11-23 15:46:16 +00:00
list.AddRange(await GetLocalIpAddressesFallback().ConfigureAwait(false));
2016-10-29 05:40:15 +00:00
}
2017-11-10 21:22:38 +00:00
var listClone = list.ToList();
return list
.OrderBy(i => i.AddressFamily == AddressFamily.InterNetwork ? 0 : 1)
.ThenBy(i => listClone.IndexOf(i))
.Where(FilterIpAddress)
2019-02-02 11:27:06 +00:00
.GroupBy(i => i.ToString())
.Select(x => x.First())
2017-11-23 15:46:16 +00:00
.ToList();
2016-10-29 05:40:15 +00:00
}
private static bool FilterIpAddress(IPAddress address)
2016-10-29 05:40:15 +00:00
{
2016-11-04 23:57:21 +00:00
var addressString = address.ToString();
2016-10-29 05:40:15 +00:00
2016-11-04 23:57:21 +00:00
if (addressString.StartsWith("169.", StringComparison.OrdinalIgnoreCase))
2016-10-29 05:40:15 +00:00
{
return false;
}
return true;
}
public bool IsInPrivateAddressSpace(string endpoint)
2018-09-12 17:26:21 +00:00
{
return IsInPrivateAddressSpace(endpoint, true);
}
private bool IsInPrivateAddressSpace(string endpoint, bool checkSubnets)
2016-10-29 05:40:15 +00:00
{
if (string.Equals(endpoint, "::1", StringComparison.OrdinalIgnoreCase))
{
return true;
}
// ipv6
if (endpoint.Split('.').Length > 4)
{
// Handle ipv4 mapped to ipv6
var originalEndpoint = endpoint;
endpoint = endpoint.Replace("::ffff:", string.Empty);
if (string.Equals(endpoint, originalEndpoint, StringComparison.OrdinalIgnoreCase))
{
return false;
}
}
2016-10-29 05:40:15 +00:00
// Private address space:
// http://en.wikipedia.org/wiki/Private_network
if (endpoint.StartsWith("172.", StringComparison.OrdinalIgnoreCase))
{
return Is172AddressPrivate(endpoint);
}
2018-09-12 17:26:21 +00:00
if (endpoint.StartsWith("localhost", StringComparison.OrdinalIgnoreCase) ||
2017-10-27 03:05:29 +00:00
endpoint.StartsWith("127.", StringComparison.OrdinalIgnoreCase) ||
2018-09-12 17:26:21 +00:00
endpoint.StartsWith("169.", StringComparison.OrdinalIgnoreCase))
{
return true;
}
if (checkSubnets && endpoint.StartsWith("192.168", StringComparison.OrdinalIgnoreCase))
{
return true;
}
if (checkSubnets && IsInPrivateAddressSpaceAndLocalSubnet(endpoint))
{
return true;
}
return false;
}
public bool IsInPrivateAddressSpaceAndLocalSubnet(string endpoint)
{
2017-10-29 03:52:05 +00:00
if (endpoint.StartsWith("10.", StringComparison.OrdinalIgnoreCase))
{
2017-11-10 21:22:38 +00:00
var endpointFirstPart = endpoint.Split('.')[0];
2017-10-27 04:14:45 +00:00
var subnets = GetSubnets(endpointFirstPart);
2017-10-27 04:14:45 +00:00
foreach (var subnet_Match in subnets)
{
//logger.LogDebug("subnet_Match:" + subnet_Match);
2017-10-27 03:43:06 +00:00
2017-10-27 04:14:45 +00:00
if (endpoint.StartsWith(subnet_Match + ".", StringComparison.OrdinalIgnoreCase))
{
return true;
2017-10-27 03:43:06 +00:00
}
}
}
2017-10-27 03:43:06 +00:00
return false;
2016-10-29 05:40:15 +00:00
}
2017-10-27 04:14:45 +00:00
private Dictionary<string, List<string>> _subnetLookup = new Dictionary<string, List<string>>(StringComparer.Ordinal);
private List<string> GetSubnets(string endpointFirstPart)
{
lock (_subnetLookup)
{
if (_subnetLookup.TryGetValue(endpointFirstPart, out var subnets))
{
2017-10-27 04:14:45 +00:00
return subnets;
}
2017-10-27 04:14:45 +00:00
subnets = new List<string>();
2019-01-13 20:37:13 +00:00
foreach (var adapter in NetworkInterface.GetAllNetworkInterfaces())
2017-10-27 04:14:45 +00:00
{
2019-01-13 20:37:13 +00:00
foreach (var unicastIPAddressInformation in adapter.GetIPProperties().UnicastAddresses)
2017-10-27 04:14:45 +00:00
{
if (unicastIPAddressInformation.Address.AddressFamily == AddressFamily.InterNetwork && endpointFirstPart == unicastIPAddressInformation.Address.ToString().Split('.')[0])
{
int subnet_Test = 0;
foreach (string part in unicastIPAddressInformation.IPv4Mask.ToString().Split('.'))
{
if (part.Equals("0")) break;
subnet_Test++;
}
var subnet_Match = string.Join(".", unicastIPAddressInformation.Address.ToString().Split('.').Take(subnet_Test).ToArray());
2017-10-27 04:14:45 +00:00
// TODO: Is this check necessary?
if (adapter.OperationalStatus == OperationalStatus.Up)
{
subnets.Add(subnet_Match);
}
}
}
}
2017-10-27 04:14:45 +00:00
_subnetLookup[endpointFirstPart] = subnets;
return subnets;
}
}
private static bool Is172AddressPrivate(string endpoint)
2016-10-29 05:40:15 +00:00
{
for (var i = 16; i <= 31; i++)
{
if (endpoint.StartsWith("172." + i.ToString(CultureInfo.InvariantCulture) + ".", StringComparison.OrdinalIgnoreCase))
{
return true;
}
}
return false;
}
public bool IsInLocalNetwork(string endpoint)
{
return IsInLocalNetworkInternal(endpoint, true);
}
2018-09-12 17:26:21 +00:00
public bool IsAddressInSubnets(string addressString, string[] subnets)
2016-10-29 05:40:15 +00:00
{
2018-09-12 17:26:21 +00:00
return IsAddressInSubnets(IPAddress.Parse(addressString), addressString, subnets);
}
private static bool IsAddressInSubnets(IPAddress address, string addressString, string[] subnets)
2018-09-12 17:26:21 +00:00
{
foreach (var subnet in subnets)
{
var normalizedSubnet = subnet.Trim();
if (string.Equals(normalizedSubnet, addressString, StringComparison.OrdinalIgnoreCase))
{
return true;
}
if (normalizedSubnet.IndexOf('/') != -1)
{
var ipnetwork = IPNetwork.IPNetwork.Parse(normalizedSubnet);
2018-09-12 17:26:21 +00:00
if (ipnetwork.Contains(address))
{
return true;
}
}
}
return false;
}
private bool IsInLocalNetworkInternal(string endpoint, bool resolveHost)
{
if (string.IsNullOrEmpty(endpoint))
2016-10-29 05:40:15 +00:00
{
throw new ArgumentNullException(nameof(endpoint));
2016-10-29 05:40:15 +00:00
}
if (IPAddress.TryParse(endpoint, out var address))
2016-10-29 05:40:15 +00:00
{
var addressString = address.ToString();
2018-09-12 17:26:21 +00:00
var localSubnetsFn = LocalSubnetsFn;
if (localSubnetsFn != null)
{
var localSubnets = localSubnetsFn();
foreach (var subnet in localSubnets)
{
// only validate if there's at least one valid entry
if (!string.IsNullOrWhiteSpace(subnet))
{
return IsAddressInSubnets(address, addressString, localSubnets) || IsInPrivateAddressSpace(addressString, false);
}
}
}
2016-10-29 05:40:15 +00:00
int lengthMatch = 100;
if (address.AddressFamily == AddressFamily.InterNetwork)
{
lengthMatch = 4;
2018-09-12 17:26:21 +00:00
if (IsInPrivateAddressSpace(addressString, true))
2016-10-29 05:40:15 +00:00
{
return true;
}
}
else if (address.AddressFamily == AddressFamily.InterNetworkV6)
{
2017-11-10 21:22:38 +00:00
lengthMatch = 9;
2018-09-12 17:26:21 +00:00
if (IsInPrivateAddressSpace(endpoint, true))
2016-10-29 05:40:15 +00:00
{
return true;
}
}
// Should be even be doing this with ipv6?
if (addressString.Length >= lengthMatch)
{
var prefix = addressString.Substring(0, lengthMatch);
2016-11-04 23:57:21 +00:00
if (GetLocalIpAddresses().Any(i => i.ToString().StartsWith(prefix, StringComparison.OrdinalIgnoreCase)))
2016-10-29 05:40:15 +00:00
{
return true;
}
}
2016-11-04 23:57:21 +00:00
}
2016-10-29 05:40:15 +00:00
else if (resolveHost)
{
if (Uri.TryCreate(endpoint, UriKind.RelativeOrAbsolute, out var uri))
2016-10-29 05:40:15 +00:00
{
try
{
var host = uri.DnsSafeHost;
Logger.LogDebug("Resolving host {0}", host);
2016-10-29 05:40:15 +00:00
2016-10-29 18:46:14 +00:00
address = GetIpAddresses(host).Result.FirstOrDefault();
2016-10-29 05:40:15 +00:00
if (address != null)
{
Logger.LogDebug("{0} resolved to {1}", host, address);
2016-10-29 05:40:15 +00:00
return IsInLocalNetworkInternal(address.ToString(), false);
}
}
catch (InvalidOperationException)
{
// Can happen with reverse proxy or IIS url rewriting
}
catch (Exception ex)
{
2018-12-20 12:11:26 +00:00
Logger.LogError(ex, "Error resolving hostname");
2016-10-29 05:40:15 +00:00
}
}
}
return false;
}
private static Task<IPAddress[]> GetIpAddresses(string hostName)
2016-10-29 05:40:15 +00:00
{
2016-10-29 18:46:14 +00:00
return Dns.GetHostAddressesAsync(hostName);
2016-10-29 05:40:15 +00:00
}
private List<IPAddress> GetIPsDefault(bool ignoreVirtualInterface)
2016-11-04 23:57:21 +00:00
{
NetworkInterface[] interfaces;
2016-10-29 05:40:15 +00:00
2016-11-04 23:57:21 +00:00
try
{
var validStatuses = new[] { OperationalStatus.Up, OperationalStatus.Unknown };
2016-10-29 05:40:15 +00:00
2016-11-04 23:57:21 +00:00
interfaces = NetworkInterface.GetAllNetworkInterfaces()
.Where(i => validStatuses.Contains(i.OperationalStatus))
.ToArray();
}
catch (Exception ex)
{
2018-12-20 12:11:26 +00:00
Logger.LogError(ex, "Error in GetAllNetworkInterfaces");
2016-11-04 23:57:21 +00:00
return new List<IPAddress>();
}
return interfaces.SelectMany(network =>
{
2016-10-29 05:40:15 +00:00
2016-11-04 23:57:21 +00:00
try
{
2017-10-05 18:10:46 +00:00
// suppress logging because it might be causing nas device wake up
//logger.LogDebug("Querying interface: {0}. Type: {1}. Status: {2}", network.Name, network.NetworkInterfaceType, network.OperationalStatus);
2016-10-29 05:40:15 +00:00
2016-12-12 21:33:37 +00:00
var ipProperties = network.GetIPProperties();
// Try to exclude virtual adapters
// http://stackoverflow.com/questions/8089685/c-sharp-finding-my-machines-local-ip-address-and-not-the-vms
var addr = ipProperties.GatewayAddresses.FirstOrDefault();
if (addr == null || (ignoreVirtualInterface && string.Equals(addr.Address.ToString(), "0.0.0.0", StringComparison.OrdinalIgnoreCase)))
2016-12-12 21:33:37 +00:00
{
return new List<IPAddress>();
}
return ipProperties.UnicastAddresses
2016-10-29 05:40:15 +00:00
.Select(i => i.Address)
2017-11-10 21:22:38 +00:00
.Where(i => i.AddressFamily == AddressFamily.InterNetwork || i.AddressFamily == AddressFamily.InterNetworkV6)
2016-11-04 23:57:21 +00:00
.ToList();
}
catch (Exception ex)
{
2018-12-20 12:11:26 +00:00
Logger.LogError(ex, "Error querying network interface");
2016-11-04 23:57:21 +00:00
return new List<IPAddress>();
}
2019-02-02 11:27:06 +00:00
}).GroupBy(i => i.ToString())
.Select(x => x.First())
2016-11-04 23:57:21 +00:00
.ToList();
}
private static async Task<IEnumerable<IPAddress>> GetLocalIpAddressesFallback()
2016-10-29 05:40:15 +00:00
{
2016-10-29 18:46:14 +00:00
var host = await Dns.GetHostEntryAsync(Dns.GetHostName()).ConfigureAwait(false);
2016-10-29 05:40:15 +00:00
// Reverse them because the last one is usually the correct one
// It's not fool-proof so ultimately the consumer will have to examine them and decide
return host.AddressList
2017-11-10 21:22:38 +00:00
.Where(i => i.AddressFamily == AddressFamily.InterNetwork || i.AddressFamily == AddressFamily.InterNetworkV6)
2016-10-29 05:40:15 +00:00
.Reverse();
}
/// <summary>
/// Gets a random port number that is currently available
/// </summary>
/// <returns>System.Int32.</returns>
2017-03-03 04:36:20 +00:00
public int GetRandomUnusedTcpPort()
2016-10-29 05:40:15 +00:00
{
var listener = new TcpListener(IPAddress.Any, 0);
listener.Start();
var port = ((IPEndPoint)listener.LocalEndpoint).Port;
listener.Stop();
return port;
}
2017-03-03 04:36:20 +00:00
public int GetRandomUnusedUdpPort()
{
2019-01-13 20:37:13 +00:00
var localEndPoint = new IPEndPoint(IPAddress.Any, 0);
2017-03-03 04:36:20 +00:00
using (var udpClient = new UdpClient(localEndPoint))
{
var port = ((IPEndPoint)(udpClient.Client.LocalEndPoint)).Port;
return port;
}
}
2018-09-12 17:26:21 +00:00
private List<string> _macAddresses;
public List<string> GetMacAddresses()
{
if (_macAddresses == null)
{
_macAddresses = GetMacAddressesInternal();
}
return _macAddresses;
}
private static List<string> GetMacAddressesInternal()
2016-10-29 05:40:15 +00:00
{
return NetworkInterface.GetAllNetworkInterfaces()
.Where(i => i.NetworkInterfaceType != NetworkInterfaceType.Loopback)
2018-09-12 17:26:21 +00:00
.Select(i =>
{
try
{
var physicalAddress = i.GetPhysicalAddress();
if (physicalAddress == null)
{
return null;
}
return physicalAddress.ToString();
}
catch (Exception)
2018-09-12 17:26:21 +00:00
{
//TODO Log exception.
2018-09-12 17:26:21 +00:00
return null;
}
})
.Where(i => i != null)
.ToList();
2016-10-29 05:40:15 +00:00
}
/// <summary>
/// Parses the specified endpointstring.
/// </summary>
/// <param name="endpointstring">The endpointstring.</param>
/// <returns>IPEndPoint.</returns>
public IPEndPoint Parse(string endpointstring)
{
2016-10-29 18:46:14 +00:00
return Parse(endpointstring, -1).Result;
2016-10-29 05:40:15 +00:00
}
/// <summary>
/// Parses the specified endpointstring.
/// </summary>
/// <param name="endpointstring">The endpointstring.</param>
/// <param name="defaultport">The defaultport.</param>
/// <returns>IPEndPoint.</returns>
2019-01-13 20:37:13 +00:00
/// <exception cref="ArgumentException">Endpoint descriptor may not be empty.</exception>
/// <exception cref="FormatException"></exception>
2016-10-29 18:46:14 +00:00
private static async Task<IPEndPoint> Parse(string endpointstring, int defaultport)
2016-10-29 05:40:15 +00:00
{
if (string.IsNullOrEmpty(endpointstring)
2016-10-29 05:40:15 +00:00
|| endpointstring.Trim().Length == 0)
{
throw new ArgumentException("Endpoint descriptor may not be empty.");
}
if (defaultport != -1 &&
(defaultport < IPEndPoint.MinPort
|| defaultport > IPEndPoint.MaxPort))
{
throw new ArgumentException(string.Format("Invalid default port '{0}'", defaultport));
2016-10-29 05:40:15 +00:00
}
string[] values = endpointstring.Split(new char[] { ':' });
IPAddress ipaddy;
int port = -1;
2016-11-04 23:57:21 +00:00
2016-10-29 05:40:15 +00:00
//check if we have an IPv6 or ports
if (values.Length <= 2) // ipv4 or hostname
{
port = values.Length == 1 ? defaultport : GetPort(values[1]);
//try to use the address as IPv4, otherwise get hostname
if (!IPAddress.TryParse(values[0], out ipaddy))
2016-10-29 18:46:14 +00:00
ipaddy = await GetIPfromHost(values[0]).ConfigureAwait(false);
2016-10-29 05:40:15 +00:00
}
else if (values.Length > 2) //ipv6
{
//could [a:b:c]:d
if (values[0].StartsWith("[") && values[values.Length - 2].EndsWith("]"))
{
string ipaddressstring = string.Join(":", values.Take(values.Length - 1).ToArray());
2016-10-29 05:40:15 +00:00
ipaddy = IPAddress.Parse(ipaddressstring);
port = GetPort(values[values.Length - 1]);
}
else //[a:b:c] or a:b:c
{
ipaddy = IPAddress.Parse(endpointstring);
port = defaultport;
}
}
else
{
throw new FormatException(string.Format("Invalid endpoint ipaddress '{0}'", endpointstring));
2016-10-29 05:40:15 +00:00
}
if (port == -1)
throw new ArgumentException(string.Format("No port specified: '{0}'", endpointstring));
2016-10-29 05:40:15 +00:00
return new IPEndPoint(ipaddy, port);
}
protected static readonly CultureInfo UsCulture = new CultureInfo("en-US");
/// <summary>
/// Gets the port.
/// </summary>
/// <param name="p">The p.</param>
/// <returns>System.Int32.</returns>
2019-01-13 20:37:13 +00:00
/// <exception cref="FormatException"></exception>
2016-10-29 05:40:15 +00:00
private static int GetPort(string p)
{
if (!int.TryParse(p, out var port)
2016-10-29 05:40:15 +00:00
|| port < IPEndPoint.MinPort
|| port > IPEndPoint.MaxPort)
{
throw new FormatException(string.Format("Invalid end point port '{0}'", p));
2016-10-29 05:40:15 +00:00
}
return port;
}
/// <summary>
/// Gets the I pfrom host.
/// </summary>
/// <param name="p">The p.</param>
/// <returns>IPAddress.</returns>
2019-01-13 20:37:13 +00:00
/// <exception cref="ArgumentException"></exception>
2016-10-29 18:46:14 +00:00
private static async Task<IPAddress> GetIPfromHost(string p)
2016-10-29 05:40:15 +00:00
{
2016-10-29 18:46:14 +00:00
var hosts = await Dns.GetHostAddressesAsync(p).ConfigureAwait(false);
2016-10-29 05:40:15 +00:00
if (hosts == null || hosts.Length == 0)
throw new ArgumentException(string.Format("Host not found: {0}", p));
2016-10-29 05:40:15 +00:00
return hosts[0];
}
2016-11-04 18:56:47 +00:00
public IpAddressInfo ParseIpAddress(string ipAddress)
{
if (TryParseIpAddress(ipAddress, out var info))
2016-11-04 18:56:47 +00:00
{
return info;
}
throw new ArgumentException("Invalid ip address: " + ipAddress);
}
public bool TryParseIpAddress(string ipAddress, out IpAddressInfo ipAddressInfo)
{
if (IPAddress.TryParse(ipAddress, out var address))
2016-11-04 18:56:47 +00:00
{
2016-11-08 18:44:23 +00:00
ipAddressInfo = ToIpAddressInfo(address);
2016-11-04 18:56:47 +00:00
return true;
}
ipAddressInfo = null;
return false;
}
2016-11-08 18:44:23 +00:00
public static IpEndPointInfo ToIpEndPointInfo(IPEndPoint endpoint)
{
if (endpoint == null)
{
return null;
}
return new IpEndPointInfo(ToIpAddressInfo(endpoint.Address), endpoint.Port);
}
public static IPEndPoint ToIPEndPoint(IpEndPointInfo endpoint)
{
if (endpoint == null)
{
return null;
}
return new IPEndPoint(ToIPAddress(endpoint.IpAddress), endpoint.Port);
}
public static IPAddress ToIPAddress(IpAddressInfo address)
{
if (address.Equals(IpAddressInfo.Any))
{
return IPAddress.Any;
}
if (address.Equals(IpAddressInfo.IPv6Any))
{
return IPAddress.IPv6Any;
}
if (address.Equals(IpAddressInfo.Loopback))
{
return IPAddress.Loopback;
}
if (address.Equals(IpAddressInfo.IPv6Loopback))
{
return IPAddress.IPv6Loopback;
}
return IPAddress.Parse(address.Address);
}
public static IpAddressInfo ToIpAddressInfo(IPAddress address)
{
if (address.Equals(IPAddress.Any))
{
return IpAddressInfo.Any;
}
if (address.Equals(IPAddress.IPv6Any))
{
return IpAddressInfo.IPv6Any;
}
if (address.Equals(IPAddress.Loopback))
{
return IpAddressInfo.Loopback;
}
if (address.Equals(IPAddress.IPv6Loopback))
{
return IpAddressInfo.IPv6Loopback;
}
return new IpAddressInfo(address.ToString(), address.AddressFamily == AddressFamily.InterNetworkV6 ? IpAddressFamily.InterNetworkV6 : IpAddressFamily.InterNetwork);
2016-11-08 18:44:23 +00:00
}
public async Task<IpAddressInfo[]> GetHostAddressesAsync(string host)
{
var addresses = await Dns.GetHostAddressesAsync(host).ConfigureAwait(false);
2018-12-28 15:48:26 +00:00
return addresses.Select(ToIpAddressInfo).ToArray();
2016-11-08 18:44:23 +00:00
}
2016-11-11 07:24:36 +00:00
/// <summary>
/// Gets the network shares.
/// </summary>
/// <param name="path">The path.</param>
/// <returns>IEnumerable{NetworkShare}.</returns>
2016-11-16 20:52:17 +00:00
public virtual IEnumerable<NetworkShare> GetNetworkShares(string path)
2016-11-11 07:24:36 +00:00
{
return new List<NetworkShare>();
}
/// <summary>
/// Gets available devices within the domain
/// </summary>
/// <returns>PC's in the Domain</returns>
2016-11-16 20:52:17 +00:00
public virtual IEnumerable<FileSystemEntryInfo> GetNetworkDevices()
2016-11-11 07:24:36 +00:00
{
return new List<FileSystemEntryInfo>();
}
2016-10-29 05:40:15 +00:00
}
}