コード例 #1
0
 public override void Start()
 {
     lock (monitorLock)
     {
         InitWatcher();
         if (IsRunning)
         {
             return;
         }
         if (IsDisposing)
         {
             return;
         }
         // If monitor is running already then...
         if (CaptureDevices.Count > 0)
         {
             OnStatusChanged(null, null, null, "Error: Monitoring already. Stop first!");
             return;
         }
         try
         {
             IpAddresses.Clear();
             // Retrieve all capture devices
             if (SettingsManager.Options.NetworkMonitorCapturingType == CapturingType.WinPcap)
             {
                 var devices = CaptureDeviceList.Instance.ToArray();
                 var wcaps   = devices.OfType <WinPcapDevice>();
                 foreach (var device in wcaps)
                 {
                     device.OnPacketArrival += Wc_OnPacketArrival;
                     device.Open(DeviceMode.Normal);
                     device.Filter = "ip";
                     foreach (var address in device.Addresses)
                     {
                         if (address.Addr != null && address.Addr.ipAddress != null)
                         {
                             IpAddresses.Add(address.Addr.ipAddress);
                         }
                     }
                     CaptureDevices.Add(device);
                 }
             }
             else if (SettingsManager.Options.NetworkMonitorCapturingType == CapturingType.SocPcap)
             {
                 var device = new SocPcapDevice();
                 device.OnPacketArrival += Wc_OnPacketArrival;
                 device.Open();
                 device.Filter = "ip";
                 foreach (var address in device.Addresses)
                 {
                     IpAddresses.Add(address);
                 }
                 CaptureDevices.Add(device);
             }
             // Set default packet filter.
             var mi = Program.MonitorItem;
             SetFilter(mi);
             var ip4c = IpAddresses.Count(x => x.AddressFamily == AddressFamily.InterNetwork);
             var ip6c = IpAddresses.Count(x => x.AddressFamily == AddressFamily.InterNetworkV6);
             OnStatusChanged(null, null, null, string.Format("Addresses: {0} IPv4, {1} IPv6", ip4c, ip6c));
             // Retrieve all capture devices
             foreach (var device in CaptureDevices)
             {
                 // Start the capturing process
                 device.StartCapture();
             }
         }
         catch (Exception ex)
         {
             LastException = ex;
         }
     }
 }
コード例 #2
0
 public void StartNetworkMonitor()
 {
     lock (monitorLock)
     {
         if (!MonitorPortCheckBox.Checked)
         {
             return;
         }
         if (Disposing || IsDisposed || !IsHandleCreated)
         {
             return;
         }
         // If monitor is running already then...
         if (CaptureDevices.Count > 0)
         {
             StateStatusLabel.Text = "Error: Monitoring already. Stop first!";
             return;
         }
         try
         {
             IpAddresses.Clear();
             continueMonitoring = true;
             // Retrieve all capture devices
             if (SettingsManager.Options.UseWinCap)
             {
                 var devices = CaptureDeviceList.Instance.ToArray();
                 var wcaps   = devices.OfType <WinPcapDevice>();
                 foreach (var device in wcaps)
                 {
                     device.OnPacketArrival += Wc_OnPacketArrival;
                     device.Open(DeviceMode.Normal);
                     device.Filter = "ip";
                     foreach (var address in device.Addresses)
                     {
                         if (address.Addr != null && address.Addr.ipAddress != null)
                         {
                             IpAddresses.Add(address.Addr.ipAddress);
                         }
                     }
                     CaptureDevices.Add(device);
                 }
             }
             else
             {
                 var device = new SocPcapDevice();
                 device.OnPacketArrival += Wc_OnPacketArrival;
                 device.Open();
                 device.Filter = "ip";
                 foreach (var address in device.Addresses)
                 {
                     IpAddresses.Add(address);
                 }
                 CaptureDevices.Add(device);
             }
             // Set default packet filter.
             SetFilter(MonitorItem);
             var ip4c = IpAddresses.Count(x => x.AddressFamily == AddressFamily.InterNetwork);
             var ip6c = IpAddresses.Count(x => x.AddressFamily == AddressFamily.InterNetworkV6);
             StateStatusLabel.Text = string.Format("Addresses: {0} IPv4, {1} IPv6", ip4c, ip6c);
             // Retrieve all capture devices
             foreach (var device in CaptureDevices)
             {
                 // Start the capturing process
                 device.StartCapture();
             }
         }
         catch (Exception ex)
         {
             LastException = ex;
         }
     }
 }