SqliteNetworkController.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 "../node/NonCopyable.hpp"
  38. namespace ZeroTier {
  39. class SqliteNetworkController : public NetworkController
  40. {
  41. public:
  42. class DBC;
  43. friend class SqliteNetworkController::DBC;
  44. SqliteNetworkController(const char *dbPath);
  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. uint64_t haveRevision,
  53. Dictionary &netconf);
  54. private:
  55. std::string _dbPath;
  56. sqlite3 *_db;
  57. sqlite3_stmt *_sGetNetworkById;
  58. sqlite3_stmt *_sGetMemberByNetworkAndNodeId;
  59. sqlite3_stmt *_sCreateMember;
  60. sqlite3_stmt *_sGetNodeIdentity;
  61. sqlite3_stmt *_sCreateNode;
  62. sqlite3_stmt *_sUpdateNode;
  63. sqlite3_stmt *_sUpdateNode2;
  64. sqlite3_stmt *_sUpdateMemberClientReportedRevision;
  65. sqlite3_stmt *_sGetEtherTypesFromRuleTable;
  66. sqlite3_stmt *_sGetMulticastRates;
  67. sqlite3_stmt *_sGetActiveBridges;
  68. sqlite3_stmt *_sGetIpAssignmentsForNode;
  69. sqlite3_stmt *_sGetIpAssignmentPools;
  70. sqlite3_stmt *_sCheckIfIpIsAllocated;
  71. sqlite3_stmt *_sAllocateIp;
  72. sqlite3_stmt *_sCacheNetconf;
  73. Mutex _lock;
  74. public:
  75. /**
  76. * Provides a safe interface for direct access to this master's database
  77. *
  78. * This acts as both a contextual lock of the master's Mutex and a pointer
  79. * to the Sqlite3 database instance. Dereferencing this with * yields the
  80. * sqlite3* pointer. Create on parent with DBC(SqliteNetworkController &).
  81. */
  82. class DBC : NonCopyable
  83. {
  84. public:
  85. DBC(SqliteNetworkController &nc) : _p(&nc) { nc._lock.lock(); }
  86. ~DBC() { _p->_lock.unlock(); }
  87. inline sqlite3 *operator*() const throw() { return _p->_db; }
  88. private:
  89. SqliteNetworkController *const _p;
  90. };
  91. };
  92. } // namespace ZeroTier
  93. #endif