statistics.cpp 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <math.h>
  5. #include "statistics.h"
  6. #include <sstream>
  7. #include <SQLiteCpp/SQLiteCpp.h>
  8. #include "statistics_db.h"
  9. #include "statistics.h"
  10. #include "utilities.h"
  11. using namespace Tins;
  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. /**
  27. * Checks the correctness of TCP checksum and increments counter if the checksum was incorrect.
  28. * @param ipAddressSender The source IP.
  29. * @param ipAddressReceiver The destination IP.
  30. * @param tcpPkt The packet to get checked.
  31. */
  32. void statistics::checkTCPChecksum(const std::string &ipAddressSender, const std::string &ipAddressReceiver, TCP tcpPkt) {
  33. if(this->getDoExtraTests()) {
  34. if(check_tcpChecksum(ipAddressSender, ipAddressReceiver, tcpPkt))
  35. correctTCPChecksumCount++;
  36. else incorrectTCPChecksumCount++;
  37. }
  38. }
  39. /**
  40. * Calculates entropy of the source and destination IPs in a time interval.
  41. * @param intervalStartTimestamp The timstamp where the interval starts.
  42. * @return a vector: contains source IP entropy and destination IP entropy.
  43. */
  44. std::vector<double> statistics::calculateLastIntervalIPsEntropy(std::chrono::microseconds intervalStartTimestamp){
  45. if(this->getDoExtraTests()) {
  46. // TODO: change datastructures
  47. std::vector<long> IPsSrcPktsCounts;
  48. std::vector<long> IPsDstPktsCounts;
  49. std::vector<long> IPsSrcNovelPktsCounts;
  50. std::vector<long> IPsDstNovelPktsCounts;
  51. std::vector<double> IPsSrcProb;
  52. std::vector<double> IPsDstProb;
  53. std::vector<double> IPsSrcNovelProb;
  54. std::vector<double> IPsDstNovelProb;
  55. long pktsSent = 0, pktsReceived = 0, novelPktsSent = 0, novelPktsReceived = 0;
  56. for (auto i = ip_statistics.begin(); i != ip_statistics.end(); i++) {
  57. long IPsSrcPktsCount = 0;
  58. long IPsSrcNovelPktsCount = 0;
  59. if (intervalCumIPStats.count(i->first) == 0) {
  60. IPsSrcPktsCount = i->second.pkts_sent;
  61. IPsSrcNovelPktsCount = i->second.pkts_sent;
  62. IPsSrcNovelPktsCounts.push_back(IPsSrcNovelPktsCount);
  63. novelPktsSent += IPsSrcNovelPktsCount;
  64. } else {
  65. IPsSrcPktsCount = i->second.pkts_sent-intervalCumIPStats[i->first].pkts_sent;
  66. }
  67. if(IPsSrcPktsCount != 0) {
  68. IPsSrcPktsCounts.push_back(IPsSrcPktsCount);
  69. pktsSent += IPsSrcPktsCount;
  70. }
  71. long IPsDstPktsCount = 0;
  72. long IPsDstNovelPktsCount = 0;
  73. if (intervalCumIPStats.count(i->first) == 0) {
  74. IPsDstPktsCount = i->second.pkts_received;
  75. IPsDstNovelPktsCount = i->second.pkts_received;
  76. IPsDstNovelPktsCounts.push_back(IPsDstNovelPktsCount);
  77. novelPktsReceived += IPsDstNovelPktsCount;
  78. } else {
  79. IPsDstPktsCount = i->second.pkts_received-intervalCumIPStats[i->first].pkts_received;
  80. }
  81. if(IPsDstPktsCount != 0) {
  82. IPsDstPktsCounts.push_back(IPsDstPktsCount);
  83. pktsReceived += IPsDstPktsCount;
  84. }
  85. }
  86. for (auto i = IPsSrcPktsCounts.begin(); i != IPsSrcPktsCounts.end(); i++) {
  87. IPsSrcProb.push_back(static_cast<double>(*i) / static_cast<double>(pktsSent));
  88. }
  89. for (auto i = IPsDstPktsCounts.begin(); i != IPsDstPktsCounts.end(); i++) {
  90. IPsDstProb.push_back(static_cast<double>(*i) / static_cast<double>(pktsReceived));
  91. }
  92. for (auto i = IPsSrcNovelPktsCounts.begin(); i != IPsSrcNovelPktsCounts.end(); i++) {
  93. IPsSrcNovelProb.push_back(static_cast<double>(*i) / static_cast<double>(novelPktsSent));
  94. }
  95. for (auto i = IPsDstNovelPktsCounts.begin(); i != IPsDstNovelPktsCounts.end(); i++) {
  96. IPsDstNovelProb.push_back(static_cast<double>(*i) / static_cast<double>(novelPktsReceived));
  97. }
  98. // Calculate IP source entropy
  99. double IPsSrcEntropy = 0;
  100. for (unsigned i = 0; i < IPsSrcProb.size(); i++) {
  101. if (IPsSrcProb[i] > 0)
  102. IPsSrcEntropy += -IPsSrcProb[i] * log2(IPsSrcProb[i]);
  103. }
  104. // Calculate IP destination entropy
  105. double IPsDstEntropy = 0;
  106. for (unsigned i = 0; i < IPsDstProb.size(); i++) {
  107. if (IPsDstProb[i] > 0)
  108. IPsDstEntropy += -IPsDstProb[i] * log2(IPsDstProb[i]);
  109. }
  110. // Calculate IP source novel entropy
  111. double IPsSrcNovelEntropy = 0;
  112. for (unsigned i = 0; i < IPsSrcNovelProb.size(); i++) {
  113. if (IPsSrcNovelProb[i] > 0)
  114. IPsSrcNovelEntropy += -IPsSrcNovelProb[i] * log2(IPsSrcNovelProb[i]);
  115. }
  116. // Calculate IP destination novel entropy
  117. double IPsDstNovelEntropy = 0;
  118. for (unsigned i = 0; i < IPsDstNovelProb.size(); i++) {
  119. if (IPsDstNovelProb[i] > 0)
  120. IPsDstNovelEntropy += -IPsDstNovelProb[i] * log2(IPsDstNovelProb[i]);
  121. }
  122. this->ip_src_novel_count = IPsSrcNovelPktsCounts.size();
  123. this->ip_dst_novel_count = IPsDstNovelPktsCounts.size();
  124. double norm_src_entropy = 0;
  125. if (IPsSrcPktsCounts.size() > 0 && log2(IPsSrcPktsCounts.size()) > 0) {
  126. norm_src_entropy = IPsSrcEntropy / log2(IPsSrcPktsCounts.size());
  127. }
  128. double norm_dst_entropy = 0;
  129. if (IPsDstPktsCounts.size() > 0 && log2(IPsDstPktsCounts.size()) > 0) {
  130. norm_dst_entropy = IPsDstEntropy / log2(IPsDstPktsCounts.size());
  131. }
  132. double norm_novel_src_entropy = 0;
  133. if (IPsSrcNovelPktsCounts.size() > 0 && log2(IPsSrcNovelPktsCounts.size()) > 0) {
  134. norm_novel_src_entropy = IPsSrcNovelEntropy / log2(IPsSrcNovelPktsCounts.size());
  135. }
  136. double norm_novel_dst_entropy = 0;
  137. if (IPsDstNovelPktsCounts.size() > 0 && log2(IPsDstNovelPktsCounts.size()) > 0) {
  138. norm_novel_dst_entropy = IPsDstNovelEntropy / log2(IPsDstNovelPktsCounts.size());
  139. }
  140. std::vector<double> entropies = {IPsSrcEntropy, IPsDstEntropy, IPsSrcNovelEntropy, IPsDstNovelEntropy, norm_src_entropy, norm_dst_entropy, norm_novel_src_entropy, norm_novel_dst_entropy};
  141. return entropies;
  142. }
  143. else {
  144. return {-1, -1, -1, -1, -1, -1, -1, -1};
  145. }
  146. }
  147. /**
  148. * Calculates the cumulative entropy of the source and destination IPs, i.e., the entropy for packets from the beginning of the pcap file.
  149. * @return a vector: contains the cumulative entropies of source and destination IPs
  150. */
  151. std::vector<double> statistics::calculateIPsCumEntropy(){
  152. if(this->getDoExtraTests()) {
  153. std::vector <std::string> IPs;
  154. std::vector <double> IPsSrcProb;
  155. std::vector <double> IPsDstProb;
  156. for (auto i = ip_statistics.begin(); i != ip_statistics.end(); i++) {
  157. IPs.push_back(i->first);
  158. IPsSrcProb.push_back(static_cast<double>(i->second.pkts_sent/packetCount));
  159. IPsDstProb.push_back(static_cast<double>(i->second.pkts_received/packetCount));
  160. }
  161. // Calculate IP source entropy
  162. double IPsSrcEntropy = 0;
  163. for(unsigned i=0; i < IPsSrcProb.size();i++){
  164. if (IPsSrcProb[i] > 0)
  165. IPsSrcEntropy += - IPsSrcProb[i]*log2(IPsSrcProb[i]);
  166. }
  167. // Calculate IP destination entropy
  168. double IPsDstEntropy = 0;
  169. for(unsigned i=0; i < IPsDstProb.size();i++){
  170. if (IPsDstProb[i] > 0)
  171. IPsDstEntropy += - IPsDstProb[i]*log2(IPsDstProb[i]);
  172. }
  173. double norm_src_entropy = 0;
  174. if (IPsSrcProb.size() > 0 && log2(IPsDstProb.size()) > 0) {
  175. norm_src_entropy = IPsSrcEntropy / log2(IPsSrcProb.size());
  176. }
  177. double norm_dst_entropy = 0;
  178. if (IPsDstProb.size() > 0 && log2(IPsDstProb.size()) > 0) {
  179. norm_dst_entropy = IPsDstEntropy / log2(IPsDstProb.size());
  180. }
  181. std::vector<double> entropies = {IPsSrcEntropy, IPsDstEntropy, norm_src_entropy, norm_dst_entropy};
  182. return entropies;
  183. }
  184. else {
  185. return {-1, -1, -1, -1};
  186. }
  187. }
  188. /**
  189. * Calculates sending packet rate for each IP in a time interval. Finds min and max packet rate and adds them to ip_statistics map.
  190. * @param intervalStartTimestamp The timstamp where the interval starts.
  191. */
  192. void statistics::calculateIPIntervalPacketRate(std::chrono::duration<int, std::micro> interval, std::chrono::microseconds intervalStartTimestamp){
  193. for (auto i = ip_statistics.begin(); i != ip_statistics.end(); i++) {
  194. int IPsSrcPktsCount = 0;
  195. for (auto j = i->second.pkts_sent_timestamp.begin(); j != i->second.pkts_sent_timestamp.end(); j++) {
  196. if(*j >= intervalStartTimestamp)
  197. IPsSrcPktsCount++;
  198. }
  199. float interval_pkt_rate = (float) IPsSrcPktsCount * 1000000 / interval.count(); // used 10^6 because interval in microseconds
  200. i->second.interval_pkt_rate.push_back(interval_pkt_rate);
  201. if(interval_pkt_rate > i->second.max_interval_pkt_rate || i->second.max_interval_pkt_rate == 0)
  202. i->second.max_interval_pkt_rate = interval_pkt_rate;
  203. if(interval_pkt_rate < i->second.min_interval_pkt_rate || i->second.min_interval_pkt_rate == 0)
  204. i->second.min_interval_pkt_rate = interval_pkt_rate;
  205. }
  206. }
  207. /**
  208. * Calculates the entropies for the count of integer values.
  209. * @param current map containing the values with counts
  210. * @param an old map containing the values with counts (from last iteration)
  211. * @return a vector containing the calculated entropies: entropy of all updated values, entropy of all novel values, normalized entropy of all, normalized entropy of novel
  212. */
  213. std::vector<double> statistics::calculateEntropies(std::unordered_map<int, int> &map, std::unordered_map<int, int> &old) {
  214. std::vector<double> counts;
  215. int count_total = 0;
  216. double entropy = 0.0;
  217. std::vector<double> novel_counts;
  218. int novel_count_total = 0;
  219. double novel_entropy = 0.0;
  220. // iterate over all values
  221. for (auto iter: map) {
  222. if (old.count(iter.first) == 0) {
  223. // count novel values
  224. double novel_count = static_cast<double>(iter.second);
  225. counts.push_back(novel_count);
  226. count_total += novel_count;
  227. novel_counts.push_back(novel_count);
  228. novel_count_total += novel_count;
  229. } else if (old.count(iter.first) != map.count(iter.first)) {
  230. // count all increased values
  231. double count = static_cast<double>(iter.second-old[iter.first]);
  232. if (count != 0.0) {
  233. counts.push_back(count);
  234. count_total += count;
  235. }
  236. }
  237. }
  238. // calculate entropy
  239. for (auto count: counts) {
  240. double prob = count / static_cast<double>(count_total);
  241. entropy += -1 * prob * log2(prob);
  242. }
  243. // calculate novelty entropy
  244. for (auto novel_count: novel_counts) {
  245. double novel_prob = novel_count / static_cast<double>(novel_count_total);
  246. novel_entropy += -1 * novel_prob * log2(novel_prob);
  247. }
  248. double norm_entropy = 0;
  249. if (counts.size() > 0 && log2(counts.size()) > 0) {
  250. norm_entropy = entropy / log2(counts.size());
  251. }
  252. double norm_novel_entropy = 0;
  253. if (novel_counts.size() > 0 && log2(novel_counts.size()) > 0) {
  254. norm_novel_entropy = novel_entropy / log2(novel_counts.size());
  255. }
  256. return {entropy, novel_entropy, norm_entropy, norm_novel_entropy};
  257. }
  258. /**
  259. * Registers statistical data for a time interval.
  260. * @param intervalStartTimestamp The timstamp where the interval starts.
  261. * @param intervalEndTimestamp The timstamp where the interval ends.
  262. * @param previousPacketCount The total number of packets in last interval.
  263. */
  264. void statistics::addIntervalStat(std::chrono::duration<int, std::micro> interval, std::chrono::microseconds intervalStartTimestamp, std::chrono::microseconds intervalEndTimestamp){
  265. // Add packet rate for each IP to ip_statistics map
  266. calculateIPIntervalPacketRate(interval, intervalStartTimestamp);
  267. std::vector<double> ipEntopies = calculateLastIntervalIPsEntropy(intervalStartTimestamp);
  268. std::vector<double> ipCumEntopies = calculateIPsCumEntropy();
  269. std::string lastPktTimestamp_s = std::to_string(intervalEndTimestamp.count());
  270. std::string intervalStartTimestamp_s = std::to_string(intervalStartTimestamp.count());
  271. // The intervalStartTimestamp_s is the previous interval lastPktTimestamp_s
  272. // TODO: check with carlos if first and last packet timestamps are alright
  273. interval_statistics[lastPktTimestamp_s].start = std::to_string(intervalStartTimestamp.count());
  274. interval_statistics[lastPktTimestamp_s].end = std::to_string(intervalEndTimestamp.count());
  275. interval_statistics[lastPktTimestamp_s].pkts_count = packetCount - intervalCumPktCount;
  276. interval_statistics[lastPktTimestamp_s].pkt_rate = static_cast<float>(interval_statistics[lastPktTimestamp_s].pkts_count) / (static_cast<double>(interval.count()) / 1000000);
  277. interval_statistics[lastPktTimestamp_s].kbytes = static_cast<float>(sumPacketSize - intervalCumSumPktSize) / 1024;
  278. interval_statistics[lastPktTimestamp_s].kbyte_rate = interval_statistics[lastPktTimestamp_s].kbytes / (static_cast<double>(interval.count()) / 1000000);
  279. interval_statistics[lastPktTimestamp_s].payload_count = payloadCount - intervalPayloadCount;
  280. interval_statistics[lastPktTimestamp_s].incorrect_tcp_checksum_count = incorrectTCPChecksumCount - intervalIncorrectTCPChecksumCount;
  281. interval_statistics[lastPktTimestamp_s].correct_tcp_checksum_count = correctTCPChecksumCount - intervalCorrectTCPChecksumCount;
  282. interval_statistics[lastPktTimestamp_s].novel_ip_src_count = this->ip_src_novel_count;
  283. interval_statistics[lastPktTimestamp_s].novel_ip_dst_count = this->ip_dst_novel_count;
  284. interval_statistics[lastPktTimestamp_s].novel_ttl_count = static_cast<int>(ttl_values.size()) - intervalCumNovelTTLCount;
  285. interval_statistics[lastPktTimestamp_s].novel_win_size_count = static_cast<int>(win_values.size()) - intervalCumNovelWinSizeCount;
  286. interval_statistics[lastPktTimestamp_s].novel_tos_count = static_cast<int>(tos_values.size()) - intervalCumNovelToSCount;
  287. interval_statistics[lastPktTimestamp_s].novel_mss_count = static_cast<int>(mss_values.size()) - intervalCumNovelMSSCount;
  288. interval_statistics[lastPktTimestamp_s].novel_port_count = static_cast<int>(port_values.size()) - intervalCumNovelPortCount;
  289. interval_statistics[lastPktTimestamp_s].ttl_entropies = calculateEntropies(ttl_values, intervalCumTTLValues);
  290. interval_statistics[lastPktTimestamp_s].win_size_entropies = calculateEntropies(win_values, intervalCumWinSizeValues);
  291. interval_statistics[lastPktTimestamp_s].tos_entropies = calculateEntropies(tos_values, intervalCumTosValues);
  292. interval_statistics[lastPktTimestamp_s].mss_entropies = calculateEntropies(mss_values, intervalCumMSSValues);
  293. interval_statistics[lastPktTimestamp_s].port_entropies = calculateEntropies(port_values, intervalCumPortValues);
  294. intervalPayloadCount = payloadCount;
  295. intervalIncorrectTCPChecksumCount = incorrectTCPChecksumCount;
  296. intervalCorrectTCPChecksumCount = correctTCPChecksumCount;
  297. intervalCumPktCount = packetCount;
  298. intervalCumSumPktSize = sumPacketSize;
  299. intervalCumNovelIPCount = static_cast<int>(ip_statistics.size());
  300. intervalCumNovelTTLCount = static_cast<int>(ttl_values.size());
  301. intervalCumNovelWinSizeCount = static_cast<int>(win_values.size());
  302. intervalCumNovelToSCount =static_cast<int>(tos_values.size());
  303. intervalCumNovelMSSCount = static_cast<int>(mss_values.size());
  304. intervalCumNovelPortCount = static_cast<int>(port_values.size());
  305. intervalCumIPStats = ip_statistics;
  306. intervalCumTTLValues = ttl_values;
  307. intervalCumWinSizeValues = win_values;
  308. intervalCumTosValues = tos_values;
  309. intervalCumMSSValues = mss_values;
  310. intervalCumPortValues = port_values;
  311. interval_statistics[lastPktTimestamp_s].ip_entropies = ipEntopies;
  312. interval_statistics[lastPktTimestamp_s].ip_cum_entropies = ipCumEntopies;
  313. }
  314. /**
  315. * Registers statistical data for a sent packet in a given conversation (two IPs, two ports).
  316. * Increments the counter packets_A_B or packets_B_A.
  317. * Adds the timestamp of the packet in pkts_A_B_timestamp or pkts_B_A_timestamp.
  318. * @param ipAddressSender The sender IP address.
  319. * @param sport The source port.
  320. * @param ipAddressReceiver The receiver IP address.
  321. * @param dport The destination port.
  322. * @param timestamp The timestamp of the packet.
  323. */
  324. void statistics::addConvStat(const std::string &ipAddressSender,int sport,const std::string &ipAddressReceiver,int dport, std::chrono::microseconds timestamp){
  325. conv f1 = {ipAddressReceiver, dport, ipAddressSender, sport};
  326. conv f2 = {ipAddressSender, sport, ipAddressReceiver, dport};
  327. // if already exist A(ipAddressReceiver, dport), B(ipAddressSender, sport) conversation
  328. if (conv_statistics.count(f1)>0){
  329. conv_statistics[f1].pkts_count++;
  330. if(conv_statistics[f1].pkts_count<=3)
  331. conv_statistics[f1].interarrival_time.push_back(std::chrono::duration_cast<std::chrono::microseconds> (timestamp - conv_statistics[f1].pkts_timestamp.back()));
  332. conv_statistics[f1].pkts_timestamp.push_back(timestamp);
  333. }
  334. // Add new conversation A(ipAddressSender, sport), B(ipAddressReceiver, dport)
  335. else{
  336. conv_statistics[f2].pkts_count++;
  337. if(conv_statistics[f2].pkts_timestamp.size()>0 && conv_statistics[f2].pkts_count<=3 )
  338. conv_statistics[f2].interarrival_time.push_back(std::chrono::duration_cast<std::chrono::microseconds> (timestamp - conv_statistics[f2].pkts_timestamp.back()));
  339. conv_statistics[f2].pkts_timestamp.push_back(timestamp);
  340. }
  341. }
  342. /**
  343. * Registers statistical data for a sent packet in a given extended conversation (two IPs, two ports, protocol).
  344. * Increments the counter packets_A_B or packets_B_A.
  345. * Adds the timestamp of the packet in pkts_A_B_timestamp or pkts_B_A_timestamp.
  346. * Updates all other statistics of conv_statistics_extended
  347. * @param ipAddressSender The sender IP address.
  348. * @param sport The source port.
  349. * @param ipAddressReceiver The receiver IP address.
  350. * @param dport The destination port.
  351. * @param protocol The used protocol.
  352. * @param timestamp The timestamp of the packet.
  353. */
  354. void statistics::addConvStatExt(const std::string &ipAddressSender,int sport,const std::string &ipAddressReceiver,int dport,const std::string &protocol, std::chrono::microseconds timestamp){
  355. if(this->getDoExtraTests()) {
  356. convWithProt f1 = {ipAddressReceiver, dport, ipAddressSender, sport, protocol};
  357. convWithProt f2 = {ipAddressSender, sport, ipAddressReceiver, dport, protocol};
  358. convWithProt f;
  359. // if there already exists a communication interval for the specified conversation
  360. if (conv_statistics_extended.count(f1) > 0 || conv_statistics_extended.count(f2) > 0){
  361. // find out which direction of conversation is contained in conv_statistics_extended
  362. if (conv_statistics_extended.count(f1) > 0)
  363. f = f1;
  364. else
  365. f = f2;
  366. // increase pkts count and check on delay
  367. conv_statistics_extended[f].pkts_count++;
  368. if (conv_statistics_extended[f].pkts_timestamp.size()>0 && conv_statistics_extended[f].pkts_count<=3)
  369. conv_statistics_extended[f].interarrival_time.push_back(std::chrono::duration_cast<std::chrono::microseconds> (timestamp - conv_statistics_extended[f].pkts_timestamp.back()));
  370. conv_statistics_extended[f].pkts_timestamp.push_back(timestamp);
  371. // if the time difference has exceeded the threshold, create a new interval with this message
  372. if (timestamp - conv_statistics_extended[f].comm_intervals.back().end > (std::chrono::microseconds) ((unsigned long) COMM_INTERVAL_THRESHOLD)) { // > or >= ?
  373. commInterval new_interval = {timestamp, timestamp, 1};
  374. conv_statistics_extended[f].comm_intervals.push_back(new_interval);
  375. }
  376. // otherwise, set the time of the last interval message to the current timestamp and increase interval packet count by 1
  377. else{
  378. conv_statistics_extended[f].comm_intervals.back().end = timestamp;
  379. conv_statistics_extended[f].comm_intervals.back().pkts_count++;
  380. }
  381. }
  382. // if there does not exist a communication interval for the specified conversation
  383. else{
  384. // add initial interval entry for this conversation
  385. commInterval initial_interval = {timestamp, timestamp, 1};
  386. entry_convStatExt entry;
  387. entry.comm_intervals.push_back(initial_interval);
  388. entry.pkts_count = 1;
  389. entry.pkts_timestamp.push_back(timestamp);
  390. conv_statistics_extended[f2] = entry;
  391. }
  392. }
  393. }
  394. /**
  395. * Aggregate the collected information about all communication intervals within conv_statistics_extended of every conversation.
  396. * Do this by computing the average packet rate per interval and the average time between intervals.
  397. * Also compute average interval duration and total communication duration (i.e. last_msg.time - first_msg.time)
  398. */
  399. void statistics::createCommIntervalStats(){
  400. // iterate over all <convWithProt, entry_convStatExt> pairs
  401. for (auto &cur_elem : conv_statistics_extended) {
  402. entry_convStatExt &entry = cur_elem.second;
  403. std::vector<commInterval> &intervals = entry.comm_intervals;
  404. // if there is only one interval, the time between intervals cannot be computed and is therefore set to 0
  405. if (intervals.size() == 1){
  406. double interval_duration = (double) (intervals[0].end - intervals[0].start).count() / (double) 1e6;
  407. entry.avg_int_pkts_count = (double) intervals[0].pkts_count;
  408. entry.avg_time_between_ints = (double) 0;
  409. entry.avg_interval_time = interval_duration;
  410. }
  411. // If there is more than one interval, compute the specified averages
  412. else if (intervals.size() > 1){
  413. long summed_pkts_count = intervals[0].pkts_count;
  414. std::chrono::microseconds time_between_ints_sum = (std::chrono::microseconds) 0;
  415. std::chrono::microseconds summed_int_duration = intervals[0].end - intervals[0].start;
  416. for (std::size_t i = 1; i < intervals.size(); i++) {
  417. summed_pkts_count += intervals[i].pkts_count;
  418. summed_int_duration += intervals[i].end - intervals[i].start;
  419. time_between_ints_sum += intervals[i].start - intervals[i - 1].end;
  420. }
  421. entry.avg_int_pkts_count = summed_pkts_count / ((double) intervals.size());
  422. entry.avg_time_between_ints = (time_between_ints_sum.count() / (double) (intervals.size() - 1)) / (double) 1e6;
  423. entry.avg_interval_time = (summed_int_duration.count() / (double) intervals.size()) / (double) 1e6;
  424. }
  425. entry.total_comm_duration = (double) (entry.pkts_timestamp.back() - entry.pkts_timestamp.front()).count() / (double) 1e6;
  426. }
  427. }
  428. /**
  429. * Increments the packet counter for the given IP address and MSS value.
  430. * @param ipAddress The IP address whose MSS packet counter should be incremented.
  431. * @param mssValue The MSS value of the packet.
  432. */
  433. void statistics::incrementMSScount(const std::string &ipAddress, int mssValue) {
  434. mss_values[mssValue]++;
  435. mss_distribution[{ipAddress, mssValue}]++;
  436. }
  437. /**
  438. * Increments the packet counter for the given IP address and window size.
  439. * @param ipAddress The IP address whose window size packet counter should be incremented.
  440. * @param winSize The window size of the packet.
  441. */
  442. void statistics::incrementWinCount(const std::string &ipAddress, int winSize) {
  443. win_values[winSize]++;
  444. win_distribution[{ipAddress, winSize}]++;
  445. }
  446. /**
  447. * Increments the packet counter for the given IP address and TTL value.
  448. * @param ipAddress The IP address whose TTL packet counter should be incremented.
  449. * @param ttlValue The TTL value of the packet.
  450. */
  451. void statistics::incrementTTLcount(const std::string &ipAddress, int ttlValue) {
  452. ttl_values[ttlValue]++;
  453. ttl_distribution[{ipAddress, ttlValue}]++;
  454. }
  455. /**
  456. * Increments the packet counter for the given IP address and ToS value.
  457. * @param ipAddress The IP address whose ToS packet counter should be incremented.
  458. * @param tosValue The ToS value of the packet.
  459. */
  460. void statistics::incrementToScount(const std::string &ipAddress, int tosValue) {
  461. tos_values[tosValue]++;
  462. tos_distribution[{ipAddress, tosValue}]++;
  463. }
  464. /**
  465. * Increments the protocol counter for the given IP address and protocol.
  466. * @param ipAddress The IP address whose protocol packet counter should be incremented.
  467. * @param protocol The protocol of the packet.
  468. */
  469. void statistics::incrementProtocolCount(const std::string &ipAddress, const std::string &protocol) {
  470. protocol_distribution[{ipAddress, protocol}].count++;
  471. }
  472. /**
  473. * Returns the number of packets seen for the given IP address and protocol.
  474. * @param ipAddress The IP address whose packet count is wanted.
  475. * @param protocol The protocol whose packet count is wanted.
  476. */
  477. int statistics::getProtocolCount(const std::string &ipAddress, const std::string &protocol) {
  478. return protocol_distribution[{ipAddress, protocol}].count;
  479. }
  480. /**
  481. * Increases the byte counter for the given IP address and protocol.
  482. * @param ipAddress The IP address whose protocol byte counter should be increased.
  483. * @param protocol The protocol of the packet.
  484. * @param byteSent The packet's size.
  485. */
  486. void statistics::increaseProtocolByteCount(const std::string &ipAddress, const std::string &protocol, long bytesSent) {
  487. protocol_distribution[{ipAddress, protocol}].byteCount += bytesSent;
  488. }
  489. /**
  490. * Returns the number of bytes seen for the given IP address and protocol.
  491. * @param ipAddress The IP address whose byte count is wanted.
  492. * @param protocol The protocol whose byte count is wanted.
  493. * @return a float: The number of bytes
  494. */
  495. float statistics::getProtocolByteCount(const std::string &ipAddress, const std::string &protocol) {
  496. return protocol_distribution[{ipAddress, protocol}].byteCount;
  497. }
  498. /**
  499. * Increments the packet counter for
  500. * - the given sender IP address with outgoing port and
  501. * - the given receiver IP address with incoming port.
  502. * @param ipAddressSender The IP address of the packet sender.
  503. * @param outgoingPort The port used by the sender.
  504. * @param ipAddressReceiver The IP address of the packet receiver.
  505. * @param incomingPort The port used by the receiver.
  506. */
  507. void statistics::incrementPortCount(const std::string &ipAddressSender, int outgoingPort, const std::string &ipAddressReceiver,
  508. int incomingPort, const std::string &protocol) {
  509. port_values[outgoingPort]++;
  510. port_values[incomingPort]++;
  511. ip_ports[{ipAddressSender, "out", outgoingPort, protocol}].count++;
  512. ip_ports[{ipAddressReceiver, "in", incomingPort, protocol}].count++;
  513. }
  514. /**
  515. * Increases the packet byte counter for
  516. * - the given sender IP address with outgoing port and
  517. * - the given receiver IP address with incoming port.
  518. * @param ipAddressSender The IP address of the packet sender.
  519. * @param outgoingPort The port used by the sender.
  520. * @param ipAddressReceiver The IP address of the packet receiver.
  521. * @param incomingPort The port used by the receiver.
  522. * @param byteSent The packet's size.
  523. */
  524. void statistics::increasePortByteCount(const std::string &ipAddressSender, int outgoingPort, const std::string &ipAddressReceiver,
  525. int incomingPort, long bytesSent, const std::string &protocol) {
  526. ip_ports[{ipAddressSender, "out", outgoingPort, protocol}].byteCount += bytesSent;
  527. ip_ports[{ipAddressReceiver, "in", incomingPort, protocol}].byteCount += bytesSent;
  528. }
  529. /**
  530. * Increments the packet counter for
  531. * - the given sender MAC address and
  532. * - the given receiver MAC address.
  533. * @param srcMac The MAC address of the packet sender.
  534. * @param dstMac The MAC address of the packet receiver.
  535. * @param typeNumber The payload type number of the packet.
  536. */
  537. void statistics::incrementUnrecognizedPDUCount(const std::string &srcMac, const std::string &dstMac, uint32_t typeNumber,
  538. const std::string &timestamp) {
  539. unrecognized_PDUs[{srcMac, dstMac, typeNumber}].count++;
  540. unrecognized_PDUs[{srcMac, dstMac, typeNumber}].timestamp_last_occurrence = timestamp;
  541. }
  542. /**
  543. * Creates a new statistics object.
  544. */
  545. statistics::statistics(std::string resourcePath) {;
  546. this->resourcePath = resourcePath;
  547. }
  548. /**
  549. * Stores the assignment IP address -> MAC address.
  550. * @param ipAddress The IP address belonging to the given MAC address.
  551. * @param macAddress The MAC address belonging to the given IP address.
  552. */
  553. void statistics::assignMacAddress(const std::string &ipAddress, const std::string &macAddress) {
  554. ip_mac_mapping[ipAddress] = macAddress;
  555. }
  556. /**
  557. * Registers statistical data for a sent packet. Increments the counter packets_sent for the sender and
  558. * packets_received for the receiver. Adds the bytes as kbytes_sent (sender) and kybtes_received (receiver).
  559. * @param ipAddressSender The IP address of the packet sender.
  560. * @param ipAddressReceiver The IP address of the packet receiver.
  561. * @param bytesSent The packet's size.
  562. */
  563. void statistics::addIpStat_packetSent(const std::string &ipAddressSender, const std::string &ipAddressReceiver, long bytesSent, std::chrono::microseconds timestamp) {
  564. // Adding IP as a sender for first time
  565. if(ip_statistics[ipAddressSender].pkts_sent==0){
  566. // Add the IP class
  567. ip_statistics[ipAddressSender].ip_class = getIPv4Class(ipAddressSender);
  568. }
  569. // Adding IP as a receiver for first time
  570. if(ip_statistics[ipAddressReceiver].pkts_received==0){
  571. // Add the IP class
  572. ip_statistics[ipAddressReceiver].ip_class = getIPv4Class(ipAddressReceiver);
  573. }
  574. // Update stats for packet sender
  575. ip_statistics[ipAddressSender].kbytes_sent += (float(bytesSent) / 1024);
  576. ip_statistics[ipAddressSender].pkts_sent++;
  577. ip_statistics[ipAddressSender].pkts_sent_timestamp.push_back(timestamp);
  578. // Update stats for packet receiver
  579. ip_statistics[ipAddressReceiver].kbytes_received += (float(bytesSent) / 1024);
  580. ip_statistics[ipAddressReceiver].pkts_received++;
  581. ip_statistics[ipAddressReceiver].pkts_received_timestamp.push_back(timestamp);
  582. if(this->getDoExtraTests()) {
  583. // Increment Degrees for sender and receiver, if Sender sends its first packet to this receiver
  584. std::unordered_set<std::string>::const_iterator found_receiver = contacted_ips[ipAddressSender].find(ipAddressReceiver);
  585. if(found_receiver == contacted_ips[ipAddressSender].end()){
  586. // Receiver is NOT contained in the List of IPs, that the Sender has contacted, therefore this is the first packet in this direction
  587. ip_statistics[ipAddressSender].out_degree++;
  588. ip_statistics[ipAddressReceiver].in_degree++;
  589. // Increment overall_degree only if this is the first packet for the connection (both directions)
  590. // Therefore check, whether Receiver has contacted Sender before
  591. std::unordered_set<std::string>::const_iterator sender_contacted = contacted_ips[ipAddressReceiver].find(ipAddressSender);
  592. if(sender_contacted == contacted_ips[ipAddressReceiver].end()){
  593. ip_statistics[ipAddressSender].overall_degree++;
  594. ip_statistics[ipAddressReceiver].overall_degree++;
  595. }
  596. contacted_ips[ipAddressSender].insert(ipAddressReceiver);
  597. }
  598. }
  599. }
  600. /**
  601. * Setter for the timestamp_firstPacket field.
  602. * @param ts The timestamp of the first packet in the PCAP file.
  603. */
  604. void statistics::setTimestampFirstPacket(Tins::Timestamp ts) {
  605. timestamp_firstPacket = ts;
  606. }
  607. /**
  608. * Setter for the timestamp_lastPacket field.
  609. * @param ts The timestamp of the last packet in the PCAP file.
  610. */
  611. void statistics::setTimestampLastPacket(Tins::Timestamp ts) {
  612. timestamp_lastPacket = ts;
  613. }
  614. /**
  615. * Getter for the timestamp_firstPacket field.
  616. */
  617. Tins::Timestamp statistics::getTimestampFirstPacket() {
  618. return timestamp_firstPacket;
  619. }
  620. /**
  621. * Getter for the timestamp_lastPacket field.
  622. */
  623. Tins::Timestamp statistics::getTimestampLastPacket() {
  624. return timestamp_lastPacket;
  625. }
  626. /**
  627. * Getter for the packetCount field.
  628. */
  629. int statistics::getPacketCount() {
  630. return packetCount;
  631. }
  632. /**
  633. * Getter for the sumPacketSize field.
  634. */
  635. int statistics::getSumPacketSize() {
  636. return sumPacketSize;
  637. }
  638. /**
  639. * Returns the average packet size.
  640. * @return a float indicating the average packet size in kbytes.
  641. */
  642. float statistics::getAvgPacketSize() const {
  643. // AvgPktSize = (Sum of all packet sizes / #Packets)
  644. return (sumPacketSize / packetCount) / 1024;
  645. }
  646. /**
  647. * Adds the size of a packet (to be used to calculate the avg. packet size).
  648. * @param packetSize The size of the current packet in bytes.
  649. */
  650. void statistics::addPacketSize(uint32_t packetSize) {
  651. sumPacketSize += ((float) packetSize);
  652. }
  653. /**
  654. * Setter for the doExtraTests field.
  655. */
  656. void statistics::setDoExtraTests(bool var) {
  657. doExtraTests = var;
  658. }
  659. /**
  660. * Getter for the doExtraTests field.
  661. */
  662. bool statistics::getDoExtraTests() {
  663. return doExtraTests;
  664. }
  665. /**
  666. * Calculates the capture duration.
  667. * @return a formatted string HH:MM:SS.mmmmmm with
  668. * HH: hour, MM: minute, SS: second, mmmmmm: microseconds
  669. */
  670. std::string statistics::getCaptureDurationTimestamp() const {
  671. // Calculate duration
  672. timeval fp, lp, d;
  673. fp.tv_sec = timestamp_firstPacket.seconds();
  674. fp.tv_usec = timestamp_firstPacket.microseconds();
  675. lp.tv_sec = timestamp_lastPacket.seconds();
  676. lp.tv_usec = timestamp_lastPacket.microseconds();
  677. timersub(&lp, &fp, &d);
  678. long int hour = d.tv_sec / 3600;
  679. long int remainder = (d.tv_sec - hour * 3600);
  680. long int minute = remainder / 60;
  681. long int second = (remainder - minute * 60) % 60;
  682. long int microseconds = d.tv_usec;
  683. // Build desired output format: YYYY-mm-dd hh:mm:ss
  684. char out[64];
  685. sprintf(out, "%02ld:%02ld:%02ld.%06ld ", hour, minute, second, microseconds);
  686. return std::string(out);
  687. }
  688. /**
  689. * Calculates the capture duration.
  690. * @return a formatted string SS.mmmmmm with
  691. * S: seconds (UNIX time), mmmmmm: microseconds
  692. */
  693. float statistics::getCaptureDurationSeconds() const {
  694. timeval fp, lp, d;
  695. fp.tv_sec = timestamp_firstPacket.seconds();
  696. fp.tv_usec = timestamp_firstPacket.microseconds();
  697. lp.tv_sec = timestamp_lastPacket.seconds();
  698. lp.tv_usec = timestamp_lastPacket.microseconds();
  699. timersub(&lp, &fp, &d);
  700. char buf[64];
  701. snprintf(buf, sizeof(buf), "%u.%06u", static_cast<uint>(d.tv_sec), static_cast<uint>(d.tv_usec));
  702. return std::stof(std::string(buf));
  703. }
  704. /**
  705. * Creates a timestamp based on a time_t seconds (UNIX time format) and microseconds.
  706. * @param seconds
  707. * @param microseconds
  708. * @return a formatted string Y-m-d H:M:S.m with
  709. * Y: year, m: month, d: day, H: hour, M: minute, S: second, m: microseconds
  710. */
  711. std::string statistics::getFormattedTimestamp(time_t seconds, suseconds_t microseconds) const {
  712. timeval tv;
  713. tv.tv_sec = seconds;
  714. tv.tv_usec = microseconds;
  715. char tmbuf[20], buf[64];
  716. auto nowtm = gmtime(&(tv.tv_sec));
  717. strftime(tmbuf, sizeof(tmbuf), "%Y-%m-%d %H:%M:%S", nowtm);
  718. snprintf(buf, sizeof(buf), "%s.%06u", tmbuf, static_cast<uint>(tv.tv_usec));
  719. return std::string(buf);
  720. }
  721. /**
  722. * Calculates the statistics for a given IP address.
  723. * @param ipAddress The IP address whose statistics should be calculated.
  724. * @return a ip_stats struct containing statistical data derived by the statistical data collected.
  725. */
  726. ip_stats statistics::getStatsForIP(const std::string &ipAddress) {
  727. float duration = getCaptureDurationSeconds();
  728. entry_ipStat ipStatEntry = ip_statistics[ipAddress];
  729. ip_stats s;
  730. s.bandwidthKBitsIn = (ipStatEntry.kbytes_received / duration) * 8;
  731. s.bandwidthKBitsOut = (ipStatEntry.kbytes_sent / duration) * 8;
  732. s.packetPerSecondIn = (ipStatEntry.pkts_received / duration);
  733. s.packetPerSecondOut = (ipStatEntry.pkts_sent / duration);
  734. s.AvgPacketSizeSent = (ipStatEntry.kbytes_sent / ipStatEntry.pkts_sent);
  735. s.AvgPacketSizeRecv = (ipStatEntry.kbytes_received / ipStatEntry.pkts_received);
  736. return s;
  737. }
  738. int statistics::getDefaultInterval() {
  739. return this->default_interval;
  740. }
  741. void statistics::setDefaultInterval(int interval) {
  742. this->default_interval = interval;
  743. }
  744. /**
  745. * Increments the packet counter.
  746. */
  747. void statistics::incrementPacketCount() {
  748. packetCount++;
  749. }
  750. /**
  751. * Prints the statistics of the PCAP and IP specific statistics for the given IP address.
  752. * @param ipAddress The IP address whose statistics should be printed. Can be empty "" to print only general file statistics.
  753. */
  754. void statistics::printStats(const std::string &ipAddress) {
  755. std::stringstream ss;
  756. ss << std::endl;
  757. ss << "Capture duration: " << getCaptureDurationSeconds() << " seconds" << std::endl;
  758. ss << "Capture duration (HH:MM:SS.mmmmmm): " << getCaptureDurationTimestamp() << std::endl;
  759. ss << "#Packets: " << packetCount << std::endl;
  760. ss << std::endl;
  761. // Print IP address specific statistics only if IP address was given
  762. if (ipAddress != "") {
  763. entry_ipStat e = ip_statistics[ipAddress];
  764. ss << "\n----- STATS FOR IP ADDRESS [" << ipAddress << "] -------" << std::endl;
  765. ss << std::endl << "KBytes sent: " << e.kbytes_sent << std::endl;
  766. ss << "KBytes received: " << e.kbytes_received << std::endl;
  767. ss << "Packets sent: " << e.pkts_sent << std::endl;
  768. ss << "Packets received: " << e.pkts_received << "\n\n";
  769. ip_stats is = getStatsForIP(ipAddress);
  770. ss << "Bandwidth IN: " << is.bandwidthKBitsIn << " kbit/s" << std::endl;
  771. ss << "Bandwidth OUT: " << is.bandwidthKBitsOut << " kbit/s" << std::endl;
  772. ss << "Packets per second IN: " << is.packetPerSecondIn << std::endl;
  773. ss << "Packets per second OUT: " << is.packetPerSecondOut << std::endl;
  774. ss << "Avg Packet Size Sent: " << is.AvgPacketSizeSent << " kbytes" << std::endl;
  775. ss << "Avg Packet Size Received: " << is.AvgPacketSizeRecv << " kbytes" << std::endl;
  776. }
  777. std::cout << ss.str();
  778. }
  779. /**
  780. * Derives general PCAP file statistics from the collected statistical data and
  781. * writes all data into a SQLite database, located at database_path.
  782. * @param database_path The path of the SQLite database file ending with .sqlite3.
  783. */
  784. void statistics::writeToDatabase(std::string database_path, std::vector<std::chrono::duration<int, std::micro>> timeIntervals, bool del) {
  785. // Generate general file statistics
  786. float duration = getCaptureDurationSeconds();
  787. long sumPacketsSent = 0, senderCountIP = 0;
  788. float sumBandwidthIn = 0.0, sumBandwidthOut = 0.0;
  789. for (auto i = ip_statistics.begin(); i != ip_statistics.end(); i++) {
  790. sumPacketsSent += i->second.pkts_sent;
  791. // Consumed bandwith (bytes) for sending packets
  792. sumBandwidthIn += (i->second.kbytes_received / duration);
  793. sumBandwidthOut += (i->second.kbytes_sent / duration);
  794. senderCountIP++;
  795. }
  796. float avgPacketRate = (packetCount / duration);
  797. long avgPacketSize = getAvgPacketSize();
  798. if(senderCountIP>0) {
  799. long avgPacketsSentPerHost = (sumPacketsSent / senderCountIP);
  800. float avgBandwidthInKBits = (sumBandwidthIn / senderCountIP) * 8;
  801. float avgBandwidthOutInKBits = (sumBandwidthOut / senderCountIP) * 8;
  802. // Create database and write information
  803. statistics_db db(database_path, resourcePath);
  804. db.writeStatisticsFile(packetCount, getCaptureDurationSeconds(),
  805. getFormattedTimestamp(timestamp_firstPacket.seconds(), timestamp_firstPacket.microseconds()),
  806. getFormattedTimestamp(timestamp_lastPacket.seconds(), timestamp_lastPacket.microseconds()),
  807. avgPacketRate, avgPacketSize, avgPacketsSentPerHost, avgBandwidthInKBits,
  808. avgBandwidthOutInKBits, doExtraTests);
  809. db.writeStatisticsIP(ip_statistics);
  810. db.writeStatisticsTTL(ttl_distribution);
  811. db.writeStatisticsIpMac(ip_mac_mapping);
  812. db.writeStatisticsDegree(ip_statistics);
  813. db.writeStatisticsPorts(ip_ports);
  814. db.writeStatisticsProtocols(protocol_distribution);
  815. db.writeStatisticsMSS(mss_distribution);
  816. db.writeStatisticsToS(tos_distribution);
  817. db.writeStatisticsWin(win_distribution);
  818. db.writeStatisticsConv(conv_statistics);
  819. db.writeStatisticsConvExt(conv_statistics_extended);
  820. db.writeStatisticsInterval(interval_statistics, timeIntervals, del, this->default_interval, this->getDoExtraTests());
  821. db.writeDbVersion();
  822. db.writeStatisticsUnrecognizedPDUs(unrecognized_PDUs);
  823. }
  824. else {
  825. // Tinslib failed to recognize the types of the packets in the input PCAP
  826. std::cerr<<"ERROR: Statistics could not be collected from the input PCAP!"<<"\n";
  827. return;
  828. }
  829. }
  830. void statistics::writeIntervalsToDatabase(std::string database_path, std::vector<std::chrono::duration<int, std::micro>> timeIntervals, bool del) {
  831. statistics_db db(database_path, resourcePath);
  832. db.writeStatisticsInterval(interval_statistics, timeIntervals, del, this->default_interval, this->getDoExtraTests());
  833. }