networking.cpp 13 KB

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