SourceForge Jpcap

net.sourceforge.jpcap.net
Class Packet

java.lang.Object
  extended by net.sourceforge.jpcap.net.Packet
All Implemented Interfaces:
Serializable
Direct Known Subclasses:
EthernetPacket

public abstract class Packet
extends Object
implements Serializable

A network packet.

This class currently contains no implementation because only Ethernet is supported. In other words, all instances of packets returned by packet factory will always be at least as specific as EthernetPacket.

On large Ethernet networks, I sometimes see packets which don't have link-level ethernet headers. If and when I figure out what these are, maybe this class will be the root node of a packet hierarchy derived from something other than Ethernet.

Version:
$Revision: 1.1 $
Author:
Patrick Charles, Jonas Lehmann, Esmond Pitt
See Also:
Serialized Form
Last modified by:
$Author: esmondpitt $
Last modified at:
$Date: 2008/08/21 02:58:27 $

Constructor Summary
Packet()
           
 
Method Summary
 int getChecksum()
          Return the actual packet checksum as received.
abstract  String getColor()
          Fetch ASCII escape sequence of the color associated with this packet type.
abstract  byte[] getData()
          Fetch the data portion of the packet.
abstract  byte[] getHeader()
          Fetch header portion of the packet.
abstract  int getHeaderLength()
          Return the length of the header.
abstract  ByteBuffer getPacketData()
          Fetch the packet data as a ByteBuffer.
abstract  int getPacketLength()
          Fetch the total length of the packet.
abstract  int getProtocol()
          Return the protocol.
abstract  Timeval getTimeval()
          Fetch the timeval containing the time the packet arrived on the device where it was captured.
protected  int ipChecksum(byte[] bytes, int start, int len)
          Compute an IP checksum.
 boolean isValidChecksum()
          Return true iff all the checksums in the packet are valid, otherwise false.
abstract  void setChecksums()
          Set all the checksums in this packet prior to sending.
abstract  byte[] toByteArray()
          Convert this packet to a byte[] array.
abstract  String toColoredString(boolean colored)
          Generate string with contents describing this packet.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Packet

public Packet()
Method Detail

getPacketLength

public abstract int getPacketLength()
Fetch the total length of the packet.


toByteArray

public abstract byte[] toByteArray()
Convert this packet to a byte[] array. The array is constructed lazily from the underlying ByteBuffer where the real packet data is captured and stored. As that ByteBuffer is a 'direct' ByteBuffer, the toByteArray() operation requires copying data across the Java/JNI boundary, which is expensive. The array returned is a copy, so altering it is futile unless you construct a new packet with it. For a much more efficient read/write interface, see getPacketData().

Returns:
packet data wrapped in a byte[].
See Also:
getPacketData()

getPacketData

public abstract ByteBuffer getPacketData()
Fetch the packet data as a ByteBuffer. This will be a 'direct' ByteBuffer, that probably doesn't support ByteBuffer.array(), so its ByteBuffer.hasArray() method will probably return false. This is the form in which the packet is actually captured under the hood, and processed internally, so it is the most efficient way to look at or alter packet data, using all the methods of ByteBuffer. For a simpler but much less efficient interface, see toByteArray().

Returns:
packet data wrapped in a ByteBuffer.
See Also:
toByteArray()

getHeader

public abstract byte[] getHeader()
Fetch header portion of the packet. This is overridden in derived classes to return the header appropriate to the packet class. For example:


getHeaderLength

public abstract int getHeaderLength()
Return the length of the header.

Returns:
the length of the header as defined by getHeader().
See Also:
getHeader()

getData

public abstract byte[] getData()
Fetch the data portion of the packet. This is overridden in derived classes to return successively smaller and smaller amounts of data. For example:


getProtocol

public abstract int getProtocol()
Return the protocol. This is overridden by derived classes: for example:

Returns:
the protocol word.

getColor

public abstract String getColor()
Fetch ASCII escape sequence of the color associated with this packet type.

Returns:
ASCII escape sequence string

getTimeval

public abstract Timeval getTimeval()
Fetch the timeval containing the time the packet arrived on the device where it was captured.

Returns:
the Timeval

isValidChecksum

public boolean isValidChecksum()
Return true iff all the checksums in the packet are valid, otherwise false. Derived classes with checksums should override, and should call super.isValidChecksum() to check the validity of base class checksums. The isValidChecksum() implementation just returns true.

Returns:
true iff all the checksums in the packet are valid.
Since:
2.0.0

getChecksum

public int getChecksum()
Return the actual packet checksum as received. Derived classes override this to return the most specific checksum, e.g.: The implementation in getChecksum() returns zero.

Since:
2.0.0

setChecksums

public abstract void setChecksums()
Set all the checksums in this packet prior to sending. Calling this method sets all the IP, ICMP, IGMP, TCP, UDP etc checksums as appropriate. It is called automatically by PacketOutputStream.writePacket(Packet). Derived classes must override this abstract method, and must call super.setChecksums(), after setting their own checksums (unless super.setChecksums() is abstract, i.e. this method). Otherwise the lower-level checksums will not be correct.

Since:
2.0.0

ipChecksum

protected int ipChecksum(byte[] bytes,
                         int start,
                         int len)
Compute an IP checksum. Uses the calculation from RFC 1071, corrected to handle a final odd byte correctly. This is (a) from a reliable source, and (b) significantly faster than the old implementation in IPPacket.onesCompSum(byte[], int, int). This is part of the fix for issue 1352176.

Parameters:
bytes - data
start - Initial offset
len - Total number of bytes to process
Returns:
the ones-complement of the ones-complement sum of the data, treated as big-endian 16-bit words, with any final odd byte causing an implicit padding with a byte of zero.
Since:
2.0.0

toColoredString

public abstract String toColoredString(boolean colored)
Generate string with contents describing this packet.

Parameters:
colored - whether or not the string should contain ansi color escape sequences.

Copyright © Patrick Charles & Jonas Lehmann, 2001; Esmond Pitt, 2008.