Esempio n. 1
0
        private void FilterAndProcessPacket(byte[] buffer)
        {
            try
            {
                var ipPacket = new IpPacket(buffer);
                if (!ipPacket.IsValid || ipPacket.Protocol != ProtocolType.Tcp)
                {
                    return;
                }

                var tcpPacket = new TcpPacket(ipPacket.Data);
                if (!tcpPacket.IsValid)
                {
                    return;
                }

                if (!tcpPacket.Flags.HasFlag(TcpFlags.ACK | TcpFlags.PSH))
                {
                    return;
                }

                var sourceEndPoint      = new IPEndPoint(ipPacket.SourceIpAddress, tcpPacket.SourcePort);
                var destinationEndPoint = new IPEndPoint(ipPacket.DestinationIpAddress, tcpPacket.DestinationPort);
                var connection          = new Connection {
                    LocalEndPoint = sourceEndPoint, RemoteEndPoint = destinationEndPoint
                };
                var reverseConnection = new Connection {
                    LocalEndPoint = destinationEndPoint, RemoteEndPoint = sourceEndPoint
                };

                if (!(_connections.Contains(connection) || _connections.Contains(reverseConnection)))
                {
                    return;
                }

                if (!_connections.Contains(reverseConnection))
                {
                    return;
                }

                lock (_lockAnalyse)
                {
                    _packetHandler.Analyze(_pid, tcpPacket.Payload, ref _state);
                }
            }
            catch (Exception ex)
            {
                _logger.Write(ex, "N: Error while Filtering PacketsS", LogLevel.Error);
            }
        }