Bläddra i källkod

Merge branch 'stat_perf' of stefan.schmidt/ID2T-toolkit into master

Jens Keim 6 år sedan
förälder
incheckning
7c8d75829d

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

@@ -83,7 +83,7 @@ std::string pcap_processor::merge_pcaps(const std::string pcap_path) {
         if (!all_attack_pkts_processed && tstmp_attack <= tstmp_base) {
             try {
                 writer.write(*iterator_attack);
-            } catch (serialization_error) {
+            } catch (serialization_error&) {
                 std::cout << std::setprecision(15) << "Could not serialize attack packet with timestamp " << tstmp_attack << std::endl;
             }
             iterator_attack++;
@@ -92,7 +92,7 @@ std::string pcap_processor::merge_pcaps(const std::string pcap_path) {
         } else {
             try {
                 writer.write(*iterator_base);
-            } catch (serialization_error) {
+            } catch (serialization_error&) {
                     std::cout << "Could not serialize base packet with timestamp " << std::setprecision(15) << tstmp_attack << std::endl;
             }
             iterator_base++;
@@ -104,7 +104,7 @@ std::string pcap_processor::merge_pcaps(const std::string pcap_path) {
     for (; iterator_attack != sniffer_attack.end(); iterator_attack++) {
         try {
             writer.write(*iterator_attack);
-        } catch (serialization_error) {
+        } catch (serialization_error&) {
             auto tstmp_attack = (iterator_attack->timestamp().seconds()) + (iterator_attack->timestamp().microseconds()*1e-6);
             std::cout << "Could not serialize attack packet with timestamp " << std::setprecision(15) << tstmp_attack << std::endl;
         }
@@ -204,7 +204,7 @@ void pcap_processor::process_packets(const Packet &pkt) {
     const PDU *pdu_l2 = pkt.pdu();
     uint32_t sizeCurrentPacket = pdu_l2->size();
     if (pdu_l2->pdu_type() == PDU::ETHERNET_II) {
-        EthernetII eth = (const EthernetII &) *pdu_l2;
+        const EthernetII &eth = (const EthernetII &) *pdu_l2;
         macAddressSender = eth.src_addr().to_string();
         macAddressReceiver = eth.dst_addr().to_string();
         sizeCurrentPacket = eth.size();
@@ -225,7 +225,7 @@ void pcap_processor::process_packets(const Packet &pkt) {
         ipAddressReceiver = ipLayer.dst_addr().to_string();
 
         // IP distribution
-        stats.addIpStat_packetSent(filePath, ipAddressSender, ipLayer.dst_addr().to_string(), sizeCurrentPacket, pkt.timestamp());
+        stats.addIpStat_packetSent(ipAddressSender, ipLayer.dst_addr().to_string(), sizeCurrentPacket, pkt.timestamp());
 
         // TTL distribution
         stats.incrementTTLcount(ipAddressSender, ipLayer.ttl());
@@ -248,7 +248,7 @@ void pcap_processor::process_packets(const Packet &pkt) {
         ipAddressReceiver = ipLayer.dst_addr().to_string();
 
         // IP distribution
-        stats.addIpStat_packetSent(filePath, ipAddressSender, ipLayer.dst_addr().to_string(), sizeCurrentPacket, pkt.timestamp());
+        stats.addIpStat_packetSent(ipAddressSender, ipLayer.dst_addr().to_string(), sizeCurrentPacket, pkt.timestamp());
 
         // TTL distribution
         stats.incrementTTLcount(ipAddressSender, ipLayer.hop_limit());
@@ -264,7 +264,7 @@ void pcap_processor::process_packets(const Packet &pkt) {
     else {
         hasUnrecognized = true;
 
-        EthernetII eth = (const EthernetII &) *pdu_l2;
+        const EthernetII &eth = (const EthernetII &) *pdu_l2;
         Tins::Timestamp ts = pkt.timestamp();
         std::string timestamp_pkt = stats.getFormattedTimestamp(ts.seconds(), ts.microseconds());
 
@@ -283,7 +283,7 @@ void pcap_processor::process_packets(const Packet &pkt) {
           }
 
         if (p == PDU::PDUType::TCP) {
-            TCP tcpPkt = (const TCP &) *pdu_l4;
+            const TCP &tcpPkt = (const TCP &) *pdu_l4;
             
             // Check TCP checksum
             if (pdu_l3_type == PDU::PDUType::IP) {
@@ -306,7 +306,7 @@ void pcap_processor::process_packets(const Packet &pkt) {
 
                 // MSS distribution
                 stats.incrementMSScount(ipAddressSender, val);
-            } catch (Tins::option_not_found) {
+            } catch (Tins::option_not_found&) {
                 // Ignore MSS if option not set
             }
             stats.incrementPortCount(ipAddressSender, tcpPkt.sport(), ipAddressReceiver, tcpPkt.dport(), "TCP");
@@ -314,7 +314,7 @@ void pcap_processor::process_packets(const Packet &pkt) {
 
           // UDP Packet
         } else if (p == PDU::PDUType::UDP) {
-            const UDP udpPkt = (const UDP &) *pdu_l4;
+            const UDP &udpPkt = (const UDP &) *pdu_l4;
             stats.incrementProtocolCount(ipAddressSender, "UDP");
             stats.increaseProtocolByteCount(ipAddressSender, "UDP", sizeCurrentPacket);
             stats.incrementPortCount(ipAddressSender, udpPkt.sport(), ipAddressReceiver, udpPkt.dport(), "UDP");

+ 80 - 76
code_boost/src/cxx/statistics.cpp

@@ -32,7 +32,7 @@ void statistics::checkPayload(const PDU *pdu_l4) {
  * @param ipAddressReceiver The destination IP.
  * @param tcpPkt The packet to get checked.
  */
-void statistics::checkTCPChecksum(std::string ipAddressSender, std::string ipAddressReceiver, TCP tcpPkt) {
+void statistics::checkTCPChecksum(const std::string &ipAddressSender, const std::string &ipAddressReceiver, TCP tcpPkt) {
     if(this->getDoExtraTests()) {
         if(check_tcpChecksum(ipAddressSender, ipAddressReceiver, tcpPkt))
             correctTCPChecksumCount++;
@@ -226,7 +226,7 @@ void statistics::addIntervalStat(std::chrono::duration<int, std::micro> interval
  * @param dport The destination port.
  * @param timestamp The timestamp of the packet.
  */
-void statistics::addConvStat(std::string ipAddressSender,int sport,std::string ipAddressReceiver,int dport, std::chrono::microseconds timestamp){
+void statistics::addConvStat(const std::string &ipAddressSender,int sport,const std::string &ipAddressReceiver,int dport, std::chrono::microseconds timestamp){
 
     conv f1 = {ipAddressReceiver, dport, ipAddressSender, sport};
     conv f2 = {ipAddressSender, sport, ipAddressReceiver, dport};
@@ -259,48 +259,50 @@ void statistics::addConvStat(std::string ipAddressSender,int sport,std::string i
  * @param protocol The used protocol.
  * @param timestamp The timestamp of the packet.
  */
-void statistics::addConvStatExt(std::string ipAddressSender,int sport,std::string ipAddressReceiver,int dport,std::string protocol, std::chrono::microseconds timestamp){
-    convWithProt f1 = {ipAddressReceiver, dport, ipAddressSender, sport, protocol};
-    convWithProt f2 = {ipAddressSender, sport, ipAddressReceiver, dport, protocol};
-    convWithProt f;
-
-    // if there already exists a communication interval for the specified conversation
-    if (conv_statistics_extended.count(f1) > 0 || conv_statistics_extended.count(f2) > 0){
-
-        // find out which direction of conversation is contained in conv_statistics_extended
-        if (conv_statistics_extended.count(f1) > 0)
-            f = f1;
-        else
-            f = f2;
-
-        // increase pkts count and check on delay
-        conv_statistics_extended[f].pkts_count++;
-        if (conv_statistics_extended[f].pkts_timestamp.size()>0 && conv_statistics_extended[f].pkts_count<=3)
-            conv_statistics_extended[f].interarrival_time.push_back(std::chrono::duration_cast<std::chrono::microseconds> (timestamp - conv_statistics_extended[f].pkts_timestamp.back()));
-        conv_statistics_extended[f].pkts_timestamp.push_back(timestamp);
-
-        // if the time difference has exceeded the threshold, create a new interval with this message
-        if (timestamp - conv_statistics_extended[f].comm_intervals.back().end > (std::chrono::microseconds) ((unsigned long) COMM_INTERVAL_THRESHOLD)) {  // > or >= ?
-            commInterval new_interval = {timestamp, timestamp, 1};
-            conv_statistics_extended[f].comm_intervals.push_back(new_interval);
-        }  
-        // otherwise, set the time of the last interval message to the current timestamp and increase interval packet count by 1
+void statistics::addConvStatExt(const std::string &ipAddressSender,int sport,const std::string &ipAddressReceiver,int dport,const std::string &protocol, std::chrono::microseconds timestamp){
+    if(this->getDoExtraTests()) {
+        convWithProt f1 = {ipAddressReceiver, dport, ipAddressSender, sport, protocol};
+        convWithProt f2 = {ipAddressSender, sport, ipAddressReceiver, dport, protocol};
+        convWithProt f;
+
+        // if there already exists a communication interval for the specified conversation
+        if (conv_statistics_extended.count(f1) > 0 || conv_statistics_extended.count(f2) > 0){
+
+            // find out which direction of conversation is contained in conv_statistics_extended
+            if (conv_statistics_extended.count(f1) > 0)
+                f = f1;
+            else
+                f = f2;
+
+            // increase pkts count and check on delay
+            conv_statistics_extended[f].pkts_count++;
+            if (conv_statistics_extended[f].pkts_timestamp.size()>0 && conv_statistics_extended[f].pkts_count<=3)
+                conv_statistics_extended[f].interarrival_time.push_back(std::chrono::duration_cast<std::chrono::microseconds> (timestamp - conv_statistics_extended[f].pkts_timestamp.back()));
+            conv_statistics_extended[f].pkts_timestamp.push_back(timestamp);
+
+            // if the time difference has exceeded the threshold, create a new interval with this message
+            if (timestamp - conv_statistics_extended[f].comm_intervals.back().end > (std::chrono::microseconds) ((unsigned long) COMM_INTERVAL_THRESHOLD)) {  // > or >= ?
+                commInterval new_interval = {timestamp, timestamp, 1};
+                conv_statistics_extended[f].comm_intervals.push_back(new_interval);
+            }
+            // otherwise, set the time of the last interval message to the current timestamp and increase interval packet count by 1
+            else{
+                conv_statistics_extended[f].comm_intervals.back().end = timestamp;
+                conv_statistics_extended[f].comm_intervals.back().pkts_count++;
+            }
+        }
+        // if there does not exist a communication interval for the specified conversation
         else{
-            conv_statistics_extended[f].comm_intervals.back().end = timestamp;
-            conv_statistics_extended[f].comm_intervals.back().pkts_count++;
+            // add initial interval entry for this conversation
+            commInterval initial_interval = {timestamp, timestamp, 1};
+
+            entry_convStatExt entry;
+            entry.comm_intervals.push_back(initial_interval);
+            entry.pkts_count = 1;
+            entry.pkts_timestamp.push_back(timestamp);
+            conv_statistics_extended[f2] = entry;
         }
     }
-    // if there does not exist a communication interval for the specified conversation
-    else{
-        // add initial interval entry for this conversation
-        commInterval initial_interval = {timestamp, timestamp, 1};
-
-        entry_convStatExt entry;
-        entry.comm_intervals.push_back(initial_interval);
-        entry.pkts_count = 1;
-        entry.pkts_timestamp.push_back(timestamp);
-        conv_statistics_extended[f2] = entry;
-    }
 }
 
 /**
@@ -347,7 +349,7 @@ void statistics::createCommIntervalStats(){
  * @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) {
+void statistics::incrementMSScount(const std::string &ipAddress, int mssValue) {
     mss_values[mssValue]++;
     mss_distribution[{ipAddress, mssValue}]++;
 }
@@ -357,7 +359,7 @@ void statistics::incrementMSScount(std::string ipAddress, int mssValue) {
  * @param ipAddress The IP address whose window size packet counter should be incremented.
  * @param winSize The window size of the packet.
  */
-void statistics::incrementWinCount(std::string ipAddress, int winSize) {
+void statistics::incrementWinCount(const std::string &ipAddress, int winSize) {
     win_values[winSize]++;
     win_distribution[{ipAddress, winSize}]++;
 }
@@ -367,7 +369,7 @@ void statistics::incrementWinCount(std::string ipAddress, int winSize) {
  * @param ipAddress The IP address whose TTL packet counter should be incremented.
  * @param ttlValue The TTL value of the packet.
  */
-void statistics::incrementTTLcount(std::string ipAddress, int ttlValue) {
+void statistics::incrementTTLcount(const std::string &ipAddress, int ttlValue) {
     ttl_values[ttlValue]++;
     ttl_distribution[{ipAddress, ttlValue}]++;
 }
@@ -377,7 +379,7 @@ void statistics::incrementTTLcount(std::string ipAddress, int ttlValue) {
  * @param ipAddress The IP address whose ToS packet counter should be incremented.
  * @param tosValue The ToS value of the packet.
  */
-void statistics::incrementToScount(std::string ipAddress, int tosValue) {
+void statistics::incrementToScount(const std::string &ipAddress, int tosValue) {
     tos_values[tosValue]++;
     tos_distribution[{ipAddress, tosValue}]++;
 }
@@ -387,7 +389,7 @@ void statistics::incrementToScount(std::string ipAddress, int tosValue) {
  * @param ipAddress The IP address whose protocol packet counter should be incremented.
  * @param protocol The protocol of the packet.
  */
-void statistics::incrementProtocolCount(std::string ipAddress, std::string protocol) {
+void statistics::incrementProtocolCount(const std::string &ipAddress, const std::string &protocol) {
     protocol_distribution[{ipAddress, protocol}].count++;
 }
 
@@ -396,7 +398,7 @@ void statistics::incrementProtocolCount(std::string ipAddress, std::string proto
  * @param ipAddress The IP address whose packet count is wanted.
  * @param protocol The protocol whose packet count is wanted.
  */
-int statistics::getProtocolCount(std::string ipAddress, std::string protocol) {
+int statistics::getProtocolCount(const std::string &ipAddress, const std::string &protocol) {
     return protocol_distribution[{ipAddress, protocol}].count;
 }
 
@@ -406,7 +408,7 @@ int statistics::getProtocolCount(std::string ipAddress, std::string protocol) {
  * @param protocol The protocol of the packet.
  * @param byteSent The packet's size.
  */
-void statistics::increaseProtocolByteCount(std::string ipAddress, std::string protocol, long bytesSent) {
+void statistics::increaseProtocolByteCount(const std::string &ipAddress, const std::string &protocol, long bytesSent) {
     protocol_distribution[{ipAddress, protocol}].byteCount += bytesSent;
 }
 
@@ -416,7 +418,7 @@ void statistics::increaseProtocolByteCount(std::string ipAddress, std::string pr
  * @param protocol The protocol whose byte count is wanted.
  * @return a float: The number of bytes
  */
-float statistics::getProtocolByteCount(std::string ipAddress, std::string protocol) {
+float statistics::getProtocolByteCount(const std::string &ipAddress, const std::string &protocol) {
     return protocol_distribution[{ipAddress, protocol}].byteCount;
 }
 
@@ -429,8 +431,8 @@ float statistics::getProtocolByteCount(std::string ipAddress, std::string protoc
  * @param ipAddressReceiver The IP address of the packet receiver.
  * @param incomingPort The port used by the receiver.
  */
-void statistics::incrementPortCount(std::string ipAddressSender, int outgoingPort, std::string ipAddressReceiver,
-                                    int incomingPort, std::string protocol) {
+void statistics::incrementPortCount(const std::string &ipAddressSender, int outgoingPort, const std::string &ipAddressReceiver,
+                                    int incomingPort, const std::string &protocol) {
     port_values[outgoingPort]++;
     port_values[incomingPort]++;
     ip_ports[{ipAddressSender, "out", outgoingPort, protocol}].count++;
@@ -447,8 +449,8 @@ void statistics::incrementPortCount(std::string ipAddressSender, int outgoingPor
  * @param incomingPort The port used by the receiver.
  * @param byteSent The packet's size.
  */
-void statistics::increasePortByteCount(std::string ipAddressSender, int outgoingPort, std::string ipAddressReceiver,
-                                       int incomingPort, long bytesSent, std::string protocol) {
+void statistics::increasePortByteCount(const std::string &ipAddressSender, int outgoingPort, const std::string &ipAddressReceiver,
+                                       int incomingPort, long bytesSent, const std::string &protocol) {
     ip_ports[{ipAddressSender, "out", outgoingPort, protocol}].byteCount += bytesSent;
     ip_ports[{ipAddressReceiver, "in", incomingPort, protocol}].byteCount += bytesSent;
 }
@@ -461,8 +463,8 @@ void statistics::increasePortByteCount(std::string ipAddressSender, int outgoing
  * @param dstMac The MAC address of the packet receiver.
  * @param typeNumber The payload type number of the packet.
  */
-void statistics::incrementUnrecognizedPDUCount(std::string srcMac, std::string dstMac, uint32_t typeNumber,
-                                               std::string timestamp) {
+void statistics::incrementUnrecognizedPDUCount(const std::string &srcMac, const std::string &dstMac, uint32_t typeNumber,
+                                               const std::string &timestamp) {
     unrecognized_PDUs[{srcMac, dstMac, typeNumber}].count++;
     unrecognized_PDUs[{srcMac, dstMac, typeNumber}].timestamp_last_occurrence = timestamp;
 }
@@ -478,7 +480,7 @@ statistics::statistics(void) {
  * @param ipAddress The IP address belonging to the given MAC address.
  * @param macAddress The MAC address belonging to the given IP address.
  */
-void statistics::assignMacAddress(std::string ipAddress, std::string macAddress) {
+void statistics::assignMacAddress(const std::string &ipAddress, const std::string &macAddress) {
     ip_mac_mapping[ipAddress] = macAddress;
 }
 
@@ -489,7 +491,7 @@ void statistics::assignMacAddress(std::string ipAddress, std::string macAddress)
  * @param ipAddressReceiver The IP address of the packet receiver.
  * @param bytesSent The packet's size.
  */
-void statistics::addIpStat_packetSent(std::string filePath, std::string ipAddressSender, std::string ipAddressReceiver, long bytesSent, std::chrono::microseconds timestamp) {
+void statistics::addIpStat_packetSent(const std::string &ipAddressSender, const std::string &ipAddressReceiver, long bytesSent, std::chrono::microseconds timestamp) {
 
     // Adding IP as a sender for first time
     if(ip_statistics[ipAddressSender].pkts_sent==0){  
@@ -513,22 +515,24 @@ void statistics::addIpStat_packetSent(std::string filePath, std::string ipAddres
     ip_statistics[ipAddressReceiver].pkts_received++;
     ip_statistics[ipAddressReceiver].pkts_received_timestamp.push_back(timestamp);
 
-    // Increment Degrees for sender and receiver, if Sender sends its first packet to this receiver
-    std::vector<std::string>::iterator found_receiver = std::find(contacted_ips[ipAddressSender].begin(), contacted_ips[ipAddressSender].end(), ipAddressReceiver);
-    if(found_receiver == contacted_ips[ipAddressSender].end()){
-        // Receiver is NOT contained in the List of IPs, that the Sender has contacted, therefore this is the first packet in this direction
-        ip_statistics[ipAddressSender].out_degree++;
-        ip_statistics[ipAddressReceiver].in_degree++;
-
-        // Increment overall_degree only if this is the first packet for the connection (both directions)
-        // Therefore check, whether Receiver has contacted Sender before
-        std::vector<std::string>::iterator sender_contacted = std::find(contacted_ips[ipAddressReceiver].begin(), contacted_ips[ipAddressReceiver].end(), ipAddressSender);
-        if(sender_contacted == contacted_ips[ipAddressReceiver].end()){
-            ip_statistics[ipAddressSender].overall_degree++;
-            ip_statistics[ipAddressReceiver].overall_degree++;
-        }  
-
-        contacted_ips[ipAddressSender].push_back(ipAddressReceiver);
+    if(this->getDoExtraTests()) {
+        // Increment Degrees for sender and receiver, if Sender sends its first packet to this receiver
+        std::unordered_set<std::string>::const_iterator found_receiver = contacted_ips[ipAddressSender].find(ipAddressReceiver);
+        if(found_receiver == contacted_ips[ipAddressSender].end()){
+            // Receiver is NOT contained in the List of IPs, that the Sender has contacted, therefore this is the first packet in this direction
+            ip_statistics[ipAddressSender].out_degree++;
+            ip_statistics[ipAddressReceiver].in_degree++;
+
+            // Increment overall_degree only if this is the first packet for the connection (both directions)
+            // Therefore check, whether Receiver has contacted Sender before
+            std::unordered_set<std::string>::const_iterator sender_contacted = contacted_ips[ipAddressReceiver].find(ipAddressSender);
+            if(sender_contacted == contacted_ips[ipAddressReceiver].end()){
+                ip_statistics[ipAddressSender].overall_degree++;
+                ip_statistics[ipAddressReceiver].overall_degree++;
+            }
+
+            contacted_ips[ipAddressSender].insert(ipAddressReceiver);
+        }
     }
 }
 
@@ -668,7 +672,7 @@ std::string statistics::getFormattedTimestamp(time_t seconds, suseconds_t micros
  * @param ipAddress The IP address whose statistics should be calculated.
  * @return a ip_stats struct containing statistical data derived by the statistical data collected.
  */
-ip_stats statistics::getStatsForIP(std::string ipAddress) {
+ip_stats statistics::getStatsForIP(const std::string &ipAddress) {
     float duration = getCaptureDurationSeconds();
     entry_ipStat ipStatEntry = ip_statistics[ipAddress];
 
@@ -693,7 +697,7 @@ void statistics::incrementPacketCount() {
  * Prints the statistics of the PCAP and IP specific statistics for the given IP address.
  * @param ipAddress The IP address whose statistics should be printed. Can be empty "" to print only general file statistics.
  */
-void statistics::printStats(std::string ipAddress) {
+void statistics::printStats(const std::string &ipAddress) {
     std::stringstream ss;
     ss << std::endl;
     ss << "Capture duration: " << getCaptureDurationSeconds() << " seconds" << std::endl;
@@ -752,7 +756,7 @@ void statistics::writeToDatabase(std::string database_path) {
                                getFormattedTimestamp(timestamp_firstPacket.seconds(), timestamp_firstPacket.microseconds()),
                                getFormattedTimestamp(timestamp_lastPacket.seconds(), timestamp_lastPacket.microseconds()),
                                avgPacketRate, avgPacketSize, avgPacketsSentPerHost, avgBandwidthInKBits,
-                               avgBandwidthOutInKBits);
+                               avgBandwidthOutInKBits, doExtraTests);
         db.writeStatisticsIP(ip_statistics);
         db.writeStatisticsTTL(ttl_distribution);
         db.writeStatisticsIpMac(ip_mac_mapping);

+ 24 - 23
code_boost/src/cxx/statistics.h

@@ -6,6 +6,7 @@
 
 #include <vector>
 #include <unordered_map>
+#include <unordered_set>
 #include <list>
 #include <tuple>
 #include <tins/timestamp.h>
@@ -528,13 +529,13 @@ public:
 
     void calculateIPIntervalPacketRate(std::chrono::duration<int, std::micro> interval, std::chrono::microseconds intervalStartTimestamp);
 
-    void incrementMSScount(std::string ipAddress, int mssValue);
+    void incrementMSScount(const std::string &ipAddress, int mssValue);
 
-    void incrementWinCount(std::string ipAddress, int winSize);
+    void incrementWinCount(const std::string &ipAddress, int winSize);
 
-    void addConvStat(std::string ipAddressSender,int sport,std::string ipAddressReceiver,int dport, std::chrono::microseconds timestamp);
+    void addConvStat(const std::string &ipAddressSender,int sport, const std::string &ipAddressReceiver,int dport, std::chrono::microseconds timestamp);
 
-    void addConvStatExt(std::string ipAddressSender,int sport,std::string ipAddressReceiver,int dport,std::string protocol, std::chrono::microseconds timestamp);
+    void addConvStatExt(const std::string &ipAddressSender,int sport, const std::string &ipAddressReceiver,int dport, const std::string &protocol, std::chrono::microseconds timestamp);
 
     void createCommIntervalStats();
 
@@ -546,30 +547,30 @@ public:
 
     void checkPayload(const PDU *pdu_l4);
 
-    void checkTCPChecksum(std::string ipAddressSender, std::string ipAddressReceiver, TCP tcpPkt);
+    void checkTCPChecksum(const std::string &ipAddressSender, const std::string &ipAddressReceiver, TCP tcpPkt);
 
     void checkToS(uint8_t ToS);
 
-    void incrementToScount(std::string ipAddress, int tosValue);
+    void incrementToScount(const std::string &ipAddress, int tosValue);
 
-    void incrementTTLcount(std::string ipAddress, int ttlValue);
+    void incrementTTLcount(const std::string &ipAddress, int ttlValue);
 
-    void incrementProtocolCount(std::string ipAddress, std::string protocol);
+    void incrementProtocolCount(const std::string &ipAddress, const std::string &protocol);
 
-    void increaseProtocolByteCount(std::string ipAddress, std::string protocol, long bytesSent);
+    void increaseProtocolByteCount(const std::string &ipAddress, const std::string &protocol, long bytesSent);
 
-    void incrementUnrecognizedPDUCount(std::string srcMac, std::string dstMac, uint32_t typeNumber,
-                                       std::string timestamp);
+    void incrementUnrecognizedPDUCount(const std::string &srcMac, const std::string &dstMac, uint32_t typeNumber,
+                                       const std::string &timestamp);
 
-    void incrementPortCount(std::string ipAddressSender, int outgoingPort, std::string ipAddressReceiver,
-                            int incomingPort, std::string protocol);
+    void incrementPortCount(const std::string &ipAddressSender, int outgoingPort, const std::string &ipAddressReceiver,
+                            int incomingPort, const std::string &protocol);
 
-    void increasePortByteCount(std::string ipAddressSender, int outgoingPort, std::string ipAddressReceiver,
-                               int incomingPort, long bytesSent, std::string protocol);
+    void increasePortByteCount(const std::string &ipAddressSender, int outgoingPort, const std::string &ipAddressReceiver,
+                               int incomingPort, long bytesSent, const std::string &protocol);
 
-    int getProtocolCount(std::string ipAddress, std::string protocol);
+    int getProtocolCount(const std::string &ipAddress, const std::string &protocol);
 
-    float getProtocolByteCount(std::string ipAddress, std::string protocol);
+    float getProtocolByteCount(const std::string &ipAddress, const std::string &protocol);
 
     void setTimestampFirstPacket(Tins::Timestamp ts);
 
@@ -578,15 +579,15 @@ public:
     Tins::Timestamp getTimestampFirstPacket();
     Tins::Timestamp getTimestampLastPacket();
 
-    void assignMacAddress(std::string ipAddress, std::string macAddress);
+    void assignMacAddress(const std::string &ipAddress, const std::string &macAddress);
     
-    void addIpStat_packetSent(std::string filePath, std::string ipAddressSender, std::string ipAddressReceiver, long bytesSent, std::chrono::microseconds timestamp);
+    void addIpStat_packetSent(const std::string &ipAddressSender, const std::string &ipAddressReceiver, long bytesSent, std::chrono::microseconds timestamp);
 
     int getPacketCount();
 
     int getSumPacketSize();
 
-    void addMSS(std::string ipAddress, int MSSvalue);
+    void addMSS(const std::string &ipAddress, int MSSvalue);
 
     void writeToDatabase(std::string database_path);
 
@@ -598,7 +599,7 @@ public:
 
     float getAvgPacketSize() const;
 
-    void printStats(std::string ipAddress);
+    void printStats(const std::string &ipAddress);
 
     bool getDoExtraTests();
 
@@ -607,7 +608,7 @@ public:
     /*
      * IP Address-specific statistics
      */
-    ip_stats getStatsForIP(std::string ipAddress);
+    ip_stats getStatsForIP(const std::string &ipAddress);
 
 private:
     /*
@@ -690,7 +691,7 @@ private:
 
 
     //{IP Address, contacted IP Addresses}
-    std::unordered_map<std::string, std::vector<std::string>> contacted_ips;
+    std::unordered_map<std::string, std::unordered_set<std::string>> contacted_ips;
 
     // {IP Address, Protocol,  #count, #Data transmitted in bytes}
     std::unordered_map<ipAddress_protocol, entry_protocolStat> protocol_distribution;

+ 77 - 83
code_boost/src/cxx/statistics_db.cpp

@@ -27,7 +27,7 @@ statistics_db::statistics_db(std::string database_path) {
  * Writes the IP statistics into the database.
  * @param ipStatistics The IP statistics from class statistics.
  */
-void statistics_db::writeStatisticsIP(std::unordered_map<std::string, entry_ipStat> ipStatistics) {
+void statistics_db::writeStatisticsIP(const std::unordered_map<std::string, entry_ipStat> &ipStatistics) {
     try {
         db->exec("DROP TABLE IF EXISTS ip_statistics");
         SQLite::Transaction transaction(*db);
@@ -44,15 +44,15 @@ void statistics_db::writeStatisticsIP(std::unordered_map<std::string, entry_ipSt
         db->exec(createTable);
         SQLite::Statement query(*db, "INSERT INTO ip_statistics VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
         for (auto it = ipStatistics.begin(); it != ipStatistics.end(); ++it) {
-            entry_ipStat e = it->second;
-            query.bind(1, it->first);
+            const entry_ipStat &e = it->second;
+            query.bindNoCopy(1, it->first);
             query.bind(2, (int) e.pkts_received);
             query.bind(3, (int) e.pkts_sent);
             query.bind(4, e.kbytes_received);
             query.bind(5, e.kbytes_sent);
             query.bind(6, e.max_interval_pkt_rate);
             query.bind(7, e.min_interval_pkt_rate);
-            query.bind(8, e.ip_class);
+            query.bindNoCopy(8, e.ip_class);
             query.exec();
             query.reset();
         }
@@ -68,7 +68,7 @@ void statistics_db::writeStatisticsIP(std::unordered_map<std::string, entry_ipSt
  * @param ipStatistics The IP statistics from class statistics. Degree Statistics are supposed to be integrated into the ip_statistics table later on,
  *        therefore they use the same parameter. But for now they are inserted into their own table.
  */
-void statistics_db::writeStatisticsDegree(std::unordered_map<std::string, entry_ipStat> ipStatistics){
+void statistics_db::writeStatisticsDegree(const std::unordered_map<std::string, entry_ipStat> &ipStatistics){
     try {
         db->exec("DROP TABLE IF EXISTS ip_degrees");
         SQLite::Transaction transaction(*db);
@@ -81,8 +81,8 @@ void statistics_db::writeStatisticsDegree(std::unordered_map<std::string, entry_
         db->exec(createTable);
         SQLite::Statement query(*db, "INSERT INTO ip_degrees VALUES (?, ?, ?, ?)");
         for (auto it = ipStatistics.begin(); it != ipStatistics.end(); ++it) {
-            entry_ipStat e = it->second;
-            query.bind(1, it->first);
+            const entry_ipStat &e = it->second;
+            query.bindNoCopy(1, it->first);
             query.bind(2, e.in_degree);
             query.bind(3, e.out_degree);
             query.bind(4, e.overall_degree);
@@ -100,7 +100,7 @@ void statistics_db::writeStatisticsDegree(std::unordered_map<std::string, entry_
  * Writes the TTL distribution into the database.
  * @param ttlDistribution The TTL distribution from class statistics.
  */
-void statistics_db::writeStatisticsTTL(std::unordered_map<ipAddress_ttl, int> ttlDistribution) {
+void statistics_db::writeStatisticsTTL(const std::unordered_map<ipAddress_ttl, int> &ttlDistribution) {
     try {
         db->exec("DROP TABLE IF EXISTS ip_ttl");
         SQLite::Transaction transaction(*db);
@@ -112,8 +112,8 @@ void statistics_db::writeStatisticsTTL(std::unordered_map<ipAddress_ttl, int> tt
         db->exec(createTable);
         SQLite::Statement query(*db, "INSERT INTO ip_ttl VALUES (?, ?, ?)");
         for (auto it = ttlDistribution.begin(); it != ttlDistribution.end(); ++it) {
-            ipAddress_ttl e = it->first;
-            query.bind(1, e.ipAddress);
+            const ipAddress_ttl &e = it->first;
+            query.bindNoCopy(1, e.ipAddress);
             query.bind(2, e.ttlValue);
             query.bind(3, it->second);
             query.exec();
@@ -130,7 +130,7 @@ 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) {
+void statistics_db::writeStatisticsMSS(const std::unordered_map<ipAddress_mss, int> &mssDistribution) {
     try {
         db->exec("DROP TABLE IF EXISTS tcp_mss");
         SQLite::Transaction transaction(*db);
@@ -142,8 +142,8 @@ void statistics_db::writeStatisticsMSS(std::unordered_map<ipAddress_mss, int> ms
         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);
+            const ipAddress_mss &e = it->first;
+            query.bindNoCopy(1, e.ipAddress);
             query.bind(2, e.mssValue);
             query.bind(3, it->second);
             query.exec();
@@ -160,7 +160,7 @@ void statistics_db::writeStatisticsMSS(std::unordered_map<ipAddress_mss, int> ms
  * 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) {
+void statistics_db::writeStatisticsToS(const std::unordered_map<ipAddress_tos, int> &tosDistribution) {
     try {
         db->exec("DROP TABLE IF EXISTS ip_tos");
         SQLite::Transaction transaction(*db);
@@ -172,8 +172,8 @@ void statistics_db::writeStatisticsToS(std::unordered_map<ipAddress_tos, int> to
         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);
+            const ipAddress_tos &e = it->first;
+            query.bindNoCopy(1, e.ipAddress);
             query.bind(2, e.tosValue);
             query.bind(3, it->second);
             query.exec();
@@ -190,7 +190,7 @@ void statistics_db::writeStatisticsToS(std::unordered_map<ipAddress_tos, int> to
  * 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) {
+void statistics_db::writeStatisticsWin(const std::unordered_map<ipAddress_win, int> &winDistribution) {
     try {
         db->exec("DROP TABLE IF EXISTS tcp_win");
         SQLite::Transaction transaction(*db);
@@ -202,8 +202,8 @@ void statistics_db::writeStatisticsWin(std::unordered_map<ipAddress_win, int> wi
         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);
+            const ipAddress_win &e = it->first;
+            query.bindNoCopy(1, e.ipAddress);
             query.bind(2, e.winSize);
             query.bind(3, it->second);
             query.exec();
@@ -220,7 +220,7 @@ void statistics_db::writeStatisticsWin(std::unordered_map<ipAddress_win, int> wi
  * Writes the protocol distribution into the database.
  * @param protocolDistribution The protocol distribution from class statistics.
  */
-void statistics_db::writeStatisticsProtocols(std::unordered_map<ipAddress_protocol, entry_protocolStat> protocolDistribution) {
+void statistics_db::writeStatisticsProtocols(const std::unordered_map<ipAddress_protocol, entry_protocolStat> &protocolDistribution) {
     try {
         db->exec("DROP TABLE IF EXISTS ip_protocols");
         SQLite::Transaction transaction(*db);
@@ -233,9 +233,9 @@ void statistics_db::writeStatisticsProtocols(std::unordered_map<ipAddress_protoc
         db->exec(createTable);
         SQLite::Statement query(*db, "INSERT INTO ip_protocols VALUES (?, ?, ?, ?)");
         for (auto it = protocolDistribution.begin(); it != protocolDistribution.end(); ++it) {
-            ipAddress_protocol e = it->first;
-            query.bind(1, e.ipAddress);
-            query.bind(2, e.protocol);
+            const ipAddress_protocol &e = it->first;
+            query.bindNoCopy(1, e.ipAddress);
+            query.bindNoCopy(2, e.protocol);
             query.bind(3, it->second.count);
             query.bind(4, it->second.byteCount);
             query.exec();
@@ -252,7 +252,7 @@ void statistics_db::writeStatisticsProtocols(std::unordered_map<ipAddress_protoc
  * Writes the port statistics into the database.
  * @param portsStatistics The ports statistics from class statistics.
  */
-void statistics_db::writeStatisticsPorts(std::unordered_map<ipAddress_inOut_port, entry_portStat> portsStatistics) {
+void statistics_db::writeStatisticsPorts(const std::unordered_map<ipAddress_inOut_port, entry_portStat> &portsStatistics) {
     try {
         db->exec("DROP TABLE IF EXISTS ip_ports");
         SQLite::Transaction transaction(*db);
@@ -264,11 +264,11 @@ void statistics_db::writeStatisticsPorts(std::unordered_map<ipAddress_inOut_port
                 "byteCount REAL,"
                 "portProtocol TEXT COLLATE NOCASE,"
                 "portService TEXT COLLATE NOCASE,"
-                "PRIMARY KEY(ipAddress,portDirection,portNumber));";
+                "PRIMARY KEY(ipAddress,portDirection,portNumber,portProtocol));";
         db->exec(createTable);
         SQLite::Statement query(*db, "INSERT INTO ip_ports VALUES (?, ?, ?, ?, ?, ?, ?)");
         for (auto it = portsStatistics.begin(); it != portsStatistics.end(); ++it) {
-            ipAddress_inOut_port e = it->first;
+            const ipAddress_inOut_port &e = it->first;
 
             std::string portService = portServices[e.portNumber];
             if(portService.empty()) {
@@ -276,13 +276,13 @@ void statistics_db::writeStatisticsPorts(std::unordered_map<ipAddress_inOut_port
                 else {portService = "unknown";}
             }
 
-            query.bind(1, e.ipAddress);
-            query.bind(2, e.trafficDirection);
+            query.bindNoCopy(1, e.ipAddress);
+            query.bindNoCopy(2, e.trafficDirection);
             query.bind(3, e.portNumber);
             query.bind(4, it->second.count);
             query.bind(5, it->second.byteCount);
-            query.bind(6, e.protocol);
-            query.bind(7, portService);
+            query.bindNoCopy(6, e.protocol);
+            query.bindNoCopy(7, portService);
             query.exec();
             query.reset();
         }
@@ -297,7 +297,7 @@ void statistics_db::writeStatisticsPorts(std::unordered_map<ipAddress_inOut_port
  *  Writes the IP address -> MAC address mapping into the database.
  * @param IpMacStatistics The IP address -> MAC address mapping from class statistics.
  */
-void statistics_db::writeStatisticsIpMac(std::unordered_map<std::string, std::string> IpMacStatistics) {
+void statistics_db::writeStatisticsIpMac(const std::unordered_map<std::string, std::string> &IpMacStatistics) {
     try {
         db->exec("DROP TABLE IF EXISTS ip_mac");
         SQLite::Transaction transaction(*db);
@@ -308,8 +308,8 @@ void statistics_db::writeStatisticsIpMac(std::unordered_map<std::string, std::st
         db->exec(createTable);
         SQLite::Statement query(*db, "INSERT INTO ip_mac VALUES (?, ?)");
         for (auto it = IpMacStatistics.begin(); it != IpMacStatistics.end(); ++it) {
-            query.bind(1, it->first);
-            query.bind(2, it->second);
+            query.bindNoCopy(1, it->first);
+            query.bindNoCopy(2, it->second);
             query.exec();
             query.reset();
         }
@@ -334,7 +334,8 @@ void statistics_db::writeStatisticsIpMac(std::unordered_map<std::string, std::st
  */
 void statistics_db::writeStatisticsFile(int packetCount, float captureDuration, std::string timestampFirstPkt,
                                         std::string timestampLastPkt, float avgPacketRate, float avgPacketSize,
-                                        float avgPacketsSentPerHost, float avgBandwidthIn, float avgBandwidthOut) {
+                                        float avgPacketsSentPerHost, float avgBandwidthIn, float avgBandwidthOut,
+                                        bool doExtraTests) {
     try {
         db->exec("DROP TABLE IF EXISTS file_statistics");
         SQLite::Transaction transaction(*db);
@@ -347,9 +348,10 @@ void statistics_db::writeStatisticsFile(int packetCount, float captureDuration,
                 "avgPacketSize REAL,"
                 "avgPacketsSentPerHost REAL,"
                 "avgBandwidthIn REAL,"
-                "avgBandwidthOut REAL);";
+                "avgBandwidthOut REAL,"
+                "doExtraTests INTEGER);";
         db->exec(createTable);
-        SQLite::Statement query(*db, "INSERT INTO file_statistics VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)");
+        SQLite::Statement query(*db, "INSERT INTO file_statistics VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
         query.bind(1, packetCount);
         query.bind(2, captureDuration);
         query.bind(3, timestampFirstPkt);
@@ -359,6 +361,7 @@ void statistics_db::writeStatisticsFile(int packetCount, float captureDuration,
         query.bind(7, avgPacketsSentPerHost);
         query.bind(8, avgBandwidthIn);
         query.bind(9, avgBandwidthOut);
+        query.bind(10, doExtraTests);
         query.exec();
         transaction.commit();
     }
@@ -372,7 +375,7 @@ void statistics_db::writeStatisticsFile(int packetCount, float captureDuration,
  * Writes the conversation statistics into the database.
  * @param convStatistics The conversation from class statistics.
  */
-void statistics_db::writeStatisticsConv(std::unordered_map<conv, entry_convStat> convStatistics){          
+void statistics_db::writeStatisticsConv(std::unordered_map<conv, entry_convStat> &convStatistics){
     try {
         db->exec("DROP TABLE IF EXISTS conv_statistics");
         SQLite::Transaction transaction(*db);
@@ -392,8 +395,8 @@ void statistics_db::writeStatisticsConv(std::unordered_map<conv, entry_convStat>
 
         // 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;
+            const conv &f = it->first;
+            entry_convStat &e = it->second;
             if (e.pkts_count > 1){
                 int sumDelay = 0;
                 int minDelay = -1;
@@ -414,9 +417,9 @@ void statistics_db::writeStatisticsConv(std::unordered_map<conv, entry_convStat>
                 std::chrono::microseconds conn_duration = end_timesttamp - start_timesttamp;
                 e.avg_pkt_rate = (float) e.pkts_count * 1000000 / conn_duration.count(); // pkt per sec
 
-                query.bind(1, f.ipAddressA);
+                query.bindNoCopy(1, f.ipAddressA);
                 query.bind(2, f.portA);
-                query.bind(3, f.ipAddressB);
+                query.bindNoCopy(3, f.ipAddressB);
                 query.bind(4, f.portB);
                 query.bind(5, (int) e.pkts_count);
                 query.bind(6, (float) e.avg_pkt_rate);
@@ -438,7 +441,7 @@ void statistics_db::writeStatisticsConv(std::unordered_map<conv, entry_convStat>
  * Writes the extended statistics for every conversation into the database.
  * @param conv_statistics_extended The extended conversation statistics from class statistics.
  */
-void statistics_db::writeStatisticsConvExt(std::unordered_map<convWithProt, entry_convStatExt> conv_statistics_extended){
+void statistics_db::writeStatisticsConvExt(std::unordered_map<convWithProt, entry_convStatExt> &conv_statistics_extended){
     try {
         db->exec("DROP TABLE IF EXISTS conv_statistics_extended");
         SQLite::Transaction transaction(*db);
@@ -447,7 +450,7 @@ void statistics_db::writeStatisticsConvExt(std::unordered_map<convWithProt, entr
                 "portA INTEGER,"
                 "ipAddressB TEXT,"              
                 "portB INTEGER,"
-                "protocol TEXT,"
+                "protocol TEXT COLLATE NOCASE,"
                 "pktsCount INTEGER,"
                 "avgPktRate REAL,"
                 "avgDelay INTEGER,"
@@ -462,8 +465,8 @@ void statistics_db::writeStatisticsConvExt(std::unordered_map<convWithProt, entr
         SQLite::Statement query(*db, "INSERT INTO conv_statistics_extended VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
         // iterate over every conversation and interval aggregation pair and store the respective values in the database
         for (auto it = conv_statistics_extended.begin(); it != conv_statistics_extended.end(); ++it) {
-            convWithProt f = it->first;
-            entry_convStatExt e = it->second;
+            const convWithProt &f = it->first;
+            entry_convStatExt &e = it->second;
 
             int sumDelay = 0;
             int minDelay = -1;
@@ -489,42 +492,34 @@ void statistics_db::writeStatisticsConvExt(std::unordered_map<convWithProt, entr
                 e.avg_pkt_rate = e.pkts_count / e.total_comm_duration;
 
             if (e.avg_int_pkts_count > 0){
-                query.bind(1, f.ipAddressA);
+                query.bindNoCopy(1, f.ipAddressA);
                 query.bind(2, f.portA);
-                query.bind(3, f.ipAddressB);
+                query.bindNoCopy(3, f.ipAddressB);
                 query.bind(4, f.portB);
-                query.bind(5, f.protocol);
+                query.bindNoCopy(5, f.protocol);
                 query.bind(6, (int) e.pkts_count);
                 query.bind(7, (float) e.avg_pkt_rate);
-                query.bind(8, (int) e.avg_interarrival_time.count());
-                query.bind(9, minDelay);
-                query.bind(10, maxDelay);
+
+                if (f.protocol == "UDP" || (f.protocol == "TCP" && e.pkts_count < 2))
+                    query.bind(8);
+                else
+                    query.bind(8, (int) e.avg_interarrival_time.count());
+
+                if (minDelay == -1)
+                    query.bind(9);
+                else
+                    query.bind(9, minDelay);
+
+                if (maxDelay == -1)
+                    query.bind(10);
+                else
+                    query.bind(10, maxDelay);
+
                 query.bind(11, e.avg_int_pkts_count);
                 query.bind(12, e.avg_time_between_ints);
                 query.bind(13, e.avg_interval_time);
                 query.bind(14, e.total_comm_duration);
                 query.exec();
-
-                std::string primary_where = "WHERE ipAddressA=\"" + f.ipAddressA + "\" AND portA=" + std::to_string(f.portA) + " AND ipAddressB=\"";
-                primary_where += f.ipAddressB + "\" AND portB=" + std::to_string(f.portB) + " AND protocol=\"" + f.protocol + "\";";
-                std::string update_stmt;
-
-                // replace -1 with null
-                if (minDelay == -1){
-                    update_stmt = "UPDATE conv_statistics_extended SET minDelay=NULL " + primary_where;
-                    db->exec(update_stmt);
-                }
-
-                if (maxDelay == -1){
-                    update_stmt = "UPDATE conv_statistics_extended SET maxDelay=NULL " + primary_where;
-                    db->exec(update_stmt);
-                }
-
-                if (f.protocol == "UDP" || (f.protocol == "TCP" && e.pkts_count < 2)){
-                    update_stmt = "UPDATE conv_statistics_extended SET avgDelay=NULL " + primary_where;
-                    db->exec(update_stmt);
-                }
-
                 query.reset();
             }
             
@@ -540,7 +535,7 @@ void statistics_db::writeStatisticsConvExt(std::unordered_map<convWithProt, entr
  * Writes the interval statistics into the database.
  * @param intervalStatistics The interval entries from class statistics.
  */
-void statistics_db::writeStatisticsInterval(std::unordered_map<std::string, entry_intervalStat> intervalStatistics){          
+void statistics_db::writeStatisticsInterval(const std::unordered_map<std::string, entry_intervalStat> &intervalStatistics){
     try {        
         db->exec("DROP TABLE IF EXISTS interval_statistics");
         SQLite::Transaction transaction(*db);
@@ -565,10 +560,9 @@ void statistics_db::writeStatisticsInterval(std::unordered_map<std::string, entr
         db->exec(createTable);
         SQLite::Statement query(*db, "INSERT INTO interval_statistics VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
         for (auto it = intervalStatistics.begin(); it != intervalStatistics.end(); ++it) {
-            std::string t = it->first;
-            entry_intervalStat e = it->second;        
+            const entry_intervalStat &e = it->second;
             
-            query.bind(1, t);
+            query.bindNoCopy(1, it->first);
             query.bind(2, (int)e.pkts_count);
             query.bind(3, e.kbytes);
             query.bind(4, e.ip_src_entropy);
@@ -707,8 +701,8 @@ bool statistics_db::pathExists(std::string path)
  * Writes the unrecognized PDUs into the database.
  * @param unrecognized_PDUs The unrecognized PDUs from class statistics.
  */
-void statistics_db::writeStatisticsUnrecognizedPDUs(std::unordered_map<unrecognized_PDU, unrecognized_PDU_stat>
-                                                    unrecognized_PDUs) {
+void statistics_db::writeStatisticsUnrecognizedPDUs(const std::unordered_map<unrecognized_PDU, unrecognized_PDU_stat>
+                                                    &unrecognized_PDUs) {
     try {
         db->exec("DROP TABLE IF EXISTS unrecognized_pdus");
         SQLite::Transaction transaction(*db);
@@ -722,12 +716,12 @@ void statistics_db::writeStatisticsUnrecognizedPDUs(std::unordered_map<unrecogni
         db->exec(createTable);
         SQLite::Statement query(*db, "INSERT INTO unrecognized_pdus VALUES (?, ?, ?, ?, ?)");
         for (auto it = unrecognized_PDUs.begin(); it != unrecognized_PDUs.end(); ++it) {
-            unrecognized_PDU e = it->first;
-            query.bind(1, e.srcMacAddress);
-            query.bind(2, e.dstMacAddress);
+            const unrecognized_PDU &e = it->first;
+            query.bindNoCopy(1, e.srcMacAddress);
+            query.bindNoCopy(2, e.dstMacAddress);
             query.bind(3, e.typeNumber);
             query.bind(4, it->second.count);
-            query.bind(5, it->second.timestamp_last_occurrence);
+            query.bindNoCopy(5, it->second.timestamp_last_occurrence);
             query.exec();
             query.reset();
         }
@@ -736,4 +730,4 @@ void statistics_db::writeStatisticsUnrecognizedPDUs(std::unordered_map<unrecogni
     catch (std::exception &e) {
         std::cout << "Exception in statistics_db: " << e.what() << std::endl;
     }
-}
+}

+ 16 - 15
code_boost/src/cxx/statistics_db.h

@@ -22,38 +22,39 @@ public:
     /*
      * Database version: Increment number on every change in the C++ code!
      */
-    static const int DB_VERSION = 4;
+    static const int DB_VERSION = 7;
 
     /*
      * Methods for writing values into database
      */
-    void writeStatisticsIP(std::unordered_map<std::string, entry_ipStat> ipStatistics);
+    void writeStatisticsIP(const std::unordered_map<std::string, entry_ipStat> &ipStatistics);
 
-    void writeStatisticsDegree(std::unordered_map<std::string, entry_ipStat> ipStatistics);
+    void writeStatisticsDegree(const std::unordered_map<std::string, entry_ipStat> &ipStatistics);
 
-    void writeStatisticsTTL(std::unordered_map<ipAddress_ttl, int> ttlDistribution);
+    void writeStatisticsTTL(const std::unordered_map<ipAddress_ttl, int> &ttlDistribution);
 
-    void writeStatisticsMSS(std::unordered_map<ipAddress_mss, int> mssDistribution);
+    void writeStatisticsMSS(const std::unordered_map<ipAddress_mss, int> &mssDistribution);
 
-    void writeStatisticsToS(std::unordered_map<ipAddress_tos, int> tosDistribution);
+    void writeStatisticsToS(const std::unordered_map<ipAddress_tos, int> &tosDistribution);
 
-    void writeStatisticsWin(std::unordered_map<ipAddress_win, int> winDistribution);
+    void writeStatisticsWin(const std::unordered_map<ipAddress_win, int> &winDistribution);
 
-    void writeStatisticsProtocols(std::unordered_map<ipAddress_protocol, entry_protocolStat> protocolDistribution);
+    void writeStatisticsProtocols(const std::unordered_map<ipAddress_protocol, entry_protocolStat> &protocolDistribution);
 
-    void writeStatisticsPorts(std::unordered_map<ipAddress_inOut_port, entry_portStat> portsStatistics);
+    void writeStatisticsPorts(const std::unordered_map<ipAddress_inOut_port, entry_portStat> &portsStatistics);
 
-    void writeStatisticsIpMac(std::unordered_map<std::string, std::string> IpMacStatistics);
+    void writeStatisticsIpMac(const std::unordered_map<std::string, std::string> &IpMacStatistics);
 
     void writeStatisticsFile(int packetCount, float captureDuration, std::string timestampFirstPkt,
                              std::string timestampLastPkt, float avgPacketRate, float avgPacketSize,
-                             float avgPacketsSentPerHost, float avgBandwidthIn, float avgBandwidthOut);
+                             float avgPacketsSentPerHost, float avgBandwidthIn, float avgBandwidthOut,
+                             bool doExtraTests);
 
-    void writeStatisticsConv(std::unordered_map<conv, entry_convStat> convStatistics);
+    void writeStatisticsConv(std::unordered_map<conv, entry_convStat> &convStatistics);
 
-    void writeStatisticsConvExt(std::unordered_map<convWithProt, entry_convStatExt> conv_statistics_extended);
+    void writeStatisticsConvExt(std::unordered_map<convWithProt, entry_convStatExt> &conv_statistics_extended);
 
-    void writeStatisticsInterval(std::unordered_map<std::string, entry_intervalStat> intervalStatistics);
+    void writeStatisticsInterval(const std::unordered_map<std::string, entry_intervalStat> &intervalStatistics);
 
     void writeDbVersion();
 
@@ -63,7 +64,7 @@ public:
 
     bool pathExists(std::string path);
 
-    void writeStatisticsUnrecognizedPDUs(std::unordered_map<unrecognized_PDU, unrecognized_PDU_stat> unrecognized_PDUs);
+    void writeStatisticsUnrecognizedPDUs(const std::unordered_map<unrecognized_PDU, unrecognized_PDU_stat> &unrecognized_PDUs);
 
 
 private:

+ 4 - 4
code_boost/src/cxx/utilities.cpp

@@ -33,7 +33,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.
  */
-std::string getIPv4Class(std::string ipAddress){
+std::string getIPv4Class(const std::string &ipAddress){
     std::string ipClass="Unknown";
     
     std::vector<std::string> ipBytes;
@@ -79,7 +79,7 @@ std::string getIPv4Class(std::string ipAddress){
  * @param IP to convert.
  * @param IP_bytes to be filled and retrieved.
  */
-void convertIPv4toArray(std::string IP, unsigned short IP_bytes[]){
+void convertIPv4toArray(const std::string &IP, unsigned short IP_bytes[]){
     std::vector<std::string> temp_v;
     split_str(IP,'.',temp_v);
     IP_bytes[0] = std::stoi(temp_v[0]);
@@ -150,7 +150,7 @@ u16 tcp_sum_calc(u16 len_tcp, u16 src_addr[],u16 dest_addr[], bool padding, u16
  * @param ipAddressReceiver The destination IP.
  * @param tcpPkt The packet to get checked.
  */
-bool check_tcpChecksum(std::string ipAddressSender, std::string ipAddressReceiver, TCP tcpPkt){
+bool check_tcpChecksum(const std::string &ipAddressSender, const std::string &ipAddressReceiver, TCP tcpPkt){
     uint16_t checksum = tcpPkt.checksum();
 
     unsigned short calculatedChecsum = 0;
@@ -160,7 +160,7 @@ bool check_tcpChecksum(std::string ipAddressSender, std::string ipAddressReceive
 
     try {
         bufferArray_8 = tcpPkt.serialize();
-    } catch (serialization_error) {
+    } catch (serialization_error&) {
         std::cout << "Error: Could not serialize TCP packet with sender: " << ipAddressSender << ", receiver: "
                   << ipAddressReceiver << ", seq: " << tcpPkt.seq() << std::endl;
         return false;

+ 3 - 3
code_boost/src/cxx/utilities.h

@@ -21,15 +21,15 @@ typedef unsigned long u32;
 using namespace Tins;
 
 
-std::string getIPv4Class(std::string ipAddress);
+std::string getIPv4Class(const std::string &ipAddress);
 
-void convertIPv4toArray(std::string IP, unsigned short IP_bytes[]);
+void convertIPv4toArray(const std::string &IP, unsigned short IP_bytes[]);
 
 void split_str(const std::string& s, char delim,std::vector<std::string>& v);
 
 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);
+bool check_tcpChecksum(const std::string &ipAddressSender, const std::string &ipAddressReceiver, TCP tcpPkt);
 
 template<class T>
 std::string integral_to_binary_string(T byte);