ManagedRoute.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2019 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 <stdexcept>
  35. #include <vector>
  36. #include <map>
  37. namespace ZeroTier {
  38. /**
  39. * A ZT-managed route that used C++ RAII semantics to automatically clean itself up on deallocate
  40. */
  41. class ManagedRoute
  42. {
  43. friend class SharedPtr<ManagedRoute>;
  44. public:
  45. ManagedRoute(const InetAddress &target,const InetAddress &via,const InetAddress &src,const char *device)
  46. {
  47. _target = target;
  48. _via = via;
  49. _src = src;
  50. if (via.ss_family == AF_INET)
  51. _via.setPort(32);
  52. else if (via.ss_family == AF_INET6)
  53. _via.setPort(128);
  54. if (src.ss_family == AF_INET) {
  55. _src.setPort(32);
  56. } else if (src.ss_family == AF_INET6) {
  57. _src.setPort(128);
  58. }
  59. Utils::scopy(_device,sizeof(_device),device);
  60. _systemDevice[0] = (char)0;
  61. }
  62. ~ManagedRoute()
  63. {
  64. this->remove();
  65. }
  66. /**
  67. * Set or update currently set route
  68. *
  69. * This must be called periodically for routes that shadow others so that
  70. * shadow routes can be updated. In some cases it has no effect
  71. *
  72. * @return True if route add/update was successful
  73. */
  74. bool sync();
  75. /**
  76. * Remove and clear this ManagedRoute
  77. *
  78. * This does nothing if this ManagedRoute is not set or has already been
  79. * removed. If this is not explicitly called it is called automatically on
  80. * destruct.
  81. */
  82. void remove();
  83. inline const InetAddress &target() const { return _target; }
  84. inline const InetAddress &via() const { return _via; }
  85. inline const InetAddress &src() const { return _src; }
  86. inline const char *device() const { return _device; }
  87. private:
  88. ManagedRoute(const ManagedRoute &) {}
  89. inline ManagedRoute &operator=(const ManagedRoute &) { return *this; }
  90. InetAddress _target;
  91. InetAddress _via;
  92. InetAddress _src;
  93. InetAddress _systemVia; // for route overrides
  94. std::map<InetAddress,bool> _applied; // routes currently applied
  95. char _device[128];
  96. char _systemDevice[128]; // for route overrides
  97. AtomicCounter __refCount;
  98. };
  99. } // namespace ZeroTier
  100. #endif