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  {
23  struct PcapStats
24  {
26  uint64_t packetsRecv;
28  uint64_t packetsDrop;
31  };
32 
36  class PcapHandle
37  {
38  public:
40  constexpr PcapHandle() noexcept = default;
43  explicit PcapHandle(pcap_t* pcapDescriptor) noexcept;
44 
45  PcapHandle(const PcapHandle&) = delete;
46  PcapHandle(PcapHandle&& other) noexcept;
47 
48  PcapHandle& operator=(const PcapHandle&) = delete;
49  PcapHandle& operator=(PcapHandle&& other) noexcept;
50  PcapHandle& operator=(std::nullptr_t) noexcept;
51 
52  ~PcapHandle();
53 
55  bool isValid() const noexcept
56  {
57  return m_PcapDescriptor != nullptr;
58  }
59 
61  pcap_t* get() const noexcept
62  {
63  return m_PcapDescriptor;
64  }
65 
68  pcap_t* release() noexcept;
69 
73  void reset(pcap_t* pcapDescriptor = nullptr) noexcept;
74 
78  char const* getLastError() const noexcept;
79 
86  bool setFilter(std::string const& filter);
87 
90  bool clearFilter();
91 
98  bool getStatistics(PcapStats& stats) const;
99 
101  explicit operator bool() const noexcept
102  {
103  return isValid();
104  }
105 
106  bool operator==(std::nullptr_t) const noexcept
107  {
108  return !isValid();
109  }
110  bool operator!=(std::nullptr_t) const noexcept
111  {
112  return isValid();
113  }
114 
115  private:
116  pcap_t* m_PcapDescriptor = nullptr;
117  };
118  } // namespace internal
119 
122  {
123  public:
124  virtual ~IPcapStatisticsProvider() = default;
125 
127 
131 
134  virtual void getStatistics(PcapStats& stats) const = 0;
135  };
136 
141  {
142  protected:
143  internal::PcapHandle m_PcapDescriptor;
144 
145  // c'tor should not be public
146  IPcapDevice() : IDevice()
147  {}
148 
149  public:
150  virtual ~IPcapDevice() = default;
151 
155  static std::string getPcapLibVersionInfo();
156 
162  static bool matchPacketWithFilter(GeneralFilter& filter, RawPacket* rawPacket);
163 
164  // implement abstract methods
165 
167 
174  bool setFilter(std::string filterAsString) override;
175 
178  bool clearFilter() override;
179  };
180 } // 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:141
bool setFilter(std::string filterAsString) override
bool clearFilter() override
static bool matchPacketWithFilter(GeneralFilter &filter, RawPacket *rawPacket)
static std::string getPcapLibVersionInfo()
An interface for providing Pcap-based device statistics.
Definition: PcapDevice.h:122
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:37
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:61
bool isValid() const noexcept
Definition: PcapDevice.h:55
The main namespace for the PcapPlusPlus lib.
Definition: PcapDevice.h:24
uint64_t packetsDropByInterface
number of packets dropped by interface (not supported on all platforms)
Definition: PcapDevice.h:30
uint64_t packetsRecv
Number of packets received.
Definition: PcapDevice.h:26
uint64_t packetsDrop
Number of packets dropped.
Definition: PcapDevice.h:28