/// <summary> /// Sends a raw packet throgh this device /// </summary> /// <param name="p">The packet bytes to send</param> /// <param name="size">The number of bytes to send</param> public override void SendPacket(byte[] p, int size) { ThrowIfNotOpen("Can't send packet, the device is closed"); if (size > p.Length) { throw new ArgumentException("Invalid packetSize value: " + size + "\nArgument size is larger than the total size of the packet."); } if (p.Length > Pcap.MAX_PACKET_SIZE) { throw new ArgumentException("Packet length can't be larger than " + Pcap.MAX_PACKET_SIZE); } IntPtr p_packet = IntPtr.Zero; p_packet = Marshal.AllocHGlobal(size); Marshal.Copy(p, 0, p_packet, size); int res = LibPcapSafeNativeMethods.pcap_sendpacket(PcapHandle, p_packet, size); Marshal.FreeHGlobal(p_packet); if (res < 0) { throw new PcapException("Can't send packet: " + LastError); } }
protected unsafe int ManagedTransmit(PcapDevice device, bool synchronized) { if (CurrentLength == 0) { return(0); } var position = 0; var hdrSize = PcapHeader.MemorySize; var sw = new Stopwatch(); fixed(byte *buf = buffer) { var bufPtr = new IntPtr(buf); var firstTimestamp = TimeSpan.FromTicks(PcapHeader.FromPointer(bufPtr).Date.Ticks); while (position < CurrentLength) { // Extract packet from buffer var header = PcapHeader.FromPointer(bufPtr + position); var pktSize = (int)header.CaptureLength; var p = new ReadOnlySpan <byte>(buffer, position + hdrSize, pktSize); if (synchronized) { var timestamp = TimeSpan.FromTicks(header.Date.Ticks); var remainingTime = timestamp.Subtract(firstTimestamp); while (sw.Elapsed < remainingTime) { // Wait for packet time System.Threading.Thread.Sleep((int)remainingTime.TotalMilliseconds / 2); } } // Send the packet int res; unsafe { fixed(byte *p_packet = p) { res = LibPcapSafeNativeMethods.pcap_sendpacket(device.PcapHandle, new IntPtr(p_packet), p.Length); } } // Start Stopwatch after sending first packet sw.Start(); if (res < 0) { break; } position += hdrSize + pktSize; } } return(position); }
/// <summary> /// Sends a raw packet through this device /// </summary> /// <param name="p">The packet bytes to send</param> public void SendPacket(ReadOnlySpan <byte> p, ICaptureHeader header = null) { ThrowIfNotOpen("Can't send packet, the device is closed"); int res; unsafe { fixed(byte *p_packet = p) { res = LibPcapSafeNativeMethods.pcap_sendpacket(Handle, new IntPtr(p_packet), p.Length); } } if (res < 0) { throw new PcapException("Can't send packet: " + LastError); } }
/// <summary> /// Sends a raw packet throgh this device /// </summary> /// <param name="p">The packet bytes to send</param> /// <param name="size">The number of bytes to send</param> public override void SendPacket(ReadOnlySpan <byte> p) { ThrowIfNotOpen("Can't send packet, the device is closed"); if (p.Length > Pcap.MAX_PACKET_SIZE) { throw new ArgumentException("Packet length can't be larger than " + Pcap.MAX_PACKET_SIZE); } int res; unsafe { fixed(byte *p_packet = p) { res = LibPcapSafeNativeMethods.pcap_sendpacket(PcapHandle, new IntPtr(p_packet), p.Length); } } if (res < 0) { throw new PcapException("Can't send packet: " + LastError); } }