Example #1
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) {
				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>
        /// Writes a packet to the pcap dump file associated with this device.
        /// </summary>
        /// <param name="p">The packet to write</param>
        public void Write(RawCapture p)
        {
            var data    = p.Data;
            var timeval = p.Timeval;
            var header  = new PcapHeader((uint)timeval.Seconds, (uint)timeval.MicroSeconds,
                                         (uint)data.Length, (uint)data.Length);

            Write(data, header);
        }
Example #3
0
        /// <summary>
        /// Convert an unmanaged packet into a managed GodLesZ.Library.Network.Packet.RawPacket
        /// </summary>
        /// <param name="header">
        /// A <see cref="IntPtr"/>
        /// </param>
        /// <param name="data">
        /// A <see cref="IntPtr"/>
        /// </param>
        /// <returns>
        /// A <see cref="RawCapture"/>
        /// </returns>
        protected virtual RawCapture MarshalRawPacket(IntPtr /* pcap_pkthdr* */ header, IntPtr data)
        {
            RawCapture p;

            // marshal the header
            var pcapHeader = PcapHeader.FromPointer(header);

            var pkt_data = new byte[pcapHeader.CaptureLength];

            Marshal.Copy(data, pkt_data, 0, (int)pcapHeader.CaptureLength);

            p = new RawCapture(LinkType,
                               new PosixTimeval(pcapHeader.Seconds,
                                                pcapHeader.MicroSeconds),
                               pkt_data);

            return(p);
        }
        /// <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);
        }
Example #5
0
		/// <summary>
		/// Add a packet to this send queue. 
		/// </summary>
		/// <param name="packet">The packet to add</param>
		/// <returns>True if success, else false</returns>
		public bool Add(RawCapture packet) {
			var data = packet.Data;
			var timeval = packet.Timeval;
			var header = new PcapHeader((uint)timeval.Seconds, (uint)timeval.MicroSeconds,
										(uint)data.Length, (uint)data.Length);
			return this.AddInternal(data, header);
		}
Example #6
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);
		}
Example #7
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);
		}
Example #8
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) {
			var header = new PcapHeader((uint)seconds, (uint)microseconds,
										(uint)packet.Length, (uint)packet.Length);

			return this.Add(packet, header);
		}
		/// <summary>
		/// Writes a packet to the pcap dump file associated with this device.
		/// </summary>
		/// <param name="p">The packet to write</param>
		public void Write(RawCapture p) {
			var data = p.Data;
			var timeval = p.Timeval;
			var header = new PcapHeader((uint)timeval.Seconds, (uint)timeval.MicroSeconds,
										(uint)data.Length, (uint)data.Length);
			Write(data, header);
		}
		/// <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);
		}