clientMain.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /**
  2. * clientMain.cpp
  3. * - compiles to bin/client
  4. * - runs a client program for experiments with PRSONA
  5. *
  6. * Stan Gurtler
  7. */
  8. #include <iostream>
  9. #include <fstream>
  10. #include "networkClient.hpp"
  11. using namespace std;
  12. /**
  13. * This program (bin/client) expects to be called as follows:
  14. * `bin/client <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 client 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 = "out/" + output + "/" + id;
  45. int mkdirOut = system(("mkdir -p " + outputDir).c_str());
  46. mutex voteOutputMtx;
  47. string voteOutputFilename = outputDir + "/vote.out";
  48. mutex repProofOutputMtx;
  49. string repProofOutputFilename = outputDir + "/repProver.out";
  50. mutex repVerifyOutputMtx;
  51. string repVerifyOutputFilename = outputDir + "/repVerifier.out";
  52. mutex memUsageOutputMtx;
  53. string memUsageOutputFilename = outputDir + "/usage.out";
  54. // Default to not doing proof batching if not specified
  55. size_t lambda = 0;
  56. if (argc > 3)
  57. lambda = atoi(argv[3]);
  58. if (lambda > 0)
  59. PrsonaBase::set_lambda(lambda);
  60. // Default to malicious security if not specified
  61. bool maliciousServers = true;
  62. if (argc > 4)
  63. maliciousServers = argv[4][0] == 't' || argv[4][0] == 'T';
  64. // Set malicious flags where necessary
  65. if (maliciousServers)
  66. PrsonaBase::set_server_malicious();
  67. // This seed is used to generate temp filenames and decide which server to make requests to at any step.
  68. string seedStr;
  69. if (id.empty())
  70. seedStr = "default-client";
  71. else
  72. {
  73. seedStr = id;
  74. seedStr += "-" + output;
  75. seedStr += "-client";
  76. }
  77. seed_seq seed(seedStr.begin(), seedStr.end());
  78. default_random_engine rng(seed);
  79. vector<string> serverIPs, clientIPs;
  80. vector<int> serverPorts, clientPorts;
  81. string selfIP, selfPortStr;
  82. int selfPort = 0;
  83. string configDir = "cfg/" + output;
  84. // Read in from config files the server locations
  85. load_multiple_instances_config(serverIPs, serverPorts, (configDir + "/serverIPs.cfg").c_str());
  86. // And now the client locations
  87. load_multiple_instances_config(clientIPs, clientPorts, (configDir + "/clientIPs.cfg").c_str());
  88. // Finally, read in the ip/port corresponding to the id that this instance was given
  89. string selfConfigFilename = configDir + "/selfIP";
  90. if (!id.empty())
  91. {
  92. selfConfigFilename += "-";
  93. selfConfigFilename += id;
  94. }
  95. selfConfigFilename += ".cfg";
  96. load_single_instance_config(selfIP, selfPortStr, selfPort, selfConfigFilename.c_str());
  97. size_t numServers = serverIPs.size();
  98. size_t numClients = clientIPs.size();
  99. uniform_int_distribution<size_t> distribution(0, numServers - 1);
  100. const char *options[] = {"listening_ports", selfPortStr.c_str(), 0};
  101. CivetServer server(options);
  102. /*
  103. * CLIENT SETUP CODE
  104. */
  105. struct synchronization_tool exitSync;
  106. unique_lock<mutex> exitLock(exitSync.mtx);
  107. cout << "[" << seedStr << "] Establishing PRSONA client with the following parameters: " << endl;
  108. cout << "[" << seedStr << "] " << numServers << " PRSONA servers" << endl;
  109. cout << "[" << seedStr << "] " << numClients << " PRSONA clients" << endl;
  110. cout << "[" << seedStr << "] " << "Proof batching " << (lambda > 0 ? "IS" : "is NOT") << " in use." << endl;
  111. if (lambda > 0)
  112. cout << "[" << seedStr << "] Batch parameter: " << lambda << endl;
  113. cout << "[" << seedStr << "] Servers are set to " << (maliciousServers ? "MALICIOUS" : "HBC") << " security" << endl;
  114. cout << "[" << seedStr << "] This client is at IP address: " << selfIP << ":" << selfPort << endl;
  115. cout << endl;
  116. cout << "[" << seedStr << "] Creating PRSONA client object." << endl;
  117. PrsonaClient *prsonaClient = create_client(rng, serverIPs, serverPorts, numServers);
  118. cout << "[" << seedStr << "] Setting up handlers for client." << endl;
  119. // Main handler (in clients, only used for verifying reputation proofs)
  120. PrsonaClientWebSocketHandler wsHandler(rng, prsonaClient, serverIPs, serverPorts, repVerifyOutputMtx, repVerifyOutputFilename, memUsageOutputMtx, memUsageOutputFilename);
  121. server.addWebSocketHandler("/ws", wsHandler);
  122. // Exit handler (allows client to be brought down by making correct GET request)
  123. exitSync.val = 0;
  124. exitSync.val2 = 0;
  125. RemoteControlHandler exitHandler(&exitSync, "Client coming down!");
  126. server.addHandler(EXIT_URI, exitHandler);
  127. // This handler tells the orchestrator when the client has finished an assigned task
  128. ClientReadyHandler clientReadyHandler(&exitSync);
  129. server.addHandler(CLIENT_READY_URI, clientReadyHandler);
  130. // Make-vote handler (allows orchestrator to cause this client to make a new, random vote)
  131. AltRemoteControlHandler triggerVoteHandler(CLIENT_MAKE_VOTE, &exitSync, "Client will make new votes!");
  132. server.addHandler(TRIGGER_VOTE_URI, triggerVoteHandler);
  133. // Make-reputation handler (allows orchestrator to cause this client to make a new reputation proof, and specifies which other client to give it to)
  134. AltRemoteControlHandler triggerRepHandler(CLIENT_MAKE_REP_PROOF, &exitSync, "Client will make reputation proof!");
  135. server.addHandler(TRIGGER_REP_URI, triggerRepHandler);
  136. /*
  137. * MAIN CLIENT LOOP CODE
  138. */
  139. cout << "[" << seedStr << "] Entering main ready loop." << endl;
  140. while (!exitSync.val)
  141. {
  142. // exitSync.val controls when an exit is signaled.
  143. // exitSync.val2 controls when making a vote or making a reputation proof is signaled
  144. while (!exitSync.val && !exitSync.val2)
  145. exitSync.cv.wait(exitLock);
  146. size_t whichServer;
  147. string fullQuery, target;
  148. size_t colonLocation;
  149. int targetPort;
  150. // exitSync.val2 is set by the make-vote and make-reputation handlers, but will be 0 in the event of signaling an exit
  151. switch (exitSync.val2)
  152. {
  153. case CLIENT_MAKE_VOTE:
  154. cout << "[" << seedStr << "] Making new vote row." << endl;
  155. whichServer = distribution(rng);
  156. make_vote(rng, prsonaClient, serverIPs, serverPorts, serverIPs[whichServer], serverPorts[whichServer], numClients, server, voteOutputMtx, voteOutputFilename, memUsageOutputMtx, memUsageOutputFilename);
  157. cout << "[" << seedStr << "] New vote row complete." << endl;
  158. break;
  159. case CLIENT_MAKE_REP_PROOF:
  160. fullQuery = triggerRepHandler.getQuery();
  161. colonLocation = fullQuery.find(":");
  162. target = fullQuery.substr(0, colonLocation);
  163. targetPort = stoi(fullQuery.substr(colonLocation + 1));
  164. cout << "[" << seedStr << "] Making new reputation proof." << endl;
  165. make_reputation_proof(rng, prsonaClient, serverIPs, serverPorts, target, targetPort, numClients, server, repProofOutputMtx, repProofOutputFilename, memUsageOutputMtx, memUsageOutputFilename);
  166. cout << "[" << seedStr << "] New reputation proof complete." << endl;
  167. break;
  168. default: // Occurs when an exit is signaled; do nothing
  169. break;
  170. }
  171. // Make sure to reset this, so that we wait until something new is signaled to do
  172. exitSync.val2 = 0;
  173. }
  174. /*
  175. * SHUTDOWN CODE
  176. */
  177. cout << "[" << seedStr << "] Shutting down." << endl;
  178. mg_exit_library();
  179. delete prsonaClient;
  180. return 0;
  181. }