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 
16 namespace pcpp
17 {
18  // Forward Declaration - required for IPcapDevice::matchPacketWithFilter
19  class GeneralFilter;
20 
21  namespace internal
22  {
28  class PcapHandle
29  {
30  public:
34  constexpr PcapHandle() noexcept = default;
39  explicit PcapHandle(pcap_t* pcapDescriptor) noexcept;
40 
41  PcapHandle(const PcapHandle&) = delete;
42  PcapHandle(PcapHandle&& other) noexcept;
43 
44  PcapHandle& operator=(const PcapHandle&) = delete;
45  PcapHandle& operator=(PcapHandle&& other) noexcept;
46  PcapHandle& operator=(std::nullptr_t) noexcept;
47 
48  ~PcapHandle();
49 
53  bool isValid() const noexcept
54  {
55  return m_PcapDescriptor != nullptr;
56  }
57 
61  pcap_t* get() const noexcept
62  {
63  return m_PcapDescriptor;
64  }
65 
70  pcap_t* release() noexcept;
71 
77  void reset(pcap_t* pcapDescriptor = nullptr) noexcept;
78 
84  char const* getLastError() const noexcept;
85 
89  explicit operator bool() const noexcept
90  {
91  return isValid();
92  }
93 
94  bool operator==(std::nullptr_t) const noexcept
95  {
96  return !isValid();
97  }
98  bool operator!=(std::nullptr_t) const noexcept
99  {
100  return isValid();
101  }
102 
103  private:
104  pcap_t* m_PcapDescriptor = nullptr;
105  };
106  } // namespace internal
107 
113  class IPcapDevice : public IDevice, public IFilterableDevice
114  {
115  protected:
116  internal::PcapHandle m_PcapDescriptor;
117 
118  // c'tor should not be public
119  IPcapDevice() : IDevice()
120  {}
121 
122  public:
127  struct PcapStats
128  {
130  uint64_t packetsRecv;
132  uint64_t packetsDrop;
135  };
136 
137  virtual ~IPcapDevice();
138 
143  virtual void getStatistics(PcapStats& stats) const = 0;
144 
150  static std::string getPcapLibVersionInfo();
151 
159  static bool matchPacketWithFilter(GeneralFilter& filter, RawPacket* rawPacket);
160 
161  // implement abstract methods
162 
164 
173  virtual bool setFilter(std::string filterAsString);
174 
179  bool clearFilter();
180  };
181 
182 } // namespace pcpp
Definition: PcapFilter.h:160
Definition: Device.h:24
Definition: Device.h:63
virtual bool setFilter(GeneralFilter &filter)
Definition: Device.h:79
Definition: PcapDevice.h:114
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:29
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:61
bool isValid() const noexcept
Definition: PcapDevice.h:53
The main namespace for the PcapPlusPlus lib.
Definition: PcapDevice.h:128
uint64_t packetsRecv
Definition: PcapDevice.h:130
uint64_t packetsDrop
Definition: PcapDevice.h:132
uint64_t packetsDropByInterface
Definition: PcapDevice.h:134