/// <summary> /// Add a packet to this send queue. The PcapHeader defines the packet length. /// </summary> /// <param name="header">The pcap header of the packet</param> /// <param name="packet">The packet bytes to add</param> /// <returns>True if success, else false</returns> public bool Add(PcapHeader header, byte[] packet) { if (buffer == null) { throw new ObjectDisposedException(nameof(SendQueue)); } var hdrSize = PcapHeader.MemorySize; var pktSize = (int)header.CaptureLength; // the header defines the size to send if (pktSize > packet.Length) { var error = string.Format("pcapHdr.CaptureLength of {0} > packet.Length {1}", pktSize, packet.Length); throw new InvalidOperationException(error); } if (hdrSize + pktSize > buffer.Length - CurrentLength) { return(false); } //Marshal header IntPtr hdrPtr = header.MarshalToIntPtr(); Marshal.Copy(hdrPtr, buffer, CurrentLength, hdrSize); Marshal.FreeHGlobal(hdrPtr); Buffer.BlockCopy(packet, 0, buffer, CurrentLength + hdrSize, pktSize); CurrentLength += hdrSize + pktSize; return(true); }
/// <summary> /// Add a packet to this send queue. The PcapHeader defines the packet length. /// </summary> /// <param name="packet">The packet bytes to add</param> /// <param name="pcapHdr">The pcap header of the packet</param> /// <returns>True if success, else false</returns> internal bool AddInternal( byte[] packet, PcapHeader pcapHdr ) { if(m_queue==IntPtr.Zero) { throw new PcapException("Can't add packet, this queue is disposed"); } // the header defines the size to send if(pcapHdr.CaptureLength > packet.Length) { var error = string.Format("pcapHdr.CaptureLength of {0} > packet.Length {1}", pcapHdr.CaptureLength, packet.Length); throw new System.InvalidOperationException(error); } //Marshal packet IntPtr pktPtr; pktPtr = Marshal.AllocHGlobal(packet.Length); Marshal.Copy(packet, 0, pktPtr, packet.Length); //Marshal header IntPtr hdrPtr = pcapHdr.MarshalToIntPtr(); int res = WinPcap.SafeNativeMethods.pcap_sendqueue_queue( m_queue, hdrPtr, pktPtr); Marshal.FreeHGlobal(pktPtr); Marshal.FreeHGlobal(hdrPtr); return (res!=-1); }
/// <summary> /// Runs the program and returns if a given filter applies to the packet /// </summary> /// <param name="bpfProgram"> /// A <see cref="IntPtr"/> /// </param> public bool Matches(ReadOnlySpan <byte> data) { var header = new PcapHeader(0, 0, (uint)data.Length, (uint)data.Length); var hdrPtr = header.MarshalToIntPtr(TimestampResolution.Microsecond); int result; unsafe { fixed(byte *p_packet = data) { result = LibPcapSafeNativeMethods.pcap_offline_filter(this, hdrPtr, new IntPtr(p_packet)); } } Marshal.FreeHGlobal(hdrPtr); return(result != 0); }
/// <summary> /// Runs the program and returns if a given filter applies to the packet /// </summary> /// <param name="bpfProgram"> /// A <see cref="IntPtr"/> /// </param> public bool Matches(ReadOnlySpan <byte> data) { var header = new PcapHeader() { CaptureLength = (uint)data.Length, PacketLength = (uint)data.Length, }; IntPtr hdrPtr = header.MarshalToIntPtr(); int result; unsafe { fixed(byte *p_packet = data) { result = LibPcapSafeNativeMethods.pcap_offline_filter(this, hdrPtr, new IntPtr(p_packet)); } } Marshal.FreeHGlobal(hdrPtr); return(result != 0); }
/// <summary> /// Writes a packet to the pcap dump file associated with this device. /// </summary> /// <param name="p">P.</param> /// <param name="h">The height.</param> public void Write(ReadOnlySpan <byte> p, ref PcapHeader h) { ThrowIfNotOpen("Cannot dump packet, device is not opened"); if (!DumpOpened) { throw new DeviceNotReadyException("Cannot dump packet, dump file is not opened"); } //Marshal header IntPtr hdrPtr = h.MarshalToIntPtr(); unsafe { fixed(byte *p_packet = p) { LibPcapSafeNativeMethods.pcap_dump(m_pcapDumpHandle, hdrPtr, new IntPtr(p_packet)); } } Marshal.FreeHGlobal(hdrPtr); }
/// <summary> /// Writes a packet to the pcap dump file associated with this device. /// </summary> public void Write(byte[] p, PcapHeader h) { ThrowIfNotOpen("Cannot dump packet, device is not opened"); if (!DumpOpened) { throw new DeviceNotReadyException("Cannot dump packet, dump file is not opened"); } //Marshal packet IntPtr pktPtr; pktPtr = Marshal.AllocHGlobal(p.Length); Marshal.Copy(p, 0, pktPtr, p.Length); //Marshal header IntPtr hdrPtr = h.MarshalToIntPtr(); LibPcapSafeNativeMethods.pcap_dump(m_pcapDumpHandle, hdrPtr, pktPtr); Marshal.FreeHGlobal(pktPtr); Marshal.FreeHGlobal(hdrPtr); }
/// <summary> /// Writes a packet to the pcap dump file associated with this device. /// </summary> public void Dump(byte[] p, PcapHeader h) { ThrowIfNotOpen("Cannot dump packet, device is not opened"); if(!DumpOpened) throw new DeviceNotReadyException("Cannot dump packet, dump file is not opened"); //Marshal packet IntPtr pktPtr; pktPtr = Marshal.AllocHGlobal(p.Length); Marshal.Copy(p, 0, pktPtr, p.Length); //Marshal header IntPtr hdrPtr = h.MarshalToIntPtr(); LibPcapSafeNativeMethods.pcap_dump(m_pcapDumpHandle, hdrPtr, pktPtr); Marshal.FreeHGlobal(pktPtr); Marshal.FreeHGlobal(hdrPtr); }