EmbeddedNetworkController.hpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2015 ZeroTier, Inc.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #ifndef ZT_SQLITENETWORKCONTROLLER_HPP
  19. #define ZT_SQLITENETWORKCONTROLLER_HPP
  20. #include <stdint.h>
  21. #include <string>
  22. #include <map>
  23. #include <vector>
  24. #include <set>
  25. #include "../node/Constants.hpp"
  26. #include "../node/NetworkController.hpp"
  27. #include "../node/Mutex.hpp"
  28. #include "../node/Utils.hpp"
  29. #include "../node/InetAddress.hpp"
  30. #include "../osdep/OSUtils.hpp"
  31. #include "../ext/json/json.hpp"
  32. namespace ZeroTier {
  33. class Node;
  34. class EmbeddedNetworkController : public NetworkController
  35. {
  36. public:
  37. EmbeddedNetworkController(Node *node,const char *dbPath);
  38. virtual ~EmbeddedNetworkController();
  39. virtual NetworkController::ResultCode doNetworkConfigRequest(
  40. const InetAddress &fromAddr,
  41. const Identity &signingId,
  42. const Identity &identity,
  43. uint64_t nwid,
  44. const Dictionary<ZT_NETWORKCONFIG_METADATA_DICT_CAPACITY> &metaData,
  45. NetworkConfig &nc);
  46. unsigned int handleControlPlaneHttpGET(
  47. const std::vector<std::string> &path,
  48. const std::map<std::string,std::string> &urlArgs,
  49. const std::map<std::string,std::string> &headers,
  50. const std::string &body,
  51. std::string &responseBody,
  52. std::string &responseContentType);
  53. unsigned int handleControlPlaneHttpPOST(
  54. const std::vector<std::string> &path,
  55. const std::map<std::string,std::string> &urlArgs,
  56. const std::map<std::string,std::string> &headers,
  57. const std::string &body,
  58. std::string &responseBody,
  59. std::string &responseContentType);
  60. unsigned int handleControlPlaneHttpDELETE(
  61. const std::vector<std::string> &path,
  62. const std::map<std::string,std::string> &urlArgs,
  63. const std::map<std::string,std::string> &headers,
  64. const std::string &body,
  65. std::string &responseBody,
  66. std::string &responseContentType);
  67. private:
  68. static void _circuitTestCallback(ZT_Node *node,ZT_CircuitTest *test,const ZT_CircuitTestReport *report);
  69. inline nlohmann::json _readJson(const std::string &path)
  70. {
  71. std::string buf;
  72. if (OSUtils::readFile(path.c_str(),buf)) {
  73. try {
  74. return nlohmann::json::parse(buf);
  75. } catch ( ... ) {}
  76. }
  77. return nlohmann::json::object();
  78. }
  79. inline bool _writeJson(const std::string &path,const nlohmann::json &obj)
  80. {
  81. std::string buf(obj.dump(2));
  82. return OSUtils::writeFile(path.c_str(),buf);
  83. }
  84. inline std::string _networkBP(const uint64_t nwid,bool create)
  85. {
  86. char tmp[64];
  87. Utils::snprintf(tmp,sizeof(tmp),"%.16llx",nwid);
  88. std::string p(_path + ZT_PATH_SEPARATOR_S + "network");
  89. if (create) OSUtils::mkdir(p.c_str());
  90. p.push_back(ZT_PATH_SEPARATOR);
  91. p.append(tmp);
  92. if (create) OSUtils::mkdir(p.c_str());
  93. return p;
  94. }
  95. inline std::string _networkJP(const uint64_t nwid,bool create)
  96. {
  97. return (_networkBP(nwid,create) + ZT_PATH_SEPARATOR + "config.json");
  98. }
  99. inline std::string _memberBP(const uint64_t nwid,const Address &member,bool create)
  100. {
  101. std::string p(_networkBP(nwid,create));
  102. p.push_back(ZT_PATH_SEPARATOR);
  103. p.append("member");
  104. if (create) OSUtils::mkdir(p.c_str());
  105. p.push_back(ZT_PATH_SEPARATOR);
  106. p.append(member.toString());
  107. if (create) OSUtils::mkdir(p.c_str());
  108. return p;
  109. }
  110. inline std::string _memberJP(const uint64_t nwid,const Address &member,bool create)
  111. {
  112. return (_memberBP(nwid,member,create) + ZT_PATH_SEPARATOR + "config.json");
  113. }
  114. inline std::set<InetAddress> _getAlreadyAllocatedIps(const uint64_t nwid)
  115. {
  116. std::set<InetAddress> ips;
  117. std::string bp(_networkBP(nwid,false) + ZT_PATH_SEPARATOR_S + "member");
  118. std::vector<std::string> members(OSUtils::listSubdirectories(bp.c_str()));
  119. for(std::vector<std::string>::iterator m(members.begin());m!=members.end();++m) {
  120. if (m->length() == ZT_ADDRESS_LENGTH_HEX) {
  121. nlohmann::json mj = _readJson(bp + ZT_PATH_SEPARATOR_S + *m + ZT_PATH_SEPARATOR_S + "config.json");
  122. auto ipAssignments = mj["ipAssignments"];
  123. if (ipAssignments.is_array()) {
  124. for(unsigned long i=0;i<ipAssignments.size();++i) {
  125. std::string ipstr = ipAssignments[i];
  126. InetAddress ip(ipstr);
  127. if (ip)
  128. ips.insert(ip);
  129. }
  130. }
  131. }
  132. }
  133. return ips;
  134. }
  135. // These are const after construction
  136. Node *const _node;
  137. std::string _path;
  138. // Circuit tests outstanding
  139. struct _CircuitTestEntry
  140. {
  141. ZT_CircuitTest *test;
  142. std::string jsonResults;
  143. };
  144. std::map< uint64_t,_CircuitTestEntry > _circuitTests;
  145. Mutex _circuitTests_m;
  146. // Last request time by address, for rate limitation
  147. std::map< std::pair<uint64_t,uint64_t>,uint64_t > _lastRequestTime;
  148. Mutex _lastRequestTime_m;
  149. };
  150. } // namespace ZeroTier
  151. #endif