14 typedef pcap_if pcap_if_t;
44 PcapHandle& operator=(std::nullptr_t) noexcept;
51 return m_PcapDescriptor !=
nullptr;
55 pcap_t*
get() const noexcept
57 return m_PcapDescriptor;
67 void reset(pcap_t* pcapDescriptor =
nullptr) noexcept;
95 explicit operator
bool() const noexcept
100 bool operator==(std::nullptr_t)
const noexcept
104 bool operator!=(std::nullptr_t)
const noexcept
110 pcap_t* m_PcapDescriptor =
nullptr;
114 class PcapLiveDevice;
176 bool m_DeviceOpened =
false;
182 pcap_t* m_PcapSendDescriptor;
183 int m_PcapSelectableFd;
187 uint32_t m_DeviceMtu;
190 std::thread m_CaptureThread;
191 std::thread m_StatsThread;
194 std::atomic<bool> m_StopThread;
196 std::atomic<bool> m_CaptureThreadStarted;
202 PcapLiveDevice(pcap_if_t* pInterface,
bool calculateMTU,
bool calculateMacAddress,
bool calculateDefaultGateway)
204 calculateDefaultGateway)
206 PcapLiveDevice(DeviceInterfaceDetails interfaceDetails,
bool calculateMTU,
bool calculateMacAddress,
207 bool calculateDefaultGateway);
210 void setDeviceMacAddress();
211 void setDefaultGateway();
373 return m_InterfaceDetails.
name;
481 void* onStatsUpdateUserCookie);
501 void* onStatsUpdateUserCookie);
544 const double timeout);
573 PCPP_DEPRECATED(
"This method is deprecated. Use sendPacket(Packet const& packet, bool checkMtu) instead")
622 bool sendPacket(
const uint8_t* packetData,
int packetDataLength,
int packetPayloadLength);
639 bool sendPacket(
const uint8_t* packetData,
int packetDataLength,
bool checkMtu =
false,
704 return m_DeviceOpened;
719 PCPP_DEPRECATED(
"Prefer GeneralFilter::matches(...) method directly.")
753 size_t* outPayloadLength =
nullptr)
const;
768 size_t* outPayloadLength =
nullptr)
const;
786 size_t* outPayloadLength =
nullptr)
const;
789 bool sendPacketUnchecked(uint8_t
const* packetData,
int packetDataLength);
790 bool sendPacketUnchecked(
RawPacket const& rawPacket)
796 bool isNflogDevice()
const;
798 bool doUpdateFilter(std::string
const* filterAsString)
override;
Definition: PcapFilter.h:158
An interface for providing Pcap-based device statistics.
Definition: PcapDevice.h:25
Definition: IpAddress.h:30
Definition: IpAddress.h:165
Definition: MacAddress.h:24
Definition: PcapLiveDevice.h:157
PcapDirection
Definition: PcapLiveDevice.h:237
@ PCPP_IN
Only capture incoming traffics.
Definition: PcapLiveDevice.h:241
@ PCPP_OUT
Only capture outgoing traffics.
Definition: PcapLiveDevice.h:243
@ PCPP_INOUT
Capture traffics both incoming and outgoing.
Definition: PcapLiveDevice.h:239
void close() override
Close the device.
LiveDeviceType
The type of the live device.
Definition: PcapLiveDevice.h:216
@ WinPcapDevice
WinPcap/Npcap live device.
Definition: PcapLiveDevice.h:220
@ RemoteDevice
WinPcap/Npcap Remote Capture device.
Definition: PcapLiveDevice.h:222
@ LibPcapDevice
libPcap live device
Definition: PcapLiveDevice.h:218
virtual int startCaptureBlockingMode(OnPacketArrivesStopBlocking onPacketArrives, void *userCookie, const double timeout)
DeviceMode
Device capturing mode.
Definition: PcapLiveDevice.h:227
@ Normal
Only packets that their destination is this NIC are captured.
Definition: PcapLiveDevice.h:229
@ Promiscuous
All packets that arrive to the NIC are captured, even packets that their destination isn't this NIC.
Definition: PcapLiveDevice.h:231
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:390
virtual bool startCapture(OnPacketArrivesCallback onPacketArrives, void *onPacketArrivesUserCookie)
bool open(const DeviceConfiguration &config)
bool doMtuCheck(int packetPayloadLength) const
virtual MacAddress getMacAddress() const
Definition: PcapLiveDevice.h:408
bool sendPacket(const uint8_t *packetData, int packetDataLength, int packetPayloadLength)
IPv4Address getDefaultGateway() const
std::string getDesc() const
Definition: PcapLiveDevice.h:378
bool sendPacket(const uint8_t *packetData, int packetDataLength, bool checkMtu=false, pcpp::LinkLayerType linkType=pcpp::LINKTYPE_ETHERNET)
const std::vector< IPv4Address > & getDnsServers() const
void getStatistics(PcapStats &stats) const override
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.
static bool matchPacketWithFilter(GeneralFilter &filter, RawPacket *rawPacket)
virtual LiveDeviceType getDeviceType() const
Definition: PcapLiveDevice.h:365
virtual bool startCapture(OnPacketArrivesCallback onPacketArrives, void *onPacketArrivesUserCookie, int intervalInSecondsToUpdateStats, OnStatsUpdateCallback onStatsUpdate, void *onStatsUpdateUserCookie)
TimestampPrecision
Definition: PcapLiveDevice.h:267
@ 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:371
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:574
virtual int sendPackets(const RawPacketVector &rawPackets, bool checkMtu=false)
virtual int sendPackets(RawPacket *rawPacketsArr, int arrLength, bool checkMtu=false)
static std::string getPcapLibVersionInfo()
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:396
bool isOpened() const override
Definition: PcapLiveDevice.h:702
TimestampProvider
Definition: PcapLiveDevice.h:249
@ 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:384
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:402
virtual void prepareCapture(bool asyncCapture, bool captureStats)
Called before starting a capture to prepare the device for capturing packets.
Definition: PcapLiveDevice.h:730
Definition: PcapLiveDeviceList.h:22
Definition: PointerVector.h:50
Definition: RawPacket.h:261
const uint8_t * getRawData() const
Definition: RawPacket.h:426
int getRawDataLen() const
Definition: RawPacket.h:445
A wrapper class for pcap_t* which is the libpcap packet capture descriptor. This class is used to man...
Definition: PcapLiveDevice.h:31
pcap_t * release() noexcept
Releases ownership of the handle and returns the pcap descriptor.
char const * getLastError() const noexcept
Helper function to retrieve a view of the last error string for this handle.
bool setFilter(std::string const &filter)
Sets a filter on the handle. Only packets that match the filter will be captured by the handle.
void reset(pcap_t *pcapDescriptor=nullptr) noexcept
Replaces the managed handle with the provided one.
bool clearFilter()
Clears the filter currently set on the handle.
constexpr PcapHandle() noexcept=default
Creates an empty handle.
bool getStatistics(PcapStats &stats) const
Retrieves statistics from the pcap handle.
pcap_t * get() const noexcept
Definition: PcapLiveDevice.h:55
bool isValid() const noexcept
Definition: PcapLiveDevice.h:49
The main namespace for the PcapPlusPlus lib.
Definition: AssertionUtils.h:19
std::function< void(PcapStats &, void *)> OnStatsUpdateCallback
Definition: PcapLiveDevice.h:133
std::function< bool(RawPacket *, PcapLiveDevice *, void *)> OnPacketArrivesStopBlocking
Definition: PcapLiveDevice.h:127
std::function< void(RawPacket *, PcapLiveDevice *, void *)> OnPacketArrivesCallback
Definition: PcapLiveDevice.h:120
LinkLayerType
An enum describing all known link layer type. Taken from: http://www.tcpdump.org/linktypes....
Definition: RawPacket.h:22
@ LINKTYPE_ETHERNET
IEEE 802.3 Ethernet.
Definition: RawPacket.h:26
Definition: PcapLiveDevice.h:278
int packetBufferTimeoutMs
Definition: PcapLiveDevice.h:285
int snapshotLength
Definition: PcapLiveDevice.h:304
TimestampProvider timestampProvider
Definition: PcapLiveDevice.h:315
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:341
bool usePoll
In Unix-like system, use poll() for blocking mode.
Definition: PcapLiveDevice.h:311
int packetBufferSize
Definition: PcapLiveDevice.h:293
unsigned int nflogGroup
Definition: PcapLiveDevice.h:308
PcapDirection direction
Definition: PcapLiveDevice.h:297
TimestampPrecision timestampPrecision
Definition: PcapLiveDevice.h:319
DeviceMode mode
Indicates whether to open the device in promiscuous or normal mode.
Definition: PcapLiveDevice.h:280
A struct that contains all details of a network interface.
Definition: PcapLiveDevice.h:164
std::string description
Description of the device.
Definition: PcapLiveDevice.h:169
std::string name
Name of the device.
Definition: PcapLiveDevice.h:167
std::vector< IPAddress > addresses
IP addresses associated with the device.
Definition: PcapLiveDevice.h:171
bool isLoopback
Flag to indicate if the device is a loopback device.
Definition: PcapLiveDevice.h:173
Definition: PcapDevice.h:14