botnet_comm_processor.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. /*
  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 = boost::python;
  34. namespace pt = boost::property_tree;
  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::list find_interval(int number_ids, double max_int_time);
  90. py::list get_messages(unsigned int start_idx, unsigned int end_idx);
  91. unsigned int parse_csv(const std::string &);
  92. unsigned int parse_xml(const std::string &);
  93. std::string write_xml(const std::string &);
  94. private:
  95. /*
  96. * Methods
  97. */
  98. py::list convert_intervals_to_py_repr(const std::vector<comm_interval>& intervals);
  99. void find_interval_helper(std::promise<std::vector<comm_interval> > && p, int number_ids, double max_int_time, int start_idx, int end_idx);
  100. int msgtype_is_request(unsigned short mtype);
  101. int msgtype_is_response(unsigned short mtype);
  102. // void print_message(const abstract_msg &message);
  103. void process_csv_attrib(abstract_msg &msg, const std::string &cur_word);
  104. void process_kv(abstract_msg &msg, const std::string &key, const std::string &value);
  105. void process_xml_attrib_assign(abstract_msg &msg, const std::string &cur_word);
  106. /*
  107. * Attributes
  108. */
  109. std::vector<abstract_msg> messages;
  110. };
  111. #endif //BOTNET_COMM_PROCESSOR_H