Browse Source

replaced portServices vector with unordered_map

Stefano Acquaviti 6 years ago
parent
commit
8cb5fecfc2
2 changed files with 6 additions and 26 deletions
  1. 4 20
      code_boost/src/cxx/statistics_db.cpp
  2. 2 6
      code_boost/src/cxx/statistics_db.h

+ 4 - 20
code_boost/src/cxx/statistics_db.cpp

@@ -237,7 +237,8 @@ void statistics_db::writeStatisticsPorts(std::unordered_map<ipAddress_inOut_port
         for (auto it = portsStatistics.begin(); it != portsStatistics.end(); ++it) {
             ipAddress_inOut_port e = it->first;
 
-            std::string portService = getPortService(e.portNumber);
+            std::string portService = portServices[e.portNumber];
+            if(portService.empty()) {portService = "unknown";}
 
             query.bind(1, e.ipAddress);
             query.bind(2, e.trafficDirection);
@@ -490,8 +491,7 @@ void statistics_db::readPortServicesFromNmap()
             getline(reader, dump);
             if(!service.empty() && !portnumber.empty())
             {
-                port_service ps = {std::stoi(portnumber), service};
-                portServices.push_back(ps);
+                portServices.insert({std::stoi(portnumber), service});
             }
         }
 
@@ -500,26 +500,10 @@ void statistics_db::readPortServicesFromNmap()
 
     else
     {
-        port_service ps = {0, "unknown"};
-        portServices.push_back(ps);
+        portServices.insert({0, "unknown"});
     }
 }
 
-/**
- * Gets the service of a given port from portServices.
- * @param port The port of which the service should be returned.
- */
-std::string statistics_db::getPortService(int port)
-{
-    for(unsigned int x = 0; x < portServices.size(); x++)
-    {
-        port_service ps = portServices[x];
-        if(ps.p == port) {return ps.s;}
-    }
-
-    return "unknown";
-}
-
 /**
  * Gets the path to nmap-services-tcp.csv and makes sure the file is reached from any working directory within "/code"
  * because the working directory can be different when running tests. Checks if the file/path exists and warns the user.

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

@@ -10,10 +10,8 @@
 #include <string>
 #include "statistics.h"
 #include <SQLiteCpp/SQLiteCpp.h>
-#include <vector>
+#include <unordered_map>
 
-// Struct which contains a port and it's corresponding service
-struct port_service {int p; std::string s;};
 
 class statistics_db {
 public:
@@ -56,8 +54,6 @@ public:
 
     void writeDbVersion();
 
-    std::string getPortService(int port);
-
     void readPortServicesFromNmap();
 
     std::string getNmapPath();
@@ -67,7 +63,7 @@ private:
     std::unique_ptr<SQLite::Database> db;
 
     // Vector which contains all ports and their corresponding services
-    std::vector<port_service> portServices;
+    std::unordered_map<int, std::string> portServices;
 };