Savarese.Org

org.savarese.vserv.tcpip
Class IPPacket

java.lang.Object
  extended byorg.savarese.vserv.tcpip.IPPacket
Direct Known Subclasses:
ICMPPacket, TCPPacket, UDPPacket

public class IPPacket
extends java.lang.Object

IPPacket wraps the raw bytes comprising an IPv4 packet and exposes its content via setter and getter methods. After you alter the header of an IP packet you have to recompute the checksum with computeIPChecksum(). The structure of IP packets is described in RFC 760.

Author:
Daniel F. Savarese

Field Summary
protected  byte[] _data_
          Raw packet data.
static int LENGTH_DESTINATION_ADDRESS
          Number of bytes in destination address.
static int LENGTH_SOURCE_ADDRESS
          Number of bytes in source address.
static int OFFSET_DESTINATION_ADDRESS
          Offset into byte array of destination address header value.
static int OFFSET_FLAGS
          Offset into byte array of the flags header value.
static int OFFSET_IDENTIFICATION
          Offset into byte array of the identification header value.
static int OFFSET_IP_CHECKSUM
          Offset into byte array of header checksum header value.
static int OFFSET_PROTOCOL
          Offset into byte array of protocol number header value.
static int OFFSET_SOURCE_ADDRESS
          Offset into byte array of source address header value.
static int OFFSET_TOTAL_LENGTH
          Offset into byte array of total packet length header value.
static int OFFSET_TTL
          Offset into byte array of time to live header value.
static int OFFSET_TYPE_OF_SERVICE
          Offset into byte array of the type of service header value.
static int PROTOCOL_ICMP
          Protocol constant for ICMP.
static int PROTOCOL_IP
          Protocol constant for IPv4.
static int PROTOCOL_TCP
          Protocol constant for TCP.
static int PROTOCOL_UDP
          Protocol constant for UDP.
 
Constructor Summary
IPPacket(int size)
          Creates a new IPPacket of a given size.
 
Method Summary
protected  int _computeChecksum_(int startOffset, int checksumOffset, int length, int virtualHeaderTotal, boolean update)
          Calculates checksums assuming the checksum is a 16-bit header field.
 int computeIPChecksum()
          Same as computeIPChecksum(true);
 int computeIPChecksum(boolean update)
          Computes the IP checksum, optionally updating the IP checksum header.
 void copy(IPPacket packet)
          Copies the contents of an IPPacket to the calling instance.
 void getData(byte[] data)
          Copies the raw packet data into a byte array.
 void getDestination(byte[] address)
          Retrieves the destionation IP address into a byte array.
 void getDestination(java.lang.StringBuffer buffer)
          Retrieves the destination IP address as a string into a StringBuffer.
 java.net.InetAddress getDestinationAsInetAddress()
           
 int getDestinationAsWord()
           
 int getFragmentOffset()
          Returns the fragment offset header value.
 int getIdentification()
          Returns the IP identification header value.
 int getIPChecksum()
           
 int getIPFlags()
          Returns the IP flags header value.
 int getIPHeaderByteLength()
           
 int getIPHeaderLength()
           
 int getIPPacketLength()
           
 int getIPVersion()
          Returns the IP version header value.
 int getProtocol()
           
 void getSource(byte[] address)
          Retrieves the source IP address into a byte array.
 void getSource(java.lang.StringBuffer buffer)
          Retrieves the source IP address as a string into a StringBuffer.
 java.net.InetAddress getSourceAsInetAddress()
           
 int getSourceAsWord()
           
 int getTTL()
           
 int getTypeOfService()
          Returns the IP type of service header value.
 void setData(byte[] data)
          Sets the raw packet byte array.
 void setDestinationAsWord(int dest)
          Sets the destination IP address using a word representation.
 void setFragmentOffset(int offset)
          Sets the fragment offset header value.
 void setIdentification(int id)
          Sets the IP identification header value.
 void setIPFlags(int flags)
          Sets the IP flags header value.
 void setIPHeaderLength(int length)
          Sets the IP header length field.
 void setIPPacketLength(int length)
          Sets the IP packet total length header value.
 void setIPVersion(int version)
          Sets the IP version header value.
 void setProtocol(int protocol)
          Sets the protocol number.
 void setSourceAsWord(int src)
          Sets the source IP address using a word representation.
 void setTTL(int ttl)
          Sets the time to live value in seconds.
 void setTypeOfService(int service)
          Sets the IP type of service header value.
 int size()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

OFFSET_TYPE_OF_SERVICE

public static final int OFFSET_TYPE_OF_SERVICE
Offset into byte array of the type of service header value.

See Also:
Constant Field Values

OFFSET_TOTAL_LENGTH

public static final int OFFSET_TOTAL_LENGTH
Offset into byte array of total packet length header value.

See Also:
Constant Field Values

OFFSET_IDENTIFICATION

public static final int OFFSET_IDENTIFICATION
Offset into byte array of the identification header value.

See Also:
Constant Field Values

OFFSET_FLAGS

public static final int OFFSET_FLAGS
Offset into byte array of the flags header value.

See Also:
Constant Field Values

OFFSET_SOURCE_ADDRESS

public static final int OFFSET_SOURCE_ADDRESS
Offset into byte array of source address header value.

See Also:
Constant Field Values

LENGTH_SOURCE_ADDRESS

public static final int LENGTH_SOURCE_ADDRESS
Number of bytes in source address.

See Also:
Constant Field Values

OFFSET_DESTINATION_ADDRESS

public static final int OFFSET_DESTINATION_ADDRESS
Offset into byte array of destination address header value.

See Also:
Constant Field Values

LENGTH_DESTINATION_ADDRESS

public static final int LENGTH_DESTINATION_ADDRESS
Number of bytes in destination address.

See Also:
Constant Field Values

OFFSET_TTL

public static final int OFFSET_TTL
Offset into byte array of time to live header value.

See Also:
Constant Field Values

OFFSET_PROTOCOL

public static final int OFFSET_PROTOCOL
Offset into byte array of protocol number header value.

See Also:
Constant Field Values

OFFSET_IP_CHECKSUM

public static final int OFFSET_IP_CHECKSUM
Offset into byte array of header checksum header value.

See Also:
Constant Field Values

PROTOCOL_IP

public static final int PROTOCOL_IP
Protocol constant for IPv4.

See Also:
Constant Field Values

PROTOCOL_ICMP

public static final int PROTOCOL_ICMP
Protocol constant for ICMP.

See Also:
Constant Field Values

PROTOCOL_TCP

public static final int PROTOCOL_TCP
Protocol constant for TCP.

See Also:
Constant Field Values

PROTOCOL_UDP

public static final int PROTOCOL_UDP
Protocol constant for UDP.

See Also:
Constant Field Values

_data_

protected byte[] _data_
Raw packet data.

Constructor Detail

IPPacket

public IPPacket(int size)
Creates a new IPPacket of a given size.

Parameters:
size - The number of bytes in the packet.
Method Detail

size

public int size()
Returns:
The size of the packet.

setData

public void setData(byte[] data)
Sets the raw packet byte array. Although this method would appear to violate object-oriented principles, it is necessary to implement efficient packet processing. You don't necessarily want to allocate a new IPPacket and data buffer every time a packet arrives and you need to be able to wrap packets from APIs that supply them as byte arrays.

Parameters:
data - The raw packet byte array to wrap.

getData

public void getData(byte[] data)
Copies the raw packet data into a byte array. If the array is too small to hold the data, the data is truncated.

Parameters:
data - The raw packet byte array to wrap.

copy

public final void copy(IPPacket packet)
Copies the contents of an IPPacket to the calling instance. If the two packets are of different lengths, a new byte array is allocated equal to the length of the packet parameter.

Parameters:
packet - The packet to copy from.

setIPVersion

public final void setIPVersion(int version)
Sets the IP version header value.

Parameters:
version - A 4-bit unsigned integer.

getIPVersion

public final int getIPVersion()
Returns the IP version header value.

Returns:
The IP version header value.

setIPHeaderLength

public void setIPHeaderLength(int length)
Sets the IP header length field. At most, this can be a four-bit value. The high order bits beyond the fourth bit will be ignored.

Parameters:
length - The length of the IP header in 32-bit words.

getIPHeaderLength

public final int getIPHeaderLength()
Returns:
The length of the IP header in 32-bit words.

getIPHeaderByteLength

public final int getIPHeaderByteLength()
Returns:
The length of the IP header in bytes.

setTypeOfService

public final void setTypeOfService(int service)
Sets the IP type of service header value. You have to set the individual service bits yourself. Convenience methods for setting the service bit fields directly may be added in a future version.

Parameters:
service - An 8-bit unsigned integer.

getTypeOfService

public final int getTypeOfService()
Returns the IP type of service header value.

Returns:
The IP type of service header value.

setIPPacketLength

public final void setIPPacketLength(int length)
Sets the IP packet total length header value.

Parameters:
length - The total IP packet length in bytes.

getIPPacketLength

public final int getIPPacketLength()
Returns:
The IP packet total length header value.

setIdentification

public void setIdentification(int id)
Sets the IP identification header value.

Parameters:
id - A 16-bit unsigned integer.

getIdentification

public final int getIdentification()
Returns the IP identification header value.

Returns:
The IP identification header value.

setIPFlags

public final void setIPFlags(int flags)
Sets the IP flags header value. You have to set the individual flag bits yourself. Convenience methods for setting the flag bit fields directly may be added in a future version.

Parameters:
flags - A 3-bit unsigned integer.

getIPFlags

public final int getIPFlags()
Returns the IP flags header value.

Returns:
The IP flags header value.

setFragmentOffset

public void setFragmentOffset(int offset)
Sets the fragment offset header value. The offset specifies a number of octets (i.e., bytes).

Parameters:
offset - A 13-bit unsigned integer.

getFragmentOffset

public final int getFragmentOffset()
Returns the fragment offset header value.

Returns:
The fragment offset header value.

setProtocol

public final void setProtocol(int protocol)
Sets the protocol number.

Parameters:
protocol - The protocol number.

getProtocol

public final int getProtocol()
Returns:
The protocol number.

setTTL

public final void setTTL(int ttl)
Sets the time to live value in seconds.

Parameters:
ttl - The time to live value in seconds.

getTTL

public final int getTTL()
Returns:
The time to live value in seconds.

_computeChecksum_

protected int _computeChecksum_(int startOffset,
                                int checksumOffset,
                                int length,
                                int virtualHeaderTotal,
                                boolean update)
Calculates checksums assuming the checksum is a 16-bit header field. This method is generalized to work for IP, ICMP, UDP, and TCP packets given the proper parameters.


computeIPChecksum

public final int computeIPChecksum(boolean update)
Computes the IP checksum, optionally updating the IP checksum header.

Parameters:
update - Specifies whether or not to update the IP checksum header after computing the checksum. A value of true indicates the header should be updated, a value of false indicates it should not be updated.
Returns:
The computed IP checksum.

computeIPChecksum

public final int computeIPChecksum()
Same as computeIPChecksum(true);

Returns:
The computed IP checksum value.

getIPChecksum

public final int getIPChecksum()
Returns:
The IP checksum header value.

getSource

public final void getSource(byte[] address)
Retrieves the source IP address into a byte array. The array should be LENGTH_SOURCE_ADDRESS bytes long.

Parameters:
address - The array in which to store the address.

getDestination

public final void getDestination(byte[] address)
Retrieves the destionation IP address into a byte array. The array should be LENGTH_DESTINATION_ADDRESS bytes long.

Parameters:
address - The array in which to store the address.

getSource

public final void getSource(java.lang.StringBuffer buffer)
Retrieves the source IP address as a string into a StringBuffer.

Parameters:
buffer - The StringBuffer in which to store the address.

getDestination

public final void getDestination(java.lang.StringBuffer buffer)
Retrieves the destination IP address as a string into a StringBuffer.

Parameters:
buffer - The StringBuffer in which to store the address.

setSourceAsWord

public final void setSourceAsWord(int src)
Sets the source IP address using a word representation.

Parameters:
src - The source IP address as a 32-bit word.

setDestinationAsWord

public final void setDestinationAsWord(int dest)
Sets the destination IP address using a word representation.

Parameters:
dest - The source IP address as a 32-bit word.

getSourceAsWord

public final int getSourceAsWord()
Returns:
The source IP address as a 32-bit word.

getDestinationAsWord

public final int getDestinationAsWord()
Returns:
The destination IP address as a 32-bit word.

getSourceAsInetAddress

public final java.net.InetAddress getSourceAsInetAddress()
                                                  throws java.net.UnknownHostException
Returns:
The source IP address as a java.net.InetAddress instance.
Throws:
java.net.UnknownHostException

getDestinationAsInetAddress

public final java.net.InetAddress getDestinationAsInetAddress()
                                                       throws java.net.UnknownHostException
Returns:
The destination IP address as a java.net.InetAddress instance.
Throws:
java.net.UnknownHostException

Savarese.Org

Copyright © 2004-2005 Daniel F. Savarese. All Rights Reserved.