botnet_comm_processor.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Class for processing messages containing abstract Membership Management Communication.
  3. * A message has to consist of (namely): Src, Dst, Type, Time.
  4. */
  5. #ifndef BOTNET_COMM_PROCESSOR_H
  6. #define BOTNET_COMM_PROCESSOR_H
  7. #include <iostream>
  8. #include <pybind11/pybind11.h>
  9. #include <vector>
  10. #include <thread>
  11. #include <deque>
  12. #include <set>
  13. #include <future>
  14. #include <fstream>
  15. #include <string>
  16. #include <istream>
  17. #include <iomanip>
  18. /*
  19. * Botnet communication types (equal to the ones contained in the MessageType class in MembersMgmtCommAttack.py)
  20. */
  21. #define TIMEOUT 3
  22. #define SALITY_NL_REQUEST 101
  23. #define SALITY_NL_REPLY 102
  24. #define SALITY_HELLO 103
  25. #define SALITY_HELLO_REPLY 104
  26. /*
  27. * Needed because of machine inprecision. E.g a time difference of 0.1s is stored as >0.1s
  28. */
  29. #define EPS_TOLERANCE 1e-12 // works for a difference of 0.1
  30. /*
  31. * For quick usage
  32. */
  33. namespace py = pybind11;
  34. /*
  35. * Definition of structs
  36. */
  37. /*
  38. * Struct used as data structure to represent an abstract communication message:
  39. * - Source ID
  40. * - Destination ID
  41. * - Message type
  42. * - Time of message
  43. */
  44. struct abstract_msg {
  45. // necessary constructors to have default values
  46. abstract_msg (unsigned int src, unsigned int dst, unsigned short type, double time, int line_no) :
  47. src(src), dst(dst), type(type), time(time), line_no(line_no) {}
  48. abstract_msg () {}
  49. // members
  50. unsigned int src = 0;
  51. unsigned int dst = 0;
  52. unsigned short type = 0;
  53. double time = 0.0;
  54. int line_no = -1;
  55. };
  56. /*
  57. * Struct used as data structure to represent an interval of communication:
  58. * - A set of all initiator IDs contained in the interval
  59. * - The number of messages sent in the interval (excluding timeouts)
  60. * - The start index of the interval with respect to the member variable 'packets'
  61. * - The end index of the interval with respect to the member variable 'packets'
  62. */
  63. struct comm_interval {
  64. std::set<unsigned int> ids;
  65. unsigned int comm_sum;
  66. unsigned int start_idx;
  67. unsigned int end_idx;
  68. };
  69. /*
  70. * A greater than operator desgined to handle slight machine inprecision up to EPS_TOLERANCE.
  71. * @param a The first number
  72. * @param b The second number
  73. * @return true (1) if a > b, otherwise false(0)
  74. */
  75. int greater_than(double a, double b){
  76. return b - a < -1 * EPS_TOLERANCE;
  77. }
  78. class botnet_comm_processor {
  79. public:
  80. /*
  81. * Class constructor
  82. */
  83. botnet_comm_processor();
  84. botnet_comm_processor(const py::list &messages_pyboost);
  85. /*
  86. * Methods
  87. */
  88. py::dict find_interval_from_startidx(int start_idx, int number_ids, double max_int_time);
  89. py::dict find_interval_from_endidx(int end_idx, int number_ids, double max_int_time);
  90. py::list find_optimal_interval(int number_ids, double max_int_time);
  91. py::list get_interval_init_ids(int start_idx, int end_idx);
  92. py::list get_messages(unsigned int start_idx, unsigned int end_idx);
  93. int get_message_count();
  94. unsigned int parse_csv(const std::string &filepath);
  95. unsigned int parse_xml(const std::string &filepath);
  96. void set_messages(const py::list &messages_pyboost);
  97. std::string write_xml(const std::string &out_dir, const std::string &basename);
  98. private:
  99. /*
  100. * Methods
  101. */
  102. py::list convert_intervals_to_py_repr(const std::vector<comm_interval>& intervals);
  103. void find_optimal_interval_helper(std::promise<std::vector<comm_interval> > && p, int number_ids, double max_int_time, int start_idx, int end_idx);
  104. int msgtype_is_request(unsigned short mtype);
  105. int msgtype_is_response(unsigned short mtype);
  106. // void print_message(const abstract_msg &message);
  107. void process_csv_attrib(abstract_msg &msg, const std::string &cur_word);
  108. void process_kv(abstract_msg &msg, const std::string &key, const std::string &value);
  109. void process_xml_attrib_assign(abstract_msg &msg, const std::string &cur_word);
  110. /*
  111. * Attributes
  112. */
  113. std::vector<abstract_msg> messages;
  114. };
  115. #endif //BOTNET_COMM_PROCESSOR_H