SqliteNetworkController.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. * --
  19. *
  20. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #ifndef ZT_SQLITENETWORKCONTROLLER_HPP
  28. #define ZT_SQLITENETWORKCONTROLLER_HPP
  29. #include <stdint.h>
  30. #include <string>
  31. #include <map>
  32. #include <vector>
  33. #include "../node/Constants.hpp"
  34. #include "../node/NetworkController.hpp"
  35. #include "../node/Mutex.hpp"
  36. #include "../osdep/Thread.hpp"
  37. #include "../ext/offbase/offbase.hpp"
  38. namespace ZeroTier {
  39. class Node;
  40. class SqliteNetworkController : public NetworkController
  41. {
  42. public:
  43. SqliteNetworkController(Node *node,const char *dbPath);
  44. virtual ~SqliteNetworkController();
  45. virtual NetworkController::ResultCode doNetworkConfigRequest(
  46. const InetAddress &fromAddr,
  47. const Identity &signingId,
  48. const Identity &identity,
  49. uint64_t nwid,
  50. const Dictionary<ZT_NETWORKCONFIG_METADATA_DICT_CAPACITY> &metaData,
  51. NetworkConfig &nc);
  52. unsigned int handleControlPlaneHttpGET(
  53. const std::vector<std::string> &path,
  54. const std::map<std::string,std::string> &urlArgs,
  55. const std::map<std::string,std::string> &headers,
  56. const std::string &body,
  57. std::string &responseBody,
  58. std::string &responseContentType);
  59. unsigned int handleControlPlaneHttpPOST(
  60. const std::vector<std::string> &path,
  61. const std::map<std::string,std::string> &urlArgs,
  62. const std::map<std::string,std::string> &headers,
  63. const std::string &body,
  64. std::string &responseBody,
  65. std::string &responseContentType);
  66. unsigned int handleControlPlaneHttpDELETE(
  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. // threadMain() for backup thread -- do not call directly
  74. void threadMain()
  75. throw();
  76. private:
  77. unsigned int _doCPGet(
  78. const std::vector<std::string> &path,
  79. const std::map<std::string,std::string> &urlArgs,
  80. const std::map<std::string,std::string> &headers,
  81. const std::string &body,
  82. std::string &responseBody,
  83. std::string &responseContentType);
  84. static void _circuitTestCallback(ZT_Node *node,ZT_CircuitTest *test,const ZT_CircuitTestReport *report);
  85. Node *const _node;
  86. std::string _instanceId;
  87. offbase _db;
  88. Thread _dbCommitThread;
  89. volatile bool _dbCommitThreadRun;
  90. // Circuit tests outstanding
  91. struct _CircuitTestEntry
  92. {
  93. ZT_CircuitTest *test;
  94. std::string jsonResults;
  95. };
  96. std::map< uint64_t,_CircuitTestEntry > _circuitTests;
  97. // Last request time by address, for rate limitation
  98. std::map< std::pair<uint64_t,uint64_t>,uint64_t > _lastRequestTime;
  99. Mutex _lock;
  100. };
  101. } // namespace ZeroTier
  102. #endif