PcapPlusPlus  Next
PcapLiveDevice.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <atomic>
4 #include <vector>
5 #include <thread>
6 #include <functional>
7 
8 #include "IpAddress.h"
9 #include "PcapDevice.h"
10 
11 // forward declarations for structs and typedefs that are defined in pcap.h
12 struct pcap_if;
13 typedef pcap_if pcap_if_t;
14 struct pcap_addr;
15 typedef struct pcap_addr pcap_addr_t;
16 
18 
21 namespace pcpp
22 {
23  class PcapLiveDevice;
24 
29  using OnPacketArrivesCallback = std::function<void(RawPacket*, PcapLiveDevice*, void*)>;
30 
36  using OnPacketArrivesStopBlocking = std::function<bool(RawPacket*, PcapLiveDevice*, void*)>;
37 
42  using OnStatsUpdateCallback = std::function<void(IPcapDevice::PcapStats&, void*)>;
43 
65  class PcapLiveDevice : public IPcapDevice
66  {
67  friend class PcapLiveDeviceList;
68 
69  protected:
73  {
74  explicit DeviceInterfaceDetails(pcap_if_t* pInterface);
76  std::string name;
78  std::string description;
80  std::vector<IPAddress> addresses;
82  bool isLoopback;
83  };
84 
85  // This is a second descriptor for the same device. It is needed because of a bug
86  // that occurs in libpcap on Linux (on Windows using WinPcap/Npcap it works well):
87  // It's impossible to capture packets sent by the same descriptor
88  pcap_t* m_PcapSendDescriptor;
89  int m_PcapSelectableFd;
90  DeviceInterfaceDetails m_InterfaceDetails;
91  // NOTE@Dimi: Possibly pull mtu, mac address and default gateway in the interface details.
92  // They only appear to be set in the constructor and not modified afterwards.
93  uint32_t m_DeviceMtu;
94  MacAddress m_MacAddress;
95  IPv4Address m_DefaultGateway;
96  std::thread m_CaptureThread;
97  std::thread m_StatsThread;
98  bool m_StatsThreadStarted;
99 
100  // Should be set to true by the Caller for the Callee
101  std::atomic<bool> m_StopThread;
102  // Should be set to true by the Callee for the Caller
103  std::atomic<bool> m_CaptureThreadStarted;
104 
105  OnPacketArrivesCallback m_cbOnPacketArrives;
106  void* m_cbOnPacketArrivesUserCookie;
107  OnStatsUpdateCallback m_cbOnStatsUpdate;
108  void* m_cbOnStatsUpdateUserCookie;
109  OnPacketArrivesStopBlocking m_cbOnPacketArrivesBlockingMode;
110  void* m_cbOnPacketArrivesBlockingModeUserCookie;
111  int m_IntervalToUpdateStats;
112  RawPacketVector* m_CapturedPackets;
113  bool m_CaptureCallbackMode;
114  LinkLayerType m_LinkType;
115  bool m_UsePoll;
116 
117  // c'tor is not public, there should be only one for every interface (created by PcapLiveDeviceList)
118  PcapLiveDevice(pcap_if_t* pInterface, bool calculateMTU, bool calculateMacAddress, bool calculateDefaultGateway)
119  : PcapLiveDevice(DeviceInterfaceDetails(pInterface), calculateMTU, calculateMacAddress,
120  calculateDefaultGateway)
121  {}
122  PcapLiveDevice(DeviceInterfaceDetails interfaceDetails, bool calculateMTU, bool calculateMacAddress,
123  bool calculateDefaultGateway);
124 
125  void setDeviceMtu();
126  void setDeviceMacAddress();
127  void setDefaultGateway();
128 
129  // threads
130  void captureThreadMain();
131  void statsThreadMain();
132 
133  static void onPacketArrives(uint8_t* user, const struct pcap_pkthdr* pkthdr, const uint8_t* packet);
134  static void onPacketArrivesNoCallback(uint8_t* user, const struct pcap_pkthdr* pkthdr, const uint8_t* packet);
135  static void onPacketArrivesBlockingMode(uint8_t* user, const struct pcap_pkthdr* pkthdr, const uint8_t* packet);
136 
137  public:
140  {
147  };
148 
151  {
153  Normal = 0,
155  Promiscuous = 1
156  };
157 
161  {
167  PCPP_OUT
168  };
169 
172  enum class TimestampProvider
173  {
175  Host = 0,
181  Adapter,
186  };
187 
191  {
193  Microseconds = 0,
195  Nanoseconds,
196  };
197 
202  {
205 
210 
218 
222 
229 
232  unsigned int nflogGroup;
233 
235  bool usePoll;
236 
240 
244 
267  int snapshotLength = 0, unsigned int nflogGroup = 0, bool usePoll = false,
270  {
271  this->mode = mode;
272  this->packetBufferTimeoutMs = packetBufferTimeoutMs;
273  this->packetBufferSize = packetBufferSize;
274  this->direction = direction;
275  this->snapshotLength = snapshotLength;
276  this->nflogGroup = nflogGroup;
277  this->usePoll = usePoll;
278  this->timestampProvider = timestampProvider;
279  this->timestampPrecision = timestampPrecision;
280  }
281  };
282 
283  PcapLiveDevice(const PcapLiveDevice& other) = delete;
284  PcapLiveDevice& operator=(const PcapLiveDevice& other) = delete;
286  ~PcapLiveDevice() override;
287 
290  {
291  return LibPcapDevice;
292  }
293 
295  std::string getName() const
296  {
297  return m_InterfaceDetails.name;
298  }
299 
302  std::string getDesc() const
303  {
304  return m_InterfaceDetails.description;
305  }
306 
308  bool getLoopback() const
309  {
310  return m_InterfaceDetails.isLoopback;
311  }
312 
314  virtual uint32_t getMtu() const
315  {
316  return m_DeviceMtu;
317  }
318 
320  virtual LinkLayerType getLinkType() const
321  {
322  return m_LinkType;
323  }
324 
326  std::vector<IPAddress> getIPAddresses() const
327  {
328  return m_InterfaceDetails.addresses;
329  }
330 
332  virtual MacAddress getMacAddress() const
333  {
334  return m_MacAddress;
335  }
336 
341 
346 
351 
356  const std::vector<IPv4Address>& getDnsServers() const;
357 
372  virtual bool startCapture(OnPacketArrivesCallback onPacketArrives, void* onPacketArrivesUserCookie);
373 
396  virtual bool startCapture(OnPacketArrivesCallback onPacketArrives, void* onPacketArrivesUserCookie,
397  int intervalInSecondsToUpdateStats, OnStatsUpdateCallback onStatsUpdate,
398  void* onStatsUpdateUserCookie);
399 
416  virtual bool startCapture(int intervalInSecondsToUpdateStats, OnStatsUpdateCallback onStatsUpdate,
417  void* onStatsUpdateUserCookie);
418 
432  virtual bool startCapture(RawPacketVector& capturedPacketsVector);
433 
457  virtual int startCaptureBlockingMode(OnPacketArrivesStopBlocking onPacketArrives, void* userCookie,
458  const double timeout);
459 
462  void stopCapture();
463 
467 
471  bool doMtuCheck(int packetPayloadLength) const;
472 
485  bool sendPacket(RawPacket const& rawPacket, bool checkMtu = false);
486 
502  bool sendPacket(const uint8_t* packetData, int packetDataLength, int packetPayloadLength);
503 
519  bool sendPacket(const uint8_t* packetData, int packetDataLength, bool checkMtu = false,
521 
534  bool sendPacket(Packet* packet, bool checkMtu = true);
535 
548  virtual int sendPackets(RawPacket* rawPacketsArr, int arrLength, bool checkMtu = false);
549 
562  virtual int sendPackets(Packet** packetsArr, int arrLength, bool checkMtu = true);
563 
575  virtual int sendPackets(const RawPacketVector& rawPackets, bool checkMtu = false);
576 
577  // implement abstract methods
578 
585  bool open() override;
586 
592  bool open(const DeviceConfiguration& config);
593 
594  void close() override;
595 
598  virtual PcapLiveDevice* clone() const;
599 
600  void getStatistics(IPcapDevice::PcapStats& stats) const override;
601 
602  protected:
603  internal::PcapHandle doOpen(const DeviceConfiguration& config);
604 
605  private:
606  bool isNflogDevice() const;
607  };
608 } // namespace pcpp
Definition: PcapDevice.h:104
Definition: IpAddress.h:28
Definition: IpAddress.h:156
Definition: MacAddress.h:21
Definition: Packet.h:22
Definition: PcapLiveDevice.h:66
PcapDirection
Definition: PcapLiveDevice.h:161
@ PCPP_IN
Only capture incoming traffics.
Definition: PcapLiveDevice.h:165
@ PCPP_OUT
Only capture outgoing traffics.
Definition: PcapLiveDevice.h:167
@ PCPP_INOUT
Capture traffics both incoming and outgoing.
Definition: PcapLiveDevice.h:163
void close() override
Close the device.
LiveDeviceType
The type of the live device.
Definition: PcapLiveDevice.h:140
@ WinPcapDevice
WinPcap/Npcap live device.
Definition: PcapLiveDevice.h:144
@ RemoteDevice
WinPcap/Npcap Remote Capture device.
Definition: PcapLiveDevice.h:146
@ LibPcapDevice
libPcap live device
Definition: PcapLiveDevice.h:142
virtual int startCaptureBlockingMode(OnPacketArrivesStopBlocking onPacketArrives, void *userCookie, const double timeout)
DeviceMode
Device capturing mode.
Definition: PcapLiveDevice.h:151
@ Normal
Only packets that their destination is this NIC are captured.
Definition: PcapLiveDevice.h:153
@ Promiscuous
All packets that arrive to the NIC are captured, even packets that their destination isn't this NIC.
Definition: PcapLiveDevice.h:155
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:314
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:332
bool sendPacket(const uint8_t *packetData, int packetDataLength, int packetPayloadLength)
IPv4Address getDefaultGateway() const
std::string getDesc() const
Definition: PcapLiveDevice.h:302
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:289
virtual bool startCapture(OnPacketArrivesCallback onPacketArrives, void *onPacketArrivesUserCookie, int intervalInSecondsToUpdateStats, OnStatsUpdateCallback onStatsUpdate, void *onStatsUpdateUserCookie)
TimestampPrecision
Definition: PcapLiveDevice.h:191
@ 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.
std::string getName() const
Definition: PcapLiveDevice.h:295
IPv4Address getIPv4Address() const
bool sendPacket(Packet *packet, bool checkMtu=true)
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:320
TimestampProvider
Definition: PcapLiveDevice.h:173
@ 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:308
virtual PcapLiveDevice * clone() const
bool open() override
std::vector< IPAddress > getIPAddresses() const
Definition: PcapLiveDevice.h:326
Definition: PcapLiveDeviceList.h:20
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:25
The main namespace for the PcapPlusPlus lib.
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: PcapDevice.h:116
Definition: PcapLiveDevice.h:202
int packetBufferTimeoutMs
Definition: PcapLiveDevice.h:209
int snapshotLength
Definition: PcapLiveDevice.h:228
TimestampProvider timestampProvider
Definition: PcapLiveDevice.h:239
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:265
bool usePoll
In Unix-like system, use poll() for blocking mode.
Definition: PcapLiveDevice.h:235
int packetBufferSize
Definition: PcapLiveDevice.h:217
unsigned int nflogGroup
Definition: PcapLiveDevice.h:232
PcapDirection direction
Definition: PcapLiveDevice.h:221
TimestampPrecision timestampPrecision
Definition: PcapLiveDevice.h:243
DeviceMode mode
Indicates whether to open the device in promiscuous or normal mode.
Definition: PcapLiveDevice.h:204
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