Savarese.Org

org.savarese.vserv.tcpip
Class UDPPacket

java.lang.Object
  extended byorg.savarese.vserv.tcpip.IPPacket
      extended byorg.savarese.vserv.tcpip.UDPPacket

public class UDPPacket
extends IPPacket

UDPPacket extends IPPacket to handle UDP packets. The UDP packet structure is described in RFC 768.

Author:
Daniel F. Savarese

Field Summary
static int LENGTH_UDP_HEADER
          Length of the UDP packet header in bytes.
static int OFFSET_DESTINATION_PORT
          Offset into the UDP packet of the destination port header value.
static int OFFSET_SOURCE_PORT
          Offset into the UDP packet of the source port header value.
static int OFFSET_UDP_CHECKSUM
          Offset into the UDP packet of the UDP checksum.
static int OFFSET_UDP_TOTAL_LENGTH
          Offset into the UDP packet of UDP total packet length header value.
 
Fields inherited from class org.savarese.vserv.tcpip.IPPacket
_data_, LENGTH_DESTINATION_ADDRESS, LENGTH_SOURCE_ADDRESS, OFFSET_DESTINATION_ADDRESS, OFFSET_FLAGS, OFFSET_IDENTIFICATION, OFFSET_IP_CHECKSUM, OFFSET_PROTOCOL, OFFSET_SOURCE_ADDRESS, OFFSET_TOTAL_LENGTH, OFFSET_TTL, OFFSET_TYPE_OF_SERVICE, PROTOCOL_ICMP, PROTOCOL_IP, PROTOCOL_TCP, PROTOCOL_UDP
 
Constructor Summary
UDPPacket(int size)
          Creates a new UDP packet of a given size.
UDPPacket(UDPPacket packet)
          Creates a new UDP packet that is a copy of a given packet.
 
Method Summary
 int computeUDPChecksum()
          Same as computeUDPChecksum(true);
 int computeUDPChecksum(boolean update)
          Computes the UDP checksum, optionally updating the UDP checksum header.
 void copyData(UDPPacket packet)
          Copies the contents of a UDPPacket.
 int getCombinedHeaderByteLength()
           
 int getDestinationPort()
           
 int getSourcePort()
           
 int getUDPChecksum()
           
 int getUDPDataByteLength()
           
 int getUDPPacketByteLength()
           
 int getUDPPacketLength()
           
 void setData(byte[] data)
          Sets the raw packet byte array.
 void setDestinationPort(int port)
          Sets the destination port.
 void setIPHeaderLength(int length)
          Sets the IP header length field.
 void setSourcePort(int port)
          Sets the source port.
 void setUDPDataByteLength(int length)
          Sets the length of the UDP data payload.
 void setUDPPacketLength(int length)
          Sets the UDP total length header field.
 
Methods inherited from class org.savarese.vserv.tcpip.IPPacket
_computeChecksum_, computeIPChecksum, computeIPChecksum, copy, getData, getDestination, getDestination, getDestinationAsInetAddress, getDestinationAsWord, getFragmentOffset, getIdentification, getIPChecksum, getIPFlags, getIPHeaderByteLength, getIPHeaderLength, getIPPacketLength, getIPVersion, getProtocol, getSource, getSource, getSourceAsInetAddress, getSourceAsWord, getTTL, getTypeOfService, setDestinationAsWord, setFragmentOffset, setIdentification, setIPFlags, setIPPacketLength, setIPVersion, setProtocol, setSourceAsWord, setTTL, setTypeOfService, size
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

OFFSET_SOURCE_PORT

public static final int OFFSET_SOURCE_PORT
Offset into the UDP packet of the source port header value.

See Also:
Constant Field Values

OFFSET_DESTINATION_PORT

public static final int OFFSET_DESTINATION_PORT
Offset into the UDP packet of the destination port header value.

See Also:
Constant Field Values

OFFSET_UDP_TOTAL_LENGTH

public static final int OFFSET_UDP_TOTAL_LENGTH
Offset into the UDP packet of UDP total packet length header value.

See Also:
Constant Field Values

OFFSET_UDP_CHECKSUM

public static final int OFFSET_UDP_CHECKSUM
Offset into the UDP packet of the UDP checksum.

See Also:
Constant Field Values

LENGTH_UDP_HEADER

public static final int LENGTH_UDP_HEADER
Length of the UDP packet header in bytes.

See Also:
Constant Field Values
Constructor Detail

UDPPacket

public UDPPacket(int size)
Creates a new UDP packet of a given size.

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

UDPPacket

public UDPPacket(UDPPacket packet)
Creates a new UDP packet that is a copy of a given packet.

Parameters:
packet - The packet to replicate.
Method Detail

copyData

public final void copyData(UDPPacket packet)
Copies the contents of a UDPPacket. If the current data array is of insufficient length to store the contents, a new array is allocated.

Parameters:
packet - The UDPPacket to copy.

setData

public void setData(byte[] data)
Description copied from class: IPPacket
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.

Overrides:
setData in class IPPacket
Parameters:
data - The raw packet byte array to wrap.

setSourcePort

public final void setSourcePort(int port)
Sets the source port.

Parameters:
port - The new source port.

setDestinationPort

public final void setDestinationPort(int port)
Sets the destination port.

Parameters:
port - The new destination port.

getSourcePort

public final int getSourcePort()
Returns:
The source port.

getDestinationPort

public final int getDestinationPort()
Returns:
The destination port.

setIPHeaderLength

public void setIPHeaderLength(int length)
Description copied from class: IPPacket
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.

Overrides:
setIPHeaderLength in class IPPacket
Parameters:
length - The length of the IP header in 32-bit words.

setUDPPacketLength

public void setUDPPacketLength(int length)
Sets the UDP total length header field.

Parameters:
length - The length of the UDP packet in bytes.

getUDPPacketLength

public final int getUDPPacketLength()
Returns:
The value of the UDP total length header field.

getUDPChecksum

public final int getUDPChecksum()
Returns:
The UDP checksum.

getUDPPacketByteLength

public final int getUDPPacketByteLength()
Returns:
The UDP packet length in bytes. This is the size of the IP packet minus the size of the IP header. Normally, you want this to equal the length stored in the UDP header (see getUDPPacketLength()).

getCombinedHeaderByteLength

public final int getCombinedHeaderByteLength()
Returns:
The IP header length plus the UDP header length in bytes.

setUDPDataByteLength

public final void setUDPDataByteLength(int length)
Sets the length of the UDP data payload.

Parameters:
length - The length of the UDP data payload in bytes.

getUDPDataByteLength

public final int getUDPDataByteLength()

computeUDPChecksum

public final int computeUDPChecksum(boolean update)
Computes the UDP checksum, optionally updating the UDP checksum header.

Parameters:
update - Specifies whether or not to update the UDP 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 UDP checksum.

computeUDPChecksum

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

Returns:
The computed UDP checksum value.

Savarese.Org

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