PcapPlusPlus  21.05
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 // forward declerations for structs and typedefs that are defined in pcap.h
12 struct pcap_if;
13 typedef pcap_if pcap_if_t;
14 struct pcap_addr;
15 typedef struct pcap_addr pcap_addr_t;
16 
18 
23 namespace pcpp
24 {
25 
26  class PcapLiveDevice;
27 
35  typedef void (*OnPacketArrivesCallback)(RawPacket* pPacket, PcapLiveDevice* pDevice, void* userCookie);
36 
45  typedef bool (*OnPacketArrivesStopBlocking)(RawPacket* pPacket, PcapLiveDevice* pDevice, void* userData);
46 
47 
54  typedef void (*OnStatsUpdateCallback)(IPcapDevice::PcapStats& stats, void* userCookie);
55 
56  // for internal use only
57  typedef void* (*ThreadStart)(void*);
58 
59  struct PcapThread;
60 
81  class PcapLiveDevice : public IPcapDevice
82  {
83  friend class PcapLiveDeviceList;
84  protected:
85  // This is a second descriptor for the same device. It is needed because of a bug
86  // that occurs in libpcap on Linux (on Windows using WinPcap/Npcap it works well):
87  // It's impossible to capture packets sent by the same descriptor
88  pcap_t* m_PcapSendDescriptor;
89  std::string m_Name;
90  std::string m_Description;
91  bool m_IsLoopback;
92  uint32_t m_DeviceMtu;
93  std::vector<pcap_addr_t> m_Addresses;
94  MacAddress m_MacAddress;
95  IPv4Address m_DefaultGateway;
96  PcapThread* m_CaptureThread;
97  bool m_CaptureThreadStarted;
98  PcapThread* m_StatsThread;
99  bool m_StatsThreadStarted;
100  bool m_StopThread;
101  OnPacketArrivesCallback m_cbOnPacketArrives;
102  void* m_cbOnPacketArrivesUserCookie;
103  OnStatsUpdateCallback m_cbOnStatsUpdate;
104  void* m_cbOnStatsUpdateUserCookie;
105  OnPacketArrivesStopBlocking m_cbOnPacketArrivesBlockingMode;
106  void* m_cbOnPacketArrivesBlockingModeUserCookie;
107  int m_IntervalToUpdateStats;
108  RawPacketVector* m_CapturedPackets;
109  bool m_CaptureCallbackMode;
110  LinkLayerType m_LinkType;
111 
112  // c'tor is not public, there should be only one for every interface (created by PcapLiveDeviceList)
113  PcapLiveDevice(pcap_if_t* pInterface, bool calculateMTU, bool calculateMacAddress, bool calculateDefaultGateway);
114  // copy c'tor is not public
115  PcapLiveDevice( const PcapLiveDevice& other );
116  PcapLiveDevice& operator=(const PcapLiveDevice& other);
117 
118  void setDeviceMtu();
119  void setDeviceMacAddress();
120  void setDefaultGateway();
121  static void* captureThreadMain(void* ptr);
122  static void* statsThreadMain(void* ptr);
123  static void onPacketArrives(uint8_t* user, const struct pcap_pkthdr* pkthdr, const uint8_t* packet);
124  static void onPacketArrivesNoCallback(uint8_t* user, const struct pcap_pkthdr* pkthdr, const uint8_t* packet);
125  static void onPacketArrivesBlockingMode(uint8_t* user, const struct pcap_pkthdr* pkthdr, const uint8_t* packet);
126  std::string printThreadId(PcapThread* id);
127  virtual ThreadStart getCaptureThreadStart();
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 
294 
300  const std::vector<IPv4Address>& getDnsServers() const;
301 
316  virtual bool startCapture(OnPacketArrivesCallback onPacketArrives, void* onPacketArrivesUserCookie);
317 
340  virtual bool startCapture(OnPacketArrivesCallback onPacketArrives, void* onPacketArrivesUserCookie, int intervalInSecondsToUpdateStats, OnStatsUpdateCallback onStatsUpdate, void* onStatsUpdateUserCookie);
341 
359  virtual bool startCapture(int intervalInSecondsToUpdateStats, OnStatsUpdateCallback onStatsUpdate, void* onStatsUpdateUserCookie);
360 
374  virtual bool startCapture(RawPacketVector& capturedPacketsVector);
375 
395  virtual int startCaptureBlockingMode(OnPacketArrivesStopBlocking onPacketArrives, void* userCookie, int timeout);
396 
401  void stopCapture();
402 
407  bool captureActive();
408 
414  bool doMtuCheck(int packetPayloadLength);
415 
428  bool sendPacket(RawPacket const& rawPacket, bool checkMtu = false);
429 
445  bool sendPacket(const uint8_t* packetData, int packetDataLength, int packetPayloadLength);
446 
461  bool sendPacket(const uint8_t* packetData, int packetDataLength, bool checkMtu = false, pcpp::LinkLayerType linkType = pcpp::LINKTYPE_ETHERNET);
462 
474  bool sendPacket(Packet* packet, bool checkMtu = true);
475 
489  virtual int sendPackets(RawPacket* rawPacketsArr, int arrLength, bool checkMtu = false);
490 
504  virtual int sendPackets(Packet** packetsArr, int arrLength, bool checkMtu = true);
505 
518  virtual int sendPackets(const RawPacketVector& rawPackets, bool checkMtu = false);
519 
520 
521  // implement abstract methods
522 
530  bool open();
531 
538  bool open(const DeviceConfiguration& config);
539 
540  void close();
541 
542  virtual void getStatistics(IPcapDevice::PcapStats& stats) const;
543 
544  protected:
545  pcap_t* doOpen(const DeviceConfiguration& config);
546  };
547 
548 } // namespace pcpp
549 
550 #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
Definition: RawPacket.h:30
virtual MacAddress getMacAddress() const
Definition: PcapLiveDevice.h:281
Definition: PcapLiveDevice.h:150
bool doMtuCheck(int packetPayloadLength)
Definition: PcapDevice.h:39
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:54
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:26
IPv4Address getIPv4Address() const
void(* OnPacketArrivesCallback)(RawPacket *pPacket, PcapLiveDevice *pDevice, void *userCookie)
Definition: PcapLiveDevice.h:35
Definition: PcapLiveDevice.h:136
Definition: PcapLiveDevice.h:81
std::string getDesc() const
Definition: PcapLiveDevice.h:256
LinkLayerType
Definition: RawPacket.h:25
bool(* OnPacketArrivesStopBlocking)(RawPacket *pPacket, PcapLiveDevice *pDevice, void *userData)
Definition: PcapLiveDevice.h:45
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:27
Definition: PcapDevice.h:53
virtual int sendPackets(RawPacket *rawPacketsArr, int arrLength, bool checkMtu=false)
Definition: PcapLiveDeviceList.h:25
std::string getName() const
Definition: PcapLiveDevice.h:251