statistics.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. bool operator==(const entry_ipStat &other) const {
  100. return pkts_received == other.pkts_received
  101. && pkts_sent == other.pkts_sent
  102. && kbytes_sent == other.kbytes_sent
  103. && kbytes_received == other.kbytes_received;
  104. }
  105. };
  106. /*
  107. * Struct used to represent:
  108. * - IP address (IPv4 or IPv6)
  109. - Traffic direction (out: outgoing connection, in: incoming connection)
  110. * - Port number
  111. */
  112. struct ipAddress_inOut_port {
  113. std::string ipAddress;
  114. std::string trafficDirection;
  115. int portNumber;
  116. bool operator==(const ipAddress_inOut_port &other) const {
  117. return ipAddress == other.ipAddress
  118. && trafficDirection == other.trafficDirection
  119. && portNumber == other.portNumber;
  120. }
  121. };
  122. /*
  123. * Definition of hash functions for structs used as key in unordered_map
  124. */
  125. namespace std {
  126. template<>
  127. struct hash<ipAddress_ttl> {
  128. std::size_t operator()(const ipAddress_ttl &k) const {
  129. using std::size_t;
  130. using std::hash;
  131. using std::string;
  132. return ((hash<string>()(k.ipAddress)
  133. ^ (hash<int>()(k.ttlValue) << 1)) >> 1);
  134. }
  135. };
  136. // Aidmar
  137. template<>
  138. struct hash<ipAddress_mss> {
  139. std::size_t operator()(const ipAddress_mss &k) const {
  140. using std::size_t;
  141. using std::hash;
  142. using std::string;
  143. return ((hash<string>()(k.ipAddress)
  144. ^ (hash<int>()(k.mssValue) << 1)) >> 1);
  145. }
  146. };
  147. // Aidmar
  148. template<>
  149. struct hash<ipAddress_win> {
  150. std::size_t operator()(const ipAddress_win &k) const {
  151. using std::size_t;
  152. using std::hash;
  153. using std::string;
  154. return ((hash<string>()(k.ipAddress)
  155. ^ (hash<int>()(k.winSize) << 1)) >> 1);
  156. }
  157. };
  158. template<>
  159. struct hash<ipAddress_protocol> {
  160. std::size_t operator()(const ipAddress_protocol &k) const {
  161. using std::size_t;
  162. using std::hash;
  163. using std::string;
  164. return ((hash<string>()(k.ipAddress)
  165. ^ (hash<string>()(k.protocol) << 1)) >> 1);
  166. }
  167. };
  168. template<>
  169. struct hash<ipAddress_inOut_port> {
  170. std::size_t operator()(const ipAddress_inOut_port &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.trafficDirection) << 1)) >> 1)
  176. ^ (hash<int>()(k.portNumber) << 1);
  177. }
  178. };
  179. }
  180. class statistics {
  181. public:
  182. /*
  183. * Constructor
  184. */
  185. statistics();
  186. /*
  187. * Methods
  188. */
  189. std::string getFormattedTimestamp(time_t seconds, suseconds_t microseconds) const;
  190. /*
  191. * Access methods for containers
  192. */
  193. void incrementPacketCount();
  194. // Adimar
  195. void incrementMSScount(std::string ipAddress, int mssValue);
  196. void incrementWinCount(std::string ipAddress, int winSize);
  197. void incrementTTLcount(std::string ipAddress, int ttlValue);
  198. void incrementProtocolCount(std::string ipAddress, std::string protocol);
  199. void incrementPortCount(std::string ipAddressSender, int outgoingPort, std::string ipAddressReceiver,
  200. int incomingPort);
  201. int getProtocolCount(std::string ipAddress, std::string protocol);
  202. void setTimestampFirstPacket(Tins::Timestamp ts);
  203. void setTimestampLastPacket(Tins::Timestamp ts);
  204. void assignMacAddress(std::string ipAddress, std::string macAddress);
  205. void addIpStat_packetSent(std::string ipAddressSender, std::string ipAddressReceiver, long bytesSent);
  206. void addMSS(std::string ipAddress, int MSSvalue);
  207. void writeToDatabase(std::string database_path);
  208. void addPacketSize(uint32_t packetSize);
  209. std::string getCaptureDurationTimestamp() const;
  210. float getCaptureDurationSeconds() const;
  211. float getAvgPacketSize() const;
  212. void printStats(std::string ipAddress);
  213. /*
  214. * IP Address-specific statistics
  215. */
  216. ip_stats getStatsForIP(std::string ipAddress);
  217. private:
  218. /*
  219. * Data fields
  220. */
  221. Tins::Timestamp timestamp_firstPacket;
  222. Tins::Timestamp timestamp_lastPacket;
  223. float sumPacketSize = 0;
  224. int packetCount = 0;
  225. /*
  226. * Data containers
  227. */
  228. // {IP Address, TTL value, count}
  229. std::unordered_map<ipAddress_ttl, int> ttl_distribution;
  230. // Aidmar
  231. // {IP Address, MSS value, count}
  232. std::unordered_map<ipAddress_mss, int> mss_distribution;
  233. // {IP Address, Win size, count}
  234. std::unordered_map<ipAddress_win, int> win_distribution;
  235. // {IP Address, Protocol, count}
  236. std::unordered_map<ipAddress_protocol, int> protocol_distribution;
  237. // {IP Address, #received packets, #sent packets, Data received in kbytes, Data sent in kbytes}
  238. std::unordered_map<std::string, entry_ipStat> ip_statistics;
  239. // {IP Address, in_out, Port Number, count}
  240. std::unordered_map<ipAddress_inOut_port, int> ip_ports;
  241. // {IP Address, MAC Address}
  242. std::unordered_map<std::string, std::string> ip_mac_mapping;
  243. // {IP Address, avg MSS}
  244. std::unordered_map<std::string, int> ip_sumMss;
  245. };
  246. #endif //CPP_PCAPREADER_STATISTICS_H