/src/PcapPlusPlus/Pcap++/header/PcapDevice.h
Line | Count | Source (jump to first uncovered line) |
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 | | |
10 | | /// @file |
11 | | |
12 | | /// @namespace pcpp |
13 | | /// @brief The main namespace for the PcapPlusPlus lib |
14 | | namespace pcpp |
15 | | { |
16 | | // Forward Declaration - required for IPcapDevice::matchPacketWithFilter |
17 | | class GeneralFilter; |
18 | | |
19 | | namespace internal |
20 | | { |
21 | | /// @struct PcapStats |
22 | | /// A container for pcap device statistics |
23 | | struct PcapStats |
24 | | { |
25 | | /// Number of packets received |
26 | | uint64_t packetsRecv; |
27 | | /// Number of packets dropped |
28 | | uint64_t packetsDrop; |
29 | | /// number of packets dropped by interface (not supported on all platforms) |
30 | | uint64_t packetsDropByInterface; |
31 | | }; |
32 | | |
33 | | /// @class PcapHandle |
34 | | /// @brief A wrapper class for pcap_t* which is the libpcap packet capture descriptor. |
35 | | /// This class is used to manage the lifecycle of the pcap_t* object |
36 | | class PcapHandle |
37 | | { |
38 | | public: |
39 | | /// @brief Creates an empty handle. |
40 | 8.59k | constexpr PcapHandle() noexcept = default; |
41 | | /// @brief Creates a handle from the provided pcap descriptor. |
42 | | /// @param pcapDescriptor The pcap descriptor to wrap. |
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 | | |
54 | | /// @return True if the handle is not null, false otherwise. |
55 | | bool isValid() const noexcept |
56 | 48.0k | { |
57 | 48.0k | return m_PcapDescriptor != nullptr; |
58 | 48.0k | } |
59 | | |
60 | | /// @return The underlying pcap descriptor. |
61 | | pcap_t* get() const noexcept |
62 | 38.0k | { |
63 | 38.0k | return m_PcapDescriptor; |
64 | 38.0k | } |
65 | | |
66 | | /// @brief Releases ownership of the handle and returns the pcap descriptor. |
67 | | /// @return The pcap descriptor or nullptr if no handle is owned. |
68 | | pcap_t* release() noexcept; |
69 | | |
70 | | /// @brief Replaces the managed handle with the provided one. |
71 | | /// @param pcapDescriptor A new pcap descriptor to manage. |
72 | | /// @remarks If the handle contains a non-null descriptor it will be closed. |
73 | | void reset(pcap_t* pcapDescriptor = nullptr) noexcept; |
74 | | |
75 | | /// @brief Helper function to retrieve a view of the last error string for this handle. |
76 | | /// @return A null-terminated view of the last error string. |
77 | | /// @remarks The returned view is only valid until the next call to a pcap function. |
78 | | char const* getLastError() const noexcept; |
79 | | |
80 | | /// @brief Sets a filter on the handle. Only packets that match the filter will be captured by the handle. |
81 | | /// |
82 | | /// The filter uses Berkeley Packet Filter (BPF) syntax (http://biot.com/capstats/bpf.html). |
83 | | /// |
84 | | /// @param[in] filter The filter to set in Berkeley Packet Filter (BPF) syntax. |
85 | | /// @return True if the filter was set successfully, false otherwise. |
86 | | bool setFilter(std::string const& filter); |
87 | | |
88 | | /// @brief Clears the filter currently set on the handle. |
89 | | /// @return True if the filter was removed successfully or if no filter was set, false otherwise. |
90 | | bool clearFilter(); |
91 | | |
92 | | /// @brief Retrieves statistics from the pcap handle. |
93 | | /// |
94 | | /// The function internally calls pcap_stats() to retrieve the statistics and only works on live devices. |
95 | | /// |
96 | | /// @param stats Structure to store the statistics. |
97 | | /// @return True if the statistics were retrieved successfully, false otherwise. |
98 | | bool getStatistics(PcapStats& stats) const; |
99 | | |
100 | | /// @return True if the handle is not null, false otherwise. |
101 | | explicit operator bool() const noexcept |
102 | 0 | { |
103 | 0 | return isValid(); |
104 | 0 | } |
105 | | |
106 | | bool operator==(std::nullptr_t) const noexcept |
107 | 34.7k | { |
108 | 34.7k | return !isValid(); |
109 | 34.7k | } |
110 | | bool operator!=(std::nullptr_t) const noexcept |
111 | 13.2k | { |
112 | 13.2k | return isValid(); |
113 | 13.2k | } |
114 | | |
115 | | private: |
116 | | pcap_t* m_PcapDescriptor = nullptr; |
117 | | }; |
118 | | } // namespace internal |
119 | | |
120 | | /// @brief An interface for providing Pcap-based device statistics |
121 | | class IPcapStatisticsProvider |
122 | | { |
123 | | public: |
124 | 8.59k | virtual ~IPcapStatisticsProvider() = default; |
125 | | |
126 | | using PcapStats = internal::PcapStats; |
127 | | |
128 | | /// @brief Get statistics from the device |
129 | | /// @return An object containing the stats |
130 | | PcapStats getStatistics() const; |
131 | | |
132 | | /// Get statistics from the device |
133 | | /// @param[out] stats An object containing the stats |
134 | | virtual void getStatistics(PcapStats& stats) const = 0; |
135 | | }; |
136 | | |
137 | | /// @class IPcapDevice |
138 | | /// An abstract class representing all libpcap-based packet capturing devices: files, libPcap, WinPcap/Npcap and |
139 | | /// RemoteCapture. This class is abstract and cannot be instantiated |
140 | | class IPcapDevice : public IDevice, public IFilterableDevice, public IPcapStatisticsProvider |
141 | | { |
142 | | protected: |
143 | | internal::PcapHandle m_PcapDescriptor; |
144 | | |
145 | | // c'tor should not be public |
146 | 8.59k | IPcapDevice() : IDevice() |
147 | 8.59k | {} |
148 | | |
149 | | public: |
150 | 8.59k | virtual ~IPcapDevice() = default; |
151 | | |
152 | | /// A static method for retrieving pcap lib (libpcap/WinPcap/etc.) version information. This method is actually |
153 | | /// a wrapper for [pcap_lib_version()](https://www.tcpdump.org/manpages/pcap_lib_version.3pcap.html) |
154 | | /// @return A string containing the pcap lib version information |
155 | | static std::string getPcapLibVersionInfo(); |
156 | | |
157 | | /// Match a raw packet with a given BPF filter. Notice this method is static which means you don't need any |
158 | | /// device instance in order to perform this match |
159 | | /// @param[in] filter A filter class to test against |
160 | | /// @param[in] rawPacket A pointer to the raw packet to match the filter with |
161 | | /// @return True if raw packet matches the filter or false otherwise |
162 | | static bool matchPacketWithFilter(GeneralFilter& filter, RawPacket* rawPacket); |
163 | | |
164 | | // implement abstract methods |
165 | | |
166 | | using IFilterableDevice::setFilter; |
167 | | |
168 | | /// Set a filter for the device. When implemented by the device, only packets that match the filter will be |
169 | | /// received. Please note that when the device is closed the filter is reset so when reopening the device you |
170 | | /// need to call this method again in order to reactivate the filter |
171 | | /// @param[in] filterAsString The filter to be set in Berkeley Packet Filter (BPF) syntax |
172 | | /// (http://biot.com/capstats/bpf.html) |
173 | | /// @return True if filter set successfully, false otherwise |
174 | | bool setFilter(std::string filterAsString) override; |
175 | | |
176 | | /// Clear the filter currently set on device |
177 | | /// @return True if filter was removed successfully or if no filter was set, false otherwise |
178 | | bool clearFilter() override; |
179 | | }; |
180 | | } // namespace pcpp |