public bool Check_IpRange(string ip) { bool flag = false; try { IPAddress cip = IPAddress.Parse(ip); IPAddressRange ipr_local_v4 = new IPAddressRange(IPAddress.Parse("127.0.0.0"), IPAddress.Parse("127.0.0.255")); IPAddressRange ipr_local_v6 = new IPAddressRange(IPAddress.Parse("::1"), IPAddress.Parse("::1")); IPAddressRange ipr_nicnet_local = new IPAddressRange(IPAddress.Parse("10.0.0.0"), IPAddress.Parse("10.255.255.255")); IPAddressRange ipr_nicnet_public = new IPAddressRange(IPAddress.Parse("164.100.0.0"), IPAddress.Parse("164.100.255.255")); if (ipr_local_v4.IsInRange(cip)) { flag = true; } else if (ipr_local_v6.IsInRange(cip)) { flag = true; } else if (ipr_nicnet_local.IsInRange(cip)) { flag = true; } else if (ipr_nicnet_public.IsInRange(cip)) { flag = true; } } catch { } return(flag); }
public static string GetIPClassFamily2(string ipAddress) { if (string.IsNullOrEmpty(ipAddress)) return "unknown"; IPAddressRange range = new IPAddressRange(IPAddress.Parse("0.0.0.0"), IPAddress.Parse("127.255.255.255")); if (range.IsInRange(IPAddress.Parse(ipAddress))) { return "A"; } else { range = new IPAddressRange(IPAddress.Parse("128.0.0.0"), IPAddress.Parse("191.255.255.255")); if (range.IsInRange(IPAddress.Parse(ipAddress))) { return "B"; } else { range = new IPAddressRange(IPAddress.Parse("192.0.0.0"), IPAddress.Parse("223.255.255.255")); if (range.IsInRange(IPAddress.Parse(ipAddress))) { return "C"; } else { return "unknown"; } } } }
public void IsInRange_Fail() { //arrange IPAddressRange range = new IPAddressRange(IPAddress.Parse("85.255.19.0"), IPAddress.Parse("85.255.19.240")); //action bool actual = range.IsInRange(IPAddress.Parse("85.255.19.243")); //assert Assert.IsFalse(actual); }
public void IsInRange_Success() { //arrange IPAddressRange range = new IPAddressRange(IPAddress.Parse("85.255.19.0"), IPAddress.Parse("85.255.19.255")); //action bool actual = range.IsInRange(IPAddress.Parse("85.255.19.3")); //assert Assert.IsTrue(actual); }
public void InRange(string ipV4string, bool isExpectedInRange) { IPAddress lower = IPAddress.Parse("192.168.4.10"); IPAddress upper = IPAddress.Parse("192.168.4.20"); IPAddressRange range = new IPAddressRange(lower, upper); IPAddress testCase = IPAddress.Parse(ipV4string); Assert.AreEqual(isExpectedInRange, range.IsInRange(testCase)); }