- All Implemented Interfaces:
AutoCloseable
- Author:
- Sly Technologies Inc, repos@slytechs.com, mark
-
Nested Class Summary
Nested classes/interfaces inherited from class org.jnetpcap.Pcap0_9
Pcap0_9.Linux0_9
Nested classes/interfaces inherited from class org.jnetpcap.Pcap0_8
Pcap0_8.Unix0_8
Nested classes/interfaces inherited from class org.jnetpcap.Pcap0_4
Pcap0_4.PcapSupplier<T extends Pcap>
Nested classes/interfaces inherited from class org.jnetpcap.Pcap
Pcap.LibraryPolicy, Pcap.Linux, Pcap.Unix
-
Field Summary
Fields inherited from class org.jnetpcap.Pcap0_4
dispatcher
Fields inherited from class org.jnetpcap.Pcap
closed, pcapHeaderABI, POINTER_TO_POINTER1, POINTER_TO_POINTER2, POINTER_TO_POINTER3, VERSION
-
Constructor Summary
ModifierConstructorDescriptionprotected
UnsafePcapHandle
(MemorySegment pcapHandle, String name, PcapHeaderABI abi) Instantiates a new unsafe pcap handle. -
Method Summary
Modifier and TypeMethodDescriptionaddress()
Address.static UnsafePcapHandle
Create a live capture handle.int
dispatchWithAccessToRawPacket
(int count, PcapHandler.NativeCallback handler, MemorySegment user) Process packets from a live capture or savefile.int
inject
(MemorySegment packet, int length) Inject.int
loopWithAccessToRawPacket
(int count, PcapHandler.NativeCallback handler) Process packets from a live capture or savefile.static String
Make dead handle name.static String
makeLiveHandleName
(String device) Make live handle name.static String
makeOfflineHandleName
(String fname) Make offline handle name.static UnsafePcapHandle
openOffline
(String fname) Open a saved capture file for reading.void
sendPacket
(MemorySegment packet, int length) Send packet.Methods inherited from class org.jnetpcap.Pcap1_10
isSupported, openDead, openDeadWithTstampPrecision, openLive
Methods inherited from class org.jnetpcap.Pcap1_5
getTstampPrecision, openDeadWithTstampPrecision, setImmediateMode, setTstampPrecision
Methods inherited from class org.jnetpcap.Pcap1_2
listTstampTypes, setTstampType
Methods inherited from class org.jnetpcap.Pcap1_0
activate, canSetRfmon, create, dataLinkExt, init, offlineFilter, setBufferSize, setPromisc, setPromisc, setRfmon, setRfmon, setSnaplen, setTimeout, statusToStr
Methods inherited from class org.jnetpcap.Pcap0_9
setDirection, setProtocolLinux
Methods inherited from class org.jnetpcap.Pcap0_8
breakloop, datalinkNameToVal, dataLinkValToDescription, dataLinkValToName, getSelectableFd, libVersion, listDataLinks, nextEx, setDatalink
Methods inherited from class org.jnetpcap.Pcap0_7
findAllDevs, getNonBlock, setNonBlock
Methods inherited from class org.jnetpcap.Pcap0_5
compileNoPcap
Methods inherited from class org.jnetpcap.Pcap0_4
close, compile, datalink, datalinkGetAsInt, dispatch, dispatch, dispatch, dispatch, dumpOpen, file, fileno, getDispatcher, geterr, isInitialized, isSwapped, lookupDev, lookupNet, loop, loop, loop, loop, majorVersion, minorVersion, next, openLive, openOffline, perror, setDispatcher, setFilter, setUncaughtExceptionHandler, snapshot, stats, strerror
Methods inherited from class org.jnetpcap.Pcap
checkPcapVersion, compile, create, datalinkValToDescription, datalinkValToName, findAllDevsEx, getErrorString, getName, getPcapHandle, getPcapHeaderABI, init, inject, inject, inject, listAllPcapIf, loadNativePcapLibrary, lookupErrorString, lookupNet, newArena, openLive, openLive, openOffline, order, sendPacket, sendPacket, sendPacket, setDatalink, setDatalink, setDirection, setDirection, setFilter, setUncaughtExceptionHandler, statusToStr, toString
-
Constructor Details
-
UnsafePcapHandle
Instantiates a new unsafe pcap handle.- Parameters:
pcapHandle
- the pcap handlename
- the nameabi
- the abi
-
-
Method Details
-
makeDeadHandleName
-
makeLiveHandleName
-
makeOfflineHandleName
-
openOffline
Open a saved capture file for reading.pcap_open_offline() and pcap_open_offline_with_tstamp_precision() are called to open a ``savefile'' for reading.
- Parameters:
fname
- specifies the name of the file to open. The file can have the pcap file format as described in pcap-savefile(5), which is the file format used by, among other programs, tcpdump(1) and tcpslice(1), or can have the pcapng file format, although not all pcapng files can be read- Returns:
- the pcap handle
- Throws:
PcapException
- any errors- Since:
- libpcap 0.4
-
create
Create a live capture handle.create
is used to create a packet capture handle to look at packets on the network. source is a string that specifies the network device to open; on Linux systems with 2.2 or later kernels, a source argument of "any" or NULL can be used to capture packets from all interfaces. The returned handle must be activated with pcap_activate() before pack' ets can be captured with it; options for the capture, such as promiscu' ous mode, can be set on the handle before activating it.- Parameters:
device
- a string that specifies the network device to open; on Linux systems with 2.2 or later kernels, a source argument of "any" or NULL can be used to capture packets from all interfaces.- Returns:
- a new pcap object that needs to be activated using
Pcap1_0.activate()
call - Throws:
PcapException
- the pcap exception- Since:
- libpcap 1.0
-
address
-
dispatchWithAccessToRawPacket
public int dispatchWithAccessToRawPacket(int count, PcapHandler.NativeCallback handler, MemorySegment user) Process packets from a live capture or savefile.Processes packets from a live capture or ``savefile'' 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''.
Note that, when doing a live capture on some platforms, if the read timeout expires when there are no packets available, pcap_dispatch() will return 0, even when not in non-blocking mode, as there are no packets to process. Applications should be prepared for this to happen, but must not rely on it happening.
Callback specifies a pcap_handler routine to be called with three arguments: a u_char pointer which is passed in the user argument to pcap_loop() or pcap_dispatch(), a const struct pcap_pkthdr pointer pointing to the packet time stamp and lengths, and a const u_char pointer to the first caplen (as given in the struct pcap_pkthdr a pointer to which is passed to the callback routine) bytes of data from the packet. The struct pcap_pkthdr and the packet data are not to be freed by the callback routine, and are not guaranteed to be valid after the callback routine returns; if the full needs them to be valid after the callback, it must make a copy of them.
The bytes of data from the packet begin with a link-layer header. The format of the link-layer header is indicated by the return value of the pcap_datalink(3PCAP) routine when handed the pcap_t value also passed to pcap_loop() or pcap_dispatch(). https://www.tcpdump.org/linktypes.html lists the values pcap_datalink() can return and describes the packet formats that correspond to those values. The value it returns will be valid for all packets received unless and until pcap_set_datalink(3PCAP) is called; after a successful call to pcap_set_datalink(), all subsequent packets will have a link-layer header of the type specified by the link-layer header type value passed to pcap_set_datalink().
Do NOT assume that the packets for a given capture or ``savefile`` will have any given link-layer header type, such as DLT_EN10MB for Ethernet. For example, the "any" device on Linux will have a link-layer header type of DLT_LINUX_SLL or DLT_LINUX_SLL2 even if all devices on the system at the time the "any" device is opened have some other data link type, such as DLT_EN10MB for Ethernet.
- Parameters:
count
- maximum number of packets to process before returninghandler
- the handleruser
- TODO- Returns:
- the number of packets processed on success; this can be 0 if no packets were read from a live capture (if, for example, they were discarded because they didn't pass the packet filter, or if, on platforms that support a packet buffer timeout that starts before any packets arrive, the timeout expires before any packets arrive, or if the file descriptor for the capture device is in non-blocking mode and no packets were available to be read) or if no more packets are available in a ``savefile.''
- Since:
- libpcap 0.4
-
loopWithAccessToRawPacket
Process packets from a live capture or savefile.Processes packets from a live capture or ``savefile'' 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''.
Note that, when doing a live capture on some platforms, if the read timeout expires when there are no packets available, pcap_dispatch() will return 0, even when not in non-blocking mode, as there are no packets to process. Applications should be prepared for this to happen, but must not rely on it happening.
Callback specifies a pcap_handler routine to be called with three arguments: a u_char pointer which is passed in the user argument to pcap_loop() or pcap_dispatch(), a const struct pcap_pkthdr pointer pointing to the packet time stamp and lengths, and a const u_char pointer to the first caplen (as given in the struct pcap_pkthdr a pointer to which is passed to the callback routine) bytes of data from the packet. The struct pcap_pkthdr and the packet data are not to be freed by the callback routine, and are not guaranteed to be valid after the callback routine returns; if the full needs them to be valid after the callback, it must make a copy of them.
The bytes of data from the packet begin with a link-layer header. The format of the link-layer header is indicated by the return value of the pcap_datalink(3PCAP) routine when handed the pcap_t value also passed to pcap_loop() or pcap_dispatch(). https://www.tcpdump.org/linktypes.html lists the values pcap_datalink() can return and describes the packet formats that correspond to those values. The value it returns will be valid for all packets received unless and until pcap_set_datalink(3PCAP) is called; after a successful call to pcap_set_datalink(), all subsequent packets will have a link-layer header of the type specified by the link-layer header type value passed to pcap_set_datalink().
Do NOT assume that the packets for a given capture or ``savefile`` will have any given link-layer header type, such as DLT_EN10MB for Ethernet. For example, the "any" device on Linux will have a link-layer header type of DLT_LINUX_SLL or DLT_LINUX_SLL2 even if all devices on the system at the time the "any" device is opened have some other data link type, such as DLT_EN10MB for Ethernet.
- Parameters:
count
- maximum number of packets to process before returninghandler
- the handler- Returns:
- the number of packets processed on success; this can be 0 if no packets were read from a live capture (if, for example, they were discarded because they didn't pass the packet filter, or if, on platforms that support a packet buffer timeout that starts before any packets arrive, the timeout expires before any packets arrive, or if the file descriptor for the capture device is in non-blocking mode and no packets were available to be read) or if no more packets are available in a ``savefile.''
- Since:
- libpcap 0.4
-
inject
Inject.- Overrides:
inject
in classPcap0_9
- Parameters:
packet
- the packetlength
- the length- Returns:
- the int
- Throws:
PcapException
- the pcap exception- See Also:
-
sendPacket
Send packet.- Overrides:
sendPacket
in classPcap0_8
- Parameters:
packet
- the packetlength
- the length- Throws:
PcapException
- the pcap exception- See Also:
-