Browse Source

WIP: Crash as fast as never before! (Also, it compiles now)

Stefan Schmidt 6 years ago
parent
commit
c5e5479836
2 changed files with 58 additions and 36 deletions
  1. 48 26
      code_boost/src/cxx/pcap_processor.cpp
  2. 10 10
      code_boost/src/cxx/statistics.h

+ 48 - 26
code_boost/src/cxx/pcap_processor.cpp

@@ -1,4 +1,6 @@
 #include "pcap_processor.h"
+#include <mutex>
+#include <thread>
 
 using namespace Tins;
 
@@ -159,7 +161,7 @@ void pcap_processor::collect_statistics() {
         std::cout << "Loading pcap..." << std::endl;
         FileSniffer sniffer(filePath);
 
-        SnifferIterator i = sniffer.begin();
+        SnifferIterator it = sniffer.begin();
         std::chrono::microseconds currentPktTimestamp;
 
         // Read PCAP file info
@@ -185,31 +187,51 @@ void pcap_processor::collect_statistics() {
         std::chrono::system_clock::time_point lastPrinted = std::chrono::system_clock::now();
 
         // Iterate over all packets and collect statistics
-        for (; i != sniffer.end(); i++) {
-            currentPktTimestamp = i->timestamp();
-            std::chrono::microseconds currentDuration = currentPktTimestamp - firstTimestamp;
-
-            // For each interval
-            if(currentDuration>barrier){
-                stats.addIntervalStat(timeInterval, intervalStartTimestamp, currentPktTimestamp);
-                timeIntervalCounter++;
-
-                barrier =  barrier + timeInterval;
-                intervalStartTimestamp = currentPktTimestamp;
-            }
-
-            stats.incrementPacketCount();
-            this->process_packets(*i);
-
-            // Indicate progress once every second
-            if (std::chrono::system_clock::now() - lastPrinted >= std::chrono::seconds(1)) {
-                int packetCount = stats.getPacketCount();
-                std::cout << "\rInspected packets: ";
-                std::cout << std::fixed << std::setprecision(1) << (static_cast<float>(packetCount)*100/totalPackets) << "%";
-                std::cout << " (" << packetCount << "/" << totalPackets << ")" << std::flush;
-                lastPrinted = std::chrono::system_clock::now();
-            }
-        }
+        //for (; i != sniffer.end(); i++) {
+        std::vector<std::thread> threads;
+        
+        std::mutex it_mutex;
+        
+        for (std::size_t i=0; i < std::thread::hardware_concurrency(); ++i) {
+			threads.push_back(std::thread([&]() {
+				while (true) {
+					// lock this!
+					SnifferIterator i;
+					it_mutex.lock();
+					i = it++;
+					it_mutex.unlock();
+					if (i == sniffer.end()) break;
+					
+					currentPktTimestamp = i->timestamp();
+					std::chrono::microseconds currentDuration = currentPktTimestamp - firstTimestamp;
+
+					// For each interval
+					if(currentDuration>barrier){
+						stats.addIntervalStat(timeInterval, intervalStartTimestamp, currentPktTimestamp);
+						timeIntervalCounter++;
+
+						barrier =  barrier + timeInterval;
+						intervalStartTimestamp = currentPktTimestamp;
+					}
+
+					stats.incrementPacketCount();
+					this->process_packets(*i);
+
+					// Indicate progress once every second
+					if (std::chrono::system_clock::now() - lastPrinted >= std::chrono::seconds(1)) {
+						int packetCount = stats.getPacketCount();
+						std::cout << "\rInspected packets: ";
+						std::cout << std::fixed << std::setprecision(1) << (static_cast<float>(packetCount)*100/totalPackets) << "%";
+						std::cout << " (" << packetCount << "/" << totalPackets << ")" << std::flush;
+						lastPrinted = std::chrono::system_clock::now();
+					}
+				}
+			}));
+		}
+		
+		for(auto &t: threads)
+			t.join();
+        
 
         std::cout << "\rInspected packets: ";
         std::cout << "100.0% (" << totalPackets << "/" << totalPackets << ")" << std::endl;

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

@@ -403,9 +403,9 @@ struct unrecognized_PDU_stat {
 /*
  * Definition of hash functions for structs used as key in unordered_map
  */
-namespace std {
+namespace tbb {
     template<>
-    struct hash<ipAddress_ttl> {
+    struct tbb_hash<ipAddress_ttl> {
         std::size_t operator()(const ipAddress_ttl &k) const {
             using std::size_t;
             using std::hash;
@@ -416,7 +416,7 @@ namespace std {
     };
 
       template<>
-    struct hash<ipAddress_mss> {
+    struct tbb_hash<ipAddress_mss> {
         std::size_t operator()(const ipAddress_mss &k) const {
             using std::size_t;
             using std::hash;
@@ -427,7 +427,7 @@ namespace std {
     };
 
     template<>
-    struct hash<ipAddress_tos> {
+    struct tbb_hash<ipAddress_tos> {
         std::size_t operator()(const ipAddress_tos &k) const {
             using std::size_t;
             using std::hash;
@@ -438,7 +438,7 @@ namespace std {
     };
 
       template<>
-    struct hash<ipAddress_win> {
+    struct tbb_hash<ipAddress_win> {
         std::size_t operator()(const ipAddress_win &k) const {
             using std::size_t;
             using std::hash;
@@ -449,7 +449,7 @@ namespace std {
     };
     
     template<>
-    struct hash<conv> {
+    struct tbb_hash<conv> {
         std::size_t operator()(const conv &k) const {
             using std::size_t;
             using std::hash;
@@ -462,7 +462,7 @@ namespace std {
     };
 
     template<>
-    struct hash<convWithProt> {
+    struct tbb_hash<convWithProt> {
         std::size_t operator()(const convWithProt &c) const {
             using std::size_t;
             using std::hash;
@@ -476,7 +476,7 @@ namespace std {
     };
     
     template<>
-    struct hash<ipAddress_protocol> {
+    struct tbb_hash<ipAddress_protocol> {
         std::size_t operator()(const ipAddress_protocol &k) const {
             using std::size_t;
             using std::hash;
@@ -487,7 +487,7 @@ namespace std {
     };
 
     template<>
-    struct hash<ipAddress_inOut_port> {
+    struct tbb_hash<ipAddress_inOut_port> {
         std::size_t operator()(const ipAddress_inOut_port &k) const {
             using std::size_t;
             using std::hash;
@@ -499,7 +499,7 @@ namespace std {
     };
 
     template<>
-    struct hash<unrecognized_PDU> {
+    struct tbb_hash<unrecognized_PDU> {
         std::size_t operator()(const unrecognized_PDU &k) const {
             using std::size_t;
             using std::hash;