mktopology.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <string>
  5. #include <iostream>
  6. #include <map>
  7. #include "../node/Utils.hpp"
  8. #include "../node/Identity.hpp"
  9. #include "../node/Dictionary.hpp"
  10. using namespace ZeroTier;
  11. int main(int argc,char **argv)
  12. {
  13. std::string buf;
  14. // Read root-topology-authority.secret signing authority, must be symlinked and online
  15. if (!Utils::readFile("root-topology-authority.secret",buf)) {
  16. std::cerr << "Cannot read root-topology-authority.secret" << std::endl;
  17. return 1;
  18. }
  19. Identity topologyAuthority(buf);
  20. Dictionary topology;
  21. // Read template.dict to populate default fields in root topology
  22. // if this file exists. Otherwise we just start empty.
  23. buf.clear();
  24. if (Utils::readFile("template.dict",buf))
  25. topology.fromString(buf);
  26. // Read all entries in supernodes/ that correspond to supernode entry dictionaries
  27. // and add them to topology under supernodes/ subkey.
  28. Dictionary supernodes;
  29. std::map<std::string,bool> supernodeDictionaries(Utils::listDirectory("supernodes"));
  30. for(std::map<std::string,bool>::iterator sn(supernodeDictionaries.begin());sn!=supernodeDictionaries.end();++sn) {
  31. if ((sn->first.length() == 10)&&(!sn->second)) {
  32. buf.clear();
  33. if (!Utils::readFile((std::string("supernodes/")+sn->first).c_str(),buf)) {
  34. std::cerr << "Cannot read supernodes/" << sn->first << std::endl;
  35. return 1;
  36. }
  37. supernodes[sn->first] = buf;
  38. }
  39. }
  40. topology["supernodes"] = supernodes.toString();
  41. // Sign topology with root-topology-authority.secret
  42. if (!topology.sign(topologyAuthority)) {
  43. std::cerr << "Unable to sign!" << std::endl;
  44. return 1;
  45. }
  46. // Test signature to make sure signing worked
  47. Dictionary test(topology.toString());
  48. if (!test.verify(topologyAuthority)) {
  49. std::cerr << "Test verification of signed dictionary failed!" << std::endl;
  50. return 1;
  51. }
  52. // Output to stdout
  53. std::cout << topology.toString();
  54. return 0;
  55. }