appconfig.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #include <iostream>
  2. #include "Untrusted.hpp"
  3. #include "appconfig.hpp"
  4. // The next line suppresses a deprecation warning within boost
  5. #define BOOST_BIND_GLOBAL_PLACEHOLDERS
  6. #include "boost/property_tree/ptree.hpp"
  7. #include "boost/property_tree/json_parser.hpp"
  8. // Split a hostport string like "127.0.0.1:12000" at the rightmost colon
  9. // into a host part "127.0.0.1" and a port part "12000".
  10. static bool split_host_port(std::string &host, std::string &port,
  11. const std::string &hostport)
  12. {
  13. size_t colon = hostport.find_last_of(':');
  14. if (colon == std::string::npos) {
  15. std::cerr << "Cannot parse \"" << hostport << "\" as host:port\n";
  16. return false;
  17. }
  18. host = hostport.substr(0, colon);
  19. port = hostport.substr(colon+1);
  20. return true;
  21. }
  22. // Convert a single hex character into its value from 0 to 15. Return
  23. // true on success, false if it wasn't a hex character.
  24. static inline bool hextoval(unsigned char &val, char hex)
  25. {
  26. if (hex >= '0' && hex <= '9') {
  27. val = ((unsigned char)hex)-'0';
  28. } else if (hex >= 'a' && hex <= 'f') {
  29. val = ((unsigned char)hex)-'a'+10;
  30. } else if (hex >= 'A' && hex <= 'F') {
  31. val = ((unsigned char)hex)-'A'+10;
  32. } else {
  33. return false;
  34. }
  35. return true;
  36. }
  37. // Convert a 2*len hex character string into a len-byte buffer. Return
  38. // true on success, false on failure.
  39. static bool hextobuf(unsigned char *buf, const char *str, size_t len)
  40. {
  41. if (strlen(str) != 2*len) {
  42. std::cerr << "Hex string was not the expected size\n";
  43. return false;
  44. }
  45. for (size_t i=0;i<len;++i) {
  46. unsigned char hi, lo;
  47. if (!hextoval(hi, str[2*i]) || !hextoval(lo, str[2*i+1])) {
  48. std::cerr << "Cannot parse string as hex\n";
  49. return false;
  50. }
  51. buf[i] = (unsigned char)((hi << 4) + lo);
  52. }
  53. return true;
  54. }
  55. bool config_parse(Config &config, const std::string configstr,
  56. const std::string &myname)
  57. {
  58. bool found_my_node = false;
  59. bool found_params = false;
  60. bool ret = true;
  61. std::istringstream configstream(configstr);
  62. boost::property_tree::ptree conftree;
  63. read_json(configstream, conftree);
  64. for (auto & entry : conftree) {
  65. if (!entry.first.compare("params")) {
  66. for (auto & pentry : entry.second) {
  67. if (!pentry.first.compare("msgsize")) {
  68. config.msgsize = pentry.second.get_value<uint16_t>();
  69. } else {
  70. std::cerr << "Unknown field in params: " <<
  71. pentry.first << "\n";
  72. ret = false;
  73. }
  74. }
  75. found_params = true;
  76. } else if (!entry.first.compare("nodes")) {
  77. for (auto & node : entry.second) {
  78. NodeConfig nc;
  79. nc.weight = 1; // default
  80. for (auto & nentry : node.second) {
  81. if (!nentry.first.compare("name")) {
  82. nc.name = nentry.second.get_value<std::string>();
  83. if (!myname.compare(nc.name)) {
  84. config.my_node_num =
  85. nodenum_t(config.nodes.size());
  86. found_my_node = true;
  87. }
  88. } else if (!nentry.first.compare("pubkey")) {
  89. ret &= hextobuf((unsigned char *)&nc.pubkey,
  90. nentry.second.get_value<std::string>().c_str(),
  91. sizeof(nc.pubkey));
  92. } else if (!nentry.first.compare("weight")) {
  93. nc.weight = nentry.second.get_value<std::uint8_t>();
  94. } else if (!nentry.first.compare("listen")) {
  95. ret &= split_host_port(nc.listenhost, nc.listenport,
  96. nentry.second.get_value<std::string>());
  97. } else if (!nentry.first.compare("clisten")) {
  98. ret &= split_host_port(nc.clistenhost, nc.clistenport,
  99. nentry.second.get_value<std::string>());
  100. } else {
  101. std::cerr << "Unknown field in host config: " <<
  102. nentry.first << "\n";
  103. ret = false;
  104. }
  105. }
  106. config.nodes.push_back(std::move(nc));
  107. }
  108. } else {
  109. std::cerr << "Unknown key in config: " <<
  110. entry.first << "\n";
  111. ret = false;
  112. }
  113. }
  114. if (!found_params) {
  115. std::cerr << "Could not find params in config\n";
  116. ret = false;
  117. }
  118. if (!found_my_node) {
  119. std::cerr << "Could not find my own node entry in config\n";
  120. ret = false;
  121. }
  122. if (!ret) return ret;
  123. // Now load the config into the enclave
  124. EnclaveAPIParams apiparams;
  125. apiparams.msgsize = config.msgsize;
  126. nodenum_t num_nodes = (nodenum_t)(config.nodes.size());
  127. std::vector<EnclaveAPINodeConfig> apinodeconfigs;
  128. apinodeconfigs.resize(num_nodes);
  129. for (nodenum_t i=0; i<num_nodes; ++i) {
  130. memmove(&apinodeconfigs[i].pubkey,
  131. &config.nodes[i].pubkey, sizeof(apinodeconfigs[i].pubkey));
  132. apinodeconfigs[i].weight = config.nodes[i].weight;
  133. }
  134. ret &= ecall_config_load(&apiparams, apinodeconfigs.data(),
  135. num_nodes, config.my_node_num);
  136. if (!ret) {
  137. std::cerr << "Loading config into enclave failed\n";
  138. }
  139. return ret;
  140. }