EmbeddedNetworkController.hpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright (c)2019 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: 2023-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 "../node/Constants.hpp"
  25. #include "../node/NetworkController.hpp"
  26. #include "../node/Utils.hpp"
  27. #include "../node/Address.hpp"
  28. #include "../node/InetAddress.hpp"
  29. #include "../osdep/OSUtils.hpp"
  30. #include "../osdep/Thread.hpp"
  31. #include "../osdep/BlockingQueue.hpp"
  32. #include "../ext/json/json.hpp"
  33. #include "DB.hpp"
  34. #include "DBMirrorSet.hpp"
  35. namespace ZeroTier {
  36. class Node;
  37. class EmbeddedNetworkController : public NetworkController,public DB::ChangeListener
  38. {
  39. public:
  40. /**
  41. * @param node Parent node
  42. * @param dbPath Database path (file path or database credentials)
  43. */
  44. EmbeddedNetworkController(Node *node,const char *ztPath,const char *dbPath, int listenPort);
  45. virtual ~EmbeddedNetworkController();
  46. virtual void init(const Identity &signingId,Sender *sender);
  47. virtual void request(
  48. uint64_t nwid,
  49. const InetAddress &fromAddr,
  50. uint64_t requestPacketId,
  51. const Identity &identity,
  52. const Dictionary<ZT_NETWORKCONFIG_METADATA_DICT_CAPACITY> &metaData);
  53. unsigned int handleControlPlaneHttpGET(
  54. const std::vector<std::string> &path,
  55. const std::map<std::string,std::string> &urlArgs,
  56. const std::map<std::string,std::string> &headers,
  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::map<std::string,std::string> &urlArgs,
  63. const std::map<std::string,std::string> &headers,
  64. const std::string &body,
  65. std::string &responseBody,
  66. std::string &responseContentType);
  67. unsigned int handleControlPlaneHttpDELETE(
  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. void handleRemoteTrace(const ZT_RemoteTrace &rt);
  75. virtual void onNetworkUpdate(const void *db,uint64_t networkId,const nlohmann::json &network);
  76. virtual void onNetworkMemberUpdate(const void *db,uint64_t networkId,uint64_t memberId,const nlohmann::json &member);
  77. virtual void onNetworkMemberDeauthorize(const void *db,uint64_t networkId,uint64_t memberId);
  78. private:
  79. void _request(uint64_t nwid,const InetAddress &fromAddr,uint64_t requestPacketId,const Identity &identity,const Dictionary<ZT_NETWORKCONFIG_METADATA_DICT_CAPACITY> &metaData);
  80. void _startThreads();
  81. struct _RQEntry
  82. {
  83. uint64_t nwid;
  84. uint64_t requestPacketId;
  85. InetAddress fromAddr;
  86. Identity identity;
  87. Dictionary<ZT_NETWORKCONFIG_METADATA_DICT_CAPACITY> metaData;
  88. enum {
  89. RQENTRY_TYPE_REQUEST = 0
  90. } type;
  91. };
  92. struct _MemberStatusKey
  93. {
  94. _MemberStatusKey() : networkId(0),nodeId(0) {}
  95. _MemberStatusKey(const uint64_t nwid,const uint64_t nid) : networkId(nwid),nodeId(nid) {}
  96. uint64_t networkId;
  97. uint64_t nodeId;
  98. inline bool operator==(const _MemberStatusKey &k) const { return ((k.networkId == networkId)&&(k.nodeId == nodeId)); }
  99. };
  100. struct _MemberStatus
  101. {
  102. _MemberStatus() : lastRequestTime(0),vMajor(-1),vMinor(-1),vRev(-1),vProto(-1) {}
  103. uint64_t lastRequestTime;
  104. int vMajor,vMinor,vRev,vProto;
  105. Dictionary<ZT_NETWORKCONFIG_METADATA_DICT_CAPACITY> lastRequestMetaData;
  106. Identity identity;
  107. inline bool online(const int64_t now) const { return ((now - lastRequestTime) < (ZT_NETWORK_AUTOCONF_DELAY * 2)); }
  108. };
  109. struct _MemberStatusHash
  110. {
  111. inline std::size_t operator()(const _MemberStatusKey &networkIdNodeId) const
  112. {
  113. return (std::size_t)(networkIdNodeId.networkId + networkIdNodeId.nodeId);
  114. }
  115. };
  116. const int64_t _startTime;
  117. int _listenPort;
  118. Node *const _node;
  119. std::string _ztPath;
  120. std::string _path;
  121. Identity _signingId;
  122. std::string _signingIdAddressString;
  123. NetworkController::Sender *_sender;
  124. DBMirrorSet _db;
  125. BlockingQueue< _RQEntry * > _queue;
  126. std::vector<std::thread> _threads;
  127. std::mutex _threads_l;
  128. std::unordered_map< _MemberStatusKey,_MemberStatus,_MemberStatusHash > _memberStatus;
  129. std::mutex _memberStatus_l;
  130. };
  131. } // namespace ZeroTier
  132. #endif