CovertProtocolBidirectional.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. /**
  77. * Get the progress
  78. *
  79. * @return progress counters
  80. */
  81. virtual std::pair<uint32_t, uint32_t> getProgress() { return protocol.getProgress(); }
  82. /**
  83. * Test if a transfer is running
  84. *
  85. * @return true - a transfer runs | false - no transfer runs
  86. */
  87. virtual bool isTransferRunning() { return protocol.isTransferRunning(); }
  88. private:
  89. /**
  90. * current segment counter
  91. */
  92. uint8_t segment;
  93. /**
  94. * last acknowledged segment counter
  95. */
  96. uint8_t lastACKedSegment = 0;
  97. /**
  98. * lastData sent
  99. */
  100. uint8_t *lastData;
  101. /**
  102. * Simple protocol
  103. */
  104. CovertProtocol<N - 1, PASSIVE> protocol;
  105. /**
  106. * Evaluates received segment number and increases own segment number if necessary
  107. *
  108. * @param seg received segment number
  109. * @return true if segment should discarded
  110. */
  111. inline bool receiveSegment(uint8_t seg) {
  112. if constexpr (PASSIVE) {
  113. if (seg == segment && seg == increaseSegment(lastACKedSegment, 1)) {
  114. lastACKedSegment = seg;
  115. return false;
  116. } else {
  117. std::cerr << "Throwing this packet away" << std::endl;
  118. return true;
  119. }
  120. } else {
  121. if (seg == increaseSegment(segment, 1) && seg == increaseSegment(lastACKedSegment, 2)) {
  122. lastACKedSegment = increaseSegment(lastACKedSegment, 1);
  123. return false;
  124. } else {
  125. std::cerr << "Throwing this packet away" << std::endl;
  126. return true;
  127. }
  128. }
  129. }
  130. /**
  131. * Returns true if the segment should be repeated.
  132. *
  133. * @return true if segment should be repeated
  134. */
  135. inline bool sendSegment() {
  136. if (segment == lastACKedSegment) {
  137. segment = increaseSegment(segment, 1);
  138. return false;
  139. } else {
  140. return true;
  141. }
  142. }
  143. /**
  144. * Calculates seg + inc
  145. * @return seg + inc
  146. */
  147. inline uint8_t increaseSegment(uint8_t seg, uint8_t inc) { return seg + inc & 0x3; }
  148. };
  149. #endif