CovertProtocol.hpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. #ifndef COVERTPROTOCOL_H
  2. #define COVERTPROTOCOL_H
  3. #include <fstream>
  4. #include <iostream>
  5. #include <string>
  6. #include <type_traits>
  7. #include "../../include/Config.h"
  8. /**
  9. * @class CovertProtocol
  10. *
  11. * An unidirectional Covert Channel protocol with a variable size template.
  12. *
  13. * The protocol works unidirectional so the active side uses send to encode data to be sent and the passive side uses receive to extract data.
  14. *
  15. * @param N number of bytes which can be used to transmit data
  16. * @param PASSIVE passive mode
  17. */
  18. template <int N, bool PASSIVE> class CovertProtocol {
  19. static_assert(N >= 1);
  20. public:
  21. /**
  22. * CovertProtocol constructor
  23. */
  24. CovertProtocol() : fileDirectory(Config::getValue("filedirectory")) {}
  25. /**
  26. * CovertProtocol destructor
  27. *
  28. * Closes the file
  29. */
  30. ~CovertProtocol() { file.close(); }
  31. /**
  32. * Starts sending a file.
  33. *
  34. * Starts sending a file if no transmission is running and the file exists.
  35. *
  36. * @param fileName name of the file in the file directory
  37. * @return true - file will be sent | false - file was not accepted
  38. */
  39. bool sendFile(const std::string &fileName) {
  40. static_assert(!PASSIVE);
  41. if (state != ProtocolState::idle || file.is_open()) {
  42. return false;
  43. }
  44. file.open(fileDirectory + fileName, std::ios::in);
  45. if (file.is_open()) {
  46. file.close();
  47. this->fileName = fileName;
  48. state = ProtocolState::fileNameSize;
  49. std::cout << "starting sending file \"" << fileName << "\"" << std::endl;
  50. return true;
  51. } else {
  52. return false;
  53. }
  54. }
  55. /**
  56. * send encodes the data into the data array
  57. *
  58. * 1. Send file name size (length)
  59. * 2. Send file name
  60. * 3. Send data size (length)
  61. * 4. Send data
  62. *
  63. * @param data must be an array of size N
  64. */
  65. void send(uint8_t *const data) {
  66. static_assert(!PASSIVE);
  67. switch (state) {
  68. case ProtocolState::idle:
  69. data[0] = 0;
  70. return;
  71. case ProtocolState::fileNameSize:
  72. fileNameSize = fileName.size();
  73. data[0] = fileNameSize;
  74. fileNamePosition = 0;
  75. state = ProtocolState::fileName;
  76. break;
  77. case ProtocolState::fileName:
  78. if (fileNameSize - fileNamePosition >= N) {
  79. for (int i = 0; i < N; i++) {
  80. data[i] = fileName.at(fileNamePosition++);
  81. }
  82. } else {
  83. int diff = fileNameSize - fileNamePosition;
  84. for (int i = 0; i < diff; i++) {
  85. data[i] = fileName.at(fileNamePosition++);
  86. }
  87. }
  88. if (fileNamePosition == fileNameSize) {
  89. file.open(fileDirectory + fileName, std::ios::in | std::ios::binary | std::ios::ate);
  90. if (!file.is_open()) {
  91. file.close();
  92. std::cerr << "File \"" << fileName << "\" does exists. Error state!!!" << std::endl;
  93. state = ProtocolState::error;
  94. return;
  95. }
  96. dataSize = file.tellg();
  97. dataPosition = 0;
  98. file.seekg(std::ios::beg);
  99. state = ProtocolState::dataSize;
  100. }
  101. break;
  102. case ProtocolState::dataSize:
  103. if constexpr (N >= 4) {
  104. for (; dataPosition < 4; dataPosition++) {
  105. data[dataPosition] = dataSize >> ((3 - dataPosition) * 8);
  106. }
  107. } else {
  108. uint32_t oldDataPosition = dataPosition;
  109. uint32_t limit = dataPosition + N;
  110. if (limit > 4) {
  111. limit = 4;
  112. }
  113. for (; dataPosition < limit; dataPosition++) {
  114. data[dataPosition - oldDataPosition] = dataSize >> ((3 - dataPosition) * 8);
  115. }
  116. }
  117. if (dataPosition == 4) {
  118. dataPosition = 0;
  119. state = ProtocolState::data;
  120. }
  121. break;
  122. case ProtocolState::data:
  123. if (dataSize - dataPosition >= N) {
  124. file.read((char *)data, N);
  125. dataPosition += N;
  126. } else {
  127. file.read((char *)data, dataSize - dataPosition);
  128. dataPosition = dataSize;
  129. }
  130. std::cout << "sent " << dataPosition << "/" << dataSize << std::endl;
  131. if (dataPosition == dataSize) {
  132. file.close();
  133. state = ProtocolState::idle;
  134. std::cout << "finished sending file \"" << fileName << "\"" << std::endl;
  135. }
  136. break;
  137. case ProtocolState::error:
  138. break;
  139. }
  140. return;
  141. }
  142. /**
  143. * receive extracts data from the data array
  144. *
  145. * 1. Receive file name size (length)
  146. * 2. Receive file name
  147. * 3. Receive data size (length)
  148. * 4. Receive data
  149. *
  150. * @param data must be an array of size N
  151. */
  152. void receive(const uint8_t *const data) {
  153. static_assert(PASSIVE);
  154. switch (state) {
  155. case ProtocolState::idle:
  156. if (data[0] == 0) {
  157. return;
  158. }
  159. // no break because the first data received is the filename size
  160. case ProtocolState::fileNameSize:
  161. std::cout << "incoming file transmission" << std::endl;
  162. fileNameSize = data[0];
  163. fileNamePosition = 0;
  164. fileName = "";
  165. state = ProtocolState::fileName;
  166. break;
  167. case ProtocolState::fileName:
  168. if (fileNameSize - fileNamePosition >= N) {
  169. uint8_t oldFileNamePosition = fileNamePosition;
  170. for (; fileNamePosition < oldFileNamePosition + N; fileNamePosition++) {
  171. fileName += data[fileNamePosition - oldFileNamePosition];
  172. }
  173. } else {
  174. uint8_t oldFileNamePosition = fileNamePosition;
  175. for (; fileNamePosition < fileNameSize; fileNamePosition++) {
  176. fileName += data[fileNamePosition - oldFileNamePosition];
  177. }
  178. }
  179. if (fileNamePosition == fileNameSize) {
  180. file.open(fileDirectory + fileName, std::ios::in);
  181. if (file.is_open()) {
  182. file.close();
  183. std::cerr << "File \"" << fileName << "\" already exists. Error state!!!" << std::endl;
  184. state = ProtocolState::error;
  185. return;
  186. }
  187. file.close();
  188. file.open(fileDirectory + fileName, std::ios::out | std::ios::binary | std::ios::app);
  189. if (!file.is_open()) {
  190. std::cerr << "File \"" << fileName << "\" could not be opened! Error state!!!" << std::endl;
  191. state = ProtocolState::error;
  192. return;
  193. }
  194. dataSize = 0;
  195. dataPosition = 0;
  196. state = ProtocolState::dataSize;
  197. std::cout << "starting receiving file \"" << fileName << "\"" << std::endl;
  198. }
  199. break;
  200. case ProtocolState::dataSize:
  201. if constexpr (N >= 4) {
  202. for (; dataPosition < 4; dataPosition++) {
  203. dataSize = dataSize | data[dataPosition] << ((3 - dataPosition) * 8);
  204. }
  205. } else {
  206. uint32_t oldDataPosition = dataPosition;
  207. uint32_t limit = dataPosition + N;
  208. if (limit > 4) {
  209. limit = 4;
  210. }
  211. for (; dataPosition < limit; dataPosition++) {
  212. dataSize = dataSize | data[dataPosition - oldDataPosition] << ((3 - dataPosition) * 8);
  213. }
  214. }
  215. if (dataPosition == 4) {
  216. dataPosition = 0;
  217. state = ProtocolState::data;
  218. }
  219. break;
  220. case ProtocolState::data:
  221. if (dataSize - dataPosition >= N) {
  222. file.write((char *)data, N);
  223. dataPosition += N;
  224. } else {
  225. file.write((char *)data, dataSize - dataPosition);
  226. dataPosition = dataSize;
  227. }
  228. std::cout << "received " << dataPosition << "/" << dataSize << std::endl;
  229. if (dataPosition == dataSize) {
  230. file.close();
  231. state = ProtocolState::idle;
  232. std::cout << "finished receiving file \"" << fileName << "\"" << std::endl;
  233. }
  234. break;
  235. case ProtocolState::error:
  236. //
  237. break;
  238. }
  239. }
  240. private:
  241. /**
  242. * folder of the files
  243. */
  244. const std::string fileDirectory;
  245. /**
  246. * States of the data transmission
  247. *
  248. * @warning error state cannot be recovered!!!
  249. */
  250. enum struct ProtocolState { idle, fileNameSize, fileName, dataSize, data, error };
  251. /**
  252. * State of the data transmission
  253. */
  254. ProtocolState state = ProtocolState::idle;
  255. /**
  256. * size of the file to be sent/received
  257. */
  258. uint32_t dataSize;
  259. /**
  260. * position in the file to be sent/received
  261. */
  262. uint32_t dataPosition;
  263. /**
  264. * file to be sent/received
  265. */
  266. std::fstream file;
  267. /**
  268. * size of the file name
  269. */
  270. uint8_t fileNameSize;
  271. /**
  272. * position of the file name so far sent/received
  273. */
  274. uint8_t fileNamePosition;
  275. /**
  276. * name of the file to be sent/received
  277. */
  278. std::string fileName;
  279. };
  280. #endif