NetworkConfigMaster.hpp 3.8 KB

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