SqliteNetworkController.hpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. namespace ZeroTier {
  40. class SqliteNetworkController : public NetworkController
  41. {
  42. public:
  43. SqliteNetworkController(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 &metaData,
  51. Dictionary &netconf);
  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. private:
  74. enum IpAssignmentType {
  75. // IP assignment is a static IP address
  76. ZT_IP_ASSIGNMENT_TYPE_ADDRESS = 0,
  77. // IP assignment is a network -- a route via this interface, not an address
  78. ZT_IP_ASSIGNMENT_TYPE_NETWORK = 1
  79. };
  80. unsigned int _doCPGet(
  81. const std::vector<std::string> &path,
  82. const std::map<std::string,std::string> &urlArgs,
  83. const std::map<std::string,std::string> &headers,
  84. const std::string &body,
  85. std::string &responseBody,
  86. std::string &responseContentType);
  87. NetworkController::ResultCode _doNetworkConfigRequest(
  88. const InetAddress &fromAddr,
  89. const Identity &signingId,
  90. const Identity &identity,
  91. uint64_t nwid,
  92. const Dictionary &metaData,
  93. Dictionary &netconf);
  94. std::string _dbPath;
  95. std::string _instanceId;
  96. // A circular buffer last log
  97. struct _LLEntry
  98. {
  99. _LLEntry()
  100. {
  101. for(long i=0;i<ZT_SQLITENETWORKCONTROLLER_IN_MEMORY_LOG_SIZE;++i)
  102. this->l[i].ts = 0;
  103. this->lastRequestTime = 0;
  104. this->totalRequests = 0;
  105. }
  106. // Circular buffer of last log entries
  107. struct {
  108. uint64_t ts; // timestamp or 0 if circular buffer entry unused
  109. char version[64];
  110. InetAddress fromAddr;
  111. bool authorized;
  112. } l[ZT_SQLITENETWORKCONTROLLER_IN_MEMORY_LOG_SIZE];
  113. // Time of last request whether successful or not
  114. uint64_t lastRequestTime;
  115. // Total requests by this address / network ID pair (also serves mod IN_MEMORY_LOG_SIZE as circular buffer ptr)
  116. uint64_t totalRequests;
  117. };
  118. // Last log entries by address and network ID pair
  119. std::map< std::pair<Address,uint64_t>,_LLEntry > _lastLog;
  120. sqlite3 *_db;
  121. sqlite3_stmt *_sGetNetworkById;
  122. sqlite3_stmt *_sGetMember;
  123. sqlite3_stmt *_sCreateMember;
  124. sqlite3_stmt *_sGetNodeIdentity;
  125. sqlite3_stmt *_sCreateOrReplaceNode;
  126. sqlite3_stmt *_sUpdateNode;
  127. sqlite3_stmt *_sUpdateNode2;
  128. sqlite3_stmt *_sGetEtherTypesFromRuleTable;
  129. sqlite3_stmt *_sGetActiveBridges;
  130. sqlite3_stmt *_sGetIpAssignmentsForNode;
  131. sqlite3_stmt *_sGetIpAssignmentPools;
  132. sqlite3_stmt *_sGetLocalRoutes;
  133. sqlite3_stmt *_sCheckIfIpIsAllocated;
  134. sqlite3_stmt *_sAllocateIp;
  135. sqlite3_stmt *_sDeleteIpAllocations;
  136. sqlite3_stmt *_sDeleteLocalRoutes;
  137. sqlite3_stmt *_sGetRelays;
  138. sqlite3_stmt *_sListNetworks;
  139. sqlite3_stmt *_sListNetworkMembers;
  140. sqlite3_stmt *_sGetMember2;
  141. sqlite3_stmt *_sGetIpAssignmentPools2;
  142. sqlite3_stmt *_sListRules;
  143. sqlite3_stmt *_sCreateRule;
  144. sqlite3_stmt *_sCreateNetwork;
  145. sqlite3_stmt *_sGetNetworkRevision;
  146. sqlite3_stmt *_sSetNetworkRevision;
  147. sqlite3_stmt *_sGetIpAssignmentsForNode2;
  148. sqlite3_stmt *_sDeleteRelaysForNetwork;
  149. sqlite3_stmt *_sCreateRelay;
  150. sqlite3_stmt *_sDeleteIpAssignmentPoolsForNetwork;
  151. sqlite3_stmt *_sDeleteRulesForNetwork;
  152. sqlite3_stmt *_sCreateIpAssignmentPool;
  153. sqlite3_stmt *_sUpdateMemberAuthorized;
  154. sqlite3_stmt *_sUpdateMemberActiveBridge;
  155. sqlite3_stmt *_sDeleteMember;
  156. sqlite3_stmt *_sDeleteAllNetworkMembers;
  157. sqlite3_stmt *_sDeleteNetwork;
  158. sqlite3_stmt *_sGetGateways;
  159. sqlite3_stmt *_sDeleteGateways;
  160. sqlite3_stmt *_sCreateGateway;
  161. sqlite3_stmt *_sIncrementMemberRevisionCounter;
  162. sqlite3_stmt *_sGetConfig;
  163. sqlite3_stmt *_sSetConfig;
  164. Mutex _lock;
  165. };
  166. } // namespace ZeroTier
  167. #endif