PcapPlusPlus  Next
SystemUtils.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <stdint.h>
4 #include <string>
5 #include <vector>
6 
8 
9 #define MAX_NUM_OF_CORES 32
10 
11 #ifdef _MSC_VER
12 int gettimeofday(struct timeval* tp, struct timezone* tzp);
13 #endif
14 
19 namespace pcpp
20 {
21 
26  struct SystemCore
27  {
34  uint32_t Mask;
35 
39  uint8_t Id;
40 
45  bool operator==(const SystemCore& other) const
46  {
47  return Id == other.Id;
48  }
49  };
50 
55  struct SystemCores
56  {
60  static const SystemCore Core0;
64  static const SystemCore Core1;
68  static const SystemCore Core2;
72  static const SystemCore Core3;
76  static const SystemCore Core4;
80  static const SystemCore Core5;
84  static const SystemCore Core6;
88  static const SystemCore Core7;
92  static const SystemCore Core8;
96  static const SystemCore Core9;
100  static const SystemCore Core10;
104  static const SystemCore Core11;
108  static const SystemCore Core12;
112  static const SystemCore Core13;
116  static const SystemCore Core14;
120  static const SystemCore Core15;
124  static const SystemCore Core16;
128  static const SystemCore Core17;
132  static const SystemCore Core18;
136  static const SystemCore Core19;
140  static const SystemCore Core20;
144  static const SystemCore Core21;
148  static const SystemCore Core22;
152  static const SystemCore Core23;
156  static const SystemCore Core24;
160  static const SystemCore Core25;
164  static const SystemCore Core26;
168  static const SystemCore Core27;
172  static const SystemCore Core28;
176  static const SystemCore Core29;
180  static const SystemCore Core30;
184  static const SystemCore Core31;
185 
189  static const SystemCore IdToSystemCore[MAX_NUM_OF_CORES];
190  };
191 
192  typedef uint32_t CoreMask;
193 
199 
205 
211  CoreMask createCoreMaskFromCoreVector(const std::vector<SystemCore>& cores);
212 
218  CoreMask createCoreMaskFromCoreIds(const std::vector<int>& coreIds);
219 
225  void createCoreVectorFromCoreMask(CoreMask coreMask, std::vector<SystemCore>& resultVec);
226 
233  std::string executeShellCommand(const std::string& command);
234 
240  bool directoryExists(const std::string& dirPath);
241 
249  int clockGetTime(long& sec, long& nsec);
250 
256  void multiPlatformSleep(uint32_t seconds);
257 
263  void multiPlatformMSleep(uint32_t milliseconds);
264 
270  uint16_t hostToNet16(uint16_t host);
271 
277  uint16_t netToHost16(uint16_t net);
278 
284  uint32_t hostToNet32(uint32_t host);
285 
291  uint32_t netToHost32(uint32_t net);
292 
299  class AppName
300  {
301  private:
302  static std::string m_AppName;
303 
304  public:
310  // cppcheck-suppress constParameter
311  static void init(int argc, char* argv[])
312  {
313  if (argc == 0)
314  {
315  m_AppName.clear();
316  return;
317  }
318 
319  m_AppName = argv[0];
320 
321  // remove Linux/Unix path
322  size_t lastPos = m_AppName.rfind('/');
323  if (lastPos != std::string::npos)
324  {
325  m_AppName = m_AppName.substr(lastPos + 1);
326  }
327 
328  // remove Windows path
329  lastPos = m_AppName.rfind('\\');
330  if (lastPos != std::string::npos)
331  {
332  m_AppName = m_AppName.substr(lastPos + 1);
333  }
334 
335  // remove file extension
336  lastPos = m_AppName.rfind('.');
337  if (lastPos != std::string::npos)
338  {
339  m_AppName.resize(lastPos);
340  }
341  }
342 
346  static const std::string& get()
347  {
348  return m_AppName;
349  }
350  };
351 
358  {
359  public:
365  typedef void (*EventHandlerCallback)(void* cookie);
366 
372  {
373  static ApplicationEventHandler instance;
374  return instance;
375  }
376 
384  void onApplicationInterrupted(EventHandlerCallback handler, void* cookie);
385 
386  private:
387  EventHandlerCallback m_ApplicationInterruptedHandler;
388  void* m_ApplicationInterruptedCookie;
389 
390  // private c'tor
392 
393 #if defined(_WIN32)
394  static int handlerRoutine(unsigned long fdwCtrlType);
395 #else
396  static void handlerRoutine(int signum);
397 #endif
398  };
399 
400 } // namespace pcpp
Definition: SystemUtils.h:300
static void init(int argc, char *argv[])
Definition: SystemUtils.h:311
static const std::string & get()
Definition: SystemUtils.h:346
Definition: SystemUtils.h:358
static ApplicationEventHandler & getInstance()
Definition: SystemUtils.h:371
void(* EventHandlerCallback)(void *cookie)
Definition: SystemUtils.h:365
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)
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:27
bool operator==(const SystemCore &other) const
Definition: SystemUtils.h:45
uint32_t Mask
Definition: SystemUtils.h:34
uint8_t Id
Definition: SystemUtils.h:39
Definition: SystemUtils.h:56
static const SystemCore Core9
Definition: SystemUtils.h:96
static const SystemCore Core16
Definition: SystemUtils.h:124
static const SystemCore Core15
Definition: SystemUtils.h:120
static const SystemCore Core29
Definition: SystemUtils.h:176
static const SystemCore Core26
Definition: SystemUtils.h:164
static const SystemCore Core13
Definition: SystemUtils.h:112
static const SystemCore Core22
Definition: SystemUtils.h:148
static const SystemCore Core10
Definition: SystemUtils.h:100
static const SystemCore Core8
Definition: SystemUtils.h:92
static const SystemCore Core1
Definition: SystemUtils.h:64
static const SystemCore Core23
Definition: SystemUtils.h:152
static const SystemCore Core17
Definition: SystemUtils.h:128
static const SystemCore Core14
Definition: SystemUtils.h:116
static const SystemCore Core20
Definition: SystemUtils.h:140
static const SystemCore Core3
Definition: SystemUtils.h:72
static const SystemCore Core27
Definition: SystemUtils.h:168
static const SystemCore Core30
Definition: SystemUtils.h:180
static const SystemCore Core18
Definition: SystemUtils.h:132
static const SystemCore Core0
Definition: SystemUtils.h:60
static const SystemCore Core12
Definition: SystemUtils.h:108
static const SystemCore Core24
Definition: SystemUtils.h:156
static const SystemCore Core25
Definition: SystemUtils.h:160
static const SystemCore Core11
Definition: SystemUtils.h:104
static const SystemCore Core28
Definition: SystemUtils.h:172
static const SystemCore Core7
Definition: SystemUtils.h:88
static const SystemCore Core19
Definition: SystemUtils.h:136
static const SystemCore Core4
Definition: SystemUtils.h:76
static const SystemCore IdToSystemCore[32]
Definition: SystemUtils.h:189
static const SystemCore Core31
Definition: SystemUtils.h:184
static const SystemCore Core2
Definition: SystemUtils.h:68
static const SystemCore Core5
Definition: SystemUtils.h:80
static const SystemCore Core6
Definition: SystemUtils.h:84
static const SystemCore Core21
Definition: SystemUtils.h:144