mirror of https://github.com/ogoun/Zero.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
93 lines
3.5 KiB
93 lines
3.5 KiB
using System;
|
|
using System.Globalization;
|
|
using System.Net;
|
|
using System.Net.NetworkInformation;
|
|
using System.Net.Sockets;
|
|
|
|
namespace ZeroLevel.Network
|
|
{
|
|
public static class NetUtils
|
|
{
|
|
public static int Compare(this IPEndPoint x, IPEndPoint y)
|
|
{
|
|
var xx = x.Address.ToString();
|
|
var yy = y.Address.ToString();
|
|
var result = string.CompareOrdinal(xx, yy);
|
|
return result == 0 ? x.Port.CompareTo(y.Port) : result;
|
|
}
|
|
|
|
public static int Compare(this IPAddress x, IPAddress y)
|
|
{
|
|
var xx = x.ToString();
|
|
var yy = y.ToString();
|
|
return string.CompareOrdinal(xx, yy);
|
|
}
|
|
|
|
public static IPEndPoint CreateIPEndPoint(string endPoint)
|
|
{
|
|
string[] ep = endPoint.Split(':');
|
|
if (ep.Length < 2) throw new FormatException("Invalid endpoint format");
|
|
IPAddress ip;
|
|
if (ep.Length > 2)
|
|
{
|
|
if (!IPAddress.TryParse(string.Join(":", ep, 0, ep.Length - 1), out ip))
|
|
{
|
|
throw new FormatException("Invalid ip-adress");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (!IPAddress.TryParse(ep[0], out ip))
|
|
{
|
|
throw new FormatException("Invalid ip-adress");
|
|
}
|
|
}
|
|
int port;
|
|
if (!int.TryParse(ep[ep.Length - 1], NumberStyles.None, NumberFormatInfo.CurrentInfo, out port))
|
|
{
|
|
throw new FormatException("Invalid port");
|
|
}
|
|
return new IPEndPoint(ip, port);
|
|
}
|
|
|
|
public static int GetFreeTcpPort()
|
|
{
|
|
TcpListener l = new TcpListener(IPAddress.Loopback, 0);
|
|
l.Start();
|
|
int port = ((IPEndPoint)l.LocalEndpoint).Port;
|
|
l.Stop();
|
|
l = null;
|
|
return port;
|
|
}
|
|
|
|
public static IPAddress GetNonLoopbackAddress(bool ignore_virtual_devices = true, bool ignore_docker_devices = true)
|
|
{
|
|
if (NetworkInterface.GetIsNetworkAvailable())
|
|
{
|
|
foreach (var adapter in NetworkInterface.GetAllNetworkInterfaces())
|
|
{
|
|
if (adapter.OperationalStatus == OperationalStatus.Up)
|
|
{
|
|
if (ignore_virtual_devices && (adapter.Description.IndexOf("Virtual", StringComparison.OrdinalIgnoreCase) >= 0 || adapter.Name.IndexOf("Virtual", StringComparison.OrdinalIgnoreCase) >= 0))
|
|
continue;
|
|
if (ignore_docker_devices && (adapter.Description.IndexOf("Docker", StringComparison.OrdinalIgnoreCase) >= 0 || adapter.Name.IndexOf("Docker", StringComparison.OrdinalIgnoreCase) >= 0))
|
|
continue;
|
|
if (adapter.NetworkInterfaceType != NetworkInterfaceType.Ethernet)
|
|
continue;
|
|
foreach (UnicastIPAddressInformation address in adapter.GetIPProperties().UnicastAddresses)
|
|
{
|
|
if (address.Address.AddressFamily == AddressFamily.InterNetwork)
|
|
{
|
|
if (!IPAddress.IsLoopback(address.Address))
|
|
{
|
|
return address.Address;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
} |