/src/PcapPlusPlus/Pcap++/header/PcapDevice.h
Line | Count | Source |
1 | | #ifndef PCAPPP_PCAP_DEVICE |
2 | | #define PCAPPP_PCAP_DEVICE |
3 | | |
4 | | #include "Device.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 | | |
11 | | /// @file |
12 | | |
13 | | /** |
14 | | * \namespace pcpp |
15 | | * \brief The main namespace for the PcapPlusPlus lib |
16 | | */ |
17 | | namespace pcpp |
18 | | { |
19 | | //Forward Declaration - required for IPcapDevice::matchPacketWithFilter |
20 | | class GeneralFilter; |
21 | | |
22 | | /** |
23 | | * @class IPcapDevice |
24 | | * An abstract class representing all libpcap-based packet capturing devices: files, libPcap, WinPcap/Npcap and RemoteCapture. |
25 | | * This class is abstract and cannot be instantiated |
26 | | */ |
27 | | class IPcapDevice : public IDevice, public IFilterableDevice |
28 | | { |
29 | | protected: |
30 | | pcap_t* m_PcapDescriptor; |
31 | | |
32 | | // c'tor should not be public |
33 | 17.6k | IPcapDevice() : IDevice() { m_PcapDescriptor = NULL; } |
34 | | |
35 | | public: |
36 | | |
37 | | /** |
38 | | * @struct PcapStats |
39 | | * A container for pcap device statistics |
40 | | */ |
41 | | struct PcapStats |
42 | | { |
43 | | /** Number of packets received */ |
44 | | uint64_t packetsRecv; |
45 | | /** Number of packets dropped */ |
46 | | uint64_t packetsDrop; |
47 | | /** number of packets dropped by interface (not supported on all platforms) */ |
48 | | uint64_t packetsDropByInterface; |
49 | | }; |
50 | | |
51 | | |
52 | | virtual ~IPcapDevice(); |
53 | | |
54 | | /** |
55 | | * Get statistics from the device |
56 | | * @param[out] stats An object containing the stats |
57 | | * @return No return value |
58 | | */ |
59 | | virtual void getStatistics(PcapStats& stats) const = 0; |
60 | | |
61 | | /** |
62 | | * A static method for retrieving pcap lib (libpcap/WinPcap/etc.) version information. This method is actually |
63 | | * a wrapper for [pcap_lib_version()](https://www.tcpdump.org/manpages/pcap_lib_version.3pcap.html) |
64 | | * @return A string containing the pcap lib version information |
65 | | */ |
66 | | static std::string getPcapLibVersionInfo(); |
67 | | |
68 | | /** |
69 | | * Match a raw packet with a given BPF filter. Notice this method is static which means you don't need any device instance |
70 | | * in order to perform this match |
71 | | * @param[in] filter A filter class to test against |
72 | | * @param[in] rawPacket A pointer to the raw packet to match the filter with |
73 | | * @return True if raw packet matches the filter or false otherwise |
74 | | */ |
75 | | static bool matchPacketWithFilter(GeneralFilter& filter, RawPacket* rawPacket); |
76 | | |
77 | | |
78 | | // implement abstract methods |
79 | | |
80 | | using IFilterableDevice::setFilter; |
81 | | |
82 | | /** |
83 | | * Set a filter for the device. When implemented by the device, only packets that match the filter will be received. |
84 | | * Please note that when the device is closed the filter is reset so when reopening the device you need to call this |
85 | | * method again in order to reactivate the filter |
86 | | * @param[in] filterAsString The filter to be set in Berkeley Packet Filter (BPF) syntax (http://biot.com/capstats/bpf.html) |
87 | | * @return True if filter set successfully, false otherwise |
88 | | */ |
89 | | virtual bool setFilter(std::string filterAsString); |
90 | | |
91 | | /** |
92 | | * Clear the filter currently set on device |
93 | | * @return True if filter was removed successfully or if no filter was set, false otherwise |
94 | | */ |
95 | | bool clearFilter(); |
96 | | }; |
97 | | |
98 | | } // namespace pcpp |
99 | | |
100 | | #endif // PCAPPP_PCAP_DEVICE |