networking.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #ifndef __PRSONA_NETWORKING_HPP
  2. #define __PRSONA_NETWORKING_HPP
  3. #include <mutex>
  4. #include <condition_variable>
  5. #include <string>
  6. #include "CivetServer.h"
  7. #define MG_WEBSOCKET_OPCODE_DATACOMPLETE 0xb
  8. #define PRSONA_PORT_STR "8080"
  9. #define USE_SSL 0
  10. #define EXIT_URI "/exit"
  11. #define SERVER_READY_URI "/init"
  12. #define EPOCH_READY_URI "/ready"
  13. #define TRIGGER_EPOCH_URI "/epoch"
  14. #define TRIGGER_VOTE_URI "/vote"
  15. #define TRIGGER_REP_URI "/rep"
  16. #define WHICH_EPOCH_URI "/which"
  17. #define UPDATE_LOCK_URI "/lock"
  18. #define UPDATE_UNLOCK_URI "/unlock"
  19. #define ADD_USER_URI "/ws?a"
  20. #define GIVE_NEW_VOTE_URI "/ws?b"
  21. #define PUBLIC_BGN_URI "/ws?c"
  22. #define NUM_CLIENTS_URI "/ws?d"
  23. #define VOTES_BY_URI "/ws?f"
  24. #define VOTE_MATRIX_URI "/ws?g"
  25. #define USER_TALLY_URI "/ws?h"
  26. #define SERVER_TALLY_URI "/ws?i"
  27. #define PSEUDONYMS_URI "/ws?j"
  28. #define VOTES_BY_COMMIT_URI "/ws?k"
  29. #define VOTE_MATRIX_COMMIT_URI "/ws?l"
  30. #define USER_TALLY_COMMIT_URI "/ws?m"
  31. #define SERVER_TALLY_COMMIT_URI "/ws?n"
  32. #define PSEUDONYMS_COMMIT_URI "/ws?o"
  33. #define PRIVATE_BGN_URI "/ws?p"
  34. #define GET_FRESH_GEN_URI "/ws?q"
  35. #define GIVE_FRESH_GEN_URI "/ws?r"
  36. #define GET_BLIND_GEN_URI "/ws?s"
  37. #define GIVE_BLIND_GEN_URI "/ws?t"
  38. #define EPOCH_BUILD_UP_URI "/ws?u"
  39. #define EPOCH_BREAK_DOWN_URI "/ws?v"
  40. #define ACCEPT_EPOCH_UPDATES_URI "/ws?w"
  41. #define GIVE_NEW_USER_URI "/ws?x"
  42. #define GET_DECRYPTION_URI "/ws?y"
  43. #define GIVE_DECRYPTION_URI "/ws?z"
  44. #define FRESH_GEN_URI "/ws?-"
  45. #define BLIND_GEN_URI "/ws?_"
  46. #define REP_PROOF_URI "/ws?d"
  47. #define TMP_FILE_SIZE 12
  48. #define TMP_DIR "./tmp/"
  49. #define TMP_DIR_SIZE 6
  50. enum RequestType {
  51. PRSONA_ADD_CLIENT = 'a',
  52. PRSONA_RECEIVE_VOTE,
  53. PRSONA_GET_BGN_PUBKEY,
  54. PRSONA_GET_NUM_CLIENTS,
  55. PRSONA_GET_NUM_SERVERS,
  56. PRSONA_GET_VOTES_BY,
  57. PRSONA_GET_ALL_VOTES,
  58. PRSONA_GET_USER_TALLY,
  59. PRSONA_GET_SERVER_TALLY,
  60. PRSONA_GET_PSEUDONYMS,
  61. PRSONA_GET_VOTE_ROW_COMMITMENT,
  62. PRSONA_GET_VOTE_MATRIX_COMMITMENT,
  63. PRSONA_GET_USER_TALLY_COMMITMENT,
  64. PRSONA_GET_SERVER_TALLY_COMMITMENT,
  65. PRSONA_GET_PSEUDONYMS_COMMITMENT,
  66. PRSONA_GET_BGN_DETAILS,
  67. PRSONA_ADD_CURR_SEED_TO_GENERATOR,
  68. PRSONA_SET_FRESH_GENERATOR,
  69. PRSONA_ADD_RAND_SEED_TO_GENERATOR,
  70. PRSONA_SET_EG_BLIND_GENERATOR,
  71. PRSONA_EPOCH_BUILD_UP,
  72. PRSONA_EPOCH_BREAK_DOWN,
  73. PRSONA_EPOCH_UPDATE,
  74. PRSONA_NEW_USER_UPDATE,
  75. PRSONA_GET_PARTIAL_DECRYPTION,
  76. PRSONA_RECEIVE_PARTIAL_DECRYPTION,
  77. PRSONA_GET_FRESH_GENERATOR = '-',
  78. PRSONA_GET_EG_BLIND_GENERATOR ='_',
  79. PRSONA_RECEIVE_FRESH_GENERATOR = 'a',
  80. PRSONA_RECEIVE_VOTE_TALLY,
  81. PRSONA_RECEIVE_NEW_USER_DATA,
  82. PRSONA_VERIFY_REPUTATION_PROOF
  83. };
  84. struct synchronization_tool {
  85. std::mutex mtx;
  86. std::condition_variable cv;
  87. size_t val, val2;
  88. };
  89. std::string random_string(std::default_random_engine& generator, size_t length);
  90. char *set_temp_filename(std::default_random_engine& generator, struct mg_connection *conn);
  91. class RemoteControlHandler : public CivetHandler
  92. {
  93. public:
  94. RemoteControlHandler(struct synchronization_tool *sync)
  95. : sync(sync)
  96. { /* */ }
  97. RemoteControlHandler(struct synchronization_tool *sync,
  98. const std::string& message)
  99. : sync(sync), message(message)
  100. { /* */ }
  101. bool handleGet(CivetServer *server, struct mg_connection *conn);
  102. private:
  103. struct synchronization_tool *sync;
  104. const std::string message;
  105. };
  106. class AltRemoteControlHandler : public CivetHandler
  107. {
  108. public:
  109. AltRemoteControlHandler(size_t value, struct synchronization_tool *sync)
  110. : value(value), sync(sync)
  111. { /* */ }
  112. AltRemoteControlHandler(size_t value,
  113. struct synchronization_tool *sync,
  114. const std::string& message)
  115. : value(value), sync(sync), message(message)
  116. { /* */ }
  117. bool handleGet(CivetServer *server, struct mg_connection *conn);
  118. std::string getQuery() const;
  119. private:
  120. const size_t value;
  121. struct synchronization_tool *sync;
  122. const std::string message;
  123. std::string query;
  124. };
  125. int empty_websocket_data_handler(
  126. struct mg_connection *conn,
  127. int bits,
  128. char *data,
  129. size_t data_len,
  130. void *user_data);
  131. void empty_websocket_close_handler(
  132. const struct mg_connection *conn,
  133. void *user_data);
  134. int synchro_websocket_data_handler(
  135. struct mg_connection *conn,
  136. int bits,
  137. char *data,
  138. size_t data_len,
  139. void *user_data);
  140. void synchro_websocket_close_handler(
  141. const struct mg_connection *conn,
  142. void *user_data);
  143. int file_websocket_data_handler(
  144. struct mg_connection *conn,
  145. int bits,
  146. char *data,
  147. size_t data_len,
  148. void *user_data);
  149. void file_websocket_close_handler(
  150. const struct mg_connection *conn,
  151. void *user_data);
  152. #endif