statistics.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <math.h>
  5. #include <sstream>
  6. #include <SQLiteCpp/SQLiteCpp.h>
  7. #include "statistics_db.h"
  8. #include "statistics.h"
  9. #include "utilities.h"
  10. using namespace Tins;
  11. // Aidmar
  12. /**
  13. * Checks if there is a payload and increments payloads counter.
  14. * @param pdu_l4 The packet that should be checked if it has a payload or not.
  15. */
  16. void statistics::checkPayload(const PDU *pdu_l4) {
  17. if(this->getDoExtraTests()) {
  18. // pdu_l4: Tarnsport layer 4
  19. int pktSize = pdu_l4->size();
  20. int headerSize = pdu_l4->header_size(); // TCP/UDP header
  21. int payloadSize = pktSize - headerSize;
  22. if (payloadSize > 0)
  23. payloadCount++;
  24. }
  25. }
  26. // Aidmar
  27. /**
  28. * Checks the correctness of TCP checksum and increments counter if the checksum was incorrect.
  29. * @param ipAddressSender The source IP.
  30. * @param ipAddressReceiver The destination IP.
  31. * @param tcpPkt The packet to get checked.
  32. */
  33. void statistics::checkTCPChecksum(std::string ipAddressSender, std::string ipAddressReceiver, TCP tcpPkt) {
  34. if(this->getDoExtraTests()) {
  35. if(check_tcpChecksum(ipAddressSender, ipAddressReceiver, tcpPkt))
  36. correctTCPChecksumCount++;
  37. else incorrectTCPChecksumCount++;
  38. }
  39. }
  40. // Aidmar
  41. /**
  42. * Calculates entropy of source and destination IPs for last time interval.
  43. * @param intervalStartTimestamp The timstamp where the interval starts.
  44. */
  45. std::vector<float> statistics::calculateLastIntervalIPsEntropy(std::chrono::microseconds intervalStartTimestamp){
  46. if(this->getDoExtraTests()) {
  47. std::vector<int> IPsSrcPktsCounts;
  48. std::vector<int> IPsDstPktsCounts;
  49. std::vector<float> IPsSrcProb;
  50. std::vector<float> IPsDstProb;
  51. int pktsSent = 0, pktsReceived = 0;
  52. for (auto i = ip_statistics.begin(); i != ip_statistics.end(); i++) {
  53. int indexStartSent = getClosestIndex(i->second.pktsSentTimestamp, intervalStartTimestamp);
  54. int IPsSrcPktsCount = i->second.pktsSentTimestamp.size() - indexStartSent;
  55. IPsSrcPktsCounts.push_back(IPsSrcPktsCount);
  56. pktsSent += IPsSrcPktsCount;
  57. int indexStartReceived = getClosestIndex(i->second.pktsReceivedTimestamp, intervalStartTimestamp);
  58. int IPsDstPktsCount = i->second.pktsReceivedTimestamp.size() - indexStartReceived;
  59. IPsDstPktsCounts.push_back(IPsDstPktsCount);
  60. pktsReceived += IPsDstPktsCount;
  61. }
  62. for (auto i = IPsSrcPktsCounts.begin(); i != IPsSrcPktsCounts.end(); i++) {
  63. IPsSrcProb.push_back((float) *i / pktsSent);
  64. }
  65. for (auto i = IPsDstPktsCounts.begin(); i != IPsDstPktsCounts.end(); i++) {
  66. IPsDstProb.push_back((float) *i / pktsReceived);
  67. }
  68. // Calculate IP source entropy
  69. float IPsSrcEntropy = 0;
  70. for (unsigned i = 0; i < IPsSrcProb.size(); i++) {
  71. if (IPsSrcProb[i] > 0)
  72. IPsSrcEntropy += -IPsSrcProb[i] * log2(IPsSrcProb[i]);
  73. }
  74. // Calculate IP destination entropy
  75. float IPsDstEntropy = 0;
  76. for (unsigned i = 0; i < IPsDstProb.size(); i++) {
  77. if (IPsDstProb[i] > 0)
  78. IPsDstEntropy += -IPsDstProb[i] * log2(IPsDstProb[i]);
  79. }
  80. std::vector<float> entropies = {IPsSrcEntropy, IPsDstEntropy};
  81. return entropies;
  82. }
  83. else {
  84. return {-1, -1};
  85. }
  86. }
  87. // Aidmar
  88. /**
  89. * Calculates cumulative entropy of source and destination IPs, i.e., the entropy for packets from the beginning of the pcap file.
  90. */
  91. std::vector<float> statistics::calculateIPsCumEntropy(){
  92. if(this->getDoExtraTests()) {
  93. std::vector <std::string> IPs;
  94. std::vector <float> IPsSrcProb;
  95. std::vector <float> IPsDstProb;
  96. //std::chrono::high_resolution_clock::time_point t1 = std::chrono::high_resolution_clock::now();
  97. for (auto i = ip_statistics.begin(); i != ip_statistics.end(); i++) {
  98. IPs.push_back(i->first);
  99. IPsSrcProb.push_back((float)i->second.pkts_sent/packetCount);
  100. IPsDstProb.push_back((float)i->second.pkts_received/packetCount);
  101. }
  102. // Calculate IP source entropy
  103. float IPsSrcEntropy = 0;
  104. for(unsigned i=0; i < IPsSrcProb.size();i++){
  105. if (IPsSrcProb[i] > 0)
  106. IPsSrcEntropy += - IPsSrcProb[i]*log2(IPsSrcProb[i]);
  107. }
  108. // Calculate IP destination entropy
  109. float IPsDstEntropy = 0;
  110. for(unsigned i=0; i < IPsDstProb.size();i++){
  111. if (IPsDstProb[i] > 0)
  112. IPsDstEntropy += - IPsDstProb[i]*log2(IPsDstProb[i]);
  113. }
  114. std::vector<float> entropies = {IPsSrcEntropy, IPsDstEntropy};
  115. return entropies;
  116. }
  117. else {
  118. return {-1, -1};
  119. }
  120. }
  121. // Aidmar
  122. /**
  123. * Calculates sending packet rate for each IP in last time interval. Finds min and max packet rate and adds them to ip_statistics map.
  124. * @param intervalStartTimestamp The timstamp where the interval starts.
  125. */
  126. void statistics::calculateIPIntervalPacketRate(std::chrono::duration<int, std::micro> interval, std::chrono::microseconds intervalStartTimestamp){
  127. for (auto i = ip_statistics.begin(); i != ip_statistics.end(); i++) {
  128. int indexStartSent = getClosestIndex(i->second.pktsSentTimestamp, intervalStartTimestamp);
  129. int IPsSrcPktsCount = i->second.pktsSentTimestamp.size() - indexStartSent;
  130. float interval_pkt_rate = (float) IPsSrcPktsCount * 1000000 / interval.count(); // used 10^6 because interval in microseconds
  131. i->second.interval_pkt_rate.push_back(interval_pkt_rate);
  132. if(interval_pkt_rate > i->second.max_pkt_rate || i->second.max_pkt_rate == 0)
  133. i->second.max_pkt_rate = interval_pkt_rate;
  134. if(interval_pkt_rate < i->second.min_pkt_rate || i->second.min_pkt_rate == 0)
  135. i->second.min_pkt_rate = interval_pkt_rate;
  136. }
  137. }
  138. // Aidmar
  139. /**
  140. * Registers statistical data for last time interval. Calculates packet rate. Calculates IPs entropy. Calculates IPs cumulative entropy.
  141. * @param intervalStartTimestamp The timstamp where the interval starts.
  142. * @param intervalEndTimestamp The timstamp where the interval ends.
  143. * @param previousPacketCount The total number of packets in last interval.
  144. */
  145. void statistics::addIntervalStat(std::chrono::duration<int, std::micro> interval, std::chrono::microseconds intervalStartTimestamp, std::chrono::microseconds intervalEndTimestamp){
  146. // Add packet rate for each IP to ip_statistics map
  147. calculateIPIntervalPacketRate(interval, intervalStartTimestamp);
  148. std::vector<float> ipEntopies = calculateLastIntervalIPsEntropy(intervalStartTimestamp);
  149. std::vector<float> ipCumEntopies = calculateIPsCumEntropy();
  150. std::string lastPktTimestamp_s = std::to_string(intervalEndTimestamp.count());
  151. std::string intervalStartTimestamp_s = std::to_string(intervalStartTimestamp.count());
  152. // The intervalStartTimestamp_s is the previous interval lastPktTimestamp_s
  153. interval_statistics[lastPktTimestamp_s].pkts_count = packetCount - lastIntervalCumPktCount;
  154. interval_statistics[lastPktTimestamp_s].kbytes = (float(sumPacketSize - lastIntervalCumSumPktSize) / 1024);
  155. interval_statistics[lastPktTimestamp_s].payload_count = payloadCount - lastIntervalPayloadCount;
  156. interval_statistics[lastPktTimestamp_s].incorrect_checksum_count = incorrectTCPChecksumCount - lastIntervalIncorrectTCPChecksumCount;
  157. interval_statistics[lastPktTimestamp_s].correct_checksum_count = correctTCPChecksumCount - lastIntervalCorrectTCPChecksumCount;
  158. interval_statistics[lastPktTimestamp_s].new_ip_count = ip_statistics.size() - lastIntervalCumNewIPCount;
  159. interval_statistics[lastPktTimestamp_s].new_ttl_count = ttl_values.size() - lastIntervalCumNewTTLCount;
  160. interval_statistics[lastPktTimestamp_s].new_win_size_count = win_values.size() - lastIntervalCumNewWinSizeCount;
  161. interval_statistics[lastPktTimestamp_s].new_tos_count = tos_values.size() - lastIntervalCumNewToSCount;
  162. interval_statistics[lastPktTimestamp_s].new_mss_count = mss_values.size() - lastIntervalCumNewMSSCount;
  163. lastIntervalPayloadCount = payloadCount;
  164. lastIntervalIncorrectTCPChecksumCount = incorrectTCPChecksumCount;
  165. lastIntervalCorrectTCPChecksumCount = correctTCPChecksumCount;
  166. lastIntervalCumPktCount = packetCount;
  167. lastIntervalCumSumPktSize = sumPacketSize;
  168. lastIntervalCumNewIPCount = ip_statistics.size();
  169. lastIntervalCumNewTTLCount = ttl_values.size();
  170. lastIntervalCumNewWinSizeCount = win_values.size();
  171. lastIntervalCumNewToSCount = tos_values.size();
  172. lastIntervalCumNewMSSCount = mss_values.size();
  173. if(ipEntopies.size()>1){
  174. interval_statistics[lastPktTimestamp_s].ip_src_entropy = ipEntopies[0];
  175. interval_statistics[lastPktTimestamp_s].ip_dst_entropy = ipEntopies[1];
  176. }
  177. if(ipCumEntopies.size()>1){
  178. interval_statistics[lastPktTimestamp_s].ip_src_cum_entropy = ipCumEntopies[0];
  179. interval_statistics[lastPktTimestamp_s].ip_dst_cum_entropy = ipCumEntopies[1];
  180. }
  181. }
  182. // Aidmar
  183. /**
  184. * Registers statistical data for a sent packet in a given conversation (two IPs, two ports).
  185. * Increments the counter packets_A_B or packets_B_A.
  186. * Adds the timestamp of the packet in pkts_A_B_timestamp or pkts_B_A_timestamp.
  187. * @param ipAddressSender The sender IP address.
  188. * @param sport The source port.
  189. * @param ipAddressReceiver The receiver IP address.
  190. * @param dport The destination port.
  191. * @param timestamp The timestamp of the packet.
  192. */
  193. void statistics::addConvStat(std::string ipAddressSender,int sport,std::string ipAddressReceiver,int dport, std::chrono::microseconds timestamp){
  194. conv f1 = {ipAddressReceiver, dport, ipAddressSender, sport};
  195. conv f2 = {ipAddressSender, sport, ipAddressReceiver, dport};
  196. // if already exist A(ipAddressReceiver, dport), B(ipAddressSender, sport) conversation
  197. if (conv_statistics.count(f1)>0){
  198. conv_statistics[f1].pkts_count++;
  199. if(conv_statistics[f1].pkts_count<=3)
  200. conv_statistics[f1].pkts_delay.push_back(std::chrono::duration_cast<std::chrono::microseconds> (timestamp - conv_statistics[f1].pkts_timestamp.back()));
  201. conv_statistics[f1].pkts_timestamp.push_back(timestamp);
  202. }
  203. else{
  204. conv_statistics[f2].pkts_count++;
  205. if(conv_statistics[f2].pkts_timestamp.size()>0 && conv_statistics[f2].pkts_count<=3 )
  206. conv_statistics[f2].pkts_delay.push_back(std::chrono::duration_cast<std::chrono::microseconds> (timestamp - conv_statistics[f2].pkts_timestamp.back()));
  207. conv_statistics[f2].pkts_timestamp.push_back(timestamp);
  208. }
  209. }
  210. // Aidmar
  211. /**
  212. * Increments the packet counter for the given IP address and MSS value.
  213. * @param ipAddress The IP address whose MSS packet counter should be incremented.
  214. * @param mssValue The MSS value of the packet.
  215. */
  216. void statistics::incrementMSScount(std::string ipAddress, int mssValue) {
  217. mss_values[mssValue]++;
  218. mss_distribution[{ipAddress, mssValue}]++;
  219. }
  220. // Aidmar
  221. /**
  222. * Increments the packet counter for the given IP address and window size.
  223. * @param ipAddress The IP address whose window size packet counter should be incremented.
  224. * @param winSize The window size of the packet.
  225. */
  226. void statistics::incrementWinCount(std::string ipAddress, int winSize) {
  227. win_values[winSize]++;
  228. win_distribution[{ipAddress, winSize}]++;
  229. }
  230. /**
  231. * Increments the packet counter for the given IP address and TTL value.
  232. * @param ipAddress The IP address whose TTL packet counter should be incremented.
  233. * @param ttlValue The TTL value of the packet.
  234. */
  235. void statistics::incrementTTLcount(std::string ipAddress, int ttlValue) {
  236. ttl_values[ttlValue]++;
  237. ttl_distribution[{ipAddress, ttlValue}]++;
  238. }
  239. /**
  240. * Increments the packet counter for the given IP address and ToS value.
  241. * @param ipAddress The IP address whose ToS packet counter should be incremented.
  242. * @param tosValue The ToS value of the packet.
  243. */
  244. void statistics::incrementToScount(std::string ipAddress, int tosValue) {
  245. tos_values[tosValue]++;
  246. tos_distribution[{ipAddress, tosValue}]++;
  247. }
  248. /**
  249. * Increments the protocol counter for the given IP address and protocol.
  250. * @param ipAddress The IP address whose protocol packet counter should be incremented.
  251. * @param protocol The protocol of the packet.
  252. */
  253. void statistics::incrementProtocolCount(std::string ipAddress, std::string protocol) {
  254. protocol_distribution[{ipAddress, protocol}]++;
  255. }
  256. /**
  257. * Returns the number of packets seen for the given IP address and protocol.
  258. * @param ipAddress The IP address whose packet count is wanted.
  259. * @param protocol The protocol whose packet count is wanted.
  260. * @return an integer: the number of packets
  261. */
  262. int statistics::getProtocolCount(std::string ipAddress, std::string protocol) {
  263. return protocol_distribution[{ipAddress, protocol}];
  264. }
  265. /**
  266. * Increments the packet counter for
  267. * - the given sender IP address with outgoing port and
  268. * - the given receiver IP address with incoming port.
  269. * @param ipAddressSender The IP address of the packet sender.
  270. * @param outgoingPort The port used by the sender.
  271. * @param ipAddressReceiver The IP address of the packet receiver.
  272. * @param incomingPort The port used by the receiver.
  273. */
  274. void statistics::incrementPortCount(std::string ipAddressSender, int outgoingPort, std::string ipAddressReceiver,
  275. int incomingPort) {
  276. ip_ports[{ipAddressSender, "out", outgoingPort}]++;
  277. ip_ports[{ipAddressReceiver, "in", incomingPort}]++;
  278. }
  279. /**
  280. * Creates a new statistics object.
  281. */
  282. statistics::statistics(void) {
  283. }
  284. /**
  285. * Stores the assignment IP address -> MAC address.
  286. * @param ipAddress The IP address belonging to the given MAC address.
  287. * @param macAddress The MAC address belonging to the given IP address.
  288. */
  289. void statistics::assignMacAddress(std::string ipAddress, std::string macAddress) {
  290. ip_mac_mapping[ipAddress] = macAddress;
  291. }
  292. /**
  293. * Registers statistical data for a sent packet. Increments the counter packets_sent for the sender and
  294. * packets_received for the receiver. Adds the bytes as kbytes_sent (sender) and kybtes_received (receiver).
  295. * @param ipAddressSender The IP address of the packet sender.
  296. * @param ipAddressReceiver The IP address of the packet receiver.
  297. * @param bytesSent The packet's size.
  298. */
  299. void statistics::addIpStat_packetSent(std::string filePath, std::string ipAddressSender, std::string ipAddressReceiver, long bytesSent, std::chrono::microseconds timestamp) {
  300. // Aidmar - Adding IP as a sender for first time
  301. if(ip_statistics[ipAddressSender].pkts_sent==0){
  302. // Add the IP class
  303. ip_statistics[ipAddressSender].ip_class = getIPv4Class(ipAddressSender);
  304. }
  305. // Aidmar - Adding IP as a receiver for first time
  306. if(ip_statistics[ipAddressReceiver].pkts_received==0){
  307. // Add the IP class
  308. ip_statistics[ipAddressReceiver].ip_class = getIPv4Class(ipAddressReceiver);
  309. }
  310. // Update stats for packet sender
  311. ip_statistics[ipAddressSender].kbytes_sent += (float(bytesSent) / 1024);
  312. ip_statistics[ipAddressSender].pkts_sent++;
  313. // Aidmar
  314. ip_statistics[ipAddressSender].pktsSentTimestamp.push_back(timestamp);
  315. // Update stats for packet receiver
  316. ip_statistics[ipAddressReceiver].kbytes_received += (float(bytesSent) / 1024);
  317. ip_statistics[ipAddressReceiver].pkts_received++;
  318. // Aidmar
  319. ip_statistics[ipAddressReceiver].pktsReceivedTimestamp.push_back(timestamp);
  320. }
  321. // Aidmar - comment out
  322. /**
  323. * Registers a value of the TCP option Maximum Segment Size (MSS).
  324. * @param ipAddress The IP address which sent the TCP packet.
  325. * @param MSSvalue The MSS value found.
  326. */
  327. //void statistics::addMSS(std::string ipAddress, int MSSvalue) {
  328. // ip_sumMss[ipAddress] += MSSvalue;
  329. //}
  330. /**
  331. * Setter for the timestamp_firstPacket field.
  332. * @param ts The timestamp of the first packet in the PCAP file.
  333. */
  334. void statistics::setTimestampFirstPacket(Tins::Timestamp ts) {
  335. timestamp_firstPacket = ts;
  336. }
  337. /**
  338. * Setter for the timestamp_lastPacket field.
  339. * @param ts The timestamp of the last packet in the PCAP file.
  340. */
  341. void statistics::setTimestampLastPacket(Tins::Timestamp ts) {
  342. timestamp_lastPacket = ts;
  343. }
  344. // Aidmar
  345. /**
  346. * Getter for the timestamp_firstPacket field.
  347. */
  348. Tins::Timestamp statistics::getTimestampFirstPacket() {
  349. return timestamp_firstPacket;
  350. }
  351. /**
  352. * Getter for the timestamp_lastPacket field.
  353. */
  354. Tins::Timestamp statistics::getTimestampLastPacket() {
  355. return timestamp_lastPacket;
  356. }
  357. /**
  358. * Getter for the packetCount field.
  359. */
  360. int statistics::getPacketCount() {
  361. return packetCount;
  362. }
  363. /**
  364. * Getter for the sumPacketSize field.
  365. */
  366. int statistics::getSumPacketSize() {
  367. return sumPacketSize;
  368. }
  369. /**
  370. * Calculates the capture duration.
  371. * @return a formatted string HH:MM:SS.mmmmmm with
  372. * HH: hour, MM: minute, SS: second, mmmmmm: microseconds
  373. */
  374. std::string statistics::getCaptureDurationTimestamp() const {
  375. // Calculate duration
  376. time_t t = (timestamp_lastPacket.seconds() - timestamp_firstPacket.seconds());
  377. time_t ms = (timestamp_lastPacket.microseconds() - timestamp_firstPacket.microseconds());
  378. long int hour = t / 3600;
  379. long int remainder = (t - hour * 3600);
  380. long int minute = remainder / 60;
  381. long int second = (remainder - minute * 60) % 60;
  382. long int microseconds = ms;
  383. // Build desired output format: YYYY-mm-dd hh:mm:ss
  384. char out[64];
  385. sprintf(out, "%02ld:%02ld:%02ld.%06ld ", hour, minute, second, microseconds);
  386. return std::string(out);
  387. }
  388. /**
  389. * Calculates the capture duration.
  390. * @return a formatted string SS.mmmmmm with
  391. * S: seconds (UNIX time), mmmmmm: microseconds
  392. */
  393. float statistics::getCaptureDurationSeconds() const {
  394. timeval d;
  395. d.tv_sec = timestamp_lastPacket.seconds() - timestamp_firstPacket.seconds();
  396. d.tv_usec = timestamp_lastPacket.microseconds() - timestamp_firstPacket.microseconds();
  397. char tmbuf[64], buf[64];
  398. auto nowtm = localtime(&(d.tv_sec));
  399. strftime(tmbuf, sizeof(tmbuf), "%S", nowtm);
  400. snprintf(buf, sizeof(buf), "%s.%06u", tmbuf, (uint) d.tv_usec);
  401. return std::stof(std::string(buf));
  402. }
  403. /**
  404. * Creates a timestamp based on a time_t seconds (UNIX time format) and microseconds.
  405. * @param seconds
  406. * @param microseconds
  407. * @return a formatted string Y-m-d H:M:S.m with
  408. * Y: year, m: month, d: day, H: hour, M: minute, S: second, m: microseconds
  409. */
  410. std::string statistics::getFormattedTimestamp(time_t seconds, suseconds_t microseconds) const {
  411. timeval tv;
  412. tv.tv_sec = seconds;
  413. tv.tv_usec = microseconds;
  414. char tmbuf[64], buf[64];
  415. auto nowtm = localtime(&(tv.tv_sec));
  416. strftime(tmbuf, sizeof(tmbuf), "%Y-%m-%d %H:%M:%S", nowtm);
  417. snprintf(buf, sizeof(buf), "%s.%06u", tmbuf, (uint) tv.tv_usec);
  418. return std::string(buf);
  419. }
  420. /**
  421. * Calculates the statistics for a given IP address.
  422. * @param ipAddress The IP address whose statistics should be calculated.
  423. * @return a ip_stats struct containing statistical data derived by the statistical data collected.
  424. */
  425. ip_stats statistics::getStatsForIP(std::string ipAddress) {
  426. float duration = getCaptureDurationSeconds();
  427. entry_ipStat ipStatEntry = ip_statistics[ipAddress];
  428. ip_stats s;
  429. s.bandwidthKBitsIn = (ipStatEntry.kbytes_received / duration) * 8;
  430. s.bandwidthKBitsOut = (ipStatEntry.kbytes_sent / duration) * 8;
  431. s.packetPerSecondIn = (ipStatEntry.pkts_received / duration);
  432. s.packetPerSecondOut = (ipStatEntry.pkts_sent / duration);
  433. s.AvgPacketSizeSent = (ipStatEntry.kbytes_sent / ipStatEntry.pkts_sent);
  434. s.AvgPacketSizeRecv = (ipStatEntry.kbytes_received / ipStatEntry.pkts_received);
  435. // Aidmar - comment out
  436. //int sumMSS = ip_sumMss[ipAddress];
  437. //int tcpPacketsSent = getProtocolCount(ipAddress, "TCP");
  438. //s.AvgMaxSegmentSizeTCP = ((sumMSS > 0 && tcpPacketsSent > 0) ? (sumMSS / tcpPacketsSent) : 0);
  439. return s;
  440. }
  441. /**
  442. * Increments the packet counter.
  443. */
  444. void statistics::incrementPacketCount() {
  445. packetCount++;
  446. }
  447. /**
  448. * Prints the statistics of the PCAP and IP specific statistics for the given IP address.
  449. * @param ipAddress The IP address whose statistics should be printed. Can be empty "" to print only general file statistics.
  450. */
  451. void statistics::printStats(std::string ipAddress) {
  452. std::stringstream ss;
  453. ss << std::endl;
  454. ss << "Capture duration: " << getCaptureDurationSeconds() << " seconds" << std::endl;
  455. ss << "Capture duration (HH:MM:SS.mmmmmm): " << getCaptureDurationTimestamp() << std::endl;
  456. ss << "#Packets: " << packetCount << std::endl;
  457. ss << std::endl;
  458. // Print IP address specific statistics only if IP address was given
  459. if (ipAddress != "") {
  460. entry_ipStat e = ip_statistics[ipAddress];
  461. ss << "\n----- STATS FOR IP ADDRESS [" << ipAddress << "] -------" << std::endl;
  462. ss << std::endl << "KBytes sent: " << e.kbytes_sent << std::endl;
  463. ss << "KBytes received: " << e.kbytes_received << std::endl;
  464. ss << "Packets sent: " << e.pkts_sent << std::endl;
  465. ss << "Packets received: " << e.pkts_received << "\n\n";
  466. ip_stats is = getStatsForIP(ipAddress);
  467. ss << "Bandwidth IN: " << is.bandwidthKBitsIn << " kbit/s" << std::endl;
  468. ss << "Bandwidth OUT: " << is.bandwidthKBitsOut << " kbit/s" << std::endl;
  469. ss << "Packets per second IN: " << is.packetPerSecondIn << std::endl;
  470. ss << "Packets per second OUT: " << is.packetPerSecondOut << std::endl;
  471. ss << "Avg Packet Size Sent: " << is.AvgPacketSizeSent << " kbytes" << std::endl;
  472. ss << "Avg Packet Size Received: " << is.AvgPacketSizeRecv << " kbytes" << std::endl;
  473. //ss << "Avg MSS: " << is.AvgMaxSegmentSizeTCP << " bytes" << std::endl;
  474. }
  475. std::cout << ss.str();
  476. }
  477. /**
  478. * Derives general PCAP file statistics from the collected statistical data and
  479. * writes all data into a SQLite database, located at database_path.
  480. * @param database_path The path of the SQLite database file ending with .sqlite3.
  481. */
  482. void statistics::writeToDatabase(std::string database_path) {
  483. // Generate general file statistics
  484. float duration = getCaptureDurationSeconds();
  485. long sumPacketsSent = 0, senderCountIP = 0;
  486. float sumBandwidthIn = 0.0, sumBandwidthOut = 0.0;
  487. for (auto i = ip_statistics.begin(); i != ip_statistics.end(); i++) {
  488. sumPacketsSent += i->second.pkts_sent;
  489. // Consumed bandwith (bytes) for sending packets
  490. sumBandwidthIn += (i->second.kbytes_received / duration);
  491. sumBandwidthOut += (i->second.kbytes_sent / duration);
  492. senderCountIP++;
  493. }
  494. float avgPacketRate = (packetCount / duration);
  495. long avgPacketSize = getAvgPacketSize();
  496. if(senderCountIP>0) {
  497. long avgPacketsSentPerHost = (sumPacketsSent / senderCountIP);
  498. float avgBandwidthInKBits = (sumBandwidthIn / senderCountIP) * 8;
  499. float avgBandwidthOutInKBits = (sumBandwidthOut / senderCountIP) * 8;
  500. // Create database and write information
  501. statistics_db db(database_path);
  502. db.writeStatisticsFile(packetCount, getCaptureDurationSeconds(),
  503. getFormattedTimestamp(timestamp_firstPacket.seconds(), timestamp_firstPacket.microseconds()),
  504. getFormattedTimestamp(timestamp_lastPacket.seconds(), timestamp_lastPacket.microseconds()),
  505. avgPacketRate, avgPacketSize, avgPacketsSentPerHost, avgBandwidthInKBits,
  506. avgBandwidthOutInKBits);
  507. db.writeStatisticsIP(ip_statistics);
  508. db.writeStatisticsTTL(ttl_distribution);
  509. db.writeStatisticsIpMac(ip_mac_mapping);
  510. //db.writeStatisticsMss(ip_sumMss);
  511. db.writeStatisticsPorts(ip_ports);
  512. db.writeStatisticsProtocols(protocol_distribution);
  513. // Aidmar
  514. db.writeStatisticsMss_dist(mss_distribution);
  515. db.writeStatisticsTos_dist(tos_distribution);
  516. db.writeStatisticsWin(win_distribution);
  517. db.writeStatisticsConv(conv_statistics);
  518. db.writeStatisticsInterval(interval_statistics);
  519. }
  520. else {
  521. // Tinslib failed to recognize the types of the packets in the input PCAP
  522. std::cout<<"ERROR: Statistics could not be collected from the input PCAP!"<<"\n";
  523. return;
  524. }
  525. }
  526. /**
  527. * Returns the average packet size.
  528. * @return a float indicating the average packet size in kbytes.
  529. */
  530. float statistics::getAvgPacketSize() const {
  531. // AvgPktSize = (Sum of all packet sizes / #Packets)
  532. return (sumPacketSize / packetCount) / 1024;
  533. }
  534. /**
  535. * Adds the size of a packet (to be used to calculate the avg. packet size).
  536. * @param packetSize The size of the current packet in bytes.
  537. */
  538. void statistics::addPacketSize(uint32_t packetSize) {
  539. sumPacketSize += ((float) packetSize);
  540. }
  541. // Aidmar
  542. void statistics::setDoExtraTests(bool var) {
  543. doExtraTests = var;
  544. }
  545. bool statistics::getDoExtraTests() {
  546. return doExtraTests;
  547. }