networking.cpp 14 KB

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