Esempio n. 1
0
        public static BpfProgram Create(PcapHandle pcapHandle, string filter, int optimize = 1, uint netmask = 0)
        {
            var bpfProgram = TryCreate(pcapHandle, filter, optimize, netmask);

            if (bpfProgram == null)
            {
                throw new PcapException(PcapDevice.GetLastError(pcapHandle));
            }
            return(bpfProgram);
        }
Esempio n. 2
0
        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);
        }
Esempio n. 3
0
        protected unsafe int NativeTransmit(PcapDevice device, bool synchronized)
        {
            int sync = synchronized ? 1 : 0;

            fixed(byte *buf = buffer)
            {
                var pcap_queue = new pcap_send_queue
                {
                    maxlen  = (uint)buffer.Length,
                    len     = (uint)CurrentLength,
                    ptrBuff = new IntPtr(buf)
                };

                return(LibPcapSafeNativeMethods.pcap_sendqueue_transmit(device.PcapHandle, ref pcap_queue, sync));
            }
        }
Esempio n. 4
0
 /// <summary>
 /// Send a queue of raw packets to the network.
 /// </summary>
 /// <param name="device">
 /// The device on which to send the queue
 /// A <see cref="PcapDevice"/>
 /// </param>
 /// <param name="synchronized">
 /// Should the timestamps be respected
 /// </param>
 /// <returns>
 /// A <see cref="int"/>
 /// </returns>
 public int Transmit(PcapDevice device, bool synchronized)
 {
     if (buffer == null)
     {
         throw new ObjectDisposedException(nameof(SendQueue));
     }
     if (!device.Opened)
     {
         throw new DeviceNotReadyException("Can't transmit queue, the pcap device is closed");
     }
     if (IsHardwareAccelerated)
     {
         return(NativeTransmit(device, synchronized));
     }
     return(ManagedTransmit(device, synchronized));
 }
Esempio n. 5
0
        protected unsafe int NativeTransmit(PcapDevice device, SendQueueTransmitModes transmitMode)
        {
            if (CurrentLength == 0)
            {
                // Npcap does not properly check for 0 packets queue
                // See https://github.com/nmap/npcap/issues/287
                return(0);
            }
            int sync = (transmitMode == SendQueueTransmitModes.Synchronized) ? 1 : 0;

            fixed(byte *buf = buffer)
            {
                var pcap_queue = new pcap_send_queue
                {
                    maxlen  = (uint)buffer.Length,
                    len     = (uint)CurrentLength,
                    ptrBuff = new IntPtr(buf)
                };

                return(LibPcapSafeNativeMethods.pcap_sendqueue_transmit(device.PcapHandle, ref pcap_queue, sync));
            }
        }
 /// <summary>
 /// Constructor for a statistics mode event
 /// </summary>
 /// <param name="packet">
 /// A <see cref="RawCapture"/>
 /// </param>
 /// <param name="device">
 /// A <see cref="PcapDevice"/>
 /// </param>
 public StatisticsModeEventArgs(RawCapture packet, PcapDevice device)
     : base(packet, device)
 {
 }
Esempio n. 7
0
 /// <summary>
 /// Send a queue of raw packets to the network.
 /// </summary>
 /// <param name="device">
 /// The device on which to send the queue
 /// A <see cref="PcapDevice"/>
 /// </param>
 /// <param name="synchronized">
 /// Should the timestamps be respected
 /// </param>
 /// <returns>
 /// The number of bytes sent as an <see cref="int"/>
 /// </returns>
 public int Transmit(PcapDevice device, bool synchronized)
 {
     return(Transmit(device, (synchronized == true) ? SendQueueTransmitModes.Synchronized : SendQueueTransmitModes.Normal));
 }