Ejemplo n.º 1
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="bas">
        /// A <see cref="ByteArraySegment"/>
        /// </param>
        public UdpPacket(ByteArraySegment bas)
        {
            log.DebugFormat("bas {0}", bas.ToString());

            // set the header field, header field values are retrieved from this byte array
            header = new ByteArraySegment(bas);
            RandomUtils.EnsurePacketLength(this, UdpFields.HeaderLength, header.Length);
            header.Length = UdpFields.HeaderLength;

            payloadPacketOrData = new PacketOrByteArraySegment();

            // is this packet going to port 7 or 9? if so it might be a WakeOnLan packet
            const int wakeOnLanPort0 = 7;
            const int wakeOnLanPort1 = 9;

            if (DestinationPort.Equals(wakeOnLanPort0) || DestinationPort.Equals(wakeOnLanPort1))
            {
                payloadPacketOrData.ThePacket = new WakeOnLanPacket(header.EncapsulatedBytes());
            }
            else
            {
                // store the payload bytes
                payloadPacketOrData.TheByteArraySegment = header.EncapsulatedBytes();
            }
        }
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="bas">
        /// A <see cref="ByteArraySegment"/>
        /// </param>
        public ICMPv6Packet(ByteArraySegment bas)
        {
            log.Debug("");

            header = new ByteArraySegment(bas);
            RandomUtils.EnsurePacketLength(this, ICMPv6Fields.HeaderLength, header.Length);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="bas">
        /// A <see cref="ByteArraySegment"/>
        /// </param>
        /// <param name="ParentPacket">
        /// A <see cref="Packet"/>
        /// </param>
        public TcpPacket(ByteArraySegment bas,
                         Packet ParentPacket) :
            this(bas)
        {
            log.DebugFormat("ParentPacket.GetType() {0}", ParentPacket.GetType());

            this.ParentPacket = ParentPacket;

            // if the parent packet is an IPv4Packet we need to adjust
            // the payload length because it is possible for us to have
            // X bytes of data but only (X - Y) bytes are actually valid
            if (this.ParentPacket is IPv4Packet)
            {
                // actual total length (tcp header + tcp payload)
                var ipv4Parent           = (IPv4Packet)this.ParentPacket;
                var ipPayloadTotalLength = ipv4Parent.TotalLength - (ipv4Parent.HeaderLength * 4);

                log.DebugFormat("ipv4Parent.TotalLength {0}, ipv4Parent.HeaderLength {1}",
                                ipv4Parent.TotalLength,
                                ipv4Parent.HeaderLength * 4);

                var newTcpPayloadLength = ipPayloadTotalLength - this.Header.Length;
                RandomUtils.EnsurePacketLength(this, newTcpPayloadLength, payloadPacketOrData.TheByteArraySegment.Length);

                log.DebugFormat("Header.Length {0}, Current payload length: {1}, new payload length {2}",
                                this.header.Length,
                                payloadPacketOrData.TheByteArraySegment.Length,
                                newTcpPayloadLength);

                // the length of the payload is the total payload length
                // above, minus the length of the tcp header
                payloadPacketOrData.TheByteArraySegment.Length = newTcpPayloadLength;
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="bas">
        /// A <see cref="ByteArraySegment"/>
        /// </param>
        public IPv4Packet(ByteArraySegment bas)
        {
            log.Debug("");

            header = new ByteArraySegment(bas);
            RandomUtils.EnsurePacketLength(this, IPv4Fields.HeaderLength, header.Length);

            // TOS? See http://en.wikipedia.org/wiki/TCP_offload_engine
            if (TotalLength == 0)
            {
                TotalLength = header.Length;
            }

            // update the header length with the correct value
            // NOTE: we take care to convert from 32bit words into bytes
            // NOTE: we do this *after* setting header because we need header to be valid
            //       before we can retrieve the HeaderLength property

            RandomUtils.EnsurePacketLength(this, HeaderLength * 4, header.Length);
            header.Length = HeaderLength * 4;

            log.DebugFormat("IPv4Packet HeaderLength {0}", HeaderLength);
            log.DebugFormat("header {0}", header);

            // parse the payload
            var payload = header.EncapsulatedBytes(PayloadLength);

            payloadPacketOrData = IpPacket.ParseEncapsulatedBytes(payload,
                                                                  NextHeader,
                                                                  this);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="bas">
        /// A <see cref="ByteArraySegment"/>
        /// </param>
        public ARPPacket(ByteArraySegment bas)
        {
            header = new ByteArraySegment(bas);
            RandomUtils.EnsurePacketLength(this, ARPFields.HeaderLength, header.Length);
            header.Length = ARPFields.HeaderLength;

            // NOTE: no need to set the payloadPacketOrData field, arp packets have
            //       no payload
        }
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="bas">
        /// A <see cref="ByteArraySegment"/>
        /// </param>
        public IGMPv2Packet(ByteArraySegment bas)
        {
            // set the header field, header field values are retrieved from this byte array
            header = new ByteArraySegment(bas);
            RandomUtils.EnsurePacketLength(this, IGMPv2Fields.HeaderLength, header.Length);
            header.Length = UdpFields.HeaderLength;

            // store the payload bytes
            payloadPacketOrData = new PacketOrByteArraySegment();
            payloadPacketOrData.TheByteArraySegment = header.EncapsulatedBytes();
        }
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="bas">
        /// A <see cref="ByteArraySegment"/>
        /// </param>
        public Ieee8021QPacket(ByteArraySegment bas)
        {
            // set the header field, header field values are retrieved from this byte array
            header = new ByteArraySegment(bas);
            RandomUtils.EnsurePacketLength(this, Ieee8021QFields.HeaderLength, header.Length);
            header.Length = Ieee8021QFields.HeaderLength;

            // parse the payload via an EthernetPacket method
            payloadPacketOrData = EthernetPacket.ParseEncapsulatedBytes(header,
                                                                        Type);
        }
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="bas">
        /// A <see cref="ByteArraySegment"/>
        /// </param>
        public ICMPv4Packet(ByteArraySegment bas)
        {
            log.Debug("");

            header = new ByteArraySegment(bas);
            RandomUtils.EnsurePacketLength(this, ICMPv4Fields.HeaderLength, header.Length);
            header.Length = ICMPv4Fields.HeaderLength;

            // store the payload bytes
            payloadPacketOrData = new PacketOrByteArraySegment();
            payloadPacketOrData.TheByteArraySegment = header.EncapsulatedBytes();
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="bas">
        /// A <see cref="ByteArraySegment"/>
        /// </param>
        public PPPPacket(ByteArraySegment bas)
        {
            log.Debug("");

            // slice off the header portion as our header
            header = new ByteArraySegment(bas);
            RandomUtils.EnsurePacketLength(this, PPPFields.HeaderLength, header.Length);
            header.Length = PPPFields.HeaderLength;

            // parse the encapsulated bytes
            payloadPacketOrData = ParseEncapsulatedBytes(header, Protocol);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="bas">
        /// A <see cref="ByteArraySegment"/>
        /// </param>
        public WakeOnLanPacket(ByteArraySegment bas)
        {
            log.Debug("");

            RandomUtils.EnsurePacketLength(this, packetLength, bas.Length);

            if (WakeOnLanPacket.IsValid(bas))
            {
                // set the header field, header field values are retrieved from this byte array
                header        = new ByteArraySegment(bas);
                header.Length = Bytes.Length;
            }
        }
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="bas">
        /// A <see cref="ByteArraySegment"/>
        /// </param>
        public EthernetPacket(ByteArraySegment bas)
        {
            log.Debug("");


            // slice off the header portion
            header        = new ByteArraySegment(bas);
            header.Length = EthernetFields.HeaderLength;

            RandomUtils.EnsurePacketLength(this, EthernetFields.HeaderLength, header.Offset + header.Length);

            // parse the encapsulated bytes
            payloadPacketOrData = ParseEncapsulatedBytes(header, Type);
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="bas">
        /// A <see cref="ByteArraySegment"/>
        /// </param>
        public TcpPacket(ByteArraySegment bas)
        {
            log.Debug("");

            // set the header field, header field values are retrieved from this byte array
            header = new ByteArraySegment(bas);

            RandomUtils.EnsurePacketLength(this, TcpPacket.HeaderMinimumLength, header.Length);
            RandomUtils.EnsurePacketLength(this, DataOffset * 4, header.Length);
            // NOTE: we update the Length field AFTER the header field because
            // we need the header to be valid to retrieve the value of DataOffset
            header.Length = DataOffset * 4;

            // store the payload bytes
            payloadPacketOrData = new PacketOrByteArraySegment();
            payloadPacketOrData.TheByteArraySegment = header.EncapsulatedBytes();
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="bas">
        /// A <see cref="ByteArraySegment"/>
        /// </param>
        public IPv6Packet(ByteArraySegment bas)
        {
            log.Debug(bas.ToString());

            // slice off the header
            header = new ByteArraySegment(bas);
            RandomUtils.EnsurePacketLength(this, IPv6Fields.HeaderLength, header.Length);
            header.Length = IPv6Packet.HeaderMinimumLength;

            // set the actual length, we need to do this because we need to set
            // header to something valid above before we can retrieve the PayloadLength
            log.DebugFormat("PayloadLength: {0}", PayloadLength);
            RandomUtils.EnsurePacketLength(this, IPv6Packet.HeaderMinimumLength, bas.Length - PayloadLength);
            header.Length = bas.Length - PayloadLength;

            // parse the payload
            var payload = header.EncapsulatedBytes(PayloadLength);

            payloadPacketOrData = IpPacket.ParseEncapsulatedBytes(payload,
                                                                  NextHeader,
                                                                  this);
        }