예제 #1
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="bas">
        /// A <see cref="ByteArraySegment"/>
        /// </param>
        public RawIPPacket(ByteArraySegment bas)
        {
            // Pcap raw link layer format does not have any header
            // you need to identify whether you have ipv4 or ipv6
            // directly by checking the IP version number.
            // If the first nibble is 0x04, then you have IP v4
            // If the first nibble is 0x06, then you have IP v6
            // The RawIPPacketProtocol enum has been defined to match this.
            var firstNibble = bas.Bytes[0] >> 4;

            this.Protocol = (RawIPPacketProtocol)firstNibble;

            this.header        = new ByteArraySegment(bas);
            this.header.Length = 0;

            // parse the encapsulated bytes
            this.payloadPacketOrData = new PacketOrByteArraySegment();

            switch (this.Protocol)
            {
            case RawIPPacketProtocol.IPv4:
                this.payloadPacketOrData.ThePacket = new IPv4Packet(this.header.EncapsulatedBytes());
                break;

            case RawIPPacketProtocol.IPv6:
                this.payloadPacketOrData.ThePacket = new IPv6Packet(this.header.EncapsulatedBytes());
                break;

            default:
                throw new NotImplementedException("Protocol of " + this.Protocol + " is not implemented");
            }
        }
예제 #2
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="bas">
        /// A <see cref="ByteArraySegment" />
        /// </param>
        public RawIPPacket(ByteArraySegment bas)
        {
            // Pcap raw link layer format does not have any header
            // you need to identify whether you have ipv4 or ipv6
            // directly by checking the IP version number.
            // If the first nibble is 0x04, then you have IP v4
            // If the first nibble is 0x06, then you have IP v6
            // The RawIPPacketProtocol enum has been defined to match this.
            var firstNibble = bas.Bytes[0] >> 4;

            Protocol = (RawIPPacketProtocol)firstNibble;

            Header = new ByteArraySegment(bas)
            {
                Length = 0
            };

            // parse the encapsulated bytes
            PayloadPacketOrData = new Lazy <PacketOrByteArraySegment>(() =>
            {
                var result = new PacketOrByteArraySegment();
                switch (Protocol)
                {
                case RawIPPacketProtocol.IPv4:
                    result.Packet = new IPv4Packet(Header.EncapsulatedBytes());
                    break;

                case RawIPPacketProtocol.IPv6:
                    result.Packet = new IPv6Packet(Header.EncapsulatedBytes());
                    break;

                default:
                    throw new NotImplementedException("Protocol of " + Protocol + " is not implemented");
                }

                return(result);
            }, LazyThreadSafetyMode.PublicationOnly);
        }