PcapPlusPlus  Next
PcapDevice.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "Device.h"
4 
5 // forward declaration for the pcap descriptor defined in pcap.h
6 struct pcap;
7 typedef pcap pcap_t;
8 struct pcap_pkthdr;
9 
11 
14 namespace pcpp
15 {
16  // Forward Declaration - required for IPcapDevice::matchPacketWithFilter
17  class GeneralFilter;
18 
19  namespace internal
20  {
24  class PcapHandle
25  {
26  public:
28  constexpr PcapHandle() noexcept = default;
31  explicit PcapHandle(pcap_t* pcapDescriptor) noexcept;
32 
33  PcapHandle(const PcapHandle&) = delete;
34  PcapHandle(PcapHandle&& other) noexcept;
35 
36  PcapHandle& operator=(const PcapHandle&) = delete;
37  PcapHandle& operator=(PcapHandle&& other) noexcept;
38  PcapHandle& operator=(std::nullptr_t) noexcept;
39 
40  ~PcapHandle();
41 
43  bool isValid() const noexcept
44  {
45  return m_PcapDescriptor != nullptr;
46  }
47 
49  pcap_t* get() const noexcept
50  {
51  return m_PcapDescriptor;
52  }
53 
56  pcap_t* release() noexcept;
57 
61  void reset(pcap_t* pcapDescriptor = nullptr) noexcept;
62 
66  char const* getLastError() const noexcept;
67 
69  explicit operator bool() const noexcept
70  {
71  return isValid();
72  }
73 
74  bool operator==(std::nullptr_t) const noexcept
75  {
76  return !isValid();
77  }
78  bool operator!=(std::nullptr_t) const noexcept
79  {
80  return isValid();
81  }
82 
83  private:
84  pcap_t* m_PcapDescriptor = nullptr;
85  };
86  } // namespace internal
87 
91  class IPcapDevice : public IDevice, public IFilterableDevice
92  {
93  protected:
94  internal::PcapHandle m_PcapDescriptor;
95 
96  // c'tor should not be public
97  IPcapDevice() : IDevice()
98  {}
99 
100  public:
103  struct PcapStats
104  {
106  uint64_t packetsRecv;
108  uint64_t packetsDrop;
111  };
112 
113  virtual ~IPcapDevice();
114 
117  virtual void getStatistics(PcapStats& stats) const = 0;
118 
122  static std::string getPcapLibVersionInfo();
123 
129  static bool matchPacketWithFilter(GeneralFilter& filter, RawPacket* rawPacket);
130 
131  // implement abstract methods
132 
134 
141  virtual bool setFilter(std::string filterAsString);
142 
145  bool clearFilter();
146  };
147 } // namespace pcpp
Definition: PcapFilter.h:134
Definition: Device.h:20
Definition: Device.h:51
virtual bool setFilter(GeneralFilter &filter)
Definition: Device.h:63
Definition: PcapDevice.h:92
virtual bool setFilter(std::string filterAsString)
virtual void getStatistics(PcapStats &stats) const =0
static bool matchPacketWithFilter(GeneralFilter &filter, RawPacket *rawPacket)
static std::string getPcapLibVersionInfo()
Definition: RawPacket.h:269
A wrapper class for pcap_t* which is the libpcap packet capture descriptor. This class is used to man...
Definition: PcapDevice.h:25
pcap_t * release() noexcept
Releases ownership of the handle and returns the pcap descriptor.
char const * getLastError() const noexcept
Helper function to retrieve a view of the last error string for this handle.
void reset(pcap_t *pcapDescriptor=nullptr) noexcept
Replaces the managed handle with the provided one.
constexpr PcapHandle() noexcept=default
Creates an empty handle.
pcap_t * get() const noexcept
Definition: PcapDevice.h:49
bool isValid() const noexcept
Definition: PcapDevice.h:43
The main namespace for the PcapPlusPlus lib.
Definition: PcapDevice.h:104
uint64_t packetsRecv
Number of packets received.
Definition: PcapDevice.h:106
uint64_t packetsDrop
Number of packets dropped.
Definition: PcapDevice.h:108
uint64_t packetsDropByInterface
number of packets dropped by interface (not supported on all platforms)
Definition: PcapDevice.h:110