/// <summary> /// Scans this Host. /// Use delay and rounds for a repeated, polling-like check (i.e. to get notified when the WOL was successful). /// Uses 1s as Ping Timeout. /// </summary> /// <param name="delay">delay to start the scan in ms</param> /// <param name="rounds">number of times the whole scan is repeated (including delay)</param> private async void CheckStatus(int delay, int rounds) { Status = HostStatus.Checking; //n rounds for (int i = 0; i < rounds; i++) { //wait delay if (delay > 0) { await Task.Delay(delay); } //send ping HostInformation hi = await MainViewModel.CheckHost(this, 1000, HostStatus.Checking); //if successful, more rounds aren't needed if (hi.Status == HostStatus.Online) { Status = HostStatus.Online; return; } } //no answer Status = HostStatus.Offline; }
private void Border_MouseDown(object sender, MouseButtonEventArgs e) { if (e.LeftButton == MouseButtonState.Pressed) { HostDummy hd = ((FrameworkElement)sender).DataContext as HostDummy; if (hd != null) { if (hd.Host == null) { HostInformation hi = new HostInformation(((MainViewModel)DataContext).CurrentClassCNetwork.ToLong() + (long)hd.LastOctettInt); hd.Host = hi; } if (hd.Host != null) { hd.Host.ScanIPCommand.Execute(null); } } } }
/// <summary> /// Performs the IP scan /// </summary> public async void ScanICMP() { IsAnalyzing = true; //set Class C grid to the right page ChangeClassCNetwork(Config.IPRangeStart.ToIP().ToLongNetwork().ToIP()); //fills the list of the Hosts with actual HostInformations for all IPs that are scanned for (long i = Config.IPRangeStart; i <= Config.IPRangeEnd; i++) { var hi = Hosts.FirstOrDefault(f => f.IP == i); if (hi == null) { hi = new HostInformation(i); hi.Pending = true; Hosts.Add(hi); } else { hi.Pending = true; } } //update the underlaying Hosts of the dummies UpdateClassCDummies(); //check internet if needed if (Config.CheckInternet && CurrentNIC != null) { await CheckConnectivity(); } int hostcount = Hosts.Where(w => w.Pending).Count(); int current = 0; int check = 1; while (check != 0 && !Stop) //as long there are unchecked IPs... { //grab a bunch of pending IPs List <HostInformation> z = Hosts.Where(w => w.Pending).Take(Config.MaxParallelConnections).ToList(); z.ForEach(e => e.Status = HostStatus.Checking); List <Task <HostInformation> > tasks = new List <Task <HostInformation> >(); foreach (var t in z) { tasks.Add(CheckHost(t, Config.PingTimeout)); } //and execute the scan for them Task.WaitAll(tasks.ToArray()); //invoke view update OnPropertyChanged("HostsOnline"); check = z.Count(); current += check; ScanProgress = ((double)current / (double)hostcount) * 100; //scroll the the next page on the Class C grid if needed if (Config.AutoScroll && check > 0 && z.FirstOrDefault() != null) { ChangeClassCNetwork(z.First().IP.ToIP().ToLongNetwork().ToIP()); } } //now get the MACs of all newly found Hosts and store them. Update RecentHosts as well. var l = GetAllDevicesOnLAN(); foreach (var d in l) { var f = Hosts.FirstOrDefault(a => a.IP == d.Key.ToLong()); if (f != null) { f.MAC = d.Value; } else { f = new HostInformation(d.Key.ToLong()) { MAC = d.Value }; Hosts.Add(f); } if (!Config.RecentHosts.Any(a => a.MAC.ToString() == f.MAC.ToString())) { Config.RecentHosts.Add(f); } } //reset Pending status and do cleanup foreach (var h in Hosts) { h.Pending = false; } ScanProgress = Stop ? 0 : 100; Stop = false; IsAnalyzing = false; FireAllPropertiesChanged(); }