NetworkConfigMaster.hpp 4.1 KB

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