Пример #1
0
        bool CheckWithFilter(IPHeader ipHdr, TCPHeader tcpHdr, UDPHeader udpHdr)
        {
            if (m_filter == null)
            {
                return(true);
            }


            if (!m_filter.IncludedProtocol(ipHdr.Protocol))
            {
                return(false);
            }

            if (m_filter.AddressList.Count > 0)
            {
                IPAddress remoteAddr1 = null;
                IPAddress remoteAddr2 = null;
                if (ipHdr.DestinationAddress.Address == m_sniffingIPAddress.Address)
                {
                    remoteAddr1 = ipHdr.SourceAddress;
                }
                else if (ipHdr.SourceAddress.Address == m_sniffingIPAddress.Address)
                {
                    remoteAddr1 = ipHdr.DestinationAddress;
                }
                else
                {
                    remoteAddr1 = ipHdr.SourceAddress;
                    remoteAddr2 = ipHdr.DestinationAddress;
                }

                if (remoteAddr2 == null)
                {
                    if ((m_filter.AddressExcluded == m_filter.IncludedAddress(remoteAddr1)))
                    {
                        return(false);
                    }
                }
                else
                {
                    if (!m_filter.AddressExcluded)
                    {
                        if (!m_filter.IncludedAddress(remoteAddr1) &&
                            !m_filter.IncludedAddress(remoteAddr2))
                        {
                            return(false);
                        }
                    }
                    else
                    {
                        if (m_filter.IncludedAddress(remoteAddr1) ||
                            m_filter.IncludedAddress(remoteAddr2))
                        {
                            return(false);
                        }
                    }
                }
            }

            ushort destport = 0;
            ushort srcport  = 0;
            ushort msglen   = 0;

            if (tcpHdr != null)
            {
                destport = tcpHdr.DestinationPort;
                srcport  = tcpHdr.SourcePort;
                msglen   = tcpHdr.MessageLength;
            }
            else if (udpHdr != null)
            {
                destport = udpHdr.DestinationPort;
                srcport  = udpHdr.SourcePort;
                msglen   = udpHdr.MessageLength;
            }
            else
            {
                return(false);
            }

            if (m_filter.DataOnly && msglen <= 0)
            {
                return(false);
            }

            if (m_filter.Portlist.Count > 0)
            {
                if (!m_filter.PortExluded)
                {
                    if ((!m_filter.IncludedPort(destport) &&
                         !m_filter.IncludedPort(srcport))
                        )
                    {
                        return(false);
                    }
                }
                else
                {
                    if ((m_filter.IncludedPort(destport) ||
                         m_filter.IncludedPort(srcport))
                        )
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }