com.voytechs.jnetstream.io
Class PacketInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by com.voytechs.jnetstream.io.ProtocolDataInputStream
          extended by com.voytechs.jnetstream.io.PacketInputStream
All Implemented Interfaces:
java.io.Closeable
Direct Known Subclasses:
PacketBuffer, PcapInputStream, QueuePacketInputStream, RawformatInputStream, SnoopInputStream

public abstract class PacketInputStream
extends ProtocolDataInputStream

Stream object that reads either an InputStream of bytes and allows access to the byte stream with the following features:
1) data can be read in either bytes or individual bits.
2) position within the stream can be pushed on to a stack
3) position can be poped off of the stack.
4) a packet structure is imposed on the stream so that you can query or be notified when the end of an individual packet byte stream is over and when the next packet byte stream is beginning.
5) data can be read in any binary format (Big Endian or Little Endian)
6) Packet capture information is accessable, such as:
a) IP address of the capture device.
b) interface or filename the packet was captured on.
c) capture time of the packet
d) length of the entire packet in bytes
e) OS name of the capture device
d) OS version of the capture device


Nested Class Summary
static class PacketInputStream.BufferedHeader
          Debug class which returns byte buffer of the headers mainly for debug purposes.
 
Field Summary
protected  java.lang.String captureDeviceArch
          Capture device hardware architecture.
protected  java.lang.String captureDeviceFilename
          Name of the file or interface the capture occured.
protected  IpAddress captureDeviceIp
          Capture device IP address.
protected  java.lang.String captureDeviceOS
          Capture device OS.
protected  boolean captureLive
          Flag indicating if the stream is comming from a live capture or a pre-captured file.
protected  boolean debugHeaders
           
protected  PacketInputStream.BufferedHeader fileHeader
           
protected  java.lang.String linkType
           
protected  java.sql.Timestamp packetCaptureTimestamp
          Capture time of the packet.
protected  long packetEnd
           
protected  long packetLength
          Length of the captured packet.
protected  long packetSnaplen
           
protected  long packetStart
           
protected  long recordEnd
           
protected  PacketInputStream.BufferedHeader recordHeader
           
protected  long recordLength
           
protected  long recordStart
           
 BitStackInputStream stackIn
          Reference to stack input so we can push() and pop() positions witin.
 
Fields inherited from class com.voytechs.jnetstream.io.ProtocolDataInputStream
bitsLeft, in
 
Constructor Summary
PacketInputStream(BitStackInputStream inputStream)
           
 
Method Summary
 int bitsLeft()
           
 void clear()
          Clears previously pushed position off of a stack and without rewinding the stream.
 IpAddress getCaptureDeviceAddress()
          Return IP address of the device the stream originated on or if known where the capture occured.
 java.lang.String getCaptureDeviceArch()
          Return OS name of the device the stream originated or if know where the capture occured.
 java.lang.String getCaptureDeviceFilename()
          Return filename name or interface name of the source of this data.
 IpAddress getCaptureDeviceIp()
          Return Ip address of the source of this data.
 java.lang.String getCaptureDeviceOS()
          Return OS name of the device the stream originated or if know where the capture occured.
 java.sql.Timestamp getCaptureTimestamp()
          returns the time the packet was captured.
 java.lang.String getFileFormatName()
           
 PacketInputStream.BufferedHeader getFileHeader()
           
 java.lang.String getLinkType()
          Returns the name of the first header in the packet.
 long getPacketEnd()
          Returns the ending position of the packet in bytes.
 long getPacketLength()
          Returns the length of the packet in bytes as seen on the wire.
 int getPacketLengthRemaining()
          Returns the length of the packet in bytes.
 long getPacketSnaplen()
          Returns the length of the packet in bytes.
 long getPacketStart()
          Returns the beginning position of the packet in bytes.
 int getPaddingLength()
          Returns the length of padding at the end of the record.
 long getRecordEnd()
          Returns the position of the record end in the stream.
 PacketInputStream.BufferedHeader getRecordHeader()
           
protected  long getRecordHeaderLength()
          Returns the length of the record header excluding the rest of the packet.
 long getRecordLength()
          Returns the length of the packet in bytes.
protected  long getRecordStart()
          Returns the start of the packet record in the stream including all the headers.
protected  void goToEndOfRecord()
          A special method that forwards the current position in the stream to the end of the packet.
 boolean gotoMark(java.lang.String markName)
           
protected  boolean hasForceRead()
          Indicates the state of the forceRead flag.
static java.lang.String hex(int b)
          A little internal conversion utility function to print int in hex.
protected  void initPacketStream()
          Read stream ID data.
protected  void initRecordHeader()
          Read pre-packet header from stream.
 boolean isCaptureLive()
          Idicates wheather this capture stream is from a live source or offline or file data.
 boolean isCompressed()
           
 boolean isDebugHeaders()
           
 boolean isPacketReady()
          Returns a flag indicating if stream is ready for packet processing.
protected  boolean isReady()
          Returns a flag indicating if stream is ready for packet processing.
protected  boolean isReady(int len)
          Returns a flag indicating if stream is ready for packet processing.
static void main(java.lang.String[] args)
          Test function for PacketInputStream
 void nextPacket()
          Prepares the stream for the next packet in the stream.
 void pop()
          Pops previously pushed position off of a stack and rewids the stream.
 long position()
          protected accessible method that reports the position within the overall stream.
static void printStream(java.io.InputStream in, int printCount)
          Dumps the contents of InputStream in hex.
 void push()
          Pushes the current position in the stream onto a stack.
 void push(java.lang.String markName)
          Pushes the current position in the stream onto a stack and mark's it with a name.
 int read()
          Read packet data from the stream.
protected  void setCaptureTimestamp(java.sql.Timestamp captureTimestamp)
          Sets the current capture timestamp.
 void setCompressed(boolean compressed)
           
 void setDebugHeaders(boolean debugHeaders)
           
protected  void setForceRead(boolean status)
          Allow reads by force.
 void setIncludePadding(boolean includePaddingFlag)
          Makes the stream also return any padding as packet data.
protected  void setLinkType(java.lang.String linkType)
          Sets the First Header in the packet, usually a Data link layer, but not neccessarily.
protected  void setPacketLength(long length)
          Sets the original packet length as seen on the network wire.
protected  void setPacketSnaplen(long length)
          Sets the trunkated packet length during the capture.
 void setPacketStart(long start)
          Sets the start of the packet in the stream.
protected  void setRecordLength(long length)
          Sets the trunkated packet length during the capture.
protected  void setRecordStart(long start)
          This is an unusual call, since record start and record end are normally computed by call to nextPacket() method which advances the stream to the beginning of the next record and after reading of the pre-header to the beginning of the packet in the stream.
 long skip(long bytes)
          Skip ahead.
 
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

stackIn

public BitStackInputStream stackIn
Reference to stack input so we can push() and pop() positions witin.


packetLength

protected long packetLength
Length of the captured packet.


packetSnaplen

protected long packetSnaplen

packetStart

protected long packetStart

packetEnd

protected long packetEnd

recordLength

protected long recordLength

recordStart

protected long recordStart

recordEnd

protected long recordEnd

debugHeaders

protected boolean debugHeaders

fileHeader

protected PacketInputStream.BufferedHeader fileHeader

recordHeader

protected PacketInputStream.BufferedHeader recordHeader

packetCaptureTimestamp

protected java.sql.Timestamp packetCaptureTimestamp
Capture time of the packet.


captureDeviceIp

protected IpAddress captureDeviceIp
Capture device IP address. Initialize to default in the constructor.


captureDeviceOS

protected java.lang.String captureDeviceOS
Capture device OS.


captureDeviceArch

protected java.lang.String captureDeviceArch
Capture device hardware architecture.


captureDeviceFilename

protected java.lang.String captureDeviceFilename
Name of the file or interface the capture occured. If known.


captureLive

protected boolean captureLive
Flag indicating if the stream is comming from a live capture or a pre-captured file.


linkType

protected java.lang.String linkType
Constructor Detail

PacketInputStream

public PacketInputStream(BitStackInputStream inputStream)
                  throws java.io.IOException,
                         EOPacketStream,
                         StreamFormatException
Parameters:
-
Throws:
java.io.IOException
EOPacketStream
StreamFormatException
Method Detail

getLinkType

public java.lang.String getLinkType()
Returns the name of the first header in the packet.


setLinkType

protected void setLinkType(java.lang.String linkType)
Sets the First Header in the packet, usually a Data link layer, but not neccessarily.

Parameters:
linkType - First Header in the packet, usually a Data Link frame type.

getRecordStart

protected long getRecordStart()
Returns the start of the packet record in the stream including all the headers. This includes the capture file's packet pre-header plus the included packet as well.


setRecordStart

protected void setRecordStart(long start)
This is an unusual call, since record start and record end are normally computed by call to nextPacket() method which advances the stream to the beginning of the next record and after reading of the pre-header to the beginning of the packet in the stream.

This method may be used to rewiding the position in the stream to a previous record's start position.

This method also resets the record length and record end properties. So they should be set appriopriately after words.

Parameters:
start - start position in the stream.

getRecordLength

public long getRecordLength()
Returns the length of the packet in bytes. This is the Snapped length value. Where the packet got chopped off.


getRecordEnd

public long getRecordEnd()
Returns the position of the record end in the stream.


setRecordLength

protected void setRecordLength(long length)
Sets the trunkated packet length during the capture. If the packet was trunkated at the time of the capture, this length will be less then the length returned by getPacketLength(). Otherwise they should be the same.

The method also sets the record end property using the setRecordEnd() method.


getRecordHeaderLength

protected long getRecordHeaderLength()
Returns the length of the record header excluding the rest of the packet.

Returns:
length of the header portion of the record.

getPacketLength

public long getPacketLength()
Returns the length of the packet in bytes as seen on the wire.


setPacketLength

protected void setPacketLength(long length)
Sets the original packet length as seen on the network wire.

Parameters:
length - length in bytes of packet as seen on the wire.

getPacketSnaplen

public long getPacketSnaplen()
Returns the length of the packet in bytes. This is the Snapped length value. Where the packet got chopped off.


setPacketSnaplen

protected void setPacketSnaplen(long length)
Sets the trunkated packet length during the capture. If the packet was trunkated at the time of the capture, this length will be less then the length returned by getPacketLength(). Otherwise they should be the same.

This method also sets the packet end property using the setPacketEnd() method. Snaplen is used to determine the packet end in the stream since getPacketLength() may not be what is actually in the capture stream due to trunkation of the packet.

Parameters:
length - length of the packet record in the capture file or stream.

getPacketStart

public long getPacketStart()
Returns the beginning position of the packet in bytes.


setPacketStart

public void setPacketStart(long start)
Sets the start of the packet in the stream. This is used to calculate the packet end so that packet boundary conditions can be enforced.

The call to this method resets the packet length and packet end properties. They should be set by a call to setPacketSnaplen() method which will set them appropriately.


getPacketEnd

public long getPacketEnd()
Returns the ending position of the packet in bytes.


getPacketLengthRemaining

public int getPacketLengthRemaining()
Returns the length of the packet in bytes.


getCaptureTimestamp

public java.sql.Timestamp getCaptureTimestamp()
returns the time the packet was captured.


setCaptureTimestamp

protected void setCaptureTimestamp(java.sql.Timestamp captureTimestamp)
Sets the current capture timestamp. Timestamp is the time at which the packet was captured off of the wire.


getCaptureDeviceAddress

public IpAddress getCaptureDeviceAddress()
Return IP address of the device the stream originated on or if known where the capture occured.


getCaptureDeviceOS

public java.lang.String getCaptureDeviceOS()
Return OS name of the device the stream originated or if know where the capture occured.


getCaptureDeviceArch

public java.lang.String getCaptureDeviceArch()
Return OS name of the device the stream originated or if know where the capture occured.


getCaptureDeviceFilename

public java.lang.String getCaptureDeviceFilename()
Return filename name or interface name of the source of this data.


getCaptureDeviceIp

public IpAddress getCaptureDeviceIp()
Return Ip address of the source of this data. data.


isCaptureLive

public boolean isCaptureLive()
Idicates wheather this capture stream is from a live source or offline or file data. If data was captured previously and saved then false will be returned. Reading from a system interface would return "true".


initPacketStream

protected void initPacketStream()
                         throws java.io.IOException,
                                EOPacketStream,
                                StreamFormatException
Read stream ID data. This is only called once during the stream initialization and no more.

Throws:
java.io.IOException
EOPacketStream
StreamFormatException

initRecordHeader

protected void initRecordHeader()
                         throws java.io.IOException,
                                EOPacketStream,
                                StreamFormatException
Read pre-packet header from stream. With basic info about the next packet to follow. this is called for every packet in the stream. Main purpose is to get packet-data length (or length of captured packet) and the capture time of the packet.

Throws:
java.io.IOException
EOPacketStream
StreamFormatException

push

public void push()
Pushes the current position in the stream onto a stack. The position can be restored and stream rewound by using pop().


push

public void push(java.lang.String markName)
Pushes the current position in the stream onto a stack and mark's it with a name.


gotoMark

public boolean gotoMark(java.lang.String markName)

pop

public void pop()
Pops previously pushed position off of a stack and rewids the stream.


clear

public void clear()
Clears previously pushed position off of a stack and without rewinding the stream.


setForceRead

protected void setForceRead(boolean status)
Allow reads by force. Normally read() method using the ProtocolInputStream object is reserved for reading packet data. In certain cases in order to proccess the the ProtocolStream jnetstream between ProtocolInputStream and ProtocolOutputStream it is neccessary to allow the read() operation to be performed. This is very implementation specific and is not accessable as a public method. Remember to always leave this at false when not required any more. If you leave it true all the time then reading of just the packet data using read() method won't be enforced and would cause all kinds of nasty side effects. Such as EOPacket exception not thrown.

Remember to catch IOException and reset this flag if it has been set to true otherwise an exception thrown might cause this flag to be "true" and break the function of read() method.


hasForceRead

protected boolean hasForceRead()
Indicates the state of the forceRead flag. If forceRead is set, then packet boundaries are ignored by this stream and you can read from any position without throwing an exception. This is used internally to read headers outside of the normal packet data boundary.

Returns:
true if set, false if not set.
See Also:
setForceRead(boolean status)

read

public int read()
         throws java.io.IOException
Read packet data from the stream. Only allow a read after nextPacket() has been called and there is still data in the packet inputstream to read. A EOPacket exception is thrown and can be used as an indication that the end of current packet-data has been reached. Even though the packet jnetstream header may indicate that there should be more data to read,

Overrides:
read in class ProtocolDataInputStream
Returns:
A byte of data from actual packet data.
Throws:
java.io.IOException - Any problems with the stream IO.

nextPacket

public void nextPacket()
                throws java.io.IOException,
                       EOPacketStream,
                       StreamFormatException
Prepares the stream for the next packet in the stream. This method must be called the first time stream is used and after all of the data in current packet being processed is exhausted. The method can also be called in the middle of data processing before entire contents of the packet-data are read. The underlying stream will be advanced to the beginning of the next packet and all unread data will be skipped.

The read() method can not be called the nextPacket() method was used to prapare and advance the stream to beginning of packet-data. A EOPacket exception will be thrown by the read() method without the nextPacket() call first.

Throws:
java.io.IOException
EOPacketStream
StreamFormatException

goToEndOfRecord

protected void goToEndOfRecord()
                        throws java.io.IOException,
                               StreamFormatException
A special method that forwards the current position in the stream to the end of the packet. After this method call it will be possible to start on the next packet or if pop() occured this method can be called again. This method is not public accessible. nextPacket() method should be used to advance to the next packet even if there is still data in existing packet-data stream.

Throws:
java.io.IOException
StreamFormatException

skip

public long skip(long bytes)
          throws java.io.IOException
Skip ahead.

Overrides:
skip in class java.io.InputStream
Throws:
java.io.IOException

position

public long position()
protected accessible method that reports the position within the overall stream.


bitsLeft

public int bitsLeft()

isPacketReady

public boolean isPacketReady()
Returns a flag indicating if stream is ready for packet processing.

Returns:
if true you can call the read() method.

setIncludePadding

public void setIncludePadding(boolean includePaddingFlag)
Makes the stream also return any padding as packet data. Normally any padding after the packet data used for alignment is skipped. By setting this flag the padding data is also returned. You must use the getPacketSnaplen() to determine the exact length of packet data and getPaddingLength().

EOPacket exception will not be thrown when the packet data boundary is reached, but will be thrown when the record data boundary is.

Parameters:
includePaddingFlag - If true, padding will be included when reading from the stream. If false, padding will not be included and skipped during the next nextPacket() call.

getPaddingLength

public int getPaddingLength()
Returns the length of padding at the end of the record. Padding is normally used for alignment of data onto convenient byte boundaries.


isReady

protected boolean isReady()
                   throws EOPacket
Returns a flag indicating if stream is ready for packet processing.

Specified by:
isReady in class ProtocolDataInputStream
Returns:
if true you can call the read() method.
Throws:
EOPacket - is trown if stream is not ready.

isReady

protected boolean isReady(int len)
                   throws EOPacket
Returns a flag indicating if stream is ready for packet processing.

Specified by:
isReady in class ProtocolDataInputStream
Returns:
This method always returns true. If boundary check fails an exception is thrown instead of returning false.
Throws:
EOPacket - is trown if stream is not ready.

isDebugHeaders

public boolean isDebugHeaders()
Returns:
Returns the debugHeaders.

setDebugHeaders

public void setDebugHeaders(boolean debugHeaders)
Parameters:
debugHeaders - The debugHeaders to set.

getFileHeader

public PacketInputStream.BufferedHeader getFileHeader()
Returns:
Returns the fileHeader.

getRecordHeader

public PacketInputStream.BufferedHeader getRecordHeader()
Returns:
Returns the recordHeader.

main

public static void main(java.lang.String[] args)
Test function for PacketInputStream

Parameters:
args - command line arguments

printStream

public static void printStream(java.io.InputStream in,
                               int printCount)
                        throws java.io.IOException
Dumps the contents of InputStream in hex.

Throws:
java.io.IOException

hex

public static java.lang.String hex(int b)
A little internal conversion utility function to print int in hex. A leading 0 is prepended to make HEX chars 2 characters long.


setCompressed

public void setCompressed(boolean compressed)
Parameters:
b -

isCompressed

public boolean isCompressed()
Returns:
Returns the compressed.

getFileFormatName

public java.lang.String getFileFormatName()