EmbeddedNetworkController.hpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. // Expiration time for network member cache entries in ms
  33. #define ZT_NETCONF_NETWORK_MEMBER_CACHE_EXPIRE 30000
  34. namespace ZeroTier {
  35. class Node;
  36. class EmbeddedNetworkController : public NetworkController
  37. {
  38. public:
  39. EmbeddedNetworkController(Node *node,const char *dbPath);
  40. virtual ~EmbeddedNetworkController();
  41. virtual NetworkController::ResultCode doNetworkConfigRequest(
  42. const InetAddress &fromAddr,
  43. const Identity &signingId,
  44. const Identity &identity,
  45. uint64_t nwid,
  46. const Dictionary<ZT_NETWORKCONFIG_METADATA_DICT_CAPACITY> &metaData,
  47. NetworkConfig &nc);
  48. unsigned int handleControlPlaneHttpGET(
  49. const std::vector<std::string> &path,
  50. const std::map<std::string,std::string> &urlArgs,
  51. const std::map<std::string,std::string> &headers,
  52. const std::string &body,
  53. std::string &responseBody,
  54. std::string &responseContentType);
  55. unsigned int handleControlPlaneHttpPOST(
  56. const std::vector<std::string> &path,
  57. const std::map<std::string,std::string> &urlArgs,
  58. const std::map<std::string,std::string> &headers,
  59. const std::string &body,
  60. std::string &responseBody,
  61. std::string &responseContentType);
  62. unsigned int handleControlPlaneHttpDELETE(
  63. const std::vector<std::string> &path,
  64. const std::map<std::string,std::string> &urlArgs,
  65. const std::map<std::string,std::string> &headers,
  66. const std::string &body,
  67. std::string &responseBody,
  68. std::string &responseContentType);
  69. private:
  70. static void _circuitTestCallback(ZT_Node *node,ZT_CircuitTest *test,const ZT_CircuitTestReport *report);
  71. // JSON blob I/O
  72. inline nlohmann::json _readJson(const std::string &path)
  73. {
  74. std::string buf;
  75. if (OSUtils::readFile(path.c_str(),buf)) {
  76. try {
  77. return nlohmann::json::parse(buf);
  78. } catch ( ... ) {}
  79. }
  80. return nlohmann::json::object();
  81. }
  82. inline bool _writeJson(const std::string &path,const nlohmann::json &obj)
  83. {
  84. return OSUtils::writeFile(path.c_str(),obj.dump(2));
  85. }
  86. // Network base path and network JSON path
  87. inline std::string _networkBP(const uint64_t nwid,bool create)
  88. {
  89. char tmp[64];
  90. std::string p(_path + ZT_PATH_SEPARATOR_S + "network");
  91. if (create) OSUtils::mkdir(p.c_str());
  92. p.push_back(ZT_PATH_SEPARATOR);
  93. Utils::snprintf(tmp,sizeof(tmp),"%.16llx",nwid);
  94. p.append(tmp);
  95. if (create) OSUtils::mkdir(p.c_str());
  96. return p;
  97. }
  98. inline std::string _networkJP(const uint64_t nwid,bool create)
  99. {
  100. return (_networkBP(nwid,create) + ZT_PATH_SEPARATOR + "config.json");
  101. }
  102. // Member base path and member JSON path
  103. inline std::string _memberBP(const uint64_t nwid,const Address &member,bool create)
  104. {
  105. std::string p(_networkBP(nwid,create));
  106. p.push_back(ZT_PATH_SEPARATOR);
  107. p.append("member");
  108. if (create) OSUtils::mkdir(p.c_str());
  109. p.push_back(ZT_PATH_SEPARATOR);
  110. p.append(member.toString());
  111. if (create) OSUtils::mkdir(p.c_str());
  112. return p;
  113. }
  114. inline std::string _memberJP(const uint64_t nwid,const Address &member,bool create)
  115. {
  116. return (_memberBP(nwid,member,create) + ZT_PATH_SEPARATOR + "config.json");
  117. }
  118. // We cache the members of networks in memory to avoid having to scan the filesystem so much
  119. std::map< uint64_t,std::pair< std::map< Address,nlohmann::json >,uint64_t > > _networkMemberCache;
  120. Mutex _networkMemberCache_m;
  121. // Gathers a bunch of statistics about members of a network, IP assignments, etc. that we need in various places
  122. // This does lock _networkMemberCache_m
  123. struct _NetworkMemberInfo
  124. {
  125. _NetworkMemberInfo() : authorizedMemberCount(0),activeMemberCount(0),totalMemberCount(0),mostRecentDeauthTime(0) {}
  126. std::set<Address> activeBridges;
  127. std::set<InetAddress> allocatedIps;
  128. unsigned long authorizedMemberCount;
  129. unsigned long activeMemberCount;
  130. unsigned long totalMemberCount;
  131. uint64_t mostRecentDeauthTime;
  132. };
  133. void _getNetworkMemberInfo(uint64_t now,uint64_t nwid,_NetworkMemberInfo &nmi);
  134. // These init objects with default and static/informational fields
  135. inline void _initMember(nlohmann::json &member)
  136. {
  137. if (!member.count("authorized")) member["authorized"] = false;
  138. if (!member.count("lastAuthorizedTime")) member["lastAuthorizedTime"] = 0ULL;
  139. if (!member.count("lastAuthorizedBy")) member["lastAuthorizedBy"] = "";
  140. if (!member.count("lastDeauthorizedTime")) member["lastDeauthorizedTime"] = 0ULL;
  141. if (!member.count("ipAssignments")) member["ipAssignments"] = nlohmann::json::array();
  142. if (!member.count("recentLog")) member["recentLog"] = nlohmann::json::array();
  143. if (!member.count("activeBridge")) member["activeBridge"] = false;
  144. if (!member.count("tags")) member["tags"] = nlohmann::json::array();
  145. if (!member.count("capabilities")) member["capabilities"] = nlohmann::json::array();
  146. if (!member.count("creationTime")) member["creationTime"] = OSUtils::now();
  147. member["objtype"] = "member";
  148. }
  149. inline void _initNetwork(nlohmann::json &network)
  150. {
  151. if (!network.count("private")) network["private"] = true;
  152. if (!network.count("creationTime")) network["creationTime"] = OSUtils::now();
  153. if (!network.count("name")) network["name"] = "";
  154. if (!network.count("multicastLimit")) network["multicastLimit"] = (uint64_t)32;
  155. if (!network.count("v4AssignMode")) network["v4AssignMode"] = {{"zt",false}};
  156. if (!network.count("v6AssignMode")) network["v6AssignMode"] = {{"rfc4193",false},{"zt",false},{"6plane",false}};
  157. if (!network.count("activeBridges")) network["activeBridges"] = nlohmann::json::array();
  158. if (!network.count("authTokens")) network["authTokens"] = nlohmann::json::array();
  159. if (!network.count("capabilities")) network["capabilities"] = nlohmann::json::array();
  160. if (!network.count("rules")) {
  161. // If unspecified, rules are set to allow anything and behave like a flat L2 segment
  162. network["rules"] = {
  163. { "not",false },
  164. { "type","ACTION_ACCEPT" }
  165. };
  166. }
  167. network["objtype"] = "network";
  168. }
  169. inline void _addNetworkNonPersistedFields(nlohmann::json &network,uint64_t now,const _NetworkMemberInfo &nmi)
  170. {
  171. network["clock"] = now;
  172. network["authorizedMemberCount"] = nmi.authorizedMemberCount;
  173. network["activeMemberCount"] = nmi.activeMemberCount;
  174. network["totalMemberCount"] = nmi.totalMemberCount;
  175. }
  176. // These are const after construction
  177. Node *const _node;
  178. std::string _path;
  179. // Circuit tests outstanding
  180. struct _CircuitTestEntry
  181. {
  182. ZT_CircuitTest *test;
  183. std::string jsonResults;
  184. };
  185. std::map< uint64_t,_CircuitTestEntry > _circuitTests;
  186. Mutex _circuitTests_m;
  187. // Last request time by address, for rate limitation
  188. std::map< std::pair<uint64_t,uint64_t>,uint64_t > _lastRequestTime;
  189. Mutex _lastRequestTime_m;
  190. };
  191. } // namespace ZeroTier
  192. #endif