EmbeddedNetworkController.hpp 8.4 KB

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