Package org.opennms.protocols.ip
Class IPHeader
java.lang.Object
org.opennms.protocols.ip.IPHeader
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
Nested ClassesModifier and TypeClassDescriptionstatic final classThis class is used to represent the End-Of-Option list in the IP header.static final classThis class represents the loose source routing options that may be part of an IP header.static classThe Option class is used as the base class for any options that are at the end of the IP header.static classThis class represents routing options that may be part of an IP header.static final classThis class represents the route record option that may be part of an IP header.static final classThis class represents the strict source routing options that may be part of an IP header. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intDon't fragment datagrams fieldstatic final intThe mask of the flags in the fragment field of the IP headerstatic final intMore fragments are necessary to reassemble this packetstatic final intThe supported version of the IP headerstatic final intThe option class maskstatic final intThe bit(s) that define if the optiosn are copied to each datagram when (or if) it is fragmented.static final intOption identifier for the End Of Options List option.static final intOption identifier for the loose source routing optionstatic final intOption identifier for the route record optionstatic final intOption identifer for the the strict source routing optionstatic final intThe option number maskstatic final intMinimize the delay when handling packets.static final intTOS delay mask as defined by RFC 791.static final intNormal packet handlingstatic final intCritical/ECP TOS.static final intFlash TOS.static final intFlash Override TOS.static final intImmediate TOS.static final intInternetworking Control TOS.static final intThe Type-Of-Service mask.static final intNetwork Critical TOS.static final intPriority TOS.static final intRoutine TOS.static final intHigh Reliability requested.static final intPacket reliablity mask.static final intNormal reliability requrestedstatic final intMask of the reseered bits.static final intHigh throughput requestedstatic final intTOS Throughput maskstatic final intNormal throughput requested -
Constructor Summary
ConstructorsConstructorDescriptionIPHeader()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.Constructs a new IP Header object that is identical to the passed IPHeader. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddOption(IPHeader.Option opt) Adds an option to the IP header.static StringaddressToString(byte[] buf) Converts the passed IPv4 address buffer to a dotted decimal IP address string.static StringaddressToString(int ipv4Addr) Converts the passed 32-bit IPv4 address to a dotted decimal IP address string.shortGets the 16-bit ones compliment checksum for the IP header.intReturns the dotted decimal string address of the destination IP address.booleangetFlag(int bit) Used to get an individual flag from the flags field.bytegetFlags()Used to get the 3-bit flags from the header.shortReturns the 13-bit fragment offset field from the IP header.intUsed to get the current length of the IP Header.shortUsed to retreive the 16-bit identity of the header.byte[]Retrieves the IP header options from the header.Returns a list of options that are associated with the IP header.intReturns the length of the IP packet, including the header, in bytes.byteGets the protocol for the IP datagram.intReturns the dotted decimal string address of the source IP address.bytegetTTL()Gets the 8-bit Time To Live (TTL) of the packet.byteRetreives the current TOS field from the header.booleangetTypeOfService(int bit) Use to test individual bits in the TOS fields.byteUsed to retreive the current version of the IP Header.voidsetChecksum(short sum) Sets the checksum for the IP header.voidsetDestinationAddress(int addr) Sets the IP headers destination address.voidsetFlags(byte flags) Sets the flags contained in the upper 3 bits of the short value for the fragmentation offset.voidsetFragmentOffset(short offset) Sets the fragmentation index for this packetvoidsetIdentity(short ident) Sets the identity of the IP headervoidsetOptionData(byte[] options) Sets the current option data for the header.voidsetPacketLength(short length) Sets the length for IP packet, including the header.voidsetProtocol(byte protocol) Sets the protocol for the IP header.voidsetSourceAddr(int addr) Sets the IP headers source address.voidsetTTL(byte ttl) Sets the time to live for the IP headervoidsetTypeOfService(byte tos) Sets the TOS flags for the IP header.intwriteBytes(byte[] data, int offset) Stores the IP header as an array of bytes into the passed data buffer.
-
Field Details
-
IP_VERSION
public static final int IP_VERSIONThe supported version of the IP header- See Also:
-
TOS_PRECEDENCE_MASK
public static final int TOS_PRECEDENCE_MASKThe 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_CRITICALNetwork Critical TOS. See RFC 791.- See Also:
-
TOS_PRECEDENCE_INTERNETWORK_CONTROL
public static final int TOS_PRECEDENCE_INTERNETWORK_CONTROLInternetworking Control TOS. See RFC 791.- See Also:
-
TOS_PRECEDENCE_CRITICAL_ECP
public static final int TOS_PRECEDENCE_CRITICAL_ECPCritical/ECP TOS. See RFC 791.- See Also:
-
TOS_PRECEDENCE_FLASH_OVERRIDE
public static final int TOS_PRECEDENCE_FLASH_OVERRIDEFlash Override TOS. See RFC 791.- See Also:
-
TOS_PRECEDENCE_FLASH
public static final int TOS_PRECEDENCE_FLASHFlash TOS. See RFC 791.- See Also:
-
TOS_PRECEDENCE_IMMEDIATE
public static final int TOS_PRECEDENCE_IMMEDIATEImmediate TOS. See RFC 791.- See Also:
-
TOS_PRECEDENCE_PRIORITY
public static final int TOS_PRECEDENCE_PRIORITYPriority TOS. See RFC 791.- See Also:
-
TOS_PRECEDENCE_ROUTINE
public static final int TOS_PRECEDENCE_ROUTINERoutine TOS. See RFC 791.- See Also:
-
TOS_DELAY_MASK
public static final int TOS_DELAY_MASKTOS delay mask as defined by RFC 791.- See Also:
-
TOS_DELAY_LOW
public static final int TOS_DELAY_LOWMinimize the delay when handling packets.- See Also:
-
TOS_DELAY_NORMAL
public static final int TOS_DELAY_NORMALNormal packet handling- See Also:
-
TOS_THROUGHPUT_MASK
public static final int TOS_THROUGHPUT_MASKTOS Throughput mask- See Also:
-
TOS_THROUGHPUT_HIGH
public static final int TOS_THROUGHPUT_HIGHHigh throughput requested- See Also:
-
TOS_THROUGHPUT_NORMAL
public static final int TOS_THROUGHPUT_NORMALNormal throughput requested- See Also:
-
TOS_RELIBILITY_MASK
public static final int TOS_RELIBILITY_MASKPacket reliablity mask.- See Also:
-
TOS_RELIBILITY_HIGH
public static final int TOS_RELIBILITY_HIGHHigh Reliability requested.- See Also:
-
TOS_RELIBILITY_NORMAL
public static final int TOS_RELIBILITY_NORMALNormal reliability requrested- See Also:
-
TOS_RESERVED_MASK
public static final int TOS_RESERVED_MASKMask of the reseered bits.- See Also:
-
FLAGS_MASK
public static final int FLAGS_MASKThe mask of the flags in the fragment field of the IP header- See Also:
-
FLAGS_DONT_FRAGMENT
public static final int FLAGS_DONT_FRAGMENTDon't fragment datagrams field- See Also:
-
FLAGS_MORE_FRAGMENTS
public static final int FLAGS_MORE_FRAGMENTSMore fragments are necessary to reassemble this packet- See Also:
-
OPTION_COPY_MASK
public static final int OPTION_COPY_MASKThe 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_MASKThe option class mask- See Also:
-
OPTION_NUMBER_MASK
public static final int OPTION_NUMBER_MASKThe option number mask- See Also:
-
OPTION_ID_EOO
public static final int OPTION_ID_EOOOption identifier for the End Of Options List option.- See Also:
-
OPTION_ID_LOOSE_SOURCE_ROUTING
public static final int OPTION_ID_LOOSE_SOURCE_ROUTINGOption identifier for the loose source routing option- See Also:
-
OPTION_ID_STRICT_SOURCE_ROUTING
public static final int OPTION_ID_STRICT_SOURCE_ROUTINGOption identifer for the the strict source routing option- See Also:
-
OPTION_ID_ROUTE_RECORD
public static final int OPTION_ID_ROUTE_RECORDOption 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
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 headeroffset- 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
Returns a list of options that are associated with the IP header.- Returns:
- The list of current options.
- Throws:
InstantiationException
-
addOption
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 dataoffset- The offset to start storing information.- Returns:
- The new offset just beyond the last written byte.
-
addressToString
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
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.
-