org.jnetpcap.packet
Class JPacket.State

java.lang.Object
  extended by org.jnetpcap.nio.JMemory
      extended by org.jnetpcap.nio.JStruct
          extended by org.jnetpcap.packet.JPacket.State
Enclosing class:
JPacket

public static class JPacket.State
extends JStruct

Class maintains the decoded packet state. The class is peered with struct packet_state_t

 typedef struct packet_state_t {
        uint64_t pkt_header_map; // bit map of presence of headers
        char *pkt_data; // packet data buffer
        int32_t pkt_header_count; // total number of headers found
 
        // Keep track of how many instances of each header we have
        uint8_t pkt_instance_counts[MAX_ID_COUNT];
        header_t pkt_headers[]; // One per header + 1 more for payload
 } packet_t;
 
and struct header_t
 typedef struct header_t {
        int32_t hdr_id; // header ID
        uint32_t hdr_offset; // offset into the packet_t->data buffer
        int32_t hdr_length; // length of the header in packet_t->data buffer
 } header_t;
 
 

The methods in this State provide 3 sets of functions. Looking up global state of the packet found in packet_state_t structure, looking up header information in struct header_t by header ID retrieved from JRegistry and instance numbers, looking up header information by direct indexes into native maps and arrays. Instance numbers specify which instance of the header, if more than 1 exists in a packet. For example if there is a packet with 2 Ip4 headers such as

 Ethernet->Ip4->Snmp->Ip4 
 or 
 Ethernet->Ip4->Ip4 (IP tunneled IP)
 
the first Ip4 header is instance 0 and the second Ip4 header is instance 2. You can use the method getInstanceCount(int) to learn how many instance headers exists. That information is stored in the packet_state_t structure for efficiency.

Author:
Mark Bednarczyk, Sly Technologies, Inc.

Nested Class Summary
 
Nested classes/interfaces inherited from class org.jnetpcap.nio.JMemory
JMemory.Type
 
Field Summary
static int FLAG_TRUNCATED
          Flag which is set when the packet that was decoded was truncated and not the original length seen on the wire.
static java.lang.String STRUCT_NAME
          The Constant STRUCT_NAME.
 
Fields inherited from class org.jnetpcap.nio.JMemory
JNETPCAP_LIBRARY_NAME, MAX_DIRECT_MEMORY_DEFAULT, POINTER
 
Constructor Summary
JPacket.State(int size)
          Instantiates a new state.
JPacket.State(JMemory.Type type)
          Instantiates a new state.
 
Method Summary
 void cleanup()
          Cleanup.
 int findHeaderIndex(int id)
          Find header index.
 int findHeaderIndex(int id, int instance)
          Find header index.
 long get64BitHeaderMap(int index)
          Gets the 64 bit header map.
 int getFlags()
          Gets the 32-bit counter that contains packet's flags in packet_state_t structure.
 JFlowKey getFlowKey()
          Gets the flow key.
 long getFrameNumber()
          The frame number is assigned by the scanner at the time of the scan.
 int getHeaderCount()
          Gets the header count.
 int getHeaderIdByIndex(int index)
          Gets the header id by index.
 int getHeaderLengthByIndex(int index)
          A convenience method that gets the length in the packet buffer of the header at specified index.
 int getHeaderOffsetByIndex(int index)
          A convenience method that gets the offset into the packet buffer of the header at specified index.
 int getInstanceCount(int id)
          Gets the instance count.
 int getWirelen()
          Gets the packet's wire length.
 int peer(java.nio.ByteBuffer peer)
          Peer.
 int peer(JBuffer peer)
          Peer.
 int peer(JBuffer peer, int offset, int length)
          Peer.
 int peer(JMemory memory, int offset)
          Peer.
 int peer(JMemoryPool.Block peer, int offset, int length)
          Peer.
 int peer(JPacket.State peer)
          Peer.
 int peerHeaderById(int id, int instance, JHeader.State dst)
          Peer header by id.
 int peerHeaderByIndex(int index, JHeader.State dst)
          Peer header by index.
 int peerTo(JBuffer buffer, int offset)
          Peers this packet's state to buffer.
 int peerTo(JBuffer buffer, int offset, int size)
          Peers this packet's state to buffer.
 int peerTo(JPacket.State state, int offset)
          Peer to.
 void setFlags(int flags)
          Sets the 32-bit counter with packet flags.
 void setWirelen(int length)
          Sets the packet's wire length.
static int sizeof(int count)
          Sizeof.
 java.lang.String toDebugString()
          Dump packet_state_t structure and its sub structures to textual debug output
 int transferTo(byte[] dst, int dstOffset)
          Transfer to.
 int transferTo(byte[] dst, int srcOffset, int length, int dstOffset)
          Transfer to.
 int transferTo(JBuffer dst, int srcOffset, int length, int dstOffset)
          Transfer to.
 int transferTo(JPacket.State dst)
          Transfer to.
 
Methods inherited from class org.jnetpcap.nio.JStruct
getStructName, toString
 
Methods inherited from class org.jnetpcap.nio.JMemory
availableDirectMemory, check, createReference, isInitialized, isJMemoryBasedOwner, isOwner, maxDirectMemory, peer, peer, reservedDirectMemory, setSize, size, softDirectMemory, toHexdump, toHexdump, totalActiveAllocated, totalAllocateCalls, totalAllocated, totalAllocatedSegments0To255Bytes, totalAllocatedSegments256OrAbove, totalDeAllocateCalls, totalDeAllocated, transferFrom, transferFrom, 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
 

Field Detail

FLAG_TRUNCATED

public static final int FLAG_TRUNCATED
Flag which is set when the packet that was decoded was truncated and not the original length seen on the wire.

See Also:
Constant Field Values

STRUCT_NAME

public static final java.lang.String STRUCT_NAME
The Constant STRUCT_NAME.

See Also:
Constant Field Values
Constructor Detail

JPacket.State

public JPacket.State(int size)
Instantiates a new state.

Parameters:
size - the size

JPacket.State

public JPacket.State(JMemory.Type type)
Instantiates a new state.

Parameters:
type - the type
Method Detail

sizeof

public static int sizeof(int count)
Sizeof.

Parameters:
count - header counter, number of headers to calaculate in
Returns:
size in bytes

cleanup

public void cleanup()
Cleanup.

Overrides:
cleanup in class JMemory
See Also:
JMemory.cleanup()

findHeaderIndex

public int findHeaderIndex(int id)
Find header index.

Parameters:
id - the id
Returns:
the int

findHeaderIndex

public int findHeaderIndex(int id,
                           int instance)
Find header index.

Parameters:
id - the id
instance - the instance
Returns:
the int

get64BitHeaderMap

public long get64BitHeaderMap(int index)
Gets the 64 bit header map.

Parameters:
index - TODO: remove index, its no longer used natively
Returns:
the 64 bit header map

getFlags

public int getFlags()
Gets the 32-bit counter that contains packet's flags in packet_state_t structure.

Returns:
bit flags for this packet

getFlowKey

public JFlowKey getFlowKey()
Gets the flow key.

Returns:
the flow key

getFrameNumber

public long getFrameNumber()
The frame number is assigned by the scanner at the time of the scan. Therefore number is only unique within the same scanner.

Returns:
frame number

getHeaderCount

public int getHeaderCount()
Gets the header count.

Returns:
the header count

getHeaderIdByIndex

public int getHeaderIdByIndex(int index)
Gets the header id by index.

Parameters:
index - the index
Returns:
the header id by index

getHeaderLengthByIndex

public int getHeaderLengthByIndex(int index)
A convenience method that gets the length in the packet buffer of the header at specified index. Typically header information is retrieved using JHeader.State structure which can access all available header information.

Parameters:
index - header index
Returns:
length in bytes of the header

getHeaderOffsetByIndex

public int getHeaderOffsetByIndex(int index)
A convenience method that gets the offset into the packet buffer of the header at specified index. Typically header information is retrieved using JHeader.State structure which can access all available header information.

Parameters:
index - header index
Returns:
offset in bytes of the start of the header

getInstanceCount

public int getInstanceCount(int id)
Gets the instance count.

Parameters:
id - the id
Returns:
the instance count

getWirelen

public int getWirelen()
Gets the packet's wire length.

Returns:
original length of the packet

peer

public int peer(java.nio.ByteBuffer peer)
         throws PeeringException
Peer.

Overrides:
peer in class JMemory
Parameters:
peer - the peer
Returns:
the int
Throws:
PeeringException - the peering exception
See Also:
JMemory.peer(java.nio.ByteBuffer)

peer

public int peer(JBuffer peer)
Peer.

Parameters:
peer - the peer
Returns:
the int

peer

public int peer(JBuffer peer,
                int offset,
                int length)
         throws java.lang.IndexOutOfBoundsException
Peer.

Parameters:
peer - the peer
offset - the offset
length - the length
Returns:
the int
Throws:
java.lang.IndexOutOfBoundsException - the index out of bounds exception

peer

public int peer(JMemory memory,
                int offset)
Peer.

Parameters:
memory - the memory
offset - the offset
Returns:
the int

peer

public int peer(JMemoryPool.Block peer,
                int offset,
                int length)
         throws java.lang.IndexOutOfBoundsException
Peer.

Parameters:
peer - the peer
offset - the offset
length - the length
Returns:
the int
Throws:
java.lang.IndexOutOfBoundsException - the index out of bounds exception

peer

public int peer(JPacket.State peer)
Peer.

Parameters:
peer - the peer
Returns:
the int

peerHeaderById

public int peerHeaderById(int id,
                          int instance,
                          JHeader.State dst)
Peer header by id.

Parameters:
id - the id
instance - the instance
dst - the dst
Returns:
the int

peerHeaderByIndex

public int peerHeaderByIndex(int index,
                             JHeader.State dst)
                      throws java.lang.IndexOutOfBoundsException
Peer header by index.

Parameters:
index - the index
dst - the dst
Returns:
the int
Throws:
java.lang.IndexOutOfBoundsException - the index out of bounds exception

peerTo

public int peerTo(JBuffer buffer,
                  int offset)
Peers this packet's state to buffer.

Parameters:
buffer - source buffer
offset - offset into the buffer
Returns:
number of bytes peered

peerTo

public int peerTo(JBuffer buffer,
                  int offset,
                  int size)
Peers this packet's state to buffer.

Parameters:
buffer - source buffer
offset - offset into the buffer
size - specifies the number of bytes to peer
Returns:
number of bytes peered

peerTo

public int peerTo(JPacket.State state,
                  int offset)
Peer to.

Parameters:
state - the state
offset - the offset
Returns:
the int

setFlags

public void setFlags(int flags)
Sets the 32-bit counter with packet flags.

Parameters:
flags - bit flags for this packet

setWirelen

public void setWirelen(int length)
Sets the packet's wire length.

Parameters:
length - the original length of the packet before truncation

toDebugString

public java.lang.String toDebugString()
Dump packet_state_t structure and its sub structures to textual debug output

Explanation:

 sizeof(packet_state_t)=16
 sizeof(header_t)=8 and *4=32
 pkt_header_map=0x1007         // bitmap, each bit represets a header
 pkt_header_count=4            // how many header found
 // per header information (4 header found in this example)
 pkt_headers[0]=<hdr_id=1  ETHERNET ,hdr_offset=0  ,hdr_length=14>
 pkt_headers[1]=<hdr_id=2  IP4      ,hdr_offset=14 ,hdr_length=60>
 pkt_headers[2]=<hdr_id=12 ICMP     ,hdr_offset=74 ,hdr_length=2>
 pkt_headers[3]=<hdr_id=0  PAYLOAD  ,hdr_offset=76 ,hdr_length=62>
 
 // hdr_id = numerical ID of the header, asssigned by JRegistry
 // hdr_offset = offset in bytes into the packet buffer
 // hdr_length = length in bytes of the entire header
 
Packet state is made up of 2 structures: packet_stat_t and an array of header_t, one per header. Total size in bytes is all of the header structures combined, that is 16 + 32 = 48 bytes. Each bit in the header_map represents the presence of that header type. The index of the bit is the numerical ID of the header. If 2 headers of the same type are present, they are both represented by a single bit in the bitmap. This way the implementation JPacket.hasHeader(int id) is a simple bit operation to test if the header is present or not.

Overrides:
toDebugString in class JMemory
Returns:
multiline string containing dump of the entire structure

transferTo

public int transferTo(byte[] dst,
                      int dstOffset)
Transfer to.

Parameters:
dst - the dst
dstOffset - the dst offset
Returns:
the int

transferTo

public int transferTo(byte[] dst,
                      int srcOffset,
                      int length,
                      int dstOffset)
Transfer to.

Overrides:
transferTo in class JMemory
Parameters:
dst - the dst
srcOffset - the src offset
length - the length
dstOffset - the dst offset
Returns:
the int
See Also:
JMemory.transferTo(byte[], int, int, int)

transferTo

public int transferTo(JBuffer dst,
                      int srcOffset,
                      int length,
                      int dstOffset)
Transfer to.

Overrides:
transferTo in class JMemory
Parameters:
dst - the dst
srcOffset - the src offset
length - the length
dstOffset - the dst offset
Returns:
the int
See Also:
JMemory.transferTo(org.jnetpcap.nio.JBuffer, int, int, int)

transferTo

public int transferTo(JPacket.State dst)
Transfer to.

Parameters:
dst - the dst
Returns:
the int