CovertProtocol.hpp 8.1 KB

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