EmbeddedNetworkController.hpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 ztPath ZeroTier base path
  57. * @param dbPath Database path (file path or database credentials)
  58. */
  59. EmbeddedNetworkController(Node *node,const char *ztPath,const char *dbPath, int listenPort, MQConfig *mqc = NULL);
  60. virtual ~EmbeddedNetworkController();
  61. virtual void init(const Identity &signingId,Sender *sender);
  62. virtual void request(
  63. uint64_t nwid,
  64. const InetAddress &fromAddr,
  65. uint64_t requestPacketId,
  66. const Identity &identity,
  67. const Dictionary<ZT_NETWORKCONFIG_METADATA_DICT_CAPACITY> &metaData);
  68. unsigned int handleControlPlaneHttpGET(
  69. const std::vector<std::string> &path,
  70. const std::map<std::string,std::string> &urlArgs,
  71. const std::map<std::string,std::string> &headers,
  72. const std::string &body,
  73. std::string &responseBody,
  74. std::string &responseContentType);
  75. unsigned int handleControlPlaneHttpPOST(
  76. const std::vector<std::string> &path,
  77. const std::map<std::string,std::string> &urlArgs,
  78. const std::map<std::string,std::string> &headers,
  79. const std::string &body,
  80. std::string &responseBody,
  81. std::string &responseContentType);
  82. unsigned int handleControlPlaneHttpDELETE(
  83. const std::vector<std::string> &path,
  84. const std::map<std::string,std::string> &urlArgs,
  85. const std::map<std::string,std::string> &headers,
  86. const std::string &body,
  87. std::string &responseBody,
  88. std::string &responseContentType);
  89. void handleRemoteTrace(const ZT_RemoteTrace &rt);
  90. virtual void onNetworkUpdate(const void *db,uint64_t networkId,const nlohmann::json &network);
  91. virtual void onNetworkMemberUpdate(const void *db,uint64_t networkId,uint64_t memberId,const nlohmann::json &member);
  92. virtual void onNetworkMemberDeauthorize(const void *db,uint64_t networkId,uint64_t memberId);
  93. private:
  94. void _request(uint64_t nwid,const InetAddress &fromAddr,uint64_t requestPacketId,const Identity &identity,const Dictionary<ZT_NETWORKCONFIG_METADATA_DICT_CAPACITY> &metaData);
  95. void _startThreads();
  96. struct _RQEntry
  97. {
  98. uint64_t nwid;
  99. uint64_t requestPacketId;
  100. InetAddress fromAddr;
  101. Identity identity;
  102. Dictionary<ZT_NETWORKCONFIG_METADATA_DICT_CAPACITY> metaData;
  103. enum {
  104. RQENTRY_TYPE_REQUEST = 0
  105. } type;
  106. };
  107. struct _MemberStatusKey
  108. {
  109. _MemberStatusKey() : networkId(0),nodeId(0) {}
  110. _MemberStatusKey(const uint64_t nwid,const uint64_t nid) : networkId(nwid),nodeId(nid) {}
  111. uint64_t networkId;
  112. uint64_t nodeId;
  113. inline bool operator==(const _MemberStatusKey &k) const { return ((k.networkId == networkId)&&(k.nodeId == nodeId)); }
  114. };
  115. struct _MemberStatus
  116. {
  117. _MemberStatus() : lastRequestTime(0),vMajor(-1),vMinor(-1),vRev(-1),vProto(-1) {}
  118. uint64_t lastRequestTime;
  119. int vMajor,vMinor,vRev,vProto;
  120. Dictionary<ZT_NETWORKCONFIG_METADATA_DICT_CAPACITY> lastRequestMetaData;
  121. Identity identity;
  122. inline bool online(const int64_t now) const { return ((now - lastRequestTime) < (ZT_NETWORK_AUTOCONF_DELAY * 2)); }
  123. };
  124. struct _MemberStatusHash
  125. {
  126. inline std::size_t operator()(const _MemberStatusKey &networkIdNodeId) const
  127. {
  128. return (std::size_t)(networkIdNodeId.networkId + networkIdNodeId.nodeId);
  129. }
  130. };
  131. const int64_t _startTime;
  132. int _listenPort;
  133. Node *const _node;
  134. std::string _ztPath;
  135. std::string _path;
  136. Identity _signingId;
  137. std::string _signingIdAddressString;
  138. NetworkController::Sender *_sender;
  139. DBMirrorSet _db;
  140. BlockingQueue< _RQEntry * > _queue;
  141. std::vector<std::thread> _threads;
  142. std::mutex _threads_l;
  143. std::unordered_map< _MemberStatusKey,_MemberStatus,_MemberStatusHash > _memberStatus;
  144. std::mutex _memberStatus_l;
  145. MQConfig *_mqc;
  146. };
  147. } // namespace ZeroTier
  148. #endif