PcapPlusPlus  Next
PcapDevice.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "Device.h"
4 #include "DeprecationUtils.h"
5 
6 // forward declaration for the pcap descriptor defined in pcap.h
7 struct pcap;
8 typedef pcap pcap_t;
9 struct pcap_pkthdr;
10 
12 
15 namespace pcpp
16 {
17  // Forward Declaration - required for IPcapDevice::matchPacketWithFilter
18  class GeneralFilter;
19 
20  namespace internal
21  {
24  struct PcapStats
25  {
27  uint64_t packetsRecv;
29  uint64_t packetsDrop;
32  };
33 
37  class PcapHandle
38  {
39  public:
41  constexpr PcapHandle() noexcept = default;
44  explicit PcapHandle(pcap_t* pcapDescriptor) noexcept;
45 
46  PcapHandle(const PcapHandle&) = delete;
47  PcapHandle(PcapHandle&& other) noexcept;
48 
49  PcapHandle& operator=(const PcapHandle&) = delete;
50  PcapHandle& operator=(PcapHandle&& other) noexcept;
51  PcapHandle& operator=(std::nullptr_t) noexcept;
52 
53  ~PcapHandle();
54 
56  bool isValid() const noexcept
57  {
58  return m_PcapDescriptor != nullptr;
59  }
60 
62  pcap_t* get() const noexcept
63  {
64  return m_PcapDescriptor;
65  }
66 
69  pcap_t* release() noexcept;
70 
74  void reset(pcap_t* pcapDescriptor = nullptr) noexcept;
75 
79  char const* getLastError() const noexcept;
80 
87  bool setFilter(std::string const& filter);
88 
91  bool clearFilter();
92 
99  bool getStatistics(PcapStats& stats) const;
100 
102  explicit operator bool() const noexcept
103  {
104  return isValid();
105  }
106 
107  bool operator==(std::nullptr_t) const noexcept
108  {
109  return !isValid();
110  }
111  bool operator!=(std::nullptr_t) const noexcept
112  {
113  return isValid();
114  }
115 
116  private:
117  pcap_t* m_PcapDescriptor = nullptr;
118  };
119  } // namespace internal
120 
123  {
124  public:
125  virtual ~IPcapStatisticsProvider() = default;
126 
128 
132 
135  virtual void getStatistics(PcapStats& stats) const = 0;
136  };
137 
142  {
143  protected:
144  internal::PcapHandle m_PcapDescriptor;
145 
146  // c'tor should not be public
147  IPcapDevice() : IDevice()
148  {}
149 
150  public:
151  virtual ~IPcapDevice() = default;
152 
156  static std::string getPcapLibVersionInfo();
157 
164  PCPP_DEPRECATED("Prefer GeneralFilter::matches(...) method directly.")
165  static bool matchPacketWithFilter(GeneralFilter& filter, RawPacket* rawPacket);
166 
167  // implement abstract methods
168 
170 
177  bool setFilter(std::string filterAsString) override;
178 
181  bool clearFilter() override;
182  };
183 } // namespace pcpp
Definition: PcapFilter.h:158
Definition: Device.h:20
Definition: Device.h:44
Definition: PcapDevice.h:142
bool clearFilter() override
static bool matchPacketWithFilter(GeneralFilter &filter, RawPacket *rawPacket)
static std::string getPcapLibVersionInfo()
virtual bool setFilter(GeneralFilter &filter)
Definition: Device.h:56
An interface for providing Pcap-based device statistics.
Definition: PcapDevice.h:123
virtual void getStatistics(PcapStats &stats) const =0
PcapStats getStatistics() const
Get statistics from the device.
Definition: RawPacket.h:259
A wrapper class for pcap_t* which is the libpcap packet capture descriptor. This class is used to man...
Definition: PcapDevice.h:38
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.
bool setFilter(std::string const &filter)
Sets a filter on the handle. Only packets that match the filter will be captured by the handle.
void reset(pcap_t *pcapDescriptor=nullptr) noexcept
Replaces the managed handle with the provided one.
bool clearFilter()
Clears the filter currently set on the handle.
constexpr PcapHandle() noexcept=default
Creates an empty handle.
bool getStatistics(PcapStats &stats) const
Retrieves statistics from the pcap handle.
pcap_t * get() const noexcept
Definition: PcapDevice.h:62
bool isValid() const noexcept
Definition: PcapDevice.h:56
The main namespace for the PcapPlusPlus lib.
Definition: AssertionUtils.h:19
Definition: PcapDevice.h:25
uint64_t packetsDropByInterface
number of packets dropped by interface (not supported on all platforms)
Definition: PcapDevice.h:31
uint64_t packetsRecv
Number of packets received.
Definition: PcapDevice.h:27
uint64_t packetsDrop
Number of packets dropped.
Definition: PcapDevice.h:29