PcapPlusPlus  20.08
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 "IpAddress.h"
9 #include "Packet.h"
10 
11 
13 
18 namespace pcpp
19 {
20 
21  class PcapLiveDevice;
22 
30  typedef void (*OnPacketArrivesCallback)(RawPacket* pPacket, PcapLiveDevice* pDevice, void* userCookie);
31 
40  typedef bool (*OnPacketArrivesStopBlocking)(RawPacket* pPacket, PcapLiveDevice* pDevice, void* userData);
41 
42 
49  typedef void (*OnStatsUpdateCallback)(pcap_stat& stats, void* userCookie);
50 
51  // for internal use only
52  typedef void* (*ThreadStart)(void*);
53 
54  struct PcapThread;
55 
76  class PcapLiveDevice : public IPcapDevice
77  {
78  friend class PcapLiveDeviceList;
79  protected:
80  // This is a second descriptor for the same device. It is needed because of a bug
81  // that occurs in libpcap on Linux (on Windows using WinPcap/Npcap it works well):
82  // It's impossible to capture packets sent by the same descriptor
83  pcap_t* m_PcapSendDescriptor;
84  const char* m_Name;
85  const char* m_Description;
86  bool m_IsLoopback;
87  uint32_t m_DeviceMtu;
88  std::vector<pcap_addr_t> m_Addresses;
89  MacAddress m_MacAddress;
90  IPv4Address m_DefaultGateway;
91  PcapThread* m_CaptureThread;
92  bool m_CaptureThreadStarted;
93  PcapThread* m_StatsThread;
94  bool m_StatsThreadStarted;
95  bool m_StopThread;
96  OnPacketArrivesCallback m_cbOnPacketArrives;
97  void* m_cbOnPacketArrivesUserCookie;
98  OnStatsUpdateCallback m_cbOnStatsUpdate;
99  void* m_cbOnStatsUpdateUserCookie;
100  OnPacketArrivesStopBlocking m_cbOnPacketArrivesBlockingMode;
101  void* m_cbOnPacketArrivesBlockingModeUserCookie;
102  int m_IntervalToUpdateStats;
103  RawPacketVector* m_CapturedPackets;
104  bool m_CaptureCallbackMode;
105  LinkLayerType m_LinkType;
106 
107  // c'tor is not public, there should be only one for every interface (created by PcapLiveDeviceList)
108  PcapLiveDevice(pcap_if_t* pInterface, bool calculateMTU, bool calculateMacAddress, bool calculateDefaultGateway);
109  // copy c'tor is not public
110  PcapLiveDevice( const PcapLiveDevice& other );
111  PcapLiveDevice& operator=(const PcapLiveDevice& other);
112 
113  void setDeviceMtu();
114  void setDeviceMacAddress();
115  void setDefaultGateway();
116  static void* captureThreadMain(void* ptr);
117  static void* statsThreadMain(void* ptr);
118  static void onPacketArrives(uint8_t* user, const struct pcap_pkthdr* pkthdr, const uint8_t* packet);
119  static void onPacketArrivesNoCallback(uint8_t* user, const struct pcap_pkthdr* pkthdr, const uint8_t* packet);
120  static void onPacketArrivesBlockingMode(uint8_t* user, const struct pcap_pkthdr* pkthdr, const uint8_t* packet);
121  std::string printThreadId(PcapThread* id);
122  virtual ThreadStart getCaptureThreadStart();
123  public:
124 
129  {
136  };
137 
138 
143  {
145  Normal = 0,
148  };
149 
150 
155  {
162  };
163 
164 
171  {
174 
180 
190 
196 
206 
223  {
224  this->mode = mode;
225  this->packetBufferTimeoutMs = packetBufferTimeoutMs;
226  this->packetBufferSize = packetBufferSize;
227  this->direction = direction;
228  this->snapshotLength = snapshotLength;
229  }
230  };
231 
232 
236  virtual ~PcapLiveDevice();
237 
241  virtual LiveDeviceType getDeviceType() const { return LibPcapDevice; }
242 
246  const char* getName() const { return m_Name; }
247 
251  const char* getDesc() const { return m_Description; }
252 
256  bool getLoopback() const { return m_IsLoopback; }
257 
261  virtual uint32_t getMtu() const { return m_DeviceMtu; }
262 
266  virtual LinkLayerType getLinkType() const { return m_LinkType; }
270  const std::vector<pcap_addr_t>& getAddresses() const { return m_Addresses; }
271 
275  virtual MacAddress getMacAddress() const { return m_MacAddress; }
276 
281  IPv4Address getIPv4Address() const;
282 
288 
294  const std::vector<IPv4Address>& getDnsServers() const;
295 
310  virtual bool startCapture(OnPacketArrivesCallback onPacketArrives, void* onPacketArrivesUserCookie);
311 
334  virtual bool startCapture(OnPacketArrivesCallback onPacketArrives, void* onPacketArrivesUserCookie, int intervalInSecondsToUpdateStats, OnStatsUpdateCallback onStatsUpdate, void* onStatsUpdateUserCookie);
335 
353  virtual bool startCapture(int intervalInSecondsToUpdateStats, OnStatsUpdateCallback onStatsUpdate, void* onStatsUpdateUserCookie);
354 
368  virtual bool startCapture(RawPacketVector& capturedPacketsVector);
369 
389  virtual int startCaptureBlockingMode(OnPacketArrivesStopBlocking onPacketArrives, void* userCookie, int timeout);
390 
395  void stopCapture();
396 
401  bool captureActive();
402 
413  bool sendPacket(RawPacket const& rawPacket);
414 
425  bool sendPacket(const uint8_t* packetData, int packetDataLength);
426 
436  bool sendPacket(Packet* packet);
437 
449  virtual int sendPackets(RawPacket* rawPacketsArr, int arrLength);
450 
462  virtual int sendPackets(Packet** packetsArr, int arrLength);
463 
474  virtual int sendPackets(const RawPacketVector& rawPackets);
475 
476 
477  // implement abstract methods
478 
486  bool open();
487 
494  bool open(const DeviceConfiguration& config);
495 
496  void close();
497 
498  virtual void getStatistics(pcap_stat& stats) const;
499 
500  protected:
501  pcap_t* doOpen(const DeviceConfiguration& config);
502  };
503 
504 } // namespace pcpp
505 
506 #endif
pcpp::PcapLiveDevice::Promiscuous
@ Promiscuous
Definition: PcapLiveDevice.h:147
pcpp::IPv4Address
Definition: IpAddress.h:26
pcpp::PcapLiveDevice::RemoteDevice
@ RemoteDevice
Definition: PcapLiveDevice.h:135
pcpp::Packet
Definition: Packet.h:26
pcpp::PcapLiveDevice
Definition: PcapLiveDevice.h:76
pcpp::PcapLiveDevice::LibPcapDevice
@ LibPcapDevice
Definition: PcapLiveDevice.h:131
pcpp::PcapLiveDevice::startCapture
virtual bool startCapture(OnPacketArrivesCallback onPacketArrives, void *onPacketArrivesUserCookie)
pcpp::PcapLiveDevice::DeviceConfiguration
Definition: PcapLiveDevice.h:170
pcpp::PcapLiveDevice::getIPv4Address
IPv4Address getIPv4Address() const
pcpp::PcapLiveDevice::DeviceConfiguration::packetBufferTimeoutMs
int packetBufferTimeoutMs
Definition: PcapLiveDevice.h:179
PcapDevice.h
pcpp::PcapLiveDevice::DeviceConfiguration::direction
PcapDirection direction
Definition: PcapLiveDevice.h:195
pcpp::PcapLiveDevice::getDesc
const char * getDesc() const
Definition: PcapLiveDevice.h:251
pcpp::PcapLiveDevice::open
bool open()
pcpp::PcapLiveDevice::getMtu
virtual uint32_t getMtu() const
Definition: PcapLiveDevice.h:261
pcpp::MacAddress
Definition: MacAddress.h:27
pcpp::PcapLiveDevice::DeviceConfiguration::mode
DeviceMode mode
Definition: PcapLiveDevice.h:173
pcpp::PcapLiveDevice::captureActive
bool captureActive()
pcpp::PcapLiveDevice::sendPackets
virtual int sendPackets(RawPacket *rawPacketsArr, int arrLength)
pcpp::PcapLiveDevice::PcapDirection
PcapDirection
Definition: PcapLiveDevice.h:154
pcpp::PcapLiveDevice::PCPP_IN
@ PCPP_IN
Definition: PcapLiveDevice.h:159
pcpp::PcapLiveDevice::getStatistics
virtual void getStatistics(pcap_stat &stats) const
pcpp::PcapLiveDevice::getMacAddress
virtual MacAddress getMacAddress() const
Definition: PcapLiveDevice.h:275
pcpp::PcapLiveDevice::DeviceConfiguration::snapshotLength
int snapshotLength
Definition: PcapLiveDevice.h:205
pcpp::PcapLiveDevice::DeviceConfiguration::DeviceConfiguration
DeviceConfiguration(DeviceMode mode=Promiscuous, int packetBufferTimeoutMs=0, int packetBufferSize=0, PcapDirection direction=PCPP_INOUT, int snapshotLength=0)
Definition: PcapLiveDevice.h:221
pcpp::PcapLiveDevice::getDnsServers
const std::vector< IPv4Address > & getDnsServers() const
pcpp::PcapLiveDevice::sendPacket
bool sendPacket(RawPacket const &rawPacket)
pcpp::PcapLiveDevice::DeviceConfiguration::packetBufferSize
int packetBufferSize
Definition: PcapLiveDevice.h:189
pcpp::PcapLiveDevice::PCPP_OUT
@ PCPP_OUT
Definition: PcapLiveDevice.h:161
pcpp::PcapLiveDevice::LiveDeviceType
LiveDeviceType
Definition: PcapLiveDevice.h:128
pcpp::PcapLiveDevice::getLoopback
bool getLoopback() const
Definition: PcapLiveDevice.h:256
pcpp::OnPacketArrivesCallback
void(* OnPacketArrivesCallback)(RawPacket *pPacket, PcapLiveDevice *pDevice, void *userCookie)
Definition: PcapLiveDevice.h:30
pcpp::PcapLiveDevice::WinPcapDevice
@ WinPcapDevice
Definition: PcapLiveDevice.h:133
pcpp::PcapLiveDevice::startCaptureBlockingMode
virtual int startCaptureBlockingMode(OnPacketArrivesStopBlocking onPacketArrives, void *userCookie, int timeout)
pcpp::PcapLiveDevice::PCPP_INOUT
@ PCPP_INOUT
Definition: PcapLiveDevice.h:157
pcpp::PcapLiveDevice::Normal
@ Normal
Definition: PcapLiveDevice.h:145
pcpp::PcapLiveDevice::getLinkType
virtual LinkLayerType getLinkType() const
Definition: PcapLiveDevice.h:266
pcpp::IPcapDevice
Definition: PcapDevice.h:35
IpAddress.h
pcpp::PcapLiveDeviceList
Definition: PcapLiveDeviceList.h:25
pcpp::PcapLiveDevice::~PcapLiveDevice
virtual ~PcapLiveDevice()
pcpp
The main namespace for the PcapPlusPlus lib.
pcpp::OnPacketArrivesStopBlocking
bool(* OnPacketArrivesStopBlocking)(RawPacket *pPacket, PcapLiveDevice *pDevice, void *userData)
Definition: PcapLiveDevice.h:40
pcpp::LinkLayerType
LinkLayerType
Definition: RawPacket.h:25
pcpp::PcapLiveDevice::getDeviceType
virtual LiveDeviceType getDeviceType() const
Definition: PcapLiveDevice.h:241
pcpp::PcapLiveDevice::stopCapture
void stopCapture()
pcpp::PcapLiveDevice::getAddresses
const std::vector< pcap_addr_t > & getAddresses() const
Definition: PcapLiveDevice.h:270
pcpp::PcapLiveDevice::DeviceMode
DeviceMode
Definition: PcapLiveDevice.h:142
Packet.h
pcpp::OnStatsUpdateCallback
void(* OnStatsUpdateCallback)(pcap_stat &stats, void *userCookie)
Definition: PcapLiveDevice.h:49
pcpp::PcapLiveDevice::getDefaultGateway
IPv4Address getDefaultGateway() const
pcpp::PointerVector
Definition: PointerVector.h:24
pcpp::RawPacket
Definition: RawPacket.h:252
pcpp::PcapLiveDevice::close
void close()
pcpp::PcapLiveDevice::getName
const char * getName() const
Definition: PcapLiveDevice.h:246