123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
-
- #region
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Net.NetworkInformation;
- using System.Text.RegularExpressions;
- using System.Threading;
- #endregion
- namespace CassiniDev
- {
- public static class CassiniNetworkUtils
- {
- public static IPAddress[] GetLocalAddresses()
- {
- string strHostName = Dns.GetHostName();
- IPHostEntry ipEntry = Dns.GetHostEntry(strHostName);
- return ipEntry.AddressList;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
- public static int GetAvailablePort(int rangeStart, int rangeEnd, IPAddress ip, bool includeIdlePorts)
- {
- IPGlobalProperties ipProps = IPGlobalProperties.GetIPGlobalProperties();
-
- Func<IPAddress, bool> isIpAnyOrLoopBack = i => IPAddress.Any.Equals(i) ||
- IPAddress.IPv6Any.Equals(i) ||
- IPAddress.Loopback.Equals(i) ||
- IPAddress.IPv6Loopback.
- Equals(i);
-
- List<ushort> excludedPorts = new List<ushort>();
-
- excludedPorts.AddRange(from n in ipProps.GetActiveTcpConnections()
- where
- n.LocalEndPoint.Port >= rangeStart &&
- n.LocalEndPoint.Port <= rangeEnd && (
- isIpAnyOrLoopBack(ip) ||
- n.LocalEndPoint.Address.Equals(ip) ||
- isIpAnyOrLoopBack(n.LocalEndPoint.Address)) &&
- (!includeIdlePorts || n.State != TcpState.TimeWait)
- select (ushort) n.LocalEndPoint.Port);
- excludedPorts.AddRange(from n in ipProps.GetActiveTcpListeners()
- where n.Port >= rangeStart && n.Port <= rangeEnd && (
- isIpAnyOrLoopBack(ip) ||
- n.Address.Equals(ip) ||
- isIpAnyOrLoopBack(n.Address))
- select (ushort) n.Port);
- excludedPorts.AddRange(from n in ipProps.GetActiveUdpListeners()
- where n.Port >= rangeStart && n.Port <= rangeEnd && (
- isIpAnyOrLoopBack(ip) ||
- n.Address.Equals(ip) ||
- isIpAnyOrLoopBack(n.Address))
- select (ushort) n.Port);
- excludedPorts.Sort();
- for (int port = rangeStart; port <= rangeEnd; port++)
- {
- if (!excludedPorts.Contains((ushort) port))
- {
- return port;
- }
- }
- return 0;
- }
-
-
-
-
-
- public static IPAddress GetExternalIPV4()
- {
- return GetIPAdresses().ToList()
- .FirstOrDefault(i => i.ToString().IndexOf(":") == -1);
- }
-
-
-
- public static string GetHostName()
- {
- return Dns.GetHostName();
- }
-
-
-
- public static IPAddress[] GetIPAdresses()
- {
- return Dns.GetHostEntry(GetHostName()).AddressList;
- }
-
-
-
-
-
- public static bool IsPortAvailable(IPAddress ipAddress, int port)
- {
- bool portAvailable = false;
- for (int i = 0; i < 5; i++)
- {
- portAvailable = GetAvailablePort(port, port, ipAddress, true) == port;
- if (portAvailable)
- {
- break;
- }
-
-
- Thread.Sleep(100);
- }
- return portAvailable;
- }
-
-
-
-
-
-
-
- public static string NormalizeUrl(string rootUrl, string relativeUrl)
- {
- relativeUrl = relativeUrl.TrimStart('/');
- if (!rootUrl.EndsWith("/"))
- {
- rootUrl += "/";
- }
- return new Uri(rootUrl + relativeUrl).ToString();
- }
-
-
-
-
- public static IPAddress ParseIPString(string ipString)
- {
- if (string.IsNullOrEmpty(ipString))
- {
- ipString = "loopback";
- }
- ipString = ipString.Trim().ToLower();
- switch (ipString)
- {
- case "any":
- return IPAddress.Any;
- case "loopback":
- return IPAddress.Loopback;
- case "ipv6any":
- return IPAddress.IPv6Any;
- case "ipv6loopback":
- return IPAddress.IPv6Loopback;
- default:
- IPAddress result;
- IPAddress.TryParse(ipString, out result);
- return result;
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public static bool ValidateHostName(string hostname)
- {
- Regex hostnameRx =
- new Regex(
- @"^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])$");
- return hostnameRx.IsMatch(hostname);
- }
- }
- }
|