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