statistics.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. #include <unordered_map>
  7. #include <list>
  8. #include <tuple>
  9. #include <tins/timestamp.h>
  10. #include <tins/ip_address.h>
  11. /*
  12. * Definition of structs used in unordered_map fields
  13. */
  14. /*
  15. * Struct used as data structure for method get_stats_for_ip, represents:
  16. * - Incoming bandwidth in KBits
  17. * - Outgoing bandwidth in KBits
  18. * - Number of incoming packets per second
  19. * - Number of outgoing packets per second
  20. * - Average size of sent packets in kbytes
  21. * - Average size of received packets in kybtes
  22. * - Average value of TCP option Maximum Segment Size (MSS)
  23. */
  24. struct ip_stats {
  25. float bandwidthKBitsIn;
  26. float bandwidthKBitsOut;
  27. float packetPerSecondIn;
  28. float packetPerSecondOut;
  29. float AvgPacketSizeSent;
  30. float AvgPacketSizeRecv;
  31. long AvgMaxSegmentSizeTCP;
  32. };
  33. // Aidmar
  34. /*
  35. * Struct used to represent:
  36. * - IP address (IPv4 or IPv6)
  37. * - MSS value
  38. */
  39. struct ipAddress_mss {
  40. std::string ipAddress;
  41. int mssValue;
  42. bool operator==(const ipAddress_mss &other) const {
  43. return ipAddress == other.ipAddress
  44. && mssValue == other.mssValue;
  45. }
  46. };
  47. // Aidmar
  48. /*
  49. * Struct used to represent:
  50. * - IP address (IPv4 or IPv6)
  51. * - Window size
  52. */
  53. struct ipAddress_win {
  54. std::string ipAddress;
  55. int winSize;
  56. bool operator==(const ipAddress_win &other) const {
  57. return ipAddress == other.ipAddress
  58. && winSize == other.winSize;
  59. }
  60. };
  61. /*
  62. * Struct used to represent:
  63. * - IP address (IPv4 or IPv6)
  64. * - TTL value
  65. */
  66. struct ipAddress_ttl {
  67. std::string ipAddress;
  68. int ttlValue;
  69. bool operator==(const ipAddress_ttl &other) const {
  70. return ipAddress == other.ipAddress
  71. && ttlValue == other.ttlValue;
  72. }
  73. };
  74. /*
  75. * Struct used to represent:
  76. * - IP address (IPv4 or IPv6)
  77. * - Protocol (e.g. TCP, UDP, IPv4, IPv6)
  78. */
  79. struct ipAddress_protocol {
  80. std::string ipAddress;
  81. std::string protocol;
  82. bool operator==(const ipAddress_protocol &other) const {
  83. return ipAddress == other.ipAddress
  84. && protocol == other.protocol;
  85. }
  86. };
  87. /*
  88. * Struct used to represent:
  89. * - Number of received packets
  90. * - Number of sent packets
  91. * - Data received in kbytes
  92. * - Data sent in kbytes
  93. */
  94. struct entry_ipStat {
  95. long pkts_received;
  96. long pkts_sent;
  97. float kbytes_received;
  98. float kbytes_sent;
  99. // Aidmar - to calculate tn/r score
  100. long firstAppearAsSenderPktCount;
  101. long firstAppearAsReceiverPktCount;
  102. long sourceAnomalyScore;
  103. long destinationAnomalyScore;
  104. bool operator==(const entry_ipStat &other) const {
  105. return pkts_received == other.pkts_received
  106. && pkts_sent == other.pkts_sent
  107. && kbytes_sent == other.kbytes_sent
  108. && kbytes_received == other.kbytes_received
  109. // Aidmar
  110. && firstAppearAsSenderPktCount == other.firstAppearAsSenderPktCount
  111. && firstAppearAsReceiverPktCount == other.firstAppearAsReceiverPktCount
  112. && sourceAnomalyScore == other.sourceAnomalyScore
  113. && destinationAnomalyScore == other.destinationAnomalyScore;
  114. }
  115. };
  116. /*
  117. * Struct used to represent:
  118. * - IP address (IPv4 or IPv6)
  119. - Traffic direction (out: outgoing connection, in: incoming connection)
  120. * - Port number
  121. */
  122. struct ipAddress_inOut_port {
  123. std::string ipAddress;
  124. std::string trafficDirection;
  125. int portNumber;
  126. bool operator==(const ipAddress_inOut_port &other) const {
  127. return ipAddress == other.ipAddress
  128. && trafficDirection == other.trafficDirection
  129. && portNumber == other.portNumber;
  130. }
  131. };
  132. /*
  133. * Definition of hash functions for structs used as key in unordered_map
  134. */
  135. namespace std {
  136. template<>
  137. struct hash<ipAddress_ttl> {
  138. std::size_t operator()(const ipAddress_ttl &k) const {
  139. using std::size_t;
  140. using std::hash;
  141. using std::string;
  142. return ((hash<string>()(k.ipAddress)
  143. ^ (hash<int>()(k.ttlValue) << 1)) >> 1);
  144. }
  145. };
  146. // Aidmar
  147. template<>
  148. struct hash<ipAddress_mss> {
  149. std::size_t operator()(const ipAddress_mss &k) const {
  150. using std::size_t;
  151. using std::hash;
  152. using std::string;
  153. return ((hash<string>()(k.ipAddress)
  154. ^ (hash<int>()(k.mssValue) << 1)) >> 1);
  155. }
  156. };
  157. // Aidmar
  158. template<>
  159. struct hash<ipAddress_win> {
  160. std::size_t operator()(const ipAddress_win &k) const {
  161. using std::size_t;
  162. using std::hash;
  163. using std::string;
  164. return ((hash<string>()(k.ipAddress)
  165. ^ (hash<int>()(k.winSize) << 1)) >> 1);
  166. }
  167. };
  168. template<>
  169. struct hash<ipAddress_protocol> {
  170. std::size_t operator()(const ipAddress_protocol &k) const {
  171. using std::size_t;
  172. using std::hash;
  173. using std::string;
  174. return ((hash<string>()(k.ipAddress)
  175. ^ (hash<string>()(k.protocol) << 1)) >> 1);
  176. }
  177. };
  178. template<>
  179. struct hash<ipAddress_inOut_port> {
  180. std::size_t operator()(const ipAddress_inOut_port &k) const {
  181. using std::size_t;
  182. using std::hash;
  183. using std::string;
  184. return ((hash<string>()(k.ipAddress)
  185. ^ (hash<string>()(k.trafficDirection) << 1)) >> 1)
  186. ^ (hash<int>()(k.portNumber) << 1);
  187. }
  188. };
  189. }
  190. class statistics {
  191. public:
  192. /*
  193. * Constructor
  194. */
  195. statistics();
  196. /*
  197. * Methods
  198. */
  199. std::string getFormattedTimestamp(time_t seconds, suseconds_t microseconds) const;
  200. /*
  201. * Access methods for containers
  202. */
  203. void incrementPacketCount();
  204. // Adimar
  205. void incrementMSScount(std::string ipAddress, int mssValue);
  206. void incrementWinCount(std::string ipAddress, int winSize);
  207. void addIPEntropy();
  208. void incrementTTLcount(std::string ipAddress, int ttlValue);
  209. void incrementProtocolCount(std::string ipAddress, std::string protocol);
  210. void incrementPortCount(std::string ipAddressSender, int outgoingPort, std::string ipAddressReceiver,
  211. int incomingPort);
  212. int getProtocolCount(std::string ipAddress, std::string protocol);
  213. void setTimestampFirstPacket(Tins::Timestamp ts);
  214. void setTimestampLastPacket(Tins::Timestamp ts);
  215. void assignMacAddress(std::string ipAddress, std::string macAddress);
  216. void addIpStat_packetSent(std::string ipAddressSender, std::string ipAddressReceiver, long bytesSent);
  217. void addMSS(std::string ipAddress, int MSSvalue);
  218. void writeToDatabase(std::string database_path);
  219. void addPacketSize(uint32_t packetSize);
  220. std::string getCaptureDurationTimestamp() const;
  221. float getCaptureDurationSeconds() const;
  222. float getAvgPacketSize() const;
  223. void printStats(std::string ipAddress);
  224. /*
  225. * IP Address-specific statistics
  226. */
  227. ip_stats getStatsForIP(std::string ipAddress);
  228. private:
  229. /*
  230. * Data fields
  231. */
  232. Tins::Timestamp timestamp_firstPacket;
  233. Tins::Timestamp timestamp_lastPacket;
  234. float sumPacketSize = 0;
  235. int packetCount = 0;
  236. /*
  237. * Data containers
  238. */
  239. // {IP Address, TTL value, count}
  240. std::unordered_map<ipAddress_ttl, int> ttl_distribution;
  241. // Aidmar
  242. // {IP Address, MSS value, count}
  243. std::unordered_map<ipAddress_mss, int> mss_distribution;
  244. // {IP Address, Win size, count}
  245. std::unordered_map<ipAddress_win, int> win_distribution;
  246. // {IP Address, Protocol, count}
  247. std::unordered_map<ipAddress_protocol, int> protocol_distribution;
  248. // {IP Address, #received packets, #sent packets, Data received in kbytes, Data sent in kbytes}
  249. std::unordered_map<std::string, entry_ipStat> ip_statistics;
  250. // {IP Address, in_out, Port Number, count}
  251. std::unordered_map<ipAddress_inOut_port, int> ip_ports;
  252. // {IP Address, MAC Address}
  253. std::unordered_map<std::string, std::string> ip_mac_mapping;
  254. // {IP Address, avg MSS}
  255. std::unordered_map<std::string, int> ip_sumMss;
  256. };
  257. #endif //CPP_PCAPREADER_STATISTICS_H