statistics.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  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, int previousPacketCount, float previousSumPacketSize){
  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. interval_statistics[lastPktTimestamp_s].pkts_count = packetCount - previousPacketCount;
  189. interval_statistics[lastPktTimestamp_s].kbytes = (float(sumPacketSize - previousSumPacketSize) / 1024);
  190. interval_statistics[lastPktTimestamp_s].payload_count = payloadCount;
  191. interval_statistics[lastPktTimestamp_s].incorrect_checksum_count = incorrectTCPChecksumCount;
  192. interval_statistics[lastPktTimestamp_s].correct_checksum_count = correctTCPChecksumCount;
  193. interval_statistics[lastPktTimestamp_s].invalid_tos_count = invalidToSCount;
  194. interval_statistics[lastPktTimestamp_s].valid_tos_count = validToSCount;
  195. std::cout<<invalidToSCount<<","<<validToSCount<<"\n";
  196. // Reset variables for next interval
  197. payloadCount = 0;
  198. incorrectTCPChecksumCount = 0;
  199. correctTCPChecksumCount = 0;
  200. invalidToSCount = 0;
  201. validToSCount = 0;
  202. if(ipEntopies.size()>1){
  203. interval_statistics[lastPktTimestamp_s].ip_src_entropy = ipEntopies[0];
  204. interval_statistics[lastPktTimestamp_s].ip_dst_entropy = ipEntopies[1];
  205. }
  206. if(ipCumEntopies.size()>1){
  207. interval_statistics[lastPktTimestamp_s].ip_src_cum_entropy = ipCumEntopies[0];
  208. interval_statistics[lastPktTimestamp_s].ip_dst_cum_entropy = ipCumEntopies[1];
  209. }
  210. }
  211. // Aidmar
  212. /**
  213. * Registers statistical data for a sent packet in a given conversation (two IPs, two ports).
  214. * Increments the counter packets_A_B or packets_B_A.
  215. * Adds the timestamp of the packet in pkts_A_B_timestamp or pkts_B_A_timestamp.
  216. * @param ipAddressSender The sender IP address.
  217. * @param sport The source port.
  218. * @param ipAddressReceiver The receiver IP address.
  219. * @param dport The destination port.
  220. * @param timestamp The timestamp of the packet.
  221. */
  222. void statistics::addConvStat(std::string ipAddressSender,int sport,std::string ipAddressReceiver,int dport, std::chrono::microseconds timestamp){
  223. conv f1 = {ipAddressReceiver, dport, ipAddressSender, sport};
  224. conv f2 = {ipAddressSender, sport, ipAddressReceiver, dport};
  225. // if already exist A(ipAddressReceiver, dport), B(ipAddressSender, sport)
  226. if (conv_statistics.count(f1)>0){
  227. conv_statistics[f1].pkts_B_A++; // increment packets number from B to A
  228. conv_statistics[f1].pkts_B_A_timestamp.push_back(timestamp);
  229. // Calculate reply delay considering only delay of first two reply packets (TCP handshake)
  230. //if(conv_statistics[f1].pkts_A_B_timestamp.size()>0 && conv_statistics[f1].pkts_A_B_timestamp.size()<=2){
  231. conv_statistics[f1].pkts_delay.push_back(std::chrono::duration_cast<std::chrono::microseconds> (timestamp - conv_statistics[f1].pkts_A_B_timestamp.back()));
  232. //}
  233. }
  234. else{
  235. conv_statistics[f2].pkts_A_B++; // increment packets number from A to B
  236. conv_statistics[f2].pkts_A_B_timestamp.push_back(timestamp);
  237. }
  238. }
  239. // Aidmar
  240. /**
  241. * Increments the packet counter for the given IP address and MSS value.
  242. * @param ipAddress The IP address whose MSS packet counter should be incremented.
  243. * @param mssValue The MSS value of the packet.
  244. */
  245. void statistics::incrementMSScount(std::string ipAddress, int mssValue) {
  246. mss_distribution[{ipAddress, mssValue}]++;
  247. }
  248. // Aidmar
  249. /**
  250. * Increments the packet counter for the given IP address and window size.
  251. * @param ipAddress The IP address whose window size packet counter should be incremented.
  252. * @param winSize The window size of the packet.
  253. */
  254. void statistics::incrementWinCount(std::string ipAddress, int winSize) {
  255. win_distribution[{ipAddress, winSize}]++;
  256. }
  257. /**
  258. * Increments the packet counter for the given IP address and TTL value.
  259. * @param ipAddress The IP address whose TTL packet counter should be incremented.
  260. * @param ttlValue The TTL value of the packet.
  261. */
  262. void statistics::incrementTTLcount(std::string ipAddress, int ttlValue) {
  263. ttl_distribution[{ipAddress, ttlValue}]++;
  264. }
  265. /**
  266. * Increments the protocol counter for the given IP address and protocol.
  267. * @param ipAddress The IP address whose protocol packet counter should be incremented.
  268. * @param protocol The protocol of the packet.
  269. */
  270. void statistics::incrementProtocolCount(std::string ipAddress, std::string protocol) {
  271. protocol_distribution[{ipAddress, protocol}]++;
  272. }
  273. /**
  274. * Returns the number of packets seen for the given IP address and protocol.
  275. * @param ipAddress The IP address whose packet count is wanted.
  276. * @param protocol The protocol whose packet count is wanted.
  277. * @return an integer: the number of packets
  278. */
  279. int statistics::getProtocolCount(std::string ipAddress, std::string protocol) {
  280. return protocol_distribution[{ipAddress, protocol}];
  281. }
  282. /**
  283. * Increments the packet counter for
  284. * - the given sender IP address with outgoing port and
  285. * - the given receiver IP address with incoming port.
  286. * @param ipAddressSender The IP address of the packet sender.
  287. * @param outgoingPort The port used by the sender.
  288. * @param ipAddressReceiver The IP address of the packet receiver.
  289. * @param incomingPort The port used by the receiver.
  290. */
  291. void statistics::incrementPortCount(std::string ipAddressSender, int outgoingPort, std::string ipAddressReceiver,
  292. int incomingPort) {
  293. ip_ports[{ipAddressSender, "out", outgoingPort}]++;
  294. ip_ports[{ipAddressReceiver, "in", incomingPort}]++;
  295. }
  296. /**
  297. * Creates a new statistics object.
  298. */
  299. statistics::statistics(void) {
  300. }
  301. /**
  302. * Stores the assignment IP address -> MAC address.
  303. * @param ipAddress The IP address belonging to the given MAC address.
  304. * @param macAddress The MAC address belonging to the given IP address.
  305. */
  306. void statistics::assignMacAddress(std::string ipAddress, std::string macAddress) {
  307. ip_mac_mapping[ipAddress] = macAddress;
  308. }
  309. /**
  310. * Registers statistical data for a sent packet. Increments the counter packets_sent for the sender and
  311. * packets_received for the receiver. Adds the bytes as kbytes_sent (sender) and kybtes_received (receiver).
  312. * @param ipAddressSender The IP address of the packet sender.
  313. * @param ipAddressReceiver The IP address of the packet receiver.
  314. * @param bytesSent The packet's size.
  315. */
  316. void statistics::addIpStat_packetSent(std::string filePath, std::string ipAddressSender, std::string ipAddressReceiver, long bytesSent, std::chrono::microseconds timestamp) {
  317. // Aidmar - Adding IP as a sender for first time
  318. if(ip_statistics[ipAddressSender].pkts_sent==0){
  319. // Add the IP class
  320. ip_statistics[ipAddressSender].ip_class = getIPv4Class(ipAddressSender);
  321. // Initialize packet rates
  322. /*ip_statistics[ipAddressSender].max_pkt_rate = 0;
  323. ip_statistics[ipAddressSender].min_pkt_rate = 0;
  324. // Caculate Mahoney anomaly score for ip.src
  325. float ipSrc_Mahoney_score = 0;
  326. // s_r: The number of IP sources (the different values)
  327. // n: The number of the total instances
  328. // s_t: The "time" since last anomalous (novel) IP was appeared
  329. int s_t = 0, n = 0, s_r = 0;
  330. for (auto i = ip_statistics.begin(); i != ip_statistics.end(); i++) {
  331. if (i->second.pkts_sent > 0)
  332. s_r++;
  333. }
  334. if(s_r > 0){
  335. // The number of the total instances
  336. n = packetCount;
  337. // The packet count when the last novel IP was added as a sender
  338. int pktCntNvlSndr = 0;
  339. for (auto i = ip_statistics.begin(); i != ip_statistics.end(); i++) {
  340. if (pktCntNvlSndr < i->second.firstAppearAsSenderPktCount)
  341. pktCntNvlSndr = i->second.firstAppearAsSenderPktCount;
  342. }
  343. // The "time" since last anomalous (novel) IP was appeared
  344. s_t = packetCount - pktCntNvlSndr + 1;
  345. ipSrc_Mahoney_score = (float)s_t*n/s_r;
  346. }
  347. ip_statistics[ipAddressSender].firstAppearAsSenderPktCount = packetCount;
  348. ip_statistics[ipAddressSender].sourceAnomalyScore = ipSrc_Mahoney_score;
  349. */
  350. }
  351. // Aidmar - Adding IP as a receiver for first time
  352. if(ip_statistics[ipAddressReceiver].pkts_received==0){
  353. // Add the IP class
  354. ip_statistics[ipAddressReceiver].ip_class = getIPv4Class(ipAddressReceiver);
  355. // Caculate Mahoney anomaly score for ip.dst
  356. /*float ipDst_Mahoney_score = 0;
  357. // s_r: The number of IP sources (the different values)
  358. // n: The number of the total instances
  359. // s_t: The "time" since last anomalous (novel) IP was appeared
  360. int s_t = 0, n = 0, s_r = 0;
  361. for (auto i = ip_statistics.begin(); i != ip_statistics.end(); i++) {
  362. if (i->second.pkts_received > 0)
  363. s_r++;
  364. }
  365. if(s_r > 0){
  366. // The number of the total instances
  367. n = packetCount;
  368. // The packet count when the last novel IP was added as a sender
  369. int pktCntNvlRcvr = 0;
  370. for (auto i = ip_statistics.begin(); i != ip_statistics.end(); i++) {
  371. if (pktCntNvlRcvr < i->second.firstAppearAsReceiverPktCount)
  372. pktCntNvlRcvr = i->second.firstAppearAsReceiverPktCount;
  373. }
  374. // The "time" since last anomalous (novel) IP was appeared
  375. s_t = packetCount - pktCntNvlRcvr + 1;
  376. ipDst_Mahoney_score = (float)s_t*n/s_r;
  377. }
  378. ip_statistics[ipAddressReceiver].firstAppearAsReceiverPktCount = packetCount;
  379. ip_statistics[ipAddressReceiver].destinationAnomalyScore = ipDst_Mahoney_score;
  380. */
  381. }
  382. // Update stats for packet sender
  383. ip_statistics[ipAddressSender].kbytes_sent += (float(bytesSent) / 1024);
  384. ip_statistics[ipAddressSender].pkts_sent++;
  385. // Aidmar
  386. ip_statistics[ipAddressSender].pktsSentTimestamp.push_back(timestamp);
  387. //// Aidmar - calculate packet rate (assumption: max_pkt_rate=1/smallest time between two consecutive pkts)
  388. // resulting in very big rates, therefore it could be better to calculate pkt rate on time intervals
  389. /*if(ip_statistics[ipAddressSender].pktsSentTimestamp.size() > 0){
  390. std::chrono::microseconds temp_pkt_consecutive_time = timestamp - ip_statistics[ipAddressSender].pktsSentTimestamp.back();
  391. float temp_pkt_rate = (float) 1000000/temp_pkt_consecutive_time.count(); // pkt per sec = 10**6/micro sec
  392. if(temp_pkt_rate > ip_statistics[ipAddressSender].max_pkt_rate || ip_statistics[ipAddressSender].max_pkt_rate == 0)
  393. ip_statistics[ipAddressSender].max_pkt_rate = temp_pkt_rate;
  394. if(temp_pkt_rate < ip_statistics[ipAddressSender].min_pkt_rate || ip_statistics[ipAddressSender].min_pkt_rate == 0)
  395. ip_statistics[ipAddressSender].min_pkt_rate = temp_pkt_rate;
  396. }*/
  397. // Update stats for packet receiver
  398. ip_statistics[ipAddressReceiver].kbytes_received += (float(bytesSent) / 1024);
  399. ip_statistics[ipAddressReceiver].pkts_received++;
  400. // Aidmar
  401. ip_statistics[ipAddressReceiver].pktsReceivedTimestamp.push_back(timestamp);
  402. }
  403. /**
  404. * Registers a value of the TCP option Maximum Segment Size (MSS).
  405. * @param ipAddress The IP address which sent the TCP packet.
  406. * @param MSSvalue The MSS value found.
  407. */
  408. void statistics::addMSS(std::string ipAddress, int MSSvalue) {
  409. ip_sumMss[ipAddress] += MSSvalue;
  410. }
  411. /**
  412. * Setter for the timestamp_firstPacket field.
  413. * @param ts The timestamp of the first packet in the PCAP file.
  414. */
  415. void statistics::setTimestampFirstPacket(Tins::Timestamp ts) {
  416. timestamp_firstPacket = ts;
  417. }
  418. /**
  419. * Setter for the timestamp_lastPacket field.
  420. * @param ts The timestamp of the last packet in the PCAP file.
  421. */
  422. void statistics::setTimestampLastPacket(Tins::Timestamp ts) {
  423. timestamp_lastPacket = ts;
  424. }
  425. // Aidmar
  426. /**
  427. * Getter for the timestamp_firstPacket field.
  428. */
  429. Tins::Timestamp statistics::getTimestampFirstPacket() {
  430. return timestamp_firstPacket;
  431. }
  432. /**
  433. * Getter for the timestamp_lastPacket field.
  434. */
  435. Tins::Timestamp statistics::getTimestampLastPacket() {
  436. return timestamp_lastPacket;
  437. }
  438. /**
  439. * Getter for the packetCount field.
  440. */
  441. int statistics::getPacketCount() {
  442. return packetCount;
  443. }
  444. /**
  445. * Getter for the sumPacketSize field.
  446. */
  447. int statistics::getSumPacketSize() {
  448. return sumPacketSize;
  449. }
  450. /**
  451. * Calculates the capture duration.
  452. * @return a formatted string HH:MM:SS.mmmmmm with
  453. * HH: hour, MM: minute, SS: second, mmmmmm: microseconds
  454. */
  455. std::string statistics::getCaptureDurationTimestamp() const {
  456. // Calculate duration
  457. time_t t = (timestamp_lastPacket.seconds() - timestamp_firstPacket.seconds());
  458. time_t ms = (timestamp_lastPacket.microseconds() - timestamp_firstPacket.microseconds());
  459. long int hour = t / 3600;
  460. long int remainder = (t - hour * 3600);
  461. long int minute = remainder / 60;
  462. long int second = (remainder - minute * 60) % 60;
  463. long int microseconds = ms;
  464. // Build desired output format: YYYY-mm-dd hh:mm:ss
  465. char out[64];
  466. sprintf(out, "%02ld:%02ld:%02ld.%06ld ", hour, minute, second, microseconds);
  467. return std::string(out);
  468. }
  469. /**
  470. * Calculates the capture duration.
  471. * @return a formatted string SS.mmmmmm with
  472. * S: seconds (UNIX time), mmmmmm: microseconds
  473. */
  474. float statistics::getCaptureDurationSeconds() const {
  475. timeval d;
  476. d.tv_sec = timestamp_lastPacket.seconds() - timestamp_firstPacket.seconds();
  477. d.tv_usec = timestamp_lastPacket.microseconds() - timestamp_firstPacket.microseconds();
  478. char tmbuf[64], buf[64];
  479. auto nowtm = localtime(&(d.tv_sec));
  480. strftime(tmbuf, sizeof(tmbuf), "%S", nowtm);
  481. snprintf(buf, sizeof(buf), "%s.%06u", tmbuf, (uint) d.tv_usec);
  482. return std::stof(std::string(buf));
  483. }
  484. /**
  485. * Creates a timestamp based on a time_t seconds (UNIX time format) and microseconds.
  486. * @param seconds
  487. * @param microseconds
  488. * @return a formatted string Y-m-d H:M:S.m with
  489. * Y: year, m: month, d: day, H: hour, M: minute, S: second, m: microseconds
  490. */
  491. std::string statistics::getFormattedTimestamp(time_t seconds, suseconds_t microseconds) const {
  492. timeval tv;
  493. tv.tv_sec = seconds;
  494. tv.tv_usec = microseconds;
  495. char tmbuf[64], buf[64];
  496. auto nowtm = localtime(&(tv.tv_sec));
  497. strftime(tmbuf, sizeof(tmbuf), "%Y-%m-%d %H:%M:%S", nowtm);
  498. snprintf(buf, sizeof(buf), "%s.%06u", tmbuf, (uint) tv.tv_usec);
  499. return std::string(buf);
  500. }
  501. /**
  502. * Calculates the statistics for a given IP address.
  503. * @param ipAddress The IP address whose statistics should be calculated.
  504. * @return a ip_stats struct containing statistical data derived by the statistical data collected.
  505. */
  506. ip_stats statistics::getStatsForIP(std::string ipAddress) {
  507. float duration = getCaptureDurationSeconds();
  508. entry_ipStat ipStatEntry = ip_statistics[ipAddress];
  509. ip_stats s;
  510. s.bandwidthKBitsIn = (ipStatEntry.kbytes_received / duration) * 8;
  511. s.bandwidthKBitsOut = (ipStatEntry.kbytes_sent / duration) * 8;
  512. s.packetPerSecondIn = (ipStatEntry.pkts_received / duration);
  513. s.packetPerSecondOut = (ipStatEntry.pkts_sent / duration);
  514. s.AvgPacketSizeSent = (ipStatEntry.kbytes_sent / ipStatEntry.pkts_sent);
  515. s.AvgPacketSizeRecv = (ipStatEntry.kbytes_received / ipStatEntry.pkts_received);
  516. int sumMSS = ip_sumMss[ipAddress];
  517. int tcpPacketsSent = getProtocolCount(ipAddress, "TCP");
  518. s.AvgMaxSegmentSizeTCP = ((sumMSS > 0 && tcpPacketsSent > 0) ? (sumMSS / tcpPacketsSent) : 0);
  519. return s;
  520. }
  521. /**
  522. * Increments the packet counter.
  523. */
  524. void statistics::incrementPacketCount() {
  525. packetCount++;
  526. }
  527. /**
  528. * Prints the statistics of the PCAP and IP specific statistics for the given IP address.
  529. * @param ipAddress The IP address whose statistics should be printed. Can be empty "" to print only general file statistics.
  530. */
  531. void statistics::printStats(std::string ipAddress) {
  532. std::stringstream ss;
  533. ss << std::endl;
  534. ss << "Capture duration: " << getCaptureDurationSeconds() << " seconds" << std::endl;
  535. ss << "Capture duration (HH:MM:SS.mmmmmm): " << getCaptureDurationTimestamp() << std::endl;
  536. ss << "#Packets: " << packetCount << std::endl;
  537. ss << std::endl;
  538. // Print IP address specific statistics only if IP address was given
  539. if (ipAddress != "") {
  540. entry_ipStat e = ip_statistics[ipAddress];
  541. ss << "\n----- STATS FOR IP ADDRESS [" << ipAddress << "] -------" << std::endl;
  542. ss << std::endl << "KBytes sent: " << e.kbytes_sent << std::endl;
  543. ss << "KBytes received: " << e.kbytes_received << std::endl;
  544. ss << "Packets sent: " << e.pkts_sent << std::endl;
  545. ss << "Packets received: " << e.pkts_received << "\n\n";
  546. ip_stats is = getStatsForIP(ipAddress);
  547. ss << "Bandwidth IN: " << is.bandwidthKBitsIn << " kbit/s" << std::endl;
  548. ss << "Bandwidth OUT: " << is.bandwidthKBitsOut << " kbit/s" << std::endl;
  549. ss << "Packets per second IN: " << is.packetPerSecondIn << std::endl;
  550. ss << "Packets per second OUT: " << is.packetPerSecondOut << std::endl;
  551. ss << "Avg Packet Size Sent: " << is.AvgPacketSizeSent << " kbytes" << std::endl;
  552. ss << "Avg Packet Size Received: " << is.AvgPacketSizeRecv << " kbytes" << std::endl;
  553. ss << "Avg MSS: " << is.AvgMaxSegmentSizeTCP << " bytes" << std::endl;
  554. }
  555. std::cout << ss.str();
  556. }
  557. /**
  558. * Derives general PCAP file statistics from the collected statistical data and
  559. * writes all data into a SQLite database, located at database_path.
  560. * @param database_path The path of the SQLite database file ending with .sqlite3.
  561. */
  562. void statistics::writeToDatabase(std::string database_path) {
  563. // Generate general file statistics
  564. float duration = getCaptureDurationSeconds();
  565. long sumPacketsSent = 0, senderCountIP = 0;
  566. float sumBandwidthIn = 0.0, sumBandwidthOut = 0.0;
  567. for (auto i = ip_statistics.begin(); i != ip_statistics.end(); i++) {
  568. sumPacketsSent += i->second.pkts_sent;
  569. // Consumed bandwith (bytes) for sending packets
  570. sumBandwidthIn += (i->second.kbytes_received / duration);
  571. sumBandwidthOut += (i->second.kbytes_sent / duration);
  572. senderCountIP++;
  573. }
  574. float avgPacketRate = (packetCount / duration);
  575. long avgPacketSize = getAvgPacketSize();
  576. long avgPacketsSentPerHost = (sumPacketsSent / senderCountIP);
  577. float avgBandwidthInKBits = (sumBandwidthIn / senderCountIP) * 8;
  578. float avgBandwidthOutInKBits = (sumBandwidthOut / senderCountIP) * 8;
  579. // Create database and write information
  580. statistics_db db(database_path);
  581. db.writeStatisticsFile(packetCount, getCaptureDurationSeconds(),
  582. getFormattedTimestamp(timestamp_firstPacket.seconds(), timestamp_firstPacket.microseconds()),
  583. getFormattedTimestamp(timestamp_lastPacket.seconds(), timestamp_lastPacket.microseconds()),
  584. avgPacketRate, avgPacketSize, avgPacketsSentPerHost, avgBandwidthInKBits,
  585. avgBandwidthOutInKBits);
  586. db.writeStatisticsIP(ip_statistics);
  587. db.writeStatisticsTTL(ttl_distribution);
  588. db.writeStatisticsIpMac(ip_mac_mapping);
  589. db.writeStatisticsMss(ip_sumMss);
  590. db.writeStatisticsPorts(ip_ports);
  591. db.writeStatisticsProtocols(protocol_distribution);
  592. // Aidmar
  593. db.writeStatisticsMss_dist(mss_distribution);
  594. db.writeStatisticsWin(win_distribution);
  595. db.writeStatisticsConv(conv_statistics);
  596. db.writeStatisticsInterval(interval_statistics);
  597. // Aidmar - Tests
  598. }
  599. /**
  600. * Returns the average packet size.
  601. * @return a float indicating the average packet size in kbytes.
  602. */
  603. float statistics::getAvgPacketSize() const {
  604. // AvgPktSize = (Sum of all packet sizes / #Packets)
  605. return (sumPacketSize / packetCount) / 1024;
  606. }
  607. /**
  608. * Adds the size of a packet (to be used to calculate the avg. packet size).
  609. * @param packetSize The size of the current packet in bytes.
  610. */
  611. void statistics::addPacketSize(uint32_t packetSize) {
  612. sumPacketSize += ((float) packetSize);
  613. }
  614. // Aidmar
  615. void statistics::setDoTests(bool var) {
  616. doTests = var;
  617. }
  618. bool statistics::getDoTests() {
  619. return doTests;
  620. }