Coverage Report

Created: 2026-08-14 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/PcapPlusPlus/Common++/header/SystemUtils.h
Line
Count
Source
1
#pragma once
2
3
#include "DeprecationUtils.h"
4
5
#include <cstdint>
6
#include <string>
7
#include <vector>
8
#include <ctime>
9
10
/// @file
11
12
// @todo Change to constexpr when C++17 is minimum supported version
13
enum : uint8_t
14
{
15
  MAX_NUM_OF_CORES = 32
16
};
17
18
#ifdef _MSC_VER
19
int gettimeofday(struct timeval* tp, struct timezone* tzp);
20
#endif
21
22
/// @namespace pcpp
23
/// @brief The main namespace for the PcapPlusPlus lib
24
namespace pcpp
25
{
26
27
  /// @struct SystemCore
28
  /// Represents data of 1 CPU core. Current implementation supports up to 32 cores
29
  struct SystemCore
30
  {
31
32
    /// Core position in a 32-bit mask. For each core this attribute holds a 4B integer where only 1 bit is set,
33
    /// according to the core ID. For example:
34
    /// - In core #0 the right-most bit will be set (meaning the number 0x01);
35
    /// - in core #5 the 5th right-most bit will be set (meaning the number 0x20)
36
    uint32_t Mask;
37
38
    /// Core ID - a value between 0 and 31
39
    uint8_t Id;
40
41
    /// Overload of the comparison operator
42
    /// @return true if 2 addresses are equal. False otherwise
43
    bool operator==(const SystemCore& other) const
44
0
    {
45
0
      return Id == other.Id;
46
0
    }
47
  };
48
49
  /// @struct SystemCores
50
  /// Contains static representation to all 32 cores and a static array to map core ID (integer) to a SystemCore
51
  /// struct
52
  struct SystemCores
53
  {
54
    /// Static representation of core #0
55
    static const SystemCore Core0;
56
    /// Static representation of core #1
57
    static const SystemCore Core1;
58
    /// Static representation of core #2
59
    static const SystemCore Core2;
60
    /// Static representation of core #3
61
    static const SystemCore Core3;
62
    /// Static representation of core #4
63
    static const SystemCore Core4;
64
    /// Static representation of core #5
65
    static const SystemCore Core5;
66
    /// Static representation of core #6
67
    static const SystemCore Core6;
68
    /// Static representation of core #7
69
    static const SystemCore Core7;
70
    /// Static representation of core #8
71
    static const SystemCore Core8;
72
    /// Static representation of core #9
73
    static const SystemCore Core9;
74
    /// Static representation of core #10
75
    static const SystemCore Core10;
76
    /// Static representation of core #11
77
    static const SystemCore Core11;
78
    /// Static representation of core #12
79
    static const SystemCore Core12;
80
    /// Static representation of core #13
81
    static const SystemCore Core13;
82
    /// Static representation of core #14
83
    static const SystemCore Core14;
84
    /// Static representation of core #15
85
    static const SystemCore Core15;
86
    /// Static representation of core #16
87
    static const SystemCore Core16;
88
    /// Static representation of core #17
89
    static const SystemCore Core17;
90
    /// Static representation of core #18
91
    static const SystemCore Core18;
92
    /// Static representation of core #19
93
    static const SystemCore Core19;
94
    /// Static representation of core #20
95
    static const SystemCore Core20;
96
    /// Static representation of core #21
97
    static const SystemCore Core21;
98
    /// Static representation of core #22
99
    static const SystemCore Core22;
100
    /// Static representation of core #23
101
    static const SystemCore Core23;
102
    /// Static representation of core #24
103
    static const SystemCore Core24;
104
    /// Static representation of core #25
105
    static const SystemCore Core25;
106
    /// Static representation of core #26
107
    static const SystemCore Core26;
108
    /// Static representation of core #27
109
    static const SystemCore Core27;
110
    /// Static representation of core #28
111
    static const SystemCore Core28;
112
    /// Static representation of core #29
113
    static const SystemCore Core29;
114
    /// Static representation of core #30
115
    static const SystemCore Core30;
116
    /// Static representation of core #31
117
    static const SystemCore Core31;
118
    /// A static array for mapping core ID (integer) to the corresponding static SystemCore representation
119
    static const SystemCore IdToSystemCore[MAX_NUM_OF_CORES];
120
  };
121
122
  using CoreMask = uint32_t;
123
124
  /// Get total number of cores on device
125
  /// @return Total number of CPU cores on device
126
  int getNumOfCores();
127
128
  /// Create a core mask for all cores available on machine. Since CoreMask is a 32-bit
129
  /// value, on machines with more than MAX_NUM_OF_CORES (32) cores only the first 32
130
  /// cores are represented in the returned mask
131
  /// @return A core mask for all cores available on machine, capped at MAX_NUM_OF_CORES
132
  CoreMask getCoreMaskForAllMachineCores();
133
134
  /// Create a core mask from a vector of system cores
135
  /// @param[in] cores A vector of SystemCore instances
136
  /// @return A core mask representing these cores
137
  CoreMask createCoreMaskFromCoreVector(const std::vector<SystemCore>& cores);
138
139
  /// Create a core mask from a vector of core IDs
140
  /// @param[in] coreIds A vector of core IDs
141
  /// @return A core mask representing these cores
142
  CoreMask createCoreMaskFromCoreIds(const std::vector<int>& coreIds);
143
144
  /// Convert a core mask into a vector of its appropriate system cores
145
  /// @param[in] coreMask The input core mask
146
  /// @param[out] resultVec The vector that will contain the system cores
147
  void createCoreVectorFromCoreMask(CoreMask coreMask, std::vector<SystemCore>& resultVec);
148
149
  /// Execute a shell command and return its output
150
  /// @param[in] command The command to run
151
  /// @return The output of the command (both stdout and stderr)
152
  /// @throws std::runtime_error Error executing the command.
153
  std::string executeShellCommand(const std::string& command);
154
155
  /// Check if a directory exists
156
  /// @param[in] dirPath Full path of the directory to search
157
  /// @return True if directory exists, false otherwise
158
  bool directoryExists(const std::string& dirPath);
159
160
  /// Retrieve a system-wide real-time accurate clock. It's actually a multi-platform version of clock_gettime() which
161
  /// is fully supported only on Linux
162
  /// @param[out] sec The second portion of the time
163
  /// @param[out] nsec The nanosecond portion of the time
164
  /// @return 0 for success, or -1 for failure
165
  int clockGetTime(long& sec, long& nsec);
166
167
  /// Convert std::tm to time_t in UTC time, ignoring local timezone
168
  /// @param[in] time The time to convert
169
  /// @return A time_t object representing the input time
170
  /// @throws std::runtime_error if a conversion error occurs
171
  time_t mkUtcTime(std::tm& time);
172
173
  /// A multi-platform version of the popular sleep method. This method simply runs the right sleep method, according
174
  /// to the platform it is running on.
175
  /// @param[in] seconds Number of seconds to sleep
176
  /// @deprecated Please use std::this_thread::sleep_for(). It is a standard C++ (since C++11) method which is already
177
  /// cross-platform
178
  PCPP_DEPRECATED("Please use std::this_thread::sleep_for(std::chrono::seconds(seconds)) instead")
179
  void multiPlatformSleep(uint32_t seconds);
180
181
  /// A multi-platform version of sleep in milliseconds resolution. This method simply runs the right sleep method,
182
  /// according to the platform it is running on.
183
  /// @param[in] milliseconds Number of milliseconds to sleep
184
  /// @deprecated Please use std::this_thread::sleep_for(). It is a standard C++ (since C++11) method which is already
185
  /// cross-platform
186
  PCPP_DEPRECATED("Please use std::this_thread::sleep_for(std::chrono::milliseconds(milliseconds)) instead")
187
  void multiPlatformMSleep(uint32_t milliseconds);
188
189
  /// A multi-platform version of `htons` which convert host to network byte order
190
  /// @param[in] host Value in host byte order
191
  /// @return Value in network byte order
192
  uint16_t hostToNet16(uint16_t host);
193
194
  /// A multi-platform version of `ntohs` which convert network to host byte order
195
  /// @param[in] net Value in network byte order
196
  /// @return Value in host byte order
197
  uint16_t netToHost16(uint16_t net);
198
199
  /// A multi-platform version of `htonl` which convert host to network byte order
200
  /// @param[in] host Value in host byte order
201
  /// @return Value in network byte order
202
  uint32_t hostToNet32(uint32_t host);
203
204
  /// A multi-platform version of `ntohl` which convert network to host byte order
205
  /// @param[in] net Value in network byte order
206
  /// @return Value in host byte order
207
  uint32_t netToHost32(uint32_t net);
208
209
  /// @class AppName
210
  /// This class extracts the application name from the current running executable and stores it for usage of the
211
  /// application throughout its runtime. This class should be initialized once in the beginning of the main() method
212
  /// using AppName#init() and from then on the app name could be retrieved using AppName#get()
213
  class AppName
214
  {
215
  private:
216
    static std::string m_AppName;
217
218
  public:
219
    /// Static init method which should be called once at the beginning of the main method.
220
    /// @param[in] argc The argc param from main()
221
    /// @param[in] argv The argv param from main()
222
    // cppcheck-suppress constParameter
223
    static void init(int argc, char* argv[])
224
0
    {
225
0
      if (argc == 0)
226
0
      {
227
0
        m_AppName.clear();
228
0
        return;
229
0
      }
230
0
231
0
      m_AppName = argv[0];
232
0
233
0
      // remove Linux/Unix path
234
0
      size_t lastPos = m_AppName.rfind('/');
235
0
      if (lastPos != std::string::npos)
236
0
      {
237
0
        m_AppName = m_AppName.substr(lastPos + 1);
238
0
      }
239
0
240
0
      // remove Windows path
241
0
      lastPos = m_AppName.rfind('\\');
242
0
      if (lastPos != std::string::npos)
243
0
      {
244
0
        m_AppName = m_AppName.substr(lastPos + 1);
245
0
      }
246
0
247
0
      // remove file extension
248
0
      lastPos = m_AppName.rfind('.');
249
0
      if (lastPos != std::string::npos)
250
0
      {
251
0
        m_AppName.resize(lastPos);
252
0
      }
253
0
    }
254
255
    /// @return The app name as extracted from the current running executable
256
    static const std::string& get()
257
0
    {
258
0
      return m_AppName;
259
0
    }
260
  };
261
262
  /// @class ApplicationEventHandler
263
  /// A singleton class that provides callbacks for events that occur during application life-cycle such as ctrl+c
264
  /// pressed, application closed, killed, etc.
265
  class ApplicationEventHandler
266
  {
267
  public:
268
    /// @typedef EventHandlerCallback
269
    /// The callback to be invoked when the event occurs
270
    /// @param[in] cookie A pointer the the cookie provided by the user in ApplicationEventHandler c'tor
271
    using EventHandlerCallback = void (*)(void*);
272
273
    /// As ApplicationEventHandler is a singleton, this is the static getter to retrieve its instance
274
    /// @return The singleton instance of ApplicationEventHandler
275
    static ApplicationEventHandler& getInstance()
276
0
    {
277
0
      static ApplicationEventHandler instance;
278
0
      return instance;
279
0
    }
280
281
    /// Register for an application-interrupted event, meaning ctrl+c was pressed
282
    /// @param[in] handler The callback to be activated when the event occurs
283
    /// @param[in] cookie A pointer to a user provided object. This object will be transferred to the
284
    /// EventHandlerCallback callback. This cookie is very useful for transferring objects that give context to the
285
    /// event callback
286
    void onApplicationInterrupted(EventHandlerCallback handler, void* cookie);
287
288
  private:
289
    EventHandlerCallback m_ApplicationInterruptedHandler;
290
    void* m_ApplicationInterruptedCookie;
291
292
    // private c'tor
293
    ApplicationEventHandler();
294
295
#if defined(_WIN32)
296
    static int handlerRoutine(unsigned long fdwCtrlType);
297
#else
298
    static void handlerRoutine(int signum);
299
#endif
300
  };
301
302
}  // namespace pcpp