public static void Start(string range, CancellationToken cancellationToken) { Ping ping = null;// List <string> addresses = new List <string>(); List <HostInformation> activeHosts = new List <HostInformation>(); Stopwatch timer = new Stopwatch(); int scanned = 0; // Setup scan ping attributes PingAttributes attrs = new PingAttributes(); attrs.InputtedAddress = "127.0.0.1"; // False address to pass lookup (TODO: ew) attrs.Timeout = 500; attrs.Interval = 0; attrs.Count = 1; Display.ShowOutput = false; // Setup ping object ping = new Ping(attrs, cancellationToken); // Get addresses to scan from range addresses = ParseRange(range); // TODO: In order to speed this up we need the following steps: /* * 1) Divide the address list by the number of threads we will us * 2) New thread method w/ mutable active hosts list * 3) Some way of updating the UI (resultsUpdateCallback?) */ timer.Start(); try { // Scan loop foreach (string host in addresses) { // Send ping PingResults results = ping.Send(host); if (results.ScanWasCanceled) { // Cancel was requested during scan throw new OperationCanceledException(); } scanned++; Display.ScanProgress(scanned, activeHosts.Count, addresses.Count, timer.Elapsed, range, attrs.ResolvedAddress); if (results.Lost == 0 && results.ErrorPackets != 1) { // If host is active, add to list activeHosts.Add(new HostInformation { Address = host, HostName = "", ResponseTime = results.CurrTime }); } } } catch (OperationCanceledException) { } // Lookup host's name Console.WriteLine(); Console.Write("Looking up host names, one sec..."); Console.CursorLeft = 0; foreach (HostInformation host in activeHosts) { string hostName = Helper.RunWithCancellationToken(() => Lookup.QueryHost(host.Address), cancellationToken); host.HostName = hostName; } Console.WriteLine(" "); Console.CursorTop--; Display.ScanResults(scanned, !cancellationToken.IsCancellationRequested, activeHosts); }
public static void Start(string range, CancellationToken cancellationToken) { List <string> addresses = new List <string>(); List <HostInformation> activeHosts = new List <HostInformation>(); Stopwatch timer = new Stopwatch(); // Get addresses to scan from range addresses = ParseRange(range); // Setup addresses and threads var splitAddresses = Helper.PartitionList(addresses, THREAD_COUNT); Thread[] threads = new Thread[THREAD_COUNT]; object lockObject = new object(); int scanned = 0; // Run the threads timer.Start(); for (int i = 0; i < THREAD_COUNT; i++) { List <string> addrs = splitAddresses[i]; threads[i] = new Thread(() => { PingAttributes attrs = new PingAttributes(); attrs.InputtedAddress = "127.0.0.1"; attrs.Timeout = 500; attrs.Interval = 0; attrs.Count = 1; Ping ping = new Ping(attrs, cancellationToken); try { foreach (string host in addrs) { // Send ping PingResults results = ping.Send(host); if (results.ScanWasCanceled) { // Cancel was requested during scan throw new OperationCanceledException(); } Interlocked.Increment(ref scanned); if (results.Lost == 0 && results.ErrorPackets != 1) { // If host is active, add to list lock (lockObject) { activeHosts.Add(new HostInformation { Address = host, HostName = "", ResponseTime = results.CurrTime }); } } } } catch (OperationCanceledException) { _cancelled = true; } }); threads[i].IsBackground = true; threads[i].Start(); } // Wait for all threads to exit int lastSent = 0, pingsPerSecond = 0; int lastSpeedCheck = 0; while (threads.Where(x => x.IsAlive).ToList().Count > 0) { int count = 0; lock (lockObject) { count = activeHosts.Count; } if (lastSpeedCheck == 5) { pingsPerSecond = Math.Abs((scanned - lastSent)); lastSent = scanned; lastSpeedCheck = 0; } ConsoleDisplay.ScanProgress( scanned, activeHosts.Count, addresses.Count, pingsPerSecond, timer.Elapsed, range); lastSpeedCheck++; Thread.Sleep(200); } // Display one last time so the bar actually completes // (scan could have completed while the main thread was sleeping) ConsoleDisplay.ScanProgress( scanned, activeHosts.Count, addresses.Count, pingsPerSecond, timer.Elapsed, range); // Exit out when the operation has been canceled if (_cancelled) { ConsoleDisplay.ScanResults(scanned, false, activeHosts); return; } // Lookup host's name Console.WriteLine(); Console.Write("Looking up host names, one sec..."); Console.CursorLeft = 0; foreach (HostInformation host in activeHosts) { string hostName = Helper.RunWithCancellationToken(() => Lookup.QueryHost(host.Address), cancellationToken); host.HostName = hostName; } Console.WriteLine(" "); Console.CursorTop--; ConsoleDisplay.ScanResults(scanned, !cancellationToken.IsCancellationRequested, activeHosts); }
/// <summary> /// Main entry point of PowerPing /// Parses arguments and runs operations /// </summary> /// <param name="args">Program arguments</param> static void Main(string[] args) { // User inputted attributes PingAttributes inputtedAttributes = new PingAttributes(); // Setup console Display.DefaultForegroundColor = Console.ForegroundColor; Display.DefaultBackgroundColor = Console.BackgroundColor; // Show current version info //Display.Version(); // Check if no arguments if (args.Length == 0) { Display.Help(); Helper.WaitForUserInput(); return; } // Parse command line arguments if (!CommandLine.Parse(args, ref inputtedAttributes)) { Helper.ErrorAndExit("Problem parsing arguments, use \"PowerPing /help\" or \"PowerPing /?\" for help."); } // Find address/host in arguments if (inputtedAttributes.Operation != PingOperation.Whoami && inputtedAttributes.Operation != PingOperation.Listen) { if (!CommandLine.FindAddress(args, ref inputtedAttributes)) { Helper.ErrorAndExit("Could not find correctly formatted address, please check and try again"); } } // Perform DNS lookup on inputted address // inputtedAttributes.ResolvedAddress = Lookup.QueryDNS(inputtedAttributes.InputtedAddress, inputtedAttributes.UseICMPv4 ? AddressFamily.InterNetwork : AddressFamily.InterNetworkV6); // Add Control C event handler if (inputtedAttributes.Operation != PingOperation.Whoami && inputtedAttributes.Operation != PingOperation.Location && inputtedAttributes.Operation != PingOperation.Whois) { Console.CancelKeyPress += new ConsoleCancelEventHandler(ExitHandler); } // Select correct function using opMode Ping p; Graph g; switch (inputtedAttributes.Operation) { case PingOperation.Listen: // If we find an address then pass it to listen, otherwise start it without one if (CommandLine.FindAddress(args, ref inputtedAttributes)) { Listen.Start(m_CancellationTokenSource.Token, inputtedAttributes.InputtedAddress); } else { Listen.Start(m_CancellationTokenSource.Token); } break; case PingOperation.Location: Console.WriteLine(Lookup.GetAddressLocationInfo(inputtedAttributes.InputtedAddress, false)); Helper.WaitForUserInput(); break; case PingOperation.Whoami: Console.WriteLine(Lookup.GetAddressLocationInfo("", true)); Helper.WaitForUserInput(); break; case PingOperation.Whois: Lookup.QueryWhoIs(inputtedAttributes.InputtedAddress); break; case PingOperation.Graph: g = new Graph(inputtedAttributes.InputtedAddress, m_CancellationTokenSource.Token); g.Start(); break; case PingOperation.CompactGraph: g = new Graph(inputtedAttributes.InputtedAddress, m_CancellationTokenSource.Token); g.CompactGraph = true; g.Start(); break; case PingOperation.Flood: Flood.Start(inputtedAttributes.InputtedAddress, m_CancellationTokenSource.Token); break; case PingOperation.Scan: Scan.Start(inputtedAttributes.InputtedAddress, m_CancellationTokenSource.Token); break; case PingOperation.Normal: // Send ping normally p = new Ping(inputtedAttributes, m_CancellationTokenSource.Token); PingResults results = p.Send(); Display.PingResults(inputtedAttributes, results); break; default: Helper.ErrorAndExit("Could not determine ping operation"); break; } // Reset console colour Display.ResetColor(); try { Console.CursorVisible = true; } catch (Exception) { } }