PcapPlusPlus  Next
SystemUtils.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "DeprecationUtils.h"
4 
5 #include <cstdint>
6 #include <string>
7 #include <vector>
8 #include <ctime>
9 
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 
24 namespace pcpp
25 {
26 
29  struct SystemCore
30  {
31 
36  uint32_t Mask;
37 
39  uint8_t Id;
40 
43  bool operator==(const SystemCore& other) const
44  {
45  return Id == other.Id;
46  }
47  };
48 
52  struct SystemCores
53  {
55  static const SystemCore Core0;
57  static const SystemCore Core1;
59  static const SystemCore Core2;
61  static const SystemCore Core3;
63  static const SystemCore Core4;
65  static const SystemCore Core5;
67  static const SystemCore Core6;
69  static const SystemCore Core7;
71  static const SystemCore Core8;
73  static const SystemCore Core9;
75  static const SystemCore Core10;
77  static const SystemCore Core11;
79  static const SystemCore Core12;
81  static const SystemCore Core13;
83  static const SystemCore Core14;
85  static const SystemCore Core15;
87  static const SystemCore Core16;
89  static const SystemCore Core17;
91  static const SystemCore Core18;
93  static const SystemCore Core19;
95  static const SystemCore Core20;
97  static const SystemCore Core21;
99  static const SystemCore Core22;
101  static const SystemCore Core23;
103  static const SystemCore Core24;
105  static const SystemCore Core25;
107  static const SystemCore Core26;
109  static const SystemCore Core27;
111  static const SystemCore Core28;
113  static const SystemCore Core29;
115  static const SystemCore Core30;
117  static const SystemCore Core31;
119  static const SystemCore IdToSystemCore[MAX_NUM_OF_CORES];
120  };
121 
122  using CoreMask = uint32_t;
123 
127 
131 
135  CoreMask createCoreMaskFromCoreVector(const std::vector<SystemCore>& cores);
136 
140  CoreMask createCoreMaskFromCoreIds(const std::vector<int>& coreIds);
141 
145  void createCoreVectorFromCoreMask(CoreMask coreMask, std::vector<SystemCore>& resultVec);
146 
151  std::string executeShellCommand(const std::string& command);
152 
156  bool directoryExists(const std::string& dirPath);
157 
163  int clockGetTime(long& sec, long& nsec);
164 
169  time_t mkUtcTime(std::tm& tm);
170 
176  PCPP_DEPRECATED("Please use std::this_thread::sleep_for(std::chrono::seconds(seconds)) instead")
177  void multiPlatformSleep(uint32_t seconds);
178 
184  PCPP_DEPRECATED("Please use std::this_thread::sleep_for(std::chrono::milliseconds(milliseconds)) instead")
185  void multiPlatformMSleep(uint32_t milliseconds);
186 
190  uint16_t hostToNet16(uint16_t host);
191 
195  uint16_t netToHost16(uint16_t net);
196 
200  uint32_t hostToNet32(uint32_t host);
201 
205  uint32_t netToHost32(uint32_t net);
206 
211  class AppName
212  {
213  private:
214  static std::string m_AppName;
215 
216  public:
220  // cppcheck-suppress constParameter
221  static void init(int argc, char* argv[])
222  {
223  if (argc == 0)
224  {
225  m_AppName.clear();
226  return;
227  }
228 
229  m_AppName = argv[0];
230 
231  // remove Linux/Unix path
232  size_t lastPos = m_AppName.rfind('/');
233  if (lastPos != std::string::npos)
234  {
235  m_AppName = m_AppName.substr(lastPos + 1);
236  }
237 
238  // remove Windows path
239  lastPos = m_AppName.rfind('\\');
240  if (lastPos != std::string::npos)
241  {
242  m_AppName = m_AppName.substr(lastPos + 1);
243  }
244 
245  // remove file extension
246  lastPos = m_AppName.rfind('.');
247  if (lastPos != std::string::npos)
248  {
249  m_AppName.resize(lastPos);
250  }
251  }
252 
254  static const std::string& get()
255  {
256  return m_AppName;
257  }
258  };
259 
264  {
265  public:
269  using EventHandlerCallback = void (*)(void*);
270 
274  {
275  static ApplicationEventHandler instance;
276  return instance;
277  }
278 
284  void onApplicationInterrupted(EventHandlerCallback handler, void* cookie);
285 
286  private:
287  EventHandlerCallback m_ApplicationInterruptedHandler;
288  void* m_ApplicationInterruptedCookie;
289 
290  // private c'tor
292 
293 #if defined(_WIN32)
294  static int handlerRoutine(unsigned long fdwCtrlType);
295 #else
296  static void handlerRoutine(int signum);
297 #endif
298  };
299 
300 } // namespace pcpp
Definition: SystemUtils.h:212
static void init(int argc, char *argv[])
Definition: SystemUtils.h:221
static const std::string & get()
Definition: SystemUtils.h:254
Definition: SystemUtils.h:264
static ApplicationEventHandler & getInstance()
Definition: SystemUtils.h:273
void(*)(void *) EventHandlerCallback
Definition: SystemUtils.h:269
void onApplicationInterrupted(EventHandlerCallback handler, void *cookie)
The main namespace for the PcapPlusPlus lib.
CoreMask createCoreMaskFromCoreIds(const std::vector< int > &coreIds)
void multiPlatformMSleep(uint32_t milliseconds)
std::string executeShellCommand(const std::string &command)
CoreMask createCoreMaskFromCoreVector(const std::vector< SystemCore > &cores)
CoreMask getCoreMaskForAllMachineCores()
void createCoreVectorFromCoreMask(CoreMask coreMask, std::vector< SystemCore > &resultVec)
uint32_t netToHost32(uint32_t net)
time_t mkUtcTime(std::tm &tm)
int getNumOfCores()
uint16_t hostToNet16(uint16_t host)
uint32_t hostToNet32(uint32_t host)
uint16_t netToHost16(uint16_t net)
int clockGetTime(long &sec, long &nsec)
void multiPlatformSleep(uint32_t seconds)
bool directoryExists(const std::string &dirPath)
Definition: SystemUtils.h:30
bool operator==(const SystemCore &other) const
Definition: SystemUtils.h:43
uint32_t Mask
Definition: SystemUtils.h:36
uint8_t Id
Core ID - a value between 0 and 31.
Definition: SystemUtils.h:39
Definition: SystemUtils.h:53
static const SystemCore Core9
Static representation of core #9.
Definition: SystemUtils.h:73
static const SystemCore Core16
Static representation of core #16.
Definition: SystemUtils.h:87
static const SystemCore Core15
Static representation of core #15.
Definition: SystemUtils.h:85
static const SystemCore Core29
Static representation of core #29.
Definition: SystemUtils.h:113
static const SystemCore Core26
Static representation of core #26.
Definition: SystemUtils.h:107
static const SystemCore Core13
Static representation of core #13.
Definition: SystemUtils.h:81
static const SystemCore Core22
Static representation of core #22.
Definition: SystemUtils.h:99
static const SystemCore Core10
Static representation of core #10.
Definition: SystemUtils.h:75
static const SystemCore Core8
Static representation of core #8.
Definition: SystemUtils.h:71
static const SystemCore Core1
Static representation of core #1.
Definition: SystemUtils.h:57
static const SystemCore Core23
Static representation of core #23.
Definition: SystemUtils.h:101
static const SystemCore Core17
Static representation of core #17.
Definition: SystemUtils.h:89
static const SystemCore Core14
Static representation of core #14.
Definition: SystemUtils.h:83
static const SystemCore Core20
Static representation of core #20.
Definition: SystemUtils.h:95
static const SystemCore Core3
Static representation of core #3.
Definition: SystemUtils.h:61
static const SystemCore Core27
Static representation of core #27.
Definition: SystemUtils.h:109
static const SystemCore Core30
Static representation of core #30.
Definition: SystemUtils.h:115
static const SystemCore Core18
Static representation of core #18.
Definition: SystemUtils.h:91
static const SystemCore Core0
Static representation of core #0.
Definition: SystemUtils.h:55
static const SystemCore Core12
Static representation of core #12.
Definition: SystemUtils.h:79
static const SystemCore Core24
Static representation of core #24.
Definition: SystemUtils.h:103
static const SystemCore Core25
Static representation of core #25.
Definition: SystemUtils.h:105
static const SystemCore Core11
Static representation of core #11.
Definition: SystemUtils.h:77
static const SystemCore Core28
Static representation of core #28.
Definition: SystemUtils.h:111
static const SystemCore Core7
Static representation of core #7.
Definition: SystemUtils.h:69
static const SystemCore Core19
Static representation of core #19.
Definition: SystemUtils.h:93
static const SystemCore Core4
Static representation of core #4.
Definition: SystemUtils.h:63
static const SystemCore Core31
Static representation of core #31.
Definition: SystemUtils.h:117
static const SystemCore Core2
Static representation of core #2.
Definition: SystemUtils.h:59
static const SystemCore Core5
Static representation of core #5.
Definition: SystemUtils.h:65
static const SystemCore Core6
Static representation of core #6.
Definition: SystemUtils.h:67
static const SystemCore Core21
Static representation of core #21.
Definition: SystemUtils.h:97
static const SystemCore IdToSystemCore[MAX_NUM_OF_CORES]
A static array for mapping core ID (integer) to the corresponding static SystemCore representation.
Definition: SystemUtils.h:119