Browse Source

Replaced by-value parameters in the hot path with const references

Stefan Schmidt 6 years ago
parent
commit
62e88a7bdf

+ 21 - 21
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,7 +259,7 @@ 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){
+void statistics::addConvStatExt(const std::string &ipAddressSender,int sport,const std::string &ipAddressReceiver,int dport,const std::string &protocol, std::chrono::microseconds timestamp){
     convWithProt f1 = {ipAddressReceiver, dport, ipAddressSender, sport, protocol};
     convWithProt f2 = {ipAddressSender, sport, ipAddressReceiver, dport, protocol};
     convWithProt f;
@@ -347,7 +347,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 +357,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 +367,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 +377,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 +387,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 +396,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 +406,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 +416,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 +429,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 +447,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 +461,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 +478,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 +489,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 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){  
@@ -668,7 +668,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 +693,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;

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

@@ -529,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();
 
@@ -547,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);
 
@@ -579,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 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);
 
@@ -599,7 +599,7 @@ public:
 
     float getAvgPacketSize() const;
 
-    void printStats(std::string ipAddress);
+    void printStats(const std::string &ipAddress);
 
     bool getDoExtraTests();
 
@@ -608,7 +608,7 @@ public:
     /*
      * IP Address-specific statistics
      */
-    ip_stats getStatsForIP(std::string ipAddress);
+    ip_stats getStatsForIP(const std::string &ipAddress);
 
 private:
     /*

+ 3 - 3
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;

+ 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);