// Spludlow Software // Copyright © Samuel P. Ludlow 2020 All Rights Reserved // Distributed under the terms of the GNU General Public License version 3 // Distributed WITHOUT ANY WARRANTY; without implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE // https://www.spludlow.co.uk/LICENCE.TXT // The Spludlow logo is a registered trademark of Samuel P. Ludlow and may not be used without permission // v1.14 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; //using System.Net; using System.Net.NetworkInformation; using System.Net.Sockets; //using System.Management; using System.Net; namespace Spludlow.Net { public class IP { public static string[] FilterPublic(string[] addresses) { List result = new List(); foreach (string address in addresses) { if (IsPrivate(address) == false) result.Add(address); } return result.ToArray(); } public static string[] FilterPrivate(string[] addresses) { return FilterPrivate(addresses, null); } public static string[] FilterPrivate(string[] addresses, string sameSubnetHostAddress) { List result = new List(); foreach (string address in addresses) { string netClass = NetworkClass(address); if (netClass != "A" && netClass != "B" && netClass != "C") continue; if (sameSubnetHostAddress != null) { if (address.StartsWith(PrivateSubnetStart(sameSubnetHostAddress)) == false) continue; } result.Add(address); } return result.ToArray(); } public static bool IsPrivate(string address) { if (NetworkClass(address) == "I") return false; return true; } public static string NetworkClass(string address) { if (address == "127.0.0.1") return "L"; if (address.StartsWith("10.") == true) return "A"; if (address.CompareTo("172.16.") > 0 && address.CompareTo("172.32.") < 0) return "B"; if (address.StartsWith("192.168.") == true) return "C"; return "I"; } public static string PrivateSubnetStart(string address) { switch (NetworkClass(address)) { case "A": return address.Substring(0, 3); case "B": return address.Substring(0, 7); case "C": int index = address.LastIndexOf("."); return address.Substring(0, index + 1); } throw new ApplicationException("Not a private IP Address: " + address); } public static string[] UnicastAddresses(string host) { return (string[])Spludlow.Call.Now(host, "Spludlow", "Spludlow.Net.IP", "UnicastAddresses"); } public static string[] UnicastAddresses() { List addresses = new List(); foreach (NetworkInterface networkInterface in NetworkInterface.GetAllNetworkInterfaces()) { if (networkInterface.OperationalStatus != OperationalStatus.Up) continue; IPInterfaceProperties interfaceProperties = networkInterface.GetIPProperties(); foreach (UnicastIPAddressInformation addressInfo in interfaceProperties.UnicastAddresses) { if (addressInfo.Address.AddressFamily != AddressFamily.InterNetwork) continue; addresses.Add(addressInfo.Address.ToString()); } } return addresses.ToArray(); } public static uint IPAddressToInt(string ipV4address) { return BitConverter.ToUInt32(IPAddress.Parse(ipV4address).GetAddressBytes().Reverse().ToArray(), 0); } public static string IntToIPAddress(uint ipV4integer) { return IPAddress.Parse(ipV4integer.ToString()).ToString(); } public static string[] IPRangeFromCIDR(string cidr) { string[] parts = cidr.Split(new char[] { '/' }); if (parts.Length != 2) throw new ApplicationException("IPRangeFromCIDR; Did not spit in 2 parts: " + cidr); string networkText = parts[0].Trim(); string maskText = parts[1].Trim(); int dotCount = 0; foreach (char ch in networkText) { if (ch == '.') ++dotCount; } while (dotCount < 3) { networkText += ".0"; ++dotCount; } uint network = IPAddressToInt(networkText); uint mask = ~(0xFFFFFFFF >> Int32.Parse(maskText)); uint start = network & mask; uint end = (network & mask) | ~mask; return new string[] { IntToIPAddress(start), IntToIPAddress(end) }; } } }