Browse Source

add tcp_mss_dist table

aidmar.wainakh 7 years ago
parent
commit
99d8aadf4c

+ 6 - 1
code_boost/src/cxx/pcap_processor.cpp

@@ -159,7 +159,7 @@ void pcap_processor::process_packets(const Packet &pkt) {
         stats.addIpStat_packetSent(ipAddressSender, ipLayer.dst_addr().to_string(), sizeCurrentPacket);
 
         // TTL distribution
-        stats.incrementTTLcount(ipAddressSender, ipLayer.ttl());
+        stats.incrementTTLcount(ipAddressSender, ipLayer.ttl());      
 
         // Protocol distribution
         stats.incrementProtocolCount(ipAddressSender, "IPv4");
@@ -202,6 +202,11 @@ void pcap_processor::process_packets(const Packet &pkt) {
             try {
                 int val = tcpPkt.mss();
                 stats.addMSS(ipAddressSender, val);
+                
+                // Aidmar
+                // MSS distribution
+                stats.incrementMSScount(ipAddressSender, val);
+
             } catch (Tins::option_not_found) {
                 // Ignore MSS if option not set
             }

+ 2 - 0
code_boost/src/cxx/pcap_processor.h

@@ -5,6 +5,8 @@
 #ifndef CPP_PCAPREADER_MAIN_H
 #define CPP_PCAPREADER_MAIN_H
 
+// Aidmar
+#include <iomanip>
 
 #include <tins/tins.h>
 #include <iostream>

+ 15 - 0
code_boost/src/cxx/statistics.cpp

@@ -1,8 +1,21 @@
+// Aidmar
+#include <iostream>
+
 #include "statistics.h"
 #include <sstream>
 #include <SQLiteCpp/SQLiteCpp.h>
 #include "statistics_db.h"
 
+// Aidmar
+/**
+ * Increments the packet counter for the given IP address and MSS value.
+ * @param ipAddress The IP address whose MSS packet counter should be incremented.
+ * @param mssValue The MSS value of the packet.
+ */
+void statistics::incrementMSScount(std::string ipAddress, int mssValue) {
+    mss_distribution[{ipAddress, mssValue}]++;
+}
+
 /**
  * Increments the packet counter for the given IP address and TTL value.
  * @param ipAddress The IP address whose TTL packet counter should be incremented.
@@ -256,6 +269,8 @@ void statistics::writeToDatabase(std::string database_path) {
     db.writeStatisticsMss(ip_sumMss);
     db.writeStatisticsPorts(ip_ports);
     db.writeStatisticsProtocols(protocol_distribution);
+    // Aidmar
+    db.writeStatisticsMss_dist(mss_distribution);
 }
 
 /**

+ 36 - 0
code_boost/src/cxx/statistics.h

@@ -35,6 +35,23 @@ struct ip_stats {
     long AvgMaxSegmentSizeTCP;
 };
 
+// Aidmar
+/*
+ * Struct used to represent:
+ * - IP address (IPv4 or IPv6)
+ * - MSS value
+ */
+struct ipAddress_mss {
+    std::string ipAddress;
+    int mssValue;
+
+    bool operator==(const ipAddress_mss &other) const {
+        return ipAddress == other.ipAddress
+               && mssValue == other.mssValue;
+    }
+};
+
+
 /*
  * Struct used to represent:
  * - IP address (IPv4 or IPv6)
@@ -120,6 +137,18 @@ namespace std {
         }
     };
 
+    // Aidmar
+      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_protocol> {
         std::size_t operator()(const ipAddress_protocol &k) const {
@@ -161,6 +190,9 @@ public:
     */
     void incrementPacketCount();
 
+    // Adimar
+    void incrementMSScount(std::string ipAddress, int mssValue);
+
     void incrementTTLcount(std::string ipAddress, int ttlValue);
 
     void incrementProtocolCount(std::string ipAddress, std::string protocol);
@@ -213,6 +245,10 @@ private:
     // {IP Address, TTL value, count}
     std::unordered_map<ipAddress_ttl, int> ttl_distribution;
 
+    // Aidmar
+    // {IP Address, MSS value, count}
+    std::unordered_map<ipAddress_mss, int> mss_distribution;
+
     // {IP Address, Protocol, count}
     std::unordered_map<ipAddress_protocol, int> protocol_distribution;
 

+ 31 - 0
code_boost/src/cxx/statistics_db.cpp

@@ -240,3 +240,34 @@ 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_dist");
+        SQLite::Transaction transaction(*db);
+        const char *createTable = "CREATE TABLE tcp_mss_dist ("
+                "ipAddress TEXT,"
+                "mssValue INTEGER,"
+                "mssCount INTEGER,"
+                "PRIMARY KEY(ipAddress,mssValue));";
+        db->exec(createTable);
+        SQLite::Statement query(*db, "INSERT INTO tcp_mss_dist 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;
+    }
+}
+

+ 6 - 0
code_boost/src/cxx/statistics_db.h

@@ -5,6 +5,9 @@
 #ifndef CPP_PCAPREADER_STATISTICSDB_H
 #define CPP_PCAPREADER_STATISTICSDB_H
 
+// Aidmar
+#include <iostream>
+#include <memory> 
 
 #include <string>
 #include "statistics.h"
@@ -36,6 +39,9 @@ public:
                              std::string timestampLastPkt, float avgPacketRate, float avgPacketSize,
                              float avgPacketsSentPerHost, float avgBandwidthIn, float avgBandwidthOut);
 
+    // Aidmar
+    void writeStatisticsMss_dist(std::unordered_map<ipAddress_mss, int> mssDistribution);
+
 private:
     // Pointer to the SQLite database
     std::unique_ptr<SQLite::Database> db;