Package jpcap
Class JpcapCaptor
java.lang.Object
jpcap.JpcapCaptor
This class is used to capture packets or read packets from a captured file.
-
Field Summary
FieldsModifier and TypeFieldDescriptionint
Number of dropped packetsint
Number of received packets -
Method Summary
Modifier and TypeMethodDescriptionvoid
Set a flag that will force processPacket() and loopPacket() to return rather than looping.void
close()
Closes the opened interface of dump file.int
dispatchPacket
(int count, PacketReceiver handler) Deprecated.static NetworkInterface[]
Returns the interfaces that can be used for capturing.Returns an error messageObtains an instance of JpcapSender that uses the same interface to send packets.Captures a single packet.int
Returns the socket read timeout (SO_RCVTIMEO) for the socket used to read packets from the kernel.boolean
Checks if the current setting is in "non-blocking" mode or not.int
loopPacket
(int count, PacketReceiver handler) Captures the specified number of packets consecutively.static JpcapCaptor
openDevice
(NetworkInterface intrface, int snaplen, boolean promisc, int to_ms) Opens the specified network interface, and returns an instance of this class.static JpcapCaptor
Opens a dump file created by tcpdump or Ethereal, and returns an instance of this class.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.void
Sets a filter.void
setNonBlockingMode
(boolean nonblocking) Sets/unsets "non-blocking" modeboolean
setPacketReadTimeout
(int millis) Sets the socket read timeout (SO_RCVTIMEO) for the socket used to read packets from the kernel.void
Updatesreceived_packets
anddropped_packets
.
-
Field Details
-
received_packets
public int received_packetsNumber of received packets- See Also:
-
dropped_packets
public int dropped_packetsNumber of dropped packets- See Also:
-
-
Method Details
-
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 packetssnaplen
- Max number of bytes captured at oncepromisc
- If true, the inferface becomes promiscuous modeto_ms
- Timeout ofprocessPacket()
. 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
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
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
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
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.Same as processPacket() -
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
Sets a filter. This filter is same as tcpdump.- Parameters:
condition
- a string representation of the filteroptimize
- If true, the filter is optimized- Throws:
IOException
- Raised if the filter condition cannot be compiled or installed
-
updateStat
public void updateStat()Updatesreceived_packets
anddropped_packets
. -
getErrorMessage
Returns an error message- Returns:
- error message
-
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
-