123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444 |
- #include <algorithm>
- #include <iostream>
- #include <fstream>
- #include <cstring>
- #include <cstdlib>
- #include <cstdio>
- #include "networking.hpp"
- /*********************************************
- **** ****
- **** "public" generic helper functions ****
- **** ****
- *********************************************/
- void initialize_prsona_classes()
- {
- Scalar::init();
- PrsonaBase::init();
- PrsonaBase::set_client_malicious();
- }
- char *set_temp_filename(
- std::default_random_engine& rng,
- struct mg_connection *conn)
- {
- std::string filename = random_string(rng, TMP_FILE_SIZE);
-
- char *c_filename = new char[TMP_FILE_SIZE+TMP_DIR_SIZE+1];
- strncpy(c_filename, TMP_DIR, TMP_DIR_SIZE);
- for (size_t i = 0; i < TMP_FILE_SIZE; i++)
- c_filename[i + TMP_DIR_SIZE] = filename[i];
- c_filename[TMP_DIR_SIZE + TMP_FILE_SIZE] = 0;
- if (conn)
- mg_set_user_connection_data(conn, c_filename);
- return c_filename;
- }
- void load_multiple_instances_config(
- std::vector<std::string>& relevantIPs,
- std::vector<int>& relevantPorts,
- const char *filename)
- {
- relevantIPs.clear();
- relevantPorts.clear();
- char buffer[46], *helper;
- std::ifstream configFile(filename);
- while (!configFile.eof())
- {
- configFile.getline(buffer, 46);
- if (strlen(buffer) > 0)
- {
- helper = buffer;
- if (strchr(helper, ':')) // File specifies a port
- {
- helper = strtok(helper, ":");
- relevantIPs.push_back(std::string(helper));
- helper = strtok(NULL, ":");
- relevantPorts.push_back(atoi(helper));
- }
- else // We use a default port
- {
- relevantIPs.push_back(std::string(helper));
- relevantPorts.push_back(atoi(DEFAULT_PRSONA_PORT_STR));
- }
- }
- }
- }
- void load_single_instance_config(
- std::string& relevantIP,
- std::string& relevantPortStr,
- int& relevantPort,
- const char *filename)
- {
- char buffer[46], *helper;
- std::ifstream configFile(filename);
- while (!configFile.eof())
- {
- configFile.getline(buffer, 46);
- if (strlen(buffer) > 0)
- {
- helper = buffer;
- if (strchr(helper, ':')) // File specifies a port
- {
- helper = strtok(helper, ":");
- relevantIP = helper;
- helper = strtok(NULL, ":");
- relevantPortStr = helper;
- relevantPort = stoi(relevantPortStr);
- }
- else // We use default port
- {
- relevantIP = helper;
- relevantPortStr = DEFAULT_PRSONA_PORT_STR;
- relevantPort = stoi(relevantPortStr);
- }
- }
- }
- }
- /***********************************************************
- **** ****
- **** "private" functions to help the generic helpers ****
- **** ****
- ***********************************************************/
- std::string random_string(
- std::default_random_engine& rng,
- size_t length)
- {
- const char charset[] =
- "0123456789_-"
- "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
- "abcdefghijklmnopqrstuvwxyz";
- const size_t max_index = (sizeof(charset) - 1);
- std::uniform_int_distribution<int> dist(0, max_index - 1);
- auto randchar = [&]() -> char
- {
- return charset[ dist(rng) ];
- };
- std::string retval(length, 0);
- std::generate_n(retval.begin(), length, randchar);
- return retval;
- }
- /***************************************
- **** ****
- **** websocket handler functions ****
- **** ****
- ***************************************/
- /*
- * NULL
- */
- int empty_websocket_data_handler(
- struct mg_connection *conn,
- int bits,
- char *data,
- size_t data_len,
- void *user_data)
- {
- return false;
- }
- void empty_websocket_close_handler(
- const struct mg_connection *conn,
- void *user_data)
- { /* */ }
- /*
- * SYNCHRONIZATION
- */
- int synchro_websocket_data_handler(
- struct mg_connection *conn,
- int bits,
- char *data,
- size_t data_len,
- void *user_data)
- {
- struct synchronization_tool *sync = (struct synchronization_tool *) user_data;
- std::unique_lock<std::mutex> lck(sync->mtx, std::defer_lock);
- switch (bits & 0xf)
- {
- case MG_WEBSOCKET_OPCODE_CONNECTION_CLOSE:
- break;
- // Responder has indicated receipt of submitted data
- case MG_WEBSOCKET_OPCODE_DATACOMPLETE:
- lck.lock();
- sync->val++;
- break;
- // Something strange has happened
- default:
- std::cerr << "Unknown packet type received. Failing." << std::endl;
- break;
- }
- return false;
- }
- void synchro_websocket_close_handler(
- const struct mg_connection *conn,
- void *user_data)
- {
- struct synchronization_tool *synch = (struct synchronization_tool *) user_data;
- std::unique_lock<std::mutex> lck(synch->mtx);
- synch->val2 = 1;
- synch->cv.notify_all();
- }
- /*
- * RECEIVE SERIALIZED DATA
- */
- int file_websocket_data_handler(
- struct mg_connection *conn,
- int bits,
- char *data,
- size_t data_len,
- void *user_data)
- {
- struct synchronization_tool *sync = (struct synchronization_tool *) user_data;
- char *filename = (char *) mg_get_user_connection_data(conn);
- FILE *currFile = NULL;
- std::unique_lock<std::mutex> lck(sync->mtx, std::defer_lock);
- switch (bits & 0xf)
- {
- // Responder has indicated they have sent all relevant data
- case MG_WEBSOCKET_OPCODE_DATACOMPLETE:
- case MG_WEBSOCKET_OPCODE_CONNECTION_CLOSE:
- break;
- // Responder has sent more data (which may theoretically be broken up into multiple packets)
- case MG_WEBSOCKET_OPCODE_BINARY:
- case MG_WEBSOCKET_OPCODE_CONTINUATION:
- lck.lock();
- currFile = fopen(filename, "ab");
- fwrite(data, sizeof(char), data_len, currFile);
- fclose(currFile);
- return true;
- // Something strange has happened
- default:
- std::cerr << "Unknown packet type received. Failing." << std::endl;
- break;
- }
- return false;
- }
- void file_websocket_close_handler(
- const struct mg_connection *conn,
- void *user_data)
- {
- struct synchronization_tool *sync = (struct synchronization_tool *) user_data;
- std::unique_lock<std::mutex> lck(sync->mtx);
- sync->val = 1;
- sync->val2 = 1;
- sync->cv.notify_all();
- }
- /*
- * SYNCHRONIZATION AND RECEIVE SERIALIZED DATA
- */
- int epoch_websocket_data_handler(
- struct mg_connection *conn,
- int bits,
- char *data,
- size_t data_len,
- void *user_data)
- {
- struct synchronization_tool *sync = (struct synchronization_tool *) user_data;
- char *filename = (char *) mg_get_user_connection_data(conn);
- FILE *currFile = NULL;
- std::unique_lock<std::mutex> lck(sync->mtx, std::defer_lock);
- switch (bits & 0xf)
- {
- case MG_WEBSOCKET_OPCODE_CONNECTION_CLOSE:
- break;
- // Responder has indicated they have sent all relevant data
- case MG_WEBSOCKET_OPCODE_DATACOMPLETE:
- lck.lock();
- sync->val++;
- break;
- // Responder has sent more data (which may theoretically be broken up into multiple packets)
- case MG_WEBSOCKET_OPCODE_BINARY:
- case MG_WEBSOCKET_OPCODE_CONTINUATION:
- lck.lock();
- currFile = fopen(filename, "ab");
- fwrite(data, sizeof(char), data_len, currFile);
- fclose(currFile);
- return true;
- // Something strange has happened
- default:
- std::cerr << "Unknown packet type received. Failing." << std::endl;
- break;
- }
- return false;
- }
- void epoch_websocket_close_handler(
- const struct mg_connection *conn,
- void *user_data)
- {
- struct synchronization_tool *sync = (struct synchronization_tool *) user_data;
- std::unique_lock<std::mutex> lck(sync->mtx);
- sync->val2 = 1;
- sync->cv.notify_all();
- }
- /*
- * SPECIAL FOR HANDLING UNUSUAL DATA
- */
- int clients_websocket_data_handler(
- struct mg_connection *conn,
- int bits,
- char *data,
- size_t data_len,
- void *user_data)
- {
- struct synchronization_tool *sync = (struct synchronization_tool *) user_data;
- std::unique_lock<std::mutex> lck(sync->mtx, std::defer_lock);
- switch (bits & 0xf)
- {
- // Responder has indicated they have sent all relevant data
- case MG_WEBSOCKET_OPCODE_DATACOMPLETE:
- case MG_WEBSOCKET_OPCODE_CONNECTION_CLOSE:
- break;
- // Responder has sent data
- case MG_WEBSOCKET_OPCODE_BINARY:
- lck.lock();
- if (data_len == sizeof(sync->val))
- sync->val = *((size_t *) data);
- break;
- // Something strange has happened
- default:
- std::cerr << "Unknown packet type received. Failing." << std::endl;
- break;
- }
- return false;
- }
- /********************************************
- **** ****
- **** Generic handler member functions ****
- **** ****
- ********************************************/
- /*
- * EXIT SYNCHRONIZATION HANDLER
- */
- RemoteControlHandler::RemoteControlHandler(
- struct synchronization_tool *sync)
- : sync(sync)
- { /* */ }
- RemoteControlHandler::RemoteControlHandler(
- struct synchronization_tool *sync,
- const std::string& message)
- : sync(sync), message(message)
- { /* */ }
- bool RemoteControlHandler::handleGet(
- CivetServer *server,
- struct mg_connection *conn)
- {
- std::unique_lock<std::mutex> lck(sync->mtx);
- mg_printf(conn, "HTTP/1.1 200 OK\r\n"
- "Content-Type: text/plain\r\n"
- "Connection: close\r\n\r\n");
- if (message.empty())
- mg_printf(conn, "Event triggered.\n");
- else
- mg_printf(conn, "%s\n", message.c_str());
- sync->val++;
- sync->cv.notify_all();
- return true;
- }
- /*
- * EXPERIMENT EVENT SYNCHRONIZATION HANDLER
- */
- AltRemoteControlHandler::AltRemoteControlHandler(
- size_t value,
- struct synchronization_tool *sync)
- : value(value), sync(sync)
- { /* */ }
- AltRemoteControlHandler::AltRemoteControlHandler(
- size_t value,
- struct synchronization_tool *sync,
- const std::string& message)
- : value(value), sync(sync), message(message)
- { /* */ }
- bool AltRemoteControlHandler::handleGet(
- CivetServer *server,
- struct mg_connection *conn)
- {
- std::unique_lock<std::mutex> lck(sync->mtx);
- const struct mg_request_info *info = mg_get_request_info(conn);
- if (info->query_string)
- query = info->query_string;
- mg_printf(conn, "HTTP/1.1 200 OK\r\n"
- "Content-Type: text/plain\r\n"
- "Connection: close\r\n\r\n");
- if (message.empty())
- mg_printf(conn, "Event triggered.\n");
- else
- mg_printf(conn, "%s\n", message.c_str());
- sync->val2 = value;
- sync->cv.notify_all();
- return true;
- }
- std::string AltRemoteControlHandler::getQuery() const
- {
- return query;
- }
|