Class BaseNetPcap
java.lang.Object
com.slytechs.sdk.jnetpcap.api.BaseNetPcap
- All Implemented Interfaces:
AutoCloseable
- Direct Known Subclasses:
NetPcap
Base class for NetPcap providing delegation to low-level Pcap bindings.
This class wraps a Pcap instance and provides pass-through access to
all pcap operations. Fluent setter methods return BaseNetPcap to
enable method chaining with covariant return type overrides in subclasses.
- Author:
- Mark Bednarczyk [mark@slytechs.com], Sly Technologies Inc.
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidactivate()Activates a pcap handle created withcreate().voidForces adispatch()orloop()call to return.booleanChecks whether monitor mode can be set on this capture handle.voidclose()Closes the capture handle and releases resources.com.slytechs.sdk.jnetpcap.BpFilterCompiles a filter expression into a BPF program.com.slytechs.sdk.jnetpcap.BpFilterCompiles a filter expression with netmask.com.slytechs.sdk.jnetpcap.constant.PcapDltdatalink()Returns the link-layer header type.com.slytechs.sdk.jnetpcap.constant.PcapDltReturns the link-layer header type with extended information.<U> intdispatch(int count, com.slytechs.sdk.jnetpcap.PcapDumper pcapDumper) Processes packets and writes them to a dump file.intdispatch(int count, com.slytechs.sdk.jnetpcap.PcapHandler.NativeCallback handler, MemorySegment user) Processes packets using a native callback.<U> intdispatch(int count, com.slytechs.sdk.jnetpcap.PcapHandler.OfArray<U> handler, U user) Processes packets using an array-based handler.<U> intdispatch(int count, com.slytechs.sdk.jnetpcap.PcapHandler.OfMemorySegment<U> handler, U user) Processes packets using a memory segment handler.com.slytechs.sdk.jnetpcap.PcapDumperOpens a dump file for writing packets.geterr()Returns the error message for the last pcap error.final StringgetName()Returns the device name associated with this handle.booleanReturns the current non-blocking mode state.com.slytechs.sdk.jnetpcap.internal.PcapHeaderABIReturns the pcap header ABI information.com.slytechs.sdk.jnetpcap.constant.PcapTStampPrecisionReturns the timestamp precision for this handle.final intinject(byte[] array) Transmits a packet, returning the number of bytes sent.final intinject(byte[] array, int offset, int length) Transmits a portion of a packet array.intinject(MemorySegment packet, int length) Transmits a packet from native memory.final intinject(ByteBuffer buf) Transmits a packet from a ByteBuffer.booleanChecks if the capture file has different byte order.List<com.slytechs.sdk.jnetpcap.constant.PcapDlt> Returns the list of supported data link types.List<com.slytechs.sdk.jnetpcap.constant.PcapTstampType> Returns the list of supported timestamp types.<U> intloop(int count, com.slytechs.sdk.jnetpcap.PcapDumper pcapDumper) Processes packets in a loop and writes to a dump file.<U> intloop(int count, com.slytechs.sdk.jnetpcap.PcapHandler.NativeCallback handler, MemorySegment user) Processes packets in a loop using a native callback.<U> intloop(int count, com.slytechs.sdk.jnetpcap.PcapHandler.OfArray<U> handler, U user) Processes packets in a loop using an array handler.<U> intloop(int count, com.slytechs.sdk.jnetpcap.PcapHandler.OfMemorySegment<U> handler, U user) Processes packets in a loop using a memory segment handler.intReturns the major version of the pcap file format.intReturns the minor version of the pcap file format.abstract com.slytechs.sdk.protocol.core.Packetnext()Retrieves the next packet.abstract com.slytechs.sdk.protocol.core.PacketnextEx()Retrieves the next packet with extended status.final ByteOrderorder()Returns the byte order of the capture file.Prints error message to stderr.final voidsendPacket(byte[] buf) Sends a raw packet on the network.final voidsendPacket(byte[] buf, int offset, int length) Sends a portion of a raw packet.voidsendPacket(MemorySegment packet, int length) Sends a raw packet from native memory.final voidsendPacket(ByteBuffer buf) Sends a raw packet from a ByteBuffer.setBufferSize(int bufferSize) Sets the kernel buffer size for the capture.setDatalink(int dlt) Sets the data link type for the capture.setDatalink(com.slytechs.sdk.jnetpcap.constant.PcapDlt dlt) Sets the data link type.setDatalink(Optional<com.slytechs.sdk.jnetpcap.constant.PcapDlt> dlt) Sets the data link type using an Optional.setDirection(int dir) Sets the capture direction.setDirection(com.slytechs.sdk.jnetpcap.constant.PcapDirection dir) Sets the capture direction.setDirection(Optional<com.slytechs.sdk.jnetpcap.constant.PcapDirection> dir) Sets the capture direction using an Optional.setFilter(com.slytechs.sdk.jnetpcap.BpFilter bpfProgram) Sets the BPF filter program.Sets the BPF filter using an Optional.setImmediateMode(boolean enable) Enables or disables immediate mode.setNonBlock(boolean nonBlock) Enables or disables non-blocking mode.setPromisc(boolean promiscuousMode) Enables or disables promiscuous mode.setRfmon(boolean rfMonitor) Enables or disables monitor mode (wireless).setSnaplen(int snaplen) Sets the snapshot length.setTimeout(int timeoutInMillis) Sets the read timeout in milliseconds.setTstampPrecision(com.slytechs.sdk.jnetpcap.constant.PcapTStampPrecision precision) Sets the timestamp precision.setTstampType(com.slytechs.sdk.jnetpcap.constant.PcapTstampType type) Sets the timestamp type.setUncaughtExceptionHandler(Thread.UncaughtExceptionHandler exceptionHandler) Sets the uncaught exception handler.setUncaughtExceptionHandler(Consumer<? super Throwable> exceptionHandler) Sets the uncaught exception handler using a Consumer.intsnapshot()Returns the snapshot length.com.slytechs.sdk.jnetpcap.PcapStatstats()Returns capture statistics.
-
Field Details
-
pcapApi
protected final com.slytechs.sdk.jnetpcap.Pcap pcapApi
-
-
Constructor Details
-
BaseNetPcap
protected BaseNetPcap(com.slytechs.sdk.jnetpcap.Pcap pcapApi)
-
-
Method Details
-
activate
public void activate() throws com.slytechs.sdk.jnetpcap.PcapActivatedException, com.slytechs.sdk.jnetpcap.PcapExceptionActivates a pcap handle created withcreate().- Throws:
com.slytechs.sdk.jnetpcap.PcapActivatedException- if handle is already activatedcom.slytechs.sdk.jnetpcap.PcapException- if activation fails- See Also:
-
breakloop
public void breakloop()Forces adispatch()orloop()call to return.- See Also:
-
canSetRfmon
public boolean canSetRfmon() throws com.slytechs.sdk.jnetpcap.PcapExceptionChecks whether monitor mode can be set on this capture handle.- Returns:
- true if monitor mode can be set
- Throws:
com.slytechs.sdk.jnetpcap.PcapException- if operation fails- See Also:
-
close
public void close()Closes the capture handle and releases resources.- Specified by:
closein interfaceAutoCloseable- See Also:
-
compile
public com.slytechs.sdk.jnetpcap.BpFilter compile(String str, boolean optimize) throws com.slytechs.sdk.jnetpcap.PcapException Compiles a filter expression into a BPF program.- Parameters:
str- the filter expressionoptimize- true to optimize the compiled code- Returns:
- the compiled BPF program
- Throws:
com.slytechs.sdk.jnetpcap.PcapException- if compilation fails- See Also:
-
compile
public com.slytechs.sdk.jnetpcap.BpFilter compile(String str, boolean optimize, int netmask) throws com.slytechs.sdk.jnetpcap.PcapException Compiles a filter expression with netmask.- Parameters:
str- the filter expressionoptimize- true to optimize the compiled codenetmask- the network mask- Returns:
- the compiled BPF program
- Throws:
com.slytechs.sdk.jnetpcap.PcapException- if compilation fails- See Also:
-
datalink
public com.slytechs.sdk.jnetpcap.constant.PcapDlt datalink() throws com.slytechs.sdk.jnetpcap.PcapExceptionReturns the link-layer header type.- Returns:
- the data link type
- Throws:
com.slytechs.sdk.jnetpcap.PcapException- if operation fails- See Also:
-
dataLinkExt
public com.slytechs.sdk.jnetpcap.constant.PcapDlt dataLinkExt() throws com.slytechs.sdk.jnetpcap.PcapExceptionReturns the link-layer header type with extended information.- Returns:
- the data link type
- Throws:
com.slytechs.sdk.jnetpcap.PcapException- if operation fails- See Also:
-
dispatch
public int dispatch(int count, com.slytechs.sdk.jnetpcap.PcapHandler.NativeCallback handler, MemorySegment user) Processes packets using a native callback.- Parameters:
count- maximum packets to processhandler- the native callback handleruser- user data passed to callback- Returns:
- number of packets processed
- See Also:
-
dispatch
public <U> int dispatch(int count, com.slytechs.sdk.jnetpcap.PcapHandler.OfArray<U> handler, U user) throws com.slytechs.sdk.jnetpcap.PcapException Processes packets using an array-based handler.- Type Parameters:
U- the user data type- Parameters:
count- maximum packets to processhandler- the array handleruser- user data passed to handler- Returns:
- number of packets processed
- Throws:
com.slytechs.sdk.jnetpcap.PcapException- if operation fails- See Also:
-
dispatch
public <U> int dispatch(int count, com.slytechs.sdk.jnetpcap.PcapHandler.OfMemorySegment<U> handler, U user) throws com.slytechs.sdk.jnetpcap.PcapException Processes packets using a memory segment handler.- Type Parameters:
U- the user data type- Parameters:
count- maximum packets to processhandler- the memory segment handleruser- user data passed to handler- Returns:
- number of packets processed
- Throws:
com.slytechs.sdk.jnetpcap.PcapException- if operation fails- See Also:
-
dispatch
public <U> int dispatch(int count, com.slytechs.sdk.jnetpcap.PcapDumper pcapDumper) throws com.slytechs.sdk.jnetpcap.PcapException Processes packets and writes them to a dump file.- Type Parameters:
U- the user data type- Parameters:
count- maximum packets to processpcapDumper- the dump file handle- Returns:
- number of packets processed
- Throws:
com.slytechs.sdk.jnetpcap.PcapException- if operation fails- See Also:
-
dumpOpen
public com.slytechs.sdk.jnetpcap.PcapDumper dumpOpen(String fname) throws com.slytechs.sdk.jnetpcap.PcapException Opens a dump file for writing packets.- Parameters:
fname- the output filename- Returns:
- a PcapDumper for writing packets
- Throws:
com.slytechs.sdk.jnetpcap.PcapException- if file cannot be opened- See Also:
-
geterr
Returns the error message for the last pcap error.- Returns:
- the error message string
- See Also:
-
getName
Returns the device name associated with this handle.- Returns:
- the device name
- See Also:
-
getNonBlock
public boolean getNonBlock() throws com.slytechs.sdk.jnetpcap.PcapExceptionReturns the current non-blocking mode state.- Returns:
- true if in non-blocking mode
- Throws:
com.slytechs.sdk.jnetpcap.PcapException- if operation fails- See Also:
-
getPcapHeaderABI
public com.slytechs.sdk.jnetpcap.internal.PcapHeaderABI getPcapHeaderABI()Returns the pcap header ABI information.- Returns:
- the header ABI
- See Also:
-
getTstampPrecision
public com.slytechs.sdk.jnetpcap.constant.PcapTStampPrecision getTstampPrecision() throws com.slytechs.sdk.jnetpcap.PcapExceptionReturns the timestamp precision for this handle.- Returns:
- the timestamp precision
- Throws:
com.slytechs.sdk.jnetpcap.PcapException- if operation fails- See Also:
-
inject
public final int inject(byte[] array) throws com.slytechs.sdk.jnetpcap.PcapException Transmits a packet, returning the number of bytes sent.- Parameters:
array- the packet data- Returns:
- number of bytes sent
- Throws:
com.slytechs.sdk.jnetpcap.PcapException- if transmission fails- See Also:
-
inject
public final int inject(byte[] array, int offset, int length) throws com.slytechs.sdk.jnetpcap.PcapException Transmits a portion of a packet array.- Parameters:
array- the packet dataoffset- starting offsetlength- number of bytes to send- Returns:
- number of bytes sent
- Throws:
com.slytechs.sdk.jnetpcap.PcapException- if transmission fails- See Also:
-
inject
Transmits a packet from a ByteBuffer.- Parameters:
buf- the packet data- Returns:
- number of bytes sent
- Throws:
com.slytechs.sdk.jnetpcap.PcapException- if transmission fails- See Also:
-
inject
Transmits a packet from native memory.- Parameters:
packet- the packet datalength- number of bytes to send- Returns:
- number of bytes sent
- Throws:
com.slytechs.sdk.jnetpcap.PcapException- if transmission fails- See Also:
-
isSwapped
public boolean isSwapped() throws com.slytechs.sdk.jnetpcap.PcapExceptionChecks if the capture file has different byte order.- Returns:
- true if byte-swapped
- Throws:
com.slytechs.sdk.jnetpcap.PcapException- if operation fails- See Also:
-
listDataLinks
public List<com.slytechs.sdk.jnetpcap.constant.PcapDlt> listDataLinks() throws com.slytechs.sdk.jnetpcap.PcapExceptionReturns the list of supported data link types.- Returns:
- list of supported link types
- Throws:
com.slytechs.sdk.jnetpcap.PcapException- if operation fails- See Also:
-
listTstampTypes
public List<com.slytechs.sdk.jnetpcap.constant.PcapTstampType> listTstampTypes() throws com.slytechs.sdk.jnetpcap.PcapExceptionReturns the list of supported timestamp types.- Returns:
- list of supported timestamp types
- Throws:
com.slytechs.sdk.jnetpcap.PcapException- if operation fails- See Also:
-
loop
public <U> int loop(int count, com.slytechs.sdk.jnetpcap.PcapHandler.NativeCallback handler, MemorySegment user) Processes packets in a loop using a native callback.- Type Parameters:
U- the user data type- Parameters:
count- packets to process (-1 for infinite)handler- the native callbackuser- user data- Returns:
- number of packets processed
- See Also:
-
loop
public <U> int loop(int count, com.slytechs.sdk.jnetpcap.PcapHandler.OfArray<U> handler, U user) throws com.slytechs.sdk.jnetpcap.PcapException Processes packets in a loop using an array handler.- Type Parameters:
U- the user data type- Parameters:
count- packets to process (-1 for infinite)handler- the array handleruser- user data- Returns:
- number of packets processed
- Throws:
com.slytechs.sdk.jnetpcap.PcapException- if operation fails- See Also:
-
loop
public <U> int loop(int count, com.slytechs.sdk.jnetpcap.PcapHandler.OfMemorySegment<U> handler, U user) Processes packets in a loop using a memory segment handler.- Type Parameters:
U- the user data type- Parameters:
count- packets to process (-1 for infinite)handler- the memory segment handleruser- user data- Returns:
- number of packets processed
- See Also:
-
loop
public <U> int loop(int count, com.slytechs.sdk.jnetpcap.PcapDumper pcapDumper) throws com.slytechs.sdk.jnetpcap.PcapException Processes packets in a loop and writes to a dump file.- Type Parameters:
U- the user data type- Parameters:
count- packets to process (-1 for infinite)pcapDumper- the dump file handle- Returns:
- number of packets processed
- Throws:
com.slytechs.sdk.jnetpcap.PcapException- if operation fails- See Also:
-
majorVersion
public int majorVersion() throws com.slytechs.sdk.jnetpcap.PcapExceptionReturns the major version of the pcap file format.- Returns:
- the major version number
- Throws:
com.slytechs.sdk.jnetpcap.PcapException- if operation fails- See Also:
-
minorVersion
public int minorVersion() throws com.slytechs.sdk.jnetpcap.PcapExceptionReturns the minor version of the pcap file format.- Returns:
- the minor version number
- Throws:
com.slytechs.sdk.jnetpcap.PcapException- if operation fails- See Also:
-
next
public abstract com.slytechs.sdk.protocol.core.Packet next() throws com.slytechs.sdk.jnetpcap.PcapExceptionRetrieves the next packet.- Returns:
- the next packet, or null if none available
- Throws:
com.slytechs.sdk.jnetpcap.PcapException- if operation fails- See Also:
-
nextEx
public abstract com.slytechs.sdk.protocol.core.Packet nextEx() throws com.slytechs.sdk.jnetpcap.PcapException, TimeoutExceptionRetrieves the next packet with extended status.- Returns:
- the next packet
- Throws:
com.slytechs.sdk.jnetpcap.PcapException- if operation fails or EOF reachedTimeoutException- if read timeout expires- See Also:
-
order
Returns the byte order of the capture file.- Returns:
- the byte order
- Throws:
com.slytechs.sdk.jnetpcap.PcapException- if operation fails- See Also:
-
perror
Prints error message to stderr.- Parameters:
prefix- prefix for the error message- Returns:
- this instance for method chaining
- See Also:
-
sendPacket
public final void sendPacket(byte[] buf) throws com.slytechs.sdk.jnetpcap.PcapException Sends a raw packet on the network.- Parameters:
buf- the packet data- Throws:
com.slytechs.sdk.jnetpcap.PcapException- if transmission fails- See Also:
-
sendPacket
public final void sendPacket(byte[] buf, int offset, int length) throws com.slytechs.sdk.jnetpcap.PcapException Sends a portion of a raw packet.- Parameters:
buf- the packet dataoffset- starting offsetlength- number of bytes to send- Throws:
com.slytechs.sdk.jnetpcap.PcapException- if transmission fails- See Also:
-
sendPacket
Sends a raw packet from a ByteBuffer.- Parameters:
buf- the packet data- Throws:
com.slytechs.sdk.jnetpcap.PcapException- if transmission fails- See Also:
-
sendPacket
public void sendPacket(MemorySegment packet, int length) throws com.slytechs.sdk.jnetpcap.PcapException Sends a raw packet from native memory.- Parameters:
packet- the packet datalength- number of bytes to send- Throws:
com.slytechs.sdk.jnetpcap.PcapException- if transmission fails- See Also:
-
setBufferSize
Sets the kernel buffer size for the capture.- Parameters:
bufferSize- the buffer size in bytes- Returns:
- this instance for method chaining
- Throws:
com.slytechs.sdk.jnetpcap.PcapException- if operation fails- See Also:
-
setDatalink
Sets the data link type for the capture.- Parameters:
dlt- the data link type value- Returns:
- this instance for method chaining
- Throws:
com.slytechs.sdk.jnetpcap.PcapException- if operation fails- See Also:
-
setDatalink
public BaseNetPcap setDatalink(Optional<com.slytechs.sdk.jnetpcap.constant.PcapDlt> dlt) throws com.slytechs.sdk.jnetpcap.PcapException Sets the data link type using an Optional.- Parameters:
dlt- the optional data link type- Returns:
- this instance for method chaining
- Throws:
com.slytechs.sdk.jnetpcap.PcapException- if operation fails- See Also:
-
setDatalink
public BaseNetPcap setDatalink(com.slytechs.sdk.jnetpcap.constant.PcapDlt dlt) throws com.slytechs.sdk.jnetpcap.PcapException Sets the data link type.- Parameters:
dlt- the data link type- Returns:
- this instance for method chaining
- Throws:
com.slytechs.sdk.jnetpcap.PcapException- if operation fails- See Also:
-
setDirection
Sets the capture direction.- Parameters:
dir- the direction value- Returns:
- this instance for method chaining
- Throws:
com.slytechs.sdk.jnetpcap.PcapException- if operation fails- See Also:
-
setDirection
public BaseNetPcap setDirection(Optional<com.slytechs.sdk.jnetpcap.constant.PcapDirection> dir) throws com.slytechs.sdk.jnetpcap.PcapException Sets the capture direction using an Optional.- Parameters:
dir- the optional direction- Returns:
- this instance for method chaining
- Throws:
com.slytechs.sdk.jnetpcap.PcapException- if operation fails- See Also:
-
setDirection
public BaseNetPcap setDirection(com.slytechs.sdk.jnetpcap.constant.PcapDirection dir) throws com.slytechs.sdk.jnetpcap.PcapException Sets the capture direction.- Parameters:
dir- the direction- Returns:
- this instance for method chaining
- Throws:
com.slytechs.sdk.jnetpcap.PcapException- if operation fails- See Also:
-
setFilter
public BaseNetPcap setFilter(com.slytechs.sdk.jnetpcap.BpFilter bpfProgram) throws com.slytechs.sdk.jnetpcap.PcapException Sets the BPF filter program.- Parameters:
bpfProgram- the compiled filter program- Returns:
- this instance for method chaining
- Throws:
com.slytechs.sdk.jnetpcap.PcapException- if operation fails- See Also:
-
setFilter
public BaseNetPcap setFilter(Optional<com.slytechs.sdk.jnetpcap.BpFilter> bpfProgram) throws com.slytechs.sdk.jnetpcap.PcapException Sets the BPF filter using an Optional.- Parameters:
bpfProgram- the optional filter program- Returns:
- this instance for method chaining
- Throws:
com.slytechs.sdk.jnetpcap.PcapException- if operation fails- See Also:
-
setImmediateMode
Enables or disables immediate mode.- Parameters:
enable- true to enable immediate mode- Returns:
- this instance for method chaining
- Throws:
com.slytechs.sdk.jnetpcap.PcapException- if operation fails- See Also:
-
setNonBlock
Enables or disables non-blocking mode.- Parameters:
nonBlock- true for non-blocking mode- Returns:
- this instance for method chaining
- Throws:
com.slytechs.sdk.jnetpcap.PcapException- if operation fails- See Also:
-
setPromisc
public BaseNetPcap setPromisc(boolean promiscuousMode) throws com.slytechs.sdk.jnetpcap.PcapException Enables or disables promiscuous mode.- Parameters:
promiscuousMode- true for promiscuous mode- Returns:
- this instance for method chaining
- Throws:
com.slytechs.sdk.jnetpcap.PcapException- if operation fails- See Also:
-
setRfmon
Enables or disables monitor mode (wireless).- Parameters:
rfMonitor- true to enable monitor mode- Returns:
- this instance for method chaining
- Throws:
com.slytechs.sdk.jnetpcap.PcapException- if operation fails- See Also:
-
setSnaplen
Sets the snapshot length.- Parameters:
snaplen- maximum bytes to capture per packet- Returns:
- this instance for method chaining
- Throws:
com.slytechs.sdk.jnetpcap.PcapException- if operation fails- See Also:
-
setTimeout
Sets the read timeout in milliseconds.- Parameters:
timeoutInMillis- timeout in milliseconds- Returns:
- this instance for method chaining
- Throws:
com.slytechs.sdk.jnetpcap.PcapException- if operation fails- See Also:
-
setTstampPrecision
public BaseNetPcap setTstampPrecision(com.slytechs.sdk.jnetpcap.constant.PcapTStampPrecision precision) throws com.slytechs.sdk.jnetpcap.PcapException Sets the timestamp precision.- Parameters:
precision- the timestamp precision- Returns:
- this instance for method chaining
- Throws:
com.slytechs.sdk.jnetpcap.PcapException- if operation fails- See Also:
-
setTstampType
public BaseNetPcap setTstampType(com.slytechs.sdk.jnetpcap.constant.PcapTstampType type) throws com.slytechs.sdk.jnetpcap.PcapException Sets the timestamp type.- Parameters:
type- the timestamp type- Returns:
- this instance for method chaining
- Throws:
com.slytechs.sdk.jnetpcap.PcapException- if operation fails- See Also:
-
setUncaughtExceptionHandler
Sets the uncaught exception handler using a Consumer.- Parameters:
exceptionHandler- the exception handler- Returns:
- this instance for method chaining
- See Also:
-
setUncaughtExceptionHandler
Sets the uncaught exception handler.- Parameters:
exceptionHandler- the exception handler- Returns:
- this instance for method chaining
- See Also:
-
snapshot
public int snapshot() throws com.slytechs.sdk.jnetpcap.PcapExceptionReturns the snapshot length.- Returns:
- the snapshot length
- Throws:
com.slytechs.sdk.jnetpcap.PcapException- if operation fails- See Also:
-
stats
public com.slytechs.sdk.jnetpcap.PcapStat stats() throws com.slytechs.sdk.jnetpcap.PcapExceptionReturns capture statistics.- Returns:
- the capture statistics
- Throws:
com.slytechs.sdk.jnetpcap.PcapException- if operation fails- See Also:
-