123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438 |
- #ifndef CPP_PCAPREADER_STATISTICS_H
- #define CPP_PCAPREADER_STATISTICS_H
- #include <vector>
- #include <unordered_map>
- #include <list>
- #include <tuple>
- #include <tins/timestamp.h>
- #include <tins/ip_address.h>
- #include "utilities.h"
- struct ip_stats {
- float bandwidthKBitsIn;
- float bandwidthKBitsOut;
- float packetPerSecondIn;
- float packetPerSecondOut;
- float AvgPacketSizeSent;
- float AvgPacketSizeRecv;
- long AvgMaxSegmentSizeTCP;
- };
- struct conv{
- std::string ipAddressA;
- int portA;
- std::string ipAddressB;
- int portB;
- bool operator==(const conv &other) const {
- return ipAddressA == other.ipAddressA
- && portA == other.portA
- &&ipAddressB == other.ipAddressB
- && portB == other.portB;
- }
- };
- struct ipAddress_mss {
- std::string ipAddress;
- int mssValue;
- bool operator==(const ipAddress_mss &other) const {
- return ipAddress == other.ipAddress
- && mssValue == other.mssValue;
- }
- };
- struct ipAddress_win {
- std::string ipAddress;
- int winSize;
- bool operator==(const ipAddress_win &other) const {
- return ipAddress == other.ipAddress
- && winSize == other.winSize;
- }
- };
- struct ipAddress_ttl {
- std::string ipAddress;
- int ttlValue;
- bool operator==(const ipAddress_ttl &other) const {
- return ipAddress == other.ipAddress
- && ttlValue == other.ttlValue;
- }
- };
- struct ipAddress_protocol {
- std::string ipAddress;
- std::string protocol;
- bool operator==(const ipAddress_protocol &other) const {
- return ipAddress == other.ipAddress
- && protocol == other.protocol;
- }
- };
- struct entry_ipStat {
- long pkts_received;
- long pkts_sent;
- float kbytes_received;
- float kbytes_sent;
-
- std::string ip_class;
- std::vector<float> interval_pkt_rate;
- float max_pkt_rate;
- float min_pkt_rate;
-
- long firstAppearAsSenderPktCount;
- long firstAppearAsReceiverPktCount;
- float sourceAnomalyScore;
- float destinationAnomalyScore;
-
- std::vector<std::chrono::microseconds> pktsSentTimestamp;
- std::vector<std::chrono::microseconds> pktsReceivedTimestamp;
- bool operator==(const entry_ipStat &other) const {
- return pkts_received == other.pkts_received
- && pkts_sent == other.pkts_sent
- && kbytes_sent == other.kbytes_sent
- && kbytes_received == other.kbytes_received
-
- && interval_pkt_rate == other.interval_pkt_rate
- && max_pkt_rate == other.max_pkt_rate
- && min_pkt_rate == other.min_pkt_rate
- && ip_class == other.ip_class
- && firstAppearAsSenderPktCount == other.firstAppearAsSenderPktCount
- && firstAppearAsReceiverPktCount == other.firstAppearAsReceiverPktCount
- && sourceAnomalyScore == other.sourceAnomalyScore
- && destinationAnomalyScore == other.destinationAnomalyScore
- && pktsSentTimestamp == other.pktsSentTimestamp
- && pktsReceivedTimestamp == other.pktsReceivedTimestamp;
- }
- };
- struct entry_intervalStat {
- int pkts_count;
- float kbytes;
- float ip_src_entropy;
- float ip_dst_entropy;
- float ip_src_cum_entropy;
- float ip_dst_cum_entropy;
-
-
-
- bool operator==(const entry_intervalStat &other) const {
- return pkts_count == other.pkts_count
- && kbytes == other.kbytes
- && ip_src_entropy == other.ip_src_entropy
- && ip_dst_entropy == other.ip_dst_entropy
- && ip_src_cum_entropy == other.ip_src_cum_entropy
- && ip_dst_cum_entropy == other.ip_dst_cum_entropy;
- }
- };
- struct entry_convStat {
- long pkts_A_B;
- long pkts_B_A;
- std::vector<std::chrono::microseconds> pkts_A_B_timestamp;
- std::vector<std::chrono::microseconds> pkts_B_A_timestamp;
- std::vector<std::chrono::microseconds> pkts_delay;
-
- std::chrono::microseconds avg_delay;
-
- bool operator==(const entry_convStat &other) const {
- return pkts_A_B == other.pkts_A_B
- && pkts_A_B_timestamp == other.pkts_A_B_timestamp
- && pkts_B_A_timestamp == other.pkts_B_A_timestamp
- && pkts_delay == other.pkts_delay
- && avg_delay == other.avg_delay;
- }
- };
- struct ipAddress_inOut_port {
- std::string ipAddress;
- std::string trafficDirection;
- int portNumber;
- bool operator==(const ipAddress_inOut_port &other) const {
- return ipAddress == other.ipAddress
- && trafficDirection == other.trafficDirection
- && portNumber == other.portNumber;
- }
- };
- namespace std {
- template<>
- struct hash<ipAddress_ttl> {
- std::size_t operator()(const ipAddress_ttl &k) const {
- using std::size_t;
- using std::hash;
- using std::string;
- return ((hash<string>()(k.ipAddress)
- ^ (hash<int>()(k.ttlValue) << 1)) >> 1);
- }
- };
-
- template<>
- struct hash<ipAddress_mss> {
- std::size_t operator()(const ipAddress_mss &k) const {
- using std::size_t;
- using std::hash;
- using std::string;
- return ((hash<string>()(k.ipAddress)
- ^ (hash<int>()(k.mssValue) << 1)) >> 1);
- }
- };
-
- template<>
- struct hash<ipAddress_win> {
- std::size_t operator()(const ipAddress_win &k) const {
- using std::size_t;
- using std::hash;
- using std::string;
- return ((hash<string>()(k.ipAddress)
- ^ (hash<int>()(k.winSize) << 1)) >> 1);
- }
- };
-
-
- template<>
- struct hash<conv> {
- std::size_t operator()(const conv &k) const {
- using std::size_t;
- using std::hash;
- using std::string;
- return ((hash<string>()(k.ipAddressA)
- ^ (hash<int>()(k.portA) << 1)) >> 1)
- ^ ((hash<string>()(k.ipAddressB)
- ^ (hash<int>()(k.portB) << 1)) >> 1);
- }
- };
-
- template<>
- struct hash<ipAddress_protocol> {
- std::size_t operator()(const ipAddress_protocol &k) const {
- using std::size_t;
- using std::hash;
- using std::string;
- return ((hash<string>()(k.ipAddress)
- ^ (hash<string>()(k.protocol) << 1)) >> 1);
- }
- };
- template<>
- struct hash<ipAddress_inOut_port> {
- std::size_t operator()(const ipAddress_inOut_port &k) const {
- using std::size_t;
- using std::hash;
- using std::string;
- return ((hash<string>()(k.ipAddress)
- ^ (hash<string>()(k.trafficDirection) << 1)) >> 1)
- ^ (hash<int>()(k.portNumber) << 1);
- }
- };
- }
- class statistics {
- public:
-
- statistics();
-
- std::string getFormattedTimestamp(time_t seconds, suseconds_t microseconds) const;
-
- void incrementPacketCount();
-
- void calculateIPIntervalPacketRate(std::chrono::duration<int, std::micro> interval, std::chrono::microseconds intervalStartTimestamp);
- void incrementMSScount(std::string ipAddress, int mssValue);
- void incrementWinCount(std::string ipAddress, int winSize);
- void addConvStat(std::string ipAddressSender,int sport,std::string ipAddressReceiver,int dport, std::chrono::microseconds timestamp);
- std::vector<float> calculateIPsCumEntropy();
- std::vector<float> calculateLastIntervalIPsEntropy(std::chrono::microseconds intervalStartTimestamp);
- void addIntervalStat(std::chrono::duration<int, std::micro> interval, std::chrono::microseconds intervalStartTimestamp, std::chrono::microseconds lastPktTimestamp, int previousPacketCount, float previousSumPacketSize);
- void incrementTTLcount(std::string ipAddress, int ttlValue);
- void incrementProtocolCount(std::string ipAddress, std::string protocol);
- void incrementPortCount(std::string ipAddressSender, int outgoingPort, std::string ipAddressReceiver,
- int incomingPort);
- int getProtocolCount(std::string ipAddress, std::string protocol);
- void setTimestampFirstPacket(Tins::Timestamp ts);
- void setTimestampLastPacket(Tins::Timestamp ts);
-
-
- Tins::Timestamp getTimestampFirstPacket();
- Tins::Timestamp getTimestampLastPacket();
- void assignMacAddress(std::string ipAddress, std::string macAddress);
-
-
- void addIpStat_packetSent(std::string filePath, std::string ipAddressSender, std::string ipAddressReceiver, long bytesSent, std::chrono::microseconds timestamp);
- int getPacketCount();
- int getSumPacketSize();
- void addMSS(std::string ipAddress, int MSSvalue);
- void writeToDatabase(std::string database_path);
- void addPacketSize(uint32_t packetSize);
- std::string getCaptureDurationTimestamp() const;
- float getCaptureDurationSeconds() const;
- float getAvgPacketSize() const;
- void printStats(std::string ipAddress);
-
- ip_stats getStatsForIP(std::string ipAddress);
- private:
-
- Tins::Timestamp timestamp_firstPacket;
- Tins::Timestamp timestamp_lastPacket;
- float sumPacketSize = 0;
- int packetCount = 0;
-
-
- std::unordered_map<ipAddress_ttl, int> ttl_distribution;
-
-
- std::unordered_map<ipAddress_mss, int> mss_distribution;
-
- std::unordered_map<ipAddress_win, int> win_distribution;
-
- std::unordered_map<conv, entry_convStat> conv_statistics;
- std::unordered_map<std::string, entry_intervalStat> interval_statistics;
-
-
- std::unordered_map<ipAddress_protocol, int> protocol_distribution;
-
- std::unordered_map<std::string, entry_ipStat> ip_statistics;
-
- std::unordered_map<ipAddress_inOut_port, int> ip_ports;
-
- std::unordered_map<std::string, std::string> ip_mac_mapping;
-
- std::unordered_map<std::string, int> ip_sumMss;
- };
- #endif
|