OpenNMS API 1.2.3

org.opennms.protocols.ip
Class IPHeader

java.lang.Object
  extended byorg.opennms.protocols.ip.IPHeader

public class IPHeader
extends java.lang.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

Nested Class Summary
static class IPHeader.EndOfOptions
          This class is used to represent the End-Of-Option list in the IP header.
static class IPHeader.LooseSourceRouteOption
          This class represents the loose source routing options that may be part of an IP header.
static class IPHeader.Option
          The Option class is used as the base class for any options that are at the end of the IP header.
static class IPHeader.RouteOption
          This class represents routing options that may be part of an IP header.
static class IPHeader.RouteRecordOption
          This class represents the route record option that may be part of an IP header.
static class IPHeader.StrictSourceRouteOption
          This class represents the strict source routing options that may be part of an IP header.
 
Field Summary
static int FLAGS_DONT_FRAGMENT
          Don't fragment datagrams field
static int FLAGS_MASK
          The mask of the flags in the fragment field of the IP header
static int FLAGS_MORE_FRAGMENTS
          More fragments are necessary to reassemble this packet
static int IP_VERSION
          The supported version of the IP header
private  short m_checksum
          One's compliment 16-bit checksum of the header only.
private  int m_dstAddr
          Destination address of the IP datagram
private  byte m_flags
          The fragmentation flags tha occupy the upper 3 bits of the fragment offset field
private  short m_fragOffset
          The fragment offset of this packet
private  byte m_hdrlen
          The length of the IP header in 32-bit words
private  short m_identity
          The identity of the IP dagagram.
private  short m_length
          The total length of the IP datagram and it's payload
private  byte[] m_options
          any option data in the datagram
private  byte m_protocol
          The protocol encapuslated by this packet
private  int m_srcAddr
          Source address of the IP datagram
private  byte m_tos
          the Type-Of-Service defined for the IP datagram
private  byte m_ttl
          The Time-To-Live for this IP packet
private  byte m_version
          The IP version
static int OPTION_CLASS_MASK
          The option class mask
static int OPTION_COPY_MASK
          The bit(s) that define if the optiosn are copied to each datagram when (or if) it is fragmented.
static int OPTION_ID_EOO
          Option identifier for the End Of Options List option.
static int OPTION_ID_LOOSE_SOURCE_ROUTING
          Option identifier for the loose source routing option
static int OPTION_ID_ROUTE_RECORD
          Option identifier for the route record option
static int OPTION_ID_STRICT_SOURCE_ROUTING
          Option identifer for the the strict source routing option
static int OPTION_NUMBER_MASK
          The option number mask
static int TOS_DELAY_LOW
          Minimize the delay when handling packets.
static int TOS_DELAY_MASK
          TOS delay mask as defined by RFC 791.
static int TOS_DELAY_NORMAL
          Normal packet handling
static int TOS_PRECEDENCE_CRITICAL_ECP
          Critical/ECP TOS.
static int TOS_PRECEDENCE_FLASH
          Flash TOS.
static int TOS_PRECEDENCE_FLASH_OVERRIDE
          Flash Override TOS.
static int TOS_PRECEDENCE_IMMEDIATE
          Immediate TOS.
static int TOS_PRECEDENCE_INTERNETWORK_CONTROL
          Internetworking Control TOS.
static int TOS_PRECEDENCE_MASK
          The Type-Of-Service mask.
static int TOS_PRECEDENCE_NETWORK_CRITICAL
          Network Critical TOS.
static int TOS_PRECEDENCE_PRIORITY
          Priority TOS.
static int TOS_PRECEDENCE_ROUTINE
          Routine TOS.
static int TOS_RELIBILITY_HIGH
          High Reliability requested.
static int TOS_RELIBILITY_MASK
          Packet reliablity mask.
static int TOS_RELIBILITY_NORMAL
          Normal reliability requrested
static int TOS_RESERVED_MASK
          Mask of the reseered bits.
static int TOS_THROUGHPUT_HIGH
          High throughput requested
static int TOS_THROUGHPUT_MASK
          TOS Throughput mask
static int TOS_THROUGHPUT_NORMAL
          Normal throughput requested
 
Constructor Summary
IPHeader()
          Constructs a basic IP header, but the header is not valid until a large part of the information is configured.
IPHeader(byte[] header, int offset)
          Constructs a new IPHeader object from the passed data buffer.
IPHeader(IPHeader second)
          Constructs a new IP Header object that is identical to the passed IPHeader.
 
Method Summary
 void addOption(IPHeader.Option opt)
          Adds an option to the IP header.
static java.lang.String addressToString(byte[] buf)
          Converts the passed IPv4 address buffer to a dotted decimal IP address string.
static java.lang.String addressToString(int ipv4Addr)
          Converts the passed 32-bit IPv4 address to a dotted decimal IP address string.
private static int byteToInt(byte b)
          Converts a byte to an integer, treating the byte as unsigned.
private static short byteToShort(byte b)
          Converts a byte to a short, treating the byte as unsigned.
private  byte[] dup(byte[] src)
          Duplicates the array of bytes.
 short getChecksum()
          Gets the 16-bit ones compliment checksum for the IP header.
 int getDestinationAddress()
          Returns the dotted decimal string address of the destination IP address.
 boolean getFlag(int bit)
          Used to get an individual flag from the flags field.
 byte getFlags()
          Used to get the 3-bit flags from the header.
 short getFragmentOffset()
          Returns the 13-bit fragment offset field from the IP header.
 int getHeaderLength()
          Used to get the current length of the IP Header.
 short getIdentity()
          Used to retreive the 16-bit identity of the header.
 byte[] getOptionData()
          Retrieves the IP header options from the header.
 java.util.List getOptions()
          Returns a list of options that are associated with the IP header.
 int getPacketLength()
          Returns the length of the IP packet, including the header, in bytes.
 byte getProtocol()
          Gets the protocol for the IP datagram.
 int getSourceAddress()
          Returns the dotted decimal string address of the source IP address.
 byte getTTL()
          Gets the 8-bit Time To Live (TTL) of the packet.
 byte getTypeOfService()
          Retreives the current TOS field from the header.
 boolean getTypeOfService(int bit)
          Use to test individual bits in the TOS fields.
 byte getVersion()
          Used to retreive the current version of the IP Header.
 void setChecksum(short sum)
          Sets the checksum for the IP header.
 void setDestinationAddress(int addr)
          Sets the IP headers destination address.
 void setFlags(byte flags)
          Sets the flags contained in the upper 3 bits of the short value for the fragmentation offset.
 void setFragmentOffset(short offset)
          Sets the fragmentation index for this packet
 void setIdentity(short ident)
          Sets the identity of the IP header
 void setOptionData(byte[] options)
          Sets the current option data for the header.
 void setPacketLength(short length)
          Sets the length for IP packet, including the header.
 void setProtocol(byte protocol)
          Sets the protocol for the IP header.
 void setSourceAddr(int addr)
          Sets the IP headers source address.
 void setTTL(byte ttl)
          Sets the time to live for the IP header
 void setTypeOfService(byte tos)
          Sets the TOS flags for the IP header.
private static int shortToInt(short s)
          Converts a short to an integer, treating the short as unsigned.
 int writeBytes(byte[] data, int offset)
          Stores the IP header as an array of bytes into the passed data buffer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

IP_VERSION

public static final int IP_VERSION
The supported version of the IP header

See Also:
Constant Field Values

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:
Constant Field Values

TOS_PRECEDENCE_NETWORK_CRITICAL

public static final int TOS_PRECEDENCE_NETWORK_CRITICAL
Network Critical TOS. See RFC 791.

See Also:
Constant Field Values

TOS_PRECEDENCE_INTERNETWORK_CONTROL

public static final int TOS_PRECEDENCE_INTERNETWORK_CONTROL
Internetworking Control TOS. See RFC 791.

See Also:
Constant Field Values

TOS_PRECEDENCE_CRITICAL_ECP

public static final int TOS_PRECEDENCE_CRITICAL_ECP
Critical/ECP TOS. See RFC 791.

See Also:
Constant Field Values

TOS_PRECEDENCE_FLASH_OVERRIDE

public static final int TOS_PRECEDENCE_FLASH_OVERRIDE
Flash Override TOS. See RFC 791.

See Also:
Constant Field Values

TOS_PRECEDENCE_FLASH

public static final int TOS_PRECEDENCE_FLASH
Flash TOS. See RFC 791.

See Also:
Constant Field Values

TOS_PRECEDENCE_IMMEDIATE

public static final int TOS_PRECEDENCE_IMMEDIATE
Immediate TOS. See RFC 791.

See Also:
Constant Field Values

TOS_PRECEDENCE_PRIORITY

public static final int TOS_PRECEDENCE_PRIORITY
Priority TOS. See RFC 791.

See Also:
Constant Field Values

TOS_PRECEDENCE_ROUTINE

public static final int TOS_PRECEDENCE_ROUTINE
Routine TOS. See RFC 791.

See Also:
Constant Field Values

TOS_DELAY_MASK

public static final int TOS_DELAY_MASK
TOS delay mask as defined by RFC 791.

See Also:
Constant Field Values

TOS_DELAY_LOW

public static final int TOS_DELAY_LOW
Minimize the delay when handling packets.

See Also:
Constant Field Values

TOS_DELAY_NORMAL

public static final int TOS_DELAY_NORMAL
Normal packet handling

See Also:
Constant Field Values

TOS_THROUGHPUT_MASK

public static final int TOS_THROUGHPUT_MASK
TOS Throughput mask

See Also:
Constant Field Values

TOS_THROUGHPUT_HIGH

public static final int TOS_THROUGHPUT_HIGH
High throughput requested

See Also:
Constant Field Values

TOS_THROUGHPUT_NORMAL

public static final int TOS_THROUGHPUT_NORMAL
Normal throughput requested

See Also:
Constant Field Values

TOS_RELIBILITY_MASK

public static final int TOS_RELIBILITY_MASK
Packet reliablity mask.

See Also:
Constant Field Values

TOS_RELIBILITY_HIGH

public static final int TOS_RELIBILITY_HIGH
High Reliability requested.

See Also:
Constant Field Values

TOS_RELIBILITY_NORMAL

public static final int TOS_RELIBILITY_NORMAL
Normal reliability requrested

See Also:
Constant Field Values

TOS_RESERVED_MASK

public static final int TOS_RESERVED_MASK
Mask of the reseered bits.

See Also:
Constant Field Values

FLAGS_MASK

public static final int FLAGS_MASK
The mask of the flags in the fragment field of the IP header

See Also:
Constant Field Values

FLAGS_DONT_FRAGMENT

public static final int FLAGS_DONT_FRAGMENT
Don't fragment datagrams field

See Also:
Constant Field Values

FLAGS_MORE_FRAGMENTS

public static final int FLAGS_MORE_FRAGMENTS
More fragments are necessary to reassemble this packet

See Also:
Constant Field Values

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:
Constant Field Values

OPTION_CLASS_MASK

public static final int OPTION_CLASS_MASK
The option class mask

See Also:
Constant Field Values

OPTION_NUMBER_MASK

public static final int OPTION_NUMBER_MASK
The option number mask

See Also:
Constant Field Values

OPTION_ID_EOO

public static final int OPTION_ID_EOO
Option identifier for the End Of Options List option.

See Also:
Constant Field Values

OPTION_ID_LOOSE_SOURCE_ROUTING

public static final int OPTION_ID_LOOSE_SOURCE_ROUTING
Option identifier for the loose source routing option

See Also:
Constant Field Values

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:
Constant Field Values

OPTION_ID_ROUTE_RECORD

public static final int OPTION_ID_ROUTE_RECORD
Option identifier for the route record option

See Also:
Constant Field Values

m_version

private byte m_version
The IP version


m_hdrlen

private byte m_hdrlen
The length of the IP header in 32-bit words


m_tos

private byte m_tos
the Type-Of-Service defined for the IP datagram


m_length

private short m_length
The total length of the IP datagram and it's payload


m_identity

private short m_identity
The identity of the IP dagagram.


m_flags

private byte m_flags
The fragmentation flags tha occupy the upper 3 bits of the fragment offset field


m_fragOffset

private short m_fragOffset
The fragment offset of this packet


m_ttl

private byte m_ttl
The Time-To-Live for this IP packet


m_protocol

private byte m_protocol
The protocol encapuslated by this packet


m_checksum

private short m_checksum
One's compliment 16-bit checksum of the header only. This does not include the value for the data


m_srcAddr

private int m_srcAddr
Source address of the IP datagram


m_dstAddr

private int m_dstAddr
Destination address of the IP datagram


m_options

private byte[] m_options
any option data in the datagram

Constructor Detail

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:
java.lang.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 Detail

dup

private byte[] dup(byte[] src)
Duplicates the array of bytes.

Parameters:
src - The source bytes to duplicate.
Returns:
The duplicated array of bytes.

byteToShort

private static short byteToShort(byte b)
Converts a byte to a short, treating the byte as unsigned.

Parameters:
b - The byte to convert.
Returns:
The converted value.

byteToInt

private static int byteToInt(byte b)
Converts a byte to an integer, treating the byte as unsigned.

Parameters:
b - The byte to convert.
Returns:
The converted value.

shortToInt

private static int shortToInt(short s)
Converts a short to an integer, treating the short as unsigned.

Parameters:
s - The short to convert.
Returns:
The converted value

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 java.util.List getOptions()
                          throws java.lang.InstantiationException
Returns a list of options that are associated with the IP header.

Returns:
The list of current options.
Throws:
java.lang.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 java.lang.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 java.lang.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:
java.lang.IllegalArgumentException - Thrown if the buffer is not exactly 4 bytes in length.

OpenNMS API 1.2.3

Generated by eevans on May 29 2005 2015.