Class IPv4

  • All Implemented Interfaces:
    IPacket

    public class IPv4
    extends IP
    Implements IPv4 packet format.
    • Field Detail

      • PROTOCOL_DESERIALIZER_MAP

        public static final java.util.Map<java.lang.Byte,​Deserializer<? extends IPacket>> PROTOCOL_DESERIALIZER_MAP
      • version

        protected byte version
      • headerLength

        protected byte headerLength
      • diffServ

        protected byte diffServ
      • totalLength

        protected short totalLength
      • identification

        protected short identification
      • flags

        protected byte flags
      • fragmentOffset

        protected short fragmentOffset
      • ttl

        protected byte ttl
      • protocol

        protected byte protocol
      • checksum

        protected short checksum
      • sourceAddress

        protected int sourceAddress
      • destinationAddress

        protected int destinationAddress
      • options

        protected byte[] options
      • isTruncated

        protected boolean isTruncated
    • Constructor Detail

      • IPv4

        public IPv4()
        Default constructor that sets the version to 4.
    • Method Detail

      • getVersion

        public byte getVersion()
        Description copied from class: IP
        Gets IP version number.
        Specified by:
        getVersion in class IP
        Returns:
        IP version number
      • setVersion

        public IPv4 setVersion​(byte version)
        Description copied from class: IP
        Sets IP version number.
        Specified by:
        setVersion in class IP
        Parameters:
        version - the version to set
        Returns:
        IP class
      • getHeaderLength

        public byte getHeaderLength()
        Returns:
        the headerLength
      • getDscp

        public byte getDscp()
        Gets the DSCP value (6 bits).
        Returns:
        the DSCP value (6 bits)
      • setDscp

        public IPv4 setDscp​(byte dscp)
        Sets the DSCP value (6 bits).
        Parameters:
        dscp - the DSCP value (6 bits)
        Returns:
        this
      • getEcn

        public byte getEcn()
        Gets the ECN value (2 bits).
        Returns:
        the ECN value (2 bits)
      • setEcn

        public IPv4 setEcn​(byte ecn)
        Sets the ECN value (2 bits).
        Parameters:
        ecn - the ECN value (2 bits)
        Returns:
        this
      • getDiffServ

        public byte getDiffServ()
        Gets the DiffServ octet (including the DSCP and ECN bits).
        Returns:
        the diffServ octet (including the DSCP and ECN bits)
      • setDiffServ

        public IPv4 setDiffServ​(byte diffServ)
        Sets the DiffServ octet (including the DSCP and ECN bits).
        Parameters:
        diffServ - the diffServ octet to set (including the DSCP and ECN bits)
        Returns:
        this
      • getTotalLength

        public short getTotalLength()
        Returns:
        the totalLength
      • getIdentification

        public short getIdentification()
        Returns:
        the identification
      • isTruncated

        public boolean isTruncated()
      • setTruncated

        public void setTruncated​(boolean isTruncated)
      • setIdentification

        public IPv4 setIdentification​(short identification)
        Parameters:
        identification - the identification to set
        Returns:
        this
      • getFlags

        public byte getFlags()
        Returns:
        the flags
      • setFlags

        public IPv4 setFlags​(byte flags)
        Parameters:
        flags - the flags to set
        Returns:
        this s
      • getFragmentOffset

        public short getFragmentOffset()
        Returns:
        the fragmentOffset
      • setFragmentOffset

        public IPv4 setFragmentOffset​(short fragmentOffset)
        Parameters:
        fragmentOffset - the fragmentOffset to set
        Returns:
        this
      • getTtl

        public byte getTtl()
        Returns:
        the ttl
      • setTtl

        public IPv4 setTtl​(byte ttl)
        Parameters:
        ttl - the ttl to set
        Returns:
        this
      • getProtocol

        public byte getProtocol()
        Returns:
        the protocol
      • setProtocol

        public IPv4 setProtocol​(byte protocol)
        Parameters:
        protocol - the protocol to set
        Returns:
        this
      • getChecksum

        public short getChecksum()
        Returns:
        the checksum
      • setChecksum

        public IPv4 setChecksum​(short checksum)
        Parameters:
        checksum - the checksum to set
        Returns:
        this
      • resetChecksum

        public void resetChecksum()
        Description copied from interface: IPacket
        Reset any checksum as needed, and call resetChecksum on all parents.
        Specified by:
        resetChecksum in interface IPacket
        Overrides:
        resetChecksum in class BasePacket
      • getSourceAddress

        public int getSourceAddress()
        Returns:
        the sourceAddress
      • setSourceAddress

        public IPv4 setSourceAddress​(int sourceAddress)
        Parameters:
        sourceAddress - the sourceAddress to set
        Returns:
        this
      • setSourceAddress

        public IPv4 setSourceAddress​(java.lang.String sourceAddress)
        Parameters:
        sourceAddress - the sourceAddress to set
        Returns:
        this
      • getDestinationAddress

        public int getDestinationAddress()
        Returns:
        the destinationAddress
      • setDestinationAddress

        public IPv4 setDestinationAddress​(int destinationAddress)
        Parameters:
        destinationAddress - the destinationAddress to set
        Returns:
        this
      • setDestinationAddress

        public IPv4 setDestinationAddress​(java.lang.String destinationAddress)
        Parameters:
        destinationAddress - the destinationAddress to set
        Returns:
        this
      • getOptions

        public byte[] getOptions()
        Returns:
        the options
      • setOptions

        public IPv4 setOptions​(byte[] options)
        Parameters:
        options - the options to set
        Returns:
        this
      • serialize

        public byte[] serialize()
        Serializes the packet. Will compute and set the following fields if they are set to specific values at the time serialize is called: -checksum : 0 -headerLength : 0 -totalLength : 0
        Returns:
        a byte[] containing this packet and payloads
      • toIPv4Address

        public static int toIPv4Address​(java.lang.String ipAddress)
        Accepts an IPv4 address of the form xxx.xxx.xxx.xxx, ie 192.168.0.1 and returns the corresponding 32 bit integer.
        Parameters:
        ipAddress - ip address in string form
        Returns:
        int ip address value
      • toIPv4Address

        public static int toIPv4Address​(byte[] ipAddress)
        Accepts an IPv4 address in a byte array and returns the corresponding 32-bit integer value.
        Parameters:
        ipAddress - ip address in byte form
        Returns:
        int ip address value
      • fromIPv4Address

        public static java.lang.String fromIPv4Address​(int ipAddress)
        Accepts an IPv4 address and returns of string of the form xxx.xxx.xxx.xxx, e.g., 192.168.0.1.
        Parameters:
        ipAddress - ip address in form
        Returns:
        string form of ip address
      • fromIPv4AddressCollection

        public static java.lang.String fromIPv4AddressCollection​(java.util.Collection<java.lang.Integer> ipAddresses)
        Accepts a collection of IPv4 addresses as integers and returns a single String useful in toString method's containing collections of IP addresses.
        Parameters:
        ipAddresses - collection
        Returns:
        ip addresses in comma-separated string form
      • toIPv4AddressBytes

        public static byte[] toIPv4AddressBytes​(java.lang.String ipAddress)
        Accepts an IPv4 address of the form xxx.xxx.xxx.xxx, ie 192.168.0.1 and returns the corresponding byte array.
        Parameters:
        ipAddress - The IP address in the form xx.xxx.xxx.xxx.
        Returns:
        The IP address separated into bytes
      • toIPv4AddressBytes

        public static byte[] toIPv4AddressBytes​(int ipAddress)
        Accepts an IPv4 address in the form of an integer and returns the corresponding byte array.
        Parameters:
        ipAddress - The IP address as an integer.
        Returns:
        The IP address separated into bytes.
      • equals

        public boolean equals​(java.lang.Object obj)
        Overrides:
        equals in class BasePacket
      • deserializer

        public static Deserializer<IPv4> deserializer()
        Deserializer function for IPv4 packets.
        Returns:
        deserializer function
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object