SqliteNetworkController.hpp 6.2 KB

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