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 
74  bool setFilter(std::string const& filter);
75 
78  bool clearFilter();
79 
81  explicit operator bool() const noexcept
82  {
83  return isValid();
84  }
85 
86  bool operator==(std::nullptr_t) const noexcept
87  {
88  return !isValid();
89  }
90  bool operator!=(std::nullptr_t) const noexcept
91  {
92  return isValid();
93  }
94 
95  private:
96  pcap_t* m_PcapDescriptor = nullptr;
97  };
98  } // namespace internal
99 
103  class IPcapDevice : public IDevice, public IFilterableDevice
104  {
105  protected:
106  internal::PcapHandle m_PcapDescriptor;
107 
108  // c'tor should not be public
109  IPcapDevice() : IDevice()
110  {}
111 
112  public:
115  struct PcapStats
116  {
118  uint64_t packetsRecv;
120  uint64_t packetsDrop;
123  };
124 
125  virtual ~IPcapDevice();
126 
129  virtual void getStatistics(PcapStats& stats) const = 0;
130 
134  static std::string getPcapLibVersionInfo();
135 
141  static bool matchPacketWithFilter(GeneralFilter& filter, RawPacket* rawPacket);
142 
143  // implement abstract methods
144 
146 
153  bool setFilter(std::string filterAsString) override;
154 
157  bool clearFilter() override;
158  };
159 } // 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:104
bool setFilter(std::string filterAsString) override
bool clearFilter() override
virtual void getStatistics(PcapStats &stats) const =0
static bool matchPacketWithFilter(GeneralFilter &filter, RawPacket *rawPacket)
static std::string getPcapLibVersionInfo()
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: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.
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.
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:116
uint64_t packetsRecv
Number of packets received.
Definition: PcapDevice.h:118
uint64_t packetsDrop
Number of packets dropped.
Definition: PcapDevice.h:120
uint64_t packetsDropByInterface
number of packets dropped by interface (not supported on all platforms)
Definition: PcapDevice.h:122