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 
23 namespace pcpp
24 {
25  class PcapLiveDevice;
26 
31  using OnPacketArrivesCallback = std::function<void(RawPacket*, PcapLiveDevice*, void*)>;
32 
38  using OnPacketArrivesStopBlocking = std::function<bool(RawPacket*, PcapLiveDevice*, void*)>;
39 
44  using OnStatsUpdateCallback = std::function<void(IPcapDevice::PcapStats&, void*)>;
45 
67  class PcapLiveDevice : public IPcapDevice
68  {
69  friend class PcapLiveDeviceList;
70 
71  protected:
75  {
76  explicit DeviceInterfaceDetails(pcap_if_t* pInterface);
78  std::string name;
80  std::string description;
82  std::vector<IPAddress> addresses;
84  bool isLoopback;
85  };
86 
87  // This is a second descriptor for the same device. It is needed because of a bug
88  // that occurs in libpcap on Linux (on Windows using WinPcap/Npcap it works well):
89  // It's impossible to capture packets sent by the same descriptor
90  pcap_t* m_PcapSendDescriptor;
91  int m_PcapSelectableFd;
92  DeviceInterfaceDetails m_InterfaceDetails;
93  // NOTE@Dimi: Possibly pull mtu, mac address and default gateway in the interface details.
94  // They only appear to be set in the constructor and not modified afterwards.
95  uint32_t m_DeviceMtu;
96  MacAddress m_MacAddress;
97  IPv4Address m_DefaultGateway;
98  std::thread m_CaptureThread;
99  std::thread m_StatsThread;
100  bool m_StatsThreadStarted;
101 
102  // Should be set to true by the Caller for the Callee
103  std::atomic<bool> m_StopThread;
104  // Should be set to true by the Callee for the Caller
105  std::atomic<bool> m_CaptureThreadStarted;
106 
107  OnPacketArrivesCallback m_cbOnPacketArrives;
108  void* m_cbOnPacketArrivesUserCookie;
109  OnStatsUpdateCallback m_cbOnStatsUpdate;
110  void* m_cbOnStatsUpdateUserCookie;
111  OnPacketArrivesStopBlocking m_cbOnPacketArrivesBlockingMode;
112  void* m_cbOnPacketArrivesBlockingModeUserCookie;
113  int m_IntervalToUpdateStats;
114  RawPacketVector* m_CapturedPackets;
115  bool m_CaptureCallbackMode;
116  LinkLayerType m_LinkType;
117  bool m_UsePoll;
118 
119  // c'tor is not public, there should be only one for every interface (created by PcapLiveDeviceList)
120  PcapLiveDevice(pcap_if_t* pInterface, bool calculateMTU, bool calculateMacAddress, bool calculateDefaultGateway)
121  : PcapLiveDevice(DeviceInterfaceDetails(pInterface), calculateMTU, calculateMacAddress,
122  calculateDefaultGateway)
123  {}
124  PcapLiveDevice(DeviceInterfaceDetails interfaceDetails, bool calculateMTU, bool calculateMacAddress,
125  bool calculateDefaultGateway);
126 
127  void setDeviceMtu();
128  void setDeviceMacAddress();
129  void setDefaultGateway();
130 
131  // threads
132  void captureThreadMain();
133  void statsThreadMain();
134 
135  static void onPacketArrives(uint8_t* user, const struct pcap_pkthdr* pkthdr, const uint8_t* packet);
136  static void onPacketArrivesNoCallback(uint8_t* user, const struct pcap_pkthdr* pkthdr, const uint8_t* packet);
137  static void onPacketArrivesBlockingMode(uint8_t* user, const struct pcap_pkthdr* pkthdr, const uint8_t* packet);
138 
139  public:
142  {
149  };
150 
153  {
155  Normal = 0,
157  Promiscuous = 1
158  };
159 
163  {
169  PCPP_OUT
170  };
171 
176  {
179 
184 
192 
196 
203 
206  unsigned int nflogGroup;
207 
209  bool usePoll;
210 
229  int snapshotLength = 0, unsigned int nflogGroup = 0, bool usePoll = false)
230  {
231  this->mode = mode;
232  this->packetBufferTimeoutMs = packetBufferTimeoutMs;
233  this->packetBufferSize = packetBufferSize;
234  this->direction = direction;
235  this->snapshotLength = snapshotLength;
236  this->nflogGroup = nflogGroup;
237  this->usePoll = usePoll;
238  }
239  };
240 
241  PcapLiveDevice(const PcapLiveDevice& other) = delete;
242  PcapLiveDevice& operator=(const PcapLiveDevice& other) = delete;
244  ~PcapLiveDevice() override;
245 
248  {
249  return LibPcapDevice;
250  }
251 
253  std::string getName() const
254  {
255  return m_InterfaceDetails.name;
256  }
257 
260  std::string getDesc() const
261  {
262  return m_InterfaceDetails.description;
263  }
264 
266  bool getLoopback() const
267  {
268  return m_InterfaceDetails.isLoopback;
269  }
270 
272  virtual uint32_t getMtu() const
273  {
274  return m_DeviceMtu;
275  }
276 
278  virtual LinkLayerType getLinkType() const
279  {
280  return m_LinkType;
281  }
282 
284  std::vector<IPAddress> getIPAddresses() const
285  {
286  return m_InterfaceDetails.addresses;
287  }
288 
290  virtual MacAddress getMacAddress() const
291  {
292  return m_MacAddress;
293  }
294 
299 
304 
309 
314  const std::vector<IPv4Address>& getDnsServers() const;
315 
330  virtual bool startCapture(OnPacketArrivesCallback onPacketArrives, void* onPacketArrivesUserCookie);
331 
354  virtual bool startCapture(OnPacketArrivesCallback onPacketArrives, void* onPacketArrivesUserCookie,
355  int intervalInSecondsToUpdateStats, OnStatsUpdateCallback onStatsUpdate,
356  void* onStatsUpdateUserCookie);
357 
374  virtual bool startCapture(int intervalInSecondsToUpdateStats, OnStatsUpdateCallback onStatsUpdate,
375  void* onStatsUpdateUserCookie);
376 
390  virtual bool startCapture(RawPacketVector& capturedPacketsVector);
391 
415  virtual int startCaptureBlockingMode(OnPacketArrivesStopBlocking onPacketArrives, void* userCookie,
416  const double timeout);
417 
420  void stopCapture();
421 
425 
429  bool doMtuCheck(int packetPayloadLength) const;
430 
443  bool sendPacket(RawPacket const& rawPacket, bool checkMtu = false);
444 
460  bool sendPacket(const uint8_t* packetData, int packetDataLength, int packetPayloadLength);
461 
477  bool sendPacket(const uint8_t* packetData, int packetDataLength, bool checkMtu = false,
479 
492  bool sendPacket(Packet* packet, bool checkMtu = true);
493 
506  virtual int sendPackets(RawPacket* rawPacketsArr, int arrLength, bool checkMtu = false);
507 
520  virtual int sendPackets(Packet** packetsArr, int arrLength, bool checkMtu = true);
521 
533  virtual int sendPackets(const RawPacketVector& rawPackets, bool checkMtu = false);
534 
535  // implement abstract methods
536 
543  bool open() override;
544 
550  bool open(const DeviceConfiguration& config);
551 
552  void close() override;
553 
556  virtual PcapLiveDevice* clone() const;
557 
558  void getStatistics(IPcapDevice::PcapStats& stats) const override;
559 
560  protected:
561  pcap_t* doOpen(const DeviceConfiguration& config);
562  };
563 } // namespace pcpp
Definition: PcapDevice.h:92
Definition: IpAddress.h:28
Definition: IpAddress.h:156
Definition: MacAddress.h:21
Definition: Packet.h:27
Definition: PcapLiveDevice.h:68
PcapDirection
Definition: PcapLiveDevice.h:163
@ PCPP_IN
Only capture incoming traffics.
Definition: PcapLiveDevice.h:167
@ PCPP_OUT
Only capture outgoing traffics.
Definition: PcapLiveDevice.h:169
@ PCPP_INOUT
Capture traffics both incoming and outgoing.
Definition: PcapLiveDevice.h:165
void close() override
Close the device.
LiveDeviceType
The type of the live device.
Definition: PcapLiveDevice.h:142
@ WinPcapDevice
WinPcap/Npcap live device.
Definition: PcapLiveDevice.h:146
@ RemoteDevice
WinPcap/Npcap Remote Capture device.
Definition: PcapLiveDevice.h:148
@ LibPcapDevice
libPcap live device
Definition: PcapLiveDevice.h:144
virtual int startCaptureBlockingMode(OnPacketArrivesStopBlocking onPacketArrives, void *userCookie, const double timeout)
DeviceMode
Device capturing mode.
Definition: PcapLiveDevice.h:153
@ Normal
Only packets that their destination is this NIC are captured.
Definition: PcapLiveDevice.h:155
@ Promiscuous
All packets that arrive to the NIC are captured, even packets that their destination isn't this NIC.
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:272
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:290
bool sendPacket(const uint8_t *packetData, int packetDataLength, int packetPayloadLength)
IPv4Address getDefaultGateway() const
std::string getDesc() const
Definition: PcapLiveDevice.h:260
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:247
virtual bool startCapture(OnPacketArrivesCallback onPacketArrives, void *onPacketArrivesUserCookie, int intervalInSecondsToUpdateStats, OnStatsUpdateCallback onStatsUpdate, void *onStatsUpdateUserCookie)
IPv6Address getIPv6Address() const
virtual bool startCapture(RawPacketVector &capturedPacketsVector)
~PcapLiveDevice() override
A destructor for this class.
std::string getName() const
Definition: PcapLiveDevice.h:253
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:278
bool getLoopback() const
Definition: PcapLiveDevice.h:266
virtual PcapLiveDevice * clone() const
bool open() override
std::vector< IPAddress > getIPAddresses() const
Definition: PcapLiveDevice.h:284
Definition: PcapLiveDeviceList.h:20
Definition: PointerVector.h:50
Definition: RawPacket.h:269
The main namespace for the PcapPlusPlus lib.
std::function< bool(RawPacket *, PcapLiveDevice *, void *)> OnPacketArrivesStopBlocking
Definition: PcapLiveDevice.h:38
std::function< void(RawPacket *, PcapLiveDevice *, void *)> OnPacketArrivesCallback
Definition: PcapLiveDevice.h:31
std::function< void(IPcapDevice::PcapStats &, void *)> OnStatsUpdateCallback
Definition: PcapLiveDevice.h:44
LinkLayerType
Definition: RawPacket.h:25
@ LINKTYPE_ETHERNET
Definition: RawPacket.h:29
Definition: PcapDevice.h:104
Definition: PcapLiveDevice.h:176
int packetBufferTimeoutMs
Definition: PcapLiveDevice.h:183
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:227
int snapshotLength
Definition: PcapLiveDevice.h:202
bool usePoll
In Unix-like system, use poll() for blocking mode.
Definition: PcapLiveDevice.h:209
int packetBufferSize
Definition: PcapLiveDevice.h:191
unsigned int nflogGroup
Definition: PcapLiveDevice.h:206
PcapDirection direction
Definition: PcapLiveDevice.h:195
DeviceMode mode
Indicates whether to open the device in promiscuous or normal mode.
Definition: PcapLiveDevice.h:178
A struct that contains all details of a network interface.
Definition: PcapLiveDevice.h:75
std::string description
Description of the device.
Definition: PcapLiveDevice.h:80
std::string name
Name of the device.
Definition: PcapLiveDevice.h:78
std::vector< IPAddress > addresses
IP addresses associated with the device.
Definition: PcapLiveDevice.h:82
bool isLoopback
Flag to indicate if the device is a loopback device.
Definition: PcapLiveDevice.h:84