PcapPlusPlus  22.11
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 "PcapDevice.h"
6 #include <vector>
7 #include <string.h>
8 #include <thread>
9 #include "IpAddress.h"
10 #include "Packet.h"
11 
12 // forward declarations for structs and typedefs that are defined in pcap.h
13 struct pcap_if;
14 typedef pcap_if pcap_if_t;
15 struct pcap_addr;
16 typedef struct pcap_addr pcap_addr_t;
17 
19 
24 namespace pcpp
25 {
26 
27  class PcapLiveDevice;
28 
36  typedef void (*OnPacketArrivesCallback)(RawPacket* pPacket, PcapLiveDevice* pDevice, void* userCookie);
37 
46  typedef bool (*OnPacketArrivesStopBlocking)(RawPacket* pPacket, PcapLiveDevice* pDevice, void* userData);
47 
48 
55  typedef void (*OnStatsUpdateCallback)(IPcapDevice::PcapStats& stats, void* userCookie);
56 
57  // for internal use only
58  typedef void* (*ThreadStart)(void*);
59 
80  class PcapLiveDevice : public IPcapDevice
81  {
82  friend class PcapLiveDeviceList;
83  protected:
84  // This is a second descriptor for the same device. It is needed because of a bug
85  // that occurs in libpcap on Linux (on Windows using WinPcap/Npcap it works well):
86  // It's impossible to capture packets sent by the same descriptor
87  pcap_t* m_PcapSendDescriptor;
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  bool m_CaptureThreadStarted;
97  std::thread m_StatsThread;
98  bool m_StatsThreadStarted;
99  bool m_StopThread;
100  OnPacketArrivesCallback m_cbOnPacketArrives;
101  void* m_cbOnPacketArrivesUserCookie;
102  OnStatsUpdateCallback m_cbOnStatsUpdate;
103  void* m_cbOnStatsUpdateUserCookie;
104  OnPacketArrivesStopBlocking m_cbOnPacketArrivesBlockingMode;
105  void* m_cbOnPacketArrivesBlockingModeUserCookie;
106  int m_IntervalToUpdateStats;
107  RawPacketVector* m_CapturedPackets;
108  bool m_CaptureCallbackMode;
109  LinkLayerType m_LinkType;
110 
111  // c'tor is not public, there should be only one for every interface (created by PcapLiveDeviceList)
112  PcapLiveDevice(pcap_if_t* pInterface, bool calculateMTU, bool calculateMacAddress, bool calculateDefaultGateway);
113  // copy c'tor is not public
114  PcapLiveDevice( const PcapLiveDevice& other );
115  PcapLiveDevice& operator=(const PcapLiveDevice& other);
116 
117  void setDeviceMtu();
118  void setDeviceMacAddress();
119  void setDefaultGateway();
120 
121  // threads
122  void captureThreadMain();
123  void statsThreadMain();
124 
125  static void onPacketArrives(uint8_t* user, const struct pcap_pkthdr* pkthdr, const uint8_t* packet);
126  static void onPacketArrivesNoCallback(uint8_t* user, const struct pcap_pkthdr* pkthdr, const uint8_t* packet);
127  static void onPacketArrivesBlockingMode(uint8_t* user, const struct pcap_pkthdr* pkthdr, const uint8_t* packet);
128  public:
129 
134  {
141  };
142 
143 
148  {
150  Normal = 0,
153  };
154 
155 
160  {
167  };
168 
169 
176  {
179 
185 
195 
201 
211 
226  DeviceConfiguration(DeviceMode mode = Promiscuous, int packetBufferTimeoutMs = 0, int packetBufferSize = 0,
227  PcapDirection direction = PCPP_INOUT, int snapshotLength = 0)
228  {
229  this->mode = mode;
230  this->packetBufferTimeoutMs = packetBufferTimeoutMs;
231  this->packetBufferSize = packetBufferSize;
232  this->direction = direction;
233  this->snapshotLength = snapshotLength;
234  }
235  };
236 
237 
241  virtual ~PcapLiveDevice();
242 
246  virtual LiveDeviceType getDeviceType() const { return LibPcapDevice; }
247 
251  std::string getName() const { return m_Name; }
252 
256  std::string getDesc() const { return m_Description; }
257 
261  bool getLoopback() const { return m_IsLoopback; }
262 
266  virtual uint32_t getMtu() const { return m_DeviceMtu; }
267 
271  virtual LinkLayerType getLinkType() const { return m_LinkType; }
272 
276  const std::vector<pcap_addr_t>& getAddresses() const { return m_Addresses; }
277 
281  virtual MacAddress getMacAddress() const { return m_MacAddress; }
282 
287  IPv4Address getIPv4Address() const;
288 
293  IPv6Address getIPv6Address() const;
294 
300 
306  const std::vector<IPv4Address>& getDnsServers() const;
307 
322  virtual bool startCapture(OnPacketArrivesCallback onPacketArrives, void* onPacketArrivesUserCookie);
323 
346  virtual bool startCapture(OnPacketArrivesCallback onPacketArrives, void* onPacketArrivesUserCookie, int intervalInSecondsToUpdateStats, OnStatsUpdateCallback onStatsUpdate, void* onStatsUpdateUserCookie);
347 
365  virtual bool startCapture(int intervalInSecondsToUpdateStats, OnStatsUpdateCallback onStatsUpdate, void* onStatsUpdateUserCookie);
366 
380  virtual bool startCapture(RawPacketVector& capturedPacketsVector);
381 
401  virtual int startCaptureBlockingMode(OnPacketArrivesStopBlocking onPacketArrives, void* userCookie, int timeout);
402 
407  void stopCapture();
408 
413  bool captureActive();
414 
420  bool doMtuCheck(int packetPayloadLength);
421 
434  bool sendPacket(RawPacket const& rawPacket, bool checkMtu = false);
435 
451  bool sendPacket(const uint8_t* packetData, int packetDataLength, int packetPayloadLength);
452 
467  bool sendPacket(const uint8_t* packetData, int packetDataLength, bool checkMtu = false, pcpp::LinkLayerType linkType = pcpp::LINKTYPE_ETHERNET);
468 
480  bool sendPacket(Packet* packet, bool checkMtu = true);
481 
495  virtual int sendPackets(RawPacket* rawPacketsArr, int arrLength, bool checkMtu = false);
496 
510  virtual int sendPackets(Packet** packetsArr, int arrLength, bool checkMtu = true);
511 
524  virtual int sendPackets(const RawPacketVector& rawPackets, bool checkMtu = false);
525 
526 
527  // implement abstract methods
528 
536  bool open();
537 
544  bool open(const DeviceConfiguration& config);
545 
546  void close();
547 
553 
554  virtual void getStatistics(IPcapDevice::PcapStats& stats) const;
555 
556  protected:
557  pcap_t* doOpen(const DeviceConfiguration& config);
558  };
559 
560 } // namespace pcpp
561 
562 #endif
The main namespace for the PcapPlusPlus lib.
Definition: PcapLiveDevice.h:162
Definition: PcapLiveDevice.h:138
Definition: PcapLiveDevice.h:166
const std::vector< pcap_addr_t > & getAddresses() const
Definition: PcapLiveDevice.h:276
Definition: PcapLiveDevice.h:175
bool sendPacket(RawPacket const &rawPacket, bool checkMtu=false)
DeviceMode
Definition: PcapLiveDevice.h:147
IPv6Address getIPv6Address() const
Definition: RawPacket.h:30
virtual MacAddress getMacAddress() const
Definition: PcapLiveDevice.h:281
Definition: PcapLiveDevice.h:150
bool doMtuCheck(int packetPayloadLength)
Definition: PcapDevice.h:27
Definition: Packet.h:26
Definition: PointerVector.h:24
LiveDeviceType
Definition: PcapLiveDevice.h:133
Definition: RawPacket.h:252
int packetBufferSize
Definition: PcapLiveDevice.h:194
PcapDirection
Definition: PcapLiveDevice.h:159
void(* OnStatsUpdateCallback)(IPcapDevice::PcapStats &stats, void *userCookie)
Definition: PcapLiveDevice.h:55
const std::vector< IPv4Address > & getDnsServers() const
Definition: PcapLiveDevice.h:152
virtual uint32_t getMtu() const
Definition: PcapLiveDevice.h:266
int packetBufferTimeoutMs
Definition: PcapLiveDevice.h:184
Definition: PcapLiveDevice.h:140
PcapDirection direction
Definition: PcapLiveDevice.h:200
Definition: IpAddress.h:27
Definition: IpAddress.h:167
IPv4Address getIPv4Address() const
void(* OnPacketArrivesCallback)(RawPacket *pPacket, PcapLiveDevice *pDevice, void *userCookie)
Definition: PcapLiveDevice.h:36
Definition: PcapLiveDevice.h:136
Definition: PcapLiveDevice.h:80
std::string getDesc() const
Definition: PcapLiveDevice.h:256
LinkLayerType
Definition: RawPacket.h:25
bool(* OnPacketArrivesStopBlocking)(RawPacket *pPacket, PcapLiveDevice *pDevice, void *userData)
Definition: PcapLiveDevice.h:46
DeviceConfiguration(DeviceMode mode=Promiscuous, int packetBufferTimeoutMs=0, int packetBufferSize=0, PcapDirection direction=PCPP_INOUT, int snapshotLength=0)
Definition: PcapLiveDevice.h:226
virtual ~PcapLiveDevice()
virtual void getStatistics(IPcapDevice::PcapStats &stats) const
IPv4Address getDefaultGateway() const
DeviceMode mode
Definition: PcapLiveDevice.h:178
virtual bool startCapture(OnPacketArrivesCallback onPacketArrives, void *onPacketArrivesUserCookie)
int snapshotLength
Definition: PcapLiveDevice.h:210
virtual LiveDeviceType getDeviceType() const
Definition: PcapLiveDevice.h:246
bool getLoopback() const
Definition: PcapLiveDevice.h:261
Definition: PcapLiveDevice.h:164
virtual int startCaptureBlockingMode(OnPacketArrivesStopBlocking onPacketArrives, void *userCookie, int timeout)
virtual LinkLayerType getLinkType() const
Definition: PcapLiveDevice.h:271
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:251