networking.hpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. #ifndef __PRSONA_NETWORKING_HPP
  2. #define __PRSONA_NETWORKING_HPP
  3. #define USE_SERVER_STATS 1
  4. #define USE_SSL 0
  5. #include <mutex>
  6. #include <condition_variable>
  7. #include <random>
  8. #include <string>
  9. #include "CivetServer.h"
  10. #include "Scalar.hpp"
  11. #include "base.hpp"
  12. #define MG_WEBSOCKET_OPCODE_DATACOMPLETE 0xb
  13. #define DEFAULT_PRSONA_PORT_STR "8080"
  14. #define TMP_FILE_SIZE 12
  15. #define TMP_DIR "./tmp/"
  16. #define TMP_DIR_SIZE 6
  17. enum RequestType {
  18. /* SERVER PUBLIC FUNCTIONS */
  19. // BASIC PUBLIC SYSTEM INFO GETTERS
  20. PRSONA_GIVE_BGN_PUBKEY = 'a',
  21. PRSONA_GIVE_NUM_CLIENTS,
  22. PRSONA_GIVE_NUM_SERVERS,
  23. PRSONA_GIVE_EG_BLIND_GENERATOR,
  24. PRSONA_GIVE_FRESH_GENERATOR,
  25. // ENCRYPTED DATA GETTERS
  26. PRSONA_GIVE_VOTE_ROW,
  27. PRSONA_GIVE_VOTE_MATRIX,
  28. PRSONA_GIVE_CLIENT_TALLY,
  29. PRSONA_GIVE_SERVER_TALLY,
  30. PRSONA_GIVE_PSEUDONYMS,
  31. // PROOF COMMITMENT GETTERS
  32. PRSONA_GIVE_VOTE_ROW_COMMITMENT,
  33. PRSONA_GIVE_VOTE_MATRIX_COMMITMENT,
  34. PRSONA_GIVE_CLIENT_TALLY_COMMITMENT,
  35. PRSONA_GIVE_SERVER_TALLY_COMMITMENT,
  36. PRSONA_GIVE_PSEUDONYMS_COMMITMENT,
  37. // CLIENT INTERACTIONS
  38. PRSONA_RECEIVE_NEW_CLIENT,
  39. PRSONA_RECEIVE_VOTE,
  40. /* SERVER PRIVATE FUNCTIONS */
  41. // CLIENT INTERACTION HELPER
  42. PRSONA_RECEIVE_UPDATE_WITH_NEW_USER,
  43. // CONSTRUCTOR HELPERS
  44. PRSONA_GIVE_BGN_PRIVKEY,
  45. PRSONA_ADD_CURR_SEED_TO_GENERATOR,
  46. PRSONA_RECEIVE_FRESH_GENERATOR,
  47. PRSONA_ADD_RAND_SEED_TO_GENERATOR,
  48. PRSONA_RECEIVE_EG_BLIND_GENERATOR,
  49. // EPOCH ROUNDS
  50. PRSONA_PERFORM_EPOCH_BUILD_UP,
  51. PRSONA_PERFORM_EPOCH_BREAK_DOWN,
  52. PRSONA_RECEIVE_EPOCH_UPDATE,
  53. // DISTRIBUTED BGN
  54. PRSONA_GIVE_PARTIAL_DECRYPTION = '-',
  55. PRSONA_RECEIVE_PARTIAL_DECRYPTION = '_',
  56. /* CLIENT PUBLIC FUNCTIONS */
  57. // REPUTATION PROOF VERIFIER
  58. PRSONA_VERIFY_REPUTATION_PROOF = 'a'
  59. };
  60. /* SERVER PUBLIC FUNCTIONS */
  61. // BASIC PUBLIC SYSTEM INFO GETTERS
  62. #define REQUEST_BGN_PUBKEY_URI "/ws?a"
  63. #define REQUEST_NUM_CLIENTS_URI "/ws?b"
  64. #define REQUEST_NUM_SERVERS_URI "/ws?c"
  65. #define REQUEST_EG_BLIND_GENERATOR_URI "/ws?d"
  66. #define REQUEST_FRESH_GENERATOR_URI "/ws?e"
  67. // ENCRYPTED DATA GETTERS
  68. #define REQUEST_VOTE_ROW_URI "/ws?f"
  69. #define REQUEST_VOTE_MATRIX_URI "/ws?g"
  70. #define REQUEST_CLIENT_TALLY_URI "/ws?h"
  71. #define REQUEST_SERVER_TALLY_URI "/ws?i"
  72. #define REQUEST_PSEUDONYMS_URI "/ws?j"
  73. // PROOF COMMITMENT GETTERS
  74. #define REQUEST_VOTE_ROW_COMMITMENT_URI "/ws?k"
  75. #define REQUEST_VOTE_MATRIX_COMMITMENT_URI "/ws?l"
  76. #define REQUEST_CLIENT_TALLY_COMMITMENT_URI "/ws?m"
  77. #define REQUEST_SERVER_TALLY_COMMITMENT_URI "/ws?n"
  78. #define REQUEST_PSEUDONYMS_COMMITMENT_URI "/ws?o"
  79. // CLIENT INTERACTIONS
  80. #define SUBMIT_NEW_CLIENT_URI "/ws?p"
  81. #define SUBMIT_VOTE_URI "/ws?q"
  82. /* SERVER PRIVATE FUNCTIONS */
  83. // CLIENT INTERACTION HELPER
  84. #define SUBMIT_UPDATE_WITH_NEW_USER_URI "/ws?r"
  85. // CONSTRUCTOR HELPERS
  86. #define REQUEST_BGN_PRIVKEY_URI "/ws?s"
  87. #define REQUEST_ADD_CURR_SEED_FOR_FRESH_GENERATOR_URI "/ws?t"
  88. #define SUBMIT_FRESH_GENERATOR_URI "/ws?u"
  89. #define REQUEST_ADD_RAND_SEED_FOR_EG_BLIND_GENERATOR_URI "/ws?v"
  90. #define SUBMIT_EG_BLIND_GENERATOR_URI "/ws?w"
  91. // EPOCH ROUNDS
  92. #define REQUEST_EPOCH_BUILD_UP_URI "/ws?x"
  93. #define REQUEST_EPOCH_BREAK_DOWN_URI "/ws?y"
  94. #define SUBMIT_EPOCH_UPDATES_URI "/ws?z"
  95. // DISTRIBUTED BGN
  96. #define REQUEST_PARTIAL_DECRYPTION_URI "/ws?-"
  97. #define SUBMIT_PARTIAL_DECRYPTION_URI "/ws?_"
  98. /* CLIENT PUBLIC FUNCTIONS */
  99. // REPUTATION PROOF VERIFIER
  100. #define VERIFY_REPUTATION_PROOF_URI "/ws?a"
  101. /* SYNCHRONIZATION HANDLERS */
  102. // ORCHESTRATOR-SERVER/CLIENT SYNCHRONIZATION
  103. #define EXIT_URI "/exit"
  104. // INTER-SERVER SYNCHRONIZATION
  105. #define SERVER_CHECK_IN_URI "/init"
  106. #define UPDATE_LOCK_URI "/lock"
  107. #define UPDATE_UNLOCK_URI "/unlock"
  108. // SERVER-ORCHESTRATOR SYNCHRONIZATION
  109. #define EPOCH_READY_URI "/ready"
  110. // CLIENT-ORCHESTRATOR SYNCHRONIZATION
  111. #define CLIENT_READY_URI "/taskdone"
  112. // SERVER EXPERIMENT TRIGGER
  113. #define TRIGGER_EPOCH_URI "/epoch"
  114. // CLIENT EXPERIMENT TRIGGERS
  115. #define TRIGGER_VOTE_URI "/vote"
  116. #define TRIGGER_REP_URI "/rep"
  117. // SERVER-CLIENT SYNCHRONIZATION
  118. #define WHICH_EPOCH_URI "/which"
  119. /* Helper types */
  120. // Struct to help synchronization efforts across multi-threaded websocket usage
  121. struct synchronization_tool {
  122. std::mutex mtx;
  123. std::condition_variable cv;
  124. size_t val, val2;
  125. };
  126. /* "PUBLIC" GENERIC HELPER FUNCTIONS */
  127. // This function must be run once before any PRSONA objects are created or used
  128. void initialize_prsona_classes();
  129. // Makes a random temp filename to be used in receiving serialized data
  130. char *set_temp_filename(
  131. std::default_random_engine& rng,
  132. struct mg_connection *conn);
  133. // Loads config information
  134. void load_multiple_instances_config(
  135. std::vector<std::string>& relevantIPs,
  136. std::vector<int>& relevantPorts,
  137. const char *filename);
  138. void load_single_instance_config(
  139. std::string& relevantIP,
  140. std::string& relevantPortStr,
  141. int& relevantPort,
  142. const char *filename);
  143. // Extracts relevant log data from server
  144. std::vector<size_t> get_server_log_data(
  145. const struct mg_context *ctx);
  146. std::vector<size_t> get_conn_log_data(
  147. const struct mg_context *ctx,
  148. bool receivedData);
  149. // Write log data to file
  150. void write_log_data(
  151. std::mutex& outputMtx,
  152. const std::string& outputFilename,
  153. const std::vector<double>& timingData,
  154. const std::vector<size_t>& bandwidthData);
  155. void write_special_log_data(
  156. std::mutex& outputMtx,
  157. const std::string& outputFilename,
  158. const std::vector<double>& timingData,
  159. const std::vector<size_t>& bandwidthData,
  160. bool corruption);
  161. void write_usage_data(
  162. std::mutex& outputMtx,
  163. const std::string& outputFilename);
  164. /* "PRIVATE" FUNCTIONS TO HELP THE GENERIC HELPERS */
  165. // Helper for set_temp_filename()
  166. std::string random_string(
  167. std::default_random_engine& rng,
  168. size_t length);
  169. // Helper for get_log_data()
  170. size_t parse_log_for_data(
  171. const char *input,
  172. const char *key);
  173. // This function gets current memory usage
  174. unsigned long mem_usage();
  175. /* WEBSOCKET HANDLER FUNCTIONS */
  176. // NULL
  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. void empty_websocket_close_handler(
  184. const struct mg_connection *conn,
  185. void *user_data);
  186. // SYNCHRONIZATION ONLY
  187. int synchro_websocket_data_handler(
  188. struct mg_connection *conn,
  189. int bits,
  190. char *data,
  191. size_t data_len,
  192. void *user_data);
  193. void synchro_websocket_close_handler(
  194. const struct mg_connection *conn,
  195. void *user_data);
  196. // RECEIVE SERIALIZED DATA ONLY
  197. int file_websocket_data_handler(
  198. struct mg_connection *conn,
  199. int bits,
  200. char *data,
  201. size_t data_len,
  202. void *user_data);
  203. void file_websocket_close_handler(
  204. const struct mg_connection *conn,
  205. void *user_data);
  206. // SYNCHRONIZATION AND RECEIVE SERIALIZED DATA
  207. int epoch_websocket_data_handler(
  208. struct mg_connection *conn,
  209. int bits,
  210. char *data,
  211. size_t data_len,
  212. void *user_data);
  213. void epoch_websocket_close_handler(
  214. const struct mg_connection *conn,
  215. void *user_data);
  216. // SPECIAL FOR HANDLING UNUSUAL DATA
  217. int clients_websocket_data_handler(
  218. struct mg_connection *conn,
  219. int bits,
  220. char *data,
  221. size_t data_len,
  222. void *user_data);
  223. /* GENERIC HANDLERS */
  224. // Used for signaling exits
  225. class RemoteControlHandler : public CivetHandler
  226. {
  227. public:
  228. RemoteControlHandler(
  229. struct synchronization_tool *sync);
  230. RemoteControlHandler(
  231. struct synchronization_tool *sync,
  232. const std::string& message);
  233. bool handleGet(
  234. CivetServer *server,
  235. struct mg_connection *conn);
  236. private:
  237. struct synchronization_tool *sync;
  238. const std::string message;
  239. };
  240. // Used for signaling events (making votes, epoch changes, etc.)
  241. class AltRemoteControlHandler : public CivetHandler
  242. {
  243. public:
  244. AltRemoteControlHandler(
  245. size_t value,
  246. struct synchronization_tool *sync);
  247. AltRemoteControlHandler(size_t value,
  248. struct synchronization_tool *sync,
  249. const std::string& message);
  250. bool handleGet(CivetServer *server, struct mg_connection *conn);
  251. std::string getQuery() const;
  252. private:
  253. const size_t value;
  254. struct synchronization_tool *sync;
  255. const std::string message;
  256. std::string query;
  257. };
  258. #endif