123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315 |
- #include <iostream>
- #include "statistics.h"
- #include <sstream>
- #include <SQLiteCpp/SQLiteCpp.h>
- #include "statistics_db.h"
- void statistics::incrementTTLcount(std::string ipAddress, int ttlValue) {
- ttl_distribution[{ipAddress, ttlValue}]++;
- }
- void statistics::incrementProtocolCount(std::string ipAddress, std::string protocol) {
- protocol_distribution[{ipAddress, protocol}]++;
- }
- int statistics::getProtocolCount(std::string ipAddress, std::string protocol) {
- return protocol_distribution[{ipAddress, protocol}];
- }
- void statistics::incrementPortCount(std::string ipAddressSender, int outgoingPort, std::string ipAddressReceiver,
- int incomingPort) {
- ip_ports[{ipAddressSender, "out", outgoingPort}]++;
- ip_ports[{ipAddressReceiver, "in", incomingPort}]++;
- }
- statistics::statistics(void) {
- }
- void statistics::assignMacAddress(std::string ipAddress, std::string macAddress) {
- ip_mac_mapping[ipAddress] = macAddress;
- }
- void statistics::addIpStat_packetSent(std::string ipAddressSender, std::string ipAddressReceiver, long bytesSent) {
-
- ip_statistics[ipAddressSender].kbytes_sent += (float(bytesSent) / 1024);
- ip_statistics[ipAddressSender].pkts_sent++;
-
- ip_statistics[ipAddressReceiver].kbytes_received += (float(bytesSent) / 1024);
- ip_statistics[ipAddressReceiver].pkts_received++;
- }
- void statistics::addMSS(std::string ipAddress, int MSSvalue) {
- ip_sumMss[ipAddress] += MSSvalue;
- }
- void statistics::setTimestampFirstPacket(Tins::Timestamp ts) {
- timestamp_firstPacket = ts;
- }
- void statistics::setTimestampLastPacket(Tins::Timestamp ts) {
- timestamp_lastPacket = ts;
- }
- std::string statistics::getCaptureDurationTimestamp() const {
-
- time_t t = (timestamp_lastPacket.seconds() - timestamp_firstPacket.seconds());
- time_t ms = (timestamp_lastPacket.microseconds() - timestamp_firstPacket.microseconds());
- long int hour = t / 3600;
- long int remainder = (t - hour * 3600);
- long int minute = remainder / 60;
- long int second = (remainder - minute * 60) % 60;
- long int microseconds = ms;
-
- char out[64];
- sprintf(out, "%02ld:%02ld:%02ld.%06ld ", hour, minute, second, microseconds);
- return std::string(out);
- }
- float statistics::getCaptureDurationSeconds() const {
- timeval d;
- d.tv_sec = timestamp_lastPacket.seconds() - timestamp_firstPacket.seconds();
- d.tv_usec = timestamp_lastPacket.microseconds() - timestamp_firstPacket.microseconds();
- char tmbuf[64], buf[64];
- auto nowtm = localtime(&(d.tv_sec));
- strftime(tmbuf, sizeof(tmbuf), "%S", nowtm);
- snprintf(buf, sizeof(buf), "%s.%06u", tmbuf, (uint) d.tv_usec);
- return std::stof(std::string(buf));
- }
- std::string statistics::getFormattedTimestamp(time_t seconds, suseconds_t microseconds) const {
- timeval tv;
- tv.tv_sec = seconds;
- tv.tv_usec = microseconds;
- char tmbuf[64], buf[64];
- auto nowtm = localtime(&(tv.tv_sec));
- strftime(tmbuf, sizeof(tmbuf), "%Y-%m-%d %H:%M:%S", nowtm);
- snprintf(buf, sizeof(buf), "%s.%06u", tmbuf, (uint) tv.tv_usec);
- return std::string(buf);
- }
- ip_stats statistics::getStatsForIP(std::string ipAddress) {
- float duration = getCaptureDurationSeconds();
- entry_ipStat ipStatEntry = ip_statistics[ipAddress];
- ip_stats s;
- s.bandwidthKBitsIn = (ipStatEntry.kbytes_received / duration) * 8;
- s.bandwidthKBitsOut = (ipStatEntry.kbytes_sent / duration) * 8;
- s.packetPerSecondIn = (ipStatEntry.pkts_received / duration);
- s.packetPerSecondOut = (ipStatEntry.pkts_sent / duration);
- s.AvgPacketSizeSent = (ipStatEntry.kbytes_sent / ipStatEntry.pkts_sent);
- s.AvgPacketSizeRecv = (ipStatEntry.kbytes_received / ipStatEntry.pkts_received);
- int sumMSS = ip_sumMss[ipAddress];
- int tcpPacketsSent = getProtocolCount(ipAddress, "TCP");
- s.AvgMaxSegmentSizeTCP = ((sumMSS > 0 && tcpPacketsSent > 0) ? (sumMSS / tcpPacketsSent) : 0);
- return s;
- }
- void statistics::incrementPacketCount() {
- packetCount++;
- }
- void statistics::printStats(std::string ipAddress) {
- std::stringstream ss;
- ss << std::endl;
- ss << "Capture duration: " << getCaptureDurationSeconds() << " seconds" << std::endl;
- ss << "Capture duration (HH:MM:SS.mmmmmm): " << getCaptureDurationTimestamp() << std::endl;
- ss << "#Packets: " << packetCount << std::endl;
- ss << std::endl;
-
- if (ipAddress != "") {
- entry_ipStat e = ip_statistics[ipAddress];
- ss << "\n----- STATS FOR IP ADDRESS [" << ipAddress << "] -------" << std::endl;
- ss << std::endl << "KBytes sent: " << e.kbytes_sent << std::endl;
- ss << "KBytes received: " << e.kbytes_received << std::endl;
- ss << "Packets sent: " << e.pkts_sent << std::endl;
- ss << "Packets received: " << e.pkts_received << "\n\n";
- ip_stats is = getStatsForIP(ipAddress);
- ss << "Bandwidth IN: " << is.bandwidthKBitsIn << " kbit/s" << std::endl;
- ss << "Bandwidth OUT: " << is.bandwidthKBitsOut << " kbit/s" << std::endl;
- ss << "Packets per second IN: " << is.packetPerSecondIn << std::endl;
- ss << "Packets per second OUT: " << is.packetPerSecondOut << std::endl;
- ss << "Avg Packet Size Sent: " << is.AvgPacketSizeSent << " kbytes" << std::endl;
- ss << "Avg Packet Size Received: " << is.AvgPacketSizeRecv << " kbytes" << std::endl;
- ss << "Avg MSS: " << is.AvgMaxSegmentSizeTCP << " bytes" << std::endl;
- }
- std::cout << ss.str();
- }
- void statistics::writeToDatabase(std::string database_path) {
-
- float duration = getCaptureDurationSeconds();
- long sumPacketsSent = 0, senderCountIP = 0;
- float sumBandwidthIn = 0.0, sumBandwidthOut = 0.0;
- for (auto i = ip_statistics.begin(); i != ip_statistics.end(); i++) {
- sumPacketsSent += i->second.pkts_sent;
-
- sumBandwidthIn += (i->second.kbytes_received / duration);
- sumBandwidthOut += (i->second.kbytes_sent / duration);
- senderCountIP++;
- }
- float avgPacketRate = (packetCount / duration);
- long avgPacketSize = getAvgPacketSize();
- long avgPacketsSentPerHost = (sumPacketsSent / senderCountIP);
- float avgBandwidthInKBits = (sumBandwidthIn / senderCountIP) * 8;
- float avgBandwidthOutInKBits = (sumBandwidthOut / senderCountIP) * 8;
-
- statistics_db db(database_path);
- db.writeStatisticsFile(packetCount, getCaptureDurationSeconds(),
- getFormattedTimestamp(timestamp_firstPacket.seconds(), timestamp_firstPacket.microseconds()),
- getFormattedTimestamp(timestamp_lastPacket.seconds(), timestamp_lastPacket.microseconds()),
- avgPacketRate, avgPacketSize, avgPacketsSentPerHost, avgBandwidthInKBits,
- avgBandwidthOutInKBits);
- db.writeStatisticsIP(ip_statistics);
- db.writeStatisticsTTL(ttl_distribution);
- db.writeStatisticsIpMac(ip_mac_mapping);
- db.writeStatisticsMss(ip_sumMss);
- db.writeStatisticsPorts(ip_ports);
- db.writeStatisticsProtocols(protocol_distribution);
- }
- float statistics::getAvgPacketSize() const {
-
- return (sumPacketSize / packetCount) / 1024;
- }
- void statistics::addPacketSize(uint32_t packetSize) {
- sumPacketSize += ((float) packetSize);
- }
|