public final class PcapHandle
extends java.lang.Object
implements java.lang.AutoCloseable
| Modifier and Type | Class and Description |
|---|---|
static class |
PcapHandle.BlockingMode |
static class |
PcapHandle.Builder
This class is used to open (i.e.
|
static class |
PcapHandle.PcapDirection
Direction of packets.
|
static class |
PcapHandle.SwappedType |
static class |
PcapHandle.TimestampPrecision |
| Modifier and Type | Field and Description |
|---|---|
static java.net.Inet4Address |
PCAP_NETMASK_UNKNOWN
The netmask used for
setFilter(String, BpfProgram.BpfCompileMode, Inet4Address)
or compileFilter(String, BpfProgram.BpfCompileMode, Inet4Address) when you
don't know what netmask you should use. |
| Modifier and Type | Method and Description |
|---|---|
void |
breakLoop()
Breaks a loop which this handle is working on.
|
void |
close()
Closes this PcapHandle.
|
BpfProgram |
compileFilter(java.lang.String bpfExpression,
BpfProgram.BpfCompileMode mode,
java.net.Inet4Address netmask) |
int |
dispatch(int packetCount,
PacketListener listener) |
int |
dispatch(int packetCount,
PacketListener listener,
java.util.concurrent.Executor executor) |
PcapDumper |
dumpOpen(java.lang.String filePath) |
PcapHandle.BlockingMode |
getBlockingMode() |
DataLinkType |
getDlt() |
java.lang.String |
getError() |
java.lang.String |
getFilteringExpression() |
int |
getMajorVersion() |
int |
getMinorVersion() |
PcapPacket |
getNextPacket() |
PcapPacket |
getNextPacketEx() |
int |
getSnapshot() |
PcapStat |
getStats() |
PcapHandle.TimestampPrecision |
getTimestampPrecision() |
boolean |
isOpen() |
PcapHandle.SwappedType |
isSwapped() |
java.util.List<DataLinkType> |
listDatalinks() |
void |
loop(int packetCount,
PacketListener listener)
A wrapper method for
int pcap_loop(pcap_t *, int, pcap_handler, u_char *). |
void |
loop(int packetCount,
PacketListener listener,
java.util.concurrent.Executor executor)
A wrapper method for
int pcap_loop(pcap_t *, int, pcap_handler, u_char *). |
void |
loop(int packetCount,
PcapDumper dumper) |
void |
sendPacket(byte[] bytes) |
void |
sendPacket(byte[] bytes,
int len) |
void |
sendPacket(Packet packet) |
void |
setBlockingMode(PcapHandle.BlockingMode mode) |
void |
setDirection(PcapHandle.PcapDirection direction)
Set direction flag, which controls whether we accept only incoming
packets, only outgoing packets, or both.
|
void |
setDlt(DataLinkType dlt) |
void |
setFilter(BpfProgram prog) |
void |
setFilter(java.lang.String bpfExpression,
BpfProgram.BpfCompileMode mode) |
void |
setFilter(java.lang.String bpfExpression,
BpfProgram.BpfCompileMode mode,
java.net.Inet4Address netmask) |
java.util.stream.Stream<PcapPacket> |
stream()
Returns a
Stream instance representing a stream of captured packets. |
java.lang.String |
toString() |
public static final java.net.Inet4Address PCAP_NETMASK_UNKNOWN
setFilter(String, BpfProgram.BpfCompileMode, Inet4Address)
or compileFilter(String, BpfProgram.BpfCompileMode, Inet4Address) when you
don't know what netmask you should use.public DataLinkType getDlt()
public void setDlt(DataLinkType dlt) throws PcapNativeException, NotOpenException
dlt - a DataLinkType
object to setPcapNativeException - if an error occurs in the pcap native library.NotOpenException - if this PcapHandle is not open.public boolean isOpen()
close());
false otherwise.public java.lang.String getFilteringExpression()
public PcapHandle.TimestampPrecision getTimestampPrecision()
public void setDirection(PcapHandle.PcapDirection direction) throws PcapNativeException, NotOpenException
direction - direction to set.PcapNativeException - if an error occurs in the pcap native library.NotOpenException - if this PcapHandle is not open.public int getSnapshot()
throws NotOpenException
NotOpenException - if this PcapHandle is not open.public PcapHandle.SwappedType isSwapped() throws NotOpenException
SwappedType object.NotOpenException - if this PcapHandle is not open.public int getMajorVersion()
throws NotOpenException
NotOpenException - if this PcapHandle is not open.public int getMinorVersion()
throws NotOpenException
NotOpenException - if this PcapHandle is not open.public BpfProgram compileFilter(java.lang.String bpfExpression, BpfProgram.BpfCompileMode mode, java.net.Inet4Address netmask) throws PcapNativeException, NotOpenException
bpfExpression - bpfExpressionmode - modenetmask - netmaskBpfProgram object.PcapNativeException - if an error occurs in the pcap native library.NotOpenException - if this PcapHandle is not open.public void setFilter(java.lang.String bpfExpression,
BpfProgram.BpfCompileMode mode,
java.net.Inet4Address netmask)
throws PcapNativeException,
NotOpenException
bpfExpression - bpfExpressionmode - modenetmask - netmaskPcapNativeException - 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.public void setFilter(java.lang.String bpfExpression,
BpfProgram.BpfCompileMode mode)
throws PcapNativeException,
NotOpenException
bpfExpression - bpfExpressionmode - modePcapNativeException - 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.public void setFilter(BpfProgram prog) throws PcapNativeException, NotOpenException
prog - progPcapNativeException - 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.public void setBlockingMode(PcapHandle.BlockingMode mode) throws PcapNativeException, NotOpenException
mode - modePcapNativeException - if an error occurs in the pcap native library.NotOpenException - if this PcapHandle is not open.public PcapHandle.BlockingMode getBlockingMode() throws PcapNativeException, NotOpenException
PcapNativeException - if an error occurs in the pcap native library.NotOpenException - if this PcapHandle is not open.public PcapPacket getNextPacket() throws NotOpenException
PcapPacket object that contains captured packet. May be null.NotOpenException - if this PcapHandle is not open.public PcapPacket getNextPacketEx() throws PcapNativeException, java.io.EOFException, java.util.concurrent.TimeoutException, NotOpenException
PcapPacket object that contains captured packet. Not null.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.public void loop(int packetCount,
PacketListener listener)
throws PcapNativeException,
java.lang.InterruptedException,
NotOpenException
int 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 to listener.gotPacket(Packet).
When a packet is captured, listener.gotPacket(Packet) is called in
the thread which called the loop(). And then this PcapHandle waits for
the thread to return from the gotPacket() before it retrieves the next
packet from the pcap buffer.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 - listenerPcapNativeException - if an error occurs in the pcap native library.java.lang.InterruptedException - if the loop terminated due to a call to breakLoop().NotOpenException - if this PcapHandle is not open.public void loop(int packetCount,
PacketListener listener,
java.util.concurrent.Executor executor)
throws PcapNativeException,
java.lang.InterruptedException,
NotOpenException
int 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 to listener.gotPacket(Packet).
When a packet is captured, the
executor.execute() is called
with a Runnable object in the thread which called the loop().
Then, the Runnable object calls listener.gotPacket(Packet).
If listener.gotPacket(Packet) is expected to take a long time to
process a packet, this method should be used with a proper executor instead of
loop(int, PacketListener) in order to prevent the pcap buffer from overflowing.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 - executorPcapNativeException - if an error occurs in the pcap native library.java.lang.InterruptedException - if the loop terminated due to a call to breakLoop().NotOpenException - if this PcapHandle is not open.public int dispatch(int packetCount,
PacketListener listener)
throws PcapNativeException,
java.lang.InterruptedException,
NotOpenException
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 - listenerPcapNativeException - if an error occurs in the pcap native library.java.lang.InterruptedException - if the loop terminated due to a call to breakLoop().NotOpenException - if this PcapHandle is not open.public int dispatch(int packetCount,
PacketListener listener,
java.util.concurrent.Executor executor)
throws PcapNativeException,
java.lang.InterruptedException,
NotOpenException
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 - executorPcapNativeException - if an error occurs in the pcap native library.java.lang.InterruptedException - if the loop terminated due to a call to breakLoop().NotOpenException - if this PcapHandle is not open.public PcapDumper dumpOpen(java.lang.String filePath) throws PcapNativeException, NotOpenException
filePath - "-" means stdout.
The dlt of the PcapHandle which captured the packets you want to dump
must be the same as this dlt.PcapNativeException - if an error occurs in the pcap native library.NotOpenException - if this PcapHandle is not open.public void loop(int packetCount,
PcapDumper dumper)
throws PcapNativeException,
java.lang.InterruptedException,
NotOpenException
packetCount - packetCountdumper - dumperPcapNativeException - if an error occurs in the pcap native library.java.lang.InterruptedException - if the loop terminated due to a call to breakLoop().NotOpenException - if this PcapHandle is not open.public void breakLoop()
throws NotOpenException
NotOpenException - if this PcapHandle is not open.public java.util.stream.Stream<PcapPacket> stream() throws NotOpenException
Stream instance representing a stream of captured packets.
When this handle become unable to capture packets anymore (e.g. reaches EOF of the pcap file),
the stream starts to supply nulls.
This method locks this handle.
Call BaseStream.close() to unlock.NotOpenException - if this PcapHandle is not open.public void sendPacket(Packet packet) throws PcapNativeException, NotOpenException
packet - packetPcapNativeException - 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.public void sendPacket(byte[] bytes)
throws NotOpenException,
PcapNativeException
bytes - raw bytesPcapNativeException - 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.public void sendPacket(byte[] bytes,
int len)
throws NotOpenException,
PcapNativeException
bytes - raw byteslen - lengthPcapNativeException - 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.public PcapStat getStats() throws PcapNativeException, NotOpenException
PcapStat object.PcapNativeException - if an error occurs in the pcap native library.NotOpenException - if this PcapHandle is not open.public java.util.List<DataLinkType> listDatalinks() throws PcapNativeException, NotOpenException
DataLinkTypePcapNativeException - if an error occurs in the pcap native library.NotOpenException - if this PcapHandle is not open.public java.lang.String getError()
throws NotOpenException
NotOpenException - if this PcapHandle is not open.public void close()
close in interface java.lang.AutoCloseablepublic java.lang.String toString()
toString in class java.lang.ObjectCopyright © 2011 - 2018 Pcap4J.org. All rights reserved.