public PacketInfo(PacketIP ip, PacketTcp tcp, string hex, string ascii) { _ip = ip; _tcp = tcp; packetHex = hex; packetAscii = ascii; }
private void ParseData(byte[] data, int numReceived) { string packetHex; string packetAscii; if (data.Length > 0 && numReceived != 0) { //Parse the IP packet PacketIP ipPacket = new PacketIP(data, numReceived); if (ipPacket.Protocol == "TCP") { //Make the key of the packet equal to the number of packets that have been received thus far string strKey = (numPacketsReceived + 1).ToString(); //Parse the TCP packet PacketTcp tcpPacket = new PacketTcp(ipPacket.Data, ipPacket.MessageLength); //Convert the packet's bytes to Hex and ASCII packetHex = BitConverter.ToString(byteData).Replace("-", String.Empty).Substring(0, numReceived * 2); packetAscii = Encoding.ASCII.GetString(byteData).Substring(0, numReceived); //Create a PacketInfo object to store in the dictionary PacketInfo pkgInfo = new PacketInfo(ipPacket, tcpPacket, packetHex, packetAscii); //Create a PacketData object to populate the datagrid PacketData packet = new PacketData { Number = (numPacketsReceived + 1).ToString(), Time_Stamp = DateTime.Now.ToString("HH:mm:ss:") + DateTime.Now.Millisecond.ToString(), Source = ipPacket.SourceAddress.ToString() + ":" + tcpPacket.SourcePort, Destination = ipPacket.DestinationAddress.ToString() + ":" + tcpPacket.DestinationPort, Protocol = ipPacket.Protocol, Length = ipPacket.TotalLength, Info = tcpPacket.Flags }; //If the buffer is not full, add the packet to the buffer and display it in the datagrid if (pkgBuffer.Count < maxBufferSize) { pkgBuffer.Add(strKey, pkgInfo); numPacketsReceived++; Dispatcher.Invoke((() => { dataGrid.Items.Add(packet); bufferProgress.Value = (double)numPacketsReceived; percentLabel.Content = (Math.Round(((double)numPacketsReceived / (double)maxBufferSize), 2) * 100).ToString() + "%"; }), DispatcherPriority.ContextIdle); } else { //Stop capturing packets because the buffer is full MessageBox.Show("The packet buffer has reached its maximum capacity. Clear the buffer or increase the maximum buffer size in order to continue.", "Packet Sniffer", MessageBoxButton.OK, MessageBoxImage.Warning); Dispatcher.Invoke((() => { Stop_Capturing(); }), DispatcherPriority.ContextIdle); } } } }