PcapPlusPlus  Next
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:
86  {
87  explicit DeviceInterfaceDetails(pcap_if_t* pInterface);
89  std::string name;
91  std::string description;
93  std::vector<IPAddress> addresses;
95  bool isLoopback;
96  };
97 
98  // This is a second descriptor for the same device. It is needed because of a bug
99  // that occurs in libpcap on Linux (on Windows using WinPcap/Npcap it works well):
100  // It's impossible to capture packets sent by the same descriptor
101  pcap_t* m_PcapSendDescriptor;
102  int m_PcapSelectableFd;
103  DeviceInterfaceDetails m_InterfaceDetails;
104  // NOTE@Dimi: Possibly pull mtu, mac address and default gateway in the interface details.
105  // They only appear to be set in the constructor and not modified afterwards.
106  uint32_t m_DeviceMtu;
107  MacAddress m_MacAddress;
108  IPv4Address m_DefaultGateway;
109  std::thread m_CaptureThread;
110  std::thread m_StatsThread;
111  bool m_StatsThreadStarted;
112 
113  // Should be set to true by the Caller for the Callee
114  std::atomic<bool> m_StopThread;
115  // Should be set to true by the Callee for the Caller
116  std::atomic<bool> m_CaptureThreadStarted;
117 
118  OnPacketArrivesCallback m_cbOnPacketArrives;
119  void* m_cbOnPacketArrivesUserCookie;
120  OnStatsUpdateCallback m_cbOnStatsUpdate;
121  void* m_cbOnStatsUpdateUserCookie;
122  OnPacketArrivesStopBlocking m_cbOnPacketArrivesBlockingMode;
123  void* m_cbOnPacketArrivesBlockingModeUserCookie;
124  int m_IntervalToUpdateStats;
125  RawPacketVector* m_CapturedPackets;
126  bool m_CaptureCallbackMode;
127  LinkLayerType m_LinkType;
128  bool m_UsePoll;
129 
130  // c'tor is not public, there should be only one for every interface (created by PcapLiveDeviceList)
131  PcapLiveDevice(pcap_if_t* pInterface, bool calculateMTU, bool calculateMacAddress, bool calculateDefaultGateway)
132  : PcapLiveDevice(DeviceInterfaceDetails(pInterface), calculateMTU, calculateMacAddress,
133  calculateDefaultGateway)
134  {}
135  PcapLiveDevice(DeviceInterfaceDetails interfaceDetails, bool calculateMTU, bool calculateMacAddress,
136  bool calculateDefaultGateway);
137 
138  void setDeviceMtu();
139  void setDeviceMacAddress();
140  void setDefaultGateway();
141 
142  // threads
143  void captureThreadMain();
144  void statsThreadMain();
145 
146  static void onPacketArrives(uint8_t* user, const struct pcap_pkthdr* pkthdr, const uint8_t* packet);
147  static void onPacketArrivesNoCallback(uint8_t* user, const struct pcap_pkthdr* pkthdr, const uint8_t* packet);
148  static void onPacketArrivesBlockingMode(uint8_t* user, const struct pcap_pkthdr* pkthdr, const uint8_t* packet);
149 
150  public:
155  {
162  };
163 
168  {
170  Normal = 0,
172  Promiscuous = 1
173  };
174 
180  {
186  PCPP_OUT
187  };
188 
195  {
198 
204 
214 
220 
229 
234  unsigned int nflogGroup;
235 
237  bool usePoll;
238 
258  int snapshotLength = 0, unsigned int nflogGroup = 0, bool usePoll = false)
259  {
260  this->mode = mode;
261  this->packetBufferTimeoutMs = packetBufferTimeoutMs;
262  this->packetBufferSize = packetBufferSize;
263  this->direction = direction;
264  this->snapshotLength = snapshotLength;
265  this->nflogGroup = nflogGroup;
266  this->usePoll = usePoll;
267  }
268  };
269 
270  PcapLiveDevice(const PcapLiveDevice& other) = delete;
271  PcapLiveDevice& operator=(const PcapLiveDevice& other) = delete;
275  ~PcapLiveDevice() override;
276 
281  {
282  return LibPcapDevice;
283  }
284 
288  std::string getName() const
289  {
290  return m_InterfaceDetails.name;
291  }
292 
297  std::string getDesc() const
298  {
299  return m_InterfaceDetails.description;
300  }
301 
305  bool getLoopback() const
306  {
307  return m_InterfaceDetails.isLoopback;
308  }
309 
313  virtual uint32_t getMtu() const
314  {
315  return m_DeviceMtu;
316  }
317 
321  virtual LinkLayerType getLinkType() const
322  {
323  return m_LinkType;
324  }
325 
329  std::vector<IPAddress> getIPAddresses() const
330  {
331  return m_InterfaceDetails.addresses;
332  }
333 
337  virtual MacAddress getMacAddress() const
338  {
339  return m_MacAddress;
340  }
341 
348 
355 
362 
369  const std::vector<IPv4Address>& getDnsServers() const;
370 
387  virtual bool startCapture(OnPacketArrivesCallback onPacketArrives, void* onPacketArrivesUserCookie);
388 
413  virtual bool startCapture(OnPacketArrivesCallback onPacketArrives, void* onPacketArrivesUserCookie,
414  int intervalInSecondsToUpdateStats, OnStatsUpdateCallback onStatsUpdate,
415  void* onStatsUpdateUserCookie);
416 
435  virtual bool startCapture(int intervalInSecondsToUpdateStats, OnStatsUpdateCallback onStatsUpdate,
436  void* onStatsUpdateUserCookie);
437 
453  virtual bool startCapture(RawPacketVector& capturedPacketsVector);
454 
479  virtual int startCaptureBlockingMode(OnPacketArrivesStopBlocking onPacketArrives, void* userCookie,
480  const double timeout);
481 
486  void stopCapture();
487 
493 
499  bool doMtuCheck(int packetPayloadLength) const;
500 
515  bool sendPacket(RawPacket const& rawPacket, bool checkMtu = false);
516 
534  bool sendPacket(const uint8_t* packetData, int packetDataLength, int packetPayloadLength);
535 
553  bool sendPacket(const uint8_t* packetData, int packetDataLength, bool checkMtu = false,
555 
570  bool sendPacket(Packet* packet, bool checkMtu = true);
571 
586  virtual int sendPackets(RawPacket* rawPacketsArr, int arrLength, bool checkMtu = false);
587 
602  virtual int sendPackets(Packet** packetsArr, int arrLength, bool checkMtu = true);
603 
617  virtual int sendPackets(const RawPacketVector& rawPackets, bool checkMtu = false);
618 
619  // implement abstract methods
620 
629  bool open() override;
630 
638  bool open(const DeviceConfiguration& config);
639 
640  void close() override;
641 
646  virtual PcapLiveDevice* clone() const;
647 
648  void getStatistics(IPcapDevice::PcapStats& stats) const override;
649 
650  protected:
651  pcap_t* doOpen(const DeviceConfiguration& config);
652  };
653 
654 } // 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:180
@ PCPP_IN
Definition: PcapLiveDevice.h:184
@ PCPP_OUT
Definition: PcapLiveDevice.h:186
@ PCPP_INOUT
Definition: PcapLiveDevice.h:182
void close() override
LiveDeviceType
Definition: PcapLiveDevice.h:155
@ WinPcapDevice
Definition: PcapLiveDevice.h:159
@ RemoteDevice
Definition: PcapLiveDevice.h:161
@ LibPcapDevice
Definition: PcapLiveDevice.h:157
virtual int startCaptureBlockingMode(OnPacketArrivesStopBlocking onPacketArrives, void *userCookie, const double timeout)
DeviceMode
Definition: PcapLiveDevice.h:168
@ Normal
Definition: PcapLiveDevice.h:170
@ Promiscuous
Definition: PcapLiveDevice.h:172
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:313
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:337
bool sendPacket(const uint8_t *packetData, int packetDataLength, int packetPayloadLength)
IPv4Address getDefaultGateway() const
std::string getDesc() const
Definition: PcapLiveDevice.h:297
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:280
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:288
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:321
bool getLoopback() const
Definition: PcapLiveDevice.h:305
virtual PcapLiveDevice * clone() const
bool open() override
std::vector< IPAddress > getIPAddresses() const
Definition: PcapLiveDevice.h:329
Definition: PcapLiveDeviceList.h:25
Definition: PointerVector.h:58
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:195
int packetBufferTimeoutMs
Definition: PcapLiveDevice.h:203
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:256
int snapshotLength
Definition: PcapLiveDevice.h:228
bool usePoll
In Unix-like system, use poll() for blocking mode.
Definition: PcapLiveDevice.h:237
int packetBufferSize
Definition: PcapLiveDevice.h:213
unsigned int nflogGroup
Definition: PcapLiveDevice.h:234
PcapDirection direction
Definition: PcapLiveDevice.h:219
DeviceMode mode
Definition: PcapLiveDevice.h:197
A struct that contains all details of a network interface.
Definition: PcapLiveDevice.h:86
std::string description
Description of the device.
Definition: PcapLiveDevice.h:91
std::string name
Name of the device.
Definition: PcapLiveDevice.h:89
std::vector< IPAddress > addresses
IP addresses associated with the device.
Definition: PcapLiveDevice.h:93
bool isLoopback
Flag to indicate if the device is a loopback device.
Definition: PcapLiveDevice.h:95