networking.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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. std::vector<size_t> get_log_data(
  95. const struct mg_context *ctx)
  96. {
  97. std::vector<size_t> retval;
  98. char buffer[4096];
  99. mg_get_context_info(ctx, buffer, 4096);
  100. retval.push_back(parse_log_for_data(buffer, "read"));
  101. retval.push_back(parse_log_for_data(buffer, "written"));
  102. return retval;
  103. }
  104. void write_log_data(
  105. std::mutex& outputMtx,
  106. const std::string& outputFilename,
  107. const std::vector<double>& timingData,
  108. const std::vector<size_t>& bandwidthData)
  109. {
  110. std::unique_lock<std::mutex> lck(outputMtx);
  111. FILE *outputFile = fopen(outputFilename.c_str(), "a");
  112. fprintf(outputFile, "%f,%f,%zu,%zu\n", timingData[0], timingData[1], bandwidthData[0], bandwidthData[1]);
  113. fclose(outputFile);
  114. }
  115. /***********************************************************
  116. **** ****
  117. **** "private" functions to help the generic helpers ****
  118. **** ****
  119. ***********************************************************/
  120. std::string random_string(
  121. std::default_random_engine& rng,
  122. size_t length)
  123. {
  124. const char charset[] =
  125. "0123456789_-"
  126. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  127. "abcdefghijklmnopqrstuvwxyz";
  128. const size_t max_index = (sizeof(charset) - 1);
  129. std::uniform_int_distribution<int> dist(0, max_index - 1);
  130. auto randchar = [&]() -> char
  131. {
  132. return charset[ dist(rng) ];
  133. };
  134. std::string retval(length, 0);
  135. std::generate_n(retval.begin(), length, randchar);
  136. return retval;
  137. }
  138. size_t parse_log_for_data(const char *input, const char *key)
  139. {
  140. size_t length = strlen(input);
  141. char *copy = new char[length + 1];
  142. strncpy(copy, input, length);
  143. copy[length] = 0;
  144. char *pos = strstr(copy, key) + strlen(key);
  145. pos = strtok(pos, "{}:,\" \n");
  146. size_t retval = strtoul(pos, NULL, 10);
  147. delete [] copy;
  148. return retval;
  149. }
  150. /***************************************
  151. **** ****
  152. **** websocket handler functions ****
  153. **** ****
  154. ***************************************/
  155. /*
  156. * NULL
  157. */
  158. int empty_websocket_data_handler(
  159. struct mg_connection *conn,
  160. int bits,
  161. char *data,
  162. size_t data_len,
  163. void *user_data)
  164. {
  165. return false;
  166. }
  167. void empty_websocket_close_handler(
  168. const struct mg_connection *conn,
  169. void *user_data)
  170. { /* */ }
  171. /*
  172. * SYNCHRONIZATION
  173. */
  174. int synchro_websocket_data_handler(
  175. struct mg_connection *conn,
  176. int bits,
  177. char *data,
  178. size_t data_len,
  179. void *user_data)
  180. {
  181. struct synchronization_tool *sync = (struct synchronization_tool *) user_data;
  182. std::unique_lock<std::mutex> lck(sync->mtx, std::defer_lock);
  183. switch (bits & 0xf)
  184. {
  185. case MG_WEBSOCKET_OPCODE_CONNECTION_CLOSE:
  186. break;
  187. // Responder has indicated receipt of submitted data
  188. case MG_WEBSOCKET_OPCODE_DATACOMPLETE:
  189. lck.lock();
  190. sync->val++;
  191. break;
  192. // Something strange has happened
  193. default:
  194. std::cerr << "Unknown packet type received. Failing." << std::endl;
  195. break;
  196. }
  197. return false;
  198. }
  199. void synchro_websocket_close_handler(
  200. const struct mg_connection *conn,
  201. void *user_data)
  202. {
  203. struct synchronization_tool *synch = (struct synchronization_tool *) user_data;
  204. std::unique_lock<std::mutex> lck(synch->mtx);
  205. synch->val2 = 1;
  206. synch->cv.notify_all();
  207. }
  208. /*
  209. * RECEIVE SERIALIZED DATA
  210. */
  211. int file_websocket_data_handler(
  212. struct mg_connection *conn,
  213. int bits,
  214. char *data,
  215. size_t data_len,
  216. void *user_data)
  217. {
  218. struct synchronization_tool *sync = (struct synchronization_tool *) user_data;
  219. char *filename = (char *) mg_get_user_connection_data(conn);
  220. FILE *currFile = NULL;
  221. std::unique_lock<std::mutex> lck(sync->mtx, std::defer_lock);
  222. switch (bits & 0xf)
  223. {
  224. // Responder has indicated they have sent all relevant data
  225. case MG_WEBSOCKET_OPCODE_DATACOMPLETE:
  226. case MG_WEBSOCKET_OPCODE_CONNECTION_CLOSE:
  227. break;
  228. // Responder has sent more data (which may theoretically be broken up into multiple packets)
  229. case MG_WEBSOCKET_OPCODE_BINARY:
  230. case MG_WEBSOCKET_OPCODE_CONTINUATION:
  231. lck.lock();
  232. currFile = fopen(filename, "ab");
  233. fwrite(data, sizeof(char), data_len, currFile);
  234. fclose(currFile);
  235. return true;
  236. // Something strange has happened
  237. default:
  238. std::cerr << "Unknown packet type received. Failing." << std::endl;
  239. break;
  240. }
  241. return false;
  242. }
  243. void file_websocket_close_handler(
  244. const struct mg_connection *conn,
  245. void *user_data)
  246. {
  247. struct synchronization_tool *sync = (struct synchronization_tool *) user_data;
  248. std::unique_lock<std::mutex> lck(sync->mtx);
  249. sync->val = 1;
  250. sync->val2 = 1;
  251. sync->cv.notify_all();
  252. }
  253. /*
  254. * SYNCHRONIZATION AND RECEIVE SERIALIZED DATA
  255. */
  256. int epoch_websocket_data_handler(
  257. struct mg_connection *conn,
  258. int bits,
  259. char *data,
  260. size_t data_len,
  261. void *user_data)
  262. {
  263. struct synchronization_tool *sync = (struct synchronization_tool *) user_data;
  264. char *filename = (char *) mg_get_user_connection_data(conn);
  265. FILE *currFile = NULL;
  266. std::unique_lock<std::mutex> lck(sync->mtx, std::defer_lock);
  267. switch (bits & 0xf)
  268. {
  269. case MG_WEBSOCKET_OPCODE_CONNECTION_CLOSE:
  270. break;
  271. // Responder has indicated they have sent all relevant data
  272. case MG_WEBSOCKET_OPCODE_DATACOMPLETE:
  273. lck.lock();
  274. sync->val++;
  275. break;
  276. // Responder has sent more data (which may theoretically be broken up into multiple packets)
  277. case MG_WEBSOCKET_OPCODE_BINARY:
  278. case MG_WEBSOCKET_OPCODE_CONTINUATION:
  279. lck.lock();
  280. currFile = fopen(filename, "ab");
  281. fwrite(data, sizeof(char), data_len, currFile);
  282. fclose(currFile);
  283. return true;
  284. // Something strange has happened
  285. default:
  286. std::cerr << "Unknown packet type received. Failing." << std::endl;
  287. break;
  288. }
  289. return false;
  290. }
  291. void epoch_websocket_close_handler(
  292. const struct mg_connection *conn,
  293. void *user_data)
  294. {
  295. struct synchronization_tool *sync = (struct synchronization_tool *) user_data;
  296. std::unique_lock<std::mutex> lck(sync->mtx);
  297. sync->val2 = 1;
  298. sync->cv.notify_all();
  299. }
  300. /*
  301. * SPECIAL FOR HANDLING UNUSUAL DATA
  302. */
  303. int clients_websocket_data_handler(
  304. struct mg_connection *conn,
  305. int bits,
  306. char *data,
  307. size_t data_len,
  308. void *user_data)
  309. {
  310. struct synchronization_tool *sync = (struct synchronization_tool *) user_data;
  311. std::unique_lock<std::mutex> lck(sync->mtx, std::defer_lock);
  312. switch (bits & 0xf)
  313. {
  314. // Responder has indicated they have sent all relevant data
  315. case MG_WEBSOCKET_OPCODE_DATACOMPLETE:
  316. case MG_WEBSOCKET_OPCODE_CONNECTION_CLOSE:
  317. break;
  318. // Responder has sent data
  319. case MG_WEBSOCKET_OPCODE_BINARY:
  320. lck.lock();
  321. if (data_len == sizeof(sync->val))
  322. sync->val = *((size_t *) data);
  323. break;
  324. // Something strange has happened
  325. default:
  326. std::cerr << "Unknown packet type received. Failing." << std::endl;
  327. break;
  328. }
  329. return false;
  330. }
  331. /********************************************
  332. **** ****
  333. **** Generic handler member functions ****
  334. **** ****
  335. ********************************************/
  336. /*
  337. * EXIT SYNCHRONIZATION HANDLER
  338. */
  339. RemoteControlHandler::RemoteControlHandler(
  340. struct synchronization_tool *sync)
  341. : sync(sync)
  342. { /* */ }
  343. RemoteControlHandler::RemoteControlHandler(
  344. struct synchronization_tool *sync,
  345. const std::string& message)
  346. : sync(sync), message(message)
  347. { /* */ }
  348. bool RemoteControlHandler::handleGet(
  349. CivetServer *server,
  350. struct mg_connection *conn)
  351. {
  352. std::unique_lock<std::mutex> lck(sync->mtx);
  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->val++;
  361. sync->cv.notify_all();
  362. return true;
  363. }
  364. /*
  365. * EXPERIMENT EVENT SYNCHRONIZATION HANDLER
  366. */
  367. AltRemoteControlHandler::AltRemoteControlHandler(
  368. size_t value,
  369. struct synchronization_tool *sync)
  370. : value(value), sync(sync)
  371. { /* */ }
  372. AltRemoteControlHandler::AltRemoteControlHandler(
  373. size_t value,
  374. struct synchronization_tool *sync,
  375. const std::string& message)
  376. : value(value), sync(sync), message(message)
  377. { /* */ }
  378. bool AltRemoteControlHandler::handleGet(
  379. CivetServer *server,
  380. struct mg_connection *conn)
  381. {
  382. std::unique_lock<std::mutex> lck(sync->mtx);
  383. const struct mg_request_info *info = mg_get_request_info(conn);
  384. if (info->query_string)
  385. query = info->query_string;
  386. mg_printf(conn, "HTTP/1.1 200 OK\r\n"
  387. "Content-Type: text/plain\r\n"
  388. "Connection: close\r\n\r\n");
  389. if (message.empty())
  390. mg_printf(conn, "Event triggered.\n");
  391. else
  392. mg_printf(conn, "%s\n", message.c_str());
  393. sync->val2 = value;
  394. sync->cv.notify_all();
  395. return true;
  396. }
  397. std::string AltRemoteControlHandler::getQuery() const
  398. {
  399. return query;
  400. }