EmbeddedNetworkController.hpp 7.3 KB

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