EmbeddedNetworkController.hpp 4.9 KB

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