void wincap_OnReceivePacket(object sender, PacketHeader p, byte[] s) { if (p.Caplength >= p.Length) // no point in trying to handle incomplete packets { PacketInfo data = new PacketInfo(); data.Data = s; data.TimeStamp = p.TimeStamp; data.Length = (uint)s.Length; data.StartIndex = 14; nrOfCapturedPackets++; if (DataReceived != null) DataReceived(data); } }
// out param so that we can change assign p and s to new mem locations, // since ReadNext calls 'new' to p and s. /// <summary> /// Read the next available packet from the capture driver. /// Returns if timeout specified in Open elapsed, or a packet has been /// forwarded by the capture driver. /// </summary> /// <param name="p">Packet header of the packet returned</param> /// <param name="packet_data">Packet data of the packet returned</param> /// <returns></returns> private PCAP_NEXT_EX_STATE ReadNextInternal(out PacketHeader p, out byte[] packet_data, out IntPtr pkthdr, out IntPtr pktdata) { pkthdr = IntPtr.Zero; pktdata = IntPtr.Zero; p = null; packet_data = null; if (this.pcap_t.ToInt32() == 0) { this.errbuf = new StringBuilder("No adapter is currently open"); return PCAP_NEXT_EX_STATE.ERROR; } int ret = pcap_next_ex(this.pcap_t, ref pkthdr, ref pktdata); //System.Diagnostics.Debug.WriteLine(this.pcap_t); if (ret == 0) { return PCAP_NEXT_EX_STATE.TIMEOUT; } else if (ret == 1) { pcap_pkthdr packetheader = (pcap_pkthdr)Marshal.PtrToStructure(pkthdr, typeof(pcap_pkthdr)); p = new PacketHeader(); p.Caplength = (int)packetheader.caplen; p.Length = (int)packetheader.len; p.ts = packetheader.ts; packet_data = new byte[p.Length]; Marshal.Copy(pktdata, packet_data, 0, p.Caplength); return PCAP_NEXT_EX_STATE.SUCCESS; } else if (ret == -1) { return PCAP_NEXT_EX_STATE.ERROR; } else if (ret == -2) { return PCAP_NEXT_EX_STATE.EOF; } else { return PCAP_NEXT_EX_STATE.UNKNOWN; } }
/// <summary> /// Read the next available packet from the capture driver. /// Returns if timeout specified in Open elapsed, or a packet has been /// forwarded by the capture driver. /// </summary> /// <param name="p">Packet header of the packet returned</param> /// <param name="packet_data">Packet data of the packet returned</param> /// <returns></returns> public PCAP_NEXT_EX_STATE ReadNextInternal(out PacketHeader p, out byte[] packet_data) { IntPtr dummy; return(this.ReadNextInternal(out p, out packet_data, out dummy, out dummy)); }
/// <summary> /// Read the next available packet from the capture driver. /// Returns if timeout specified in Open elapsed, or a packet has been /// forwarded by the capture driver. /// </summary> /// <param name="p">Packet header of the packet returned</param> /// <param name="packet_data">Packet data of the packet returned</param> /// <returns></returns> public PCAP_NEXT_EX_STATE ReadNextInternal(out PacketHeader p, out byte[] packet_data) { IntPtr dummy; return this.ReadNextInternal(out p, out packet_data, out dummy, out dummy); }