clients.cpp 8.1 KB

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