com.voytechs.jnetstream.codec
Class PacketImpl

java.lang.Object
  extended by com.voytechs.jnetstream.codec.PacketImpl
All Implemented Interfaces:
MutablePacket, Packet

public class PacketImpl
extends java.lang.Object
implements Packet, MutablePacket

Note: The PacketImpl class * is usually hidden from the user and the default public JNetStream access interfaces.

Default implementation class of the Packet interface. A packet contains a list of headers that make up the packet. The last header is usually a data header containing user data in the packet.

Property Tables

This packet object is made up of two sets of property tables. Properties are user definable information objects that can be attached to packets. First set of tables is shared by all PacketImpl objects so there is only a single instance accross all packets. This is useful where information such as protocol/header description, RFC references are stored.

The second property table is private and is unique to each packet object. The properties are also user definable as well as some properties are automatically exported into the PacketImpl property table. Such properties as capture time, packet length and others. Users can define their own properties using the NPL statement "property local" in a NPL definition for the protocol.


Field Summary
 
Fields inherited from interface com.voytechs.jnetstream.codec.Packet
CAPTURE_DEVICE_ARCH, CAPTURE_DEVICE_FILENAME, CAPTURE_DEVICE_IP, CAPTURE_DEVICE_OS, CAPTURE_TIMESTAMP, DADDR, FIRST_HEADER, FLOWKEY, INDEX, PACKET_END, PACKET_LENGTH, PACKET_REMAINING, PACKET_SNAPLEN, PACKET_START, POST_NOTE, PRE_NOTE, SADDR, SUMMARY
 
Constructor Summary
PacketImpl(java.util.Map permProperties, PacketInputStream in)
          Initialize the PacketImpl object from the packet stream.
 
Method Summary
 void addHeader(Header header)
          Adds a new header to the packet.
 Header getDataHeader()
           Returns the data header in the packet.
 byte[] getDataValue()
          Retries the byte[] value from the Data header.
 Header getHeader(int index)
          Returns the indexed header from the packet.
 Header getHeader(java.lang.String name)
          Returns the named header from the packet.
 Header getHeader(java.lang.String name, int index)
          Finds all instances of the named header if more than one, and returns the index header from the headers found.
 int getHeaderCount()
          Returns the size of this packet.
 Header getLastHeader()
           Returns the last header in the packet excluding the Data header if its the last header.
 java.util.Map getPermProperties()
           
 Primitive getProperty(java.lang.String name)
          Retrieve a named property from either the permanent or temporary properties tables.
 java.lang.String getSummary()
          Returns a 1 line summary of the packet content.
 java.util.Map getTempProperties()
           
 java.lang.Object getValue(int headerIndex, java.lang.String fieldName)
          Conveniece method to retrieve a value of a field directly from a header.
 java.lang.Object getValue(java.lang.String propertyName)
          Conveniece method to retrieve a value of a property directly from the Packet.
 java.lang.Object getValue(java.lang.String headerName, java.lang.String fieldName)
          Conveniece method to retrieve a value of a field directly from a header.
 boolean hasDataHeader()
          Check for presence of the Data header.
 boolean hasHeader(java.lang.String name)
          Checks to if the specified header exists in the packet.
 boolean hasHeader(java.lang.String name, int index)
          Checks if the specified index header exists.
 boolean hasLastHeader()
          Checks to see if there is atleast 1 header in the packet.
 boolean hasProperty(java.lang.String name)
          Checks if the specified property exists.
 boolean isReassembled()
          If true, this packet is based on a reassembled buffer of other packets.
protected  void setPacketPropertiesFromStream(PacketInputStream in)
          Extract some properties fromt the input stream and export them into the temporary properties tables.
 void setProperty(java.lang.String name, Primitive property)
          Set a property in the temporary properties table.
 void setReassembled(boolean reassembled)
           
 java.lang.String toString()
          Convert the PacketImpl object to pretty text for display back to user.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

PacketImpl

public PacketImpl(java.util.Map permProperties,
                  PacketInputStream in)
Initialize the PacketImpl object from the packet stream.

Parameters:
permProperties - permProperties hashtable is the shared properties table for all PacketImpl objects.
in - Initialize the PacketImpl object from the given InputStream. Main purpose of this is to extract Stream specific properties and export them into the PacketImpl temporary properties table. Such as capture time, packet length, etc...
Method Detail

setPacketPropertiesFromStream

protected void setPacketPropertiesFromStream(PacketInputStream in)
Extract some properties fromt the input stream and export them into the temporary properties tables. Properties such as capture timestamp, packet length, etc...

Parameters:
in - Input stream to extract properties from. The stream has to follow the PacketInputStream interface.

setProperty

public void setProperty(java.lang.String name,
                        Primitive property)
Set a property in the temporary properties table.

Specified by:
setProperty in interface MutablePacket
Parameters:
name - Name of the property.
property - Property value.

getProperty

public Primitive getProperty(java.lang.String name)
Retrieve a named property from either the permanent or temporary properties tables. First the temporary table is looked up and if property not found then a lookup in the permanent table is done. Permanent tables are shared accross all packets while temporary properties table is unique to each packet.

Specified by:
getProperty in interface Packet
Returns:
Value of the property.

addHeader

public void addHeader(Header header)
Adds a new header to the packet.

Specified by:
addHeader in interface MutablePacket
Parameters:
header - A reference to a header to be added to the list of header within this PacketImpl object.

getHeader

public Header getHeader(java.lang.String name)
Returns the named header from the packet.

Specified by:
getHeader in interface Packet
Parameters:
name - Name of the header to retrieve from the PacketImpl list of headers it has.
Returns:
Header object that if name has been found in the list of headers.

hasHeader

public boolean hasHeader(java.lang.String name,
                         int index)
Description copied from interface: Packet
Checks if the specified index header exists.

Specified by:
hasHeader in interface Packet
Parameters:
name - name of the indexed header.
index - index of the header with the above name
Returns:
Header if found, otherwise null.

getHeader

public Header getHeader(java.lang.String name,
                        int index)
Finds all instances of the named header if more than one, and returns the index header from the headers found. Some headers can appear more than once in a packet such as IPv4 header in an ICMP desitination unreachable packet. The IPv4 header is first found after the DataLink OSI layer typically Ethernet and then a second time after the ICMP header. You can use the index to specify which header you would like of the 2 in our example. This is zero (0) based index so index of 0 would refer to the main packet IPv4 header and index of 1 would refer to the second IPv4 header after the ICMP header in our example. null is returned if the header is not found by name or the index is out of range.
Packet is made up of a number of headers that the decoder was able to decode from the capture file or stream. To find out how many headers use the getHeaderCount() method call.

Specified by:
getHeader in interface Packet
Returns:
The header if found, otherwise nul..

getHeader

public Header getHeader(int index)
Returns the indexed header from the packet. As the PacketImpl object is built and the stream decoded, headers are added sequentially to the PacketImpl object. The index parameter is used to return the header at that index in the list of headers.

Specified by:
getHeader in interface Packet
Parameters:
index - Index of the header to return from the list of headers.
Returns:
The header found at this header index within the packet. Header are added and stored within the packet in the order they were decoded. null is returned if index is out of range.

getLastHeader

public Header getLastHeader()

Returns the last header in the packet excluding the Data header if its the last header. Use getDataHeader() to retrieve the data header.

Packet is made up of a number of headers that the decoder was able to decode from the capture file or stream. To find out how many headers use the getHeaderCount() method call.

Specified by:
getLastHeader in interface Packet
Returns:
The header found at this header index within the packet. Header are added and stored within the packet in the order they were decoded. null is returned if index is out of range.
Since:
JNetStream 0.2.2

getDataHeader

public Header getDataHeader()

Returns the data header in the packet. If present.

Packet is made up of a number of headers that the decoder was able to decode from the capture file or stream. To find out how many headers use the getHeaderCount() method call. Usually there is a catch all header that captures the remainder of the data in the packet. This header is called the Data header. If all available data is retrieved then there could be an empty Data header or none at all.

Specified by:
getDataHeader in interface Packet
Returns:
The data header found within the packet or null if no data header is present or getHeaderCount() == 0
Since:
JNetStream 0.2.2

getHeaderCount

public int getHeaderCount()
Returns the size of this packet. Size refers to number of headers decoded. Only headers that this codec understands are decoded. There may be additional undecoded headers of other protocoal still in the stack.

Specified by:
getHeaderCount in interface Packet
Returns:
Number of headers in the packet.

getValue

public java.lang.Object getValue(java.lang.String headerName,
                                 java.lang.String fieldName)
Conveniece method to retrieve a value of a field directly from a header. First the header name is looked up and then the field within that header.

Specified by:
getValue in interface Packet
Parameters:
headerName - Header name to lookup the field.
fieldName - Field name to retrieve the value from.
Returns:
Opaque value of the field. Null if header or the field is not found within the packet.
Since:
JNetStream 0.2.2

getValue

public java.lang.Object getValue(int headerIndex,
                                 java.lang.String fieldName)
Conveniece method to retrieve a value of a field directly from a header. First the header is looked up by indexand then the field within that header.

Specified by:
getValue in interface Packet
Parameters:
headerIndex - Header index to lookup the field.
fieldName - Field name to retrieve the value from.
Returns:
Opaque value of the field. Null if header or the field is not found within the packet.
Since:
JNetStream 0.2.2

getValue

public java.lang.Object getValue(java.lang.String propertyName)
Conveniece method to retrieve a value of a property directly from the Packet.

Specified by:
getValue in interface Packet
Parameters:
popertyName - property name to retrieve the value from.
Returns:
Opaque value of the property. Null if property is not found within the packet.
Since:
JNetStream 0.2.2

toString

public java.lang.String toString()
Convert the PacketImpl object to pretty text for display back to user.

Overrides:
toString in class java.lang.Object
Returns:
The nicely formatted string of this PacketImpl object.

getPermProperties

public java.util.Map getPermProperties()

getTempProperties

public java.util.Map getTempProperties()

hasHeader

public boolean hasHeader(java.lang.String name)
Description copied from interface: Packet
Checks to if the specified header exists in the packet. You can also check for the "Data" header using this method, although there is a convience method hasDataHeader()

Specified by:
hasHeader in interface Packet
Parameters:
name - name of the header to check for.
Returns:
true if header exists in the packet, otherwise false.

hasLastHeader

public boolean hasLastHeader()
Description copied from interface: Packet
Checks to see if there is atleast 1 header in the packet. If the packet contains only a single Data header, then last header does not exist.

Specified by:
hasLastHeader in interface Packet
Returns:
true if the packet contains the last header (excluding the data header), otherwise false.

hasDataHeader

public boolean hasDataHeader()
Description copied from interface: Packet
Check for presence of the Data header. Not all packets may contain a Data header. If all data in the packet has been decoded a Data header may not be created.

Specified by:
hasDataHeader in interface Packet
Returns:
true if Data header exists, otherwise false.

getDataValue

public byte[] getDataValue()
Description copied from interface: Packet
Retries the byte[] value from the Data header. You should call Packet.hasDataHeader() first to make sure that Data header is present.

Specified by:
getDataValue in interface Packet
Returns:
Raw bytes of the data header, past all the decoded headers in the packet.

hasProperty

public boolean hasProperty(java.lang.String name)
Description copied from interface: Packet
Checks if the specified property exists.

Specified by:
hasProperty in interface Packet
Parameters:
name - Name of the property to check for.
Returns:
Opaque value of the property.

getSummary

public java.lang.String getSummary()
Description copied from interface: Packet
Returns a 1 line summary of the packet content.

Specified by:
getSummary in interface Packet
Returns:
summary of the packet

setReassembled

public void setReassembled(boolean reassembled)
Parameters:
b -

isReassembled

public boolean isReassembled()
Description copied from interface: Packet
If true, this packet is based on a reassembled buffer of other packets. Thus this packet did not strictly came from the capture file.

Specified by:
isReassembled in interface Packet
Returns:
Returns the reassembled.