private async void StartScan()
        {
            DisplayStatusMessage = false;
            IsScanRunning        = true;
            PreparingScan        = true;

            // Measure the time
            StartTime = DateTime.Now;
            stopwatch.Start();
            dispatcherTimer.Tick    += DispatcherTimer_Tick;
            dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 100);
            dispatcherTimer.Start();
            EndTime = null;

            IPScanResult.Clear();
            HostsFound = 0;

            cancellationTokenSource = new CancellationTokenSource();

            IPAddress[] ipAddresses;

            try
            {
                // Create a list of all ip addresses
                ipAddresses = await IPScanRangeHelper.ConvertIPRangeToIPAddressesAsync(IPRange, cancellationTokenSource.Token);
            }
            catch (OperationCanceledException)
            {
                IpScanner_UserHasCanceled(this, EventArgs.Empty);
                return;
            }

            IPAddressesToScan  = ipAddresses.Length;
            IPAddressesScanned = 0;

            PreparingScan = false;

            // Add the range to the history
            AddIPRangeToHistory(IPRange);

            IPScannerOptions ipScannerOptions = new IPScannerOptions
            {
                Threads           = SettingsManager.Current.IPScanner_Threads,
                Timeout           = SettingsManager.Current.IPScanner_Timeout,
                Buffer            = new byte[SettingsManager.Current.IPScanner_Buffer],
                Attempts          = SettingsManager.Current.IPScanner_Attempts,
                ResolveHostname   = SettingsManager.Current.IPScanner_ResolveHostname,
                ResolveMACAddress = SettingsManager.Current.IPScanner_ResolveMACAddress,
                ShowScanResultForAllIPAddresses = SettingsManager.Current.IPScanner_ShowScanResultForAllIPAddresses
            };

            IPScanner ipScanner = new IPScanner();

            ipScanner.HostFound       += IpScanner_HostFound;
            ipScanner.ScanComplete    += IpScanner_ScanComplete;
            ipScanner.ProgressChanged += IpScanner_ProgressChanged;
            ipScanner.UserHasCanceled += IpScanner_UserHasCanceled;

            ipScanner.ScanAsync(ipAddresses, ipScannerOptions, cancellationTokenSource.Token);
        }
Beispiel #2
0
        private async void StartScan()
        {
            DisplayStatusMessage = false;
            IsScanRunning        = true;
            PreparingScan        = true;

            // Measure the time
            StartTime = DateTime.Now;
            stopwatch.Start();
            dispatcherTimer.Tick    += DispatcherTimer_Tick;
            dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 100);
            dispatcherTimer.Start();
            EndTime = null;

            IPScanResult.Clear();
            HostsFound = 0;

            // Change the tab title (not nice, but it works)
            Window window = Application.Current.Windows.OfType <Window>().FirstOrDefault(x => x.IsActive);

            if (window != null)
            {
                foreach (TabablzControl tabablzControl in VisualTreeHelper.FindVisualChildren <TabablzControl>(window))
                {
                    tabablzControl.Items.OfType <DragablzTabItem>().First(x => x.ID == _tabId).Header = IPRange;
                }
            }

            cancellationTokenSource = new CancellationTokenSource();

            IPAddress[] ipAddresses;

            try
            {
                // Create a list of all ip addresses
                ipAddresses = await IPScanRangeHelper.ConvertIPRangeToIPAddressesAsync(IPRange, cancellationTokenSource.Token);
            }
            catch (OperationCanceledException)
            {
                IpScanner_UserHasCanceled(this, EventArgs.Empty);
                return;
            }

            IPAddressesToScan  = ipAddresses.Length;
            IPAddressesScanned = 0;

            PreparingScan = false;

            // Add the range to the history
            AddIPRangeToHistory(IPRange);

            IPScannerOptions ipScannerOptions = new IPScannerOptions
            {
                Threads           = SettingsManager.Current.IPScanner_Threads,
                Timeout           = SettingsManager.Current.IPScanner_Timeout,
                Buffer            = new byte[SettingsManager.Current.IPScanner_Buffer],
                Attempts          = SettingsManager.Current.IPScanner_Attempts,
                ResolveHostname   = SettingsManager.Current.IPScanner_ResolveHostname,
                ResolveMACAddress = SettingsManager.Current.IPScanner_ResolveMACAddress,
                ShowScanResultForAllIPAddresses = SettingsManager.Current.IPScanner_ShowScanResultForAllIPAddresses
            };

            IPScanner ipScanner = new IPScanner();

            ipScanner.HostFound       += IpScanner_HostFound;
            ipScanner.ScanComplete    += IpScanner_ScanComplete;
            ipScanner.ProgressChanged += IpScanner_ProgressChanged;
            ipScanner.UserHasCanceled += IpScanner_UserHasCanceled;

            ipScanner.ScanAsync(ipAddresses, ipScannerOptions, cancellationTokenSource.Token);
        }
        private async void StartScan()
        {
            DisplayStatusMessage = false;
            IsScanRunning        = true;
            PreparingScan        = true;

            // Measure the time
            StartTime = DateTime.Now;
            stopwatch.Start();
            dispatcherTimer.Tick    += DispatcherTimer_Tick;
            dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 100);
            dispatcherTimer.Start();
            EndTime = null;

            IPScanResult.Clear();
            HostsFound = 0;

            // Change the tab title (not nice, but it works)
            Window window = Application.Current.Windows.OfType <Window>().FirstOrDefault(x => x.IsActive);

            if (window != null)
            {
                foreach (TabablzControl tabablzControl in VisualTreeHelper.FindVisualChildren <TabablzControl>(window))
                {
                    tabablzControl.Items.OfType <DragablzTabItem>().First(x => x.ID == _tabId).Header = IPRange;
                }
            }

            cancellationTokenSource = new CancellationTokenSource();

            string[] ipHostOrRanges = IPRange.Replace(" ", "").Split(';');

            // Resolve hostnames
            List <string> ipRanges = new List <string>();

            try
            {
                ipRanges = await IPScanRangeHelper.ResolveHostnamesInIPRangeAsync(ipHostOrRanges, cancellationTokenSource.Token);
            }
            catch (OperationCanceledException)
            {
                IpScanner_UserHasCanceled(this, EventArgs.Empty);
                return;
            }
            catch (AggregateException exceptions) // DNS error (could not resolve hostname...)
            {
                IpScanner_DnsResolveFailed(this, exceptions);
                return;
            }

            IPAddress[] ipAddresses;

            try
            {
                // Create a list of all ip addresses
                ipAddresses = await IPScanRangeHelper.ConvertIPRangeToIPAddressesAsync(ipRanges.ToArray(), cancellationTokenSource.Token);
            }
            catch (OperationCanceledException)
            {
                IpScanner_UserHasCanceled(this, EventArgs.Empty);
                return;
            }

            IPAddressesToScan  = ipAddresses.Length;
            IPAddressesScanned = 0;

            PreparingScan = false;

            // Add the range to the history
            AddIPRangeToHistory(IPRange);

            IPScannerOptions ipScannerOptions = new IPScannerOptions
            {
                Threads                         = SettingsManager.Current.IPScanner_Threads,
                ICMPTimeout                     = SettingsManager.Current.IPScanner_ICMPTimeout,
                ICMPBuffer                      = new byte[SettingsManager.Current.IPScanner_ICMPBuffer],
                ICMPAttempts                    = SettingsManager.Current.IPScanner_ICMPAttempts,
                ResolveHostname                 = SettingsManager.Current.IPScanner_ResolveHostname,
                UseCustomDNSServer              = SettingsManager.Current.IPScanner_UseCustomDNSServer,
                CustomDNSServer                 = SettingsManager.Current.IPScanner_CustomDNSServer.Select(x => x.Trim()).ToList(),
                DNSPort                         = SettingsManager.Current.IPScanner_DNSPort,
                DNSTransportType                = SettingsManager.Current.IPScanner_DNSTransportType,
                DNSRecursion                    = SettingsManager.Current.IPScanner_DNSRecursion,
                DNSUseResolverCache             = SettingsManager.Current.IPScanner_DNSUseResolverCache,
                DNSAttempts                     = SettingsManager.Current.IPScanner_DNSAttempts,
                DNSTimeout                      = SettingsManager.Current.IPScanner_DNSTimeout,
                ResolveMACAddress               = SettingsManager.Current.IPScanner_ResolveMACAddress,
                ShowScanResultForAllIPAddresses = SettingsManager.Current.IPScanner_ShowScanResultForAllIPAddresses
            };

            IPScanner ipScanner = new IPScanner();

            ipScanner.HostFound       += IpScanner_HostFound;
            ipScanner.ScanComplete    += IpScanner_ScanComplete;
            ipScanner.ProgressChanged += IpScanner_ProgressChanged;
            ipScanner.UserHasCanceled += IpScanner_UserHasCanceled;

            ipScanner.ScanAsync(ipAddresses, ipScannerOptions, cancellationTokenSource.Token);
        }