EmbeddedNetworkController.hpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 <thread>
  27. #include <unordered_map>
  28. #include <atomic>
  29. #include "../node/Constants.hpp"
  30. #include "../node/NetworkController.hpp"
  31. #include "../node/Utils.hpp"
  32. #include "../node/Address.hpp"
  33. #include "../node/InetAddress.hpp"
  34. #include "../node/NonCopyable.hpp"
  35. #include "../osdep/OSUtils.hpp"
  36. #include "../osdep/Thread.hpp"
  37. #include "../osdep/BlockingQueue.hpp"
  38. #include "../ext/json/json.hpp"
  39. #include "DB.hpp"
  40. #include "FileDB.hpp"
  41. #ifdef ZT_CONTROLLER_USE_RETHINKDB
  42. #include "RethinkDB.hpp"
  43. #endif
  44. namespace ZeroTier {
  45. class Node;
  46. class EmbeddedNetworkController : public NetworkController
  47. {
  48. public:
  49. /**
  50. * @param node Parent node
  51. * @param dbPath Database path (file path or database credentials)
  52. */
  53. EmbeddedNetworkController(Node *node,const char *dbPath);
  54. virtual ~EmbeddedNetworkController();
  55. virtual void init(const Identity &signingId,Sender *sender);
  56. virtual void request(
  57. uint64_t nwid,
  58. const InetAddress &fromAddr,
  59. uint64_t requestPacketId,
  60. const Identity &identity,
  61. const Dictionary<ZT_NETWORKCONFIG_METADATA_DICT_CAPACITY> &metaData);
  62. unsigned int handleControlPlaneHttpGET(
  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. unsigned int handleControlPlaneHttpPOST(
  70. const std::vector<std::string> &path,
  71. const std::map<std::string,std::string> &urlArgs,
  72. const std::map<std::string,std::string> &headers,
  73. const std::string &body,
  74. std::string &responseBody,
  75. std::string &responseContentType);
  76. unsigned int handleControlPlaneHttpDELETE(
  77. const std::vector<std::string> &path,
  78. const std::map<std::string,std::string> &urlArgs,
  79. const std::map<std::string,std::string> &headers,
  80. const std::string &body,
  81. std::string &responseBody,
  82. std::string &responseContentType);
  83. void handleRemoteTrace(const ZT_RemoteTrace &rt);
  84. // Called on update via POST or by JSONDB on external update of network or network member records
  85. void onNetworkUpdate(const uint64_t networkId);
  86. void onNetworkMemberUpdate(const uint64_t networkId,const uint64_t memberId);
  87. void onNetworkMemberDeauthorize(const uint64_t networkId,const uint64_t memberId);
  88. private:
  89. void _request(uint64_t nwid,const InetAddress &fromAddr,uint64_t requestPacketId,const Identity &identity,const Dictionary<ZT_NETWORKCONFIG_METADATA_DICT_CAPACITY> &metaData);
  90. void _startThreads();
  91. // These init objects with default and static/informational fields
  92. inline void _initMember(nlohmann::json &member)
  93. {
  94. if (!member.count("authorized")) member["authorized"] = false;
  95. if (!member.count("ipAssignments")) member["ipAssignments"] = nlohmann::json::array();
  96. if (!member.count("activeBridge")) member["activeBridge"] = false;
  97. if (!member.count("tags")) member["tags"] = nlohmann::json::array();
  98. if (!member.count("capabilities")) member["capabilities"] = nlohmann::json::array();
  99. if (!member.count("creationTime")) member["creationTime"] = OSUtils::now();
  100. if (!member.count("noAutoAssignIps")) member["noAutoAssignIps"] = false;
  101. if (!member.count("revision")) member["revision"] = 0ULL;
  102. if (!member.count("lastDeauthorizedTime")) member["lastDeauthorizedTime"] = 0ULL;
  103. if (!member.count("lastAuthorizedTime")) member["lastAuthorizedTime"] = 0ULL;
  104. if (!member.count("lastAuthorizedCredentialType")) member["lastAuthorizedCredentialType"] = nlohmann::json();
  105. if (!member.count("lastAuthorizedCredential")) member["lastAuthorizedCredential"] = nlohmann::json();
  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("remoteTraceTarget")) member["remoteTraceTarget"] = nlohmann::json();
  111. if (!member.count("removeTraceLevel")) member["remoteTraceLevel"] = 0;
  112. member["objtype"] = "member";
  113. }
  114. inline void _initNetwork(nlohmann::json &network)
  115. {
  116. if (!network.count("private")) network["private"] = true;
  117. if (!network.count("creationTime")) network["creationTime"] = OSUtils::now();
  118. if (!network.count("name")) network["name"] = "";
  119. if (!network.count("multicastLimit")) network["multicastLimit"] = (uint64_t)32;
  120. if (!network.count("enableBroadcast")) network["enableBroadcast"] = true;
  121. if (!network.count("v4AssignMode")) network["v4AssignMode"] = {{"zt",false}};
  122. if (!network.count("v6AssignMode")) network["v6AssignMode"] = {{"rfc4193",false},{"zt",false},{"6plane",false}};
  123. if (!network.count("authTokens")) network["authTokens"] = {{}};
  124. if (!network.count("capabilities")) network["capabilities"] = nlohmann::json::array();
  125. if (!network.count("tags")) network["tags"] = nlohmann::json::array();
  126. if (!network.count("routes")) network["routes"] = nlohmann::json::array();
  127. if (!network.count("ipAssignmentPools")) network["ipAssignmentPools"] = nlohmann::json::array();
  128. if (!network.count("mtu")) network["mtu"] = ZT_DEFAULT_MTU;
  129. if (!network.count("remoteTraceTarget")) network["remoteTraceTarget"] = nlohmann::json();
  130. if (!network.count("removeTraceLevel")) network["remoteTraceLevel"] = 0;
  131. if (!network.count("rules")) {
  132. // If unspecified, rules are set to allow anything and behave like a flat L2 segment
  133. network["rules"] = {{
  134. { "not",false },
  135. { "or", false },
  136. { "type","ACTION_ACCEPT" }
  137. }};
  138. }
  139. network["objtype"] = "network";
  140. }
  141. inline void _cleanNetwork(nlohmann::json &network)
  142. {
  143. network.erase("clock");
  144. network.erase("authorizedMemberCount");
  145. network.erase("activeMemberCount");
  146. network.erase("totalMemberCount");
  147. network.erase("lastModified");
  148. }
  149. inline void _cleanMember(nlohmann::json &member)
  150. {
  151. member.erase("clock");
  152. member.erase("physicalAddr");
  153. member.erase("recentLog");
  154. member.erase("lastModified");
  155. member.erase("lastRequestMetaData");
  156. }
  157. struct _RQEntry
  158. {
  159. uint64_t nwid;
  160. uint64_t requestPacketId;
  161. InetAddress fromAddr;
  162. Identity identity;
  163. Dictionary<ZT_NETWORKCONFIG_METADATA_DICT_CAPACITY> metaData;
  164. enum {
  165. RQENTRY_TYPE_REQUEST = 0
  166. } type;
  167. };
  168. struct _MemberStatusKey
  169. {
  170. _MemberStatusKey() : networkId(0),nodeId(0) {}
  171. _MemberStatusKey(const uint64_t nwid,const uint64_t nid) : networkId(nwid),nodeId(nid) {}
  172. uint64_t networkId;
  173. uint64_t nodeId;
  174. inline bool operator==(const _MemberStatusKey &k) const { return ((k.networkId == networkId)&&(k.nodeId == nodeId)); }
  175. };
  176. struct _MemberStatus
  177. {
  178. _MemberStatus() : lastRequestTime(0),vMajor(-1),vMinor(-1),vRev(-1),vProto(-1) {}
  179. uint64_t lastRequestTime;
  180. int vMajor,vMinor,vRev,vProto;
  181. Dictionary<ZT_NETWORKCONFIG_METADATA_DICT_CAPACITY> lastRequestMetaData;
  182. Identity identity;
  183. InetAddress physicalAddr; // last known physical address
  184. inline bool online(const int64_t now) const { return ((now - lastRequestTime) < (ZT_NETWORK_AUTOCONF_DELAY * 2)); }
  185. };
  186. struct _MemberStatusHash
  187. {
  188. inline std::size_t operator()(const _MemberStatusKey &networkIdNodeId) const
  189. {
  190. return (std::size_t)(networkIdNodeId.networkId + networkIdNodeId.nodeId);
  191. }
  192. };
  193. const int64_t _startTime;
  194. Node *const _node;
  195. std::string _path;
  196. Identity _signingId;
  197. std::string _signingIdAddressString;
  198. NetworkController::Sender *_sender;
  199. std::unique_ptr<DB> _db;
  200. BlockingQueue< _RQEntry * > _queue;
  201. std::vector<std::thread> _threads;
  202. std::mutex _threads_l;
  203. std::unordered_map< _MemberStatusKey,_MemberStatus,_MemberStatusHash > _memberStatus;
  204. std::mutex _memberStatus_l;
  205. };
  206. } // namespace ZeroTier
  207. #endif