PcapPlusPlus  20.08+
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(std::string dirPath);
240 
248  int clockGetTime(long& sec, long& nsec);
249 
255  void multiPlatformSleep(uint32_t seconds);
256 
262  uint16_t hostToNet16(uint16_t host);
263 
269  uint16_t netToHost16(uint16_t net);
270 
276  uint32_t hostToNet32(uint32_t host);
277 
283  uint32_t netToHost32(uint32_t net);
284 
290  class AppName
291  {
292  private:
293  static std::string m_AppName;
294 
295  public:
301  static void init(int argc, char* argv[])
302  {
303  if (argc == 0)
304  {
305  m_AppName.clear();
306  return;
307  }
308 
309  m_AppName = argv[0];
310 
311  // remove Linux/Unix path
312  size_t lastPos = m_AppName.rfind('/');
313  if (lastPos != std::string::npos)
314  {
315  m_AppName = m_AppName.substr(lastPos + 1);
316  }
317 
318  // remove Windows path
319  lastPos = m_AppName.rfind('\\');
320  if (lastPos != std::string::npos)
321  {
322  m_AppName = m_AppName.substr(lastPos + 1);
323  }
324 
325  // remove file extension
326  m_AppName = m_AppName.substr(0, m_AppName.rfind('.'));
327  }
328 
332  static const std::string& get() { return m_AppName; }
333  };
334 
341  {
342  public:
348  typedef void (*EventHandlerCallback)(void* cookie);
349 
355  {
356  static ApplicationEventHandler instance;
357  return instance;
358  }
359 
366  void onApplicationInterrupted(EventHandlerCallback handler, void* cookie);
367 
368  private:
369  EventHandlerCallback m_ApplicationInterruptedHandler;
370  void* m_ApplicationInterruptedCookie;
371 
372  // private c'tor
374 
375 #if defined(WIN32) || defined(WINx64) || defined(PCAPPP_MINGW_ENV)
376  static int handlerRoutine(unsigned long fdwCtrlType);
377 #else
378  static void handlerRoutine(int signum);
379 #endif
380  };
381 
382 } // namespace pcpp
383 
384 #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
std::string executeShellCommand(const std::string command)
Definition: SystemUtils.h:290
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:301
static const SystemCore Core17
Definition: SystemUtils.h:125
uint32_t Mask
Definition: SystemUtils.h:34
static ApplicationEventHandler & getInstance()
Definition: SystemUtils.h:354
uint8_t Id
Definition: SystemUtils.h:39
static const SystemCore Core16
Definition: SystemUtils.h:121
static const SystemCore Core5
Definition: SystemUtils.h:77
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
Definition: SystemUtils.h:340
static const SystemCore Core27
Definition: SystemUtils.h:165
bool directoryExists(std::string dirPath)
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)