org.jnetstream.packet.templates
Class TcpNic0Template

java.lang.Object
  extended by org.jnetstream.packet.templates.TcpNic0Template
All Implemented Interfaces:
java.lang.Iterable<Header>, Packet, PacketTemplate

public class TcpNic0Template
extends java.lang.Object
implements PacketTemplate

A packet template which initializes the packet with Ethernet, IPv4 and TCP headers. Each header is then further initialized to hold the source addresses of network interface #0 found on this system.

Author:
Mark Bednarczyk, Sly Technologies, Inc.

Nested Class Summary
 
Nested classes/interfaces inherited from interface org.jnetstream.packet.Packet
Packet.Property
 
Constructor Summary
TcpNic0Template()
           
 
Method Summary
 java.nio.ByteBuffer add(byte[] buffer)
           
 java.nio.ByteBuffer add(byte[] buffer, java.lang.Class<? extends Header> c)
           
 java.nio.ByteBuffer add(java.nio.ByteBuffer buffer)
           
 java.nio.ByteBuffer add(java.nio.ByteBuffer buffer, java.lang.Class<? extends Header> c)
           
 void add(java.lang.Class<? extends Header> c1)
           
<T extends Header>
T
add(java.lang.Class<T> c)
           
<T extends Header>
T
add(T t)
           
 void addAll(java.lang.Class<? extends Header> c1, java.lang.Class<?>... c3)
           
 void addAll(java.util.List<java.lang.Class<? extends Header>> c)
           
 void clearAll()
           
 java.nio.ByteBuffer compact()
           
 java.nio.ByteBuffer copy(byte[] buffer)
           
 java.nio.ByteBuffer copy(java.nio.ByteBuffer buffer)
           
 void format()
          Formats content of this packet into human readable format using the default formatter and sends the output to the default output device.
 void free()
           
 Header[] getAllHeaders()
           
 com.slytechs.utils.memory.BitBuffer getBuffer()
          Gets a buffer that contains the packet's data.
 com.slytechs.utils.memory.BitBuffer getData()
           Returns the undecoded portion of the packet.
<T extends Header>
T
getHeader(java.lang.Class<T> c)
           Returns the specified header from this packet.
<T extends Header>
T
getHeader(Protocol p)
           Same as the alternative version of Packet.getHeader(Class) method, but faster.
 int getHeaderCount()
          Returns the number of decoded headers within this packet.
 long getIncludedLength()
          Gets the length of the packet content from the current buffer.
 int getOffset(ProtocolInfo<? extends Header> protocol)
           
 long getOriginalLength()
          Gets the length of the packet as it was seen on the wire while originally captured.
 java.lang.Object getProperty(Packet.Property property)
           
 long getTimestampNanos()
          Gets the timestamp as recorded at the time of the packet capture in fraction of a second, in nanoseconds.
 long getTimestampSeconds()
          Gets the timestamp as recorded at the time of the packet capture in number of seconds since 1.1.1970.
<T extends Header>
boolean
hasCompleteHeader(java.lang.Class<T> c)
           Checks if a complete, untruncated, header exists in this packet.
<T extends Header>
boolean
hasCompleteHeader(ProtocolInfo<T> p)
           
<T extends Header>
boolean
hasHeader(java.lang.Class<T> c)
           Checks for presence of the specified header within this particular packet.
<T extends Header>
boolean
hasHeader(ProtocolEntry p)
           
 void init()
          Initialization method for the template.
 void init(PacketInitializer initializer)
           
<T extends Header>
T
insert(java.lang.Class<T> c, java.lang.Class<? extends Header> afterHeader)
           
<T extends Header>
T
insert(java.lang.Class<T> c, int afterIndex)
           
 boolean isTruncated()
          Checks if the packet has been truncated from the original length as seen on the network wire.
 java.util.Iterator<Header> iterator()
          Allows all the headers to be iterated through using the foreach() syntax.
<T extends Header>
T
remove(java.lang.Class<T> c)
           
 Header remove(int index)
           
 Header[] removeAll()
           
 java.nio.ByteBuffer replace(byte[] buffer)
           
 java.nio.ByteBuffer replace(java.nio.ByteBuffer buffer)
           
 void resize(int size)
           
 void setTimestamp(long seconds, int nanos)
           
 void update()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TcpNic0Template

public TcpNic0Template()
Method Detail

format

public void format()
            throws java.io.IOException
Description copied from interface: Packet
Formats content of this packet into human readable format using the default formatter and sends the output to the default output device.

Specified by:
format in interface Packet
Throws:
java.io.IOException - any IO errors while sending output

getAllHeaders

public Header[] getAllHeaders()
Specified by:
getAllHeaders in interface Packet
Returns:

getBuffer

public com.slytechs.utils.memory.BitBuffer getBuffer()
Description copied from interface: Packet
Gets a buffer that contains the packet's data. The the position and limit of the buffer will be set to the start and length of the packet data, respectively. The capacity of the buffer may be more then the limit as the underlying storage or may be allocated larger then actual packet size to preempt possible expansion of the packet data. That is if the buffer the user can exapand the packet by adding fields to it and pushing the "limit" property of the buffer out to as much as buffer's capacity.

Specified by:
getBuffer in interface Packet
Returns:
buffer containing this packet data

getHeader

public <T extends Header> T getHeader(java.lang.Class<T> c)
                           throws java.lang.IllegalStateException
Description copied from interface: Packet

Returns the specified header from this packet. Before calling this method the Packet.hasHeader(java.lang.Class) or Packet.hasCompleteHeader(java.lang.Class) calls must be called atleast once for each header being requested. If the check is not made, even though the header exists in the packet, an IllegalStateException will be thrown. This is simlar to standard Iterator's hasNext requirement. The reason for this requirement is that it is the structure of a packet is so dynamic and changing from packet to packet, that this check has been made mandatory and a requirement to prevent unexpected errors. Also note that the same checks have to be done again after a call to #update on a packet. This is because the packet structure may have changed enough that all previously returned headers are no longer valid or exist in the packet. Only the first header of the specified type encountered is returned.

Example:

 for (Packet packet : Captures.openLive(10)) { // Capture 10 packets
        if (packet.hasHeader(Ethernet.class)) {
                Ethernet ethernet = packet.getHeader(Ethernet.class);
                EUI48 destination = ethernet.destination();
                EUI48 source = ethernet.source();
                short type = ethernet.type();
        }
 
        if (packet.hasCompleteHeader(Ip.class)) {
                Ip ip = packet.getHeader(Ip.class);
                // And so forth
        }
 }
 Captures.close(); // Closes last open capture
 

Specified by:
getHeader in interface Packet
Type Parameters:
T - protocol class type
Parameters:
c - protocol's static class file
Returns:
header of the class type and this method never returns null
Throws:
java.lang.IllegalStateException - thrown if header was not found
See Also:
Packet.getHeader(Protocol), Packet.hasHeader(Class)

getHeaderCount

public int getHeaderCount()
Description copied from interface: Packet
Returns the number of decoded headers within this packet. This method call may encure performance penalty as usually the only way to count how may headers are within packet is to actually iterate and decode parts of each header to the end. This may not neccessarily mean that the headers are decoded completely, but only that portions of the headers is decoded so that the header type of the header following the one being decoded can be assertained.

Specified by:
getHeaderCount in interface Packet
Returns:
number of currently decoded or decodable headers within the packet

getIncludedLength

public long getIncludedLength()
                       throws java.io.IOException
Description copied from interface: Packet
Gets the length of the packet content from the current buffer. The length is the current length of the available data, which may differ from the original length of the packet if the packet has been trucated. This property can also be accessed using the property object accessor getProperty.

Specified by:
getIncludedLength in interface Packet
Returns:
length in octets of the full packet size that is contain
Throws:
java.io.IOException

getOriginalLength

public long getOriginalLength()
                       throws java.io.IOException
Description copied from interface: Packet
Gets the length of the packet as it was seen on the wire while originally captured. The original length may differ from included length if the captured packet was trucated after the capture and only portion of the packet had been included.

Specified by:
getOriginalLength in interface Packet
Returns:
length in octets of the full packet size of the original packet as seen on the network wire
Throws:
java.io.IOException

getTimestampNanos

public long getTimestampNanos()
                       throws java.io.IOException
Description copied from interface: Packet
Gets the timestamp as recorded at the time of the packet capture in fraction of a second, in nanoseconds. The valid range is from 0 to 999,999,999 nanoseconds. The value is always returned in nanoseconds even when the capturing device, such as a network interface, is not capable of timestamping at nanosecond resolution. In case the capture device has microsecond resolution, the valid value of this counter would be from 0 to 999,999,000. To determine the timestamp real resolution of the capturing device use the method #getCaptureDevice() and then CaptureDevice.getTimestampResolution() which will return an enum constant which describes if the resulution is in nanoseconds or microseconds. In either case this information should only be required in rare circuimstances as the timestamp is always converted and reported in nanoseconds, no matter what the resolution actually is.

Specified by:
getTimestampNanos in interface Packet
Returns:
fraction of a second in nanosecond increments in valid range of 0 to 999,999,999
Throws:
java.io.IOException

getTimestampSeconds

public long getTimestampSeconds()
                         throws java.io.IOException
Description copied from interface: Packet
Gets the timestamp as recorded at the time of the packet capture in number of seconds since 1.1.1970.

Specified by:
getTimestampSeconds in interface Packet
Returns:
number of seconds
Throws:
java.io.IOException

getData

public com.slytechs.utils.memory.BitBuffer getData()
Description copied from interface: Packet

Returns the undecoded portion of the packet. This is typically the data portion of the packet starting at the first octet past the end of the last header decoded in the packet. The buffer returned has its "position", "limit" and "capacity" properties initialized as follows, where position = the start of the undecoded buffer within the main packet buffer. Undecoded buffer position property is set to 0, the limit is set to Packet.getIncludedLength() - position which is the end of the packet data. Capacity is set to Packet.getBuffer().capacity() - position which corresponds to all the underlying packet buffer capacity allocated for this packet. Packets typically captured from a live network interface will not have any extra capacity but packets read from a file or initialized by a user may.

Note that if you modify the contents of this buffer the underlying packet buffer will also be modified and visa versa. Also note that unlike modifying decoded headers it may or may not be neccessary to call update. In the case where you do not want the new packet protocol structure to be redecoded, you do not need to call the #update method. If you append a new header by adding the neccessary byte data into this buffer and you want the new protocol header to be discovered, then you need to call on update method. Afterwards, if all goes well you should be able to call Packet.getLastHeader(MyHeader.class) and get the newly added header. Lastly note that if you do change the structure of the packet and do call update method, you must also call the getUndecodedBuffer method again since the buffer will change due to the new header that was added.

Specified by:
getData in interface Packet
Returns:
a new buffer which contains the undecoded (usually data) portion of the packet

hasCompleteHeader

public <T extends Header> boolean hasCompleteHeader(java.lang.Class<T> c)
Description copied from interface: Packet

Checks if a complete, untruncated, header exists in this packet. If the header exists, but is incomplete due to snaplen, this method will return false. Only the first header of the specified type encountered is returned.

Implementation Note: the check performed is very efficient and does not neccessarily mean that any back-end objects needed to be instantiated. The presence of a header is encoded using bit fields, and checked very efficiently. This method does require a scan of the packet the first time its called, inorder to determine which headers are present, but that is also done very efficiently using low level scanner. Once the single scan of a packet has been performed any subsequent header checks are typically a single bitwise operation.

Specified by:
hasCompleteHeader in interface Packet
Type Parameters:
T - Stubfile's header class for the requested header.
Parameters:
c - Class of the header to check for. Java classes for all headers are generated as java stubfiles during NPL definition compile.
Returns:
true if header exists and has not been truncated, false if it exists and has been truncated and also false if it doesn't exist.

hasHeader

public <T extends Header> boolean hasHeader(java.lang.Class<T> c)
Description copied from interface: Packet

Checks for presence of the specified header within this particular packet. The header may be either complete or truncated due to snaplen, but it does exist in this header. Only the first header of the specified type encountered is returned.

Implementation Note: the check performed is very efficient and does not neccessarily mean that any back-end objects needed to be instantiated. The presence of a header is encoded using bit fields, and checked very efficiently. This method does require a scan of the packet the first time its called, inorder to determine which headers are present, but that is also done very efficiently using low level scanner. Once the single scan of a packet has been performed any subsequent header checks are typically a single bitwise operation.

Specified by:
hasHeader in interface Packet
Type Parameters:
T - Stubfile's header class for the requested header.
Parameters:
c - Class of the header to check for. Java classes for all headers are generated as java stubfiles during NPL definition compile.
Returns:
true if header exists

isTruncated

public boolean isTruncated()
                    throws java.io.IOException
Description copied from interface: Packet
Checks if the packet has been truncated from the original length as seen on the network wire. If snaplen has been set during the packet capture, the data captured is only partial data of the original.

Specified by:
isTruncated in interface Packet
Returns:
true that packet has been truncated, otherwise false
Throws:
java.io.IOException

iterator

public java.util.Iterator<Header> iterator()
Description copied from interface: Packet
Allows all the headers to be iterated through using the foreach() syntax.

Specified by:
iterator in interface java.lang.Iterable<Header>
Specified by:
iterator in interface Packet
Returns:
iterator that can be used in a foreach() statement. Iterator will iterate through all the headers

add

public java.nio.ByteBuffer add(java.nio.ByteBuffer buffer)

add

public java.nio.ByteBuffer add(byte[] buffer)

add

public java.nio.ByteBuffer add(java.nio.ByteBuffer buffer,
                               java.lang.Class<? extends Header> c)

add

public java.nio.ByteBuffer add(byte[] buffer,
                               java.lang.Class<? extends Header> c)

resize

public void resize(int size)

setTimestamp

public void setTimestamp(long seconds,
                         int nanos)
                  throws java.lang.IllegalArgumentException,
                         java.io.IOException
Throws:
java.lang.IllegalArgumentException
java.io.IOException

add

public <T extends Header> T add(T t)

add

public <T extends Header> T add(java.lang.Class<T> c)

addAll

public void addAll(java.lang.Class<? extends Header> c1,
                   java.lang.Class<?>... c3)

addAll

public void addAll(java.util.List<java.lang.Class<? extends Header>> c)

clearAll

public void clearAll()

compact

public java.nio.ByteBuffer compact()

copy

public java.nio.ByteBuffer copy(java.nio.ByteBuffer buffer)

copy

public java.nio.ByteBuffer copy(byte[] buffer)

insert

public <T extends Header> T insert(java.lang.Class<T> c,
                                   java.lang.Class<? extends Header> afterHeader)

insert

public <T extends Header> T insert(java.lang.Class<T> c,
                                   int afterIndex)

remove

public <T extends Header> T remove(java.lang.Class<T> c)

remove

public Header remove(int index)

removeAll

public Header[] removeAll()

replace

public java.nio.ByteBuffer replace(java.nio.ByteBuffer buffer)

replace

public java.nio.ByteBuffer replace(byte[] buffer)

update

public void update()
            throws java.io.IOException,
                   UpdateException
Throws:
java.io.IOException
UpdateException

init

public void init()
Description copied from interface: PacketTemplate
Initialization method for the template. You need to put all your packet initialization code in this method. Create any properties, add headers and set values.

Specified by:
init in interface PacketTemplate

init

public void init(PacketInitializer initializer)

free

public void free()

add

public void add(java.lang.Class<? extends Header> c1)

getHeader

public <T extends Header> T getHeader(Protocol p)
                           throws java.lang.IllegalStateException
Description copied from interface: Packet

Same as the alternative version of Packet.getHeader(Class) method, but faster. You can speed up retrieval of headers if you do the lookup of the protocol yourself. The use of parameterized header retrieval ensures compile time type safety of the retrieved header.

Here is an example:

 final Protocol<Ethernet> ETHERNET = ProtocolRegistry.getProtocol(Ethernet.class);
 final Protocol<Ip> IP = ProtocolRegistry.getProtocol(Ip.class);
 
 for (Packet packet: Capture.openLive(10)) {
        if (packet.hasHeader(ETHERNET) {
                Ethernet ethernet = packet.getHeader(ETHERNET);
                // And so forth
        }
 }
 Captures.close(); // Closes last open capture
 

Specified by:
getHeader in interface Packet
Type Parameters:
T - protocol class type
Parameters:
p - instance of a protocol as retrieved from protocol registry
Returns:
header of the class type and this method never returns null
Throws:
java.lang.IllegalStateException - thrown if header was not found
See Also:
Packet.getHeader(Class), Packet.hasHeader(ProtocolEntry), ProtocolRegistry#getProtocol(Class)

hasCompleteHeader

public <T extends Header> boolean hasCompleteHeader(ProtocolInfo<T> p)
                          throws CodecCreateException,
                                 java.io.IOException
Specified by:
hasCompleteHeader in interface Packet
Throws:
CodecCreateException
java.io.IOException

hasHeader

public <T extends Header> boolean hasHeader(ProtocolEntry p)
                  throws CodecCreateException,
                         java.io.IOException
Specified by:
hasHeader in interface Packet
Throws:
CodecCreateException
java.io.IOException

getOffset

public int getOffset(ProtocolInfo<? extends Header> protocol)

getProperty

public java.lang.Object getProperty(Packet.Property property)
Specified by:
getProperty in interface Packet
Returns: