EmbeddedNetworkController.hpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 "../osdep/BlockingQueue.hpp"
  35. #include "../ext/json/json.hpp"
  36. #include "JSONDB.hpp"
  37. // Number of background threads to start -- not actually started until needed
  38. #define ZT_EMBEDDEDNETWORKCONTROLLER_BACKGROUND_THREAD_COUNT 2
  39. namespace ZeroTier {
  40. class Node;
  41. class EmbeddedNetworkController : public NetworkController
  42. {
  43. public:
  44. /**
  45. * @param node Parent node
  46. * @param dbPath Path to store data
  47. * @param feed FILE to send feed of all data and changes to (zero-delimited JSON objects) or NULL for none
  48. */
  49. EmbeddedNetworkController(Node *node,const char *dbPath,FILE *feed);
  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. static void _circuitTestCallback(ZT_Node *node,ZT_CircuitTest *test,const ZT_CircuitTestReport *report);
  83. void _request(
  84. uint64_t nwid,
  85. const InetAddress &fromAddr,
  86. uint64_t requestPacketId,
  87. const Identity &identity,
  88. const Dictionary<ZT_NETWORKCONFIG_METADATA_DICT_CAPACITY> &metaData);
  89. struct _RQEntry
  90. {
  91. uint64_t nwid;
  92. uint64_t requestPacketId;
  93. InetAddress fromAddr;
  94. Identity identity;
  95. Dictionary<ZT_NETWORKCONFIG_METADATA_DICT_CAPACITY> metaData;
  96. };
  97. BlockingQueue<_RQEntry *> _queue;
  98. Thread _threads[ZT_EMBEDDEDNETWORKCONTROLLER_BACKGROUND_THREAD_COUNT];
  99. bool _threadsStarted;
  100. Mutex _threads_m;
  101. // Gathers a bunch of statistics about members of a network, IP assignments, etc. that we need in various places
  102. // This does lock _networkMemberCache_m
  103. struct _NetworkMemberInfo
  104. {
  105. _NetworkMemberInfo() : authorizedMemberCount(0),activeMemberCount(0),totalMemberCount(0),mostRecentDeauthTime(0) {}
  106. std::set<Address> activeBridges;
  107. std::set<InetAddress> allocatedIps;
  108. unsigned long authorizedMemberCount;
  109. unsigned long activeMemberCount;
  110. unsigned long totalMemberCount;
  111. uint64_t mostRecentDeauthTime;
  112. uint64_t nmiTimestamp; // time this NMI structure was computed
  113. };
  114. std::map<uint64_t,_NetworkMemberInfo> _nmiCache;
  115. Mutex _nmiCache_m;
  116. void _getNetworkMemberInfo(uint64_t now,uint64_t nwid,_NetworkMemberInfo &nmi);
  117. void _pushMemberUpdate(uint64_t now,uint64_t nwid,const nlohmann::json &member);
  118. // These init objects with default and static/informational fields
  119. inline void _initMember(nlohmann::json &member)
  120. {
  121. if (!member.count("authorized")) member["authorized"] = false;
  122. if (!member.count("authHistory")) member["authHistory"] = nlohmann::json::array();
  123. if (!member.count("ipAssignments")) member["ipAssignments"] = nlohmann::json::array();
  124. if (!member.count("recentLog")) member["recentLog"] = nlohmann::json::array();
  125. if (!member.count("activeBridge")) member["activeBridge"] = false;
  126. if (!member.count("tags")) member["tags"] = nlohmann::json::array();
  127. if (!member.count("capabilities")) member["capabilities"] = nlohmann::json::array();
  128. if (!member.count("creationTime")) member["creationTime"] = OSUtils::now();
  129. if (!member.count("noAutoAssignIps")) member["noAutoAssignIps"] = false;
  130. if (!member.count("revision")) member["revision"] = 0ULL;
  131. if (!member.count("lastDeauthorizedTime")) member["lastDeauthorizedTime"] = 0ULL;
  132. if (!member.count("lastAuthorizedTime")) member["lastAuthorizedTime"] = 0ULL;
  133. member["objtype"] = "member";
  134. }
  135. inline void _initNetwork(nlohmann::json &network)
  136. {
  137. if (!network.count("private")) network["private"] = true;
  138. if (!network.count("creationTime")) network["creationTime"] = OSUtils::now();
  139. if (!network.count("name")) network["name"] = "";
  140. if (!network.count("multicastLimit")) network["multicastLimit"] = (uint64_t)32;
  141. if (!network.count("enableBroadcast")) network["enableBroadcast"] = true;
  142. if (!network.count("v4AssignMode")) network["v4AssignMode"] = {{"zt",false}};
  143. if (!network.count("v6AssignMode")) network["v6AssignMode"] = {{"rfc4193",false},{"zt",false},{"6plane",false}};
  144. if (!network.count("authTokens")) network["authTokens"] = nlohmann::json::array();
  145. if (!network.count("capabilities")) network["capabilities"] = nlohmann::json::array();
  146. if (!network.count("ipAssignmentPools")) network["ipAssignmentPools"] = nlohmann::json::array();
  147. if (!network.count("rules")) {
  148. // If unspecified, rules are set to allow anything and behave like a flat L2 segment
  149. network["rules"] = {{
  150. { "not",false },
  151. { "or", false },
  152. { "type","ACTION_ACCEPT" }
  153. }};
  154. }
  155. network["objtype"] = "network";
  156. }
  157. inline void _addNetworkNonPersistedFields(nlohmann::json &network,uint64_t now,const _NetworkMemberInfo &nmi)
  158. {
  159. network["clock"] = now;
  160. network["authorizedMemberCount"] = nmi.authorizedMemberCount;
  161. network["activeMemberCount"] = nmi.activeMemberCount;
  162. network["totalMemberCount"] = nmi.totalMemberCount;
  163. }
  164. inline void _addMemberNonPersistedFields(nlohmann::json &member,uint64_t now)
  165. {
  166. member["clock"] = now;
  167. }
  168. JSONDB _db;
  169. Mutex _db_m;
  170. Node *const _node;
  171. std::string _path;
  172. NetworkController::Sender *_sender;
  173. Identity _signingId;
  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. std::map< std::pair<uint64_t,uint64_t>,uint64_t > _lastRequestTime;
  182. Mutex _lastRequestTime_m;
  183. };
  184. } // namespace ZeroTier
  185. #endif