EmbeddedNetworkController.hpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 "../node/NonCopyable.hpp"
  33. #include "../osdep/OSUtils.hpp"
  34. #include "../osdep/Thread.hpp"
  35. #include "../osdep/BlockingQueue.hpp"
  36. #include "../ext/json/json.hpp"
  37. #include "JSONDB.hpp"
  38. // TTL for circuit tests
  39. #define ZT_EMBEDDEDNETWORKCONTROLLER_CIRCUIT_TEST_EXPIRATION 120000
  40. namespace ZeroTier {
  41. class Node;
  42. class EmbeddedNetworkController : public NetworkController,NonCopyable
  43. {
  44. public:
  45. /**
  46. * @param node Parent node
  47. * @param dbPath Path to store data
  48. */
  49. EmbeddedNetworkController(Node *node,const char *dbPath);
  50. virtual ~EmbeddedNetworkController();
  51. virtual void init(const Identity &signingId,Sender *sender);
  52. virtual void request(
  53. uint64_t nwid,
  54. const InetAddress &fromAddr,
  55. uint64_t requestPacketId,
  56. const Identity &identity,
  57. const Dictionary<ZT_NETWORKCONFIG_METADATA_DICT_CAPACITY> &metaData);
  58. unsigned int handleControlPlaneHttpGET(
  59. const std::vector<std::string> &path,
  60. const std::map<std::string,std::string> &urlArgs,
  61. const std::map<std::string,std::string> &headers,
  62. const std::string &body,
  63. std::string &responseBody,
  64. std::string &responseContentType);
  65. unsigned int handleControlPlaneHttpPOST(
  66. const std::vector<std::string> &path,
  67. const std::map<std::string,std::string> &urlArgs,
  68. const std::map<std::string,std::string> &headers,
  69. const std::string &body,
  70. std::string &responseBody,
  71. std::string &responseContentType);
  72. unsigned int handleControlPlaneHttpDELETE(
  73. const std::vector<std::string> &path,
  74. const std::map<std::string,std::string> &urlArgs,
  75. const std::map<std::string,std::string> &headers,
  76. const std::string &body,
  77. std::string &responseBody,
  78. std::string &responseContentType);
  79. void threadMain()
  80. throw();
  81. private:
  82. struct _RQEntry
  83. {
  84. uint64_t nwid;
  85. uint64_t requestPacketId;
  86. InetAddress fromAddr;
  87. Identity identity;
  88. Dictionary<ZT_NETWORKCONFIG_METADATA_DICT_CAPACITY> metaData;
  89. };
  90. static void _circuitTestCallback(ZT_Node *node,ZT_CircuitTest *test,const ZT_CircuitTestReport *report);
  91. void _request(uint64_t nwid,const InetAddress &fromAddr,uint64_t requestPacketId,const Identity &identity,const Dictionary<ZT_NETWORKCONFIG_METADATA_DICT_CAPACITY> &metaData);
  92. // These init objects with default and static/informational fields
  93. inline void _initMember(nlohmann::json &member)
  94. {
  95. if (!member.count("authorized")) member["authorized"] = false;
  96. if (!member.count("authHistory")) member["authHistory"] = nlohmann::json::array();
  97. if (!member.count("ipAssignments")) member["ipAssignments"] = nlohmann::json::array();
  98. if (!member.count("activeBridge")) member["activeBridge"] = false;
  99. if (!member.count("tags")) member["tags"] = nlohmann::json::array();
  100. if (!member.count("capabilities")) member["capabilities"] = nlohmann::json::array();
  101. if (!member.count("creationTime")) member["creationTime"] = OSUtils::now();
  102. if (!member.count("noAutoAssignIps")) member["noAutoAssignIps"] = false;
  103. if (!member.count("revision")) member["revision"] = 0ULL;
  104. if (!member.count("lastDeauthorizedTime")) member["lastDeauthorizedTime"] = 0ULL;
  105. if (!member.count("lastAuthorizedTime")) member["lastAuthorizedTime"] = 0ULL;
  106. if (!member.count("vMajor")) member["vMajor"] = -1;
  107. if (!member.count("vMinor")) member["vMinor"] = -1;
  108. if (!member.count("vRev")) member["vRev"] = -1;
  109. if (!member.count("vProto")) member["vProto"] = -1;
  110. if (!member.count("physicalAddr")) member["physicalAddr"] = nlohmann::json();
  111. member["objtype"] = "member";
  112. }
  113. inline void _initNetwork(nlohmann::json &network)
  114. {
  115. if (!network.count("private")) network["private"] = true;
  116. if (!network.count("creationTime")) network["creationTime"] = OSUtils::now();
  117. if (!network.count("name")) network["name"] = "";
  118. if (!network.count("multicastLimit")) network["multicastLimit"] = (uint64_t)32;
  119. if (!network.count("enableBroadcast")) network["enableBroadcast"] = true;
  120. if (!network.count("v4AssignMode")) network["v4AssignMode"] = {{"zt",false}};
  121. if (!network.count("v6AssignMode")) network["v6AssignMode"] = {{"rfc4193",false},{"zt",false},{"6plane",false}};
  122. if (!network.count("authTokens")) network["authTokens"] = nlohmann::json::array();
  123. if (!network.count("capabilities")) network["capabilities"] = nlohmann::json::array();
  124. if (!network.count("tags")) network["tags"] = nlohmann::json::array();
  125. if (!network.count("routes")) network["routes"] = nlohmann::json::array();
  126. if (!network.count("ipAssignmentPools")) network["ipAssignmentPools"] = nlohmann::json::array();
  127. if (!network.count("rules")) {
  128. // If unspecified, rules are set to allow anything and behave like a flat L2 segment
  129. network["rules"] = {{
  130. { "not",false },
  131. { "or", false },
  132. { "type","ACTION_ACCEPT" }
  133. }};
  134. }
  135. network["objtype"] = "network";
  136. }
  137. inline void _addNetworkNonPersistedFields(nlohmann::json &network,uint64_t now,const JSONDB::NetworkSummaryInfo &ns)
  138. {
  139. network["clock"] = now;
  140. network["authorizedMemberCount"] = ns.authorizedMemberCount;
  141. network["activeMemberCount"] = ns.activeMemberCount;
  142. network["totalMemberCount"] = ns.totalMemberCount;
  143. }
  144. inline void _removeNetworkNonPersistedFields(nlohmann::json &network)
  145. {
  146. network.erase("clock");
  147. network.erase("authorizedMemberCount");
  148. network.erase("activeMemberCount");
  149. network.erase("totalMemberCount");
  150. // legacy fields
  151. network.erase("lastModified");
  152. }
  153. inline void _addMemberNonPersistedFields(uint64_t nwid,uint64_t nodeId,nlohmann::json &member,uint64_t now)
  154. {
  155. member["clock"] = now;
  156. Mutex::Lock _l(_memberStatus_m);
  157. member["online"] = _memberStatus[_MemberStatusKey(nwid,nodeId)].online(now);
  158. }
  159. inline void _removeMemberNonPersistedFields(nlohmann::json &member)
  160. {
  161. member.erase("clock");
  162. // legacy fields
  163. member.erase("recentLog");
  164. member.erase("lastModified");
  165. member.erase("lastRequestMetaData");
  166. }
  167. const uint64_t _startTime;
  168. volatile bool _running;
  169. BlockingQueue<_RQEntry *> _queue;
  170. std::vector<Thread> _threads;
  171. Mutex _threads_m;
  172. JSONDB _db;
  173. Node *const _node;
  174. std::string _path;
  175. NetworkController::Sender *_sender;
  176. Identity _signingId;
  177. std::list< ZT_CircuitTest > _tests;
  178. Mutex _tests_m;
  179. struct _MemberStatusKey
  180. {
  181. _MemberStatusKey() : networkId(0),nodeId(0) {}
  182. _MemberStatusKey(const uint64_t nwid,const uint64_t nid) : networkId(nwid),nodeId(nid) {}
  183. uint64_t networkId;
  184. uint64_t nodeId;
  185. inline bool operator==(const _MemberStatusKey &k) const { return ((k.networkId == networkId)&&(k.nodeId == nodeId)); }
  186. };
  187. struct _MemberStatus
  188. {
  189. _MemberStatus() : lastRequestTime(0),vMajor(-1),vMinor(-1),vRev(-1),vProto(-1) {}
  190. uint64_t lastRequestTime;
  191. int vMajor,vMinor,vRev,vProto;
  192. Dictionary<ZT_NETWORKCONFIG_METADATA_DICT_CAPACITY> lastRequestMetaData;
  193. Identity identity;
  194. InetAddress physicalAddr; // last known physical address
  195. inline bool online(const uint64_t now) const { return ((now - lastRequestTime) < (ZT_NETWORK_AUTOCONF_DELAY * 2)); }
  196. };
  197. struct _MemberStatusHash
  198. {
  199. inline std::size_t operator()(const _MemberStatusKey &networkIdNodeId) const
  200. {
  201. return (std::size_t)(networkIdNodeId.networkId + networkIdNodeId.nodeId);
  202. }
  203. };
  204. std::unordered_map< _MemberStatusKey,_MemberStatus,_MemberStatusHash > _memberStatus;
  205. Mutex _memberStatus_m;
  206. };
  207. } // namespace ZeroTier
  208. #endif