networking.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <cstring>
  5. #include <cstdlib>
  6. #include <cstdio>
  7. #include "networking.hpp"
  8. /*********************************************
  9. **** ****
  10. **** "public" generic helper functions ****
  11. **** ****
  12. *********************************************/
  13. void initialize_prsona_classes()
  14. {
  15. Scalar::init();
  16. PrsonaBase::init();
  17. PrsonaBase::set_client_malicious();
  18. }
  19. char *set_temp_filename(
  20. std::default_random_engine& rng,
  21. struct mg_connection *conn)
  22. {
  23. std::string filename = random_string(rng, TMP_FILE_SIZE);
  24. char *c_filename = new char[TMP_FILE_SIZE+TMP_DIR_SIZE+1];
  25. strncpy(c_filename, TMP_DIR, TMP_DIR_SIZE);
  26. for (size_t i = 0; i < TMP_FILE_SIZE; i++)
  27. c_filename[i + TMP_DIR_SIZE] = filename[i];
  28. c_filename[TMP_DIR_SIZE + TMP_FILE_SIZE] = 0;
  29. if (conn)
  30. mg_set_user_connection_data(conn, c_filename);
  31. return c_filename;
  32. }
  33. void load_multiple_instances_config(
  34. std::vector<std::string>& relevantIPs,
  35. std::vector<int>& relevantPorts,
  36. const char *filename)
  37. {
  38. relevantIPs.clear();
  39. relevantPorts.clear();
  40. char buffer[46], *helper;
  41. std::ifstream configFile(filename);
  42. while (!configFile.eof())
  43. {
  44. configFile.getline(buffer, 46);
  45. if (strlen(buffer) > 0)
  46. {
  47. helper = buffer;
  48. if (strchr(helper, ':')) // File specifies a port
  49. {
  50. helper = strtok(helper, ":");
  51. relevantIPs.push_back(std::string(helper));
  52. helper = strtok(NULL, ":");
  53. relevantPorts.push_back(atoi(helper));
  54. }
  55. else // We use a default port
  56. {
  57. relevantIPs.push_back(std::string(helper));
  58. relevantPorts.push_back(atoi(DEFAULT_PRSONA_PORT_STR));
  59. }
  60. }
  61. }
  62. }
  63. void load_single_instance_config(
  64. std::string& relevantIP,
  65. std::string& relevantPortStr,
  66. int& relevantPort,
  67. const char *filename)
  68. {
  69. char buffer[46], *helper;
  70. std::ifstream configFile(filename);
  71. while (!configFile.eof())
  72. {
  73. configFile.getline(buffer, 46);
  74. if (strlen(buffer) > 0)
  75. {
  76. helper = buffer;
  77. if (strchr(helper, ':')) // File specifies a port
  78. {
  79. helper = strtok(helper, ":");
  80. relevantIP = helper;
  81. helper = strtok(NULL, ":");
  82. relevantPortStr = helper;
  83. relevantPort = stoi(relevantPortStr);
  84. }
  85. else // We use default port
  86. {
  87. relevantIP = helper;
  88. relevantPortStr = DEFAULT_PRSONA_PORT_STR;
  89. relevantPort = stoi(relevantPortStr);
  90. }
  91. }
  92. }
  93. }
  94. /***********************************************************
  95. **** ****
  96. **** "private" functions to help the generic helpers ****
  97. **** ****
  98. ***********************************************************/
  99. std::string random_string(
  100. std::default_random_engine& rng,
  101. size_t length)
  102. {
  103. const char charset[] =
  104. "0123456789_-"
  105. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  106. "abcdefghijklmnopqrstuvwxyz";
  107. const size_t max_index = (sizeof(charset) - 1);
  108. std::uniform_int_distribution<int> dist(0, max_index - 1);
  109. auto randchar = [&]() -> char
  110. {
  111. return charset[ dist(rng) ];
  112. };
  113. std::string retval(length, 0);
  114. std::generate_n(retval.begin(), length, randchar);
  115. return retval;
  116. }
  117. /***************************************
  118. **** ****
  119. **** websocket handler functions ****
  120. **** ****
  121. ***************************************/
  122. /*
  123. * NULL
  124. */
  125. int empty_websocket_data_handler(
  126. struct mg_connection *conn,
  127. int bits,
  128. char *data,
  129. size_t data_len,
  130. void *user_data)
  131. {
  132. return false;
  133. }
  134. void empty_websocket_close_handler(
  135. const struct mg_connection *conn,
  136. void *user_data)
  137. { /* */ }
  138. /*
  139. * SYNCHRONIZATION
  140. */
  141. int synchro_websocket_data_handler(
  142. struct mg_connection *conn,
  143. int bits,
  144. char *data,
  145. size_t data_len,
  146. void *user_data)
  147. {
  148. struct synchronization_tool *sync = (struct synchronization_tool *) user_data;
  149. std::unique_lock<std::mutex> lck(sync->mtx, std::defer_lock);
  150. switch (bits & 0xf)
  151. {
  152. case MG_WEBSOCKET_OPCODE_CONNECTION_CLOSE:
  153. break;
  154. // Responder has indicated receipt of submitted data
  155. case MG_WEBSOCKET_OPCODE_DATACOMPLETE:
  156. lck.lock();
  157. sync->val++;
  158. break;
  159. // Something strange has happened
  160. default:
  161. std::cerr << "Unknown packet type received. Failing." << std::endl;
  162. break;
  163. }
  164. return false;
  165. }
  166. void synchro_websocket_close_handler(
  167. const struct mg_connection *conn,
  168. void *user_data)
  169. {
  170. struct synchronization_tool *synch = (struct synchronization_tool *) user_data;
  171. std::unique_lock<std::mutex> lck(synch->mtx);
  172. synch->val2 = 1;
  173. synch->cv.notify_all();
  174. }
  175. /*
  176. * RECEIVE SERIALIZED DATA
  177. */
  178. int file_websocket_data_handler(
  179. struct mg_connection *conn,
  180. int bits,
  181. char *data,
  182. size_t data_len,
  183. void *user_data)
  184. {
  185. struct synchronization_tool *sync = (struct synchronization_tool *) user_data;
  186. char *filename = (char *) mg_get_user_connection_data(conn);
  187. FILE *currFile = NULL;
  188. std::unique_lock<std::mutex> lck(sync->mtx, std::defer_lock);
  189. switch (bits & 0xf)
  190. {
  191. // Responder has indicated they have sent all relevant data
  192. case MG_WEBSOCKET_OPCODE_DATACOMPLETE:
  193. case MG_WEBSOCKET_OPCODE_CONNECTION_CLOSE:
  194. break;
  195. // Responder has sent more data (which may theoretically be broken up into multiple packets)
  196. case MG_WEBSOCKET_OPCODE_BINARY:
  197. case MG_WEBSOCKET_OPCODE_CONTINUATION:
  198. lck.lock();
  199. currFile = fopen(filename, "ab");
  200. fwrite(data, sizeof(char), data_len, currFile);
  201. fclose(currFile);
  202. return true;
  203. // Something strange has happened
  204. default:
  205. std::cerr << "Unknown packet type received. Failing." << std::endl;
  206. break;
  207. }
  208. return false;
  209. }
  210. void file_websocket_close_handler(
  211. const struct mg_connection *conn,
  212. void *user_data)
  213. {
  214. struct synchronization_tool *sync = (struct synchronization_tool *) user_data;
  215. std::unique_lock<std::mutex> lck(sync->mtx);
  216. sync->val = 1;
  217. sync->val2 = 1;
  218. sync->cv.notify_all();
  219. }
  220. /*
  221. * SYNCHRONIZATION AND RECEIVE SERIALIZED DATA
  222. */
  223. int epoch_websocket_data_handler(
  224. struct mg_connection *conn,
  225. int bits,
  226. char *data,
  227. size_t data_len,
  228. void *user_data)
  229. {
  230. struct synchronization_tool *sync = (struct synchronization_tool *) user_data;
  231. char *filename = (char *) mg_get_user_connection_data(conn);
  232. FILE *currFile = NULL;
  233. std::unique_lock<std::mutex> lck(sync->mtx, std::defer_lock);
  234. switch (bits & 0xf)
  235. {
  236. case MG_WEBSOCKET_OPCODE_CONNECTION_CLOSE:
  237. break;
  238. // Responder has indicated they have sent all relevant data
  239. case MG_WEBSOCKET_OPCODE_DATACOMPLETE:
  240. lck.lock();
  241. sync->val++;
  242. break;
  243. // Responder has sent more data (which may theoretically be broken up into multiple packets)
  244. case MG_WEBSOCKET_OPCODE_BINARY:
  245. case MG_WEBSOCKET_OPCODE_CONTINUATION:
  246. lck.lock();
  247. currFile = fopen(filename, "ab");
  248. fwrite(data, sizeof(char), data_len, currFile);
  249. fclose(currFile);
  250. return true;
  251. // Something strange has happened
  252. default:
  253. std::cerr << "Unknown packet type received. Failing." << std::endl;
  254. break;
  255. }
  256. return false;
  257. }
  258. void epoch_websocket_close_handler(
  259. const struct mg_connection *conn,
  260. void *user_data)
  261. {
  262. struct synchronization_tool *sync = (struct synchronization_tool *) user_data;
  263. std::unique_lock<std::mutex> lck(sync->mtx);
  264. sync->val2 = 1;
  265. sync->cv.notify_all();
  266. }
  267. /*
  268. * SPECIAL FOR HANDLING UNUSUAL DATA
  269. */
  270. int clients_websocket_data_handler(
  271. struct mg_connection *conn,
  272. int bits,
  273. char *data,
  274. size_t data_len,
  275. void *user_data)
  276. {
  277. struct synchronization_tool *sync = (struct synchronization_tool *) user_data;
  278. std::unique_lock<std::mutex> lck(sync->mtx, std::defer_lock);
  279. switch (bits & 0xf)
  280. {
  281. // Responder has indicated they have sent all relevant data
  282. case MG_WEBSOCKET_OPCODE_DATACOMPLETE:
  283. case MG_WEBSOCKET_OPCODE_CONNECTION_CLOSE:
  284. break;
  285. // Responder has sent data
  286. case MG_WEBSOCKET_OPCODE_BINARY:
  287. lck.lock();
  288. if (data_len == sizeof(sync->val))
  289. sync->val = *((size_t *) data);
  290. break;
  291. // Something strange has happened
  292. default:
  293. std::cerr << "Unknown packet type received. Failing." << std::endl;
  294. break;
  295. }
  296. return false;
  297. }
  298. /********************************************
  299. **** ****
  300. **** Generic handler member functions ****
  301. **** ****
  302. ********************************************/
  303. /*
  304. * EXIT SYNCHRONIZATION HANDLER
  305. */
  306. RemoteControlHandler::RemoteControlHandler(
  307. struct synchronization_tool *sync)
  308. : sync(sync)
  309. { /* */ }
  310. RemoteControlHandler::RemoteControlHandler(
  311. struct synchronization_tool *sync,
  312. const std::string& message)
  313. : sync(sync), message(message)
  314. { /* */ }
  315. bool RemoteControlHandler::handleGet(
  316. CivetServer *server,
  317. struct mg_connection *conn)
  318. {
  319. std::unique_lock<std::mutex> lck(sync->mtx);
  320. mg_printf(conn, "HTTP/1.1 200 OK\r\n"
  321. "Content-Type: text/plain\r\n"
  322. "Connection: close\r\n\r\n");
  323. if (message.empty())
  324. mg_printf(conn, "Event triggered.\n");
  325. else
  326. mg_printf(conn, "%s\n", message.c_str());
  327. sync->val++;
  328. sync->cv.notify_all();
  329. return true;
  330. }
  331. /*
  332. * EXPERIMENT EVENT SYNCHRONIZATION HANDLER
  333. */
  334. AltRemoteControlHandler::AltRemoteControlHandler(
  335. size_t value,
  336. struct synchronization_tool *sync)
  337. : value(value), sync(sync)
  338. { /* */ }
  339. AltRemoteControlHandler::AltRemoteControlHandler(
  340. size_t value,
  341. struct synchronization_tool *sync,
  342. const std::string& message)
  343. : value(value), sync(sync), message(message)
  344. { /* */ }
  345. bool AltRemoteControlHandler::handleGet(
  346. CivetServer *server,
  347. struct mg_connection *conn)
  348. {
  349. std::unique_lock<std::mutex> lck(sync->mtx);
  350. const struct mg_request_info *info = mg_get_request_info(conn);
  351. if (info->query_string)
  352. query = info->query_string;
  353. mg_printf(conn, "HTTP/1.1 200 OK\r\n"
  354. "Content-Type: text/plain\r\n"
  355. "Connection: close\r\n\r\n");
  356. if (message.empty())
  357. mg_printf(conn, "Event triggered.\n");
  358. else
  359. mg_printf(conn, "%s\n", message.c_str());
  360. sync->val2 = value;
  361. sync->cv.notify_all();
  362. return true;
  363. }
  364. std::string AltRemoteControlHandler::getQuery() const
  365. {
  366. return query;
  367. }