public final class PcapHandle extends Object
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.SwappedType |
static class |
PcapHandle.TimestampPrecision |
Modifier and Type | Method and Description |
---|---|
void |
breakLoop()
Breaks a loop which this handle is working on.
|
void |
close()
Closes this PcapHandle.
|
BpfProgram |
compileFilter(String bpfExpression,
BpfProgram.BpfCompileMode mode,
Inet4Address netmask) |
int |
dispatch(int packetCount,
PacketListener listener) |
int |
dispatch(int packetCount,
PacketListener listener,
Executor executor) |
int |
dispatch(int packetCount,
RawPacketListener listener) |
int |
dispatch(int packetCount,
RawPacketListener listener,
Executor executor) |
PcapDumper |
dumpOpen(String filePath) |
PcapHandle.BlockingMode |
getBlockingMode() |
DataLinkType |
getDlt() |
String |
getError() |
String |
getFilteringExpression() |
int |
getMajorVersion() |
int |
getMinorVersion() |
Packet |
getNextPacket() |
Packet |
getNextPacketEx() |
byte[] |
getNextRawPacket() |
byte[] |
getNextRawPacketEx() |
Integer |
getOriginalLength() |
int |
getSnapshot() |
PcapStat |
getStats() |
Timestamp |
getTimestamp() |
PcapHandle.TimestampPrecision |
getTimestampPrecision() |
boolean |
isOpen() |
PcapHandle.SwappedType |
isSwapped() |
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,
Executor executor)
A wrapper method for
int pcap_loop(pcap_t *, int, pcap_handler, u_char *) . |
void |
loop(int packetCount,
PcapDumper dumper) |
void |
loop(int packetCount,
RawPacketListener listener)
A wrapper method for
int pcap_loop(pcap_t *, int, pcap_handler, u_char *) . |
void |
loop(int packetCount,
RawPacketListener listener,
Executor executor)
A wrapper method for
int pcap_loop(pcap_t *, int, pcap_handler, u_char *) . |
void |
sendPacket(byte[] bytes) |
void |
sendPacket(Packet packet) |
void |
setBlockingMode(PcapHandle.BlockingMode mode) |
void |
setDlt(DataLinkType dlt) |
void |
setFilter(BpfProgram prog) |
void |
setFilter(String bpfExpression,
BpfProgram.BpfCompileMode mode) |
void |
setFilter(String bpfExpression,
BpfProgram.BpfCompileMode mode,
Inet4Address netmask) |
String |
toString() |
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 String getFilteringExpression()
public PcapHandle.TimestampPrecision getTimestampPrecision()
public Timestamp getTimestamp()
public Integer getOriginalLength()
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(String bpfExpression, BpfProgram.BpfCompileMode mode, 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(String bpfExpression, BpfProgram.BpfCompileMode mode, 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.NullPointerException
- if any of arguments are null.public void setFilter(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.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.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 Packet getNextPacket() throws NotOpenException
NotOpenException
- if this PcapHandle is not open.public byte[] getNextRawPacket() throws NotOpenException
NotOpenException
- if this PcapHandle is not open.public Packet getNextPacketEx() throws PcapNativeException, EOFException, TimeoutException, NotOpenException
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.public byte[] getNextRawPacketEx() throws PcapNativeException, EOFException, TimeoutException, NotOpenException
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.public void loop(int packetCount, PacketListener listener) throws PcapNativeException, 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.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, Executor executor) throws PcapNativeException, 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.InterruptedException
- if the loop terminated due to a call to breakLoop()
.NotOpenException
- if this PcapHandle is not open.public void loop(int packetCount, RawPacketListener listener) throws PcapNativeException, InterruptedException, NotOpenException
int pcap_loop(pcap_t *, int, pcap_handler, u_char *)
.
When a packet is captured, listener.gotPacket(byte[])
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.InterruptedException
- if the loop terminated due to a call to breakLoop()
.NotOpenException
- if this PcapHandle is not open.public void loop(int packetCount, RawPacketListener listener, Executor executor) throws PcapNativeException, InterruptedException, NotOpenException
int pcap_loop(pcap_t *, int, pcap_handler, u_char *)
.
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(byte[])
.
If listener.gotPacket(byte[])
is expected to take a long time to
process a packet, this method should be used with a proper executor instead of
loop(int, RawPacketListener)
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.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, 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.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, Executor executor) throws PcapNativeException, 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.InterruptedException
- if the loop terminated due to a call to breakLoop()
.NotOpenException
- if this PcapHandle is not open.public int dispatch(int packetCount, RawPacketListener listener) throws PcapNativeException, 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.InterruptedException
- if the loop terminated due to a call to breakLoop()
.NotOpenException
- if this PcapHandle is not open.public int dispatch(int packetCount, RawPacketListener listener, Executor executor) throws PcapNativeException, 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.InterruptedException
- if the loop terminated due to a call to breakLoop()
.NotOpenException
- if this PcapHandle is not open.public PcapDumper dumpOpen(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, InterruptedException, NotOpenException
packetCount
- packetCountdumper
- dumperPcapNativeException
- if an error occurs in the pcap native library.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 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.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.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 List<DataLinkType> listDatalinks() throws PcapNativeException, NotOpenException
DataLinkType
PcapNativeException
- if an error occurs in the pcap native library.NotOpenException
- if this PcapHandle is not open.public String getError() throws NotOpenException
NotOpenException
- if this PcapHandle is not open.public void close()
Copyright © 2011–2016 Pcap4J.org. All rights reserved.