net.clackrouter.packets
Class IPPacket.Header

java.lang.Object
  extended by net.clackrouter.packets.IPPacket.Header
Enclosing class:
IPPacket

public class IPPacket.Header
extends java.lang.Object

Encapsulates IP packet header.


Field Summary
static int ADDR_LEN
           
static int CHECKSUM_LEN
           
static int FLAGS_AND_FRAG_OFFSET_LEN
           
static int FLAGS_BIT_LEN
           
static int FRAG_OFFSET_BIT_LEN
           
static int ID_LEN
           
static int IHL_BIT_LEN
           
static int PROTOCOL_LEN
           
static int TOS_LEN
           
static int TOTAL_LENGTH_LEN
           
static int TTL_LEN
           
static int VERSION_AND_IHL_LEN
           
static int VERSION_BIT_LEN
           
 
Constructor Summary
IPPacket.Header(java.nio.ByteBuffer headerBuffer)
          Constructs IP packet header.
IPPacket.Header(byte version, byte ihl, byte tos, short totalLength, short identification, short flags, short fragmentOffset, byte ttl, byte protocol, java.nio.ByteBuffer srcAddr, java.nio.ByteBuffer dstAddr)
          Constructs an IP header from given parameters.
 
Method Summary
 short calculateChecksum()
          Calculates IP header checksum.
 java.nio.ByteBuffer getByteBuffer()
          Returns a byte buffer containing the header.
 byte[] getBytes()
          Returns an array of bytes representing the header.
 short getChecksum()
          Returns the packet's checksum field value.
 java.nio.ByteBuffer getDestinationAddress()
          Returns the destination address in the IP header.
 int getLength()
          Returns the length of the header, in bytes.
 int getPacketLength()
           
 byte getProtocol()
          Returns the packet's Protocol field value.
 java.nio.ByteBuffer getSourceAddress()
          Returns the source address in the IP header.
 int getTTL()
          Returns the packet's Time-To-Live field value.
 byte getVIHL()
           
 void pack()
          Write all header fields to the internal buffer.
 void setChecksum()
          Sets the checksum to the value calculated by the method calculateChecksum.
 void setChecksum(short checksum)
          Sets the checksum to the supplied value.
 void setDestAddr(java.nio.ByteBuffer dst)
           
 void setSrcAddr(java.nio.ByteBuffer src)
           
 void setTTL(int newTTL)
          Sets the packet's Time-To-Live field value.
 java.lang.String toString()
          Represents the header in readable format, by field.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

VERSION_BIT_LEN

public static final int VERSION_BIT_LEN
See Also:
Constant Field Values

IHL_BIT_LEN

public static final int IHL_BIT_LEN
See Also:
Constant Field Values

FLAGS_BIT_LEN

public static final int FLAGS_BIT_LEN
See Also:
Constant Field Values

FRAG_OFFSET_BIT_LEN

public static final int FRAG_OFFSET_BIT_LEN
See Also:
Constant Field Values

VERSION_AND_IHL_LEN

public static final int VERSION_AND_IHL_LEN
See Also:
Constant Field Values

TOS_LEN

public static final int TOS_LEN
See Also:
Constant Field Values

TOTAL_LENGTH_LEN

public static final int TOTAL_LENGTH_LEN
See Also:
Constant Field Values

ID_LEN

public static final int ID_LEN
See Also:
Constant Field Values

FLAGS_AND_FRAG_OFFSET_LEN

public static final int FLAGS_AND_FRAG_OFFSET_LEN
See Also:
Constant Field Values

TTL_LEN

public static final int TTL_LEN
See Also:
Constant Field Values

PROTOCOL_LEN

public static final int PROTOCOL_LEN
See Also:
Constant Field Values

CHECKSUM_LEN

public static final int CHECKSUM_LEN
See Also:
Constant Field Values

ADDR_LEN

public static final int ADDR_LEN
See Also:
Constant Field Values
Constructor Detail

IPPacket.Header

public IPPacket.Header(java.nio.ByteBuffer headerBuffer)
Constructs IP packet header.

Parameters:
headerBuffer - Byte buffer containing the header of an IP packet

IPPacket.Header

public IPPacket.Header(byte version,
                       byte ihl,
                       byte tos,
                       short totalLength,
                       short identification,
                       short flags,
                       short fragmentOffset,
                       byte ttl,
                       byte protocol,
                       java.nio.ByteBuffer srcAddr,
                       java.nio.ByteBuffer dstAddr)
Constructs an IP header from given parameters. Checksum is omitted, to be inserted after all field in the header are filled.

Parameters:
version - IP version; only 4 least significant bits are used
ihl - Internet header length; only 4 least significant bits are used
tos - Type of service
totalLength - Total length of a packet, in bytes
identification - Identification
flags - Control flags; only 3 least significant bits are used
fragmentOffset - Fragmentation offset; only 13 least significant bits are used
ttl - Time-To-Live
protocol - Protocol
srcAddr - Source IP address
dstAddr - Destination IP address
Method Detail

pack

public void pack()
Write all header fields to the internal buffer.


setChecksum

public void setChecksum(short checksum)
Sets the checksum to the supplied value.

Parameters:
checksum - Checksum

setChecksum

public void setChecksum()
Sets the checksum to the value calculated by the method calculateChecksum.

See Also:
calculateChecksum()

getPacketLength

public int getPacketLength()

getByteBuffer

public java.nio.ByteBuffer getByteBuffer()
Returns a byte buffer containing the header.

Returns:
Byte buffer containing the header

getLength

public int getLength()
Returns the length of the header, in bytes.

Returns:
Length of the IP header

getBytes

public byte[] getBytes()
Returns an array of bytes representing the header.

Returns:
An array of bytes representing the header

getSourceAddress

public java.nio.ByteBuffer getSourceAddress()
Returns the source address in the IP header.

Returns:
Byte buffer containing the source IP address

getDestinationAddress

public java.nio.ByteBuffer getDestinationAddress()
Returns the destination address in the IP header.

Returns:
Byte buffer containing the destination IP address

calculateChecksum

public short calculateChecksum()
Calculates IP header checksum. The checksum field is the 16 bit one's complement of the one's complement sum of all 16 bit words in the header. For purposes of computing the checksum, the value of the checksum field is zero.

Returns:
Checksum

getChecksum

public short getChecksum()
Returns the packet's checksum field value.

Returns:
Checksum

getTTL

public int getTTL()
Returns the packet's Time-To-Live field value.

Returns:
Time-To-Live

setTTL

public void setTTL(int newTTL)
Sets the packet's Time-To-Live field value.

Parameters:
newTTL - Time-To-Live

setSrcAddr

public void setSrcAddr(java.nio.ByteBuffer src)

setDestAddr

public void setDestAddr(java.nio.ByteBuffer dst)

getProtocol

public byte getProtocol()
Returns the packet's Protocol field value.

Returns:
Protocol

toString

public java.lang.String toString()
Represents the header in readable format, by field.

Overrides:
toString in class java.lang.Object
Returns:
Header in readable format

getVIHL

public byte getVIHL()