networking.cpp 14 KB

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