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 
106  // c'tor is not public, there should be only one for every interface (created by PcapLiveDeviceList)
107  PcapLiveDevice(pcap_if_t* pInterface, bool calculateMTU, bool calculateMacAddress, bool calculateDefaultGateway);
108  // copy c'tor is not public
109  PcapLiveDevice( const PcapLiveDevice& other );
110  PcapLiveDevice& operator=(const PcapLiveDevice& other);
111 
112  void setDeviceMtu();
113  void setDeviceMacAddress();
114  void setDefaultGateway();
115  static void* captureThreadMain(void *ptr);
116  static void* statsThreadMain(void *ptr);
117  static void onPacketArrives(uint8_t *user, const struct pcap_pkthdr *pkthdr, const uint8_t *packet);
118  static void onPacketArrivesNoCallback(uint8_t *user, const struct pcap_pkthdr *pkthdr, const uint8_t *packet);
119  static void onPacketArrivesBlockingMode(uint8_t *user, const struct pcap_pkthdr *pkthdr, const uint8_t *packet);
120  std::string printThreadId(PcapThread* id);
121  virtual ThreadStart getCaptureThreadStart();
122  public:
133  };
134 
138  enum DeviceMode {
140  Normal = 0,
143  };
144 
148  virtual ~PcapLiveDevice();
149 
154 
158  inline const char* getName() { return m_Name; }
159 
163  inline const char* getDesc() { return m_Description; }
164 
168  inline bool getLoopback() { return m_IsLoopback; }
169 
173  virtual inline uint16_t getMtu() { return m_DeviceMtu; }
174 
178  inline std::vector<pcap_addr_t>& getAddresses() { return m_Addresses; }
179 
183  virtual inline MacAddress getMacAddress() { return m_MacAddress; }
184 
190 
196 
202  std::vector<IPv4Address>& getDnsServers();
203 
218  virtual bool startCapture(OnPacketArrivesCallback onPacketArrives, void* onPacketArrivesUserCookie);
219 
242  virtual bool startCapture(OnPacketArrivesCallback onPacketArrives, void* onPacketArrivesUserCookie, int intervalInSecondsToUpdateStats, OnStatsUpdateCallback onStatsUpdate, void* onStatsUpdateUserCookie);
243 
261  virtual bool startCapture(int intervalInSecondsToUpdateStats, OnStatsUpdateCallback onStatsUpdate, void* onStatsUpdateUserCookie);
262 
276  virtual bool startCapture(RawPacketVector& capturedPacketsVector);
277 
297  virtual int startCaptureBlockingMode(OnPacketArrivesStopBlocking onPacketArrives, void* userCookie, int timeout);
298 
303  void stopCapture();
304 
315  bool sendPacket(RawPacket const& rawPacket);
316 
327  bool sendPacket(const uint8_t* packetData, int packetDataLength);
328 
338  bool sendPacket(Packet* packet);
339 
351  virtual int sendPackets(RawPacket* rawPacketsArr, int arrLength);
352 
364  virtual int sendPackets(Packet** packetsArr, int arrLength);
365 
376  virtual int sendPackets(const RawPacketVector& rawPackets);
377 
378  //override methods
379 
387  bool open();
388 
389  void close();
390 
391  virtual void getStatistics(pcap_stat& stats);
392 
398  bool open(DeviceMode mode);
399  protected:
400  pcap_t* doOpen(DeviceMode mode);
401  };
402 
403 } // namespace pcpp
404 
405 #endif
virtual int startCaptureBlockingMode(OnPacketArrivesStopBlocking onPacketArrives, void *userCookie, int timeout)
virtual MacAddress getMacAddress()
Definition: PcapLiveDevice.h:183
Definition: RawPacket.h:220
Definition: PcapLiveDevice.h:132
virtual ~PcapLiveDevice()
LiveDeviceType
Definition: PcapLiveDevice.h:126
std::vector< IPv4Address > & getDnsServers()
Definition: PcapLiveDevice.h:128
IPv4Address getIPv4Address()
bool sendPacket(RawPacket const &rawPacket)
Definition: PcapDevice.h:26
bool getLoopback()
Definition: PcapLiveDevice.h:168
std::vector< pcap_addr_t > & getAddresses()
Definition: PcapLiveDevice.h:178
Definition: MacAddress.h:21
virtual LiveDeviceType getDeviceType()
Definition: PcapLiveDevice.h:153
const char * getName()
Definition: PcapLiveDevice.h:158
Definition: IpAddress.h:99
virtual void getStatistics(pcap_stat &stats)
Definition: Packet.h:26
void(* OnPacketArrivesCallback)(RawPacket *pPacket, PcapLiveDevice *pDevice, void *userCookie)
Definition: PcapLiveDevice.h:30
virtual int sendPackets(RawPacket *rawPacketsArr, int arrLength)
Definition: PcapLiveDeviceList.h:25
const char * getDesc()
Definition: PcapLiveDevice.h:163
Definition: PcapLiveDevice.h:140
Definition: PcapLiveDevice.h:130
void(* OnStatsUpdateCallback)(pcap_stat &stats, void *userCookie)
Definition: PcapLiveDevice.h:49
bool(* OnPacketArrivesStopBlocking)(RawPacket *pPacket, PcapLiveDevice *pDevice, void *userData)
Definition: PcapLiveDevice.h:40
virtual uint16_t getMtu()
Definition: PcapLiveDevice.h:173
virtual bool startCapture(OnPacketArrivesCallback onPacketArrives, void *onPacketArrivesUserCookie)
Definition: PcapLiveDevice.h:76
The main namespace for the PcapPlusPlus lib.
IPv4Address getDefaultGateway()
Definition: PointerVector.h:24
DeviceMode
Definition: PcapLiveDevice.h:138
Definition: PcapLiveDevice.h:142