SqliteNetworkController.hpp 5.8 KB

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