private void ReceivePacketListener(IAsyncResult result) { //try { int received = socket.EndReceive(result); try { byte[] data = new byte[received]; Array.Copy(buffer, 0, data, 0, received); /* * //ensure that the IP type is correct * if(this.basePacketType==PacketReceivedEventArgs.PacketTypes.IPv4Packet) { * if(data!=null && data.Length>0 && (data[0]>>4)==0x06) * this.basePacketType= PacketReceivedEventArgs.PacketTypes.IPv6Packet; * } * else if(this.basePacketType== PacketReceivedEventArgs.PacketTypes.IPv6Packet) { * if(data!=null && data.Length>0 && (data[0]>>4)==0x04) * this.basePacketType= PacketReceivedEventArgs.PacketTypes.IPv4Packet; * } * */ PacketReceivedEventArgs eventArgs = new PacketReceivedEventArgs(data, DateTime.Now, this.BasePacketType); PacketReceived(this, eventArgs); //OnNewPacket(new Packet(packet)); } catch { } // invalid packet; ignore if (this.snifferActive) { socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(this.ReceivePacketListener), null); } // } //catch { //Stop(); //} }
//callback-function in order to remove packets from dotnetWinpCap //private void ReceivePacketListener(object sender, PacketHeader ph, byte[] data) { private void ReceivePacketListener(object sender, NetworkWrapper.PcapHeader ph, byte[] data) { nPacketsReceived++; //detect the DataLinkType (see PcapFileReader) to see if it is Ethernet or WLAN //At the moment I will just assume it is Ethernet2 PacketReceivedEventArgs eventArgs = new PacketReceivedEventArgs(data, ph.TimeStamp, this.basePacketType); PacketReceived(this, eventArgs); }
void packetQueue_OnPacketReceive(object sender, IntPtr ThParam, IntPtr hPacket, IntPtr pPacketData, uint IncPacketSize) { //throw new Exception("The method or operation is not implemented."); PacketReceivedEventArgs.PacketTypes packetBaseType; //byte* pPkt = (byte*)pPacketData; microOLAP.PSSDK.HNPacket packet = new microOLAP.PSSDK.HNPacket(); packet.Handle = hPacket; if (packet.MediumType == microOLAP.PSSDK.HNNetAdapterType.atEthernet) { packetBaseType = PacketReceivedEventArgs.PacketTypes.Ethernet2Packet; } else//I think it will be ethernet always... { packetBaseType = PacketReceivedEventArgs.PacketTypes.Ethernet2Packet; } byte[] byteArray = new byte[packet.PacketSize]; System.Runtime.InteropServices.Marshal.Copy(packet.PacketData, byteArray, 0, byteArray.Length); DateTime packetTimestamp; if (packet.TimeStamp < (new DateTime(1970, 1, 1)).Ticks) { packetTimestamp = new DateTime(1601, 1, 1).AddTicks(packet.TimeStamp).ToLocalTime(); } else { packetTimestamp = new DateTime(packet.TimeStamp).ToLocalTime(); } PacketReceivedEventArgs eventArgs = new PacketReceivedEventArgs(byteArray, packetTimestamp, packetBaseType); PacketReceived(this, eventArgs); //PacketReceived(this, /*unsafe { * byte* packetDataPointer=(byte*)pPacketData; * * PacketReceivedEventArgs eventArgs=new PacketReceivedEventArgs((byte[])packetDataPointer, new DateTime(packet.TimeStamp), packetBaseType); * PacketReceived(this, eventArgs); * }*/ }