clientMain.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #include <mutex>
  2. #include <condition_variable>
  3. #include <chrono>
  4. #include <iostream>
  5. #include <fstream>
  6. #include <cstring>
  7. #include <cstdlib>
  8. #include <vector>
  9. #include <string>
  10. #include "networkClient.hpp"
  11. #define EPOCH_URI "/epoch"
  12. #define EXIT_URI "/exit"
  13. #define BGN_URI "/ws?c"
  14. #define BLIND_GEN_URI "/ws?_"
  15. #define BGN_TMP_FILE "~/tmp/bgn"
  16. #define GEN_TMP_FILE "~/tmp/generator"
  17. using namespace std;
  18. struct synchronization_tool exitSync, bgnSync, generatorSync;
  19. size_t epochNum = 0;
  20. // Initialize the classes we use
  21. void initialize_prsona_classes()
  22. {
  23. Scalar::init();
  24. PrsonaBase::init();
  25. PrsonaBase::set_client_malicious();
  26. }
  27. PrsonaClient *create_client_from_files(size_t numServers)
  28. {
  29. unique_lock<mutex> lck1(bgnSync.mtx);
  30. ifstream bgnFile(BGN_TMP_FILE);
  31. BGNPublicKey publicKey;
  32. bgnFile >> publicKey;
  33. lck1.unlock();
  34. unique_lock<mutex> lck2(generatorSync.mtx);
  35. ifstream genFile(GEN_TMP_FILE);
  36. vector<Proof> pi;
  37. Twistpoint generator;
  38. size_t sizeOfPi;
  39. genFile >> sizeOfPi;
  40. for (size_t i = 0; i < sizeOfPi; i++)
  41. {
  42. Proof currProof;
  43. genFile >> currProof;
  44. pi.push_back(currProof);
  45. }
  46. genFile >> generator;
  47. return new PrsonaClient(pi, generator, publicKey, numServers);
  48. }
  49. PrsonaClient *create_client(size_t numServers)
  50. {
  51. struct mg_connection *conn = mg_connect_websocket_client(
  52. serverIPs[0].c_str(),
  53. PRSONA_PORT,
  54. USE_SSL,
  55. NULL,
  56. 0,
  57. BGN_URI,
  58. "null",
  59. bgn_websocket_data_handler,
  60. bgn_websocket_close_handler,
  61. NULL);
  62. if (!conn)
  63. {
  64. cerr << "Couldn't obtain BGN details" << endl;
  65. return 1;
  66. }
  67. unique_lock<mutex> lck1(bgnSync.mtx);
  68. remove(BGN_TMP_FILE);
  69. bgnSync.val = 0;
  70. mg_websocket_client_write(
  71. conn,
  72. MG_WEBSOCKET_OPCODE_DATACOMPLETE,
  73. "",
  74. 0);
  75. while (!bgnSync.val)
  76. bgnSync.cv.wait(lck1);
  77. lck1.unlock();
  78. struct mg_connection *conn = mg_connect_websocket_client(
  79. serverIPs[0].c_str(),
  80. PRSONA_PORT,
  81. USE_SSL,
  82. NULL,
  83. 0,
  84. BLIND_GEN_URI,
  85. "null",
  86. blind_gen_websocket_data_handler,
  87. blind_gen_websocket_close_handler,
  88. NULL);
  89. if (!conn)
  90. {
  91. cerr << "Couldn't obtain BGN details" << endl;
  92. return 1;
  93. }
  94. unique_lock<mutex> lck2(generatorSync.mtx);
  95. remove(GEN_TMP_FILE);
  96. generatorSync.val = 0;
  97. mg_websocket_client_write(
  98. conn,
  99. MG_WEBSOCKET_OPCODE_DATACOMPLETE,
  100. "",
  101. 0);
  102. while (!generatorSync.val)
  103. generatorSync.cv.wait(lck2);
  104. lck2.unlock();
  105. return create_client_from_files(numServers);
  106. }
  107. int main(int argc, char *argv[])
  108. {
  109. initialize_prsona_classes();
  110. #if USE_SSL
  111. mg_init_library(0);
  112. #else
  113. mg_init_library(MG_FEATURES_SSL);
  114. #endif
  115. const char *options[] = {"listening_ports", PRSONA_PORT, 0};
  116. vector<string> serverIPs, clientIPs;
  117. string selfIP;
  118. char buffer[40];
  119. ifstream serverConfig("serverIPs.cfg");
  120. while (!serverConfig.eof())
  121. {
  122. serverConfig.getline(buffer, 40);
  123. if (strlen(buffer) > 0)
  124. serverIPs.push_back(string(buffer));
  125. }
  126. ifstream clientConfig("clientIPs.cfg");
  127. while (!clientConfig.eof())
  128. {
  129. clientConfig.getline(buffer, 40);
  130. if (strlen(buffer) > 0)
  131. clientIPs.push_back(string(buffer));
  132. }
  133. ifstream selfConfig("selfIP.cfg");
  134. while (!selfConfig.eof())
  135. {
  136. selfConfig.getline(buffer, 40);
  137. if (strlen(buffer) > 0)
  138. selfIP = buffer;
  139. }
  140. // Defaults
  141. size_t numServers = serverIPs.size();
  142. size_t numClients = clientIPs.size();
  143. bool maliciousServers = true;
  144. if (argc > 1)
  145. {
  146. bool setting = argv[1][0] == 't' || argv[1][0] == 'T';
  147. maliciousServers = setting;
  148. }
  149. cout << "Establishing PRSONA client with the following parameters: " << endl;
  150. cout << numServers << " PRSONA servers" << endl;
  151. cout << numClients << " PRSONA clients" << endl;
  152. cout << "Servers are set to " << (maliciousServers ? "MALICIOUS" : "HBC") << " security" << endl;
  153. cout << "This client is at IP address: " << selfIP << endl;
  154. cout << endl;
  155. // Set malicious flags where necessary
  156. if (maliciousServers)
  157. PrsonaBase::set_server_malicious();
  158. // Entities we operate with
  159. PrsonaClient *prsonaClient = create_client(numServers);
  160. CivetServer server(options);
  161. PrsonaClientWebSocketHandler wsHandler(prsonaClient, &updateMtx, &epochNum, serverIPs, selfIP);
  162. server.addWebSocketHandler("/ws", wsHandler);
  163. ExitHandler exitHandler;
  164. server.addHandler(EXIT_URI, exitHandler);
  165. unique_lock<mutex> lck(exitSync.mtx);
  166. while (!exitSync.val)
  167. exitSync.cv.wait(lck);
  168. mg_exit_library();
  169. delete prsonaClient;
  170. return 0;
  171. }