SourceForge Jpcap

net.sourceforge.jpcap.net
Class EthernetPacket

java.lang.Object
  extended by net.sourceforge.jpcap.net.Packet
      extended by net.sourceforge.jpcap.net.EthernetPacket
All Implemented Interfaces:
Serializable, EthernetFields
Direct Known Subclasses:
ARPPacket, CDP.Packet, DTP.Packet, IP.Packet, ISL.Packet, STP.Packet

public class EthernetPacket
extends Packet
implements EthernetFields

An Ethernet packet.

Contains link-layer header and data payload encapsulated by an Ethernet packet.

IP and ARP protocols are supported as subclasses, as are various link-layer and SNAP subclasses.

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

Field Summary
protected  ByteBuffer _byteBuffer
          The entire packet data wrapped in a ByteBuffer.
protected  byte[] _bytes
          Deprecated. Do not use this field: it will be removed in a future release.
protected  int _ethOffset
          Offset from beginning of byte array where the data payload (e.g.
protected  Timeval _timeval
          Time the packet was captured off the wire.
 
Fields inherited from interface net.sourceforge.jpcap.net.EthernetFields
ETH_CODE_LEN, ETH_CODE_POS, ETH_DST_POS, ETH_HEADER_LEN, ETH_SRC_POS
 
Constructor Summary
EthernetPacket(int lLen, byte[] bytes)
          Construct a new ethernet packet.
EthernetPacket(int lLen, byte[] bytes, Timeval tv)
          Construct a new ethernet packet, including the capture time.
EthernetPacket(int lLen, ByteBuffer byteBuffer, Timeval tv)
          Construct a new ethernet packet, including the capture time.
 
Method Summary
 boolean equals(Object that)
          
 String getColor()
          Fetch ASCII escape sequence of the color associated with this packet type.
 byte[] getData()
          Fetch the data portion of the packet.
 String getDestinationHwAddress()
          Deprecated. Please use getDestinationMACAddress()
 MACAddress getDestinationMACAddress()
          Fetch the MAC address of the host where the packet originated from.
 int getEthernetCRC32()
          Return the Ethernet CRC-32.
 byte[] getEthernetData()
          Fetch the Ethernet data as a byte array.
 byte[] getEthernetHeader()
          Fetch the ethernet header as a byte array.
 int getEthernetHeaderLength()
          Fetch the Ethernet header length in bytes.
 int getEthernetProtocol()
          Fetch the Ethernet protocol.
 byte[] getHeader()
          Fetch header portion of the packet.
 int getHeaderLength()
          Return the length of the header.
 ByteBuffer getPacketData()
          Fetch the packet data as a ByteBuffer.
 int getPacketLength()
          Fetch the total length of the packet.
 int getProtocol()
          Return the protocol.
 String getSourceHwAddress()
          Deprecated. Please use getSourceMACAddress()
 MACAddress getSourceMACAddress()
          Fetch the MAC address of the host where the packet originated from.
 Timeval getTimeval()
          Fetch the timeval containing the time the packet arrived on the device where it was captured.
 int hashCode()
          
 void setChecksums()
          Set all the checksums in this packet prior to sending.
 void setDestinationMACAddress(MACAddress macAddress)
          Set the destination hardware address.
 void setEthernetProtocol(int protocol)
          Set the Ethernet protocol
 void setSourceMACAddress(MACAddress macAddress)
          Set the source hardware address.
 byte[] toByteArray()
          Convert this packet to a byte[] array.
 String toColoredString(boolean colored)
          Generate string with contents describing this packet.
 String toString()
          
 
Methods inherited from class net.sourceforge.jpcap.net.Packet
getChecksum, ipChecksum, isValidChecksum
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

_bytes

protected byte[] _bytes
Deprecated. Do not use this field: it will be removed in a future release.
The entire packet data.

See Also:
toByteArray()

_byteBuffer

protected transient ByteBuffer _byteBuffer
The entire packet data wrapped in a ByteBuffer.

Since:
2.0.0

_ethOffset

protected int _ethOffset
Offset from beginning of byte array where the data payload (e.g. IP packet) starts. The size of the Ethernet header.


_timeval

protected Timeval _timeval
Time the packet was captured off the wire.

Constructor Detail

EthernetPacket

public EthernetPacket(int lLen,
                      byte[] bytes)
Construct a new ethernet packet.

For the purpose of Jpcap, when the type of ethernet packet is recognized as a protocol for which a class exists network library, a more specific class like IPPacket or ARPPacket is instantiated.

Parameters:
lLen - link-layer header length
bytes - Packet data

EthernetPacket

public EthernetPacket(int lLen,
                      byte[] bytes,
                      Timeval tv)
Construct a new ethernet packet, including the capture time.

Parameters:
lLen - Length of link-layer data
bytes - Packet data bytes
tv - Timeval when captured, or null

EthernetPacket

public EthernetPacket(int lLen,
                      ByteBuffer byteBuffer,
                      Timeval tv)
Construct a new ethernet packet, including the capture time.

Parameters:
lLen - Length of link-layer data
byteBuffer - Packet data in a ByteBuffer
tv - Timeval when captured, or null
Method Detail

getPacketLength

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

Specified by:
getPacketLength in class Packet

toByteArray

public final 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().

Specified by:
toByteArray in class Packet
Returns:
packet data wrapped in a byte[].
See Also:
Packet.getPacketData()

getPacketData

public final 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().

Specified by:
getPacketData in class Packet
Returns:
packet data wrapped in a ByteBuffer.
See Also:
Packet.toByteArray()

getEthernetHeaderLength

public final int getEthernetHeaderLength()
Fetch the Ethernet header length in bytes.


getHeaderLength

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

Specified by:
getHeaderLength in class Packet
Returns:
the length of the header as defined by Packet.getHeader().
See Also:
Packet.getHeader()

getEthernetHeader

public final byte[] getEthernetHeader()
Fetch the ethernet header as a byte array.

Returns:
the Ethernet header

getHeader

public 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:

Specified by:
getHeader in class Packet

getEthernetData

public final byte[] getEthernetData()
Fetch the Ethernet data as a byte array.

Returns:
the Ethernet payload

getData

public 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:

Specified by:
getData in class Packet

getSourceHwAddress

public String getSourceHwAddress()
Deprecated. Please use getSourceMACAddress()

Fetch the MAC address of the host where the packet originated from.

Returns:
the source MAC address, in 00:11:22:33:44:55 format

getSourceMACAddress

public MACAddress getSourceMACAddress()
Fetch the MAC address of the host where the packet originated from.

Returns:
the source MAC address.
Since:
2.0.0

setSourceMACAddress

public void setSourceMACAddress(MACAddress macAddress)
Set the source hardware address.

Parameters:
macAddress - Source MAC address.
Since:
2.0.0

getDestinationHwAddress

public String getDestinationHwAddress()
Deprecated. Please use getDestinationMACAddress()

Fetch the MAC address of the host where the packet originated from.

Returns:
the destination MAC address, in 00:11:22:33:44:55 format

getDestinationMACAddress

public MACAddress getDestinationMACAddress()
Fetch the MAC address of the host where the packet originated from.

Returns:
the destination MAC address.
Since:
2.0.0

setDestinationMACAddress

public void setDestinationMACAddress(MACAddress macAddress)
Set the destination hardware address.

Parameters:
macAddress - Destination MAC address.
Since:
2.0.0

getEthernetProtocol

public final int getEthernetProtocol()
Fetch the Ethernet protocol.

Returns:
the Ethernet protocol word

setEthernetProtocol

public final void setEthernetProtocol(int protocol)
Set the Ethernet protocol

Parameters:
protocol - Protocol word
Since:
2.0.0

getProtocol

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

Specified by:
getProtocol in class Packet
Returns:
the protocol word.

getTimeval

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

Specified by:
getTimeval in class Packet
Returns:
the Timeval

getEthernetCRC32

public int getEthernetCRC32()
Return the Ethernet CRC-32. This is recomputed each time, as the actual Ethernet CRC is removed by the Ethernet layer before it gets into Jpcap, and it is recomputed and appended by the Ethernet layer when sending. For this reason no setEthernetCRC32() method is provided, and setChecksums() doesn't re-compute it.

Returns:
the CRC-32.

setChecksums

public 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.

After calling this method the hashCode() of this object will probably change.

Specified by:
setChecksums in class Packet

hashCode

public int hashCode()

This implementation returns the Ethernet CRC-32.

Overrides:
hashCode in class Object

equals

public boolean equals(Object that)

This implementation returns true iff the hashCodes, i.e. the CRC-32s, are equal.

Overrides:
equals in class Object

toString

public String toString()

Overrides:
toString in class Object

toColoredString

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

Specified by:
toColoredString in class Packet
Parameters:
colored - whether or not the string should contain ansi color escape sequences.

getColor

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

Specified by:
getColor in class Packet
Returns:
ASCII escape sequence string

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