2
0

SqliteNetworkController.hpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 "../node/Constants.hpp"
  25. #include "../node/NetworkController.hpp"
  26. #include "../node/Mutex.hpp"
  27. #include "../node/Utils.hpp"
  28. #include "../osdep/OSUtils.hpp"
  29. #include "../ext/json/json.hpp"
  30. namespace ZeroTier {
  31. class Node;
  32. class SqliteNetworkController : public NetworkController
  33. {
  34. public:
  35. SqliteNetworkController(Node *node,const char *dbPath);
  36. virtual ~SqliteNetworkController();
  37. virtual NetworkController::ResultCode doNetworkConfigRequest(
  38. const InetAddress &fromAddr,
  39. const Identity &signingId,
  40. const Identity &identity,
  41. uint64_t nwid,
  42. const Dictionary<ZT_NETWORKCONFIG_METADATA_DICT_CAPACITY> &metaData,
  43. NetworkConfig &nc);
  44. unsigned int handleControlPlaneHttpGET(
  45. const std::vector<std::string> &path,
  46. const std::map<std::string,std::string> &urlArgs,
  47. const std::map<std::string,std::string> &headers,
  48. const std::string &body,
  49. std::string &responseBody,
  50. std::string &responseContentType);
  51. unsigned int handleControlPlaneHttpPOST(
  52. const std::vector<std::string> &path,
  53. const std::map<std::string,std::string> &urlArgs,
  54. const std::map<std::string,std::string> &headers,
  55. const std::string &body,
  56. std::string &responseBody,
  57. std::string &responseContentType);
  58. unsigned int handleControlPlaneHttpDELETE(
  59. const std::vector<std::string> &path,
  60. const std::map<std::string,std::string> &urlArgs,
  61. const std::map<std::string,std::string> &headers,
  62. const std::string &body,
  63. std::string &responseBody,
  64. std::string &responseContentType);
  65. private:
  66. unsigned int _doCPGet(
  67. const std::vector<std::string> &path,
  68. const std::map<std::string,std::string> &urlArgs,
  69. const std::map<std::string,std::string> &headers,
  70. const std::string &body,
  71. std::string &responseBody,
  72. std::string &responseContentType);
  73. static void _circuitTestCallback(ZT_Node *node,ZT_CircuitTest *test,const ZT_CircuitTestReport *report);
  74. inline nlohmann::json _readJson(const std::string &path)
  75. {
  76. std::string buf;
  77. if (OSUtils::readFile(path.c_str(),buf)) {
  78. try {
  79. return nlohmann::json::parse(buf);
  80. } catch ( ... ) {}
  81. }
  82. return nlohmann::json::object();
  83. }
  84. inline bool _writeJson(const std::string &path,const nlohmann::json &obj)
  85. {
  86. std::string buf(obj.dump(2));
  87. return OSUtils::writeFile(path.c_str(),buf);
  88. }
  89. inline std::string _networkBP(const uint64_t nwid,bool create)
  90. {
  91. char tmp[64];
  92. Utils::snprintf(tmp,sizeof(tmp),"%.16llx",nwid);
  93. std::string p(_path + ZT_PATH_SEPARATOR_S + "network");
  94. if (create) OSUtils::mkdir(p.c_str());
  95. p.push_back(ZT_PATH_SEPARATOR);
  96. p.append(tmp);
  97. if (create) OSUtils::mkdir(p.c_str());
  98. return p;
  99. }
  100. inline std::string _networkJP(const uint64_t nwid,bool create)
  101. {
  102. return (_networkBP(nwid,create) + ZT_PATH_SEPARATOR + "config.json");
  103. }
  104. inline std::string _memberBP(const uint64_t nwid,const Address &member,bool create)
  105. {
  106. std::string p(_networkBP(nwid,create));
  107. p.push_back(ZT_PATH_SEPARATOR);
  108. p.append("member");
  109. if (create) OSUtils::mkdir(p.c_str());
  110. p.push_back(ZT_PATH_SEPARATOR);
  111. p.append(member.toString());
  112. if (create) OSUtils::mkdir(p.c_str());
  113. return p;
  114. }
  115. inline std::string _memberJP(const uint64_t nwid,const Address &member,bool create)
  116. {
  117. return (_memberBP(nwid,member,create) + ZT_PATH_SEPARATOR + "config.json");
  118. }
  119. // These are const after construction
  120. Node *const _node;
  121. std::string _path;
  122. // Circuit tests outstanding
  123. struct _CircuitTestEntry
  124. {
  125. ZT_CircuitTest *test;
  126. std::string jsonResults;
  127. };
  128. std::map< uint64_t,_CircuitTestEntry > _circuitTests;
  129. Mutex _circuitTests_m;
  130. // Last request time by address, for rate limitation
  131. std::map< std::pair<uint64_t,uint64_t>,uint64_t > _lastRequestTime;
  132. Mutex _lastRequestTime_m;
  133. };
  134. } // namespace ZeroTier
  135. #endif