CovertProtocolBidirectional.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #ifndef COVERTPROTOCOLBIDIRECTIONAL_H
  2. #define COVERTPROTOCOLBIDIRECTIONAL_H
  3. #include <fstream>
  4. #include <iostream>
  5. #include <string>
  6. #include <type_traits>
  7. #include "CovertProtocol.hpp"
  8. /**
  9. * @class CovertProtocolBidirectional
  10. *
  11. * A bidirectional Covert Channel protocol with a variable size template.
  12. *
  13. * The protocol works bidirectional so the active side uses send to encode data to be sent and the passive side uses receive to extract data. And the passive
  14. * side uses send to send ACKs and and the active side uses receive to receive ACKs.
  15. *
  16. * @param N number of bytes which can be used to transmit data
  17. * @param PASSIVE passive mode
  18. */
  19. template <int N, bool PASSIVE> class CovertProtocolBidirectional {
  20. static_assert(N >= 2);
  21. public:
  22. /**
  23. * CovertProtocolBidirectional constructor
  24. */
  25. CovertProtocolBidirectional() : segment(PASSIVE ? 1 : 0) { lastData = new uint8_t[N](); }
  26. /**
  27. * CovertProtocol destructor
  28. */
  29. ~CovertProtocolBidirectional() { delete[](lastData); }
  30. /**
  31. * Starts sending a file.
  32. *
  33. * Starts sending a file if no transmission is running and the file exists.
  34. *
  35. * @param fileName name of the file in the file directory
  36. * @return true - file will be sent | false - file was not accepted
  37. */
  38. bool sendFile(const std::string &fileName) {
  39. if constexpr (PASSIVE) {
  40. return false;
  41. }
  42. return protocol.sendFile(fileName);
  43. }
  44. /**
  45. * send encodes the data into the data array
  46. *
  47. * @param data must be an array of size N
  48. */
  49. void send(uint8_t *const data) {
  50. if (sendSegment()) {
  51. std::memcpy(data, lastData, N);
  52. return;
  53. }
  54. if constexpr (!PASSIVE) {
  55. protocol.send(data + 1);
  56. }
  57. data[0] = (segment << 6);
  58. // save data to lastData
  59. std::memcpy(lastData, data, N);
  60. }
  61. /**
  62. * receive extracts data from the data array
  63. *
  64. * @param data must be an array of size N
  65. */
  66. void receive(const uint8_t *const data) {
  67. uint8_t seg = data[0] >> 6;
  68. std::cerr << "segment: " << (int)segment << "\tack: " << (int)lastACKedSegment << "\tseg: " << (int)seg << std::endl;
  69. if (receiveSegment(seg)) {
  70. return;
  71. }
  72. if constexpr (PASSIVE) {
  73. protocol.receive(data + 1);
  74. }
  75. }
  76. private:
  77. /**
  78. * current segment counter
  79. */
  80. uint8_t segment;
  81. /**
  82. * last acknowledged segment counter
  83. */
  84. uint8_t lastACKedSegment = 0;
  85. /**
  86. * lastData sent
  87. */
  88. uint8_t *lastData;
  89. /**
  90. * Simple protocol
  91. */
  92. CovertProtocol<N - 1, PASSIVE> protocol;
  93. /**
  94. * Evaluates received segment number and increases own segment number if necessary
  95. *
  96. * @param seg received segment number
  97. * @return true if segment should discarded
  98. */
  99. inline bool receiveSegment(uint8_t seg) {
  100. if constexpr (PASSIVE) {
  101. if (seg == segment && seg == increaseSegment(lastACKedSegment, 1)) {
  102. lastACKedSegment = seg;
  103. return false;
  104. } else {
  105. std::cerr << "Throwing this packet away" << std::endl;
  106. return true;
  107. }
  108. } else {
  109. if (seg == increaseSegment(segment, 1) && seg == increaseSegment(lastACKedSegment, 2)) {
  110. lastACKedSegment = increaseSegment(lastACKedSegment, 1);
  111. return false;
  112. } else {
  113. std::cerr << "Throwing this packet away" << std::endl;
  114. return true;
  115. }
  116. }
  117. }
  118. /**
  119. * Returns true if the segment should be repeated.
  120. *
  121. * @return true if segment should be repeated
  122. */
  123. inline bool sendSegment() {
  124. if (segment == lastACKedSegment) {
  125. segment = increaseSegment(segment, 1);
  126. return false;
  127. } else {
  128. return true;
  129. }
  130. }
  131. /**
  132. * Calculates seg + inc
  133. * @return seg + inc
  134. */
  135. inline uint8_t increaseSegment(uint8_t seg, uint8_t inc) { return seg + inc & 0x3; }
  136. };
  137. #endif