networkClient.hpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. std::mutex& usageMtx,
  32. const std::string& usageFilename);
  33. bool make_reputation_proof(
  34. std::default_random_engine& rng,
  35. PrsonaClient* prsonaClient,
  36. const std::vector<std::string>& serverIPs,
  37. const std::vector<int>& serverPorts,
  38. const std::string& target,
  39. int targetPort,
  40. size_t numClients,
  41. const CivetServer& civetServer,
  42. std::mutex& outputMtx,
  43. const std::string& outputFilename,
  44. std::mutex& usageMtx,
  45. const std::string& usageFilename);
  46. /* "PRIVATE" FUNCTIONS */
  47. // HELPERS TO ADD THIS CLIENT TO SERVERS
  48. void register_new_client(
  49. std::default_random_engine& rng,
  50. PrsonaClient *newUser,
  51. const std::vector<std::string>& serverIPs,
  52. const std::vector<int>& serverPorts,
  53. const Proof& proofOfValidSTPK,
  54. const Twistpoint& shortTermPublicKey);
  55. void verify_valid_addition(
  56. std::default_random_engine& rng,
  57. PrsonaClient *newUser,
  58. const std::vector<std::string>& serverIPs,
  59. const std::vector<int>& serverPorts,
  60. const std::vector<Proof>& proofOfValidAddition,
  61. const Twistpoint& shortTermPublicKey);
  62. // GETTERS FOR VARIOUS SERVER VALUES
  63. Twistpoint get_generator(
  64. std::default_random_engine& rng,
  65. const std::vector<std::string>& serverIPs,
  66. const std::vector<int>& serverPorts,
  67. bool fresh,
  68. std::vector<Proof>& pi,
  69. std::vector<size_t>& bandwidthData);
  70. BGNPublicKey get_bgn_public_key(
  71. std::default_random_engine& rng,
  72. const std::vector<std::string>& serverIPs,
  73. const std::vector<int>& serverPorts);
  74. template <typename T>
  75. T get_server_committed_val(
  76. std::default_random_engine& rng,
  77. const std::vector<std::string>& serverIPs,
  78. const std::vector<int>& serverPorts,
  79. const char *firstUri,
  80. const char *commitUri,
  81. std::vector<Proof>& pi,
  82. const Twistpoint& shortTermPublicKey,
  83. std::vector<size_t>& bandwidthData);
  84. // HELPERS FOR GENERALIZED GETTER FUNCTION
  85. template <typename T>
  86. T get_first_committed_val(
  87. std::default_random_engine& rng,
  88. const std::string& serverIP,
  89. int serverPort,
  90. const char *firstUri,
  91. Proof& pi,
  92. const Twistpoint& shortTermPublicKey,
  93. std::vector<size_t>& bandwidthData);
  94. void get_additional_commitment(
  95. std::default_random_engine& rng,
  96. const std::vector<std::string>& serverIPs,
  97. const std::vector<int>& serverPorts,
  98. const std::string& skipIP,
  99. int skipPort,
  100. const char *commitUri,
  101. std::vector<Proof>& pi,
  102. const Twistpoint& shortTermPublicKey,
  103. std::vector<size_t>& bandwidthData);
  104. // FILE I/O HELPERS FOR ALL GETTERS
  105. std::vector<Proof> get_valid_addition_proof_from_file(
  106. const char *filename);
  107. Twistpoint get_generator_from_file(
  108. const char *filename,
  109. std::vector<Proof>& pi);
  110. BGNPublicKey get_bgn_public_key_from_file(
  111. const char *filename);
  112. template <typename T>
  113. T get_committed_val_from_file(
  114. const char *filename,
  115. Proof& pi);
  116. Proof get_commitment_from_file(
  117. struct synchronization_tool *sync,
  118. const char *filename);
  119. // GENERALIZED SENDER FOR ORCHESTRATOR-SIGNALED OPERATIONS
  120. char *send_item(
  121. std::default_random_engine& rng,
  122. const std::string& target,
  123. int targetPort,
  124. const char* whichUri,
  125. const std::string& data,
  126. bool responseExpected,
  127. std::vector<size_t>& bandwidthData);
  128. // DATA SERIALIZERS
  129. std::string make_vote_string(
  130. const std::vector<Proof>& pi,
  131. const std::vector<TwistBipoint>& newVotes,
  132. const Twistpoint& shortTermPublicKey);
  133. std::string make_rep_proof_string(
  134. const std::vector<Proof>& pi,
  135. const Twistpoint& shortTermPublicKey,
  136. const Scalar& threshold);
  137. /* OTHER CLIENT-RELEVANT HANDLERS */
  138. // Used to tell orchestrator when the client is no longer doing a triggered task
  139. class ClientReadyHandler : public CivetHandler
  140. {
  141. public:
  142. ClientReadyHandler(
  143. struct synchronization_tool *exitSync);
  144. bool handleGet(
  145. CivetServer *server,
  146. struct mg_connection *conn);
  147. private:
  148. struct synchronization_tool *exitSync;
  149. };
  150. /* CLIENT-SPECIFIC HANDLER */
  151. class PrsonaClientWebSocketHandler : public CivetWebSocketHandler {
  152. public:
  153. // CONSTRUCTOR
  154. PrsonaClientWebSocketHandler(
  155. std::default_random_engine& rng,
  156. PrsonaClient *prsonaClient,
  157. const std::vector<std::string>& serverIPs,
  158. const std::vector<int>& serverPorts,
  159. std::mutex& outputMtx,
  160. const std::string& outputFilename,
  161. std::mutex& usageMtx,
  162. const std::string& usageFilename);
  163. // REQUIRED BY INHERITED CLASS
  164. virtual bool handleConnection(
  165. CivetServer *server,
  166. const struct mg_connection *conn);
  167. virtual void handleReadyState(
  168. CivetServer *server,
  169. struct mg_connection *conn);
  170. virtual bool handleData(
  171. CivetServer *server,
  172. struct mg_connection *conn,
  173. int bits,
  174. char *data,
  175. size_t data_len);
  176. virtual void handleClose(
  177. CivetServer *server,
  178. const struct mg_connection *conn);
  179. private:
  180. std::default_random_engine &rng;
  181. PrsonaClient *prsonaClient;
  182. const std::vector<std::string> serverIPs;
  183. const std::vector<int> serverPorts;
  184. std::mutex& outputMtx;
  185. const std::string outputFilename;
  186. std::mutex& usageMtx;
  187. const std::string usageFilename;
  188. // RESPONSE ROUTER FUNCTION
  189. void generate_response(
  190. CivetServer *server,
  191. struct mg_connection *conn,
  192. const char *filename);
  193. // REPUTATION PROOF RESPONSE
  194. void verify_reputation_proof(
  195. CivetServer *civetServer,
  196. struct mg_connection *conn,
  197. const char *filename);
  198. };
  199. #endif