com.voytechs.jnetstream.io
Class QueuePacketInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by com.voytechs.jnetstream.io.ProtocolDataInputStream
          extended by com.voytechs.jnetstream.io.PacketInputStream
              extended by com.voytechs.jnetstream.io.QueuePacketInputStream
All Implemented Interfaces:
CaptureMetaFormat, java.io.Closeable

public class QueuePacketInputStream
extends PacketInputStream
implements CaptureMetaFormat

Allows packet data to be passed in as byte[] directly into the inputstream. No capture files are decoded, only user supplied packet data is decoded from the supplied user buffers. Here is an example:


 import java.io.*;
 
 import java.sql.*;
 
 import com.voytechs.jnetstream.io.*;
 import com.voytechs.jnetstream.codec.*;
 
 public class InMemoryTest
 {
     public static void main(String[] args)
     {
         byte[] buffer = {
             (byte)0x00, (byte)0x03, (byte)0x42, (byte)0x57,
             (byte)0x80, (byte)0x02, (byte)0x00, (byte)0x60,
             (byte)0x0f, (byte)0xec, (byte)0x78, (byte)0xee,
             (byte)0x88, (byte)0x64, (byte)0x11, (byte)0x00,
             (byte)0xbf, (byte)0x91, (byte)0x00, (byte)0x44,
             (byte)0x00, (byte)0x21, (byte)0x45, (byte)0x00,
             (byte)0x00, (byte)0x42, (byte)0x08, (byte)0x93,
             (byte)0x00, (byte)0x00, (byte)0x40, (byte)0x11,
             (byte)0x24, (byte)0x65, (byte)0x41, (byte)0x06,
             (byte)0xae, (byte)0xfd, (byte)0xcd, (byte)0x98,
             (byte)0x90, (byte)0x17, (byte)0xc4, (byte)0x01,
             (byte)0x00, (byte)0x35, (byte)0x00, (byte)0x2e,
             (byte)0xa9, (byte)0x88, (byte)0x15, (byte)0xb8,
             (byte)0x01, (byte)0x00, (byte)0x00, (byte)0x01,
             (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
             (byte)0x00, (byte)0x00, (byte)0x04, (byte)0x77,
             (byte)0x70, (byte)0x61, (byte)0x64, (byte)0x0b,
             (byte)0x6c, (byte)0x61, (byte)0x75, (byte)0x6e,
             (byte)0x63, (byte)0x68, (byte)0x6d, (byte)0x6f,
             (byte)0x64, (byte)0x65, (byte)0x6d, (byte)0x03,
             (byte)0x63, (byte)0x6f, (byte)0x6d, (byte)0x00,
             (byte)0x00, (byte)0x01, (byte)0x00, (byte)0x01
         };
 
         String firstHeader = "Ethernet2";
         Timestamp timestamp = new Timestamp (System.currentTimeMillis());
 
         try {
             QueuePacketInputStream in = new QueuePacketInputStream();
             Decoder decoder = new Decoder(in);
             in.addBuffer(buffer, firstHeader, timestamp);
             Packet pkt = decoder.nextPacket();
 
             System.out.println(pkt);
 
         }
         catch (Exception ex) {
             System.out.println("ERROR in jnetstream. Exception: " + ex);
         }
 
     }
 
 }
 
 
This produces the following output:

 Ether: ---- Ethernet2 OSI=2 Frame #1 Captured on 2005-05-23 18:15:42.803 ----
 Ether:
 Ether:   dst =  0:3:42:57:80:2
 Ether:   src =  0:60:f:ec:78:ee
 Ether: proto =  0x8864 "(PPPOE) PPP Over Ethernet"
 Ether:   crc = (Frame check supressed)
 Ether:
 PPPOE: ---- PPPOE RFC=2516 OSI=2 ----
 PPPOE:
 PPPOE:        ver =  1 "Version 1"
 PPPOE:       type =  1 "Type 1"
 PPPOE:       code =  0 "PPP session packet"
 PPPOE: session_id =  49041
 PPPOE:     length =  68 (octets)
 PPPOE:
 PPP: ---- PPP RFC=1661,1662 OSI=2 ----
 PPP:
 PPP: protocol =  0x21 "Internet Protocol Version 4 (IPv4)"
 PPP:
 IPv4: ---- IPv4 RFC=791 OSI=3 ----
 IPv4:
 IPv4:      ver =  4 "Internet Protocol version 4"
 IPv4:     hlen =  5 (20 bytes) "No IP options present"
 IPv4:      tos = 00000000 0x0
 IPv4:            000..... 0x0 = [precedence] "Routine"
 IPv4:            ...0.... 0x0 = [delay] "Normal delay"
 IPv4:            ....0... 0x0 = [throughtput] "Normal throughput"
 IPv4:            .....0.. 0x0 = [reliability] "Normal reliability"
 IPv4:            ......00 0x0 = [reserved]
 IPv4:      len =  66 (46 bytes of data)
 IPv4:       id =  0x893
 IPv4:    flags = 000 0x0 (bit fields)
 IPv4:            0.. 0x0 = [reserved]
 IPv4:            .0. 0x0 = [df] "fragmentation OK"
 IPv4:            ..0 0x0 = [mf] "no more fragments"
 IPv4:   offset =  0 (bytes)
 IPv4:      ttl =  64 (hops)
 IPv4:    proto =  17 "(UDP) User Datagram Protocol (RFC768)"
 IPv4: checksum =  0x2465
 IPv4:    saddr =  65.6.174.253
 IPv4:    daddr =  205.152.144.23
 IPv4:
 UDP: ---- UDP RFC=768 OSI=4 ----
 UDP:
 UDP: sport =  50177
 UDP: dport =  53
 UDP:   len =  46 (38 bytes of data)
 UDP:   crc =  0xa988
 UDP:
 DNS: ---- DNS RFC=1035 OSI=7 ----
 DNS:
 DNS: transactionid =  0x15b8
 DNS:         flags = 0000000100000000 0x100
 DNS:                 0............... 0 = [qr] "Query"
 DNS:                 .0000........... 0 = [op] "Standard query"
 DNS:                 .....0.......... 0 = [aa] "Server isn't authority for domain"
 DNS:                 ......0......... 0 = [tc] "Message is not truncated"
 DNS:                 .......1........ 1 = [rd] "Do query recursively"
 DNS:                 ........0....... 0 = [ra] "Server can't do recursive queries"
 DNS:                 .........000.... 0 = [z] "Answer/authority portion was not authenticated by the server"
 DNS:                 ............0000 0 = [rcode] "No error condition"
 DNS:     Questions =  1
 DNS:        Answer =  0 (RRs)
 DNS:     Authority =  0 (RRs)
 DNS:    Additional =  0 (RRs)
 DNS:
 DNS: DNS.QuestionSection[0]
 DNS:          name = "wpad.launchmodem.com."
 DNS:          Type =  1
 DNS:         Class =  1 "Internet Address"
 

Since:
0.2.2
Author:
Mark Bednarczyk

Nested Class Summary
 class QueuePacketInputStream.BufferEntry
          A little utility class to hold extra information along with the buffer.
 
Nested classes/interfaces inherited from class com.voytechs.jnetstream.io.PacketInputStream
PacketInputStream.BufferedHeader
 
Field Summary
static java.lang.String MODULE
           
 
Fields inherited from class com.voytechs.jnetstream.io.PacketInputStream
captureDeviceArch, captureDeviceFilename, captureDeviceIp, captureDeviceOS, captureLive, debugHeaders, fileHeader, linkType, packetCaptureTimestamp, packetEnd, packetLength, packetSnaplen, packetStart, recordEnd, recordHeader, recordLength, recordStart, stackIn
 
Fields inherited from class com.voytechs.jnetstream.io.ProtocolDataInputStream
bitsLeft, in
 
Constructor Summary
QueuePacketInputStream()
          Sets up an empty ByteArray input stream.
QueuePacketInputStream(ExpandableByteArrayInputStream ein)
          Sets up an empty ByteArray input stream.
 
Method Summary
 void addBuffer(byte[] buf, java.lang.String firstHeader, java.sql.Timestamp captureTimestamp)
          Adds a new buffer to the FIFO list of buffers to process.
 java.lang.String getCaptureFormatName()
          Not supported
 java.util.Enumeration getMetaEnumeration()
          Not supported.
protected  void initRecordHeader()
          Retrieve next packet from our FIFO queue and prep the inputstream for packet reading and boundary checking..
 boolean isMetaDataSupported()
          Meta information is not supported with this particular class.
static void main(java.lang.String[] args)
          Example method of how to use QueuePacketInputStream.
 
Methods inherited from class com.voytechs.jnetstream.io.PacketInputStream
bitsLeft, clear, getCaptureDeviceAddress, getCaptureDeviceArch, getCaptureDeviceFilename, getCaptureDeviceIp, getCaptureDeviceOS, getCaptureTimestamp, getFileFormatName, getFileHeader, getLinkType, getPacketEnd, getPacketLength, getPacketLengthRemaining, getPacketSnaplen, getPacketStart, getPaddingLength, getRecordEnd, getRecordHeader, getRecordHeaderLength, getRecordLength, getRecordStart, goToEndOfRecord, gotoMark, hasForceRead, hex, initPacketStream, isCaptureLive, isCompressed, isDebugHeaders, isPacketReady, isReady, isReady, nextPacket, pop, position, printStream, push, push, read, setCaptureTimestamp, setCompressed, setDebugHeaders, setForceRead, setIncludePadding, setLinkType, setPacketLength, setPacketSnaplen, setPacketStart, setRecordLength, setRecordStart, skip
 
Methods inherited from class com.voytechs.jnetstream.io.ProtocolDataInputStream
readBits, readBitsLong, readBoolean, readByte, readerUnsignedLongLittleEndian, readFromPacket, readFromPacket, readInt, readIntLittleEndian, readLong, readLongLittleEndian, readShort, readShortLittleEndian, readUnsignedByte, readUnsignedInt, readUnsignedIntLittleEndian, readUnsignedLong, readUnsignedShort, readUnsignedShortLittleEndian, reThrow
 
Methods inherited from class java.io.InputStream
available, close, mark, markSupported, read, read, reset
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MODULE

public static final java.lang.String MODULE
See Also:
Constant Field Values
Constructor Detail

QueuePacketInputStream

public QueuePacketInputStream()
                       throws java.io.IOException,
                              EOPacketStream,
                              StreamFormatException
Sets up an empty ByteArray input stream. You need to use addBuffer() method to add 1 or more buffers to this input stream. Each buffer added will retrieved in the FIFO order in which it was added.

Throws:
java.io.IOException
EOPacketStream
StreamFormatException

QueuePacketInputStream

public QueuePacketInputStream(ExpandableByteArrayInputStream ein)
                       throws java.io.IOException,
                              EOPacketStream,
                              StreamFormatException
Sets up an empty ByteArray input stream. You need to use addBuffer() method to add 1 or more buffers to this input stream. Each buffer added will retrieved in the FIFO order in which it was added.

Throws:
java.io.IOException
EOPacketStream
StreamFormatException
Method Detail

addBuffer

public void addBuffer(byte[] buf,
                      java.lang.String firstHeader,
                      java.sql.Timestamp captureTimestamp)
Adds a new buffer to the FIFO list of buffers to process.

Parameters:
buf - Buffer to add to the FIFO queue.
firstHeader - Name of the first header to use to process the packet buffer. The name has to be defined in NPL protocol definitions of the current version of this software.
captureTimestamp - amount of milli seconds since 1970 greenwitch time specifying the time at which this packet was created.

initRecordHeader

protected void initRecordHeader()
                         throws java.io.IOException,
                                EOPacketStream,
                                StreamFormatException
Retrieve next packet from our FIFO queue and prep the inputstream for packet reading and boundary checking..

Overrides:
initRecordHeader in class PacketInputStream
Throws:
java.io.IOException
EOPacketStream
StreamFormatException

getCaptureFormatName

public java.lang.String getCaptureFormatName()
Not supported

Specified by:
getCaptureFormatName in interface CaptureMetaFormat
Returns:
Returns a warning message.

getMetaEnumeration

public java.util.Enumeration getMetaEnumeration()
Not supported.

Specified by:
getMetaEnumeration in interface CaptureMetaFormat
Returns:
Always returns null as Meta information is not supported with this particular class.

isMetaDataSupported

public boolean isMetaDataSupported()
Meta information is not supported with this particular class. Since there is no associated capture file with this type of QueuePacketInputStream there is no Meta information to be returned about it.

Specified by:
isMetaDataSupported in interface CaptureMetaFormat
Returns:
Always returns false to indicate that Meta information is not supported by this class.

main

public static void main(java.lang.String[] args)
Example method of how to use QueuePacketInputStream. The example uses data from capture files. Ofcourse it works fine with any type of static buffer as well, just hard to do a decent demo with a couple of packets. All the capture file headers in the capture file are stripped and only the packet data is used and copied into byte[] buffers allocated directly in the main() method. Then those buffers are passed into an instance of QueuePacketInputStream. The decoder is initialized as the QueuePacketInputStream as its input stream. So any packets you add to the queue stream are then decoded by the decoder when you call its nextPacket() method.

Parameters:
args - Expects a single argument, filename of a capture file.