NetworkConfigMaster.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2011-2015 ZeroTier Networks
  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_NETWORKCONFIGMASTER_HPP
  28. #define ZT_NETWORKCONFIGMASTER_HPP
  29. #include "Constants.hpp"
  30. #define ZT_ENABLE_NETCONF_MASTER
  31. #ifdef ZT_ENABLE_NETCONF_MASTER
  32. #include <stdint.h>
  33. #include <string>
  34. #include <map>
  35. #include "Address.hpp"
  36. #include "Dictionary.hpp"
  37. #include "Mutex.hpp"
  38. #include <hiredis/hiredis.h>
  39. namespace ZeroTier {
  40. class RuntimeEnvironment;
  41. /**
  42. * Network configuration master -- responds to NETCONF requests
  43. *
  44. * This requires the 'hiredis' C library to build.
  45. */
  46. class NetworkConfigMaster
  47. {
  48. public:
  49. /**
  50. * Create netconf master
  51. *
  52. * This doesn't connect to Redis until the first request is received.
  53. *
  54. * @param renv Runtime environment
  55. * @param redisHost Hostname or IP of Redis server
  56. * @param redisPort Redis IP port number
  57. * @param redisPassword Redis AUTH password or NULL if none
  58. * @param redisDatabaseNumber Redis database number (usually 0)
  59. */
  60. NetworkConfigMaster(
  61. const RuntimeEnvironment *renv,
  62. const char *redisHost,
  63. unsigned int redisPort,
  64. const char *redisPassword,
  65. unsigned int redisDatabaseNumber);
  66. ~NetworkConfigMaster();
  67. /**
  68. * Handle a network config request, sending replies if necessary
  69. *
  70. * This is a blocking call, so rate is limited by Redis. It will fail
  71. * and log its failure if the Redis server is not available or times out.
  72. *
  73. * @param packetId 64-bit packet ID
  74. * @param from Originating peer ZeroTier address
  75. * @param nwid 64-bit network ID
  76. * @param metaData Meta-data bundled with request (empty if none)
  77. * @param haveTimestamp Timestamp requesting peer has or 0 if none or not included
  78. */
  79. void doNetworkConfigRequest(
  80. uint64_t packetId,
  81. const Address &from,
  82. uint64_t nwid,
  83. const Dictionary &metaData,
  84. uint64_t haveTimestamp);
  85. private:
  86. // These assume _lock is locked
  87. bool _reconnect();
  88. bool _hgetall(const char *key,std::map<std::string,std::string> &hdata);
  89. bool _hmset(const char *key,const std::map<std::string,std::string> &hdata);
  90. bool _hget(const char *key,const char *hashKey,std::string &value);
  91. bool _hset(const char *key,const char *hashKey,const char *value);
  92. Mutex _lock;
  93. std::string _redisHost;
  94. std::string _redisPassword;
  95. unsigned int _redisPort;
  96. unsigned int _redisDatabaseNumber;
  97. const RuntimeEnvironment *RR;
  98. redisContext *_rc;
  99. };
  100. } // namespace ZeroTier
  101. #endif // ZT_ENABLE_NETCONF_MASTER
  102. #endif