- java.lang.Object
-
- org.pcap4j.core.PcapHandle
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable
public final class PcapHandle extends java.lang.Object implements java.io.CloseableA wrapper class for struct pcap_t.- Since:
- pcap4j 0.9.1
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classPcapHandle.BlockingModestatic classPcapHandle.BuilderThis class is used to open (i.e.private classPcapHandle.GotPacketFuncExecutorprivate classPcapHandle.GotRawPacketFuncExecutorstatic classPcapHandle.PcapDirectionDirection of packets.private static classPcapHandle.SimpleExecutorstatic classPcapHandle.SwappedTypestatic classPcapHandle.TimestampPrecision
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.ObjectcompileLockprivate DataLinkTypedltprivate java.lang.StringfilteringExpressionprivate com.sun.jna.Pointerhandleprivate java.util.concurrent.locks.ReentrantReadWriteLockhandleLockprivate static org.slf4j.Loggerloggerprivate booleanopenprivate java.lang.ThreadLocal<java.lang.Integer>originalLengthsstatic java.net.Inet4AddressPCAP_NETMASK_UNKNOWNThe netmask used forsetFilter(String, BpfProgram.BpfCompileMode, Inet4Address)orcompileFilter(String, BpfProgram.BpfCompileMode, Inet4Address)when you don't know what netmask you should use.private PcapHandle.TimestampPrecisiontimestampPrecisionprivate java.lang.ThreadLocal<java.sql.Timestamp>timestamps
-
Constructor Summary
Constructors Modifier Constructor Description (package private)PcapHandle(com.sun.jna.Pointer handle, PcapHandle.TimestampPrecision timestampPrecision)privatePcapHandle(PcapHandle.Builder builder)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidbreakLoop()Breaks a loop which this handle is working on.private java.sql.TimestampbuildTimestamp(com.sun.jna.Pointer header)voidclose()Closes this PcapHandle.BpfProgramcompileFilter(java.lang.String bpfExpression, BpfProgram.BpfCompileMode mode, java.net.Inet4Address netmask)intdispatch(int packetCount, PacketListener listener)intdispatch(int packetCount, PacketListener listener, java.util.concurrent.Executor executor)intdispatch(int packetCount, RawPacketListener listener)intdispatch(int packetCount, RawPacketListener listener, java.util.concurrent.Executor executor)private intdoDispatch(int packetCount, NativeMappings.pcap_handler handler)private voiddoLoop(int packetCount, NativeMappings.pcap_handler handler)PcapDumperdumpOpen(java.lang.String filePath)PcapHandle.BlockingModegetBlockingMode()DataLinkTypegetDlt()private DataLinkTypegetDltByNative()java.lang.StringgetError()java.lang.StringgetFilteringExpression()intgetMajorVersion()intgetMinorVersion()PacketgetNextPacket()PacketgetNextPacketEx()byte[]getNextRawPacket()byte[]getNextRawPacketEx()java.lang.IntegergetOriginalLength()intgetSnapshot()PcapStatgetStats()java.sql.TimestampgetTimestamp()PcapHandle.TimestampPrecisiongetTimestampPrecision()booleanisOpen()PcapHandle.SwappedTypeisSwapped()java.util.List<DataLinkType>listDatalinks()voidloop(int packetCount, PacketListener listener)A wrapper method forint pcap_loop(pcap_t *, int, pcap_handler, u_char *).voidloop(int packetCount, PacketListener listener, java.util.concurrent.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, java.util.concurrent.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)voidsetBlockingMode(PcapHandle.BlockingMode mode)voidsetDirection(PcapHandle.PcapDirection direction)Set direction flag, which controls whether we accept only incoming packets, only outgoing packets, or both.voidsetDlt(DataLinkType dlt)voidsetFilter(java.lang.String bpfExpression, BpfProgram.BpfCompileMode mode)voidsetFilter(java.lang.String bpfExpression, BpfProgram.BpfCompileMode mode, java.net.Inet4Address netmask)voidsetFilter(BpfProgram prog)java.lang.StringtoString()
-
-
-
Field Detail
-
logger
private static final org.slf4j.Logger logger
-
dlt
private volatile DataLinkType dlt
-
timestampPrecision
private final PcapHandle.TimestampPrecision timestampPrecision
-
handle
private final com.sun.jna.Pointer handle
-
timestamps
private final java.lang.ThreadLocal<java.sql.Timestamp> timestamps
-
originalLengths
private final java.lang.ThreadLocal<java.lang.Integer> originalLengths
-
handleLock
private final java.util.concurrent.locks.ReentrantReadWriteLock handleLock
-
compileLock
private static final java.lang.Object compileLock
-
open
private volatile boolean open
-
filteringExpression
private volatile java.lang.String filteringExpression
-
PCAP_NETMASK_UNKNOWN
public static final java.net.Inet4Address 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 Detail
-
PcapHandle
PcapHandle(com.sun.jna.Pointer handle, PcapHandle.TimestampPrecision timestampPrecision)
-
PcapHandle
private PcapHandle(PcapHandle.Builder builder) throws PcapNativeException
- Throws:
PcapNativeException
-
-
Method Detail
-
getDltByNative
private DataLinkType getDltByNative()
-
getDlt
public DataLinkType getDlt()
- Returns:
- the Data Link Type of this PcapHandle
-
setDlt
public void setDlt(DataLinkType dlt) throws PcapNativeException, NotOpenException
- 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
public java.lang.String getFilteringExpression()
- Returns:
- the filtering expression of this PcapHandle
-
getTimestampPrecision
public PcapHandle.TimestampPrecision 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
public java.sql.Timestamp getTimestamp()
- Returns:
- the timestamp of the last packet captured by this handle in the current thread.
-
getOriginalLength
public java.lang.Integer getOriginalLength()
- Returns:
- the original length of the last packet captured by this handle in the current thread.
-
getSnapshot
public int getSnapshot() throws NotOpenException- Returns:
- the dimension of the packet portion (in bytes) that is delivered to the application.
- Throws:
NotOpenException- if this PcapHandle is not open.
-
isSwapped
public PcapHandle.SwappedType isSwapped() throws NotOpenException
- Returns:
- a
SwappedTypeobject. - Throws:
NotOpenException- if this PcapHandle is not open.
-
getMajorVersion
public int getMajorVersion() throws NotOpenException- Returns:
- the major version number of the pcap library used to write the savefile.
- Throws:
NotOpenException- if this PcapHandle is not open.
-
getMinorVersion
public int getMinorVersion() throws NotOpenException- 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(java.lang.String bpfExpression, BpfProgram.BpfCompileMode mode, java.net.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(java.lang.String bpfExpression, BpfProgram.BpfCompileMode mode, java.net.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.java.lang.NullPointerException- if any of arguments are null.
-
setFilter
public void setFilter(java.lang.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.java.lang.NullPointerException- if any of arguments are null.
-
setFilter
public void setFilter(BpfProgram prog) throws PcapNativeException, NotOpenException
- Parameters:
prog- prog- Throws:
PcapNativeException- if an error occurs in the pcap native library.NotOpenException- if this PcapHandle is not open.java.lang.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
public PcapHandle.BlockingMode getBlockingMode() throws PcapNativeException, NotOpenException
- Returns:
- blocking mode
- Throws:
PcapNativeException- if an error occurs in the pcap native library.NotOpenException- if this PcapHandle is not open.
-
getNextPacket
public Packet getNextPacket() throws NotOpenException
- 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
public byte[] getNextRawPacket() throws NotOpenException- Returns:
- a captured packet. May be null.
- Throws:
NotOpenException- if this PcapHandle is not open.
-
getNextPacketEx
public Packet getNextPacketEx() throws PcapNativeException, java.io.EOFException, java.util.concurrent.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.java.io.EOFException- if packets are being read from a pcap file and there are no more packets to read from the file.java.util.concurrent.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, java.io.EOFException, java.util.concurrent.TimeoutException, NotOpenException- Returns:
- a captured packet. Not null.
- Throws:
PcapNativeException- if an error occurs in the pcap native library.java.io.EOFException- if packets are being read from a pcap file and there are no more packets to read from the file.java.util.concurrent.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, java.lang.InterruptedException, NotOpenExceptionA 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.java.lang.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, java.util.concurrent.Executor executor) throws PcapNativeException, java.lang.InterruptedException, NotOpenExceptionA 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.java.lang.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, java.lang.InterruptedException, NotOpenExceptionA 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.java.lang.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, java.util.concurrent.Executor executor) throws PcapNativeException, java.lang.InterruptedException, NotOpenExceptionA 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.java.lang.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, java.lang.InterruptedException, NotOpenException- Throws:
PcapNativeExceptionjava.lang.InterruptedExceptionNotOpenException
-
dispatch
public int dispatch(int packetCount, PacketListener listener) throws PcapNativeException, java.lang.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.java.lang.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, java.util.concurrent.Executor executor) throws PcapNativeException, java.lang.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.java.lang.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, java.lang.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.java.lang.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, java.util.concurrent.Executor executor) throws PcapNativeException, java.lang.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.java.lang.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, java.lang.InterruptedException, NotOpenException- Throws:
PcapNativeExceptionjava.lang.InterruptedExceptionNotOpenException
-
dumpOpen
public PcapDumper dumpOpen(java.lang.String filePath) throws PcapNativeException, NotOpenException
- 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, java.lang.InterruptedException, NotOpenException- Parameters:
packetCount- packetCountdumper- dumper- Throws:
PcapNativeException- if an error occurs in the pcap native library.java.lang.InterruptedException- if the loop terminated due to a call tobreakLoop().NotOpenException- if this PcapHandle is not open.
-
breakLoop
public void breakLoop() throws NotOpenExceptionBreaks 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
public void sendPacket(Packet packet) throws PcapNativeException, NotOpenException
- Parameters:
packet- packet- Throws:
PcapNativeException- if an error occurs in the pcap native library.NotOpenException- if this PcapHandle is not open.java.lang.NullPointerException- if any of arguments are null.
-
sendPacket
public void sendPacket(byte[] bytes) throws NotOpenException, PcapNativeException- Parameters:
bytes- raw bytes- Throws:
PcapNativeException- if an error occurs in the pcap native library.NotOpenException- if this PcapHandle is not open.java.lang.NullPointerException- if any of arguments are null.
-
sendPacket
public void sendPacket(byte[] bytes, int len) throws NotOpenException, PcapNativeException- Parameters:
bytes- raw byteslen- length- Throws:
PcapNativeException- if an error occurs in the pcap native library.NotOpenException- if this PcapHandle is not open.java.lang.NullPointerException- if any of arguments are null.
-
getStats
public PcapStat getStats() throws PcapNativeException, NotOpenException
- Returns:
- a
PcapStatobject. - Throws:
PcapNativeException- if an error occurs in the pcap native library.NotOpenException- if this PcapHandle is not open.
-
listDatalinks
public java.util.List<DataLinkType> listDatalinks() throws PcapNativeException, NotOpenException
- Returns:
- a list of
DataLinkType - Throws:
PcapNativeException- if an error occurs in the pcap native library.NotOpenException- if this PcapHandle is not open.
-
getError
public java.lang.String getError() throws NotOpenException- Returns:
- an error message.
- Throws:
NotOpenException- if this PcapHandle is not open.
-
close
public void close()
Closes this PcapHandle.- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
buildTimestamp
private java.sql.Timestamp buildTimestamp(com.sun.jna.Pointer header)
-
-