public void ClearAllClicked() { if (_view.ListReceivedPackets.Items.Count > 0) _view.ListReceivedPackets.Items.Clear(); if (_view.TreePackedDetails.Nodes.Count > 0) _view.TreePackedDetails.Nodes.Clear(); if (_view.ListOpenPorts.Items.Count > 0) _view.ListOpenPorts.Items.Clear(); if (_pkgBuffer.Count > 0) { _pkgBuffer.Clear(); _view.ProgressBufferusage.Value = 0; _view.Invoke(() => _view.SetBufferUsage("0")); _view.Invoke(() => _view.SetTotalPacketReceivedText("0")); } }
/// <summary> /// This method converts received data /// to packets information /// </summary> /// <param name="buffer"></param> /// <param name="iReceived"></param> public void ConvertReceivedData(byte[] buffer, int iReceived) { _view.Invoke(() => _view.SetTotalPacketReceivedText(_decPackagesReceived.ToString())); if (buffer.Length > 0 && iReceived != 0) { //getting IP header and data information PacketIP ipPacket = new PacketIP(buffer, iReceived); // this string used as a key in the buffer string strKey = _decPackagesReceived.ToString(); // Guid.NewGuid().ToString(); //searching which uperlevel protocol contain IP packet switch (ipPacket.Protocol) { case "TCP": { //if IP contains TCP creating new TCPData object //and assigning all TCP fields PacketTcp tcpPacket = new PacketTcp(ipPacket.Data, ipPacket.MessageLength); //creating new PacketInfo object to fill the buffer PacketInfo pkgInfo = new PacketInfo(ipPacket, tcpPacket); //_pkgBuffer.Add(strKey, pkgInfo); //creating new list item to fill the list view control ListViewItem item = new ListViewItem(_decPackagesReceived.ToString()); item.SubItems.Add(DateTime.Now.ToString("HH:mm:ss:") + DateTime.Now.Millisecond.ToString()); item.SubItems.Add(ipPacket.SourceAddress.ToString()); item.SubItems.Add(tcpPacket.SourcePort); item.SubItems.Add(ipPacket.DestinationAddress.ToString()); item.SubItems.Add(tcpPacket.DestinationPort); item.SubItems.Add(ipPacket.Protocol); item.SubItems.Add(ipPacket.TotalLength); item.SubItems.Add(strKey); // checking if current buffer size is larger then maximum allowed buffer size //then removing item from top of the list view control and also removing //the same item from buffer , then filling the list and buffer with new item if (_pkgBuffer.Count == StartupInformation.PacketsToCapture && _view.ListReceivedPackets.Items.Count > 0) { _view.Invoke(() => _pkgBuffer.Remove(_view.ListReceivedPackets.Items[0].SubItems[8].Text)); _view.ListReceivedPackets.Invoke(RemoveFromList); _pkgBuffer.Add(strKey, pkgInfo); _view.ListReceivedPackets.Invoke(AddToList, new object[] { item }); } else { _pkgBuffer.Add(strKey, pkgInfo); _view.ListReceivedPackets.Invoke(AddToList, new object[] { item }); } } break; // see TCP case "UDP": { PacketUdp udpPacket = new PacketUdp(ipPacket.Data, ipPacket.MessageLength); PacketInfo pkgInfo = new PacketInfo(ipPacket, udpPacket); //_pkgBuffer.Add(strKey, pkgInfo); ListViewItem item = new ListViewItem(_decPackagesReceived.ToString()); item.SubItems.Add(DateTime.Now.ToString("HH:mm:ss:") + DateTime.Now.Millisecond.ToString()); item.SubItems.Add(ipPacket.SourceAddress.ToString()); item.SubItems.Add(udpPacket.SourcePort); item.SubItems.Add(ipPacket.DestinationAddress.ToString()); item.SubItems.Add(udpPacket.DestinationPort); item.SubItems.Add(ipPacket.Protocol); item.SubItems.Add(ipPacket.TotalLength); item.SubItems.Add(strKey); if (_pkgBuffer.Count == StartupInformation.PacketsToCapture && _view.ListReceivedPackets.Items.Count > 0) { _view.Invoke(() => _pkgBuffer.Remove(_view.ListReceivedPackets.Items[0].SubItems[8].Text)); _view.ListReceivedPackets.Invoke(RemoveFromList); _pkgBuffer.Add(strKey, pkgInfo); _view.ListReceivedPackets.Invoke(AddToList, new object[] { item }); } else { _pkgBuffer.Add(strKey, pkgInfo); _view.ListReceivedPackets.Invoke(AddToList, new object[] { item }); } } break; //see TCP case "ICMP": { PacketIcmp icmpPacket = new PacketIcmp(ipPacket.Data, ipPacket.MessageLength); PacketInfo pkgInfo = new PacketInfo(ipPacket, icmpPacket); //_pkgBuffer.Add(strKey, pkgInfo); ListViewItem item = new ListViewItem(_decPackagesReceived.ToString()); item.SubItems.Add(DateTime.Now.ToString("HH:mm:ss:") + DateTime.Now.Millisecond.ToString()); item.SubItems.Add(ipPacket.SourceAddress.ToString()); item.SubItems.Add("0"); item.SubItems.Add(ipPacket.DestinationAddress.ToString()); item.SubItems.Add("0"); item.SubItems.Add(ipPacket.Protocol); item.SubItems.Add(ipPacket.TotalLength); item.SubItems.Add("N/A"); item.SubItems.Add(strKey); if (_pkgBuffer.Count == StartupInformation.PacketsToCapture && _view.ListReceivedPackets.Items.Count > 0) { _view.Invoke(() => _pkgBuffer.Remove(_view.ListReceivedPackets.Items[0].SubItems[8].Text)); _view.ListReceivedPackets.Invoke(RemoveFromList); _view.ListReceivedPackets.Invoke(AddToList, new object[] { item }); _pkgBuffer.Add(strKey, pkgInfo); } else { _view.ListReceivedPackets.Invoke(AddToList, new object[] { item }); _pkgBuffer.Add(strKey, pkgInfo); } } break; case "IGMP": { PacketIgmp igmpPacket = new PacketIgmp(ipPacket.Data, ipPacket.MessageLength); PacketInfo pkgInfo = new PacketInfo(ipPacket, igmpPacket); //_pkgBuffer.Add(strKey, pkgInfo); ListViewItem item = new ListViewItem(_decPackagesReceived.ToString()); item.SubItems.Add(DateTime.Now.ToString("HH:mm:ss:") + DateTime.Now.Millisecond.ToString()); item.SubItems.Add(ipPacket.SourceAddress.ToString()); item.SubItems.Add("0"); item.SubItems.Add(ipPacket.DestinationAddress.ToString()); item.SubItems.Add("0"); item.SubItems.Add(ipPacket.Protocol); item.SubItems.Add(ipPacket.TotalLength); item.SubItems.Add("N/A"); item.SubItems.Add(strKey); if (_pkgBuffer.Count == StartupInformation.PacketsToCapture && _view.ListReceivedPackets.Items.Count > 0) { _view.Invoke(() => _pkgBuffer.Remove(_view.ListReceivedPackets.Items[0].SubItems[8].Text)); _view.ListReceivedPackets.Invoke(RemoveFromList); _pkgBuffer.Add(strKey, pkgInfo); _view.ListReceivedPackets.Invoke(AddToList, new object[] { item }); } else { _pkgBuffer.Add(strKey, pkgInfo); _view.ListReceivedPackets.Invoke(AddToList, new object[] { item }); } } break; case "Unknown": { PacketInfo pkgInfo = new PacketInfo(ipPacket); _pkgBuffer.Add(strKey, pkgInfo); ListViewItem item = new ListViewItem(_decPackagesReceived.ToString()); item.SubItems.Add(DateTime.Now.ToString("HH:mm:ss:") + DateTime.Now.Millisecond.ToString()); item.SubItems.Add(ipPacket.SourceAddress.ToString()); item.SubItems.Add("0"); item.SubItems.Add(ipPacket.DestinationAddress.ToString()); item.SubItems.Add("0"); item.SubItems.Add(ipPacket.Protocol); item.SubItems.Add(ipPacket.TotalLength); item.SubItems.Add(strKey); if (_pkgBuffer.Count > StartupInformation.PacketsToCapture && _view.ListReceivedPackets.Items.Count > 0) { _view.Invoke(() => _pkgBuffer.Remove(_view.ListReceivedPackets.Items[0].SubItems[8].Text)); _view.ListReceivedPackets.Invoke(RemoveFromList); _view.ListReceivedPackets.Invoke(AddToList, new object[] { item }); } else { _view.ListReceivedPackets.Invoke(AddToList, new object[] { item }); } } break; } //_view.Invoke(() => _view.ProgressBufferusage.Value = _pkgBuffer.Count); //_view.Invoke(() => _view.SetBufferUsage(_pkgBuffer.Count.ToString())); } }