Interface Pcap
- All Superinterfaces:
AutoCloseable
public interface Pcap extends AutoCloseable
pcap
api.- Since:
- 1.0.0
- Author:
- Ardika Rommy Sanjaya
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static class
Pcap.Direction
Used to specify a direction that packets will be captured. -
Method Summary
Modifier and Type Method Description <T> T
allocate(Class<T> cls)
Create empty pointer.void
breakLoop()
Force aloop(int, PacketHandler, Object)
call to return And throwBreakException
onloop(int, PacketHandler, Object)
.void
close()
ClosePcapLive
orPcapOffline
.<T> void
dispatch(int count, PacketHandler<T> handler, T args)
processes packets from a live capture orPcapLive
until cnt packets are processed, the end of the current bufferful of packets is reached when doing a live capture, the end of the'savefile'
is reached when reading from a'savefile'
,Pcap#breakLoop()
is called, or an error occurs.Dumper
dumpOpen(String file)
OpenDumper
handler.Dumper
dumpOpenAppend(String file)
Append packet buffer on existingpcap
file.<T> void
loop(int count, PacketHandler<T> handler, T args)
Process packets from a livePcapLive
orPcapOffline
.void
nextEx(PacketBuffer packetBuffer, PacketHeader packetHeader)
Reads the next packet and returns a success/failure indication.void
send(ByteBuffer directBuffer, int size)
Deprecated.void
send(PacketBuffer directBuffer, int size)
Sends a raw packet through the network interface.void
setDirection(Pcap.Direction direction)
Used to specify a direction that packets will be captured.void
setFilter(String filter, boolean optimize)
BPF packet filter.void
setNonBlock(boolean blocking)
Puts a this capture handle into `non-blocking` mode, or takes it out of `non-blocking` mode, depending on whether the nonblock argument is `true` or `false`.Status
status()
Represent packet statistics from the start of the run to the time of the call.
-
Method Details
-
dumpOpen
OpenDumper
handler.- Parameters:
file
- location of capture file will saved.- Returns:
- returns
Pcap
Dumper
handle. - Throws:
ErrorException
- generic exception.- Since:
- 1.0.0
-
dumpOpenAppend
Append packet buffer on existingpcap
file.- Parameters:
file
- location of saved file.- Returns:
- returns
Pcap
Dumper
handle. - Throws:
ErrorException
- generic error.- Since:
- 1.0.0
-
setFilter
BPF packet filter.- Parameters:
filter
- filter expression.optimize
-true
for optimized filter,false
otherwise.- Throws:
ErrorException
- generic error.- Since:
- 1.0.0
-
loop
Process packets from a livePcapLive
orPcapOffline
.- Type Parameters:
T
- args type.- Parameters:
count
- maximum number of packets to process before returning. A value of -1 or 0 for count is equivalent to infinity, so that packets are processed until another ending condition occurs.handler
-PacketHandler
callback function.args
- user args.- Throws:
BreakException
-breakLoop()
is called.ErrorException
- Generic error.- Since:
- 1.0.0
-
nextEx
void nextEx(PacketBuffer packetBuffer, PacketHeader packetHeader) throws BreakException, TimeoutException, ErrorExceptionReads the next packet and returns a success/failure indication.- Parameters:
packetBuffer
- packet buffer.packetHeader
- packet header.- Throws:
BreakException
- there are no more packets to read from `savefile`.TimeoutException
- if packets are being read from a `live capture` and the packet buffer timeout expired.ErrorException
- generic exception.- Since:
- 1.0.0
-
dispatch
<T> void dispatch(int count, PacketHandler<T> handler, T args) throws BreakException, ErrorExceptionprocesses packets from a live capture orPcapLive
until cnt packets are processed, the end of the current bufferful of packets is reached when doing a live capture, the end of the'savefile'
is reached when reading from a'savefile'
,Pcap#breakLoop()
is called, or an error occurs. Thus, when doing a live capture, cnt is the maximum number of packets to process before returning, but is not a minimum number; when reading a live capture, only one bufferful of packets is read at a time, so fewer than cnt packets may be processed. A value of -1 or 0 for cnt causes all the packets received in one buffer to be processed when reading a live capture, and causes all the packets in the file to be processed when reading a'savefile'
.(In older versions of libpcap, the behavior when cnt was 0 was undefined; different platforms and devices behaved differently, so code that must work with older versions of libpcap should use -1, nor 0, as the value of cnt.)
callback specifies a
PacketHandler
routine to be called with three arguments : aargs
which is passed in the user argument toPcap#loop()
orPcap#dispatch()
, a const struct pcap_pkthdr pointer pointing to the packet time stamp and lengths, and aargs
to the first caplen bytes of data from the packet.(In older versions of libpcap, the behavior when cnt was 0 was undefined; different platforms and devices behaved differently, so code that must work with older versions of libpcap should use -1, nor 0, as the value of cnt.)
- Type Parameters:
T
- args type.- Parameters:
count
- number of packets.handler
-PacketHandler
callback function.args
- user args.- Throws:
BreakException
-breakLoop()
is called.ErrorException
- Generic error.
-
status
Represent packet statistics from the start of the run to the time of the call.Supported only on live captures, not on
PcapOffline
; no statistics are stored inPcapOffline
so no statistics are available when reading from aPcapOffline
- Returns:
- returns
Status
on success. - Throws:
ErrorException
- There is an error or if thisPcap
doesn't support packet statistics.- Since:
- 1.0.0
-
breakLoop
void breakLoop()Force aloop(int, PacketHandler, Object)
call to return And throwBreakException
onloop(int, PacketHandler, Object)
.- Since:
- 1.0.0
-
send
Deprecated.Sends a raw packet through the network interface.- Parameters:
directBuffer
- the data of the packet, including the link-layer header.size
- the number of bytes in the packet.- Throws:
ErrorException
- generic error.
-
send
Sends a raw packet through the network interface.- Parameters:
directBuffer
- the data of the packet, including the link-layer header.size
- the number of bytes in the packet.- Throws:
ErrorException
- generic error.
-
setDirection
Used to specify a direction that packets will be captured. This method isn't necessarily fully supported on all platforms; some platforms might return an error for all values, and some other platforms might not supportPcap.Direction.PCAP_D_OUT
.This operation is not supported if a
PcapOffline
is being read.Below is list of directions:
PCAP_D_INOUT
is the default direction and it will capture packets received by or sent by the device.PCAP_D_IN
only capture packets received by the device.PCAP_D_OUT
only capture packets sent by the device.
- Parameters:
direction
- is one of the constantsPcap.Direction
.- Throws:
ErrorException
- generic exception.
-
setNonBlock
Puts a this capture handle into `non-blocking` mode, or takes it out of `non-blocking` mode, depending on whether the nonblock argument is `true` or `false`. It has no effect on `savefiles`. In `non-blocking` mode, an attempt to read from the capture descriptor withdispatch(int, PacketHandler, Object)
will, if no packets are currently available to be read, return void; immediately rather than blocking waiting for packets to arrive.loop(int, PacketHandler, Object)
will not work in `non-blocking` mode.- Parameters:
blocking
- `true` for enable non blocking mode, `false` otherwise.- Throws:
ErrorException
- throwing some error when calling this method.
-
close
void close()ClosePcapLive
orPcapOffline
.
Note: BPF handle will closed automaticly.- Specified by:
close
in interfaceAutoCloseable
- Since:
- 1.0.0
-
allocate
Create empty pointer.- Type Parameters:
T
- pointer type.- Parameters:
cls
- a class, exPacketHeader
andPacketBuffer
.- Returns:
- returns
<T>
instance. - Throws:
IllegalArgumentException
-