NetworkConfigMaster.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. #include "Constants.hpp"
  28. #ifdef ZT_ENABLE_NETCONF_MASTER
  29. #include <stdint.h>
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <time.h>
  34. #include <sys/time.h>
  35. #include <sys/types.h>
  36. #include "NetworkConfigMaster.hpp"
  37. #include "RuntimeEnvironment.hpp"
  38. #include "Switch.hpp"
  39. #include "Packet.hpp"
  40. #include "NetworkConfig.hpp"
  41. #include "Utils.hpp"
  42. #include "Node.hpp"
  43. #include "Logger.hpp"
  44. // Redis timeout in seconds
  45. #define ZT_NETCONF_REDIS_TIMEOUT 10
  46. namespace ZeroTier {
  47. NetworkConfigMaster::NetworkConfigMaster(
  48. const RuntimeEnvironment *renv,
  49. const char *redisHost,
  50. unsigned int redisPort,
  51. const char *redisPassword,
  52. unsigned int redisDatabaseNumber) :
  53. _lock(),
  54. _redisHost(redisHost),
  55. _redisPassword((redisPassword) ? redisPassword : ""),
  56. _redisPort(redisPort),
  57. _redisDatabaseNumber(redisDatabaseNumber),
  58. RR(renv),
  59. _rc((redisContext *)0)
  60. {
  61. }
  62. NetworkConfigMaster::~NetworkConfigMaster()
  63. {
  64. Mutex::Lock _l(_lock);
  65. if (_rc)
  66. redisFree(_rc);
  67. }
  68. void NetworkConfigMaster::doNetworkConfigRequest(
  69. uint64_t packetId,
  70. const Address &from,
  71. uint64_t nwid,
  72. const Dictionary &metaData,
  73. uint64_t haveTimestamp)
  74. {
  75. }
  76. bool NetworkConfigMaster::_reconnect()
  77. {
  78. struct timeval tv;
  79. if (_rc)
  80. redisFree(_rc);
  81. tv.tv_sec = ZT_NETCONF_REDIS_TIMEOUT;
  82. tv.tv_usec = 0;
  83. _rc = redisConnectWithTimeout(_redisHost.c_str(),_redisPort,&tv);
  84. if (!_rc)
  85. return false;
  86. if (_rc->err) {
  87. redisFree(_rc);
  88. _rc = (redisContext *)0;
  89. return false;
  90. }
  91. redisSetTimeout(_rc,&tv); // necessary???
  92. // TODO: support AUTH and SELECT !!!
  93. return true;
  94. }
  95. bool NetworkConfigMaster::_hgetall(const char *key,std::map<std::string,std::string> &hdata)
  96. {
  97. if (!_rc) {
  98. if (!_reconnect())
  99. return false;
  100. }
  101. redisReply *reply = (redisReply *)redisCommand(_rc,"HGETALL %s",key);
  102. if (!reply) {
  103. if (_reconnect())
  104. return _hgetall(key,hdata);
  105. return false;
  106. }
  107. hdata.clear();
  108. if (reply->type == REDIS_REPLY_ARRAY) {
  109. for(long i=0;i<reply->elements;) {
  110. try {
  111. const char *k = reply->elements[i]->str;
  112. if (++i >= reply->elements)
  113. break;
  114. if ((k)&&(reply->elements[i]->str))
  115. hdata[k] = reply->elements[i]->str;
  116. ++i;
  117. } catch ( ... ) {
  118. break; // memory safety
  119. }
  120. }
  121. }
  122. freeReplyObject(reply);
  123. return true;
  124. }
  125. bool NetworkConfigMaster::_hmset(const char *key,const std::map<std::string,std::string> &hdata)
  126. {
  127. const const char *hargv[1024];
  128. if (!hdata.size())
  129. return true;
  130. if (!_rc) {
  131. if (!_reconnect())
  132. return false;
  133. }
  134. hargv[0] = "HMSET";
  135. hargv[1] = key;
  136. int hargc = 2;
  137. for(std::map<std::string,std::string>::const_iterator i(hdata.begin());i!=hdata.end();++i) {
  138. if (hargc >= 1024)
  139. break;
  140. hargv[hargc++] = i->first.c_str();
  141. hargv[hargc++] = i->second.c_str();
  142. }
  143. redisReply *reply = (redisReply *)redisCommandArgv(_rc,hargc,hargv,(const size_t *)0);
  144. if (!reply) {
  145. if (_reconnect())
  146. return _hmset(key,hdata);
  147. return false;
  148. }
  149. if (reply->type == REDIS_REPLY_ERROR) {
  150. freeReplyObject(reply);
  151. return false;
  152. }
  153. freeReplyObject(reply);
  154. return true;
  155. }
  156. bool NetworkConfigMaster::_hget(const char *key,const char *hashKey,std::string &value)
  157. {
  158. if (!_rc) {
  159. if (!_reconnect())
  160. return false;
  161. }
  162. redisReply *reply = (redisReply *)redisCommand(_rc,"HGET %s %s",key,hashKey);
  163. if (!reply) {
  164. if (_reconnect())
  165. return _hget(key,hashKey,value);
  166. return false;
  167. }
  168. if (reply->type == REDIS_REPLY_STRING)
  169. value = reply->str;
  170. else value = "";
  171. freeReplyObject(reply);
  172. return true;
  173. }
  174. bool NetworkConfigMaster::_hset(const char *key,const char *hashKey,const char *value)
  175. {
  176. if (!_rc) {
  177. if (!_reconnect())
  178. return false;
  179. }
  180. redisReply *reply = (redisReply *)redisCommand(_rc,"HSET %s %s %s",key,hashKey,value);
  181. if (!reply) {
  182. if (_reconnect())
  183. return _hset(key,hashKey,value);
  184. return false;
  185. }
  186. if (reply->type == REDIS_REPLY_ERROR) {
  187. freeReplyObject(reply);
  188. return false;
  189. }
  190. freeReplyObject(reply);
  191. return true;
  192. }
  193. } // namespace ZeroTier
  194. #endif // ZT_ENABLE_NETCONF_MASTER