/// <summary>
 /// When one packet is captured by threads, we store the information in the manager.
 /// </summary>
 /// <param name="packet">The information of one packet</param>
 /// <param name="len">The length of bytes of one packet</param>
 public void AddPacket(PacketFlow packet, int len)
 {
     lock (this)
     {
         if (packet.type != PacketFlow.FlowType.DROP)
         {
             if (packet.hasIPAndPort)
             {
                 Port p = new Port(packet.myIP, packet.port, packet.protocol);
                 if (!portMap.TryGetValue(p, out PortStatistic statistic))
                 {
                     statistic = new PortStatistic
                     {
                         port = p
                     };
                     portMap[p] = statistic;
                 }
                 if (packet.type == PacketFlow.FlowType.DOWNLOAD)
                 {
                     statistic.downLen += len;
                     downloadStatistic += len;
                 }
                 else
                 {
                     statistic.upLen += len;
                     uploadStatistic += len;
                 }
             }
             else
             {
                 if (packet.type == PacketFlow.FlowType.UPLOAD)
                 {
                     noPortStatistic.upLen += len;
                     uploadStatistic       += len;
                 }
                 else
                 {
                     noPortStatistic.downLen += len;
                     downloadStatistic       += len;
                 }
             }
         }
     }
 }
예제 #2
0
 /// <summary>
 /// Get the data flow direction of a packet.
 /// </summary>
 /// <param name="address">Addresses of a packet</param>
 /// <returns>Data flow direction</returns>
 public PacketFlow GetPacketFlow(PacketAddress address)
 {
     lock (this)
     {
         if (!cache.TryGetValue(address, out PacketFlow result))
         {
             result = new PacketFlow();
             if (myIPSet.Contains(address.source))
             {
                 if (myIPSet.Contains(address.destination))
                 {
                     result.type = PacketFlow.FlowType.DROP;
                 }
                 else
                 {
                     result.type         = PacketFlow.FlowType.UPLOAD;
                     result.hasIPAndPort = true;
                     result.myIP         = address.source;
                     result.port         = address.sourcePort;
                 }
             }
             else
             {
                 if (myIPSet.Contains(address.destination))
                 {
                     result.type         = PacketFlow.FlowType.DOWNLOAD;
                     result.hasIPAndPort = true;
                     result.myIP         = address.destination;
                     result.port         = address.destinationPort;
                 }
                 else
                 {
                     if (IsAddressInNetwork(address.source))
                     {
                         if (IsAddressInNetwork(address.destination))
                         {
                             result.type = PacketFlow.FlowType.DROP;
                         }
                         else
                         {
                             result.type         = PacketFlow.FlowType.DOWNLOAD;
                             result.hasIPAndPort = false;
                         }
                     }
                     else
                     {
                         if (IsAddressInNetwork(address.destination))
                         {
                             result.type         = PacketFlow.FlowType.UPLOAD;
                             result.hasIPAndPort = false;
                         }
                         else
                         {
                             result.type = PacketFlow.FlowType.DROP;
                         }
                     }
                 }
             }
             cache[address] = result;
         }
         return(result);
     }
 }