From 97ebf4d1454d9b2c9c508af33ee49c3e87d0d85d Mon Sep 17 00:00:00 2001 From: Stone_Red <56473591+Stone-Red-Code@users.noreply.github.com> Date: Sat, 16 Dec 2023 18:17:51 +0100 Subject: [PATCH] Fix multiple bugs and improve code --- src/ARP-Scanner/ARP-Scanner.csproj | 2 +- src/ARP-Scanner/ExtentionMethods.cs | 52 +++++++++++++++++++++++ src/ARP-Scanner/MacVendorLookup.cs | 2 +- src/ARP-Scanner/Program.cs | 65 +++++++++++------------------ 4 files changed, 78 insertions(+), 43 deletions(-) create mode 100644 src/ARP-Scanner/ExtentionMethods.cs diff --git a/src/ARP-Scanner/ARP-Scanner.csproj b/src/ARP-Scanner/ARP-Scanner.csproj index a9b7949..43e853b 100644 --- a/src/ARP-Scanner/ARP-Scanner.csproj +++ b/src/ARP-Scanner/ARP-Scanner.csproj @@ -10,7 +10,7 @@ - + diff --git a/src/ARP-Scanner/ExtentionMethods.cs b/src/ARP-Scanner/ExtentionMethods.cs new file mode 100644 index 0000000..e260b86 --- /dev/null +++ b/src/ARP-Scanner/ExtentionMethods.cs @@ -0,0 +1,52 @@ +using NetTools; + +using System.Diagnostics; + +namespace ARP_Scanner; + +internal static class ExtentionMethods +{ + public static long Count(this IPAddressRange ipAddresses) + { + byte[] byteBegin = ipAddresses.Begin.GetAddressBytes(); + byte[] byteEnd = ipAddresses.End.GetAddressBytes(); + + long sum = 1; + for (int i = byteBegin.Length - 1; i >= 0; i--) + { + if (byteEnd[i] - byteBegin[i] == 0) + { + continue; + } + + sum += (long)((byteEnd[i] - byteBegin[i]) * Math.Pow(2D, (double)(byteBegin.Length - 1 - i) * 8)); + Debug.WriteLine(sum); + } + + return sum; + } + + public static T[,] To2D(this T[][] source) + { + try + { + int FirstDim = source.Length; + int SecondDim = source.GroupBy(row => row.Length).Single().Key; // throws InvalidOperationException if source is not rectangular + + T[,]? result = new T[FirstDim, SecondDim]; + for (int i = 0; i < FirstDim; ++i) + { + for (int j = 0; j < SecondDim; ++j) + { + result[i, j] = source[i][j]; + } + } + + return result; + } + catch (InvalidOperationException) + { + throw new InvalidOperationException("The given jagged array is not rectangular."); + } + } +} \ No newline at end of file diff --git a/src/ARP-Scanner/MacVendorLookup.cs b/src/ARP-Scanner/MacVendorLookup.cs index 59a9ba8..3d3fb1f 100644 --- a/src/ARP-Scanner/MacVendorLookup.cs +++ b/src/ARP-Scanner/MacVendorLookup.cs @@ -24,7 +24,7 @@ internal class MacVendorLookup public string[] GetInformation(string macAdress) { - string[]? data = fields.FirstOrDefault(f => macAdress.StartsWith(f[0]))?.ToArray(); + string[]? data = Array.Find(fields, f => macAdress.StartsWith(f[0])); if (fields.Length == 0 || header.Length == 0) { diff --git a/src/ARP-Scanner/Program.cs b/src/ARP-Scanner/Program.cs index 61e0ab8..b304b3d 100644 --- a/src/ARP-Scanner/Program.cs +++ b/src/ARP-Scanner/Program.cs @@ -15,8 +15,6 @@ internal static class Program { private static async Task Main(string[] args) { - bool success = false; - IPAddress[]? ipAddresses = null; MacVendorLookup macVendorLookup = new MacVendorLookup("mac-vendors.csv"); if (!Arp.IsSupported) @@ -25,20 +23,15 @@ internal static class Program return; } - if (args.Length >= 1) - { - success = IPAddressRange.TryParse(string.Join("", args), out IPAddressRange iPAddressRange); - ipAddresses = iPAddressRange.AsEnumerable().ToArray(); - } - - if (!success || ipAddresses is null) + if (!IPAddressRange.TryParse(string.Join("", args), out IPAddressRange ipAddressRange)) { ConsoleExt.WriteLine("Invalid IP range!", ConsoleColor.Red); return; } - int ipAddressesCount = ipAddresses.Length; - int processedIpAddressesCount = 0; + long ipAddressesCount = ipAddressRange.Count(); + long processedIpAddressesCount = 0; + int numberOfDigits = ipAddressesCount.ToString().Length; List header = new List() { "IP", "MAC" }; ConcurrentBag activeHosts = new ConcurrentBag(); @@ -47,23 +40,37 @@ internal static class Program ConsoleExt.WriteLine("Starting scan...", ConsoleColor.DarkYellow); - await Parallel.ForEachAsync(ipAddresses, async (ipAddress, _) => + await Parallel.ForEachAsync(ipAddressRange, async (IPAddress ipAddress, CancellationToken _) => { - PhysicalAddress? mac = await Arp.LookupAsync(ipAddress); + PhysicalAddress? mac = null; + bool fail = false; - int localProcessedIpAddressesCount = Interlocked.Increment(ref processedIpAddressesCount); + try + { + mac = await Arp.LookupAsync(ipAddress); + } + catch + { + fail = true; + } + + long localProcessedIpAddressesCount = Interlocked.Increment(ref processedIpAddressesCount); if (mac is not null) { string formattedMac = BitConverter.ToString(mac.GetAddressBytes()); List info = new List { ipAddress.ToString(), formattedMac }; info.AddRange(macVendorLookup.GetInformation(formattedMac)); - ConsoleExt.WriteLine($"Progress: {localProcessedIpAddressesCount}/{ipAddressesCount} [{100d / ipAddressesCount * localProcessedIpAddressesCount:0.00}%] | Active: {ipAddress}", ConsoleColor.Green); + ConsoleExt.WriteLine($"Progress: {localProcessedIpAddressesCount.ToString().PadLeft(numberOfDigits)}/{ipAddressesCount} [{100d / ipAddressesCount * localProcessedIpAddressesCount,6:##0.00}%] | Active: {ipAddress}", ConsoleColor.Green); activeHosts.Add(info.ToArray()); } + else if (fail) + { + ConsoleExt.WriteLine($"Progress: {localProcessedIpAddressesCount.ToString().PadLeft(numberOfDigits)}/{ipAddressesCount} [{100d / ipAddressesCount * localProcessedIpAddressesCount,6:##0.00}%] | Failed: {ipAddress}", ConsoleColor.Red); + } else { - ConsoleExt.WriteLine($"Progress: {localProcessedIpAddressesCount}/{ipAddressesCount} [{100d / ipAddressesCount * localProcessedIpAddressesCount:0.00}%] | Inactive: {ipAddress}", ConsoleColor.Red); + ConsoleExt.WriteLine($"Progress: {localProcessedIpAddressesCount.ToString().PadLeft(numberOfDigits)}/{ipAddressesCount} [{100d / ipAddressesCount * localProcessedIpAddressesCount,6:##0.00}%] | Inactive: {ipAddress}", ConsoleColor.Red); } }); @@ -75,7 +82,7 @@ internal static class Program activeHostsTable.Insert(0, header.ToArray()); - To2D(activeHostsTable.ToArray()).PrintTable(TableStyle.List); + activeHostsTable.ToArray().To2D().PrintTable(TableStyle.List); ConsoleExt.WriteLine($"{Environment.NewLine}Found {activeHosts.Count} active hosts", ConsoleColor.Green); } @@ -84,28 +91,4 @@ internal static class Program ConsoleExt.WriteLine($"{Environment.NewLine}No active hosts found", ConsoleColor.Red); } } - - private static T[,] To2D(T[][] source) - { - try - { - int FirstDim = source.Length; - int SecondDim = source.GroupBy(row => row.Length).Single().Key; // throws InvalidOperationException if source is not rectangular - - T[,]? result = new T[FirstDim, SecondDim]; - for (int i = 0; i < FirstDim; ++i) - { - for (int j = 0; j < SecondDim; ++j) - { - result[i, j] = source[i][j]; - } - } - - return result; - } - catch (InvalidOperationException) - { - throw new InvalidOperationException("The given jagged array is not rectangular."); - } - } } \ No newline at end of file