botnet_comm_processor.h 4.0 KB

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