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 
17 namespace pcpp
18 {
19 
22  struct SystemCore
23  {
24 
29  uint32_t Mask;
30 
32  uint8_t Id;
33 
36  bool operator==(const SystemCore& other) const
37  {
38  return Id == other.Id;
39  }
40  };
41 
45  struct SystemCores
46  {
48  static const SystemCore Core0;
50  static const SystemCore Core1;
52  static const SystemCore Core2;
54  static const SystemCore Core3;
56  static const SystemCore Core4;
58  static const SystemCore Core5;
60  static const SystemCore Core6;
62  static const SystemCore Core7;
64  static const SystemCore Core8;
66  static const SystemCore Core9;
68  static const SystemCore Core10;
70  static const SystemCore Core11;
72  static const SystemCore Core12;
74  static const SystemCore Core13;
76  static const SystemCore Core14;
78  static const SystemCore Core15;
80  static const SystemCore Core16;
82  static const SystemCore Core17;
84  static const SystemCore Core18;
86  static const SystemCore Core19;
88  static const SystemCore Core20;
90  static const SystemCore Core21;
92  static const SystemCore Core22;
94  static const SystemCore Core23;
96  static const SystemCore Core24;
98  static const SystemCore Core25;
100  static const SystemCore Core26;
102  static const SystemCore Core27;
104  static const SystemCore Core28;
106  static const SystemCore Core29;
108  static const SystemCore Core30;
110  static const SystemCore Core31;
112  static const SystemCore IdToSystemCore[MAX_NUM_OF_CORES];
113  };
114 
115  typedef uint32_t CoreMask;
116 
120 
124 
128  CoreMask createCoreMaskFromCoreVector(const std::vector<SystemCore>& cores);
129 
133  CoreMask createCoreMaskFromCoreIds(const std::vector<int>& coreIds);
134 
138  void createCoreVectorFromCoreMask(CoreMask coreMask, std::vector<SystemCore>& resultVec);
139 
144  std::string executeShellCommand(const std::string& command);
145 
149  bool directoryExists(const std::string& dirPath);
150 
156  int clockGetTime(long& sec, long& nsec);
157 
161  void multiPlatformSleep(uint32_t seconds);
162 
166  void multiPlatformMSleep(uint32_t milliseconds);
167 
171  uint16_t hostToNet16(uint16_t host);
172 
176  uint16_t netToHost16(uint16_t net);
177 
181  uint32_t hostToNet32(uint32_t host);
182 
186  uint32_t netToHost32(uint32_t net);
187 
192  class AppName
193  {
194  private:
195  static std::string m_AppName;
196 
197  public:
201  // cppcheck-suppress constParameter
202  static void init(int argc, char* argv[])
203  {
204  if (argc == 0)
205  {
206  m_AppName.clear();
207  return;
208  }
209 
210  m_AppName = argv[0];
211 
212  // remove Linux/Unix path
213  size_t lastPos = m_AppName.rfind('/');
214  if (lastPos != std::string::npos)
215  {
216  m_AppName = m_AppName.substr(lastPos + 1);
217  }
218 
219  // remove Windows path
220  lastPos = m_AppName.rfind('\\');
221  if (lastPos != std::string::npos)
222  {
223  m_AppName = m_AppName.substr(lastPos + 1);
224  }
225 
226  // remove file extension
227  lastPos = m_AppName.rfind('.');
228  if (lastPos != std::string::npos)
229  {
230  m_AppName.resize(lastPos);
231  }
232  }
233 
235  static const std::string& get()
236  {
237  return m_AppName;
238  }
239  };
240 
245  {
246  public:
250  typedef void (*EventHandlerCallback)(void* cookie);
251 
255  {
256  static ApplicationEventHandler instance;
257  return instance;
258  }
259 
265  void onApplicationInterrupted(EventHandlerCallback handler, void* cookie);
266 
267  private:
268  EventHandlerCallback m_ApplicationInterruptedHandler;
269  void* m_ApplicationInterruptedCookie;
270 
271  // private c'tor
273 
274 #if defined(_WIN32)
275  static int handlerRoutine(unsigned long fdwCtrlType);
276 #else
277  static void handlerRoutine(int signum);
278 #endif
279  };
280 
281 } // namespace pcpp
Definition: SystemUtils.h:193
static void init(int argc, char *argv[])
Definition: SystemUtils.h:202
static const std::string & get()
Definition: SystemUtils.h:235
Definition: SystemUtils.h:245
static ApplicationEventHandler & getInstance()
Definition: SystemUtils.h:254
void(* EventHandlerCallback)(void *cookie)
Definition: SystemUtils.h:250
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:23
bool operator==(const SystemCore &other) const
Definition: SystemUtils.h:36
uint32_t Mask
Definition: SystemUtils.h:29
uint8_t Id
Core ID - a value between 0 and 31.
Definition: SystemUtils.h:32
Definition: SystemUtils.h:46
static const SystemCore Core9
Static representation of core #9.
Definition: SystemUtils.h:66
static const SystemCore Core16
Static representation of core #16.
Definition: SystemUtils.h:80
static const SystemCore Core15
Static representation of core #15.
Definition: SystemUtils.h:78
static const SystemCore Core29
Static representation of core #29.
Definition: SystemUtils.h:106
static const SystemCore Core26
Static representation of core #26.
Definition: SystemUtils.h:100
static const SystemCore Core13
Static representation of core #13.
Definition: SystemUtils.h:74
static const SystemCore Core22
Static representation of core #22.
Definition: SystemUtils.h:92
static const SystemCore Core10
Static representation of core #10.
Definition: SystemUtils.h:68
static const SystemCore Core8
Static representation of core #8.
Definition: SystemUtils.h:64
static const SystemCore Core1
Static representation of core #1.
Definition: SystemUtils.h:50
static const SystemCore Core23
Static representation of core #23.
Definition: SystemUtils.h:94
static const SystemCore Core17
Static representation of core #17.
Definition: SystemUtils.h:82
static const SystemCore Core14
Static representation of core #14.
Definition: SystemUtils.h:76
static const SystemCore Core20
Static representation of core #20.
Definition: SystemUtils.h:88
static const SystemCore Core3
Static representation of core #3.
Definition: SystemUtils.h:54
static const SystemCore Core27
Static representation of core #27.
Definition: SystemUtils.h:102
static const SystemCore Core30
Static representation of core #30.
Definition: SystemUtils.h:108
static const SystemCore Core18
Static representation of core #18.
Definition: SystemUtils.h:84
static const SystemCore Core0
Static representation of core #0.
Definition: SystemUtils.h:48
static const SystemCore Core12
Static representation of core #12.
Definition: SystemUtils.h:72
static const SystemCore Core24
Static representation of core #24.
Definition: SystemUtils.h:96
static const SystemCore Core25
Static representation of core #25.
Definition: SystemUtils.h:98
static const SystemCore Core11
Static representation of core #11.
Definition: SystemUtils.h:70
static const SystemCore Core28
Static representation of core #28.
Definition: SystemUtils.h:104
static const SystemCore Core7
Static representation of core #7.
Definition: SystemUtils.h:62
static const SystemCore Core19
Static representation of core #19.
Definition: SystemUtils.h:86
static const SystemCore Core4
Static representation of core #4.
Definition: SystemUtils.h:56
static const SystemCore IdToSystemCore[32]
A static array for mapping core ID (integer) to the corresponding static SystemCore representation.
Definition: SystemUtils.h:112
static const SystemCore Core31
Static representation of core #31.
Definition: SystemUtils.h:110
static const SystemCore Core2
Static representation of core #2.
Definition: SystemUtils.h:52
static const SystemCore Core5
Static representation of core #5.
Definition: SystemUtils.h:58
static const SystemCore Core6
Static representation of core #6.
Definition: SystemUtils.h:60
static const SystemCore Core21
Static representation of core #21.
Definition: SystemUtils.h:90