Browse Source

add portProtocol to database

Stefano Acquaviti 6 years ago
parent
commit
566b847f30

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

@@ -277,16 +277,16 @@ void pcap_processor::process_packets(const Packet &pkt) {
             } catch (Tins::option_not_found) {
                 // Ignore MSS if option not set
             }
-            stats.incrementPortCount(ipAddressSender, tcpPkt.sport(), ipAddressReceiver, tcpPkt.dport());
-            stats.increasePortByteCount(ipAddressSender, tcpPkt.sport(), ipAddressReceiver, tcpPkt.dport(), sizeCurrentPacket);
+            stats.incrementPortCount(ipAddressSender, tcpPkt.sport(), ipAddressReceiver, tcpPkt.dport(), "TCP");
+            stats.increasePortByteCount(ipAddressSender, tcpPkt.sport(), ipAddressReceiver, tcpPkt.dport(), sizeCurrentPacket, "TCP");
             
           // UDP Packet
         } else if (p == PDU::PDUType::UDP) {
             const UDP udpPkt = (const UDP &) *pdu_l4;
             stats.incrementProtocolCount(ipAddressSender, "UDP");
             stats.increaseProtocolByteCount(ipAddressSender, "UDP", sizeCurrentPacket);
-            stats.incrementPortCount(ipAddressSender, udpPkt.sport(), ipAddressReceiver, udpPkt.dport());
-            stats.increasePortByteCount(ipAddressSender, udpPkt.sport(), ipAddressReceiver, udpPkt.dport(), sizeCurrentPacket);
+            stats.incrementPortCount(ipAddressSender, udpPkt.sport(), ipAddressReceiver, udpPkt.dport(), "UDP");
+            stats.increasePortByteCount(ipAddressSender, udpPkt.sport(), ipAddressReceiver, udpPkt.dport(), sizeCurrentPacket, "UDP");
           
         } else if (p == PDU::PDUType::ICMP) {
             stats.incrementProtocolCount(ipAddressSender, "ICMP");

+ 6 - 6
code_boost/src/cxx/statistics.cpp

@@ -336,11 +336,11 @@ float statistics::getProtocolByteCount(std::string ipAddress, std::string protoc
  * @param incomingPort The port used by the receiver.
  */
 void statistics::incrementPortCount(std::string ipAddressSender, int outgoingPort, std::string ipAddressReceiver,
-                                    int incomingPort) {
+                                    int incomingPort, std::string protocol) {
     port_values[outgoingPort]++;
     port_values[incomingPort]++;
-    ip_ports[{ipAddressSender, "out", outgoingPort}].count++;
-    ip_ports[{ipAddressReceiver, "in", incomingPort}].count++;
+    ip_ports[{ipAddressSender, "out", outgoingPort, protocol}].count++;
+    ip_ports[{ipAddressReceiver, "in", incomingPort, protocol}].count++;
 }
 
 /**
@@ -354,9 +354,9 @@ void statistics::incrementPortCount(std::string ipAddressSender, int outgoingPor
  * @param byteSent The packet's size.
  */
 void statistics::increasePortByteCount(std::string ipAddressSender, int outgoingPort, std::string ipAddressReceiver,
-                                       int incomingPort, long bytesSent) {
-    ip_ports[{ipAddressSender, "out", outgoingPort}].byteCount += bytesSent;
-    ip_ports[{ipAddressReceiver, "in", incomingPort}].byteCount += bytesSent;
+                                       int incomingPort, long bytesSent, std::string protocol) {
+    ip_ports[{ipAddressSender, "out", outgoingPort, protocol}].byteCount += bytesSent;
+    ip_ports[{ipAddressReceiver, "in", incomingPort, protocol}].byteCount += bytesSent;
 }
 
 /**

+ 5 - 3
code_boost/src/cxx/statistics.h

@@ -275,11 +275,13 @@ struct ipAddress_inOut_port {
     std::string ipAddress;
     std::string trafficDirection;
     int portNumber;
+    std::string protocol;
 
     bool operator==(const ipAddress_inOut_port &other) const {
         return ipAddress == other.ipAddress
                && trafficDirection == other.trafficDirection
-               && portNumber == other.portNumber;
+               && portNumber == other.portNumber
+               && protocol == other.protocol;
     }
 };
 
@@ -414,10 +416,10 @@ public:
     void increaseProtocolByteCount(std::string ipAddress, std::string protocol, long bytesSent);
 
     void incrementPortCount(std::string ipAddressSender, int outgoingPort, std::string ipAddressReceiver,
-                            int incomingPort);
+                            int incomingPort, std::string protocol);
 
     void increasePortByteCount(std::string ipAddressSender, int outgoingPort, std::string ipAddressReceiver,
-                               int incomingPort, long bytesSent);
+                               int incomingPort, long bytesSent, std::string protocol);
 
     int getProtocolCount(std::string ipAddress, std::string protocol);
 

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

@@ -229,10 +229,11 @@ void statistics_db::writeStatisticsPorts(std::unordered_map<ipAddress_inOut_port
                 "portNumber INTEGER,"
                 "portCount INTEGER,"
                 "byteCount REAL,"
+                "portProtocol TEXT COLLATE NOCASE,"
                 "portService TEXT COLLATE NOCASE,"
                 "PRIMARY KEY(ipAddress,portDirection,portNumber));";
         db->exec(createTable);
-        SQLite::Statement query(*db, "INSERT INTO ip_ports VALUES (?, ?, ?, ?, ?, ?)");
+        SQLite::Statement query(*db, "INSERT INTO ip_ports VALUES (?, ?, ?, ?, ?, ?, ?)");
         for (auto it = portsStatistics.begin(); it != portsStatistics.end(); ++it) {
             ipAddress_inOut_port e = it->first;
 
@@ -243,7 +244,8 @@ void statistics_db::writeStatisticsPorts(std::unordered_map<ipAddress_inOut_port
             query.bind(3, e.portNumber);
             query.bind(4, it->second.count);
             query.bind(5, it->second.byteCount);
-            query.bind(6, portService);
+            query.bind(6, e.protocol);
+            query.bind(7, portService);
             query.exec();
             query.reset();
         }