RedisNetworkConfigMaster.hpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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_REDISNETWORKCONFIGMASTER_HPP
  28. #define ZT_REDISNETWORKCONFIGMASTER_HPP
  29. #include <stdint.h>
  30. #include <string>
  31. #include <map>
  32. #include <vector>
  33. #include "../node/Constants.hpp"
  34. #include "../node/NetworkConfigMaster.hpp"
  35. #include "../node/Mutex.hpp"
  36. #include <hiredis/hiredis.h>
  37. // Redis timeout in seconds
  38. #define ZT_NETCONF_REDIS_TIMEOUT 10
  39. namespace ZeroTier {
  40. class RedisNetworkConfigMaster : public NetworkConfigMaster
  41. {
  42. public:
  43. RedisNetworkConfigMaster(
  44. const Identity &signingId,
  45. const char *redisHost,
  46. unsigned int redisPort,
  47. const char *redisPassword,
  48. unsigned int redisDatabaseNumber);
  49. virtual ~RedisNetworkConfigMaster();
  50. virtual NetworkConfigMaster::ResultCode doNetworkConfigRequest(
  51. const InetAddress &fromAddr,
  52. uint64_t packetId,
  53. const Identity &member,
  54. uint64_t nwid,
  55. const Dictionary &metaData,
  56. uint64_t haveTimestamp,
  57. Dictionary &netconf);
  58. private:
  59. // These assume _lock is locked
  60. bool _reconnect();
  61. bool _hgetall(const char *key,Dictionary &hdata);
  62. bool _hmset(const char *key,const Dictionary &hdata);
  63. bool _hget(const char *key,const char *hashKey,std::string &value);
  64. bool _hset(const char *key,const char *hashKey,const char *value);
  65. bool _get(const char *key,std::string &value);
  66. bool _smembers(const char *key,std::vector<std::string> &sdata);
  67. bool _initNewMember(uint64_t nwid,const Identity &member,const Dictionary &metaData,Dictionary &memberRecord);
  68. bool _generateNetconf(uint64_t nwid,const Identity &member,const Dictionary &metaData,Dictionary &netconf,uint64_t &ts,std::string &errorMessage);
  69. Mutex _lock;
  70. Identity _signingId;
  71. std::string _redisHost;
  72. std::string _redisPassword;
  73. unsigned int _redisPort;
  74. unsigned int _redisDatabaseNumber;
  75. redisContext *_rc;
  76. };
  77. } // namespace ZeroTier
  78. #endif