PcapPlusPlus
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 
157  {
160 
166 
176 
185  DeviceConfiguration(DeviceMode mode = Promiscuous, int packetBufferTimeoutMs = 0, int packetBufferSize = 0)
186  {
187  this->mode = mode;
188  this->packetBufferTimeoutMs = packetBufferTimeoutMs;
189  this->packetBufferSize = packetBufferSize;
190  }
191  };
192 
193 
197  virtual ~PcapLiveDevice();
198 
203 
207  inline const char* getName() { return m_Name; }
208 
212  inline const char* getDesc() { return m_Description; }
213 
217  inline bool getLoopback() { return m_IsLoopback; }
218 
222  virtual inline uint16_t getMtu() { return m_DeviceMtu; }
223 
227  virtual inline LinkLayerType getLinkType() { return m_LinkType; }
231  inline std::vector<pcap_addr_t>& getAddresses() { return m_Addresses; }
232 
236  virtual inline MacAddress getMacAddress() { return m_MacAddress; }
237 
243 
249 
255  std::vector<IPv4Address>& getDnsServers();
256 
271  virtual bool startCapture(OnPacketArrivesCallback onPacketArrives, void* onPacketArrivesUserCookie);
272 
295  virtual bool startCapture(OnPacketArrivesCallback onPacketArrives, void* onPacketArrivesUserCookie, int intervalInSecondsToUpdateStats, OnStatsUpdateCallback onStatsUpdate, void* onStatsUpdateUserCookie);
296 
314  virtual bool startCapture(int intervalInSecondsToUpdateStats, OnStatsUpdateCallback onStatsUpdate, void* onStatsUpdateUserCookie);
315 
329  virtual bool startCapture(RawPacketVector& capturedPacketsVector);
330 
350  virtual int startCaptureBlockingMode(OnPacketArrivesStopBlocking onPacketArrives, void* userCookie, int timeout);
351 
356  void stopCapture();
357 
368  bool sendPacket(RawPacket const& rawPacket);
369 
380  bool sendPacket(const uint8_t* packetData, int packetDataLength);
381 
391  bool sendPacket(Packet* packet);
392 
404  virtual int sendPackets(RawPacket* rawPacketsArr, int arrLength);
405 
417  virtual int sendPackets(Packet** packetsArr, int arrLength);
418 
429  virtual int sendPackets(const RawPacketVector& rawPackets);
430 
431 
432  // implement abstract methods
433 
441  bool open();
442 
449  bool open(const DeviceConfiguration& config);
450 
451  void close();
452 
453  virtual void getStatistics(pcap_stat& stats);
454 
455  protected:
456  pcap_t* doOpen(const DeviceConfiguration& config);
457  };
458 
459 } // namespace pcpp
460 
461 #endif
DeviceMode
Definition: PcapLiveDevice.h:142
Definition: PcapLiveDevice.h:76
virtual uint16_t getMtu()
Definition: PcapLiveDevice.h:222
virtual LiveDeviceType getDeviceType()
Definition: PcapLiveDevice.h:202
int packetBufferSize
Definition: PcapLiveDevice.h:175
virtual LinkLayerType getLinkType()
Definition: PcapLiveDevice.h:227
Definition: PcapLiveDeviceList.h:25
Definition: PointerVector.h:24
Definition: IpAddress.h:113
Definition: PcapLiveDevice.h:145
std::vector< IPv4Address > & getDnsServers()
Definition: MacAddress.h:21
const char * getDesc()
Definition: PcapLiveDevice.h:212
bool sendPacket(RawPacket const &rawPacket)
bool getLoopback()
Definition: PcapLiveDevice.h:217
IPv4Address getIPv4Address()
std::vector< pcap_addr_t > & getAddresses()
Definition: PcapLiveDevice.h:231
Definition: PcapLiveDevice.h:135
Definition: RawPacket.h:219
Definition: Packet.h:26
Definition: PcapLiveDevice.h:156
Definition: PcapLiveDevice.h:131
virtual MacAddress getMacAddress()
Definition: PcapLiveDevice.h:236
virtual int startCaptureBlockingMode(OnPacketArrivesStopBlocking onPacketArrives, void *userCookie, int timeout)
DeviceConfiguration(DeviceMode mode=Promiscuous, int packetBufferTimeoutMs=0, int packetBufferSize=0)
Definition: PcapLiveDevice.h:185
virtual ~PcapLiveDevice()
int packetBufferTimeoutMs
Definition: PcapLiveDevice.h:165
Definition: PcapLiveDevice.h:133
const char * getName()
Definition: PcapLiveDevice.h:207
virtual bool startCapture(OnPacketArrivesCallback onPacketArrives, void *onPacketArrivesUserCookie)
void(* OnStatsUpdateCallback)(pcap_stat &stats, void *userCookie)
Definition: PcapLiveDevice.h:49
Definition: PcapLiveDevice.h:147
IPv4Address getDefaultGateway()
LiveDeviceType
Definition: PcapLiveDevice.h:128
DeviceMode mode
Definition: PcapLiveDevice.h:159
bool(* OnPacketArrivesStopBlocking)(RawPacket *pPacket, PcapLiveDevice *pDevice, void *userData)
Definition: PcapLiveDevice.h:40
The main namespace for the PcapPlusPlus lib.
LinkLayerType
Definition: RawPacket.h:24
virtual int sendPackets(RawPacket *rawPacketsArr, int arrLength)
void(* OnPacketArrivesCallback)(RawPacket *pPacket, PcapLiveDevice *pDevice, void *userCookie)
Definition: PcapLiveDevice.h:30
virtual void getStatistics(pcap_stat &stats)
Definition: PcapDevice.h:32