networkClient.hpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #ifndef __PRSONA_NETWORK_CLIENT_HPP
  2. #define __PRSONA_NETWORK_CLIENT_HPP
  3. #include <string>
  4. #include <vector>
  5. #include <random>
  6. #include "client.hpp"
  7. #include "networking.hpp"
  8. enum EventType {
  9. CLIENT_MAKE_VOTE = 1,
  10. CLIENT_MAKE_REP_PROOF
  11. };
  12. /* "PUBLIC" FUNCTIONS */
  13. // CREATOR FOR A NEW CLIENT
  14. PrsonaClient *create_client(
  15. std::default_random_engine& rng,
  16. const std::vector<std::string>& serverIPs,
  17. const std::vector<int>& serverPorts,
  18. size_t numServers);
  19. // FUNCTIONS TO PERFORM OPERATIONS FOR EXPERIMENT
  20. void make_vote(
  21. std::default_random_engine& rng,
  22. PrsonaClient* prsonaClient,
  23. const std::vector<std::string>& serverIPs,
  24. const std::vector<int>& serverPorts,
  25. const std::string& target,
  26. int targetPort,
  27. size_t numClients,
  28. const CivetServer& civetServer,
  29. std::mutex& outputMtx,
  30. const std::string& outputFilename);
  31. bool make_reputation_proof(
  32. std::default_random_engine& rng,
  33. PrsonaClient* prsonaClient,
  34. const std::vector<std::string>& serverIPs,
  35. const std::vector<int>& serverPorts,
  36. const std::string& target,
  37. int targetPort,
  38. size_t numClients,
  39. const CivetServer& civetServer,
  40. std::mutex& outputMtx,
  41. const std::string& outputFilename);
  42. /* "PRIVATE" FUNCTIONS */
  43. // HELPERS TO ADD THIS CLIENT TO SERVERS
  44. void register_new_client(
  45. std::default_random_engine& rng,
  46. PrsonaClient *newUser,
  47. const std::vector<std::string>& serverIPs,
  48. const std::vector<int>& serverPorts,
  49. const Proof& proofOfValidSTPK,
  50. const Twistpoint& shortTermPublicKey);
  51. void verify_valid_addition(
  52. std::default_random_engine& rng,
  53. PrsonaClient *newUser,
  54. const std::vector<std::string>& serverIPs,
  55. const std::vector<int>& serverPorts,
  56. const std::vector<Proof>& proofOfValidAddition,
  57. const Twistpoint& shortTermPublicKey);
  58. // GETTERS FOR VARIOUS SERVER VALUES
  59. Twistpoint get_generator(
  60. std::default_random_engine& rng,
  61. const std::vector<std::string>& serverIPs,
  62. const std::vector<int>& serverPorts,
  63. bool fresh,
  64. std::vector<Proof>& pi);
  65. BGNPublicKey get_bgn_public_key(
  66. std::default_random_engine& rng,
  67. const std::vector<std::string>& serverIPs,
  68. const std::vector<int>& serverPorts);
  69. template <typename T>
  70. T get_server_committed_val(
  71. std::default_random_engine& rng,
  72. const std::vector<std::string>& serverIPs,
  73. const std::vector<int>& serverPorts,
  74. const char *firstUri,
  75. const char *commitUri,
  76. std::vector<Proof>& pi,
  77. const Twistpoint& shortTermPublicKey);
  78. // HELPERS FOR GENERALIZED GETTER FUNCTION
  79. template <typename T>
  80. T get_first_committed_val(
  81. std::default_random_engine& rng,
  82. const std::string& server,
  83. int serverPort,
  84. const char *firstUri,
  85. Proof& pi,
  86. const Twistpoint& shortTermPublicKey);
  87. void get_additional_commitment(
  88. std::default_random_engine& rng,
  89. const std::vector<std::string>& serverIPs,
  90. const std::vector<int>& serverPorts,
  91. const std::string& skipIP,
  92. int skipPort,
  93. const char *commitUri,
  94. std::vector<Proof>& pi,
  95. const Twistpoint& shortTermPublicKey);
  96. // FILE I/O HELPERS FOR ALL GETTERS
  97. std::vector<Proof> get_valid_addition_proof_from_file(
  98. const char *filename);
  99. Twistpoint get_generator_from_file(
  100. const char *filename,
  101. std::vector<Proof>& pi);
  102. BGNPublicKey get_bgn_public_key_from_file(
  103. const char *filename);
  104. template <typename T>
  105. T get_committed_val_from_file(
  106. const char *filename,
  107. Proof& pi);
  108. Proof get_commitment_from_file(
  109. struct synchronization_tool *sync,
  110. const char *filename);
  111. // GENERALIZED SENDER FOR ORCHESTRATOR-SIGNALED OPERATIONS
  112. char *send_item(
  113. std::default_random_engine& rng,
  114. const std::string& target,
  115. int targetPort,
  116. const char* whichUri,
  117. const std::string& data,
  118. bool responseExpected);
  119. // DATA SERIALIZERS
  120. std::string make_vote_string(
  121. const std::vector<Proof>& pi,
  122. const std::vector<TwistBipoint>& newVotes,
  123. const Twistpoint& shortTermPublicKey);
  124. std::string make_rep_proof_string(
  125. const std::vector<Proof>& pi,
  126. const Twistpoint& shortTermPublicKey,
  127. const Scalar& threshold);
  128. /* CLIENT-SPECIFIC HANDLER */
  129. class PrsonaClientWebSocketHandler : public CivetWebSocketHandler {
  130. public:
  131. // CONSTRUCTOR
  132. PrsonaClientWebSocketHandler(
  133. std::default_random_engine& rng,
  134. PrsonaClient *prsonaClient,
  135. const std::vector<std::string>& serverIPs,
  136. const std::vector<int>& serverPorts,
  137. std::mutex& outputMtx,
  138. const std::string& outputFilename);
  139. // REQUIRED BY INHERITED CLASS
  140. virtual bool handleConnection(
  141. CivetServer *server,
  142. const struct mg_connection *conn);
  143. virtual void handleReadyState(
  144. CivetServer *server,
  145. struct mg_connection *conn);
  146. virtual bool handleData(
  147. CivetServer *server,
  148. struct mg_connection *conn,
  149. int bits,
  150. char *data,
  151. size_t data_len);
  152. virtual void handleClose(
  153. CivetServer *server,
  154. const struct mg_connection *conn);
  155. private:
  156. std::default_random_engine &rng;
  157. PrsonaClient *prsonaClient;
  158. const std::vector<std::string> serverIPs;
  159. const std::vector<int> serverPorts;
  160. std::mutex& outputMtx;
  161. const std::string outputFilename;
  162. // RESPONSE ROUTER FUNCTION
  163. void generate_response(
  164. CivetServer *server,
  165. struct mg_connection *conn,
  166. const char *filename);
  167. // REPUTATION PROOF RESPONSE
  168. void verify_reputation_proof(
  169. CivetServer *civetServer,
  170. struct mg_connection *conn,
  171. const char *filename);
  172. };
  173. #endif