PcapPlusPlus  21.05+
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:
302  static void init(int argc, char* argv[])
303  {
304  if (argc == 0)
305  {
306  m_AppName.clear();
307  return;
308  }
309 
310  m_AppName = argv[0];
311 
312  // remove Linux/Unix path
313  size_t lastPos = m_AppName.rfind('/');
314  if (lastPos != std::string::npos)
315  {
316  m_AppName = m_AppName.substr(lastPos + 1);
317  }
318 
319  // remove Windows path
320  lastPos = m_AppName.rfind('\\');
321  if (lastPos != std::string::npos)
322  {
323  m_AppName = m_AppName.substr(lastPos + 1);
324  }
325 
326  // remove file extension
327  m_AppName = m_AppName.substr(0, m_AppName.rfind('.'));
328  }
329 
333  static const std::string& get() { return m_AppName; }
334  };
335 
342  {
343  public:
349  typedef void (*EventHandlerCallback)(void* cookie);
350 
356  {
357  static ApplicationEventHandler instance;
358  return instance;
359  }
360 
367  void onApplicationInterrupted(EventHandlerCallback handler, void* cookie);
368 
369  private:
370  EventHandlerCallback m_ApplicationInterruptedHandler;
371  void* m_ApplicationInterruptedCookie;
372 
373  // private c'tor
375 
376 #if defined(WIN32) || defined(WINx64) || defined(PCAPPP_MINGW_ENV)
377  static int handlerRoutine(unsigned long fdwCtrlType);
378 #else
379  static void handlerRoutine(int signum);
380 #endif
381  };
382 
383 } // namespace pcpp
384 
385 #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:302
static const SystemCore Core17
Definition: SystemUtils.h:125
uint32_t Mask
Definition: SystemUtils.h:34
static ApplicationEventHandler & getInstance()
Definition: SystemUtils.h:355
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:341
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)