13 typedef pcap_if pcap_if_t;
15 typedef struct pcap_addr pcap_addr_t;
85 bool m_DeviceOpened =
false;
90 pcap_t* m_PcapSendDescriptor;
91 int m_PcapSelectableFd;
98 std::thread m_CaptureThread;
99 std::thread m_StatsThread;
102 std::atomic<bool> m_StopThread;
104 std::atomic<bool> m_CaptureThreadStarted;
107 void* m_cbOnPacketArrivesUserCookie;
109 void* m_cbOnPacketArrivesBlockingModeUserCookie;
111 bool m_CaptureCallbackMode;
116 PcapLiveDevice(pcap_if_t* pInterface,
bool calculateMTU,
bool calculateMacAddress,
bool calculateDefaultGateway)
118 calculateDefaultGateway)
120 PcapLiveDevice(DeviceInterfaceDetails interfaceDetails,
bool calculateMTU,
bool calculateMacAddress,
121 bool calculateDefaultGateway);
124 void setDeviceMacAddress();
125 void setDefaultGateway();
128 void captureThreadMain();
130 static void onPacketArrives(uint8_t* user,
const struct pcap_pkthdr* pkthdr,
const uint8_t* packet);
131 static void onPacketArrivesNoCallback(uint8_t* user,
const struct pcap_pkthdr* pkthdr,
const uint8_t* packet);
132 static void onPacketArrivesBlockingMode(uint8_t* user,
const struct pcap_pkthdr* pkthdr,
const uint8_t* packet);
294 return m_InterfaceDetails.
name;
397 void* onStatsUpdateUserCookie);
417 void* onStatsUpdateUserCookie);
460 const double timeout);
489 PCPP_DEPRECATED(
"This method is deprecated. Use sendPacket(Packet const& packet, bool checkMtu) instead")
538 bool sendPacket(
const uint8_t* packetData,
int packetDataLength,
int packetPayloadLength);
555 bool sendPacket(
const uint8_t* packetData,
int packetDataLength,
bool checkMtu =
false,
620 return m_DeviceOpened;
660 size_t* outPayloadLength =
nullptr)
const;
675 size_t* outPayloadLength =
nullptr)
const;
693 size_t* outPayloadLength =
nullptr)
const;
696 bool sendPacketUnchecked(uint8_t
const* packetData,
int packetDataLength);
697 bool sendPacketUnchecked(
RawPacket const& rawPacket)
703 bool isNflogDevice()
const;
Definition: PcapDevice.h:141
Definition: IpAddress.h:30
Definition: IpAddress.h:165
Definition: MacAddress.h:24
Definition: PcapLiveDevice.h:66
PcapDirection
Definition: PcapLiveDevice.h:158
@ PCPP_IN
Only capture incoming traffics.
Definition: PcapLiveDevice.h:162
@ PCPP_OUT
Only capture outgoing traffics.
Definition: PcapLiveDevice.h:164
@ PCPP_INOUT
Capture traffics both incoming and outgoing.
Definition: PcapLiveDevice.h:160
void close() override
Close the device.
LiveDeviceType
The type of the live device.
Definition: PcapLiveDevice.h:137
@ WinPcapDevice
WinPcap/Npcap live device.
Definition: PcapLiveDevice.h:141
@ RemoteDevice
WinPcap/Npcap Remote Capture device.
Definition: PcapLiveDevice.h:143
@ LibPcapDevice
libPcap live device
Definition: PcapLiveDevice.h:139
virtual int startCaptureBlockingMode(OnPacketArrivesStopBlocking onPacketArrives, void *userCookie, const double timeout)
DeviceMode
Device capturing mode.
Definition: PcapLiveDevice.h:148
@ Normal
Only packets that their destination is this NIC are captured.
Definition: PcapLiveDevice.h:150
@ Promiscuous
All packets that arrive to the NIC are captured, even packets that their destination isn't this NIC.
Definition: PcapLiveDevice.h:152
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:311
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:329
bool sendPacket(const uint8_t *packetData, int packetDataLength, int packetPayloadLength)
IPv4Address getDefaultGateway() const
std::string getDesc() const
Definition: PcapLiveDevice.h:299
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)
bool isPayloadWithinMtu(size_t payloadLength) const
Checks whether the packetPayloadLength is smaller or equal than the device MTU.
virtual LiveDeviceType getDeviceType() const
Definition: PcapLiveDevice.h:286
virtual bool startCapture(OnPacketArrivesCallback onPacketArrives, void *onPacketArrivesUserCookie, int intervalInSecondsToUpdateStats, OnStatsUpdateCallback onStatsUpdate, void *onStatsUpdateUserCookie)
TimestampPrecision
Definition: PcapLiveDevice.h:188
@ 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:292
bool isPayloadWithinMtu(uint8_t const *packetData, size_t packetLen, LinkLayerType linkType=pcpp::LINKTYPE_ETHERNET, bool allowUnknownLength=false, size_t *outPayloadLength=nullptr) const
Checks whether the payload length of a packet's raw data is smaller or equal than the device MTU.
IPv4Address getIPv4Address() const
bool sendPacket(Packet *packet, bool checkMtu=true)
Definition: PcapLiveDevice.h:490
virtual int sendPackets(const RawPacketVector &rawPackets, bool checkMtu=false)
virtual int sendPackets(RawPacket *rawPacketsArr, int arrLength, bool checkMtu=false)
bool isPayloadWithinMtu(Packet const &packet, bool allowUnknownLength=false, size_t *outPayloadLength=nullptr) const
Checks whether the packet's payload length is smaller or equal than the device MTU.
virtual LinkLayerType getLinkType() const
Definition: PcapLiveDevice.h:317
bool isOpened() const override
Definition: PcapLiveDevice.h:618
TimestampProvider
Definition: PcapLiveDevice.h:170
@ 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:305
virtual PcapLiveDevice * clone() const
bool isPayloadWithinMtu(RawPacket const &rawPacket, bool allowUnknownLength=false, size_t *outPayloadLength=nullptr) const
Checks whether the payload length of a RawPacket is smaller or equal than the device MTU.
std::vector< IPAddress > getIPAddresses() const
Definition: PcapLiveDevice.h:323
virtual void prepareCapture(bool asyncCapture, bool captureStats)
Called before starting a capture to prepare the device for capturing packets.
Definition: PcapLiveDevice.h:637
Definition: PcapLiveDeviceList.h:22
Definition: PointerVector.h:50
Definition: RawPacket.h:259
const uint8_t * getRawData() const
Definition: RawPacket.h:370
int getRawDataLen() const
Definition: RawPacket.h:389
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.
Definition: AssertionUtils.h:19
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:199
int packetBufferTimeoutMs
Definition: PcapLiveDevice.h:206
int snapshotLength
Definition: PcapLiveDevice.h:225
TimestampProvider timestampProvider
Definition: PcapLiveDevice.h:236
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:262
bool usePoll
In Unix-like system, use poll() for blocking mode.
Definition: PcapLiveDevice.h:232
int packetBufferSize
Definition: PcapLiveDevice.h:214
unsigned int nflogGroup
Definition: PcapLiveDevice.h:229
PcapDirection direction
Definition: PcapLiveDevice.h:218
TimestampPrecision timestampPrecision
Definition: PcapLiveDevice.h:240
DeviceMode mode
Indicates whether to open the device in promiscuous or normal mode.
Definition: PcapLiveDevice.h:201
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