Пример #1
0
        /// <summary>
        /// Writes a packet to the pcap dump file associated with this device.
        /// </summary>
        /// <param name="p">The packet to write</param>
        public void Dump(byte[] p, PcapHeader h)
        {
            if (!Opened)
            {
                throw new PcapDeviceNotReadyException("Cannot dump packet, device is not opened");
            }
            if (!DumpOpened)
            {
                throw new PcapDeviceNotReadyException("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();

            SafeNativeMethods.pcap_dump(m_pcapDumpHandle, hdrPtr, pktPtr);

            Marshal.FreeHGlobal(pktPtr);
            Marshal.FreeHGlobal(hdrPtr);
        }
Пример #2
0
        /// <summary>
        /// Writes a packet to the pcap dump file associated with this device.
        /// </summary>
        /// <param name="p">The packet to write</param>
        public void Dump(byte[] p, PcapHeader h)
        {
            if (!Opened)
            {
                throw new PcapDeviceNotReadyException("Cannot dump packet, device is not opened");
            }
            if (!DumpOpened)
            {
                throw new PcapDeviceNotReadyException("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;

            hdrPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(PcapUnmanagedStructures.pcap_pkthdr)));
            Marshal.StructureToPtr(h.m_pcap_pkthdr, hdrPtr, true);

            SafeNativeMethods.pcap_dump(m_pcapDumpHandle, hdrPtr, pktPtr);

            Marshal.FreeHGlobal(pktPtr);
            Marshal.FreeHGlobal(hdrPtr);
        }
Пример #3
0
        /// <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)
            {
                string 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 = SafeNativeMethods.pcap_sendqueue_queue(m_queue, hdrPtr, pktPtr);

            Marshal.FreeHGlobal(pktPtr);
            Marshal.FreeHGlobal(hdrPtr);

            return(res != -1);
        }
Пример #4
0
        /// <summary>
        /// Add a packet to this send queue.
        /// </summary>
        /// <param name="packet">The packet bytes to add</param>
        /// <returns>True if success, else false</returns>
        public bool Add(byte[] packet)
        {
            PcapHeader hdr = new PcapHeader();

            hdr.CaptureLength = (uint)packet.Length;
            return(this.AddInternal(packet, hdr));
        }
Пример #5
0
        /// <summary>
        /// Add a packet to this send queue.
        /// </summary>
        /// <param name="packet">The packet to add</param>
        /// <param name="seconds">The 'seconds' part of the packet's timestamp</param>
        /// <param name="microseconds">The 'microseconds' part of the packet's timestamp</param>
        /// <returns>True if success, else false</returns>
        public bool Add(byte[] packet, int seconds, int microseconds)
        {
            PcapHeader header = new PcapHeader((ulong)seconds, (ulong)microseconds,
                                               (uint)packet.Length, (uint)packet.Length);

            return(this.Add(packet, header));
        }
Пример #6
0
        protected virtual RawPacket MarshalRawPacket(IntPtr /* pcap_pkthdr* */ header, IntPtr data)
        {
            RawPacket p;

            // marshal the header
            PcapHeader pcapHeader = new PcapHeader(header);

            byte[] pkt_data = new byte[pcapHeader.CaptureLength];
            Marshal.Copy(data, pkt_data, 0, (int)pcapHeader.CaptureLength);

            p = new RawPacket(PcapDataLink,
                              new Packets.Util.Timeval(pcapHeader.Seconds,
                                                       pcapHeader.MicroSeconds),
                              pkt_data);

            return(p);
        }
Пример #7
0
        protected virtual Packet MarshalPacket(IntPtr header, IntPtr data)
        {
            Packet p;

            // marshal the header
            PcapHeader pcapHeader = new PcapHeader(header);

            byte[] pkt_data = new byte[pcapHeader.CaptureLength];
            Marshal.Copy(data, pkt_data, 0, (int)pcapHeader.CaptureLength);

            p = Packets.PacketFactory.dataToPacket(PcapDataLink, pkt_data,
                                                   new Packets.Util.Timeval(pcapHeader.Seconds,
                                                                            pcapHeader.MicroSeconds));
            p.pcapHeader = pcapHeader;

            return(p);
        }
Пример #8
0
 /// <summary>
 /// Add a packet to this send queue. 
 /// </summary>
 /// <param name="packet">The packet bytes to add</param>
 /// <returns>True if success, else false</returns>
 public bool Add(byte[] packet)
 {
     PcapHeader hdr = new PcapHeader();
     hdr.CaptureLength = (uint)packet.Length;
     return this.AddInternal(packet, hdr);
 }
 internal PcapStatisticsModePacket(Packets.Packet p)
 {
     this.m_pktHdr  = p.PcapHeader;
     this.m_pktData = p.Bytes;
 }
Пример #10
0
        /// <summary>
        /// Writes a packet to the pcap dump file associated with this device.
        /// </summary>
        /// <param name="p">The packet to write</param>
        public void Dump(byte[] p, PcapHeader h)
        {
            if(!Opened)
                throw new PcapDeviceNotReadyException("Cannot dump packet, device is not opened");
            if(!DumpOpened)
                throw new PcapDeviceNotReadyException("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;
            hdrPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(PcapUnmanagedStructures.pcap_pkthdr)));
            Marshal.StructureToPtr(h.m_pcap_pkthdr, hdrPtr, true);

            SafeNativeMethods.pcap_dump(m_pcapDumpHandle, hdrPtr, pktPtr);

            Marshal.FreeHGlobal(pktPtr);
            Marshal.FreeHGlobal(hdrPtr);
        }
Пример #11
0
 /// <summary>
 /// Add a packet to this send queue.
 /// </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 Add(byte[] packet, PcapHeader pcapHdr)
 {
     return(this.AddInternal(packet, pcapHdr));
 }
Пример #12
0
        protected virtual RawPacket MarshalRawPacket(IntPtr /* pcap_pkthdr* */ header, IntPtr data)
        {
            RawPacket p;

            // marshal the header
            PcapHeader pcapHeader = new PcapHeader(header);

            byte[] pkt_data = new byte[pcapHeader.CaptureLength];
            Marshal.Copy(data, pkt_data, 0, (int)pcapHeader.CaptureLength);

            p = new RawPacket(PcapDataLink,
                              new Packets.Util.Timeval(pcapHeader.Seconds,
                                                       pcapHeader.MicroSeconds),
                              pkt_data);

            return p;
        }
Пример #13
0
        protected virtual Packet MarshalPacket(IntPtr header, IntPtr data)
        {
            Packet p;

            // marshal the header
            PcapHeader pcapHeader = new PcapHeader(header);

            byte[] pkt_data = new byte[pcapHeader.CaptureLength];
            Marshal.Copy(data, pkt_data, 0, (int)pcapHeader.CaptureLength);

            p = Packets.PacketFactory.dataToPacket(PcapDataLink, pkt_data,
                                                   new Packets.Util.Timeval(pcapHeader.Seconds,
                                                                            pcapHeader.MicroSeconds));
            p.pcapHeader = pcapHeader;

            return p;
        }
Пример #14
0
        /// <summary>
        /// Writes a packet to the pcap dump file associated with this device.
        /// </summary>
        /// <param name="p">The packet to write</param>
        public void Dump(byte[] p, PcapHeader h)
        {
            if (!Opened)
                throw new PcapDeviceNotReadyException("Cannot dump packet, device is not opened");
            if (!DumpOpened)
                throw new PcapDeviceNotReadyException("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();

            SafeNativeMethods.pcap_dump(m_pcapDumpHandle, hdrPtr, pktPtr);

            Marshal.FreeHGlobal(pktPtr);
            Marshal.FreeHGlobal(hdrPtr);
        }
 /// <summary>
 /// Constructs a new Pcap Statistics strcuture
 /// </summary>
 /// <param name="pktHdr">Time value as PCAP_PKTHDR</param>
 /// <param name="pktData">Statistics values as PCAP_PKTDATA</param>
 internal PcapStatisticsModePacket(PcapHeader pktHdr, PcapUnmanagedStructures.PCAP_PKTDATA pktData)
 {
     this.m_pktHdr = pktHdr;
     this.m_pktData = pktData.bytes;
 }
Пример #16
0
        /// <summary>
        /// Add a packet to this send queue.
        /// </summary>
        /// <param name="packet">The packet to add</param>
        /// <param name="seconds">The 'seconds' part of the packet's timestamp</param>
        /// <param name="microseconds">The 'microseconds' part of the packet's timestamp</param>
        /// <returns>True if success, else false</returns>
        public bool Add(byte[] packet, int seconds, int microseconds)
        {
            PcapHeader header = new PcapHeader((ulong)seconds, (ulong)microseconds,
                                        (uint)packet.Length, (uint)packet.Length);

            return this.Add(packet, header);
        }
 internal PcapStatisticsModePacket(Packets.Packet p)
 {
     this.m_pktHdr = p.PcapHeader;
     this.m_pktData = p.Bytes;
 }
Пример #18
0
 /// <summary>
 /// Add a packet to this send queue. 
 /// </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 Add(byte[] packet, PcapHeader pcapHdr)
 {
     return this.AddInternal(packet, pcapHdr);
 }
Пример #19
0
 /// <summary>
 /// Add a packet to this send queue. 
 /// </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 Add( byte[] packet, PcapHeader pcapHdr )
 {
     return this.Add( packet, pcapHdr.m_pcap_pkthdr);
 }
Пример #20
0
        /// <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)
            {
                string 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 = SafeNativeMethods.pcap_sendqueue_queue(m_queue, hdrPtr, pktPtr);

            Marshal.FreeHGlobal(pktPtr);
            Marshal.FreeHGlobal(hdrPtr);

            return (res != -1);
        }
Пример #21
0
 /// <summary>
 /// Add a packet to this send queue.
 /// </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 Add(byte[] packet, PcapHeader pcapHdr)
 {
     return(this.Add(packet, pcapHdr.m_pcap_pkthdr));
 }
 /// <summary>
 /// Constructs a new Pcap Statistics strcuture
 /// </summary>
 /// <param name="pktHdr">Time value as PCAP_PKTHDR</param>
 /// <param name="pktData">Statistics values as PCAP_PKTDATA</param>
 internal PcapStatisticsModePacket(PcapHeader pktHdr, PcapUnmanagedStructures.PCAP_PKTDATA pktData)
 {
     this.m_pktHdr  = pktHdr;
     this.m_pktData = pktData.bytes;
 }