org.opennms.protocols.ip
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.

Version:
0.1
Author:
Brian Weaver

Field Summary
private  short m_checksum
           
private  int m_dstAddr
           
private  byte m_flags
           
private  short m_fragOffset
           
private  byte m_hdrlen
           
private  short m_identity
           
private  short m_length
           
private  byte[] m_options
           
private  byte m_protocol
           
private  int m_srcAddr
           
private  byte m_tos
           
private  byte m_ttl
           
private  byte m_version
           
 
Constructor Summary
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
static String addressToString(byte[] buf)
          Converts the passed IPv4 address buffer to a dotted decimal IP address string.
static 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.
 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.
private static int shortToInt(short s)
          Converts a short to an integer, treating the short as unsigned.
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

m_version

private byte m_version

m_hdrlen

private byte m_hdrlen

m_tos

private byte m_tos

m_length

private short m_length

m_identity

private short m_identity

m_flags

private byte m_flags

m_fragOffset

private short m_fragOffset

m_ttl

private byte m_ttl

m_protocol

private byte m_protocol

m_checksum

private short m_checksum

m_srcAddr

private int m_srcAddr

m_dstAddr

private int m_dstAddr

m_options

private byte[] m_options
Constructor Detail

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:
InsufficientDataException - 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.

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

getIdentity

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

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.

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.

getTTL

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

getProtocol

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

getChecksum

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

getSourceAddress

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

getDestinationAddress

public int getDestinationAddress()
Returns the dotted decimal string address of the destination IP address.
Returns:
The 32-bit IPv4 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.

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.