Coverage Report

Created: 2024-02-25 06:29

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