パッケージ org.pcap4j.core
クラス PcapHandle
- java.lang.Object
-
- org.pcap4j.core.PcapHandle
-
- すべての実装されたインタフェース:
Closeable
,AutoCloseable
public final class PcapHandle extends Object implements Closeable
A wrapper class for struct pcap_t.- 導入されたバージョン:
- pcap4j 0.9.1
- 作成者:
- Kaito Yamada
-
-
ネストされたクラスの概要
ネストされたクラス 修飾子とタイプ クラス 説明 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
-
フィールドの概要
フィールド 修飾子とタイプ フィールド 説明 static 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.
-
メソッドの概要
すべてのメソッド インスタンス・メソッド concreteメソッド 修飾子とタイプ メソッド 説明 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 forint pcap_loop(pcap_t *, int, pcap_handler, u_char *)
.void
loop(int packetCount, PacketListener listener, Executor executor)
A wrapper method forint pcap_loop(pcap_t *, int, pcap_handler, u_char *)
.void
loop(int packetCount, PcapDumper dumper)
void
loop(int packetCount, RawPacketListener listener)
A wrapper method forint pcap_loop(pcap_t *, int, pcap_handler, u_char *)
.void
loop(int packetCount, RawPacketListener listener, Executor executor)
A wrapper method forint pcap_loop(pcap_t *, int, pcap_handler, u_char *)
.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(String bpfExpression, BpfProgram.BpfCompileMode mode)
void
setFilter(String bpfExpression, BpfProgram.BpfCompileMode mode, Inet4Address netmask)
void
setFilter(BpfProgram prog)
String
toString()
-
-
-
フィールドの詳細
-
PCAP_NETMASK_UNKNOWN
public static final 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.
-
-
メソッドの詳細
-
getDlt
public DataLinkType getDlt()
- 戻り値:
- the Data Link Type of this PcapHandle
-
setDlt
public void setDlt(DataLinkType dlt) throws PcapNativeException, NotOpenException
- パラメータ:
dlt
- aDataLinkType
object to set- 例外:
PcapNativeException
- if an error occurs in the pcap native library.NotOpenException
- if this PcapHandle is not open.
-
isOpen
public boolean isOpen()
- 戻り値:
- true if this PcapHandle object is open (i.e. not yet closed by
close()
); false otherwise.
-
getFilteringExpression
public String getFilteringExpression()
- 戻り値:
- the filtering expression of this PcapHandle
-
getTimestampPrecision
public PcapHandle.TimestampPrecision getTimestampPrecision()
- 戻り値:
- 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.- パラメータ:
direction
- direction to set.- 例外:
PcapNativeException
- if an error occurs in the pcap native library.NotOpenException
- if this PcapHandle is not open.
-
getTimestamp
public Timestamp getTimestamp()
- 戻り値:
- the timestamp of the last packet captured by this handle in the current thread.
-
getOriginalLength
public Integer getOriginalLength()
- 戻り値:
- the original length of the last packet captured by this handle in the current thread.
-
getSnapshot
public int getSnapshot() throws NotOpenException
- 戻り値:
- the dimension of the packet portion (in bytes) that is delivered to the application.
- 例外:
NotOpenException
- if this PcapHandle is not open.
-
isSwapped
public PcapHandle.SwappedType isSwapped() throws NotOpenException
- 戻り値:
- a
SwappedType
object. - 例外:
NotOpenException
- if this PcapHandle is not open.
-
getMajorVersion
public int getMajorVersion() throws NotOpenException
- 戻り値:
- the major version number of the pcap library used to write the savefile.
- 例外:
NotOpenException
- if this PcapHandle is not open.
-
getMinorVersion
public int getMinorVersion() throws NotOpenException
- 戻り値:
- the minor version number of the pcap library used to write the savefile.
- 例外:
NotOpenException
- if this PcapHandle is not open.
-
compileFilter
public BpfProgram compileFilter(String bpfExpression, BpfProgram.BpfCompileMode mode, Inet4Address netmask) throws PcapNativeException, NotOpenException
- パラメータ:
bpfExpression
- bpfExpressionmode
- modenetmask
- netmask- 戻り値:
- a
BpfProgram
object. - 例外:
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
- パラメータ:
bpfExpression
- bpfExpressionmode
- modenetmask
- netmask- 例外:
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
- パラメータ:
bpfExpression
- bpfExpressionmode
- mode- 例外:
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(BpfProgram prog) throws PcapNativeException, NotOpenException
- パラメータ:
prog
- prog- 例外:
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
- パラメータ:
mode
- mode- 例外:
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
- 戻り値:
- blocking mode
- 例外:
PcapNativeException
- if an error occurs in the pcap native library.NotOpenException
- if this PcapHandle is not open.
-
getNextPacket
public Packet getNextPacket() throws NotOpenException
- 戻り値:
- a Packet object created from a captured packet using the packet factory. May be null.
- 例外:
NotOpenException
- if this PcapHandle is not open.
-
getNextRawPacket
public byte[] getNextRawPacket() throws NotOpenException
- 戻り値:
- a captured packet. May be null.
- 例外:
NotOpenException
- if this PcapHandle is not open.
-
getNextPacketEx
public Packet getNextPacketEx() throws PcapNativeException, EOFException, TimeoutException, NotOpenException
- 戻り値:
- a Packet object created from a captured packet using the packet factory. Not null.
- 例外:
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
- 戻り値:
- a captured packet. Not null.
- 例外:
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.- パラメータ:
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- 例外:
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.- パラメータ:
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- 例外:
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.- パラメータ:
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- 例外:
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.- パラメータ:
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- 例外:
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) 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
- listener- 戻り値:
- the number of captured packets.
- 例外:
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
- パラメータ:
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- 戻り値:
- the number of captured packets.
- 例外:
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
- パラメータ:
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- 戻り値:
- the number of captured packets.
- 例外:
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
- パラメータ:
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- 戻り値:
- the number of captured packets.
- 例外:
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.
-
dumpOpen
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.- 戻り値:
- an opened PcapDumper.
- 例外:
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
- パラメータ:
packetCount
- packetCountdumper
- dumper- 例外:
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
public void breakLoop() throws NotOpenException
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.
- 例外:
NotOpenException
- if this PcapHandle is not open.
-
sendPacket
public void sendPacket(Packet packet) throws PcapNativeException, NotOpenException
- パラメータ:
packet
- packet- 例外:
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
public void sendPacket(byte[] bytes) throws NotOpenException, PcapNativeException
- パラメータ:
bytes
- raw bytes- 例外:
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
public void sendPacket(byte[] bytes, int len) throws NotOpenException, PcapNativeException
- パラメータ:
bytes
- raw byteslen
- length- 例外:
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
public PcapStat getStats() throws PcapNativeException, NotOpenException
- 戻り値:
- a
PcapStat
object. - 例外:
PcapNativeException
- if an error occurs in the pcap native library.NotOpenException
- if this PcapHandle is not open.
-
listDatalinks
public List<DataLinkType> listDatalinks() throws PcapNativeException, NotOpenException
- 戻り値:
- a list of
DataLinkType
- 例外:
PcapNativeException
- if an error occurs in the pcap native library.NotOpenException
- if this PcapHandle is not open.
-
getError
public String getError() throws NotOpenException
- 戻り値:
- an error message.
- 例外:
NotOpenException
- if this PcapHandle is not open.
-
close
public void close()
Closes this PcapHandle.- 定義:
close
インタフェース内AutoCloseable
- 定義:
close
インタフェース内Closeable
-
-