Coverage Report

Created: 2025-08-09 06:42

/src/PcapPlusPlus/Pcap++/src/PcapDevice.cpp
Line
Count
Source (jump to first uncovered line)
1
#include "PcapDevice.h"
2
#include "PcapFilter.h"
3
#include "Logger.h"
4
#include "pcap.h"
5
6
namespace pcpp
7
{
8
  namespace internal
9
  {
10
11
4.72k
    PcapHandle::PcapHandle(pcap_t* pcapDescriptor) noexcept : m_PcapDescriptor(pcapDescriptor)
12
4.72k
    {}
13
14
0
    PcapHandle::PcapHandle(PcapHandle&& other) noexcept : m_PcapDescriptor(other.m_PcapDescriptor)
15
0
    {
16
0
      other.m_PcapDescriptor = nullptr;
17
0
    }
18
19
    PcapHandle& PcapHandle::operator=(PcapHandle&& other) noexcept
20
3.95k
    {
21
3.95k
      if (this != &other)
22
3.95k
      {
23
3.95k
        reset(other.m_PcapDescriptor);
24
3.95k
        other.m_PcapDescriptor = nullptr;
25
3.95k
      }
26
3.95k
      return *this;
27
3.95k
    }
28
29
    PcapHandle& PcapHandle::operator=(std::nullptr_t) noexcept
30
3.95k
    {
31
3.95k
      reset();
32
3.95k
      return *this;
33
3.95k
    }
34
35
    PcapHandle::~PcapHandle()
36
13.4k
    {
37
13.4k
      reset();
38
13.4k
    }
39
40
    pcap_t* PcapHandle::release() noexcept
41
0
    {
42
0
      auto result = m_PcapDescriptor;
43
0
      m_PcapDescriptor = nullptr;
44
0
      return result;
45
0
    }
46
47
    void PcapHandle::reset(pcap_t* pcapDescriptor) noexcept
48
21.3k
    {
49
21.3k
      pcap_t* oldDescriptor = m_PcapDescriptor;
50
21.3k
      m_PcapDescriptor = pcapDescriptor;
51
21.3k
      if (oldDescriptor != nullptr)
52
4.00k
      {
53
4.00k
        pcap_close(oldDescriptor);
54
4.00k
      }
55
21.3k
    }
56
57
    char const* PcapHandle::getLastError() const noexcept
58
0
    {
59
0
      if (!isValid())
60
0
      {
61
0
        static char const* const noHandleError = "No pcap handle";
62
0
        return noHandleError;
63
0
      }
64
65
0
      return pcap_geterr(m_PcapDescriptor);
66
0
    }
67
68
    bool PcapHandle::setFilter(std::string const& filter)
69
0
    {
70
0
      if (!isValid())
71
0
      {
72
0
        PCPP_LOG_ERROR("Cannot set filter to invalid handle");
73
0
        return false;
74
0
      }
75
76
0
      bpf_program prog;
77
0
      PCPP_LOG_DEBUG("Compiling the filter '" << filter << "'");
78
0
      if (pcap_compile(m_PcapDescriptor, &prog, filter.c_str(), 1, 0) < 0)
79
0
      {
80
        // Print out appropriate text, followed by the error message
81
        // generated by the packet capture library.
82
0
        PCPP_LOG_ERROR("Error compiling filter. Error message is: " << getLastError());
83
0
        return false;
84
0
      }
85
86
0
      PCPP_LOG_DEBUG("Setting the compiled filter");
87
0
      if (pcap_setfilter(m_PcapDescriptor, &prog) < 0)
88
0
      {
89
        // Print out error. The format will be the prefix string,
90
        // created above, followed by the error message that the packet
91
        // capture library generates.
92
0
        PCPP_LOG_ERROR("Error setting a compiled filter. Error message is: " << getLastError());
93
0
        pcap_freecode(&prog);
94
0
        return false;
95
0
      }
96
97
0
      PCPP_LOG_DEBUG("Filter set successfully");
98
0
      pcap_freecode(&prog);
99
0
      return true;
100
0
    }
101
102
    bool PcapHandle::clearFilter()
103
0
    {
104
0
      return setFilter("");
105
0
    }
106
107
    bool PcapHandle::getStatistics(PcapStats& stats) const
108
0
    {
109
0
      if (!isValid())
110
0
      {
111
0
        PCPP_LOG_ERROR("Cannot get stats from invalid handle");
112
0
        return false;
113
0
      }
114
115
0
      pcap_stat pcapStats;
116
0
      if (pcap_stats(m_PcapDescriptor, &pcapStats) < 0)
117
0
      {
118
0
        PCPP_LOG_ERROR("Error getting stats. Error message is: " << getLastError());
119
0
        return false;
120
0
      }
121
122
0
      stats.packetsRecv = pcapStats.ps_recv;
123
0
      stats.packetsDrop = pcapStats.ps_drop;
124
0
      stats.packetsDropByInterface = pcapStats.ps_ifdrop;
125
0
      return true;
126
0
    }
127
  }  // namespace internal
128
129
  IPcapStatisticsProvider::PcapStats IPcapStatisticsProvider::getStatistics() const
130
0
  {
131
0
    PcapStats stats;
132
0
    getStatistics(stats);
133
0
    return stats;
134
0
  }
135
136
  bool IPcapDevice::setFilter(std::string filterAsString)
137
0
  {
138
0
    PCPP_LOG_DEBUG("Filter to be set: '" << filterAsString << "'");
139
0
    if (!m_DeviceOpened)
140
0
    {
141
0
      PCPP_LOG_ERROR("Device not Opened!! cannot set filter");
142
0
      return false;
143
0
    }
144
145
0
    return m_PcapDescriptor.setFilter(filterAsString);
146
0
  }
147
148
  bool IPcapDevice::clearFilter()
149
0
  {
150
0
    return m_PcapDescriptor.clearFilter();
151
0
  }
152
153
  bool IPcapDevice::matchPacketWithFilter(GeneralFilter& filter, RawPacket* rawPacket)
154
0
  {
155
0
    return filter.matchPacketWithFilter(rawPacket);
156
0
  }
157
158
  std::string IPcapDevice::getPcapLibVersionInfo()
159
0
  {
160
0
    return std::string(pcap_lib_version());
161
0
  }
162
163
}  // namespace pcpp