Coverage Report

Created: 2025-11-24 07:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/PcapPlusPlus/Pcap++/header/Device.h
Line
Count
Source
1
#pragma once
2
3
/// @file
4
5
#include "PointerVector.h"
6
#include "RawPacket.h"
7
#include "PcapFilter.h"
8
9
/// @namespace pcpp
10
/// @brief The main namespace for the PcapPlusPlus lib
11
namespace pcpp
12
{
13
  /// A vector of pointers to RawPacket
14
  using RawPacketVector = PointerVector<RawPacket>;
15
16
  /// @class IDevice
17
  /// An abstract interface representing all packet processing devices. It stands as the root class for all devices.
18
  /// This is an abstract class that cannot be instantiated
19
  class IDevice
20
  {
21
  protected:
22
    // c'tor should not be public
23
11.4k
    IDevice() = default;
24
25
  public:
26
11.4k
    virtual ~IDevice() = default;
27
28
    /// Open the device
29
    /// @return True if device was opened successfully, false otherwise
30
    virtual bool open() = 0;
31
32
    /// Close the device
33
    virtual void close() = 0;
34
35
    /// @return True if the file is opened, false otherwise
36
    virtual bool isOpened() const = 0;
37
  };
38
39
  /// @class IFilterableDevice
40
  /// An abstract interface representing all devices that have BPF (Berkeley Packet Filter) filtering capabilities,
41
  /// meaning devices that can filter packets based on the BPF filtering syntax.
42
  /// This is an abstract class that cannot be instantiated
43
  class IFilterableDevice : public IDevice
44
  {
45
  protected:
46
    // c'tor should not be public
47
11.4k
    IFilterableDevice() = default;
48
49
  public:
50
    virtual ~IFilterableDevice() = default;
51
52
    /// Set a filter for the device. When implemented by the device, only packets that match the filter will be
53
    /// received
54
    /// @param[in] filter The filter to be set in PcapPlusPlus' GeneralFilter format
55
    /// @return True if filter set successfully, false otherwise
56
    bool setFilter(GeneralFilter& filter)
57
0
    {
58
0
      std::string filterAsString;
59
0
      filter.parseToString(filterAsString);
60
0
      return doUpdateFilter(&filterAsString);
61
0
    }
62
63
    /// Set a filter for the device. When implemented by the device, only packets that match the filter will be
64
    /// processed.
65
    /// @param[in] filterAsString The filter to be set in Berkeley Packet Filter (BPF) syntax
66
    /// (http://biot.com/capstats/bpf.html)
67
    /// @return True if filter set successfully, false otherwise
68
    bool setFilter(std::string const& filterAsString)
69
0
    {
70
0
      return doUpdateFilter(&filterAsString);
71
0
    }
72
73
    /// Clear the filter currently set on the device
74
    /// @return True if filter was removed successfully or if no filter was set, false otherwise
75
    bool clearFilter()
76
0
    {
77
0
      return doUpdateFilter(nullptr);
78
0
    }
79
80
  protected:
81
    /// @brief Updates the filter on the device with a BPF string.
82
    ///
83
    /// Only packets that match the filter should be processed by the device after this method is called.
84
    /// A nullptr should disable any existing filter on the device.
85
    ///
86
    /// @param filterAsString A pointer to a string representing the filter in BPF syntax
87
    /// (http://biot.com/capstats/bpf.html).
88
    /// @return True if the operation was successful, false otherwise.
89
    virtual bool doUpdateFilter(std::string const* filterAsString) = 0;
90
  };
91
}  // namespace pcpp