3
0
فهرست منبع

draft code for calculating entropy

aidmar.wainakh 7 سال پیش
والد
کامیت
509f16b328
3فایلهای تغییر یافته به همراه39 افزوده شده و 2 حذف شده
  1. 10 1
      code_boost/src/cxx/pcap_processor.cpp
  2. 28 0
      code_boost/src/cxx/statistics.cpp
  3. 1 1
      code_boost/src/cxx/statistics.h

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

@@ -112,12 +112,21 @@ void pcap_processor::collect_statistics() {
 
         // Save timestamp of first packet
         stats.setTimestampFirstPacket(i->timestamp());
-
+    
+        int counter=0;
         // Iterate over all packets and collect statistics
         for (; i != sniffer.end(); i++) {
+            
+            // Aidmar
+            if(counter%100==0){
+                stats.addIPEntropy();
+            }
+                        
             stats.incrementPacketCount();
             this->process_packets(*i);
             lastProcessedPacket = i->timestamp();
+            
+            counter++;
         }
         // Save timestamp of last packet into statistics
         stats.setTimestampLastPacket(lastProcessedPacket);

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

@@ -1,11 +1,39 @@
 // Aidmar
 #include <iostream>
+#include <vector>
+#include <math.h> 
 
 #include "statistics.h"
 #include <sstream>
 #include <SQLiteCpp/SQLiteCpp.h>
 #include "statistics_db.h"
 
+// Aidmar
+void statistics::addIPEntropy(){
+    std::vector <std::string> IPs; 
+    std::vector <float> IPsSrcProb; 
+    std::vector <float> IPsDstProb;
+    for (auto i = ip_statistics.begin(); i != ip_statistics.end(); i++) {
+        IPs.push_back(i->first);
+        IPsSrcProb.push_back((float)i->second.pkts_sent/packetCount);
+        IPsDstProb.push_back((float)i->second.pkts_received/packetCount);
+    }
+    
+    float IPsSrcEntropy = 0;
+    for(unsigned i=0; i < IPsSrcProb.size();i++){
+        if (IPsSrcProb[i] > 0)
+            IPsSrcEntropy += - IPsSrcProb[i]*log2(IPsSrcProb[i]);
+    }
+    std::cout << "SrcEnt: " << IPsSrcEntropy << "\n";
+    
+    float IPsDstEntropy = 0;
+    for(unsigned i=0; i < IPsDstProb.size();i++){
+        if (IPsDstProb[i] > 0)
+            IPsDstEntropy += - IPsDstProb[i]*log2(IPsDstProb[i]);
+    }
+    std::cout << "DstEnt: " << IPsDstEntropy << "\n";
+}
+
 // Aidmar
 /**
  * Increments the packet counter for the given IP address and MSS value.

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

@@ -221,7 +221,7 @@ public:
     // Adimar
     void incrementMSScount(std::string ipAddress, int mssValue);
     void incrementWinCount(std::string ipAddress, int winSize);
-
+    void addIPEntropy();
 
     void incrementTTLcount(std::string ipAddress, int ttlValue);