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(), set the
appropriate options on the handle, and then activate it with
Pcap.activate()
. If pcap_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 list is automatically freed by
jNePcap. 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_open_offline(); to set up a handle
for a ``savefile'', given a FILE * referring to a file already opened for
reading, call Pcap.openOffline(java.io.File)
.
In order to get a ``fake'' pcap_t for use in routines that require a pcap_t
as an argument, such as routines to open a ``savefile'' for writing and to
compile a filter expression, call Pcap.openDead(org.jnetpcap.constant.PcapDlt, int)
.
Pcap.create(org.jnetpcap.PcapIf)
, Pcap.openOffline(java.io.File)
,
pcap_fopen_offline(), and Pcap.openDead(org.jnetpcap.constant.PcapDlt, int)
return a pointer
to a pcap_t, which is the handle used for reading packets from the capture
stream or the ``savefile'', and for finding out information about the capture
stream or ``savefile''. To close a handle, use pcap_close().
Here is an example which uses PcapReceiver and several of its functional packet handler interfaces.
try (Pcap pcap = Pcap.openOffline(PCAP_FILE)) {
BpFilter filter = pcap.compile("tcp", true);
pcap.setFilter(filter);
pcap.loop(1, PcapExample1::nextDefault, "Hello, this is a copy to byte[] dispatch");
pcap.loop(1, PcapExample1::nextByteBuffer, "Hello, this is a no-copy to ByteBuffer dispatch");
}
...
private static void nextByteBuffer(String message, PcapHeader header, ByteBuffer packet) {
System.out.println(message);
System.out.printf("Packet [timestamp=%s, wirelen=%-4d caplen=%-4d %s]%n",
Instant.ofEpochMilli(header.toEpochMillis()),
header.wireLength(),
header.captureLength(),
PcapUtils.toHexCurleyString(packet.limit(6)));
}
private static void nextDefault(String message, PcapHeader header, byte[] packet) {
System.out.println(message);
System.out.printf("Packet [timestamp=%s, wirelen=%-4d caplen=%-4d %s]%n",
Instant.ofEpochMilli(header.toEpochMillis()),
header.wireLength(),
header.captureLength(),
PcapUtils.toHexCurleyString(packet, 0, 6));
}
Output:
Hello, this is a copy to byte[] dispatch Packet [timestamp=2011-03-01T20:45:13.266Z, wirelen=74 caplen=74 {00:26:62:2f:47:87}] Hello, this is a no-copy to ByteBuffer dispatch Packet [timestamp=2011-03-01T20:45:13.313Z, wirelen=74 caplen=74 {00:1d:60:b3:01:84}]
- Author:
- Sly Technologies, repos@slytechs.com
-
ClassDescriptionA Berkley Packet Filter program.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.4Provides 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.8Provides Pcap API method calls for up to libpcap version 0.9Provides 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.Native Type pcap_if_t has the following members.PcapIf.PcapAddr<T extends SockAddr>The struct pcap_addr structure containing network interfaces/devices addresses.Pcap message localizer.Packet statistics from the start of the pcap run to the time of the call.The low levelsockaddr
structure containing an address of different types, depending on the protocol family value.The structure ofsockaddr_in6
, used for IPv6 sockets.The structure ofsockaddr_in
, used for IPv4 sockets.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.