Class IPHeader

java.lang.Object
org.opennms.protocols.ip.IPHeader

public class IPHeader extends Object
This class defines a loaded IP header object. It does not allow the user of the class to set any of the values. Nor can a default object be constructed. A copy of an existing header can be created or one can be loaded from a collection of bytes. For more information on the IP header see the book "TCP/IP Illustrated, Volume 1: The Protocols" by W. Richard Stevens.
Author:
Brian Weaver
  • Field Details

    • IP_VERSION

      public static final int IP_VERSION
      The supported version of the IP header
      See Also:
    • TOS_PRECEDENCE_MASK

      public static final int TOS_PRECEDENCE_MASK
      The Type-Of-Service mask. This constant is used to mask bits that define the type of service field. See RFC 791.
      See Also:
    • TOS_PRECEDENCE_NETWORK_CRITICAL

      public static final int TOS_PRECEDENCE_NETWORK_CRITICAL
      Network Critical TOS. See RFC 791.
      See Also:
    • TOS_PRECEDENCE_INTERNETWORK_CONTROL

      public static final int TOS_PRECEDENCE_INTERNETWORK_CONTROL
      Internetworking Control TOS. See RFC 791.
      See Also:
    • TOS_PRECEDENCE_CRITICAL_ECP

      public static final int TOS_PRECEDENCE_CRITICAL_ECP
      Critical/ECP TOS. See RFC 791.
      See Also:
    • TOS_PRECEDENCE_FLASH_OVERRIDE

      public static final int TOS_PRECEDENCE_FLASH_OVERRIDE
      Flash Override TOS. See RFC 791.
      See Also:
    • TOS_PRECEDENCE_FLASH

      public static final int TOS_PRECEDENCE_FLASH
      Flash TOS. See RFC 791.
      See Also:
    • TOS_PRECEDENCE_IMMEDIATE

      public static final int TOS_PRECEDENCE_IMMEDIATE
      Immediate TOS. See RFC 791.
      See Also:
    • TOS_PRECEDENCE_PRIORITY

      public static final int TOS_PRECEDENCE_PRIORITY
      Priority TOS. See RFC 791.
      See Also:
    • TOS_PRECEDENCE_ROUTINE

      public static final int TOS_PRECEDENCE_ROUTINE
      Routine TOS. See RFC 791.
      See Also:
    • TOS_DELAY_MASK

      public static final int TOS_DELAY_MASK
      TOS delay mask as defined by RFC 791.
      See Also:
    • TOS_DELAY_LOW

      public static final int TOS_DELAY_LOW
      Minimize the delay when handling packets.
      See Also:
    • TOS_DELAY_NORMAL

      public static final int TOS_DELAY_NORMAL
      Normal packet handling
      See Also:
    • TOS_THROUGHPUT_MASK

      public static final int TOS_THROUGHPUT_MASK
      TOS Throughput mask
      See Also:
    • TOS_THROUGHPUT_HIGH

      public static final int TOS_THROUGHPUT_HIGH
      High throughput requested
      See Also:
    • TOS_THROUGHPUT_NORMAL

      public static final int TOS_THROUGHPUT_NORMAL
      Normal throughput requested
      See Also:
    • TOS_RELIBILITY_MASK

      public static final int TOS_RELIBILITY_MASK
      Packet reliablity mask.
      See Also:
    • TOS_RELIBILITY_HIGH

      public static final int TOS_RELIBILITY_HIGH
      High Reliability requested.
      See Also:
    • TOS_RELIBILITY_NORMAL

      public static final int TOS_RELIBILITY_NORMAL
      Normal reliability requrested
      See Also:
    • TOS_RESERVED_MASK

      public static final int TOS_RESERVED_MASK
      Mask of the reseered bits.
      See Also:
    • FLAGS_MASK

      public static final int FLAGS_MASK
      The mask of the flags in the fragment field of the IP header
      See Also:
    • FLAGS_DONT_FRAGMENT

      public static final int FLAGS_DONT_FRAGMENT
      Don't fragment datagrams field
      See Also:
    • FLAGS_MORE_FRAGMENTS

      public static final int FLAGS_MORE_FRAGMENTS
      More fragments are necessary to reassemble this packet
      See Also:
    • OPTION_COPY_MASK

      public static final int OPTION_COPY_MASK
      The bit(s) that define if the optiosn are copied to each datagram when (or if) it is fragmented.
      See Also:
    • OPTION_CLASS_MASK

      public static final int OPTION_CLASS_MASK
      The option class mask
      See Also:
    • OPTION_NUMBER_MASK

      public static final int OPTION_NUMBER_MASK
      The option number mask
      See Also:
    • OPTION_ID_EOO

      public static final int OPTION_ID_EOO
      Option identifier for the End Of Options List option.
      See Also:
    • OPTION_ID_LOOSE_SOURCE_ROUTING

      public static final int OPTION_ID_LOOSE_SOURCE_ROUTING
      Option identifier for the loose source routing option
      See Also:
    • OPTION_ID_STRICT_SOURCE_ROUTING

      public static final int OPTION_ID_STRICT_SOURCE_ROUTING
      Option identifer for the the strict source routing option
      See Also:
    • OPTION_ID_ROUTE_RECORD

      public static final int OPTION_ID_ROUTE_RECORD
      Option identifier for the route record option
      See Also:
  • Constructor Details

    • IPHeader

      public IPHeader()
      Constructs a basic IP header, but the header is not valid until a large part of the information is configured.
    • IPHeader

      public IPHeader(IPHeader second)
      Constructs a new IP Header object that is identical to the passed IPHeader. The new object is a complete duplicate including the option data that is copied into a newly allocated array.
      Parameters:
      second - The object to duplicate.
    • IPHeader

      public IPHeader(byte[] header, int offset)
      Constructs a new IPHeader object from the passed data buffer. The data is gathered from the buffer starting at the location marked by offset. If there is not sufficent data in the buffer then an exception is thrown.
      Parameters:
      header - The buffer containing the header
      offset - The offset into the buffer where the IP header is located.
      Throws:
      IndexOutOfBoundsException - This exception is thrown if the minimum number of bytes are not present to represent an IPHeader object.
      UnknownIPVersionException - Thrown if the format of the version is unknown.
  • Method Details

    • getVersion

      public byte getVersion()
      Used to retreive the current version of the IP Header. Currently only version 4 is supported.
      Returns:
      The current IP version.
    • getHeaderLength

      public int getHeaderLength()
      Used to get the current length of the IP Header.
      Returns:
      The current IP header length.
    • getTypeOfService

      public byte getTypeOfService()
      Retreives the current TOS field from the header.
      Returns:
      The current TOS.
    • setTypeOfService

      public void setTypeOfService(byte tos)
      Sets the TOS flags for the IP header.
      Parameters:
      tos - The new TOS for the IP header
    • getTypeOfService

      public boolean getTypeOfService(int bit)
      Use to test individual bits in the TOS fields. If the field is set then a value of true is returned. If the field is not set then a false value is returned.
      Parameters:
      bit - The bit to validate. Valid values are 0 - 7.
      Returns:
      True if the bit is set, false otherwise.
    • getPacketLength

      public int getPacketLength()
      Returns the length of the IP packet, including the header, in bytes.
      Returns:
      The total packet length
    • setPacketLength

      public void setPacketLength(short length)
      Sets the length for IP packet, including the header. When setting this value the size of the IP header must be accounted for.
      Parameters:
      length - The length of the IP header plus the data contained within
    • getIdentity

      public short getIdentity()
      Used to retreive the 16-bit identity of the header.
      Returns:
      The header's identity.
    • setIdentity

      public void setIdentity(short ident)
      Sets the identity of the IP header
      Parameters:
      ident - The new identity of the IP header
    • getFlags

      public byte getFlags()
      Used to get the 3-bit flags from the header. The flags are located in the 3 least significant bits of the returned byte.
      Returns:
      The byte containing the three flags.
    • setFlags

      public void setFlags(byte flags)
      Sets the flags contained in the upper 3 bits of the short value for the fragmentation offset. The passed bits should occupy the lower 3 bits of the passed byte.
      Parameters:
      flags - The flag bits, set in the lower 3 bits of the value.
    • getFlag

      public boolean getFlag(int bit)
      Used to get an individual flag from the flags field. The bit must be in the range of [0..3).
      Parameters:
      bit - The flag to retreive.
      Returns:
      True if the bit is set, false otherwise.
    • getFragmentOffset

      public short getFragmentOffset()
      Returns the 13-bit fragment offset field from the IP header.
      Returns:
      The 13-bit fragment offset.
    • setFragmentOffset

      public void setFragmentOffset(short offset)
      Sets the fragmentation index for this packet
    • getTTL

      public byte getTTL()
      Gets the 8-bit Time To Live (TTL) of the packet.
      Returns:
      The packet's ttl.
    • setTTL

      public void setTTL(byte ttl)
      Sets the time to live for the IP header
    • getProtocol

      public byte getProtocol()
      Gets the protocol for the IP datagram.
      Returns:
      The 8-bit protocol field.
    • setProtocol

      public void setProtocol(byte protocol)
      Sets the protocol for the IP header.
      Parameters:
      protocol - The IP protocol.
    • getChecksum

      public short getChecksum()
      Gets the 16-bit ones compliment checksum for the IP header.
      Returns:
      The 16-bit ones compliment checksum.
    • setChecksum

      public void setChecksum(short sum)
      Sets the checksum for the IP header.
      Parameters:
      sum - The IP header checksum.
    • getSourceAddress

      public int getSourceAddress()
      Returns the dotted decimal string address of the source IP address.
      Returns:
      The 32-bit IPv4 address
    • setSourceAddr

      public void setSourceAddr(int addr)
      Sets the IP headers source address.
      Parameters:
      addr - The soruce address for the header.
    • getDestinationAddress

      public int getDestinationAddress()
      Returns the dotted decimal string address of the destination IP address.
      Returns:
      The 32-bit IPv4 address.
    • setDestinationAddress

      public void setDestinationAddress(int addr)
      Sets the IP headers destination address.
      Parameters:
      addr - The destination address
    • getOptionData

      public byte[] getOptionData()
      Retrieves the IP header options from the header. The data is treated as a varaiable length of option data. The IPHeader object does not attempt to interpert the data.
      Returns:
      The IP header option data, null if there is none.
    • setOptionData

      public void setOptionData(byte[] options)
      Sets the current option data for the header.
      Parameters:
      options - The new options data.
    • getOptions

      public List<IPHeader.Option> getOptions() throws InstantiationException
      Returns a list of options that are associated with the IP header.
      Returns:
      The list of current options.
      Throws:
      InstantiationException
    • addOption

      public void addOption(IPHeader.Option opt)
      Adds an option to the IP header.
      Parameters:
      opt - The option to add to the header.
    • writeBytes

      public int writeBytes(byte[] data, int offset)
      Stores the IP header as an array of bytes into the passed data buffer. The IP header is written starting at the specified offset, and the new offset is returned to the caller.
      Parameters:
      data - The location to write the data
      offset - The offset to start storing information.
      Returns:
      The new offset just beyond the last written byte.
    • addressToString

      public static String addressToString(int ipv4Addr)
      Converts the passed 32-bit IPv4 address to a dotted decimal IP address string.
      Parameters:
      ipv4Addr - The 32-bit address
      Returns:
      The dotted decimal address in the format "xxx.xxx.xxx.xxx" where 0 <= xxx < 256
    • addressToString

      public static String addressToString(byte[] buf)
      Converts the passed IPv4 address buffer to a dotted decimal IP address string.
      Parameters:
      buf - The 4 byte buffer
      Returns:
      The dotted decimal address in the format "xxx.xxx.xxx.xxx" where 0 <= xxx < 256
      Throws:
      IllegalArgumentException - Thrown if the buffer is not exactly 4 bytes in length.