13 typedef pcap_if pcap_if_t;
15 typedef struct pcap_addr pcap_addr_t;
96 void* onStatsUpdateUserCookie =
nullptr,
unsigned int updateIntervalMs = 1000);
106 void* cbOnStatsUpdateUserCookie =
nullptr;
107 unsigned int updateIntervalMs = 1000;
110 struct SharedThreadData
112 std::atomic_bool stopRequested{
false };
117 static void workerMain(std::shared_ptr<SharedThreadData> sharedThreadData, ThreadData threadData);
119 std::shared_ptr<SharedThreadData> m_SharedThreadData;
120 std::thread m_WorkerThread;
126 pcap_t* m_PcapSendDescriptor;
127 int m_PcapSelectableFd;
128 DeviceInterfaceDetails m_InterfaceDetails;
131 uint32_t m_DeviceMtu;
132 MacAddress m_MacAddress;
133 IPv4Address m_DefaultGateway;
134 std::thread m_CaptureThread;
137 std::unique_ptr<StatisticsUpdateWorker> m_StatisticsUpdateWorker;
140 std::atomic<bool> m_StopThread;
142 std::atomic<bool> m_CaptureThreadStarted;
145 void* m_cbOnPacketArrivesUserCookie;
147 void* m_cbOnPacketArrivesBlockingModeUserCookie;
149 bool m_CaptureCallbackMode;
154 PcapLiveDevice(pcap_if_t* pInterface,
bool calculateMTU,
bool calculateMacAddress,
bool calculateDefaultGateway)
155 : PcapLiveDevice(DeviceInterfaceDetails(pInterface), calculateMTU, calculateMacAddress,
156 calculateDefaultGateway)
158 PcapLiveDevice(DeviceInterfaceDetails interfaceDetails,
bool calculateMTU,
bool calculateMacAddress,
159 bool calculateDefaultGateway);
162 void setDeviceMacAddress();
163 void setDefaultGateway();
166 void captureThreadMain();
168 static void onPacketArrives(uint8_t* user,
const struct pcap_pkthdr* pkthdr,
const uint8_t* packet);
169 static void onPacketArrivesNoCallback(uint8_t* user,
const struct pcap_pkthdr* pkthdr,
const uint8_t* packet);
170 static void onPacketArrivesBlockingMode(uint8_t* user,
const struct pcap_pkthdr* pkthdr,
const uint8_t* packet);
332 return m_InterfaceDetails.name;
339 return m_InterfaceDetails.description;
345 return m_InterfaceDetails.isLoopback;
363 return m_InterfaceDetails.addresses;
435 void* onStatsUpdateUserCookie);
455 void* onStatsUpdateUserCookie);
498 const double timeout);
526 PCPP_DEPRECATED(
"This method is deprecated. Use sendPacket(Packet const& packet, bool checkMtu) instead")
575 bool sendPacket(
const uint8_t* packetData,
int packetDataLength,
int packetPayloadLength);
592 bool sendPacket(
const uint8_t* packetData,
int packetDataLength,
bool checkMtu =
false,
675 bool sendPacketDirect(uint8_t
const* packetData,
int packetDataLength);
678 bool isNflogDevice()
const;
Definition: PcapDevice.h:141
Definition: IpAddress.h:28
Definition: IpAddress.h:156
Definition: MacAddress.h:21
A worker thread that periodically calls the provided callback with updated statistics.
Definition: PcapLiveDevice.h:87
StatisticsUpdateWorker(PcapLiveDevice const &pcapDevice, OnStatsUpdateCallback onStatsUpdateCallback, void *onStatsUpdateUserCookie=nullptr, unsigned int updateIntervalMs=1000)
Constructs and starts a worker thread that periodically calls the provided callback with updated stat...
void stopWorker()
Stops the worker thread.
Definition: PcapLiveDevice.h:66
PcapDirection
Definition: PcapLiveDevice.h:196
@ PCPP_IN
Only capture incoming traffics.
Definition: PcapLiveDevice.h:200
@ PCPP_OUT
Only capture outgoing traffics.
Definition: PcapLiveDevice.h:202
@ PCPP_INOUT
Capture traffics both incoming and outgoing.
Definition: PcapLiveDevice.h:198
void close() override
Close the device.
LiveDeviceType
The type of the live device.
Definition: PcapLiveDevice.h:175
@ WinPcapDevice
WinPcap/Npcap live device.
Definition: PcapLiveDevice.h:179
@ RemoteDevice
WinPcap/Npcap Remote Capture device.
Definition: PcapLiveDevice.h:181
@ LibPcapDevice
libPcap live device
Definition: PcapLiveDevice.h:177
virtual int startCaptureBlockingMode(OnPacketArrivesStopBlocking onPacketArrives, void *userCookie, const double timeout)
DeviceMode
Device capturing mode.
Definition: PcapLiveDevice.h:186
@ Normal
Only packets that their destination is this NIC are captured.
Definition: PcapLiveDevice.h:188
@ Promiscuous
All packets that arrive to the NIC are captured, even packets that their destination isn't this NIC.
Definition: PcapLiveDevice.h:190
virtual bool startCapture(int intervalInSecondsToUpdateStats, OnStatsUpdateCallback onStatsUpdate, void *onStatsUpdateUserCookie)
bool sendPacket(RawPacket const &rawPacket, bool checkMtu=false)
virtual uint32_t getMtu() const
Definition: PcapLiveDevice.h:349
virtual bool startCapture(OnPacketArrivesCallback onPacketArrives, void *onPacketArrivesUserCookie)
void getStatistics(IPcapDevice::PcapStats &stats) const override
bool open(const DeviceConfiguration &config)
bool doMtuCheck(int packetPayloadLength) const
virtual MacAddress getMacAddress() const
Definition: PcapLiveDevice.h:367
bool sendPacket(const uint8_t *packetData, int packetDataLength, int packetPayloadLength)
IPv4Address getDefaultGateway() const
std::string getDesc() const
Definition: PcapLiveDevice.h:337
bool sendPacket(const uint8_t *packetData, int packetDataLength, bool checkMtu=false, pcpp::LinkLayerType linkType=pcpp::LINKTYPE_ETHERNET)
const std::vector< IPv4Address > & getDnsServers() const
virtual int sendPackets(Packet **packetsArr, int arrLength, bool checkMtu=true)
virtual LiveDeviceType getDeviceType() const
Definition: PcapLiveDevice.h:324
virtual bool startCapture(OnPacketArrivesCallback onPacketArrives, void *onPacketArrivesUserCookie, int intervalInSecondsToUpdateStats, OnStatsUpdateCallback onStatsUpdate, void *onStatsUpdateUserCookie)
TimestampPrecision
Definition: PcapLiveDevice.h:226
@ Microseconds
use timestamps with microsecond precision, default
@ Nanoseconds
use timestamps with nanosecond precision
IPv6Address getIPv6Address() const
virtual bool startCapture(RawPacketVector &capturedPacketsVector)
~PcapLiveDevice() override
A destructor for this class.
bool sendPacket(Packet const &packet, bool checkMtu=true)
std::string getName() const
Definition: PcapLiveDevice.h:330
IPv4Address getIPv4Address() const
bool sendPacket(Packet *packet, bool checkMtu=true)
Definition: PcapLiveDevice.h:527
virtual int sendPackets(const RawPacketVector &rawPackets, bool checkMtu=false)
virtual int sendPackets(RawPacket *rawPacketsArr, int arrLength, bool checkMtu=false)
virtual LinkLayerType getLinkType() const
Definition: PcapLiveDevice.h:355
TimestampProvider
Definition: PcapLiveDevice.h:208
@ AdapterUnsynced
device-provided, not synced with the system clock
@ HostLowPrecision
host-provided, low precision, synced with the system clock
@ Adapter
device-provided, synced with the system clock
@ HostHighPrecision
host-provided, high precision, synced with the system clock
@ Host
host-provided, unknown characteristics, default
@ HostHighPrecisionUnsynced
host-provided, high precision, not synced with the system clock
bool getLoopback() const
Definition: PcapLiveDevice.h:343
virtual PcapLiveDevice * clone() const
std::vector< IPAddress > getIPAddresses() const
Definition: PcapLiveDevice.h:361
virtual void prepareCapture(bool asyncCapture, bool captureStats)
Called before starting a capture to prepare the device for capturing packets.
Definition: PcapLiveDevice.h:669
Definition: PcapLiveDeviceList.h:22
Definition: PointerVector.h:50
Definition: RawPacket.h:259
A wrapper class for pcap_t* which is the libpcap packet capture descriptor. This class is used to man...
Definition: PcapDevice.h:37
The main namespace for the PcapPlusPlus lib.
PointerVector< RawPacket > RawPacketVector
A vector of pointers to RawPacket.
Definition: Device.h:14
std::function< bool(RawPacket *, PcapLiveDevice *, void *)> OnPacketArrivesStopBlocking
Definition: PcapLiveDevice.h:36
std::function< void(RawPacket *, PcapLiveDevice *, void *)> OnPacketArrivesCallback
Definition: PcapLiveDevice.h:29
std::function< void(IPcapDevice::PcapStats &, void *)> OnStatsUpdateCallback
Definition: PcapLiveDevice.h:42
LinkLayerType
An enum describing all known link layer type. Taken from: http://www.tcpdump.org/linktypes....
Definition: RawPacket.h:20
@ LINKTYPE_ETHERNET
IEEE 802.3 Ethernet.
Definition: RawPacket.h:24
Definition: PcapLiveDevice.h:237
int packetBufferTimeoutMs
Definition: PcapLiveDevice.h:244
int snapshotLength
Definition: PcapLiveDevice.h:263
TimestampProvider timestampProvider
Definition: PcapLiveDevice.h:274
DeviceConfiguration(DeviceMode mode=Promiscuous, int packetBufferTimeoutMs=0, int packetBufferSize=0, PcapDirection direction=PCPP_INOUT, int snapshotLength=0, unsigned int nflogGroup=0, bool usePoll=false, TimestampProvider timestampProvider=TimestampProvider::Host, TimestampPrecision timestampPrecision=TimestampPrecision::Microseconds)
Definition: PcapLiveDevice.h:300
bool usePoll
In Unix-like system, use poll() for blocking mode.
Definition: PcapLiveDevice.h:270
int packetBufferSize
Definition: PcapLiveDevice.h:252
unsigned int nflogGroup
Definition: PcapLiveDevice.h:267
PcapDirection direction
Definition: PcapLiveDevice.h:256
TimestampPrecision timestampPrecision
Definition: PcapLiveDevice.h:278
DeviceMode mode
Indicates whether to open the device in promiscuous or normal mode.
Definition: PcapLiveDevice.h:239
A struct that contains all details of a network interface.
Definition: PcapLiveDevice.h:73
std::string description
Description of the device.
Definition: PcapLiveDevice.h:78
std::string name
Name of the device.
Definition: PcapLiveDevice.h:76
std::vector< IPAddress > addresses
IP addresses associated with the device.
Definition: PcapLiveDevice.h:80
bool isLoopback
Flag to indicate if the device is a loopback device.
Definition: PcapLiveDevice.h:82
Definition: PcapDevice.h:24