Class PcapHandle
java.lang.Object
org.pcap4j.core.PcapHandle
- All Implemented Interfaces:
Closeable, AutoCloseable
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumstatic final classThis class is used to open (i.e.private final classprivate final classstatic enumDirection of packets.private static final classstatic enumstatic enum -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final Objectprivate DataLinkTypeprivate Stringprivate final com.sun.jna.Pointerprivate final ReentrantReadWriteLockprivate static final org.slf4j.Loggerprivate booleanprivate final ThreadLocal<Integer> static final Inet4AddressThe netmask used forsetFilter(String, BpfProgram.BpfCompileMode, Inet4Address)orcompileFilter(String, BpfProgram.BpfCompileMode, Inet4Address)when you don't know what netmask you should use.private final PcapHandle.TimestampPrecisionprivate final ThreadLocal<Timestamp> -
Constructor Summary
ConstructorsModifierConstructorDescription(package private)PcapHandle(com.sun.jna.Pointer handle, PcapHandle.TimestampPrecision timestampPrecision) privatePcapHandle(PcapHandle.Builder builder) -
Method Summary
Modifier and TypeMethodDescriptionvoidBreaks a loop which this handle is working on.private TimestampbuildTimestamp(com.sun.jna.Pointer header) voidclose()Closes this PcapHandle.compileFilter(String bpfExpression, BpfProgram.BpfCompileMode mode, Inet4Address netmask) intdispatch(int packetCount, PacketListener listener) intdispatch(int packetCount, PacketListener listener, Executor executor) intdispatch(int packetCount, RawPacketListener listener) intdispatch(int packetCount, RawPacketListener listener, Executor executor) private intdoDispatch(int packetCount, NativeMappings.pcap_handler handler) private voiddoLoop(int packetCount, NativeMappings.pcap_handler handler) getDlt()private DataLinkTypegetError()intintbyte[]byte[]intgetStats()booleanisOpen()voidloop(int packetCount, PacketListener listener) A wrapper method forint pcap_loop(pcap_t *, int, pcap_handler, u_char *).voidloop(int packetCount, PacketListener listener, Executor executor) A wrapper method forint pcap_loop(pcap_t *, int, pcap_handler, u_char *).voidloop(int packetCount, PcapDumper dumper) voidloop(int packetCount, RawPacketListener listener) A wrapper method forint pcap_loop(pcap_t *, int, pcap_handler, u_char *).voidloop(int packetCount, RawPacketListener listener, Executor executor) A wrapper method forint pcap_loop(pcap_t *, int, pcap_handler, u_char *).voidsendPacket(byte[] bytes) voidsendPacket(byte[] bytes, int len) voidsendPacket(Packet packet) voidvoidsetDirection(PcapHandle.PcapDirection direction) Set direction flag, which controls whether we accept only incoming packets, only outgoing packets, or both.voidsetDlt(DataLinkType dlt) voidsetFilter(String bpfExpression, BpfProgram.BpfCompileMode mode) voidsetFilter(String bpfExpression, BpfProgram.BpfCompileMode mode, Inet4Address netmask) voidsetFilter(BpfProgram prog) toString()
-
Field Details
-
logger
private static final org.slf4j.Logger logger -
dlt
-
timestampPrecision
-
handle
private final com.sun.jna.Pointer handle -
timestamps
-
originalLengths
-
handleLock
-
compileLock
-
open
private volatile boolean open -
filteringExpression
-
PCAP_NETMASK_UNKNOWN
The netmask used forsetFilter(String, BpfProgram.BpfCompileMode, Inet4Address)orcompileFilter(String, BpfProgram.BpfCompileMode, Inet4Address)when you don't know what netmask you should use.
-
-
Constructor Details
-
PcapHandle
PcapHandle(com.sun.jna.Pointer handle, PcapHandle.TimestampPrecision timestampPrecision) -
PcapHandle
- Throws:
PcapNativeException
-
-
Method Details
-
getDltByNative
-
getDlt
- Returns:
- the Data Link Type of this PcapHandle
-
setDlt
- Parameters:
dlt- aDataLinkTypeobject to set- Throws:
PcapNativeException- if an error occurs in the pcap native library.NotOpenException- if this PcapHandle is not open.
-
isOpen
public boolean isOpen()- Returns:
- true if this PcapHandle object is open (i.e. not yet closed by
close()); false otherwise.
-
getFilteringExpression
- Returns:
- the filtering expression of this PcapHandle
-
getTimestampPrecision
- Returns:
- Timestamp precision
-
setDirection
public void setDirection(PcapHandle.PcapDirection direction) throws PcapNativeException, NotOpenException Set direction flag, which controls whether we accept only incoming packets, only outgoing packets, or both. Note that, depending on the platform, some or all direction arguments might not be supported.- Parameters:
direction- direction to set.- Throws:
PcapNativeException- if an error occurs in the pcap native library.NotOpenException- if this PcapHandle is not open.
-
getTimestamp
- Returns:
- the timestamp of the last packet captured by this handle in the current thread.
-
getOriginalLength
- Returns:
- the original length of the last packet captured by this handle in the current thread.
-
getSnapshot
- Returns:
- the dimension of the packet portion (in bytes) that is delivered to the application.
- Throws:
NotOpenException- if this PcapHandle is not open.
-
isSwapped
- Returns:
- a
SwappedTypeobject. - Throws:
NotOpenException- if this PcapHandle is not open.
-
getMajorVersion
- Returns:
- the major version number of the pcap library used to write the savefile.
- Throws:
NotOpenException- if this PcapHandle is not open.
-
getMinorVersion
- Returns:
- the minor version number of the pcap library used to write the savefile.
- Throws:
NotOpenException- if this PcapHandle is not open.
-
compileFilter
public BpfProgram compileFilter(String bpfExpression, BpfProgram.BpfCompileMode mode, Inet4Address netmask) throws PcapNativeException, NotOpenException - Parameters:
bpfExpression- bpfExpressionmode- modenetmask- netmask- Returns:
- a
BpfProgramobject. - Throws:
PcapNativeException- if an error occurs in the pcap native library.NotOpenException- if this PcapHandle is not open.
-
setFilter
public void setFilter(String bpfExpression, BpfProgram.BpfCompileMode mode, Inet4Address netmask) throws PcapNativeException, NotOpenException - Parameters:
bpfExpression- bpfExpressionmode- modenetmask- netmask- Throws:
PcapNativeException- if an error occurs in the pcap native library.NotOpenException- if this PcapHandle is not open.NullPointerException- if any of arguments are null.
-
setFilter
public void setFilter(String bpfExpression, BpfProgram.BpfCompileMode mode) throws PcapNativeException, NotOpenException - Parameters:
bpfExpression- bpfExpressionmode- mode- Throws:
PcapNativeException- if an error occurs in the pcap native library.NotOpenException- if this PcapHandle is not open.NullPointerException- if any of arguments are null.
-
setFilter
- Parameters:
prog- prog- Throws:
PcapNativeException- if an error occurs in the pcap native library.NotOpenException- if this PcapHandle is not open.NullPointerException- if any of arguments are null.
-
setBlockingMode
public void setBlockingMode(PcapHandle.BlockingMode mode) throws PcapNativeException, NotOpenException - Parameters:
mode- mode- Throws:
PcapNativeException- if an error occurs in the pcap native library.NotOpenException- if this PcapHandle is not open.
-
getBlockingMode
- Returns:
- blocking mode
- Throws:
PcapNativeException- if an error occurs in the pcap native library.NotOpenException- if this PcapHandle is not open.
-
getNextPacket
- Returns:
- a Packet object created from a captured packet using the packet factory. May be null.
- Throws:
NotOpenException- if this PcapHandle is not open.
-
getNextRawPacket
- Returns:
- a captured packet. May be null.
- Throws:
NotOpenException- if this PcapHandle is not open.
-
getNextPacketEx
public Packet getNextPacketEx() throws PcapNativeException, EOFException, TimeoutException, NotOpenException- Returns:
- a Packet object created from a captured packet using the packet factory. Not null.
- Throws:
PcapNativeException- if an error occurs in the pcap native library.EOFException- if packets are being read from a pcap file and there are no more packets to read from the file.TimeoutException- if packets are being read from a live capture and the timeout expired.NotOpenException- if this PcapHandle is not open.
-
getNextRawPacketEx
public byte[] getNextRawPacketEx() throws PcapNativeException, EOFException, TimeoutException, NotOpenException- Returns:
- a captured packet. Not null.
- Throws:
PcapNativeException- if an error occurs in the pcap native library.EOFException- if packets are being read from a pcap file and there are no more packets to read from the file.TimeoutException- if packets are being read from a live capture and the timeout expired.NotOpenException- if this PcapHandle is not open.
-
loop
public void loop(int packetCount, PacketListener listener) throws PcapNativeException, InterruptedException, NotOpenException A wrapper method forint pcap_loop(pcap_t *, int, pcap_handler, u_char *). This method creates a Packet object from a captured packet using the packet factory and passes it tolistener.gotPacket(Packet). When a packet is captured,listener.gotPacket(Packet)is called in the thread which called theloop(). And then this PcapHandle waits for the thread to return from thegotPacket()before it retrieves the next packet from the pcap buffer.- Parameters:
packetCount- the number of packets to capture. -1 is equivalent to infinity. 0 may result in different behaviors between platforms and pcap library versions.listener- listener- Throws:
PcapNativeException- if an error occurs in the pcap native library.InterruptedException- if the loop terminated due to a call tobreakLoop().NotOpenException- if this PcapHandle is not open.
-
loop
public void loop(int packetCount, PacketListener listener, Executor executor) throws PcapNativeException, InterruptedException, NotOpenException A wrapper method forint pcap_loop(pcap_t *, int, pcap_handler, u_char *). This method creates a Packet object from a captured packet using the packet factory and passes it tolistener.gotPacket(Packet). When a packet is captured, theexecutor.execute()is called with a Runnable object in the thread which called theloop(). Then, the Runnable object callslistener.gotPacket(Packet). Iflistener.gotPacket(Packet)is expected to take a long time to process a packet, this method should be used with a proper executor instead ofloop(int, PacketListener)in order to prevent the pcap buffer from overflowing.- Parameters:
packetCount- the number of packets to capture. -1 is equivalent to infinity. 0 may result in different behaviors between platforms and pcap library versions.listener- listenerexecutor- executor- Throws:
PcapNativeException- if an error occurs in the pcap native library.InterruptedException- if the loop terminated due to a call tobreakLoop().NotOpenException- if this PcapHandle is not open.
-
loop
public void loop(int packetCount, RawPacketListener listener) throws PcapNativeException, InterruptedException, NotOpenException A wrapper method forint pcap_loop(pcap_t *, int, pcap_handler, u_char *). When a packet is captured,listener.gotPacket(byte[])is called in the thread which called theloop(). And then this PcapHandle waits for the thread to return from thegotPacket()before it retrieves the next packet from the pcap buffer.- Parameters:
packetCount- the number of packets to capture. -1 is equivalent to infinity. 0 may result in different behaviors between platforms and pcap library versions.listener- listener- Throws:
PcapNativeException- if an error occurs in the pcap native library.InterruptedException- if the loop terminated due to a call tobreakLoop().NotOpenException- if this PcapHandle is not open.
-
loop
public void loop(int packetCount, RawPacketListener listener, Executor executor) throws PcapNativeException, InterruptedException, NotOpenException A wrapper method forint pcap_loop(pcap_t *, int, pcap_handler, u_char *). When a packet is captured, theexecutor.execute()is called with a Runnable object in the thread which called theloop(). Then, the Runnable object callslistener.gotPacket(byte[]). Iflistener.gotPacket(byte[])is expected to take a long time to process a packet, this method should be used with a proper executor instead ofloop(int, RawPacketListener)in order to prevent the pcap buffer from overflowing.- Parameters:
packetCount- the number of packets to capture. -1 is equivalent to infinity. 0 may result in different behaviors between platforms and pcap library versions.listener- listenerexecutor- executor- Throws:
PcapNativeException- if an error occurs in the pcap native library.InterruptedException- if the loop terminated due to a call tobreakLoop().NotOpenException- if this PcapHandle is not open.
-
doLoop
private void doLoop(int packetCount, NativeMappings.pcap_handler handler) throws PcapNativeException, InterruptedException, NotOpenException -
dispatch
public int dispatch(int packetCount, PacketListener listener) throws PcapNativeException, InterruptedException, NotOpenException - Parameters:
packetCount- the maximum number of packets to process. If -1 is specified, all the packets in the pcap buffer or pcap file will be processed before returning. 0 may result in different behaviors between platforms and pcap library versions.listener- listener- Returns:
- the number of captured packets.
- Throws:
PcapNativeException- if an error occurs in the pcap native library.InterruptedException- if the loop terminated due to a call tobreakLoop().NotOpenException- if this PcapHandle is not open.
-
dispatch
public int dispatch(int packetCount, PacketListener listener, Executor executor) throws PcapNativeException, InterruptedException, NotOpenException - Parameters:
packetCount- the maximum number of packets to process. If -1 is specified, all the packets in the pcap buffer or pcap file will be processed before returning. 0 may result in different behaviors between platforms and pcap library versions.listener- listenerexecutor- executor- Returns:
- the number of captured packets.
- Throws:
PcapNativeException- if an error occurs in the pcap native library.InterruptedException- if the loop terminated due to a call tobreakLoop().NotOpenException- if this PcapHandle is not open.
-
dispatch
public int dispatch(int packetCount, RawPacketListener listener) throws PcapNativeException, InterruptedException, NotOpenException - Parameters:
packetCount- the maximum number of packets to process. If -1 is specified, all the packets in the pcap buffer or pcap file will be processed before returning. 0 may result in different behaviors between platforms and pcap library versions.listener- listener- Returns:
- the number of captured packets.
- Throws:
PcapNativeException- if an error occurs in the pcap native library.InterruptedException- if the loop terminated due to a call tobreakLoop().NotOpenException- if this PcapHandle is not open.
-
dispatch
public int dispatch(int packetCount, RawPacketListener listener, Executor executor) throws PcapNativeException, InterruptedException, NotOpenException - Parameters:
packetCount- the maximum number of packets to process. If -1 is specified, all the packets in the pcap buffer or pcap file will be processed before returning. 0 may result in different behaviors between platforms and pcap library versions.listener- listenerexecutor- executor- Returns:
- the number of captured packets.
- Throws:
PcapNativeException- if an error occurs in the pcap native library.InterruptedException- if the loop terminated due to a call tobreakLoop().NotOpenException- if this PcapHandle is not open.
-
doDispatch
private int doDispatch(int packetCount, NativeMappings.pcap_handler handler) throws PcapNativeException, InterruptedException, NotOpenException -
dumpOpen
- Parameters:
filePath- "-" means stdout. The dlt of the PcapHandle which captured the packets you want to dump must be the same as this dlt.- Returns:
- an opened PcapDumper.
- Throws:
PcapNativeException- if an error occurs in the pcap native library.NotOpenException- if this PcapHandle is not open.
-
loop
public void loop(int packetCount, PcapDumper dumper) throws PcapNativeException, InterruptedException, NotOpenException - Parameters:
packetCount- packetCountdumper- dumper- Throws:
PcapNativeException- if an error occurs in the pcap native library.InterruptedException- if the loop terminated due to a call tobreakLoop().NotOpenException- if this PcapHandle is not open.
-
breakLoop
Breaks a loop which this handle is working on.The loop may not be broken immediately on some OSes because of buffering or something. As a workaround, letting this capture some bogus packets after calling this method may work.
- Throws:
NotOpenException- if this PcapHandle is not open.
-
sendPacket
- Parameters:
packet- packet- Throws:
PcapNativeException- if an error occurs in the pcap native library.NotOpenException- if this PcapHandle is not open.NullPointerException- if any of arguments are null.
-
sendPacket
- Parameters:
bytes- raw bytes- Throws:
PcapNativeException- if an error occurs in the pcap native library.NotOpenException- if this PcapHandle is not open.NullPointerException- if any of arguments are null.
-
sendPacket
- Parameters:
bytes- raw byteslen- length- Throws:
PcapNativeException- if an error occurs in the pcap native library.NotOpenException- if this PcapHandle is not open.NullPointerException- if any of arguments are null.
-
getStats
- Returns:
- a
PcapStatobject. - Throws:
PcapNativeException- if an error occurs in the pcap native library.NotOpenException- if this PcapHandle is not open.
-
listDatalinks
- Returns:
- a list of
DataLinkType - Throws:
PcapNativeException- if an error occurs in the pcap native library.NotOpenException- if this PcapHandle is not open.
-
getError
- Returns:
- an error message.
- Throws:
NotOpenException- if this PcapHandle is not open.
-
close
public void close()Closes this PcapHandle.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-
toString
-
buildTimestamp
-