PcapPlusPlus  24.09
PcapLiveDevice.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <atomic>
4 #include <vector>
5 #include <string.h>
6 #include <thread>
7 #include <functional>
8 
9 #include "IpAddress.h"
10 #include "Packet.h"
11 #include "PcapDevice.h"
12 
13 // forward declarations for structs and typedefs that are defined in pcap.h
14 struct pcap_if;
15 typedef pcap_if pcap_if_t;
16 struct pcap_addr;
17 typedef struct pcap_addr pcap_addr_t;
18 
20 
25 namespace pcpp
26 {
27 
28  class PcapLiveDevice;
29 
36  using OnPacketArrivesCallback = std::function<void(RawPacket*, PcapLiveDevice*, void*)>;
37 
45  using OnPacketArrivesStopBlocking = std::function<bool(RawPacket*, PcapLiveDevice*, void*)>;
46 
53  using OnStatsUpdateCallback = std::function<void(IPcapDevice::PcapStats&, void*)>;
54 
78  class PcapLiveDevice : public IPcapDevice
79  {
80  friend class PcapLiveDeviceList;
81 
82  protected:
83  // This is a second descriptor for the same device. It is needed because of a bug
84  // that occurs in libpcap on Linux (on Windows using WinPcap/Npcap it works well):
85  // It's impossible to capture packets sent by the same descriptor
86  pcap_t* m_PcapSendDescriptor;
87  int m_PcapSelectableFd;
88  std::string m_Name;
89  std::string m_Description;
90  bool m_IsLoopback;
91  uint32_t m_DeviceMtu;
92  std::vector<pcap_addr_t> m_Addresses;
93  MacAddress m_MacAddress;
94  IPv4Address m_DefaultGateway;
95  std::thread m_CaptureThread;
96  std::thread m_StatsThread;
97  bool m_StatsThreadStarted;
98 
99  // Should be set to true by the Caller for the Callee
100  std::atomic<bool> m_StopThread;
101  // Should be set to true by the Callee for the Caller
102  std::atomic<bool> m_CaptureThreadStarted;
103 
104  OnPacketArrivesCallback m_cbOnPacketArrives;
105  void* m_cbOnPacketArrivesUserCookie;
106  OnStatsUpdateCallback m_cbOnStatsUpdate;
107  void* m_cbOnStatsUpdateUserCookie;
108  OnPacketArrivesStopBlocking m_cbOnPacketArrivesBlockingMode;
109  void* m_cbOnPacketArrivesBlockingModeUserCookie;
110  int m_IntervalToUpdateStats;
111  RawPacketVector* m_CapturedPackets;
112  bool m_CaptureCallbackMode;
113  LinkLayerType m_LinkType;
114  bool m_UsePoll;
115 
116  // c'tor is not public, there should be only one for every interface (created by PcapLiveDeviceList)
117  PcapLiveDevice(pcap_if_t* pInterface, bool calculateMTU, bool calculateMacAddress,
118  bool calculateDefaultGateway);
119  // copy c'tor is not public
120  PcapLiveDevice(const PcapLiveDevice& other);
121  PcapLiveDevice& operator=(const PcapLiveDevice& other);
122 
123  void setDeviceMtu();
124  void setDeviceMacAddress();
125  void setDefaultGateway();
126 
127  // threads
128  void captureThreadMain();
129  void statsThreadMain();
130 
131  static void onPacketArrives(uint8_t* user, const struct pcap_pkthdr* pkthdr, const uint8_t* packet);
132  static void onPacketArrivesNoCallback(uint8_t* user, const struct pcap_pkthdr* pkthdr, const uint8_t* packet);
133  static void onPacketArrivesBlockingMode(uint8_t* user, const struct pcap_pkthdr* pkthdr, const uint8_t* packet);
134 
135  public:
140  {
147  };
148 
153  {
155  Normal = 0,
157  Promiscuous = 1
158  };
159 
165  {
171  PCPP_OUT
172  };
173 
180  {
183 
189 
199 
205 
214 
219  unsigned int nflogGroup;
220 
222  bool usePoll;
223 
243  int snapshotLength = 0, unsigned int nflogGroup = 0, bool usePoll = false)
244  {
245  this->mode = mode;
246  this->packetBufferTimeoutMs = packetBufferTimeoutMs;
247  this->packetBufferSize = packetBufferSize;
248  this->direction = direction;
249  this->snapshotLength = snapshotLength;
250  this->nflogGroup = nflogGroup;
251  this->usePoll = usePoll;
252  }
253  };
254 
258  ~PcapLiveDevice() override;
259 
264  {
265  return LibPcapDevice;
266  }
267 
271  std::string getName() const
272  {
273  return m_Name;
274  }
275 
280  std::string getDesc() const
281  {
282  return m_Description;
283  }
284 
288  bool getLoopback() const
289  {
290  return m_IsLoopback;
291  }
292 
296  virtual uint32_t getMtu() const
297  {
298  return m_DeviceMtu;
299  }
300 
304  virtual LinkLayerType getLinkType() const
305  {
306  return m_LinkType;
307  }
308 
314  // clang-format off
315  // Breaking the macro into multiple lines causes doxygen to cause a fit.
316  PCPP_DEPRECATED("This method is deprecated and will be removed in future versions. Please use getIPAddresses() instead.")
317  // clang-format on
318  const std::vector<pcap_addr_t>& getAddresses() const
319  {
320  return m_Addresses;
321  }
322 
326  std::vector<IPAddress> getIPAddresses() const;
327 
331  virtual MacAddress getMacAddress() const
332  {
333  return m_MacAddress;
334  }
335 
342 
349 
356 
363  const std::vector<IPv4Address>& getDnsServers() const;
364 
381  virtual bool startCapture(OnPacketArrivesCallback onPacketArrives, void* onPacketArrivesUserCookie);
382 
407  virtual bool startCapture(OnPacketArrivesCallback onPacketArrives, void* onPacketArrivesUserCookie,
408  int intervalInSecondsToUpdateStats, OnStatsUpdateCallback onStatsUpdate,
409  void* onStatsUpdateUserCookie);
410 
429  virtual bool startCapture(int intervalInSecondsToUpdateStats, OnStatsUpdateCallback onStatsUpdate,
430  void* onStatsUpdateUserCookie);
431 
447  virtual bool startCapture(RawPacketVector& capturedPacketsVector);
448 
473  virtual int startCaptureBlockingMode(OnPacketArrivesStopBlocking onPacketArrives, void* userCookie,
474  const double timeout);
475 
480  void stopCapture();
481 
487 
493  bool doMtuCheck(int packetPayloadLength) const;
494 
509  bool sendPacket(RawPacket const& rawPacket, bool checkMtu = false);
510 
528  bool sendPacket(const uint8_t* packetData, int packetDataLength, int packetPayloadLength);
529 
547  bool sendPacket(const uint8_t* packetData, int packetDataLength, bool checkMtu = false,
549 
564  bool sendPacket(Packet* packet, bool checkMtu = true);
565 
580  virtual int sendPackets(RawPacket* rawPacketsArr, int arrLength, bool checkMtu = false);
581 
596  virtual int sendPackets(Packet** packetsArr, int arrLength, bool checkMtu = true);
597 
611  virtual int sendPackets(const RawPacketVector& rawPackets, bool checkMtu = false);
612 
613  // implement abstract methods
614 
623  bool open() override;
624 
632  bool open(const DeviceConfiguration& config);
633 
634  void close() override;
635 
641 
642  void getStatistics(IPcapDevice::PcapStats& stats) const override;
643 
644  protected:
645  pcap_t* doOpen(const DeviceConfiguration& config);
646 
647  virtual PcapLiveDevice* cloneInternal(pcap_if_t& devInterface) const;
648  };
649 
650 } // namespace pcpp
Definition: PcapDevice.h:114
Definition: IpAddress.h:32
Definition: IpAddress.h:199
Definition: MacAddress.h:25
Definition: Packet.h:27
Definition: PcapLiveDevice.h:79
PcapDirection
Definition: PcapLiveDevice.h:165
@ PCPP_IN
Definition: PcapLiveDevice.h:169
@ PCPP_OUT
Definition: PcapLiveDevice.h:171
@ PCPP_INOUT
Definition: PcapLiveDevice.h:167
void close() override
LiveDeviceType
Definition: PcapLiveDevice.h:140
@ WinPcapDevice
Definition: PcapLiveDevice.h:144
@ RemoteDevice
Definition: PcapLiveDevice.h:146
@ LibPcapDevice
Definition: PcapLiveDevice.h:142
virtual int startCaptureBlockingMode(OnPacketArrivesStopBlocking onPacketArrives, void *userCookie, const double timeout)
DeviceMode
Definition: PcapLiveDevice.h:153
@ Normal
Definition: PcapLiveDevice.h:155
@ Promiscuous
Definition: PcapLiveDevice.h:157
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:296
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:331
bool sendPacket(const uint8_t *packetData, int packetDataLength, int packetPayloadLength)
IPv4Address getDefaultGateway() const
std::string getDesc() const
Definition: PcapLiveDevice.h:280
bool sendPacket(const uint8_t *packetData, int packetDataLength, bool checkMtu=false, pcpp::LinkLayerType linkType=pcpp::LINKTYPE_ETHERNET)
const std::vector< pcap_addr_t > & getAddresses() const
Definition: PcapLiveDevice.h:318
const std::vector< IPv4Address > & getDnsServers() const
virtual int sendPackets(Packet **packetsArr, int arrLength, bool checkMtu=true)
virtual LiveDeviceType getDeviceType() const
Definition: PcapLiveDevice.h:263
virtual bool startCapture(OnPacketArrivesCallback onPacketArrives, void *onPacketArrivesUserCookie, int intervalInSecondsToUpdateStats, OnStatsUpdateCallback onStatsUpdate, void *onStatsUpdateUserCookie)
IPv6Address getIPv6Address() const
virtual bool startCapture(RawPacketVector &capturedPacketsVector)
~PcapLiveDevice() override
std::string getName() const
Definition: PcapLiveDevice.h:271
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:304
PcapLiveDevice * clone() const
bool getLoopback() const
Definition: PcapLiveDevice.h:288
bool open() override
std::vector< IPAddress > getIPAddresses() const
Definition: PcapLiveDeviceList.h:25
Definition: PointerVector.h:29
Definition: RawPacket.h:269
The main namespace for the PcapPlusPlus lib.
std::function< bool(RawPacket *, PcapLiveDevice *, void *)> OnPacketArrivesStopBlocking
Definition: PcapLiveDevice.h:45
std::function< void(RawPacket *, PcapLiveDevice *, void *)> OnPacketArrivesCallback
Definition: PcapLiveDevice.h:36
std::function< void(IPcapDevice::PcapStats &, void *)> OnStatsUpdateCallback
Definition: PcapLiveDevice.h:53
LinkLayerType
Definition: RawPacket.h:25
@ LINKTYPE_ETHERNET
Definition: RawPacket.h:29
Definition: PcapDevice.h:128
Definition: PcapLiveDevice.h:180
int packetBufferTimeoutMs
Definition: PcapLiveDevice.h:188
DeviceConfiguration(DeviceMode mode=Promiscuous, int packetBufferTimeoutMs=0, int packetBufferSize=0, PcapDirection direction=PCPP_INOUT, int snapshotLength=0, unsigned int nflogGroup=0, bool usePoll=false)
Definition: PcapLiveDevice.h:241
int snapshotLength
Definition: PcapLiveDevice.h:213
bool usePoll
In Unix-like system, use poll() for blocking mode.
Definition: PcapLiveDevice.h:222
int packetBufferSize
Definition: PcapLiveDevice.h:198
unsigned int nflogGroup
Definition: PcapLiveDevice.h:219
PcapDirection direction
Definition: PcapLiveDevice.h:204
DeviceMode mode
Definition: PcapLiveDevice.h:182