ManagedRoute.hpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
  4. *
  5. * (c) ZeroTier, Inc.
  6. * https://www.zerotier.com/
  7. */
  8. #ifndef ZT_MANAGEDROUTE_HPP
  9. #define ZT_MANAGEDROUTE_HPP
  10. #include "../node/AtomicCounter.hpp"
  11. #include "../node/InetAddress.hpp"
  12. #include "../node/SharedPtr.hpp"
  13. #include "../node/Utils.hpp"
  14. #include <map>
  15. #include <stdexcept>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <vector>
  19. namespace ZeroTier {
  20. /**
  21. * A ZT-managed route that used C++ RAII semantics to automatically clean itself up on deallocate
  22. */
  23. class ManagedRoute {
  24. friend class SharedPtr<ManagedRoute>;
  25. public:
  26. ManagedRoute(const InetAddress& target, const InetAddress& via, const InetAddress& src, const char* device);
  27. ~ManagedRoute();
  28. /**
  29. * Set or update currently set route
  30. *
  31. * This must be called periodically for routes that shadow others so that
  32. * shadow routes can be updated. In some cases it has no effect
  33. *
  34. * @return True if route add/update was successful
  35. */
  36. bool sync();
  37. /**
  38. * Remove and clear this ManagedRoute
  39. *
  40. * This does nothing if this ManagedRoute is not set or has already been
  41. * removed. If this is not explicitly called it is called automatically on
  42. * destruct.
  43. */
  44. void remove();
  45. inline const InetAddress& target() const
  46. {
  47. return _target;
  48. }
  49. inline const InetAddress& via() const
  50. {
  51. return _via;
  52. }
  53. inline const InetAddress& src() const
  54. {
  55. return _src;
  56. }
  57. inline const char* device() const
  58. {
  59. return _device;
  60. }
  61. private:
  62. ManagedRoute(const ManagedRoute&)
  63. {
  64. }
  65. inline ManagedRoute& operator=(const ManagedRoute&)
  66. {
  67. return *this;
  68. }
  69. InetAddress _target;
  70. InetAddress _via;
  71. InetAddress _src;
  72. InetAddress _systemVia; // for route overrides
  73. std::map<InetAddress, bool> _applied; // routes currently applied
  74. char _device[128];
  75. char _systemDevice[128]; // for route overrides
  76. AtomicCounter __refCount;
  77. };
  78. } // namespace ZeroTier
  79. #endif