org.jnetpcap.packet
Class JHeaderScanner

java.lang.Object
  extended by org.jnetpcap.nio.JMemory
      extended by org.jnetpcap.nio.JFunction
          extended by org.jnetpcap.packet.JHeaderScanner

public class JHeaderScanner
extends JFunction

A header scanner, there is one per header, that is able to scan raw memory buffer and determine the length of the header and the next header ID after examining the current header's structure. The header scanner is bound to the native direct scanner provided by the jNetPcap native implementation. The header scanner can be overriden with a java implementation by simply subclassing it and overriding the getHeaderLength and getNextHeader methods. If either of the 2 types of methods are overriden, then the user should also overriden the isDirect() method and return false to indicate that this is not a native direct scanner.

The header scanner is natively peered directly with the appropriate function that performs the scan and determines the next protocol in chain of headers found in the data buffer. Another words this class is peered using a function pointer and dispatched appropriately when invoked to scan for length or next header id.

Here is a typedef definition and the function pointer signature.

 typedef void (*native_protocol_func_t)(scan_t *scan);
 
Note that scan_t structure is implemented by java class JScan which is peered with that structure.

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

Nested Class Summary
 
Nested classes/interfaces inherited from class org.jnetpcap.nio.JMemory
JMemory.Type
 
Field Summary
 
Fields inherited from class org.jnetpcap.nio.JMemory
JNETPCAP_LIBRARY_NAME, MAX_DIRECT_MEMORY_DEFAULT, POINTER
 
Constructor Summary
JHeaderScanner(java.lang.Class<? extends JHeader> c)
          Instantiates a new j header scanner.
JHeaderScanner(JProtocol protocol)
          A java scanner for headers out of a native packet buffer.
 
Method Summary
 boolean addBindings(JBinding... bindings)
          Adds the bindings.
 void clearBindings()
          Clear bindings.
 JBinding[] getBindings()
          Gets the bindings.
 int getGapLength(JPacket packet, int offset)
          Gets the gap length.
 int getHeaderLength(JPacket packet, int offset)
          Returns the length of the header this scanner is registered for.
 int getId()
          Gets the protocol header's numerical ID as assigned by JRegistry
 int getPayloadLength(JPacket packet, int offset)
          Gets the payload length.
 int getPostfixLength(JPacket packet, int offset)
          Gets the postfix length.
 int getPrefixLength(JPacket packet, int offset)
          Gets the prefix length.
 boolean hasBindings()
          Checks for bindings.
 boolean hasScanMethod()
          Checks for scan method.
 boolean isDirect()
          Checks if the scanner at the given ID is a direct or java scanner.
 boolean removeBindings(JBinding... bindings)
          Removes the bindings.
 int scanAllBindings(JPacket packet, int offset)
          Calculates the next header in sequence of headers within the packet buffer.
protected  void scanHeader(JScan scan)
          The main method that this header scanner is called on by the packet scanner, typically from native user space.
 void setScannerMethod(AnnotatedScannerMethod method)
          Sets the scanner method.
 java.lang.String toString()
          To string.
 
Methods inherited from class org.jnetpcap.nio.JFunction
getName
 
Methods inherited from class org.jnetpcap.nio.JMemory
availableDirectMemory, check, cleanup, createReference, isInitialized, isJMemoryBasedOwner, isOwner, maxDirectMemory, peer, peer, peer, reservedDirectMemory, setSize, size, softDirectMemory, toDebugString, toHexdump, toHexdump, totalActiveAllocated, totalAllocateCalls, totalAllocated, totalAllocatedSegments0To255Bytes, totalAllocatedSegments256OrAbove, totalDeAllocateCalls, totalDeAllocated, transferFrom, transferFrom, transferFrom, transferFrom, transferFromDirect, transferOwnership, transferTo, transferTo, 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

JHeaderScanner

public JHeaderScanner(java.lang.Class<? extends JHeader> c)
Instantiates a new j header scanner.

Parameters:
c - the c

JHeaderScanner

public JHeaderScanner(JProtocol protocol)
A java scanner for headers out of a native packet buffer. This constructor allows a custom header scanner to be implemented and registered with JRegistry. The packet scanner, JScanner, uses builtin native scanners to scan packet buffers but also allows custom java scanners to override or provide additional header scanners. Any new protocol header being added to jNetPcap library of protocols, that is not officially released with this API, will have to provide its own custom header scanner.

Parameters:
protocol - core protocol constant for which to override its default native header scanner
Method Detail

addBindings

public boolean addBindings(JBinding... bindings)
Adds the bindings.

Parameters:
bindings - the bindings
Returns:
true, if successful

clearBindings

public void clearBindings()
Clear bindings.


hasBindings

public boolean hasBindings()
Checks for bindings.

Returns:
true, if successful

getBindings

public JBinding[] getBindings()
Gets the bindings.

Returns:
the bindings

getHeaderLength

public int getHeaderLength(JPacket packet,
                           int offset)
Returns the length of the header this scanner is registered for.

Parameters:
packet - the packet object this header is bound to
offset - offset into the packet buffer in bytes of the start of this header
Returns:
length of the header or 0 if this header is not found in the packet buffer

getPrefixLength

public int getPrefixLength(JPacket packet,
                           int offset)
Gets the prefix length.

Parameters:
packet - the packet
offset - the offset
Returns:
the prefix length

getGapLength

public int getGapLength(JPacket packet,
                        int offset)
Gets the gap length.

Parameters:
packet - the packet
offset - the offset
Returns:
the gap length

getPayloadLength

public int getPayloadLength(JPacket packet,
                            int offset)
Gets the payload length.

Parameters:
packet - the packet
offset - the offset
Returns:
the payload length

getPostfixLength

public int getPostfixLength(JPacket packet,
                            int offset)
Gets the postfix length.

Parameters:
packet - the packet
offset - the offset
Returns:
the postfix length

getId

public final int getId()
Gets the protocol header's numerical ID as assigned by JRegistry

Returns:
the id numerical ID of the header

isDirect

public boolean isDirect()
Checks if the scanner at the given ID is a direct or java scanner.

Returns:
true there is a native scanner for this id, otherwise false

removeBindings

public boolean removeBindings(JBinding... bindings)
Removes the bindings.

Parameters:
bindings - the bindings
Returns:
true, if successful

scanAllBindings

public int scanAllBindings(JPacket packet,
                           int offset)
Calculates the next header in sequence of headers within the packet buffer.

Parameters:
packet - the packet object this header is bound to
offset - offset into the packet buffer in bytes of the start of this header
Returns:
numerical ID of the next header as assigned by JRegistry

scanHeader

protected void scanHeader(JScan scan)
The main method that this header scanner is called on by the packet scanner, typically from native user space.

Parameters:
scan - scan state structure that is used to pass around state both in java and native user space

setScannerMethod

public void setScannerMethod(AnnotatedScannerMethod method)
Sets the scanner method.

Parameters:
method - the new scanner method

toString

public java.lang.String toString()
To string.

Overrides:
toString in class java.lang.Object
Returns:
the string
See Also:
Object.toString()

hasScanMethod

public boolean hasScanMethod()
Checks for scan method.

Returns:
true, if successful