org.jnetpcap.packet
Class PcapPacket

java.lang.Object
  extended by org.jnetpcap.nio.JMemory
      extended by org.jnetpcap.nio.JBuffer
          extended by org.jnetpcap.packet.JPacket
              extended by org.jnetpcap.packet.PcapPacket
All Implemented Interfaces:
JHeaderAccessor

public class PcapPacket
extends JPacket

A pcap packet. Fully decoded packet that provides access to protocol headers as determined during the decoding process. A PcapPacket class is designed to work with pcap library. It can not be used to create a new packet from an external memory buffer that only contains packet data, such as preparing a packet to be sent from a network interface. You can use JMemoryPacket to create an in memory packet from scratch. PcapPackets need a PcapHeader which is provided by libpcap at the time the packet was captured. Also the PcapPacket contains decoded state information which can be used to query the packet for its contents using friendly java API and compile-time type-safety.

Packet accessors

Once a decoded packet is received, the user can query the packet for its various properties. The most important of which is the existance of any particular protocol header within the packet data buffer. The data buffer is scanned and decoded. Any discovery of a protocol header within, is recorded in packet's state. The following accessors can be used to query if a particular header has been found within a packet:

Here is an example of how to use an accessor form a PcapPacketHandler:

 public void nextPacket(PcapPacket packet, Object user) {
        if (packet.hasHeader(Ethernet.ID)) {
                Ethernet eth = packet.getHeader(new Ethernet());
 
                System.out.printf("ethernet.type=%X\n", eth.type());
        }
 }
 
Or more conveniently, combine hasHeader and getHeader in a single call
 private Ethernet eth = new Ethernet(); // Preallocate our ethernet header
 
 private Ip4 ip = new Ip4(); // Preallocat IP version 4 header
 
 public void nextPacket(PcapPacket packet, Object user) {
        if (packet.hasHeader(eth)) {
                System.out.printf("ethernet.type=%X\n", eth.type());
        }
 
        if (packet.hasHeader(ip)) {
                System.out.println("ip.version=%d\n", ip.version());
        }
 }
 

Accessing a subheader such as ip options

You can also access sub headers, usually supplied as options by the protocol during transmission.
 private Ip4 ip = new Ip4(); // Preallocat IP version 4 header
 private Ip4.Timestamp timestamp = new Ip4.Timestamp(); // Optional header
 
 public void nextPacket(PcapPacket packet, Object user) {
  if (packet.hasHeader(ip) && ip.hasSubHeader(timestamp)) {
    System.out.println("ip.version=%d\n", ip.version);
    System.out.println("timestamp optional header length=%d\n", timstamp.length());
  }
 
A couple of points about the sub header example. Notice that we preallocated a Timestamp header, which is defined from within Ip4 class itself, but is a separate class on its own none the less. Next we first check if Ip4 header is present at all in the packet, peer it if exists (combined hasHeader and getHeader accessor method) and as a second step we check with the Ip4 header if it has an optional header using ip.hasSubHeader(timestamp). If the method returns true, it also peers the sub header timestamp with the appropriate packet data buffer where the optional header resides.

Formatting packet for output

A packet can easily be formatted for textual output. Any supported formatter, such as TextFormatter or XmlFormatter can be used to format a packet for output. Also JPacket.toString() method uses an internal StringBuilder based TextFormatter that formats the packet for textual output in a string buffer. At this time both ip and timestamp header instances are properly intialized and can be used to access their respective headers.
 JPacket packet = // From out handler
 TextFormatter out = new TextFormatter(System.out);
 
 out.format(packet); // Send pretty output to stdout
 
 // Or save time
 System.out.println(packet.toString()); // Use internal TextFormatter
 

Packet's lifecycle

A PcapPacket is made up of 3 parts:

Each part of the packet is managed independently, that is either part can be initialized or not. Either part can point to any memory location, including a large single buffer of contigues bytes that contains all 3 parts, header, state and packet data. There are various methods supplied by PcapPacket that allow an external buffer to be peered with all 3 parts of the packet. There are also many methods for transfering (deep copy) the data to and from buffers.

All of these components are stored in native memory in native C structures that are peered with the packet API classes. The classes, managed by JMemory class are referencing native memory locations. Any native method that is called upon in the PcapPacket class or its base classes, will perform those operations on the peered structure and data.

When a packet is delivered from either Pcap.loop or Pcap.dispatch methods, the capture header, packet state and packet data all point to different unrelated memory locations. That is, capture header is peered with the libpcap supplied pcap_pkthdr structure, Packet data buffer (the packet itself) is peered with the data buffer supplied by pcap and the packet state is peered with its packet_state_t structure as supplied by the JScanner, typically out of its internal buffer. None of these default memory locations are persistent for very long time. Both libpcap and JScanner buffers are round robin buffers that eventually wrap around and reuse previously dispatched memory.

These temporary packets are only suitable for immediate use. That is if the packets are processed immediately when received and then discarded, they do not need to be preserved. If a packet is to be put on a queue and for later processing, the packet needs to preserve its state. That requires a physical copy of all 3 components of the packet to a new memory location. The most efficient way to store the new packet is to allocate a memory buffer large enough to hold all of the packets state and data out of a JMemoryPool. The JPacket provides a default singleton memory pool out of which all packets allocate memory out of for the required space.

Advanced topiccs

Below are several sections that describe the lifecycle of a packet in more depth. For simply usage, the termporary packets can be used immediately in the handler and then the packets can be discarded. For more advanced usage lets go into the detail of how packet data can be copied, preserved and peered to one another.

Perserving packet's state and data

In order to preserve packet's state and data a deep copy needs to be performed of all 3 components of he packet. PcapPacket class provides several PcapPacket.transferTo methods that perform deep copies of the packet. For efficiency reasons, each transferTo method are designed to copy data into a memory buffer of larger size. The packet state and data are copied to the buffer with the following layout within the buffer:
 +----------+-----+----+
 |PcapHeader|State|Data|
 +----------+-----+----+
 

The buffer to which this copy takes place can be an external buffer or an internally allocated one by the packet class itself. As stated before, packet's use an interal singleton memory pool to allocate memory out of more efficiently. This memory allocates large native memory blocks which are then sub divided further and given out by the memory pool on a per request basis. All the copies are done natively by low level native copy routines, not in java space for maximum performace.

The easiest way to copy packet contents as received, for example, from PcapPacketHandler, is to pass the temporary packet to the PcapPacket constructor which will automatically allocate new space for the packet state and data and perform a deep copy. The new packet immediately becomes usable and is permanently stored in memory with its state and data, until garbage collected. Here is an example of a PcapPacketHandler that copies the temporary packet to new permanent one:

 pulic void nextPacket(PcapPacket packet, Queue<PcapPacket> queue) {
   PcapPacket permanent = new PcapPacket(packet);  
   queue.offer(permanent); 
 }
 

Alternative is to reused another packet and transfer the temporary packets state and data to it or create a new unitiatialized packet with new PcapPacket(JMemory.Type.POINTER) constructor and subsequently perform PcapPacket.transferTo(PcapPacket) call to copy the contents. In the first case where an existing packet is being reused, if that packet already contains a large enough memory buffer to hold the state and data of the temporary packet, that buffer is reused. Otherwise a new buffer is allocated out of the default memory pool. Here is an exmaple: *

 
 final PcapPacket permanent = new PcapPacket(Type.POINTER);
 
 pulic void nextPacket(PcapPacket packet, Queue<PcapPacket> queue) {
   permanent.transferStateAndData(packet); 
   // Or
   packet.transferTo(permanent);
 }
 
In either case, any existing buffer previously allocated in the permanent packet if its big enough to hold the state and data of the packet, is reused, saving time on memory allocation. You can also manually allocate a large buffer and reuse a packet:
 
 final PcapPacket permanent = new PcapPacket(64 * 1024); // Preallocate 64K
 
 pulic void nextPacket(PcapPacket packet, Queue<PcapPacket> queue) {
   permanent.transferStateAndData(packet); 
   // Or
   packet.transferTo(permanent);
 }
 
In this example, the packet buffer will always be large enough and resused. But still this is a semi permanentn state.

Yet another alternative is to store the contents of the packet in an external buffer such as ByteBuffer, JBuffer or simply a byte[] and then at an appropriate time, transfer the data back or peer the external buffer with a packet object. Only the byte[] buffer type and ByteBuffer backed by a byte array, can not be peered directly with a packet as only buffer sources that are native memory based can be peered. All external buffer types can be copied back into a packet, if peering is not required. New memory space is allocated for the copy. Here is an example:

 pulic void nextPacket(PcapPacket packet, Queue<PcapPacket> queue) {
   JBuffer jbuf = new JBuffer(packet.getTotalSize());
   packet.transferTo(jbuf);
   // Or
   ByteBuffer bbuf = ByteBuffer.allocateDirect(packet.getTotalSize());
   packet.transferTo(bbuf);
   // Or
   byte[] babuf = new byte[packet.getTotalSize())];
   packet.transferTo(babuf);
 }
 
In all 3 cases, complete the packet's state and data buffer are copied to external buffer.

Initializing packet from an external buffer

Packet state and data can be preseved in an external buffer large enough to hold the entire packet with its state. PcapPacket class provides transferStateAndData and peer methods that allow the external packet data to be either copied into a packet or the packet be peered directly with the external buffer. Peering does not need to allocate memory to hold the packet state, but its state and data are directly read out of the extern buffer. If you change the contents of the external buffer, the packet's state and data will change as well. Care must be take with a direct reference to an external buffer, as its easy to override sensitive data causing the packet to behave wildly and unexpectidly. JMemory class prevents buffer overrun attacks and any access to memory that has not been allocated. a direct reference. Here is an example:
 pulic void nextPacket(PcapPacket packet, Queue<PcapPacket> queue) {
   JBuffer jbuf = new JBuffer(packet.getTotalSize());
   packet.transferTo(jbuf);
   // Or
   ByteBuffer bbuf = ByteBuffer.allocateDirect(packet.getTotalSize());
   packet.transferTo(bbuf);
   // Or
   byte[] babuf = new byte[packet.getTotalSize())];
   packet.transferTo(babuf);
   
   PcapPacket p1 = new PcapPacket(jbuf); // Deep copy
   
   PcapPacket p2 = new PcapPacket(Type.POINTER); // Uninitialized
   bbuf.flip(); // Have to flip the buffer to access the just written contents
   p2.peer(bbuf); // No copies, peered directly with external buffer
   
   PcapPacket p3 = new PcapPacket(Type.POINTER); // Uninitialized
   p3.transferStateAndData(babuf); // Deep copy - byte[] buffers can not be peered
   
   PcapPacket p4 = new PcapPacket(Type.POINTER); // Uninitialized
   p4.peer(p3); // both point at same internal memory space
 }
 
The above example demonstrates 3 different ways that data from an external buffer can be either copied or peered with a new packet object. In all cases the data and state were transfered from the temporary packet received by the handler to a more permenant buffer and then packet. An interesting scenerio occures with packet p4. Lets take a closer look.

First, p3 is created unitialized, meaning that packet header, state and data are null pointers at this time, they don't point to anything and any accessor method used will immediately throw a NullPointerException. Second, the byte[] external buffer is copied into newly allocate memory space by p3. The packet is intiailized to pointer at its internal buffer for the header, state and packet data. Then we create p4, also unitialized and in the following step we peer p4 to p3. That is p4 points at the exact same memory location for packet's header, state and data. No new memory was allocated and changing the contents in either packet, p3 or p4, will have immediate effect on the other packet. Another words, both p3 and p4 are peered to the same internal memory space.

Author:
Mark Bednarczyk, Sly Technologies, Inc.
See Also:
JMemoryPool

Nested Class Summary
 
Nested classes/interfaces inherited from class org.jnetpcap.packet.JPacket
JPacket.State
 
Nested classes/interfaces inherited from class org.jnetpcap.nio.JMemory
JMemory.Type
 
Field Summary
 
Fields inherited from class org.jnetpcap.packet.JPacket
DEFAULT_STATE_HEADER_COUNT, defaultScanner, memory, pool, state
 
Fields inherited from class org.jnetpcap.nio.JMemory
JNETPCAP_LIBRARY_NAME, MAX_DIRECT_MEMORY_DEFAULT, POINTER
 
Constructor Summary
PcapPacket(byte[] buffer)
          Copies contents of the buffer to new packet.
PcapPacket(java.nio.ByteBuffer buffer)
          Copies contents of the buffer to new packet.
PcapPacket(int size)
          Allocates a memory buffer large enough to hold atleast size bytes of data and the decoded packet state.
PcapPacket(int size, int headerCount)
          Allocates memory for packet data and certain amount of state and headers.
PcapPacket(JBuffer buffer)
          Copies contents of the buffer to new packet.
PcapPacket(JMemory.Type type)
          Special type of instantiation that allows an empty packet to be peered, or in C terms its a packet pointer with no actual memory allocated.
PcapPacket(JPacket src)
          Does a deep copy of the source packet into newly allocated native memory location.
PcapPacket(PcapHeader header, java.nio.ByteBuffer buffer)
          Allocates memory for new packet and copies both the header and packet buffer to newly allocated memory.
PcapPacket(PcapHeader header, JBuffer buffer)
          Allocates memory for new packet and copies both the header and packet buffer to newly allocated memory.
PcapPacket(PcapPacket src)
          Does a deep copy of the source packet into newly allocated native memory location.
 
Method Summary
 PcapHeader getCaptureHeader()
          Retrieves the PcapHeader, capture header provided by libpcap.
 int getTotalSize()
          Gets the total size of the packet including pcap header, decoded state and data buffer.
 int peer(PcapHeader header, JBuffer buffer)
          Peer.
 int peerAndScan(int dlt, PcapHeader header, JBuffer buffer)
          Peer and scan.
 int peerHeaderAndData(JBuffer buffer)
          Peers both header and data to buffer.
 int peerHeaderAndData(PcapHeader header, java.nio.ByteBuffer buffer)
          Peer header and data.
 int peerHeaderAndData(PcapHeader header, JBuffer buffer)
          Peer header and data.
 int peerStateAndData(java.nio.ByteBuffer buffer)
          Peers the contents of the buffer directly with this packet.
 int peerStateAndData(JBuffer buffer)
          Peers the contents of the buffer directly with this packet.
 int transferHeaderAndDataFrom(PcapHeader header, java.nio.ByteBuffer buffer)
          Copies contents of header and packet buffer to a single newly allocated buffer.
 int transferHeaderAndDataFrom(PcapHeader header, JBuffer buffer)
          Copies contents of header and packet buffer to a single newly allocated buffer.
 int transferStateAndDataFrom(byte[] buffer)
          Copies contents of the buffer to new packet.
 int transferStateAndDataFrom(java.nio.ByteBuffer buffer)
          Copies contents of the buffer to new packet.
 int transferStateAndDataFrom(JBuffer buffer)
          Copies contents of the buffer to new packet.
 int transferStateAndDataFrom(PcapPacket packet)
          Deep copy of the supplied packet to this packet.
 int transferStateAndDataTo(byte[] buffer)
          Copies contents of this packet to buffer.
 int transferStateAndDataTo(java.nio.ByteBuffer buffer)
          Copies contents of this packet to buffer.
 int transferStateAndDataTo(JBuffer buffer)
          Copies contents of this packet to buffer.
 int transferStateAndDataTo(JBuffer buffer, int offset)
          Copies contents of this packet to buffer.
 int transferStateAndDataTo(PcapPacket packet)
          Deep copy of the this packet to the supplied packet.
 
Methods inherited from class org.jnetpcap.packet.JPacket
allocate, getAllocatedMemorySize, getDefaultScanner, getFormatter, getFrameNumber, getHeader, getHeader, getHeaderByIndex, getHeaderCount, getHeaderIdByIndex, getHeaderInstanceCount, getMemoryBuffer, getMemoryBuffer, getMemoryBuffer, getMemoryBuffer, getMemoryPool, getPacketWirelen, getScanner, getState, hasHeader, hasHeader, hasHeader, hasHeader, remaining, remaining, scan, setFormatter, setMemoryPool, shutdown, toHexdump, toString
 
Methods inherited from class org.jnetpcap.nio.JBuffer
findUTF8String, getByte, getByteArray, getByteArray, getByteArray, getDouble, getFloat, getInt, getLong, getShort, getUByte, getUInt, getUShort, getUTF8Char, getUTF8String, getUTF8String, getUTF8String, getUTF8String, isReadonly, order, order, peer, peer, peer, peer, setByte, setByteArray, setByteBuffer, setDouble, setFloat, setInt, setLong, setShort, setShort0, setUByte, setUInt, setUShort, transferFrom, transferFrom, transferFrom, transferTo, transferTo, transferTo
 
Methods inherited from class org.jnetpcap.nio.JMemory
availableDirectMemory, check, cleanup, createReference, isInitialized, isJMemoryBasedOwner, isOwner, maxDirectMemory, peer, reservedDirectMemory, setSize, size, softDirectMemory, toDebugString, toHexdump, totalActiveAllocated, totalAllocateCalls, totalAllocated, totalAllocatedSegments0To255Bytes, totalAllocatedSegments256OrAbove, totalDeAllocateCalls, totalDeAllocated, transferFrom, transferFrom, transferFromDirect, transferOwnership, transferTo, transferTo, transferTo, transferTo, transferTo, transferTo0
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

PcapPacket

public PcapPacket(byte[] buffer)
Copies contents of the buffer to new packet. All of the contents of the buffer are deep copied to new packet. The new packet allocates new memory for the packet contents, state and header if existing memory buffer is not large enough. Otherwise the existing memory buffer is overriden and reused. Existing buffers are not cleared before hand and may contain old data outside of the new header, state and packet data areas that are being overriden.

Supplied buffer layout expected:

 +----------+-----+----+
 |PcapHeader|State|Data|
 +----------+-----+----+
 

Parameters:
buffer - buffer containing capture header, packet state and data buffer sequentially in the buffer

PcapPacket

public PcapPacket(java.nio.ByteBuffer buffer)
Copies contents of the buffer to new packet. All of the contents of the buffer are deep copied to new packet. The new packet allocates new memory for the packet contents, state and header if existing memory buffer is not large enough. Otherwise the existing memory buffer is overriden and reused. Existing buffers are not cleared before hand and may contain old data outside of the new header, state and packet data areas that are being overriden.

Supplied buffer layout expected:

 +----------+-----+----+
 |PcapHeader|State|Data|
 +----------+-----+----+
 

Parameters:
buffer - buffer containing capture header, packet state and data buffer sequentially in the buffer

PcapPacket

public PcapPacket(int size)
Allocates a memory buffer large enough to hold atleast size bytes of data and the decoded packet state. The size of the the state structure is estimated to contain maximum of DEFAULT_STATE_HEADER_COUNT headers.

Parameters:
size - amount of memory to allocate to hold packet data

PcapPacket

public PcapPacket(int size,
                  int headerCount)
Allocates memory for packet data and certain amount of state and headers.

Parameters:
size - number of bytes for packet data
headerCount - maximum number of header to allocate space for

PcapPacket

public PcapPacket(JBuffer buffer)
Copies contents of the buffer to new packet. All of the contents of the buffer are deep copied to new packet. The new packet allocates new memory for the packet contents, state and header if existing memory buffer is not large enough. Otherwise the existing memory buffer is overriden and reused. Existing buffers are not cleared before hand and may contain old data outside of the new header, state and packet data areas that are being overriden.

Supplied buffer layout expected:

 +----------+-----+----+
 |PcapHeader|State|Data|
 +----------+-----+----+
 

Parameters:
buffer - buffer containing capture header, packet state and data buffer sequentially in the buffer

PcapPacket

public PcapPacket(JPacket src)
Does a deep copy of the source packet into newly allocated native memory location.

Parameters:
src - source packet

PcapPacket

public PcapPacket(PcapHeader header,
                  java.nio.ByteBuffer buffer)
Allocates memory for new packet and copies both the header and packet buffer to newly allocated memory. Packet state is uninitialized and needs to be decoded.

Parameters:
header - capture header
buffer - packet data buffer

PcapPacket

public PcapPacket(PcapHeader header,
                  JBuffer buffer)
Allocates memory for new packet and copies both the header and packet buffer to newly allocated memory. Packet state is uninitialized and needs to be decoded.

Parameters:
header - capture header
buffer - packet data buffer

PcapPacket

public PcapPacket(PcapPacket src)
Does a deep copy of the source packet into newly allocated native memory location.

Parameters:
src - source packet

PcapPacket

public PcapPacket(JMemory.Type type)
Special type of instantiation that allows an empty packet to be peered, or in C terms its a packet pointer with no actual memory allocated. Accessing most methods in this packet object before its initialized will throw NullPointerException as the object has not been initialized yet.

Parameters:
type - state of the object to create
Method Detail

getCaptureHeader

public PcapHeader getCaptureHeader()
Retrieves the PcapHeader, capture header provided by libpcap.

Specified by:
getCaptureHeader in class JPacket
Returns:
capture header

getTotalSize

public int getTotalSize()
Gets the total size of the packet including pcap header, decoded state and data buffer.

Specified by:
getTotalSize in class JPacket
Returns:
total size of the packet in bytes

peerHeaderAndData

public int peerHeaderAndData(JBuffer buffer)
Peers both header and data to buffer. The buffer must contain first header then packet data layout in its memory. Packet state is uninitialized.

Parameters:
buffer - the buffer
Returns:
number of bytes peered

peer

public int peer(PcapHeader header,
                JBuffer buffer)
Peer.

Parameters:
header - the header
buffer - the buffer
Returns:
the int

peerAndScan

public int peerAndScan(int dlt,
                       PcapHeader header,
                       JBuffer buffer)
Peer and scan.

Parameters:
dlt - the dlt
header - the header
buffer - the buffer
Returns:
the int

peerHeaderAndData

public int peerHeaderAndData(PcapHeader header,
                             java.nio.ByteBuffer buffer)
                      throws PeeringException
Peer header and data.

Parameters:
header - the header
buffer - the buffer
Returns:
the int
Throws:
PeeringException - the peering exception

peerHeaderAndData

public int peerHeaderAndData(PcapHeader header,
                             JBuffer buffer)
Peer header and data.

Parameters:
header - the header
buffer - the buffer
Returns:
the int

peerStateAndData

public int peerStateAndData(java.nio.ByteBuffer buffer)
                     throws PeeringException
Peers the contents of the buffer directly with this packet. No copies are performed but the capture header, packet state and data are expected to be contained within the buffer with a certain layout as described below:

Supplied buffer layout expected:

 +----------+-----+----+
 |PcapHeader|State|Data|
 +----------+-----+----+
 

Parameters:
buffer - Buffer containing packet header, state and data. Position property specifies that start within the buffer where to peer the first byte.
Returns:
number of bytes that were peered out of the buffer
Throws:
PeeringException - thrown if ByteBuffer is not direct byte buffer type

peerStateAndData

public int peerStateAndData(JBuffer buffer)
Peers the contents of the buffer directly with this packet. No copies are performed but the capture header, packet state and data are expected to be contained within the buffer with a certain layout as described below:

Supplied buffer layout expected:

 +----------+-----+----+
 |PcapHeader|State|Data|
 +----------+-----+----+
 

Parameters:
buffer - buffer containing packet header, state and data
Returns:
number of bytes that were peered out of the buffer

transferHeaderAndDataFrom

public int transferHeaderAndDataFrom(PcapHeader header,
                                     java.nio.ByteBuffer buffer)
Copies contents of header and packet buffer to a single newly allocated buffer. State is uninitialized. The packet's header and buffer's are peered with newly allocated buffer.

Parameters:
header - source header
buffer - source packet data buffer
Returns:
number of bytes copied.

transferHeaderAndDataFrom

public int transferHeaderAndDataFrom(PcapHeader header,
                                     JBuffer buffer)
Copies contents of header and packet buffer to a single newly allocated buffer. State is uninitialized. The packet's header and buffer's are peered with newly allocated buffer.

Parameters:
header - source header
buffer - source packet data buffer
Returns:
number of bytes copied.

transferStateAndDataFrom

public int transferStateAndDataFrom(byte[] buffer)
Copies contents of the buffer to new packet. All of the contents of the buffer are deep copied to new packet. The new packet allocates new memory for the packet contents, state and header if existing memory buffer is not large enough. Otherwise the existing memory buffer is overriden and reused. Existing buffers are not cleared before hand and may contain old data outside of the new header, state and packet data areas that are being overriden.

Supplied buffer layout expected:

 +----------+-----+----+
 |PcapHeader|State|Data|
 +----------+-----+----+
 

Parameters:
buffer - buffer containing capture header, packet state and data buffer sequentially in the buffer
Returns:
number of bytes copied

transferStateAndDataFrom

public int transferStateAndDataFrom(java.nio.ByteBuffer buffer)
Copies contents of the buffer to new packet. All of the contents of the buffer are deep copied to new packet. The new packet allocates new memory for the packet contents, state and header if existing memory buffer is not large enough. Otherwise the existing memory buffer is overriden and reused. Existing buffers are not cleared before hand and may contain old data outside of the new header, state and packet data areas that are being overriden.

Supplied buffer layout expected:

 +----------+-----+----+
 |PcapHeader|State|Data|
 +----------+-----+----+
 

Parameters:
buffer - Buffer containing capture header, packet state and data buffer sequentially in the buffer. Current buffer position points at the start of pcap header.
Returns:
number of bytes copied

transferStateAndDataFrom

public int transferStateAndDataFrom(JBuffer buffer)
Copies contents of the buffer to new packet. All of the contents of the buffer are deep copied to new packet. The new packet allocates new memory for the packet contents, state and header if existing memory buffer is not large enough. Otherwise the existing memory buffer is overriden and reused. Existing buffers are not cleared before hand and may contain old data outside of the new header, state and packet data areas that are being overriden.

Supplied buffer layout expected:

 +----------+-----+----+
 |PcapHeader|State|Data|
 +----------+-----+----+
 

Parameters:
buffer - buffer containing capture header, packet state and data buffer sequentially in the buffer
Returns:
number of bytes copied

transferStateAndDataFrom

public int transferStateAndDataFrom(PcapPacket packet)
Deep copy of the supplied packet to this packet. Contents of the supplied packet such as pcap header, packet state and packet data are deep copied into newly allocated memory if necessary or existing memory buffer if it is large enough to hold the new packet with its complete state. In either case, the new packet will be stored with its header and state in a single contigues buffer.

Parameters:
packet - source packet from which to copy from
Returns:
number of bytes copied

transferStateAndDataTo

public int transferStateAndDataTo(byte[] buffer)
Copies contents of this packet to buffer. The packets capture header, state and packet data are copied to new buffer. After completion of this operation the complete contents and state of the packet will be transfered to the buffer. The layout of the buffer data will be as described below. A buffer with this type of layout is suitable for any transferStateAndData or peer methods for any buffers that are JMemory based. The buffer has to be large enough to hold all of the packet content as returned by method

Parameters:
buffer - buffer containing capture header, packet state and data buffer sequentially in the buffer
Returns:
number of bytes copied getTotalSize(). If the buffer is too small and a runtime exception may be thrown.

The buffer layout will look like the following:

 +----------+-----+----+
 |PcapHeader|State|Data|
 +----------+-----+----+
 


transferStateAndDataTo

public int transferStateAndDataTo(java.nio.ByteBuffer buffer)
Copies contents of this packet to buffer. The packets capture header, state and packet data are copied to new buffer. After completion of this operation the complete contents and state of the packet will be transfered to the buffer. The layout of the buffer data will be as described below. A buffer with this type of layout is suitable for any transferStateAndData or peer methods for any buffers that are JMemory based. The buffer has to be large enough to hold all of the packet content as returned by method

Parameters:
buffer - buffer containing capture header, packet state and data buffer sequentially in the buffer
Returns:
number of bytes copied getTotalSize(). If the buffer is too small and a runtime exception may be thrown.

The buffer layout will look like the following:

 +----------+-----+----+
 |PcapHeader|State|Data|
 +----------+-----+----+
 


transferStateAndDataTo

public int transferStateAndDataTo(JBuffer buffer)
Copies contents of this packet to buffer. The packets capture header, state and packet data are copied to new buffer. After completion of this operation the complete contents and state of the packet will be transfered to the buffer. The layout of the buffer data will be as described below. A buffer with this type of layout is suitable for any transferStateAndData or peer methods for any buffers that are JMemory based. The buffer has to be large enough to hold all of the packet content as returned by method

Parameters:
buffer - buffer containing capture header, packet state and data buffer sequentially in the buffer
Returns:
number of bytes copied getTotalSize(). If the buffer is too small and a runtime exception may be thrown.

The buffer layout will look like the following:

 +----------+-----+----+
 |PcapHeader|State|Data|
 +----------+-----+----+
 


transferStateAndDataTo

public int transferStateAndDataTo(JBuffer buffer,
                                  int offset)
Copies contents of this packet to buffer. The packets capture header, state and packet data are copied to new buffer. After completion of this operation the complete contents and state of the packet will be transfered to the buffer. The layout of the buffer data will be as described below. A buffer with this type of layout is suitable for any transferStateAndData or peer methods for any buffers that are JMemory based. The buffer has to be large enough to hold all of the packet content as returned by method

Parameters:
buffer - buffer containing capture header, packet state and data buffer sequentially in the buffer
offset - the offset
Returns:
number of bytes copied getTotalSize(). If the buffer is too small and a runtime exception may be thrown.

The buffer layout will look like the following:

 +----------+-----+----+
 |PcapHeader|State|Data|
 +----------+-----+----+
 


transferStateAndDataTo

public int transferStateAndDataTo(PcapPacket packet)
Deep copy of the this packet to the supplied packet. Contents of the this packet such as pcap header, packet state and packet data are deep copied into the suppliedpacket, allocating memory if necessary or existing memory buffer if it is large enough to hold the new packet with its complete state. In either case, the packet will be stored with its header and state in a single contigues buffer in the supplied packet.

Parameters:
packet - destination packet to which to copy header, state and packet data
Returns:
number of bytes copied