SqliteNetworkController.hpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 <sqlite3.h>
  31. #include <string>
  32. #include <map>
  33. #include <vector>
  34. #include "../node/Constants.hpp"
  35. #include "../node/NetworkController.hpp"
  36. #include "../node/Mutex.hpp"
  37. #include "../osdep/Thread.hpp"
  38. // Number of in-memory last log entries to maintain per user
  39. #define ZT_SQLITENETWORKCONTROLLER_IN_MEMORY_LOG_SIZE 32
  40. // How long do circuit tests "live"? This is just to prevent buildup in memory.
  41. #define ZT_SQLITENETWORKCONTROLLER_CIRCUIT_TEST_TIMEOUT 300000
  42. namespace ZeroTier {
  43. class Node;
  44. class SqliteNetworkController : public NetworkController
  45. {
  46. public:
  47. SqliteNetworkController(Node *node,const char *dbPath,const char *circuitTestPath);
  48. virtual ~SqliteNetworkController();
  49. virtual NetworkController::ResultCode doNetworkConfigRequest(
  50. const InetAddress &fromAddr,
  51. const Identity &signingId,
  52. const Identity &identity,
  53. uint64_t nwid,
  54. const Dictionary &metaData,
  55. Dictionary &netconf);
  56. unsigned int handleControlPlaneHttpGET(
  57. const std::vector<std::string> &path,
  58. const std::map<std::string,std::string> &urlArgs,
  59. const std::map<std::string,std::string> &headers,
  60. const std::string &body,
  61. std::string &responseBody,
  62. std::string &responseContentType);
  63. unsigned int handleControlPlaneHttpPOST(
  64. const std::vector<std::string> &path,
  65. const std::map<std::string,std::string> &urlArgs,
  66. const std::map<std::string,std::string> &headers,
  67. const std::string &body,
  68. std::string &responseBody,
  69. std::string &responseContentType);
  70. unsigned int handleControlPlaneHttpDELETE(
  71. const std::vector<std::string> &path,
  72. const std::map<std::string,std::string> &urlArgs,
  73. const std::map<std::string,std::string> &headers,
  74. const std::string &body,
  75. std::string &responseBody,
  76. std::string &responseContentType);
  77. // threadMain() for backup thread -- do not call directly
  78. void threadMain()
  79. throw();
  80. private:
  81. enum IpAssignmentType {
  82. // IP assignment is a static IP address
  83. ZT_IP_ASSIGNMENT_TYPE_ADDRESS = 0,
  84. // IP assignment is a network -- a route via this interface, not an address
  85. ZT_IP_ASSIGNMENT_TYPE_NETWORK = 1
  86. };
  87. unsigned int _doCPGet(
  88. const std::vector<std::string> &path,
  89. const std::map<std::string,std::string> &urlArgs,
  90. const std::map<std::string,std::string> &headers,
  91. const std::string &body,
  92. std::string &responseBody,
  93. std::string &responseContentType);
  94. NetworkController::ResultCode _doNetworkConfigRequest(
  95. const InetAddress &fromAddr,
  96. const Identity &signingId,
  97. const Identity &identity,
  98. uint64_t nwid,
  99. const Dictionary &metaData,
  100. Dictionary &netconf);
  101. static void _circuitTestCallback(ZT_Node *node,ZT_CircuitTest *test,const ZT_CircuitTestReport *report);
  102. Node *_node;
  103. Thread _backupThread;
  104. volatile bool _backupThreadRun;
  105. std::string _dbPath;
  106. std::string _circuitTestPath;
  107. std::string _instanceId;
  108. // A circular buffer last log
  109. struct _LLEntry
  110. {
  111. _LLEntry()
  112. {
  113. for(long i=0;i<ZT_SQLITENETWORKCONTROLLER_IN_MEMORY_LOG_SIZE;++i)
  114. this->l[i].ts = 0;
  115. this->lastRequestTime = 0;
  116. this->totalRequests = 0;
  117. }
  118. // Circular buffer of last log entries
  119. struct {
  120. uint64_t ts; // timestamp or 0 if circular buffer entry unused
  121. char version[64];
  122. InetAddress fromAddr;
  123. bool authorized;
  124. } l[ZT_SQLITENETWORKCONTROLLER_IN_MEMORY_LOG_SIZE];
  125. // Time of last request whether successful or not
  126. uint64_t lastRequestTime;
  127. // Total requests by this address / network ID pair (also serves mod IN_MEMORY_LOG_SIZE as circular buffer ptr)
  128. uint64_t totalRequests;
  129. };
  130. // Last log entries by address and network ID pair
  131. std::map< std::pair<Address,uint64_t>,_LLEntry > _lastLog;
  132. // Circuit tests outstanding
  133. std::map< uint64_t,ZT_CircuitTest * > _circuitTests;
  134. sqlite3 *_db;
  135. sqlite3_stmt *_sGetNetworkById;
  136. sqlite3_stmt *_sGetMember;
  137. sqlite3_stmt *_sCreateMember;
  138. sqlite3_stmt *_sGetNodeIdentity;
  139. sqlite3_stmt *_sCreateOrReplaceNode;
  140. sqlite3_stmt *_sGetEtherTypesFromRuleTable;
  141. sqlite3_stmt *_sGetActiveBridges;
  142. sqlite3_stmt *_sGetIpAssignmentsForNode;
  143. sqlite3_stmt *_sGetIpAssignmentPools;
  144. sqlite3_stmt *_sGetLocalRoutes;
  145. sqlite3_stmt *_sCheckIfIpIsAllocated;
  146. sqlite3_stmt *_sAllocateIp;
  147. sqlite3_stmt *_sDeleteIpAllocations;
  148. sqlite3_stmt *_sDeleteLocalRoutes;
  149. sqlite3_stmt *_sGetRelays;
  150. sqlite3_stmt *_sListNetworks;
  151. sqlite3_stmt *_sListNetworkMembers;
  152. sqlite3_stmt *_sGetMember2;
  153. sqlite3_stmt *_sGetIpAssignmentPools2;
  154. sqlite3_stmt *_sListRules;
  155. sqlite3_stmt *_sCreateRule;
  156. sqlite3_stmt *_sCreateNetwork;
  157. sqlite3_stmt *_sGetNetworkRevision;
  158. sqlite3_stmt *_sSetNetworkRevision;
  159. sqlite3_stmt *_sGetIpAssignmentsForNode2;
  160. sqlite3_stmt *_sDeleteRelaysForNetwork;
  161. sqlite3_stmt *_sCreateRelay;
  162. sqlite3_stmt *_sDeleteIpAssignmentPoolsForNetwork;
  163. sqlite3_stmt *_sDeleteRulesForNetwork;
  164. sqlite3_stmt *_sCreateIpAssignmentPool;
  165. sqlite3_stmt *_sUpdateMemberAuthorized;
  166. sqlite3_stmt *_sUpdateMemberActiveBridge;
  167. sqlite3_stmt *_sDeleteMember;
  168. sqlite3_stmt *_sDeleteAllNetworkMembers;
  169. sqlite3_stmt *_sDeleteNetwork;
  170. sqlite3_stmt *_sGetGateways;
  171. sqlite3_stmt *_sDeleteGateways;
  172. sqlite3_stmt *_sCreateGateway;
  173. sqlite3_stmt *_sIncrementMemberRevisionCounter;
  174. sqlite3_stmt *_sGetConfig;
  175. sqlite3_stmt *_sSetConfig;
  176. Mutex _lock;
  177. };
  178. } // namespace ZeroTier
  179. #endif