Package pcap.spi
Interface Pcap
- All Superinterfaces:
AutoCloseable
,Selectable
A handle for
pcap
instance.- Since:
- 1.0.0
-
Nested Class Summary
Modifier and TypeInterfaceDescriptionstatic enum
Used to specify a direction that packets will be captured. -
Method Summary
Modifier and TypeMethodDescription<T> T
Create pointer to given type.void
Force aloop(int, PacketHandler, Object)
ordispatch(int, PacketHandler, Object)
call to return And throwBreakException
onloop(int, PacketHandler, Object)
.void
close()
ClosePcapLive
orPcapOffline
.Compile filter syntax.int
datalink()
Get link-layer header type for for bothService.live(Interface, Service.LiveOptions)
andService.offline(String, Service.OfflineOptions)
.<T> void
dispatch
(int count, PacketHandler<T> handler, T args) Processes packets from a live capture orPcapLive
until cnt packets are processed.OpenDumper
handler for writing the packets tosavefile
(Create/Override existing file).dumpOpenAppend
(String file) Opensavefile
or create the new one if it's doesn't exist.boolean
Returns blocking mode.Get the time stamp precision returned in captures.int
inject
(PacketBuffer directBuffer) Sends a raw packet through the network interface; directBuffer points to the data of the packet, including the link-layer header, and size is the number of bytes in the packet.boolean
Find out out whether asavefile
has the native byte order.<T> void
loop
(int count, PacketHandler<T> handler, T args) Process packets from a livePcapLive
orPcapOffline
.int
Get major version number of asavefile
.int
Get minor version number of asavefile
.next
(PacketHeader header) Read the next packet (by callingdispatch(int, PacketHandler, Object)
) with a cnt of 1) and returns aPacketBuffer
.void
nextEx
(PacketHeader packetHeader, PacketBuffer packetBuffer) Reads the next packet and returns a success/failure indication.void
sendPacket
(PacketBuffer directBuffer) Send a raw packet through the network interface.void
setDirection
(Pcap.Direction direction) Used to specify a direction that packets will be captured.void
BPF packet filter.void
setFilter
(PacketFilter filter) BPF packet filter.void
setNonBlock
(boolean blocking) Puts a this capture handle intonon-blocking
mode, or takes it out ofnon-blocking
mode, depending on whether theblocking
argument istrue
orfalse
.int
snapshot()
Get snapshot length.stats()
Represent packet statistics from the start of the run to the time of the call.Methods inherited from interface pcap.spi.Selectable
id, register
-
Method Details
-
dumpOpen
OpenDumper
handler for writing the packets tosavefile
(Create/Override existing file).- Parameters:
file
- location ofsavefile
will saved.- Returns:
- returns new
Dumper
handle. - Throws:
ErrorException
- generic exception.- Since:
- 1.0.0
-
dumpOpenAppend
Opensavefile
or create the new one if it's doesn't exist.- Parameters:
file
- location of saved file.- Returns:
- returns new
Dumper
handle. - Throws:
ErrorException
- generic error.- Since:
- 1.0.0
-
compile
Compile filter syntax.- Parameters:
filter
- filter syntax.optimize
-true
optimized,false
otherwise.- Returns:
- returns packet filter.
- Throws:
ErrorException
- generic error.- Since:
- 1.5.0
-
setFilter
BPF packet filter.- Parameters:
filter
- packet filter.- Throws:
ErrorException
- generic error.- Since:
- 1.5.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
-
next
Read the next packet (by callingdispatch(int, PacketHandler, Object)
) with a cnt of 1) and returns aPacketBuffer
. ThePacketBuffer
andPacketHeader
is not freed by the caller, and not not guaranteed to be valid after the next call tonextEx(PacketHeader, PacketBuffer)
,next(PacketHeader)
,loop(int, PacketHandler, Object)
, ordispatch(int, PacketHandler, Object)
. If the code needs it to remain valid, it must make a copy of it. ThePacketHeader
pointed to byheader
is filled in with the appropriate values for the packet.- Parameters:
header
- header.- Returns:
- returns
PacketBuffer
appropriate with for thePacketHeader
.
-
nextEx
void nextEx(PacketHeader packetHeader, PacketBuffer packetBuffer) throws BreakException, TimeoutException, ErrorException Reads the next packet and returns a success/failure indication. ThePacketBuffer
andPacketHeader
is not freed by the caller, and not not guaranteed to be valid after the next call tonextEx(PacketHeader, PacketBuffer)
,next(PacketHeader)
,loop(int, PacketHandler, Object)
, ordispatch(int, PacketHandler, Object)
. If the code needs it to remain valid, it must make a copy of it.The
PacketHeader
pointed to byheader
and thePacketBuffer
pointer bybuffer
is filled in with the appropriate values for the packet.- Parameters:
packetHeader
- header.packetBuffer
- buffer.- Throws:
BreakException
- there are no more packets to read fromsavefile
.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, ErrorException, TimeoutException Processes packets from a live capture orPcapLive
until cnt packets are processed.- 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.TimeoutException
- timeout.
-
stats
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
Statistics
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)
ordispatch(int, PacketHandler, Object)
call to return And throwBreakException
onloop(int, PacketHandler, Object)
.- Since:
- 1.0.0
-
sendPacket
Send a raw packet through the network interface.- Parameters:
directBuffer
- buffer started fromPacketBuffer.readerIndex()
toPacketBuffer.writerIndex()
.- Throws:
ErrorException
- generic error.- Since:
- 1.0.0
- See Also:
-
inject
Sends a raw packet through the network interface; directBuffer points to the data of the packet, including the link-layer header, and size is the number of bytes in the packet.- Parameters:
directBuffer
- buffer started fromPacketBuffer.readerIndex()
toPacketBuffer.writerIndex()
.- Returns:
- returns the number of bytes written on success and throws
ErrorException
on failure. - Throws:
ErrorException
- error exception.- Since:
- 1.0.0
- See Also:
-
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.- Since:
- 1.0.0
-
isSwapped
Find out out whether asavefile
has the native byte order.- Returns:
- returns
true
if a handle is on offline mode (savefile
) and using a different byte order with current system. For live handle, it's always returnsfalse
. - Throws:
NotActivatedException
- if called this function on a capture handle that has been created but not activated.- Since:
- 1.0.0
-
getTimestampPrecision
Timestamp.Precision getTimestampPrecision()Get the time stamp precision returned in captures.- Returns:
- returns
Timestamp.Precision.MICRO
orTimestamp.Precision.NANO
, which indicates that pcap captures contains time stamps in microseconds or nanoseconds respectively. - Since:
- 1.0.0
-
majorVersion
int majorVersion()Get major version number of asavefile
. IfPcap
handle is in live mode, this method are not meaningful.- Returns:
- returns major version of a
savefile
. - Since:
- 1.0.0
-
minorVersion
int minorVersion()Get minor version number of asavefile
. IfPcap
handle is in live mode, this method are not meaningful.- Returns:
- returns minor version of a
savefile
. - Since:
- 1.0.0
-
snapshot
int snapshot()Get snapshot length.- Returns:
- returns snapshot length.
- Since:
- 1.0.0
-
getNonBlock
Returns blocking mode. Always returns false if aPcap
handle in offline handle (savefile
).- Returns:
- returns
true
if non blocking,false otherwise
. - Throws:
ErrorException
- error occurred.- Since:
- 1.0.0
-
setNonBlock
Puts a this capture handle intonon-blocking
mode, or takes it out ofnon-blocking
mode, depending on whether theblocking
argument istrue
orfalse
. It has no effect onsavefiles
. Innon-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 innon-blocking
mode.When
Pcap
handle created, a handle is not in non blocking mode.- Parameters:
blocking
-true
for enable non blocking mode,false
otherwise.- Throws:
ErrorException
- throwing some error when calling this method.- Since:
- 1.0.0
-
datalink
int datalink()Get link-layer header type for for bothService.live(Interface, Service.LiveOptions)
andService.offline(String, Service.OfflineOptions)
.- Returns:
- returns the link-layer header type.
-
close
void close()ClosePcapLive
orPcapOffline
.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceSelectable
- Since:
- 1.0.0
-
allocate
Create pointer to given type.- Type Parameters:
T
- pointer type.- Parameters:
cls
- a class, exPacketHeader
andPacketBuffer
.- Returns:
- returns
<T>
instance. - Throws:
IllegalArgumentException
- Since:
- 1.0.0
-