Coverage Report

Created: 2023-01-17 06:15

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