statistics.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  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_cum_entropy;
  225. double ip_dst_cum_entropy;
  226. std::vector<double> ttl_entropies;
  227. std::vector<double> win_size_entropies;
  228. std::vector<double> tos_entropies;
  229. std::vector<double> mss_entropies;
  230. std::vector<double> port_entropies;
  231. int payload_count;
  232. int incorrect_tcp_checksum_count;
  233. int correct_tcp_checksum_count;
  234. size_t novel_ip_src_count;
  235. size_t novel_ip_dst_count;
  236. int novel_ttl_count;
  237. int novel_win_size_count;
  238. int novel_tos_count;
  239. int novel_mss_count;
  240. int novel_port_count;
  241. // FIXME: add new attributes to operator==
  242. bool operator==(const entry_intervalStat &other) const {
  243. return start == other.start
  244. && end == other.end
  245. && pkts_count == other.pkts_count
  246. && pkt_rate == other.pkt_rate
  247. && kbytes == other.kbytes
  248. && kbyte_rate == other.kbyte_rate
  249. && ip_src_entropy == other.ip_src_entropy
  250. && ip_dst_entropy == other.ip_dst_entropy
  251. && ip_src_novel_entropy == other.ip_src_novel_entropy
  252. && ip_dst_novel_entropy == other.ip_dst_novel_entropy
  253. && ip_src_cum_entropy == other.ip_src_cum_entropy
  254. && ip_dst_cum_entropy == other.ip_dst_cum_entropy
  255. && payload_count == other.payload_count
  256. && incorrect_tcp_checksum_count == other.incorrect_tcp_checksum_count
  257. && correct_tcp_checksum_count == other.correct_tcp_checksum_count
  258. && novel_ip_src_count == other.novel_ip_src_count
  259. && novel_ip_dst_count == other.novel_ip_dst_count
  260. && novel_ttl_count == other.novel_ttl_count
  261. && novel_win_size_count == other.novel_win_size_count
  262. && novel_tos_count == other.novel_tos_count
  263. && novel_mss_count == other.novel_mss_count
  264. && novel_port_count == other.novel_port_count
  265. && ttl_entropies == other.ttl_entropies
  266. && win_size_entropies == other.win_size_entropies
  267. && tos_entropies == other.tos_entropies
  268. && mss_entropies == other.mss_entropies
  269. && port_entropies == other.port_entropies;
  270. }
  271. };
  272. /*
  273. * Struct used to represent converstaion statistics:
  274. * - # packets
  275. * - Average packet rate
  276. * - Timestamps of packets
  277. * - Inter-arrival time
  278. * - Average inter-arrival time
  279. */
  280. struct entry_convStat {
  281. long pkts_count;
  282. float avg_pkt_rate;
  283. std::vector<std::chrono::microseconds> pkts_timestamp;
  284. std::vector<std::chrono::microseconds> interarrival_time;
  285. std::chrono::microseconds avg_interarrival_time;
  286. bool operator==(const entry_convStat &other) const {
  287. return pkts_count == other.pkts_count
  288. && avg_pkt_rate == avg_pkt_rate
  289. && pkts_timestamp == other.pkts_timestamp
  290. && interarrival_time == other.interarrival_time
  291. && avg_interarrival_time == other.avg_interarrival_time;
  292. }
  293. };
  294. /*
  295. * Struct used to represent:
  296. * - IP address (IPv4 or IPv6)
  297. - Traffic direction (out: outgoing connection, in: incoming connection)
  298. * - Port number
  299. */
  300. struct ipAddress_inOut_port {
  301. std::string ipAddress;
  302. std::string trafficDirection;
  303. int portNumber;
  304. std::string protocol;
  305. bool operator==(const ipAddress_inOut_port &other) const {
  306. return ipAddress == other.ipAddress
  307. && trafficDirection == other.trafficDirection
  308. && portNumber == other.portNumber
  309. && protocol == other.protocol;
  310. }
  311. };
  312. /*
  313. * Struct used to represent a communication interval (for two hosts):
  314. * - Timestamp of the first packet in the interval
  315. * - Timestamp of the last packet in the interval
  316. * - The count of packets within the interval
  317. */
  318. struct commInterval{
  319. std::chrono::microseconds start;
  320. std::chrono::microseconds end;
  321. long pkts_count;
  322. bool operator==(const commInterval &other) const {
  323. return start == other.start
  324. && end == other.end
  325. && pkts_count == other.pkts_count;
  326. }
  327. };
  328. /*
  329. * Struct used to represent converstaion statistics:
  330. * - commnication intervals
  331. * - # packets
  332. * - Average packet rate
  333. * - average # packets per communication interval
  334. * - Average time between intervals
  335. * - Average duration of a communication interval
  336. * - Overall communication duration
  337. * - Timestamps of packets
  338. * - Inter-arrival time
  339. * - Average inter-arrival time
  340. */
  341. struct entry_convStatExt {
  342. std::vector<commInterval> comm_intervals;
  343. long pkts_count;
  344. float avg_pkt_rate;
  345. double avg_int_pkts_count;
  346. double avg_time_between_ints;
  347. double avg_interval_time;
  348. double total_comm_duration;
  349. std::chrono::duration<int, std::micro> timeInterval;
  350. std::vector<std::chrono::microseconds> pkts_timestamp;
  351. std::vector<std::chrono::microseconds> interarrival_time;
  352. std::chrono::microseconds avg_interarrival_time;
  353. bool operator==(const entry_convStatExt &other) const {
  354. return comm_intervals == other.comm_intervals
  355. && pkts_count == other.pkts_count
  356. && avg_pkt_rate == avg_pkt_rate
  357. && avg_int_pkts_count == other.avg_int_pkts_count
  358. && avg_time_between_ints == other.avg_time_between_ints
  359. && avg_interval_time == other.avg_interval_time
  360. && total_comm_duration == other.total_comm_duration
  361. && pkts_timestamp == other.pkts_timestamp
  362. && interarrival_time == other.interarrival_time
  363. && avg_interarrival_time == other.avg_interarrival_time;
  364. }
  365. };
  366. /*
  367. * Struct used to represent:
  368. * - Source MAC address
  369. * - Destination MAC address
  370. * - Payload type number
  371. */
  372. struct unrecognized_PDU {
  373. std::string srcMacAddress;
  374. std::string dstMacAddress;
  375. uint32_t typeNumber;
  376. bool operator==(const unrecognized_PDU &other) const {
  377. return srcMacAddress == other.srcMacAddress
  378. && dstMacAddress == other.dstMacAddress
  379. && typeNumber == other.typeNumber;
  380. }
  381. };
  382. /*
  383. * Struct used to represent:
  384. * - Number of occurrences
  385. * - Formatted timestamp of last occurrence
  386. */
  387. struct unrecognized_PDU_stat {
  388. int count;
  389. std::string timestamp_last_occurrence;
  390. };
  391. /*
  392. * Definition of hash functions for structs used as key in unordered_map
  393. */
  394. namespace std {
  395. template<>
  396. struct hash<ipAddress_ttl> {
  397. std::size_t operator()(const ipAddress_ttl &k) const {
  398. using std::size_t;
  399. using std::hash;
  400. using std::string;
  401. return ((hash<string>()(k.ipAddress)
  402. ^ (hash<int>()(k.ttlValue) << 1)) >> 1);
  403. }
  404. };
  405. template<>
  406. struct hash<ipAddress_mss> {
  407. std::size_t operator()(const ipAddress_mss &k) const {
  408. using std::size_t;
  409. using std::hash;
  410. using std::string;
  411. return ((hash<string>()(k.ipAddress)
  412. ^ (hash<int>()(k.mssValue) << 1)) >> 1);
  413. }
  414. };
  415. template<>
  416. struct hash<ipAddress_tos> {
  417. std::size_t operator()(const ipAddress_tos &k) const {
  418. using std::size_t;
  419. using std::hash;
  420. using std::string;
  421. return ((hash<string>()(k.ipAddress)
  422. ^ (hash<int>()(k.tosValue) << 1)) >> 1);
  423. }
  424. };
  425. template<>
  426. struct hash<ipAddress_win> {
  427. std::size_t operator()(const ipAddress_win &k) const {
  428. using std::size_t;
  429. using std::hash;
  430. using std::string;
  431. return ((hash<string>()(k.ipAddress)
  432. ^ (hash<int>()(k.winSize) << 1)) >> 1);
  433. }
  434. };
  435. template<>
  436. struct hash<conv> {
  437. std::size_t operator()(const conv &k) const {
  438. using std::size_t;
  439. using std::hash;
  440. using std::string;
  441. return ((hash<string>()(k.ipAddressA)
  442. ^ (hash<int>()(k.portA) << 1)) >> 1)
  443. ^ ((hash<string>()(k.ipAddressB)
  444. ^ (hash<int>()(k.portB) << 1)) >> 1);
  445. }
  446. };
  447. template<>
  448. struct hash<convWithProt> {
  449. std::size_t operator()(const convWithProt &c) const {
  450. using std::size_t;
  451. using std::hash;
  452. using std::string;
  453. return ((hash<string>()(c.ipAddressA)
  454. ^ (hash<int>()(c.portA) << 1)) >> 1)
  455. ^ ((hash<string>()(c.ipAddressB)
  456. ^ (hash<int>()(c.portB) << 1)) >> 1)
  457. ^ (hash<string>()(c.protocol));
  458. }
  459. };
  460. template<>
  461. struct hash<ipAddress_protocol> {
  462. std::size_t operator()(const ipAddress_protocol &k) const {
  463. using std::size_t;
  464. using std::hash;
  465. using std::string;
  466. return ((hash<string>()(k.ipAddress)
  467. ^ (hash<string>()(k.protocol) << 1)) >> 1);
  468. }
  469. };
  470. template<>
  471. struct hash<ipAddress_inOut_port> {
  472. std::size_t operator()(const ipAddress_inOut_port &k) const {
  473. using std::size_t;
  474. using std::hash;
  475. using std::string;
  476. return ((hash<string>()(k.ipAddress)
  477. ^ (hash<string>()(k.trafficDirection) << 1)) >> 1)
  478. ^ (hash<int>()(k.portNumber) << 1);
  479. }
  480. };
  481. template<>
  482. struct hash<unrecognized_PDU> {
  483. std::size_t operator()(const unrecognized_PDU &k) const {
  484. using std::size_t;
  485. using std::hash;
  486. using std::string;
  487. return ((hash<string>()(k.srcMacAddress)
  488. ^ (hash<string>()(k.dstMacAddress) << 1)) >> 1)
  489. ^ (hash<uint32_t>()(k.typeNumber) << 1);
  490. }
  491. };
  492. }
  493. class statistics {
  494. public:
  495. /*
  496. * Constructor
  497. */
  498. statistics(std::string resourcePath);
  499. /*
  500. * Methods
  501. */
  502. std::string getFormattedTimestamp(time_t seconds, suseconds_t microseconds) const;
  503. /*
  504. * Access methods for containers
  505. */
  506. void incrementPacketCount();
  507. void calculateIPIntervalPacketRate(std::chrono::duration<int, std::micro> interval, std::chrono::microseconds intervalStartTimestamp);
  508. void incrementMSScount(const std::string &ipAddress, int mssValue);
  509. void incrementWinCount(const std::string &ipAddress, int winSize);
  510. void addConvStat(const std::string &ipAddressSender,int sport, const std::string &ipAddressReceiver,int dport, std::chrono::microseconds timestamp);
  511. void addConvStatExt(const std::string &ipAddressSender,int sport, const std::string &ipAddressReceiver,int dport, const std::string &protocol, std::chrono::microseconds timestamp);
  512. void createCommIntervalStats();
  513. std::vector<float> calculateIPsCumEntropy();
  514. std::vector<double> calculateLastIntervalIPsEntropy(std::chrono::microseconds intervalStartTimestamp);
  515. std::vector<double> calculateEntropies(std::unordered_map<int, int> &map, std::unordered_map<int, int> &old);
  516. void addIntervalStat(std::chrono::duration<int, std::micro> interval, std::chrono::microseconds intervalStartTimestamp, std::chrono::microseconds lastPktTimestamp);
  517. void checkPayload(const PDU *pdu_l4);
  518. void checkTCPChecksum(const std::string &ipAddressSender, const std::string &ipAddressReceiver, TCP tcpPkt);
  519. void checkToS(uint8_t ToS);
  520. void incrementToScount(const std::string &ipAddress, int tosValue);
  521. void incrementTTLcount(const std::string &ipAddress, int ttlValue);
  522. void incrementProtocolCount(const std::string &ipAddress, const std::string &protocol);
  523. void increaseProtocolByteCount(const std::string &ipAddress, const std::string &protocol, long bytesSent);
  524. void incrementUnrecognizedPDUCount(const std::string &srcMac, const std::string &dstMac, uint32_t typeNumber,
  525. const std::string &timestamp);
  526. void incrementPortCount(const std::string &ipAddressSender, int outgoingPort, const std::string &ipAddressReceiver,
  527. int incomingPort, const std::string &protocol);
  528. void increasePortByteCount(const std::string &ipAddressSender, int outgoingPort, const std::string &ipAddressReceiver,
  529. int incomingPort, long bytesSent, const std::string &protocol);
  530. int getProtocolCount(const std::string &ipAddress, const std::string &protocol);
  531. float getProtocolByteCount(const std::string &ipAddress, const std::string &protocol);
  532. void setTimestampFirstPacket(Tins::Timestamp ts);
  533. void setTimestampLastPacket(Tins::Timestamp ts);
  534. Tins::Timestamp getTimestampFirstPacket();
  535. Tins::Timestamp getTimestampLastPacket();
  536. void assignMacAddress(const std::string &ipAddress, const std::string &macAddress);
  537. void addIpStat_packetSent(const std::string &ipAddressSender, const std::string &ipAddressReceiver, long bytesSent, std::chrono::microseconds timestamp);
  538. int getPacketCount();
  539. int getSumPacketSize();
  540. void addMSS(const std::string &ipAddress, int MSSvalue);
  541. void writeToDatabase(std::string database_path, std::vector<std::chrono::duration<int, std::micro>> timeInterval, bool del);
  542. void writeIntervalsToDatabase(std::string database_path, std::vector<std::chrono::duration<int, std::micro>> timeIntervals, bool del);
  543. void addPacketSize(uint32_t packetSize);
  544. std::string getCaptureDurationTimestamp() const;
  545. float getCaptureDurationSeconds() const;
  546. float getAvgPacketSize() const;
  547. void printStats(const std::string &ipAddress);
  548. bool getDoExtraTests();
  549. void setDoExtraTests(bool var);
  550. int getDefaultInterval();
  551. void setDefaultInterval(int interval);
  552. /*
  553. * IP Address-specific statistics
  554. */
  555. ip_stats getStatsForIP(const std::string &ipAddress);
  556. private:
  557. /*
  558. * Data fields
  559. */
  560. Tins::Timestamp timestamp_firstPacket;
  561. Tins::Timestamp timestamp_lastPacket;
  562. float sumPacketSize = 0;
  563. int packetCount = 0;
  564. std::string resourcePath;
  565. /* Extra tests includes:
  566. * - calculate IPs entropies for intervals
  567. * - calculate IPs cumulative entropies interval-wise
  568. * - check payload availability
  569. * - chech TCP checksum correctness
  570. */
  571. bool doExtraTests = false;
  572. int payloadCount = 0;
  573. int incorrectTCPChecksumCount = 0;
  574. int correctTCPChecksumCount = 0;
  575. // Variables that are used for interval-wise statistics
  576. int intervalPayloadCount = 0;
  577. int intervalIncorrectTCPChecksumCount = 0;
  578. int intervalCorrectTCPChecksumCount = 0;
  579. int intervalCumPktCount = 0;
  580. float intervalCumSumPktSize = 0;
  581. size_t ip_src_novel_count = 0;
  582. size_t ip_dst_novel_count = 0;
  583. int intervalCumNovelIPCount = 0;
  584. int intervalCumNovelTTLCount = 0;
  585. int intervalCumNovelWinSizeCount = 0;
  586. int intervalCumNovelToSCount = 0;
  587. int intervalCumNovelMSSCount = 0;
  588. int intervalCumNovelPortCount = 0;
  589. std::unordered_map<std::string, entry_ipStat> intervalCumIPStats;
  590. std::unordered_map<int,int> intervalCumTTLValues;
  591. std::unordered_map<int,int> intervalCumWinSizeValues;
  592. std::unordered_map<int,int> intervalCumTosValues;
  593. std::unordered_map<int,int> intervalCumMSSValues;
  594. std::unordered_map<int,int> intervalCumPortValues;
  595. int default_interval = 0;
  596. /*
  597. * Data containers
  598. */
  599. // {IP Address, TTL value, count}
  600. std::unordered_map<ipAddress_ttl, int> ttl_distribution;
  601. // {IP Address, MSS value, count}
  602. std::unordered_map<ipAddress_mss, int> mss_distribution;
  603. // {IP Address, Win size, count}
  604. std::unordered_map<ipAddress_win, int> win_distribution;
  605. // {IP Address, ToS value, count}
  606. std::unordered_map<ipAddress_tos, int> tos_distribution;
  607. // {IP Address A, Port A, IP Address B, Port B, #packets, packets timestamps, inter-arrival times,
  608. // average of inter-arrival times}
  609. std::unordered_map<conv, entry_convStat> conv_statistics;
  610. // {IP Address A, Port A, IP Address B, Port B, comm_intervals, #packets, avg. pkt rate, avg. #packets per interval,
  611. // avg. time between intervals, avg. interval time, duration, packets timestamps, inter-arrivtal times, average of inter-arrival times}
  612. // Also stores conversation with only one exchanged message. In this case avgPktRate, minDelay, maxDelay and avgDelay are -1
  613. std::unordered_map<convWithProt, entry_convStatExt> conv_statistics_extended;
  614. // {Last timestamp in the interval, #packets, #bytes, source IP entropy, destination IP entropy,
  615. // source IP cumulative entropy, destination IP cumulative entropy, #payload, #incorrect TCP checksum,
  616. // #correct TCP checksum, #novel IP, #novel TTL, #novel Window Size, #novel ToS,#novel MSS}
  617. std::unordered_map<std::string, entry_intervalStat> interval_statistics;
  618. // {TTL value, count}
  619. std::unordered_map<int, int> ttl_values;
  620. // {Win size, count}
  621. std::unordered_map<int, int> win_values;
  622. // {ToS, count}
  623. std::unordered_map<int, int> tos_values;
  624. // {MSS, count}
  625. std::unordered_map<int, int> mss_values;
  626. // {Port, count}
  627. std::unordered_map<int, int> port_values;
  628. //{IP Address, contacted IP Addresses}
  629. std::unordered_map<std::string, std::unordered_set<std::string>> contacted_ips;
  630. // {IP Address, Protocol, #count, #Data transmitted in bytes}
  631. std::unordered_map<ipAddress_protocol, entry_protocolStat> protocol_distribution;
  632. // {IP Address, #received packets, #sent packets, Data received in kbytes, Data sent in kbytes}
  633. std::unordered_map<std::string, entry_ipStat> ip_statistics;
  634. // {IP Address, in_out, Port Number, #count, #Data transmitted in bytes}
  635. std::unordered_map<ipAddress_inOut_port, entry_portStat> ip_ports;
  636. // {IP Address, MAC Address}
  637. std::unordered_map<std::string, std::string> ip_mac_mapping;
  638. // {Source MAC, Destination MAC, typeNumber, #count, #timestamp of last occurrence}
  639. std::unordered_map<unrecognized_PDU, unrecognized_PDU_stat> unrecognized_PDUs;
  640. };
  641. #endif //CPP_PCAPREADER_STATISTICS_H