SqliteNetworkController.hpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. namespace ZeroTier {
  38. class SqliteNetworkController : public NetworkController
  39. {
  40. public:
  41. SqliteNetworkController(const char *dbPath);
  42. virtual ~SqliteNetworkController();
  43. virtual NetworkController::ResultCode doNetworkConfigRequest(
  44. const InetAddress &fromAddr,
  45. const Identity &signingId,
  46. const Identity &identity,
  47. uint64_t nwid,
  48. const Dictionary &metaData,
  49. Dictionary &netconf);
  50. unsigned int handleControlPlaneHttpGET(
  51. const std::vector<std::string> &path,
  52. const std::map<std::string,std::string> &urlArgs,
  53. const std::map<std::string,std::string> &headers,
  54. const std::string &body,
  55. std::string &responseBody,
  56. std::string &responseContentType);
  57. unsigned int handleControlPlaneHttpPOST(
  58. const std::vector<std::string> &path,
  59. const std::map<std::string,std::string> &urlArgs,
  60. const std::map<std::string,std::string> &headers,
  61. const std::string &body,
  62. std::string &responseBody,
  63. std::string &responseContentType);
  64. unsigned int handleControlPlaneHttpDELETE(
  65. const std::vector<std::string> &path,
  66. const std::map<std::string,std::string> &urlArgs,
  67. const std::map<std::string,std::string> &headers,
  68. const std::string &body,
  69. std::string &responseBody,
  70. std::string &responseContentType);
  71. private:
  72. enum IpAssignmentType {
  73. // IP assignment is a static IP address
  74. ZT_IP_ASSIGNMENT_TYPE_ADDRESS = 0,
  75. // IP assignment is a network -- a route via this interface, not an address
  76. ZT_IP_ASSIGNMENT_TYPE_NETWORK = 1
  77. };
  78. unsigned int _doCPGet(
  79. const std::vector<std::string> &path,
  80. const std::map<std::string,std::string> &urlArgs,
  81. const std::map<std::string,std::string> &headers,
  82. const std::string &body,
  83. std::string &responseBody,
  84. std::string &responseContentType);
  85. std::string _dbPath;
  86. std::string _instanceId;
  87. std::map< std::pair<Address,uint64_t>,uint64_t > _lastRequestTime;
  88. sqlite3 *_db;
  89. sqlite3_stmt *_sGetNetworkById;
  90. sqlite3_stmt *_sGetMember;
  91. sqlite3_stmt *_sCreateMember;
  92. sqlite3_stmt *_sGetNodeIdentity;
  93. sqlite3_stmt *_sCreateOrReplaceNode;
  94. sqlite3_stmt *_sUpdateNode;
  95. sqlite3_stmt *_sUpdateNode2;
  96. sqlite3_stmt *_sGetEtherTypesFromRuleTable;
  97. sqlite3_stmt *_sGetActiveBridges;
  98. sqlite3_stmt *_sGetIpAssignmentsForNode;
  99. sqlite3_stmt *_sGetIpAssignmentPools;
  100. sqlite3_stmt *_sGetLocalRoutes;
  101. sqlite3_stmt *_sCheckIfIpIsAllocated;
  102. sqlite3_stmt *_sAllocateIp;
  103. sqlite3_stmt *_sDeleteIpAllocations;
  104. sqlite3_stmt *_sDeleteLocalRoutes;
  105. sqlite3_stmt *_sGetRelays;
  106. sqlite3_stmt *_sListNetworks;
  107. sqlite3_stmt *_sListNetworkMembers;
  108. sqlite3_stmt *_sGetMember2;
  109. sqlite3_stmt *_sGetIpAssignmentPools2;
  110. sqlite3_stmt *_sListRules;
  111. sqlite3_stmt *_sCreateRule;
  112. sqlite3_stmt *_sCreateNetwork;
  113. sqlite3_stmt *_sGetNetworkRevision;
  114. sqlite3_stmt *_sSetNetworkRevision;
  115. sqlite3_stmt *_sGetIpAssignmentsForNode2;
  116. sqlite3_stmt *_sDeleteRelaysForNetwork;
  117. sqlite3_stmt *_sCreateRelay;
  118. sqlite3_stmt *_sDeleteIpAssignmentPoolsForNetwork;
  119. sqlite3_stmt *_sDeleteRulesForNetwork;
  120. sqlite3_stmt *_sCreateIpAssignmentPool;
  121. sqlite3_stmt *_sUpdateMemberAuthorized;
  122. sqlite3_stmt *_sUpdateMemberActiveBridge;
  123. sqlite3_stmt *_sDeleteMember;
  124. sqlite3_stmt *_sDeleteNetwork;
  125. sqlite3_stmt *_sGetGateways;
  126. sqlite3_stmt *_sDeleteGateways;
  127. sqlite3_stmt *_sCreateGateway;
  128. sqlite3_stmt *_sIncrementMemberRevisionCounter;
  129. sqlite3_stmt *_sGetConfig;
  130. sqlite3_stmt *_sSetConfig;
  131. sqlite3_stmt *_sPutLog;
  132. sqlite3_stmt *_sGetMemberLog;
  133. sqlite3_stmt *_sGetRecentMemberLog;
  134. Mutex _lock;
  135. };
  136. } // namespace ZeroTier
  137. #endif