statistics.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /*
  2. * Class providing containers and access methods for statistical data collection.
  3. */
  4. #ifndef CPP_PCAPREADER_STATISTICS_H
  5. #define CPP_PCAPREADER_STATISTICS_H
  6. // Aidmar
  7. #include <vector>
  8. #include <unordered_map>
  9. #include <list>
  10. #include <tuple>
  11. #include <tins/timestamp.h>
  12. #include <tins/ip_address.h>
  13. /*
  14. * Definition of structs used in unordered_map fields
  15. */
  16. /*
  17. * Struct used as data structure for method get_stats_for_ip, represents:
  18. * - Incoming bandwidth in KBits
  19. * - Outgoing bandwidth in KBits
  20. * - Number of incoming packets per second
  21. * - Number of outgoing packets per second
  22. * - Average size of sent packets in kbytes
  23. * - Average size of received packets in kybtes
  24. * - Average value of TCP option Maximum Segment Size (MSS)
  25. */
  26. struct ip_stats {
  27. float bandwidthKBitsIn;
  28. float bandwidthKBitsOut;
  29. float packetPerSecondIn;
  30. float packetPerSecondOut;
  31. float AvgPacketSizeSent;
  32. float AvgPacketSizeRecv;
  33. long AvgMaxSegmentSizeTCP;
  34. };
  35. // Aidmar
  36. /*
  37. * Struct used to represent a flow by:
  38. * - IP address A
  39. * - Port A
  40. * - IP address B
  41. * - Port B
  42. */
  43. struct flow{
  44. std::string ipAddressA;
  45. int portA;
  46. std::string ipAddressB;
  47. int portB;
  48. bool operator==(const flow &other) const {
  49. return ipAddressA == other.ipAddressA
  50. && portA == other.portA
  51. &&ipAddressB == other.ipAddressB
  52. && portB == other.portB;
  53. }
  54. };
  55. // Aidmar
  56. /*
  57. * Struct used to represent:
  58. * - IP address (IPv4 or IPv6)
  59. * - MSS value
  60. */
  61. struct ipAddress_mss {
  62. std::string ipAddress;
  63. int mssValue;
  64. bool operator==(const ipAddress_mss &other) const {
  65. return ipAddress == other.ipAddress
  66. && mssValue == other.mssValue;
  67. }
  68. };
  69. // Aidmar
  70. /*
  71. * Struct used to represent:
  72. * - IP address (IPv4 or IPv6)
  73. * - Window size
  74. */
  75. struct ipAddress_win {
  76. std::string ipAddress;
  77. int winSize;
  78. bool operator==(const ipAddress_win &other) const {
  79. return ipAddress == other.ipAddress
  80. && winSize == other.winSize;
  81. }
  82. };
  83. /*
  84. * Struct used to represent:
  85. * - IP address (IPv4 or IPv6)
  86. * - TTL value
  87. */
  88. struct ipAddress_ttl {
  89. std::string ipAddress;
  90. int ttlValue;
  91. bool operator==(const ipAddress_ttl &other) const {
  92. return ipAddress == other.ipAddress
  93. && ttlValue == other.ttlValue;
  94. }
  95. };
  96. /*
  97. * Struct used to represent:
  98. * - IP address (IPv4 or IPv6)
  99. * - Protocol (e.g. TCP, UDP, IPv4, IPv6)
  100. */
  101. struct ipAddress_protocol {
  102. std::string ipAddress;
  103. std::string protocol;
  104. bool operator==(const ipAddress_protocol &other) const {
  105. return ipAddress == other.ipAddress
  106. && protocol == other.protocol;
  107. }
  108. };
  109. /*
  110. * Struct used to represent:
  111. * - Number of received packets
  112. * - Number of sent packets
  113. * - Data received in kbytes
  114. * - Data sent in kbytes
  115. */
  116. struct entry_ipStat {
  117. long pkts_received;
  118. long pkts_sent;
  119. float kbytes_received;
  120. float kbytes_sent;
  121. // Aidmar
  122. std::string ip_class;
  123. // Aidmar - to calculate Mahoney anomaly score
  124. long firstAppearAsSenderPktCount;
  125. long firstAppearAsReceiverPktCount;
  126. long sourceAnomalyScore;
  127. long destinationAnomalyScore;
  128. // Aidmar- To collect statstics over time interval
  129. std::vector<std::chrono::microseconds> pktsSentTimestamp;
  130. std::vector<std::chrono::microseconds> pktsReceivedTimestamp;
  131. bool operator==(const entry_ipStat &other) const {
  132. return pkts_received == other.pkts_received
  133. && pkts_sent == other.pkts_sent
  134. && kbytes_sent == other.kbytes_sent
  135. && kbytes_received == other.kbytes_received
  136. && ip_class == other.ip_class
  137. // Aidmar
  138. && firstAppearAsSenderPktCount == other.firstAppearAsSenderPktCount
  139. && firstAppearAsReceiverPktCount == other.firstAppearAsReceiverPktCount
  140. && sourceAnomalyScore == other.sourceAnomalyScore
  141. && destinationAnomalyScore == other.destinationAnomalyScore
  142. && pktsSentTimestamp == other.pktsSentTimestamp
  143. && pktsReceivedTimestamp == other.pktsReceivedTimestamp;
  144. }
  145. };
  146. // Aidmar
  147. /*
  148. * Struct used to represent:
  149. * - Number of packets from A to B
  150. * - Number of packets from B to A
  151. */
  152. struct entry_flowStat {
  153. long pkts_A_B;
  154. long pkts_B_A;
  155. std::vector<std::chrono::microseconds> pkts_A_B_timestamp;
  156. std::vector<std::chrono::microseconds> pkts_B_A_timestamp;
  157. std::vector<std::chrono::microseconds> pkts_delay;
  158. //std::chrono::duration<double, std::micro> median_delay;
  159. std::chrono::microseconds median_delay;
  160. bool operator==(const entry_flowStat &other) const {
  161. return pkts_A_B == other.pkts_A_B
  162. && pkts_A_B_timestamp == other.pkts_A_B_timestamp
  163. && pkts_B_A_timestamp == other.pkts_B_A_timestamp
  164. && pkts_delay == other.pkts_delay
  165. && median_delay == other.median_delay;
  166. }
  167. };
  168. /*
  169. * Struct used to represent:
  170. * - IP address (IPv4 or IPv6)
  171. - Traffic direction (out: outgoing connection, in: incoming connection)
  172. * - Port number
  173. */
  174. struct ipAddress_inOut_port {
  175. std::string ipAddress;
  176. std::string trafficDirection;
  177. int portNumber;
  178. bool operator==(const ipAddress_inOut_port &other) const {
  179. return ipAddress == other.ipAddress
  180. && trafficDirection == other.trafficDirection
  181. && portNumber == other.portNumber;
  182. }
  183. };
  184. /*
  185. * Definition of hash functions for structs used as key in unordered_map
  186. */
  187. namespace std {
  188. template<>
  189. struct hash<ipAddress_ttl> {
  190. std::size_t operator()(const ipAddress_ttl &k) const {
  191. using std::size_t;
  192. using std::hash;
  193. using std::string;
  194. return ((hash<string>()(k.ipAddress)
  195. ^ (hash<int>()(k.ttlValue) << 1)) >> 1);
  196. }
  197. };
  198. // Aidmar
  199. template<>
  200. struct hash<ipAddress_mss> {
  201. std::size_t operator()(const ipAddress_mss &k) const {
  202. using std::size_t;
  203. using std::hash;
  204. using std::string;
  205. return ((hash<string>()(k.ipAddress)
  206. ^ (hash<int>()(k.mssValue) << 1)) >> 1);
  207. }
  208. };
  209. // Aidmar
  210. template<>
  211. struct hash<ipAddress_win> {
  212. std::size_t operator()(const ipAddress_win &k) const {
  213. using std::size_t;
  214. using std::hash;
  215. using std::string;
  216. return ((hash<string>()(k.ipAddress)
  217. ^ (hash<int>()(k.winSize) << 1)) >> 1);
  218. }
  219. };
  220. // Aidmar: TO-DO:??
  221. template<>
  222. struct hash<flow> {
  223. std::size_t operator()(const flow &k) const {
  224. using std::size_t;
  225. using std::hash;
  226. using std::string;
  227. return ((hash<string>()(k.ipAddressA)
  228. ^ (hash<int>()(k.portA) << 1)) >> 1)
  229. ^ ((hash<string>()(k.ipAddressB)
  230. ^ (hash<int>()(k.portB) << 1)) >> 1);
  231. }
  232. };
  233. template<>
  234. struct hash<ipAddress_protocol> {
  235. std::size_t operator()(const ipAddress_protocol &k) const {
  236. using std::size_t;
  237. using std::hash;
  238. using std::string;
  239. return ((hash<string>()(k.ipAddress)
  240. ^ (hash<string>()(k.protocol) << 1)) >> 1);
  241. }
  242. };
  243. template<>
  244. struct hash<ipAddress_inOut_port> {
  245. std::size_t operator()(const ipAddress_inOut_port &k) const {
  246. using std::size_t;
  247. using std::hash;
  248. using std::string;
  249. return ((hash<string>()(k.ipAddress)
  250. ^ (hash<string>()(k.trafficDirection) << 1)) >> 1)
  251. ^ (hash<int>()(k.portNumber) << 1);
  252. }
  253. };
  254. }
  255. class statistics {
  256. public:
  257. /*
  258. * Constructor
  259. */
  260. statistics();
  261. /*
  262. * Methods
  263. */
  264. std::string getFormattedTimestamp(time_t seconds, suseconds_t microseconds) const;
  265. /*
  266. * Access methods for containers
  267. */
  268. void incrementPacketCount();
  269. // Adimar
  270. void incrementMSScount(std::string ipAddress, int mssValue);
  271. void incrementWinCount(std::string ipAddress, int winSize);
  272. void addIPEntropy(std::string filePath);
  273. void addFlowStat(std::string ipAddressSender,int sport,std::string ipAddressReceiver,int dport, std::chrono::microseconds timestamp);
  274. void calculateLastIntervalIPsEntropy(std::string filePath, std::chrono::microseconds intervalStartTimestamp);
  275. void incrementTTLcount(std::string ipAddress, int ttlValue);
  276. void incrementProtocolCount(std::string ipAddress, std::string protocol);
  277. void incrementPortCount(std::string ipAddressSender, int outgoingPort, std::string ipAddressReceiver,
  278. int incomingPort);
  279. int getProtocolCount(std::string ipAddress, std::string protocol);
  280. void setTimestampFirstPacket(Tins::Timestamp ts);
  281. void setTimestampLastPacket(Tins::Timestamp ts);
  282. // Aidmar
  283. Tins::Timestamp getTimestampFirstPacket();
  284. Tins::Timestamp getTimestampLastPacket();
  285. void assignMacAddress(std::string ipAddress, std::string macAddress);
  286. // Aidmar
  287. void addIpStat_packetSent(std::string filePath, std::string ipAddressSender, std::string ipAddressReceiver, long bytesSent, std::chrono::microseconds timestamp);
  288. void addMSS(std::string ipAddress, int MSSvalue);
  289. void writeToDatabase(std::string database_path);
  290. void addPacketSize(uint32_t packetSize);
  291. std::string getCaptureDurationTimestamp() const;
  292. float getCaptureDurationSeconds() const;
  293. float getAvgPacketSize() const;
  294. void printStats(std::string ipAddress);
  295. /*
  296. * IP Address-specific statistics
  297. */
  298. ip_stats getStatsForIP(std::string ipAddress);
  299. private:
  300. /*
  301. * Data fields
  302. */
  303. Tins::Timestamp timestamp_firstPacket;
  304. Tins::Timestamp timestamp_lastPacket;
  305. float sumPacketSize = 0;
  306. int packetCount = 0;
  307. /*
  308. * Data containers
  309. */
  310. // {IP Address, TTL value, count}
  311. std::unordered_map<ipAddress_ttl, int> ttl_distribution;
  312. // Aidmar
  313. // {IP Address, MSS value, count}
  314. std::unordered_map<ipAddress_mss, int> mss_distribution;
  315. // {IP Address, Win size, count}
  316. std::unordered_map<ipAddress_win, int> win_distribution;
  317. // {IP Address A, Port A, IP Address B, Port B, #packets_A_B, #packets_B_A}
  318. std::unordered_map<flow, entry_flowStat> flow_statistics;
  319. // {IP Address, Protocol, count}
  320. std::unordered_map<ipAddress_protocol, int> protocol_distribution;
  321. // {IP Address, #received packets, #sent packets, Data received in kbytes, Data sent in kbytes}
  322. std::unordered_map<std::string, entry_ipStat> ip_statistics;
  323. // {IP Address, in_out, Port Number, count}
  324. std::unordered_map<ipAddress_inOut_port, int> ip_ports;
  325. // {IP Address, MAC Address}
  326. std::unordered_map<std::string, std::string> ip_mac_mapping;
  327. // {IP Address, avg MSS}
  328. std::unordered_map<std::string, int> ip_sumMss;
  329. };
  330. #endif //CPP_PCAPREADER_STATISTICS_H