botnet_comm_processor.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 <vector>
  10. #include <chrono>
  11. #include <thread>
  12. #include <deque>
  13. #include <set>
  14. #include <future>
  15. /*
  16. * Botnet communication types (equal to the ones contained in the MessageType class in MembersMgmtCommAttack.py)
  17. */
  18. #define TIMEOUT 3
  19. #define SALITY_NL_REQUEST 101
  20. #define SALITY_NL_REPLY 102
  21. #define SALITY_HELLO 103
  22. #define SALITY_HELLO_REPLY 104
  23. /*
  24. * Needed because of machine inprecision. E.g a time difference of 0.1s is stored as >0.1s
  25. */
  26. #define EPS_TOLERANCE 1e-12 // works for a difference of 0.1
  27. /*
  28. * For quick usage
  29. */
  30. namespace py = boost::python;
  31. /*
  32. * Definition of structs
  33. */
  34. /*
  35. * Struct used as data structure to represent an abstract communication message:
  36. * - Source ID
  37. * - Destination ID
  38. * - Message type
  39. * - Time of message
  40. */
  41. struct abstract_msg {
  42. unsigned int src;
  43. unsigned int dst;
  44. unsigned short type;
  45. double time;
  46. };
  47. /*
  48. * Struct used as data structure to represent an interval of communication:
  49. * - A set of all initiator IDs contained in the interval
  50. * - The number of messages sent in the interval (excluding timeouts)
  51. * - The start index of the interval with respect to the member variable 'packets'
  52. * - The end index of the interval with respect to the member variable 'packets'
  53. */
  54. struct comm_interval {
  55. std::set<unsigned int> ids;
  56. unsigned int comm_sum;
  57. unsigned int start_idx;
  58. unsigned int end_idx;
  59. };
  60. /*
  61. * A greater than operator desgined to handle slight machine inprecision up to EPS_TOLERANCE.
  62. * @param a The first number
  63. * @param b The second number
  64. * @return true (1) if a > b, otherwise false(0)
  65. */
  66. int greater_than(double a, double b){
  67. return b - a < -1 * EPS_TOLERANCE;
  68. }
  69. class botnet_comm_processor {
  70. public:
  71. /*
  72. * Class constructor
  73. */
  74. botnet_comm_processor(py::list packets);
  75. /*
  76. * Methods
  77. */
  78. py::list find_interval(int number_ids, double max_int_time);
  79. private:
  80. /*
  81. * Methods
  82. */
  83. void print_message(abstract_msg packet);
  84. int msgtype_is_request(unsigned short mtype);
  85. int msgtype_is_response(unsigned short mtype);
  86. // py::list std_vector_to_py_list(std::vector<comm_interval> intervals)
  87. py::list convert_to_py_repr(const std::vector<comm_interval>& intervals);
  88. void find_interval_helper(std::promise<std::vector<comm_interval> > && p, int number_ids, double max_int_time, int start_idx, int end_idx);
  89. /*
  90. * Attributes
  91. */
  92. std::vector<abstract_msg> messages;
  93. };
  94. #endif //BOTNET_COMM_PROCESSOR_H