Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

com.voytechs.jnetstream.io
Class PacketInputStream  view PacketInputStream download PacketInputStream.java

java.lang.Object
  extended byjava.io.InputStream
      extended bycom.voytechs.jnetstream.io.ProtocolDataInputStream
          extended bycom.voytechs.jnetstream.io.PacketInputStream
All Implemented Interfaces:
java.io.Closeable
Direct Known Subclasses:
PcapInputStream, RawformatInputStream, SnoopInputStream

public 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


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  com.voytechs.jnetstream.primitive.address.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.
private static boolean debug
           
private  boolean forceRead
          Flag indicates that we as a stream are ready to process the packet data portion of the input stream.
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
           
private  long recordHeaderLength
           
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.
 com.voytechs.jnetstream.primitive.address.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.
 com.voytechs.jnetstream.primitive.address.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 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.
 long getRecordEnd()
          Returns the position of the record end in the stream.
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()
           
static java.lang.String hex(int b)
           
protected  void initPacketStream()
          Read stream ID data.
 boolean isCaptureLive()
          Idicates wheather this capture stream is from a live source or offline or file data.
 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)
           
 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 readPacketPreHeader()
          Read pre-packet header from stream.
protected  void setCaptureTimestamp(java.sql.Timestamp captureTimestamp)
          Sets the current capture timestamp.
protected  void setForceRead(boolean status)
          Allow reads by force.
protected  void setLinkType(java.lang.String linkType)
          Sets the First Header in the packet, usually a Data link layer, but not neccessarily.
private  void setPacketEnd(long end)
           
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.
private  void setRecordEnd(long end)
           
private  void setRecordHeaderLength(long length)
          Sets the length of the record header.
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, readFromPacket, readFromPacket, readInt, readIntLittleEndian, readLong, readLongLittleEndian, readShort, readShortLittleEndian, readUnsignedByte, readUnsignedInt, readUnsignedIntLittleEndian, 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

debug

private static final boolean debug
See Also:
Constant Field Values

stackIn

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


forceRead

private boolean forceRead
Flag indicates that we as a stream are ready to process the packet data portion of the input stream. First a call to nextPacket() must be made, otherwise we won't be able to read the packet specific pre-header.


packetLength

protected long packetLength
Length of the captured packet.


packetSnaplen

protected long packetSnaplen

packetStart

protected long packetStart

packetEnd

protected long packetEnd

recordHeaderLength

private long recordHeaderLength

recordLength

protected long recordLength

recordStart

protected long recordStart

recordEnd

protected long recordEnd

packetCaptureTimestamp

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


captureDeviceIp

protected com.voytechs.jnetstream.primitive.address.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
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.


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.


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.


setRecordEnd

private void setRecordEnd(long end)

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.


setRecordHeaderLength

private void setRecordHeaderLength(long length)
Sets the length of the record header. This method also advances the packet start position and end positions by this amount starting at record start. The packet end position could end up different from record end properties if there is a post header in the capture file or stream.


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.


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.


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.


setPacketEnd

private void setPacketEnd(long end)

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 com.voytechs.jnetstream.primitive.address.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 com.voytechs.jnetstream.primitive.address.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.


readPacketPreHeader

protected void readPacketPreHeader()
                            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.


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()

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

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.


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.


skip

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


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.


isReady

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

Specified by:
isReady in class ProtocolDataInputStream

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

main

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


printStream

public static void printStream(java.io.InputStream in,
                               int printCount)
                        throws java.io.IOException

hex

public static java.lang.String hex(int b)