ManagedRoute.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2017 ZeroTier, Inc. https://www.zerotier.com/
  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. * You can be released from the requirements of the license by purchasing
  21. * a commercial license. Buying such a license is mandatory as soon as you
  22. * develop commercial closed-source software that incorporates or links
  23. * directly against ZeroTier software without disclosing the source code
  24. * of your own application.
  25. */
  26. #ifndef ZT_MANAGEDROUTE_HPP
  27. #define ZT_MANAGEDROUTE_HPP
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include "../node/InetAddress.hpp"
  31. #include "../node/Utils.hpp"
  32. #include "../node/SharedPtr.hpp"
  33. #include "../node/AtomicCounter.hpp"
  34. #include "../node/NonCopyable.hpp"
  35. #include <stdexcept>
  36. #include <vector>
  37. #include <map>
  38. namespace ZeroTier {
  39. /**
  40. * A ZT-managed route that used C++ RAII semantics to automatically clean itself up on deallocate
  41. */
  42. class ManagedRoute : NonCopyable
  43. {
  44. friend class SharedPtr<ManagedRoute>;
  45. public:
  46. ManagedRoute(const InetAddress &target,const InetAddress &via,const char *device)
  47. {
  48. _target = target;
  49. _via = via;
  50. if (via.ss_family == AF_INET)
  51. _via.setPort(32);
  52. else if (via.ss_family == AF_INET6)
  53. _via.setPort(128);
  54. Utils::scopy(_device,sizeof(_device),device);
  55. _systemDevice[0] = (char)0;
  56. }
  57. ~ManagedRoute()
  58. {
  59. this->remove();
  60. }
  61. /**
  62. * Set or update currently set route
  63. *
  64. * This must be called periodically for routes that shadow others so that
  65. * shadow routes can be updated. In some cases it has no effect
  66. *
  67. * @return True if route add/update was successful
  68. */
  69. bool sync();
  70. /**
  71. * Remove and clear this ManagedRoute
  72. *
  73. * This does nothing if this ManagedRoute is not set or has already been
  74. * removed. If this is not explicitly called it is called automatically on
  75. * destruct.
  76. */
  77. void remove();
  78. inline const InetAddress &target() const { return _target; }
  79. inline const InetAddress &via() const { return _via; }
  80. inline const char *device() const { return _device; }
  81. private:
  82. InetAddress _target;
  83. InetAddress _via;
  84. InetAddress _systemVia; // for route overrides
  85. std::map<InetAddress,bool> _applied; // routes currently applied
  86. char _device[128];
  87. char _systemDevice[128]; // for route overrides
  88. AtomicCounter __refCount;
  89. };
  90. } // namespace ZeroTier
  91. #endif