/// <summary> /// Filters all received packets from PacketList /// </summary> private void FilterAllPackets() { // To filter all packets, we must refresh the whole list FilteredPacketList.Clear(); lock (PacketList) { foreach (IPPacket packet in PacketList) { lock (FilteredPacketList) { AddToFilteredList(packet); } } } // This condition here avoids threading problem: // If a new packet is captured just before FilterAllPackets() is called, // this removes all newPackets that arrived before this function call. while (FilteredPacketList.Count > 2) { uint firstPacketID = filteredPacketList[0].PacketID; uint lastPacketID = filteredPacketList[filteredPacketList.Count - 2].PacketID; if (firstPacketID > lastPacketID) { filteredPacketList.RemoveAt(0); continue; } break; } }
private void ClearPacketListExecute() { PacketList.Clear(); FilteredPacketList.Clear(); StatsHandler.ResetStats(); if (monitor != null) { StatsHandler.StopWatch.Start(); } ClearSelectedPacketData(); IsClearEnabled = false; }
/// <summary> /// Decides whether newPacket should be added to FilteredPacketList or not /// </summary> /// <param name="newPacket">Packet to be processed and added to FilteredPacketList if it satisfies filter conditions</param> private void AddToFilteredList(IPPacket newPacket) { // If the filterString is empty, just add newPacket to the FilterPacketList if (string.IsNullOrEmpty(filter)) { FilteredPacketList.Add(newPacket); return; } // If none of the substrings uses the proper syntax, ignore it and add packet // as if there was no filter at all. if (protocolList.Count == 0 && protocolListToExclude.Count == 0 && srcIPList.Count == 0 && destIPList.Count == 0 && srcPortList.Count == 0 && destPortList.Count == 0 && higherLengthList.Count == 0 && lowerLengthList.Count == 0) { FilteredPacketList.Add(newPacket); return; } // These are rules a newPacket must satisfy to be added in the FilteredPacketList. // By default all rules are true, so in case one of the condition list is empty // a newPacket could be added to FilteredList. Otherwise it is set to false once it // enters foreach loop where it must satisfy the conditon to be set to true bool IncludeProtocolRule = true; bool ExcludeProtocolRule = false; bool SrcIPRule = true; bool DstIPRule = true; bool SrcPortRule = true; bool DestPortRule = true; bool LowerLengthRule = true; bool HigherLengthRule = true; // Checking empty protocolList would change the default value of IncludeProtocolRule to false if (protocolList.Count != 0) { IncludeProtocolRule = ApplyProtocolRule(newPacket, protocolList); } if (protocolListToExclude.Count != 0) { ExcludeProtocolRule = ApplyProtocolRule(newPacket, protocolListToExclude); } foreach (string ip in srcIPList) { SrcIPRule = false; if (ip == newPacket.IPHeader[0].SourceIPAddress.ToString()) { SrcIPRule = true; break; } } foreach (string ip in destIPList) { DstIPRule = false; if (ip == newPacket.IPHeader[0].DestinationIPAddress.ToString()) { DstIPRule = true; break; } } foreach (string port in srcPortList) { SrcPortRule = false; if (newPacket.TCPPacket.Count > 0 && port == newPacket.TCPPacket[0].TCPHeader[0].SourcePort.ToString()) { SrcPortRule = true; break; } else if (newPacket.UDPPacket.Count > 0 && port == newPacket.UDPPacket[0].UDPHeader[0].SourcePort.ToString()) { SrcPortRule = true; break; } } foreach (string port in destPortList) { DestPortRule = false; if (newPacket.TCPPacket.Count > 0 && port == newPacket.TCPPacket[0].TCPHeader[0].DestinationPort.ToString()) { DestPortRule = true; break; } else if (newPacket.UDPPacket.Count > 0 && port == newPacket.UDPPacket[0].UDPHeader[0].DestinationPort.ToString()) { DestPortRule = true; break; } } ushort packetLength = newPacket.IPHeader[0].TotalLength; foreach (string LowerLength in lowerLengthList) { LowerLengthRule = false; ushort lowerLenght = ushort.Parse(LowerLength); if (lowerLenght > packetLength) { LowerLengthRule = true; break; } } foreach (string HigherLength in higherLengthList) { HigherLengthRule = false; ushort higherLenght = ushort.Parse(HigherLength); if (higherLenght < packetLength) { HigherLengthRule = true; break; } } // If newPacket satisfies all the filter rules, add it to filteredPacketList if (IncludeProtocolRule == true && ExcludeProtocolRule == false && SrcIPRule == true && DstIPRule == true && SrcPortRule == true && DestPortRule == true && LowerLengthRule == true && HigherLengthRule == true) { FilteredPacketList.Add(newPacket); } }