Package org.jnetpcap
Opening a capture handle for reading
To open a handle for a live capture, given the name of the network or other
interface on which the capture should be done, call
Pcap.create(String)
, set the appropriate options on the handle, and
then activate it with Pcap.activate()
. If activate() fails, the
handle should be closed with Pcap.close()
.
To obtain a list of devices that can be opened for a live capture, call
Pcap.findAllDevs()
; the returned list contains PcapIf
objects
representing each interface. Pcap.lookupDev()
will return the first
device on that list that is not a loopback network interface.
To open a handle for a savefile from which to read packets, given the
pathname of the savefile, call Pcap.openOffline(String)
. To set up a
handle for writing to a savefile, use Pcap.dumpOpen(String)
.
To create a "fake" handle for use in routines that require a Pcap instance as
an argument, such as routines to compile a filter expression, call
Pcap.openDead(PcapDlt, int)
.
All Pcap instances implement AutoCloseable
, so they can be used with
try-with-resources statements to ensure proper cleanup. When you're done with
a handle, it will be automatically closed when exiting the try block.
Example Usage
Here is an example which demonstrates capturing packets using different handler types:
try (Pcap pcap = Pcap.openOffline("capture.pcap")) {
// Create and apply a filter
BpFilter filter = pcap.compile("tcp", true);
pcap.setFilter(filter);
// Capture packets using byte array handler
pcap.loop(1, (String msg, PcapHeader header, byte[] packet) -> {
System.out.printf("Packet [timestamp=%s, wirelen=%d caplen=%d]%n",
Instant.ofEpochMilli(header.toEpochMillis()),
header.wireLength(),
header.captureLength());
}, "Example message");
// Capture packets using ByteBuffer handler for zero-copy
pcap.loop(1, (String msg, PcapHeader header, ByteBuffer packet) -> {
System.out.printf("Packet [timestamp=%s, wirelen=%d caplen=%d]%n",
Instant.ofEpochMilli(header.toEpochMillis()),
header.wireLength(),
header.captureLength());
}, "Example message");
}
Packet Handlers
The library provides several types of packet handlers through thePcapHandler
interface:
PcapHandler.OfArray
- Receives packets as byte arrays (with copy)PcapHandler.OfByteBuffer
- Receives packets as ByteBuffersPcapHandler.OfMemorySegment
- Direct access to native memory segments (advanced usage)
Network Interfaces
Network interfaces are represented by thePcapIf
class, which provides information about:
- Interface name and description
- Network addresses (IPv4, IPv6)
- Interface flags and capabilities
- Hardware (MAC) addresses
- Author:
- Mark Bednarczyk [mark@slytechs.com], Sly Technologies Inc.
-
ClassDescriptionBerkeley Packet Filter (BPF) program implementation for packet filtering.Error message resource bundle factory.Entry point and base class for all Pcap API methods provided by jNetPcap library.An interface which provides a hook into Pcap initialization process.Linux only/specific calls.Unix only/specific calls.Provides Pcap API method calls for up to libpcap version 0.4Pcap0_4.PcapSupplier<T extends Pcap>The Interface PcapSupplier.Provides Pcap API method calls for up to libpcap version 0.5Provides Pcap API method calls for up to libpcap version 0.6Provides Pcap API method calls for up to libpcap version 0.7Provides Pcap API method calls for up to libpcap version 0.8Symbol container for lazy initialization.Provides Pcap API method calls for up to libpcap version 0.9Symbol container for lazy initialization.Provides Pcap API method calls for up to libpcap version 1.0Provides Pcap API method calls for up to libpcap version 1.10Provides Pcap API method calls for up to libpcap version 1.2Provides Pcap API method calls for up to libpcap version 1.5Provides Pcap API method calls for up to libpcap version 1.9Indicates that an operation is not permitted on an already activated pcap handle.Dump packets to a capture file.A multi-mudule I8N error handler for all jNetPcap messages.Checked Pcap errors, warnings and exceptions.A marker interface for all Pcap packet handling functional interfaces.A native pcap callback which is called with packets captured using the
Pcap.loop(int, org.jnetpcap.PcapDumper)
orPcap.dispatch(int, org.jnetpcap.PcapDumper)
calls.A safe packet handler which receives copies of packets in a byte array.A safeByteBuffer
packet handler.An advanced low level, no copy, packet handler.A Pcap packet header also called a descriptor that precedes each packet.Reports any packet header runtime errors.Reports an out of range error for a value of native Pcap header field.A Java representation of the nativepcap_if_t
structure which contains information about a network interface.PcapIf.PcapAddr<T extends SockAddr>The struct pcap_addr structure containing network interfaces/devices addresses.Pcap message localizer.Provides packet statistics from the start of the pcap run to the time of the call.A Java representation of the native socket address (sockaddr) structure and its protocol-specific variants.Represents an IPv6 socket address (sockaddr_in6 structure).Represents an IPv4 socket address (sockaddr_in structure).The structure ofsockaddr_ipx
, used for AF_IPX sockets.The structure ofsockaddr_irda
, used with AF_IRDA sockets on windows (winsock2.h) to access link-layer information.The structure ofsockaddr_dl
, used with AF_LINK sockets on macOS to access link-layer information.The structure ofsockaddr_ll
, used with AF_PACKET sockets for raw packet access on Linux.