EmbeddedNetworkController.hpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. void _pushMemberUpdate(uint64_t now,uint64_t nwid,const nlohmann::json &member);
  93. // These init objects with default and static/informational fields
  94. inline void _initMember(nlohmann::json &member)
  95. {
  96. if (!member.count("authorized")) member["authorized"] = false;
  97. if (!member.count("authHistory")) member["authHistory"] = nlohmann::json::array();
  98. if (!member.count("ipAssignments")) member["ipAssignments"] = nlohmann::json::array();
  99. if (!member.count("recentLog")) member["recentLog"] = nlohmann::json::array();
  100. if (!member.count("activeBridge")) member["activeBridge"] = false;
  101. if (!member.count("tags")) member["tags"] = nlohmann::json::array();
  102. if (!member.count("capabilities")) member["capabilities"] = nlohmann::json::array();
  103. if (!member.count("creationTime")) member["creationTime"] = OSUtils::now();
  104. if (!member.count("noAutoAssignIps")) member["noAutoAssignIps"] = false;
  105. if (!member.count("revision")) member["revision"] = 0ULL;
  106. if (!member.count("lastDeauthorizedTime")) member["lastDeauthorizedTime"] = 0ULL;
  107. if (!member.count("lastAuthorizedTime")) member["lastAuthorizedTime"] = 0ULL;
  108. member["objtype"] = "member";
  109. }
  110. inline void _initNetwork(nlohmann::json &network)
  111. {
  112. if (!network.count("private")) network["private"] = true;
  113. if (!network.count("creationTime")) network["creationTime"] = OSUtils::now();
  114. if (!network.count("name")) network["name"] = "";
  115. if (!network.count("multicastLimit")) network["multicastLimit"] = (uint64_t)32;
  116. if (!network.count("enableBroadcast")) network["enableBroadcast"] = true;
  117. if (!network.count("v4AssignMode")) network["v4AssignMode"] = {{"zt",false}};
  118. if (!network.count("v6AssignMode")) network["v6AssignMode"] = {{"rfc4193",false},{"zt",false},{"6plane",false}};
  119. if (!network.count("authTokens")) network["authTokens"] = nlohmann::json::array();
  120. if (!network.count("capabilities")) network["capabilities"] = nlohmann::json::array();
  121. if (!network.count("tags")) network["tags"] = nlohmann::json::array();
  122. if (!network.count("routes")) network["routes"] = nlohmann::json::array();
  123. if (!network.count("ipAssignmentPools")) network["ipAssignmentPools"] = nlohmann::json::array();
  124. if (!network.count("rules")) {
  125. // If unspecified, rules are set to allow anything and behave like a flat L2 segment
  126. network["rules"] = {{
  127. { "not",false },
  128. { "or", false },
  129. { "type","ACTION_ACCEPT" }
  130. }};
  131. }
  132. network["objtype"] = "network";
  133. }
  134. inline void _addNetworkNonPersistedFields(nlohmann::json &network,uint64_t now,const JSONDB::NetworkSummaryInfo &ns)
  135. {
  136. network["clock"] = now;
  137. network["authorizedMemberCount"] = ns.authorizedMemberCount;
  138. network["activeMemberCount"] = ns.activeMemberCount;
  139. network["totalMemberCount"] = ns.totalMemberCount;
  140. }
  141. inline void _removeNetworkNonPersistedFields(nlohmann::json &network)
  142. {
  143. network.erase("clock");
  144. network.erase("authorizedMemberCount");
  145. network.erase("activeMemberCount");
  146. network.erase("totalMemberCount");
  147. }
  148. inline void _addMemberNonPersistedFields(nlohmann::json &member,uint64_t now)
  149. {
  150. member["clock"] = now;
  151. }
  152. inline void _removeMemberNonPersistedFields(nlohmann::json &member)
  153. {
  154. member.erase("clock");
  155. }
  156. const uint64_t _startTime;
  157. volatile bool _running;
  158. BlockingQueue<_RQEntry *> _queue;
  159. std::vector<Thread> _threads;
  160. Mutex _threads_m;
  161. JSONDB _db;
  162. Node *const _node;
  163. std::string _path;
  164. NetworkController::Sender *_sender;
  165. Identity _signingId;
  166. std::list< ZT_CircuitTest > _tests;
  167. Mutex _tests_m;
  168. std::map< std::pair<uint64_t,uint64_t>,uint64_t > _lastRequestTime; // last request time by <address,networkId>
  169. Mutex _lastRequestTime_m;
  170. };
  171. } // namespace ZeroTier
  172. #endif