EmbeddedNetworkController.hpp 4.9 KB

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