statistics.cpp 27 KB

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