EmbeddedNetworkController.hpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2019 ZeroTier, Inc. https://www.zerotier.com/
  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. * --
  19. *
  20. * You can be released from the requirements of the license by purchasing
  21. * a commercial license. Buying such a license is mandatory as soon as you
  22. * develop commercial closed-source software that incorporates or links
  23. * directly against ZeroTier software without disclosing the source code
  24. * of your own application.
  25. */
  26. #ifndef ZT_SQLITENETWORKCONTROLLER_HPP
  27. #define ZT_SQLITENETWORKCONTROLLER_HPP
  28. #include <stdint.h>
  29. #include <string>
  30. #include <map>
  31. #include <vector>
  32. #include <set>
  33. #include <list>
  34. #include <thread>
  35. #include <unordered_map>
  36. #include <atomic>
  37. #include "../node/Constants.hpp"
  38. #include "../node/NetworkController.hpp"
  39. #include "../node/Utils.hpp"
  40. #include "../node/Address.hpp"
  41. #include "../node/InetAddress.hpp"
  42. #include "../osdep/OSUtils.hpp"
  43. #include "../osdep/Thread.hpp"
  44. #include "../osdep/BlockingQueue.hpp"
  45. #include "../ext/json/json.hpp"
  46. #include "DB.hpp"
  47. #include "DBMirrorSet.hpp"
  48. namespace ZeroTier {
  49. class Node;
  50. struct MQConfig;
  51. class EmbeddedNetworkController : public NetworkController,public DB::ChangeListener
  52. {
  53. public:
  54. /**
  55. * @param node Parent node
  56. * @param dbPath Database path (file path or database credentials)
  57. */
  58. EmbeddedNetworkController(Node *node,const char *dbPath, int listenPort, MQConfig *mqc = NULL);
  59. virtual ~EmbeddedNetworkController();
  60. virtual void init(const Identity &signingId,Sender *sender);
  61. virtual void request(
  62. uint64_t nwid,
  63. const InetAddress &fromAddr,
  64. uint64_t requestPacketId,
  65. const Identity &identity,
  66. const Dictionary<ZT_NETWORKCONFIG_METADATA_DICT_CAPACITY> &metaData);
  67. unsigned int handleControlPlaneHttpGET(
  68. const std::vector<std::string> &path,
  69. const std::map<std::string,std::string> &urlArgs,
  70. const std::map<std::string,std::string> &headers,
  71. const std::string &body,
  72. std::string &responseBody,
  73. std::string &responseContentType);
  74. unsigned int handleControlPlaneHttpPOST(
  75. const std::vector<std::string> &path,
  76. const std::map<std::string,std::string> &urlArgs,
  77. const std::map<std::string,std::string> &headers,
  78. const std::string &body,
  79. std::string &responseBody,
  80. std::string &responseContentType);
  81. unsigned int handleControlPlaneHttpDELETE(
  82. const std::vector<std::string> &path,
  83. const std::map<std::string,std::string> &urlArgs,
  84. const std::map<std::string,std::string> &headers,
  85. const std::string &body,
  86. std::string &responseBody,
  87. std::string &responseContentType);
  88. void handleRemoteTrace(const ZT_RemoteTrace &rt);
  89. virtual void onNetworkUpdate(const void *db,uint64_t networkId,const nlohmann::json &network);
  90. virtual void onNetworkMemberUpdate(const void *db,uint64_t networkId,uint64_t memberId,const nlohmann::json &member);
  91. virtual void onNetworkMemberDeauthorize(const void *db,uint64_t networkId,uint64_t memberId);
  92. private:
  93. void _request(uint64_t nwid,const InetAddress &fromAddr,uint64_t requestPacketId,const Identity &identity,const Dictionary<ZT_NETWORKCONFIG_METADATA_DICT_CAPACITY> &metaData);
  94. void _startThreads();
  95. struct _RQEntry
  96. {
  97. uint64_t nwid;
  98. uint64_t requestPacketId;
  99. InetAddress fromAddr;
  100. Identity identity;
  101. Dictionary<ZT_NETWORKCONFIG_METADATA_DICT_CAPACITY> metaData;
  102. enum {
  103. RQENTRY_TYPE_REQUEST = 0
  104. } type;
  105. };
  106. struct _MemberStatusKey
  107. {
  108. _MemberStatusKey() : networkId(0),nodeId(0) {}
  109. _MemberStatusKey(const uint64_t nwid,const uint64_t nid) : networkId(nwid),nodeId(nid) {}
  110. uint64_t networkId;
  111. uint64_t nodeId;
  112. inline bool operator==(const _MemberStatusKey &k) const { return ((k.networkId == networkId)&&(k.nodeId == nodeId)); }
  113. };
  114. struct _MemberStatus
  115. {
  116. _MemberStatus() : lastRequestTime(0),vMajor(-1),vMinor(-1),vRev(-1),vProto(-1) {}
  117. uint64_t lastRequestTime;
  118. int vMajor,vMinor,vRev,vProto;
  119. Dictionary<ZT_NETWORKCONFIG_METADATA_DICT_CAPACITY> lastRequestMetaData;
  120. Identity identity;
  121. inline bool online(const int64_t now) const { return ((now - lastRequestTime) < (ZT_NETWORK_AUTOCONF_DELAY * 2)); }
  122. };
  123. struct _MemberStatusHash
  124. {
  125. inline std::size_t operator()(const _MemberStatusKey &networkIdNodeId) const
  126. {
  127. return (std::size_t)(networkIdNodeId.networkId + networkIdNodeId.nodeId);
  128. }
  129. };
  130. const int64_t _startTime;
  131. int _listenPort;
  132. Node *const _node;
  133. std::string _path;
  134. Identity _signingId;
  135. std::string _signingIdAddressString;
  136. NetworkController::Sender *_sender;
  137. DBMirrorSet _db;
  138. BlockingQueue< _RQEntry * > _queue;
  139. std::vector<std::thread> _threads;
  140. std::mutex _threads_l;
  141. std::unordered_map< _MemberStatusKey,_MemberStatus,_MemberStatusHash > _memberStatus;
  142. std::mutex _memberStatus_l;
  143. MQConfig *_mqc;
  144. };
  145. } // namespace ZeroTier
  146. #endif