Package jpcap

Class JpcapCaptor

java.lang.Object
jpcap.JpcapCaptor

public class JpcapCaptor extends Object
This class is used to capture packets or read packets from a captured file.
  • Field Details

    • received_packets

      public int received_packets
      Number of received packets
      See Also:
    • dropped_packets

      public int dropped_packets
      Number of dropped packets
      See Also:
  • Method Details

    • getDeviceList

      public static NetworkInterface[] getDeviceList()
      Returns the interfaces that can be used for capturing.
      Returns:
      List of Interface objects
    • openDevice

      public static JpcapCaptor openDevice(NetworkInterface intrface, int snaplen, boolean promisc, int to_ms) throws IOException
      Opens the specified network interface, and returns an instance of this class.
      Parameters:
      intrface - The network interface to capture packets
      snaplen - Max number of bytes captured at once
      promisc - If true, the inferface becomes promiscuous mode
      to_ms - Timeout of processPacket(). Not all platforms support a timeout; on platforms that don't, the timeout is ignored. On platforms that support a timeout, a zero value will cause Jpcap to wait forever to allow enough packets to arrive, with no timeout.
      Returns:
      an instance of this class Jpcap.
      Throws:
      IOException - Raised when the specified interface cannot be opened
    • openFile

      public static JpcapCaptor openFile(String filename) throws IOException
      Opens a dump file created by tcpdump or Ethereal, and returns an instance of this class.
      Parameters:
      filename - File name of the dump file
      Returns:
      an instance of this class Jpcap
      Throws:
      IOException - If the file cannot be opened
    • close

      public void close()
      Closes the opened interface of dump file.
    • getPacket

      public Packet getPacket()
      Captures a single packet.
      Returns:
      a captured packet.
      null if an error occured or timeout has elapsed.
      Packet.EOF is EOF was reached when reading from a offline file.
    • processPacket

      public int processPacket(int count, PacketReceiver handler)
      Captures the specified number of packets consecutively.
      Unlike loopPacket(), this method returns (althrough not guaranteed) when the timeout expires. Also, in "non-blocking" mode, this method returns immediately when there is no packet to capture.
      Parameters:
      count - Number of packets to be captured
      You can specify -1 to capture packets parmanently until timeour, EOF or an error occurs.
      handler - an instnace of JpcapHandler that analyzes the captured packets
      Returns:
      Number of captured packets
    • loopPacket

      public int loopPacket(int count, PacketReceiver handler)
      Captures the specified number of packets consecutively.

      Unlike processPacket(), this method ignores the timeout. This method also does not support "non-blocking" mode.

      Parameters:
      count - Number of packets to be captured
      You can specify -1 to capture packets parmanently until EOF or an error occurs.
      handler - an instnace of JpcapHandler that analyzes the captured packets
      Returns:
      Number of captured packets
    • dispatchPacket

      @Deprecated public int dispatchPacket(int count, PacketReceiver handler)
      Deprecated.
    • setNonBlockingMode

      public void setNonBlockingMode(boolean nonblocking)
      Sets/unsets "non-blocking" mode
      Parameters:
      nonblocking - TRUE to set "non-blocking" mode. FALSE to set "blocking" mode
    • isNonBlockinMode

      public boolean isNonBlockinMode()
      Checks if the current setting is in "non-blocking" mode or not.
      Returns:
      TRUE if it is in "non-blocking" mode. FALSE otherwise.
    • breakLoop

      public void breakLoop()
      Set a flag that will force processPacket() and loopPacket() to return rather than looping.

      Note that processPacket() and loopPacket() will not return after this flag is set UNTIL a packet is received or a read timeout occurs. By default, there is no read timeout. See comments in setPacketReadTimeout().

    • setPacketReadTimeout

      public boolean setPacketReadTimeout(int millis)
      Sets the socket read timeout (SO_RCVTIMEO) for the socket used to read packets from the kernel. Setting this timeout is useful if using processPacket() or loopPacket() in blocking mode and you expect breakLoop() to work. breakLoop() will only have an effect if (a) you are actually getting packets or (b) if the read on the socket times out occasionally.

      This is currently only supported on UNIX.

      Parameters:
      millis - Timeout in milliseconds; 0 for no timeout.
      Returns:
      true upon success; false upon failure or if unsupported.
    • getPacketReadTimeout

      public int getPacketReadTimeout()
      Returns the socket read timeout (SO_RCVTIMEO) for the socket used to read packets from the kernel.

      This is currently only supported on UNIX.

      Returns:
      Read timeout in milliseconds; 0 for no timeout; -1 if an error occurred or this feature is unsupported.
    • setFilter

      public void setFilter(String condition, boolean optimize) throws IOException
      Sets a filter. This filter is same as tcpdump.
      Parameters:
      condition - a string representation of the filter
      optimize - If true, the filter is optimized
      Throws:
      IOException - Raised if the filter condition cannot be compiled or installed
    • updateStat

      public void updateStat()
    • getErrorMessage

      public String getErrorMessage()
      Returns an error message
      Returns:
      error message
    • getJpcapSenderInstance

      public JpcapSender getJpcapSenderInstance()
      Obtains an instance of JpcapSender that uses the same interface to send packets. You can use this method only if you opened an interface with openDevice() method.
      Returns:
      returns an instance of JpcapSender