public static string ConvertCidrToSubnetmask(int cidr) { var bits = string.Empty; for (var i = 0; i < cidr; i++) { bits += "1"; } return(IPv4Address.ToHumanString(bits.PadRight(32, '0'))); }
public static List <ARPInfo> GetTable() { var list = new List <ARPInfo>(); // The number of bytes needed. var bytesNeeded = 0; // The result from the API call. var result = GetIpNetTable(IntPtr.Zero, ref bytesNeeded, false); // Call the function, expecting an insufficient buffer. if (result != ERROR_INSUFFICIENT_BUFFER) { // Throw an exception. throw new Win32Exception(result); } // Allocate the memory, do it in a try/finally block, to ensure // that it is released. var buffer = IntPtr.Zero; // Try/finally. try { // Allocate the memory. buffer = Marshal.AllocCoTaskMem(bytesNeeded); // Make the call again. If it did not succeed, then // raise an error. result = GetIpNetTable(buffer, ref bytesNeeded, false); // If the result is not 0 (no error), then throw an exception. if (result != 0) { // Throw an exception. throw new Win32Exception(result); } // Now we have the buffer, we have to marshal it. We can read // the first 4 bytes to get the length of the buffer. var entries = Marshal.ReadInt32(buffer); // Increment the memory pointer by the size of the int. var currentBuffer = new IntPtr(buffer.ToInt64() + Marshal.SizeOf(typeof(int))); // Allocate an array of entries. var table = new MIB_IPNETROW[entries]; // Cycle through the entries. for (var i = 0; i < entries; i++) { // Call PtrToStructure, getting the structure information. table[i] = (MIB_IPNETROW)Marshal.PtrToStructure(new IntPtr(currentBuffer.ToInt64() + (i * Marshal.SizeOf(typeof(MIB_IPNETROW)))), typeof(MIB_IPNETROW)); } var virtualMAC = new PhysicalAddress(new byte[] { 0, 0, 0, 0, 0, 0 }); var broadcastMAC = new PhysicalAddress(new byte[] { 255, 255, 255, 255, 255, 255 }); for (var i = 0; i < entries; i++) { var row = table[i]; var ipAddress = new IPAddress(BitConverter.GetBytes(row.dwAddr)); var macAddress = new PhysicalAddress(new[] { row.mac0, row.mac1, row.mac2, row.mac3, row.mac4, row.mac5 }); // Filter 0.0.0.0.0.0, 255.255.255.255.255.255 if (!macAddress.Equals(virtualMAC) && !macAddress.Equals(broadcastMAC)) { list.Add(new ARPInfo(ipAddress, macAddress, ipAddress.IsIPv6Multicast || IPv4Address.IsMulticast(ipAddress))); } } return(list); } finally { // Release the memory. FreeMibTable(buffer); } }
public static IPAddress[] CreateIPAddressesFromIPRanges(string[] ipRanges, CancellationToken cancellationToken) { var bag = new ConcurrentBag <IPAddress>(); var parallelOptions = new ParallelOptions { CancellationToken = cancellationToken }; foreach (var ipOrRange in ipRanges) { // Match 192.168.0.1 if (Regex.IsMatch(ipOrRange, RegexHelper.IPv4AddressRegex)) { bag.Add(IPAddress.Parse(ipOrRange)); continue; } // Match 192.168.0.0/24 or 192.168.0.0/255.255.255.0 if (Regex.IsMatch(ipOrRange, RegexHelper.IPv4AddressCidrRegex) || Regex.IsMatch(ipOrRange, RegexHelper.IPv4AddressSubnetmaskRegex)) { var network = IPNetwork.Parse(ipOrRange); Parallel.For(IPv4Address.ToInt32(network.Network), IPv4Address.ToInt32(network.Broadcast) + 1, parallelOptions, i => { bag.Add(IPv4Address.FromInt32(i)); parallelOptions.CancellationToken.ThrowIfCancellationRequested(); }); continue; } // Match 192.168.0.0 - 192.168.0.100 if (Regex.IsMatch(ipOrRange, RegexHelper.IPv4AddressRangeRegex)) { var range = ipOrRange.Split('-'); Parallel.For(IPv4Address.ToInt32(IPAddress.Parse(range[0])), IPv4Address.ToInt32(IPAddress.Parse(range[1])) + 1, parallelOptions, i => { bag.Add(IPv4Address.FromInt32(i)); parallelOptions.CancellationToken.ThrowIfCancellationRequested(); }); continue; } // Convert 192.168.[50-100,200].1 to 192.168.50.1, 192.168.51.1, 192.168.52.1, {..}, 192.168.200.1 if (!Regex.IsMatch(ipOrRange, RegexHelper.IPv4AddressSpecialRangeRegex)) { continue; } { var octets = ipOrRange.Split('.'); var list = new List <List <int> >(); // Go through each octet... foreach (var octet in octets) { var innerList = new List <int>(); // Create a range for each octet if (Regex.IsMatch(octet, RegexHelper.SpecialRangeRegex)) { foreach (var numberOrRange in octet.Substring(1, octet.Length - 2).Split(',')) { // 50-100 if (numberOrRange.Contains("-")) { var rangeNumbers = numberOrRange.Split('-'); for (var i = int.Parse(rangeNumbers[0]); i < (int.Parse(rangeNumbers[1]) + 1); i++) { innerList.Add(i); } } // 200 else { innerList.Add(int.Parse(numberOrRange)); } } } else { innerList.Add(int.Parse(octet)); } list.Add(innerList); } // Build the new ipv4 foreach (var i in list[0]) { foreach (var j in list[1]) { foreach (var k in list[2]) { foreach (var h in list[3]) { bag.Add(IPAddress.Parse($"{i}.{j}.{k}.{h}")); } } } } } } return(bag.ToArray()); }
public static int ConvertSubnetmaskToCidr(IPAddress subnetmask) { return(string.Join("", IPv4Address.ToBinaryString(subnetmask.ToString()).Replace(".", "").TrimEnd('0')).Length); }