PcapPlusPlus  19.12
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 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  uint16_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 
208  {
209  this->mode = mode;
210  this->packetBufferTimeoutMs = packetBufferTimeoutMs;
211  this->packetBufferSize = packetBufferSize;
212  this->direction = PCPP_INOUT;
213  }
214  };
215 
216 
220  virtual ~PcapLiveDevice();
221 
225  virtual LiveDeviceType getDeviceType() const { return LibPcapDevice; }
226 
230  const char* getName() const { return m_Name; }
231 
235  const char* getDesc() const { return m_Description; }
236 
240  bool getLoopback() const { return m_IsLoopback; }
241 
245  virtual uint16_t getMtu() const { return m_DeviceMtu; }
246 
250  virtual LinkLayerType getLinkType() const { return m_LinkType; }
254  const std::vector<pcap_addr_t>& getAddresses() const { return m_Addresses; }
255 
259  virtual MacAddress getMacAddress() const { return m_MacAddress; }
260 
265  IPv4Address getIPv4Address() const;
266 
272 
278  const std::vector<IPv4Address>& getDnsServers() const;
279 
294  virtual bool startCapture(OnPacketArrivesCallback onPacketArrives, void* onPacketArrivesUserCookie);
295 
318  virtual bool startCapture(OnPacketArrivesCallback onPacketArrives, void* onPacketArrivesUserCookie, int intervalInSecondsToUpdateStats, OnStatsUpdateCallback onStatsUpdate, void* onStatsUpdateUserCookie);
319 
337  virtual bool startCapture(int intervalInSecondsToUpdateStats, OnStatsUpdateCallback onStatsUpdate, void* onStatsUpdateUserCookie);
338 
352  virtual bool startCapture(RawPacketVector& capturedPacketsVector);
353 
373  virtual int startCaptureBlockingMode(OnPacketArrivesStopBlocking onPacketArrives, void* userCookie, int timeout);
374 
379  void stopCapture();
380 
385  bool captureActive();
386 
397  bool sendPacket(RawPacket const& rawPacket);
398 
409  bool sendPacket(const uint8_t* packetData, int packetDataLength);
410 
420  bool sendPacket(Packet* packet);
421 
433  virtual int sendPackets(RawPacket* rawPacketsArr, int arrLength);
434 
446  virtual int sendPackets(Packet** packetsArr, int arrLength);
447 
458  virtual int sendPackets(const RawPacketVector& rawPackets);
459 
460 
461  // implement abstract methods
462 
470  bool open();
471 
478  bool open(const DeviceConfiguration& config);
479 
480  void close();
481 
482  virtual void getStatistics(pcap_stat& stats) const;
483 
484  protected:
485  pcap_t* doOpen(const DeviceConfiguration& config);
486  };
487 
488 } // namespace pcpp
489 
490 #endif
pcpp::PcapLiveDevice::Promiscuous
Definition: PcapLiveDevice.h:147
pcpp::IPv4Address
Definition: IpAddress.h:119
pcpp::PcapLiveDevice::RemoteDevice
Definition: PcapLiveDevice.h:135
pcpp::Packet
Definition: Packet.h:26
pcpp::PcapLiveDevice
Definition: PcapLiveDevice.h:76
pcpp::PcapLiveDevice::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:235
pcpp::PcapLiveDevice::open
bool open()
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
Definition: PcapLiveDevice.h:159
pcpp::PcapLiveDevice::getStatistics
virtual void getStatistics(pcap_stat &stats) const
pcpp::PcapLiveDevice::getMacAddress
virtual MacAddress getMacAddress() const
Definition: PcapLiveDevice.h:259
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
Definition: PcapLiveDevice.h:161
pcpp::PcapLiveDevice::LiveDeviceType
LiveDeviceType
Definition: PcapLiveDevice.h:128
pcpp::PcapLiveDevice::getLoopback
bool getLoopback() const
Definition: PcapLiveDevice.h:240
pcpp::OnPacketArrivesCallback
void(* OnPacketArrivesCallback)(RawPacket *pPacket, PcapLiveDevice *pDevice, void *userCookie)
Definition: PcapLiveDevice.h:30
pcpp::PcapLiveDevice::WinPcapDevice
Definition: PcapLiveDevice.h:133
pcpp::PcapLiveDevice::startCaptureBlockingMode
virtual int startCaptureBlockingMode(OnPacketArrivesStopBlocking onPacketArrives, void *userCookie, int timeout)
pcpp::PcapLiveDevice::PCPP_INOUT
Definition: PcapLiveDevice.h:157
pcpp::PcapLiveDevice::Normal
Definition: PcapLiveDevice.h:145
pcpp::PcapLiveDevice::getLinkType
virtual LinkLayerType getLinkType() const
Definition: PcapLiveDevice.h:250
pcpp::IPcapDevice
Definition: PcapDevice.h:35
pcpp::PcapLiveDevice::DeviceConfiguration::DeviceConfiguration
DeviceConfiguration(DeviceMode mode=Promiscuous, int packetBufferTimeoutMs=0, int packetBufferSize=0, PcapDirection direction=PCPP_INOUT)
Definition: PcapLiveDevice.h:207
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:24
pcpp::PcapLiveDevice::getDeviceType
virtual LiveDeviceType getDeviceType() const
Definition: PcapLiveDevice.h:225
pcpp::PcapLiveDevice::stopCapture
void stopCapture()
pcpp::PcapLiveDevice::getAddresses
const std::vector< pcap_addr_t > & getAddresses() const
Definition: PcapLiveDevice.h:254
pcpp::PcapLiveDevice::DeviceMode
DeviceMode
Definition: PcapLiveDevice.h:142
pcpp::PcapLiveDevice::getMtu
virtual uint16_t getMtu() const
Definition: PcapLiveDevice.h:245
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:219
pcpp::PcapLiveDevice::close
void close()
pcpp::PcapLiveDevice::getName
const char * getName() const
Definition: PcapLiveDevice.h:230