Browse Source

clean the code and add comments

aidmar.wainakh 6 years ago
parent
commit
8c6b29c1e4

+ 2 - 2
code_boost/src/cxx/statistics.cpp

@@ -578,8 +578,8 @@ void statistics::writeToDatabase(std::string database_path) {
         db.writeStatisticsIpMac(ip_mac_mapping);
         db.writeStatisticsPorts(ip_ports);
         db.writeStatisticsProtocols(protocol_distribution);
-        db.writeStatisticsMss_dist(mss_distribution);
-        db.writeStatisticsTos_dist(tos_distribution);
+        db.writeStatisticsMSS(mss_distribution);
+        db.writeStatisticsToS(tos_distribution);
         db.writeStatisticsWin(win_distribution);
         db.writeStatisticsConv(conv_statistics);
         db.writeStatisticsInterval(interval_statistics);

+ 1 - 1
code_boost/src/cxx/statistics.h

@@ -239,7 +239,7 @@ struct entry_convStat {
                && avg_pkt_rate == avg_pkt_rate
                && pkts_timestamp == other.pkts_timestamp
                && interarrival_time == other.interarrival_time
-               && avg_interarrival_time == other.avg_interarrival_time
+               && avg_interarrival_time == other.avg_interarrival_time;
     }
 };
 

+ 91 - 103
code_boost/src/cxx/statistics_db.cpp

@@ -88,6 +88,96 @@ void statistics_db::writeStatisticsTTL(std::unordered_map<ipAddress_ttl, int> tt
     }
 }
 
+/**
+ * Writes the MSS distribution into the database.
+ * @param mssDistribution The MSS distribution from class statistics.
+ */
+void statistics_db::writeStatisticsMSS(std::unordered_map<ipAddress_mss, int> mssDistribution) {
+    try {
+        db->exec("DROP TABLE IF EXISTS tcp_mss");
+        SQLite::Transaction transaction(*db);
+        const char *createTable = "CREATE TABLE tcp_mss ("
+                "ipAddress TEXT,"
+                "mssValue INTEGER,"
+                "mssCount INTEGER,"
+                "PRIMARY KEY(ipAddress,mssValue));";
+        db->exec(createTable);
+        SQLite::Statement query(*db, "INSERT INTO tcp_mss VALUES (?, ?, ?)");
+        for (auto it = mssDistribution.begin(); it != mssDistribution.end(); ++it) {
+            ipAddress_mss e = it->first;
+            query.bind(1, e.ipAddress);
+            query.bind(2, e.mssValue);
+            query.bind(3, it->second);
+            query.exec();
+            query.reset();
+        }
+        transaction.commit();
+    }
+    catch (std::exception &e) {
+        std::cout << "Exception in statistics_db: " << e.what() << std::endl;
+    }
+}
+
+/**
+ * Writes the ToS distribution into the database.
+ * @param tosDistribution The ToS distribution from class statistics.
+ */
+void statistics_db::writeStatisticsToS(std::unordered_map<ipAddress_tos, int> tosDistribution) {
+    try {
+        db->exec("DROP TABLE IF EXISTS ip_tos");
+        SQLite::Transaction transaction(*db);
+        const char *createTable = "CREATE TABLE ip_tos ("
+                "ipAddress TEXT,"
+                "tosValue INTEGER,"
+                "tosCount INTEGER,"
+                "PRIMARY KEY(ipAddress,tosValue));";
+        db->exec(createTable);
+        SQLite::Statement query(*db, "INSERT INTO ip_tos VALUES (?, ?, ?)");
+        for (auto it = tosDistribution.begin(); it != tosDistribution.end(); ++it) {
+            ipAddress_tos e = it->first;
+            query.bind(1, e.ipAddress);
+            query.bind(2, e.tosValue);
+            query.bind(3, it->second);
+            query.exec();
+            query.reset();
+        }
+        transaction.commit();
+    }
+    catch (std::exception &e) {
+        std::cout << "Exception in statistics_db: " << e.what() << std::endl;
+    }
+}
+
+/**
+ * Writes the window size distribution into the database.
+ * @param winDistribution The window size distribution from class statistics.
+ */
+void statistics_db::writeStatisticsWin(std::unordered_map<ipAddress_win, int> winDistribution) {
+    try {
+        db->exec("DROP TABLE IF EXISTS tcp_win");
+        SQLite::Transaction transaction(*db);
+        const char *createTable = "CREATE TABLE tcp_win ("
+                "ipAddress TEXT,"
+                "winSize INTEGER,"
+                "winCount INTEGER,"
+                "PRIMARY KEY(ipAddress,winSize));";
+        db->exec(createTable);
+        SQLite::Statement query(*db, "INSERT INTO tcp_win VALUES (?, ?, ?)");
+        for (auto it = winDistribution.begin(); it != winDistribution.end(); ++it) {
+            ipAddress_win e = it->first;
+            query.bind(1, e.ipAddress);
+            query.bind(2, e.winSize);
+            query.bind(3, it->second);
+            query.exec();
+            query.reset();
+        }
+        transaction.commit();
+    }
+    catch (std::exception &e) {
+        std::cout << "Exception in statistics_db: " << e.what() << std::endl;
+    }
+}
+
 /**
  * Writes the protocol distribution into the database.
  * @param protocolDistribution The protocol distribution from class statistics.
@@ -224,100 +314,7 @@ void statistics_db::writeStatisticsFile(int packetCount, float captureDuration,
     }
 }
 
-// Aidamr
-/**
- * Writes the MSS distribution into the database.
- * @param mssDistribution The MSS distribution from class statistics.
- */
-void statistics_db::writeStatisticsMss_dist(std::unordered_map<ipAddress_mss, int> mssDistribution) {
-    try {
-        db->exec("DROP TABLE IF EXISTS tcp_mss");
-        SQLite::Transaction transaction(*db);
-        const char *createTable = "CREATE TABLE tcp_mss ("
-                "ipAddress TEXT,"
-                "mssValue INTEGER,"
-                "mssCount INTEGER,"
-                "PRIMARY KEY(ipAddress,mssValue));";
-        db->exec(createTable);
-        SQLite::Statement query(*db, "INSERT INTO tcp_mss VALUES (?, ?, ?)");
-        for (auto it = mssDistribution.begin(); it != mssDistribution.end(); ++it) {
-            ipAddress_mss e = it->first;
-            query.bind(1, e.ipAddress);
-            query.bind(2, e.mssValue);
-            query.bind(3, it->second);
-            query.exec();
-            query.reset();
-        }
-        transaction.commit();
-    }
-    catch (std::exception &e) {
-        std::cout << "Exception in statistics_db: " << e.what() << std::endl;
-    }
-}
-
-// Aidamr
-/**
- * Writes the ToS distribution into the database.
- * @param tosDistribution The ToS distribution from class statistics.
- */
-void statistics_db::writeStatisticsTos_dist(std::unordered_map<ipAddress_tos, int> tosDistribution) {
-    try {
-        db->exec("DROP TABLE IF EXISTS ip_tos");
-        SQLite::Transaction transaction(*db);
-        const char *createTable = "CREATE TABLE ip_tos ("
-                "ipAddress TEXT,"
-                "tosValue INTEGER,"
-                "tosCount INTEGER,"
-                "PRIMARY KEY(ipAddress,tosValue));";
-        db->exec(createTable);
-        SQLite::Statement query(*db, "INSERT INTO ip_tos VALUES (?, ?, ?)");
-        for (auto it = tosDistribution.begin(); it != tosDistribution.end(); ++it) {
-            ipAddress_tos e = it->first;
-            query.bind(1, e.ipAddress);
-            query.bind(2, e.tosValue);
-            query.bind(3, it->second);
-            query.exec();
-            query.reset();
-        }
-        transaction.commit();
-    }
-    catch (std::exception &e) {
-        std::cout << "Exception in statistics_db: " << e.what() << std::endl;
-    }
-}
-
-// Aidamr
-/**
- * Writes the window size distribution into the database.
- * @param winDistribution The window size distribution from class statistics.
- */
-void statistics_db::writeStatisticsWin(std::unordered_map<ipAddress_win, int> winDistribution) {
-    try {
-        db->exec("DROP TABLE IF EXISTS tcp_win");
-        SQLite::Transaction transaction(*db);
-        const char *createTable = "CREATE TABLE tcp_win ("
-                "ipAddress TEXT,"
-                "winSize INTEGER,"
-                "winCount INTEGER,"
-                "PRIMARY KEY(ipAddress,winSize));";
-        db->exec(createTable);
-        SQLite::Statement query(*db, "INSERT INTO tcp_win VALUES (?, ?, ?)");
-        for (auto it = winDistribution.begin(); it != winDistribution.end(); ++it) {
-            ipAddress_win e = it->first;
-            query.bind(1, e.ipAddress);
-            query.bind(2, e.winSize);
-            query.bind(3, it->second);
-            query.exec();
-            query.reset();
-        }
-        transaction.commit();
-    }
-    catch (std::exception &e) {
-        std::cout << "Exception in statistics_db: " << e.what() << std::endl;
-    }
-}
 
-// Aidamr
 /**
  * Writes the conversation statistics into the database.
  * @param convStatistics The conversation from class statistics.
@@ -340,6 +337,7 @@ void statistics_db::writeStatisticsConv(std::unordered_map<conv, entry_convStat>
         db->exec(createTable);
         SQLite::Statement query(*db, "INSERT INTO conv_statistics VALUES (?, ?, ?, ?, ?,  ?, ?, ?, ?)");
 
+        // Calculate average of inter-arrival times and average packet rate
         for (auto it = convStatistics.begin(); it != convStatistics.end(); ++it) {
             conv f = it->first;
             entry_convStat e = it->second;
@@ -358,15 +356,6 @@ void statistics_db::writeStatisticsConv(std::unordered_map<conv, entry_convStat>
                     e.avg_interarrival_time = (std::chrono::microseconds) sumDelay / e.interarrival_time.size(); // average
                 else e.avg_interarrival_time = (std::chrono::microseconds) 0;
 
-                // Calculate the variance
-                long temp = 0;
-                for (int i = 0; (unsigned) i < e.interarrival_time.size(); i++) {
-                    long del = e.interarrival_time[i].count();
-                    long avg = e.avg_interarrival_time.count();
-                    temp += (del - avg) * (del - avg);
-                }
-                long standardDeviation = sqrt(temp / e.interarrival_time.size());
-
                 std::chrono::microseconds start_timesttamp = e.pkts_timestamp[0];
                 std::chrono::microseconds end_timesttamp = e.pkts_timestamp.back();
                 std::chrono::microseconds conn_duration = end_timesttamp - start_timesttamp;
@@ -392,7 +381,6 @@ void statistics_db::writeStatisticsConv(std::unordered_map<conv, entry_convStat>
     }
 }
 
-// Aidamr
 /**
  * Writes the interval statistics into the database.
  * @param intervalStatistics The interval entries from class statistics.

+ 9 - 11
code_boost/src/cxx/statistics_db.h

@@ -1,14 +1,12 @@
 /**
- *
+ * Class writing the statistics to the database.
  */
 
 #ifndef CPP_PCAPREADER_STATISTICSDB_H
 #define CPP_PCAPREADER_STATISTICSDB_H
 
-// Aidmar
 #include <iostream>
-#include <memory> 
-
+#include <memory>
 #include <string>
 #include "statistics.h"
 #include <SQLiteCpp/SQLiteCpp.h>
@@ -27,24 +25,24 @@ public:
 
     void writeStatisticsTTL(std::unordered_map<ipAddress_ttl, int> ttlDistribution);
 
+    void writeStatisticsMSS(std::unordered_map<ipAddress_mss, int> mssDistribution);
+
+    void writeStatisticsToS(std::unordered_map<ipAddress_tos, int> tosDistribution);
+
+    void writeStatisticsWin(std::unordered_map<ipAddress_win, int> winDistribution);
+
     void writeStatisticsProtocols(std::unordered_map<ipAddress_protocol, int> protocolDistribution);
 
     void writeStatisticsPorts(std::unordered_map<ipAddress_inOut_port, int> portsStatistics);
 
     void writeStatisticsIpMac(std::unordered_map<std::string, std::string> IpMacStatistics);
 
-    // Aidmar - comment out
-    //void writeStatisticsMss(std::unordered_map<std::string, int> mssStatistics);
-
     void writeStatisticsFile(int packetCount, float captureDuration, std::string timestampFirstPkt,
                              std::string timestampLastPkt, float avgPacketRate, float avgPacketSize,
                              float avgPacketsSentPerHost, float avgBandwidthIn, float avgBandwidthOut);
 
-    // Aidmar - new tables
-    void writeStatisticsMss_dist(std::unordered_map<ipAddress_mss, int> mssDistribution);
-    void writeStatisticsTos_dist(std::unordered_map<ipAddress_tos, int> tosDistribution);
-    void writeStatisticsWin(std::unordered_map<ipAddress_win, int> winDistribution);
     void writeStatisticsConv(std::unordered_map<conv, entry_convStat> convStatistics);
+
     void writeStatisticsInterval(std::unordered_map<std::string, entry_intervalStat> intervalStatistics);
 
 private:

+ 24 - 15
code_boost/src/cxx/utilities.cpp

@@ -1,5 +1,3 @@
-// Created by Aidmar
-
 #include "utilities.h"
 
 using namespace Tins;
@@ -13,9 +11,9 @@ std::string integral_to_binary_string(T byte)
 
 /**
  * Split a string.
- * @param str string to be splitted 
- * @param delimiter delimiter to use in splitting
- * @return vector of substrings
+ * @param str string to be splitted.
+ * @param delimiter delimiter to use in splitting.
+ * @return vector of substrings.
  */
 void split_str(const std::string& s, char delim,std::vector<std::string>& v) {
     auto i = 0;
@@ -33,7 +31,7 @@ void split_str(const std::string& s, char delim,std::vector<std::string>& v) {
 
 /**
  * Get the class (A,B,C,D,E) of IP address.
- * @param ipAddress IP that we get its class
+ * @param ipAddress IP that we get its class.
  */
 std::string getIPv4Class(std::string ipAddress){
     std::string ipClass="Unknown";
@@ -77,9 +75,9 @@ std::string getIPv4Class(std::string ipAddress){
 }
 
 /**
- * Get closest index for element in vector.
- * @param v vector
- * @param refElem element that we search for or for closest element
+ * Get closest index for element in a vector.
+ * @param v vector.
+ * @param refElem element that we search for or for closest element.
  */
 int getClosestIndex(std::vector<std::chrono::microseconds> v, std::chrono::microseconds refElem)
 {
@@ -91,14 +89,18 @@ int getClosestIndex(std::vector<std::chrono::microseconds> v, std::chrono::micro
 }
 
 /**
- * Advance iterator by 100 steps.
- * @param iterator to advance
+ * Advance iterator by 10 steps.
+ * @param iterator to advance.
  */
 void snifferIteratorIncrement(Tins::SnifferIterator& iterator){
     (((((((((iterator++)++)++)++)++)++)++)++)++)++;  
 }
 
-
+/**
+ * Convert IP address from string to array of bytes.
+ * @param IP to convert.
+ * @param IP_bytes to be filled and retrieved.
+ */
 void convertIPv4toArray(std::string IP, unsigned short IP_bytes[]){
     std::vector<std::string> temp_v;
     split_str(IP,'.',temp_v);
@@ -108,7 +110,15 @@ void convertIPv4toArray(std::string IP, unsigned short IP_bytes[]){
     IP_bytes[3] = std::stoi(temp_v[3]);
 }
 
-//Calculate TCP checksum
+/**
+ * Calculate TCP checksum.
+ * @param len_tcp TCP packet length
+ * @param src_addr source IP address
+ * @param dest_addr destination IP address
+ * @param padding
+ * @param buff
+ * @return checksum.
+ */
 u16 tcp_sum_calc(u16 len_tcp, u16 src_addr[],u16 dest_addr[], bool padding, u16 buff[])
 {
     u16 prot_tcp=6;
@@ -189,9 +199,8 @@ bool check_tcpChecksum(std::string ipAddressSender, std::string ipAddressReceive
     convertIPv4toArray(ipAddressSender, ipAddressSender_bytes);
     convertIPv4toArray(ipAddressReceiver, ipAddressReceiver_bytes);
 
-    //tcp_sum_calc(unsigned short len_tcp, unsigned short src_addr[],unsigned short dest_addr[], bool padding, unsigned short buff[])
     bool padding = false;
-    int dataSize = bufferArray_8.size() - headerSize;  // TO-DO: why don't you use pkt.size()
+    int dataSize = bufferArray_8.size() - headerSize;
     if(dataSize != 0)
         if(dataSize % 2 != 0)
             padding = true; // padding if the data size is odd

+ 13 - 2
code_boost/src/cxx/utilities.h

@@ -1,3 +1,7 @@
+/*
+ * Class providing utilities functions.
+ */
+
 #ifndef UTILITIES_H
 #define UTILITIES_H
 
@@ -16,12 +20,19 @@ typedef unsigned long u32;
 
 using namespace Tins;
 
-void split_str(const std::string& s, char delim,std::vector<std::string>& v);
+
 std::string getIPv4Class(std::string ipAddress);
-int getClosestIndex(std::vector<std::chrono::microseconds> v, std::chrono::microseconds refElem);
+
 void snifferIteratorIncrement(Tins::SnifferIterator& iterator);
+
 void convertIPv4toArray(std::string IP, unsigned short IP_bytes[]);
+
+void split_str(const std::string& s, char delim,std::vector<std::string>& v);
+
+int getClosestIndex(std::vector<std::chrono::microseconds> v, std::chrono::microseconds refElem);
+
 u16 tcp_sum_calc(u16 len_tcp, u16 src_addr[],u16 dest_addr[], bool padding, u16 buff[]);
+
 bool check_tcpChecksum(std::string ipAddressSender, std::string ipAddressReceiver, TCP tcpPkt);
 
 template<class T>