EmbeddedNetworkController.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright (c)2013-2020 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2025-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #ifndef ZT_SQLITENETWORKCONTROLLER_HPP
  14. #define ZT_SQLITENETWORKCONTROLLER_HPP
  15. #include <stdint.h>
  16. #include <string>
  17. #include <map>
  18. #include <vector>
  19. #include <set>
  20. #include <list>
  21. #include <thread>
  22. #include <unordered_map>
  23. #include <atomic>
  24. #include "../core/Constants.hpp"
  25. #include "../core/NetworkController.hpp"
  26. #include "../core/Utils.hpp"
  27. #include "../core/Address.hpp"
  28. #include "../core/InetAddress.hpp"
  29. #include "../osdep/OSUtils.hpp"
  30. #include "../osdep/Thread.hpp"
  31. #include "../osdep/BlockingQueue.hpp"
  32. #include "./thirdparty/json/json.hpp"
  33. #include "DB.hpp"
  34. #include "DBMirrorSet.hpp"
  35. namespace ZeroTier {
  36. class Node;
  37. struct RedisConfig;
  38. class EmbeddedNetworkController : public NetworkController,public DB::ChangeListener
  39. {
  40. public:
  41. /**
  42. * @param node Parent node
  43. * @param ztPath ZeroTier base path
  44. * @param dbPath Database path (file path or database credentials)
  45. */
  46. EmbeddedNetworkController(Node *node,const char *ztPath,const char *dbPath, int listenPort, RedisConfig *rc = NULL);
  47. virtual ~EmbeddedNetworkController();
  48. virtual void init(const Identity &signingId,Sender *sender);
  49. virtual void request(
  50. uint64_t nwid,
  51. const InetAddress &fromAddr,
  52. uint64_t requestPacketId,
  53. const Identity &identity,
  54. const Dictionary &metaData);
  55. unsigned int handleControlPlaneHttpGET(
  56. const std::vector<std::string> &path,
  57. const std::string &body,
  58. std::string &responseBody,
  59. std::string &responseContentType);
  60. unsigned int handleControlPlaneHttpPOST(
  61. const std::vector<std::string> &path,
  62. const std::string &body,
  63. std::string &responseBody,
  64. std::string &responseContentType);
  65. unsigned int handleControlPlaneHttpDELETE(
  66. const std::vector<std::string> &path,
  67. const std::string &body,
  68. std::string &responseBody,
  69. std::string &responseContentType);
  70. virtual void onNetworkUpdate(const void *db,uint64_t networkId,const nlohmann::json &network);
  71. virtual void onNetworkMemberUpdate(const void *db,uint64_t networkId,uint64_t memberId,const nlohmann::json &member);
  72. virtual void onNetworkMemberDeauthorize(const void *db,uint64_t networkId,uint64_t memberId);
  73. private:
  74. void _request(uint64_t nwid,const InetAddress &fromAddr,uint64_t requestPacketId,const Identity &identity,const Dictionary &metaData);
  75. void _startThreads();
  76. struct _RQEntry
  77. {
  78. uint64_t nwid;
  79. uint64_t requestPacketId;
  80. InetAddress fromAddr;
  81. Identity identity;
  82. Dictionary metaData;
  83. enum {
  84. RQENTRY_TYPE_REQUEST = 0
  85. } type;
  86. };
  87. struct _MemberStatusKey
  88. {
  89. ZT_INLINE _MemberStatusKey() : networkId(0),nodeId(0) {}
  90. ZT_INLINE _MemberStatusKey(const uint64_t nwid,const uint64_t nid) : networkId(nwid),nodeId(nid) {}
  91. uint64_t networkId;
  92. uint64_t nodeId;
  93. inline bool operator==(const _MemberStatusKey &k) const { return ((k.networkId == networkId)&&(k.nodeId == nodeId)); }
  94. };
  95. struct _MemberStatus
  96. {
  97. ZT_INLINE _MemberStatus() : lastRequestTime(0),vMajor(-1),vMinor(-1),vRev(-1),vProto(-1) {}
  98. uint64_t lastRequestTime;
  99. int vMajor,vMinor,vRev,vProto;
  100. Dictionary lastRequestMetaData;
  101. Identity identity;
  102. inline bool online(const int64_t now) const { return ((now - lastRequestTime) < (ZT_NETWORK_AUTOCONF_DELAY * 2)); }
  103. };
  104. struct _MemberStatusHash
  105. {
  106. ZT_INLINE std::size_t operator()(const _MemberStatusKey &networkIdNodeId) const
  107. {
  108. return (std::size_t)(networkIdNodeId.networkId + networkIdNodeId.nodeId);
  109. }
  110. };
  111. const int64_t _startTime;
  112. int _listenPort;
  113. Node *const _node;
  114. std::string _ztPath;
  115. std::string _path;
  116. Identity _signingId;
  117. std::string _signingIdAddressString;
  118. NetworkController::Sender *_sender;
  119. DBMirrorSet _db;
  120. BlockingQueue< _RQEntry * > _queue;
  121. std::vector<std::thread> _threads;
  122. std::mutex _threads_l;
  123. std::unordered_map< _MemberStatusKey,_MemberStatus,_MemberStatusHash > _memberStatus;
  124. std::mutex _memberStatus_l;
  125. RedisConfig *_rc;
  126. };
  127. } // namespace ZeroTier
  128. #endif