statistics.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. /*
  34. * Struct used to represent:
  35. * - IP address (IPv4 or IPv6)
  36. * - TTL value
  37. */
  38. struct ipAddress_ttl {
  39. std::string ipAddress;
  40. int ttlValue;
  41. bool operator==(const ipAddress_ttl &other) const {
  42. return ipAddress == other.ipAddress
  43. && ttlValue == other.ttlValue;
  44. }
  45. };
  46. /*
  47. * Struct used to represent:
  48. * - IP address (IPv4 or IPv6)
  49. * - Protocol (e.g. TCP, UDP, IPv4, IPv6)
  50. */
  51. struct ipAddress_protocol {
  52. std::string ipAddress;
  53. std::string protocol;
  54. bool operator==(const ipAddress_protocol &other) const {
  55. return ipAddress == other.ipAddress
  56. && protocol == other.protocol;
  57. }
  58. };
  59. /*
  60. * Struct used to represent:
  61. * - Number of received packets
  62. * - Number of sent packets
  63. * - Data received in kbytes
  64. * - Data sent in kbytes
  65. */
  66. struct entry_ipStat {
  67. long pkts_received;
  68. long pkts_sent;
  69. float kbytes_received;
  70. float kbytes_sent;
  71. bool operator==(const entry_ipStat &other) const {
  72. return pkts_received == other.pkts_received
  73. && pkts_sent == other.pkts_sent
  74. && kbytes_sent == other.kbytes_sent
  75. && kbytes_received == other.kbytes_received;
  76. }
  77. };
  78. /*
  79. * Struct used to represent:
  80. * - IP address (IPv4 or IPv6)
  81. - Traffic direction (out: outgoing connection, in: incoming connection)
  82. * - Port number
  83. */
  84. struct ipAddress_inOut_port {
  85. std::string ipAddress;
  86. std::string trafficDirection;
  87. int portNumber;
  88. bool operator==(const ipAddress_inOut_port &other) const {
  89. return ipAddress == other.ipAddress
  90. && trafficDirection == other.trafficDirection
  91. && portNumber == other.portNumber;
  92. }
  93. };
  94. /*
  95. * Definition of hash functions for structs used as key in unordered_map
  96. */
  97. namespace std {
  98. template<>
  99. struct hash<ipAddress_ttl> {
  100. std::size_t operator()(const ipAddress_ttl &k) const {
  101. using std::size_t;
  102. using std::hash;
  103. using std::string;
  104. return ((hash<string>()(k.ipAddress)
  105. ^ (hash<int>()(k.ttlValue) << 1)) >> 1);
  106. }
  107. };
  108. template<>
  109. struct hash<ipAddress_protocol> {
  110. std::size_t operator()(const ipAddress_protocol &k) const {
  111. using std::size_t;
  112. using std::hash;
  113. using std::string;
  114. return ((hash<string>()(k.ipAddress)
  115. ^ (hash<string>()(k.protocol) << 1)) >> 1);
  116. }
  117. };
  118. template<>
  119. struct hash<ipAddress_inOut_port> {
  120. std::size_t operator()(const ipAddress_inOut_port &k) const {
  121. using std::size_t;
  122. using std::hash;
  123. using std::string;
  124. return ((hash<string>()(k.ipAddress)
  125. ^ (hash<string>()(k.trafficDirection) << 1)) >> 1)
  126. ^ (hash<int>()(k.portNumber) << 1);
  127. }
  128. };
  129. }
  130. class statistics {
  131. public:
  132. /*
  133. * Constructor
  134. */
  135. statistics();
  136. /*
  137. * Methods
  138. */
  139. std::string getFormattedTimestamp(time_t seconds, suseconds_t microseconds) const;
  140. /*
  141. * Access methods for containers
  142. */
  143. void incrementPacketCount();
  144. void incrementTTLcount(std::string ipAddress, int ttlValue);
  145. void incrementProtocolCount(std::string ipAddress, std::string protocol);
  146. void incrementPortCount(std::string ipAddressSender, int outgoingPort, std::string ipAddressReceiver,
  147. int incomingPort);
  148. int getProtocolCount(std::string ipAddress, std::string protocol);
  149. void setTimestampFirstPacket(Tins::Timestamp ts);
  150. void setTimestampLastPacket(Tins::Timestamp ts);
  151. void assignMacAddress(std::string ipAddress, std::string macAddress);
  152. void addIpStat_packetSent(std::string ipAddressSender, std::string ipAddressReceiver, long bytesSent);
  153. void addMSS(std::string ipAddress, int MSSvalue);
  154. void writeToDatabase(std::string database_path);
  155. void addPacketSize(uint32_t packetSize);
  156. std::string getCaptureDurationTimestamp() const;
  157. float getCaptureDurationSeconds() const;
  158. float getAvgPacketSize() const;
  159. void printStats(std::string ipAddress);
  160. /*
  161. * IP Address-specific statistics
  162. */
  163. ip_stats getStatsForIP(std::string ipAddress);
  164. private:
  165. /*
  166. * Data fields
  167. */
  168. Tins::Timestamp timestamp_firstPacket;
  169. Tins::Timestamp timestamp_lastPacket;
  170. float sumPacketSize = 0;
  171. int packetCount = 0;
  172. /*
  173. * Data containers
  174. */
  175. // {IP Address, TTL value, count}
  176. std::unordered_map<ipAddress_ttl, int> ttl_distribution;
  177. // {IP Address, Protocol, count}
  178. std::unordered_map<ipAddress_protocol, int> protocol_distribution;
  179. // {IP Address, #received packets, #sent packets, Data received in kbytes, Data sent in kbytes}
  180. std::unordered_map<std::string, entry_ipStat> ip_statistics;
  181. // {IP Address, in_out, Port Number, count}
  182. std::unordered_map<ipAddress_inOut_port, int> ip_ports;
  183. // {IP Address, MAC Address}
  184. std::unordered_map<std::string, std::string> ip_mac_mapping;
  185. // {IP Address, avg MSS}
  186. std::unordered_map<std::string, int> ip_sumMss;
  187. };
  188. #endif //CPP_PCAPREADER_STATISTICS_H