serverMain.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /**
  2. * serverMain.cpp
  3. * - compiles to bin/server
  4. * - runs a server program for experiments with PRSONA
  5. *
  6. * Stan Gurtler
  7. */
  8. #include <iostream>
  9. #include <fstream>
  10. #include "networkServer.hpp"
  11. using namespace std;
  12. /**
  13. * This program (bin/server) expects to be called as follows:
  14. * `bin/server <id> <output> <servers_are_malicious>`
  15. *
  16. * <id> - a name for this instance, which comes with a config file identifying
  17. * its IP address and port
  18. * <output> - a string that will name the file in which outputs for this run of
  19. * the program will be written (that is, timings and traffic data)
  20. * <lambda> - a positive integer that determines the absolute soundness parameter
  21. * for batched proofs
  22. * <servers_are_malicious> - a bool (given as T/t or F/f)
  23. * which is true when servers are in malicious security
  24. * and false when they are in HBC security
  25. */
  26. int main(int argc, char *argv[])
  27. {
  28. /*
  29. * PRELIMINARY SETUP CODE
  30. */
  31. initialize_prsona_classes();
  32. #if USE_SSL
  33. mg_init_library(MG_FEATURES_SSL);
  34. #else
  35. mg_init_library(0);
  36. #endif
  37. // The id effectively tells the server what ip/port it is
  38. string id = "";
  39. if (argc > 1)
  40. id = argv[1];
  41. string output = "default";
  42. if (argc > 2)
  43. output = argv[2];
  44. string outputDir = "/home/tmgurtle/prsona/prsona/out/" + output + "/" + id;
  45. int mkdirOut = system(("mkdir -p " + outputDir).c_str());
  46. mutex epochBuildUpOutputMtx;
  47. string epochBuildUpOutputFilename = outputDir + "/epochUp.out";
  48. mutex epochBreakDownOutputMtx;
  49. string epochBreakDownOutputFilename = outputDir + "/epochDown.out";
  50. mutex epochUpdateOutputMtx;
  51. string epochUpdateOutputFilename = outputDir + "/epochUpdate.out";
  52. mutex voteUpdateOutputMtx;
  53. string voteUpdateOutputFilename = outputDir + "/voteUpdate.out";
  54. mutex fullEpochOutputMtx;
  55. string fullEpochOutputFilename = outputDir + "/overallEpoch.out";
  56. mutex memUsageOutputMtx;
  57. string memUsageOutputFilename = outputDir + "/usage.out";
  58. // Default to no proof batching if not specified
  59. size_t lambda = 0;
  60. if (argc > 3)
  61. lambda = atoi(argv[3]);
  62. // Set lambda where necessary
  63. if (lambda > 0)
  64. PrsonaBase::set_lambda(lambda);
  65. // Default to malicious security if not specified
  66. bool maliciousServers = true;
  67. if (argc > 4)
  68. maliciousServers = argv[4][0] == 't' || argv[4][0] == 'T';
  69. // Set malicious flags where necessary
  70. if (maliciousServers)
  71. PrsonaBase::set_server_malicious();
  72. // This seed is used to generate temp filenames.
  73. string seedStr;
  74. if (id.empty())
  75. seedStr = "default-server";
  76. else
  77. {
  78. seedStr = id;
  79. seedStr += "-" + output;
  80. seedStr += "-server";
  81. }
  82. seed_seq seed(seedStr.begin(), seedStr.end());
  83. default_random_engine rng(seed);
  84. vector<string> serverIPs;
  85. vector<int> serverPorts;
  86. string selfIP, selfPortStr, dealerIP, dealerPortStr;
  87. int selfPort = 0, dealerPort = 0;
  88. string configDir = "/home/tmgurtle/prsona/prsona/cfg/" + output;
  89. // Read in from config files the server locations
  90. load_multiple_instances_config(serverIPs, serverPorts, (configDir + "/serverIPs.cfg").c_str());
  91. // Read in the ip/port corresponding to the id that this instance was given
  92. string selfConfigFilename = configDir + "/selfIP";
  93. if (!id.empty())
  94. {
  95. selfConfigFilename += "-";
  96. selfConfigFilename += id;
  97. }
  98. selfConfigFilename += ".cfg";
  99. load_single_instance_config(selfIP, selfPortStr, selfPort, selfConfigFilename.c_str());
  100. // And finally the dealer location
  101. load_single_instance_config(dealerIP, dealerPortStr, dealerPort, (configDir + "/dealerIP.cfg").c_str());
  102. size_t numServers = serverIPs.size();
  103. bool bgnDealer = selfIP == dealerIP && selfPort == dealerPort;
  104. struct synchronization_tool exitSync, readySync;
  105. mutex updateMtx;
  106. unique_lock<mutex> *updateLockHolder = NULL;
  107. atomic<size_t> epochNum(0);
  108. const char *options[] = {"listening_ports", selfPortStr.c_str(), 0};
  109. CivetServer server(options);
  110. /*
  111. * SERVER SETUP CODE
  112. */
  113. cout << "[" << seedStr << "] Establishing PRSONA server with the following parameters: " << endl;
  114. cout << "[" << seedStr << "] " << numServers << " PRSONA servers" << endl;
  115. cout << "[" << seedStr << "] This server " << (bgnDealer ? "IS" : "is NOT") << " the trusted BGN dealer" << endl;
  116. cout << "[" << seedStr << "] " << "Proof batching " << (lambda > 0 ? "IS" : "is NOT") << " in use." << endl;
  117. if (lambda > 0)
  118. cout << "[" << seedStr << "] Batch parameter: " << lambda << endl;
  119. cout << "[" << seedStr << "] Servers are set to " << (maliciousServers ? "MALICIOUS" : "HBC") << " security" << endl;
  120. cout << "[" << seedStr << "] This server is at IP address: " << selfIP << ":" << selfPort << endl;
  121. cout << "[" << seedStr << "] The BGN dealer is at IP address: " << dealerIP << ":" << dealerPort << endl;
  122. cout << endl;
  123. cout << "[" << seedStr << "] Creating PRSONA server object." << endl;
  124. PrsonaServer *prsonaServer = create_server(rng, dealerIP, dealerPort, bgnDealer, numServers);
  125. cout << "[" << seedStr << "] Setting up handlers for server." << endl;
  126. // Main handler
  127. PrsonaServerWebSocketHandler wsHandler(rng, prsonaServer, serverIPs, serverPorts, selfIP, selfPort, updateMtx, epochNum, epochBuildUpOutputMtx, epochBuildUpOutputFilename, epochBreakDownOutputMtx, epochBreakDownOutputFilename, epochUpdateOutputMtx, epochUpdateOutputFilename, voteUpdateOutputMtx, voteUpdateOutputFilename, memUsageOutputMtx, memUsageOutputFilename);
  128. server.addWebSocketHandler("/ws", wsHandler);
  129. // Exit handler (allows server to be brought down by making correct GET request)
  130. unique_lock<mutex> exitLock(exitSync.mtx);
  131. exitSync.val = 0;
  132. exitSync.val2 = 0;
  133. RemoteControlHandler exitHandler(&exitSync, "Server coming down!");
  134. server.addHandler(EXIT_URI, exitHandler);
  135. // Update mutex handler (allows servers to synchronize updates, like epochs and new users)
  136. UpdateLockWebSocketHandler lockHandler(updateMtx, &updateLockHolder, true);
  137. UpdateLockWebSocketHandler unlockHandler(updateMtx, &updateLockHolder, false);
  138. server.addWebSocketHandler(UPDATE_LOCK_URI, lockHandler);
  139. server.addWebSocketHandler(UPDATE_UNLOCK_URI, unlockHandler);
  140. // Epoch number handler (allows a server to indicate what epoch the system is on)
  141. EpochNumHandler epochNumHandler(epochNum);
  142. server.addHandler(WHICH_EPOCH_URI, epochNumHandler);
  143. // This handler (to determine system main loop readiness) is only used by the dealer
  144. RemoteControlHandler serverReadyHandler(&readySync, "ACK");
  145. // Similarly, this handler (to tell the orchestrator when epochs are ready to happen) is only used by the dealer
  146. EpochReadyHandler epochReadyHandler(&exitSync, &readySync, updateMtx, numServers);
  147. // Make-epoch trigger (which causes the dealer to initiate an epoch change)
  148. AltRemoteControlHandler triggerEpochHandler(1, &exitSync, "Server will initiate epoch!");
  149. // It's probably best to give them all a lifetime as long as the CivetWeb server object
  150. if (bgnDealer) // The dealer needs to wait for the other servers to check in, because it needs to lead forming the first fresh generator
  151. {
  152. // The readySync lock is only used here, while we're waiting for other servers to check in
  153. unique_lock<mutex> lck(readySync.mtx);
  154. readySync.val = 1;
  155. server.addHandler(SERVER_CHECK_IN_URI, serverReadyHandler);
  156. // In addition, there are also a few extra handlers only the dealer needs
  157. server.addHandler(TRIGGER_EPOCH_URI, triggerEpochHandler);
  158. server.addHandler(EPOCH_READY_URI, epochReadyHandler);
  159. cout << "[" << seedStr << "] Waiting for other servers to retrieve BGN details and check in." << endl;
  160. // Wait for the other servers to check in
  161. while (readySync.val < numServers)
  162. {
  163. cout << "[" << seedStr << "] " << readySync.val << " servers are checked in." << endl;
  164. readySync.cv.wait(lck);
  165. }
  166. // Once we're ready, form and distribute the first fresh generator and the H used in ElGamal operations
  167. initiate_generators(rng, prsonaServer, serverIPs, serverPorts, selfIP, selfPort);
  168. }
  169. else // Non-dealer servers need to check in with the dealer
  170. {
  171. cout << "[" << seedStr << "] Checking in with the BGN dealer." << endl;
  172. check_in_with_dealer(dealerIP, dealerPort);
  173. }
  174. /*
  175. * MAIN SERVER LOOP CODE
  176. */
  177. cout << "[" << seedStr << "] Entering main ready loop." << endl;
  178. if (bgnDealer)
  179. {
  180. while (!exitSync.val)
  181. {
  182. // exitSync.val controls when an exit is signaled.
  183. // exitSync.val2 controls when an epoch change is signaled
  184. while (!exitSync.val && !exitSync.val2)
  185. exitSync.cv.wait(exitLock);
  186. // exitSync.val2 is set by the make-epoch handler, and will be 0 in the event of signaling an exit
  187. if (exitSync.val2)
  188. {
  189. size_t currEpoch = epochNum.load();
  190. cout << "[" << seedStr << "] Executing epoch calculations (going from t = " << currEpoch << " to " << currEpoch + 1 << ")." << endl;
  191. make_epoch(rng, prsonaServer, serverIPs, serverPorts, selfIP, selfPort, updateMtx, epochNum, server, epochBuildUpOutputMtx, epochBuildUpOutputFilename, epochBreakDownOutputMtx, epochBreakDownOutputFilename, fullEpochOutputMtx, fullEpochOutputFilename, memUsageOutputMtx, memUsageOutputFilename);
  192. currEpoch = epochNum.load();
  193. cout << "[" << seedStr << "] Epoch calculations complete (now in t = " << currEpoch << ")." << endl;
  194. }
  195. // Make sure to reset this, so that we wait until something new is signaled to do
  196. exitSync.val2 = 0;
  197. }
  198. }
  199. else
  200. {
  201. // exitSync.val controls when an exit is signaled.
  202. // exitSync.val2 is not used by non-dealer servers.
  203. while (!exitSync.val)
  204. exitSync.cv.wait(exitLock);
  205. }
  206. /*
  207. * SHUTDOWN CODE
  208. */
  209. cout << "[" << seedStr << "] Shutting down." << endl;
  210. mg_exit_library();
  211. delete prsonaServer;
  212. return 0;
  213. }