/// <summary> /// Example showing how to retrieve a list of AirPcap devices /// We can't use this because AirPcap devices don't have a pcap handle /// but this code is worth keeping around /// </summary> /// <returns> /// A <see cref="List<AirPcapDevice>"/> /// </returns> private static List <AirPcapDevice> GetAirPcapDevices() { var deviceList = new List <AirPcapDevice>(); var devicePtr = IntPtr.Zero; var errorBuffer = new StringBuilder(Pcap.PCAP_ERRBUF_SIZE); int result = AirPcapSafeNativeMethods.AirpcapGetDeviceList(ref devicePtr, errorBuffer); if (result < 0) { throw new PcapException(errorBuffer.ToString()); } IntPtr nextDevPtr = devicePtr; while (nextDevPtr != IntPtr.Zero) { // Marshal pointer into a struct AirPcapUnmanagedStructures.AirpcapDeviceDescription deviceDescUnmanaged = (AirPcapUnmanagedStructures.AirpcapDeviceDescription)Marshal.PtrToStructure(nextDevPtr, typeof(AirPcapUnmanagedStructures.AirpcapDeviceDescription)); var managedDeviceDesc = new AirPcapDeviceDescription(deviceDescUnmanaged); // deviceList.Add(new AirPcapDevice(managedDeviceDesc)); nextDevPtr = deviceDescUnmanaged.next; } AirPcapSafeNativeMethods.AirpcapFreeDeviceList(devicePtr); // Free unmanaged memory return(deviceList); }
/// <summary> /// Returns the version in separate fields /// </summary> /// <param name="Major"></param> /// <param name="Minor"></param> /// <param name="Rev"></param> /// <param name="Build"></param> /// <returns></returns> public static void Version(out uint Major, out uint Minor, out uint Rev, out uint Build) { UInt32 major, minor, rev, build; AirPcapSafeNativeMethods.AirpcapGetVersion(out major, out minor, out rev, out build); Major = (uint)major; Minor = (uint)minor; Rev = (uint)rev; Build = (uint)build; }
/// <summary> /// AirPcap specific capture thread /// </summary> protected override void CaptureThread() { IntPtr ReadEvent; IntPtr WaitIntervalMilliseconds = (IntPtr)500; // // Get the read event // if (!AirPcapSafeNativeMethods.AirpcapGetReadEvent(AirPcapDeviceHandle, out ReadEvent)) { SendCaptureStoppedEvent(CaptureStoppedEventStatus.ErrorWhileCapturing); Close(); return; } // allocate a packet bufer in unmanaged memory var packetBufferSize = 256000; var packetBuffer = Marshal.AllocHGlobal(packetBufferSize); UInt32 BytesReceived; List <RawCapture> packets; while (!shouldCaptureThreadStop) { // capture the packets if (!AirPcapSafeNativeMethods.AirpcapRead(AirPcapDeviceHandle, packetBuffer, (uint)packetBufferSize, out BytesReceived)) { Marshal.FreeHGlobal(packetBuffer); Close(); SendCaptureStoppedEvent(CaptureStoppedEventStatus.ErrorWhileCapturing); return; } var bufferEnd = new IntPtr(packetBuffer.ToInt64() + (long)BytesReceived); MarshalPackets(packetBuffer, bufferEnd, out packets); foreach (var p in packets) { SendPacketArrivalEvent(p); } // wait until some packets are available. This prevents polling and keeps the CPU low. Win32SafeNativeMethods.WaitForSingleObject(ReadEvent, WaitIntervalMilliseconds); } Marshal.FreeHGlobal(packetBuffer); }
/// <summary> /// Constructor /// </summary> /// <param name="ledIndex"> /// A <see cref="System.Int32"/> /// </param> /// <param name="newLedState"> /// A <see cref="LedState"/> /// </param> public void Led(int ledIndex, LedState newLedState) { ThrowIfNotOpen(); if (newLedState == LedState.On) { AirPcapSafeNativeMethods.AirpcapTurnLedOn(AirPcapDeviceHandle, (UInt32)ledIndex); } else if (newLedState == LedState.Off) { AirPcapSafeNativeMethods.AirpcapTurnLedOff(AirPcapDeviceHandle, (UInt32)ledIndex); } }
internal AirPcapStatistics(IntPtr AirPcapDeviceHandle) { // allocate memory for the struct IntPtr stat = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(AirPcapUnmanagedStructures.AirpcapStats))); AirPcapSafeNativeMethods.AirpcapGetStats(AirPcapDeviceHandle, stat); var managedStat = (AirPcapUnmanagedStructures.AirpcapStats)Marshal.PtrToStructure(stat, typeof(AirPcapUnmanagedStructures.AirpcapStats)); this.ReceivedPackets = managedStat.Recvs; this.DroppedPackets = managedStat.Drops; this.InterfaceDroppedPackets = managedStat.IfDrops; this.CapturedPackets = managedStat.Capt; // free the stats memory so we don't leak Marshal.FreeHGlobal(stat); }
/// <summary> /// Retrieve the last error string for a given pcap_t* device /// </summary> /// <param name="AirPcapDeviceHandle"> /// A <see cref="IntPtr"/> /// </param> /// <returns> /// A <see cref="System.String"/> /// </returns> new internal static string GetLastError(IntPtr AirPcapDeviceHandle) { IntPtr err_ptr = AirPcapSafeNativeMethods.AirpcapGetLastError(AirPcapDeviceHandle); return(Marshal.PtrToStringAnsi(err_ptr)); }