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 
133 
137  CoreMask createCoreMaskFromCoreVector(const std::vector<SystemCore>& cores);
138 
142  CoreMask createCoreMaskFromCoreIds(const std::vector<int>& coreIds);
143 
147  void createCoreVectorFromCoreMask(CoreMask coreMask, std::vector<SystemCore>& resultVec);
148 
153  std::string executeShellCommand(const std::string& command);
154 
158  bool directoryExists(const std::string& dirPath);
159 
165  int clockGetTime(long& sec, long& nsec);
166 
171  time_t mkUtcTime(std::tm& time);
172 
178  PCPP_DEPRECATED("Please use std::this_thread::sleep_for(std::chrono::seconds(seconds)) instead")
179  void multiPlatformSleep(uint32_t seconds);
180 
186  PCPP_DEPRECATED("Please use std::this_thread::sleep_for(std::chrono::milliseconds(milliseconds)) instead")
187  void multiPlatformMSleep(uint32_t milliseconds);
188 
192  uint16_t hostToNet16(uint16_t host);
193 
197  uint16_t netToHost16(uint16_t net);
198 
202  uint32_t hostToNet32(uint32_t host);
203 
207  uint32_t netToHost32(uint32_t net);
208 
213  class AppName
214  {
215  private:
216  static std::string m_AppName;
217 
218  public:
222  // cppcheck-suppress constParameter
223  static void init(int argc, char* argv[])
224  {
225  if (argc == 0)
226  {
227  m_AppName.clear();
228  return;
229  }
230 
231  m_AppName = argv[0];
232 
233  // remove Linux/Unix path
234  size_t lastPos = m_AppName.rfind('/');
235  if (lastPos != std::string::npos)
236  {
237  m_AppName = m_AppName.substr(lastPos + 1);
238  }
239 
240  // remove Windows path
241  lastPos = m_AppName.rfind('\\');
242  if (lastPos != std::string::npos)
243  {
244  m_AppName = m_AppName.substr(lastPos + 1);
245  }
246 
247  // remove file extension
248  lastPos = m_AppName.rfind('.');
249  if (lastPos != std::string::npos)
250  {
251  m_AppName.resize(lastPos);
252  }
253  }
254 
256  static const std::string& get()
257  {
258  return m_AppName;
259  }
260  };
261 
266  {
267  public:
271  using EventHandlerCallback = void (*)(void*);
272 
276  {
277  static ApplicationEventHandler instance;
278  return instance;
279  }
280 
286  void onApplicationInterrupted(EventHandlerCallback handler, void* cookie);
287 
288  private:
289  EventHandlerCallback m_ApplicationInterruptedHandler;
290  void* m_ApplicationInterruptedCookie;
291 
292  // private c'tor
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
Definition: SystemUtils.h:214
static void init(int argc, char *argv[])
Definition: SystemUtils.h:223
static const std::string & get()
Definition: SystemUtils.h:256
Definition: SystemUtils.h:266
static ApplicationEventHandler & getInstance()
Definition: SystemUtils.h:275
void(*)(void *) EventHandlerCallback
Definition: SystemUtils.h:271
void onApplicationInterrupted(EventHandlerCallback handler, void *cookie)
The main namespace for the PcapPlusPlus lib.
Definition: AssertionUtils.h:19
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)
int getNumOfCores()
time_t mkUtcTime(std::tm &time)
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