Package org.jnetstream.packet

A network packet API.

See:
          Description

Interface Summary
Buffered Allows retrieved of a buffer from a buffered object.
CommonProperty<T>  
DataField  
Field<T> A field within a packet after its been decoded.
Field.DynamicField<T>  
Field.StaticField  
FiniteField<T> A field that is constant, finite offset and length.
FiniteHeader A normal header that has constant, finite length.
Header Super interface of all headers contained within a packet.
HeaderCache  
HeaderElement  
HeaderModifier Mutable interface which allows many different types of operations to be performend upon a container such as adding, removing or clearing headers.
Monitor<T,M> A marker, no method interface, that marks objects that are monitored.
Monitored<T>  
MonitorListener<T>  
MutablePacket  
Packet A packet that has been captured or read from a file and decoded.
PacketFactory<T extends Packet> A factory for creating new packet objects.
PacketInitializer Interface which allows packets to be initialized from other objects.
PacketListener Allows an interested observer to monitor packet reads, writes, inserts, deletions and packet modifications.
PacketTemplate Interface which allows a packet to be configured and initialized once and then reused as a template to initialize efficiently other packets.
ProtocolFilterTarget An empty interface that adds additional requirement that a filter target be related to a packet.
SerializedPacketFactory Creates a new packet that can be easily serialized from a normal non-serializable packet.
Updatable Interface allows update operation to be performed on an object.
 

Class Summary
DataHeader  
EnumProperties<D extends Enum<D>,S extends Enum<S>> A specialized properties map that maintains a set of static global and dynamic properties.
MonitorSupport<T> Utility class to help manage listeners
PacketPrintStream  
 

Enum Summary
Header.DynamicProperty Common properties found in every header that are dynamic.
Header.StaticProperty Constants which define various automatic and user defined properties.
Packet.Property Properties which are autogenerated or exported from NPL definition.
 

Exception Summary
UpdateException An exception which may be thrown during an Updatable.update method call.
 

Package org.jnetstream.packet Description

A network packet API. This package defines the API for accessing packet buffer, decoding its contents and modifying its state. The main interface is the Packet interface which provides top level access to each captured packet. Using the Packet interface, you can access the raw packet buffer which contains the sequence of octets that make up the contents of the packet. You can manipulate those buffer contents using high level accessor and setter methods, insert, remove and copy buffer contents around.

Example: accessing packet's buffer content directly and then using high level API for which Packet interface is the superclass:

LiveCapture capture = CaptureFactory.openLive();

while (capture.hasNext()) {
  Packet packet = capture.next();
  
  ByteBuffer rawBuffer = packet.getBuffer();
  // Check and make sure buffer has packet data
  if (rawBuffer.limit() == 0) {
    return; // Empty buffer
  }
  
  /*
   * Next lets call on higher level API to decode the contents of the buffer.
   * The IPv4 interface is auto generated from the related NPL definition. It 
   * provides full access to the structure of the IP header. All data returned
   * is backed by the back-end rawBuffer returned earlier. Any changes to
   * the rawBuffer, are also reflected by the returned values from the higher
   * level API. If the packet structure is altered significantly, Packet.update()
   * must be called as well.
   */
  if (packet.hasCompleteHeader(IPv4.class)) {
    IPv4 ip = packet.getHeader(IPv4.class);
    System.out.println("IP header version = " + ip.version());
    System.out.println("IP header flags = " + ip.flags());
  }
}

Creating new packets

Creating new packets is easy. The package provides a static factory called Packets for creating an empty packet that can easily be populated with headers and data.
Packet packet = Packet.newPacket();
packet.addAll(Ethernet2.class, IPv4.class, TCP.class);
The above example creates a new empty packet and then adds to the packet 3 headers, Ethernet2, IPv4 and TCP. Each header is initialized to its defaults. To aid in further initializing the packets can be initialized to a known state using CaptureDevice, java.util.Properties or another packet using one of the flavours of initFrom method call.
CaptureDevice device = Captures.deviceList().get(0);

Packet packet = Packet.newPacket(Ethernet2.class, IPv4.class, TCP.class);
packet.initSource(device);

IPv4 ip = packet.getHeader(IPv4.class);
ip.destination(IPv4.BROADCAST);
First we acquire the first network interface for our system. Next we create a new packet with Ethernet2, IPv4 and TCP headers initialized to their defaults and bound to each other. Next we allow each header to pull properties out of the CaptureDevice to initialize any source parameters. This would be IPv4.source and Ethernet2.source fields in those headers. Destination fields stay at their defaults which is all zeroed out. Next we set the IPv4.destionation field to value of a IPv4.BROADCAST which equates to 255.255.255.255. Now we have a properly configured packet that we can transmit out a network interface or save to a file.