Esempio n. 1
0
        /// <summary>
        /// </summary>
        /// <param name="p"></param>
        /// <param name="header"></param>
        public void SendPacket(ReadOnlySpan <byte> p, ICaptureHeader captureHeader)
        {
            ThrowIfNotOpen();
            bool             res;
            WinDivertAddress addr = default;

            if (!(captureHeader is WinDivertHeader))
            {
                addr = GetAddress(p);
            }
            else
            {
                var header = captureHeader as WinDivertHeader;
                addr.IfIdx    = header.InterfaceIndex;
                addr.SubIfIdx = header.SubInterfaceIndex;
                addr.Flags    = header.Flags;
            }

            unsafe
            {
                fixed(byte *p_packet = p)
                {
                    res = WinDivertNative.WinDivertSend(Handle, new IntPtr(p_packet), (uint)p.Length, out var pSendLen, ref addr);
                }
            }
            if (!res)
            {
                ThrowLastWin32Error("Can't send packet");
            }
        }
Esempio n. 2
0
 public RawCapture(ICaptureDevice device, ICaptureHeader header, System.ReadOnlySpan <byte> data)
 {
     this.LinkLayerType = device.LinkType;
     this.Timeval       = header.Timeval;
     this.Data          = data.ToArray();
     this.PacketLength  = Data?.Length ?? 0;
 }
Esempio n. 3
0
        /// <summary>
        /// Sends a raw packet through this device
        /// </summary>
        /// <param name="p">The packet bytes to send</param>
        /// <param name="header">The packet header</param>
        public void SendPacket(ReadOnlySpan <byte> p, ICaptureHeader header = null)
        {
            if (p.Length > NativeMethods.MAX_ETHER_FRAME)
            {
                throw new ArgumentOutOfRangeException(nameof(p));
            }
            var hdr    = header as WinpkFilterHeader;
            var buffer = new IntermediateBuffer();

            buffer.Header.Dot1q  = hdr?.Dot1q ?? 0;
            buffer.Header.Source = hdr?.Source ?? PacketSource.System;
            buffer.Header.Length = (uint)p.Length;
            EthRequest ethRequest;

            unsafe
            {
                p.CopyTo(new Span <byte>(buffer.Frame, p.Length));
                ethRequest.AdapterHandle = AdapterHandle;
                ethRequest.Buffer        = new IntPtr(&buffer);
            }

            var ret = buffer.Header.Source == PacketSource.System
                ? NativeMethods.SendPacketToAdapter(DriverHandle, ref ethRequest)
                : NativeMethods.SendPacketToMstcp(DriverHandle, ref ethRequest);

            if (!ret)
            {
                throw new PcapException("Failed to send packet");
            }
        }
Esempio n. 4
0
        /// <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);
            }
        }
Esempio n. 5
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="packet">
 /// A <see cref="RawCapture"/>
 /// </param>
 /// <param name="device">
 /// A <see cref="ICaptureDevice"/>
 /// </param>
 public PacketCapture(ICaptureDevice device, ICaptureHeader header, ReadOnlySpan <byte> data)
 {
     this.Header = header;
     this.Device = device;
     this.Data   = data;
 }
Esempio n. 6
0
 void IInjectionDevice.SendPacket(ReadOnlySpan <byte> p, ICaptureHeader header)
 {
     Write(p);
 }
Esempio n. 7
0
 /// <summary>
 /// Send a raw packet through this device
 /// </summary>
 /// <param name="device"></param>
 /// <param name="p"></param>
 /// <param name="header"></param>
 public static void SendPacket(this IInjectionDevice device, RawCapture p, ICaptureHeader header = null)
 {
     device.SendPacket(new ReadOnlySpan <byte>(p.Data), header);
 }