mktopology.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <string>
  5. #include <iostream>
  6. #include <map>
  7. #include "../osdep/OSUtils.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. Identity topologyAuthority;
  16. if (OSUtils::readFile("root-topology-authority.secret",buf))
  17. topologyAuthority.fromString(buf);
  18. else std::cerr << "Warning: root-topology-authority.secret not found, creating unsigned topology." << std::endl;
  19. Dictionary topology;
  20. // Read template.dict to populate default fields in root topology
  21. // if this file exists. Otherwise we just start empty.
  22. buf.clear();
  23. if (OSUtils::readFile("template.dict",buf))
  24. topology.fromString(buf);
  25. // Read all entries in rootservers/ that correspond to rootserver entry dictionaries
  26. // and add them to topology under rootservers/ subkey.
  27. Dictionary rootservers;
  28. std::vector<std::string> rootserverDictionaries(OSUtils::listDirectory("rootservers"));
  29. for(std::vector<std::string>::const_iterator sn(rootserverDictionaries.begin());sn!=rootserverDictionaries.end();++sn) {
  30. if (sn->length() == 10) {
  31. buf.clear();
  32. if (!OSUtils::readFile((std::string("rootservers/")+(*sn)).c_str(),buf)) {
  33. std::cerr << "Cannot read rootservers/" << *sn << std::endl;
  34. return 1;
  35. }
  36. rootservers[*sn] = buf;
  37. }
  38. }
  39. topology["rootservers"] = rootservers.toString();
  40. if ((topologyAuthority)&&(topologyAuthority.hasPrivate())) {
  41. // Sign topology with root-topology-authority.secret
  42. if (!topology.sign(topologyAuthority,OSUtils::now())) {
  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. }
  53. // Output to stdout
  54. std::cout << topology.toString();
  55. return 0;
  56. }