statistics.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  1. /*
  2. * Class providing containers and access methods for statistical data collection.
  3. */
  4. #ifndef CPP_PCAPREADER_STATISTICS_H
  5. #define CPP_PCAPREADER_STATISTICS_H
  6. #include <vector>
  7. #include <unordered_map>
  8. #include <unordered_set>
  9. #include <list>
  10. #include <tuple>
  11. #include <tins/timestamp.h>
  12. #include <tins/ip_address.h>
  13. #include "utilities.h"
  14. using namespace Tins;
  15. #define COMM_INTERVAL_THRESHOLD 10e6 // in microseconds; i.e. here 10s
  16. /*
  17. * Definition of structs used in unordered_map fields
  18. */
  19. /*
  20. * Struct used as data structure for method get_stats_for_ip, represents:
  21. * - Incoming bandwidth in KBits
  22. * - Outgoing bandwidth in KBits
  23. * - Number of incoming packets per second
  24. * - Number of outgoing packets per second
  25. * - Average size of sent packets in kbytes
  26. * - Average size of received packets in kybtes
  27. * - Average value of TCP option Maximum Segment Size (MSS)
  28. */
  29. struct ip_stats {
  30. float bandwidthKBitsIn;
  31. float bandwidthKBitsOut;
  32. float packetPerSecondIn;
  33. float packetPerSecondOut;
  34. float AvgPacketSizeSent;
  35. float AvgPacketSizeRecv;
  36. };
  37. /*
  38. * Struct used to represent a conversation by:
  39. * - IP address A
  40. * - Port A
  41. * - IP address B
  42. * - Port B
  43. */
  44. struct conv{
  45. std::string ipAddressA;
  46. int portA;
  47. std::string ipAddressB;
  48. int portB;
  49. bool operator==(const conv &other) const {
  50. return ipAddressA == other.ipAddressA
  51. && portA == other.portA
  52. &&ipAddressB == other.ipAddressB
  53. && portB == other.portB;
  54. }
  55. };
  56. /*
  57. * Struct used to represent a conversation by:
  58. * - IP address A
  59. * - Port A
  60. * - IP address B
  61. * - Port B
  62. * - Protocol
  63. */
  64. struct convWithProt{
  65. std::string ipAddressA;
  66. int portA;
  67. std::string ipAddressB;
  68. int portB;
  69. std::string protocol;
  70. bool operator==(const convWithProt &other) const {
  71. return ipAddressA == other.ipAddressA
  72. && portA == other.portA
  73. &&ipAddressB == other.ipAddressB
  74. && portB == other.portB
  75. && protocol == other.protocol;
  76. }
  77. };
  78. /*
  79. * Struct used to represent:
  80. * - IP address (IPv4 or IPv6)
  81. * - MSS value
  82. */
  83. struct ipAddress_mss {
  84. std::string ipAddress;
  85. int mssValue;
  86. bool operator==(const ipAddress_mss &other) const {
  87. return ipAddress == other.ipAddress
  88. && mssValue == other.mssValue;
  89. }
  90. };
  91. /*
  92. * Struct used to represent:
  93. * - IP address (IPv4 or IPv6)
  94. * - ToS value
  95. */
  96. struct ipAddress_tos {
  97. std::string ipAddress;
  98. int tosValue;
  99. bool operator==(const ipAddress_tos &other) const {
  100. return ipAddress == other.ipAddress
  101. && tosValue == other.tosValue;
  102. }
  103. };
  104. /*
  105. * Struct used to represent:
  106. * - IP address (IPv4 or IPv6)
  107. * - Window size
  108. */
  109. struct ipAddress_win {
  110. std::string ipAddress;
  111. int winSize;
  112. bool operator==(const ipAddress_win &other) const {
  113. return ipAddress == other.ipAddress
  114. && winSize == other.winSize;
  115. }
  116. };
  117. /*
  118. * Struct used to represent:
  119. * - IP address (IPv4 or IPv6)
  120. * - TTL value
  121. */
  122. struct ipAddress_ttl {
  123. std::string ipAddress;
  124. int ttlValue;
  125. bool operator==(const ipAddress_ttl &other) const {
  126. return ipAddress == other.ipAddress
  127. && ttlValue == other.ttlValue;
  128. }
  129. };
  130. /*
  131. * Struct used to represent:
  132. * - IP address (IPv4 or IPv6)
  133. * - Protocol (e.g. TCP, UDP, IPv4, IPv6)
  134. */
  135. struct ipAddress_protocol {
  136. std::string ipAddress;
  137. std::string protocol;
  138. bool operator==(const ipAddress_protocol &other) const {
  139. return ipAddress == other.ipAddress
  140. && protocol == other.protocol;
  141. }
  142. };
  143. /*
  144. * Struct used to represent:
  145. * - Number of received packets
  146. * - Number of sent packets
  147. * - Data received in kbytes
  148. * - Data sent in kbytes
  149. */
  150. struct entry_ipStat {
  151. long pkts_received;
  152. long pkts_sent;
  153. float kbytes_received;
  154. float kbytes_sent;
  155. std::string ip_class;
  156. int in_degree;
  157. int out_degree;
  158. int overall_degree;
  159. // Collects statstics over time interval
  160. std::vector<float> interval_pkt_rate;
  161. float max_interval_pkt_rate;
  162. float min_interval_pkt_rate;
  163. std::vector<std::chrono::microseconds> pkts_sent_timestamp;
  164. std::vector<std::chrono::microseconds> pkts_received_timestamp;
  165. bool operator==(const entry_ipStat &other) const {
  166. return pkts_received == other.pkts_received
  167. && pkts_sent == other.pkts_sent
  168. && kbytes_sent == other.kbytes_sent
  169. && kbytes_received == other.kbytes_received
  170. && interval_pkt_rate == other.interval_pkt_rate
  171. && max_interval_pkt_rate == other.max_interval_pkt_rate
  172. && min_interval_pkt_rate == other.min_interval_pkt_rate
  173. && ip_class == other.ip_class
  174. && pkts_sent_timestamp == other.pkts_sent_timestamp
  175. && pkts_received_timestamp == other.pkts_received_timestamp;
  176. }
  177. };
  178. /*
  179. * Struct used to represent:
  180. * - Number of transmitted packets
  181. * - Number of transmitted bytes
  182. */
  183. struct entry_portStat {
  184. int count;
  185. float byteCount;
  186. };
  187. /*
  188. * Struct used to represent:
  189. * - Number of times the protocol is seen
  190. * - Amount of bytes transmitted with this protocol
  191. */
  192. struct entry_protocolStat {
  193. int count;
  194. float byteCount;
  195. };
  196. /*
  197. * Struct used to represent interval statistics:
  198. * - # packets
  199. * - # bytes
  200. * - IP source entropy
  201. * - IP destination entropy
  202. * - IP source cumulative entropy
  203. * - IP destination cumulative entropy
  204. * - # packets that have payload
  205. * - # incorrect TCP checksum
  206. * - # correct TCP checksum
  207. * - # novel IPs
  208. * - # novel TTL
  209. * - # novel Window Size
  210. * - # novel ToS
  211. * - # novel MSS
  212. */
  213. struct entry_intervalStat {
  214. std::string start;
  215. std::string end;
  216. int pkts_count;
  217. float pkt_rate;
  218. float kbytes;
  219. float kbyte_rate;
  220. double ip_src_entropy;
  221. double ip_dst_entropy;
  222. double ip_src_novel_entropy;
  223. double ip_dst_novel_entropy;
  224. double ip_src_entropy_norm;
  225. double ip_dst_entropy_norm;
  226. double ip_src_novel_entropy_norm;
  227. double ip_dst_novel_entropy_norm;
  228. double ip_src_cum_entropy;
  229. double ip_dst_cum_entropy;
  230. double ip_src_cum_entropy_norm;
  231. double ip_dst_cum_entropy_norm;
  232. std::vector<double> ttl_entropies;
  233. std::vector<double> win_size_entropies;
  234. std::vector<double> tos_entropies;
  235. std::vector<double> mss_entropies;
  236. std::vector<double> port_entropies;
  237. int payload_count;
  238. int incorrect_tcp_checksum_count;
  239. int correct_tcp_checksum_count;
  240. size_t novel_ip_src_count;
  241. size_t novel_ip_dst_count;
  242. int novel_ttl_count;
  243. int novel_win_size_count;
  244. int novel_tos_count;
  245. int novel_mss_count;
  246. int novel_port_count;
  247. // FIXME: add new attributes to operator==
  248. bool operator==(const entry_intervalStat &other) const {
  249. return start == other.start
  250. && end == other.end
  251. && pkts_count == other.pkts_count
  252. && pkt_rate == other.pkt_rate
  253. && kbytes == other.kbytes
  254. && kbyte_rate == other.kbyte_rate
  255. && ip_src_entropy == other.ip_src_entropy
  256. && ip_dst_entropy == other.ip_dst_entropy
  257. && ip_src_novel_entropy == other.ip_src_novel_entropy
  258. && ip_dst_novel_entropy == other.ip_dst_novel_entropy
  259. && ip_src_entropy_norm == other.ip_src_entropy_norm
  260. && ip_dst_entropy_norm == other.ip_dst_entropy_norm
  261. && ip_src_novel_entropy_norm == other.ip_src_novel_entropy_norm
  262. && ip_dst_novel_entropy_norm == other.ip_dst_novel_entropy_norm
  263. && ip_src_cum_entropy == other.ip_src_cum_entropy
  264. && ip_dst_cum_entropy == other.ip_dst_cum_entropy
  265. && ip_src_cum_entropy_norm == other.ip_src_cum_entropy_norm
  266. && ip_dst_cum_entropy_norm == other.ip_dst_cum_entropy_norm
  267. && payload_count == other.payload_count
  268. && incorrect_tcp_checksum_count == other.incorrect_tcp_checksum_count
  269. && correct_tcp_checksum_count == other.correct_tcp_checksum_count
  270. && novel_ip_src_count == other.novel_ip_src_count
  271. && novel_ip_dst_count == other.novel_ip_dst_count
  272. && novel_ttl_count == other.novel_ttl_count
  273. && novel_win_size_count == other.novel_win_size_count
  274. && novel_tos_count == other.novel_tos_count
  275. && novel_mss_count == other.novel_mss_count
  276. && novel_port_count == other.novel_port_count
  277. && ttl_entropies == other.ttl_entropies
  278. && win_size_entropies == other.win_size_entropies
  279. && tos_entropies == other.tos_entropies
  280. && mss_entropies == other.mss_entropies
  281. && port_entropies == other.port_entropies;
  282. }
  283. };
  284. /*
  285. * Struct used to represent converstaion statistics:
  286. * - # packets
  287. * - Average packet rate
  288. * - Timestamps of packets
  289. * - Inter-arrival time
  290. * - Average inter-arrival time
  291. */
  292. struct entry_convStat {
  293. long pkts_count;
  294. float avg_pkt_rate;
  295. std::vector<std::chrono::microseconds> pkts_timestamp;
  296. std::vector<std::chrono::microseconds> interarrival_time;
  297. std::chrono::microseconds avg_interarrival_time;
  298. bool operator==(const entry_convStat &other) const {
  299. return pkts_count == other.pkts_count
  300. && avg_pkt_rate == avg_pkt_rate
  301. && pkts_timestamp == other.pkts_timestamp
  302. && interarrival_time == other.interarrival_time
  303. && avg_interarrival_time == other.avg_interarrival_time;
  304. }
  305. };
  306. /*
  307. * Struct used to represent:
  308. * - IP address (IPv4 or IPv6)
  309. - Traffic direction (out: outgoing connection, in: incoming connection)
  310. * - Port number
  311. */
  312. struct ipAddress_inOut_port {
  313. std::string ipAddress;
  314. std::string trafficDirection;
  315. int portNumber;
  316. std::string protocol;
  317. bool operator==(const ipAddress_inOut_port &other) const {
  318. return ipAddress == other.ipAddress
  319. && trafficDirection == other.trafficDirection
  320. && portNumber == other.portNumber
  321. && protocol == other.protocol;
  322. }
  323. };
  324. /*
  325. * Struct used to represent a communication interval (for two hosts):
  326. * - Timestamp of the first packet in the interval
  327. * - Timestamp of the last packet in the interval
  328. * - The count of packets within the interval
  329. */
  330. struct commInterval{
  331. std::chrono::microseconds start;
  332. std::chrono::microseconds end;
  333. long pkts_count;
  334. bool operator==(const commInterval &other) const {
  335. return start == other.start
  336. && end == other.end
  337. && pkts_count == other.pkts_count;
  338. }
  339. };
  340. /*
  341. * Struct used to represent converstaion statistics:
  342. * - commnication intervals
  343. * - # packets
  344. * - Average packet rate
  345. * - average # packets per communication interval
  346. * - Average time between intervals
  347. * - Average duration of a communication interval
  348. * - Overall communication duration
  349. * - Timestamps of packets
  350. * - Inter-arrival time
  351. * - Average inter-arrival time
  352. */
  353. struct entry_convStatExt {
  354. std::vector<commInterval> comm_intervals;
  355. long pkts_count;
  356. float avg_pkt_rate;
  357. double avg_int_pkts_count;
  358. double avg_time_between_ints;
  359. double avg_interval_time;
  360. double total_comm_duration;
  361. std::chrono::duration<int, std::micro> timeInterval;
  362. std::vector<std::chrono::microseconds> pkts_timestamp;
  363. std::vector<std::chrono::microseconds> interarrival_time;
  364. std::chrono::microseconds avg_interarrival_time;
  365. bool operator==(const entry_convStatExt &other) const {
  366. return comm_intervals == other.comm_intervals
  367. && pkts_count == other.pkts_count
  368. && avg_pkt_rate == avg_pkt_rate
  369. && avg_int_pkts_count == other.avg_int_pkts_count
  370. && avg_time_between_ints == other.avg_time_between_ints
  371. && avg_interval_time == other.avg_interval_time
  372. && total_comm_duration == other.total_comm_duration
  373. && pkts_timestamp == other.pkts_timestamp
  374. && interarrival_time == other.interarrival_time
  375. && avg_interarrival_time == other.avg_interarrival_time;
  376. }
  377. };
  378. /*
  379. * Struct used to represent:
  380. * - Source MAC address
  381. * - Destination MAC address
  382. * - Payload type number
  383. */
  384. struct unrecognized_PDU {
  385. std::string srcMacAddress;
  386. std::string dstMacAddress;
  387. uint32_t typeNumber;
  388. bool operator==(const unrecognized_PDU &other) const {
  389. return srcMacAddress == other.srcMacAddress
  390. && dstMacAddress == other.dstMacAddress
  391. && typeNumber == other.typeNumber;
  392. }
  393. };
  394. /*
  395. * Struct used to represent:
  396. * - Number of occurrences
  397. * - Formatted timestamp of last occurrence
  398. */
  399. struct unrecognized_PDU_stat {
  400. int count;
  401. std::string timestamp_last_occurrence;
  402. };
  403. /*
  404. * Definition of hash functions for structs used as key in unordered_map
  405. */
  406. namespace std {
  407. template<>
  408. struct hash<ipAddress_ttl> {
  409. std::size_t operator()(const ipAddress_ttl &k) const {
  410. using std::size_t;
  411. using std::hash;
  412. using std::string;
  413. return ((hash<string>()(k.ipAddress)
  414. ^ (hash<int>()(k.ttlValue) << 1)) >> 1);
  415. }
  416. };
  417. template<>
  418. struct hash<ipAddress_mss> {
  419. std::size_t operator()(const ipAddress_mss &k) const {
  420. using std::size_t;
  421. using std::hash;
  422. using std::string;
  423. return ((hash<string>()(k.ipAddress)
  424. ^ (hash<int>()(k.mssValue) << 1)) >> 1);
  425. }
  426. };
  427. template<>
  428. struct hash<ipAddress_tos> {
  429. std::size_t operator()(const ipAddress_tos &k) const {
  430. using std::size_t;
  431. using std::hash;
  432. using std::string;
  433. return ((hash<string>()(k.ipAddress)
  434. ^ (hash<int>()(k.tosValue) << 1)) >> 1);
  435. }
  436. };
  437. template<>
  438. struct hash<ipAddress_win> {
  439. std::size_t operator()(const ipAddress_win &k) const {
  440. using std::size_t;
  441. using std::hash;
  442. using std::string;
  443. return ((hash<string>()(k.ipAddress)
  444. ^ (hash<int>()(k.winSize) << 1)) >> 1);
  445. }
  446. };
  447. template<>
  448. struct hash<conv> {
  449. std::size_t operator()(const conv &k) const {
  450. using std::size_t;
  451. using std::hash;
  452. using std::string;
  453. return ((hash<string>()(k.ipAddressA)
  454. ^ (hash<int>()(k.portA) << 1)) >> 1)
  455. ^ ((hash<string>()(k.ipAddressB)
  456. ^ (hash<int>()(k.portB) << 1)) >> 1);
  457. }
  458. };
  459. template<>
  460. struct hash<convWithProt> {
  461. std::size_t operator()(const convWithProt &c) const {
  462. using std::size_t;
  463. using std::hash;
  464. using std::string;
  465. return ((hash<string>()(c.ipAddressA)
  466. ^ (hash<int>()(c.portA) << 1)) >> 1)
  467. ^ ((hash<string>()(c.ipAddressB)
  468. ^ (hash<int>()(c.portB) << 1)) >> 1)
  469. ^ (hash<string>()(c.protocol));
  470. }
  471. };
  472. template<>
  473. struct hash<ipAddress_protocol> {
  474. std::size_t operator()(const ipAddress_protocol &k) const {
  475. using std::size_t;
  476. using std::hash;
  477. using std::string;
  478. return ((hash<string>()(k.ipAddress)
  479. ^ (hash<string>()(k.protocol) << 1)) >> 1);
  480. }
  481. };
  482. template<>
  483. struct hash<ipAddress_inOut_port> {
  484. std::size_t operator()(const ipAddress_inOut_port &k) const {
  485. using std::size_t;
  486. using std::hash;
  487. using std::string;
  488. return ((hash<string>()(k.ipAddress)
  489. ^ (hash<string>()(k.trafficDirection) << 1)) >> 1)
  490. ^ (hash<int>()(k.portNumber) << 1);
  491. }
  492. };
  493. template<>
  494. struct hash<unrecognized_PDU> {
  495. std::size_t operator()(const unrecognized_PDU &k) const {
  496. using std::size_t;
  497. using std::hash;
  498. using std::string;
  499. return ((hash<string>()(k.srcMacAddress)
  500. ^ (hash<string>()(k.dstMacAddress) << 1)) >> 1)
  501. ^ (hash<uint32_t>()(k.typeNumber) << 1);
  502. }
  503. };
  504. }
  505. class statistics {
  506. public:
  507. /*
  508. * Constructor
  509. */
  510. statistics(std::string resourcePath);
  511. /*
  512. * Methods
  513. */
  514. std::string getFormattedTimestamp(time_t seconds, suseconds_t microseconds) const;
  515. /*
  516. * Access methods for containers
  517. */
  518. void incrementPacketCount();
  519. void calculateIPIntervalPacketRate(std::chrono::duration<int, std::micro> interval, std::chrono::microseconds intervalStartTimestamp);
  520. void incrementMSScount(const std::string &ipAddress, int mssValue);
  521. void incrementWinCount(const std::string &ipAddress, int winSize);
  522. void addConvStat(const std::string &ipAddressSender,int sport, const std::string &ipAddressReceiver,int dport, std::chrono::microseconds timestamp);
  523. void addConvStatExt(const std::string &ipAddressSender,int sport, const std::string &ipAddressReceiver,int dport, const std::string &protocol, std::chrono::microseconds timestamp);
  524. void createCommIntervalStats();
  525. std::vector<double> calculateIPsCumEntropy();
  526. std::vector<double> calculateLastIntervalIPsEntropy(std::chrono::microseconds intervalStartTimestamp);
  527. std::vector<double> calculateEntropies(std::unordered_map<int, int> &map, std::unordered_map<int, int> &old);
  528. void addIntervalStat(std::chrono::duration<int, std::micro> interval, std::chrono::microseconds intervalStartTimestamp, std::chrono::microseconds lastPktTimestamp);
  529. void checkPayload(const PDU *pdu_l4);
  530. void checkTCPChecksum(const std::string &ipAddressSender, const std::string &ipAddressReceiver, TCP tcpPkt);
  531. void checkToS(uint8_t ToS);
  532. void incrementToScount(const std::string &ipAddress, int tosValue);
  533. void incrementTTLcount(const std::string &ipAddress, int ttlValue);
  534. void incrementProtocolCount(const std::string &ipAddress, const std::string &protocol);
  535. void increaseProtocolByteCount(const std::string &ipAddress, const std::string &protocol, long bytesSent);
  536. void incrementUnrecognizedPDUCount(const std::string &srcMac, const std::string &dstMac, uint32_t typeNumber,
  537. const std::string &timestamp);
  538. void incrementPortCount(const std::string &ipAddressSender, int outgoingPort, const std::string &ipAddressReceiver,
  539. int incomingPort, const std::string &protocol);
  540. void increasePortByteCount(const std::string &ipAddressSender, int outgoingPort, const std::string &ipAddressReceiver,
  541. int incomingPort, long bytesSent, const std::string &protocol);
  542. int getProtocolCount(const std::string &ipAddress, const std::string &protocol);
  543. float getProtocolByteCount(const std::string &ipAddress, const std::string &protocol);
  544. void setTimestampFirstPacket(Tins::Timestamp ts);
  545. void setTimestampLastPacket(Tins::Timestamp ts);
  546. Tins::Timestamp getTimestampFirstPacket();
  547. Tins::Timestamp getTimestampLastPacket();
  548. void assignMacAddress(const std::string &ipAddress, const std::string &macAddress);
  549. void addIpStat_packetSent(const std::string &ipAddressSender, const std::string &ipAddressReceiver, long bytesSent, std::chrono::microseconds timestamp);
  550. int getPacketCount();
  551. int getSumPacketSize();
  552. void addMSS(const std::string &ipAddress, int MSSvalue);
  553. void writeToDatabase(std::string database_path, std::vector<std::chrono::duration<int, std::micro>> timeInterval, bool del);
  554. void writeIntervalsToDatabase(std::string database_path, std::vector<std::chrono::duration<int, std::micro>> timeIntervals, bool del);
  555. void addPacketSize(uint32_t packetSize);
  556. std::string getCaptureDurationTimestamp() const;
  557. float getCaptureDurationSeconds() const;
  558. float getAvgPacketSize() const;
  559. void printStats(const std::string &ipAddress);
  560. bool getDoExtraTests();
  561. void setDoExtraTests(bool var);
  562. int getDefaultInterval();
  563. void setDefaultInterval(int interval);
  564. /*
  565. * IP Address-specific statistics
  566. */
  567. ip_stats getStatsForIP(const std::string &ipAddress);
  568. private:
  569. /*
  570. * Data fields
  571. */
  572. Tins::Timestamp timestamp_firstPacket;
  573. Tins::Timestamp timestamp_lastPacket;
  574. float sumPacketSize = 0;
  575. int packetCount = 0;
  576. std::string resourcePath;
  577. /* Extra tests includes:
  578. * - calculate IPs entropies for intervals
  579. * - calculate IPs cumulative entropies interval-wise
  580. * - check payload availability
  581. * - chech TCP checksum correctness
  582. */
  583. bool doExtraTests = false;
  584. int payloadCount = 0;
  585. int incorrectTCPChecksumCount = 0;
  586. int correctTCPChecksumCount = 0;
  587. // Variables that are used for interval-wise statistics
  588. int intervalPayloadCount = 0;
  589. int intervalIncorrectTCPChecksumCount = 0;
  590. int intervalCorrectTCPChecksumCount = 0;
  591. int intervalCumPktCount = 0;
  592. float intervalCumSumPktSize = 0;
  593. size_t ip_src_novel_count = 0;
  594. size_t ip_dst_novel_count = 0;
  595. int intervalCumNovelIPCount = 0;
  596. int intervalCumNovelTTLCount = 0;
  597. int intervalCumNovelWinSizeCount = 0;
  598. int intervalCumNovelToSCount = 0;
  599. int intervalCumNovelMSSCount = 0;
  600. int intervalCumNovelPortCount = 0;
  601. std::unordered_map<std::string, entry_ipStat> intervalCumIPStats;
  602. std::unordered_map<int,int> intervalCumTTLValues;
  603. std::unordered_map<int,int> intervalCumWinSizeValues;
  604. std::unordered_map<int,int> intervalCumTosValues;
  605. std::unordered_map<int,int> intervalCumMSSValues;
  606. std::unordered_map<int,int> intervalCumPortValues;
  607. int default_interval = 0;
  608. /*
  609. * Data containers
  610. */
  611. // {IP Address, TTL value, count}
  612. std::unordered_map<ipAddress_ttl, int> ttl_distribution;
  613. // {IP Address, MSS value, count}
  614. std::unordered_map<ipAddress_mss, int> mss_distribution;
  615. // {IP Address, Win size, count}
  616. std::unordered_map<ipAddress_win, int> win_distribution;
  617. // {IP Address, ToS value, count}
  618. std::unordered_map<ipAddress_tos, int> tos_distribution;
  619. // {IP Address A, Port A, IP Address B, Port B, #packets, packets timestamps, inter-arrival times,
  620. // average of inter-arrival times}
  621. std::unordered_map<conv, entry_convStat> conv_statistics;
  622. // {IP Address A, Port A, IP Address B, Port B, comm_intervals, #packets, avg. pkt rate, avg. #packets per interval,
  623. // avg. time between intervals, avg. interval time, duration, packets timestamps, inter-arrivtal times, average of inter-arrival times}
  624. // Also stores conversation with only one exchanged message. In this case avgPktRate, minDelay, maxDelay and avgDelay are -1
  625. std::unordered_map<convWithProt, entry_convStatExt> conv_statistics_extended;
  626. // {Last timestamp in the interval, #packets, #bytes, source IP entropy, destination IP entropy,
  627. // source IP cumulative entropy, destination IP cumulative entropy, #payload, #incorrect TCP checksum,
  628. // #correct TCP checksum, #novel IP, #novel TTL, #novel Window Size, #novel ToS,#novel MSS}
  629. std::unordered_map<std::string, entry_intervalStat> interval_statistics;
  630. // {TTL value, count}
  631. std::unordered_map<int, int> ttl_values;
  632. // {Win size, count}
  633. std::unordered_map<int, int> win_values;
  634. // {ToS, count}
  635. std::unordered_map<int, int> tos_values;
  636. // {MSS, count}
  637. std::unordered_map<int, int> mss_values;
  638. // {Port, count}
  639. std::unordered_map<int, int> port_values;
  640. //{IP Address, contacted IP Addresses}
  641. std::unordered_map<std::string, std::unordered_set<std::string>> contacted_ips;
  642. // {IP Address, Protocol, #count, #Data transmitted in bytes}
  643. std::unordered_map<ipAddress_protocol, entry_protocolStat> protocol_distribution;
  644. // {IP Address, #received packets, #sent packets, Data received in kbytes, Data sent in kbytes}
  645. std::unordered_map<std::string, entry_ipStat> ip_statistics;
  646. // {IP Address, in_out, Port Number, #count, #Data transmitted in bytes}
  647. std::unordered_map<ipAddress_inOut_port, entry_portStat> ip_ports;
  648. // {IP Address, MAC Address}
  649. std::unordered_map<std::string, std::string> ip_mac_mapping;
  650. // {Source MAC, Destination MAC, typeNumber, #count, #timestamp of last occurrence}
  651. std::unordered_map<unrecognized_PDU, unrecognized_PDU_stat> unrecognized_PDUs;
  652. };
  653. #endif //CPP_PCAPREADER_STATISTICS_H