Class Packet

  • Direct Known Subclasses:
    Ethernet, LLDP, LLDPTLV

    public abstract class Packet
    extends Object
    Abstract class which represents the generic network packet object. It provides the basic methods which are common for all the packets, like serialize and deserialize.
    • Field Detail

      • writeAccess

        protected boolean writeAccess
      • corrupted

        protected boolean corrupted
      • parent

        protected Packet parent
      • payload

        protected Packet payload
      • rawPayload

        protected byte[] rawPayload
      • hdrFieldCoordMap

        protected Map<String,​org.apache.commons.lang3.tuple.Pair<Integer,​Integer>> hdrFieldCoordMap
      • hdrFieldsMap

        protected Map<String,​byte[]> hdrFieldsMap
      • payloadClass

        protected Class<? extends Packet> payloadClass
    • Constructor Detail

      • Packet

        public Packet()
      • Packet

        public Packet​(boolean writeAccess)
    • Method Detail

      • getParent

        public Packet getParent()
      • getPayload

        public Packet getPayload()
      • setParent

        public void setParent​(Packet parent)
      • setPayload

        public void setPayload​(Packet payload)
      • setHeaderField

        public void setHeaderField​(String headerField,
                                   byte[] readValue)
      • deserialize

        public Packet deserialize​(byte[] data,
                                  int bitOffset,
                                  int size)
                           throws PacketException
        This method deserializes the data bits obtained from the wire into the respective header and payload which are of type Packet.
        Parameters:
        data - - data from wire to deserialize
        bitOffset - bit position where packet header starts in data array
        size - size of packet in bits
        Returns:
        Packet
        Throws:
        PacketException - if deserialization fails
      • serialize

        public byte[] serialize()
                         throws PacketException
        This method serializes the header and payload from the respective packet class, into a single stream of bytes to be sent on the wire.
        Returns:
        The byte array representing the serialized Packet
        Throws:
        PacketException - if serialization fails
      • postSerializeCustomOperation

        protected void postSerializeCustomOperation​(byte[] myBytes)
                                             throws PacketException
        This method gets called at the end of the serialization process It is intended for the child packets to insert some custom data into the output byte stream which cannot be done or cannot be done efficiently during the normal Packet.serialize() path. An example is the checksum computation for IPv4
        Parameters:
        myBytes - serialized bytes
        Throws:
        PacketException - on failure
      • postDeserializeCustomOperation

        protected void postDeserializeCustomOperation​(byte[] data,
                                                      int startBitOffset)
                                               throws PacketException
        This method re-computes the checksum of the bits received on the wire and validates it with the checksum in the bits received Since the computation of checksum varies based on the protocol, this method is overridden. Currently only IPv4 and ICMP do checksum computation and validation. TCP and UDP need to implement these if required.
        Parameters:
        data - The byte stream representing the Ethernet frame
        startBitOffset - The bit offset from where the byte array corresponding to this Packet starts in the frame
        Throws:
        PacketException - on failure
      • getHeaderSize

        public int getHeaderSize()
        Gets the header length in bits.
        Returns:
        int the header length in bits
      • getfieldOffset

        public int getfieldOffset​(String fieldName)
        This method fetches the start bit offset for header field specified by 'fieldname'. The offset is present in the hdrFieldCoordMap of the respective packet class
        Returns:
        Integer - startOffset of the requested field
      • getfieldnumBits

        public int getfieldnumBits​(String fieldName)
        This method fetches the number of bits for header field specified by 'fieldname'. The numBits are present in the hdrFieldCoordMap of the respective packet class
        Returns:
        Integer - number of bits of the requested field
      • getRawPayload

        public byte[] getRawPayload()
        Returns the raw payload carried by this packet in case payload was not parsed. Caller can call this function in case the getPaylod() returns null.
        Returns:
        The raw payload if not parsable as an array of bytes, null otherwise
      • setRawPayload

        public void setRawPayload​(byte[] bytes)
        Set a raw payload in the packet class.
        Parameters:
        bytes - The raw payload as byte array
      • isCorrupted

        public boolean isCorrupted()
        Return whether the deserialized packet is to be considered corrupted. This is the case when the checksum computed after reconstructing the packet received from wire is not equal to the checksum read from the stream. For the Packet class which do not have a checksum field, this function will always return false.
        Returns:
        true if the deserialized packet's recomputed checksum is not equal to the packet carried checksum
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object