clients.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #include <iostream>
  2. #include "../App/appconfig.hpp"
  3. // The next line suppresses a deprecation warning within boost
  4. #define BOOST_BIND_GLOBAL_PLACEHOLDERS
  5. #include "boost/property_tree/ptree.hpp"
  6. #include "boost/property_tree/json_parser.hpp"
  7. // Split a hostport string like "127.0.0.1:12000" at the rightmost colon
  8. // into a host part "127.0.0.1" and a port part "12000".
  9. static bool split_host_port(std::string &host, std::string &port,
  10. const std::string &hostport)
  11. {
  12. size_t colon = hostport.find_last_of(':');
  13. if (colon == std::string::npos) {
  14. std::cerr << "Cannot parse \"" << hostport << "\" as host:port\n";
  15. return false;
  16. }
  17. host = hostport.substr(0, colon);
  18. port = hostport.substr(colon+1);
  19. return true;
  20. }
  21. // Convert a single hex character into its value from 0 to 15. Return
  22. // true on success, false if it wasn't a hex character.
  23. static inline bool hextoval(unsigned char &val, char hex)
  24. {
  25. if (hex >= '0' && hex <= '9') {
  26. val = ((unsigned char)hex)-'0';
  27. } else if (hex >= 'a' && hex <= 'f') {
  28. val = ((unsigned char)hex)-'a'+10;
  29. } else if (hex >= 'A' && hex <= 'F') {
  30. val = ((unsigned char)hex)-'A'+10;
  31. } else {
  32. return false;
  33. }
  34. return true;
  35. }
  36. // Convert a 2*len hex character string into a len-byte buffer. Return
  37. // true on success, false on failure.
  38. static bool hextobuf(unsigned char *buf, const char *str, size_t len)
  39. {
  40. if (strlen(str) != 2*len) {
  41. std::cerr << "Hex string was not the expected size\n";
  42. return false;
  43. }
  44. for (size_t i=0;i<len;++i) {
  45. unsigned char hi, lo;
  46. if (!hextoval(hi, str[2*i]) || !hextoval(lo, str[2*i+1])) {
  47. std::cerr << "Cannot parse string as hex\n";
  48. return false;
  49. }
  50. buf[i] = (unsigned char)((hi << 4) + lo);
  51. }
  52. return true;
  53. }
  54. bool config_parse(Config &config, const std::string configstr,
  55. std::vector<NodeConfig> &ingestion_nodes,
  56. std::vector<NodeConfig> &storage_nodes)
  57. {
  58. bool found_params = false;
  59. bool ret = true;
  60. std::istringstream configstream(configstr);
  61. boost::property_tree::ptree conftree;
  62. read_json(configstream, conftree);
  63. for (auto & entry : conftree) {
  64. if (!entry.first.compare("params")) {
  65. for (auto & pentry : entry.second) {
  66. if (!pentry.first.compare("msg_size")) {
  67. config.msg_size = pentry.second.get_value<uint16_t>();
  68. } else if (!pentry.first.compare("user_count")) {
  69. config.user_count = pentry.second.get_value<uint32_t>();
  70. } else if (!pentry.first.compare("priv_out")) {
  71. config.m_priv_out = pentry.second.get_value<uint8_t>();
  72. } else if (!pentry.first.compare("priv_in")) {
  73. config.m_priv_in = pentry.second.get_value<uint8_t>();
  74. } else if (!pentry.first.compare("pub_out")) {
  75. config.m_pub_out = pentry.second.get_value<uint8_t>();
  76. } else if (!pentry.first.compare("pub_in")) {
  77. config.m_pub_in = pentry.second.get_value<uint8_t>();
  78. // Currently hardcoding an AES key for client -> server communication
  79. } else if (!pentry.first.compare("client_aes_key")) {
  80. std::string hex_key = pentry.second.data();
  81. memcpy(config.client_aes_key, hex_key.c_str(), SGX_AESGCM_KEY_SIZE);
  82. } else {
  83. std::cerr << "Unknown field in params: " <<
  84. pentry.first << "\n";
  85. ret = false;
  86. }
  87. }
  88. found_params = true;
  89. } else if (!entry.first.compare("nodes")) {
  90. for (auto & node : entry.second) {
  91. NodeConfig nc;
  92. // All nodes need to be assigned their role in manifest.yaml
  93. nc.roles = 0;
  94. for (auto & nentry : node.second) {
  95. if (!nentry.first.compare("name")) {
  96. nc.name = nentry.second.get_value<std::string>();
  97. } else if (!nentry.first.compare("pubkey")) {
  98. ret &= hextobuf((unsigned char *)&nc.pubkey,
  99. nentry.second.get_value<std::string>().c_str(),
  100. sizeof(nc.pubkey));
  101. } else if (!nentry.first.compare("weight")) {
  102. nc.weight = nentry.second.get_value<std::uint8_t>();
  103. } else if (!nentry.first.compare("listen")) {
  104. ret &= split_host_port(nc.listenhost, nc.listenport,
  105. nentry.second.get_value<std::string>());
  106. } else if (!nentry.first.compare("clisten")) {
  107. ret &= split_host_port(nc.clistenhost, nc.clistenport,
  108. nentry.second.get_value<std::string>());
  109. } else if (!nentry.first.compare("role")) {
  110. nc.roles = nentry.second.get_value<std::uint8_t>();
  111. } else {
  112. std::cerr << "Unknown field in host config: " <<
  113. nentry.first << "\n";
  114. ret = false;
  115. }
  116. }
  117. if(nc.roles == ROLE_INGESTION) {
  118. ingestion_nodes.push_back(std::move(nc));
  119. } else if(nc.roles == ROLE_STORAGE) {
  120. storage_nodes.push_back(std::move(nc));
  121. }
  122. }
  123. } else {
  124. std::cerr << "Unknown key in config: " <<
  125. entry.first << "\n";
  126. ret = false;
  127. }
  128. }
  129. if (!found_params) {
  130. std::cerr << "Could not find params in config\n";
  131. ret = false;
  132. }
  133. return ret;
  134. }
  135. static void usage(const char *argv0)
  136. {
  137. fprintf(stderr, "%s [-t nthreads] < config.json\n",
  138. argv0);
  139. exit(1);
  140. }
  141. int main(int argc, char **argv)
  142. {
  143. // Unbuffer stdout
  144. setbuf(stdout, NULL);
  145. uint16_t nthreads = 1;
  146. const char *progname = argv[0];
  147. std::vector<NodeConfig> ingestion_nodes, storage_nodes;
  148. ++argv;
  149. // Parse options
  150. while (*argv && (*argv)[0] == '-') {
  151. if (!strcmp(*argv, "-t")) {
  152. if (argv[1] == NULL) {
  153. usage(progname);
  154. }
  155. nthreads = uint16_t(atoi(argv[1]));
  156. argv += 2;
  157. } else {
  158. usage(progname);
  159. }
  160. }
  161. // Read the config.json from the first line of stdin. We have to do
  162. // this before outputting anything to avoid potential deadlock with
  163. // the launch program.
  164. std::string configstr;
  165. std::getline(std::cin, configstr);
  166. Config config;
  167. if (!config_parse(config, configstr, ingestion_nodes, storage_nodes)) {
  168. exit(1);
  169. }
  170. printf("Number of ingestion_nodes = %ld, Number of storage_node = %ld\n",
  171. ingestion_nodes.size(), storage_nodes.size());
  172. /*
  173. Spin config.user_client actual clients. Each client:
  174. 1) Retrieve messages and tokens from their storage server
  175. 2) Send all their messages to the ingestion server
  176. 3) Wait for a predetermined EPOCH_DURATION time
  177. 4) Repeat from 1)
  178. */
  179. }