PcapPlusPlus  23.09
SystemUtils.h
Go to the documentation of this file.
1 #ifndef PCAPPP_SYSTEM_UTILS
2 #define PCAPPP_SYSTEM_UTILS
3 
4 #include <stdint.h>
5 #include <string>
6 #include <vector>
7 
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 
20 namespace pcpp
21 {
22 
27  struct SystemCore
28  {
34  uint32_t Mask;
35 
39  uint8_t Id;
40 
45  bool operator==(const SystemCore& other) const { return Id == other.Id; }
46  };
47 
52  struct SystemCores
53  {
57  static const SystemCore Core0;
61  static const SystemCore Core1;
65  static const SystemCore Core2;
69  static const SystemCore Core3;
73  static const SystemCore Core4;
77  static const SystemCore Core5;
81  static const SystemCore Core6;
85  static const SystemCore Core7;
89  static const SystemCore Core8;
93  static const SystemCore Core9;
97  static const SystemCore Core10;
101  static const SystemCore Core11;
105  static const SystemCore Core12;
109  static const SystemCore Core13;
113  static const SystemCore Core14;
117  static const SystemCore Core15;
121  static const SystemCore Core16;
125  static const SystemCore Core17;
129  static const SystemCore Core18;
133  static const SystemCore Core19;
137  static const SystemCore Core20;
141  static const SystemCore Core21;
145  static const SystemCore Core22;
149  static const SystemCore Core23;
153  static const SystemCore Core24;
157  static const SystemCore Core25;
161  static const SystemCore Core26;
165  static const SystemCore Core27;
169  static const SystemCore Core28;
173  static const SystemCore Core29;
177  static const SystemCore Core30;
181  static const SystemCore Core31;
182 
186  static const SystemCore IdToSystemCore[MAX_NUM_OF_CORES];
187  };
188 
189  typedef uint32_t CoreMask;
190 
195  int getNumOfCores();
196 
202 
203 
209  CoreMask createCoreMaskFromCoreVector(std::vector<SystemCore> cores);
210 
211 
217  CoreMask createCoreMaskFromCoreIds(std::vector<int> coreIds);
218 
219 
225  void createCoreVectorFromCoreMask(CoreMask coreMask, std::vector<SystemCore>& resultVec);
226 
232  std::string executeShellCommand(const std::string &command);
233 
239  bool directoryExists(const std::string &dirPath);
240 
248  int clockGetTime(long& sec, long& nsec);
249 
255  void multiPlatformSleep(uint32_t seconds);
256 
262  void multiPlatformMSleep(uint32_t milliseconds);
263 
269  uint16_t hostToNet16(uint16_t host);
270 
276  uint16_t netToHost16(uint16_t net);
277 
283  uint32_t hostToNet32(uint32_t host);
284 
290  uint32_t netToHost32(uint32_t net);
291 
297  class AppName
298  {
299  private:
300  static std::string m_AppName;
301 
302  public:
308  // cppcheck-suppress constParameter
309  static void init(int argc, char* argv[])
310  {
311  if (argc == 0)
312  {
313  m_AppName.clear();
314  return;
315  }
316 
317  m_AppName = argv[0];
318 
319  // remove Linux/Unix path
320  size_t lastPos = m_AppName.rfind('/');
321  if (lastPos != std::string::npos)
322  {
323  m_AppName = m_AppName.substr(lastPos + 1);
324  }
325 
326  // remove Windows path
327  lastPos = m_AppName.rfind('\\');
328  if (lastPos != std::string::npos)
329  {
330  m_AppName = m_AppName.substr(lastPos + 1);
331  }
332 
333  // remove file extension
334  lastPos = m_AppName.rfind('.');
335  if (lastPos != std::string::npos) {
336  m_AppName.resize(lastPos);
337  }
338  }
339 
343  static const std::string& get() { return m_AppName; }
344  };
345 
352  {
353  public:
359  typedef void (*EventHandlerCallback)(void* cookie);
360 
366  {
367  static ApplicationEventHandler instance;
368  return instance;
369  }
370 
377  void onApplicationInterrupted(EventHandlerCallback handler, void* cookie);
378 
379  private:
380  EventHandlerCallback m_ApplicationInterruptedHandler;
381  void* m_ApplicationInterruptedCookie;
382 
383  // private c'tor
385 
386 #if defined(_WIN32)
387  static int handlerRoutine(unsigned long fdwCtrlType);
388 #else
389  static void handlerRoutine(int signum);
390 #endif
391  };
392 
393 } // namespace pcpp
394 
395 #endif /* PCAPPP_SYSTEM_UTILS */
The main namespace for the PcapPlusPlus lib.
static const SystemCore Core1
Definition: SystemUtils.h:61
int getNumOfCores()
static const SystemCore Core26
Definition: SystemUtils.h:161
static const SystemCore Core24
Definition: SystemUtils.h:153
static const SystemCore Core3
Definition: SystemUtils.h:69
static const SystemCore Core30
Definition: SystemUtils.h:177
Definition: SystemUtils.h:297
Definition: SystemUtils.h:27
static const SystemCore Core14
Definition: SystemUtils.h:113
static const SystemCore Core21
Definition: SystemUtils.h:141
uint32_t hostToNet32(uint32_t host)
int clockGetTime(long &sec, long &nsec)
bool operator==(const SystemCore &other) const
Definition: SystemUtils.h:45
static const SystemCore Core22
Definition: SystemUtils.h:145
void createCoreVectorFromCoreMask(CoreMask coreMask, std::vector< SystemCore > &resultVec)
uint16_t netToHost16(uint16_t net)
uint16_t hostToNet16(uint16_t host)
static const SystemCore Core9
Definition: SystemUtils.h:93
static const SystemCore Core18
Definition: SystemUtils.h:129
static void init(int argc, char *argv[])
Definition: SystemUtils.h:309
bool directoryExists(const std::string &dirPath)
static const SystemCore Core17
Definition: SystemUtils.h:125
uint32_t Mask
Definition: SystemUtils.h:34
static ApplicationEventHandler & getInstance()
Definition: SystemUtils.h:365
uint8_t Id
Definition: SystemUtils.h:39
static const SystemCore Core16
Definition: SystemUtils.h:121
static const SystemCore Core5
Definition: SystemUtils.h:77
void multiPlatformMSleep(uint32_t milliseconds)
uint32_t netToHost32(uint32_t net)
CoreMask createCoreMaskFromCoreVector(std::vector< SystemCore > cores)
static const SystemCore Core4
Definition: SystemUtils.h:73
static const SystemCore Core0
Definition: SystemUtils.h:57
static const SystemCore Core29
Definition: SystemUtils.h:173
Definition: SystemUtils.h:52
static const SystemCore Core7
Definition: SystemUtils.h:85
static const SystemCore Core23
Definition: SystemUtils.h:149
static const SystemCore Core8
Definition: SystemUtils.h:89
static const SystemCore Core25
Definition: SystemUtils.h:157
static const SystemCore Core6
Definition: SystemUtils.h:81
static const SystemCore Core31
Definition: SystemUtils.h:181
static const SystemCore Core20
Definition: SystemUtils.h:137
static const SystemCore Core28
Definition: SystemUtils.h:169
static const SystemCore Core15
Definition: SystemUtils.h:117
std::string executeShellCommand(const std::string &command)
Definition: SystemUtils.h:351
static const SystemCore Core27
Definition: SystemUtils.h:165
static const SystemCore Core2
Definition: SystemUtils.h:65
static const SystemCore Core19
Definition: SystemUtils.h:133
static const SystemCore Core13
Definition: SystemUtils.h:109
static const SystemCore Core11
Definition: SystemUtils.h:101
CoreMask getCoreMaskForAllMachineCores()
static const SystemCore Core10
Definition: SystemUtils.h:97
static const SystemCore Core12
Definition: SystemUtils.h:105
void multiPlatformSleep(uint32_t seconds)
CoreMask createCoreMaskFromCoreIds(std::vector< int > coreIds)