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