PcapPlusPlus  23.09
PcapLiveDevice.h
Go to the documentation of this file.
1 //TODO: replace all these defines with #pragma once
2 #ifndef PCAPPP_LIVE_DEVICE
3 #define PCAPPP_LIVE_DEVICE
4 
5 #include <atomic>
6 #include <vector>
7 #include <string.h>
8 #include <thread>
9 
10 #include "IpAddress.h"
11 #include "Packet.h"
12 #include "PcapDevice.h"
13 
14 // forward declarations for structs and typedefs that are defined in pcap.h
15 struct pcap_if;
16 typedef pcap_if pcap_if_t;
17 struct pcap_addr;
18 typedef struct pcap_addr pcap_addr_t;
19 
21 
26 namespace pcpp
27 {
28 
29  class PcapLiveDevice;
30 
38  typedef void (*OnPacketArrivesCallback)(RawPacket* pPacket, PcapLiveDevice* pDevice, void* userCookie);
39 
48  typedef bool (*OnPacketArrivesStopBlocking)(RawPacket* pPacket, PcapLiveDevice* pDevice, void* userCookie);
49 
50 
57  typedef void (*OnStatsUpdateCallback)(IPcapDevice::PcapStats& stats, void* userCookie);
58 
59  // for internal use only
60  typedef void* (*ThreadStart)(void*);
61 
82  class PcapLiveDevice : public IPcapDevice
83  {
84  friend class PcapLiveDeviceList;
85  protected:
86  // This is a second descriptor for the same device. It is needed because of a bug
87  // that occurs in libpcap on Linux (on Windows using WinPcap/Npcap it works well):
88  // It's impossible to capture packets sent by the same descriptor
89  pcap_t* m_PcapSendDescriptor;
90  std::string m_Name;
91  std::string m_Description;
92  bool m_IsLoopback;
93  uint32_t m_DeviceMtu;
94  std::vector<pcap_addr_t> m_Addresses;
95  MacAddress m_MacAddress;
96  IPv4Address m_DefaultGateway;
97  std::thread m_CaptureThread;
98  bool m_CaptureThreadStarted;
99  std::thread m_StatsThread;
100  bool m_StatsThreadStarted;
101  std::atomic<bool> m_StopThread;
102  OnPacketArrivesCallback m_cbOnPacketArrives;
103  void* m_cbOnPacketArrivesUserCookie;
104  OnStatsUpdateCallback m_cbOnStatsUpdate;
105  void* m_cbOnStatsUpdateUserCookie;
106  OnPacketArrivesStopBlocking m_cbOnPacketArrivesBlockingMode;
107  void* m_cbOnPacketArrivesBlockingModeUserCookie;
108  int m_IntervalToUpdateStats;
109  RawPacketVector* m_CapturedPackets;
110  bool m_CaptureCallbackMode;
111  LinkLayerType m_LinkType;
112 
113  // c'tor is not public, there should be only one for every interface (created by PcapLiveDeviceList)
114  PcapLiveDevice(pcap_if_t* pInterface, bool calculateMTU, bool calculateMacAddress, bool calculateDefaultGateway);
115  // copy c'tor is not public
116  PcapLiveDevice( const PcapLiveDevice& other );
117  PcapLiveDevice& operator=(const PcapLiveDevice& other);
118 
119  void setDeviceMtu();
120  void setDeviceMacAddress();
121  void setDefaultGateway();
122 
123  // threads
124  void captureThreadMain();
125  void statsThreadMain();
126 
127  static void onPacketArrives(uint8_t* user, const struct pcap_pkthdr* pkthdr, const uint8_t* packet);
128  static void onPacketArrivesNoCallback(uint8_t* user, const struct pcap_pkthdr* pkthdr, const uint8_t* packet);
129  static void onPacketArrivesBlockingMode(uint8_t* user, const struct pcap_pkthdr* pkthdr, const uint8_t* packet);
130  public:
131 
136  {
143  };
144 
145 
150  {
152  Normal = 0,
155  };
156 
157 
162  {
169  };
170 
171 
178  {
181 
187 
197 
203 
213 
218  unsigned int nflogGroup;
219 
235  explicit DeviceConfiguration(DeviceMode mode = Promiscuous, int packetBufferTimeoutMs = 0, int packetBufferSize = 0,
236  PcapDirection direction = PCPP_INOUT, int snapshotLength = 0, unsigned int nflogGroup = 0)
237  {
238  this->mode = mode;
239  this->packetBufferTimeoutMs = packetBufferTimeoutMs;
240  this->packetBufferSize = packetBufferSize;
241  this->direction = direction;
242  this->snapshotLength = snapshotLength;
243  this->nflogGroup = nflogGroup;
244  }
245  };
246 
247 
251  virtual ~PcapLiveDevice();
252 
256  virtual LiveDeviceType getDeviceType() const { return LibPcapDevice; }
257 
261  std::string getName() const { return m_Name; }
262 
266  std::string getDesc() const { return m_Description; }
267 
271  bool getLoopback() const { return m_IsLoopback; }
272 
276  virtual uint32_t getMtu() const { return m_DeviceMtu; }
277 
281  virtual LinkLayerType getLinkType() const { return m_LinkType; }
282 
286  const std::vector<pcap_addr_t>& getAddresses() const { return m_Addresses; }
287 
291  virtual MacAddress getMacAddress() const { return m_MacAddress; }
292 
297  IPv4Address getIPv4Address() const;
298 
303  IPv6Address getIPv6Address() const;
304 
310 
316  const std::vector<IPv4Address>& getDnsServers() const;
317 
332  virtual bool startCapture(OnPacketArrivesCallback onPacketArrives, void* onPacketArrivesUserCookie);
333 
356  virtual bool startCapture(OnPacketArrivesCallback onPacketArrives, void* onPacketArrivesUserCookie, int intervalInSecondsToUpdateStats, OnStatsUpdateCallback onStatsUpdate, void* onStatsUpdateUserCookie);
357 
375  virtual bool startCapture(int intervalInSecondsToUpdateStats, OnStatsUpdateCallback onStatsUpdate, void* onStatsUpdateUserCookie);
376 
390  virtual bool startCapture(RawPacketVector& capturedPacketsVector);
391 
411  virtual int startCaptureBlockingMode(OnPacketArrivesStopBlocking onPacketArrives, void* userCookie, int timeout);
412 
417  void stopCapture();
418 
423  bool captureActive();
424 
430  bool doMtuCheck(int packetPayloadLength);
431 
444  bool sendPacket(RawPacket const& rawPacket, bool checkMtu = false);
445 
461  bool sendPacket(const uint8_t* packetData, int packetDataLength, int packetPayloadLength);
462 
477  bool sendPacket(const uint8_t* packetData, int packetDataLength, bool checkMtu = false, pcpp::LinkLayerType linkType = pcpp::LINKTYPE_ETHERNET);
478 
490  bool sendPacket(Packet* packet, bool checkMtu = true);
491 
505  virtual int sendPackets(RawPacket* rawPacketsArr, int arrLength, bool checkMtu = false);
506 
520  virtual int sendPackets(Packet** packetsArr, int arrLength, bool checkMtu = true);
521 
534  virtual int sendPackets(const RawPacketVector& rawPackets, bool checkMtu = false);
535 
536 
537  // implement abstract methods
538 
546  bool open();
547 
554  bool open(const DeviceConfiguration& config);
555 
556  void close();
557 
563 
564  virtual void getStatistics(IPcapDevice::PcapStats& stats) const;
565 
566  protected:
567  pcap_t* doOpen(const DeviceConfiguration& config);
568  };
569 
570 } // namespace pcpp
571 
572 #endif
The main namespace for the PcapPlusPlus lib.
Definition: PcapLiveDevice.h:164
Definition: PcapLiveDevice.h:140
Definition: PcapLiveDevice.h:168
const std::vector< pcap_addr_t > & getAddresses() const
Definition: PcapLiveDevice.h:286
bool(* OnPacketArrivesStopBlocking)(RawPacket *pPacket, PcapLiveDevice *pDevice, void *userCookie)
Definition: PcapLiveDevice.h:48
Definition: PcapLiveDevice.h:177
bool sendPacket(RawPacket const &rawPacket, bool checkMtu=false)
DeviceMode
Definition: PcapLiveDevice.h:149
IPv6Address getIPv6Address() const
Definition: RawPacket.h:30
virtual MacAddress getMacAddress() const
Definition: PcapLiveDevice.h:291
Definition: PcapLiveDevice.h:152
bool doMtuCheck(int packetPayloadLength)
Definition: PcapDevice.h:27
Definition: Packet.h:26
Definition: PointerVector.h:24
LiveDeviceType
Definition: PcapLiveDevice.h:135
Definition: RawPacket.h:254
int packetBufferSize
Definition: PcapLiveDevice.h:196
PcapDirection
Definition: PcapLiveDevice.h:161
void(* OnStatsUpdateCallback)(IPcapDevice::PcapStats &stats, void *userCookie)
Definition: PcapLiveDevice.h:57
const std::vector< IPv4Address > & getDnsServers() const
DeviceConfiguration(DeviceMode mode=Promiscuous, int packetBufferTimeoutMs=0, int packetBufferSize=0, PcapDirection direction=PCPP_INOUT, int snapshotLength=0, unsigned int nflogGroup=0)
Definition: PcapLiveDevice.h:235
Definition: PcapLiveDevice.h:154
virtual uint32_t getMtu() const
Definition: PcapLiveDevice.h:276
int packetBufferTimeoutMs
Definition: PcapLiveDevice.h:186
Definition: PcapLiveDevice.h:142
PcapDirection direction
Definition: PcapLiveDevice.h:202
Definition: IpAddress.h:42
Definition: IpAddress.h:192
IPv4Address getIPv4Address() const
void(* OnPacketArrivesCallback)(RawPacket *pPacket, PcapLiveDevice *pDevice, void *userCookie)
Definition: PcapLiveDevice.h:38
Definition: PcapLiveDevice.h:138
Definition: PcapLiveDevice.h:82
std::string getDesc() const
Definition: PcapLiveDevice.h:266
unsigned int nflogGroup
Definition: PcapLiveDevice.h:218
LinkLayerType
Definition: RawPacket.h:25
virtual ~PcapLiveDevice()
virtual void getStatistics(IPcapDevice::PcapStats &stats) const
IPv4Address getDefaultGateway() const
DeviceMode mode
Definition: PcapLiveDevice.h:180
virtual bool startCapture(OnPacketArrivesCallback onPacketArrives, void *onPacketArrivesUserCookie)
int snapshotLength
Definition: PcapLiveDevice.h:212
virtual LiveDeviceType getDeviceType() const
Definition: PcapLiveDevice.h:256
bool getLoopback() const
Definition: PcapLiveDevice.h:271
Definition: PcapLiveDevice.h:166
virtual int startCaptureBlockingMode(OnPacketArrivesStopBlocking onPacketArrives, void *userCookie, int timeout)
virtual LinkLayerType getLinkType() const
Definition: PcapLiveDevice.h:281
Definition: MacAddress.h:28
Definition: PcapDevice.h:41
PcapLiveDevice * clone()
virtual int sendPackets(RawPacket *rawPacketsArr, int arrLength, bool checkMtu=false)
Definition: PcapLiveDeviceList.h:24
std::string getName() const
Definition: PcapLiveDevice.h:261