networking.hpp 4.4 KB

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