private void lstDevices_SelectedIndexChanged(object sender, EventArgs e) { if (lstDevices.SelectedIndex > -1) { lblSelectedDevice.Text = lstDevices.SelectedItem.ToString(); Log("selected device changed to " + devices[lstDevices.SelectedIndex].Description.ToString()); device = devices[lstDevices.SelectedIndex]; } }
/// <summary> /// Constructor /// </summary> /// <param name="device"> /// A <see cref="LibPcapLiveDevice"/> /// </param> /// <param name="captureFilename"> /// A <see cref="System.String"/> /// </param> public CaptureFileWriterDevice(LibPcapLiveDevice device, string captureFilename) : this((PacketDotNet.LinkLayers)LibPcapSafeNativeMethods.pcap_datalink(device.PcapHandle), LibPcapSafeNativeMethods.pcap_snapshot(device.PcapHandle), captureFilename, FileMode.OpenOrCreate) { }
public void TestStatistics() { var devices = LibPcapLiveDeviceList.Instance; if (devices.Count == 0) { var error = "No pcap supported devices found, are you running" + " as a user with access to adapters (root on Linux)?"; throw new System.InvalidOperationException(error); } else { Console.WriteLine("Found {0} devices", devices.Count); } SharpPcap.LibPcap.LibPcapLiveDevice dev = null; foreach (var d in devices) { Console.WriteLine(d.ToString()); if (d.Name == "any") { dev = d; } } // if we couldn't find the 'any' device (maybe we are running on Windows) // then just use the first device we can find, if there are any devices if ((dev == null) && devices.Count != 0) { dev = devices[0]; } Assert.IsNotNull(dev, "Unable to find a capture device"); // open a device for capture dev.Open(); // wait a little while so maybe packets will pass by System.Threading.Thread.Sleep(500); // retrieve the statistics var statistics = dev.Statistics; // output the statistics Console.WriteLine("statistics: {0}", statistics.ToString()); dev.Close(); }
/// <summary> /// Constructs a new ARP Resolver /// </summary> /// <param name="device">The network device on which this resolver sends its ARP packets</param> public ARP(LibPcap.LibPcapLiveDevice device) { _device = device; }
private void SetupAdapter() { if (mDevice != null) { mDevice.Close(); } foreach (LibPcapLiveDevice device in LibPcapLiveDeviceList.Instance) { if (device.Interface.FriendlyName == Config.Instance.Interface) { mDevice = device; break; } } if (mDevice == null) { // Well shit... MessageBox.Show("Invalid configuration. Please re-setup your MultiShark configuration.", "MultiShark", MessageBoxButtons.OK, MessageBoxIcon.Error); if (ShowSetupForm() != DialogResult.OK) { Close(); return; } SetupAdapter(); } try { mDevice.Open(DeviceMode.Promiscuous, 1); } catch { MessageBox.Show("Failed to set the device in Promiscuous mode! But that doesn't really matter lol."); mDevice.Open(); } mDevice.Filter = string.Format("tcp portrange {0}-{1}", Config.Instance.LowPort, Config.Instance.HighPort); }
/// <summary> /// Creates a new ethernet port instance /// </summary> /// <param name="options">The options for the port</param> public EthernetPort(EthernetPortOptions options) { this._options = options.Clone(); this._device = _getCaptureDevice(); this._device.OnPacketArrival += _onPacketArrival; }
private void MainForm_Load(object pSender, EventArgs pArgs) { if (new SetupForm().ShowDialog(this) != DialogResult.OK) { Close(); return; } foreach (LibPcapLiveDevice device in LibPcapLiveDeviceList.Instance) { if (device.Interface.FriendlyName == Config.Instance.Interface) { mDevice = device; break; } } try { mDevice.Open(DeviceMode.Promiscuous, 1); } catch { MessageBox.Show("Failed to set the device in Promiscuous mode! But that doesn't really matter lol."); mDevice.Open(); } mDevice.Filter = string.Format("tcp portrange {0}-{1}", Config.Instance.LowPort, Config.Instance.HighPort); mTimer.Enabled = true; mSearchForm.Show(mDockPanel); mDataForm.Show(mDockPanel); mStructureForm.Show(mDockPanel); mPropertyForm.Show(mDockPanel); DockPane rightPane1 = new DockPane(mStructureForm, DockState.DockRight, true); DockPane rightPane2 = new DockPane(mPropertyForm, DockState.DockRight, true); rightPane1.Show(); rightPane2.Show(); }
/// <summary> /// Constructor /// </summary> /// <param name="device"> /// A <see cref="LibPcapLiveDevice"/> /// </param> /// <param name="captureFilename"> /// A <see cref="System.String"/> /// </param> /// <param name="mode"> /// A <see cref="FileMode"/> /// </param> public CaptureFileWriterDevice(LibPcapLiveDevice device, string captureFilename, FileMode mode) : this((PacketDotNet.LinkLayers)LibPcapSafeNativeMethods.pcap_datalink(device.PcapHandle), LibPcapSafeNativeMethods.pcap_snapshot(device.PcapHandle), captureFilename, mode) { }
public void Start() { _device = Open(); if (_device == null) throw new Exception("Cannot open Ethernet interface"); _deviceMac = _device.Interface.MacAddress.GetAddressBytes(); // filter to only bacnet packets _device.Filter = "ether proto 0x82"; System.Threading.Thread th = new Threading.Thread(CaptureThread); th.IsBackground = true; th.Start(); }
/// <summary> /// Retrieve pcap statistics from the adapter /// </summary> /// <param name="pcap_t"> /// pcap_t* for the adapter /// A <see cref="IntPtr"/> /// </param> internal PcapStatistics(IntPtr pcap_t) { IntPtr stat; if (Environment.OSVersion.Platform == PlatformID.Unix) { // allocate memory for the struct stat = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(PcapUnmanagedStructures.pcap_stat_unix))); } else { // allocate memory for the struct stat = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(PcapUnmanagedStructures.pcap_stat_windows))); } // retrieve the stats var result = (PcapUnmanagedStructures.PcapStatReturnValue)LibPcapSafeNativeMethods.pcap_stats(pcap_t, stat); // process the return value switch (result) { case PcapUnmanagedStructures.PcapStatReturnValue.Error: // retrieve the error information var error = LibPcapLiveDevice.GetLastError(pcap_t); // free the stats memory so we don't leak before we throw Marshal.FreeHGlobal(stat); throw new StatisticsException(error); case PcapUnmanagedStructures.PcapStatReturnValue.Success: // nothing to do upon success break; } // marshal the unmanaged memory into an object of the proper type if (Environment.OSVersion.Platform == PlatformID.Unix) { var managedStat = (PcapUnmanagedStructures.pcap_stat_unix)Marshal.PtrToStructure(stat, typeof(PcapUnmanagedStructures.pcap_stat_unix)); // copy the values this.ReceivedPackets = (uint)managedStat.ps_recv; this.DroppedPackets = (uint)managedStat.ps_drop; // this.InterfaceDroppedPackets = (uint)managedStat.ps_ifdrop; } else { var managedStat = (PcapUnmanagedStructures.pcap_stat_windows)Marshal.PtrToStructure(stat, typeof(PcapUnmanagedStructures.pcap_stat_windows)); // copy the values this.ReceivedPackets = (uint)managedStat.ps_recv; this.DroppedPackets = (uint)managedStat.ps_drop; // this.InterfaceDroppedPackets = (uint)managedStat.ps_ifdrop; } // NOTE: Not supported on unix or winpcap, no need to // put a bogus value in this field this.InterfaceDroppedPackets = 0; // free the stats Marshal.FreeHGlobal(stat); }