コード例 #1
0
        public PacketCaptureHandler(PacketCapture parent, FilterIp[] filters, PacketCaptureCallback success, PacketErrorCallback error)
        {
            this.parent = parent;
            this.filters = filters;
            this.success = success;
            this.error = error;

            this.parent.AddHandler(this);
        }
コード例 #2
0
        /// <summary>
        /// Parses an IP packet from the specified buffer at the given index, filtering according to the specified filters.
        /// </summary>
        /// <param name="buffer">The buffer.</param>
        /// <param name="index">The index.</param>
        /// <param name="length">The length.</param>
        /// <param name="filters">The list of filters.</param>
        /// <param name="packet">The IP packet or <b>null</b>.</param>
        /// <returns><b>True</b> if th packet was parsed, <b>false</b> otherwise.</returns>
        public static bool ParseFilter(byte[] buffer, ref int index, int length, FilterIp[] filters, out ProtoPacketIp packet)
        {
            // Set the packet to null.
            packet = null;

            // Validate the packet.
            if (buffer[index] >> 4 != ProtoPacketIp.version) return false;

            // Validate the length.
            if ((ushort)((buffer[index + 2] << 8) | buffer[index + 3]) != length - index) return false;

            // Set the protocol.
            byte protocol = buffer[index + 9];

            // Parse the source address.
            IPAddress sourceAddress = new IPAddress(new byte[] { buffer[index + 12], buffer[index + 13], buffer[index + 14], buffer[index + 15] });
            // Parse the destination address.
            IPAddress destinationAddress = new IPAddress(new byte[] { buffer[index + 16], buffer[index + 17], buffer[index + 18], buffer[index + 19] });

            // Try and match the filters.
            bool match = false;
            for (int idx = 0; (idx < filters.Length) && (!match); idx++)
            {
                match = match || filters[idx].Matches(sourceAddress, destinationAddress, protocol);
            }

            if (!match)
                return false;

            // Parse the packet.
            packet = ProtoPacketIp.Parse(buffer, ref index, length);

            // Return true.
            return true;
        }