Coverage Report

Created: 2025-07-11 07:47

/src/PcapPlusPlus/Pcap++/header/Device.h
Line
Count
Source (jump to first uncovered line)
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
  typedef PointerVector<RawPacket> RawPacketVector;
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
    bool m_DeviceOpened;
23
24
    // c'tor should not be public
25
11.5k
    IDevice() : m_DeviceOpened(false)
26
11.5k
    {}
27
28
  public:
29
    virtual ~IDevice()
30
11.5k
    {}
31
32
    /// Open the device
33
    /// @return True if device was opened successfully, false otherwise
34
    virtual bool open() = 0;
35
36
    /// Close the device
37
    virtual void close() = 0;
38
39
    /// @return True if the file is opened, false otherwise
40
    inline bool isOpened()
41
787
    {
42
787
      return m_DeviceOpened;
43
787
    }
44
  };
45
46
  /// @class IFilterableDevice
47
  /// An abstract interface representing all devices that have BPF (Berkeley Packet Filter) filtering capabilities,
48
  /// meaning devices that can filter packets based on the BPF filtering syntax.
49
  /// This is an abstract class that cannot be instantiated
50
  class IFilterableDevice
51
  {
52
  protected:
53
    // c'tor should not be public
54
11.5k
    IFilterableDevice() = default;
55
56
  public:
57
11.5k
    virtual ~IFilterableDevice() = default;
58
59
    /// Set a filter for the device. When implemented by the device, only packets that match the filter will be
60
    /// received
61
    /// @param[in] filter The filter to be set in PcapPlusPlus' GeneralFilter format
62
    /// @return True if filter set successfully, false otherwise
63
    virtual bool setFilter(GeneralFilter& filter)
64
0
    {
65
0
      std::string filterAsString;
66
0
      filter.parseToString(filterAsString);
67
0
      return setFilter(filterAsString);
68
0
    }
69
70
    /// Set a filter for the device. When implemented by the device, only packets that match the filter will be
71
    /// received
72
    /// @param[in] filterAsString The filter to be set in Berkeley Packet Filter (BPF) syntax
73
    /// (http://biot.com/capstats/bpf.html)
74
    /// @return True if filter set successfully, false otherwise
75
    virtual bool setFilter(std::string filterAsString) = 0;
76
77
    /// Clear the filter currently set on the device
78
    /// @return True if filter was removed successfully or if no filter was set, false otherwise
79
    virtual bool clearFilter() = 0;
80
  };
81
}  // namespace pcpp