PcapPlusPlus  Next
PcapLiveDevice.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <atomic>
4 #include <vector>
5 #include <thread>
6 #include <functional>
7 
8 #include "IpAddress.h"
9 #include "PcapDevice.h"
10 
11 // forward declarations 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 
21 namespace pcpp
22 {
23  class PcapLiveDevice;
24 
29  using OnPacketArrivesCallback = std::function<void(RawPacket*, PcapLiveDevice*, void*)>;
30 
36  using OnPacketArrivesStopBlocking = std::function<bool(RawPacket*, PcapLiveDevice*, void*)>;
37 
42  using OnStatsUpdateCallback = std::function<void(IPcapDevice::PcapStats&, void*)>;
43 
65  class PcapLiveDevice : public IPcapDevice
66  {
67  friend class PcapLiveDeviceList;
68 
69  protected:
73  {
74  explicit DeviceInterfaceDetails(pcap_if_t* pInterface);
76  std::string name;
78  std::string description;
80  std::vector<IPAddress> addresses;
82  bool isLoopback;
83  };
84 
85  bool m_DeviceOpened = false;
86 
87  // This is a second descriptor for the same device. It is needed because of a bug
88  // that occurs in libpcap on Linux (on Windows using WinPcap/Npcap it works well):
89  // It's impossible to capture packets sent by the same descriptor
90  pcap_t* m_PcapSendDescriptor;
91  int m_PcapSelectableFd;
92  DeviceInterfaceDetails m_InterfaceDetails;
93  // NOTE@Dimi: Possibly pull mtu, mac address and default gateway in the interface details.
94  // They only appear to be set in the constructor and not modified afterwards.
95  uint32_t m_DeviceMtu;
96  MacAddress m_MacAddress;
97  IPv4Address m_DefaultGateway;
98  std::thread m_CaptureThread;
99  std::thread m_StatsThread;
100 
101  // Should be set to true by the Caller for the Callee
102  std::atomic<bool> m_StopThread;
103  // Should be set to true by the Callee for the Caller
104  std::atomic<bool> m_CaptureThreadStarted;
105 
106  OnPacketArrivesStopBlocking m_cbOnPacketArrivesBlockingMode;
107  void* m_cbOnPacketArrivesBlockingModeUserCookie;
108  LinkLayerType m_LinkType;
109  bool m_UsePoll;
110 
111  // c'tor is not public, there should be only one for every interface (created by PcapLiveDeviceList)
112  PcapLiveDevice(pcap_if_t* pInterface, bool calculateMTU, bool calculateMacAddress, bool calculateDefaultGateway)
113  : PcapLiveDevice(DeviceInterfaceDetails(pInterface), calculateMTU, calculateMacAddress,
114  calculateDefaultGateway)
115  {}
116  PcapLiveDevice(DeviceInterfaceDetails interfaceDetails, bool calculateMTU, bool calculateMacAddress,
117  bool calculateDefaultGateway);
118 
119  void setDeviceMtu();
120  void setDeviceMacAddress();
121  void setDefaultGateway();
122 
123  static void onPacketArrivesBlockingMode(uint8_t* user, const struct pcap_pkthdr* pkthdr, const uint8_t* packet);
124 
125  public:
128  {
135  };
136 
139  {
141  Normal = 0,
143  Promiscuous = 1
144  };
145 
149  {
155  PCPP_OUT
156  };
157 
160  enum class TimestampProvider
161  {
163  Host = 0,
169  Adapter,
174  };
175 
179  {
181  Microseconds = 0,
183  Nanoseconds,
184  };
185 
190  {
193 
198 
206 
210 
217 
220  unsigned int nflogGroup;
221 
223  bool usePoll;
224 
228 
232 
255  int snapshotLength = 0, unsigned int nflogGroup = 0, bool usePoll = false,
258  {
259  this->mode = mode;
260  this->packetBufferTimeoutMs = packetBufferTimeoutMs;
261  this->packetBufferSize = packetBufferSize;
262  this->direction = direction;
263  this->snapshotLength = snapshotLength;
264  this->nflogGroup = nflogGroup;
265  this->usePoll = usePoll;
266  this->timestampProvider = timestampProvider;
267  this->timestampPrecision = timestampPrecision;
268  }
269  };
270 
271  PcapLiveDevice(const PcapLiveDevice& other) = delete;
272  PcapLiveDevice& operator=(const PcapLiveDevice& other) = delete;
274  ~PcapLiveDevice() override;
275 
278  {
279  return LibPcapDevice;
280  }
281 
283  std::string getName() const
284  {
285  return m_InterfaceDetails.name;
286  }
287 
290  std::string getDesc() const
291  {
292  return m_InterfaceDetails.description;
293  }
294 
296  bool getLoopback() const
297  {
298  return m_InterfaceDetails.isLoopback;
299  }
300 
302  virtual uint32_t getMtu() const
303  {
304  return m_DeviceMtu;
305  }
306 
308  virtual LinkLayerType getLinkType() const
309  {
310  return m_LinkType;
311  }
312 
314  std::vector<IPAddress> getIPAddresses() const
315  {
316  return m_InterfaceDetails.addresses;
317  }
318 
320  virtual MacAddress getMacAddress() const
321  {
322  return m_MacAddress;
323  }
324 
329 
334 
339 
344  const std::vector<IPv4Address>& getDnsServers() const;
345 
361  virtual bool startCapture(OnPacketArrivesCallback onPacketArrives, void* onPacketArrivesUserCookie);
362 
386  virtual bool startCapture(OnPacketArrivesCallback onPacketArrives, void* onPacketArrivesUserCookie,
387  int intervalInSecondsToUpdateStats, OnStatsUpdateCallback onStatsUpdate,
388  void* onStatsUpdateUserCookie);
389 
407  virtual bool startCapture(int intervalInSecondsToUpdateStats, OnStatsUpdateCallback onStatsUpdate,
408  void* onStatsUpdateUserCookie);
409 
424  virtual bool startCapture(RawPacketVector& capturedPacketsVector);
425 
450  virtual int startCaptureBlockingMode(OnPacketArrivesStopBlocking onPacketArrives, void* userCookie,
451  const double timeout);
452 
456  void stopCapture();
457 
461 
465  bool doMtuCheck(int packetPayloadLength) const;
466 
480  PCPP_DEPRECATED("This method is deprecated. Use sendPacket(Packet const& packet, bool checkMtu) instead")
481  bool sendPacket(Packet* packet, bool checkMtu = true)
482  {
483  return sendPacket(*packet, checkMtu);
484  }
485 
498  bool sendPacket(Packet const& packet, bool checkMtu = true);
499 
512  bool sendPacket(RawPacket const& rawPacket, bool checkMtu = false);
513 
529  bool sendPacket(const uint8_t* packetData, int packetDataLength, int packetPayloadLength);
530 
546  bool sendPacket(const uint8_t* packetData, int packetDataLength, bool checkMtu = false,
548 
561  virtual int sendPackets(RawPacket* rawPacketsArr, int arrLength, bool checkMtu = false);
562 
575  virtual int sendPackets(Packet** packetsArr, int arrLength, bool checkMtu = true);
576 
588  virtual int sendPackets(const RawPacketVector& rawPackets, bool checkMtu = false);
589 
590  // implement abstract methods
591 
598  bool open() override;
599 
605  bool open(const DeviceConfiguration& config);
606 
607  void close() override;
608 
609  bool isOpened() const override
610  {
611  return m_DeviceOpened;
612  }
613 
616  virtual PcapLiveDevice* clone() const;
617 
618  void getStatistics(IPcapDevice::PcapStats& stats) const override;
619 
620  protected:
628  virtual void prepareCapture(bool asyncCapture, bool captureStats)
629  {}
630 
631  internal::PcapHandle doOpen(const DeviceConfiguration& config);
632 
636  bool isPayloadWithinMtu(size_t payloadLength) const;
637 
650  bool isPayloadWithinMtu(Packet const& packet, bool allowUnknownLength = false,
651  size_t* outPayloadLength = nullptr) const;
652 
665  bool isPayloadWithinMtu(RawPacket const& rawPacket, bool allowUnknownLength = false,
666  size_t* outPayloadLength = nullptr) const;
667 
682  bool isPayloadWithinMtu(uint8_t const* packetData, size_t packetLen,
683  LinkLayerType linkType = pcpp::LINKTYPE_ETHERNET, bool allowUnknownLength = false,
684  size_t* outPayloadLength = nullptr) const;
685 
686  // Sends a packet directly to the network.
687  bool sendPacketUnchecked(uint8_t const* packetData, int packetDataLength);
688  bool sendPacketUnchecked(RawPacket const& rawPacket)
689  {
690  return sendPacketUnchecked(rawPacket.getRawData(), rawPacket.getRawDataLen());
691  }
692 
693  private:
694  bool isNflogDevice() const;
695  };
696 } // namespace pcpp
Definition: PcapDevice.h:142
Definition: IpAddress.h:30
Definition: IpAddress.h:165
Definition: MacAddress.h:24
Definition: Packet.h:22
Definition: PcapLiveDevice.h:66
PcapDirection
Definition: PcapLiveDevice.h:149
@ PCPP_IN
Only capture incoming traffics.
Definition: PcapLiveDevice.h:153
@ PCPP_OUT
Only capture outgoing traffics.
Definition: PcapLiveDevice.h:155
@ PCPP_INOUT
Capture traffics both incoming and outgoing.
Definition: PcapLiveDevice.h:151
void close() override
Close the device.
LiveDeviceType
The type of the live device.
Definition: PcapLiveDevice.h:128
@ WinPcapDevice
WinPcap/Npcap live device.
Definition: PcapLiveDevice.h:132
@ RemoteDevice
WinPcap/Npcap Remote Capture device.
Definition: PcapLiveDevice.h:134
@ LibPcapDevice
libPcap live device
Definition: PcapLiveDevice.h:130
virtual int startCaptureBlockingMode(OnPacketArrivesStopBlocking onPacketArrives, void *userCookie, const double timeout)
DeviceMode
Device capturing mode.
Definition: PcapLiveDevice.h:139
@ Normal
Only packets that their destination is this NIC are captured.
Definition: PcapLiveDevice.h:141
@ Promiscuous
All packets that arrive to the NIC are captured, even packets that their destination isn't this NIC.
Definition: PcapLiveDevice.h:143
virtual bool startCapture(int intervalInSecondsToUpdateStats, OnStatsUpdateCallback onStatsUpdate, void *onStatsUpdateUserCookie)
bool sendPacket(RawPacket const &rawPacket, bool checkMtu=false)
virtual uint32_t getMtu() const
Definition: PcapLiveDevice.h:302
virtual bool startCapture(OnPacketArrivesCallback onPacketArrives, void *onPacketArrivesUserCookie)
void getStatistics(IPcapDevice::PcapStats &stats) const override
bool open(const DeviceConfiguration &config)
bool doMtuCheck(int packetPayloadLength) const
virtual MacAddress getMacAddress() const
Definition: PcapLiveDevice.h:320
bool sendPacket(const uint8_t *packetData, int packetDataLength, int packetPayloadLength)
IPv4Address getDefaultGateway() const
std::string getDesc() const
Definition: PcapLiveDevice.h:290
bool sendPacket(const uint8_t *packetData, int packetDataLength, bool checkMtu=false, pcpp::LinkLayerType linkType=pcpp::LINKTYPE_ETHERNET)
const std::vector< IPv4Address > & getDnsServers() const
virtual int sendPackets(Packet **packetsArr, int arrLength, bool checkMtu=true)
bool isPayloadWithinMtu(size_t payloadLength) const
Checks whether the packetPayloadLength is smaller or equal than the device MTU.
virtual LiveDeviceType getDeviceType() const
Definition: PcapLiveDevice.h:277
virtual bool startCapture(OnPacketArrivesCallback onPacketArrives, void *onPacketArrivesUserCookie, int intervalInSecondsToUpdateStats, OnStatsUpdateCallback onStatsUpdate, void *onStatsUpdateUserCookie)
TimestampPrecision
Definition: PcapLiveDevice.h:179
@ Microseconds
use timestamps with microsecond precision, default
@ Nanoseconds
use timestamps with nanosecond precision
IPv6Address getIPv6Address() const
virtual bool startCapture(RawPacketVector &capturedPacketsVector)
~PcapLiveDevice() override
A destructor for this class.
bool sendPacket(Packet const &packet, bool checkMtu=true)
std::string getName() const
Definition: PcapLiveDevice.h:283
bool isPayloadWithinMtu(uint8_t const *packetData, size_t packetLen, LinkLayerType linkType=pcpp::LINKTYPE_ETHERNET, bool allowUnknownLength=false, size_t *outPayloadLength=nullptr) const
Checks whether the payload length of a packet's raw data is smaller or equal than the device MTU.
IPv4Address getIPv4Address() const
bool sendPacket(Packet *packet, bool checkMtu=true)
Definition: PcapLiveDevice.h:481
virtual int sendPackets(const RawPacketVector &rawPackets, bool checkMtu=false)
virtual int sendPackets(RawPacket *rawPacketsArr, int arrLength, bool checkMtu=false)
bool isPayloadWithinMtu(Packet const &packet, bool allowUnknownLength=false, size_t *outPayloadLength=nullptr) const
Checks whether the packet's payload length is smaller or equal than the device MTU.
virtual LinkLayerType getLinkType() const
Definition: PcapLiveDevice.h:308
bool isOpened() const override
Definition: PcapLiveDevice.h:609
TimestampProvider
Definition: PcapLiveDevice.h:161
@ AdapterUnsynced
device-provided, not synced with the system clock
@ HostLowPrecision
host-provided, low precision, synced with the system clock
@ Adapter
device-provided, synced with the system clock
@ HostHighPrecision
host-provided, high precision, synced with the system clock
@ Host
host-provided, unknown characteristics, default
@ HostHighPrecisionUnsynced
host-provided, high precision, not synced with the system clock
bool getLoopback() const
Definition: PcapLiveDevice.h:296
virtual PcapLiveDevice * clone() const
bool isPayloadWithinMtu(RawPacket const &rawPacket, bool allowUnknownLength=false, size_t *outPayloadLength=nullptr) const
Checks whether the payload length of a RawPacket is smaller or equal than the device MTU.
bool open() override
std::vector< IPAddress > getIPAddresses() const
Definition: PcapLiveDevice.h:314
virtual void prepareCapture(bool asyncCapture, bool captureStats)
Called before starting a capture to prepare the device for capturing packets.
Definition: PcapLiveDevice.h:628
Definition: PcapLiveDeviceList.h:22
Definition: PointerVector.h:50
Definition: RawPacket.h:259
const uint8_t * getRawData() const
Definition: RawPacket.h:370
int getRawDataLen() const
Definition: RawPacket.h:389
A wrapper class for pcap_t* which is the libpcap packet capture descriptor. This class is used to man...
Definition: PcapDevice.h:38
The main namespace for the PcapPlusPlus lib.
Definition: AssertionUtils.h:19
std::function< bool(RawPacket *, PcapLiveDevice *, void *)> OnPacketArrivesStopBlocking
Definition: PcapLiveDevice.h:36
std::function< void(RawPacket *, PcapLiveDevice *, void *)> OnPacketArrivesCallback
Definition: PcapLiveDevice.h:29
std::function< void(IPcapDevice::PcapStats &, void *)> OnStatsUpdateCallback
Definition: PcapLiveDevice.h:42
LinkLayerType
An enum describing all known link layer type. Taken from: http://www.tcpdump.org/linktypes....
Definition: RawPacket.h:20
@ LINKTYPE_ETHERNET
IEEE 802.3 Ethernet.
Definition: RawPacket.h:24
Definition: PcapLiveDevice.h:190
int packetBufferTimeoutMs
Definition: PcapLiveDevice.h:197
int snapshotLength
Definition: PcapLiveDevice.h:216
TimestampProvider timestampProvider
Definition: PcapLiveDevice.h:227
DeviceConfiguration(DeviceMode mode=Promiscuous, int packetBufferTimeoutMs=0, int packetBufferSize=0, PcapDirection direction=PCPP_INOUT, int snapshotLength=0, unsigned int nflogGroup=0, bool usePoll=false, TimestampProvider timestampProvider=TimestampProvider::Host, TimestampPrecision timestampPrecision=TimestampPrecision::Microseconds)
Definition: PcapLiveDevice.h:253
bool usePoll
In Unix-like system, use poll() for blocking mode.
Definition: PcapLiveDevice.h:223
int packetBufferSize
Definition: PcapLiveDevice.h:205
unsigned int nflogGroup
Definition: PcapLiveDevice.h:220
PcapDirection direction
Definition: PcapLiveDevice.h:209
TimestampPrecision timestampPrecision
Definition: PcapLiveDevice.h:231
DeviceMode mode
Indicates whether to open the device in promiscuous or normal mode.
Definition: PcapLiveDevice.h:192
A struct that contains all details of a network interface.
Definition: PcapLiveDevice.h:73
std::string description
Description of the device.
Definition: PcapLiveDevice.h:78
std::string name
Name of the device.
Definition: PcapLiveDevice.h:76
std::vector< IPAddress > addresses
IP addresses associated with the device.
Definition: PcapLiveDevice.h:80
bool isLoopback
Flag to indicate if the device is a loopback device.
Definition: PcapLiveDevice.h:82
Definition: PcapDevice.h:25