clientMain.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. * <servers_are_malicious> - a bool (given as T/t or F/f)
  21. * which is true when servers are in malicious security
  22. * and false when they are in HBC security
  23. */
  24. int main(int argc, char *argv[])
  25. {
  26. /*
  27. * PRELIMINARY SETUP CODE
  28. */
  29. initialize_prsona_classes();
  30. #if USE_SSL
  31. mg_init_library(MG_FEATURES_SSL);
  32. #else
  33. mg_init_library(0);
  34. #endif
  35. // The id effectively tells the client what ip/port it is
  36. string id = "";
  37. if (argc > 1)
  38. id = argv[1];
  39. string output = "default";
  40. if (argc > 2)
  41. output = argv[2];
  42. string outputDir = "out/" + output + "/" + id;
  43. int mkdirOut = system(("mkdir -p " + outputDir).c_str());
  44. mutex voteOutputMtx;
  45. string voteOutputFilename = outputDir + "/vote.out";
  46. mutex repProofOutputMtx;
  47. string repProofOutputFilename = outputDir + "/repProver.out";
  48. mutex repVerifyOutputMtx;
  49. string repVerifyOutputFilename = outputDir + "/repVerifier.out";
  50. // Default to malicious security if not specified
  51. bool maliciousServers = true;
  52. if (argc > 3)
  53. {
  54. bool setting = argv[3][0] == 't' || argv[3][0] == 'T';
  55. maliciousServers = setting;
  56. }
  57. // Set malicious flags where necessary
  58. if (maliciousServers)
  59. PrsonaBase::set_server_malicious();
  60. // This seed is used to generate temp filenames and decide which server to make requests to at any step.
  61. string seedStr;
  62. if (id.empty())
  63. seedStr = "default-client";
  64. else
  65. {
  66. seedStr = id;
  67. seedStr += "-client";
  68. }
  69. seed_seq seed(seedStr.begin(), seedStr.end());
  70. default_random_engine rng(seed);
  71. vector<string> serverIPs, clientIPs;
  72. vector<int> serverPorts, clientPorts;
  73. string selfIP, selfPortStr;
  74. int selfPort = 0;
  75. // Read in from config files the server locations
  76. load_multiple_instances_config(serverIPs, serverPorts, "cfg/serverIPs.cfg");
  77. // And now the client locations
  78. load_multiple_instances_config(clientIPs, clientPorts, "cfg/clientIPs.cfg");
  79. // Finally, read in the ip/port corresponding to the id that this instance was given
  80. string selfConfigFilename = "cfg/selfIP";
  81. if (!id.empty())
  82. {
  83. selfConfigFilename += "-";
  84. selfConfigFilename += id;
  85. }
  86. selfConfigFilename += ".cfg";
  87. load_single_instance_config(selfIP, selfPortStr, selfPort, selfConfigFilename.c_str());
  88. size_t numServers = serverIPs.size();
  89. size_t numClients = clientIPs.size();
  90. uniform_int_distribution<size_t> distribution(0, numServers - 1);
  91. const char *options[] = {"listening_ports", selfPortStr.c_str(), 0};
  92. CivetServer server(options);
  93. /*
  94. * CLIENT SETUP CODE
  95. */
  96. cout << "[" << seedStr << "] Establishing PRSONA client with the following parameters: " << endl;
  97. cout << "[" << seedStr << "] " << numServers << " PRSONA servers" << endl;
  98. cout << "[" << seedStr << "] " << numClients << " PRSONA clients" << endl;
  99. cout << "[" << seedStr << "] Servers are set to " << (maliciousServers ? "MALICIOUS" : "HBC") << " security" << endl;
  100. cout << "[" << seedStr << "] This client is at IP address: " << selfIP << ":" << selfPort << endl;
  101. cout << endl;
  102. cout << "[" << seedStr << "] Creating PRSONA client object." << endl;
  103. PrsonaClient *prsonaClient = create_client(rng, serverIPs, serverPorts, numServers);
  104. cout << "[" << seedStr << "] Setting up handlers for client." << endl;
  105. // Main handler (in clients, only used for verifying reputation proofs)
  106. PrsonaClientWebSocketHandler wsHandler(rng, prsonaClient, serverIPs, serverPorts, repVerifyOutputMtx, repVerifyOutputFilename);
  107. server.addWebSocketHandler("/ws", wsHandler);
  108. // Exit handler (allows client to be brought down by making correct GET request)
  109. struct synchronization_tool exitSync;
  110. unique_lock<mutex> exitLock(exitSync.mtx);
  111. exitSync.val = 0;
  112. exitSync.val2 = 0;
  113. RemoteControlHandler exitHandler(&exitSync, "Client coming down!");
  114. server.addHandler(EXIT_URI, exitHandler);
  115. // Make-vote handler (allows orchestrator to cause this client to make a new, random vote)
  116. AltRemoteControlHandler triggerVoteHandler(CLIENT_MAKE_VOTE, &exitSync, "Client will make new votes!");
  117. server.addHandler(TRIGGER_VOTE_URI, triggerVoteHandler);
  118. // Make-reputation handler (allows orchestrator to cause this client to make a new reputation proof, and specifies which other client to give it to)
  119. AltRemoteControlHandler triggerRepHandler(CLIENT_MAKE_REP_PROOF, &exitSync, "Client will make reputation proof!");
  120. server.addHandler(TRIGGER_REP_URI, triggerRepHandler);
  121. /*
  122. * MAIN CLIENT LOOP CODE
  123. */
  124. cout << "[" << seedStr << "] Entering main ready loop." << endl;
  125. while (!exitSync.val)
  126. {
  127. // exitSync.val controls when an exit is signaled.
  128. // exitSync.val2 controls when making a vote or making a reputation proof is signaled
  129. while (!exitSync.val && !exitSync.val2)
  130. exitSync.cv.wait(exitLock);
  131. size_t whichServer;
  132. string fullQuery, target;
  133. size_t colonLocation;
  134. int targetPort;
  135. // exitSync.val2 is set by the make-vote and make-reputation handlers, but will be 0 in the event of signaling an exit
  136. switch (exitSync.val2)
  137. {
  138. case CLIENT_MAKE_VOTE:
  139. cout << "[" << seedStr << "] Making new vote row." << endl;
  140. whichServer = distribution(rng);
  141. make_vote(rng, prsonaClient, serverIPs, serverPorts, serverIPs[whichServer], serverPorts[whichServer], numClients, server, voteOutputMtx, voteOutputFilename);
  142. cout << "[" << seedStr << "] New vote row complete." << endl;
  143. break;
  144. case CLIENT_MAKE_REP_PROOF:
  145. fullQuery = triggerRepHandler.getQuery();
  146. colonLocation = fullQuery.find(":");
  147. target = fullQuery.substr(0, colonLocation);
  148. targetPort = stoi(fullQuery.substr(colonLocation + 1));
  149. cout << "[" << seedStr << "] Making new reputation proof." << endl;
  150. make_reputation_proof(rng, prsonaClient, serverIPs, serverPorts, target, targetPort, numClients, server, repProofOutputMtx, repProofOutputFilename);
  151. cout << "[" << seedStr << "] New reputation proof complete." << endl;
  152. break;
  153. default: // Occurs when an exit is signaled; do nothing
  154. break;
  155. }
  156. // Make sure to reset this, so that we wait until something new is signaled to do
  157. exitSync.val2 = 0;
  158. }
  159. /*
  160. * SHUTDOWN CODE
  161. */
  162. cout << "[" << seedStr << "] Shutting down." << endl;
  163. mg_exit_library();
  164. delete prsonaClient;
  165. return 0;
  166. }