Class ICMPHeader

java.lang.Object
org.opennms.protocols.icmp.ICMPHeader
Direct Known Subclasses:
AddressMaskReply, AddressMaskRequest, ErrorMessage, ICMPEchoPacket, TimestampReply, TimestampRequest

public class ICMPHeader extends Object
This class defines Internet Control Message Protocol header. The header defines the first 8 bytes of any ICMP message. Some ICMP messages may need to override the format of the second 4 bytes, the first 4 bytes are fixed by the RFC. The minimum message size for any ICMP message is 8 bytes.
Version:
0.1
Author:
Brian Weaver
  • Field Details

    • TYPE_ECHO_REPLY

      public static final byte TYPE_ECHO_REPLY
      See Also:
    • TYPE_DESTINATION_UNREACHABLE

      public static final byte TYPE_DESTINATION_UNREACHABLE
      See Also:
    • CODE_NETWORK_UNREACHABLE

      public static final byte CODE_NETWORK_UNREACHABLE
      See Also:
    • CODE_HOST_UNREACHABLE

      public static final byte CODE_HOST_UNREACHABLE
      See Also:
    • CODE_PROTOCOL_UNREACHABLE

      public static final byte CODE_PROTOCOL_UNREACHABLE
      See Also:
    • CODE_PORT_UNREACHABLE

      public static final byte CODE_PORT_UNREACHABLE
      See Also:
    • CODE_FRAGMENTATION_NEEDED

      public static final byte CODE_FRAGMENTATION_NEEDED
      See Also:
    • CODE_SOURCE_ROUTE_FAILED

      public static final byte CODE_SOURCE_ROUTE_FAILED
      See Also:
    • CODE_DESTINATION_NETWORK_UNKNOWN

      public static final byte CODE_DESTINATION_NETWORK_UNKNOWN
      See Also:
    • CODE_DESTINATION_HOST_UNKNOWN

      public static final byte CODE_DESTINATION_HOST_UNKNOWN
      See Also:
    • CODE_SOURCE_HOST_ISOLATED

      public static final byte CODE_SOURCE_HOST_ISOLATED
      See Also:
    • CODE_DESTINATION_NETWORK_ADMIN_PROHIBITED

      public static final byte CODE_DESTINATION_NETWORK_ADMIN_PROHIBITED
      See Also:
    • CODE_DESTINATION_HOST_ADMIN_PROHIBITED

      public static final byte CODE_DESTINATION_HOST_ADMIN_PROHIBITED
      See Also:
    • CODE_NETWORK_UNREACHABLE_FOR_TOS

      public static final byte CODE_NETWORK_UNREACHABLE_FOR_TOS
      See Also:
    • CODE_HOST_UNREACHABLE_FOR_TOS

      public static final byte CODE_HOST_UNREACHABLE_FOR_TOS
      See Also:
    • CODE_COMMUNICATIONS_ADMIN_PROHIBITIED

      public static final byte CODE_COMMUNICATIONS_ADMIN_PROHIBITIED
      See Also:
    • CODE_HOST_PRECEDENCE_VIOLATION

      public static final byte CODE_HOST_PRECEDENCE_VIOLATION
      See Also:
    • CODE_PRECEDENCE_CUTOFF_IN_EFFECT

      public static final byte CODE_PRECEDENCE_CUTOFF_IN_EFFECT
      See Also:
    • TYPE_SOURCE_QUENCH

      public static final byte TYPE_SOURCE_QUENCH
      See Also:
    • TYPE_REDIRECT

      public static final byte TYPE_REDIRECT
      See Also:
    • CODE_REDIRECT_FOR_NETWORK

      public static final byte CODE_REDIRECT_FOR_NETWORK
      See Also:
    • CODE_REDIRECT_FOR_HOST

      public static final byte CODE_REDIRECT_FOR_HOST
      See Also:
    • CODE_REDIRECT_FOR_TYPE_OF_SERVICE_AND_NETWORK

      public static final byte CODE_REDIRECT_FOR_TYPE_OF_SERVICE_AND_NETWORK
      See Also:
    • CODE_REDIRECT_FOR_TYPE_OF_SERVICE_AND_HOST

      public static final byte CODE_REDIRECT_FOR_TYPE_OF_SERVICE_AND_HOST
      See Also:
    • TYPE_ECHO_REQUEST

      public static final byte TYPE_ECHO_REQUEST
      See Also:
    • TYPE_ROUTER_ADVERTISEMENT

      public static final byte TYPE_ROUTER_ADVERTISEMENT
      See Also:
    • TYPE_ROUTER_SOLICITATION

      public static final byte TYPE_ROUTER_SOLICITATION
      See Also:
    • TYPE_TIME_EXCEEDED

      public static final byte TYPE_TIME_EXCEEDED
      See Also:
    • CODE_TTL_EQ_ZERO_IN_TRANSIT

      public static final byte CODE_TTL_EQ_ZERO_IN_TRANSIT
      See Also:
    • CODE_TTL_EQ_ZERO_IN_REASSEMBLY

      public static final byte CODE_TTL_EQ_ZERO_IN_REASSEMBLY
      See Also:
    • TYPE_PARAMETER_PROBLEM

      public static final byte TYPE_PARAMETER_PROBLEM
      See Also:
    • CODE_BAD_IP_HEADER

      public static final byte CODE_BAD_IP_HEADER
      See Also:
    • CODE_REQUIRED_OPTION_MISSING

      public static final byte CODE_REQUIRED_OPTION_MISSING
      See Also:
    • TYPE_TIMESTAMP_REQUEST

      public static final byte TYPE_TIMESTAMP_REQUEST
      See Also:
    • TYPE_TIMESTAMP_REPLY

      public static final byte TYPE_TIMESTAMP_REPLY
      See Also:
    • TYPE_INFORMATION_REQUEST

      public static final byte TYPE_INFORMATION_REQUEST
      See Also:
    • TYPE_INFORMATION_REPLY

      public static final byte TYPE_INFORMATION_REPLY
      See Also:
    • TYPE_ADDRESS_MASK_REQUEST

      public static final byte TYPE_ADDRESS_MASK_REQUEST
      See Also:
    • TYPE_ADDRESS_MASK_REPLY

      public static final byte TYPE_ADDRESS_MASK_REPLY
      See Also:
  • Constructor Details

    • ICMPHeader

      public ICMPHeader()
      Initializes the header to a default value.
    • ICMPHeader

      public ICMPHeader(byte type)
      Initializes the header using the specified type.
      Parameters:
      type - The header type.
    • ICMPHeader

      public ICMPHeader(byte type, byte code)
      Initializes the header with the specified type and code values.
      Parameters:
      type - The type value for the header
      code - The code value for the header
    • ICMPHeader

      public ICMPHeader(byte type, byte code, short checksum, short identity, short sequence)
      Constructs an ICMP header with the specified header fields.
      Parameters:
      type - The 8-bit ICMP type.
      code - The 8-bit ICMP code.
      checksum - The 16-bit checksum header.
      identity - The 16-bit identity (user).
      sequence - The 16-bit sequence id.
    • ICMPHeader

      public ICMPHeader(ICMPHeader second)
      Constructs a duplicate ICMP header that is identical to the passed ICMPHeader object.
      Parameters:
      second - The object to duplicate.
    • ICMPHeader

      public ICMPHeader(byte[] data, int offset)

      Constructs a new ICMP header based upon the data contained in the buffer. The buffer is decode in network byte ordering (big-endin) and must be at least a minimumnumber of bytes available to be decoded.

      If there is an insufficent amount of data to decode a header then an exception is thrown.

      Parameters:
      data - The data buffer containing the header
      offset - The offset of the header in the buffer
      Throws:
      IndexOutOfBoundsException - Thrown if there is not enough data to construct the header.
  • Method Details

    • nextSequenceId

      public static final short nextSequenceId()
      Returns the next 16-bit sequence identifier for the class. The method is synchronized to prevent duplicate identifiers from being issued. depending on the number of classes and how often the method is called it will eventually wrap. When the value wraps back to zero, a random number is generated and may cause a collision with an existing identifier. The probability is low, but possible.
      Returns:
      The next 16-bit sequence number, may be negative.
    • byteToShort

      protected static short byteToShort(byte b)
      Converts a byte to a short.
      Parameters:
      b - The byte to convert.
      Returns:
      The converted byte.
    • byteToInt

      protected static int byteToInt(byte b)
      Converts a byte to an integer.
      Parameters:
      b - The byte to convert.
      Returns:
      The converted byte.
    • getType

      public final byte getType()
      Returns the 8-bit type code for the ICMP packet.
      Returns:
      The ICMP type.
    • setType

      protected void setType(byte type)
      Sets the 8-bit type code for the packet.
      Parameters:
      type - The new ICMP type.
    • getCode

      public final byte getCode()
      Returns the 8-bit code for the ICMP packet.
      Returns:
      The ICMP code.
    • setCode

      public final void setCode(byte code)
      Sets the 8-bit code for the ICMP packet
      Parameters:
      code - The new ICMP code.
    • getSequenceId

      public final short getSequenceId()
      Returns the sequence identifier for the ICMP header.
      Returns:
      The 16-bit sequence identifier.
    • setNextSequenceId

      public final short setNextSequenceId()
      Gets the next global identifier and sets the value in the object. In addition the new sequence identifier is returned.
      Returns:
      The new 16-bit sequence identifier.
    • setSequenceId

      public final void setSequenceId(short id)
      Sets the headers 16-bit sequence identifier.
      Parameters:
      id - The new 16-bit sequence id.
    • getIdentity

      public final short getIdentity()
      Used to get the headers user defined identity.
      Returns:
      The 16-bit identity.
    • setIdentity

      public final void setIdentity(short identity)
      Sets the header's 16-bit user defined identity value.
      Parameters:
      identity - The header's new identity.
    • getChecksum

      public final short getChecksum()
      Used to retrieve the current checksum for the header. This is the last checksum computed for the header (or it's derived classes). To compute the new checksum a call to the method computeChecksum() must be called.
      Returns:
      The 16-bit one's compliment checksum.
    • setChecksum

      protected void setChecksum(short sum)
    • computeChecksum

      public void computeChecksum()
      Provides the default checksum implementation for the ICMP header. It MUST be overriden by any derived classes to ensure that the checksum includes the dervived class data. Once the checksum is calculated, a call to the method getChecksum() will return the calculated sum.
    • computeChecksum

      protected void computeChecksum(OC16ChecksumProducer summer)
      Used by derived classes to begin the checksum process. The process involves setting the checksum to zero and then performing the checksum over the various values.
      Parameters:
      summer - The checksum builder object.
    • storeToBuffer

      protected int storeToBuffer(byte[] buf, int offset)
      Writes the ICMP header out to the specified buffer at the starting offset. If the buffer does not have sufficent data to store the information then an IndexOutOfBoundsException is thrown.
      Parameters:
      buf - The storage buffer.
      offset - The location to start in buf.
      Returns:
      The new offset after storing to the buffer.
      Throws:
      IndexOutOfBoundsException - Thrown if the buffer does not have enough storage space.
    • loadFromBuffer

      protected int loadFromBuffer(byte[] buf, int offset)
      Reads the ICMP header from the specified buffer and sets the internal fields equal to the data. If the buffer does not have sufficent data to restore the header then an IndexOutOfBoundsExceptoin is thrown by the method.
      Parameters:
      buf - The buffer to read the data from.
      offset - The offset to start reading data.
      Returns:
      The new offset after reading the data.
      Throws:
      IndexOutOfBoundsException - is thrown if there is not sufficent data in the buffer.
    • getNetworkSize

      @Deprecated public static int getNetworkSize()
      Deprecated.
      Returns the number of bytes required to read/write an icmp header. A header is composed of a fixed size number of byte. This is not expected to change, but this method allows derived classes to query the number of bytes for reading/writing an icmp header. Thus should the standard ever change, the derived class should be able to dynamically handle the change.
      Returns:
      The number of bytes required to read/write an icmp header.
    • getHeaderSize

      public int getHeaderSize()
    • isEchoReply

      public final boolean isEchoReply()
      Used to test to see if the header is an echo reply message. If it is an echo reply message then true is returned.
      Returns:
      True if the header marks an echo reply.
    • isEchoRequest

      public final boolean isEchoRequest()
      Used to test to see if the header is an echo request message. If it is an echo request message then true is returned.
      Returns:
      True if the header marks an echo request.
    • toBytes

      public byte[] toBytes()
      Converts the object to a string of bytes.