2
0

NodeConfig.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2012-2013 ZeroTier Networks LLC
  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_NODECONFIG_HPP
  28. #define _ZT_NODECONFIG_HPP
  29. #include <map>
  30. #include <set>
  31. #include <string>
  32. #include <stdint.h>
  33. #include "SharedPtr.hpp"
  34. #include "Network.hpp"
  35. #include "Utils.hpp"
  36. #include "Http.hpp"
  37. namespace ZeroTier {
  38. class RuntimeEnvironment;
  39. /**
  40. * Node configuration holder and fetcher
  41. */
  42. class NodeConfig
  43. {
  44. public:
  45. /**
  46. * @param renv Runtime environment
  47. * @param url Autoconfiguration URL (http:// or file://)
  48. */
  49. NodeConfig(const RuntimeEnvironment *renv,const std::string &url);
  50. ~NodeConfig();
  51. /**
  52. * @param nwid Network ID
  53. * @return Network or NULL if no network for that ID
  54. */
  55. inline SharedPtr<Network> network(uint64_t nwid) const
  56. {
  57. Mutex::Lock _l(_networks_m);
  58. std::map< uint64_t,SharedPtr<Network> >::const_iterator n(_networks.find(nwid));
  59. return ((n == _networks.end()) ? SharedPtr<Network>() : n->second);
  60. }
  61. /**
  62. * @return Vector containing all networks
  63. */
  64. inline std::vector< SharedPtr<Network> > networks() const
  65. {
  66. std::vector< SharedPtr<Network> > nwlist;
  67. Mutex::Lock _l(_networks_m);
  68. for(std::map< uint64_t,SharedPtr<Network> >::const_iterator n(_networks.begin());n!=_networks.end();++n)
  69. nwlist.push_back(n->second);
  70. return nwlist;
  71. }
  72. /**
  73. * Call whack() on all networks' tap devices
  74. */
  75. inline void whackAllTaps()
  76. {
  77. std::vector< SharedPtr<Network> > nwlist;
  78. Mutex::Lock _l(_networks_m);
  79. for(std::map< uint64_t,SharedPtr<Network> >::const_iterator n(_networks.begin());n!=_networks.end();++n)
  80. n->second->tap().whack();
  81. }
  82. /**
  83. * @param nwid Network ID
  84. * @return True if this network exists
  85. */
  86. inline bool hasNetwork(uint64_t nwid)
  87. {
  88. Mutex::Lock _l(_networks_m);
  89. return (_networks.count(nwid) > 0);
  90. }
  91. /**
  92. * @return Set of network tap device names
  93. */
  94. inline std::set<std::string> networkTapDeviceNames() const
  95. {
  96. std::set<std::string> tapDevs;
  97. Mutex::Lock _l(_networks_m);
  98. for(std::map< uint64_t,SharedPtr<Network> >::const_iterator n(_networks.begin());n!=_networks.end();++n)
  99. tapDevs.insert(n->second->tap().deviceName());
  100. return tapDevs;
  101. }
  102. /**
  103. * @return Time of last successful autoconfigure or refresh
  104. */
  105. inline uint64_t lastAutoconfigure() const { return _lastAutoconfigure; }
  106. /**
  107. * @return Autoconfiguration URL
  108. */
  109. inline const std::string &url() const { return _url; }
  110. /**
  111. * Refresh configuration from autoconf URL
  112. */
  113. void refreshConfiguration();
  114. private:
  115. void __CBautoconfHandler(const std::string &lastModified,const std::string &body);
  116. static bool _CBautoconfHandler(Http::Request *req,void *arg,const std::string &url,int code,const std::map<std::string,std::string> &headers,const std::string &body);
  117. const RuntimeEnvironment *_r;
  118. volatile uint64_t _lastAutoconfigure;
  119. std::string _lastAutoconfigureLastModified;
  120. std::string _url;
  121. Mutex _autoconfigureLock;
  122. std::map< uint64_t,SharedPtr<Network> > _networks;
  123. Mutex _networks_m;
  124. };
  125. } // namespace ZeroTier
  126. #endif